kernel: fix dst reference leak in flow offload
[openwrt/openwrt.git] / target / linux / generic / hack-4.14 / 650-netfilter-add-xt_OFFLOAD-target.patch
index 7296cfa6c460649ffa79bf26c9ece7ddd583219b..7c93feccdc70da6764358ce071040e435c6b1fad 100644 (file)
@@ -8,7 +8,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 
 --- a/net/ipv4/netfilter/Kconfig
 +++ b/net/ipv4/netfilter/Kconfig
-@@ -75,8 +75,6 @@ config NF_TABLES_ARP
+@@ -76,8 +76,6 @@ config NF_TABLES_ARP
        help
          This option enables the ARP support for nf_tables.
  
@@ -17,7 +17,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  config NF_FLOW_TABLE_IPV4
        tristate "Netfilter flow table IPv4 module"
        depends on NF_FLOW_TABLE
-@@ -85,6 +83,8 @@ config NF_FLOW_TABLE_IPV4
+@@ -86,6 +84,8 @@ config NF_FLOW_TABLE_IPV4
  
          To compile it as a module, choose M here.
  
@@ -28,7 +28,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
        depends on !NF_CONNTRACK || NF_CONNTRACK
 --- a/net/ipv6/netfilter/Kconfig
 +++ b/net/ipv6/netfilter/Kconfig
-@@ -69,7 +69,6 @@ config NFT_FIB_IPV6
+@@ -97,7 +97,6 @@ config NFT_FIB_IPV6
          multicast or blackhole.
  
  endif # NF_TABLES_IPV6
@@ -36,7 +36,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  
  config NF_FLOW_TABLE_IPV6
        tristate "Netfilter flow table IPv6 module"
-@@ -79,6 +78,8 @@ config NF_FLOW_TABLE_IPV6
+@@ -107,6 +106,8 @@ config NF_FLOW_TABLE_IPV6
  
          To compile it as a module, choose M here.
  
@@ -47,7 +47,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
        depends on !NF_CONNTRACK || NF_CONNTRACK
 --- a/net/netfilter/Kconfig
 +++ b/net/netfilter/Kconfig
-@@ -665,8 +665,6 @@ config NFT_FIB_NETDEV
+@@ -671,8 +671,6 @@ config NFT_FIB_NETDEV
  
  endif # NF_TABLES_NETDEV
  
@@ -56,7 +56,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  config NF_FLOW_TABLE_INET
        tristate "Netfilter flow table mixed IPv4/IPv6 module"
        depends on NF_FLOW_TABLE
-@@ -675,11 +673,12 @@ config NF_FLOW_TABLE_INET
+@@ -681,11 +679,12 @@ config NF_FLOW_TABLE_INET
  
          To compile it as a module, choose M here.
  
@@ -70,7 +70,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
        help
          This option adds the flow table core infrastructure.
  
-@@ -959,6 +958,15 @@ config NETFILTER_XT_TARGET_NOTRACK
+@@ -974,6 +973,15 @@ config NETFILTER_XT_TARGET_NOTRACK
        depends on NETFILTER_ADVANCED
        select NETFILTER_XT_TARGET_CT
  
@@ -88,7 +88,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
        depends on NETFILTER_ADVANCED
 --- a/net/netfilter/Makefile
 +++ b/net/netfilter/Makefile
-@@ -133,6 +133,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIF
+@@ -134,6 +134,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIF
  obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
  obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
  obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
@@ -98,7 +98,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
 --- /dev/null
 +++ b/net/netfilter/xt_FLOWOFFLOAD.c
