kernel: Make the patches apply on top of 4.19
[openwrt/staging/chunkeey.git] / target / linux / generic / backport-4.19 / 366-netfilter-nf_flow_table-clean-up-and-fix-dst-handlin.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Thu, 15 Mar 2018 18:21:43 +0100
3 Subject: [PATCH] netfilter: nf_flow_table: clean up and fix dst handling
4
5 dst handling in the code is inconsistent and possibly wrong. In my test,
6 skb_dst(skb) holds the dst entry after routing but before NAT, so the
7 code could possibly return the same dst entry for both directions of a
8 connection.
9 Additionally, there was some confusion over the dst entry vs the address
10 passed as parameter to rt_nexthop/rt6_nexthop.
11
12 Do an explicit dst lookup for both ends of the connection and always use
13 the source address for it. When running the IP hook, use the dst entry
14 for the opposite direction for determining the route.
15
16 Signed-off-by: Felix Fietkau <nbd@nbd.name>
17 ---
18
19 --- a/net/netfilter/nf_flow_table_ip.c
20 +++ b/net/netfilter/nf_flow_table_ip.c
21 @@ -241,7 +241,7 @@ nf_flow_offload_ip_hook(void *priv, stru
22
23 dir = tuplehash->tuple.dir;
24 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
25 - rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
26 + rt = (struct rtable *)flow->tuplehash[!dir].tuple.dst_cache;
27
28 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)) &&
29 (ip_hdr(skb)->frag_off & htons(IP_DF)) != 0)
30 @@ -459,7 +459,7 @@ nf_flow_offload_ipv6_hook(void *priv, st
31
32 dir = tuplehash->tuple.dir;
33 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
34 - rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
35 + rt = (struct rt6_info *)flow->tuplehash[!dir].tuple.dst_cache;
36
37 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
38 return NF_ACCEPT;
39 --- a/net/netfilter/nft_flow_offload.c
40 +++ b/net/netfilter/nft_flow_offload.c
41 @@ -17,27 +17,38 @@ struct nft_flow_offload {
42 struct nft_flowtable *flowtable;
43 };
44
45 -static int nft_flow_route(const struct nft_pktinfo *pkt,
46 - const struct nf_conn *ct,
47 - struct nf_flow_route *route,
48 - enum ip_conntrack_dir dir)
49 +static struct dst_entry *
50 +nft_flow_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
51 + const struct nft_pktinfo *pkt)
52 {
53 - struct dst_entry *this_dst = skb_dst(pkt->skb);
54 - struct dst_entry *other_dst = NULL;
55 + struct dst_entry *dst;
56 struct flowi fl;
57
58 memset(&fl, 0, sizeof(fl));
59 switch (nft_pf(pkt)) {
60 case NFPROTO_IPV4:
61 - fl.u.ip4.daddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
62 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
63 break;
64 case NFPROTO_IPV6:
65 - fl.u.ip6.daddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
66 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
67 break;
68 }
69
70 - nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
71 - if (!other_dst)
72 + nf_route(nft_net(pkt), &dst, &fl, false, nft_pf(pkt));
73 +
74 + return dst;
75 +}
76 +
77 +static int nft_flow_route(const struct nft_pktinfo *pkt,
78 + const struct nf_conn *ct,
79 + struct nf_flow_route *route,
80 + enum ip_conntrack_dir dir)
81 +{
82 + struct dst_entry *this_dst, *other_dst;
83 +
84 + this_dst = nft_flow_dst(ct, dir, pkt);
85 + other_dst = nft_flow_dst(ct, !dir, pkt);
86 + if (!this_dst || !other_dst)
87 return -ENOENT;
88
89 route->tuple[dir].dst = this_dst;