Merge branches 'topic/asoc', 'topic/misc-fixes', 'topic/ps3-csbits' and 'topic/stagin...
[linux-2.6-block.git] / net / netfilter / nf_conntrack_sane.c
CommitLineData
6fecd198
MS
1/* SANE connection tracking helper
2 * (SANE = Scanner Access Now Easy)
3 * For documentation about the SANE network protocol see
4 * http://www.sane-project.org/html/doc015.html
5 */
6
7/* Copyright (C) 2007 Red Hat, Inc.
8 * Author: Michal Schmidt <mschmidt@redhat.com>
9 * Based on the FTP conntrack helper (net/netfilter/nf_conntrack_ftp.c):
10 * (C) 1999-2001 Paul `Rusty' Russell
11 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
12 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
13 * (C) 2003 Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 */
19
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/netfilter.h>
23#include <linux/in.h>
24#include <linux/tcp.h>
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_helper.h>
27#include <net/netfilter/nf_conntrack_expect.h>
28#include <linux/netfilter/nf_conntrack_sane.h>
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
32MODULE_DESCRIPTION("SANE connection tracking helper");
33
34static char *sane_buffer;
35
36static DEFINE_SPINLOCK(nf_sane_lock);
37
38#define MAX_PORTS 8
39static u_int16_t ports[MAX_PORTS];
40static unsigned int ports_c;
41module_param_array(ports, ushort, &ports_c, 0400);
42
6fecd198
MS
43struct sane_request {
44 __be32 RPC_code;
45#define SANE_NET_START 7 /* RPC code */
46
47 __be32 handle;
48};
49
50struct sane_reply_net_start {
51 __be32 status;
52#define SANE_STATUS_SUCCESS 0
53
54 __be16 zero;
55 __be16 port;
56 /* other fields aren't interesting for conntrack */
57};
58
3db05fea 59static int help(struct sk_buff *skb,
6fecd198
MS
60 unsigned int protoff,
61 struct nf_conn *ct,
62 enum ip_conntrack_info ctinfo)
63{
64 unsigned int dataoff, datalen;
02e23f40
JE
65 const struct tcphdr *th;
66 struct tcphdr _tcph;
67 void *sb_ptr;
6fecd198
MS
68 int ret = NF_ACCEPT;
69 int dir = CTINFO2DIR(ctinfo);
70 struct nf_ct_sane_master *ct_sane_info;
71 struct nf_conntrack_expect *exp;
72 struct nf_conntrack_tuple *tuple;
73 struct sane_request *req;
74 struct sane_reply_net_start *reply;
6fecd198
MS
75
76 ct_sane_info = &nfct_help(ct)->help.ct_sane_info;
77 /* Until there's been traffic both ways, don't look in packets. */
78 if (ctinfo != IP_CT_ESTABLISHED &&
79 ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY)
80 return NF_ACCEPT;
81
82 /* Not a full tcp header? */
3db05fea 83 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
6fecd198
MS
84 if (th == NULL)
85 return NF_ACCEPT;
86
87 /* No data? */
88 dataoff = protoff + th->doff * 4;
3db05fea 89 if (dataoff >= skb->len)
6fecd198
MS
90 return NF_ACCEPT;
91
3db05fea 92 datalen = skb->len - dataoff;
6fecd198
MS
93
94 spin_lock_bh(&nf_sane_lock);
3db05fea 95 sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
6fecd198
MS
96 BUG_ON(sb_ptr == NULL);
97
98 if (dir == IP_CT_DIR_ORIGINAL) {
99 if (datalen != sizeof(struct sane_request))
100 goto out;
101
02e23f40 102 req = sb_ptr;
6fecd198
MS
103 if (req->RPC_code != htonl(SANE_NET_START)) {
104 /* Not an interesting command */
105 ct_sane_info->state = SANE_STATE_NORMAL;
106 goto out;
107 }
108
109 /* We're interested in the next reply */
110 ct_sane_info->state = SANE_STATE_START_REQUESTED;
111 goto out;
112 }
113
114 /* Is it a reply to an uninteresting command? */
115 if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
116 goto out;
117
118 /* It's a reply to SANE_NET_START. */
119 ct_sane_info->state = SANE_STATE_NORMAL;
120
121 if (datalen < sizeof(struct sane_reply_net_start)) {
0d53778e 122 pr_debug("nf_ct_sane: NET_START reply too short\n");
6fecd198
MS
123 goto out;
124 }
125
02e23f40 126 reply = sb_ptr;
6fecd198
MS
127 if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
128 /* saned refused the command */
0d53778e
PM
129 pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
130 ntohl(reply->status));
6fecd198
MS
131 goto out;
132 }
133
134 /* Invalid saned reply? Ignore it. */
135 if (reply->zero != 0)
136 goto out;
137
6823645d 138 exp = nf_ct_expect_alloc(ct);
6fecd198
MS
139 if (exp == NULL) {
140 ret = NF_DROP;
141 goto out;
142 }
143
144 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
5e8fbe2a 145 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
6002f266 146 &tuple->src.u3, &tuple->dst.u3,
6823645d 147 IPPROTO_TCP, NULL, &reply->port);
6fecd198 148
0d53778e 149 pr_debug("nf_ct_sane: expect: ");
3c9fba65 150 nf_ct_dump_tuple(&exp->tuple);
6fecd198
MS
151
152 /* Can't expect this? Best to drop packet now. */
6823645d 153 if (nf_ct_expect_related(exp) != 0)
6fecd198
MS
154 ret = NF_DROP;
155
6823645d 156 nf_ct_expect_put(exp);
6fecd198
MS
157
158out:
159 spin_unlock_bh(&nf_sane_lock);
160 return ret;
161}
162
ec59a111
PM
163static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
164static char sane_names[MAX_PORTS][2][sizeof("sane-65535")] __read_mostly;
6fecd198 165
6002f266
PM
166static const struct nf_conntrack_expect_policy sane_exp_policy = {
167 .max_expected = 1,
168 .timeout = 5 * 60,
169};
170
6fecd198
MS
171/* don't make this __exit, since it's called from __init ! */
172static void nf_conntrack_sane_fini(void)
173{
174 int i, j;
175
176 for (i = 0; i < ports_c; i++) {
177 for (j = 0; j < 2; j++) {
0d53778e
PM
178 pr_debug("nf_ct_sane: unregistering helper for pf: %d "
179 "port: %d\n",
180 sane[i][j].tuple.src.l3num, ports[i]);
6fecd198
MS
181 nf_conntrack_helper_unregister(&sane[i][j]);
182 }
183 }
184
185 kfree(sane_buffer);
186}
187
188static int __init nf_conntrack_sane_init(void)
189{
190 int i, j = -1, ret = 0;
191 char *tmpname;
192
193 sane_buffer = kmalloc(65536, GFP_KERNEL);
194 if (!sane_buffer)
195 return -ENOMEM;
196
197 if (ports_c == 0)
198 ports[ports_c++] = SANE_PORT;
199
200 /* FIXME should be configurable whether IPv4 and IPv6 connections
201 are tracked or not - YK */
202 for (i = 0; i < ports_c; i++) {
203 sane[i][0].tuple.src.l3num = PF_INET;
204 sane[i][1].tuple.src.l3num = PF_INET6;
205 for (j = 0; j < 2; j++) {
206 sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
207 sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
6002f266 208 sane[i][j].expect_policy = &sane_exp_policy;
6fecd198
MS
209 sane[i][j].me = THIS_MODULE;
210 sane[i][j].help = help;
211 tmpname = &sane_names[i][j][0];
212 if (ports[i] == SANE_PORT)
213 sprintf(tmpname, "sane");
214 else
215 sprintf(tmpname, "sane-%d", ports[i]);
216 sane[i][j].name = tmpname;
217
0d53778e
PM
218 pr_debug("nf_ct_sane: registering helper for pf: %d "
219 "port: %d\n",
220 sane[i][j].tuple.src.l3num, ports[i]);
6fecd198
MS
221 ret = nf_conntrack_helper_register(&sane[i][j]);
222 if (ret) {
223 printk(KERN_ERR "nf_ct_sane: failed to "
224 "register helper for pf: %d port: %d\n",
225 sane[i][j].tuple.src.l3num, ports[i]);
226 nf_conntrack_sane_fini();
227 return ret;
228 }
229 }
230 }
231
232 return 0;
233}
234
235module_init(nf_conntrack_sane_init);
236module_exit(nf_conntrack_sane_fini);