]>
Commit | Line | Data |
---|---|---|
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 | 23 | #include <linux/netfilter_ipv6.h> |
121d1e09 | 24 | #include <linux/netfilter_ipv6/ip6_tables.h> |
9fb9cbb1 YK |
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> |
58a317f1 | 32 | #include <net/netfilter/nf_nat_helper.h> |
e97c3e27 | 33 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
74f7a655 | 34 | #include <net/netfilter/nf_log.h> |
9fb9cbb1 | 35 | |
8ce8439a JE |
36 | static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff, |
37 | struct nf_conntrack_tuple *tuple) | |
9fb9cbb1 | 38 | { |
32948588 JE |
39 | const u_int32_t *ap; |
40 | u_int32_t _addrs[8]; | |
9fb9cbb1 YK |
41 | |
42 | ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr), | |
43 | sizeof(_addrs), _addrs); | |
44 | if (ap == NULL) | |
8ce8439a | 45 | return false; |
9fb9cbb1 YK |
46 | |
47 | memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6)); | |
48 | memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6)); | |
49 | ||
8ce8439a | 50 | return true; |
9fb9cbb1 YK |
51 | } |
52 | ||
8ce8439a JE |
53 | static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
54 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 YK |
55 | { |
56 | memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6)); | |
57 | memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6)); | |
58 | ||
8ce8439a | 59 | return true; |
9fb9cbb1 YK |
60 | } |
61 | ||
62 | static int ipv6_print_tuple(struct seq_file *s, | |
63 | const struct nf_conntrack_tuple *tuple) | |
64 | { | |
5b095d98 | 65 | return seq_printf(s, "src=%pI6 dst=%pI6 ", |
0c6ce78a | 66 | tuple->src.u3.ip6, tuple->dst.u3.ip6); |
9fb9cbb1 YK |
67 | } |
68 | ||
ffc30690 YK |
69 | static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
70 | unsigned int *dataoff, u_int8_t *protonum) | |
9fb9cbb1 | 71 | { |
ffc30690 | 72 | unsigned int extoff = nhoff + sizeof(struct ipv6hdr); |
2b60af01 | 73 | __be16 frag_off; |
ffc30690 | 74 | int protoff; |
2b60af01 | 75 | u8 nexthdr; |
ffc30690 YK |
76 | |
77 | if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr), | |
2b60af01 | 78 | &nexthdr, sizeof(nexthdr)) != 0) { |
ffc30690 YK |
79 | pr_debug("ip6_conntrack_core: can't get nexthdr\n"); |
80 | return -NF_ACCEPT; | |
81 | } | |
2b60af01 | 82 | protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off); |
9fb9cbb1 | 83 | /* |
d7a769ff FF |
84 | * (protoff == skb->len) means the packet has not data, just |
85 | * IPv6 and possibly extensions headers, but it is tracked anyway | |
9fb9cbb1 | 86 | */ |
2b60af01 | 87 | if (protoff < 0 || (frag_off & htons(~0x7)) != 0) { |
0d53778e | 88 | pr_debug("ip6_conntrack_core: can't find proto in pkt\n"); |
9fb9cbb1 YK |
89 | return -NF_ACCEPT; |
90 | } | |
91 | ||
92 | *dataoff = protoff; | |
2b60af01 | 93 | *protonum = nexthdr; |
9fb9cbb1 YK |
94 | return NF_ACCEPT; |
95 | } | |
96 | ||
12f7a505 PNA |
97 | static unsigned int ipv6_helper(unsigned int hooknum, |
98 | struct sk_buff *skb, | |
99 | const struct net_device *in, | |
100 | const struct net_device *out, | |
101 | int (*okfn)(struct sk_buff *)) | |
9fb9cbb1 YK |
102 | { |
103 | struct nf_conn *ct; | |
32948588 JE |
104 | const struct nf_conn_help *help; |
105 | const struct nf_conntrack_helper *helper; | |
9fb9cbb1 | 106 | enum ip_conntrack_info ctinfo; |
4cdd3408 PM |
107 | unsigned int ret; |
108 | __be16 frag_off; | |
109 | int protoff; | |
110 | u8 nexthdr; | |
9fb9cbb1 YK |
111 | |
112 | /* This is where we call the helper: as the packet goes out. */ | |
3db05fea | 113 | ct = nf_ct_get(skb, &ctinfo); |
fb048833 | 114 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) |
12f7a505 | 115 | return NF_ACCEPT; |
dc808fe2 HW |
116 | |
117 | help = nfct_help(ct); | |
3c158f7f | 118 | if (!help) |
12f7a505 | 119 | return NF_ACCEPT; |
3c158f7f PM |
120 | /* rcu_read_lock()ed by nf_hook_slow */ |
121 | helper = rcu_dereference(help->helper); | |
122 | if (!helper) | |
12f7a505 | 123 | return NF_ACCEPT; |
dc808fe2 | 124 | |
4cdd3408 PM |
125 | nexthdr = ipv6_hdr(skb)->nexthdr; |
126 | protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, | |
127 | &frag_off); | |
128 | if (protoff < 0 || (frag_off & htons(~0x7)) != 0) { | |
0d53778e | 129 | pr_debug("proto header not found\n"); |
dc808fe2 | 130 | return NF_ACCEPT; |
9fb9cbb1 YK |
131 | } |
132 | ||
3db05fea | 133 | ret = helper->help(skb, protoff, ct, ctinfo); |
12f7a505 | 134 | if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) { |
74f7a655 PM |
135 | nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL, |
136 | "nf_ct_%s: dropping packet", helper->name); | |
74f7a655 | 137 | } |
12f7a505 PNA |
138 | return ret; |
139 | } | |
140 | ||
141 | static unsigned int ipv6_confirm(unsigned int hooknum, | |
142 | struct sk_buff *skb, | |
143 | const struct net_device *in, | |
144 | const struct net_device *out, | |
145 | int (*okfn)(struct sk_buff *)) | |
146 | { | |
58a317f1 PM |
147 | struct nf_conn *ct; |
148 | enum ip_conntrack_info ctinfo; | |
149 | unsigned char pnum = ipv6_hdr(skb)->nexthdr; | |
150 | int protoff; | |
151 | __be16 frag_off; | |
152 | ||
153 | ct = nf_ct_get(skb, &ctinfo); | |
154 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) | |
155 | goto out; | |
156 | ||
157 | protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum, | |
158 | &frag_off); | |
159 | if (protoff < 0 || (frag_off & htons(~0x7)) != 0) { | |
160 | pr_debug("proto header not found\n"); | |
161 | goto out; | |
162 | } | |
163 | ||
164 | /* adjust seqs for loopback traffic only in outgoing direction */ | |
165 | if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && | |
166 | !nf_is_loopback_packet(skb)) { | |
167 | typeof(nf_nat_seq_adjust_hook) seq_adjust; | |
168 | ||
169 | seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook); | |
170 | if (!seq_adjust || | |
171 | !seq_adjust(skb, ct, ctinfo, protoff)) { | |
172 | NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop); | |
173 | return NF_DROP; | |
174 | } | |
175 | } | |
176 | out: | |
9fb9cbb1 | 177 | /* We've seen it coming out the other side: confirm it */ |
3db05fea | 178 | return nf_conntrack_confirm(skb); |
9fb9cbb1 YK |
179 | } |
180 | ||
a702a65f AD |
181 | static unsigned int __ipv6_conntrack_in(struct net *net, |
182 | unsigned int hooknum, | |
183 | struct sk_buff *skb, | |
4cdd3408 PM |
184 | const struct net_device *in, |
185 | const struct net_device *out, | |
a702a65f | 186 | int (*okfn)(struct sk_buff *)) |
9fb9cbb1 | 187 | { |
3db05fea | 188 | struct sk_buff *reasm = skb->nfct_reasm; |
4cdd3408 PM |
189 | const struct nf_conn_help *help; |
190 | struct nf_conn *ct; | |
191 | enum ip_conntrack_info ctinfo; | |
9fb9cbb1 YK |
192 | |
193 | /* This packet is fragmented and has reassembled packet. */ | |
194 | if (reasm) { | |
195 | /* Reassembled packet isn't parsed yet ? */ | |
196 | if (!reasm->nfct) { | |
197 | unsigned int ret; | |
198 | ||
a702a65f | 199 | ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm); |
9fb9cbb1 YK |
200 | if (ret != NF_ACCEPT) |
201 | return ret; | |
202 | } | |
4cdd3408 PM |
203 | |
204 | /* Conntrack helpers need the entire reassembled packet in the | |
58a317f1 PM |
205 | * POST_ROUTING hook. In case of unconfirmed connections NAT |
206 | * might reassign a helper, so the entire packet is also | |
207 | * required. | |
4cdd3408 PM |
208 | */ |
209 | ct = nf_ct_get(reasm, &ctinfo); | |
210 | if (ct != NULL && !nf_ct_is_untracked(ct)) { | |
211 | help = nfct_help(ct); | |
58a317f1 | 212 | if ((help && help->helper) || !nf_ct_is_confirmed(ct)) { |
4cdd3408 PM |
213 | nf_conntrack_get_reasm(skb); |
214 | NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, reasm, | |
215 | (struct net_device *)in, | |
216 | (struct net_device *)out, | |
217 | okfn, NF_IP6_PRI_CONNTRACK + 1); | |
218 | return NF_DROP_ERR(-ECANCELED); | |
219 | } | |
220 | } | |
221 | ||
9fb9cbb1 | 222 | nf_conntrack_get(reasm->nfct); |
3db05fea HX |
223 | skb->nfct = reasm->nfct; |
224 | skb->nfctinfo = reasm->nfctinfo; | |
9fb9cbb1 YK |
225 | return NF_ACCEPT; |
226 | } | |
227 | ||
a702a65f AD |
228 | return nf_conntrack_in(net, PF_INET6, hooknum, skb); |
229 | } | |
230 | ||
231 | static unsigned int ipv6_conntrack_in(unsigned int hooknum, | |
232 | struct sk_buff *skb, | |
233 | const struct net_device *in, | |
234 | const struct net_device *out, | |
235 | int (*okfn)(struct sk_buff *)) | |
236 | { | |
4cdd3408 | 237 | return __ipv6_conntrack_in(dev_net(in), hooknum, skb, in, out, okfn); |
9fb9cbb1 YK |
238 | } |
239 | ||
240 | static unsigned int ipv6_conntrack_local(unsigned int hooknum, | |
3db05fea | 241 | struct sk_buff *skb, |
9fb9cbb1 YK |
242 | const struct net_device *in, |
243 | const struct net_device *out, | |
244 | int (*okfn)(struct sk_buff *)) | |
245 | { | |
246 | /* root is playing with raw sockets. */ | |
3db05fea | 247 | if (skb->len < sizeof(struct ipv6hdr)) { |
e87cc472 | 248 | net_notice_ratelimited("ipv6_conntrack_local: packet too short\n"); |
9fb9cbb1 YK |
249 | return NF_ACCEPT; |
250 | } | |
4cdd3408 | 251 | return __ipv6_conntrack_in(dev_net(out), hooknum, skb, in, out, okfn); |
9fb9cbb1 YK |
252 | } |
253 | ||
1999414a | 254 | static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = { |
964ddaa1 PM |
255 | { |
256 | .hook = ipv6_conntrack_in, | |
257 | .owner = THIS_MODULE, | |
57750a22 | 258 | .pf = NFPROTO_IPV6, |
6e23ae2a | 259 | .hooknum = NF_INET_PRE_ROUTING, |
964ddaa1 PM |
260 | .priority = NF_IP6_PRI_CONNTRACK, |
261 | }, | |
262 | { | |
263 | .hook = ipv6_conntrack_local, | |
264 | .owner = THIS_MODULE, | |
57750a22 | 265 | .pf = NFPROTO_IPV6, |
6e23ae2a | 266 | .hooknum = NF_INET_LOCAL_OUT, |
964ddaa1 PM |
267 | .priority = NF_IP6_PRI_CONNTRACK, |
268 | }, | |
12f7a505 PNA |
269 | { |
270 | .hook = ipv6_helper, | |
271 | .owner = THIS_MODULE, | |
272 | .pf = NFPROTO_IPV6, | |
273 | .hooknum = NF_INET_POST_ROUTING, | |
274 | .priority = NF_IP6_PRI_CONNTRACK_HELPER, | |
275 | }, | |
964ddaa1 PM |
276 | { |
277 | .hook = ipv6_confirm, | |
278 | .owner = THIS_MODULE, | |
57750a22 | 279 | .pf = NFPROTO_IPV6, |
6e23ae2a | 280 | .hooknum = NF_INET_POST_ROUTING, |
964ddaa1 PM |
281 | .priority = NF_IP6_PRI_LAST, |
282 | }, | |
12f7a505 PNA |
283 | { |
284 | .hook = ipv6_helper, | |
285 | .owner = THIS_MODULE, | |
286 | .pf = NFPROTO_IPV6, | |
287 | .hooknum = NF_INET_LOCAL_IN, | |
288 | .priority = NF_IP6_PRI_CONNTRACK_HELPER, | |
289 | }, | |
964ddaa1 PM |
290 | { |
291 | .hook = ipv6_confirm, | |
292 | .owner = THIS_MODULE, | |
57750a22 | 293 | .pf = NFPROTO_IPV6, |
6e23ae2a | 294 | .hooknum = NF_INET_LOCAL_IN, |
964ddaa1 PM |
295 | .priority = NF_IP6_PRI_LAST-1, |
296 | }, | |
9fb9cbb1 YK |
297 | }; |
298 | ||
121d1e09 FW |
299 | static int |
300 | ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len) | |
301 | { | |
302 | const struct inet_sock *inet = inet_sk(sk); | |
303 | const struct ipv6_pinfo *inet6 = inet6_sk(sk); | |
304 | const struct nf_conntrack_tuple_hash *h; | |
305 | struct sockaddr_in6 sin6; | |
306 | struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 }; | |
307 | struct nf_conn *ct; | |
308 | ||
309 | tuple.src.u3.in6 = inet6->rcv_saddr; | |
310 | tuple.src.u.tcp.port = inet->inet_sport; | |
311 | tuple.dst.u3.in6 = inet6->daddr; | |
312 | tuple.dst.u.tcp.port = inet->inet_dport; | |
313 | tuple.dst.protonum = sk->sk_protocol; | |
314 | ||
315 | if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) | |
316 | return -ENOPROTOOPT; | |
317 | ||
318 | if (*len < 0 || (unsigned int) *len < sizeof(sin6)) | |
319 | return -EINVAL; | |
320 | ||
321 | h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple); | |
322 | if (!h) { | |
323 | pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n", | |
324 | &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port), | |
325 | &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port)); | |
326 | return -ENOENT; | |
327 | } | |
328 | ||
329 | ct = nf_ct_tuplehash_to_ctrack(h); | |
330 | ||
331 | sin6.sin6_family = AF_INET6; | |
332 | sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port; | |
333 | sin6.sin6_flowinfo = inet6->flow_label & IPV6_FLOWINFO_MASK; | |
334 | memcpy(&sin6.sin6_addr, | |
335 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6, | |
336 | sizeof(sin6.sin6_addr)); | |
121d1e09 FW |
337 | |
338 | nf_ct_put(ct); | |
d3976a53 FW |
339 | |
340 | if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
341 | sin6.sin6_scope_id = sk->sk_bound_dev_if; | |
342 | else | |
343 | sin6.sin6_scope_id = 0; | |
344 | ||
121d1e09 FW |
345 | return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0; |
346 | } | |
347 | ||
07a93626 | 348 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c1d10adb PNA |
349 | |
350 | #include <linux/netfilter/nfnetlink.h> | |
351 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
352 | ||
fdf70832 | 353 | static int ipv6_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
354 | const struct nf_conntrack_tuple *tuple) |
355 | { | |
e549a6b3 DM |
356 | if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4, |
357 | &tuple->src.u3.ip6) || | |
358 | nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4, | |
359 | &tuple->dst.u3.ip6)) | |
360 | goto nla_put_failure; | |
c1d10adb PNA |
361 | return 0; |
362 | ||
df6fb868 | 363 | nla_put_failure: |
c1d10adb PNA |
364 | return -1; |
365 | } | |
366 | ||
f73e924c PM |
367 | static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = { |
368 | [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 }, | |
369 | [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 }, | |
c1d10adb PNA |
370 | }; |
371 | ||
fdf70832 | 372 | static int ipv6_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
373 | struct nf_conntrack_tuple *t) |
374 | { | |
df6fb868 | 375 | if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST]) |
c1d10adb PNA |
376 | return -EINVAL; |
377 | ||
df6fb868 | 378 | memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]), |
c1d10adb | 379 | sizeof(u_int32_t) * 4); |
df6fb868 | 380 | memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]), |
c1d10adb PNA |
381 | sizeof(u_int32_t) * 4); |
382 | ||
383 | return 0; | |
384 | } | |
a400c30e HE |
385 | |
386 | static int ipv6_nlattr_tuple_size(void) | |
387 | { | |
388 | return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1); | |
389 | } | |
c1d10adb PNA |
390 | #endif |
391 | ||
61075af5 | 392 | struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = { |
9fb9cbb1 YK |
393 | .l3proto = PF_INET6, |
394 | .name = "ipv6", | |
395 | .pkt_to_tuple = ipv6_pkt_to_tuple, | |
396 | .invert_tuple = ipv6_invert_tuple, | |
397 | .print_tuple = ipv6_print_tuple, | |
ffc30690 | 398 | .get_l4proto = ipv6_get_l4proto, |
07a93626 | 399 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 400 | .tuple_to_nlattr = ipv6_tuple_to_nlattr, |
a400c30e | 401 | .nlattr_tuple_size = ipv6_nlattr_tuple_size, |
fdf70832 | 402 | .nlattr_to_tuple = ipv6_nlattr_to_tuple, |
f73e924c | 403 | .nla_policy = ipv6_nla_policy, |
c1d10adb | 404 | #endif |
9fb9cbb1 YK |
405 | .me = THIS_MODULE, |
406 | }; | |
407 | ||
32292a7f PM |
408 | MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6)); |
409 | MODULE_LICENSE("GPL"); | |
410 | MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>"); | |
411 | ||
121d1e09 FW |
412 | static struct nf_sockopt_ops so_getorigdst6 = { |
413 | .pf = NFPROTO_IPV6, | |
414 | .get_optmin = IP6T_SO_ORIGINAL_DST, | |
415 | .get_optmax = IP6T_SO_ORIGINAL_DST + 1, | |
416 | .get = ipv6_getorigdst, | |
417 | .owner = THIS_MODULE, | |
418 | }; | |
419 | ||
a7c439d3 | 420 | static int ipv6_net_init(struct net *net) |
9fb9cbb1 YK |
421 | { |
422 | int ret = 0; | |
423 | ||
a7c439d3 G |
424 | ret = nf_conntrack_l4proto_register(net, |
425 | &nf_conntrack_l4proto_tcp6); | |
9fb9cbb1 | 426 | if (ret < 0) { |
a7c439d3 G |
427 | printk(KERN_ERR "nf_conntrack_l4proto_tcp6: protocol register failed\n"); |
428 | goto out; | |
9fb9cbb1 | 429 | } |
a7c439d3 G |
430 | ret = nf_conntrack_l4proto_register(net, |
431 | &nf_conntrack_l4proto_udp6); | |
9fb9cbb1 | 432 | if (ret < 0) { |
a7c439d3 G |
433 | printk(KERN_ERR "nf_conntrack_l4proto_udp6: protocol register failed\n"); |
434 | goto cleanup_tcp6; | |
9fb9cbb1 | 435 | } |
a7c439d3 G |
436 | ret = nf_conntrack_l4proto_register(net, |
437 | &nf_conntrack_l4proto_icmpv6); | |
9fb9cbb1 | 438 | if (ret < 0) { |
a7c439d3 G |
439 | printk(KERN_ERR "nf_conntrack_l4proto_icmp6: protocol register failed\n"); |
440 | goto cleanup_udp6; | |
9fb9cbb1 | 441 | } |
6330750d | 442 | ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv6); |
9fb9cbb1 | 443 | if (ret < 0) { |
6330750d | 444 | pr_err("nf_conntrack_ipv6: pernet registration failed.\n"); |
9fb9cbb1 YK |
445 | goto cleanup_icmpv6; |
446 | } | |
a7c439d3 G |
447 | return 0; |
448 | cleanup_icmpv6: | |
449 | nf_conntrack_l4proto_unregister(net, | |
450 | &nf_conntrack_l4proto_icmpv6); | |
451 | cleanup_udp6: | |
452 | nf_conntrack_l4proto_unregister(net, | |
453 | &nf_conntrack_l4proto_udp6); | |
454 | cleanup_tcp6: | |
455 | nf_conntrack_l4proto_unregister(net, | |
456 | &nf_conntrack_l4proto_tcp6); | |
457 | out: | |
458 | return ret; | |
459 | } | |
9fb9cbb1 | 460 | |
a7c439d3 G |
461 | static void ipv6_net_exit(struct net *net) |
462 | { | |
6330750d | 463 | nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6); |
a7c439d3 G |
464 | nf_conntrack_l4proto_unregister(net, |
465 | &nf_conntrack_l4proto_icmpv6); | |
466 | nf_conntrack_l4proto_unregister(net, | |
467 | &nf_conntrack_l4proto_udp6); | |
468 | nf_conntrack_l4proto_unregister(net, | |
469 | &nf_conntrack_l4proto_tcp6); | |
470 | } | |
471 | ||
472 | static struct pernet_operations ipv6_net_ops = { | |
473 | .init = ipv6_net_init, | |
474 | .exit = ipv6_net_exit, | |
475 | }; | |
476 | ||
477 | static int __init nf_conntrack_l3proto_ipv6_init(void) | |
478 | { | |
479 | int ret = 0; | |
480 | ||
481 | need_conntrack(); | |
482 | nf_defrag_ipv6_enable(); | |
483 | ||
121d1e09 FW |
484 | ret = nf_register_sockopt(&so_getorigdst6); |
485 | if (ret < 0) { | |
486 | pr_err("Unable to register netfilter socket option\n"); | |
487 | return ret; | |
488 | } | |
489 | ||
a7c439d3 G |
490 | ret = register_pernet_subsys(&ipv6_net_ops); |
491 | if (ret < 0) | |
6330750d G |
492 | goto cleanup_sockopt; |
493 | ||
964ddaa1 PM |
494 | ret = nf_register_hooks(ipv6_conntrack_ops, |
495 | ARRAY_SIZE(ipv6_conntrack_ops)); | |
9fb9cbb1 | 496 | if (ret < 0) { |
654d0fbd | 497 | pr_err("nf_conntrack_ipv6: can't register pre-routing defrag " |
9fb9cbb1 | 498 | "hook.\n"); |
6330750d G |
499 | goto cleanup_pernet; |
500 | } | |
501 | ||
502 | ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv6); | |
503 | if (ret < 0) { | |
504 | pr_err("nf_conntrack_ipv6: can't register ipv6 proto.\n"); | |
505 | goto cleanup_hooks; | |
9fb9cbb1 | 506 | } |
9fb9cbb1 YK |
507 | return ret; |
508 | ||
6330750d G |
509 | cleanup_hooks: |
510 | nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops)); | |
a7c439d3 | 511 | cleanup_pernet: |
6330750d G |
512 | unregister_pernet_subsys(&ipv6_net_ops); |
513 | cleanup_sockopt: | |
121d1e09 | 514 | nf_unregister_sockopt(&so_getorigdst6); |
9fb9cbb1 YK |
515 | return ret; |
516 | } | |
517 | ||
65b4b4e8 | 518 | static void __exit nf_conntrack_l3proto_ipv6_fini(void) |
9fb9cbb1 | 519 | { |
32292a7f | 520 | synchronize_net(); |
6330750d | 521 | nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv6); |
32292a7f | 522 | nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops)); |
a7c439d3 | 523 | unregister_pernet_subsys(&ipv6_net_ops); |
121d1e09 | 524 | nf_unregister_sockopt(&so_getorigdst6); |
9fb9cbb1 YK |
525 | } |
526 | ||
65b4b4e8 AM |
527 | module_init(nf_conntrack_l3proto_ipv6_init); |
528 | module_exit(nf_conntrack_l3proto_ipv6_fini); |