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