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