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