kernel: Copy patches from kernel 4.14 to 4.19
[oweals/openwrt.git] / target / linux / generic / backport-4.19 / 349-v4.18-netfilter-nf_flow_table-clean-up-flow_offload_alloc.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Fri, 16 Feb 2018 09:42:32 +0100
3 Subject: [PATCH] netfilter: nf_flow_table: clean up flow_offload_alloc
4
5 Reduce code duplication and make it much easier to read
6
7 Signed-off-by: Felix Fietkau <nbd@nbd.name>
8 ---
9
10 --- a/net/netfilter/nf_flow_table.c
11 +++ b/net/netfilter/nf_flow_table.c
12 @@ -16,6 +16,38 @@ struct flow_offload_entry {
13         struct rcu_head         rcu_head;
14  };
15  
16 +static void
17 +flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
18 +                     struct nf_flow_route *route,
19 +                     enum flow_offload_tuple_dir dir)
20 +{
21 +       struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
22 +       struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
23 +
24 +       ft->dir = dir;
25 +
26 +       switch (ctt->src.l3num) {
27 +       case NFPROTO_IPV4:
28 +               ft->src_v4 = ctt->src.u3.in;
29 +               ft->dst_v4 = ctt->dst.u3.in;
30 +               break;
31 +       case NFPROTO_IPV6:
32 +               ft->src_v6 = ctt->src.u3.in6;
33 +               ft->dst_v6 = ctt->dst.u3.in6;
34 +               break;
35 +       }
36 +
37 +       ft->l3proto = ctt->src.l3num;
38 +       ft->l4proto = ctt->dst.protonum;
39 +       ft->src_port = ctt->src.u.tcp.port;
40 +       ft->dst_port = ctt->dst.u.tcp.port;
41 +
42 +       ft->iifidx = route->tuple[dir].ifindex;
43 +       ft->oifidx = route->tuple[!dir].ifindex;
44 +
45 +       ft->dst_cache = route->tuple[dir].dst;
46 +}
47 +
48  struct flow_offload *
49  flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
50  {
51 @@ -40,65 +72,8 @@ flow_offload_alloc(struct nf_conn *ct, s
52  
53         entry->ct = ct;
54  
55 -       switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num) {
56 -       case NFPROTO_IPV4:
57 -               flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4 =
58 -                       ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in;
59 -               flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4 =
60 -                       ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in;
61 -               flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4 =
62 -                       ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.in;
63 -               flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4 =
64 -                       ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.in;
65 -               break;
66 -       case NFPROTO_IPV6:
67 -               flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6 =
68 -                       ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6;
69 -               flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6 =
70 -                       ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6;
71 -               flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6 =
72 -                       ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.in6;
73 -               flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6 =
74 -                       ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.in6;
75 -               break;
76 -       }
77 -
78 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.l3proto =
79 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
80 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.l4proto =
81 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
82 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.l3proto =
83 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
84 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.l4proto =
85 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
86 -
87 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache =
88 -                 route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst;
89 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache =
90 -                 route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst;
91 -
92 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port =
93 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port;
94 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port =
95 -               ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
96 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port =
97 -               ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.tcp.port;
98 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port =
99 -               ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
100 -
101 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dir =
102 -                                               FLOW_OFFLOAD_DIR_ORIGINAL;
103 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dir =
104 -                                               FLOW_OFFLOAD_DIR_REPLY;
105 -
106 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.iifidx =
107 -               route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].ifindex;
108 -       flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.oifidx =
109 -               route->tuple[FLOW_OFFLOAD_DIR_REPLY].ifindex;
110 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.iifidx =
111 -               route->tuple[FLOW_OFFLOAD_DIR_REPLY].ifindex;
112 -       flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.oifidx =
113 -               route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].ifindex;
114 +       flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
115 +       flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
116  
117         if (ct->status & IPS_SRC_NAT)
118                 flow->flags |= FLOW_OFFLOAD_SNAT;