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 61 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work); 62 63 /** 64 * enum batadv_dup_status - duplicate status 65 */ 66 enum 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 */ 91 static 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 */ 104 static 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 exist, it is created and initialised. 138 */ 139 static struct batadv_orig_node * 140 batadv_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 164 free_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 173 static struct batadv_neigh_node * 174 batadv_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 188 out: 189 return neigh_node; 190 } 191 192 static 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 226 static 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 236 static 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 253 unlock: 254 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex); 255 } 256 257 static void 258 batadv_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 272 unlock: 273 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex); 274 } 275 276 /* when do we schedule our own ogm to be sent */ 277 static unsigned long 278 batadv_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_max(2 * BATADV_JITTER); 284 285 return jiffies + msecs_to_jiffies(msecs); 286 } 287 288 /* when do we schedule a ogm packet to be sent */ 289 static unsigned long batadv_iv_ogm_fwd_send_time(void) 290 { 291 return jiffies + msecs_to_jiffies(prandom_u32_max(BATADV_JITTER / 2)); 292 } 293 294 /* apply hop penalty for a normal link */ 295 static 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 */ 314 static bool 315 batadv_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 */ 333 static 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 */ 398 static 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 */ 436 static bool 437 batadv_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 519 out: 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 */ 536 static 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 */ 591 static 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 */ 618 static 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 681 static 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 */ 739 static void 740 batadv_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 */ 779 static 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 857 out: 858 if (primary_if) 859 batadv_hardif_put(primary_if); 860 } 861 862 static 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 interface 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 */ 883 static 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 */ 914 static void 915 batadv_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 1044 unlock: 1045 rcu_read_unlock(); 1046 out: 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 */ 1067 static 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 tq_iface_hop_penalty = BATADV_TQ_MAX_VALUE; 1079 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; 1080 unsigned int tq_asym_penalty, inv_asym_penalty; 1081 unsigned int combined_tq; 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 tq_iface_hop_penalty -= atomic_read(&if_incoming->hop_penalty); 1161 1162 /* penalize if the OGM is forwarded on the same interface. WiFi 1163 * interfaces and other half duplex devices suffer from throughput 1164 * drops as they can't send and receive at the same time. 1165 */ 1166 if (if_outgoing && if_incoming == if_outgoing && 1167 batadv_is_wifi_hardif(if_outgoing)) 1168 tq_iface_hop_penalty = batadv_hop_penalty(tq_iface_hop_penalty, 1169 bat_priv); 1170 1171 combined_tq = batadv_ogm_packet->tq * 1172 tq_own * 1173 tq_asym_penalty * 1174 tq_iface_hop_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_hop_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, 1184 tq_iface_hop_penalty, batadv_ogm_packet->tq, 1185 if_incoming->net_dev->name, 1186 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT"); 1187 1188 /* if link has the minimum required transmission quality 1189 * consider it bidirectional 1190 */ 1191 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT) 1192 ret = true; 1193 1194 out: 1195 if (neigh_node) 1196 batadv_neigh_node_put(neigh_node); 1197 return ret; 1198 } 1199 1200 /** 1201 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces, 1202 * adjust the sequence number and find out whether it is a duplicate 1203 * @ethhdr: ethernet header of the packet 1204 * @batadv_ogm_packet: OGM packet to be considered 1205 * @if_incoming: interface on which the OGM packet was received 1206 * @if_outgoing: interface for which the retransmission should be considered 1207 * 1208 * Return: duplicate status as enum batadv_dup_status 1209 */ 1210 static enum batadv_dup_status 1211 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, 1212 const struct batadv_ogm_packet *batadv_ogm_packet, 1213 const struct batadv_hard_iface *if_incoming, 1214 struct batadv_hard_iface *if_outgoing) 1215 { 1216 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1217 struct batadv_orig_node *orig_node; 1218 struct batadv_orig_ifinfo *orig_ifinfo = NULL; 1219 struct batadv_neigh_node *neigh_node; 1220 struct batadv_neigh_ifinfo *neigh_ifinfo; 1221 bool is_dup; 1222 s32 seq_diff; 1223 bool need_update = false; 1224 int set_mark; 1225 enum batadv_dup_status ret = BATADV_NO_DUP; 1226 u32 seqno = ntohl(batadv_ogm_packet->seqno); 1227 u8 *neigh_addr; 1228 u8 packet_count; 1229 unsigned long *bitmap; 1230 1231 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); 1232 if (!orig_node) 1233 return BATADV_NO_DUP; 1234 1235 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 1236 if (WARN_ON(!orig_ifinfo)) { 1237 batadv_orig_node_put(orig_node); 1238 return 0; 1239 } 1240 1241 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1242 seq_diff = seqno - orig_ifinfo->last_real_seqno; 1243 1244 /* signalize caller that the packet is to be dropped. */ 1245 if (!hlist_empty(&orig_node->neigh_list) && 1246 batadv_window_protected(bat_priv, seq_diff, 1247 BATADV_TQ_LOCAL_WINDOW_SIZE, 1248 &orig_ifinfo->batman_seqno_reset, NULL)) { 1249 ret = BATADV_PROTECTED; 1250 goto out; 1251 } 1252 1253 rcu_read_lock(); 1254 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { 1255 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, 1256 if_outgoing); 1257 if (!neigh_ifinfo) 1258 continue; 1259 1260 neigh_addr = neigh_node->addr; 1261 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits, 1262 orig_ifinfo->last_real_seqno, 1263 seqno); 1264 1265 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && 1266 neigh_node->if_incoming == if_incoming) { 1267 set_mark = 1; 1268 if (is_dup) 1269 ret = BATADV_NEIGH_DUP; 1270 } else { 1271 set_mark = 0; 1272 if (is_dup && ret != BATADV_NEIGH_DUP) 1273 ret = BATADV_ORIG_DUP; 1274 } 1275 1276 /* if the window moved, set the update flag. */ 1277 bitmap = neigh_ifinfo->bat_iv.real_bits; 1278 need_update |= batadv_bit_get_packet(bat_priv, bitmap, 1279 seq_diff, set_mark); 1280 1281 packet_count = bitmap_weight(bitmap, 1282 BATADV_TQ_LOCAL_WINDOW_SIZE); 1283 neigh_ifinfo->bat_iv.real_packet_count = packet_count; 1284 batadv_neigh_ifinfo_put(neigh_ifinfo); 1285 } 1286 rcu_read_unlock(); 1287 1288 if (need_update) { 1289 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1290 "%s updating last_seqno: old %u, new %u\n", 1291 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT", 1292 orig_ifinfo->last_real_seqno, seqno); 1293 orig_ifinfo->last_real_seqno = seqno; 1294 } 1295 1296 out: 1297 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1298 batadv_orig_node_put(orig_node); 1299 batadv_orig_ifinfo_put(orig_ifinfo); 1300 return ret; 1301 } 1302 1303 /** 1304 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing 1305 * interface 1306 * @skb: the skb containing the OGM 1307 * @ogm_offset: offset from skb->data to start of ogm header 1308 * @orig_node: the (cached) orig node for the originator of this OGM 1309 * @if_incoming: the interface where this packet was received 1310 * @if_outgoing: the interface for which the packet should be considered 1311 */ 1312 static void 1313 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset, 1314 struct batadv_orig_node *orig_node, 1315 struct batadv_hard_iface *if_incoming, 1316 struct batadv_hard_iface *if_outgoing) 1317 { 1318 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1319 struct batadv_hardif_neigh_node *hardif_neigh = NULL; 1320 struct batadv_neigh_node *router = NULL; 1321 struct batadv_neigh_node *router_router = NULL; 1322 struct batadv_orig_node *orig_neigh_node; 1323 struct batadv_orig_ifinfo *orig_ifinfo; 1324 struct batadv_neigh_node *orig_neigh_router = NULL; 1325 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 1326 struct batadv_ogm_packet *ogm_packet; 1327 enum batadv_dup_status dup_status; 1328 bool is_from_best_next_hop = false; 1329 bool is_single_hop_neigh = false; 1330 bool sameseq, similar_ttl; 1331 struct sk_buff *skb_priv; 1332 struct ethhdr *ethhdr; 1333 u8 *prev_sender; 1334 bool is_bidirect; 1335 1336 /* create a private copy of the skb, as some functions change tq value 1337 * and/or flags. 1338 */ 1339 skb_priv = skb_copy(skb, GFP_ATOMIC); 1340 if (!skb_priv) 1341 return; 1342 1343 ethhdr = eth_hdr(skb_priv); 1344 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset); 1345 1346 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet, 1347 if_incoming, if_outgoing); 1348 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig)) 1349 is_single_hop_neigh = true; 1350 1351 if (dup_status == BATADV_PROTECTED) { 1352 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1353 "Drop packet: packet within seqno protection time (sender: %pM)\n", 1354 ethhdr->h_source); 1355 goto out; 1356 } 1357 1358 if (ogm_packet->tq == 0) { 1359 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1360 "Drop packet: originator packet with tq equal 0\n"); 1361 goto out; 1362 } 1363 1364 if (is_single_hop_neigh) { 1365 hardif_neigh = batadv_hardif_neigh_get(if_incoming, 1366 ethhdr->h_source); 1367 if (hardif_neigh) 1368 hardif_neigh->last_seen = jiffies; 1369 } 1370 1371 router = batadv_orig_router_get(orig_node, if_outgoing); 1372 if (router) { 1373 router_router = batadv_orig_router_get(router->orig_node, 1374 if_outgoing); 1375 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); 1376 } 1377 1378 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) && 1379 (batadv_compare_eth(router->addr, ethhdr->h_source))) 1380 is_from_best_next_hop = true; 1381 1382 prev_sender = ogm_packet->prev_sender; 1383 /* avoid temporary routing loops */ 1384 if (router && router_router && 1385 (batadv_compare_eth(router->addr, prev_sender)) && 1386 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) && 1387 (batadv_compare_eth(router->addr, router_router->addr))) { 1388 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1389 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", 1390 ethhdr->h_source); 1391 goto out; 1392 } 1393 1394 if (if_outgoing == BATADV_IF_DEFAULT) 1395 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node); 1396 1397 /* if sender is a direct neighbor the sender mac equals 1398 * originator mac 1399 */ 1400 if (is_single_hop_neigh) 1401 orig_neigh_node = orig_node; 1402 else 1403 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, 1404 ethhdr->h_source); 1405 1406 if (!orig_neigh_node) 1407 goto out; 1408 1409 /* Update nc_nodes of the originator */ 1410 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node, 1411 ogm_packet, is_single_hop_neigh); 1412 1413 orig_neigh_router = batadv_orig_router_get(orig_neigh_node, 1414 if_outgoing); 1415 1416 /* drop packet if sender is not a direct neighbor and if we 1417 * don't route towards it 1418 */ 1419 if (!is_single_hop_neigh && !orig_neigh_router) { 1420 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1421 "Drop packet: OGM via unknown neighbor!\n"); 1422 goto out_neigh; 1423 } 1424 1425 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node, 1426 ogm_packet, if_incoming, 1427 if_outgoing); 1428 1429 /* update ranking if it is not a duplicate or has the same 1430 * seqno and similar ttl as the non-duplicate 1431 */ 1432 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 1433 if (!orig_ifinfo) 1434 goto out_neigh; 1435 1436 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno); 1437 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl; 1438 1439 if (is_bidirect && (dup_status == BATADV_NO_DUP || 1440 (sameseq && similar_ttl))) { 1441 batadv_iv_ogm_orig_update(bat_priv, orig_node, 1442 orig_ifinfo, ethhdr, 1443 ogm_packet, if_incoming, 1444 if_outgoing, dup_status); 1445 } 1446 batadv_orig_ifinfo_put(orig_ifinfo); 1447 1448 /* only forward for specific interface, not for the default one. */ 1449 if (if_outgoing == BATADV_IF_DEFAULT) 1450 goto out_neigh; 1451 1452 /* is single hop (direct) neighbor */ 1453 if (is_single_hop_neigh) { 1454 /* OGMs from secondary interfaces should only scheduled once 1455 * per interface where it has been received, not multiple times 1456 */ 1457 if (ogm_packet->ttl <= 2 && 1458 if_incoming != if_outgoing) { 1459 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1460 "Drop packet: OGM from secondary interface and wrong outgoing interface\n"); 1461 goto out_neigh; 1462 } 1463 /* mark direct link on incoming interface */ 1464 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, 1465 is_single_hop_neigh, 1466 is_from_best_next_hop, if_incoming, 1467 if_outgoing); 1468 1469 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1470 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); 1471 goto out_neigh; 1472 } 1473 1474 /* multihop originator */ 1475 if (!is_bidirect) { 1476 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1477 "Drop packet: not received via bidirectional link\n"); 1478 goto out_neigh; 1479 } 1480 1481 if (dup_status == BATADV_NEIGH_DUP) { 1482 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1483 "Drop packet: duplicate packet received\n"); 1484 goto out_neigh; 1485 } 1486 1487 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1488 "Forwarding packet: rebroadcast originator packet\n"); 1489 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, 1490 is_single_hop_neigh, is_from_best_next_hop, 1491 if_incoming, if_outgoing); 1492 1493 out_neigh: 1494 if (orig_neigh_node && !is_single_hop_neigh) 1495 batadv_orig_node_put(orig_neigh_node); 1496 out: 1497 if (router_ifinfo) 1498 batadv_neigh_ifinfo_put(router_ifinfo); 1499 if (router) 1500 batadv_neigh_node_put(router); 1501 if (router_router) 1502 batadv_neigh_node_put(router_router); 1503 if (orig_neigh_router) 1504 batadv_neigh_node_put(orig_neigh_router); 1505 if (hardif_neigh) 1506 batadv_hardif_neigh_put(hardif_neigh); 1507 1508 consume_skb(skb_priv); 1509 } 1510 1511 /** 1512 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it 1513 * @ogm_packet: rebroadcast OGM packet to process 1514 * @if_incoming: the interface where this packet was received 1515 * @orig_node: originator which reproadcasted the OGMs 1516 * @if_incoming_seqno: OGM sequence number when rebroadcast was received 1517 */ 1518 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet, 1519 struct batadv_hard_iface *if_incoming, 1520 struct batadv_orig_node *orig_node, 1521 u32 if_incoming_seqno) 1522 { 1523 struct batadv_orig_ifinfo *orig_ifinfo; 1524 s32 bit_pos; 1525 u8 *weight; 1526 1527 /* neighbor has to indicate direct link and it has to 1528 * come via the corresponding interface 1529 */ 1530 if (!(ogm_packet->flags & BATADV_DIRECTLINK)) 1531 return; 1532 1533 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr, 1534 ogm_packet->orig)) 1535 return; 1536 1537 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming); 1538 if (!orig_ifinfo) 1539 return; 1540 1541 /* save packet seqno for bidirectional check */ 1542 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1543 bit_pos = if_incoming_seqno - 2; 1544 bit_pos -= ntohl(ogm_packet->seqno); 1545 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos); 1546 weight = &orig_ifinfo->bat_iv.bcast_own_sum; 1547 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own, 1548 BATADV_TQ_LOCAL_WINDOW_SIZE); 1549 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1550 1551 batadv_orig_ifinfo_put(orig_ifinfo); 1552 } 1553 1554 /** 1555 * batadv_iv_ogm_process() - process an incoming batman iv OGM 1556 * @skb: the skb containing the OGM 1557 * @ogm_offset: offset to the OGM which should be processed (for aggregates) 1558 * @if_incoming: the interface where this packet was received 1559 */ 1560 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset, 1561 struct batadv_hard_iface *if_incoming) 1562 { 1563 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1564 struct batadv_orig_node *orig_neigh_node, *orig_node; 1565 struct batadv_hard_iface *hard_iface; 1566 struct batadv_ogm_packet *ogm_packet; 1567 u32 if_incoming_seqno; 1568 bool has_directlink_flag; 1569 struct ethhdr *ethhdr; 1570 bool is_my_oldorig = false; 1571 bool is_my_addr = false; 1572 bool is_my_orig = false; 1573 1574 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset); 1575 ethhdr = eth_hdr(skb); 1576 1577 /* Silently drop when the batman packet is actually not a 1578 * correct packet. 1579 * 1580 * This might happen if a packet is padded (e.g. Ethernet has a 1581 * minimum frame length of 64 byte) and the aggregation interprets 1582 * it as an additional length. 1583 * 1584 * TODO: A more sane solution would be to have a bit in the 1585 * batadv_ogm_packet to detect whether the packet is the last 1586 * packet in an aggregation. Here we expect that the padding 1587 * is always zero (or not 0x01) 1588 */ 1589 if (ogm_packet->packet_type != BATADV_IV_OGM) 1590 return; 1591 1592 /* could be changed by schedule_own_packet() */ 1593 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno); 1594 1595 if (ogm_packet->flags & BATADV_DIRECTLINK) 1596 has_directlink_flag = true; 1597 else 1598 has_directlink_flag = false; 1599 1600 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1601 "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", 1602 ethhdr->h_source, if_incoming->net_dev->name, 1603 if_incoming->net_dev->dev_addr, ogm_packet->orig, 1604 ogm_packet->prev_sender, ntohl(ogm_packet->seqno), 1605 ogm_packet->tq, ogm_packet->ttl, 1606 ogm_packet->version, has_directlink_flag); 1607 1608 rcu_read_lock(); 1609 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 1610 if (hard_iface->if_status != BATADV_IF_ACTIVE) 1611 continue; 1612 1613 if (hard_iface->soft_iface != if_incoming->soft_iface) 1614 continue; 1615 1616 if (batadv_compare_eth(ethhdr->h_source, 1617 hard_iface->net_dev->dev_addr)) 1618 is_my_addr = true; 1619 1620 if (batadv_compare_eth(ogm_packet->orig, 1621 hard_iface->net_dev->dev_addr)) 1622 is_my_orig = true; 1623 1624 if (batadv_compare_eth(ogm_packet->prev_sender, 1625 hard_iface->net_dev->dev_addr)) 1626 is_my_oldorig = true; 1627 } 1628 rcu_read_unlock(); 1629 1630 if (is_my_addr) { 1631 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1632 "Drop packet: received my own broadcast (sender: %pM)\n", 1633 ethhdr->h_source); 1634 return; 1635 } 1636 1637 if (is_my_orig) { 1638 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, 1639 ethhdr->h_source); 1640 if (!orig_neigh_node) 1641 return; 1642 1643 batadv_iv_ogm_process_reply(ogm_packet, if_incoming, 1644 orig_neigh_node, if_incoming_seqno); 1645 1646 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1647 "Drop packet: originator packet from myself (via neighbor)\n"); 1648 batadv_orig_node_put(orig_neigh_node); 1649 return; 1650 } 1651 1652 if (is_my_oldorig) { 1653 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1654 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", 1655 ethhdr->h_source); 1656 return; 1657 } 1658 1659 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) { 1660 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1661 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", 1662 ethhdr->h_source); 1663 return; 1664 } 1665 1666 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig); 1667 if (!orig_node) 1668 return; 1669 1670 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, 1671 if_incoming, BATADV_IF_DEFAULT); 1672 1673 rcu_read_lock(); 1674 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 1675 if (hard_iface->if_status != BATADV_IF_ACTIVE) 1676 continue; 1677 1678 if (hard_iface->soft_iface != bat_priv->soft_iface) 1679 continue; 1680 1681 if (!kref_get_unless_zero(&hard_iface->refcount)) 1682 continue; 1683 1684 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, 1685 if_incoming, hard_iface); 1686 1687 batadv_hardif_put(hard_iface); 1688 } 1689 rcu_read_unlock(); 1690 1691 batadv_orig_node_put(orig_node); 1692 } 1693 1694 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work) 1695 { 1696 struct delayed_work *delayed_work; 1697 struct batadv_forw_packet *forw_packet; 1698 struct batadv_priv *bat_priv; 1699 bool dropped = false; 1700 1701 delayed_work = to_delayed_work(work); 1702 forw_packet = container_of(delayed_work, struct batadv_forw_packet, 1703 delayed_work); 1704 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface); 1705 1706 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) { 1707 dropped = true; 1708 goto out; 1709 } 1710 1711 batadv_iv_ogm_emit(forw_packet); 1712 1713 /* we have to have at least one packet in the queue to determine the 1714 * queues wake up time unless we are shutting down. 1715 * 1716 * only re-schedule if this is the "original" copy, e.g. the OGM of the 1717 * primary interface should only be rescheduled once per period, but 1718 * this function will be called for the forw_packet instances of the 1719 * other secondary interfaces as well. 1720 */ 1721 if (forw_packet->own && 1722 forw_packet->if_incoming == forw_packet->if_outgoing) 1723 batadv_iv_ogm_schedule(forw_packet->if_incoming); 1724 1725 out: 1726 /* do we get something for free()? */ 1727 if (batadv_forw_packet_steal(forw_packet, 1728 &bat_priv->forw_bat_list_lock)) 1729 batadv_forw_packet_free(forw_packet, dropped); 1730 } 1731 1732 static int batadv_iv_ogm_receive(struct sk_buff *skb, 1733 struct batadv_hard_iface *if_incoming) 1734 { 1735 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1736 struct batadv_ogm_packet *ogm_packet; 1737 u8 *packet_pos; 1738 int ogm_offset; 1739 bool res; 1740 int ret = NET_RX_DROP; 1741 1742 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); 1743 if (!res) 1744 goto free_skb; 1745 1746 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface 1747 * that does not have B.A.T.M.A.N. IV enabled ? 1748 */ 1749 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable) 1750 goto free_skb; 1751 1752 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); 1753 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, 1754 skb->len + ETH_HLEN); 1755 1756 ogm_offset = 0; 1757 ogm_packet = (struct batadv_ogm_packet *)skb->data; 1758 1759 /* unpack the aggregated packets and process them one by one */ 1760 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb), 1761 ogm_packet)) { 1762 batadv_iv_ogm_process(skb, ogm_offset, if_incoming); 1763 1764 ogm_offset += BATADV_OGM_HLEN; 1765 ogm_offset += ntohs(ogm_packet->tvlv_len); 1766 1767 packet_pos = skb->data + ogm_offset; 1768 ogm_packet = (struct batadv_ogm_packet *)packet_pos; 1769 } 1770 1771 ret = NET_RX_SUCCESS; 1772 1773 free_skb: 1774 if (ret == NET_RX_SUCCESS) 1775 consume_skb(skb); 1776 else 1777 kfree_skb(skb); 1778 1779 return ret; 1780 } 1781 1782 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 1783 /** 1784 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table 1785 * @orig_node: the orig_node for which the neighbors are printed 1786 * @if_outgoing: outgoing interface for these entries 1787 * @seq: debugfs table seq_file struct 1788 * 1789 * Must be called while holding an rcu lock. 1790 */ 1791 static void 1792 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node, 1793 struct batadv_hard_iface *if_outgoing, 1794 struct seq_file *seq) 1795 { 1796 struct batadv_neigh_node *neigh_node; 1797 struct batadv_neigh_ifinfo *n_ifinfo; 1798 1799 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { 1800 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); 1801 if (!n_ifinfo) 1802 continue; 1803 1804 seq_printf(seq, " %pM (%3i)", 1805 neigh_node->addr, 1806 n_ifinfo->bat_iv.tq_avg); 1807 1808 batadv_neigh_ifinfo_put(n_ifinfo); 1809 } 1810 } 1811 1812 /** 1813 * batadv_iv_ogm_orig_print() - print the originator table 1814 * @bat_priv: the bat priv with all the soft interface information 1815 * @seq: debugfs table seq_file struct 1816 * @if_outgoing: the outgoing interface for which this should be printed 1817 */ 1818 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, 1819 struct seq_file *seq, 1820 struct batadv_hard_iface *if_outgoing) 1821 { 1822 struct batadv_neigh_node *neigh_node; 1823 struct batadv_hashtable *hash = bat_priv->orig_hash; 1824 int last_seen_msecs, last_seen_secs; 1825 struct batadv_orig_node *orig_node; 1826 struct batadv_neigh_ifinfo *n_ifinfo; 1827 unsigned long last_seen_jiffies; 1828 struct hlist_head *head; 1829 int batman_count = 0; 1830 u32 i; 1831 1832 seq_puts(seq, 1833 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n"); 1834 1835 for (i = 0; i < hash->size; i++) { 1836 head = &hash->table[i]; 1837 1838 rcu_read_lock(); 1839 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 1840 neigh_node = batadv_orig_router_get(orig_node, 1841 if_outgoing); 1842 if (!neigh_node) 1843 continue; 1844 1845 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, 1846 if_outgoing); 1847 if (!n_ifinfo) 1848 goto next; 1849 1850 if (n_ifinfo->bat_iv.tq_avg == 0) 1851 goto next; 1852 1853 last_seen_jiffies = jiffies - orig_node->last_seen; 1854 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 1855 last_seen_secs = last_seen_msecs / 1000; 1856 last_seen_msecs = last_seen_msecs % 1000; 1857 1858 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", 1859 orig_node->orig, last_seen_secs, 1860 last_seen_msecs, n_ifinfo->bat_iv.tq_avg, 1861 neigh_node->addr, 1862 neigh_node->if_incoming->net_dev->name); 1863 1864 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing, 1865 seq); 1866 seq_putc(seq, '\n'); 1867 batman_count++; 1868 1869 next: 1870 batadv_neigh_node_put(neigh_node); 1871 if (n_ifinfo) 1872 batadv_neigh_ifinfo_put(n_ifinfo); 1873 } 1874 rcu_read_unlock(); 1875 } 1876 1877 if (batman_count == 0) 1878 seq_puts(seq, "No batman nodes in range ...\n"); 1879 } 1880 #endif 1881 1882 /** 1883 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a 1884 * given outgoing interface. 1885 * @neigh_node: Neighbour of interest 1886 * @if_outgoing: Outgoing interface of interest 1887 * @tq_avg: Pointer of where to store the TQ average 1888 * 1889 * Return: False if no average TQ available, otherwise true. 1890 */ 1891 static bool 1892 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node, 1893 struct batadv_hard_iface *if_outgoing, 1894 u8 *tq_avg) 1895 { 1896 struct batadv_neigh_ifinfo *n_ifinfo; 1897 1898 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); 1899 if (!n_ifinfo) 1900 return false; 1901 1902 *tq_avg = n_ifinfo->bat_iv.tq_avg; 1903 batadv_neigh_ifinfo_put(n_ifinfo); 1904 1905 return true; 1906 } 1907 1908 /** 1909 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a 1910 * message 1911 * @msg: Netlink message to dump into 1912 * @portid: Port making netlink request 1913 * @seq: Sequence number of netlink message 1914 * @bat_priv: The bat priv with all the soft interface information 1915 * @if_outgoing: Limit dump to entries with this outgoing interface 1916 * @orig_node: Originator to dump 1917 * @neigh_node: Single hops neighbour 1918 * @best: Is the best originator 1919 * 1920 * Return: Error code, or 0 on success 1921 */ 1922 static int 1923 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq, 1924 struct batadv_priv *bat_priv, 1925 struct batadv_hard_iface *if_outgoing, 1926 struct batadv_orig_node *orig_node, 1927 struct batadv_neigh_node *neigh_node, 1928 bool best) 1929 { 1930 void *hdr; 1931 u8 tq_avg; 1932 unsigned int last_seen_msecs; 1933 1934 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen); 1935 1936 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg)) 1937 return 0; 1938 1939 if (if_outgoing != BATADV_IF_DEFAULT && 1940 if_outgoing != neigh_node->if_incoming) 1941 return 0; 1942 1943 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, 1944 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS); 1945 if (!hdr) 1946 return -ENOBUFS; 1947 1948 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, 1949 orig_node->orig) || 1950 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN, 1951 neigh_node->addr) || 1952 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, 1953 neigh_node->if_incoming->net_dev->ifindex) || 1954 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) || 1955 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, 1956 last_seen_msecs)) 1957 goto nla_put_failure; 1958 1959 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) 1960 goto nla_put_failure; 1961 1962 genlmsg_end(msg, hdr); 1963 return 0; 1964 1965 nla_put_failure: 1966 genlmsg_cancel(msg, hdr); 1967 return -EMSGSIZE; 1968 } 1969 1970 /** 1971 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message 1972 * @msg: Netlink message to dump into 1973 * @portid: Port making netlink request 1974 * @seq: Sequence number of netlink message 1975 * @bat_priv: The bat priv with all the soft interface information 1976 * @if_outgoing: Limit dump to entries with this outgoing interface 1977 * @orig_node: Originator to dump 1978 * @sub_s: Number of sub entries to skip 1979 * 1980 * This function assumes the caller holds rcu_read_lock(). 1981 * 1982 * Return: Error code, or 0 on success 1983 */ 1984 static int 1985 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, 1986 struct batadv_priv *bat_priv, 1987 struct batadv_hard_iface *if_outgoing, 1988 struct batadv_orig_node *orig_node, int *sub_s) 1989 { 1990 struct batadv_neigh_node *neigh_node_best; 1991 struct batadv_neigh_node *neigh_node; 1992 int sub = 0; 1993 bool best; 1994 u8 tq_avg_best; 1995 1996 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing); 1997 if (!neigh_node_best) 1998 goto out; 1999 2000 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing, 2001 &tq_avg_best)) 2002 goto out; 2003 2004 if (tq_avg_best == 0) 2005 goto out; 2006 2007 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { 2008 if (sub++ < *sub_s) 2009 continue; 2010 2011 best = (neigh_node == neigh_node_best); 2012 2013 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq, 2014 bat_priv, if_outgoing, 2015 orig_node, neigh_node, 2016 best)) { 2017 batadv_neigh_node_put(neigh_node_best); 2018 2019 *sub_s = sub - 1; 2020 return -EMSGSIZE; 2021 } 2022 } 2023 2024 out: 2025 if (neigh_node_best) 2026 batadv_neigh_node_put(neigh_node_best); 2027 2028 *sub_s = 0; 2029 return 0; 2030 } 2031 2032 /** 2033 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a 2034 * message 2035 * @msg: Netlink message to dump into 2036 * @portid: Port making netlink request 2037 * @seq: Sequence number of netlink message 2038 * @bat_priv: The bat priv with all the soft interface information 2039 * @if_outgoing: Limit dump to entries with this outgoing interface 2040 * @head: Bucket to be dumped 2041 * @idx_s: Number of entries to be skipped 2042 * @sub: Number of sub entries to be skipped 2043 * 2044 * Return: Error code, or 0 on success 2045 */ 2046 static int 2047 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq, 2048 struct batadv_priv *bat_priv, 2049 struct batadv_hard_iface *if_outgoing, 2050 struct hlist_head *head, int *idx_s, int *sub) 2051 { 2052 struct batadv_orig_node *orig_node; 2053 int idx = 0; 2054 2055 rcu_read_lock(); 2056 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 2057 if (idx++ < *idx_s) 2058 continue; 2059 2060 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv, 2061 if_outgoing, orig_node, 2062 sub)) { 2063 rcu_read_unlock(); 2064 *idx_s = idx - 1; 2065 return -EMSGSIZE; 2066 } 2067 } 2068 rcu_read_unlock(); 2069 2070 *idx_s = 0; 2071 *sub = 0; 2072 return 0; 2073 } 2074 2075 /** 2076 * batadv_iv_ogm_orig_dump() - Dump the originators into a message 2077 * @msg: Netlink message to dump into 2078 * @cb: Control block containing additional options 2079 * @bat_priv: The bat priv with all the soft interface information 2080 * @if_outgoing: Limit dump to entries with this outgoing interface 2081 */ 2082 static void 2083 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb, 2084 struct batadv_priv *bat_priv, 2085 struct batadv_hard_iface *if_outgoing) 2086 { 2087 struct batadv_hashtable *hash = bat_priv->orig_hash; 2088 struct hlist_head *head; 2089 int bucket = cb->args[0]; 2090 int idx = cb->args[1]; 2091 int sub = cb->args[2]; 2092 int portid = NETLINK_CB(cb->skb).portid; 2093 2094 while (bucket < hash->size) { 2095 head = &hash->table[bucket]; 2096 2097 if (batadv_iv_ogm_orig_dump_bucket(msg, portid, 2098 cb->nlh->nlmsg_seq, 2099 bat_priv, if_outgoing, head, 2100 &idx, &sub)) 2101 break; 2102 2103 bucket++; 2104 } 2105 2106 cb->args[0] = bucket; 2107 cb->args[1] = idx; 2108 cb->args[2] = sub; 2109 } 2110 2111 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 2112 /** 2113 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node 2114 * @seq: neighbour table seq_file struct 2115 * @hardif_neigh: hardif neighbour information 2116 */ 2117 static void 2118 batadv_iv_hardif_neigh_print(struct seq_file *seq, 2119 struct batadv_hardif_neigh_node *hardif_neigh) 2120 { 2121 int last_secs, last_msecs; 2122 2123 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000; 2124 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000; 2125 2126 seq_printf(seq, " %10s %pM %4i.%03is\n", 2127 hardif_neigh->if_incoming->net_dev->name, 2128 hardif_neigh->addr, last_secs, last_msecs); 2129 } 2130 2131 /** 2132 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list 2133 * @bat_priv: the bat priv with all the soft interface information 2134 * @seq: neighbour table seq_file struct 2135 */ 2136 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv, 2137 struct seq_file *seq) 2138 { 2139 struct net_device *net_dev = (struct net_device *)seq->private; 2140 struct batadv_hardif_neigh_node *hardif_neigh; 2141 struct batadv_hard_iface *hard_iface; 2142 int batman_count = 0; 2143 2144 seq_puts(seq, " IF Neighbor last-seen\n"); 2145 2146 rcu_read_lock(); 2147 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 2148 if (hard_iface->soft_iface != net_dev) 2149 continue; 2150 2151 hlist_for_each_entry_rcu(hardif_neigh, 2152 &hard_iface->neigh_list, list) { 2153 batadv_iv_hardif_neigh_print(seq, hardif_neigh); 2154 batman_count++; 2155 } 2156 } 2157 rcu_read_unlock(); 2158 2159 if (batman_count == 0) 2160 seq_puts(seq, "No batman nodes in range ...\n"); 2161 } 2162 #endif 2163 2164 /** 2165 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors 2166 * @neigh1: the first neighbor object of the comparison 2167 * @if_outgoing1: outgoing interface for the first neighbor 2168 * @neigh2: the second neighbor object of the comparison 2169 * @if_outgoing2: outgoing interface for the second neighbor 2170 * @diff: pointer to integer receiving the calculated difference 2171 * 2172 * The content of *@diff is only valid when this function returns true. 2173 * It is less, equal to or greater than 0 if the metric via neigh1 is lower, 2174 * the same as or higher than the metric via neigh2 2175 * 2176 * Return: true when the difference could be calculated, false otherwise 2177 */ 2178 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1, 2179 struct batadv_hard_iface *if_outgoing1, 2180 struct batadv_neigh_node *neigh2, 2181 struct batadv_hard_iface *if_outgoing2, 2182 int *diff) 2183 { 2184 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; 2185 u8 tq1, tq2; 2186 bool ret = true; 2187 2188 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); 2189 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); 2190 2191 if (!neigh1_ifinfo || !neigh2_ifinfo) { 2192 ret = false; 2193 goto out; 2194 } 2195 2196 tq1 = neigh1_ifinfo->bat_iv.tq_avg; 2197 tq2 = neigh2_ifinfo->bat_iv.tq_avg; 2198 *diff = (int)tq1 - (int)tq2; 2199 2200 out: 2201 if (neigh1_ifinfo) 2202 batadv_neigh_ifinfo_put(neigh1_ifinfo); 2203 if (neigh2_ifinfo) 2204 batadv_neigh_ifinfo_put(neigh2_ifinfo); 2205 2206 return ret; 2207 } 2208 2209 /** 2210 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message 2211 * @msg: Netlink message to dump into 2212 * @portid: Port making netlink request 2213 * @seq: Sequence number of netlink message 2214 * @hardif_neigh: Neighbour to be dumped 2215 * 2216 * Return: Error code, or 0 on success 2217 */ 2218 static int 2219 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq, 2220 struct batadv_hardif_neigh_node *hardif_neigh) 2221 { 2222 void *hdr; 2223 unsigned int last_seen_msecs; 2224 2225 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen); 2226 2227 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, 2228 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS); 2229 if (!hdr) 2230 return -ENOBUFS; 2231 2232 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN, 2233 hardif_neigh->addr) || 2234 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, 2235 hardif_neigh->if_incoming->net_dev->ifindex) || 2236 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, 2237 last_seen_msecs)) 2238 goto nla_put_failure; 2239 2240 genlmsg_end(msg, hdr); 2241 return 0; 2242 2243 nla_put_failure: 2244 genlmsg_cancel(msg, hdr); 2245 return -EMSGSIZE; 2246 } 2247 2248 /** 2249 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface 2250 * into a message 2251 * @msg: Netlink message to dump into 2252 * @portid: Port making netlink request 2253 * @seq: Sequence number of netlink message 2254 * @bat_priv: The bat priv with all the soft interface information 2255 * @hard_iface: Hard interface to dump the neighbours for 2256 * @idx_s: Number of entries to skip 2257 * 2258 * This function assumes the caller holds rcu_read_lock(). 2259 * 2260 * Return: Error code, or 0 on success 2261 */ 2262 static int 2263 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq, 2264 struct batadv_priv *bat_priv, 2265 struct batadv_hard_iface *hard_iface, 2266 int *idx_s) 2267 { 2268 struct batadv_hardif_neigh_node *hardif_neigh; 2269 int idx = 0; 2270 2271 hlist_for_each_entry_rcu(hardif_neigh, 2272 &hard_iface->neigh_list, list) { 2273 if (idx++ < *idx_s) 2274 continue; 2275 2276 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq, 2277 hardif_neigh)) { 2278 *idx_s = idx - 1; 2279 return -EMSGSIZE; 2280 } 2281 } 2282 2283 *idx_s = 0; 2284 return 0; 2285 } 2286 2287 /** 2288 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message 2289 * @msg: Netlink message to dump into 2290 * @cb: Control block containing additional options 2291 * @bat_priv: The bat priv with all the soft interface information 2292 * @single_hardif: Limit dump to this hard interface 2293 */ 2294 static void 2295 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb, 2296 struct batadv_priv *bat_priv, 2297 struct batadv_hard_iface *single_hardif) 2298 { 2299 struct batadv_hard_iface *hard_iface; 2300 int i_hardif = 0; 2301 int i_hardif_s = cb->args[0]; 2302 int idx = cb->args[1]; 2303 int portid = NETLINK_CB(cb->skb).portid; 2304 2305 rcu_read_lock(); 2306 if (single_hardif) { 2307 if (i_hardif_s == 0) { 2308 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid, 2309 cb->nlh->nlmsg_seq, 2310 bat_priv, 2311 single_hardif, 2312 &idx) == 0) 2313 i_hardif++; 2314 } 2315 } else { 2316 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, 2317 list) { 2318 if (hard_iface->soft_iface != bat_priv->soft_iface) 2319 continue; 2320 2321 if (i_hardif++ < i_hardif_s) 2322 continue; 2323 2324 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid, 2325 cb->nlh->nlmsg_seq, 2326 bat_priv, 2327 hard_iface, &idx)) { 2328 i_hardif--; 2329 break; 2330 } 2331 } 2332 } 2333 rcu_read_unlock(); 2334 2335 cb->args[0] = i_hardif; 2336 cb->args[1] = idx; 2337 } 2338 2339 /** 2340 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors 2341 * @neigh1: the first neighbor object of the comparison 2342 * @if_outgoing1: outgoing interface for the first neighbor 2343 * @neigh2: the second neighbor object of the comparison 2344 * @if_outgoing2: outgoing interface for the second neighbor 2345 * 2346 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is 2347 * lower, the same as or higher than the metric via neigh2 2348 */ 2349 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, 2350 struct batadv_hard_iface *if_outgoing1, 2351 struct batadv_neigh_node *neigh2, 2352 struct batadv_hard_iface *if_outgoing2) 2353 { 2354 bool ret; 2355 int diff; 2356 2357 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, 2358 if_outgoing2, &diff); 2359 if (!ret) 2360 return 0; 2361 2362 return diff; 2363 } 2364 2365 /** 2366 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better 2367 * than neigh2 from the metric prospective 2368 * @neigh1: the first neighbor object of the comparison 2369 * @if_outgoing1: outgoing interface for the first neighbor 2370 * @neigh2: the second neighbor object of the comparison 2371 * @if_outgoing2: outgoing interface for the second neighbor 2372 * 2373 * Return: true if the metric via neigh1 is equally good or better than 2374 * the metric via neigh2, false otherwise. 2375 */ 2376 static bool 2377 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1, 2378 struct batadv_hard_iface *if_outgoing1, 2379 struct batadv_neigh_node *neigh2, 2380 struct batadv_hard_iface *if_outgoing2) 2381 { 2382 bool ret; 2383 int diff; 2384 2385 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, 2386 if_outgoing2, &diff); 2387 if (!ret) 2388 return false; 2389 2390 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD; 2391 return ret; 2392 } 2393 2394 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface) 2395 { 2396 /* begin scheduling originator messages on that interface */ 2397 batadv_iv_ogm_schedule(hard_iface); 2398 } 2399 2400 /** 2401 * batadv_iv_init_sel_class() - initialize GW selection class 2402 * @bat_priv: the bat priv with all the soft interface information 2403 */ 2404 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv) 2405 { 2406 /* set default TQ difference threshold to 20 */ 2407 atomic_set(&bat_priv->gw.sel_class, 20); 2408 } 2409 2410 static struct batadv_gw_node * 2411 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv) 2412 { 2413 struct batadv_neigh_node *router; 2414 struct batadv_neigh_ifinfo *router_ifinfo; 2415 struct batadv_gw_node *gw_node, *curr_gw = NULL; 2416 u64 max_gw_factor = 0; 2417 u64 tmp_gw_factor = 0; 2418 u8 max_tq = 0; 2419 u8 tq_avg; 2420 struct batadv_orig_node *orig_node; 2421 2422 rcu_read_lock(); 2423 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) { 2424 orig_node = gw_node->orig_node; 2425 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 2426 if (!router) 2427 continue; 2428 2429 router_ifinfo = batadv_neigh_ifinfo_get(router, 2430 BATADV_IF_DEFAULT); 2431 if (!router_ifinfo) 2432 goto next; 2433 2434 if (!kref_get_unless_zero(&gw_node->refcount)) 2435 goto next; 2436 2437 tq_avg = router_ifinfo->bat_iv.tq_avg; 2438 2439 switch (atomic_read(&bat_priv->gw.sel_class)) { 2440 case 1: /* fast connection */ 2441 tmp_gw_factor = tq_avg * tq_avg; 2442 tmp_gw_factor *= gw_node->bandwidth_down; 2443 tmp_gw_factor *= 100 * 100; 2444 tmp_gw_factor >>= 18; 2445 2446 if (tmp_gw_factor > max_gw_factor || 2447 (tmp_gw_factor == max_gw_factor && 2448 tq_avg > max_tq)) { 2449 if (curr_gw) 2450 batadv_gw_node_put(curr_gw); 2451 curr_gw = gw_node; 2452 kref_get(&curr_gw->refcount); 2453 } 2454 break; 2455 2456 default: /* 2: stable connection (use best statistic) 2457 * 3: fast-switch (use best statistic but change as 2458 * soon as a better gateway appears) 2459 * XX: late-switch (use best statistic but change as 2460 * soon as a better gateway appears which has 2461 * $routing_class more tq points) 2462 */ 2463 if (tq_avg > max_tq) { 2464 if (curr_gw) 2465 batadv_gw_node_put(curr_gw); 2466 curr_gw = gw_node; 2467 kref_get(&curr_gw->refcount); 2468 } 2469 break; 2470 } 2471 2472 if (tq_avg > max_tq) 2473 max_tq = tq_avg; 2474 2475 if (tmp_gw_factor > max_gw_factor) 2476 max_gw_factor = tmp_gw_factor; 2477 2478 batadv_gw_node_put(gw_node); 2479 2480 next: 2481 batadv_neigh_node_put(router); 2482 if (router_ifinfo) 2483 batadv_neigh_ifinfo_put(router_ifinfo); 2484 } 2485 rcu_read_unlock(); 2486 2487 return curr_gw; 2488 } 2489 2490 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv, 2491 struct batadv_orig_node *curr_gw_orig, 2492 struct batadv_orig_node *orig_node) 2493 { 2494 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL; 2495 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL; 2496 struct batadv_neigh_node *router_gw = NULL; 2497 struct batadv_neigh_node *router_orig = NULL; 2498 u8 gw_tq_avg, orig_tq_avg; 2499 bool ret = false; 2500 2501 /* dynamic re-election is performed only on fast or late switch */ 2502 if (atomic_read(&bat_priv->gw.sel_class) <= 2) 2503 return false; 2504 2505 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT); 2506 if (!router_gw) { 2507 ret = true; 2508 goto out; 2509 } 2510 2511 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw, 2512 BATADV_IF_DEFAULT); 2513 if (!router_gw_ifinfo) { 2514 ret = true; 2515 goto out; 2516 } 2517 2518 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); 2519 if (!router_orig) 2520 goto out; 2521 2522 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig, 2523 BATADV_IF_DEFAULT); 2524 if (!router_orig_ifinfo) 2525 goto out; 2526 2527 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg; 2528 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg; 2529 2530 /* the TQ value has to be better */ 2531 if (orig_tq_avg < gw_tq_avg) 2532 goto out; 2533 2534 /* if the routing class is greater than 3 the value tells us how much 2535 * greater the TQ value of the new gateway must be 2536 */ 2537 if ((atomic_read(&bat_priv->gw.sel_class) > 3) && 2538 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class))) 2539 goto out; 2540 2541 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 2542 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", 2543 gw_tq_avg, orig_tq_avg); 2544 2545 ret = true; 2546 out: 2547 if (router_gw_ifinfo) 2548 batadv_neigh_ifinfo_put(router_gw_ifinfo); 2549 if (router_orig_ifinfo) 2550 batadv_neigh_ifinfo_put(router_orig_ifinfo); 2551 if (router_gw) 2552 batadv_neigh_node_put(router_gw); 2553 if (router_orig) 2554 batadv_neigh_node_put(router_orig); 2555 2556 return ret; 2557 } 2558 2559 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 2560 /* fails if orig_node has no router */ 2561 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv, 2562 struct seq_file *seq, 2563 const struct batadv_gw_node *gw_node) 2564 { 2565 struct batadv_gw_node *curr_gw; 2566 struct batadv_neigh_node *router; 2567 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 2568 int ret = -1; 2569 2570 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); 2571 if (!router) 2572 goto out; 2573 2574 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 2575 if (!router_ifinfo) 2576 goto out; 2577 2578 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2579 2580 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 2581 (curr_gw == gw_node ? "=>" : " "), 2582 gw_node->orig_node->orig, 2583 router_ifinfo->bat_iv.tq_avg, router->addr, 2584 router->if_incoming->net_dev->name, 2585 gw_node->bandwidth_down / 10, 2586 gw_node->bandwidth_down % 10, 2587 gw_node->bandwidth_up / 10, 2588 gw_node->bandwidth_up % 10); 2589 ret = seq_has_overflowed(seq) ? -1 : 0; 2590 2591 if (curr_gw) 2592 batadv_gw_node_put(curr_gw); 2593 out: 2594 if (router_ifinfo) 2595 batadv_neigh_ifinfo_put(router_ifinfo); 2596 if (router) 2597 batadv_neigh_node_put(router); 2598 return ret; 2599 } 2600 2601 static void batadv_iv_gw_print(struct batadv_priv *bat_priv, 2602 struct seq_file *seq) 2603 { 2604 struct batadv_gw_node *gw_node; 2605 int gw_count = 0; 2606 2607 seq_puts(seq, 2608 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n"); 2609 2610 rcu_read_lock(); 2611 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) { 2612 /* fails if orig_node has no router */ 2613 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0) 2614 continue; 2615 2616 gw_count++; 2617 } 2618 rcu_read_unlock(); 2619 2620 if (gw_count == 0) 2621 seq_puts(seq, "No gateways in range ...\n"); 2622 } 2623 #endif 2624 2625 /** 2626 * batadv_iv_gw_dump_entry() - Dump a gateway into a message 2627 * @msg: Netlink message to dump into 2628 * @portid: Port making netlink request 2629 * @cb: Control block containing additional options 2630 * @bat_priv: The bat priv with all the soft interface information 2631 * @gw_node: Gateway to be dumped 2632 * 2633 * Return: Error code, or 0 on success 2634 */ 2635 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, 2636 struct netlink_callback *cb, 2637 struct batadv_priv *bat_priv, 2638 struct batadv_gw_node *gw_node) 2639 { 2640 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 2641 struct batadv_neigh_node *router; 2642 struct batadv_gw_node *curr_gw = NULL; 2643 int ret = 0; 2644 void *hdr; 2645 2646 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); 2647 if (!router) 2648 goto out; 2649 2650 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); 2651 if (!router_ifinfo) 2652 goto out; 2653 2654 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 2655 2656 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq, 2657 &batadv_netlink_family, NLM_F_MULTI, 2658 BATADV_CMD_GET_GATEWAYS); 2659 if (!hdr) { 2660 ret = -ENOBUFS; 2661 goto out; 2662 } 2663 2664 genl_dump_check_consistent(cb, hdr); 2665 2666 ret = -EMSGSIZE; 2667 2668 if (curr_gw == gw_node) 2669 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) { 2670 genlmsg_cancel(msg, hdr); 2671 goto out; 2672 } 2673 2674 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, 2675 gw_node->orig_node->orig) || 2676 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) || 2677 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, 2678 router->addr) || 2679 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME, 2680 router->if_incoming->net_dev->name) || 2681 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN, 2682 gw_node->bandwidth_down) || 2683 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, 2684 gw_node->bandwidth_up)) { 2685 genlmsg_cancel(msg, hdr); 2686 goto out; 2687 } 2688 2689 genlmsg_end(msg, hdr); 2690 ret = 0; 2691 2692 out: 2693 if (curr_gw) 2694 batadv_gw_node_put(curr_gw); 2695 if (router_ifinfo) 2696 batadv_neigh_ifinfo_put(router_ifinfo); 2697 if (router) 2698 batadv_neigh_node_put(router); 2699 return ret; 2700 } 2701 2702 /** 2703 * batadv_iv_gw_dump() - Dump gateways into a message 2704 * @msg: Netlink message to dump into 2705 * @cb: Control block containing additional options 2706 * @bat_priv: The bat priv with all the soft interface information 2707 */ 2708 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb, 2709 struct batadv_priv *bat_priv) 2710 { 2711 int portid = NETLINK_CB(cb->skb).portid; 2712 struct batadv_gw_node *gw_node; 2713 int idx_skip = cb->args[0]; 2714 int idx = 0; 2715 2716 spin_lock_bh(&bat_priv->gw.list_lock); 2717 cb->seq = bat_priv->gw.generation << 1 | 1; 2718 2719 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) { 2720 if (idx++ < idx_skip) 2721 continue; 2722 2723 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv, 2724 gw_node)) { 2725 idx_skip = idx - 1; 2726 goto unlock; 2727 } 2728 } 2729 2730 idx_skip = idx; 2731 unlock: 2732 spin_unlock_bh(&bat_priv->gw.list_lock); 2733 2734 cb->args[0] = idx_skip; 2735 } 2736 2737 static struct batadv_algo_ops batadv_batman_iv __read_mostly = { 2738 .name = "BATMAN_IV", 2739 .iface = { 2740 .enable = batadv_iv_ogm_iface_enable, 2741 .enabled = batadv_iv_iface_enabled, 2742 .disable = batadv_iv_ogm_iface_disable, 2743 .update_mac = batadv_iv_ogm_iface_update_mac, 2744 .primary_set = batadv_iv_ogm_primary_iface_set, 2745 }, 2746 .neigh = { 2747 .cmp = batadv_iv_ogm_neigh_cmp, 2748 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob, 2749 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 2750 .print = batadv_iv_neigh_print, 2751 #endif 2752 .dump = batadv_iv_ogm_neigh_dump, 2753 }, 2754 .orig = { 2755 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 2756 .print = batadv_iv_ogm_orig_print, 2757 #endif 2758 .dump = batadv_iv_ogm_orig_dump, 2759 }, 2760 .gw = { 2761 .init_sel_class = batadv_iv_init_sel_class, 2762 .get_best_gw_node = batadv_iv_gw_get_best_gw_node, 2763 .is_eligible = batadv_iv_gw_is_eligible, 2764 #ifdef CONFIG_BATMAN_ADV_DEBUGFS 2765 .print = batadv_iv_gw_print, 2766 #endif 2767 .dump = batadv_iv_gw_dump, 2768 }, 2769 }; 2770 2771 /** 2772 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function 2773 * 2774 * Return: 0 on success or negative error number in case of failure 2775 */ 2776 int __init batadv_iv_init(void) 2777 { 2778 int ret; 2779 2780 /* batman originator packet */ 2781 ret = batadv_recv_handler_register(BATADV_IV_OGM, 2782 batadv_iv_ogm_receive); 2783 if (ret < 0) 2784 goto out; 2785 2786 ret = batadv_algo_register(&batadv_batman_iv); 2787 if (ret < 0) 2788 goto handler_unregister; 2789 2790 goto out; 2791 2792 handler_unregister: 2793 batadv_recv_handler_unregister(BATADV_IV_OGM); 2794 out: 2795 return ret; 2796 } 2797