[NETFILTER]: nf_conntrack: remove old memory allocator of conntrack
[deliverable/linux.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
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.
7 */
8
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>
16 #include <net/route.h>
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>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
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
33 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
35 {
36 __be32 _addrs[2], *ap;
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
48 static 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
57 static 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 ",
61 NIPQUAD(tuple->src.u3.ip),
62 NIPQUAD(tuple->dst.u3.ip));
63 }
64
65 static 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 */
72 static struct sk_buff *
73 nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
74 {
75 skb_orphan(skb);
76
77 local_bh_disable();
78 skb = ip_defrag(skb, user);
79 local_bh_enable();
80
81 if (skb)
82 ip_send_check(ip_hdr(skb));
83
84 return skb;
85 }
86
87 static int
88 ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
89 u_int8_t *protonum)
90 {
91 /* Never happen */
92 if (ip_hdr(*pskb)->frag_off & htons(IP_OFFSET)) {
93 if (net_ratelimit()) {
94 printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
95 ip_hdr(*pskb)->protocol, hooknum);
96 }
97 return -NF_DROP;
98 }
99
100 *dataoff = skb_network_offset(*pskb) + ip_hdrlen(*pskb);
101 *protonum = ip_hdr(*pskb)->protocol;
102
103 return NF_ACCEPT;
104 }
105
106 static 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
116 static 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;
124 struct nf_conn_help *help;
125 struct nf_conntrack_helper *helper;
126
127 /* This is where we call the helper: as the packet goes out. */
128 ct = nf_ct_get(*pskb, &ctinfo);
129 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
130 return NF_ACCEPT;
131
132 help = nfct_help(ct);
133 if (!help)
134 return NF_ACCEPT;
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);
141 }
142
143 static 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 {
149 /* Previously seen (loopback)? Ignore. Do this before
150 fragment check. */
151 if ((*pskb)->nfct)
152 return NF_ACCEPT;
153
154 /* Gather fragments. */
155 if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
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
166 static 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
175 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
176 struct sk_buff **pskb,
177 const struct net_device *in,
178 const struct net_device *out,
179 int (*okfn)(struct sk_buff *))
180 {
181 /* root is playing with raw sockets. */
182 if ((*pskb)->len < sizeof(struct iphdr)
183 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
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. */
193 static 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 },
250 };
251
252 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
253 static int log_invalid_proto_min = 0;
254 static int log_invalid_proto_max = 255;
255
256 static 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
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. */
310 static int
311 getorigdst(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;
316
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;
347 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
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
363 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
364
365 #include <linux/netfilter/nfnetlink.h>
366 #include <linux/netfilter/nfnetlink_conntrack.h>
367
368 static 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
377 nfattr_failure:
378 return -1;
379 }
380
381 static 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
386 static 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
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]);
397
398 return 0;
399 }
400 #endif
401
402 static 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
409 struct 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,
417 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
418 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
419 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
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,
424 #endif
425 .me = THIS_MODULE,
426 };
427
428 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
429 MODULE_ALIAS("ip_conntrack");
430 MODULE_LICENSE("GPL");
431
432 static int __init nf_conntrack_l3proto_ipv4_init(void)
433 {
434 int ret = 0;
435
436 need_conntrack();
437
438 ret = nf_register_sockopt(&so_getorigdst);
439 if (ret < 0) {
440 printk(KERN_ERR "Unable to register netfilter socket option\n");
441 return ret;
442 }
443
444 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
445 if (ret < 0) {
446 printk("nf_conntrack_ipv4: can't register tcp.\n");
447 goto cleanup_sockopt;
448 }
449
450 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
451 if (ret < 0) {
452 printk("nf_conntrack_ipv4: can't register udp.\n");
453 goto cleanup_tcp;
454 }
455
456 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
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
468 ret = nf_register_hooks(ipv4_conntrack_ops,
469 ARRAY_SIZE(ipv4_conntrack_ops));
470 if (ret < 0) {
471 printk("nf_conntrack_ipv4: can't register hooks.\n");
472 goto cleanup_ipv4;
473 }
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
479 return ret;
480 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
481 cleanup_hooks:
482 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
483 #endif
484 cleanup_ipv4:
485 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
486 cleanup_icmp:
487 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
488 cleanup_udp:
489 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
490 cleanup_tcp:
491 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
492 cleanup_sockopt:
493 nf_unregister_sockopt(&so_getorigdst);
494 return ret;
495 }
496
497 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
498 {
499 synchronize_net();
500 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
501 nf_conntrack_ipv4_compat_fini();
502 #endif
503 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
504 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
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);
508 nf_unregister_sockopt(&so_getorigdst);
509 }
510
511 module_init(nf_conntrack_l3proto_ipv4_init);
512 module_exit(nf_conntrack_l3proto_ipv4_fini);
This page took 0.042593 seconds and 6 git commands to generate.