vxlan: add implicit fdb entry for default destination
[linux-2.6-microblaze.git] / drivers / net / vxlan.c
index 117b7fa..bdfe46e 100644 (file)
@@ -68,17 +68,19 @@ struct vxlanhdr {
 
 /* UDP port for VXLAN traffic.
  * The IANA assigned port is 4789, but the Linux default is 8472
- * for compatability with early adopters.
+ * for compatibility with early adopters.
  */
-static unsigned int vxlan_port __read_mostly = 8472;
-module_param_named(udp_port, vxlan_port, uint, 0444);
+static unsigned short vxlan_port __read_mostly = 8472;
+module_param_named(udp_port, vxlan_port, ushort, 0444);
 MODULE_PARM_DESC(udp_port, "Destination UDP port");
 
 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");
 
-static unsigned int vxlan_net_id;
+static int vxlan_net_id;
+
+static const u8 all_zeros_mac[ETH_ALEN];
 
 /* per UDP socket information */
 struct vxlan_sock {
@@ -210,9 +212,9 @@ static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
 
 /* Fill in neighbour message in skbuff. */
 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
-                          const struct vxlan_fdb *fdb,
-                          u32 portid, u32 seq, int type, unsigned int flags,
-                          const struct vxlan_rdst *rdst)
+                         const struct vxlan_fdb *fdb,
+                         u32 portid, u32 seq, int type, unsigned int flags,
+                         const struct vxlan_rdst *rdst)
 {
        unsigned long now = jiffies;
        struct nda_cacheinfo ci;
@@ -250,7 +252,7 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
            nla_put_be16(skb, NDA_PORT, rdst->remote_port))
                goto nla_put_failure;
        if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
-           nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
+           nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
                goto nla_put_failure;
        if (rdst->remote_ifindex &&
            nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
@@ -311,14 +313,13 @@ errout:
 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
-       struct vxlan_fdb f;
-       struct vxlan_rdst remote;
-
-       memset(&f, 0, sizeof f);
-       f.state = NUD_STALE;
-
-       remote.remote_ip = ipa; /* goes to NDA_DST */
-       remote.remote_vni = VXLAN_N_VID;
+       struct vxlan_fdb f = {
+               .state = NUD_STALE,
+       };
+       struct vxlan_rdst remote = {
+               .remote_ip = ipa, /* goes to NDA_DST */
+               .remote_vni = VXLAN_N_VID,
+       };
 
        INIT_LIST_HEAD(&f.remotes);
        list_add_rcu(&remote.list, &f.remotes);
@@ -328,11 +329,11 @@ static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
 
 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
 {
-       struct vxlan_fdb        f;
+       struct vxlan_fdb f = {
+               .state = NUD_STALE,
+       };
 
-       memset(&f, 0, sizeof f);
        INIT_LIST_HEAD(&f.remotes);
-       f.state = NUD_STALE;
        memcpy(f.eth_addr, eth_addr, ETH_ALEN);
 
        vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
@@ -1032,7 +1033,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
        struct flowi4 fl4;
        __be32 dst;
        __be16 src_port, dst_port;
-        u32 vni;
+       u32 vni;
        __be16 df = 0;
        __u8 tos, ttl;
        int err;
@@ -1152,7 +1153,7 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct ethhdr *eth;
        bool did_rsc = false;
-       struct vxlan_rdst *rdst0, *rdst;
+       struct vxlan_rdst *rdst;
        struct vxlan_fdb *f;
 
        skb_reset_mac_header(skb);
@@ -1172,26 +1173,27 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
        }
 
        if (f == NULL) {
-               rdst0 = &vxlan->default_dst;
-
-               if (rdst0->remote_ip == htonl(INADDR_ANY) &&
-                   (vxlan->flags & VXLAN_F_L2MISS) &&
-                   !is_multicast_ether_addr(eth->h_dest))
-                       vxlan_fdb_miss(vxlan, eth->h_dest);
-       } else {
-               rdst = rdst0 = first_remote(f);
+               f = vxlan_find_mac(vxlan, all_zeros_mac);
+               if (f == NULL) {
+                       if ((vxlan->flags & VXLAN_F_L2MISS) &&
+                           !is_multicast_ether_addr(eth->h_dest))
+                               vxlan_fdb_miss(vxlan, eth->h_dest);
+
+                       dev->stats.tx_dropped++;
+                       dev_kfree_skb(skb);
+                       return NETDEV_TX_OK;
+               }
+       }
 
-               /* if there are multiple destinations, send copies */
-               list_for_each_entry_continue_rcu(rdst, &f->remotes, list) {
-                       struct sk_buff *skb1;
+       list_for_each_entry_rcu(rdst, &f->remotes, list) {
+               struct sk_buff *skb1;
 
-                       skb1 = skb_clone(skb, GFP_ATOMIC);
-                       if (skb1)
-                               vxlan_xmit_one(skb1, dev, rdst, did_rsc);
-               }
+               skb1 = skb_clone(skb, GFP_ATOMIC);
+               if (skb1)
+                       vxlan_xmit_one(skb1, dev, rdst, did_rsc);
        }
 
-       vxlan_xmit_one(skb, dev, rdst0, did_rsc);
+       dev_kfree_skb(skb);
        return NETDEV_TX_OK;
 }
 
