netfilter: nf_flow_table: use IP_CT_DIR_* values for FLOW_OFFLOAD_DIR_*
Subsystems (3)
RFC 1/9 netfilter: nf_flow_table: use IP_CT_DIR_* values for FLOW_OFFLOAD_DIR_*
Simplifies further code cleanups
--- include/net/netfilter/nf_flow_table.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +4 -4
@@ -6,6 +6,7 @@#include <linux/netdevice.h> #include <linux/rhashtable.h> #include <linux/rcupdate.h> +#include <linux/netfilter/nf_conntrack_tuple_common.h> #include <net/dst.h> struct nf_flowtable ;
@@ -34,11 +35,10 @@ struct nf_flowtable { };enum flow_offload_tuple_dir { -FLOW_OFFLOAD_DIR_ORIGINAL , -FLOW_OFFLOAD_DIR_REPLY , - __FLOW_OFFLOAD_DIR_MAX =FLOW_OFFLOAD_DIR_REPLY , +FLOW_OFFLOAD_DIR_ORIGINAL =IP_CT_DIR_ORIGINAL , +FLOW_OFFLOAD_DIR_REPLY =IP_CT_DIR_REPLY , +FLOW_OFFLOAD_DIR_MAX =IP_CT_DIR_MAX }; -#define FLOW_OFFLOAD_DIR_MAX (__FLOW_OFFLOAD_DIR_MAX + 1) struct flow_offload_tuple { union {
Signature · 1 line
2.14.2
RFC 2/9 netfilter: nf_flow_table: clean up flow_offload_alloc
Reduce code duplication and make it much easier to read
--- net/netfilter/nf_flow_table.c | 93 ++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 59 deletions(-)
net/netfilter/nf_flow_table.c +34 -59
@@ -16,6 +16,38 @@ struct flow_offload_entry {struct rcu_head rcu_head; }; +static void +flow_offload_fill_dir (struct flow_offload *flow,struct nf_conn *ct, +struct nf_flow_route *route, +enum flow_offload_tuple_dir dir) +{ +struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple; +struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple; + + ft->dir = dir; + +switch (ctt->src.l3num) { +case NFPROTO_IPV4 : + ft->src_v4 = ctt->src.u3.in; + ft->dst_v4 = ctt->dst.u3.in; +break ; +case NFPROTO_IPV6 : + ft->src_v6 = ctt->src.u3.in6; + ft->dst_v6 = ctt->dst.u3.in6; +break ; + } + + ft->l3proto = ctt->src.l3num; + ft->l4proto = ctt->dst.protonum; + ft->src_port = ctt->src.u.tcp.port; + ft->dst_port = ctt->dst.u.tcp.port; + + ft->iifidx = route->tuple[dir].ifindex; + ft->oifidx = route->tuple[!dir].ifindex; + + ft->dst_cache = route->tuple[dir].dst; +} +struct flow_offload *flow_offload_alloc (struct nf_conn *ct,struct nf_flow_route *route) {
@@ -40,65 +72,8 @@ flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route) entry->ct = ct; -switch (ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.l3num) { -case NFPROTO_IPV4 : - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.src_v4 = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u3.in; - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_v4 = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.u3.in; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.src_v4 = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.src.u3.in; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_v4 = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.dst.u3.in; -break ; -case NFPROTO_IPV6 : - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.src_v6 = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u3.in6; - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_v6 = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.u3.in6; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.src_v6 = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.src.u3.in6; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_v6 = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.dst.u3.in6; -break ; - } - - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.l3proto = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.l3num; - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.l4proto = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.protonum; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.l3proto = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.l3num; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.l4proto = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.protonum; - - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_cache = - route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL ].dst; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_cache = - route->tuple[FLOW_OFFLOAD_DIR_REPLY ].dst; - - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.src_port = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u.tcp.port; - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_port = - ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.dst.u.tcp.port; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.src_port = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.src.u.tcp.port; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_port = - ct->tuplehash[IP_CT_DIR_REPLY ].tuple.dst.u.tcp.port; - - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dir = -FLOW_OFFLOAD_DIR_ORIGINAL ; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dir = -FLOW_OFFLOAD_DIR_REPLY ; - - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.iifidx = - route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL ].ifindex; - flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.oifidx = - route->tuple[FLOW_OFFLOAD_DIR_REPLY ].ifindex; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.iifidx = - route->tuple[FLOW_OFFLOAD_DIR_REPLY ].ifindex; - flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.oifidx = - route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL ].ifindex; +flow_offload_fill_dir (flow, ct, route,FLOW_OFFLOAD_DIR_ORIGINAL ); +flow_offload_fill_dir (flow, ct, route,FLOW_OFFLOAD_DIR_REPLY );if (ct->status &IPS_SRC_NAT ) flow->flags |=FLOW_OFFLOAD_SNAT ;
Signature · 1 line
2.14.2
RFC 3/9 ipv6: make ip6_dst_mtu_forward inline
In reply to Felix FietkauRemoves a direct dependency on ipv6.ko
--- include/net/ip6_route.h | 22 ++++++++++++++++++++++ include/net/ipv6.h | 2 -- net/ipv6/ip6_output.c | 22 ---------------------- 3 files changed, 22 insertions(+), 24 deletions(-)
include/net/ip6_route.h +22 -0
@@ -266,4 +266,26 @@ static inline bool rt6_duplicate_nexthop(struct rt6_info *a, struct rt6_info *b)ipv6_addr_equal (&a->rt6i_gateway, &b->rt6i_gateway) && !lwtunnel_cmp_encap(a->dst.lwtstate, b->dst.lwtstate); } + +static inline unsigned int ip6_dst_mtu_forward (const struct dst_entry *dst) +{ +unsigned int mtu; +struct inet6_dev *idev; + +if (dst_metric_locked (dst,RTAX_MTU )) { + mtu =dst_metric_raw (dst,RTAX_MTU ); +if (mtu) +return mtu; + } + + mtu =IPV6_MIN_MTU ; +rcu_read_lock (); + idev =__in6_dev_get (dst->dev); +if (idev) + mtu = idev->cnf.mtu6; +rcu_read_unlock (); + +return mtu; +} +#endif
include/net/ipv6.h +0 -2
@@ -970,8 +970,6 @@ static inline struct sk_buff *ip6_finish_skb(struct sock *sk) &inet6_sk(sk)->cork); } -unsigned int ip6_dst_mtu_forward (const struct dst_entry *dst); -int ip6_dst_lookup (struct net *net,struct sock *sk,struct dst_entry **dst,struct flowi6 *fl6); struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6,
net/ipv6/ip6_output.c +0 -22
@@ -378,28 +378,6 @@ static inline int ip6_forward_finish(struct net *net, struct sock *sk,return dst_output (net, sk, skb); } -unsigned int ip6_dst_mtu_forward (const struct dst_entry *dst) -{ -unsigned int mtu; -struct inet6_dev *idev; - -if (dst_metric_locked (dst,RTAX_MTU )) { - mtu =dst_metric_raw (dst,RTAX_MTU ); -if (mtu) -return mtu; - } - - mtu =IPV6_MIN_MTU ; -rcu_read_lock (); - idev =__in6_dev_get (dst->dev); -if (idev) - mtu = idev->cnf.mtu6; -rcu_read_unlock (); - -return mtu; -} -EXPORT_SYMBOL_GPL (ip6_dst_mtu_forward); -static bool ip6_pkt_too_big (const struct sk_buff *skb,unsigned int mtu) {if (skb->len <= mtu)
Signature · 1 line
2.14.2
RFC 4/9 netfilter: nf_flow_table: cache mtu in struct flow_offload_tuple
In reply to Felix FietkauReduces the number of cache lines touched in the offload forwarding path
--- include/net/netfilter/nf_flow_table.h | 2 ++ net/ipv4/netfilter/nf_flow_table_ipv4.c | 17 +++-------------- net/ipv6/netfilter/nf_flow_table_ipv6.c | 17 +++-------------- net/netfilter/nf_flow_table.c | 8 ++++++-- 4 files changed, 14 insertions(+), 30 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -0
@@ -62,6 +62,8 @@ struct flow_offload_tuple {int oifidx; +u16 mtu; +struct dst_entry *dst_cache; };
net/ipv4/netfilter/nf_flow_table_ipv4.c +3 -14
@@ -177,7 +177,7 @@ static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev, }/* Based on ip_exceeds_mtu(). */ -static bool __nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) +static bool nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) {if (skb->len <= mtu)return false;
@@ -191,17 +191,6 @@ static bool __nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)return true; } -static bool nf_flow_exceeds_mtu (struct sk_buff *skb,const struct rtable *rt) -{ -u32 mtu; - - mtu =ip_dst_mtu_maybe_forward (&rt->dst, true); -if (__nf_flow_exceeds_mtu (skb, mtu)) -return true; - -return false; -} -unsigned int nf_flow_offload_ip_hook (void *priv,struct sk_buff *skb,const struct nf_hook_state *state)
@@ -232,9 +221,9 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, dir = tuplehash->tuple.dir; flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache; -if (unlikely (nf_flow_exceeds_mtu (skb, rt))) + +if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu)))return NF_ACCEPT ;if (skb_try_make_writable (skb,sizeof (*iph)))
net/ipv6/netfilter/nf_flow_table_ipv6.c +3 -14
@@ -173,7 +173,7 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev, }/* Based on ip_exceeds_mtu(). */ -static bool __nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) +static bool nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) {if (skb->len <= mtu)return false;
@@ -184,17 +184,6 @@ static bool __nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)return true; } -static bool nf_flow_exceeds_mtu (struct sk_buff *skb,const struct rt6_info *rt) -{ -u32 mtu; - - mtu =ip6_dst_mtu_forward (&rt->dst); -if (__nf_flow_exceeds_mtu (skb, mtu)) -return true; - -return false; -} -unsigned int nf_flow_offload_ipv6_hook (void *priv,struct sk_buff *skb,const struct nf_hook_state *state)
@@ -225,9 +214,9 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb, dir = tuplehash->tuple.dir; flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache; -if (unlikely (nf_flow_exceeds_mtu (skb, rt))) + +if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu)))return NF_ACCEPT ;if (skb_try_make_writable (skb,sizeof (*ip6h)))
net/netfilter/nf_flow_table.c +6 -2
@@ -4,6 +4,8 @@#include <linux/netfilter.h> #include <linux/rhashtable.h> #include <linux/netdevice.h> +#include <net/ip.h> +#include <net/ip6_route.h> #include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_flow_table.h> #include <net/netfilter/nf_conntrack.h>
@@ -23,6 +25,7 @@ flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct, {struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple; +struct dst_entry *dst = route->tuple[dir].dst; ft->dir = dir;
@@ -30,10 +33,12 @@ flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,case NFPROTO_IPV4 : ft->src_v4 = ctt->src.u3.in; ft->dst_v4 = ctt->dst.u3.in; + ft->mtu =ip_dst_mtu_maybe_forward (dst, true);break ;case NFPROTO_IPV6 : ft->src_v6 = ctt->src.u3.in6; ft->dst_v6 = ctt->dst.u3.in6; + ft->mtu =ip6_dst_mtu_forward (dst);break ; }
@@ -44,8 +49,7 @@ flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct, ft->iifidx = route->tuple[dir].ifindex; ft->oifidx = route->tuple[!dir].ifindex; - - ft->dst_cache = route->tuple[dir].dst; + ft->dst_cache = dst; } struct flow_offload *
Signature · 1 line
2.14.2
RFC 5/9 netfilter: nf_flow_table: rename nf_flow_table.c to nf_flow_table_core.c
In reply to Felix FietkauPreparation for adding more code to the same module
---
net/netfilter/Makefile | 2 ++
net/netfilter/{nf_flow_table.c => nf_flow_table_core.c} | 0
2 files changed, 2 insertions(+)
rename net/netfilter/{nf_flow_table.c => nf_flow_table_core.c} (100%)net/netfilter/Makefile +2 -0
@@ -112,6 +112,8 @@ obj-$(CONFIG_NFT_FWD_NETDEV) += nft_fwd_netdev.o # flow table infrastructure obj-$(CONFIG_NF_FLOW_TABLE) += nf_flow_table.o +nf_flow_table-objs := nf_flow_table_core.o + obj-$(CONFIG_NF_FLOW_TABLE_INET) += nf_flow_table_inet.o obj-$(CONFIG_NF_FLOW_TABLE_HW) += nf_flow_table_hw.o
net/netfilter/nf_flow_table_core.c renamed from net/netfilter/nf_flow_table.c +0 -0
Signature · 1 line
2.14.2
RFC 6/9 netfilter: nf_flow_table: move ipv4 offload hook code to nf_flow_table
In reply to Felix FietkauAllows some minor code sharing with the ipv6 hook code and is also useful as preparation for adding iptables support for offload
--- net/ipv4/netfilter/nf_flow_table_ipv4.c | 240 ------------------------------- net/netfilter/Makefile | 2 +- net/netfilter/nf_flow_table_ip.c | 245 ++++++++++++++++++++++++++++++++ 3 files changed, 246 insertions(+), 241 deletions(-) create mode 100644 net/netfilter/nf_flow_table_ip.c
net/ipv4/netfilter/nf_flow_table_ipv4.c +0 -240
@@ -2,248 +2,8 @@#include <linux/init.h> #include <linux/module.h> #include <linux/netfilter.h> -#include <linux/rhashtable.h> -#include <linux/ip.h> -#include <linux/netdevice.h> -#include <net/ip.h> -#include <net/neighbour.h> #include <net/netfilter/nf_flow_table.h> #include <net/netfilter/nf_tables.h> -/* For layer 4 checksum field offset. */ -#include <linux/tcp.h> -#include <linux/udp.h> - -static int nf_flow_nat_ip_tcp (struct sk_buff *skb,unsigned int thoff, -__be32 addr,__be32 new_addr) -{ -struct tcphdr *tcph; - -if (!pskb_may_pull (skb, thoff +sizeof (*tcph)) || -skb_try_make_writable (skb, thoff +sizeof (*tcph))) -return -1 ; - - tcph = (void *)(skb_network_header (skb) + thoff); -inet_proto_csum_replace4 (&tcph->check, skb, addr, new_addr, true); - -return 0 ; -} - -static int nf_flow_nat_ip_udp (struct sk_buff *skb,unsigned int thoff, -__be32 addr,__be32 new_addr) -{ -struct udphdr *udph; - -if (!pskb_may_pull (skb, thoff +sizeof (*udph)) || -skb_try_make_writable (skb, thoff +sizeof (*udph))) -return -1 ; - - udph = (void *)(skb_network_header (skb) + thoff); -if (udph->check || skb->ip_summed ==CHECKSUM_PARTIAL ) { -inet_proto_csum_replace4 (&udph->check, skb, addr, - new_addr, true); -if (!udph->check) - udph->check =CSUM_MANGLED_0 ; - } - -return 0 ; -} - -static int nf_flow_nat_ip_l4proto (struct sk_buff *skb,struct iphdr *iph, -unsigned int thoff,__be32 addr, -__be32 new_addr) -{ -switch (iph->protocol) { -case IPPROTO_TCP : -if (nf_flow_nat_ip_tcp (skb, thoff, addr, new_addr) <0 ) -return NF_DROP ; -break ; -case IPPROTO_UDP : -if (nf_flow_nat_ip_udp (skb, thoff, addr, new_addr) <0 ) -return NF_DROP ; -break ; - } - -return 0 ; -} - -static int nf_flow_snat_ip (const struct flow_offload *flow,struct sk_buff *skb, -struct iphdr *iph,unsigned int thoff, -enum flow_offload_tuple_dir dir) -{ -__be32 addr, new_addr; - -switch (dir) { -case FLOW_OFFLOAD_DIR_ORIGINAL : - addr = iph->saddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_v4.s_addr; - iph->saddr = new_addr; -break ; -case FLOW_OFFLOAD_DIR_REPLY : - addr = iph->daddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.src_v4.s_addr; - iph->daddr = new_addr; -break ; -default : -return -1 ; - } -csum_replace4 (&iph->check, addr, new_addr); - -return nf_flow_nat_ip_l4proto (skb, iph, thoff, addr, new_addr); -} - -static int nf_flow_dnat_ip (const struct flow_offload *flow,struct sk_buff *skb, -struct iphdr *iph,unsigned int thoff, -enum flow_offload_tuple_dir dir) -{ -__be32 addr, new_addr; - -switch (dir) { -case FLOW_OFFLOAD_DIR_ORIGINAL : - addr = iph->daddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.src_v4.s_addr; - iph->daddr = new_addr; -break ; -case FLOW_OFFLOAD_DIR_REPLY : - addr = iph->saddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_v4.s_addr; - iph->saddr = new_addr; -break ; -default : -return -1 ; - } - -return nf_flow_nat_ip_l4proto (skb, iph, thoff, addr, new_addr); -} - -static int nf_flow_nat_ip (const struct flow_offload *flow,struct sk_buff *skb, -enum flow_offload_tuple_dir dir) -{ -struct iphdr *iph =ip_hdr (skb); -unsigned int thoff = iph->ihl *4 ; - -if (flow->flags &FLOW_OFFLOAD_SNAT && - (nf_flow_snat_port (flow, skb, thoff, iph->protocol, dir) <0 || -nf_flow_snat_ip (flow, skb, iph, thoff, dir) <0 )) -return -1 ; -if (flow->flags &FLOW_OFFLOAD_DNAT && - (nf_flow_dnat_port (flow, skb, thoff, iph->protocol, dir) <0 || -nf_flow_dnat_ip (flow, skb, iph, thoff, dir) <0 )) -return -1 ; - -return 0 ; -} - -static bool ip_has_options (unsigned int thoff) -{ -return thoff !=sizeof (struct iphdr ); -} - -static int nf_flow_tuple_ip (struct sk_buff *skb,const struct net_device *dev, -struct flow_offload_tuple *tuple) -{ -struct flow_ports *ports; -unsigned int thoff; -struct iphdr *iph; - -if (!pskb_may_pull (skb,sizeof (*iph))) -return -1 ; - - iph =ip_hdr (skb); - thoff = iph->ihl *4 ; - -if (ip_is_fragment (iph) || -unlikely (ip_has_options (thoff))) -return -1 ; - -if (iph->protocol !=IPPROTO_TCP && - iph->protocol !=IPPROTO_UDP ) -return -1 ; - - thoff = iph->ihl *4 ; -if (!pskb_may_pull (skb, thoff +sizeof (*ports))) -return -1 ; - - ports = (struct flow_ports *)(skb_network_header (skb) + thoff); - - tuple->src_v4.s_addr = iph->saddr; - tuple->dst_v4.s_addr = iph->daddr; - tuple->src_port = ports->source; - tuple->dst_port = ports->dest; - tuple->l3proto =AF_INET ; - tuple->l4proto = iph->protocol; - tuple->iifidx = dev->ifindex; - -return 0 ; -} - -/* Based on ip_exceeds_mtu(). */ -static bool nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) -{ -if (skb->len <= mtu) -return false; - -if ((ip_hdr (skb)->frag_off &htons (IP_DF )) ==0 ) -return false; - -if (skb_is_gso (skb) &&skb_gso_validate_mtu (skb, mtu)) -return false; - -return true; -} - -unsigned int -nf_flow_offload_ip_hook (void *priv,struct sk_buff *skb, -const struct nf_hook_state *state) -{ -struct flow_offload_tuple_rhash *tuplehash; -struct nf_flowtable *flow_table = priv; -struct flow_offload_tuple tuple = {}; -enum flow_offload_tuple_dir dir; -struct flow_offload *flow; -struct net_device *outdev; -const struct rtable *rt; -struct iphdr *iph; -__be32 nexthop; - -if (skb->protocol !=htons (ETH_P_IP )) -return NF_ACCEPT ; - -if (nf_flow_tuple_ip (skb, state->in, &tuple) <0 ) -return NF_ACCEPT ; - - tuplehash =flow_offload_lookup (flow_table, &tuple); -if (tuplehash ==NULL ) -return NF_ACCEPT ; - - outdev =dev_get_by_index_rcu (state->net, tuplehash->tuple.oifidx); -if (!outdev) -return NF_ACCEPT ; - - dir = tuplehash->tuple.dir; - flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache; - -if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu))) -return NF_ACCEPT ; - -if (skb_try_make_writable (skb,sizeof (*iph))) -return NF_DROP ; - -if (flow->flags & (FLOW_OFFLOAD_SNAT |FLOW_OFFLOAD_DNAT ) && -nf_flow_nat_ip (flow, skb, dir) <0 ) -return NF_DROP ; - - flow->timeout = (u32 )jiffies +NF_FLOW_TIMEOUT ; - iph =ip_hdr (skb); -ip_decrease_ttl (iph); - - skb->dev = outdev; - nexthop =rt_nexthop (rt, flow->tuplehash[!dir].tuple.src_v4.s_addr); -neigh_xmit (NEIGH_ARP_TABLE , outdev, &nexthop, skb); - -return NF_STOLEN ; -} -EXPORT_SYMBOL_GPL (nf_flow_offload_ip_hook);static struct nf_flowtable_type flowtable_ipv4 = { .family =NFPROTO_IPV4 ,
net/netfilter/Makefile +1 -1
@@ -112,7 +112,7 @@ obj-$(CONFIG_NFT_FWD_NETDEV) += nft_fwd_netdev.o # flow table infrastructure obj-$(CONFIG_NF_FLOW_TABLE) += nf_flow_table.o -nf_flow_table-objs := nf_flow_table_core.o +nf_flow_table-objs := nf_flow_table_core.o nf_flow_table_ip.o obj-$(CONFIG_NF_FLOW_TABLE_INET) += nf_flow_table_inet.o obj-$(CONFIG_NF_FLOW_TABLE_HW) += nf_flow_table_hw.o
net/netfilter/nf_flow_table_ip.c new file +245 -0
245 lines not on this page · raw message
Signature · 1 line
2.14.2
RFC 7/9 netfilter: nf_flow_table: move ip header check out of nf_flow_exceeds_mtu
In reply to Felix FietkauAllows the function to be shared with the IPv6 hook code
--- net/netfilter/nf_flow_table_ip.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/netfilter/nf_flow_table_ip.c +2 -4
@@ -181,9 +181,6 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)if (skb->len <= mtu)return false; -if ((ip_hdr (skb)->frag_off &htons (IP_DF )) ==0 ) -return false; -if (skb_is_gso (skb) &&skb_gso_validate_mtu (skb, mtu))return false;
@@ -222,7 +219,8 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache; -if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu))) +if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu)) && + (ip_hdr (skb)->frag_off &htons (IP_DF )) !=0 )return NF_ACCEPT ;if (skb_try_make_writable (skb,sizeof (*iph)))
Signature · 1 line
2.14.2
RFC 8/9 netfilter: nf_flow_table: move ipv6 offload hook code to nf_flow_table
In reply to Felix FietkauUseful as preparation for adding iptables support for offload
--- net/ipv6/netfilter/nf_flow_table_ipv6.c | 232 -------------------------------- net/netfilter/nf_flow_table_ip.c | 215 +++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+), 232 deletions(-)
net/ipv6/netfilter/nf_flow_table_ipv6.c +0 -232
@@ -3,240 +3,8 @@#include <linux/module.h> #include <linux/netfilter.h> #include <linux/rhashtable.h> -#include <linux/ipv6.h> -#include <linux/netdevice.h> -#include <net/ipv6.h> -#include <net/ip6_route.h> -#include <net/neighbour.h> #include <net/netfilter/nf_flow_table.h> #include <net/netfilter/nf_tables.h> -/* For layer 4 checksum field offset. */ -#include <linux/tcp.h> -#include <linux/udp.h> - -static int nf_flow_nat_ipv6_tcp (struct sk_buff *skb,unsigned int thoff, -struct in6_addr *addr, -struct in6_addr *new_addr) -{ -struct tcphdr *tcph; - -if (!pskb_may_pull (skb, thoff +sizeof (*tcph)) || -skb_try_make_writable (skb, thoff +sizeof (*tcph))) -return -1 ; - - tcph = (void *)(skb_network_header (skb) + thoff); -inet_proto_csum_replace16 (&tcph->check, skb, addr->s6_addr32, - new_addr->s6_addr32, true); - -return 0 ; -} - -static int nf_flow_nat_ipv6_udp (struct sk_buff *skb,unsigned int thoff, -struct in6_addr *addr, -struct in6_addr *new_addr) -{ -struct udphdr *udph; - -if (!pskb_may_pull (skb, thoff +sizeof (*udph)) || -skb_try_make_writable (skb, thoff +sizeof (*udph))) -return -1 ; - - udph = (void *)(skb_network_header (skb) + thoff); -if (udph->check || skb->ip_summed ==CHECKSUM_PARTIAL ) { -inet_proto_csum_replace16 (&udph->check, skb, addr->s6_addr32, - new_addr->s6_addr32, true); -if (!udph->check) - udph->check =CSUM_MANGLED_0 ; - } - -return 0 ; -} - -static int nf_flow_nat_ipv6_l4proto (struct sk_buff *skb,struct ipv6hdr *ip6h, -unsigned int thoff,struct in6_addr *addr, -struct in6_addr *new_addr) -{ -switch (ip6h->nexthdr) { -case IPPROTO_TCP : -if (nf_flow_nat_ipv6_tcp (skb, thoff, addr, new_addr) <0 ) -return NF_DROP ; -break ; -case IPPROTO_UDP : -if (nf_flow_nat_ipv6_udp (skb, thoff, addr, new_addr) <0 ) -return NF_DROP ; -break ; - } - -return 0 ; -} - -static int nf_flow_snat_ipv6 (const struct flow_offload *flow, -struct sk_buff *skb,struct ipv6hdr *ip6h, -unsigned int thoff, -enum flow_offload_tuple_dir dir) -{ -struct in6_addr addr, new_addr; - -switch (dir) { -case FLOW_OFFLOAD_DIR_ORIGINAL : - addr = ip6h->saddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.dst_v6; - ip6h->saddr = new_addr; -break ; -case FLOW_OFFLOAD_DIR_REPLY : - addr = ip6h->daddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.src_v6; - ip6h->daddr = new_addr; -break ; -default : -return -1 ; - } - -return nf_flow_nat_ipv6_l4proto (skb, ip6h, thoff, &addr, &new_addr); -} - -static int nf_flow_dnat_ipv6 (const struct flow_offload *flow, -struct sk_buff *skb,struct ipv6hdr *ip6h, -unsigned int thoff, -enum flow_offload_tuple_dir dir) -{ -struct in6_addr addr, new_addr; - -switch (dir) { -case FLOW_OFFLOAD_DIR_ORIGINAL : - addr = ip6h->daddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY ].tuple.src_v6; - ip6h->daddr = new_addr; -break ; -case FLOW_OFFLOAD_DIR_REPLY : - addr = ip6h->saddr; - new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL ].tuple.dst_v6; - ip6h->saddr = new_addr; -break ; -default : -return -1 ; - } - -return nf_flow_nat_ipv6_l4proto (skb, ip6h, thoff, &addr, &new_addr); -} - -static int nf_flow_nat_ipv6 (const struct flow_offload *flow, -struct sk_buff *skb, -enum flow_offload_tuple_dir dir) -{ -struct ipv6hdr *ip6h =ipv6_hdr (skb); -unsigned int thoff =sizeof (*ip6h); - -if (flow->flags &FLOW_OFFLOAD_SNAT && - (nf_flow_snat_port (flow, skb, thoff, ip6h->nexthdr, dir) <0 || -nf_flow_snat_ipv6 (flow, skb, ip6h, thoff, dir) <0 )) -return -1 ; -if (flow->flags &FLOW_OFFLOAD_DNAT && - (nf_flow_dnat_port (flow, skb, thoff, ip6h->nexthdr, dir) <0 || -nf_flow_dnat_ipv6 (flow, skb, ip6h, thoff, dir) <0 )) -return -1 ; - -return 0 ; -} - -static int nf_flow_tuple_ipv6 (struct sk_buff *skb,const struct net_device *dev, -struct flow_offload_tuple *tuple) -{ -struct flow_ports *ports; -struct ipv6hdr *ip6h; -unsigned int thoff; - -if (!pskb_may_pull (skb,sizeof (*ip6h))) -return -1 ; - - ip6h =ipv6_hdr (skb); - -if (ip6h->nexthdr !=IPPROTO_TCP && - ip6h->nexthdr !=IPPROTO_UDP ) -return -1 ; - - thoff =sizeof (*ip6h); -if (!pskb_may_pull (skb, thoff +sizeof (*ports))) -return -1 ; - - ports = (struct flow_ports *)(skb_network_header (skb) + thoff); - - tuple->src_v6 = ip6h->saddr; - tuple->dst_v6 = ip6h->daddr; - tuple->src_port = ports->source; - tuple->dst_port = ports->dest; - tuple->l3proto =AF_INET6 ; - tuple->l4proto = ip6h->nexthdr; - tuple->iifidx = dev->ifindex; - -return 0 ; -} - -/* Based on ip_exceeds_mtu(). */ -static bool nf_flow_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) -{ -if (skb->len <= mtu) -return false; - -if (skb_is_gso (skb) &&skb_gso_validate_mtu (skb, mtu)) -return false; - -return true; -} - -unsigned int -nf_flow_offload_ipv6_hook (void *priv,struct sk_buff *skb, -const struct nf_hook_state *state) -{ -struct flow_offload_tuple_rhash *tuplehash; -struct nf_flowtable *flow_table = priv; -struct flow_offload_tuple tuple = {}; -enum flow_offload_tuple_dir dir; -struct flow_offload *flow; -struct net_device *outdev; -struct in6_addr *nexthop; -struct ipv6hdr *ip6h; -struct rt6_info *rt; - -if (skb->protocol !=htons (ETH_P_IPV6 )) -return NF_ACCEPT ; - -if (nf_flow_tuple_ipv6 (skb, state->in, &tuple) <0 ) -return NF_ACCEPT ; - - tuplehash =flow_offload_lookup (flow_table, &tuple); -if (tuplehash ==NULL ) -return NF_ACCEPT ; - - outdev =dev_get_by_index_rcu (state->net, tuplehash->tuple.oifidx); -if (!outdev) -return NF_ACCEPT ; - - dir = tuplehash->tuple.dir; - flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache; - -if (unlikely (nf_flow_exceeds_mtu (skb, flow->tuplehash[dir].tuple.mtu))) -return NF_ACCEPT ; - -if (skb_try_make_writable (skb,sizeof (*ip6h))) -return NF_DROP ; - -if (flow->flags & (FLOW_OFFLOAD_SNAT |FLOW_OFFLOAD_DNAT ) && -nf_flow_nat_ipv6 (flow, skb, dir) <0 ) -return NF_DROP ; - - flow->timeout = (u32 )jiffies +NF_FLOW_TIMEOUT ; - ip6h =ipv6_hdr (skb); - ip6h->hop_limit--; - - skb->dev = outdev; - nexthop =rt6_nexthop (rt, &flow->tuplehash[!dir].tuple.src_v6); -neigh_xmit (NEIGH_ND_TABLE , outdev, nexthop, skb); - -return NF_STOLEN ; -} -EXPORT_SYMBOL_GPL (nf_flow_offload_ipv6_hook);static struct nf_flowtable_type flowtable_ipv6 = { .family =NFPROTO_IPV6 ,
net/netfilter/nf_flow_table_ip.c +215 -0
226 lines not on this page · raw message
Signature · 1 line
2.14.2
RFC 9/9 netfilter: nf_flow_table: relax mixed ipv4/ipv6 flowtable dependencies
In reply to Felix FietkauSince the offload hook code was moved, this table no longer depends on the IPv4 and IPv6 flowtable modules
--- net/netfilter/Kconfig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/netfilter/Kconfig +1 -2
@@ -666,8 +666,7 @@ endif # NF_TABLES config NF_FLOW_TABLE_INET tristate "Netfilter flow table mixed IPv4/IPv6 module" - depends on NF_FLOW_TABLE_IPV4 - depends on NF_FLOW_TABLE_IPV6 + depends on NF_FLOW_TABLE help This option adds the flow table mixed IPv4/IPv6 support.
Signature · 1 line
2.14.2