]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
net: Convert net_ratelimit uses to net_<level>_ratelimited
[mirror_ubuntu-artful-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
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);
fb048833 163 if (!ct || ctinfo == IP_CT_RELATED_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
a702a65f
AD
192static unsigned int __ipv6_conntrack_in(struct net *net,
193 unsigned int hooknum,
194 struct sk_buff *skb,
195 int (*okfn)(struct sk_buff *))
9fb9cbb1 196{
3db05fea 197 struct sk_buff *reasm = skb->nfct_reasm;
9fb9cbb1
YK
198
199 /* This packet is fragmented and has reassembled packet. */
200 if (reasm) {
201 /* Reassembled packet isn't parsed yet ? */
202 if (!reasm->nfct) {
203 unsigned int ret;
204
a702a65f 205 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
9fb9cbb1
YK
206 if (ret != NF_ACCEPT)
207 return ret;
208 }
209 nf_conntrack_get(reasm->nfct);
3db05fea
HX
210 skb->nfct = reasm->nfct;
211 skb->nfctinfo = reasm->nfctinfo;
9fb9cbb1
YK
212 return NF_ACCEPT;
213 }
214
a702a65f
AD
215 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
216}
217
218static unsigned int ipv6_conntrack_in(unsigned int hooknum,
219 struct sk_buff *skb,
220 const struct net_device *in,
221 const struct net_device *out,
222 int (*okfn)(struct sk_buff *))
223{
224 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
9fb9cbb1
YK
225}
226
227static unsigned int ipv6_conntrack_local(unsigned int hooknum,
3db05fea 228 struct sk_buff *skb,
9fb9cbb1
YK
229 const struct net_device *in,
230 const struct net_device *out,
231 int (*okfn)(struct sk_buff *))
232{
233 /* root is playing with raw sockets. */
3db05fea 234 if (skb->len < sizeof(struct ipv6hdr)) {
e87cc472 235 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
9fb9cbb1
YK
236 return NF_ACCEPT;
237 }
a702a65f 238 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
9fb9cbb1
YK
239}
240
1999414a 241static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
964ddaa1
PM
242 {
243 .hook = ipv6_conntrack_in,
244 .owner = THIS_MODULE,
57750a22 245 .pf = NFPROTO_IPV6,
6e23ae2a 246 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
247 .priority = NF_IP6_PRI_CONNTRACK,
248 },
249 {
250 .hook = ipv6_conntrack_local,
251 .owner = THIS_MODULE,
57750a22 252 .pf = NFPROTO_IPV6,
6e23ae2a 253 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
254 .priority = NF_IP6_PRI_CONNTRACK,
255 },
964ddaa1
PM
256 {
257 .hook = ipv6_confirm,
258 .owner = THIS_MODULE,
57750a22 259 .pf = NFPROTO_IPV6,
6e23ae2a 260 .hooknum = NF_INET_POST_ROUTING,
964ddaa1
PM
261 .priority = NF_IP6_PRI_LAST,
262 },
263 {
264 .hook = ipv6_confirm,
265 .owner = THIS_MODULE,
57750a22 266 .pf = NFPROTO_IPV6,
6e23ae2a 267 .hooknum = NF_INET_LOCAL_IN,
964ddaa1
PM
268 .priority = NF_IP6_PRI_LAST-1,
269 },
9fb9cbb1
YK
270};
271
e281db5c 272#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
273
274#include <linux/netfilter/nfnetlink.h>
275#include <linux/netfilter/nfnetlink_conntrack.h>
276
fdf70832 277static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
278 const struct nf_conntrack_tuple *tuple)
279{
e549a6b3
DM
280 if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
281 &tuple->src.u3.ip6) ||
282 nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
283 &tuple->dst.u3.ip6))
284 goto nla_put_failure;
c1d10adb
PNA
285 return 0;
286
df6fb868 287nla_put_failure:
c1d10adb
PNA
288 return -1;
289}
290
f73e924c
PM
291static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
292 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
293 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
c1d10adb
PNA
294};
295
fdf70832 296static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
297 struct nf_conntrack_tuple *t)
298{
df6fb868 299 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
c1d10adb
PNA
300 return -EINVAL;
301
df6fb868 302 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
c1d10adb 303 sizeof(u_int32_t) * 4);
df6fb868 304 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
c1d10adb
PNA
305 sizeof(u_int32_t) * 4);
306
307 return 0;
308}
a400c30e
HE
309
310static int ipv6_nlattr_tuple_size(void)
311{
312 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
313}
c1d10adb
PNA
314#endif
315
61075af5 316struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
9fb9cbb1
YK
317 .l3proto = PF_INET6,
318 .name = "ipv6",
319 .pkt_to_tuple = ipv6_pkt_to_tuple,
320 .invert_tuple = ipv6_invert_tuple,
321 .print_tuple = ipv6_print_tuple,
ffc30690 322 .get_l4proto = ipv6_get_l4proto,
e281db5c 323#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832 324 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
a400c30e 325 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
fdf70832 326 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
f73e924c 327 .nla_policy = ipv6_nla_policy,
c1d10adb 328#endif
9fb9cbb1
YK
329 .me = THIS_MODULE,
330};
331
32292a7f
PM
332MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
333MODULE_LICENSE("GPL");
334MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
335
336static int __init nf_conntrack_l3proto_ipv6_init(void)
9fb9cbb1
YK
337{
338 int ret = 0;
339
32292a7f 340 need_conntrack();
e97c3e27 341 nf_defrag_ipv6_enable();
9fb9cbb1 342
605dcad6 343 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
9fb9cbb1 344 if (ret < 0) {
654d0fbd 345 pr_err("nf_conntrack_ipv6: can't register tcp.\n");
e97c3e27 346 return ret;
9fb9cbb1
YK
347 }
348
605dcad6 349 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
9fb9cbb1 350 if (ret < 0) {
654d0fbd 351 pr_err("nf_conntrack_ipv6: can't register udp.\n");
9fb9cbb1
YK
352 goto cleanup_tcp;
353 }
354
605dcad6 355 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1 356 if (ret < 0) {
654d0fbd 357 pr_err("nf_conntrack_ipv6: can't register icmpv6.\n");
9fb9cbb1
YK
358 goto cleanup_udp;
359 }
360
361 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
362 if (ret < 0) {
654d0fbd 363 pr_err("nf_conntrack_ipv6: can't register ipv6\n");
9fb9cbb1
YK
364 goto cleanup_icmpv6;
365 }
366
964ddaa1
PM
367 ret = nf_register_hooks(ipv6_conntrack_ops,
368 ARRAY_SIZE(ipv6_conntrack_ops));
9fb9cbb1 369 if (ret < 0) {
654d0fbd 370 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
9fb9cbb1
YK
371 "hook.\n");
372 goto cleanup_ipv6;
373 }
9fb9cbb1
YK
374 return ret;
375
9fb9cbb1
YK
376 cleanup_ipv6:
377 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
378 cleanup_icmpv6:
605dcad6 379 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
9fb9cbb1 380 cleanup_udp:
605dcad6 381 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
9fb9cbb1 382 cleanup_tcp:
605dcad6 383 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
384 return ret;
385}
386
65b4b4e8 387static void __exit nf_conntrack_l3proto_ipv6_fini(void)
9fb9cbb1 388{
32292a7f 389 synchronize_net();
32292a7f
PM
390 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
391 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
605dcad6
MJ
392 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
393 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
394 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
9fb9cbb1
YK
395}
396
65b4b4e8
AM
397module_init(nf_conntrack_l3proto_ipv6_init);
398module_exit(nf_conntrack_l3proto_ipv6_fini);