[INET]: Collect common frag sysctl variables together
[linux-2.6/verdex.git] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
blob0e40948f4fc6516237b90f940f58bb0e1298c5f8
1 /*
2 * Copyright (C)2004 USAGI/WIDE Project
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.
8 * Author:
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <linux/sysctl.h>
20 #include <net/ipv6.h>
21 #include <net/inet_frag.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_core.h>
30 static int ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
31 struct nf_conntrack_tuple *tuple)
33 u_int32_t _addrs[8], *ap;
35 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
36 sizeof(_addrs), _addrs);
37 if (ap == NULL)
38 return 0;
40 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
41 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
43 return 1;
46 static int ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
47 const struct nf_conntrack_tuple *orig)
49 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
50 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
52 return 1;
55 static int ipv6_print_tuple(struct seq_file *s,
56 const struct nf_conntrack_tuple *tuple)
58 return seq_printf(s, "src=" NIP6_FMT " dst=" NIP6_FMT " ",
59 NIP6(*((struct in6_addr *)tuple->src.u3.ip6)),
60 NIP6(*((struct in6_addr *)tuple->dst.u3.ip6)));
63 static int ipv6_print_conntrack(struct seq_file *s,
64 const struct nf_conn *conntrack)
66 return 0;
70 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
72 * This function parses (probably truncated) exthdr set "hdr"
73 * of length "len". "nexthdrp" initially points to some place,
74 * where type of the first header can be found.
76 * It skips all well-known exthdrs, and returns pointer to the start
77 * of unparsable area i.e. the first header with unknown type.
78 * if success, *nexthdr is updated by type/protocol of this header.
80 * NOTES: - it may return pointer pointing beyond end of packet,
81 * if the last recognized header is truncated in the middle.
82 * - if packet is truncated, so that all parsed headers are skipped,
83 * it returns -1.
84 * - if packet is fragmented, return pointer of the fragment header.
85 * - ESP is unparsable for now and considered like
86 * normal payload protocol.
87 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
90 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
91 u8 *nexthdrp, int len)
93 u8 nexthdr = *nexthdrp;
95 while (ipv6_ext_hdr(nexthdr)) {
96 struct ipv6_opt_hdr hdr;
97 int hdrlen;
99 if (len < (int)sizeof(struct ipv6_opt_hdr))
100 return -1;
101 if (nexthdr == NEXTHDR_NONE)
102 break;
103 if (nexthdr == NEXTHDR_FRAGMENT)
104 break;
105 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
106 BUG();
107 if (nexthdr == NEXTHDR_AUTH)
108 hdrlen = (hdr.hdrlen+2)<<2;
109 else
110 hdrlen = ipv6_optlen(&hdr);
112 nexthdr = hdr.nexthdr;
113 len -= hdrlen;
114 start += hdrlen;
117 *nexthdrp = nexthdr;
118 return start;
121 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
122 unsigned int *dataoff, u_int8_t *protonum)
124 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
125 unsigned char pnum;
126 int protoff;
128 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
129 &pnum, sizeof(pnum)) != 0) {
130 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
131 return -NF_ACCEPT;
133 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
135 * (protoff == skb->len) mean that the packet doesn't have no data
136 * except of IPv6 & ext headers. but it's tracked anyway. - YK
138 if ((protoff < 0) || (protoff > skb->len)) {
139 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
140 return -NF_ACCEPT;
143 *dataoff = protoff;
144 *protonum = pnum;
145 return NF_ACCEPT;
148 static unsigned int ipv6_confirm(unsigned int hooknum,
149 struct sk_buff *skb,
150 const struct net_device *in,
151 const struct net_device *out,
152 int (*okfn)(struct sk_buff *))
154 struct nf_conn *ct;
155 struct nf_conn_help *help;
156 struct nf_conntrack_helper *helper;
157 enum ip_conntrack_info ctinfo;
158 unsigned int ret, protoff;
159 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
160 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
163 /* This is where we call the helper: as the packet goes out. */
164 ct = nf_ct_get(skb, &ctinfo);
165 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
166 goto out;
168 help = nfct_help(ct);
169 if (!help)
170 goto out;
171 /* rcu_read_lock()ed by nf_hook_slow */
172 helper = rcu_dereference(help->helper);
173 if (!helper)
174 goto out;
176 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
177 skb->len - extoff);
178 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
179 pr_debug("proto header not found\n");
180 return NF_ACCEPT;
183 ret = helper->help(skb, protoff, ct, ctinfo);
184 if (ret != NF_ACCEPT)
185 return ret;
186 out:
187 /* We've seen it coming out the other side: confirm it */
188 return nf_conntrack_confirm(skb);
191 static unsigned int ipv6_defrag(unsigned int hooknum,
192 struct sk_buff *skb,
193 const struct net_device *in,
194 const struct net_device *out,
195 int (*okfn)(struct sk_buff *))
197 struct sk_buff *reasm;
199 /* Previously seen (loopback)? */
200 if (skb->nfct)
201 return NF_ACCEPT;
203 reasm = nf_ct_frag6_gather(skb);
205 /* queued */
206 if (reasm == NULL)
207 return NF_STOLEN;
209 /* error occured or not fragmented */
210 if (reasm == skb)
211 return NF_ACCEPT;
213 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
214 (struct net_device *)out, okfn);
216 return NF_STOLEN;
219 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
220 struct sk_buff *skb,
221 const struct net_device *in,
222 const struct net_device *out,
223 int (*okfn)(struct sk_buff *))
225 struct sk_buff *reasm = skb->nfct_reasm;
227 /* This packet is fragmented and has reassembled packet. */
228 if (reasm) {
229 /* Reassembled packet isn't parsed yet ? */
230 if (!reasm->nfct) {
231 unsigned int ret;
233 ret = nf_conntrack_in(PF_INET6, hooknum, reasm);
234 if (ret != NF_ACCEPT)
235 return ret;
237 nf_conntrack_get(reasm->nfct);
238 skb->nfct = reasm->nfct;
239 skb->nfctinfo = reasm->nfctinfo;
240 return NF_ACCEPT;
243 return nf_conntrack_in(PF_INET6, hooknum, skb);
246 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
247 struct sk_buff *skb,
248 const struct net_device *in,
249 const struct net_device *out,
250 int (*okfn)(struct sk_buff *))
252 /* root is playing with raw sockets. */
253 if (skb->len < sizeof(struct ipv6hdr)) {
254 if (net_ratelimit())
255 printk("ipv6_conntrack_local: packet too short\n");
256 return NF_ACCEPT;
258 return ipv6_conntrack_in(hooknum, skb, in, out, okfn);
261 static struct nf_hook_ops ipv6_conntrack_ops[] = {
263 .hook = ipv6_defrag,
264 .owner = THIS_MODULE,
265 .pf = PF_INET6,
266 .hooknum = NF_IP6_PRE_ROUTING,
267 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
270 .hook = ipv6_conntrack_in,
271 .owner = THIS_MODULE,
272 .pf = PF_INET6,
273 .hooknum = NF_IP6_PRE_ROUTING,
274 .priority = NF_IP6_PRI_CONNTRACK,
277 .hook = ipv6_conntrack_local,
278 .owner = THIS_MODULE,
279 .pf = PF_INET6,
280 .hooknum = NF_IP6_LOCAL_OUT,
281 .priority = NF_IP6_PRI_CONNTRACK,
284 .hook = ipv6_defrag,
285 .owner = THIS_MODULE,
286 .pf = PF_INET6,
287 .hooknum = NF_IP6_LOCAL_OUT,
288 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
291 .hook = ipv6_confirm,
292 .owner = THIS_MODULE,
293 .pf = PF_INET6,
294 .hooknum = NF_IP6_POST_ROUTING,
295 .priority = NF_IP6_PRI_LAST,
298 .hook = ipv6_confirm,
299 .owner = THIS_MODULE,
300 .pf = PF_INET6,
301 .hooknum = NF_IP6_LOCAL_IN,
302 .priority = NF_IP6_PRI_LAST-1,
306 #ifdef CONFIG_SYSCTL
307 static ctl_table nf_ct_ipv6_sysctl_table[] = {
309 .ctl_name = NET_NF_CONNTRACK_FRAG6_TIMEOUT,
310 .procname = "nf_conntrack_frag6_timeout",
311 .data = &nf_frags_ctl.timeout,
312 .maxlen = sizeof(unsigned int),
313 .mode = 0644,
314 .proc_handler = &proc_dointvec_jiffies,
317 .ctl_name = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
318 .procname = "nf_conntrack_frag6_low_thresh",
319 .data = &nf_frags_ctl.low_thresh,
320 .maxlen = sizeof(unsigned int),
321 .mode = 0644,
322 .proc_handler = &proc_dointvec,
325 .ctl_name = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
326 .procname = "nf_conntrack_frag6_high_thresh",
327 .data = &nf_frags_ctl.high_thresh,
328 .maxlen = sizeof(unsigned int),
329 .mode = 0644,
330 .proc_handler = &proc_dointvec,
332 { .ctl_name = 0 }
334 #endif
336 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
338 #include <linux/netfilter/nfnetlink.h>
339 #include <linux/netfilter/nfnetlink_conntrack.h>
341 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
342 const struct nf_conntrack_tuple *tuple)
344 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
345 &tuple->src.u3.ip6);
346 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
347 &tuple->dst.u3.ip6);
348 return 0;
350 nla_put_failure:
351 return -1;
354 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
355 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
356 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
359 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
360 struct nf_conntrack_tuple *t)
362 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
363 return -EINVAL;
365 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
366 sizeof(u_int32_t) * 4);
367 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
368 sizeof(u_int32_t) * 4);
370 return 0;
372 #endif
374 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
375 .l3proto = PF_INET6,
376 .name = "ipv6",
377 .pkt_to_tuple = ipv6_pkt_to_tuple,
378 .invert_tuple = ipv6_invert_tuple,
379 .print_tuple = ipv6_print_tuple,
380 .print_conntrack = ipv6_print_conntrack,
381 .get_l4proto = ipv6_get_l4proto,
382 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
383 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
384 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
385 .nla_policy = ipv6_nla_policy,
386 #endif
387 #ifdef CONFIG_SYSCTL
388 .ctl_table_path = nf_net_netfilter_sysctl_path,
389 .ctl_table = nf_ct_ipv6_sysctl_table,
390 #endif
391 .me = THIS_MODULE,
394 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
395 MODULE_LICENSE("GPL");
396 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
398 static int __init nf_conntrack_l3proto_ipv6_init(void)
400 int ret = 0;
402 need_conntrack();
404 ret = nf_ct_frag6_init();
405 if (ret < 0) {
406 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
407 return ret;
409 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
410 if (ret < 0) {
411 printk("nf_conntrack_ipv6: can't register tcp.\n");
412 goto cleanup_frag6;
415 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
416 if (ret < 0) {
417 printk("nf_conntrack_ipv6: can't register udp.\n");
418 goto cleanup_tcp;
421 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
422 if (ret < 0) {
423 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
424 goto cleanup_udp;
427 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
428 if (ret < 0) {
429 printk("nf_conntrack_ipv6: can't register ipv6\n");
430 goto cleanup_icmpv6;
433 ret = nf_register_hooks(ipv6_conntrack_ops,
434 ARRAY_SIZE(ipv6_conntrack_ops));
435 if (ret < 0) {
436 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
437 "hook.\n");
438 goto cleanup_ipv6;
440 return ret;
442 cleanup_ipv6:
443 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
444 cleanup_icmpv6:
445 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
446 cleanup_udp:
447 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
448 cleanup_tcp:
449 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
450 cleanup_frag6:
451 nf_ct_frag6_cleanup();
452 return ret;
455 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
457 synchronize_net();
458 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
459 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
460 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
461 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
462 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
463 nf_ct_frag6_cleanup();
466 module_init(nf_conntrack_l3proto_ipv6_init);
467 module_exit(nf_conntrack_l3proto_ipv6_fini);