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
5 Reduce code duplication and make it much easier to read
7 Signed-off-by: Felix Fietkau <nbd@nbd.name>
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;
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)
21 + struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
22 + struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
26 + switch (ctt->src.l3num) {
28 + ft->src_v4 = ctt->src.u3.in;
29 + ft->dst_v4 = ctt->dst.u3.in;
32 + ft->src_v6 = ctt->src.u3.in6;
33 + ft->dst_v6 = ctt->dst.u3.in6;
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;
42 + ft->iifidx = route->tuple[dir].ifindex;
43 + ft->oifidx = route->tuple[!dir].ifindex;
45 + ft->dst_cache = route->tuple[dir].dst;
49 flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
51 @@ -40,65 +72,8 @@ flow_offload_alloc(struct nf_conn *ct, s
55 - switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num) {
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;
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;
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;
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;
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;
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;
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);
117 if (ct->status & IPS_SRC_NAT)
118 flow->flags |= FLOW_OFFLOAD_SNAT;