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