Skip to content

Commit 1ec4310

Browse files
gentoo-rootgregkh
authored andcommitted
netfilter: socket: Lookup orig tuple for IPv6 SNAT
commit 932b32ffd7604fb00b5c57e239a3cc4d901ccf6e upstream. nf_sk_lookup_slow_v4 does the conntrack lookup for IPv4 packets to restore the original 5-tuple in case of SNAT, to be able to find the right socket (if any). Then socket_match() can correctly check whether the socket was transparent. However, the IPv6 counterpart (nf_sk_lookup_slow_v6) lacks this conntrack lookup, making xt_socket fail to match on the socket when the packet was SNATed. Add the same logic to nf_sk_lookup_slow_v6. IPv6 SNAT is used in Kubernetes clusters for pod-to-world packets, as pods' addresses are in the fd00::/8 ULA subnet and need to be replaced with the node's external address. Cilium leverages Envoy to enforce L7 policies, and Envoy uses transparent sockets. Cilium inserts an iptables prerouting rule that matches on `-m socket --transparent` and redirects the packets to localhost, but it fails to match SNATed IPv6 packets due to that missing conntrack lookup. Closes: cilium/cilium#37932 Fixes: eb31628 ("netfilter: nf_tables: Add support for IPv6 NAT") Signed-off-by: Maxim Mikityanskiy <[email protected]> Reviewed-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 04805ef commit 1ec4310

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

net/ipv6/netfilter/nf_socket_ipv6.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,
103103
struct sk_buff *data_skb = NULL;
104104
int doff = 0;
105105
int thoff = 0, tproto;
106+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
107+
enum ip_conntrack_info ctinfo;
108+
struct nf_conn const *ct;
109+
#endif
106110

107111
tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
108112
if (tproto < 0) {
@@ -136,6 +140,25 @@ struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,
136140
return NULL;
137141
}
138142

143+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
144+
/* Do the lookup with the original socket address in
145+
* case this is a reply packet of an established
146+
* SNAT-ted connection.
147+
*/
148+
ct = nf_ct_get(skb, &ctinfo);
149+
if (ct &&
150+
((tproto != IPPROTO_ICMPV6 &&
151+
ctinfo == IP_CT_ESTABLISHED_REPLY) ||
152+
(tproto == IPPROTO_ICMPV6 &&
153+
ctinfo == IP_CT_RELATED_REPLY)) &&
154+
(ct->status & IPS_SRC_NAT_DONE)) {
155+
daddr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6;
156+
dport = (tproto == IPPROTO_TCP) ?
157+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
158+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
159+
}
160+
#endif
161+
139162
return nf_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
140163
sport, dport, indev);
141164
}

0 commit comments

Comments
 (0)