1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/types.h>
4 #include <linux/netfilter.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/mutex.h>
8 #include <linux/vmalloc.h>
9 #include <linux/stddef.h>
10 #include <linux/err.h>
11 #include <linux/percpu.h>
12 #include <linux/notifier.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_l4proto.h>
18 #include <net/netfilter/nf_conntrack_core.h>
19 #include <net/netfilter/nf_conntrack_bridge.h>
20 #include <net/netfilter/nf_log.h>
23 #include <linux/icmp.h>
24 #include <linux/sysctl.h>
25 #include <net/route.h>
28 #include <linux/netfilter_ipv4.h>
29 #include <linux/netfilter_ipv6.h>
30 #include <linux/netfilter_ipv6/ip6_tables.h>
31 #include <net/netfilter/nf_conntrack_helper.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_seqadj.h>
34 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
35 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
36 #include <net/netfilter/nf_nat_helper.h>
37 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
38 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
40 #include <linux/ipv6.h>
41 #include <linux/in6.h>
43 #include <net/inet_frag.h>
45 static DEFINE_MUTEX(nf_ct_proto_mutex);
49 void nf_l4proto_log_invalid(const struct sk_buff *skb,
50 const struct nf_hook_state *state,
54 struct net *net = state->net;
58 if (net->ct.sysctl_log_invalid != protonum &&
59 net->ct.sysctl_log_invalid != IPPROTO_RAW)
66 nf_log_packet(net, state->pf, 0, skb, state->in, state->out,
67 NULL, "nf_ct_proto_%d: %pV ", protonum, &vaf);
70 EXPORT_SYMBOL_GPL(nf_l4proto_log_invalid);
73 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
74 const struct nf_conn *ct,
75 const struct nf_hook_state *state,
83 if (likely(net->ct.sysctl_log_invalid == 0))
90 nf_l4proto_log_invalid(skb, state,
91 nf_ct_protonum(ct), "%pV", &vaf);
94 EXPORT_SYMBOL_GPL(nf_ct_l4proto_log_invalid);
97 const struct nf_conntrack_l4proto *nf_ct_l4proto_find(u8 l4proto)
100 case IPPROTO_UDP: return &nf_conntrack_l4proto_udp;
101 case IPPROTO_TCP: return &nf_conntrack_l4proto_tcp;
102 case IPPROTO_ICMP: return &nf_conntrack_l4proto_icmp;
103 #ifdef CONFIG_NF_CT_PROTO_DCCP
104 case IPPROTO_DCCP: return &nf_conntrack_l4proto_dccp;
106 #ifdef CONFIG_NF_CT_PROTO_SCTP
107 case IPPROTO_SCTP: return &nf_conntrack_l4proto_sctp;
109 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
110 case IPPROTO_UDPLITE: return &nf_conntrack_l4proto_udplite;
112 #ifdef CONFIG_NF_CT_PROTO_GRE
113 case IPPROTO_GRE: return &nf_conntrack_l4proto_gre;
115 #if IS_ENABLED(CONFIG_IPV6)
116 case IPPROTO_ICMPV6: return &nf_conntrack_l4proto_icmpv6;
117 #endif /* CONFIG_IPV6 */
120 return &nf_conntrack_l4proto_generic;
122 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find);
124 unsigned int nf_confirm(struct sk_buff *skb, unsigned int protoff,
125 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
127 const struct nf_conn_help *help;
129 help = nfct_help(ct);
131 const struct nf_conntrack_helper *helper;
134 /* rcu_read_lock()ed by nf_hook_thresh */
135 helper = rcu_dereference(help->helper);
137 ret = helper->help(skb,
140 if (ret != NF_ACCEPT)
145 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
146 !nf_is_loopback_packet(skb)) {
147 if (!nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) {
148 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
153 /* We've seen it coming out the other side: confirm it */
154 return nf_conntrack_confirm(skb);
156 EXPORT_SYMBOL_GPL(nf_confirm);
158 static bool in_vrf_postrouting(const struct nf_hook_state *state)
160 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
161 if (state->hook == NF_INET_POST_ROUTING &&
162 netif_is_l3_master(state->out))
168 static unsigned int ipv4_confirm(void *priv,
170 const struct nf_hook_state *state)
172 enum ip_conntrack_info ctinfo;
175 ct = nf_ct_get(skb, &ctinfo);
176 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
177 return nf_conntrack_confirm(skb);
179 if (in_vrf_postrouting(state))
182 return nf_confirm(skb,
183 skb_network_offset(skb) + ip_hdrlen(skb),
187 static unsigned int ipv4_conntrack_in(void *priv,
189 const struct nf_hook_state *state)
191 return nf_conntrack_in(skb, state);
194 static unsigned int ipv4_conntrack_local(void *priv,
196 const struct nf_hook_state *state)
198 if (ip_is_fragment(ip_hdr(skb))) { /* IP_NODEFRAG setsockopt set */
199 enum ip_conntrack_info ctinfo;
200 struct nf_conn *tmpl;
202 tmpl = nf_ct_get(skb, &ctinfo);
203 if (tmpl && nf_ct_is_template(tmpl)) {
204 /* when skipping ct, clear templates to avoid fooling
205 * later targets/matches
213 return nf_conntrack_in(skb, state);
216 /* Connection tracking may drop packets, but never alters them, so
217 * make it the first hook.
219 static const struct nf_hook_ops ipv4_conntrack_ops[] = {
221 .hook = ipv4_conntrack_in,
223 .hooknum = NF_INET_PRE_ROUTING,
224 .priority = NF_IP_PRI_CONNTRACK,
227 .hook = ipv4_conntrack_local,
229 .hooknum = NF_INET_LOCAL_OUT,
230 .priority = NF_IP_PRI_CONNTRACK,
233 .hook = ipv4_confirm,
235 .hooknum = NF_INET_POST_ROUTING,
236 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
239 .hook = ipv4_confirm,
241 .hooknum = NF_INET_LOCAL_IN,
242 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
246 /* Fast function for those who don't want to parse /proc (and I don't
248 * Reversing the socket's dst/src point of view gives us the reply
252 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
254 const struct inet_sock *inet = inet_sk(sk);
255 const struct nf_conntrack_tuple_hash *h;
256 struct nf_conntrack_tuple tuple;
258 memset(&tuple, 0, sizeof(tuple));
261 tuple.src.u3.ip = inet->inet_rcv_saddr;
262 tuple.src.u.tcp.port = inet->inet_sport;
263 tuple.dst.u3.ip = inet->inet_daddr;
264 tuple.dst.u.tcp.port = inet->inet_dport;
265 tuple.src.l3num = PF_INET;
266 tuple.dst.protonum = sk->sk_protocol;
269 /* We only do TCP and SCTP at the moment: is there a better way? */
270 if (tuple.dst.protonum != IPPROTO_TCP &&
271 tuple.dst.protonum != IPPROTO_SCTP) {
272 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
276 if ((unsigned int)*len < sizeof(struct sockaddr_in)) {
277 pr_debug("SO_ORIGINAL_DST: len %d not %zu\n",
278 *len, sizeof(struct sockaddr_in));
282 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple);
284 struct sockaddr_in sin;
285 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
287 sin.sin_family = AF_INET;
288 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
289 .tuple.dst.u.tcp.port;
290 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
292 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
294 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
295 &sin.sin_addr.s_addr, ntohs(sin.sin_port));
297 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
302 pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n",
303 &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port),
304 &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port));
308 static struct nf_sockopt_ops so_getorigdst = {
310 .get_optmin = SO_ORIGINAL_DST,
311 .get_optmax = SO_ORIGINAL_DST + 1,
313 .owner = THIS_MODULE,
316 #if IS_ENABLED(CONFIG_IPV6)
318 ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
320 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
321 const struct ipv6_pinfo *inet6 = inet6_sk(sk);
322 const struct inet_sock *inet = inet_sk(sk);
323 const struct nf_conntrack_tuple_hash *h;
324 struct sockaddr_in6 sin6;
330 tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
331 tuple.src.u.tcp.port = inet->inet_sport;
332 tuple.dst.u3.in6 = sk->sk_v6_daddr;
333 tuple.dst.u.tcp.port = inet->inet_dport;
334 tuple.dst.protonum = sk->sk_protocol;
335 bound_dev_if = sk->sk_bound_dev_if;
336 flow_label = inet6->flow_label;
339 if (tuple.dst.protonum != IPPROTO_TCP &&
340 tuple.dst.protonum != IPPROTO_SCTP)
343 if (*len < 0 || (unsigned int)*len < sizeof(sin6))
346 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple);
348 pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n",
349 &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
350 &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port));
354 ct = nf_ct_tuplehash_to_ctrack(h);
356 sin6.sin6_family = AF_INET6;
357 sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
358 sin6.sin6_flowinfo = flow_label & IPV6_FLOWINFO_MASK;
359 memcpy(&sin6.sin6_addr,
360 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
361 sizeof(sin6.sin6_addr));
364 sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr, bound_dev_if);
365 return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
368 static struct nf_sockopt_ops so_getorigdst6 = {
370 .get_optmin = IP6T_SO_ORIGINAL_DST,
371 .get_optmax = IP6T_SO_ORIGINAL_DST + 1,
372 .get = ipv6_getorigdst,
373 .owner = THIS_MODULE,
376 static unsigned int ipv6_confirm(void *priv,
378 const struct nf_hook_state *state)
381 enum ip_conntrack_info ctinfo;
382 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
386 ct = nf_ct_get(skb, &ctinfo);
387 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
388 return nf_conntrack_confirm(skb);
390 if (in_vrf_postrouting(state))
393 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
395 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
396 pr_debug("proto header not found\n");
397 return nf_conntrack_confirm(skb);
400 return nf_confirm(skb, protoff, ct, ctinfo);
403 static unsigned int ipv6_conntrack_in(void *priv,
405 const struct nf_hook_state *state)
407 return nf_conntrack_in(skb, state);
410 static unsigned int ipv6_conntrack_local(void *priv,
412 const struct nf_hook_state *state)
414 return nf_conntrack_in(skb, state);
417 static const struct nf_hook_ops ipv6_conntrack_ops[] = {
419 .hook = ipv6_conntrack_in,
421 .hooknum = NF_INET_PRE_ROUTING,
422 .priority = NF_IP6_PRI_CONNTRACK,
425 .hook = ipv6_conntrack_local,
427 .hooknum = NF_INET_LOCAL_OUT,
428 .priority = NF_IP6_PRI_CONNTRACK,
431 .hook = ipv6_confirm,
433 .hooknum = NF_INET_POST_ROUTING,
434 .priority = NF_IP6_PRI_LAST,
437 .hook = ipv6_confirm,
439 .hooknum = NF_INET_LOCAL_IN,
440 .priority = NF_IP6_PRI_LAST - 1,
445 static int nf_ct_tcp_fixup(struct nf_conn *ct, void *_nfproto)
447 u8 nfproto = (unsigned long)_nfproto;
449 if (nf_ct_l3num(ct) != nfproto)
452 if (nf_ct_protonum(ct) == IPPROTO_TCP &&
453 ct->proto.tcp.state == TCP_CONNTRACK_ESTABLISHED) {
454 ct->proto.tcp.seen[0].td_maxwin = 0;
455 ct->proto.tcp.seen[1].td_maxwin = 0;
461 static struct nf_ct_bridge_info *nf_ct_bridge_info;
463 static int nf_ct_netns_do_get(struct net *net, u8 nfproto)
465 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
466 bool fixup_needed = false, retry = true;
469 mutex_lock(&nf_ct_proto_mutex);
474 if (cnet->users4 > 1)
476 err = nf_defrag_ipv4_enable(net);
482 err = nf_register_net_hooks(net, ipv4_conntrack_ops,
483 ARRAY_SIZE(ipv4_conntrack_ops));
489 #if IS_ENABLED(CONFIG_IPV6)
492 if (cnet->users6 > 1)
494 err = nf_defrag_ipv6_enable(net);
500 err = nf_register_net_hooks(net, ipv6_conntrack_ops,
501 ARRAY_SIZE(ipv6_conntrack_ops));
509 if (!nf_ct_bridge_info) {
514 mutex_unlock(&nf_ct_proto_mutex);
515 request_module("nf_conntrack_bridge");
519 if (!try_module_get(nf_ct_bridge_info->me)) {
523 cnet->users_bridge++;
524 if (cnet->users_bridge > 1)
527 err = nf_register_net_hooks(net, nf_ct_bridge_info->ops,
528 nf_ct_bridge_info->ops_size);
530 cnet->users_bridge = 0;
539 mutex_unlock(&nf_ct_proto_mutex);
542 nf_ct_iterate_cleanup_net(net, nf_ct_tcp_fixup,
543 (void *)(unsigned long)nfproto, 0, 0);
548 static void nf_ct_netns_do_put(struct net *net, u8 nfproto)
550 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
552 mutex_lock(&nf_ct_proto_mutex);
555 if (cnet->users4 && (--cnet->users4 == 0)) {
556 nf_unregister_net_hooks(net, ipv4_conntrack_ops,
557 ARRAY_SIZE(ipv4_conntrack_ops));
558 nf_defrag_ipv4_disable(net);
561 #if IS_ENABLED(CONFIG_IPV6)
563 if (cnet->users6 && (--cnet->users6 == 0)) {
564 nf_unregister_net_hooks(net, ipv6_conntrack_ops,
565 ARRAY_SIZE(ipv6_conntrack_ops));
566 nf_defrag_ipv6_disable(net);
571 if (!nf_ct_bridge_info)
573 if (cnet->users_bridge && (--cnet->users_bridge == 0))
574 nf_unregister_net_hooks(net, nf_ct_bridge_info->ops,
575 nf_ct_bridge_info->ops_size);
577 module_put(nf_ct_bridge_info->me);
580 mutex_unlock(&nf_ct_proto_mutex);
583 static int nf_ct_netns_inet_get(struct net *net)
587 err = nf_ct_netns_do_get(net, NFPROTO_IPV4);
588 #if IS_ENABLED(CONFIG_IPV6)
591 err = nf_ct_netns_do_get(net, NFPROTO_IPV6);
597 nf_ct_netns_put(net, NFPROTO_IPV4);
603 int nf_ct_netns_get(struct net *net, u8 nfproto)
609 err = nf_ct_netns_inet_get(net);
612 err = nf_ct_netns_do_get(net, NFPROTO_BRIDGE);
616 err = nf_ct_netns_inet_get(net);
618 nf_ct_netns_put(net, NFPROTO_BRIDGE);
623 err = nf_ct_netns_do_get(net, nfproto);
628 EXPORT_SYMBOL_GPL(nf_ct_netns_get);
630 void nf_ct_netns_put(struct net *net, uint8_t nfproto)
634 nf_ct_netns_do_put(net, NFPROTO_BRIDGE);
637 nf_ct_netns_do_put(net, NFPROTO_IPV4);
638 nf_ct_netns_do_put(net, NFPROTO_IPV6);
641 nf_ct_netns_do_put(net, nfproto);
645 EXPORT_SYMBOL_GPL(nf_ct_netns_put);
647 void nf_ct_bridge_register(struct nf_ct_bridge_info *info)
649 WARN_ON(nf_ct_bridge_info);
650 mutex_lock(&nf_ct_proto_mutex);
651 nf_ct_bridge_info = info;
652 mutex_unlock(&nf_ct_proto_mutex);
654 EXPORT_SYMBOL_GPL(nf_ct_bridge_register);
656 void nf_ct_bridge_unregister(struct nf_ct_bridge_info *info)
658 WARN_ON(!nf_ct_bridge_info);
659 mutex_lock(&nf_ct_proto_mutex);
660 nf_ct_bridge_info = NULL;
661 mutex_unlock(&nf_ct_proto_mutex);
663 EXPORT_SYMBOL_GPL(nf_ct_bridge_unregister);
665 int nf_conntrack_proto_init(void)
669 ret = nf_register_sockopt(&so_getorigdst);
673 #if IS_ENABLED(CONFIG_IPV6)
674 ret = nf_register_sockopt(&so_getorigdst6);
676 goto cleanup_sockopt;
681 #if IS_ENABLED(CONFIG_IPV6)
683 nf_unregister_sockopt(&so_getorigdst);
688 void nf_conntrack_proto_fini(void)
690 nf_unregister_sockopt(&so_getorigdst);
691 #if IS_ENABLED(CONFIG_IPV6)
692 nf_unregister_sockopt(&so_getorigdst6);
696 void nf_conntrack_proto_pernet_init(struct net *net)
698 nf_conntrack_generic_init_net(net);
699 nf_conntrack_udp_init_net(net);
700 nf_conntrack_tcp_init_net(net);
701 nf_conntrack_icmp_init_net(net);
702 #if IS_ENABLED(CONFIG_IPV6)
703 nf_conntrack_icmpv6_init_net(net);
705 #ifdef CONFIG_NF_CT_PROTO_DCCP
706 nf_conntrack_dccp_init_net(net);
708 #ifdef CONFIG_NF_CT_PROTO_SCTP
709 nf_conntrack_sctp_init_net(net);
711 #ifdef CONFIG_NF_CT_PROTO_GRE
712 nf_conntrack_gre_init_net(net);
716 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
717 &nf_conntrack_htable_size, 0600);
719 MODULE_ALIAS("ip_conntrack");
720 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
721 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
722 MODULE_LICENSE("GPL");