@@ -1261,12 +1263,25 @@ static int vxlan_init(struct net_device *dev)
        return 0;
 }
 
+static void vxlan_fdb_delete_defualt(struct vxlan_dev *vxlan)
+{
+       struct vxlan_fdb *f;
+
+       spin_lock_bh(&vxlan->hash_lock);
+       f = __vxlan_find_mac(vxlan, all_zeros_mac);
+       if (f)
+               vxlan_fdb_destroy(vxlan, f);
+       spin_unlock_bh(&vxlan->hash_lock);
+}
+
 static void vxlan_uninit(struct net_device *dev)
 {
        struct vxlan_dev *vxlan = netdev_priv(dev);
        struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
        struct vxlan_sock *vs = vxlan->vn_sock;
 
+       vxlan_fdb_delete_defualt(vxlan);
+
        if (vs)
                vxlan_sock_release(vn, vs);
        free_percpu(dev->tstats);
@@ -1305,7 +1320,9 @@ static void vxlan_flush(struct vxlan_dev *vxlan)
                hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
                        struct vxlan_fdb *f
                                = container_of(p, struct vxlan_fdb, hlist);
-                       vxlan_fdb_destroy(vxlan, f);
+                       /* the all_zeros_mac entry is deleted at vxlan_uninit */
+                       if (!is_zero_ether_addr(f->eth_addr))
+                               vxlan_fdb_destroy(vxlan, f);
                }
        }
        spin_unlock_bh(&vxlan->hash_lock);
@@ -1485,6 +1502,7 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
        struct sockaddr_in vxlan_addr = {
                .sin_family = AF_INET,
                .sin_addr.s_addr = htonl(INADDR_ANY),
+               .sin_port = port,
        };
        int rc;
        unsigned int h;
@@ -1510,8 +1528,6 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
        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) {
@@ -1659,10 +1675,22 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,
 
        SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
 
-       err = register_netdevice(dev);
+       /* create an fdb entry for default destination */
+       err = vxlan_fdb_create(vxlan, all_zeros_mac,
+                              vxlan->default_dst.remote_ip,
+                              NUD_REACHABLE|NUD_PERMANENT,
+                              NLM_F_EXCL|NLM_F_CREATE,
+                              vxlan->dst_port, vxlan->default_dst.remote_vni,
+                              vxlan->default_dst.remote_ifindex, NTF_SELF);
        if (err)
                return err;
 
+       err = register_netdevice(dev);
+       if (err) {
+               vxlan_fdb_delete_defualt(vxlan);
+               return err;
+       }
+
        list_add(&vxlan->next, &vn->vxlan_list);
 
        return 0;