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