linux/net/batman-adv/bat_iv_ogm.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2007-2019  B.A.T.M.A.N. contributors:
   3 *
   4 * Marek Lindner, Simon Wunderlich
   5 */
   6
   7#include "bat_iv_ogm.h"
   8#include "main.h"
   9
  10#include <linux/atomic.h>
  11#include <linux/bitmap.h>
  12#include <linux/bitops.h>
  13#include <linux/bug.h>
  14#include <linux/byteorder/generic.h>
  15#include <linux/cache.h>
  16#include <linux/errno.h>
  17#include <linux/etherdevice.h>
  18#include <linux/gfp.h>
  19#include <linux/if_ether.h>
  20#include <linux/init.h>
  21#include <linux/jiffies.h>
  22#include <linux/kernel.h>
  23#include <linux/kref.h>
  24#include <linux/list.h>
  25#include <linux/netdevice.h>
  26#include <linux/netlink.h>
  27#include <linux/pkt_sched.h>
  28#include <linux/printk.h>
  29#include <linux/random.h>
  30#include <linux/rculist.h>
  31#include <linux/rcupdate.h>
  32#include <linux/seq_file.h>
  33#include <linux/skbuff.h>
  34#include <linux/slab.h>
  35#include <linux/spinlock.h>
  36#include <linux/stddef.h>
  37#include <linux/string.h>
  38#include <linux/types.h>
  39#include <linux/workqueue.h>
  40#include <net/genetlink.h>
  41#include <net/netlink.h>
  42#include <uapi/linux/batadv_packet.h>
  43#include <uapi/linux/batman_adv.h>
  44
  45#include "bat_algo.h"
  46#include "bitarray.h"
  47#include "gateway_client.h"
  48#include "hard-interface.h"
  49#include "hash.h"
  50#include "log.h"
  51#include "netlink.h"
  52#include "network-coding.h"
  53#include "originator.h"
  54#include "routing.h"
  55#include "send.h"
  56#include "translation-table.h"
  57#include "tvlv.h"
  58
  59static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
  60
  61/**
  62 * enum batadv_dup_status - duplicate status
  63 */
  64enum batadv_dup_status {
  65        /** @BATADV_NO_DUP: the packet is no duplicate */
  66        BATADV_NO_DUP = 0,
  67
  68        /**
  69         * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
  70         *  the neighbor)
  71         */
  72        BATADV_ORIG_DUP,
  73
  74        /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
  75        BATADV_NEIGH_DUP,
  76
  77        /**
  78         * @BATADV_PROTECTED: originator is currently protected (after reboot)
  79         */
  80        BATADV_PROTECTED,
  81};
  82
  83/**
  84 * batadv_ring_buffer_set() - update the ring buffer with the given value
  85 * @lq_recv: pointer to the ring buffer
  86 * @lq_index: index to store the value at
  87 * @value: value to store in the ring buffer
  88 */
  89static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
  90{
  91        lq_recv[*lq_index] = value;
  92        *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
  93}
  94
  95/**
  96 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
  97 * in the given ring buffer
  98 * @lq_recv: pointer to the ring buffer
  99 *
 100 * Return: computed average value.
 101 */
 102static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
 103{
 104        const u8 *ptr;
 105        u16 count = 0;
 106        u16 i = 0;
 107        u16 sum = 0;
 108
 109        ptr = lq_recv;
 110
 111        while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
 112                if (*ptr != 0) {
 113                        count++;
 114                        sum += *ptr;
 115                }
 116
 117                i++;
 118                ptr++;
 119        }
 120
 121        if (count == 0)
 122                return 0;
 123
 124        return (u8)(sum / count);
 125}
 126
 127/**
 128 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
 129 *  originator
 130 * @bat_priv: the bat priv with all the soft interface information
 131 * @addr: mac address of the originator
 132 *
 133 * Return: the originator object corresponding to the passed mac address or NULL
 134 * on failure.
 135 * If the object does not exists it is created an initialised.
 136 */
 137static struct batadv_orig_node *
 138batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
 139{
 140        struct batadv_orig_node *orig_node;
 141        int hash_added;
 142
 143        orig_node = batadv_orig_hash_find(bat_priv, addr);
 144        if (orig_node)
 145                return orig_node;
 146
 147        orig_node = batadv_orig_node_new(bat_priv, addr);
 148        if (!orig_node)
 149                return NULL;
 150
 151        spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
 152
 153        kref_get(&orig_node->refcount);
 154        hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
 155                                     batadv_choose_orig, orig_node,
 156                                     &orig_node->hash_entry);
 157        if (hash_added != 0)
 158                goto free_orig_node_hash;
 159
 160        return orig_node;
 161
 162free_orig_node_hash:
 163        /* reference for batadv_hash_add */
 164        batadv_orig_node_put(orig_node);
 165        /* reference from batadv_orig_node_new */
 166        batadv_orig_node_put(orig_node);
 167
 168        return NULL;
 169}
 170
 171static struct batadv_neigh_node *
 172batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
 173                        const u8 *neigh_addr,
 174                        struct batadv_orig_node *orig_node,
 175                        struct batadv_orig_node *orig_neigh)
 176{
 177        struct batadv_neigh_node *neigh_node;
 178
 179        neigh_node = batadv_neigh_node_get_or_create(orig_node,
 180                                                     hard_iface, neigh_addr);
 181        if (!neigh_node)
 182                goto out;
 183
 184        neigh_node->orig_node = orig_neigh;
 185
 186out:
 187        return neigh_node;
 188}
 189
 190static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
 191{
 192        struct batadv_ogm_packet *batadv_ogm_packet;
 193        unsigned char *ogm_buff;
 194        u32 random_seqno;
 195
 196        /* randomize initial seqno to avoid collision */
 197        get_random_bytes(&random_seqno, sizeof(random_seqno));
 198        atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
 199
 200        hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
 201        ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
 202        if (!ogm_buff)
 203                return -ENOMEM;
 204
 205        hard_iface->bat_iv.ogm_buff = ogm_buff;
 206
 207        batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
 208        batadv_ogm_packet->packet_type = BATADV_IV_OGM;
 209        batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
 210        batadv_ogm_packet->ttl = 2;
 211        batadv_ogm_packet->flags = BATADV_NO_FLAGS;
 212        batadv_ogm_packet->reserved = 0;
 213        batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
 214
 215        return 0;
 216}
 217
 218static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
 219{
 220        kfree(hard_iface->bat_iv.ogm_buff);
 221        hard_iface->bat_iv.ogm_buff = NULL;
 222}
 223
 224static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
 225{
 226        struct batadv_ogm_packet *batadv_ogm_packet;
 227        unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
 228
 229        batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
 230        ether_addr_copy(batadv_ogm_packet->orig,
 231                        hard_iface->net_dev->dev_addr);
 232        ether_addr_copy(batadv_ogm_packet->prev_sender,
 233                        hard_iface->net_dev->dev_addr);
 234}
 235
 236static void
 237batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
 238{
 239        struct batadv_ogm_packet *batadv_ogm_packet;
 240        unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
 241
 242        batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
 243        batadv_ogm_packet->ttl = BATADV_TTL;
 244}
 245
 246/* when do we schedule our own ogm to be sent */
 247static unsigned long
 248batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
 249{
 250        unsigned int msecs;
 251
 252        msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
 253        msecs += prandom_u32() % (2 * BATADV_JITTER);
 254
 255        return jiffies + msecs_to_jiffies(msecs);
 256}
 257
 258/* when do we schedule a ogm packet to be sent */
 259static unsigned long batadv_iv_ogm_fwd_send_time(void)
 260{
 261        return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
 262}
 263
 264/* apply hop penalty for a normal link */
 265static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
 266{
 267        int hop_penalty = atomic_read(&bat_priv->hop_penalty);
 268        int new_tq;
 269
 270        new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
 271        new_tq /= BATADV_TQ_MAX_VALUE;
 272
 273        return new_tq;
 274}
 275
 276/**
 277 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
 278 * @buff_pos: current position in the skb
 279 * @packet_len: total length of the skb
 280 * @tvlv_len: tvlv length of the previously considered OGM
 281 *
 282 * Return: true if there is enough space for another OGM, false otherwise.
 283 */
 284static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
 285                                      __be16 tvlv_len)
 286{
 287        int next_buff_pos = 0;
 288
 289        next_buff_pos += buff_pos + BATADV_OGM_HLEN;
 290        next_buff_pos += ntohs(tvlv_len);
 291
 292        return (next_buff_pos <= packet_len) &&
 293               (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
 294}
 295
 296/* send a batman ogm to a given interface */
 297static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
 298                                     struct batadv_hard_iface *hard_iface)
 299{
 300        struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 301        const char *fwd_str;
 302        u8 packet_num;
 303        s16 buff_pos;
 304        struct batadv_ogm_packet *batadv_ogm_packet;
 305        struct sk_buff *skb;
 306        u8 *packet_pos;
 307
 308        if (hard_iface->if_status != BATADV_IF_ACTIVE)
 309                return;
 310
 311        packet_num = 0;
 312        buff_pos = 0;
 313        packet_pos = forw_packet->skb->data;
 314        batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
 315
 316        /* adjust all flags and log packets */
 317        while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
 318                                         batadv_ogm_packet->tvlv_len)) {
 319                /* we might have aggregated direct link packets with an
 320                 * ordinary base packet
 321                 */
 322                if (forw_packet->direct_link_flags & BIT(packet_num) &&
 323                    forw_packet->if_incoming == hard_iface)
 324                        batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
 325                else
 326                        batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
 327
 328                if (packet_num > 0 || !forw_packet->own)
 329                        fwd_str = "Forwarding";
 330                else
 331                        fwd_str = "Sending own";
 332
 333                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 334                           "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
 335                           fwd_str, (packet_num > 0 ? "aggregated " : ""),
 336                           batadv_ogm_packet->orig,
 337                           ntohl(batadv_ogm_packet->seqno),
 338                           batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
 339                           ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
 340                            "on" : "off"),
 341                           hard_iface->net_dev->name,
 342                           hard_iface->net_dev->dev_addr);
 343
 344                buff_pos += BATADV_OGM_HLEN;
 345                buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
 346                packet_num++;
 347                packet_pos = forw_packet->skb->data + buff_pos;
 348                batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
 349        }
 350
 351        /* create clone because function is called more than once */
 352        skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
 353        if (skb) {
 354                batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
 355                batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
 356                                   skb->len + ETH_HLEN);
 357                batadv_send_broadcast_skb(skb, hard_iface);
 358        }
 359}
 360
 361/* send a batman ogm packet */
 362static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
 363{
 364        struct net_device *soft_iface;
 365
 366        if (!forw_packet->if_incoming) {
 367                pr_err("Error - can't forward packet: incoming iface not specified\n");
 368                return;
 369        }
 370
 371        soft_iface = forw_packet->if_incoming->soft_iface;
 372
 373        if (WARN_ON(!forw_packet->if_outgoing))
 374                return;
 375
 376        if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
 377                return;
 378
 379        if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
 380                return;
 381
 382        /* only for one specific outgoing interface */
 383        batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
 384}
 385
 386/**
 387 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
 388 *  existing forward packet
 389 * @new_bat_ogm_packet: OGM packet to be aggregated
 390 * @bat_priv: the bat priv with all the soft interface information
 391 * @packet_len: (total) length of the OGM
 392 * @send_time: timestamp (jiffies) when the packet is to be sent
 393 * @directlink: true if this is a direct link packet
 394 * @if_incoming: interface where the packet was received
 395 * @if_outgoing: interface for which the retransmission should be considered
 396 * @forw_packet: the forwarded packet which should be checked
 397 *
 398 * Return: true if new_packet can be aggregated with forw_packet
 399 */
 400static bool
 401batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
 402                            struct batadv_priv *bat_priv,
 403                            int packet_len, unsigned long send_time,
 404                            bool directlink,
 405                            const struct batadv_hard_iface *if_incoming,
 406                            const struct batadv_hard_iface *if_outgoing,
 407                            const struct batadv_forw_packet *forw_packet)
 408{
 409        struct batadv_ogm_packet *batadv_ogm_packet;
 410        int aggregated_bytes = forw_packet->packet_len + packet_len;
 411        struct batadv_hard_iface *primary_if = NULL;
 412        bool res = false;
 413        unsigned long aggregation_end_time;
 414
 415        batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
 416        aggregation_end_time = send_time;
 417        aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
 418
 419        /* we can aggregate the current packet to this aggregated packet
 420         * if:
 421         *
 422         * - the send time is within our MAX_AGGREGATION_MS time
 423         * - the resulting packet wont be bigger than
 424         *   MAX_AGGREGATION_BYTES
 425         * otherwise aggregation is not possible
 426         */
 427        if (!time_before(send_time, forw_packet->send_time) ||
 428            !time_after_eq(aggregation_end_time, forw_packet->send_time))
 429                return false;
 430
 431        if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
 432                return false;
 433
 434        /* packet is not leaving on the same interface. */
 435        if (forw_packet->if_outgoing != if_outgoing)
 436                return false;
 437
 438        /* check aggregation compatibility
 439         * -> direct link packets are broadcasted on
 440         *    their interface only
 441         * -> aggregate packet if the current packet is
 442         *    a "global" packet as well as the base
 443         *    packet
 444         */
 445        primary_if = batadv_primary_if_get_selected(bat_priv);
 446        if (!primary_if)
 447                return false;
 448
 449        /* packets without direct link flag and high TTL
 450         * are flooded through the net
 451         */
 452        if (!directlink &&
 453            !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
 454            batadv_ogm_packet->ttl != 1 &&
 455
 456            /* own packets originating non-primary
 457             * interfaces leave only that interface
 458             */
 459            (!forw_packet->own ||
 460             forw_packet->if_incoming == primary_if)) {
 461                res = true;
 462                goto out;
 463        }
 464
 465        /* if the incoming packet is sent via this one
 466         * interface only - we still can aggregate
 467         */
 468        if (directlink &&
 469            new_bat_ogm_packet->ttl == 1 &&
 470            forw_packet->if_incoming == if_incoming &&
 471
 472            /* packets from direct neighbors or
 473             * own secondary interface packets
 474             * (= secondary interface packets in general)
 475             */
 476            (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
 477             (forw_packet->own &&
 478              forw_packet->if_incoming != primary_if))) {
 479                res = true;
 480                goto out;
 481        }
 482
 483out:
 484        if (primary_if)
 485                batadv_hardif_put(primary_if);
 486        return res;
 487}
 488
 489/**
 490 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
 491 *  packet to it.
 492 * @packet_buff: pointer to the OGM
 493 * @packet_len: (total) length of the OGM
 494 * @send_time: timestamp (jiffies) when the packet is to be sent
 495 * @direct_link: whether this OGM has direct link status
 496 * @if_incoming: interface where the packet was received
 497 * @if_outgoing: interface for which the retransmission should be considered
 498 * @own_packet: true if it is a self-generated ogm
 499 */
 500static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
 501                                        int packet_len, unsigned long send_time,
 502                                        bool direct_link,
 503                                        struct batadv_hard_iface *if_incoming,
 504                                        struct batadv_hard_iface *if_outgoing,
 505                                        int own_packet)
 506{
 507        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
 508        struct batadv_forw_packet *forw_packet_aggr;
 509        struct sk_buff *skb;
 510        unsigned char *skb_buff;
 511        unsigned int skb_size;
 512        atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
 513
 514        if (atomic_read(&bat_priv->aggregated_ogms) &&
 515            packet_len < BATADV_MAX_AGGREGATION_BYTES)
 516                skb_size = BATADV_MAX_AGGREGATION_BYTES;
 517        else
 518                skb_size = packet_len;
 519
 520        skb_size += ETH_HLEN;
 521
 522        skb = netdev_alloc_skb_ip_align(NULL, skb_size);
 523        if (!skb)
 524                return;
 525
 526        forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
 527                                                    queue_left, bat_priv, skb);
 528        if (!forw_packet_aggr) {
 529                kfree_skb(skb);
 530                return;
 531        }
 532
 533        forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
 534        skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
 535
 536        skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
 537        forw_packet_aggr->packet_len = packet_len;
 538        memcpy(skb_buff, packet_buff, packet_len);
 539
 540        forw_packet_aggr->own = own_packet;
 541        forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
 542        forw_packet_aggr->send_time = send_time;
 543
 544        /* save packet direct link flag status */
 545        if (direct_link)
 546                forw_packet_aggr->direct_link_flags |= 1;
 547
 548        INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
 549                          batadv_iv_send_outstanding_bat_ogm_packet);
 550
 551        batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
 552}
 553
 554/* aggregate a new packet into the existing ogm packet */
 555static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
 556                                    const unsigned char *packet_buff,
 557                                    int packet_len, bool direct_link)
 558{
 559        unsigned long new_direct_link_flag;
 560
 561        skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
 562        forw_packet_aggr->packet_len += packet_len;
 563        forw_packet_aggr->num_packets++;
 564
 565        /* save packet direct link flag status */
 566        if (direct_link) {
 567                new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
 568                forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
 569        }
 570}
 571
 572/**
 573 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
 574 * @bat_priv: the bat priv with all the soft interface information
 575 * @packet_buff: pointer to the OGM
 576 * @packet_len: (total) length of the OGM
 577 * @if_incoming: interface where the packet was received
 578 * @if_outgoing: interface for which the retransmission should be considered
 579 * @own_packet: true if it is a self-generated ogm
 580 * @send_time: timestamp (jiffies) when the packet is to be sent
 581 */
 582static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
 583                                    unsigned char *packet_buff,
 584                                    int packet_len,
 585                                    struct batadv_hard_iface *if_incoming,
 586                                    struct batadv_hard_iface *if_outgoing,
 587                                    int own_packet, unsigned long send_time)
 588{
 589        /* _aggr -> pointer to the packet we want to aggregate with
 590         * _pos -> pointer to the position in the queue
 591         */
 592        struct batadv_forw_packet *forw_packet_aggr = NULL;
 593        struct batadv_forw_packet *forw_packet_pos = NULL;
 594        struct batadv_ogm_packet *batadv_ogm_packet;
 595        bool direct_link;
 596        unsigned long max_aggregation_jiffies;
 597
 598        batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
 599        direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
 600        max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
 601
 602        /* find position for the packet in the forward queue */
 603        spin_lock_bh(&bat_priv->forw_bat_list_lock);
 604        /* own packets are not to be aggregated */
 605        if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
 606                hlist_for_each_entry(forw_packet_pos,
 607                                     &bat_priv->forw_bat_list, list) {
 608                        if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
 609                                                        bat_priv, packet_len,
 610                                                        send_time, direct_link,
 611                                                        if_incoming,
 612                                                        if_outgoing,
 613                                                        forw_packet_pos)) {
 614                                forw_packet_aggr = forw_packet_pos;
 615                                break;
 616                        }
 617                }
 618        }
 619
 620        /* nothing to aggregate with - either aggregation disabled or no
 621         * suitable aggregation packet found
 622         */
 623        if (!forw_packet_aggr) {
 624                /* the following section can run without the lock */
 625                spin_unlock_bh(&bat_priv->forw_bat_list_lock);
 626
 627                /* if we could not aggregate this packet with one of the others
 628                 * we hold it back for a while, so that it might be aggregated
 629                 * later on
 630                 */
 631                if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
 632                        send_time += max_aggregation_jiffies;
 633
 634                batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
 635                                            send_time, direct_link,
 636                                            if_incoming, if_outgoing,
 637                                            own_packet);
 638        } else {
 639                batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
 640                                        packet_len, direct_link);
 641                spin_unlock_bh(&bat_priv->forw_bat_list_lock);
 642        }
 643}
 644
 645static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
 646                                  const struct ethhdr *ethhdr,
 647                                  struct batadv_ogm_packet *batadv_ogm_packet,
 648                                  bool is_single_hop_neigh,
 649                                  bool is_from_best_next_hop,
 650                                  struct batadv_hard_iface *if_incoming,
 651                                  struct batadv_hard_iface *if_outgoing)
 652{
 653        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
 654        u16 tvlv_len;
 655
 656        if (batadv_ogm_packet->ttl <= 1) {
 657                batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
 658                return;
 659        }
 660
 661        if (!is_from_best_next_hop) {
 662                /* Mark the forwarded packet when it is not coming from our
 663                 * best next hop. We still need to forward the packet for our
 664                 * neighbor link quality detection to work in case the packet
 665                 * originated from a single hop neighbor. Otherwise we can
 666                 * simply drop the ogm.
 667                 */
 668                if (is_single_hop_neigh)
 669                        batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
 670                else
 671                        return;
 672        }
 673
 674        tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
 675
 676        batadv_ogm_packet->ttl--;
 677        ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
 678
 679        /* apply hop penalty */
 680        batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
 681                                                   bat_priv);
 682
 683        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 684                   "Forwarding packet: tq: %i, ttl: %i\n",
 685                   batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
 686
 687        if (is_single_hop_neigh)
 688                batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
 689        else
 690                batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
 691
 692        batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
 693                                BATADV_OGM_HLEN + tvlv_len,
 694                                if_incoming, if_outgoing, 0,
 695                                batadv_iv_ogm_fwd_send_time());
 696}
 697
 698/**
 699 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
 700 *  for the given interface
 701 * @hard_iface: the interface for which the windows have to be shifted
 702 */
 703static void
 704batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
 705{
 706        struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 707        struct batadv_hashtable *hash = bat_priv->orig_hash;
 708        struct hlist_head *head;
 709        struct batadv_orig_node *orig_node;
 710        struct batadv_orig_ifinfo *orig_ifinfo;
 711        unsigned long *word;
 712        u32 i;
 713        u8 *w;
 714
 715        for (i = 0; i < hash->size; i++) {
 716                head = &hash->table[i];
 717
 718                rcu_read_lock();
 719                hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
 720                        hlist_for_each_entry_rcu(orig_ifinfo,
 721                                                 &orig_node->ifinfo_list,
 722                                                 list) {
 723                                if (orig_ifinfo->if_outgoing != hard_iface)
 724                                        continue;
 725
 726                                spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
 727                                word = orig_ifinfo->bat_iv.bcast_own;
 728                                batadv_bit_get_packet(bat_priv, word, 1, 0);
 729                                w = &orig_ifinfo->bat_iv.bcast_own_sum;
 730                                *w = bitmap_weight(word,
 731                                                   BATADV_TQ_LOCAL_WINDOW_SIZE);
 732                                spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
 733                        }
 734                }
 735                rcu_read_unlock();
 736        }
 737}
 738
 739static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
 740{
 741        struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 742        unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
 743        struct batadv_ogm_packet *batadv_ogm_packet;
 744        struct batadv_hard_iface *primary_if, *tmp_hard_iface;
 745        int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
 746        u32 seqno;
 747        u16 tvlv_len = 0;
 748        unsigned long send_time;
 749
 750        if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
 751            hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
 752                return;
 753
 754        /* the interface gets activated here to avoid race conditions between
 755         * the moment of activating the interface in
 756         * hardif_activate_interface() where the originator mac is set and
 757         * outdated packets (especially uninitialized mac addresses) in the
 758         * packet queue
 759         */
 760        if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
 761                hard_iface->if_status = BATADV_IF_ACTIVE;
 762
 763        primary_if = batadv_primary_if_get_selected(bat_priv);
 764
 765        if (hard_iface == primary_if) {
 766                /* tt changes have to be committed before the tvlv data is
 767                 * appended as it may alter the tt tvlv container
 768                 */
 769                batadv_tt_local_commit_changes(bat_priv);
 770                tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
 771                                                            ogm_buff_len,
 772                                                            BATADV_OGM_HLEN);
 773        }
 774
 775        batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
 776        batadv_ogm_packet->tvlv_len = htons(tvlv_len);
 777
 778        /* change sequence number to network order */
 779        seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
 780        batadv_ogm_packet->seqno = htonl(seqno);
 781        atomic_inc(&hard_iface->bat_iv.ogm_seqno);
 782
 783        batadv_iv_ogm_slide_own_bcast_window(hard_iface);
 784
 785        send_time = batadv_iv_ogm_emit_send_time(bat_priv);
 786
 787        if (hard_iface != primary_if) {
 788                /* OGMs from secondary interfaces are only scheduled on their
 789                 * respective interfaces.
 790                 */
 791                batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
 792                                        hard_iface, hard_iface, 1, send_time);
 793                goto out;
 794        }
 795
 796        /* OGMs from primary interfaces are scheduled on all
 797         * interfaces.
 798         */
 799        rcu_read_lock();
 800        list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
 801                if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
 802                        continue;
 803
 804                if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
 805                        continue;
 806
 807                batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
 808                                        *ogm_buff_len, hard_iface,
 809                                        tmp_hard_iface, 1, send_time);
 810
 811                batadv_hardif_put(tmp_hard_iface);
 812        }
 813        rcu_read_unlock();
 814
 815out:
 816        if (primary_if)
 817                batadv_hardif_put(primary_if);
 818}
 819
 820/**
 821 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over iterface
 822 * @orig_node: originator which reproadcasted the OGMs directly
 823 * @if_outgoing: interface which transmitted the original OGM and received the
 824 *  direct rebroadcast
 825 *
 826 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
 827 *  an originator and directly (without intermediate hop) received by a specific
 828 *  interface
 829 */
 830static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
 831                                    struct batadv_hard_iface *if_outgoing)
 832{
 833        struct batadv_orig_ifinfo *orig_ifinfo;
 834        u8 sum;
 835
 836        orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
 837        if (!orig_ifinfo)
 838                return 0;
 839
 840        spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
 841        sum = orig_ifinfo->bat_iv.bcast_own_sum;
 842        spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
 843
 844        batadv_orig_ifinfo_put(orig_ifinfo);
 845
 846        return sum;
 847}
 848
 849/**
 850 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
 851 *  originator
 852 * @bat_priv: the bat priv with all the soft interface information
 853 * @orig_node: the orig node who originally emitted the ogm packet
 854 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
 855 * @ethhdr: Ethernet header of the OGM
 856 * @batadv_ogm_packet: the ogm packet
 857 * @if_incoming: interface where the packet was received
 858 * @if_outgoing: interface for which the retransmission should be considered
 859 * @dup_status: the duplicate status of this ogm packet.
 860 */
 861static void
 862batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
 863                          struct batadv_orig_node *orig_node,
 864                          struct batadv_orig_ifinfo *orig_ifinfo,
 865                          const struct ethhdr *ethhdr,
 866                          const struct batadv_ogm_packet *batadv_ogm_packet,
 867                          struct batadv_hard_iface *if_incoming,
 868                          struct batadv_hard_iface *if_outgoing,
 869                          enum batadv_dup_status dup_status)
 870{
 871        struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
 872        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
 873        struct batadv_neigh_node *neigh_node = NULL;
 874        struct batadv_neigh_node *tmp_neigh_node = NULL;
 875        struct batadv_neigh_node *router = NULL;
 876        u8 sum_orig, sum_neigh;
 877        u8 *neigh_addr;
 878        u8 tq_avg;
 879
 880        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 881                   "%s(): Searching and updating originator entry of received packet\n",
 882                   __func__);
 883
 884        rcu_read_lock();
 885        hlist_for_each_entry_rcu(tmp_neigh_node,
 886                                 &orig_node->neigh_list, list) {
 887                neigh_addr = tmp_neigh_node->addr;
 888                if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
 889                    tmp_neigh_node->if_incoming == if_incoming &&
 890                    kref_get_unless_zero(&tmp_neigh_node->refcount)) {
 891                        if (WARN(neigh_node, "too many matching neigh_nodes"))
 892                                batadv_neigh_node_put(neigh_node);
 893                        neigh_node = tmp_neigh_node;
 894                        continue;
 895                }
 896
 897                if (dup_status != BATADV_NO_DUP)
 898                        continue;
 899
 900                /* only update the entry for this outgoing interface */
 901                neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
 902                                                       if_outgoing);
 903                if (!neigh_ifinfo)
 904                        continue;
 905
 906                spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
 907                batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
 908                                       &neigh_ifinfo->bat_iv.tq_index, 0);
 909                tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
 910                neigh_ifinfo->bat_iv.tq_avg = tq_avg;
 911                spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
 912
 913                batadv_neigh_ifinfo_put(neigh_ifinfo);
 914                neigh_ifinfo = NULL;
 915        }
 916
 917        if (!neigh_node) {
 918                struct batadv_orig_node *orig_tmp;
 919
 920                orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
 921                if (!orig_tmp)
 922                        goto unlock;
 923
 924                neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
 925                                                     ethhdr->h_source,
 926                                                     orig_node, orig_tmp);
 927
 928                batadv_orig_node_put(orig_tmp);
 929                if (!neigh_node)
 930                        goto unlock;
 931        } else {
 932                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 933                           "Updating existing last-hop neighbor of originator\n");
 934        }
 935
 936        rcu_read_unlock();
 937        neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
 938        if (!neigh_ifinfo)
 939                goto out;
 940
 941        neigh_node->last_seen = jiffies;
 942
 943        spin_lock_bh(&neigh_node->ifinfo_lock);
 944        batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
 945                               &neigh_ifinfo->bat_iv.tq_index,
 946                               batadv_ogm_packet->tq);
 947        tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
 948        neigh_ifinfo->bat_iv.tq_avg = tq_avg;
 949        spin_unlock_bh(&neigh_node->ifinfo_lock);
 950
 951        if (dup_status == BATADV_NO_DUP) {
 952                orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
 953                neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
 954        }
 955
 956        /* if this neighbor already is our next hop there is nothing
 957         * to change
 958         */
 959        router = batadv_orig_router_get(orig_node, if_outgoing);
 960        if (router == neigh_node)
 961                goto out;
 962
 963        if (router) {
 964                router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
 965                if (!router_ifinfo)
 966                        goto out;
 967
 968                /* if this neighbor does not offer a better TQ we won't
 969                 * consider it
 970                 */
 971                if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
 972                        goto out;
 973        }
 974
 975        /* if the TQ is the same and the link not more symmetric we
 976         * won't consider it either
 977         */
 978        if (router_ifinfo &&
 979            neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
 980                sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
 981                                                     router->if_incoming);
 982                sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
 983                                                      neigh_node->if_incoming);
 984                if (sum_orig >= sum_neigh)
 985                        goto out;
 986        }
 987
 988        batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
 989        goto out;
 990
 991unlock:
 992        rcu_read_unlock();
 993out:
 994        if (neigh_node)
 995                batadv_neigh_node_put(neigh_node);
 996        if (router)
 997                batadv_neigh_node_put(router);
 998        if (neigh_ifinfo)
 999                batadv_neigh_ifinfo_put(neigh_ifinfo);
