]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
netfilter: nf_conntrack: add support for "conntrack zones"
[net-next-2.6.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 22
8fa9ff68 23#include <linux/netfilter_bridge.h>
9fb9cbb1
YK
24#include <linux/netfilter_ipv6.h>
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_helper.h>
605dcad6 27#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1
YK
28#include <net/netfilter/nf_conntrack_l3proto.h>
29#include <net/netfilter/nf_conntrack_core.h>
5d0aa2cc 30#include <net/netfilter/nf_conntrack_zones.h>
9d2493f8 31#include <net/netfilter/ipv6/nf_conntrack_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
9fb9cbb1 146static unsigned int ipv6_confirm(unsigned int hooknum,
3db05fea 147 struct sk_buff *skb,
9fb9cbb1
YK
148 const struct net_device *in,
149 const struct net_device *out,
150 int (*okfn)(struct sk_buff *))
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;
dc808fe2 156 unsigned int ret, protoff;
3db05fea
HX
157 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
158 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
dc808fe2 159
9fb9cbb1
YK
160
161 /* This is where we call the helper: as the packet goes out. */
3db05fea 162 ct = nf_ct_get(skb, &ctinfo);
6442f1cf 163 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
dc808fe2
HW
164 goto out;
165
166 help = nfct_help(ct);
3c158f7f
PM
167 if (!help)
168 goto out;
169 /* rcu_read_lock()ed by nf_hook_slow */
170 helper = rcu_dereference(help->helper);
171 if (!helper)
dc808fe2
HW
172 goto out;
173
3db05fea
HX
174 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
175 skb->len - extoff);
176 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
0d53778e 177 pr_debug("proto header not found\n");
dc808fe2 178 return NF_ACCEPT;
9fb9cbb1
YK
179 }
180
3db05fea 181 ret = helper->help(skb, protoff, ct, ctinfo);
74f7a655
PM
182 if (ret != NF_ACCEPT) {
183 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
184 "nf_ct_%s: dropping packet", helper->name);
dc808fe2 185 return ret;
74f7a655 186 }
dc808fe2 187out:
9fb9cbb1 188 /* We've seen it coming out the other side: confirm it */
3db05fea 189 return nf_conntrack_confirm(skb);
9fb9cbb1
YK
190}
191
0b5ccb2e
PM
192static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
193 struct sk_buff *skb)
194{
5d0aa2cc
PM
195 u16 zone = NF_CT_DEFAULT_ZONE;
196
197 if (skb->nfct)
198 zone = nf_ct_zone((struct nf_conn *)skb->nfct);
199
8fa9ff68
PM
200#ifdef CONFIG_BRIDGE_NETFILTER
201 if (skb->nf_bridge &&
202 skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)
5d0aa2cc 203 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone;
8fa9ff68 204#endif
0b5ccb2e 205 if (hooknum == NF_INET_PRE_ROUTING)
5d0aa2cc 206 return IP6_DEFRAG_CONNTRACK_IN + zone;
0b5ccb2e 207 else
5d0aa2cc 208 return IP6_DEFRAG_CONNTRACK_OUT + zone;
0b5ccb2e
PM
209
210}
211
9fb9cbb1 212static unsigned int ipv6_defrag(unsigned int hooknum,
3db05fea 213 struct sk_buff *skb,
9fb9cbb1
YK
214 const struct net_device *in,
215 const struct net_device *out,
216 int (*okfn)(struct sk_buff *))
217{
218 struct sk_buff *reasm;
219
220 /* Previously seen (loopback)? */
b2a15a60 221 if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
9fb9cbb1
YK
222 return NF_ACCEPT;
223
0b5ccb2e 224 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb));
9fb9cbb1
YK
225 /* queued */
226 if (reasm == NULL)
227 return NF_STOLEN;
228
229 /* error occured or not fragmented */
3db05fea 230 if (reasm == skb)
9fb9cbb1
YK
231 return NF_ACCEPT;
232
233 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
234 (struct net_device *)out, okfn);
235
236 return NF_STOLEN;
237}
238
a702a65f
AD
239static unsigned int __ipv6_conntrack_in(struct net *net,
240 unsigned int hooknum,
241 struct sk_buff *skb,
242 int (*okfn)(struct sk_buff *))
9fb9cbb1 243{
3db05fea 244 struct sk_buff *reasm = skb->nfct_reasm;
9fb9cbb1
YK
245
246 /* This packet is fragmented and has reassembled packet. */
247 if (reasm) {
248 /* Reassembled packet isn't parsed yet ? */
249 if (!reasm->nfct) {
250 unsigned int ret;
251
a702a65f 252 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
9fb9cbb1
YK
253 if (ret != NF_ACCEPT)
254 return ret;
255 }
256 nf_conntrack_get(reasm->nfct);
3db05fea
HX
257 skb->nfct = reasm->nfct;
258 skb->nfctinfo = reasm->nfctinfo;
9fb9cbb1
YK
259 return NF_ACCEPT;
260 }
261
a702a65f
AD
262 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
263}
264
265static unsigned int ipv6_conntrack_in(unsigned int hooknum,
266 struct sk_buff *skb,
267 const struct net_device *in,
268 const struct net_device *out,
269 int (*okfn)(struct sk_buff *))
270{
271 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
9fb9cbb1
YK
272}
273
274static unsigned int ipv6_conntrack_local(unsigned int hooknum,
3db05fea 275 struct sk_buff *skb,
9fb9cbb1
YK
276 const struct net_device *in,
277 const struct net_device *out,
278 int (*okfn)(struct sk_buff *))
279{
280 /* root is playing with raw sockets. */
3db05fea 281 if (skb->len < sizeof(struct ipv6hdr)) {
9fb9cbb1
YK
282 if (net_ratelimit())
283 printk("ipv6_conntrack_local: packet too short\n");
284 return NF_ACCEPT;
285 }
a702a65f 286 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
9fb9cbb1
YK
287}
288
1999414a 289static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
964ddaa1
PM
290 {
291 .hook = ipv6_defrag,
292 .owner = THIS_MODULE,
57750a22 293 .pf = NFPROTO_IPV6,
6e23ae2a 294 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
295 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
296 },
297 {
298 .hook = ipv6_conntrack_in,
299 .owner = THIS_MODULE,
57750a22 300 .pf = NFPROTO_IPV6,
6e23ae2a 301 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
302 .priority = NF_IP6_PRI_CONNTRACK,
303 },
304 {
305 .hook = ipv6_conntrack_local,
306 .owner = THIS_MODULE,
57750a22 307 .pf = NFPROTO_IPV6,
6e23ae2a 308 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
309 .priority = NF_IP6_PRI_CONNTRACK,
310 },
311 {
312 .hook = ipv6_defrag,
313 .owner = THIS_MODULE,
57750a22 314 .pf = NFPROTO_IPV6,
6e23ae2a 315 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
316 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
317 },
318 {
319 .hook = ipv6_confirm,
320 .owner = THIS_MODULE,
57750a22 321 .pf = NFPROTO_IPV6,
6e23ae2a 322 .hooknum = NF_INET_POST_ROUTING,
964ddaa1
PM
323 .priority = NF_IP6_PRI_LAST,
324 },
325 {
326 .hook = ipv6_confirm,
327 .owner = THIS_MODULE,
57750a22 328 .pf = NFPROTO_IPV6,
6e23ae2a 329 .hooknum = NF_INET_LOCAL_IN,
964ddaa1
PM
330 .priority = NF_IP6_PRI_LAST-1,
331 },
9fb9cbb1
YK
332};
333
e281db5c 334#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
335
336#include <linux/netfilter/nfnetlink.h>
337#include <linux/netfilter/nfnetlink_conntrack.h>
338
fdf70832 339static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
340 const struct nf_conntrack_tuple *tuple)
341{
df6fb868 342 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
c1d10adb 343 &tuple->src.u3.ip6);
df6fb868 344 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
c1d10adb
PNA
345 &tuple->dst.u3.ip6);
346 return 0;
347
df6fb868 348nla_put_failure:
c1d10adb
PNA
349 return -1;
350}
351
f73e924c
PM
352static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
353 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
354 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
c1d10adb
PNA
355};
356
fdf70832 357static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
358 struct nf_conntrack_tuple *t)
359{
df6fb868 360 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
c1d10adb
PNA
361 return -EINVAL;
362
df6fb868 363 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
c1d10adb 364 sizeof(u_int32_t) * 4);
df6fb868 365 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
c1d10adb
PNA
366 sizeof(u_int32_t) * 4);
367
368 return 0;
369}
a400c30e
HE
370
371static int ipv6_nlattr_tuple_size(void)
372{
373 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
374}
c1d10adb
PNA
375#endif
376
61075af5 377struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
9fb9cbb1
YK
378 .l3proto = PF_INET6,
379 .name = "ipv6",
380 .pkt_to_tuple = ipv6_pkt_to_tuple,
381 .invert_tuple = ipv6_invert_tuple,
382 .print_tuple = ipv6_print_tuple,
ffc30690 383 .get_l4proto = ipv6_get_l4proto,
e281db5c 384#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832 385 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
a400c30e 386 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
fdf70832 387 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
f73e924c 388 .nla_policy = ipv6_nla_policy,
933a41e7
PM
389#endif
390#ifdef CONFIG_SYSCTL
391 .ctl_table_path = nf_net_netfilter_sysctl_path,
392 .ctl_table = nf_ct_ipv6_sysctl_table,
c1d10adb 393#endif
9fb9cbb1
YK
394 .me = THIS_MODULE,
395};
396
32292a7f
PM
397MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
398MODULE_LICENSE("GPL");
399MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
400
401static int __init nf_conntrack_l3proto_ipv6_init(void)
9fb9cbb1
YK
402{
403 int ret = 0;
404
32292a7f 405 need_conntrack();
9fb9cbb1
YK
406
407 ret = nf_ct_frag6_init();
408 if (ret < 0) {
409 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
32292a7f 410 return ret;
9fb9cbb1 411 }
605dcad6 412 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
413 if (ret < 0) {
414 printk("nf_conntrack_ipv6: can't register tcp.\n");
415 goto cleanup_frag6;
416 }
417
605dcad6 418 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
9fb9cbb1
YK
419 if (ret < 0) {
420 printk("nf_conntrack_ipv6: can't register udp.\n");
421 goto cleanup_tcp;
422 }
423
605dcad6 424 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1
YK
425 if (ret < 0) {
426 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
427 goto cleanup_udp;
428 }
429
430 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
431 if (ret < 0) {
432 printk("nf_conntrack_ipv6: can't register ipv6\n");
433 goto cleanup_icmpv6;
434 }
435
964ddaa1
PM
436 ret = nf_register_hooks(ipv6_conntrack_ops,
437 ARRAY_SIZE(ipv6_conntrack_ops));
9fb9cbb1
YK
438 if (ret < 0) {
439 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
440 "hook.\n");
441 goto cleanup_ipv6;
442 }
9fb9cbb1
YK
443 return ret;
444
9fb9cbb1
YK
445 cleanup_ipv6:
446 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
447 cleanup_icmpv6:
605dcad6 448 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1 449 cleanup_udp:
605dcad6 450 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
9fb9cbb1 451 cleanup_tcp:
605dcad6 452 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
453 cleanup_frag6:
454 nf_ct_frag6_cleanup();
9fb9cbb1
YK
455 return ret;
456}
457
65b4b4e8 458static void __exit nf_conntrack_l3proto_ipv6_fini(void)
9fb9cbb1 459{
32292a7f 460 synchronize_net();
32292a7f
PM
461 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
462 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
605dcad6
MJ
463 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
464 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
465 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
32292a7f 466 nf_ct_frag6_cleanup();
9fb9cbb1
YK
467}
468
65b4b4e8
AM
469module_init(nf_conntrack_l3proto_ipv6_init);
470module_exit(nf_conntrack_l3proto_ipv6_fini);