[CIFS] remove checkpatch warning
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
blob2a15c2d66c69dc23ff63f7ba81ebacf337134a84
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>
29 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
31 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
32 struct nf_conntrack_tuple *tuple)
34 const u_int32_t *ap;
35 u_int32_t _addrs[8];
37 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
38 sizeof(_addrs), _addrs);
39 if (ap == NULL)
40 return false;
42 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
43 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
45 return true;
48 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
49 const struct nf_conntrack_tuple *orig)
51 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
52 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
54 return true;
57 static int ipv6_print_tuple(struct seq_file *s,
58 const struct nf_conntrack_tuple *tuple)
60 return seq_printf(s, "src=%pI6 dst=%pI6 ",
61 tuple->src.u3.ip6, tuple->dst.u3.ip6);
65 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
67 * This function parses (probably truncated) exthdr set "hdr"
68 * of length "len". "nexthdrp" initially points to some place,
69 * where type of the first header can be found.
71 * It skips all well-known exthdrs, and returns pointer to the start
72 * of unparsable area i.e. the first header with unknown type.
73 * if success, *nexthdr is updated by type/protocol of this header.
75 * NOTES: - it may return pointer pointing beyond end of packet,
76 * if the last recognized header is truncated in the middle.
77 * - if packet is truncated, so that all parsed headers are skipped,
78 * it returns -1.
79 * - if packet is fragmented, return pointer of the fragment header.
80 * - ESP is unparsable for now and considered like
81 * normal payload protocol.
82 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
85 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
86 u8 *nexthdrp, int len)
88 u8 nexthdr = *nexthdrp;
90 while (ipv6_ext_hdr(nexthdr)) {
91 struct ipv6_opt_hdr hdr;
92 int hdrlen;
94 if (len < (int)sizeof(struct ipv6_opt_hdr))
95 return -1;
96 if (nexthdr == NEXTHDR_NONE)
97 break;
98 if (nexthdr == NEXTHDR_FRAGMENT)
99 break;
100 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
101 BUG();
102 if (nexthdr == NEXTHDR_AUTH)
103 hdrlen = (hdr.hdrlen+2)<<2;
104 else
105 hdrlen = ipv6_optlen(&hdr);
107 nexthdr = hdr.nexthdr;
108 len -= hdrlen;
109 start += hdrlen;
112 *nexthdrp = nexthdr;
113 return start;
116 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
117 unsigned int *dataoff, u_int8_t *protonum)
119 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
120 unsigned char pnum;
121 int protoff;
123 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
124 &pnum, sizeof(pnum)) != 0) {
125 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
126 return -NF_ACCEPT;
128 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
130 * (protoff == skb->len) mean that the packet doesn't have no data
131 * except of IPv6 & ext headers. but it's tracked anyway. - YK
133 if ((protoff < 0) || (protoff > skb->len)) {
134 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
135 return -NF_ACCEPT;
138 *dataoff = protoff;
139 *protonum = pnum;
140 return NF_ACCEPT;
143 static unsigned int ipv6_confirm(unsigned int hooknum,
144 struct sk_buff *skb,
145 const struct net_device *in,
146 const struct net_device *out,
147 int (*okfn)(struct sk_buff *))
149 struct nf_conn *ct;
150 const struct nf_conn_help *help;
151 const struct nf_conntrack_helper *helper;
152 enum ip_conntrack_info ctinfo;
153 unsigned int ret, protoff;
154 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
155 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
158 /* This is where we call the helper: as the packet goes out. */
159 ct = nf_ct_get(skb, &ctinfo);
160 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
161 goto out;
163 help = nfct_help(ct);
164 if (!help)
165 goto out;
166 /* rcu_read_lock()ed by nf_hook_slow */
167 helper = rcu_dereference(help->helper);
168 if (!helper)
169 goto out;
171 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
172 skb->len - extoff);
173 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
174 pr_debug("proto header not found\n");
175 return NF_ACCEPT;
178 ret = helper->help(skb, protoff, ct, ctinfo);
179 if (ret != NF_ACCEPT)
180 return ret;
181 out:
182 /* We've seen it coming out the other side: confirm it */
183 return nf_conntrack_confirm(skb);
186 static unsigned int ipv6_defrag(unsigned int hooknum,
187 struct sk_buff *skb,
188 const struct net_device *in,
189 const struct net_device *out,
190 int (*okfn)(struct sk_buff *))
192 struct sk_buff *reasm;
194 /* Previously seen (loopback)? */
195 if (skb->nfct)
196 return NF_ACCEPT;
198 reasm = nf_ct_frag6_gather(skb);
200 /* queued */
201 if (reasm == NULL)
202 return NF_STOLEN;
204 /* error occured or not fragmented */
205 if (reasm == skb)
206 return NF_ACCEPT;
208 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
209 (struct net_device *)out, okfn);
211 return NF_STOLEN;
214 static unsigned int __ipv6_conntrack_in(struct net *net,
215 unsigned int hooknum,
216 struct sk_buff *skb,
217 int (*okfn)(struct sk_buff *))
219 struct sk_buff *reasm = skb->nfct_reasm;
221 /* This packet is fragmented and has reassembled packet. */
222 if (reasm) {
223 /* Reassembled packet isn't parsed yet ? */
224 if (!reasm->nfct) {
225 unsigned int ret;
227 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
228 if (ret != NF_ACCEPT)
229 return ret;
231 nf_conntrack_get(reasm->nfct);
232 skb->nfct = reasm->nfct;
233 skb->nfctinfo = reasm->nfctinfo;
234 return NF_ACCEPT;
237 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
240 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
241 struct sk_buff *skb,
242 const struct net_device *in,
243 const struct net_device *out,
244 int (*okfn)(struct sk_buff *))
246 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
249 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
250 struct sk_buff *skb,
251 const struct net_device *in,
252 const struct net_device *out,
253 int (*okfn)(struct sk_buff *))
255 /* root is playing with raw sockets. */
256 if (skb->len < sizeof(struct ipv6hdr)) {
257 if (net_ratelimit())
258 printk("ipv6_conntrack_local: packet too short\n");
259 return NF_ACCEPT;
261 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
264 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
266 .hook = ipv6_defrag,
267 .owner = THIS_MODULE,
268 .pf = PF_INET6,
269 .hooknum = NF_INET_PRE_ROUTING,
270 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
273 .hook = ipv6_conntrack_in,
274 .owner = THIS_MODULE,
275 .pf = PF_INET6,
276 .hooknum = NF_INET_PRE_ROUTING,
277 .priority = NF_IP6_PRI_CONNTRACK,
280 .hook = ipv6_conntrack_local,
281 .owner = THIS_MODULE,
282 .pf = PF_INET6,
283 .hooknum = NF_INET_LOCAL_OUT,
284 .priority = NF_IP6_PRI_CONNTRACK,
287 .hook = ipv6_defrag,
288 .owner = THIS_MODULE,
289 .pf = PF_INET6,
290 .hooknum = NF_INET_LOCAL_OUT,
291 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
294 .hook = ipv6_confirm,
295 .owner = THIS_MODULE,
296 .pf = PF_INET6,
297 .hooknum = NF_INET_POST_ROUTING,
298 .priority = NF_IP6_PRI_LAST,
301 .hook = ipv6_confirm,
302 .owner = THIS_MODULE,
303 .pf = PF_INET6,
304 .hooknum = NF_INET_LOCAL_IN,
305 .priority = NF_IP6_PRI_LAST-1,
309 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
311 #include <linux/netfilter/nfnetlink.h>
312 #include <linux/netfilter/nfnetlink_conntrack.h>
314 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
315 const struct nf_conntrack_tuple *tuple)
317 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
318 &tuple->src.u3.ip6);
319 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
320 &tuple->dst.u3.ip6);
321 return 0;
323 nla_put_failure:
324 return -1;
327 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
328 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
329 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
332 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
333 struct nf_conntrack_tuple *t)
335 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
336 return -EINVAL;
338 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
339 sizeof(u_int32_t) * 4);
340 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
341 sizeof(u_int32_t) * 4);
343 return 0;
346 static int ipv6_nlattr_tuple_size(void)
348 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
350 #endif
352 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
353 .l3proto = PF_INET6,
354 .name = "ipv6",
355 .pkt_to_tuple = ipv6_pkt_to_tuple,
356 .invert_tuple = ipv6_invert_tuple,
357 .print_tuple = ipv6_print_tuple,
358 .get_l4proto = ipv6_get_l4proto,
359 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
360 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
361 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
362 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
363 .nla_policy = ipv6_nla_policy,
364 #endif
365 #ifdef CONFIG_SYSCTL
366 .ctl_table_path = nf_net_netfilter_sysctl_path,
367 .ctl_table = nf_ct_ipv6_sysctl_table,
368 #endif
369 .me = THIS_MODULE,
372 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
373 MODULE_LICENSE("GPL");
374 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
376 static int __init nf_conntrack_l3proto_ipv6_init(void)
378 int ret = 0;
380 need_conntrack();
382 ret = nf_ct_frag6_init();
383 if (ret < 0) {
384 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
385 return ret;
387 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
388 if (ret < 0) {
389 printk("nf_conntrack_ipv6: can't register tcp.\n");
390 goto cleanup_frag6;
393 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
394 if (ret < 0) {
395 printk("nf_conntrack_ipv6: can't register udp.\n");
396 goto cleanup_tcp;
399 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
400 if (ret < 0) {
401 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
402 goto cleanup_udp;
405 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
406 if (ret < 0) {
407 printk("nf_conntrack_ipv6: can't register ipv6\n");
408 goto cleanup_icmpv6;
411 ret = nf_register_hooks(ipv6_conntrack_ops,
412 ARRAY_SIZE(ipv6_conntrack_ops));
413 if (ret < 0) {
414 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
415 "hook.\n");
416 goto cleanup_ipv6;
418 return ret;
420 cleanup_ipv6:
421 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
422 cleanup_icmpv6:
423 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
424 cleanup_udp:
425 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
426 cleanup_tcp:
427 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
428 cleanup_frag6:
429 nf_ct_frag6_cleanup();
430 return ret;
433 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
435 synchronize_net();
436 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
437 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
438 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
439 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
440 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
441 nf_ct_frag6_cleanup();
444 module_init(nf_conntrack_l3proto_ipv6_init);
445 module_exit(nf_conntrack_l3proto_ipv6_fini);