Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / net / batman-adv / soft-interface.c
index c5cb0a7f834977023a56d1fe0f953d595226804e..403b8c46085e40a667da281c8352781197f6539b 100644 (file)
@@ -417,7 +417,6 @@ static void batadv_softif_destroy_finish(struct work_struct *work)
                                cleanup_work);
        soft_iface = bat_priv->soft_iface;
 
-       batadv_debugfs_del_meshif(soft_iface);
        batadv_sysfs_del_meshif(soft_iface);
 
        rtnl_lock();
@@ -510,6 +509,58 @@ free_bat_counters:
        return ret;
 }
 
+/**
+ * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
+ * @dev: batadv_soft_interface used as master interface
+ * @slave_dev: net_device which should become the slave interface
+ *
+ * Return 0 if successful or error otherwise.
+ */
+static int batadv_softif_slave_add(struct net_device *dev,
+                                  struct net_device *slave_dev)
+{
+       struct batadv_hard_iface *hard_iface;
+       int ret = -EINVAL;
+
+       hard_iface = batadv_hardif_get_by_netdev(slave_dev);
+       if (!hard_iface || hard_iface->soft_iface != NULL)
+               goto out;
+
+       ret = batadv_hardif_enable_interface(hard_iface, dev->name);
+
+out:
+       if (hard_iface)
+               batadv_hardif_free_ref(hard_iface);
+       return ret;
+}
+
+/**
+ * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
+ * @dev: batadv_soft_interface used as master interface
+ * @slave_dev: net_device which should be removed from the master interface
+ *
+ * Return 0 if successful or error otherwise.
+ */
+static int batadv_softif_slave_del(struct net_device *dev,
+                                  struct net_device *slave_dev)
+{
+       struct batadv_hard_iface *hard_iface;
+       int ret = -EINVAL;
+
+       hard_iface = batadv_hardif_get_by_netdev(slave_dev);
+
+       if (!hard_iface || hard_iface->soft_iface != dev)
+               goto out;
+
+       batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
+       ret = 0;
+
+out:
+       if (hard_iface)
+               batadv_hardif_free_ref(hard_iface);
+       return ret;
+}
+
 static const struct net_device_ops batadv_netdev_ops = {
        .ndo_init = batadv_softif_init_late,
        .ndo_open = batadv_interface_open,
@@ -518,9 +569,22 @@ static const struct net_device_ops batadv_netdev_ops = {
        .ndo_set_mac_address = batadv_interface_set_mac_addr,
        .ndo_change_mtu = batadv_interface_change_mtu,
        .ndo_start_xmit = batadv_interface_tx,
-       .ndo_validate_addr = eth_validate_addr
+       .ndo_validate_addr = eth_validate_addr,
+       .ndo_add_slave = batadv_softif_slave_add,
+       .ndo_del_slave = batadv_softif_slave_del,
 };
 
+/**
+ * batadv_softif_free - Deconstructor of batadv_soft_interface
+ * @dev: Device to cleanup and remove
+ */
+static void batadv_softif_free(struct net_device *dev)
+{
+       batadv_debugfs_del_meshif(dev);
+       batadv_mesh_free(dev);
+       free_netdev(dev);
+}
+
 /**
  * batadv_softif_init_early - early stage initialization of soft interface
  * @dev: registered network device to modify
@@ -532,7 +596,7 @@ static void batadv_softif_init_early(struct net_device *dev)
        ether_setup(dev);
 
        dev->netdev_ops = &batadv_netdev_ops;
-       dev->destructor = free_netdev;
+       dev->destructor = batadv_softif_free;
        dev->tx_queue_len = 0;
 
        /* can't call min_mtu, because the needed variables
@@ -560,6 +624,8 @@ struct net_device *batadv_softif_create(const char *name)
        if (!soft_iface)
                return NULL;
 
+       soft_iface->rtnl_link_ops = &batadv_link_ops;
+
        ret = register_netdevice(soft_iface);
        if (ret < 0) {
                pr_err("Unable to register the batman interface '%s': %i\n",
@@ -571,14 +637,37 @@ struct net_device *batadv_softif_create(const char *name)
        return soft_iface;
 }
 
-void batadv_softif_destroy(struct net_device *soft_iface)
+/**
+ * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
+ * @soft_iface: the to-be-removed batman-adv interface
+ */
+void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
 {
        struct batadv_priv *bat_priv = netdev_priv(soft_iface);
 
-       batadv_mesh_free(soft_iface);
        queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
 }
 
+/**
+ * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
+ * @soft_iface: the to-be-removed batman-adv interface
+ * @head: list pointer
+ */
+static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
+                                         struct list_head *head)
+{
+       struct batadv_hard_iface *hard_iface;
+
+       list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
+               if (hard_iface->soft_iface == soft_iface)
+                       batadv_hardif_disable_interface(hard_iface,
+                                                       BATADV_IF_CLEANUP_KEEP);
+       }
+
+       batadv_sysfs_del_meshif(soft_iface);
+       unregister_netdevice_queue(soft_iface, head);
+}
+
 int batadv_softif_is_valid(const struct net_device *net_dev)
 {
        if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
@@ -587,6 +676,13 @@ int batadv_softif_is_valid(const struct net_device *net_dev)
        return 0;
 }
 
+struct rtnl_link_ops batadv_link_ops __read_mostly = {
+       .kind           = "batadv",
+       .priv_size      = sizeof(struct batadv_priv),
+       .setup          = batadv_softif_init_early,
+       .dellink        = batadv_softif_destroy_netlink,
+};
+
 /* ethtool */
 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {