ipv6: reassembly: use seperate reassembly queues for conntrack and local delivery
[deliverable/linux.git] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
CommitLineData
9fb9cbb1
YK
1/*
2 * Copyright (C)2004 USAGI/WIDE Project
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 * Author:
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9fb9cbb1
YK
10 */
11
9fb9cbb1
YK
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>
04128f23 21#include <net/inet_frag.h>
9fb9cbb1
YK
22
23#include <linux/netfilter_ipv6.h>
24#include <net/netfilter/nf_conntrack.h>
25#include <net/netfilter/nf_conntrack_helper.h>
605dcad6 26#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1
YK
27#include <net/netfilter/nf_conntrack_l3proto.h>
28#include <net/netfilter/nf_conntrack_core.h>
9d2493f8 29#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
74f7a655 30#include <net/netfilter/nf_log.h>
9fb9cbb1 31
8ce8439a
JE
32static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
33 struct nf_conntrack_tuple *tuple)
9fb9cbb1 34{
32948588
JE
35 const u_int32_t *ap;
36 u_int32_t _addrs[8];
9fb9cbb1
YK
37
38 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
39 sizeof(_addrs), _addrs);
40 if (ap == NULL)
8ce8439a 41 return false;
9fb9cbb1
YK
42
43 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
44 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
45
8ce8439a 46 return true;
9fb9cbb1
YK
47}
48
8ce8439a
JE
49static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
9fb9cbb1
YK
51{
52 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
53 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
54
8ce8439a 55 return true;
9fb9cbb1
YK
56}
57
58static int ipv6_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
60{
5b095d98 61 return seq_printf(s, "src=%pI6 dst=%pI6 ",
0c6ce78a 62 tuple->src.u3.ip6, tuple->dst.u3.ip6);
9fb9cbb1
YK
63}
64
9fb9cbb1
YK
65/*
66 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
67 *
68 * This function parses (probably truncated) exthdr set "hdr"
69 * of length "len". "nexthdrp" initially points to some place,
70 * where type of the first header can be found.
71 *
72 * It skips all well-known exthdrs, and returns pointer to the start
73 * of unparsable area i.e. the first header with unknown type.
74 * if success, *nexthdr is updated by type/protocol of this header.
75 *
76 * NOTES: - it may return pointer pointing beyond end of packet,
77 * if the last recognized header is truncated in the middle.
78 * - if packet is truncated, so that all parsed headers are skipped,
79 * it returns -1.
80 * - if packet is fragmented, return pointer of the fragment header.
81 * - ESP is unparsable for now and considered like
82 * normal payload protocol.
83 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
84 */
85
1a3a206f
AB
86static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
87 u8 *nexthdrp, int len)
9fb9cbb1
YK
88{
89 u8 nexthdr = *nexthdrp;
90
91 while (ipv6_ext_hdr(nexthdr)) {
92 struct ipv6_opt_hdr hdr;
93 int hdrlen;
94
95 if (len < (int)sizeof(struct ipv6_opt_hdr))
96 return -1;
97 if (nexthdr == NEXTHDR_NONE)
98 break;
99 if (nexthdr == NEXTHDR_FRAGMENT)
100 break;
101 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
102 BUG();
103 if (nexthdr == NEXTHDR_AUTH)
104 hdrlen = (hdr.hdrlen+2)<<2;
105 else
106 hdrlen = ipv6_optlen(&hdr);
107
108 nexthdr = hdr.nexthdr;
109 len -= hdrlen;
110 start += hdrlen;
111 }
112
113 *nexthdrp = nexthdr;
114 return start;
115}
116
ffc30690
YK
117static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
118 unsigned int *dataoff, u_int8_t *protonum)
9fb9cbb1 119{
ffc30690
YK
120 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
121 unsigned char pnum;
122 int protoff;
123
124 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
125 &pnum, sizeof(pnum)) != 0) {
126 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
127 return -NF_ACCEPT;
128 }
129 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
9fb9cbb1 130 /*
ffc30690 131 * (protoff == skb->len) mean that the packet doesn't have no data
9fb9cbb1
YK
132 * except of IPv6 & ext headers. but it's tracked anyway. - YK
133 */
ffc30690 134 if ((protoff < 0) || (protoff > skb->len)) {
0d53778e 135 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
9fb9cbb1
YK
136 return -NF_ACCEPT;
137 }
138
139 *dataoff = protoff;
140 *protonum = pnum;
141 return NF_ACCEPT;
142}
143
9fb9cbb1 144static unsigned int ipv6_confirm(unsigned int hooknum,
3db05fea 145 struct sk_buff *skb,
9fb9cbb1
YK
146 const struct net_device *in,
147 const struct net_device *out,
148 int (*okfn)(struct sk_buff *))
149{
150 struct nf_conn *ct;
32948588
JE
151 const struct nf_conn_help *help;
152 const struct nf_conntrack_helper *helper;
9fb9cbb1 153 enum ip_conntrack_info ctinfo;
dc808fe2 154 unsigned int ret, protoff;
3db05fea
HX
155 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
156 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
dc808fe2 157
9fb9cbb1
YK
158
159 /* This is where we call the helper: as the packet goes out. */
3db05fea 160 ct = nf_ct_get(skb, &ctinfo);
6442f1cf 161 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
dc808fe2
HW
162 goto out;
163
164 help = nfct_help(ct);
3c158f7f
PM
165 if (!help)
166 goto out;
167 /* rcu_read_lock()ed by nf_hook_slow */
168 helper = rcu_dereference(help->helper);
169 if (!helper)
dc808fe2
HW
170 goto out;
171
3db05fea
HX
172 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
173 skb->len - extoff);
174 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
0d53778e 175 pr_debug("proto header not found\n");
dc808fe2 176 return NF_ACCEPT;
9fb9cbb1
YK
177 }
178
3db05fea 179 ret = helper->help(skb, protoff, ct, ctinfo);
74f7a655
PM
180 if (ret != NF_ACCEPT) {
181 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
182 "nf_ct_%s: dropping packet", helper->name);
dc808fe2 183 return ret;
74f7a655 184 }
dc808fe2 185out:
9fb9cbb1 186 /* We've seen it coming out the other side: confirm it */
3db05fea 187 return nf_conntrack_confirm(skb);
9fb9cbb1
YK
188}
189
0b5ccb2e
PM
190static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
191 struct sk_buff *skb)
192{
193 if (hooknum == NF_INET_PRE_ROUTING)
194 return IP6_DEFRAG_CONNTRACK_IN;
195 else
196 return IP6_DEFRAG_CONNTRACK_OUT;
197
198}
199
9fb9cbb1 200static unsigned int ipv6_defrag(unsigned int hooknum,
3db05fea 201 struct sk_buff *skb,
9fb9cbb1
YK
202 const struct net_device *in,
203 const struct net_device *out,
204 int (*okfn)(struct sk_buff *))
205{
206 struct sk_buff *reasm;
207
208 /* Previously seen (loopback)? */
3db05fea 209 if (skb->nfct)
9fb9cbb1
YK
210 return NF_ACCEPT;
211
0b5ccb2e 212 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb));
9fb9cbb1
YK
213 /* queued */
214 if (reasm == NULL)
215 return NF_STOLEN;
216
217 /* error occured or not fragmented */
3db05fea 218 if (reasm == skb)
9fb9cbb1
YK
219 return NF_ACCEPT;
220
221 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
222 (struct net_device *)out, okfn);
223
224 return NF_STOLEN;
225}
226
a702a65f
AD
227static unsigned int __ipv6_conntrack_in(struct net *net,
228 unsigned int hooknum,
229 struct sk_buff *skb,
230 int (*okfn)(struct sk_buff *))
9fb9cbb1 231{
3db05fea 232 struct sk_buff *reasm = skb->nfct_reasm;
9fb9cbb1
YK
233
234 /* This packet is fragmented and has reassembled packet. */
235 if (reasm) {
236 /* Reassembled packet isn't parsed yet ? */
237 if (!reasm->nfct) {
238 unsigned int ret;
239
a702a65f 240 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
9fb9cbb1
YK
241 if (ret != NF_ACCEPT)
242 return ret;
243 }
244 nf_conntrack_get(reasm->nfct);
3db05fea
HX
245 skb->nfct = reasm->nfct;
246 skb->nfctinfo = reasm->nfctinfo;
9fb9cbb1
YK
247 return NF_ACCEPT;
248 }
249
a702a65f
AD
250 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
251}
252
253static unsigned int ipv6_conntrack_in(unsigned int hooknum,
254 struct sk_buff *skb,
255 const struct net_device *in,
256 const struct net_device *out,
257 int (*okfn)(struct sk_buff *))
258{
259 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
9fb9cbb1
YK
260}
261
262static unsigned int ipv6_conntrack_local(unsigned int hooknum,
3db05fea 263 struct sk_buff *skb,
9fb9cbb1
YK
264 const struct net_device *in,
265 const struct net_device *out,
266 int (*okfn)(struct sk_buff *))
267{
268 /* root is playing with raw sockets. */
3db05fea 269 if (skb->len < sizeof(struct ipv6hdr)) {
9fb9cbb1
YK
270 if (net_ratelimit())
271 printk("ipv6_conntrack_local: packet too short\n");
272 return NF_ACCEPT;
273 }
a702a65f 274 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
9fb9cbb1
YK
275}
276
1999414a 277static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
964ddaa1
PM
278 {
279 .hook = ipv6_defrag,
280 .owner = THIS_MODULE,
57750a22 281 .pf = NFPROTO_IPV6,
6e23ae2a 282 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
283 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
284 },
285 {
286 .hook = ipv6_conntrack_in,
287 .owner = THIS_MODULE,
57750a22 288 .pf = NFPROTO_IPV6,
6e23ae2a 289 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
290 .priority = NF_IP6_PRI_CONNTRACK,
291 },
292 {
293 .hook = ipv6_conntrack_local,
294 .owner = THIS_MODULE,
57750a22 295 .pf = NFPROTO_IPV6,
6e23ae2a 296 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
297 .priority = NF_IP6_PRI_CONNTRACK,
298 },
299 {
300 .hook = ipv6_defrag,
301 .owner = THIS_MODULE,
57750a22 302 .pf = NFPROTO_IPV6,
6e23ae2a 303 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
304 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
305 },
306 {
307 .hook = ipv6_confirm,
308 .owner = THIS_MODULE,
57750a22 309 .pf = NFPROTO_IPV6,
6e23ae2a 310 .hooknum = NF_INET_POST_ROUTING,
964ddaa1
PM
311 .priority = NF_IP6_PRI_LAST,
312 },
313 {
314 .hook = ipv6_confirm,
315 .owner = THIS_MODULE,
57750a22 316 .pf = NFPROTO_IPV6,
6e23ae2a 317 .hooknum = NF_INET_LOCAL_IN,
964ddaa1
PM
318 .priority = NF_IP6_PRI_LAST-1,
319 },
9fb9cbb1
YK
320};
321
e281db5c 322#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
323
324#include <linux/netfilter/nfnetlink.h>
325#include <linux/netfilter/nfnetlink_conntrack.h>
326
fdf70832 327static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
328 const struct nf_conntrack_tuple *tuple)
329{
df6fb868 330 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
c1d10adb 331 &tuple->src.u3.ip6);
df6fb868 332 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
c1d10adb
PNA
333 &tuple->dst.u3.ip6);
334 return 0;
335
df6fb868 336nla_put_failure:
c1d10adb
PNA
337 return -1;
338}
339
f73e924c
PM
340static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
341 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
342 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
c1d10adb
PNA
343};
344
fdf70832 345static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
346 struct nf_conntrack_tuple *t)
347{
df6fb868 348 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
c1d10adb
PNA
349 return -EINVAL;
350
df6fb868 351 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
c1d10adb 352 sizeof(u_int32_t) * 4);
df6fb868 353 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
c1d10adb
PNA
354 sizeof(u_int32_t) * 4);
355
356 return 0;
357}
a400c30e
HE
358
359static int ipv6_nlattr_tuple_size(void)
360{
361 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
362}
c1d10adb
PNA
363#endif
364
61075af5 365struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
9fb9cbb1
YK
366 .l3proto = PF_INET6,
367 .name = "ipv6",
368 .pkt_to_tuple = ipv6_pkt_to_tuple,
369 .invert_tuple = ipv6_invert_tuple,
370 .print_tuple = ipv6_print_tuple,
ffc30690 371 .get_l4proto = ipv6_get_l4proto,
e281db5c 372#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832 373 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
a400c30e 374 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
fdf70832 375 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
f73e924c 376 .nla_policy = ipv6_nla_policy,
933a41e7
PM
377#endif
378#ifdef CONFIG_SYSCTL
379 .ctl_table_path = nf_net_netfilter_sysctl_path,
380 .ctl_table = nf_ct_ipv6_sysctl_table,
c1d10adb 381#endif
9fb9cbb1
YK
382 .me = THIS_MODULE,
383};
384
32292a7f
PM
385MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
386MODULE_LICENSE("GPL");
387MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
388
389static int __init nf_conntrack_l3proto_ipv6_init(void)
9fb9cbb1
YK
390{
391 int ret = 0;
392
32292a7f 393 need_conntrack();
9fb9cbb1
YK
394
395 ret = nf_ct_frag6_init();
396 if (ret < 0) {
397 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
32292a7f 398 return ret;
9fb9cbb1 399 }
605dcad6 400 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
401 if (ret < 0) {
402 printk("nf_conntrack_ipv6: can't register tcp.\n");
403 goto cleanup_frag6;
404 }
405
605dcad6 406 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
9fb9cbb1
YK
407 if (ret < 0) {
408 printk("nf_conntrack_ipv6: can't register udp.\n");
409 goto cleanup_tcp;
410 }
411
605dcad6 412 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1
YK
413 if (ret < 0) {
414 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
415 goto cleanup_udp;
416 }
417
418 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
419 if (ret < 0) {
420 printk("nf_conntrack_ipv6: can't register ipv6\n");
421 goto cleanup_icmpv6;
422 }
423
964ddaa1
PM
424 ret = nf_register_hooks(ipv6_conntrack_ops,
425 ARRAY_SIZE(ipv6_conntrack_ops));
9fb9cbb1
YK
426 if (ret < 0) {
427 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
428 "hook.\n");
429 goto cleanup_ipv6;
430 }
9fb9cbb1
YK
431 return ret;
432
9fb9cbb1
YK
433 cleanup_ipv6:
434 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
435 cleanup_icmpv6:
605dcad6 436 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1 437 cleanup_udp:
605dcad6 438 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
9fb9cbb1 439 cleanup_tcp:
605dcad6 440 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
441 cleanup_frag6:
442 nf_ct_frag6_cleanup();
9fb9cbb1
YK
443 return ret;
444}
445
65b4b4e8 446static void __exit nf_conntrack_l3proto_ipv6_fini(void)
9fb9cbb1 447{
32292a7f 448 synchronize_net();
32292a7f
PM
449 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
450 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
605dcad6
MJ
451 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
452 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
453 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
32292a7f 454 nf_ct_frag6_cleanup();
9fb9cbb1
YK
455}
456
65b4b4e8
AM
457module_init(nf_conntrack_l3proto_ipv6_init);
458module_exit(nf_conntrack_l3proto_ipv6_fini);
This page took 0.469377 seconds and 5 git commands to generate.