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