ip_tunnels: extend iptunnel_xmit()
[firefly-linux-kernel-4.4.55.git] / drivers / net / vxlan.c
index 3b1d2ee7156b00195376c674f605f5749a2daf64..284c6c00c3539c1ddb584b8eb99cbd3f46ccdd6b 100644 (file)
@@ -44,6 +44,8 @@
 
 #define VXLAN_VERSION  "0.1"
 
+#define PORT_HASH_BITS 8
+#define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
 #define VNI_HASH_BITS  10
 #define VNI_HASH_SIZE  (1<<VNI_HASH_BITS)
 #define FDB_HASH_BITS  8
@@ -76,15 +78,25 @@ static bool log_ecn_error = true;
 module_param(log_ecn_error, bool, 0644);
 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
 
-/* per-net private data for this module */
 static unsigned int vxlan_net_id;
-struct vxlan_net {
-       struct socket     *sock;        /* UDP encap socket */
+
+/* per UDP socket information */
+struct vxlan_sock {
+       struct hlist_node hlist;
+       struct rcu_head   rcu;
+       struct work_struct del_work;
+       unsigned int      refcnt;
+       struct socket     *sock;
        struct hlist_head vni_list[VNI_HASH_SIZE];
 };
 
+/* per-network namespace private data for this module */
+struct vxlan_net {
+       struct list_head  vxlan_list;
+       struct hlist_head sock_list[PORT_HASH_SIZE];
+};
+
 struct vxlan_rdst {
-       struct rcu_head          rcu;
        __be32                   remote_ip;
        __be16                   remote_port;
        u32                      remote_vni;
@@ -106,7 +118,9 @@ struct vxlan_fdb {
 
 /* Pseudo network device */
 struct vxlan_dev {
-       struct hlist_node hlist;
+       struct hlist_node hlist;        /* vni hash table */
+       struct list_head  next;         /* vxlan's per namespace list */
+       struct vxlan_sock *vn_sock;     /* listening socket */
        struct net_device *dev;
        struct vxlan_rdst default_dst;  /* default destination */
        __be32            saddr;        /* source address */
@@ -135,19 +149,43 @@ struct vxlan_dev {
 /* salt for hash table */
 static u32 vxlan_salt __read_mostly;
 
-static inline struct hlist_head *vni_head(struct net *net, u32 id)
+/* Virtual Network hash table head */
+static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
+{
+       return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
+}
+
+/* Socket hash table head */
+static inline struct hlist_head *vs_head(struct net *net, __be16 port)
 {
        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
 
-       return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
+       return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
+}
+
+/* Find VXLAN socket based on network namespace and UDP port */
+static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
+{
+       struct vxlan_sock *vs;
+
+       hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
+               if (inet_sk(vs->sock->sk)->inet_sport == port)
+                       return vs;
+       }
+       return NULL;
 }
 
 /* Look up VNI in a per net namespace table */
-static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
+static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
 {
+       struct vxlan_sock *vs;
        struct vxlan_dev *vxlan;
 
-       hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
+       vs = vxlan_find_port(net, port);
+       if (!vs)
+               return NULL;
+
+       hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
                if (vxlan->default_dst.remote_vni == id)
                        return vxlan;
        }
@@ -565,18 +603,22 @@ skip:
 
 /* Watch incoming packets to learn mapping between Ethernet address
  * and Tunnel endpoint.
+ * Return true if packet is bogus and should be droppped.
  */
-static void vxlan_snoop(struct net_device *dev,
+static bool vxlan_snoop(struct net_device *dev,
                        __be32 src_ip, const u8 *src_mac)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct vxlan_fdb *f;
-       int err;
 
        f = vxlan_find_mac(vxlan, src_mac);
        if (likely(f)) {
                if (likely(f->remote.remote_ip == src_ip))
-                       return;
+                       return false;
+
+               /* Don't migrate static entries, drop packets */
+               if (f->state & NUD_NOARP)
+                       return true;
 
                if (net_ratelimit())
                        netdev_info(dev,
@@ -588,14 +630,19 @@ static void vxlan_snoop(struct net_device *dev,
        } else {
                /* learned new entry */
                spin_lock(&vxlan->hash_lock);
-               err = vxlan_fdb_create(vxlan, src_mac, src_ip,
-                                      NUD_REACHABLE,
-                                      NLM_F_EXCL|NLM_F_CREATE,
-                                      vxlan->dst_port,
-                                      vxlan->default_dst.remote_vni,
-                                      0, NTF_SELF);
+
+               /* close off race between vxlan_flush and incoming packets */
+               if (netif_running(dev))
+                       vxlan_fdb_create(vxlan, src_mac, src_ip,
+                                        NUD_REACHABLE,
+                                        NLM_F_EXCL|NLM_F_CREATE,
+                                        vxlan->dst_port,
+                                        vxlan->default_dst.remote_vni,
+                                        0, NTF_SELF);
                spin_unlock(&vxlan->hash_lock);
        }
+
+       return false;
 }
 
 
@@ -603,20 +650,18 @@ static void vxlan_snoop(struct net_device *dev,
 static bool vxlan_group_used(struct vxlan_net *vn,
                             const struct vxlan_dev *this)
 {
-       const struct vxlan_dev *vxlan;
-       unsigned h;
+       struct vxlan_dev *vxlan;
 
-       for (h = 0; h < VNI_HASH_SIZE; ++h)
-               hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
-                       if (vxlan == this)
-                               continue;
+       list_for_each_entry(vxlan, &vn->vxlan_list, next) {
+               if (vxlan == this)
+                       continue;
 
-                       if (!netif_running(vxlan->dev))
-                               continue;
+               if (!netif_running(vxlan->dev))
+                       continue;
 
-                       if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
-                               return true;
-               }
+               if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
+                       return true;
+       }
 
        return false;
 }
@@ -626,7 +671,7 @@ static int vxlan_join_group(struct net_device *dev)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
-       struct sock *sk = vn->sock->sk;
+       struct sock *sk = vxlan->vn_sock->sock->sk;
        struct ip_mreqn mreq = {
                .imr_multiaddr.s_addr   = vxlan->default_dst.remote_ip,
                .imr_ifindex            = vxlan->default_dst.remote_ifindex,
@@ -654,7 +699,7 @@ static int vxlan_leave_group(struct net_device *dev)
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
        int err = 0;
-       struct sock *sk = vn->sock->sk;
+       struct sock *sk = vxlan->vn_sock->sock->sk;
        struct ip_mreqn mreq = {
                .imr_multiaddr.s_addr   = vxlan->default_dst.remote_ip,
                .imr_ifindex            = vxlan->default_dst.remote_ifindex,
@@ -681,6 +726,7 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
        struct vxlanhdr *vxh;
        struct vxlan_dev *vxlan;
        struct pcpu_tstats *stats;
+       __be16 port;
        __u32 vni;
        int err;
 
@@ -704,9 +750,11 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 
        /* Is this VNI defined? */
        vni = ntohl(vxh->vx_vni) >> 8;
-       vxlan = vxlan_find_vni(sock_net(sk), vni);
+       port = inet_sk(sk)->inet_sport;
+       vxlan = vxlan_find_vni(sock_net(sk), vni, port);
        if (!vxlan) {
-               netdev_dbg(skb->dev, "unknown vni %d\n", vni);
+               netdev_dbg(skb->dev, "unknown vni %d port %u\n",
+                          vni, ntohs(port));
                goto drop;
        }
 
@@ -727,8 +775,9 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
                               vxlan->dev->dev_addr) == 0)
                goto drop;
 
-       if (vxlan->flags & VXLAN_F_LEARN)
-               vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
+       if ((vxlan->flags & VXLAN_F_LEARN) &&
+           vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
+               goto drop;
 
        __skb_tunnel_rx(skb, vxlan->dev);
        skb_reset_network_header(skb);
@@ -886,7 +935,7 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
        return false;
 }
 
-static void vxlan_sock_free(struct sk_buff *skb)
+static void vxlan_sock_put(struct sk_buff *skb)
 {
        sock_put(skb->sk);
 }
@@ -894,13 +943,13 @@ static void vxlan_sock_free(struct sk_buff *skb)
 /* On transmit, associate with the tunnel socket */
 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
 {
-       struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
-       struct sock *sk = vn->sock->sk;
+       struct vxlan_dev *vxlan = netdev_priv(dev);
+       struct sock *sk = vxlan->vn_sock->sock->sk;
 
        skb_orphan(skb);
        sock_hold(sk);
        skb->sk = sk;
-       skb->destructor = vxlan_sock_free;
+       skb->destructor = vxlan_sock_put;
 }
 
 /* Compute source port for outgoing packet
@@ -972,7 +1021,6 @@ static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct rtable *rt;
        const struct iphdr *old_iph;
-       struct iphdr *iph;
        struct vxlanhdr *vxh;
        struct udphdr *uh;
        struct flowi4 fl4;
@@ -981,6 +1029,7 @@ static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
         u32 vni;
        __be16 df = 0;
        __u8 tos, ttl;
+       int err;
 
        dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
        vni = rdst->remote_vni;
@@ -1042,19 +1091,12 @@ static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
                struct vxlan_dev *dst_vxlan;
 
                ip_rt_put(rt);
-               dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
+               dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
                if (!dst_vxlan)
                        goto tx_error;
                vxlan_encap_bypass(skb, vxlan, dst_vxlan);
                return NETDEV_TX_OK;
        }
-
-       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
-       IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
-                             IPSKB_REROUTED);
-       skb_dst_drop(skb);
-       skb_dst_set(skb, &rt->dst);
-
        vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
        vxh->vx_flags = htonl(VXLAN_FLAGS);
        vxh->vx_vni = htonl(vni << 8);
@@ -1069,27 +1111,18 @@ static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
        uh->len = htons(skb->len);
        uh->check = 0;
 
-       __skb_push(skb, sizeof(*iph));
-       skb_reset_network_header(skb);
-       iph             = ip_hdr(skb);
-       iph->version    = 4;
-       iph->ihl        = sizeof(struct iphdr) >> 2;
-       iph->frag_off   = df;
-       iph->protocol   = IPPROTO_UDP;
-       iph->tos        = ip_tunnel_ecn_encap(tos, old_iph, skb);
-       iph->daddr      = dst;
-       iph->saddr      = fl4.saddr;
-       iph->ttl        = ttl ? : ip4_dst_hoplimit(&rt->dst);
-       tunnel_ip_select_ident(skb, old_iph, &rt->dst);
-
-       nf_reset(skb);
-
        vxlan_set_owner(dev, skb);
 
        if (handle_offloads(skb))
                goto drop;
 
-       iptunnel_xmit(skb, dev);
+       tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
+       ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
+
+       err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
+                           IPPROTO_UDP, tos, ttl, df);
+       iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
+
        return NETDEV_TX_OK;
 
 drop:
@@ -1151,9 +1184,11 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
                struct sk_buff *skb1;
 
                skb1 = skb_clone(skb, GFP_ATOMIC);
-               rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
-               if (rc == NETDEV_TX_OK)
-                       rc = rc1;
+               if (skb1) {
+                       rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
+                       if (rc == NETDEV_TX_OK)
+                               rc = rc1;
+               }
        }
 
        rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
@@ -1230,7 +1265,7 @@ static int vxlan_open(struct net_device *dev)
 /* Purge the forwarding table */
 static void vxlan_flush(struct vxlan_dev *vxlan)
 {
-       unsigned h;
+       unsigned int h;
 
        spin_lock_bh(&vxlan->hash_lock);
        for (h = 0; h < FDB_HASH_SIZE; ++h) {
@@ -1294,7 +1329,7 @@ static void vxlan_free(struct net_device *dev)
 static void vxlan_setup(struct net_device *dev)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
-       unsigned h;
+       unsigned int h;
        int low, high;
 
        eth_hw_addr_random(dev);
@@ -1317,6 +1352,7 @@ static void vxlan_setup(struct net_device *dev)
        dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
        dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
 
+       INIT_LIST_HEAD(&vxlan->next);
        spin_lock_init(&vxlan->hash_lock);
 
        init_timer_deferrable(&vxlan->age_timer);
@@ -1401,11 +1437,78 @@ static const struct ethtool_ops vxlan_ethtool_ops = {
        .get_link       = ethtool_op_get_link,
 };
 
+static void vxlan_del_work(struct work_struct *work)
+{
+       struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
+
+       sk_release_kernel(vs->sock->sk);
+       kfree_rcu(vs, rcu);
+}
+
+/* Create new listen socket if needed */
+static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
+{
+       struct vxlan_sock *vs;
+       struct sock *sk;
+       struct sockaddr_in vxlan_addr = {
+               .sin_family = AF_INET,
+               .sin_addr.s_addr = htonl(INADDR_ANY),
+       };
+       int rc;
+       unsigned int h;
+
+       vs = kmalloc(sizeof(*vs), GFP_KERNEL);
+       if (!vs)
+               return ERR_PTR(-ENOMEM);
+
+       for (h = 0; h < VNI_HASH_SIZE; ++h)
+               INIT_HLIST_HEAD(&vs->vni_list[h]);
+
+       INIT_WORK(&vs->del_work, vxlan_del_work);
+
+       /* Create UDP socket for encapsulation receive. */
+       rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
+       if (rc < 0) {
+               pr_debug("UDP socket create failed\n");
+               kfree(vs);
+               return ERR_PTR(rc);
+       }
+
+       /* Put in proper namespace */
+       sk = vs->sock->sk;
+       sk_change_net(sk, net);
+
+       vxlan_addr.sin_port = port;
+
+       rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
+                        sizeof(vxlan_addr));
+       if (rc < 0) {
+               pr_debug("bind for UDP socket %pI4:%u (%d)\n",
+                        &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
+               sk_release_kernel(sk);
+               kfree(vs);
+               return ERR_PTR(rc);
+       }
+
+       /* Disable multicast loopback */
+       inet_sk(sk)->mc_loop = 0;
+
+       /* Mark socket as an encapsulation socket. */
+       udp_sk(sk)->encap_type = 1;
+       udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
+       udp_encap_enable();
+
+       vs->refcnt = 1;
+       return vs;
+}
+
 static int vxlan_newlink(struct net *net, struct net_device *dev,
                         struct nlattr *tb[], struct nlattr *data[])
 {
+       struct vxlan_net *vn = net_generic(net, vxlan_net_id);
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct vxlan_rdst *dst = &vxlan->default_dst;
+       struct vxlan_sock *vs;
        __u32 vni;
        int err;
 
@@ -1413,10 +1516,6 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,
                return -EINVAL;
 
        vni = nla_get_u32(data[IFLA_VXLAN_ID]);
-       if (vxlan_find_vni(net, vni)) {
-               pr_info("duplicate VNI %u\n", vni);
-               return -EEXIST;
-       }
        dst->remote_vni = vni;
 
        if (data[IFLA_VXLAN_GROUP])
@@ -1482,22 +1581,58 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,
        if (data[IFLA_VXLAN_PORT])
                vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
 
+       if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
+               pr_info("duplicate VNI %u\n", vni);
+               return -EEXIST;
+       }
+
+       vs = vxlan_find_port(net, vxlan->dst_port);
+       if (vs)
+               ++vs->refcnt;
+       else {
+               /* Drop lock because socket create acquires RTNL lock */
+               rtnl_unlock();
+               vs = vxlan_socket_create(net, vxlan->dst_port);
+               rtnl_lock();
+               if (IS_ERR(vs))
+                       return PTR_ERR(vs);
+
+               hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
+       }
+       vxlan->vn_sock = vs;
+
        SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
 
        err = register_netdevice(dev);
-       if (!err)
-               hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
+       if (err) {
+               if (--vs->refcnt == 0) {
+                       rtnl_unlock();
+                       sk_release_kernel(vs->sock->sk);
+                       kfree(vs);
+                       rtnl_lock();
+               }
+               return err;
+       }
 
-       return err;
+       list_add(&vxlan->next, &vn->vxlan_list);
+       hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
+
+       return 0;
 }
 
 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
+       struct vxlan_sock *vs = vxlan->vn_sock;
 
        hlist_del_rcu(&vxlan->hlist);
-
+       list_del(&vxlan->next);
        unregister_netdevice_queue(dev, head);
+
+       if (--vs->refcnt == 0) {
+               hlist_del_rcu(&vs->hlist);
+               schedule_work(&vs->del_work);
+       }
 }
 
 static size_t vxlan_get_size(const struct net_device *dev)
@@ -1583,46 +1718,12 @@ static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
 static __net_init int vxlan_init_net(struct net *net)
 {
        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
-       struct sock *sk;
-       struct sockaddr_in vxlan_addr = {
-               .sin_family = AF_INET,
-               .sin_addr.s_addr = htonl(INADDR_ANY),
-       };
-       int rc;
-       unsigned h;
-
-       /* Create UDP socket for encapsulation receive. */
-       rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
-       if (rc < 0) {
-               pr_debug("UDP socket create failed\n");
-               return rc;
-       }
-       /* Put in proper namespace */
-       sk = vn->sock->sk;
-       sk_change_net(sk, net);
-
-       vxlan_addr.sin_port = htons(vxlan_port);
-
-       rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
-                        sizeof(vxlan_addr));
-       if (rc < 0) {
-               pr_debug("bind for UDP socket %pI4:%u (%d)\n",
-                        &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
-               sk_release_kernel(sk);
-               vn->sock = NULL;
-               return rc;
-       }
+       unsigned int h;
 
-       /* Disable multicast loopback */
-       inet_sk(sk)->mc_loop = 0;
+       INIT_LIST_HEAD(&vn->vxlan_list);
 
-       /* Mark socket as an encapsulation socket. */
-       udp_sk(sk)->encap_type = 1;
-       udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
-       udp_encap_enable();
-
-       for (h = 0; h < VNI_HASH_SIZE; ++h)
-               INIT_HLIST_HEAD(&vn->vni_list[h]);
+       for (h = 0; h < PORT_HASH_SIZE; ++h)
+               INIT_HLIST_HEAD(&vn->sock_list[h]);
 
        return 0;
 }
@@ -1631,18 +1732,11 @@ static __net_exit void vxlan_exit_net(struct net *net)
 {
        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
        struct vxlan_dev *vxlan;
-       unsigned h;
 
        rtnl_lock();
-       for (h = 0; h < VNI_HASH_SIZE; ++h)
-               hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
-                       dev_close(vxlan->dev);
+       list_for_each_entry(vxlan, &vn->vxlan_list, next)
+               dev_close(vxlan->dev);
        rtnl_unlock();
-
-       if (vn->sock) {
-               sk_release_kernel(vn->sock->sk);
-               vn->sock = NULL;
-       }
 }
 
 static struct pernet_operations vxlan_net_ops = {
@@ -1673,7 +1767,7 @@ out2:
 out1:
        return rc;
 }
-module_init(vxlan_init_module);
+late_initcall(vxlan_init_module);
 
 static void __exit vxlan_cleanup_module(void)
 {