1000        if (router_ifinfo)
1001                batadv_neigh_ifinfo_put(router_ifinfo);
1002}
1003
1004/**
1005 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1006 * @orig_node: the orig node who originally emitted the ogm packet
1007 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1008 * @batadv_ogm_packet: the ogm packet
1009 * @if_incoming: interface where the packet was received
1010 * @if_outgoing: interface for which the retransmission should be considered
1011 *
1012 * Return: true if the link can be considered bidirectional, false otherwise
1013 */
1014static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1015                                  struct batadv_orig_node *orig_neigh_node,
1016                                  struct batadv_ogm_packet *batadv_ogm_packet,
1017                                  struct batadv_hard_iface *if_incoming,
1018                                  struct batadv_hard_iface *if_outgoing)
1019{
1020        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1021        struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1022        struct batadv_neigh_ifinfo *neigh_ifinfo;
1023        u8 total_count;
1024        u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1025        unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1026        unsigned int tq_asym_penalty, inv_asym_penalty;
1027        unsigned int combined_tq;
1028        unsigned int tq_iface_penalty;
1029        bool ret = false;
1030
1031        /* find corresponding one hop neighbor */
1032        rcu_read_lock();
1033        hlist_for_each_entry_rcu(tmp_neigh_node,
1034                                 &orig_neigh_node->neigh_list, list) {
1035                if (!batadv_compare_eth(tmp_neigh_node->addr,
1036                                        orig_neigh_node->orig))
1037                        continue;
1038
1039                if (tmp_neigh_node->if_incoming != if_incoming)
1040                        continue;
1041
1042                if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1043                        continue;
1044
1045                neigh_node = tmp_neigh_node;
1046                break;
1047        }
1048        rcu_read_unlock();
1049
1050        if (!neigh_node)
1051                neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1052                                                     orig_neigh_node->orig,
1053                                                     orig_neigh_node,
1054                                                     orig_neigh_node);
1055
1056        if (!neigh_node)
1057                goto out;
1058
1059        /* if orig_node is direct neighbor update neigh_node last_seen */
1060        if (orig_node == orig_neigh_node)
1061                neigh_node->last_seen = jiffies;
1062
1063        orig_node->last_seen = jiffies;
1064
1065        /* find packet count of corresponding one hop neighbor */
1066        orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1067        neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1068        if (neigh_ifinfo) {
1069                neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1070                batadv_neigh_ifinfo_put(neigh_ifinfo);
1071        } else {
1072                neigh_rq_count = 0;
1073        }
1074
1075        /* pay attention to not get a value bigger than 100 % */
1076        if (orig_eq_count > neigh_rq_count)
1077                total_count = neigh_rq_count;
1078        else
1079                total_count = orig_eq_count;
1080
1081        /* if we have too few packets (too less data) we set tq_own to zero
1082         * if we receive too few packets it is not considered bidirectional
1083         */
1084        if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1085            neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1086                tq_own = 0;
1087        else
1088                /* neigh_node->real_packet_count is never zero as we
1089                 * only purge old information when getting new
1090                 * information
1091                 */
1092                tq_own = (BATADV_TQ_MAX_VALUE * total_count) /  neigh_rq_count;
1093
1094        /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1095         * affect the nearly-symmetric links only a little, but
1096         * punishes asymmetric links more.  This will give a value
1097         * between 0 and TQ_MAX_VALUE
1098         */
1099        neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1100        neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1101        neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1102                            BATADV_TQ_LOCAL_WINDOW_SIZE *
1103                            BATADV_TQ_LOCAL_WINDOW_SIZE;
1104        inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1105        inv_asym_penalty /= neigh_rq_max_cube;
1106        tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1107
1108        /* penalize if the OGM is forwarded on the same interface. WiFi
1109         * interfaces and other half duplex devices suffer from throughput
1110         * drops as they can't send and receive at the same time.
1111         */
1112        tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1113        if (if_outgoing && if_incoming == if_outgoing &&
1114            batadv_is_wifi_hardif(if_outgoing))
1115                tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1116                                                      bat_priv);
1117
1118        combined_tq = batadv_ogm_packet->tq *
1119                      tq_own *
1120                      tq_asym_penalty *
1121                      tq_iface_penalty;
1122        combined_tq /= BATADV_TQ_MAX_VALUE *
1123                       BATADV_TQ_MAX_VALUE *
1124                       BATADV_TQ_MAX_VALUE;
1125        batadv_ogm_packet->tq = combined_tq;
1126
1127        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1128                   "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1129                   orig_node->orig, orig_neigh_node->orig, total_count,
1130                   neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1131                   batadv_ogm_packet->tq, if_incoming->net_dev->name,
1132                   if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1133
1134        /* if link has the minimum required transmission quality
1135         * consider it bidirectional
1136         */
1137        if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1138                ret = true;
1139
1140out:
1141        if (neigh_node)
1142                batadv_neigh_node_put(neigh_node);
1143        return ret;
1144}
1145
1146/**
1147 * batadv_iv_ogm_update_seqnos() -  process a batman packet for all interfaces,
1148 *  adjust the sequence number and find out whether it is a duplicate
1149 * @ethhdr: ethernet header of the packet
1150 * @batadv_ogm_packet: OGM packet to be considered
1151 * @if_incoming: interface on which the OGM packet was received
1152 * @if_outgoing: interface for which the retransmission should be considered
1153 *
1154 * Return: duplicate status as enum batadv_dup_status
1155 */
1156static enum batadv_dup_status
1157batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1158                            const struct batadv_ogm_packet *batadv_ogm_packet,
1159                            const struct batadv_hard_iface *if_incoming,
1160                            struct batadv_hard_iface *if_outgoing)
1161{
1162        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1163        struct batadv_orig_node *orig_node;
1164        struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1165        struct batadv_neigh_node *neigh_node;
1166        struct batadv_neigh_ifinfo *neigh_ifinfo;
1167        bool is_dup;
1168        s32 seq_diff;
1169        bool need_update = false;
1170        int set_mark;
1171        enum batadv_dup_status ret = BATADV_NO_DUP;
1172        u32 seqno = ntohl(batadv_ogm_packet->seqno);
1173        u8 *neigh_addr;
1174        u8 packet_count;
1175        unsigned long *bitmap;
1176
1177        orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1178        if (!orig_node)
1179                return BATADV_NO_DUP;
1180
1181        orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1182        if (WARN_ON(!orig_ifinfo)) {
1183                batadv_orig_node_put(orig_node);
1184                return 0;
1185        }
1186
1187        spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1188        seq_diff = seqno - orig_ifinfo->last_real_seqno;
1189
1190        /* signalize caller that the packet is to be dropped. */
1191        if (!hlist_empty(&orig_node->neigh_list) &&
1192            batadv_window_protected(bat_priv, seq_diff,
1193                                    BATADV_TQ_LOCAL_WINDOW_SIZE,
1194                                    &orig_ifinfo->batman_seqno_reset, NULL)) {
1195                ret = BATADV_PROTECTED;
1196                goto out;
1197        }
1198
1199        rcu_read_lock();
1200        hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1201                neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1202                                                       if_outgoing);
1203                if (!neigh_ifinfo)
1204                        continue;
1205
1206                neigh_addr = neigh_node->addr;
1207                is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1208                                         orig_ifinfo->last_real_seqno,
1209                                         seqno);
1210
1211                if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1212                    neigh_node->if_incoming == if_incoming) {
1213                        set_mark = 1;
1214                        if (is_dup)
1215                                ret = BATADV_NEIGH_DUP;
1216                } else {
1217                        set_mark = 0;
1218                        if (is_dup && ret != BATADV_NEIGH_DUP)
1219                                ret = BATADV_ORIG_DUP;
1220                }
1221
1222                /* if the window moved, set the update flag. */
1223                bitmap = neigh_ifinfo->bat_iv.real_bits;
1224                need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1225                                                     seq_diff, set_mark);
1226
1227                packet_count = bitmap_weight(bitmap,
1228                                             BATADV_TQ_LOCAL_WINDOW_SIZE);
1229                neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1230                batadv_neigh_ifinfo_put(neigh_ifinfo);
1231        }
1232        rcu_read_unlock();
1233
1234        if (need_update) {
1235                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1236                           "%s updating last_seqno: old %u, new %u\n",
1237                           if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1238                           orig_ifinfo->last_real_seqno, seqno);
1239                orig_ifinfo->last_real_seqno = seqno;
1240        }
1241
1242out:
1243        spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1244        batadv_orig_node_put(orig_node);
1245        batadv_orig_ifinfo_put(orig_ifinfo);
1246        return ret;
1247}
1248
1249/**
1250 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1251 *  interface
1252 * @skb: the skb containing the OGM
1253 * @ogm_offset: offset from skb->data to start of ogm header
1254 * @orig_node: the (cached) orig node for the originator of this OGM
1255 * @if_incoming: the interface where this packet was received
1256 * @if_outgoing: the interface for which the packet should be considered
1257 */
1258static void
1259batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1260                                struct batadv_orig_node *orig_node,
1261                                struct batadv_hard_iface *if_incoming,
1262                                struct batadv_hard_iface *if_outgoing)
1263{
1264        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1265        struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1266        struct batadv_neigh_node *router = NULL;
1267        struct batadv_neigh_node *router_router = NULL;
1268        struct batadv_orig_node *orig_neigh_node;
1269        struct batadv_orig_ifinfo *orig_ifinfo;
1270        struct batadv_neigh_node *orig_neigh_router = NULL;
1271        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1272        struct batadv_ogm_packet *ogm_packet;
1273        enum batadv_dup_status dup_status;
1274        bool is_from_best_next_hop = false;
1275        bool is_single_hop_neigh = false;
1276        bool sameseq, similar_ttl;
1277        struct sk_buff *skb_priv;
1278        struct ethhdr *ethhdr;
1279        u8 *prev_sender;
1280        bool is_bidirect;
1281
1282        /* create a private copy of the skb, as some functions change tq value
1283         * and/or flags.
1284         */
1285        skb_priv = skb_copy(skb, GFP_ATOMIC);
1286        if (!skb_priv)
1287                return;
1288
1289        ethhdr = eth_hdr(skb_priv);
1290        ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1291
1292        dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1293                                                 if_incoming, if_outgoing);
1294        if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1295                is_single_hop_neigh = true;
1296
1297        if (dup_status == BATADV_PROTECTED) {
1298                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1299                           "Drop packet: packet within seqno protection time (sender: %pM)\n",
1300                           ethhdr->h_source);
1301                goto out;
1302        }
1303
1304        if (ogm_packet->tq == 0) {
1305                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1306                           "Drop packet: originator packet with tq equal 0\n");
1307                goto out;
1308        }
1309
1310        if (is_single_hop_neigh) {
1311                hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1312                                                       ethhdr->h_source);
1313                if (hardif_neigh)
1314                        hardif_neigh->last_seen = jiffies;
1315        }
1316
1317        router = batadv_orig_router_get(orig_node, if_outgoing);
1318        if (router) {
1319                router_router = batadv_orig_router_get(router->orig_node,
1320                                                       if_outgoing);
1321                router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1322        }
1323
1324        if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1325            (batadv_compare_eth(router->addr, ethhdr->h_source)))
1326                is_from_best_next_hop = true;
1327
1328        prev_sender = ogm_packet->prev_sender;
1329        /* avoid temporary routing loops */
1330        if (router && router_router &&
1331            (batadv_compare_eth(router->addr, prev_sender)) &&
1332            !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1333            (batadv_compare_eth(router->addr, router_router->addr))) {
1334                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1335                           "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1336                           ethhdr->h_source);
1337                goto out;
1338        }
1339
1340        if (if_outgoing == BATADV_IF_DEFAULT)
1341                batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1342
1343        /* if sender is a direct neighbor the sender mac equals
1344         * originator mac
1345         */
1346        if (is_single_hop_neigh)
1347                orig_neigh_node = orig_node;
1348        else
1349                orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1350                                                         ethhdr->h_source);
1351
1352        if (!orig_neigh_node)
1353                goto out;
1354
1355        /* Update nc_nodes of the originator */
1356        batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1357                                 ogm_packet, is_single_hop_neigh);
1358
1359        orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1360                                                   if_outgoing);
1361
1362        /* drop packet if sender is not a direct neighbor and if we
1363         * don't route towards it
1364         */
1365        if (!is_single_hop_neigh && !orig_neigh_router) {
1366                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1367                           "Drop packet: OGM via unknown neighbor!\n");
1368                goto out_neigh;
1369        }
1370
1371        is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1372                                            ogm_packet, if_incoming,
1373                                            if_outgoing);
1374
1375        /* update ranking if it is not a duplicate or has the same
1376         * seqno and similar ttl as the non-duplicate
1377         */
1378        orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1379        if (!orig_ifinfo)
1380                goto out_neigh;
1381
1382        sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1383        similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1384
1385        if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1386                            (sameseq && similar_ttl))) {
1387                batadv_iv_ogm_orig_update(bat_priv, orig_node,
1388                                          orig_ifinfo, ethhdr,
1389                                          ogm_packet, if_incoming,
1390                                          if_outgoing, dup_status);
1391        }
1392        batadv_orig_ifinfo_put(orig_ifinfo);
1393
1394        /* only forward for specific interface, not for the default one. */
1395        if (if_outgoing == BATADV_IF_DEFAULT)
1396                goto out_neigh;
1397
1398        /* is single hop (direct) neighbor */
1399        if (is_single_hop_neigh) {
1400                /* OGMs from secondary interfaces should only scheduled once
1401                 * per interface where it has been received, not multiple times
1402                 */
1403                if (ogm_packet->ttl <= 2 &&
1404                    if_incoming != if_outgoing) {
1405                        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1406                                   "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1407                        goto out_neigh;
1408                }
1409                /* mark direct link on incoming interface */
1410                batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1411                                      is_single_hop_neigh,
1412                                      is_from_best_next_hop, if_incoming,
1413                                      if_outgoing);
1414
1415                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1416                           "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1417                goto out_neigh;
1418        }
1419
1420        /* multihop originator */
1421        if (!is_bidirect) {
1422                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1423                           "Drop packet: not received via bidirectional link\n");
1424                goto out_neigh;
1425        }
1426
1427        if (dup_status == BATADV_NEIGH_DUP) {
1428                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1429                           "Drop packet: duplicate packet received\n");
1430                goto out_neigh;
1431        }
1432
1433        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1434                   "Forwarding packet: rebroadcast originator packet\n");
1435        batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1436                              is_single_hop_neigh, is_from_best_next_hop,
1437                              if_incoming, if_outgoing);
1438
1439out_neigh:
1440        if (orig_neigh_node && !is_single_hop_neigh)
1441                batadv_orig_node_put(orig_neigh_node);
1442out:
1443        if (router_ifinfo)
1444                batadv_neigh_ifinfo_put(router_ifinfo);
1445        if (router)
1446                batadv_neigh_node_put(router);
1447        if (router_router)
1448                batadv_neigh_node_put(router_router);
1449        if (orig_neigh_router)
1450                batadv_neigh_node_put(orig_neigh_router);
1451        if (hardif_neigh)
1452                batadv_hardif_neigh_put(hardif_neigh);
1453
1454        consume_skb(skb_priv);
1455}
1456
1457/**
1458 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1459 * @ogm_packet: rebroadcast OGM packet to process
1460 * @if_incoming: the interface where this packet was received
1461 * @orig_node: originator which reproadcasted the OGMs
1462 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1463 */
1464static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1465                                        struct batadv_hard_iface *if_incoming,
1466                                        struct batadv_orig_node *orig_node,
1467                                        u32 if_incoming_seqno)
1468{
1469        struct batadv_orig_ifinfo *orig_ifinfo;
1470        s32 bit_pos;
1471        u8 *weight;
1472
1473        /* neighbor has to indicate direct link and it has to
1474         * come via the corresponding interface
1475         */
1476        if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1477                return;
1478
1479        if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1480                                ogm_packet->orig))
1481                return;
1482
1483        orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1484        if (!orig_ifinfo)
1485                return;
1486
1487        /* save packet seqno for bidirectional check */
1488        spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1489        bit_pos = if_incoming_seqno - 2;
1490        bit_pos -= ntohl(ogm_packet->seqno);
1491        batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1492        weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1493        *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1494                                BATADV_TQ_LOCAL_WINDOW_SIZE);
1495        spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1496
1497        batadv_orig_ifinfo_put(orig_ifinfo);
1498}
1499
1500/**
1501 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1502 * @skb: the skb containing the OGM
1503 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1504 * @if_incoming: the interface where this packet was receved
1505 */
1506static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1507                                  struct batadv_hard_iface *if_incoming)
1508{
1509        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1510        struct batadv_orig_node *orig_neigh_node, *orig_node;
1511        struct batadv_hard_iface *hard_iface;
1512        struct batadv_ogm_packet *ogm_packet;
1513        u32 if_incoming_seqno;
1514        bool has_directlink_flag;
1515        struct ethhdr *ethhdr;
1516        bool is_my_oldorig = false;
1517        bool is_my_addr = false;
1518        bool is_my_orig = false;
1519
1520        ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1521        ethhdr = eth_hdr(skb);
1522
1523        /* Silently drop when the batman packet is actually not a
1524         * correct packet.
1525         *
1526         * This might happen if a packet is padded (e.g. Ethernet has a
1527         * minimum frame length of 64 byte) and the aggregation interprets
1528         * it as an additional length.
1529         *
1530         * TODO: A more sane solution would be to have a bit in the
1531         * batadv_ogm_packet to detect whether the packet is the last
1532         * packet in an aggregation.  Here we expect that the padding
1533         * is always zero (or not 0x01)
1534         */
1535        if (ogm_packet->packet_type != BATADV_IV_OGM)
1536                return;
1537
1538        /* could be changed by schedule_own_packet() */
1539        if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1540
1541        if (ogm_packet->flags & BATADV_DIRECTLINK)
1542                has_directlink_flag = true;
1543        else
1544                has_directlink_flag = false;
1545
1546        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1547                   "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1548                   ethhdr->h_source, if_incoming->net_dev->name,
1549                   if_incoming->net_dev->dev_addr, ogm_packet->orig,
1550                   ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1551                   ogm_packet->tq, ogm_packet->ttl,
1552                   ogm_packet->version, has_directlink_flag);
1553
1554        rcu_read_lock();
1555        list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1556                if (hard_iface->if_status != BATADV_IF_ACTIVE)
1557                        continue;
1558
1559                if (hard_iface->soft_iface != if_incoming->soft_iface)
1560                        continue;
1561
1562                if (batadv_compare_eth(ethhdr->h_source,
1563                                       hard_iface->net_dev->dev_addr))
1564                        is_my_addr = true;
1565
1566                if (batadv_compare_eth(ogm_packet->orig,
1567                                       hard_iface->net_dev->dev_addr))
1568                        is_my_orig = true;
1569
1570                if (batadv_compare_eth(ogm_packet->prev_sender,
1571                                       hard_iface->net_dev->dev_addr))
1572                        is_my_oldorig = true;
1573        }
1574        rcu_read_unlock();
1575
1576        if (is_my_addr) {
1577                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1578                           "Drop packet: received my own broadcast (sender: %pM)\n",
1579                           ethhdr->h_source);
1580                return;
1581        }
1582
1583        if (is_my_orig) {
1584                orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1585                                                         ethhdr->h_source);
1586                if (!orig_neigh_node)
1587                        return;
1588
1589                batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1590                                            orig_neigh_node, if_incoming_seqno);
1591
1592                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1593                           "Drop packet: originator packet from myself (via neighbor)\n");
1594                batadv_orig_node_put(orig_neigh_node);
1595                return;
1596        }
1597
1598        if (is_my_oldorig) {
1599                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1600                           "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1601                           ethhdr->h_source);
1602                return;
1603        }
1604
1605        if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1606                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1607                           "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1608                           ethhdr->h_source);
1609                return;
1610        }
1611
1612        orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1613        if (!orig_node)
1614                return;
1615
1616        batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1617                                        if_incoming, BATADV_IF_DEFAULT);
1618
1619        rcu_read_lock();
1620        list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1621                if (hard_iface->if_status != BATADV_IF_ACTIVE)
1622                        continue;
1623
1624                if (hard_iface->soft_iface != bat_priv->soft_iface)
1625                        continue;
1626
1627                if (!kref_get_unless_zero(&hard_iface->refcount))
1628                        continue;
1629
1630                batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1631                                                if_incoming, hard_iface);
1632
1633                batadv_hardif_put(hard_iface);
1634        }
1635        rcu_read_unlock();
1636
1637        batadv_orig_node_put(orig_node);
1638}
1639
1640static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1641{
1642        struct delayed_work *delayed_work;
1643        struct batadv_forw_packet *forw_packet;
1644        struct batadv_priv *bat_priv;
1645        bool dropped = false;
1646
1647        delayed_work = to_delayed_work(work);
1648        forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1649                                   delayed_work);
1650        bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1651
1652        if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1653                dropped = true;
1654                goto out;
1655        }
1656
1657        batadv_iv_ogm_emit(forw_packet);
1658
1659        /* we have to have at least one packet in the queue to determine the
1660         * queues wake up time unless we are shutting down.
1661         *
1662         * only re-schedule if this is the "original" copy, e.g. the OGM of the
1663         * primary interface should only be rescheduled once per period, but
1664         * this function will be called for the forw_packet instances of the
1665         * other secondary interfaces as well.
1666         */
1667        if (forw_packet->own &&
1668            forw_packet->if_incoming == forw_packet->if_outgoing)
1669                batadv_iv_ogm_schedule(forw_packet->if_incoming);
1670
1671out:
1672        /* do we get something for free()? */
1673        if (batadv_forw_packet_steal(forw_packet,
1674                                     &bat_priv->forw_bat_list_lock))
1675                batadv_forw_packet_free(forw_packet, dropped);
1676}
1677
1678static int batadv_iv_ogm_receive(struct sk_buff *skb,
1679                                 struct batadv_hard_iface *if_incoming)
1680{
1681        struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1682        struct batadv_ogm_packet *ogm_packet;
1683        u8 *packet_pos;
1684        int ogm_offset;
1685        bool res;
1686        int ret = NET_RX_DROP;
1687
1688        res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1689        if (!res)
1690                goto free_skb;
1691
1692        /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1693         * that does not have B.A.T.M.A.N. IV enabled ?
1694         */
1695        if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1696                goto free_skb;
1697
1698        batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1699        batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1700                           skb->len + ETH_HLEN);
1701
1702        ogm_offset = 0;
1703        ogm_packet = (struct batadv_ogm_packet *)skb->data;
1704
1705        /* unpack the aggregated packets and process them one by one */
1706        while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1707                                         ogm_packet->tvlv_len)) {
1708                batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1709
1710                ogm_offset += BATADV_OGM_HLEN;
1711                ogm_offset += ntohs(ogm_packet->tvlv_len);
1712
1713                packet_pos = skb->data + ogm_offset;
1714                ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1715        }
1716
1717        ret = NET_RX_SUCCESS;
1718
1719free_skb:
1720        if (ret == NET_RX_SUCCESS)
1721                consume_skb(skb);
1722        else
1723                kfree_skb(skb);
1724
1725        return ret;
1726}
1727
1728#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1729/**
1730 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
1731 * @orig_node: the orig_node for which the neighbors are printed
1732 * @if_outgoing: outgoing interface for these entries
1733 * @seq: debugfs table seq_file struct
1734 *
1735 * Must be called while holding an rcu lock.
1736 */
1737static void
1738batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1739                               struct batadv_hard_iface *if_outgoing,
1740                               struct seq_file *seq)
1741{
1742        struct batadv_neigh_node *neigh_node;
1743        struct batadv_neigh_ifinfo *n_ifinfo;
1744
1745        hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1746                n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1747                if (!n_ifinfo)
1748                        continue;
1749
1750                seq_printf(seq, " %pM (%3i)",
1751                           neigh_node->addr,
1752                           n_ifinfo->bat_iv.tq_avg);
1753
1754                batadv_neigh_ifinfo_put(n_ifinfo);
1755        }
1756}
1757
1758/**
1759 * batadv_iv_ogm_orig_print() - print the originator table
1760 * @bat_priv: the bat priv with all the soft interface information
1761 * @seq: debugfs table seq_file struct
1762 * @if_outgoing: the outgoing interface for which this should be printed
1763 */
1764static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1765                                     struct seq_file *seq,
1766                                     struct batadv_hard_iface *if_outgoing)
1767{
1768        struct batadv_neigh_node *neigh_node;
1769        struct batadv_hashtable *hash = bat_priv->orig_hash;
1770        int last_seen_msecs, last_seen_secs;
1771        struct batadv_orig_node *orig_node;
1772        struct batadv_neigh_ifinfo *n_ifinfo;
1773        unsigned long last_seen_jiffies;
1774        struct hlist_head *head;
1775        int batman_count = 0;
1776        u32 i;
1777
1778        seq_puts(seq,
1779                 "  Originator      last-seen (#/255)           Nexthop [outgoingIF]:   Potential nexthops ...\n");
1780
1781        for (i = 0; i < hash->size; i++) {
1782                head = &hash->table[i];
1783
1784                rcu_read_lock();
1785                hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1786                        neigh_node = batadv_orig_router_get(orig_node,
1787                                                            if_outgoing);
1788                        if (!neigh_node)
1789                                continue;
1790
1791                        n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1792                                                           if_outgoing);
1793                        if (!n_ifinfo)
1794                                goto next;
1795
1796                        if (n_ifinfo->bat_iv.tq_avg == 0)
1797                                goto next;
1798
1799                        last_seen_jiffies = jiffies - orig_node->last_seen;
1800                        last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1801                        last_seen_secs = last_seen_msecs / 1000;
1802                        last_seen_msecs = last_seen_msecs % 1000;
1803
1804                        seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
1805                                   orig_node->orig, last_seen_secs,
1806                                   last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1807                                   neigh_node->addr,
1808                                   neigh_node->if_incoming->net_dev->name);
1809
1810                        batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1811                                                       seq);
1812                        seq_putc(seq, '\n');
1813                        batman_count++;
1814
1815next:
1816                        batadv_neigh_node_put(neigh_node);
1817                        if (n_ifinfo)
1818                                batadv_neigh_ifinfo_put(n_ifinfo);
1819                }
1820                rcu_read_unlock();
1821        }
1822
1823        if (batman_count == 0)
1824                seq_puts(seq, "No batman nodes in range ...\n");
1825}
1826#endif
1827
1828/**
1829 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1830 *  given outgoing interface.
1831 * @neigh_node: Neighbour of interest
1832 * @if_outgoing: Outgoing interface of interest
1833 * @tq_avg: Pointer of where to store the TQ average
1834 *
1835 * Return: False if no average TQ available, otherwise true.
1836 */
1837static bool
1838batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1839                               struct batadv_hard_iface *if_outgoing,
1840                               u8 *tq_avg)
1841{
1842        struct batadv_neigh_ifinfo *n_ifinfo;
1843
1844        n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1845        if (!n_ifinfo)
1846                return false;
1847
1848        *tq_avg = n_ifinfo->bat_iv.tq_avg;
1849        batadv_neigh_ifinfo_put(n_ifinfo);
1850
1851        return true;
1852}
1853
1854/**
1855 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1856 *  message
1857 * @msg: Netlink message to dump into
1858 * @portid: Port making netlink request
1859 * @seq: Sequence number of netlink message
1860 * @bat_priv: The bat priv with all the soft interface information
1861 * @if_outgoing: Limit dump to entries with this outgoing interface
1862 * @orig_node: Originator to dump
1863 * @neigh_node: Single hops neighbour
1864 * @best: Is the best originator
1865 *
1866 * Return: Error code, or 0 on success
1867 */
1868static int
1869batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1870                                 struct batadv_priv *bat_priv,
1871                                 struct batadv_hard_iface *if_outgoing,
1872                                 struct batadv_orig_node *orig_node,
1873                                 struct batadv_neigh_node *neigh_node,
1874                                 bool best)
1875{
1876        void *hdr;
1877        u8 tq_avg;
1878        unsigned int last_seen_msecs;
1879
1880        last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1881
1882        if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1883                return 0;
1884
1885        if (if_outgoing != BATADV_IF_DEFAULT &&
1886            if_outgoing != neigh_node->if_incoming)
1887                return 0;
1888
1889        hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1890                          NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1891        if (!hdr)
1892                return -ENOBUFS;
1893
1894        if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1895                    orig_node->orig) ||
1896            nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1897                    neigh_node->addr) ||
1898            nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1899                        neigh_node->if_incoming->net_dev->ifindex) ||
1900            nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1901            nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1902                        last_seen_msecs))
1903                goto nla_put_failure;
1904
1905        if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1906                goto nla_put_failure;
1907
1908        genlmsg_end(msg, hdr);
1909        return 0;
1910
1911 nla_put_failure:
1912        genlmsg_cancel(msg, hdr);
1913        return -EMSGSIZE;
1914}
1915
1916/**
1917 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
1918 * @msg: Netlink message to dump into
1919 * @portid: Port making netlink request
1920 * @seq: Sequence number of netlink message
1921 * @bat_priv: The bat priv with all the soft interface information
1922 * @if_outgoing: Limit dump to entries with this outgoing interface
1923 * @orig_node: Originator to dump
1924 * @sub_s: Number of sub entries to skip
1925 *
1926 * This function assumes the caller holds rcu_read_lock().
1927 *
1928 * Return: Error code, or 0 on success
1929 */
1930static int
1931batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1932                              struct batadv_priv *bat_priv,
1933                              struct batadv_hard_iface *if_outgoing,
1934                              struct batadv_orig_node *orig_node, int *sub_s)
1935{
1936        struct batadv_neigh_node *neigh_node_best;
1937        struct batadv_neigh_node *neigh_node;
1938        int sub = 0;
1939        bool best;
1940        u8 tq_avg_best;
1941
1942        neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1943        if (!neigh_node_best)
1944                goto out;
1945
1946        if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1947                                            &tq_avg_best))
1948                goto out;
1949
1950        if (tq_avg_best == 0)
1951                goto out;
1952
1953        hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1954                if (sub++ < *sub_s)
1955                        continue;
1956
1957                best = (neigh_node == neigh_node_best);
1958
1959                if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1960                                                     bat_priv, if_outgoing,
1961                                                     orig_node, neigh_node,
1962                                                     best)) {
1963                        batadv_neigh_node_put(neigh_node_best);
1964
1965                        *sub_s = sub - 1;
1966                        return -EMSGSIZE;
1967                }
1968        }
1969
1970 out:
1971        if (neigh_node_best)
1972                batadv_neigh_node_put(neigh_node_best);
1973
1974        *sub_s = 0;
1975        return 0;
1976}
1977
1978/**
1979 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
1980 *  message
1981 * @msg: Netlink message to dump into
1982 * @portid: Port making netlink request
1983 * @seq: Sequence number of netlink message
1984 * @bat_priv: The bat priv with all the soft interface information
1985 * @if_outgoing: Limit dump to entries with this outgoing interface
1986 * @head: Bucket to be dumped
1987 * @idx_s: Number of entries to be skipped
1988 * @sub: Number of sub entries to be skipped
1989 *
1990 * Return: Error code, or 0 on success
1991 */
1992static int
1993batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1994                               struct batadv_priv *bat_priv,
1995                               struct batadv_hard_iface *if_outgoing,
1996                               struct hlist_head *head, int *idx_s, int *sub)
1997{
1998        struct batadv_orig_node *orig_node;
1999        int idx = 0;
2000
2001        rcu_read_lock();
2002        hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2003                if (idx++ < *idx_s)
2004                        continue;
2005
2006                if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2007                                                  if_outgoing, orig_node,
2008                                                  sub)) {
2009                        rcu_read_unlock();
2010                        *idx_s = idx - 1;
2011                        return -EMSGSIZE;
2012                }
2013        }
2014        rcu_read_unlock();
2015
2016        *idx_s = 0;
2017        *sub = 0;
2018        return 0;
2019}
2020
2021/**
2022 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2023 * @msg: Netlink message to dump into
2024 * @cb: Control block containing additional options
2025 * @bat_priv: The bat priv with all the soft interface information
2026 * @if_outgoing: Limit dump to entries with this outgoing interface
2027 */
2028static void
2029batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2030                        struct batadv_priv *bat_priv,
2031                        struct batadv_hard_iface *if_outgoing)
2032{
2033        struct batadv_hashtable *hash = bat_priv->orig_hash;
2034        struct hlist_head *head;
2035        int bucket = cb->args[0];
2036        int idx = cb->args[1];
2037        int sub = cb->args[2];
2038        int portid = NETLINK_CB(cb->skb).portid;
2039
2040        while (bucket < hash->size) {
2041                head = &hash->table[bucket];
2042
2043                if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2044                                                   cb->nlh->nlmsg_seq,
2045                                                   bat_priv, if_outgoing, head,
2046                                                   &idx, &sub))
2047                        break;
2048
2049                bucket++;
2050        }
2051
2052        cb->args[0] = bucket;
2053        cb->args[1] = idx;
2054        cb->args[2] = sub;
2055}
2056
2057#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2058/**
2059 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
2060 * @seq: neighbour table seq_file struct
2061 * @hardif_neigh: hardif neighbour information
2062 */
2063static void
2064batadv_iv_hardif_neigh_print(struct seq_file *seq,
2065                             struct batadv_hardif_neigh_node *hardif_neigh)
2066{
2067        int last_secs, last_msecs;
2068
2069        last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2070        last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2071
2072        seq_printf(seq, "   %10s   %pM %4i.%03is\n",
2073                   hardif_neigh->if_incoming->net_dev->name,
2074                   hardif_neigh->addr, last_secs, last_msecs);
2075}
2076
2077/**
2078 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
2079 * @bat_priv: the bat priv with all the soft interface information
2080 * @seq: neighbour table seq_file struct
2081 */
2082static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2083                                  struct seq_file *seq)
2084{
2085        struct net_device *net_dev = (struct net_device *)seq->private;
2086        struct batadv_hardif_neigh_node *hardif_neigh;
2087        struct batadv_hard_iface *hard_iface;
2088        int batman_count = 0;
2089
2090        seq_puts(seq, "           IF        Neighbor      last-seen\n");
2091
2092        rcu_read_lock();
2093        list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2094                if (hard_iface->soft_iface != net_dev)
2095                        continue;
2096
2097                hlist_for_each_entry_rcu(hardif_neigh,
2098                                         &hard_iface->neigh_list, list) {
2099                        batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2100                        batman_count++;
2101                }
2102        }
2103        rcu_read_unlock();
2104
2105        if (batman_count == 0)
2106                seq_puts(seq, "No batman nodes in range ...\n");
2107}
2108#endif
2109
2110/**
2111 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2112 * @neigh1: the first neighbor object of the comparison
2113 * @if_outgoing1: outgoing interface for the first neighbor
2114 * @neigh2: the second neighbor object of the comparison
2115 * @if_outgoing2: outgoing interface for the second neighbor
2116 * @diff: pointer to integer receiving the calculated difference
2117 *
2118 * The content of *@diff is only valid when this function returns true.
2119 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2120 * the same as or higher than the metric via neigh2
2121 *
2122 * Return: true when the difference could be calculated, false otherwise
2123 */
2124static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2125                                     struct batadv_hard_iface *if_outgoing1,
2126                                     struct batadv_neigh_node *neigh2,
2127                                     struct batadv_hard_iface *if_outgoing2,
2128                                     int *diff)
2129{
2130        struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2131        u8 tq1, tq2;
2132        bool ret = true;
2133
2134        neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2135        neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2136
2137        if (!neigh1_ifinfo || !neigh2_ifinfo) {
2138                ret = false;
2139                goto out;
2140        }
2141
2142        tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2143        tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2144        *diff = (int)tq1 - (int)tq2;
2145
2146out:
2147        if (neigh1_ifinfo)
2148                batadv_neigh_ifinfo_put(neigh1_ifinfo);
2149        if (neigh2_ifinfo)
2150                batadv_neigh_ifinfo_put(neigh2_ifinfo);
2151
2152        return ret;
2153}
2154
2155/**
2156 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2157 * @msg: Netlink message to dump into
2158 * @portid: Port making netlink request
2159 * @seq: Sequence number of netlink message
2160 * @hardif_neigh: Neighbour to be dumped
2161 *
2162 * Return: Error code, or 0 on success
2163 */
2164static int
2165batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2166                               struct batadv_hardif_neigh_node *hardif_neigh)
2167{
2168        void *hdr;
2169        unsigned int last_seen_msecs;
2170
2171        last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2172
2173        hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2174                          NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2175        if (!hdr)
2176                return -ENOBUFS;
2177
2178        if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2179                    hardif_neigh->addr) ||
2180            nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2181                        hardif_neigh->if_incoming->net_dev->ifindex) ||
2182            nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2183                        last_seen_msecs))
2184                goto nla_put_failure;
2185
2186        genlmsg_end(msg, hdr);
2187        return 0;
2188
2189 nla_put_failure:
2190        genlmsg_cancel(msg, hdr);
2191        return -EMSGSIZE;
2192}
2193
2194/**
2195 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2196 *  into a message
2197 * @msg: Netlink message to dump into
2198 * @portid: Port making netlink request
2199 * @seq: Sequence number of netlink message
2200 * @bat_priv: The bat priv with all the soft interface information
2201 * @hard_iface: Hard interface to dump the neighbours for
2202 * @idx_s: Number of entries to skip
2203 *
2204 * This function assumes the caller holds rcu_read_lock().
2205 *
2206 * Return: Error code, or 0 on success
2207 */
2208static int
2209batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2210                                struct batadv_priv *bat_priv,
2211                                struct batadv_hard_iface *hard_iface,
2212                                int *idx_s)
2213{
2214        struct batadv_hardif_neigh_node *hardif_neigh;
2215        int idx = 0;
2216
2217        hlist_for_each_entry_rcu(hardif_neigh,
2218                                 &hard_iface->neigh_list, list) {
2219                if (idx++ < *idx_s)
2220                        continue;
2221
2222                if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2223                                                   hardif_neigh)) {
2224                        *idx_s = idx - 1;
2225                        return -EMSGSIZE;
2226                }
2227        }
2228
2229        *idx_s = 0;
2230        return 0;
2231}
2232
2233/**
2234 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2235 * @msg: Netlink message to dump into
2236 * @cb: Control block containing additional options
2237 * @bat_priv: The bat priv with all the soft interface information
2238 * @single_hardif: Limit dump to this hard interfaace
2239 */
2240static void
2241batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2242                         struct batadv_priv *bat_priv,
2243                         struct batadv_hard_iface *single_hardif)
2244{
2245        struct batadv_hard_iface *hard_iface;
2246        int i_hardif = 0;
2247        int i_hardif_s = cb->args[0];
2248        int idx = cb->args[1];
2249        int portid = NETLINK_CB(cb->skb).portid;
2250
2251        rcu_read_lock();
2252        if (single_hardif) {
2253                if (i_hardif_s == 0) {
2254                        if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2255                                                            cb->nlh->nlmsg_seq,
2256                                                            bat_priv,
2257                                                            single_hardif,
2258                                                            &idx) == 0)
2259                                i_hardif++;
2260                }
2261        } else {
2262                list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2263                                        list) {
2264                        if (hard_iface->soft_iface != bat_priv->soft_iface)
2265                                continue;
2266
2267                        if (i_hardif++ < i_hardif_s)
2268                                continue;
2269
2270                        if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2271                                                            cb->nlh->nlmsg_seq,
2272                                                            bat_priv,
2273                                                            hard_iface, &idx)) {
2274                                i_hardif--;
2275                                break;
2276                        }
2277                }
2278        }
2279        rcu_read_unlock();
2280
2281        cb->args[0] = i_hardif;
2282        cb->args[1] = idx;
2283}
2284
2285/**
2286 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2287 * @neigh1: the first neighbor object of the comparison
2288 * @if_outgoing1: outgoing interface for the first neighbor
2289 * @neigh2: the second neighbor object of the comparison
2290 * @if_outgoing2: outgoing interface for the second neighbor
2291 *
2292 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2293 * lower, the same as or higher than the metric via neigh2
2294 */
2295static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2296                                   struct batadv_hard_iface *if_outgoing1,
2297                                   struct batadv_neigh_node *neigh2,
2298                                   struct batadv_hard_iface *if_outgoing2)
2299{
2300        bool ret;
2301        int diff;
2302
2303        ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2304                                       if_outgoing2, &diff);
2305        if (!ret)
2306                return 0;
2307
2308        return diff;
2309}
2310
2311/**
2312 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2313 *  than neigh2 from the metric prospective
2314 * @neigh1: the first neighbor object of the comparison
2315 * @if_outgoing1: outgoing interface for the first neighbor
2316 * @neigh2: the second neighbor object of the comparison
2317 * @if_outgoing2: outgoing interface for the second neighbor
2318 *
2319 * Return: true if the metric via neigh1 is equally good or better than
2320 * the metric via neigh2, false otherwise.
2321 */
2322static bool
2323batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2324                           struct batadv_hard_iface *if_outgoing1,
2325                           struct batadv_neigh_node *neigh2,
2326                           struct batadv_hard_iface *if_outgoing2)
2327{
2328        bool ret;
2329        int diff;
2330
2331        ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2332                                       if_outgoing2, &diff);
2333        if (!ret)
2334                return false;
2335
2336        ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2337        return ret;
2338}
2339
2340static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2341{
2342        /* begin scheduling originator messages on that interface */
2343        batadv_iv_ogm_schedule(hard_iface);
2344}
2345
2346/**
2347 * batadv_iv_init_sel_class() - initialize GW selection class
2348 * @bat_priv: the bat priv with all the soft interface information
2349 */
2350static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2351{
2352        /* set default TQ difference threshold to 20 */
2353        atomic_set(&bat_priv->gw.sel_class, 20);
2354}
2355
2356static struct batadv_gw_node *
2357batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2358{
2359        struct batadv_neigh_node *router;
2360        struct batadv_neigh_ifinfo *router_ifinfo;
2361        struct batadv_gw_node *gw_node, *curr_gw = NULL;
2362        u64 max_gw_factor = 0;
2363        u64 tmp_gw_factor = 0;
2364        u8 max_tq = 0;
2365        u8 tq_avg;
2366        struct batadv_orig_node *orig_node;
2367
2368        rcu_read_lock();
2369        hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2370                orig_node = gw_node->orig_node;
2371                router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2372                if (!router)
2373                        continue;
2374
2375                router_ifinfo = batadv_neigh_ifinfo_get(router,
2376                                                        BATADV_IF_DEFAULT);
2377                if (!router_ifinfo)
2378                        goto next;
2379
2380                if (!kref_get_unless_zero(&gw_node->refcount))
2381                        goto next;
2382
2383                tq_avg = router_ifinfo->bat_iv.tq_avg;
2384
2385                switch (atomic_read(&bat_priv->gw.sel_class)) {
2386                case 1: /* fast connection */
2387                        tmp_gw_factor = tq_avg * tq_avg;
2388                        tmp_gw_factor *= gw_node->bandwidth_down;
2389                        tmp_gw_factor *= 100 * 100;
2390                        tmp_gw_factor >>= 18;
2391
2392                        if (tmp_gw_factor > max_gw_factor ||
2393                            (tmp_gw_factor == max_gw_factor &&
2394                             tq_avg > max_tq)) {
2395                                if (curr_gw)
2396                                        batadv_gw_node_put(curr_gw);
2397                                curr_gw = gw_node;
2398                                kref_get(&curr_gw->refcount);
2399                        }
2400                        break;
2401
2402                default: /* 2:  stable connection (use best statistic)
2403                          * 3:  fast-switch (use best statistic but change as
2404                          *     soon as a better gateway appears)
2405                          * XX: late-switch (use best statistic but change as
2406                          *     soon as a better gateway appears which has
2407                          *     $routing_class more tq points)
2408                          */
2409                        if (tq_avg > max_tq) {
2410                                if (curr_gw)
2411                                        batadv_gw_node_put(curr_gw);
2412                                curr_gw = gw_node;
2413                                kref_get(&curr_gw->refcount);
2414                        }
2415                        break;
2416                }
2417
2418                if (tq_avg > max_tq)
2419                        max_tq = tq_avg;
2420
2421                if (tmp_gw_factor > max_gw_factor)
2422                        max_gw_factor = tmp_gw_factor;
2423
2424                batadv_gw_node_put(gw_node);
2425
2426next:
2427                batadv_neigh_node_put(router);
2428                if (router_ifinfo)
2429                        batadv_neigh_ifinfo_put(router_ifinfo);
2430        }
2431        rcu_read_unlock();
2432
2433        return curr_gw;
2434}
2435
2436static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2437                                     struct batadv_orig_node *curr_gw_orig,
2438                                     struct batadv_orig_node *orig_node)
2439{
2440        struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2441        struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2442        struct batadv_neigh_node *router_gw = NULL;
2443        struct batadv_neigh_node *router_orig = NULL;
2444        u8 gw_tq_avg, orig_tq_avg;
2445        bool ret = false;
2446
2447        /* dynamic re-election is performed only on fast or late switch */
2448        if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2449                return false;
2450
2451        router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2452        if (!router_gw) {
2453                ret = true;
2454                goto out;
2455        }
2456
2457        router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2458                                                   BATADV_IF_DEFAULT);
2459        if (!router_gw_ifinfo) {
2460                ret = true;
2461                goto out;
2462        }
2463
2464        router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2465        if (!router_orig)
2466                goto out;
2467
2468        router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2469                                                     BATADV_IF_DEFAULT);
2470        if (!router_orig_ifinfo)
2471                goto out;
2472
2473        gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2474        orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2475
2476        /* the TQ value has to be better */
2477        if (orig_tq_avg < gw_tq_avg)
2478                goto out;
2479
2480        /* if the routing class is greater than 3 the value tells us how much
2481         * greater the TQ value of the new gateway must be
2482         */
2483        if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2484            (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2485                goto out;
2486
2487        batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2488                   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2489                   gw_tq_avg, orig_tq_avg);
2490
2491        ret = true;
2492out:
2493        if (router_gw_ifinfo)
2494                batadv_neigh_ifinfo_put(router_gw_ifinfo);
2495        if (router_orig_ifinfo)
2496                batadv_neigh_ifinfo_put(router_orig_ifinfo);
2497        if (router_gw)
2498                batadv_neigh_node_put(router_gw);
2499        if (router_orig)
2500                batadv_neigh_node_put(router_orig);
2501
2502        return ret;
2503}
2504
2505#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2506/* fails if orig_node has no router */
2507static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2508                                          struct seq_file *seq,
2509                                          const struct batadv_gw_node *gw_node)
2510{
2511        struct batadv_gw_node *curr_gw;
2512        struct batadv_neigh_node *router;
2513        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2514        int ret = -1;
2515
2516        router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2517        if (!router)
2518                goto out;
2519
2520        router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2521        if (!router_ifinfo)
2522                goto out;
2523
2524        curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2525
2526        seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2527                   (curr_gw == gw_node ? "=>" : "  "),
2528                   gw_node->orig_node->orig,
2529                   router_ifinfo->bat_iv.tq_avg, router->addr,
2530                   router->if_incoming->net_dev->name,
2531                   gw_node->bandwidth_down / 10,
2532                   gw_node->bandwidth_down % 10,
2533                   gw_node->bandwidth_up / 10,
2534                   gw_node->bandwidth_up % 10);
2535        ret = seq_has_overflowed(seq) ? -1 : 0;
2536
2537        if (curr_gw)
2538                batadv_gw_node_put(curr_gw);
2539out:
2540        if (router_ifinfo)
2541                batadv_neigh_ifinfo_put(router_ifinfo);
2542        if (router)
2543                batadv_neigh_node_put(router);
2544        return ret;
2545}
2546
2547static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2548                               struct seq_file *seq)
2549{
2550        struct batadv_gw_node *gw_node;
2551        int gw_count = 0;
2552
2553        seq_puts(seq,
2554                 "      Gateway      (#/255)           Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2555
2556        rcu_read_lock();
2557        hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2558                /* fails if orig_node has no router */
2559                if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2560                        continue;
2561
2562                gw_count++;
2563        }
2564        rcu_read_unlock();
2565
2566        if (gw_count == 0)
2567                seq_puts(seq, "No gateways in range ...\n");
2568}
2569#endif
2570
2571/**
2572 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2573 * @msg: Netlink message to dump into
2574 * @portid: Port making netlink request
2575 * @cb: Control block containing additional options
2576 * @bat_priv: The bat priv with all the soft interface information
2577 * @gw_node: Gateway to be dumped
2578 *
2579 * Return: Error code, or 0 on success
2580 */
2581static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2582                                   struct netlink_callback *cb,
2583                                   struct batadv_priv *bat_priv,
2584                                   struct batadv_gw_node *gw_node)
2585{
2586        struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2587        struct batadv_neigh_node *router;
2588        struct batadv_gw_node *curr_gw = NULL;
2589        int ret = 0;
2590        void *hdr;
2591
2592        router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2593        if (!router)
2594                goto out;
2595
2596        router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2597        if (!router_ifinfo)
2598                goto out;
2599
2600        curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2601
2602        hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2603                          &batadv_netlink_family, NLM_F_MULTI,
2604                          BATADV_CMD_GET_GATEWAYS);
2605        if (!hdr) {
2606                ret = -ENOBUFS;
2607                goto out;
2608        }
2609
2610        genl_dump_check_consistent(cb, hdr);
2611
2612        ret = -EMSGSIZE;
2613
2614        if (curr_gw == gw_node)
2615                if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2616                        genlmsg_cancel(msg, hdr);
2617                        goto out;
2618                }
2619
2620        if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2621                    gw_node->orig_node->orig) ||
2622            nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2623            nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2624                    router->addr) ||
2625            nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2626                           router->if_incoming->net_dev->name) ||
2627            nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2628                        gw_node->bandwidth_down) ||
2629            nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2630                        gw_node->bandwidth_up)) {
2631                genlmsg_cancel(msg, hdr);
2632                goto out;
2633        }
2634
2635        genlmsg_end(msg, hdr);
2636        ret = 0;
2637
2638out:
2639        if (curr_gw)
2640                batadv_gw_node_put(curr_gw);
2641        if (router_ifinfo)
2642                batadv_neigh_ifinfo_put(router_ifinfo);
2643        if (router)
2644                batadv_neigh_node_put(router);
2645        return ret;
2646}
2647
2648/**
2649 * batadv_iv_gw_dump() - Dump gateways into a message
2650 * @msg: Netlink message to dump into
2651 * @cb: Control block containing additional options
2652 * @bat_priv: The bat priv with all the soft interface information
2653 */
2654static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2655                              struct batadv_priv *bat_priv)
2656{
2657        int portid = NETLINK_CB(cb->skb).portid;
2658        struct batadv_gw_node *gw_node;
2659        int idx_skip = cb->args[0];
2660        int idx = 0;
2661
2662        spin_lock_bh(&bat_priv->gw.list_lock);
2663        cb->seq = bat_priv->gw.generation << 1 | 1;
2664
2665        hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2666                if (idx++ < idx_skip)
2667                        continue;
2668
2669                if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2670                                            gw_node)) {
2671                        idx_skip = idx - 1;
2672                        goto unlock;
2673                }
2674        }
2675
2676        idx_skip = idx;
2677unlock:
2678        spin_unlock_bh(&bat_priv->gw.list_lock);
2679
2680        cb->args[0] = idx_skip;
2681}
2682
2683static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2684        .name = "BATMAN_IV",
2685        .iface = {
2686                .activate = batadv_iv_iface_activate,
2687                .enable = batadv_iv_ogm_iface_enable,
2688                .disable = batadv_iv_ogm_iface_disable,
2689                .update_mac = batadv_iv_ogm_iface_update_mac,
2690                .primary_set = batadv_iv_ogm_primary_iface_set,
2691        },
2692        .neigh = {
2693                .cmp = batadv_iv_ogm_neigh_cmp,
2694                .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2695#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2696                .print = batadv_iv_neigh_print,
2697#endif
2698                .dump = batadv_iv_ogm_neigh_dump,
2699        },
2700        .orig = {
2701#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2702                .print = batadv_iv_ogm_orig_print,
2703#endif
2704                .dump = batadv_iv_ogm_orig_dump,
2705        },
2706        .gw = {
2707                .init_sel_class = batadv_iv_init_sel_class,
2708                .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2709                .is_eligible = batadv_iv_gw_is_eligible,
2710#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2711                .print = batadv_iv_gw_print,
2712#endif
2713                .dump = batadv_iv_gw_dump,
2714        },
2715};
2716
2717/**
2718 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2719 *
2720 * Return: 0 on success or negative error number in case of failure
2721 */
2722int __init batadv_iv_init(void)
2723{
2724        int ret;
2725
2726        /* batman originator packet */
2727        ret = batadv_recv_handler_register(BATADV_IV_OGM,
2728                                           batadv_iv_ogm_receive);
2729        if (ret < 0)
2730                goto out;
2731
2732        ret = batadv_algo_register(&batadv_batman_iv);
2733        if (ret < 0)
2734                goto handler_unregister;
2735
2736        goto out;
2737
2738handler_unregister:
2739        batadv_recv_handler_unregister(BATADV_IV_OGM);
2740out:
2741        return ret;
2742}
2743