linux/net/batman-adv/bat_v.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) B.A.T.M.A.N. contributors:
   3 *
   4 * Linus Lüssing, Marek Lindner
   5 */
   6
   7#include "bat_v.h"
   8#include "main.h"
   9
  10#include <linux/atomic.h>
  11#include <linux/cache.h>
  12#include <linux/errno.h>
  13#include <linux/if_ether.h>
  14#include <linux/init.h>
  15#include <linux/jiffies.h>
  16#include <linux/kref.h>
  17#include <linux/list.h>
  18#include <linux/minmax.h>
  19#include <linux/netdevice.h>
  20#include <linux/netlink.h>
  21#include <linux/rculist.h>
  22#include <linux/rcupdate.h>
  23#include <linux/skbuff.h>
  24#include <linux/spinlock.h>
  25#include <linux/stddef.h>
  26#include <linux/types.h>
  27#include <linux/workqueue.h>
  28#include <net/genetlink.h>
  29#include <net/netlink.h>
  30#include <uapi/linux/batadv_packet.h>
  31#include <uapi/linux/batman_adv.h>
  32
  33#include "bat_algo.h"
  34#include "bat_v_elp.h"
  35#include "bat_v_ogm.h"
  36#include "gateway_client.h"
  37#include "gateway_common.h"
  38#include "hard-interface.h"
  39#include "hash.h"
  40#include "log.h"
  41#include "netlink.h"
  42#include "originator.h"
  43
  44static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
  45{
  46        struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  47        struct batadv_hard_iface *primary_if;
  48
  49        primary_if = batadv_primary_if_get_selected(bat_priv);
  50
  51        if (primary_if) {
  52                batadv_v_elp_iface_activate(primary_if, hard_iface);
  53                batadv_hardif_put(primary_if);
  54        }
  55
  56        /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
  57         * set the interface as ACTIVE right away, without any risk of race
  58         * condition
  59         */
  60        if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  61                hard_iface->if_status = BATADV_IF_ACTIVE;
  62}
  63
  64static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
  65{
  66        int ret;
  67
  68        ret = batadv_v_elp_iface_enable(hard_iface);
  69        if (ret < 0)
  70                return ret;
  71
  72        ret = batadv_v_ogm_iface_enable(hard_iface);
  73        if (ret < 0)
  74                batadv_v_elp_iface_disable(hard_iface);
  75
  76        return ret;
  77}
  78
  79static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
  80{
  81        batadv_v_ogm_iface_disable(hard_iface);
  82        batadv_v_elp_iface_disable(hard_iface);
  83}
  84
  85static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
  86{
  87        batadv_v_elp_primary_iface_set(hard_iface);
  88        batadv_v_ogm_primary_iface_set(hard_iface);
  89}
  90
  91/**
  92 * batadv_v_iface_update_mac() - react to hard-interface MAC address change
  93 * @hard_iface: the modified interface
  94 *
  95 * If the modified interface is the primary one, update the originator
  96 * address in the ELP and OGM messages to reflect the new MAC address.
  97 */
  98static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
  99{
 100        struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 101        struct batadv_hard_iface *primary_if;
 102
 103        primary_if = batadv_primary_if_get_selected(bat_priv);
 104        if (primary_if != hard_iface)
 105                goto out;
 106
 107        batadv_v_primary_iface_set(hard_iface);
 108out:
 109        if (primary_if)
 110                batadv_hardif_put(primary_if);
 111}
 112
 113static void
 114batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
 115{
 116        ewma_throughput_init(&hardif_neigh->bat_v.throughput);
 117        INIT_WORK(&hardif_neigh->bat_v.metric_work,
 118                  batadv_v_elp_throughput_metric_update);
 119}
 120
 121/**
 122 * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
 123 * @msg: Netlink message to dump into
 124 * @portid: Port making netlink request
 125 * @seq: Sequence number of netlink message
 126 * @hardif_neigh: Neighbour to dump
 127 *
 128 * Return: Error code, or 0 on success
 129 */
 130static int
 131batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
 132                          struct batadv_hardif_neigh_node *hardif_neigh)
 133{
 134        void *hdr;
 135        unsigned int last_seen_msecs;
 136        u32 throughput;
 137
 138        last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
 139        throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
 140        throughput = throughput * 100;
 141
 142        hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
 143                          BATADV_CMD_GET_NEIGHBORS);
 144        if (!hdr)
 145                return -ENOBUFS;
 146
 147        if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
 148                    hardif_neigh->addr) ||
 149            nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
 150                        hardif_neigh->if_incoming->net_dev->ifindex) ||
 151            nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
 152                        last_seen_msecs) ||
 153            nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
 154                goto nla_put_failure;
 155
 156        genlmsg_end(msg, hdr);
 157        return 0;
 158
 159 nla_put_failure:
 160        genlmsg_cancel(msg, hdr);
 161        return -EMSGSIZE;
 162}
 163
 164/**
 165 * batadv_v_neigh_dump_hardif() - Dump the  neighbours of a hard interface into
 166 *  a message
 167 * @msg: Netlink message to dump into
 168 * @portid: Port making netlink request
 169 * @seq: Sequence number of netlink message
 170 * @bat_priv: The bat priv with all the soft interface information
 171 * @hard_iface: The hard interface to be dumped
 172 * @idx_s: Entries to be skipped
 173 *
 174 * This function assumes the caller holds rcu_read_lock().
 175 *
 176 * Return: Error code, or 0 on success
 177 */
 178static int
 179batadv_v_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
 180                           struct batadv_priv *bat_priv,
 181                           struct batadv_hard_iface *hard_iface,
 182                           int *idx_s)
 183{
 184        struct batadv_hardif_neigh_node *hardif_neigh;
 185        int idx = 0;
 186
 187        hlist_for_each_entry_rcu(hardif_neigh,
 188                                 &hard_iface->neigh_list, list) {
 189                if (idx++ < *idx_s)
 190                        continue;
 191
 192                if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
 193                        *idx_s = idx - 1;
 194                        return -EMSGSIZE;
 195                }
 196        }
 197
 198        *idx_s = 0;
 199        return 0;
 200}
 201
 202/**
 203 * batadv_v_neigh_dump() - Dump the neighbours of a hard interface  into a
 204 *  message
 205 * @msg: Netlink message to dump into
 206 * @cb: Control block containing additional options
 207 * @bat_priv: The bat priv with all the soft interface information
 208 * @single_hardif: Limit dumping to this hard interface
 209 */
 210static void
 211batadv_v_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
 212                    struct batadv_priv *bat_priv,
 213                    struct batadv_hard_iface *single_hardif)
 214{
 215        struct batadv_hard_iface *hard_iface;
 216        int i_hardif = 0;
 217        int i_hardif_s = cb->args[0];
 218        int idx = cb->args[1];
 219        int portid = NETLINK_CB(cb->skb).portid;
 220
 221        rcu_read_lock();
 222        if (single_hardif) {
 223                if (i_hardif_s == 0) {
 224                        if (batadv_v_neigh_dump_hardif(msg, portid,
 225                                                       cb->nlh->nlmsg_seq,
 226                                                       bat_priv, single_hardif,
 227                                                       &idx) == 0)
 228                                i_hardif++;
 229                }
 230        } else {
 231                list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
 232                        if (hard_iface->soft_iface != bat_priv->soft_iface)
 233                                continue;
 234
 235                        if (i_hardif++ < i_hardif_s)
 236                                continue;
 237
 238                        if (batadv_v_neigh_dump_hardif(msg, portid,
 239                                                       cb->nlh->nlmsg_seq,
 240                                                       bat_priv, hard_iface,
 241                                                       &idx)) {
 242                                i_hardif--;
 243                                break;
 244                        }
 245                }
 246        }
 247        rcu_read_unlock();
 248
 249        cb->args[0] = i_hardif;
 250        cb->args[1] = idx;
 251}
 252
 253/**
 254 * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
 255 * @msg: Netlink message to dump into
 256 * @portid: Port making netlink request
 257 * @seq: Sequence number of netlink message
 258 * @bat_priv: The bat priv with all the soft interface information
 259 * @if_outgoing: Limit dump to entries with this outgoing interface
 260 * @orig_node: Originator to dump
 261 * @neigh_node: Single hops neighbour
 262 * @best: Is the best originator
 263 *
 264 * Return: Error code, or 0 on success
 265 */
 266static int
 267batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
 268                            struct batadv_priv *bat_priv,
 269                            struct batadv_hard_iface *if_outgoing,
 270                            struct batadv_orig_node *orig_node,
 271                            struct batadv_neigh_node *neigh_node,
 272                            bool best)
 273{
 274        struct batadv_neigh_ifinfo *n_ifinfo;
 275        unsigned int last_seen_msecs;
 276        u32 throughput;
 277        void *hdr;
 278
 279        n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
 280        if (!n_ifinfo)
 281                return 0;
 282
 283        throughput = n_ifinfo->bat_v.throughput * 100;
 284
 285        batadv_neigh_ifinfo_put(n_ifinfo);
 286
 287        last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
 288
 289        if (if_outgoing != BATADV_IF_DEFAULT &&
 290            if_outgoing != neigh_node->if_incoming)
 291                return 0;
 292
 293        hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
 294                          BATADV_CMD_GET_ORIGINATORS);
 295        if (!hdr)
 296                return -ENOBUFS;
 297
 298        if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
 299            nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
 300                    neigh_node->addr) ||
 301            nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
 302                        neigh_node->if_incoming->net_dev->ifindex) ||
 303            nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
 304            nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
 305                        last_seen_msecs))
 306                goto nla_put_failure;
 307
 308        if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
 309                goto nla_put_failure;
 310
 311        genlmsg_end(msg, hdr);
 312        return 0;
 313
 314 nla_put_failure:
 315        genlmsg_cancel(msg, hdr);
 316        return -EMSGSIZE;
 317}
 318
 319/**
 320 * batadv_v_orig_dump_entry() - Dump an originator entry into a message
 321 * @msg: Netlink message to dump into
 322 * @portid: Port making netlink request
 323 * @seq: Sequence number of netlink message
 324 * @bat_priv: The bat priv with all the soft interface information
 325 * @if_outgoing: Limit dump to entries with this outgoing interface
 326 * @orig_node: Originator to dump
 327 * @sub_s: Number of sub entries to skip
 328 *
 329 * This function assumes the caller holds rcu_read_lock().
 330 *
 331 * Return: Error code, or 0 on success
 332 */
 333static int
 334batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
 335                         struct batadv_priv *bat_priv,
 336                         struct batadv_hard_iface *if_outgoing,
 337                         struct batadv_orig_node *orig_node, int *sub_s)
 338{
 339        struct batadv_neigh_node *neigh_node_best;
 340        struct batadv_neigh_node *neigh_node;
 341        int sub = 0;
 342        bool best;
 343
 344        neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
 345        if (!neigh_node_best)
 346                goto out;
 347
 348        hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
 349                if (sub++ < *sub_s)
 350                        continue;
 351
 352                best = (neigh_node == neigh_node_best);
 353
 354                if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
 355                                                if_outgoing, orig_node,
 356                                                neigh_node, best)) {
 357                        batadv_neigh_node_put(neigh_node_best);
 358
 359                        *sub_s = sub - 1;
 360                        return -EMSGSIZE;
 361                }
 362        }
 363
 364 out:
 365        if (neigh_node_best)
 366                batadv_neigh_node_put(neigh_node_best);
 367
 368        *sub_s = 0;
 369        return 0;
 370}
 371
 372/**
 373 * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
 374 * @msg: Netlink message to dump into
 375 * @portid: Port making netlink request
 376 * @seq: Sequence number of netlink message
 377 * @bat_priv: The bat priv with all the soft interface information
 378 * @if_outgoing: Limit dump to entries with this outgoing interface
 379 * @head: Bucket to be dumped
 380 * @idx_s: Number of entries to be skipped
 381 * @sub: Number of sub entries to be skipped
 382 *
 383 * Return: Error code, or 0 on success
 384 */
 385static int
 386batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
 387                          struct batadv_priv *bat_priv,
 388                          struct batadv_hard_iface *if_outgoing,
 389                          struct hlist_head *head, int *idx_s, int *sub)
 390{
 391        struct batadv_orig_node *orig_node;
 392        int idx = 0;
 393
 394        rcu_read_lock();
 395        hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
 396                if (idx++ < *idx_s)
 397                        continue;
 398
 399                if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
 400                                             if_outgoing, orig_node, sub)) {
 401                        rcu_read_unlock();
 402                        *idx_s = idx - 1;
 403                        return -EMSGSIZE;
 404                }
 405        }
 406        rcu_read_unlock();
 407
 408        *idx_s = 0;
 409        *sub = 0;
 410        return 0;
 411}
 412
 413/**
 414 * batadv_v_orig_dump() - Dump the originators into a message
 415 * @msg: Netlink message to dump into
 416 * @cb: Control block containing additional options
 417 * @bat_priv: The bat priv with all the soft interface information
 418 * @if_outgoing: Limit dump to entries with this outgoing interface
 419 */
 420static void
 421batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
 422                   struct batadv_priv *bat_priv,
 423                   struct batadv_hard_iface *if_outgoing)
 424{
 425        struct batadv_hashtable *hash = bat_priv->orig_hash;
 426        struct hlist_head *head;
 427        int bucket = cb->args[0];
 428        int idx = cb->args[1];
 429        int sub = cb->args[2];
 430        int portid = NETLINK_CB(cb->skb).portid;
 431
 432        while (bucket < hash->size) {
 433                head = &hash->table[bucket];
 434
 435                if (batadv_v_orig_dump_bucket(msg, portid,
 436                                              cb->nlh->nlmsg_seq,
 437                                              bat_priv, if_outgoing, head, &idx,
 438                                              &sub))
 439                        break;
 440
 441                bucket++;
 442        }
 443
 444        cb->args[0] = bucket;
 445        cb->args[1] = idx;
 446        cb->args[2] = sub;
 447}
 448
 449static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
 450                              struct batadv_hard_iface *if_outgoing1,
 451                              struct batadv_neigh_node *neigh2,
 452                              struct batadv_hard_iface *if_outgoing2)
 453{
 454        struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
 455        int ret = 0;
 456
 457        ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
 458        if (!ifinfo1)
 459                goto err_ifinfo1;
 460
 461        ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
 462        if (!ifinfo2)
 463                goto err_ifinfo2;
 464
 465        ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
 466
 467        batadv_neigh_ifinfo_put(ifinfo2);
 468err_ifinfo2:
 469        batadv_neigh_ifinfo_put(ifinfo1);
 470err_ifinfo1:
 471        return ret;
 472}
 473
 474static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
 475                                  struct batadv_hard_iface *if_outgoing1,
 476                                  struct batadv_neigh_node *neigh2,
 477                                  struct batadv_hard_iface *if_outgoing2)
 478{
 479        struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
 480        u32 threshold;
 481        bool ret = false;
 482
 483        ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
 484        if (!ifinfo1)
 485                goto err_ifinfo1;
 486
 487        ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
 488        if (!ifinfo2)
 489                goto err_ifinfo2;
 490
 491        threshold = ifinfo1->bat_v.throughput / 4;
 492        threshold = ifinfo1->bat_v.throughput - threshold;
 493
 494        ret = ifinfo2->bat_v.throughput > threshold;
 495
 496        batadv_neigh_ifinfo_put(ifinfo2);
 497err_ifinfo2:
 498        batadv_neigh_ifinfo_put(ifinfo1);
 499err_ifinfo1:
 500        return ret;
 501}
 502
 503/**
 504 * batadv_v_init_sel_class() - initialize GW selection class
 505 * @bat_priv: the bat priv with all the soft interface information
 506 */
 507static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
 508{
 509        /* set default throughput difference threshold to 5Mbps */
 510        atomic_set(&bat_priv->gw.sel_class, 50);
 511}
 512
 513static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
 514                                        char *buff, size_t count)
 515{
 516        u32 old_class, class;
 517
 518        if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
 519                                     "B.A.T.M.A.N. V GW selection class",
 520                                     &class))
 521                return -EINVAL;
 522
 523        old_class = atomic_read(&bat_priv->gw.sel_class);
 524        atomic_set(&bat_priv->gw.sel_class, class);
 525
 526        if (old_class != class)
 527                batadv_gw_reselect(bat_priv);
 528
 529        return count;
 530}
 531
 532/**
 533 * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
 534 * @gw_node: the GW to retrieve the metric for
 535 * @bw: the pointer where the metric will be stored. The metric is computed as
 536 *  the minimum between the GW advertised throughput and the path throughput to
 537 *  it in the mesh
 538 *
 539 * Return: 0 on success, -1 on failure
 540 */
 541static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
 542{
 543        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
 544        struct batadv_orig_node *orig_node;
 545        struct batadv_neigh_node *router;
 546        int ret = -1;
 547
 548        orig_node = gw_node->orig_node;
 549        router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
 550        if (!router)
 551                goto out;
 552
 553        router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
 554        if (!router_ifinfo)
 555                goto out;
 556
 557        /* the GW metric is computed as the minimum between the path throughput
 558         * to reach the GW itself and the advertised bandwidth.
 559         * This gives us an approximation of the effective throughput that the
 560         * client can expect via this particular GW node
 561         */
 562        *bw = router_ifinfo->bat_v.throughput;
 563        *bw = min_t(u32, *bw, gw_node->bandwidth_down);
 564
 565        ret = 0;
 566out:
 567        if (router)
 568                batadv_neigh_node_put(router);
 569        if (router_ifinfo)
 570                batadv_neigh_ifinfo_put(router_ifinfo);
 571
 572        return ret;
 573}
 574
 575/**
 576 * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
 577 * @bat_priv: the bat priv with all the soft interface information
 578 *
 579 * Return: the GW node having the best GW-metric, NULL if no GW is known
 580 */
 581static struct batadv_gw_node *
 582batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
 583{
 584        struct batadv_gw_node *gw_node, *curr_gw = NULL;
 585        u32 max_bw = 0, bw;
 586
 587        rcu_read_lock();
 588        hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
 589                if (!kref_get_unless_zero(&gw_node->refcount))
 590                        continue;
 591
 592                if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
 593                        goto next;
 594
 595                if (curr_gw && bw <= max_bw)
 596                        goto next;
 597
 598                if (curr_gw)
 599                        batadv_gw_node_put(curr_gw);
 600
 601                curr_gw = gw_node;
 602                kref_get(&curr_gw->refcount);
 603                max_bw = bw;
 604
 605next:
 606                batadv_gw_node_put(gw_node);
 607        }
 608        rcu_read_unlock();
 609
 610        return curr_gw;
 611}
 612
 613/**
 614 * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
 615 * @bat_priv: the bat priv with all the soft interface information
 616 * @curr_gw_orig: originator representing the currently selected GW
 617 * @orig_node: the originator representing the new candidate
 618 *
 619 * Return: true if orig_node can be selected as current GW, false otherwise
 620 */
 621static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
 622                                    struct batadv_orig_node *curr_gw_orig,
 623                                    struct batadv_orig_node *orig_node)
 624{
 625        struct batadv_gw_node *curr_gw, *orig_gw = NULL;
 626        u32 gw_throughput, orig_throughput, threshold;
 627        bool ret = false;
 628
 629        threshold = atomic_read(&bat_priv->gw.sel_class);
 630
 631        curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
 632        if (!curr_gw) {
 633                ret = true;
 634                goto out;
 635        }
 636
 637        if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
 638                ret = true;
 639                goto out;
 640        }
 641
 642        orig_gw = batadv_gw_node_get(bat_priv, orig_node);
 643        if (!orig_gw)
 644                goto out;
 645
 646        if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
 647                goto out;
 648
 649        if (orig_throughput < gw_throughput)
 650                goto out;
 651
 652        if ((orig_throughput - gw_throughput) < threshold)
 653                goto out;
 654
 655        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 656                   "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
 657                   gw_throughput, orig_throughput);
 658
 659        ret = true;
 660out:
 661        if (curr_gw)
 662                batadv_gw_node_put(curr_gw);
 663        if (orig_gw)
 664                batadv_gw_node_put(orig_gw);
 665
 666        return ret;
 667}
 668
 669/**
 670 * batadv_v_gw_dump_entry() - Dump a gateway into a message
 671 * @msg: Netlink message to dump into
 672 * @portid: Port making netlink request
 673 * @cb: Control block containing additional options
 674 * @bat_priv: The bat priv with all the soft interface information
 675 * @gw_node: Gateway to be dumped
 676 *
 677 * Return: Error code, or 0 on success
 678 */
 679static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
 680                                  struct netlink_callback *cb,
 681                                  struct batadv_priv *bat_priv,
 682                                  struct batadv_gw_node *gw_node)
 683{
 684        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
 685        struct batadv_neigh_node *router;
 686        struct batadv_gw_node *curr_gw = NULL;
 687        int ret = 0;
 688        void *hdr;
 689
 690        router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
 691        if (!router)
 692                goto out;
 693
 694        router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
 695        if (!router_ifinfo)
 696                goto out;
 697
 698        curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
 699
 700        hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
 701                          &batadv_netlink_family, NLM_F_MULTI,
 702                          BATADV_CMD_GET_GATEWAYS);
 703        if (!hdr) {
 704                ret = -ENOBUFS;
 705                goto out;
 706        }
 707
 708        genl_dump_check_consistent(cb, hdr);
 709
 710        ret = -EMSGSIZE;
 711
 712        if (curr_gw == gw_node) {
 713                if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
 714                        genlmsg_cancel(msg, hdr);
 715                        goto out;
 716                }
 717        }
 718
 719        if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
 720                    gw_node->orig_node->orig)) {
 721                genlmsg_cancel(msg, hdr);
 722                goto out;
 723        }
 724
 725        if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
 726                        router_ifinfo->bat_v.throughput)) {
 727                genlmsg_cancel(msg, hdr);
 728                goto out;
 729        }
 730
 731        if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
 732                genlmsg_cancel(msg, hdr);
 733                goto out;
 734        }
 735
 736        if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
 737                           router->if_incoming->net_dev->name)) {
 738                genlmsg_cancel(msg, hdr);
 739                goto out;
 740        }
 741
 742        if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
 743                        gw_node->bandwidth_down)) {
 744                genlmsg_cancel(msg, hdr);
 745                goto out;
 746        }
 747
 748        if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
 749                genlmsg_cancel(msg, hdr);
 750                goto out;
 751        }
 752
 753        genlmsg_end(msg, hdr);
 754        ret = 0;
 755
 756out:
 757        if (curr_gw)
 758                batadv_gw_node_put(curr_gw);
 759        if (router_ifinfo)
 760                batadv_neigh_ifinfo_put(router_ifinfo);
 761        if (router)
 762                batadv_neigh_node_put(router);
 763        return ret;
 764}
 765
 766/**
 767 * batadv_v_gw_dump() - Dump gateways into a message
 768 * @msg: Netlink message to dump into
 769 * @cb: Control block containing additional options
 770 * @bat_priv: The bat priv with all the soft interface information
 771 */
 772static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
 773                             struct batadv_priv *bat_priv)
 774{
 775        int portid = NETLINK_CB(cb->skb).portid;
 776        struct batadv_gw_node *gw_node;
 777        int idx_skip = cb->args[0];
 778        int idx = 0;
 779
 780        spin_lock_bh(&bat_priv->gw.list_lock);
 781        cb->seq = bat_priv->gw.generation << 1 | 1;
 782
 783        hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
 784                if (idx++ < idx_skip)
 785                        continue;
 786
 787                if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
 788                                           gw_node)) {
 789                        idx_skip = idx - 1;
 790                        goto unlock;
 791                }
 792        }
 793
 794        idx_skip = idx;
 795unlock:
 796        spin_unlock_bh(&bat_priv->gw.list_lock);
 797
 798        cb->args[0] = idx_skip;
 799}
 800
 801static struct batadv_algo_ops batadv_batman_v __read_mostly = {
 802        .name = "BATMAN_V",
 803        .iface = {
 804                .activate = batadv_v_iface_activate,
 805                .enable = batadv_v_iface_enable,
 806                .disable = batadv_v_iface_disable,
 807                .update_mac = batadv_v_iface_update_mac,
 808                .primary_set = batadv_v_primary_iface_set,
 809        },
 810        .neigh = {
 811                .hardif_init = batadv_v_hardif_neigh_init,
 812                .cmp = batadv_v_neigh_cmp,
 813                .is_similar_or_better = batadv_v_neigh_is_sob,
 814                .dump = batadv_v_neigh_dump,
 815        },
 816        .orig = {
 817                .dump = batadv_v_orig_dump,
 818        },
 819        .gw = {
 820                .init_sel_class = batadv_v_init_sel_class,
 821                .store_sel_class = batadv_v_store_sel_class,
 822                .get_best_gw_node = batadv_v_gw_get_best_gw_node,
 823                .is_eligible = batadv_v_gw_is_eligible,
 824                .dump = batadv_v_gw_dump,
 825        },
 826};
 827
 828/**
 829 * batadv_v_hardif_init() - initialize the algorithm specific fields in the
 830 *  hard-interface object
 831 * @hard_iface: the hard-interface to initialize
 832 */
 833void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
 834{
 835        /* enable link throughput auto-detection by setting the throughput
 836         * override to zero
 837         */
 838        atomic_set(&hard_iface->bat_v.throughput_override, 0);
 839        atomic_set(&hard_iface->bat_v.elp_interval, 500);
 840
 841        hard_iface->bat_v.aggr_len = 0;
 842        skb_queue_head_init(&hard_iface->bat_v.aggr_list);
 843        INIT_DELAYED_WORK(&hard_iface->bat_v.aggr_wq,
 844                          batadv_v_ogm_aggr_work);
 845}
 846
 847/**
 848 * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
 849 *  mesh
 850 * @bat_priv: the object representing the mesh interface to initialise
 851 *
 852 * Return: 0 on success or a negative error code otherwise
 853 */
 854int batadv_v_mesh_init(struct batadv_priv *bat_priv)
 855{
 856        int ret = 0;
 857
 858        ret = batadv_v_ogm_init(bat_priv);
 859        if (ret < 0)
 860                return ret;
 861
 862        return 0;
 863}
 864
 865/**
 866 * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
 867 * @bat_priv: the object representing the mesh interface to free
 868 */
 869void batadv_v_mesh_free(struct batadv_priv *bat_priv)
 870{
 871        batadv_v_ogm_free(bat_priv);
 872}
 873
 874/**
 875 * batadv_v_init() - B.A.T.M.A.N. V initialization function
 876 *
 877 * Description: Takes care of initializing all the subcomponents.
 878 * It is invoked upon module load only.
 879 *
 880 * Return: 0 on success or a negative error code otherwise
 881 */
 882int __init batadv_v_init(void)
 883{
 884        int ret;
 885
 886        /* B.A.T.M.A.N. V echo location protocol packet  */
 887        ret = batadv_recv_handler_register(BATADV_ELP,
 888                                           batadv_v_elp_packet_recv);
 889        if (ret < 0)
 890                return ret;
 891
 892        ret = batadv_recv_handler_register(BATADV_OGM2,
 893                                           batadv_v_ogm_packet_recv);
 894        if (ret < 0)
 895                goto elp_unregister;
 896
 897        ret = batadv_algo_register(&batadv_batman_v);
 898        if (ret < 0)
 899                goto ogm_unregister;
 900
 901        return ret;
 902
 903ogm_unregister:
 904        batadv_recv_handler_unregister(BATADV_OGM2);
 905
 906elp_unregister:
 907        batadv_recv_handler_unregister(BATADV_ELP);
 908
 909        return ret;
 910}
 911