1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <net/netfilter/nf_tproxy.h>
3 #include <linux/module.h>
4 #include <net/inet6_hashtables.h>
5 #include <net/addrconf.h>
9 const struct in6_addr *
10 nf_tproxy_laddr6(struct sk_buff *skb, const struct in6_addr *user_laddr,
11 const struct in6_addr *daddr)
13 struct inet6_dev *indev;
14 struct inet6_ifaddr *ifa;
15 struct in6_addr *laddr;
17 if (!ipv6_addr_any(user_laddr))
21 indev = __in6_dev_get(skb->dev);
23 read_lock_bh(&indev->lock);
24 list_for_each_entry(ifa, &indev->addr_list, if_list) {
25 if (ifa->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
31 read_unlock_bh(&indev->lock);
34 return laddr ? laddr : daddr;
36 EXPORT_SYMBOL_GPL(nf_tproxy_laddr6);
39 nf_tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
41 const struct in6_addr *laddr,
45 const struct ipv6hdr *iph = ipv6_hdr(skb);
46 struct tcphdr _hdr, *hp;
48 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
50 inet_twsk_put(inet_twsk(sk));
54 if (hp->syn && !hp->rst && !hp->ack && !hp->fin) {
55 /* SYN to a TIME_WAIT socket, we'd rather redirect it
56 * to a listener socket if there's one */
59 sk2 = nf_tproxy_get_sock_v6(net, skb, thoff, tproto,
61 nf_tproxy_laddr6(skb, laddr, &iph->daddr),
63 lport ? lport : hp->dest,
64 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
66 nf_tproxy_twsk_deschedule_put(inet_twsk(sk));
73 EXPORT_SYMBOL_GPL(nf_tproxy_handle_time_wait6);
76 nf_tproxy_get_sock_v6(struct net *net, struct sk_buff *skb, int thoff,
78 const struct in6_addr *saddr, const struct in6_addr *daddr,
79 const __be16 sport, const __be16 dport,
80 const struct net_device *in,
81 const enum nf_tproxy_lookup_t lookup_type)
87 struct tcphdr _hdr, *hp;
89 hp = skb_header_pointer(skb, thoff,
90 sizeof(struct tcphdr), &_hdr);
94 switch (lookup_type) {
95 case NF_TPROXY_LOOKUP_LISTENER:
96 sk = inet6_lookup_listener(net, &tcp_hashinfo, skb,
97 thoff + __tcp_hdrlen(hp),
102 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
104 /* NOTE: we return listeners even if bound to
105 * 0.0.0.0, those are filtered out in
106 * xt_socket, since xt_TPROXY needs 0 bound
110 case NF_TPROXY_LOOKUP_ESTABLISHED:
111 sk = __inet6_lookup_established(net, &tcp_hashinfo,
112 saddr, sport, daddr, ntohs(dport),
121 sk = udp6_lib_lookup(net, saddr, sport, daddr, dport,
124 int connected = (sk->sk_state == TCP_ESTABLISHED);
125 int wildcard = ipv6_addr_any(&sk->sk_v6_rcv_saddr);
127 /* NOTE: we return listeners even if bound to
128 * 0.0.0.0, those are filtered out in
129 * xt_socket, since xt_TPROXY needs 0 bound
132 if ((lookup_type == NF_TPROXY_LOOKUP_ESTABLISHED && (!connected || wildcard)) ||
133 (lookup_type == NF_TPROXY_LOOKUP_LISTENER && connected)) {
144 pr_debug("tproxy socket lookup: proto %u %pI6:%u -> %pI6:%u, lookup type: %d, sock %p\n",
145 protocol, saddr, ntohs(sport), daddr, ntohs(dport), lookup_type, sk);
149 EXPORT_SYMBOL_GPL(nf_tproxy_get_sock_v6);
151 MODULE_LICENSE("GPL");
152 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
153 MODULE_DESCRIPTION("Netfilter IPv6 transparent proxy support");