From: wenxu <we...@ucloud.cn>

This patch separate the inet family operation in nf_flow_table_ip to single
function. Prepare for support the bridge family.

Signed-off-by: wenxu <we...@ucloud.cn>
---
 net/netfilter/nf_flow_table_ip.c | 72 ++++++++++++++++++++++++----------------
 1 file changed, 43 insertions(+), 29 deletions(-)

diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
index 4304070..88b4d59 100644
--- a/net/netfilter/nf_flow_table_ip.c
+++ b/net/netfilter/nf_flow_table_ip.c
@@ -214,6 +214,25 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, 
unsigned int mtu)
        return true;
 }
 
+static void nf_flow_ipv4_xmit(struct flow_offload *flow, struct sk_buff *skb,
+                             enum flow_offload_tuple_dir dir)
+{
+       struct net_device *outdev;
+       struct rtable *rt;
+       struct iphdr *iph;
+       __be32 nexthop;
+
+       rt = (struct rtable *)flow->tuplehash[dir].tuple.dst.dst_cache;
+       outdev = rt->dst.dev;
+       iph = ip_hdr(skb);
+       ip_decrease_ttl(iph);
+
+       skb->dev = outdev;
+       nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
+       skb_dst_set_noref(skb, &rt->dst);
+       neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
+}
+
 unsigned int
 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
                        const struct nf_hook_state *state)
@@ -223,11 +242,7 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, 
unsigned int mtu)
        struct flow_offload_tuple tuple = {};
        enum flow_offload_tuple_dir dir;
        struct flow_offload *flow;
-       struct net_device *outdev;
-       struct rtable *rt;
        unsigned int thoff;
-       struct iphdr *iph;
-       __be32 nexthop;
 
        if (skb->protocol != htons(ETH_P_IP))
                return NF_ACCEPT;
@@ -241,13 +256,11 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff 
*skb, unsigned int mtu)
 
        dir = tuplehash->tuple.dir;
        flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
-       rt = (struct rtable *)flow->tuplehash[dir].tuple.dst.dst_cache;
-       outdev = rt->dst.dev;
 
        if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
                return NF_ACCEPT;
 
-       if (skb_try_make_writable(skb, sizeof(*iph)))
+       if (skb_try_make_writable(skb, sizeof(struct iphdr)))
                return NF_DROP;
 
        thoff = ip_hdr(skb)->ihl * 4;
@@ -258,13 +271,7 @@ static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, 
unsigned int mtu)
                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);
-       skb_dst_set_noref(skb, &rt->dst);
-       neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
+       nf_flow_ipv4_xmit(flow, skb, dir);
 
        return NF_STOLEN;
 }
@@ -431,6 +438,25 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const 
struct net_device *dev,
        return 0;
 }
 
+static void nf_flow_ipv6_xmit(struct flow_offload *flow, struct sk_buff *skb,
+                             enum flow_offload_tuple_dir dir)
+{
+       const struct in6_addr *nexthop;
+       struct net_device *outdev;
+       struct ipv6hdr *ip6h;
+       struct rt6_info *rt;
+
+       rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst.dst_cache;
+       outdev = rt->dst.dev;
+       ip6h = ipv6_hdr(skb);
+       ip6h->hop_limit--;
+
+       skb->dev = outdev;
+       nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
+       skb_dst_set_noref(skb, &rt->dst);
+       neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
+}
+
 unsigned int
 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
                          const struct nf_hook_state *state)
@@ -439,11 +465,7 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const 
struct net_device *dev,
        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;
-       struct ipv6hdr *ip6h;
-       struct rt6_info *rt;
 
        if (skb->protocol != htons(ETH_P_IPV6))
                return NF_ACCEPT;
@@ -457,30 +479,22 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const 
struct net_device *dev,
 
        dir = tuplehash->tuple.dir;
        flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
-       rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst.dst_cache;
-       outdev = rt->dst.dev;
 
        if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
                return NF_ACCEPT;
 
        if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
-                               sizeof(*ip6h)))
+                               sizeof(struct ipv6hdr)))
                return NF_ACCEPT;
 
-       if (skb_try_make_writable(skb, sizeof(*ip6h)))
+       if (skb_try_make_writable(skb, sizeof(struct ipv6hdr)))
                return NF_DROP;
 
        if (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);
-       skb_dst_set_noref(skb, &rt->dst);
-       neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
+       nf_flow_ipv6_xmit(flow, skb, dir);
 
        return NF_STOLEN;
 }
-- 
1.8.3.1

Reply via email to