netfilter: flowtable: simplify route logic
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 2/3 netfilter: flowtable: split IPv4 datapath in helper functions
In reply to Pablo Neira AyusoAdd context structure and helper functions to look up for a matching IPv4 entry in the flowtable and to forward packets.
nf_flow_offload_forward() returns -1 to NF_DROP, 0 to pass up packets to classic path and 1 to enter fast path.
No functional changes are intended.
--- net/netfilter/nf_flow_table_ip.c | 116 ++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 42 deletions(-)
net/netfilter/nf_flow_table_ip.c +74 -42
@@ -163,38 +163,43 @@ static void nf_flow_tuple_encap(struct sk_buff *skb, } } -static int nf_flow_tuple_ip (struct sk_buff *skb,const struct net_device *dev, -struct flow_offload_tuple *tuple,u32 *hdrsize, -u32 offset) +struct nf_flowtable_ctx { +const struct net_device *in; +u32 offset; +u32 hdrsize; +}; + +static int nf_flow_tuple_ip (struct nf_flowtable_ctx *ctx,struct sk_buff *skb, +struct flow_offload_tuple *tuple) {struct flow_ports *ports;unsigned int thoff;struct iphdr *iph;u8 ipproto; -if (!pskb_may_pull (skb,sizeof (*iph) + offset)) +if (!pskb_may_pull (skb,sizeof (*iph) + ctx->offset))return -1 ; - iph = (struct iphdr *)(skb_network_header (skb) + offset); + iph = (struct iphdr *)(skb_network_header (skb) + ctx->offset); thoff = (iph->ihl *4 );if (ip_is_fragment (iph) ||unlikely (ip_has_options (thoff)))return -1 ; - thoff += offset; + thoff += ctx->offset; ipproto = iph->protocol;switch (ipproto) {case IPPROTO_TCP : - *hdrsize =sizeof (struct tcphdr ); + ctx->hdrsize =sizeof (struct tcphdr );break ;case IPPROTO_UDP : - *hdrsize =sizeof (struct udphdr ); + ctx->hdrsize =sizeof (struct udphdr );break ;#ifdef CONFIG_NF_CT_PROTO_GRE case IPPROTO_GRE : - *hdrsize =sizeof (struct gre_base_hdr ); + ctx->hdrsize =sizeof (struct gre_base_hdr );break ;#endif default :
@@ -204,7 +209,7 @@ static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,if (iph->ttl <=1 )return -1 ; -if (!pskb_may_pull (skb, thoff + *hdrsize)) +if (!pskb_may_pull (skb, thoff + ctx->hdrsize))return -1 ;switch (ipproto) {
@@ -224,13 +229,13 @@ static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev, } } - iph = (struct iphdr *)(skb_network_header (skb) + offset); + iph = (struct iphdr *)(skb_network_header (skb) + ctx->offset); tuple->src_v4.s_addr = iph->saddr; tuple->dst_v4.s_addr = iph->daddr; tuple->l3proto =AF_INET ; tuple->l4proto = ipproto; - tuple->iifidx = dev->ifindex; + tuple->iifidx = ctx->in->ifindex;nf_flow_tuple_encap (skb, tuple);return 0 ;
@@ -336,58 +341,56 @@ static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,return NF_STOLEN ; } -unsigned int -nf_flow_offload_ip_hook (void *priv,struct sk_buff *skb, -const struct nf_hook_state *state) +static struct flow_offload_tuple_rhash * +nf_flow_offload_lookup (struct nf_flowtable_ctx *ctx, +struct nf_flowtable *flow_table,struct sk_buff *skb) { -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; -u32 hdrsize, offset =0 ; -unsigned int thoff, mtu; -struct rtable *rt; -struct iphdr *iph; -__be32 nexthop; -int ret;if (skb->protocol !=htons (ETH_P_IP ) && - !nf_flow_skb_encap_protocol (skb,htons (ETH_P_IP ), &offset)) -return NF_ACCEPT ; + !nf_flow_skb_encap_protocol (skb,htons (ETH_P_IP ), &ctx->offset)) +return NULL ; -if (nf_flow_tuple_ip (skb, state->in, &tuple, &hdrsize, offset) <0 ) -return NF_ACCEPT ; +if (nf_flow_tuple_ip (ctx, skb, &tuple) <0 ) +return NULL ; - tuplehash =flow_offload_lookup (flow_table, &tuple); -if (tuplehash ==NULL ) -return NF_ACCEPT ; +return flow_offload_lookup (flow_table, &tuple); +} + +static int nf_flow_offload_forward (struct nf_flowtable_ctx *ctx, +struct nf_flowtable *flow_table, +struct flow_offload_tuple_rhash *tuplehash, +struct sk_buff *skb) +{ +enum flow_offload_tuple_dir dir; +struct flow_offload *flow; +unsigned int thoff, mtu; +struct iphdr *iph; dir = tuplehash->tuple.dir; flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - mtu = flow->tuplehash[dir].tuple.mtu + offset; + mtu = flow->tuplehash[dir].tuple.mtu + ctx->offset;if (unlikely (nf_flow_exceeds_mtu (skb, mtu))) -return NF_ACCEPT ; +return 0 ; - iph = (struct iphdr *)(skb_network_header (skb) + offset); - thoff = (iph->ihl *4 ) + offset; + iph = (struct iphdr *)(skb_network_header (skb) + ctx->offset); + thoff = (iph->ihl *4 ) + ctx->offset;if (nf_flow_state_check (flow, iph->protocol, skb, thoff)) -return NF_ACCEPT ; +return 0 ;if (!nf_flow_dst_check (&tuplehash->tuple)) {flow_offload_teardown (flow); -return NF_ACCEPT ; +return 0 ; } -if (skb_try_make_writable (skb, thoff + hdrsize)) -return NF_DROP ; +if (skb_try_make_writable (skb, thoff + ctx->hdrsize)) +return -1 ;flow_offload_refresh (flow_table, flow);nf_flow_encap_pop (skb, tuplehash); - thoff -= offset; + thoff -= ctx->offset; iph =ip_hdr (skb);nf_flow_nat_ip (flow, skb, thoff, dir, iph);
@@ -398,6 +401,35 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,if (flow_table->flags &NF_FLOWTABLE_COUNTER )nf_ct_acct_update (flow->ct, tuplehash->tuple.dir, skb->len); +return 1 ; +} + +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; +enum flow_offload_tuple_dir dir; +struct nf_flowtable_ctx ctx = { + .in = state->in, + }; +struct flow_offload *flow; +struct net_device *outdev; +struct rtable *rt; +__be32 nexthop; +int ret; + + tuplehash =nf_flow_offload_lookup (&ctx, flow_table, skb); +if (!tuplehash) +return NF_ACCEPT ; + + ret =nf_flow_offload_forward (&ctx, flow_table, tuplehash, skb); +if (ret <0 ) +return NF_DROP ; +else if (ret ==0 ) +return NF_ACCEPT ; +if (unlikely (tuplehash->tuple.xmit_type ==FLOW_OFFLOAD_XMIT_XFRM )) { rt = (struct rtable *)tuplehash->tuple.dst_cache;memset (skb->cb,0 ,sizeof (struct inet_skb_parm ));
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 1/3 netfilter: flowtable: simplify route logic
Grab reference to dst from skbuff earlier to simplify route caching.
--- include/net/netfilter/nf_flow_table.h | 4 ++-- net/netfilter/nf_flow_table_core.c | 24 +++--------------------- net/netfilter/nft_flow_offload.c | 12 ++++++++---- 3 files changed, 13 insertions(+), 27 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -2
@@ -263,8 +263,8 @@ nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,up_write (&flow_table->flow_block_lock); } -int flow_offload_route_init (struct flow_offload *flow, -const struct nf_flow_route *route); +void flow_offload_route_init (struct flow_offload *flow, +const struct nf_flow_route *route);int flow_offload_add (struct nf_flowtable *flow_table,struct flow_offload *flow); void flow_offload_refresh(struct nf_flowtable *flow_table,
net/netfilter/nf_flow_table_core.c +3 -21
@@ -125,9 +125,6 @@ static int flow_offload_fill_route(struct flow_offload *flow,break ;case FLOW_OFFLOAD_XMIT_XFRM :case FLOW_OFFLOAD_XMIT_NEIGH : -if (!dst_hold_safe (route->tuple[dir].dst)) -return -1 ; - flow_tuple->dst_cache = dst; flow_tuple->dst_cookie =flow_offload_dst_cookie (flow_tuple);break ;
@@ -148,27 +145,12 @@ static void nft_flow_dst_release(struct flow_offload *flow,dst_release (flow->tuplehash[dir].tuple.dst_cache); } -int flow_offload_route_init (struct flow_offload *flow, +void flow_offload_route_init (struct flow_offload *flow,const struct nf_flow_route *route) { -int err; - - err =flow_offload_fill_route (flow, route,FLOW_OFFLOAD_DIR_ORIGINAL ); -if (err <0 ) -return err; - - err =flow_offload_fill_route (flow, route,FLOW_OFFLOAD_DIR_REPLY ); -if (err <0 ) - goto err_route_reply; - +flow_offload_fill_route (flow, route,FLOW_OFFLOAD_DIR_ORIGINAL ); +flow_offload_fill_route (flow, route,FLOW_OFFLOAD_DIR_REPLY ); flow->type =NF_FLOW_OFFLOAD_ROUTE ; - -return 0 ; - -err_route_reply: -nft_flow_dst_release (flow,FLOW_OFFLOAD_DIR_ORIGINAL ); - -return err; }EXPORT_SYMBOL_GPL (flow_offload_route_init);
net/netfilter/nft_flow_offload.c +8 -4
@@ -250,9 +250,14 @@ static int nft_flow_route(const struct nft_pktinfo *pkt,break ; } +if (!dst_hold_safe (this_dst)) +return -ENOENT ; +nf_route (nft_net (pkt), &other_dst, &fl, false,nft_pf (pkt)); -if (!other_dst) +if (!other_dst) { +dst_release (this_dst);return -ENOENT ; + }nft_default_forward_path (route, this_dst, dir);nft_default_forward_path (route, other_dst, !dir);
@@ -349,8 +354,7 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,if (!flow) goto err_flow_alloc; -if (flow_offload_route_init (flow, &route) <0 ) - goto err_flow_add; +flow_offload_route_init (flow, &route);if (tcph) { ct->proto.tcp.seen[0 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ;
@@ -361,12 +365,12 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,if (ret <0 ) goto err_flow_add; -dst_release (route.tuple[!dir].dst);return ; err_flow_add:flow_offload_free (flow); err_flow_alloc: +dst_release (route.tuple[dir].dst);dst_release (route.tuple[!dir].dst); err_flow_route:clear_bit (IPS_OFFLOAD_BIT , &ct->status);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/3 netfilter: flowtable: split IPv6 datapath in helper functions
Add context structure and helper functions to look up for a matching IPv6 entry in the flowtable and to forward packets.
nf_flow_offload_forward() returns -1 to NF_DROP, 0 to pass up packets to classic path and 1 to enter fast path.
No functional changes are intended.
--- net/netfilter/nf_flow_table_ip.c | 109 +++++++++++++++++++------------ 1 file changed, 68 insertions(+), 41 deletions(-)
net/netfilter/nf_flow_table_ip.c +68 -41
@@ -567,32 +567,31 @@ static void nf_flow_nat_ipv6(const struct flow_offload *flow, } } -static int nf_flow_tuple_ipv6 (struct sk_buff *skb,const struct net_device *dev, -struct flow_offload_tuple *tuple,u32 *hdrsize, -u32 offset) +static int nf_flow_tuple_ipv6 (struct nf_flowtable_ctx *ctx,struct sk_buff *skb, +struct flow_offload_tuple *tuple) {struct flow_ports *ports;struct ipv6hdr *ip6h;unsigned int thoff;u8 nexthdr; - thoff =sizeof (*ip6h) + offset; + thoff =sizeof (*ip6h) + ctx->offset;if (!pskb_may_pull (skb, thoff))return -1 ; - ip6h = (struct ipv6hdr *)(skb_network_header (skb) + offset); + ip6h = (struct ipv6hdr *)(skb_network_header (skb) + ctx->offset); nexthdr = ip6h->nexthdr;switch (nexthdr) {case IPPROTO_TCP : - *hdrsize =sizeof (struct tcphdr ); + ctx->hdrsize =sizeof (struct tcphdr );break ;case IPPROTO_UDP : - *hdrsize =sizeof (struct udphdr ); + ctx->hdrsize =sizeof (struct udphdr );break ;#ifdef CONFIG_NF_CT_PROTO_GRE case IPPROTO_GRE : - *hdrsize =sizeof (struct gre_base_hdr ); + ctx->hdrsize =sizeof (struct gre_base_hdr );break ;#endif default :
@@ -602,7 +601,7 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,if (ip6h->hop_limit <=1 )return -1 ; -if (!pskb_may_pull (skb, thoff + *hdrsize)) +if (!pskb_may_pull (skb, thoff + ctx->hdrsize))return -1 ;switch (nexthdr) {
@@ -622,65 +621,47 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev, } } - ip6h = (struct ipv6hdr *)(skb_network_header (skb) + offset); + ip6h = (struct ipv6hdr *)(skb_network_header (skb) + ctx->offset); tuple->src_v6 = ip6h->saddr; tuple->dst_v6 = ip6h->daddr; tuple->l3proto =AF_INET6 ; tuple->l4proto = nexthdr; - tuple->iifidx = dev->ifindex; + tuple->iifidx = ctx->in->ifindex;nf_flow_tuple_encap (skb, tuple);return 0 ; } -unsigned int -nf_flow_offload_ipv6_hook (void *priv,struct sk_buff *skb, -const struct nf_hook_state *state) +static int nf_flow_offload_ipv6_forward (struct nf_flowtable_ctx *ctx, +struct nf_flowtable *flow_table, +struct flow_offload_tuple_rhash *tuplehash, +struct sk_buff *skb) { -struct flow_offload_tuple_rhash *tuplehash; -struct nf_flowtable *flow_table = priv; -struct flow_offload_tuple tuple = {};enum flow_offload_tuple_dir dir; -const struct in6_addr *nexthop;struct flow_offload *flow; -struct net_device *outdev;unsigned int thoff, mtu; -u32 hdrsize, offset =0 ;struct ipv6hdr *ip6h; -struct rt6_info *rt; -int ret; - -if (skb->protocol !=htons (ETH_P_IPV6 ) && - !nf_flow_skb_encap_protocol (skb,htons (ETH_P_IPV6 ), &offset)) -return NF_ACCEPT ; - -if (nf_flow_tuple_ipv6 (skb, state->in, &tuple, &hdrsize, offset) <0 ) -return NF_ACCEPT ; - - tuplehash =flow_offload_lookup (flow_table, &tuple); -if (tuplehash ==NULL ) -return NF_ACCEPT ; dir = tuplehash->tuple.dir; flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); - mtu = flow->tuplehash[dir].tuple.mtu + offset; + mtu = flow->tuplehash[dir].tuple.mtu + ctx->offset;if (unlikely (nf_flow_exceeds_mtu (skb, mtu))) -return NF_ACCEPT ; +return 0 ; - ip6h = (struct ipv6hdr *)(skb_network_header (skb) + offset); - thoff =sizeof (*ip6h) + offset; + ip6h = (struct ipv6hdr *)(skb_network_header (skb) + ctx->offset); + thoff =sizeof (*ip6h) + ctx->offset;if (nf_flow_state_check (flow, ip6h->nexthdr, skb, thoff)) -return NF_ACCEPT ; +return 0 ;if (!nf_flow_dst_check (&tuplehash->tuple)) {flow_offload_teardown (flow); -return NF_ACCEPT ; +return 0 ; } -if (skb_try_make_writable (skb, thoff + hdrsize)) -return NF_DROP ; +if (skb_try_make_writable (skb, thoff + ctx->hdrsize)) +return -1 ;flow_offload_refresh (flow_table, flow);
@@ -695,6 +676,52 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,if (flow_table->flags &NF_FLOWTABLE_COUNTER )nf_ct_acct_update (flow->ct, tuplehash->tuple.dir, skb->len); +return 1 ; +} + +static struct flow_offload_tuple_rhash * +nf_flow_offload_ipv6_lookup (struct nf_flowtable_ctx *ctx, +struct nf_flowtable *flow_table, +struct sk_buff *skb) +{ +struct flow_offload_tuple tuple = {}; + +if (skb->protocol !=htons (ETH_P_IPV6 ) && + !nf_flow_skb_encap_protocol (skb,htons (ETH_P_IPV6 ), &ctx->offset)) +return NULL ; + +if (nf_flow_tuple_ipv6 (ctx, skb, &tuple) <0 ) +return NULL ; + +return flow_offload_lookup (flow_table, &tuple); +} + +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; +enum flow_offload_tuple_dir dir; +struct nf_flowtable_ctx ctx = { + .in = state->in, + }; +const struct in6_addr *nexthop; +struct flow_offload *flow; +struct net_device *outdev; +struct rt6_info *rt; +int ret; + + tuplehash =nf_flow_offload_ipv6_lookup (&ctx, flow_table, skb); +if (tuplehash ==NULL ) +return NF_ACCEPT ; + + ret =nf_flow_offload_ipv6_forward (&ctx, flow_table, tuplehash, skb); +if (ret <0 ) +return NF_DROP ; +else if (ret ==0 ) +return NF_ACCEPT ; +if (unlikely (tuplehash->tuple.xmit_type ==FLOW_OFFLOAD_XMIT_XFRM )) { rt = (struct rt6_info *)tuplehash->tuple.dst_cache;memset (skb->cb,0 ,sizeof (struct inet6_skb_parm ));
Signature · 1 line
2.30.2