Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[linux-2.6-microblaze.git] / net / netfilter / nf_flow_table_core.c
index c77ba86..39c02d1 100644 (file)
@@ -74,16 +74,25 @@ err_ct_refcnt:
 }
 EXPORT_SYMBOL_GPL(flow_offload_alloc);
 
+static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
+{
+       const struct rt6_info *rt;
+
+       if (flow_tuple->l3proto == NFPROTO_IPV6) {
+               rt = (const struct rt6_info *)flow_tuple->dst_cache;
+               return rt6_get_cookie(rt);
+       }
+
+       return 0;
+}
+
 static int flow_offload_fill_route(struct flow_offload *flow,
                                   const struct nf_flow_route *route,
                                   enum flow_offload_tuple_dir dir)
 {
        struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
-       struct dst_entry *other_dst = route->tuple[!dir].dst;
        struct dst_entry *dst = route->tuple[dir].dst;
-
-       if (!dst_hold_safe(route->tuple[dir].dst))
-               return -1;
+       int i, j = 0;
 
        switch (flow_tuple->l3proto) {
        case NFPROTO_IPV4:
@@ -94,12 +103,50 @@ static int flow_offload_fill_route(struct flow_offload *flow,
                break;
        }
 
-       flow_tuple->iifidx = other_dst->dev->ifindex;
-       flow_tuple->dst_cache = dst;
+       flow_tuple->iifidx = route->tuple[dir].in.ifindex;
+       for (i = route->tuple[dir].in.num_encaps - 1; i >= 0; i--) {
+               flow_tuple->encap[j].id = route->tuple[dir].in.encap[i].id;
+               flow_tuple->encap[j].proto = route->tuple[dir].in.encap[i].proto;
+               if (route->tuple[dir].in.ingress_vlans & BIT(i))
+                       flow_tuple->in_vlan_ingress |= BIT(j);
+               j++;
+       }
+       flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
+
+       switch (route->tuple[dir].xmit_type) {
+       case FLOW_OFFLOAD_XMIT_DIRECT:
+               memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,
+                      ETH_ALEN);
+               memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
+                      ETH_ALEN);
+               flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
+               flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
+               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;
+       default:
+               WARN_ON_ONCE(1);
+               break;
+       }
+       flow_tuple->xmit_type = route->tuple[dir].xmit_type;
 
        return 0;
 }
 
+static void nft_flow_dst_release(struct flow_offload *flow,
+                                enum flow_offload_tuple_dir dir)
+{
+       if (flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
+           flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)
+               dst_release(flow->tuplehash[dir].tuple.dst_cache);
+}
+
 int flow_offload_route_init(struct flow_offload *flow,
                            const struct nf_flow_route *route)
 {
@@ -118,7 +165,7 @@ int flow_offload_route_init(struct flow_offload *flow,
        return 0;
 
 err_route_reply:
-       dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
+       nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
 
        return err;
 }
@@ -169,8 +216,8 @@ static void flow_offload_fixup_ct(struct nf_conn *ct)
 
 static void flow_offload_route_release(struct flow_offload *flow)
 {
-       dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
-       dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
+       nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
+       nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
 }
 
 void flow_offload_free(struct flow_offload *flow)
@@ -359,11 +406,33 @@ nf_flow_table_iterate(struct nf_flowtable *flow_table,
        return err;
 }
 
+static bool flow_offload_stale_dst(struct flow_offload_tuple *tuple)
+{
+       struct dst_entry *dst;
+
+       if (tuple->xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
+           tuple->xmit_type == FLOW_OFFLOAD_XMIT_XFRM) {
+               dst = tuple->dst_cache;
+               if (!dst_check(dst, tuple->dst_cookie))
+                       return true;
+       }
+
+       return false;
+}
+
+static bool nf_flow_has_stale_dst(struct flow_offload *flow)
+{
+       return flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
+              flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple);
+}
+
 static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
 {
        struct nf_flowtable *flow_table = data;
 
-       if (nf_flow_has_expired(flow) || nf_ct_is_dying(flow->ct))
+       if (nf_flow_has_expired(flow) ||
+           nf_ct_is_dying(flow->ct) ||
+           nf_flow_has_stale_dst(flow))
                set_bit(NF_FLOW_TEARDOWN, &flow->flags);
 
        if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
@@ -389,29 +458,20 @@ static void nf_flow_offload_work_gc(struct work_struct *work)
        queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
 }
 
-
-static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
-                               __be16 port, __be16 new_port)
+static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
+                                __be16 port, __be16 new_port)
 {
        struct tcphdr *tcph;
 
-       if (skb_try_make_writable(skb, thoff + sizeof(*tcph)))
-               return -1;
-
        tcph = (void *)(skb_network_header(skb) + thoff);
        inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
-
-       return 0;
 }
 
-static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
-                               __be16 port, __be16 new_port)
+static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
+                                __be16 port, __be16 new_port)
 {
        struct udphdr *udph;
 
-       if (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_replace2(&udph->check, skb, port,
@@ -419,37 +479,28 @@ static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
                if (!udph->check)
                        udph->check = CSUM_MANGLED_0;
        }
-
-       return 0;
 }
 
-static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
-                           u8 protocol, __be16 port, __be16 new_port)
+static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
+                            u8 protocol, __be16 port, __be16 new_port)
 {
        switch (protocol) {
        case IPPROTO_TCP:
-               if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
-                       return NF_DROP;
+               nf_flow_nat_port_tcp(skb, thoff, port, new_port);
                break;
        case IPPROTO_UDP:
-               if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
-                       return NF_DROP;
+               nf_flow_nat_port_udp(skb, thoff, port, new_port);
                break;
        }
-
-       return 0;
 }
 
-int nf_flow_snat_port(const struct flow_offload *flow,
-                     struct sk_buff *skb, unsigned int thoff,
-                     u8 protocol, enum flow_offload_tuple_dir dir)
+void nf_flow_snat_port(const struct flow_offload *flow,
+                      struct sk_buff *skb, unsigned int thoff,
+                      u8 protocol, enum flow_offload_tuple_dir dir)
 {
        struct flow_ports *hdr;
        __be16 port, new_port;
 
-       if (skb_try_make_writable(skb, thoff + sizeof(*hdr)))
-               return -1;
-
        hdr = (void *)(skb_network_header(skb) + thoff);
 
        switch (dir) {
@@ -463,24 +514,19 @@ int nf_flow_snat_port(const struct flow_offload *flow,
                new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
                hdr->dest = new_port;
                break;
-       default:
-               return -1;
        }
 
-       return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
+       nf_flow_nat_port(skb, thoff, protocol, port, new_port);
 }
 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
 
-int nf_flow_dnat_port(const struct flow_offload *flow,
-                     struct sk_buff *skb, unsigned int thoff,
-                     u8 protocol, enum flow_offload_tuple_dir dir)
+void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
+                      unsigned int thoff, u8 protocol,
+                      enum flow_offload_tuple_dir dir)
 {
        struct flow_ports *hdr;
        __be16 port, new_port;
 
-       if (skb_try_make_writable(skb, thoff + sizeof(*hdr)))
-               return -1;
-
        hdr = (void *)(skb_network_header(skb) + thoff);
 
        switch (dir) {
@@ -494,11 +540,9 @@ int nf_flow_dnat_port(const struct flow_offload *flow,
                new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
                hdr->source = new_port;
                break;
-       default:
-               return -1;
        }
 
-       return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
+       nf_flow_nat_port(skb, thoff, protocol, port, new_port);
 }
 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);