1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 17 Feb 2018 11:49:44 +0100
3 Subject: [PATCH] netfilter: nf_flow_table: move ipv4 offload hook code to
6 Allows some minor code sharing with the ipv6 hook code and is also
7 useful as preparation for adding iptables support for offload
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 create mode 100644 net/netfilter/nf_flow_table_ip.c
13 --- a/net/ipv4/netfilter/nf_flow_table_ipv4.c
14 +++ b/net/ipv4/netfilter/nf_flow_table_ipv4.c
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/netfilter.h>
19 -#include <linux/rhashtable.h>
20 -#include <linux/ip.h>
21 -#include <linux/netdevice.h>
23 -#include <net/neighbour.h>
24 #include <net/netfilter/nf_flow_table.h>
25 #include <net/netfilter/nf_tables.h>
26 -/* For layer 4 checksum field offset. */
27 -#include <linux/tcp.h>
28 -#include <linux/udp.h>
30 -static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
31 - __be32 addr, __be32 new_addr)
33 - struct tcphdr *tcph;
35 - if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
36 - skb_try_make_writable(skb, thoff + sizeof(*tcph)))
39 - tcph = (void *)(skb_network_header(skb) + thoff);
40 - inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
45 -static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
46 - __be32 addr, __be32 new_addr)
48 - struct udphdr *udph;
50 - if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
51 - skb_try_make_writable(skb, thoff + sizeof(*udph)))
54 - udph = (void *)(skb_network_header(skb) + thoff);
55 - if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
56 - inet_proto_csum_replace4(&udph->check, skb, addr,
59 - udph->check = CSUM_MANGLED_0;
65 -static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
66 - unsigned int thoff, __be32 addr,
69 - switch (iph->protocol) {
71 - if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
75 - if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
83 -static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
84 - struct iphdr *iph, unsigned int thoff,
85 - enum flow_offload_tuple_dir dir)
87 - __be32 addr, new_addr;
90 - case FLOW_OFFLOAD_DIR_ORIGINAL:
92 - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
93 - iph->saddr = new_addr;
95 - case FLOW_OFFLOAD_DIR_REPLY:
97 - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
98 - iph->daddr = new_addr;
103 - csum_replace4(&iph->check, addr, new_addr);
105 - return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
108 -static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
109 - struct iphdr *iph, unsigned int thoff,
110 - enum flow_offload_tuple_dir dir)
112 - __be32 addr, new_addr;
115 - case FLOW_OFFLOAD_DIR_ORIGINAL:
117 - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
118 - iph->daddr = new_addr;
120 - case FLOW_OFFLOAD_DIR_REPLY:
122 - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
123 - iph->saddr = new_addr;
129 - return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
132 -static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
133 - enum flow_offload_tuple_dir dir)
135 - struct iphdr *iph = ip_hdr(skb);
136 - unsigned int thoff = iph->ihl * 4;
138 - if (flow->flags & FLOW_OFFLOAD_SNAT &&
139 - (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
140 - nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
142 - if (flow->flags & FLOW_OFFLOAD_DNAT &&
143 - (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
144 - nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
150 -static bool ip_has_options(unsigned int thoff)
152 - return thoff != sizeof(struct iphdr);
155 -static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
156 - struct flow_offload_tuple *tuple)
158 - struct flow_ports *ports;
159 - unsigned int thoff;
162 - if (!pskb_may_pull(skb, sizeof(*iph)))
166 - thoff = iph->ihl * 4;
168 - if (ip_is_fragment(iph) ||
169 - unlikely(ip_has_options(thoff)))
172 - if (iph->protocol != IPPROTO_TCP &&
173 - iph->protocol != IPPROTO_UDP)
176 - thoff = iph->ihl * 4;
177 - if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
180 - ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
182 - tuple->src_v4.s_addr = iph->saddr;
183 - tuple->dst_v4.s_addr = iph->daddr;
184 - tuple->src_port = ports->source;
185 - tuple->dst_port = ports->dest;
186 - tuple->l3proto = AF_INET;
187 - tuple->l4proto = iph->protocol;
188 - tuple->iifidx = dev->ifindex;
193 -/* Based on ip_exceeds_mtu(). */
194 -static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
196 - if (skb->len <= mtu)
199 - if ((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0)
202 - if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
209 -nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
210 - const struct nf_hook_state *state)
212 - struct flow_offload_tuple_rhash *tuplehash;
213 - struct nf_flowtable *flow_table = priv;
214 - struct flow_offload_tuple tuple = {};
215 - enum flow_offload_tuple_dir dir;
216 - struct flow_offload *flow;
217 - struct net_device *outdev;
218 - const struct rtable *rt;
222 - if (skb->protocol != htons(ETH_P_IP))
225 - if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
228 - tuplehash = flow_offload_lookup(flow_table, &tuple);
229 - if (tuplehash == NULL)
232 - outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
236 - dir = tuplehash->tuple.dir;
237 - flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
238 - rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
240 - if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
243 - if (skb_try_make_writable(skb, sizeof(*iph)))
246 - if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
247 - nf_flow_nat_ip(flow, skb, dir) < 0)
250 - flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
252 - ip_decrease_ttl(iph);
255 - nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
256 - neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
260 -EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
262 static struct nf_flowtable_type flowtable_ipv4 = {
263 .family = NFPROTO_IPV4,
264 --- a/net/netfilter/Makefile
265 +++ b/net/netfilter/Makefile
266 @@ -113,7 +113,7 @@ obj-$(CONFIG_NFT_FWD_NETDEV) += nft_fwd_
268 # flow table infrastructure
269 obj-$(CONFIG_NF_FLOW_TABLE) += nf_flow_table.o
270 -nf_flow_table-objs := nf_flow_table_core.o
271 +nf_flow_table-objs := nf_flow_table_core.o nf_flow_table_ip.o
273 obj-$(CONFIG_NF_FLOW_TABLE_INET) += nf_flow_table_inet.o
276 +++ b/net/netfilter/nf_flow_table_ip.c
278 +#include <linux/kernel.h>
279 +#include <linux/init.h>
280 +#include <linux/module.h>
281 +#include <linux/netfilter.h>
282 +#include <linux/rhashtable.h>
283 +#include <linux/ip.h>
284 +#include <linux/netdevice.h>
286 +#include <net/neighbour.h>
287 +#include <net/netfilter/nf_flow_table.h>
288 +/* For layer 4 checksum field offset. */
289 +#include <linux/tcp.h>
290 +#include <linux/udp.h>
292 +static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
293 + __be32 addr, __be32 new_addr)
295 + struct tcphdr *tcph;
297 + if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
298 + skb_try_make_writable(skb, thoff + sizeof(*tcph)))
301 + tcph = (void *)(skb_network_header(skb) + thoff);
302 + inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
307 +static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
308 + __be32 addr, __be32 new_addr)
310 + struct udphdr *udph;
312 + if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
313 + skb_try_make_writable(skb, thoff + sizeof(*udph)))
316 + udph = (void *)(skb_network_header(skb) + thoff);
317 + if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
318 + inet_proto_csum_replace4(&udph->check, skb, addr,
321 + udph->check = CSUM_MANGLED_0;
327 +static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
328 + unsigned int thoff, __be32 addr,
331 + switch (iph->protocol) {
333 + if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
337 + if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
345 +static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
346 + struct iphdr *iph, unsigned int thoff,
347 + enum flow_offload_tuple_dir dir)
349 + __be32 addr, new_addr;
352 + case FLOW_OFFLOAD_DIR_ORIGINAL:
354 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
355 + iph->saddr = new_addr;
357 + case FLOW_OFFLOAD_DIR_REPLY:
359 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
360 + iph->daddr = new_addr;
365 + csum_replace4(&iph->check, addr, new_addr);
367 + return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
370 +static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
371 + struct iphdr *iph, unsigned int thoff,
372 + enum flow_offload_tuple_dir dir)
374 + __be32 addr, new_addr;
377 + case FLOW_OFFLOAD_DIR_ORIGINAL:
379 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
380 + iph->daddr = new_addr;
382 + case FLOW_OFFLOAD_DIR_REPLY:
384 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
385 + iph->saddr = new_addr;
391 + return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
394 +static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
395 + enum flow_offload_tuple_dir dir)
397 + struct iphdr *iph = ip_hdr(skb);
398 + unsigned int thoff = iph->ihl * 4;
400 + if (flow->flags & FLOW_OFFLOAD_SNAT &&
401 + (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
402 + nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
404 + if (flow->flags & FLOW_OFFLOAD_DNAT &&
405 + (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
406 + nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
412 +static bool ip_has_options(unsigned int thoff)
414 + return thoff != sizeof(struct iphdr);
417 +static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
418 + struct flow_offload_tuple *tuple)
420 + struct flow_ports *ports;
421 + unsigned int thoff;
424 + if (!pskb_may_pull(skb, sizeof(*iph)))
428 + thoff = iph->ihl * 4;
430 + if (ip_is_fragment(iph) ||
431 + unlikely(ip_has_options(thoff)))
434 + if (iph->protocol != IPPROTO_TCP &&
435 + iph->protocol != IPPROTO_UDP)
438 + thoff = iph->ihl * 4;
439 + if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
442 + ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
444 + tuple->src_v4.s_addr = iph->saddr;
445 + tuple->dst_v4.s_addr = iph->daddr;
446 + tuple->src_port = ports->source;
447 + tuple->dst_port = ports->dest;
448 + tuple->l3proto = AF_INET;
449 + tuple->l4proto = iph->protocol;
450 + tuple->iifidx = dev->ifindex;
455 +/* Based on ip_exceeds_mtu(). */
456 +static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
458 + if (skb->len <= mtu)
461 + if ((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0)
464 + if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
471 +nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
472 + const struct nf_hook_state *state)
474 + struct flow_offload_tuple_rhash *tuplehash;
475 + struct nf_flowtable *flow_table = priv;
476 + struct flow_offload_tuple tuple = {};
477 + enum flow_offload_tuple_dir dir;
478 + struct flow_offload *flow;
479 + struct net_device *outdev;
480 + const struct rtable *rt;
484 + if (skb->protocol != htons(ETH_P_IP))
487 + if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
490 + tuplehash = flow_offload_lookup(flow_table, &tuple);
491 + if (tuplehash == NULL)
494 + outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
498 + dir = tuplehash->tuple.dir;
499 + flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
500 + rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
502 + if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
505 + if (skb_try_make_writable(skb, sizeof(*iph)))
508 + if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
509 + nf_flow_nat_ip(flow, skb, dir) < 0)
512 + flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
514 + ip_decrease_ttl(iph);
517 + nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
518 + neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
522 +EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);