Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-microblaze.git] / net / netfilter / nf_conntrack_sane.c
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/netfilter.h>
25 #include <linux/slab.h>
26 #include <linux/in.h>
27 #include <linux/tcp.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_expect.h>
31 #include <linux/netfilter/nf_conntrack_sane.h>
32
33 #define HELPER_NAME "sane"
34
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
37 MODULE_DESCRIPTION("SANE connection tracking helper");
38 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
39
40 static char *sane_buffer;
41
42 static DEFINE_SPINLOCK(nf_sane_lock);
43
44 #define MAX_PORTS 8
45 static u_int16_t ports[MAX_PORTS];
46 static unsigned int ports_c;
47 module_param_array(ports, ushort, &ports_c, 0400);
48
49 struct sane_request {
50         __be32 RPC_code;
51 #define SANE_NET_START      7   /* RPC code */
52
53         __be32 handle;
54 };
55
56 struct sane_reply_net_start {
57         __be32 status;
58 #define SANE_STATUS_SUCCESS 0
59
60         __be16 zero;
61         __be16 port;
62         /* other fields aren't interesting for conntrack */
63 };
64
65 static int help(struct sk_buff *skb,
66                 unsigned int protoff,
67                 struct nf_conn *ct,
68                 enum ip_conntrack_info ctinfo)
69 {
70         unsigned int dataoff, datalen;
71         const struct tcphdr *th;
72         struct tcphdr _tcph;
73         void *sb_ptr;
74         int ret = NF_ACCEPT;
75         int dir = CTINFO2DIR(ctinfo);
76         struct nf_ct_sane_master *ct_sane_info = nfct_help_data(ct);
77         struct nf_conntrack_expect *exp;
78         struct nf_conntrack_tuple *tuple;
79         struct sane_request *req;
80         struct sane_reply_net_start *reply;
81
82         /* Until there's been traffic both ways, don't look in packets. */
83         if (ctinfo != IP_CT_ESTABLISHED &&
84             ctinfo != IP_CT_ESTABLISHED_REPLY)
85                 return NF_ACCEPT;
86
87         /* Not a full tcp header? */
88         th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
89         if (th == NULL)
90                 return NF_ACCEPT;
91
92         /* No data? */
93         dataoff = protoff + th->doff * 4;
94         if (dataoff >= skb->len)
95                 return NF_ACCEPT;
96
97         datalen = skb->len - dataoff;
98
99         spin_lock_bh(&nf_sane_lock);
100         sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
101         BUG_ON(sb_ptr == NULL);
102
103         if (dir == IP_CT_DIR_ORIGINAL) {
104                 if (datalen != sizeof(struct sane_request))
105                         goto out;
106
107                 req = sb_ptr;
108                 if (req->RPC_code != htonl(SANE_NET_START)) {
109                         /* Not an interesting command */
110                         ct_sane_info->state = SANE_STATE_NORMAL;
111                         goto out;
112                 }
113
114                 /* We're interested in the next reply */
115                 ct_sane_info->state = SANE_STATE_START_REQUESTED;
116                 goto out;
117         }
118
119         /* Is it a reply to an uninteresting command? */
120         if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
121                 goto out;
122
123         /* It's a reply to SANE_NET_START. */
124         ct_sane_info->state = SANE_STATE_NORMAL;
125
126         if (datalen < sizeof(struct sane_reply_net_start)) {
127                 pr_debug("NET_START reply too short\n");
128                 goto out;
129         }
130
131         reply = sb_ptr;
132         if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
133                 /* saned refused the command */
134                 pr_debug("unsuccessful SANE_STATUS = %u\n",
135                          ntohl(reply->status));
136                 goto out;
137         }
138
139         /* Invalid saned reply? Ignore it. */
140         if (reply->zero != 0)
141                 goto out;
142
143         exp = nf_ct_expect_alloc(ct);
144         if (exp == NULL) {
145                 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
146                 ret = NF_DROP;
147                 goto out;
148         }
149
150         tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
151         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
152                           &tuple->src.u3, &tuple->dst.u3,
153                           IPPROTO_TCP, NULL, &reply->port);
154
155         pr_debug("expect: ");
156         nf_ct_dump_tuple(&exp->tuple);
157
158         /* Can't expect this?  Best to drop packet now. */
159         if (nf_ct_expect_related(exp) != 0) {
160                 nf_ct_helper_log(skb, ct, "cannot add expectation");
161                 ret = NF_DROP;
162         }
163
164         nf_ct_expect_put(exp);
165
166 out:
167         spin_unlock_bh(&nf_sane_lock);
168         return ret;
169 }
170
171 static struct nf_conntrack_helper sane[MAX_PORTS * 2] __read_mostly;
172
173 static const struct nf_conntrack_expect_policy sane_exp_policy = {
174         .max_expected   = 1,
175         .timeout        = 5 * 60,
176 };
177
178 static void __exit nf_conntrack_sane_fini(void)
179 {
180         nf_conntrack_helpers_unregister(sane, ports_c * 2);
181         kfree(sane_buffer);
182 }
183
184 static int __init nf_conntrack_sane_init(void)
185 {
186         int i, ret = 0;
187
188         NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sane_master));
189
190         sane_buffer = kmalloc(65536, GFP_KERNEL);
191         if (!sane_buffer)
192                 return -ENOMEM;
193
194         if (ports_c == 0)
195                 ports[ports_c++] = SANE_PORT;
196
197         /* FIXME should be configurable whether IPv4 and IPv6 connections
198                  are tracked or not - YK */
199         for (i = 0; i < ports_c; i++) {
200                 nf_ct_helper_init(&sane[2 * i], AF_INET, IPPROTO_TCP,
201                                   HELPER_NAME, SANE_PORT, ports[i], ports[i],
202                                   &sane_exp_policy, 0, help, NULL,
203                                   THIS_MODULE);
204                 nf_ct_helper_init(&sane[2 * i + 1], AF_INET6, IPPROTO_TCP,
205                                   HELPER_NAME, SANE_PORT, ports[i], ports[i],
206                                   &sane_exp_policy, 0, help, NULL,
207                                   THIS_MODULE);
208         }
209
210         ret = nf_conntrack_helpers_register(sane, ports_c * 2);
211         if (ret < 0) {
212                 pr_err("failed to register helpers\n");
213                 kfree(sane_buffer);
214                 return ret;
215         }
216
217         return 0;
218 }
219
220 module_init(nf_conntrack_sane_init);
221 module_exit(nf_conntrack_sane_fini);