-@@ -0,0 +1,351 @@
+@@ -0,0 +1,422 @@
 +/*
 + * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
 + *
@@ -109,8 +109,11 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +#include <linux/module.h>
 +#include <linux/init.h>
 +#include <linux/netfilter.h>
++#include <linux/netfilter/xt_FLOWOFFLOAD.h>
 +#include <net/ip.h>
 +#include <net/netfilter/nf_conntrack.h>
++#include <net/netfilter/nf_conntrack_extend.h>
++#include <net/netfilter/nf_conntrack_helper.h>
 +#include <net/netfilter/nf_flow_table.h>
 +
 +static struct nf_flowtable nf_flowtable;
@@ -278,32 +281,38 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +}
 +
 +static bool
-+xt_flowoffload_skip(struct sk_buff *skb)
++xt_flowoffload_skip(struct sk_buff *skb, int family)
 +{
-+      struct ip_options *opt = &(IPCB(skb)->opt);
-+
-+      if (unlikely(opt->optlen))
-+              return true;
 +      if (skb_sec_path(skb))
 +              return true;
 +
++      if (family == NFPROTO_IPV4) {
++              const struct ip_options *opt = &(IPCB(skb)->opt);
++
++              if (unlikely(opt->optlen))
++                      return true;
++      }
++
 +      return false;
 +}
 +
 +static struct dst_entry *
 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
-+                 const struct xt_action_param *par)
++                 const struct xt_action_param *par, int ifindex)
 +{
-+      struct dst_entry *dst;
++      struct dst_entry *dst = NULL;
 +      struct flowi fl;
 +
 +      memset(&fl, 0, sizeof(fl));
 +      switch (xt_family(par)) {
 +      case NFPROTO_IPV4:
 +              fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
++              fl.u.ip4.flowi4_oif = ifindex;
 +              break;
 +      case NFPROTO_IPV6:
++              fl.u.ip6.saddr = ct->tuplehash[dir].tuple.dst.u3.in6;
 +              fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
++              fl.u.ip6.flowi6_oif = ifindex;
 +              break;
 +      }
 +
@@ -319,15 +328,17 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +{
 +      struct dst_entry *this_dst, *other_dst;
 +
-+      this_dst = xt_flowoffload_dst(ct, dir, par);
-+      other_dst = xt_flowoffload_dst(ct, !dir, par);
-+      if (!this_dst || !other_dst)
-+              return -ENOENT;
++      this_dst = xt_flowoffload_dst(ct, !dir, par, xt_out(par)->ifindex);
++      other_dst = xt_flowoffload_dst(ct, dir, par, xt_in(par)->ifindex);
 +
 +      route->tuple[dir].dst           = this_dst;
-+      route->tuple[dir].ifindex       = xt_in(par)->ifindex;
 +      route->tuple[!dir].dst          = other_dst;
-+      route->tuple[!dir].ifindex      = xt_out(par)->ifindex;
++
++      if (!this_dst || !other_dst)
++              return -ENOENT;
++
++      if (dst_xfrm(this_dst) || dst_xfrm(other_dst))
++              return -EINVAL;
 +
 +      return 0;
 +}
@@ -335,13 +346,16 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +static unsigned int
 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
 +{
++      const struct xt_flowoffload_target_info *info = par->targinfo;
++      struct tcphdr _tcph, *tcph = NULL;
 +      enum ip_conntrack_info ctinfo;
 +      enum ip_conntrack_dir dir;
 +      struct nf_flow_route route;
-+      struct flow_offload *flow;
++      struct flow_offload *flow = NULL;
 +      struct nf_conn *ct;
++      struct net *net;
 +
-+      if (xt_flowoffload_skip(skb))
++      if (xt_flowoffload_skip(skb, xt_family(par)))
 +              return XT_CONTINUE;
 +
 +      ct = nf_ct_get(skb, &ctinfo);
@@ -352,6 +366,11 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +      case IPPROTO_TCP:
 +              if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
 +                      return XT_CONTINUE;
++
++              tcph = skb_header_pointer(skb, par->thoff,
++                                        sizeof(_tcph), &_tcph);
++              if (unlikely(!tcph || tcph->fin || tcph->rst))
++                      return XT_CONTINUE;
 +              break;
 +      case IPPROTO_UDP:
 +              break;
@@ -359,11 +378,11 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +              return XT_CONTINUE;
 +      }
 +
-+      if (test_bit(IPS_HELPER_BIT, &ct->status))
++      if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
++          ct->status & IPS_SEQ_ADJUST)
 +              return XT_CONTINUE;
 +
-+      if (ctinfo == IP_CT_NEW ||
-+          ctinfo == IP_CT_RELATED)
++      if (!nf_ct_is_confirmed(ct))
 +              return XT_CONTINUE;
 +
 +      if (!xt_in(par) || !xt_out(par))
@@ -374,12 +393,19 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +
 +      dir = CTINFO2DIR(ctinfo);
 +
-+      if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
-+              goto err_flow_route;
++      if (xt_flowoffload_route(skb, ct, par, &route, dir) == 0)
++              flow = flow_offload_alloc(ct, &route);
++
++      dst_release(route.tuple[dir].dst);
++      dst_release(route.tuple[!dir].dst);
 +
-+      flow = flow_offload_alloc(ct, &route);
 +      if (!flow)
-+              goto err_flow_alloc;
++              goto err_flow_route;
++
++      if (tcph) {
++              ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
++              ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
++      }
 +
 +      if (flow_offload_add(&nf_flowtable, flow) < 0)
 +              goto err_flow_add;
@@ -387,12 +413,17 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +      xt_flowoffload_check_device(xt_in(par));
 +      xt_flowoffload_check_device(xt_out(par));
 +
++      net = read_pnet(&nf_flowtable.ft_net);
++      if (!net)
++              write_pnet(&nf_flowtable.ft_net, xt_net(par));
++
++      if (info->flags & XT_FLOWOFFLOAD_HW)
++              nf_flow_offload_hw_add(xt_net(par), flow, ct);
++
 +      return XT_CONTINUE;
 +
 +err_flow_add:
 +      flow_offload_free(flow);
-+err_flow_alloc:
-+      dst_release(route.tuple[!dir].dst);
 +err_flow_route:
 +      clear_bit(IPS_OFFLOAD_BIT, &ct->status);
 +      return XT_CONTINUE;
@@ -401,6 +432,11 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +
 +static int flowoffload_chk(const struct xt_tgchk_param *par)
 +{
++      struct xt_flowoffload_target_info *info = par->targinfo;
++
++      if (info->flags & ~XT_FLOWOFFLOAD_MASK)
++              return -EINVAL;
++
 +      return 0;
 +}
 +
@@ -408,6 +444,8 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +      .family         = NFPROTO_UNSPEC,
 +      .name           = "FLOWOFFLOAD",
 +      .revision       = 0,
++      .targetsize     = sizeof(struct xt_flowoffload_target_info),
++      .usersize       = sizeof(struct xt_flowoffload_target_info),
 +      .checkentry     = flowoffload_chk,
 +      .target         = flowoffload_tg,
 +      .me             = THIS_MODULE,
@@ -415,6 +453,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +
 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
 +{
++      table->flags = NF_FLOWTABLE_F_HW;
 +      nf_flow_table_init(table);
 +      return 0;
 +}
@@ -424,10 +463,41 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +      nf_flow_table_free(table);
 +}
 +
++static int flow_offload_netdev_event(struct notifier_block *this,
++                                   unsigned long event, void *ptr)
++{
++      struct xt_flowoffload_hook *hook = NULL;
++      struct net_device *dev = netdev_notifier_info_to_dev(ptr);
++
++      if (event != NETDEV_UNREGISTER)
++              return NOTIFY_DONE;
++
++      spin_lock_bh(&hooks_lock);
++      hook = flow_offload_lookup_hook(dev);
++      if (hook) {
++              hlist_del(&hook->list);
++      }
++      spin_unlock_bh(&hooks_lock);
++      if (hook) {
++              nf_unregister_net_hook(hook->net, &hook->ops);
++              kfree(hook);
++      }
++
++      nf_flow_table_cleanup(dev_net(dev), dev);
++
++      return NOTIFY_DONE;
++}
++
++static struct notifier_block flow_offload_netdev_notifier = {
++      .notifier_call  = flow_offload_netdev_event,
++};
++
 +static int __init xt_flowoffload_tg_init(void)
 +{
 +      int ret;
 +
++      register_netdevice_notifier(&flow_offload_netdev_notifier);
++
 +      INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
 +
 +      ret = xt_flowoffload_table_init(&nf_flowtable);
@@ -445,6 +515,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +{
 +      xt_unregister_target(&offload_tg_reg);
 +      xt_flowoffload_table_cleanup(&nf_flowtable);
++      unregister_netdevice_notifier(&flow_offload_netdev_notifier);
 +}
 +
 +MODULE_LICENSE("GPL");
@@ -460,3 +531,23 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  #include <net/netfilter/nf_flow_table.h>
  #include <net/netfilter/nf_conntrack.h>
  #include <net/netfilter/nf_conntrack_core.h>
+--- /dev/null
++++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
+@@ -0,0 +1,17 @@
++/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
++#ifndef _XT_FLOWOFFLOAD_H
++#define _XT_FLOWOFFLOAD_H
++
++#include <linux/types.h>
++
++enum {
++      XT_FLOWOFFLOAD_HW       = 1 << 0,
++
++      XT_FLOWOFFLOAD_MASK     = XT_FLOWOFFLOAD_HW
++};
++
++struct xt_flowoffload_target_info {
++      __u32 flags;
++};
++
++#endif /* _XT_FLOWOFFLOAD_H */