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