2 * Copyright (C)2004 USAGI/WIDE Project
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.
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
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>
20 #include <net/inet_frag.h>
22 #include <linux/netfilter_bridge.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <linux/netfilter_ipv6/ip6_tables.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_l3proto.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <net/netfilter/nf_conntrack_seqadj.h>
32 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
33 #include <net/netfilter/nf_nat_helper.h>
34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
35 #include <net/netfilter/nf_log.h>
37 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
38 struct nf_conntrack_tuple *tuple)
43 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
44 sizeof(_addrs), _addrs);
48 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
49 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
54 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
57 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
58 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
63 static void ipv6_print_tuple(struct seq_file *s,
64 const struct nf_conntrack_tuple *tuple)
66 seq_printf(s, "src=%pI6 dst=%pI6 ",
67 tuple->src.u3.ip6, tuple->dst.u3.ip6);
70 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
71 unsigned int *dataoff, u_int8_t *protonum)
73 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
78 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
79 &nexthdr, sizeof(nexthdr)) != 0) {
80 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
83 protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
85 * (protoff == skb->len) means the packet has not data, just
86 * IPv6 and possibly extensions headers, but it is tracked anyway
88 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
89 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
98 static unsigned int ipv6_helper(void *priv,
100 const struct nf_hook_state *state)
103 const struct nf_conn_help *help;
104 const struct nf_conntrack_helper *helper;
105 enum ip_conntrack_info ctinfo;
110 /* This is where we call the helper: as the packet goes out. */
111 ct = nf_ct_get(skb, &ctinfo);
112 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
115 help = nfct_help(ct);
118 /* rcu_read_lock()ed by nf_hook_thresh */
119 helper = rcu_dereference(help->helper);
123 nexthdr = ipv6_hdr(skb)->nexthdr;
124 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
126 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
127 pr_debug("proto header not found\n");
131 return helper->help(skb, protoff, ct, ctinfo);
134 static unsigned int ipv6_confirm(void *priv,
136 const struct nf_hook_state *state)
139 enum ip_conntrack_info ctinfo;
140 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
144 ct = nf_ct_get(skb, &ctinfo);
145 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
148 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
150 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
151 pr_debug("proto header not found\n");
155 /* adjust seqs for loopback traffic only in outgoing direction */
156 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
157 !nf_is_loopback_packet(skb)) {
158 if (!nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) {
159 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
164 /* We've seen it coming out the other side: confirm it */
165 return nf_conntrack_confirm(skb);
168 static unsigned int ipv6_conntrack_in(void *priv,
170 const struct nf_hook_state *state)
172 return nf_conntrack_in(state->net, PF_INET6, state->hook, skb);
175 static unsigned int ipv6_conntrack_local(void *priv,
177 const struct nf_hook_state *state)
179 /* root is playing with raw sockets. */
180 if (skb->len < sizeof(struct ipv6hdr)) {
181 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
184 return nf_conntrack_in(state->net, PF_INET6, state->hook, skb);
187 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
189 .hook = ipv6_conntrack_in,
191 .hooknum = NF_INET_PRE_ROUTING,
192 .priority = NF_IP6_PRI_CONNTRACK,
195 .hook = ipv6_conntrack_local,
197 .hooknum = NF_INET_LOCAL_OUT,
198 .priority = NF_IP6_PRI_CONNTRACK,
203 .hooknum = NF_INET_POST_ROUTING,
204 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
207 .hook = ipv6_confirm,
209 .hooknum = NF_INET_POST_ROUTING,
210 .priority = NF_IP6_PRI_LAST,
215 .hooknum = NF_INET_LOCAL_IN,
216 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
219 .hook = ipv6_confirm,
221 .hooknum = NF_INET_LOCAL_IN,
222 .priority = NF_IP6_PRI_LAST-1,
227 ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
229 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
230 const struct ipv6_pinfo *inet6 = inet6_sk(sk);
231 const struct inet_sock *inet = inet_sk(sk);
232 const struct nf_conntrack_tuple_hash *h;
233 struct sockaddr_in6 sin6;
239 tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
240 tuple.src.u.tcp.port = inet->inet_sport;
241 tuple.dst.u3.in6 = sk->sk_v6_daddr;
242 tuple.dst.u.tcp.port = inet->inet_dport;
243 tuple.dst.protonum = sk->sk_protocol;
244 bound_dev_if = sk->sk_bound_dev_if;
245 flow_label = inet6->flow_label;
248 if (tuple.dst.protonum != IPPROTO_TCP &&
249 tuple.dst.protonum != IPPROTO_SCTP)
252 if (*len < 0 || (unsigned int) *len < sizeof(sin6))
255 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple);
257 pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n",
258 &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
259 &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port));
263 ct = nf_ct_tuplehash_to_ctrack(h);
265 sin6.sin6_family = AF_INET6;
266 sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
267 sin6.sin6_flowinfo = flow_label & IPV6_FLOWINFO_MASK;
268 memcpy(&sin6.sin6_addr,
269 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
270 sizeof(sin6.sin6_addr));
273 sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr, bound_dev_if);
274 return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
277 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
279 #include <linux/netfilter/nfnetlink.h>
280 #include <linux/netfilter/nfnetlink_conntrack.h>
282 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
283 const struct nf_conntrack_tuple *tuple)
285 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
286 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
287 goto nla_put_failure;
294 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
295 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
296 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
299 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
300 struct nf_conntrack_tuple *t)
302 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
305 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
306 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
311 static int ipv6_nlattr_tuple_size(void)
313 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
317 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
320 .pkt_to_tuple = ipv6_pkt_to_tuple,
321 .invert_tuple = ipv6_invert_tuple,
322 .print_tuple = ipv6_print_tuple,
323 .get_l4proto = ipv6_get_l4proto,
324 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
325 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
326 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
327 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
328 .nla_policy = ipv6_nla_policy,
333 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
334 MODULE_LICENSE("GPL");
335 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
337 static struct nf_sockopt_ops so_getorigdst6 = {
339 .get_optmin = IP6T_SO_ORIGINAL_DST,
340 .get_optmax = IP6T_SO_ORIGINAL_DST + 1,
341 .get = ipv6_getorigdst,
342 .owner = THIS_MODULE,
345 static int ipv6_net_init(struct net *net)
349 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp6);
351 pr_err("nf_conntrack_tcp6: pernet registration failed\n");
354 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp6);
356 pr_err("nf_conntrack_udp6: pernet registration failed\n");
359 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmpv6);
361 pr_err("nf_conntrack_icmp6: pernet registration failed\n");
364 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv6);
366 pr_err("nf_conntrack_ipv6: pernet registration failed.\n");
371 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
373 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
375 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
380 static void ipv6_net_exit(struct net *net)
382 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
383 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
384 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
385 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
388 static struct pernet_operations ipv6_net_ops = {
389 .init = ipv6_net_init,
390 .exit = ipv6_net_exit,
393 static int __init nf_conntrack_l3proto_ipv6_init(void)
398 nf_defrag_ipv6_enable();
400 ret = nf_register_sockopt(&so_getorigdst6);
402 pr_err("Unable to register netfilter socket option\n");
406 ret = register_pernet_subsys(&ipv6_net_ops);
408 goto cleanup_sockopt;
410 ret = nf_register_hooks(ipv6_conntrack_ops,
411 ARRAY_SIZE(ipv6_conntrack_ops));
413 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
418 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp6);
420 pr_err("nf_conntrack_ipv6: can't register tcp6 proto.\n");
424 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp6);
426 pr_err("nf_conntrack_ipv6: can't register udp6 proto.\n");
430 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmpv6);
432 pr_err("nf_conntrack_ipv6: can't register icmpv6 proto.\n");
436 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv6);
438 pr_err("nf_conntrack_ipv6: can't register ipv6 proto.\n");
444 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
446 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
448 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
450 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
452 unregister_pernet_subsys(&ipv6_net_ops);
454 nf_unregister_sockopt(&so_getorigdst6);
458 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
461 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
462 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
463 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
464 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
465 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
466 unregister_pernet_subsys(&ipv6_net_ops);
467 nf_unregister_sockopt(&so_getorigdst6);
470 module_init(nf_conntrack_l3proto_ipv6_init);
471 module_exit(nf_conntrack_l3proto_ipv6_fini);