]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
[NETFILTER]: nf_conntrack: remove old memory allocator of conntrack
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
CommitLineData
9fb9cbb1
YK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9fb9cbb1
YK
7 */
8
9fb9cbb1
YK
9#include <linux/types.h>
10#include <linux/ip.h>
11#include <linux/netfilter.h>
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/icmp.h>
15#include <linux/sysctl.h>
0ae2cfe7 16#include <net/route.h>
9fb9cbb1
YK
17#include <net/ip.h>
18
19#include <linux/netfilter_ipv4.h>
20#include <net/netfilter/nf_conntrack.h>
21#include <net/netfilter/nf_conntrack_helper.h>
605dcad6 22#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1
YK
23#include <net/netfilter/nf_conntrack_l3proto.h>
24#include <net/netfilter/nf_conntrack_core.h>
25#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26
27#if 0
28#define DEBUGP printk
29#else
30#define DEBUGP(format, args...)
31#endif
32
9fb9cbb1
YK
33static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
35{
bff9a89b 36 __be32 _addrs[2], *ap;
9fb9cbb1
YK
37 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
38 sizeof(u_int32_t) * 2, _addrs);
39 if (ap == NULL)
40 return 0;
41
42 tuple->src.u3.ip = ap[0];
43 tuple->dst.u3.ip = ap[1];
44
45 return 1;
46}
47
48static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
49 const struct nf_conntrack_tuple *orig)
50{
51 tuple->src.u3.ip = orig->dst.u3.ip;
52 tuple->dst.u3.ip = orig->src.u3.ip;
53
54 return 1;
55}
56
57static int ipv4_print_tuple(struct seq_file *s,
58 const struct nf_conntrack_tuple *tuple)
59{
60 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
e905a9ed 61 NIPQUAD(tuple->src.u3.ip),
9fb9cbb1
YK
62 NIPQUAD(tuple->dst.u3.ip));
63}
64
65static int ipv4_print_conntrack(struct seq_file *s,
66 const struct nf_conn *conntrack)
67{
68 return 0;
69}
70
71/* Returns new sk_buff, or NULL */
72static struct sk_buff *
73nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
74{
75 skb_orphan(skb);
76
e905a9ed
YH
77 local_bh_disable();
78 skb = ip_defrag(skb, user);
79 local_bh_enable();
9fb9cbb1 80
e905a9ed 81 if (skb)
eddc9ec5 82 ip_send_check(ip_hdr(skb));
9fb9cbb1 83
e905a9ed 84 return skb;
9fb9cbb1
YK
85}
86
87static int
88ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
89 u_int8_t *protonum)
90{
91 /* Never happen */
eddc9ec5 92 if (ip_hdr(*pskb)->frag_off & htons(IP_OFFSET)) {
9fb9cbb1
YK
93 if (net_ratelimit()) {
94 printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
eddc9ec5 95 ip_hdr(*pskb)->protocol, hooknum);
9fb9cbb1
YK
96 }
97 return -NF_DROP;
98 }
99
c9bdd4b5 100 *dataoff = skb_network_offset(*pskb) + ip_hdrlen(*pskb);
eddc9ec5 101 *protonum = ip_hdr(*pskb)->protocol;
9fb9cbb1
YK
102
103 return NF_ACCEPT;
104}
105
9fb9cbb1
YK
106static unsigned int ipv4_confirm(unsigned int hooknum,
107 struct sk_buff **pskb,
108 const struct net_device *in,
109 const struct net_device *out,
110 int (*okfn)(struct sk_buff *))
111{
112 /* We've seen it coming out the other side: confirm it */
113 return nf_conntrack_confirm(pskb);
114}
115
116static unsigned int ipv4_conntrack_help(unsigned int hooknum,
117 struct sk_buff **pskb,
118 const struct net_device *in,
119 const struct net_device *out,
120 int (*okfn)(struct sk_buff *))
121{
122 struct nf_conn *ct;
123 enum ip_conntrack_info ctinfo;
dc808fe2 124 struct nf_conn_help *help;
3c158f7f 125 struct nf_conntrack_helper *helper;
9fb9cbb1
YK
126
127 /* This is where we call the helper: as the packet goes out. */
128 ct = nf_ct_get(*pskb, &ctinfo);
6442f1cf 129 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
dc808fe2
HW
130 return NF_ACCEPT;
131
132 help = nfct_help(ct);
3c158f7f 133 if (!help)
dc808fe2 134 return NF_ACCEPT;
3c158f7f
PM
135 /* rcu_read_lock()ed by nf_hook_slow */
136 helper = rcu_dereference(help->helper);
137 if (!helper)
138 return NF_ACCEPT;
139 return helper->help(pskb, skb_network_offset(*pskb) + ip_hdrlen(*pskb),
140 ct, ctinfo);
9fb9cbb1
YK
141}
142
143static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
144 struct sk_buff **pskb,
145 const struct net_device *in,
146 const struct net_device *out,
147 int (*okfn)(struct sk_buff *))
148{
9fb9cbb1
YK
149 /* Previously seen (loopback)? Ignore. Do this before
150 fragment check. */
151 if ((*pskb)->nfct)
152 return NF_ACCEPT;
9fb9cbb1
YK
153
154 /* Gather fragments. */
eddc9ec5 155 if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
9fb9cbb1
YK
156 *pskb = nf_ct_ipv4_gather_frags(*pskb,
157 hooknum == NF_IP_PRE_ROUTING ?
158 IP_DEFRAG_CONNTRACK_IN :
159 IP_DEFRAG_CONNTRACK_OUT);
160 if (!*pskb)
161 return NF_STOLEN;
162 }
163 return NF_ACCEPT;
164}
165
9fb9cbb1
YK
166static unsigned int ipv4_conntrack_in(unsigned int hooknum,
167 struct sk_buff **pskb,
168 const struct net_device *in,
169 const struct net_device *out,
170 int (*okfn)(struct sk_buff *))
171{
172 return nf_conntrack_in(PF_INET, hooknum, pskb);
173}
174
175static unsigned int ipv4_conntrack_local(unsigned int hooknum,
e905a9ed
YH
176 struct sk_buff **pskb,
177 const struct net_device *in,
178 const struct net_device *out,
179 int (*okfn)(struct sk_buff *))
9fb9cbb1
YK
180{
181 /* root is playing with raw sockets. */
182 if ((*pskb)->len < sizeof(struct iphdr)
c9bdd4b5 183 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
9fb9cbb1
YK
184 if (net_ratelimit())
185 printk("ipt_hook: happy cracking.\n");
186 return NF_ACCEPT;
187 }
188 return nf_conntrack_in(PF_INET, hooknum, pskb);
189}
190
191/* Connection tracking may drop packets, but never alters them, so
192 make it the first hook. */
964ddaa1
PM
193static struct nf_hook_ops ipv4_conntrack_ops[] = {
194 {
195 .hook = ipv4_conntrack_defrag,
196 .owner = THIS_MODULE,
197 .pf = PF_INET,
198 .hooknum = NF_IP_PRE_ROUTING,
199 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
200 },
201 {
202 .hook = ipv4_conntrack_in,
203 .owner = THIS_MODULE,
204 .pf = PF_INET,
205 .hooknum = NF_IP_PRE_ROUTING,
206 .priority = NF_IP_PRI_CONNTRACK,
207 },
208 {
209 .hook = ipv4_conntrack_defrag,
210 .owner = THIS_MODULE,
211 .pf = PF_INET,
212 .hooknum = NF_IP_LOCAL_OUT,
213 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
214 },
215 {
216 .hook = ipv4_conntrack_local,
217 .owner = THIS_MODULE,
218 .pf = PF_INET,
219 .hooknum = NF_IP_LOCAL_OUT,
220 .priority = NF_IP_PRI_CONNTRACK,
221 },
222 {
223 .hook = ipv4_conntrack_help,
224 .owner = THIS_MODULE,
225 .pf = PF_INET,
226 .hooknum = NF_IP_POST_ROUTING,
227 .priority = NF_IP_PRI_CONNTRACK_HELPER,
228 },
229 {
230 .hook = ipv4_conntrack_help,
231 .owner = THIS_MODULE,
232 .pf = PF_INET,
233 .hooknum = NF_IP_LOCAL_IN,
234 .priority = NF_IP_PRI_CONNTRACK_HELPER,
235 },
236 {
237 .hook = ipv4_confirm,
238 .owner = THIS_MODULE,
239 .pf = PF_INET,
240 .hooknum = NF_IP_POST_ROUTING,
241 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
242 },
243 {
244 .hook = ipv4_confirm,
245 .owner = THIS_MODULE,
246 .pf = PF_INET,
247 .hooknum = NF_IP_LOCAL_IN,
248 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
249 },
9fb9cbb1
YK
250};
251
a999e683
PM
252#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
253static int log_invalid_proto_min = 0;
254static int log_invalid_proto_max = 255;
255
256static ctl_table ip_ct_sysctl_table[] = {
257 {
258 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
259 .procname = "ip_conntrack_max",
260 .data = &nf_conntrack_max,
261 .maxlen = sizeof(int),
262 .mode = 0644,
263 .proc_handler = &proc_dointvec,
264 },
265 {
266 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
267 .procname = "ip_conntrack_count",
268 .data = &nf_conntrack_count,
269 .maxlen = sizeof(int),
270 .mode = 0444,
271 .proc_handler = &proc_dointvec,
272 },
273 {
274 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
275 .procname = "ip_conntrack_buckets",
276 .data = &nf_conntrack_htable_size,
277 .maxlen = sizeof(unsigned int),
278 .mode = 0444,
279 .proc_handler = &proc_dointvec,
280 },
281 {
282 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
283 .procname = "ip_conntrack_checksum",
284 .data = &nf_conntrack_checksum,
285 .maxlen = sizeof(int),
286 .mode = 0644,
287 .proc_handler = &proc_dointvec,
288 },
289 {
290 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
291 .procname = "ip_conntrack_log_invalid",
292 .data = &nf_ct_log_invalid,
293 .maxlen = sizeof(unsigned int),
294 .mode = 0644,
295 .proc_handler = &proc_dointvec_minmax,
296 .strategy = &sysctl_intvec,
297 .extra1 = &log_invalid_proto_min,
298 .extra2 = &log_invalid_proto_max,
299 },
300 {
301 .ctl_name = 0
302 }
303};
304#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
305
9fb9cbb1
YK
306/* Fast function for those who don't want to parse /proc (and I don't
307 blame them). */
308/* Reversing the socket's dst/src point of view gives us the reply
309 mapping. */
310static int
311getorigdst(struct sock *sk, int optval, void __user *user, int *len)
312{
313 struct inet_sock *inet = inet_sk(sk);
314 struct nf_conntrack_tuple_hash *h;
315 struct nf_conntrack_tuple tuple;
e905a9ed 316
9fb9cbb1
YK
317 NF_CT_TUPLE_U_BLANK(&tuple);
318 tuple.src.u3.ip = inet->rcv_saddr;
319 tuple.src.u.tcp.port = inet->sport;
320 tuple.dst.u3.ip = inet->daddr;
321 tuple.dst.u.tcp.port = inet->dport;
322 tuple.src.l3num = PF_INET;
323 tuple.dst.protonum = IPPROTO_TCP;
324
325 /* We only do TCP at the moment: is there a better way? */
326 if (strcmp(sk->sk_prot->name, "TCP")) {
327 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
328 return -ENOPROTOOPT;
329 }
330
331 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
332 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
333 *len, sizeof(struct sockaddr_in));
334 return -EINVAL;
335 }
336
337 h = nf_conntrack_find_get(&tuple, NULL);
338 if (h) {
339 struct sockaddr_in sin;
340 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
341
342 sin.sin_family = AF_INET;
343 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
344 .tuple.dst.u.tcp.port;
345 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
346 .tuple.dst.u3.ip;
6c813c3f 347 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
9fb9cbb1
YK
348
349 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
350 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
351 nf_ct_put(ct);
352 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
353 return -EFAULT;
354 else
355 return 0;
356 }
357 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
358 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
359 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
360 return -ENOENT;
361}
362
e281db5c 363#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
364
365#include <linux/netfilter/nfnetlink.h>
366#include <linux/netfilter/nfnetlink_conntrack.h>
367
368static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
369 const struct nf_conntrack_tuple *tuple)
370{
371 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
372 &tuple->src.u3.ip);
373 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
374 &tuple->dst.u3.ip);
375 return 0;
376
377nfattr_failure:
378 return -1;
379}
380
381static const size_t cta_min_ip[CTA_IP_MAX] = {
382 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
383 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
384};
385
386static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
387 struct nf_conntrack_tuple *t)
388{
389 if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
390 return -EINVAL;
391
392 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
393 return -EINVAL;
394
bff9a89b
PM
395 t->src.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
396 t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
c1d10adb
PNA
397
398 return 0;
399}
400#endif
401
9fb9cbb1
YK
402static struct nf_sockopt_ops so_getorigdst = {
403 .pf = PF_INET,
404 .get_optmin = SO_ORIGINAL_DST,
405 .get_optmax = SO_ORIGINAL_DST+1,
406 .get = &getorigdst,
407};
408
409struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
410 .l3proto = PF_INET,
411 .name = "ipv4",
412 .pkt_to_tuple = ipv4_pkt_to_tuple,
413 .invert_tuple = ipv4_invert_tuple,
414 .print_tuple = ipv4_print_tuple,
415 .print_conntrack = ipv4_print_conntrack,
416 .prepare = ipv4_prepare,
e281db5c 417#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
418 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
419 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
a999e683
PM
420#endif
421#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
422 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
423 .ctl_table = ip_ct_sysctl_table,
c1d10adb 424#endif
9fb9cbb1
YK
425 .me = THIS_MODULE,
426};
427
32292a7f 428MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
d2483dde 429MODULE_ALIAS("ip_conntrack");
32292a7f
PM
430MODULE_LICENSE("GPL");
431
432static int __init nf_conntrack_l3proto_ipv4_init(void)
9fb9cbb1
YK
433{
434 int ret = 0;
435
32292a7f 436 need_conntrack();
9fb9cbb1
YK
437
438 ret = nf_register_sockopt(&so_getorigdst);
439 if (ret < 0) {
440 printk(KERN_ERR "Unable to register netfilter socket option\n");
32292a7f 441 return ret;
9fb9cbb1
YK
442 }
443
605dcad6 444 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
9fb9cbb1
YK
445 if (ret < 0) {
446 printk("nf_conntrack_ipv4: can't register tcp.\n");
447 goto cleanup_sockopt;
448 }
449
605dcad6 450 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
9fb9cbb1
YK
451 if (ret < 0) {
452 printk("nf_conntrack_ipv4: can't register udp.\n");
453 goto cleanup_tcp;
454 }
455
605dcad6 456 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
9fb9cbb1
YK
457 if (ret < 0) {
458 printk("nf_conntrack_ipv4: can't register icmp.\n");
459 goto cleanup_udp;
460 }
461
462 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
463 if (ret < 0) {
464 printk("nf_conntrack_ipv4: can't register ipv4\n");
465 goto cleanup_icmp;
466 }
467
964ddaa1
PM
468 ret = nf_register_hooks(ipv4_conntrack_ops,
469 ARRAY_SIZE(ipv4_conntrack_ops));
9fb9cbb1 470 if (ret < 0) {
964ddaa1 471 printk("nf_conntrack_ipv4: can't register hooks.\n");
9fb9cbb1
YK
472 goto cleanup_ipv4;
473 }
e4bd8bce
PM
474#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
475 ret = nf_conntrack_ipv4_compat_init();
476 if (ret < 0)
477 goto cleanup_hooks;
478#endif
9fb9cbb1 479 return ret;
e4bd8bce
PM
480#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
481 cleanup_hooks:
e905a9ed 482 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
e4bd8bce 483#endif
9fb9cbb1
YK
484 cleanup_ipv4:
485 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
486 cleanup_icmp:
605dcad6 487 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
9fb9cbb1 488 cleanup_udp:
605dcad6 489 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
9fb9cbb1 490 cleanup_tcp:
605dcad6 491 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
9fb9cbb1
YK
492 cleanup_sockopt:
493 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
494 return ret;
495}
496
65b4b4e8 497static void __exit nf_conntrack_l3proto_ipv4_fini(void)
9fb9cbb1 498{
32292a7f 499 synchronize_net();
e4bd8bce
PM
500#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
501 nf_conntrack_ipv4_compat_fini();
502#endif
32292a7f
PM
503 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
504 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
605dcad6
MJ
505 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
506 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
507 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
32292a7f 508 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
509}
510
65b4b4e8
AM
511module_init(nf_conntrack_l3proto_ipv4_init);
512module_exit(nf_conntrack_l3proto_ipv4_fini);