Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[firefly-linux-kernel-4.4.55.git] / net / batman-adv / gateway_client.c
index 2acd7a666bdab09ead4332accbb599aa429796a3..61605a0f3f39464b8e87a7512c97caf23de59504 100644 (file)
 #include <linux/udp.h>
 #include <linux/if_vlan.h>
 
-static void gw_node_free_rcu(struct rcu_head *rcu)
-{
-       struct gw_node *gw_node;
-
-       gw_node = container_of(rcu, struct gw_node, rcu);
-       kfree(gw_node);
-}
-
 static void gw_node_free_ref(struct gw_node *gw_node)
 {
        if (atomic_dec_and_test(&gw_node->refcount))
-               call_rcu(&gw_node->rcu, gw_node_free_rcu);
+               kfree_rcu(gw_node, rcu);
 }
 
 static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
@@ -127,7 +119,7 @@ void gw_election(struct bat_priv *bat_priv)
                return;
 
        curr_gw = gw_get_selected_gw_node(bat_priv);
-       if (!curr_gw)
+       if (curr_gw)
                goto out;
 
        rcu_read_lock();
@@ -310,9 +302,13 @@ void gw_node_update(struct bat_priv *bat_priv,
        struct hlist_node *node;
        struct gw_node *gw_node, *curr_gw;
 
+       /**
+        * Note: We don't need a NULL check here, since curr_gw never gets
+        * dereferenced. If curr_gw is NULL we also should not exit as we may
+        * have this gateway in our list (duplication check!) even though we
+        * have no currently selected gateway.
+        */
        curr_gw = gw_get_selected_gw_node(bat_priv);
-       if (!curr_gw)
-               goto out;
 
        rcu_read_lock();
        hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
@@ -350,7 +346,7 @@ deselect:
        gw_deselect(bat_priv);
 unlock:
        rcu_read_unlock();
-out:
+
        if (curr_gw)
                gw_node_free_ref(curr_gw);
 }
@@ -435,30 +431,32 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)
 {
        struct net_device *net_dev = (struct net_device *)seq->private;
        struct bat_priv *bat_priv = netdev_priv(net_dev);
+       struct hard_iface *primary_if;
        struct gw_node *gw_node;
        struct hlist_node *node;
-       int gw_count = 0;
+       int gw_count = 0, ret = 0;
 
-       if (!bat_priv->primary_if) {
-
-               return seq_printf(seq, "BATMAN mesh %s disabled - please "
-                                 "specify interfaces to enable it\n",
-                                 net_dev->name);
+       primary_if = primary_if_get_selected(bat_priv);
+       if (!primary_if) {
+               ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
+                                "specify interfaces to enable it\n",
+                                net_dev->name);
+               goto out;
        }
 
-       if (bat_priv->primary_if->if_status != IF_ACTIVE) {
-
-               return seq_printf(seq, "BATMAN mesh %s disabled - "
-                                      "primary interface not active\n",
-                                      net_dev->name);
+       if (primary_if->if_status != IF_ACTIVE) {
+               ret = seq_printf(seq, "BATMAN mesh %s disabled - "
+                                "primary interface not active\n",
+                                net_dev->name);
+               goto out;
        }
 
        seq_printf(seq, "      %-12s (%s/%i) %17s [%10s]: gw_class ... "
                   "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
                   "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
                   "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
-                  bat_priv->primary_if->net_dev->name,
-                  bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
+                  primary_if->net_dev->name,
+                  primary_if->net_dev->dev_addr, net_dev->name);
 
        rcu_read_lock();
        hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
@@ -476,7 +474,10 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)
        if (gw_count == 0)
                seq_printf(seq, "No gateways in range ...\n");
 
-       return 0;
+out:
+       if (primary_if)
+               hardif_free_ref(primary_if);
+       return ret;
 }
 
 int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)