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