1 /* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors: 2 * 3 * Marek Lindner, Simon Wunderlich 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of version 2 of the GNU General Public 7 * License as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "main.h" 19 #include "translation-table.h" 20 #include "originator.h" 21 #include "routing.h" 22 #include "gateway_common.h" 23 #include "gateway_client.h" 24 #include "hard-interface.h" 25 #include "send.h" 26 #include "bat_algo.h" 27 #include "network-coding.h" 28 29 30 /** 31 * batadv_dup_status - duplicate status 32 * @BATADV_NO_DUP: the packet is a duplicate 33 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the 34 * neighbor) 35 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor 36 * @BATADV_PROTECTED: originator is currently protected (after reboot) 37 */ 38 enum batadv_dup_status { 39 BATADV_NO_DUP = 0, 40 BATADV_ORIG_DUP, 41 BATADV_NEIGH_DUP, 42 BATADV_PROTECTED, 43 }; 44 45 /** 46 * batadv_ring_buffer_set - update the ring buffer with the given value 47 * @lq_recv: pointer to the ring buffer 48 * @lq_index: index to store the value at 49 * @value: value to store in the ring buffer 50 */ 51 static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index, 52 uint8_t value) 53 { 54 lq_recv[*lq_index] = value; 55 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE; 56 } 57 58 /** 59 * batadv_ring_buffer_set - compute the average of all non-zero values stored 60 * in the given ring buffer 61 * @lq_recv: pointer to the ring buffer 62 * 63 * Returns computed average value. 64 */ 65 static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) 66 { 67 const uint8_t *ptr; 68 uint16_t count = 0, i = 0, sum = 0; 69 70 ptr = lq_recv; 71 72 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) { 73 if (*ptr != 0) { 74 count++; 75 sum += *ptr; 76 } 77 78 i++; 79 ptr++; 80 } 81 82 if (count == 0) 83 return 0; 84 85 return (uint8_t)(sum / count); 86 } 87 88 /** 89 * batadv_iv_ogm_orig_free - free the private resources allocated for this 90 * orig_node 91 * @orig_node: the orig_node for which the resources have to be free'd 92 */ 93 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node) 94 { 95 kfree(orig_node->bat_iv.bcast_own); 96 kfree(orig_node->bat_iv.bcast_own_sum); 97 } 98 99 /** 100 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to 101 * include the new hard-interface 102 * @orig_node: the orig_node that has to be changed 103 * @max_if_num: the current amount of interfaces 104 * 105 * Returns 0 on success, a negative error code otherwise. 106 */ 107 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node, 108 int max_if_num) 109 { 110 void *data_ptr; 111 size_t data_size, old_size; 112 int ret = -ENOMEM; 113 114 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 115 116 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; 117 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; 118 data_ptr = kmalloc(data_size, GFP_ATOMIC); 119 if (!data_ptr) 120 goto unlock; 121 122 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size); 123 kfree(orig_node->bat_iv.bcast_own); 124 orig_node->bat_iv.bcast_own = data_ptr; 125 126 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); 127 if (!data_ptr) { 128 kfree(orig_node->bat_iv.bcast_own); 129 goto unlock; 130 } 131 132 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, 133 (max_if_num - 1) * sizeof(uint8_t)); 134 kfree(orig_node->bat_iv.bcast_own_sum); 135 orig_node->bat_iv.bcast_own_sum = data_ptr; 136 137 ret = 0; 138 139 unlock: 140 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 141 142 return ret; 143 } 144 145 /** 146 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to 147 * exclude the removed interface 148 * @orig_node: the orig_node that has to be changed 149 * @max_if_num: the current amount of interfaces 150 * @del_if_num: the index of the interface being removed 151 * 152 * Returns 0 on success, a negative error code otherwise. 153 */ 154 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node, 155 int max_if_num, int del_if_num) 156 { 157 int chunk_size, ret = -ENOMEM, if_offset; 158 void *data_ptr = NULL; 159 160 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 161 162 /* last interface was removed */ 163 if (max_if_num == 0) 164 goto free_bcast_own; 165 166 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; 167 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); 168 if (!data_ptr) 169 goto unlock; 170 171 /* copy first part */ 172 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size); 173 174 /* copy second part */ 175 memcpy((char *)data_ptr + del_if_num * chunk_size, 176 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size), 177 (max_if_num - del_if_num) * chunk_size); 178 179 free_bcast_own: 180 kfree(orig_node->bat_iv.bcast_own); 181 orig_node->bat_iv.bcast_own = data_ptr; 182 183 if (max_if_num == 0) 184 goto free_own_sum; 185 186 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); 187 if (!data_ptr) { 188 kfree(orig_node->bat_iv.bcast_own); 189 goto unlock; 190 } 191 192 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, 193 del_if_num * sizeof(uint8_t)); 194 195 if_offset = (del_if_num + 1) * sizeof(uint8_t); 196 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), 197 orig_node->bat_iv.bcast_own_sum + if_offset, 198 (max_if_num - del_if_num) * sizeof(uint8_t)); 199 200 free_own_sum: 201 kfree(orig_node->bat_iv.bcast_own_sum); 202 orig_node->bat_iv.bcast_own_sum = data_ptr; 203 204 ret = 0; 205 unlock: 206 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 207 208 return ret; 209 } 210 211 /** 212 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator 213 * @bat_priv: the bat priv with all the soft interface information 214 * @addr: mac address of the originator 215 * 216 * Returns the originator object corresponding to the passed mac address or NULL 217 * on failure. 218 * If the object does not exists it is created an initialised. 219 */ 220 static struct batadv_orig_node * 221 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr) 222 { 223 struct batadv_orig_node *orig_node; 224 int size, hash_added; 225 226 orig_node = batadv_orig_hash_find(bat_priv, addr); 227 if (orig_node) 228 return orig_node; 229 230 orig_node = batadv_orig_node_new(bat_priv, addr); 231 if (!orig_node) 232 return NULL; 233 234 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock); 235 236 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; 237 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC); 238 if (!orig_node->bat_iv.bcast_own) 239 goto free_orig_node; 240 241 size = bat_priv->num_ifaces * sizeof(uint8_t); 242 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC); 243 if (!orig_node->bat_iv.bcast_own_sum) 244 goto free_bcast_own; 245 246 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, 247 batadv_choose_orig, orig_node, 248 &orig_node->hash_entry); 249 if (hash_added != 0) 250 goto free_bcast_own; 251 252 return orig_node; 253 254 free_bcast_own: 255 kfree(orig_node->bat_iv.bcast_own); 256 free_orig_node: 257 batadv_orig_node_free_ref(orig_node); 258 259 return NULL; 260 } 261 262 static struct batadv_neigh_node * 263 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface, 264 const uint8_t *neigh_addr, 265 struct batadv_orig_node *orig_node, 266 struct batadv_orig_node *orig_neigh) 267 { 268 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 269 struct batadv_neigh_node *neigh_node; 270 271 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node); 272 if (!neigh_node) 273 goto out; 274 275 if (!atomic_inc_not_zero(&hard_iface->refcount)) { 276 kfree(neigh_node); 277 neigh_node = NULL; 278 goto out; 279 } 280 281 neigh_node->orig_node = orig_neigh; 282 neigh_node->if_incoming = hard_iface; 283 284 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 285 "Creating new neighbor %pM for orig_node %pM on interface %s\n", 286 neigh_addr, orig_node->orig, hard_iface->net_dev->name); 287 288 spin_lock_bh(&orig_node->neigh_list_lock); 289 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); 290 spin_unlock_bh(&orig_node->neigh_list_lock); 291 292 out: 293 return neigh_node; 294 } 295 296 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) 297 { 298 struct batadv_ogm_packet *batadv_ogm_packet; 299 unsigned char *ogm_buff; 300 uint32_t random_seqno; 301 int res = -ENOMEM; 302 303 /* randomize initial seqno to avoid collision */ 304 get_random_bytes(&random_seqno, sizeof(random_seqno)); 305 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno); 306 307 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN; 308 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC); 309 if (!ogm_buff) 310 goto out; 311 312 hard_iface->bat_iv.ogm_buff = ogm_buff; 313 314 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; 315 batadv_ogm_packet->packet_type = BATADV_IV_OGM; 316 batadv_ogm_packet->version = BATADV_COMPAT_VERSION; 317 batadv_ogm_packet->ttl = 2; 318 batadv_ogm_packet->flags = BATADV_NO_FLAGS; 319 batadv_ogm_packet->reserved = 0; 320 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE; 321 322 res = 0; 323 324 out: 325 return res; 326 } 327 328 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface) 329 { 330 kfree(hard_iface->bat_iv.ogm_buff); 331 hard_iface->bat_iv.ogm_buff = NULL; 332 } 333 334 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface) 335 { 336 struct batadv_ogm_packet *batadv_ogm_packet; 337 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; 338 339 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; 340 memcpy(batadv_ogm_packet->orig, 341 hard_iface->net_dev->dev_addr, ETH_ALEN); 342 memcpy(batadv_ogm_packet->prev_sender, 343 hard_iface->net_dev->dev_addr, ETH_ALEN); 344 } 345 346 static void 347 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface) 348 { 349 struct batadv_ogm_packet *batadv_ogm_packet; 350 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; 351 352 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; 353 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP; 354 batadv_ogm_packet->ttl = BATADV_TTL; 355 } 356 357 /* when do we schedule our own ogm to be sent */ 358 static unsigned long 359 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv) 360 { 361 unsigned int msecs; 362 363 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER; 364 msecs += prandom_u32() % (2 * BATADV_JITTER); 365 366 return jiffies + msecs_to_jiffies(msecs); 367 } 368 369 /* when do we schedule a ogm packet to be sent */ 370 static unsigned long batadv_iv_ogm_fwd_send_time(void) 371 { 372 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2)); 373 } 374 375 /* apply hop penalty for a normal link */ 376 static uint8_t batadv_hop_penalty(uint8_t tq, 377 const struct batadv_priv *bat_priv) 378 { 379 int hop_penalty = atomic_read(&bat_priv->hop_penalty); 380 int new_tq; 381 382 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty); 383 new_tq /= BATADV_TQ_MAX_VALUE; 384 385 return new_tq; 386 } 387 388 /* is there another aggregated packet here? */ 389 static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, 390 __be16 tvlv_len) 391 { 392 int next_buff_pos = 0; 393 394 next_buff_pos += buff_pos + BATADV_OGM_HLEN; 395 next_buff_pos += ntohs(tvlv_len); 396 397 return (next_buff_pos <= packet_len) && 398 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES); 399 } 400 401 /* send a batman ogm to a given interface */ 402 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet, 403 struct batadv_hard_iface *hard_iface) 404 { 405 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 406 char *fwd_str; 407 uint8_t packet_num; 408 int16_t buff_pos; 409 struct batadv_ogm_packet *batadv_ogm_packet; 410 struct sk_buff *skb; 411 uint8_t *packet_pos; 412 413 if (hard_iface->if_status != BATADV_IF_ACTIVE) 414 return; 415 416 packet_num = 0; 417 buff_pos = 0; 418 packet_pos = forw_packet->skb->data; 419 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; 420 421 /* adjust all flags and log packets */ 422 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, 423 batadv_ogm_packet->tvlv_len)) { 424 /* we might have aggregated direct link packets with an 425 * ordinary base packet 426 */ 427 if (forw_packet->direct_link_flags & BIT(packet_num) && 428 forw_packet->if_incoming == hard_iface) 429 batadv_ogm_packet->flags |= BATADV_DIRECTLINK; 430 else 431 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; 432 433 if (packet_num > 0 || !forw_packet->own) 434 fwd_str = "Forwarding"; 435 else 436 fwd_str = "Sending own"; 437 438 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 439 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n", 440 fwd_str, (packet_num > 0 ? "aggregated " : ""), 441 batadv_ogm_packet->orig, 442 ntohl(batadv_ogm_packet->seqno), 443 batadv_ogm_packet->tq, batadv_ogm_packet->ttl, 444 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 445 "on" : "off"), 446 hard_iface->net_dev->name, 447 hard_iface->net_dev->dev_addr); 448 449 buff_pos += BATADV_OGM_HLEN; 450 buff_pos += ntohs(batadv_ogm_packet->tvlv_len); 451 packet_num++; 452 packet_pos = forw_packet->skb->data + buff_pos; 453 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; 454 } 455 456 /* create clone because function is called more than once */ 457 skb = skb_clone(forw_packet->skb, GFP_ATOMIC); 458 if (skb) { 459 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX); 460 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES, 461 skb->len + ETH_HLEN); 462 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr); 463 } 464 } 465 466 /* send a batman ogm packet */ 467 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet) 468 { 469 struct net_device *soft_iface; 470 struct batadv_priv *bat_priv; 471 struct batadv_hard_iface *primary_if = NULL; 472 473 if (!forw_packet->if_incoming) { 474 pr_err("Error - can't forward packet: incoming iface not specified\n"); 475 goto out; 476 } 477 478 soft_iface = forw_packet->if_incoming->soft_iface; 479 bat_priv = netdev_priv(soft_iface); 480 481 if (WARN_ON(!forw_packet->if_outgoing)) 482 goto out; 483 484 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface)) 485 goto out; 486 487 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE) 488 goto out; 489 490 primary_if = batadv_primary_if_get_selected(bat_priv); 491 if (!primary_if) 492 goto out; 493 494 /* only for one specific outgoing interface */ 495 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing); 496 497 out: 498 if (primary_if) 499 batadv_hardif_free_ref(primary_if); 500 } 501 502 /** 503 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an 504 * existing forward packet 505 * @new_bat_ogm_packet: OGM packet to be aggregated 506 * @bat_priv: the bat priv with all the soft interface information 507 * @packet_len: (total) length of the OGM 508 * @send_time: timestamp (jiffies) when the packet is to be sent 509 * @direktlink: true if this is a direct link packet 510 * @if_incoming: interface where the packet was received 511 * @if_outgoing: interface for which the retransmission should be considered 512 * @forw_packet: the forwarded packet which should be checked 513 * 514 * Returns true if new_packet can be aggregated with forw_packet 515 */ 516 static bool 517 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet, 518 struct batadv_priv *bat_priv, 519 int packet_len, unsigned long send_time, 520 bool directlink, 521 const struct batadv_hard_iface *if_incoming, 522 const struct batadv_hard_iface *if_outgoing, 523 const struct batadv_forw_packet *forw_packet) 524 { 525 struct batadv_ogm_packet *batadv_ogm_packet; 526 int aggregated_bytes = forw_packet->packet_len + packet_len; 527 struct batadv_hard_iface *primary_if = NULL; 528 bool res = false; 529 unsigned long aggregation_end_time; 530 531 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data; 532 aggregation_end_time = send_time; 533 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); 534 535 /* we can aggregate the current packet to this aggregated packet 536 * if: 537 * 538 * - the send time is within our MAX_AGGREGATION_MS time 539 * - the resulting packet wont be bigger than 540 * MAX_AGGREGATION_BYTES 541 */ 542 if (time_before(send_time, forw_packet->send_time) && 543 time_after_eq(aggregation_end_time, forw_packet->send_time) && 544 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) { 545 /* check aggregation compatibility 546 * -> direct link packets are broadcasted on 547 * their interface only 548 * -> aggregate packet if the current packet is 549 * a "global" packet as well as the base 550 * packet 551 */ 552 primary_if = batadv_primary_if_get_selected(bat_priv); 553 if (!primary_if) 554 goto out; 555 556 /* packet is not leaving on the same interface. */ 557 if (forw_packet->if_outgoing != if_outgoing) 558 goto out; 559 560 /* packets without direct link flag and high TTL 561 * are flooded through the net 562 */ 563 if ((!directlink) && 564 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) && 565 (batadv_ogm_packet->ttl != 1) && 566 567 /* own packets originating non-primary 568 * interfaces leave only that interface 569 */ 570 ((!forw_packet->own) || 571 (forw_packet->if_incoming == primary_if))) { 572 res = true; 573 goto out; 574 } 575 576 /* if the incoming packet is sent via this one 577 * interface only - we still can aggregate 578 */ 579 if ((directlink) && 580 (new_bat_ogm_packet->ttl == 1) && 581 (forw_packet->if_incoming == if_incoming) && 582 583 /* packets from direct neighbors or 584 * own secondary interface packets 585 * (= secondary interface packets in general) 586 */ 587 (batadv_ogm_packet->flags & BATADV_DIRECTLINK || 588 (forw_packet->own && 589 forw_packet->if_incoming != primary_if))) { 590 res = true; 591 goto out; 592 } 593 } 594 595 out: 596 if (primary_if) 597 batadv_hardif_free_ref(primary_if); 598 return res; 599 } 600 601 /** 602 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this 603 * packet to it. 604 * @packet_buff: pointer to the OGM 605 * @packet_len: (total) length of the OGM 606 * @send_time: timestamp (jiffies) when the packet is to be sent 607 * @direct_link: whether this OGM has direct link status 608 * @if_incoming: interface where the packet was received 609 * @if_outgoing: interface for which the retransmission should be considered 610 * @own_packet: true if it is a self-generated ogm 611 */ 612 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, 613 int packet_len, unsigned long send_time, 614 bool direct_link, 615 struct batadv_hard_iface *if_incoming, 616 struct batadv_hard_iface *if_outgoing, 617 int own_packet) 618 { 619 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 620 struct batadv_forw_packet *forw_packet_aggr; 621 unsigned char *skb_buff; 622 unsigned int skb_size; 623 624 if (!atomic_inc_not_zero(&if_incoming->refcount)) 625 return; 626 627 if (!atomic_inc_not_zero(&if_outgoing->refcount)) 628 goto out_free_incoming; 629 630 /* own packet should always be scheduled */ 631 if (!own_packet) { 632 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) { 633 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 634 "batman packet queue full\n"); 635 goto out; 636 } 637 } 638 639 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); 640 if (!forw_packet_aggr) { 641 if (!own_packet) 642 atomic_inc(&bat_priv->batman_queue_left); 643 goto out; 644 } 645 646 if ((atomic_read(&bat_priv->aggregated_ogms)) && 647 (packet_len < BATADV_MAX_AGGREGATION_BYTES)) 648 skb_size = BATADV_MAX_AGGREGATION_BYTES; 649 else 650 skb_size = packet_len; 651 652 skb_size += ETH_HLEN; 653 654 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size); 655 if (!forw_packet_aggr->skb) { 656 if (!own_packet) 657 atomic_inc(&bat_priv->batman_queue_left); 658 kfree(forw_packet_aggr); 659 goto out; 660 } 661 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL; 662 skb_reserve(forw_packet_aggr->skb, ETH_HLEN); 663 664 skb_buff = skb_put(forw_packet_aggr->skb, packet_len); 665 forw_packet_aggr->packet_len = packet_len; 666 memcpy(skb_buff, packet_buff, packet_len); 667 668 forw_packet_aggr->own = own_packet; 669 forw_packet_aggr->if_incoming = if_incoming; 670 forw_packet_aggr->if_outgoing = if_outgoing; 671 forw_packet_aggr->num_packets = 0; 672 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS; 673 forw_packet_aggr->send_time = send_time; 674 675 /* save packet direct link flag status */ 676 if (direct_link) 677 forw_packet_aggr->direct_link_flags |= 1; 678 679 /* add new packet to packet list */ 680 spin_lock_bh(&bat_priv->forw_bat_list_lock); 681 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); 682 spin_unlock_bh(&bat_priv->forw_bat_list_lock); 683 684 /* start timer for this packet */ 685 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, 686 batadv_send_outstanding_bat_ogm_packet); 687 queue_delayed_work(batadv_event_workqueue, 688 &forw_packet_aggr->delayed_work, 689 send_time - jiffies); 690 691 return; 692 out: 693 batadv_hardif_free_ref(if_outgoing); 694 out_free_incoming: 695 batadv_hardif_free_ref(if_incoming); 696 } 697 698 /* aggregate a new packet into the existing ogm packet */ 699 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr, 700 const unsigned char *packet_buff, 701 int packet_len, bool direct_link) 702 { 703 unsigned char *skb_buff; 704 unsigned long new_direct_link_flag; 705 706 skb_buff = skb_put(forw_packet_aggr->skb, packet_len); 707 memcpy(skb_buff, packet_buff, packet_len); 708 forw_packet_aggr->packet_len += packet_len; 709 forw_packet_aggr->num_packets++; 710 711 /* save packet direct link flag status */ 712 if (direct_link) { 713 new_direct_link_flag = BIT(forw_packet_aggr->num_packets); 714 forw_packet_aggr->direct_link_flags |= new_direct_link_flag; 715 } 716 } 717 718 /** 719 * batadv_iv_ogm_queue_add - queue up an OGM for transmission 720 * @bat_priv: the bat priv with all the soft interface information 721 * @packet_buff: pointer to the OGM 722 * @packet_len: (total) length of the OGM 723 * @if_incoming: interface where the packet was received 724 * @if_outgoing: interface for which the retransmission should be considered 725 * @own_packet: true if it is a self-generated ogm 726 * @send_time: timestamp (jiffies) when the packet is to be sent 727 */ 728 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv, 729 unsigned char *packet_buff, 730 int packet_len, 731 struct batadv_hard_iface *if_incoming, 732 struct batadv_hard_iface *if_outgoing, 733 int own_packet, unsigned long send_time) 734 { 735 /* _aggr -> pointer to the packet we want to aggregate with 736 * _pos -> pointer to the position in the queue 737 */ 738 struct batadv_forw_packet *forw_packet_aggr = NULL; 739 struct batadv_forw_packet *forw_packet_pos = NULL; 740 struct batadv_ogm_packet *batadv_ogm_packet; 741 bool direct_link; 742 unsigned long max_aggregation_jiffies; 743 744 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; 745 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0; 746 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); 747 748 /* find position for the packet in the forward queue */ 749 spin_lock_bh(&bat_priv->forw_bat_list_lock); 750 /* own packets are not to be aggregated */ 751 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { 752 hlist_for_each_entry(forw_packet_pos, 753 &bat_priv->forw_bat_list, list) { 754 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet, 755 bat_priv, packet_len, 756 send_time, direct_link, 757 if_incoming, 758 if_outgoing, 759 forw_packet_pos)) { 760 forw_packet_aggr = forw_packet_pos; 761 break; 762 } 763 } 764 } 765 766 /* nothing to aggregate with - either aggregation disabled or no 767 * suitable aggregation packet found 768 */ 769 if (!forw_packet_aggr) { 770 /* the following section can run without the lock */ 771 spin_unlock_bh(&bat_priv->forw_bat_list_lock); 772 773 /* if we could not aggregate this packet with one of the others 774 * we hold it back for a while, so that it might be aggregated 775 * later on 776 */ 777 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms)) 778 send_time += max_aggregation_jiffies; 779 780 batadv_iv_ogm_aggregate_new(packet_buff, packet_len, 781 send_time, direct_link, 782 if_incoming, if_outgoing, 783 own_packet); 784 } else { 785 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff, 786 packet_len, direct_link); 787 spin_unlock_bh(&bat_priv->forw_bat_list_lock); 788 } 789 } 790 791 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node, 792 const struct ethhdr *ethhdr, 793 struct batadv_ogm_packet *batadv_ogm_packet, 794 bool is_single_hop_neigh, 795 bool is_from_best_next_hop, 796 struct batadv_hard_iface *if_incoming, 797 struct batadv_hard_iface *if_outgoing) 798 { 799 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 800 uint16_t tvlv_len; 801 802 if (batadv_ogm_packet->ttl <= 1) { 803 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); 804 return; 805 } 806 807 if (!is_from_best_next_hop) { 808 /* Mark the forwarded packet when it is not coming from our 809 * best next hop. We still need to forward the packet for our 810 * neighbor link quality detection to work in case the packet 811 * originated from a single hop neighbor. Otherwise we can 812 * simply drop the ogm. 813 */ 814 if (is_single_hop_neigh) 815 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP; 816 else 817 return; 818 } 819 820 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len); 821 822 batadv_ogm_packet->ttl--; 823 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN); 824 825 /* apply hop penalty */ 826 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq, 827 bat_priv); 828 829 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 830 "Forwarding packet: tq: %i, ttl: %i\n", 831 batadv_ogm_packet->tq, batadv_ogm_packet->ttl); 832 833 /* switch of primaries first hop flag when forwarding */ 834 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP; 835 if (is_single_hop_neigh) 836 batadv_ogm_packet->flags |= BATADV_DIRECTLINK; 837 else 838 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; 839 840 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet, 841 BATADV_OGM_HLEN + tvlv_len, 842 if_incoming, if_outgoing, 0, 843 batadv_iv_ogm_fwd_send_time()); 844 } 845 846 /** 847 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for 848 * the given interface 849 * @hard_iface: the interface for which the windows have to be shifted 850 */ 851 static void 852 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) 853 { 854 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 855 struct batadv_hashtable *hash = bat_priv->orig_hash; 856 struct hlist_head *head; 857 struct batadv_orig_node *orig_node; 858 unsigned long *word; 859 uint32_t i; 860 size_t word_index; 861 uint8_t *w; 862 int if_num; 863 864 for (i = 0; i < hash->size; i++) { 865 head = &hash->table[i]; 866 867 rcu_read_lock(); 868 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 869 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 870 word_index = hard_iface->if_num * BATADV_NUM_WORDS; 871 word = &(orig_node->bat_iv.bcast_own[word_index]); 872 873 batadv_bit_get_packet(bat_priv, word, 1, 0); 874 if_num = hard_iface->if_num; 875 w = &orig_node->bat_iv.bcast_own_sum[if_num]; 876 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); 877 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 878 } 879 rcu_read_unlock(); 880 } 881 } 882 883 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface) 884 { 885 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 886 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff; 887 struct batadv_ogm_packet *batadv_ogm_packet; 888 struct batadv_hard_iface *primary_if, *tmp_hard_iface; 889 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len; 890 uint32_t seqno; 891 uint16_t tvlv_len = 0; 892 unsigned long send_time; 893 894 primary_if = batadv_primary_if_get_selected(bat_priv); 895 896 if (hard_iface == primary_if) { 897 /* tt changes have to be committed before the tvlv data is 898 * appended as it may alter the tt tvlv container 899 */ 900 batadv_tt_local_commit_changes(bat_priv); 901 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff, 902 ogm_buff_len, 903 BATADV_OGM_HLEN); 904 } 905 906 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff); 907 batadv_ogm_packet->tvlv_len = htons(tvlv_len); 908 909 /* change sequence number to network order */ 910 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno); 911 batadv_ogm_packet->seqno = htonl(seqno); 912 atomic_inc(&hard_iface->bat_iv.ogm_seqno); 913 914 batadv_iv_ogm_slide_own_bcast_window(hard_iface); 915 916 send_time = batadv_iv_ogm_emit_send_time(bat_priv); 917 918 if (hard_iface != primary_if) { 919 /* OGMs from secondary interfaces are only scheduled on their 920 * respective interfaces. 921 */ 922 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len, 923 hard_iface, hard_iface, 1, send_time); 924 goto out; 925 } 926 927 /* OGMs from primary interfaces are scheduled on all 928 * interfaces. 929 */ 930 rcu_read_lock(); 931 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) { 932 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface) 933 continue; 934 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, 935 *ogm_buff_len, hard_iface, 936 tmp_hard_iface, 1, send_time); 937 } 938 rcu_read_unlock(); 939 940 out: 941 if (primary_if) 942 batadv_hardif_free_ref(primary_if); 943 } 944 945 /** 946 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an 947 * originator 948 * @bat_priv: the bat priv with all the soft interface information 949 * @orig_node: the orig node who originally emitted the ogm packet 950 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node 951 * @ethhdr: Ethernet header of the OGM 952 * @batadv_ogm_packet: the ogm packet 953 * @if_incoming: interface where the packet was received 954 * @if_outgoing: interface for which the retransmission should be considered 955 * @dup_status: the duplicate status of this ogm packet. 956 */ 957 static void 958 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, 959 struct batadv_orig_node *orig_node, 960 struct batadv_orig_ifinfo *orig_ifinfo, 961 const struct ethhdr *ethhdr, 962 const struct batadv_ogm_packet *batadv_ogm_packet, 963 struct batadv_hard_iface *if_incoming, 964 struct batadv_hard_iface *if_outgoing, 965 enum batadv_dup_status dup_status) 966 { 967 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL; 968 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 969 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL; 970 struct batadv_neigh_node *router = NULL; 971 struct batadv_orig_node *orig_node_tmp; 972 int if_num; 973 uint8_t sum_orig, sum_neigh; 974 uint8_t *neigh_addr; 975 uint8_t tq_avg; 976 977 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 978 "update_originator(): Searching and updating originator entry of received packet\n"); 979 980 rcu_read_lock(); 981 hlist_for_each_entry_rcu(tmp_neigh_node, 982 &orig_node->neigh_list, list) { 983 neigh_addr = tmp_neigh_node->addr; 984 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && 985 tmp_neigh_node->if_incoming == if_incoming && 986 atomic_inc_not_zero(&tmp_neigh_node->refcount)) { 987 if (WARN(neigh_node, "too many matching neigh_nodes")) 988 batadv_neigh_node_free_ref(neigh_node); 989 neigh_node = tmp_neigh_node; 990 continue; 991 } 992 993 if (dup_status != BATADV_NO_DUP) 994 continue; 995 996 /* only update the entry for this outgoing interface */ 997 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node, 998 if_outgoing); 999 if (!neigh_ifinfo) 1000 continue; 1001 1002 spin_lock_bh(&tmp_neigh_node->ifinfo_lock); 1003 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv, 1004 &neigh_ifinfo->bat_iv.tq_index, 0); 1005 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv); 1006 neigh_ifinfo->bat_iv.tq_avg = tq_avg; 1007 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock); 1008 1009 batadv_neigh_ifinfo_free_ref(neigh_ifinfo); 1010 neigh_ifinfo = NULL; 1011 } 1012 1013 if (!neigh_node) { 1014 struct batadv_orig_node *orig_tmp; 1015 1016 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source); 1017 if (!orig_tmp) 1018 goto unlock; 1019 1020 neigh_node = batadv_iv_ogm_neigh_new(if_incoming, 1021 ethhdr->h_source, 1022 orig_node, orig_tmp); 1023 1024 batadv_orig_node_free_ref(orig_tmp); 1025 if (!neigh_node) 1026 goto unlock; 1027 } else 1028 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1029 "Updating existing last-hop neighbor of originator\n"); 1030 1031 rcu_read_unlock(); 1032 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing); 1033 if (!neigh_ifinfo) 1034 goto out; 1035 1036 neigh_node->last_seen = jiffies; 1037 1038 spin_lock_bh(&neigh_node->ifinfo_lock); 1039 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv, 1040 &neigh_ifinfo->bat_iv.tq_index, 1041 batadv_ogm_packet->tq); 1042 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv); 1043 neigh_ifinfo->bat_iv.tq_avg = tq_avg; 1044 spin_unlock_bh(&neigh_node->ifinfo_lock); 1045 1046 if (dup_status == BATADV_NO_DUP) { 1047 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl; 1048 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl; 1049 } 1050 1051 /* if this neighbor already is our next hop there is nothing 1052 * to change 1053 */ 1054 router = batadv_orig_router_get(orig_node, if_outgoing); 1055 if (router == neigh_node) 1056 goto out; 1057 1058 if (router) { 1059 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); 1060 if (!router_ifinfo) 1061 goto out; 1062 1063 /* if this neighbor does not offer a better TQ we won't 1064 * consider it 1065 */ 1066 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg) 1067 goto out; 1068 } 1069 1070 /* if the TQ is the same and the link not more symmetric we 1071 * won't consider it either 1072 */ 1073 if (router_ifinfo && 1074 (neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg)) { 1075 orig_node_tmp = router->orig_node; 1076 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); 1077 if_num = router->if_incoming->if_num; 1078 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; 1079 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); 1080 1081 orig_node_tmp = neigh_node->orig_node; 1082 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); 1083 if_num = neigh_node->if_incoming->if_num; 1084 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; 1085 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); 1086 1087 if (sum_orig >= sum_neigh) 1088 goto out; 1089 } 1090 1091 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node); 1092 goto out; 1093 1094 unlock: 1095 rcu_read_unlock(); 1096 out: 1097 if (neigh_node) 1098 batadv_neigh_node_free_ref(neigh_node); 1099 if (router) 1100 batadv_neigh_node_free_ref(router); 1101 if (neigh_ifinfo) 1102 batadv_neigh_ifinfo_free_ref(neigh_ifinfo); 1103 if (router_ifinfo) 1104 batadv_neigh_ifinfo_free_ref(router_ifinfo); 1105 } 1106 1107 /** 1108 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet 1109 * @orig_node: the orig node who originally emitted the ogm packet 1110 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet 1111 * @batadv_ogm_packet: the ogm packet 1112 * @if_incoming: interface where the packet was received 1113 * @if_outgoing: interface for which the retransmission should be considered 1114 * 1115 * Returns 1 if the link can be considered bidirectional, 0 otherwise 1116 */ 1117 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node, 1118 struct batadv_orig_node *orig_neigh_node, 1119 struct batadv_ogm_packet *batadv_ogm_packet, 1120 struct batadv_hard_iface *if_incoming, 1121 struct batadv_hard_iface *if_outgoing) 1122 { 1123 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1124 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node; 1125 struct batadv_neigh_ifinfo *neigh_ifinfo; 1126 uint8_t total_count; 1127 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own; 1128 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; 1129 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0; 1130 unsigned int combined_tq; 1131 int tq_iface_penalty; 1132 1133 /* find corresponding one hop neighbor */ 1134 rcu_read_lock(); 1135 hlist_for_each_entry_rcu(tmp_neigh_node, 1136 &orig_neigh_node->neigh_list, list) { 1137 if (!batadv_compare_eth(tmp_neigh_node->addr, 1138 orig_neigh_node->orig)) 1139 continue; 1140 1141 if (tmp_neigh_node->if_incoming != if_incoming) 1142 continue; 1143 1144 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) 1145 continue; 1146 1147 neigh_node = tmp_neigh_node; 1148 break; 1149 } 1150 rcu_read_unlock(); 1151 1152 if (!neigh_node) 1153 neigh_node = batadv_iv_ogm_neigh_new(if_incoming, 1154 orig_neigh_node->orig, 1155 orig_neigh_node, 1156 orig_neigh_node); 1157 1158 if (!neigh_node) 1159 goto out; 1160 1161 /* if orig_node is direct neighbor update neigh_node last_seen */ 1162 if (orig_node == orig_neigh_node) 1163 neigh_node->last_seen = jiffies; 1164 1165 orig_node->last_seen = jiffies; 1166 1167 /* find packet count of corresponding one hop neighbor */ 1168 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1169 if_num = if_incoming->if_num; 1170 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num]; 1171 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing); 1172 if (neigh_ifinfo) { 1173 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count; 1174 batadv_neigh_ifinfo_free_ref(neigh_ifinfo); 1175 } else { 1176 neigh_rq_count = 0; 1177 } 1178 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1179 1180 /* pay attention to not get a value bigger than 100 % */ 1181 if (orig_eq_count > neigh_rq_count) 1182 total_count = neigh_rq_count; 1183 else 1184 total_count = orig_eq_count; 1185 1186 /* if we have too few packets (too less data) we set tq_own to zero 1187 * if we receive too few packets it is not considered bidirectional 1188 */ 1189 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM || 1190 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM) 1191 tq_own = 0; 1192 else 1193 /* neigh_node->real_packet_count is never zero as we 1194 * only purge old information when getting new 1195 * information 1196 */ 1197 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count; 1198 1199 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does 1200 * affect the nearly-symmetric links only a little, but 1201 * punishes asymmetric links more. This will give a value 1202 * between 0 and TQ_MAX_VALUE 1203 */ 1204 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count; 1205 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv; 1206 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE * 1207 BATADV_TQ_LOCAL_WINDOW_SIZE * 1208 BATADV_TQ_LOCAL_WINDOW_SIZE; 1209 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube; 1210 inv_asym_penalty /= neigh_rq_max_cube; 1211 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty; 1212 1213 /* penalize if the OGM is forwarded on the same interface. WiFi 1214 * interfaces and other half duplex devices suffer from throughput 1215 * drops as they can't send and receive at the same time. 1216 */ 1217 tq_iface_penalty = BATADV_TQ_MAX_VALUE; 1218 if (if_outgoing && (if_incoming == if_outgoing) && 1219 batadv_is_wifi_netdev(if_outgoing->net_dev)) 1220 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE, 1221 bat_priv); 1222 1223 combined_tq = batadv_ogm_packet->tq * 1224 tq_own * 1225 tq_asym_penalty * 1226 tq_iface_penalty; 1227 combined_tq /= BATADV_TQ_MAX_VALUE * 1228 BATADV_TQ_MAX_VALUE * 1229 BATADV_TQ_MAX_VALUE; 1230 batadv_ogm_packet->tq = combined_tq; 1231 1232 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1233 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n", 1234 orig_node->orig, orig_neigh_node->orig, total_count, 1235 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty, 1236 batadv_ogm_packet->tq, if_incoming->net_dev->name, 1237 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT"); 1238 1239 /* if link has the minimum required transmission quality 1240 * consider it bidirectional 1241 */ 1242 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT) 1243 ret = 1; 1244 1245 out: 1246 if (neigh_node) 1247 batadv_neigh_node_free_ref(neigh_node); 1248 return ret; 1249 } 1250 1251 /** 1252 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces, 1253 * adjust the sequence number and find out whether it is a duplicate 1254 * @ethhdr: ethernet header of the packet 1255 * @batadv_ogm_packet: OGM packet to be considered 1256 * @if_incoming: interface on which the OGM packet was received 1257 * @if_outgoing: interface for which the retransmission should be considered 1258 * 1259 * Returns duplicate status as enum batadv_dup_status 1260 */ 1261 static enum batadv_dup_status 1262 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, 1263 const struct batadv_ogm_packet *batadv_ogm_packet, 1264 const struct batadv_hard_iface *if_incoming, 1265 struct batadv_hard_iface *if_outgoing) 1266 { 1267 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1268 struct batadv_orig_node *orig_node; 1269 struct batadv_orig_ifinfo *orig_ifinfo = NULL; 1270 struct batadv_neigh_node *neigh_node; 1271 struct batadv_neigh_ifinfo *neigh_ifinfo; 1272 int is_dup; 1273 int32_t seq_diff; 1274 int need_update = 0; 1275 int set_mark; 1276 enum batadv_dup_status ret = BATADV_NO_DUP; 1277 uint32_t seqno = ntohl(batadv_ogm_packet->seqno); 1278 uint8_t *neigh_addr; 1279 uint8_t packet_count; 1280 unsigned long *bitmap; 1281 1282 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); 1283 if (!orig_node) 1284 return BATADV_NO_DUP; 1285 1286 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 1287 if (WARN_ON(!orig_ifinfo)) { 1288 batadv_orig_node_free_ref(orig_node); 1289 return 0; 1290 } 1291 1292 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1293 seq_diff = seqno - orig_ifinfo->last_real_seqno; 1294 1295 /* signalize caller that the packet is to be dropped. */ 1296 if (!hlist_empty(&orig_node->neigh_list) && 1297 batadv_window_protected(bat_priv, seq_diff, 1298 &orig_ifinfo->batman_seqno_reset)) { 1299 ret = BATADV_PROTECTED; 1300 goto out; 1301 } 1302 1303 rcu_read_lock(); 1304 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { 1305 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, 1306 if_outgoing); 1307 if (!neigh_ifinfo) 1308 continue; 1309 1310 neigh_addr = neigh_node->addr; 1311 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits, 1312 orig_ifinfo->last_real_seqno, 1313 seqno); 1314 1315 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && 1316 neigh_node->if_incoming == if_incoming) { 1317 set_mark = 1; 1318 if (is_dup) 1319 ret = BATADV_NEIGH_DUP; 1320 } else { 1321 set_mark = 0; 1322 if (is_dup && (ret != BATADV_NEIGH_DUP)) 1323 ret = BATADV_ORIG_DUP; 1324 } 1325 1326 /* if the window moved, set the update flag. */ 1327 bitmap = neigh_ifinfo->bat_iv.real_bits; 1328 need_update |= batadv_bit_get_packet(bat_priv, bitmap, 1329 seq_diff, set_mark); 1330 1331 packet_count = bitmap_weight(bitmap, 1332 BATADV_TQ_LOCAL_WINDOW_SIZE); 1333 neigh_ifinfo->bat_iv.real_packet_count = packet_count; 1334 batadv_neigh_ifinfo_free_ref(neigh_ifinfo); 1335 } 1336 rcu_read_unlock(); 1337 1338 if (need_update) { 1339 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1340 "%s updating last_seqno: old %u, new %u\n", 1341 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT", 1342 orig_ifinfo->last_real_seqno, seqno); 1343 orig_ifinfo->last_real_seqno = seqno; 1344 } 1345 1346 out: 1347 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); 1348 batadv_orig_node_free_ref(orig_node); 1349 if (orig_ifinfo) 1350 batadv_orig_ifinfo_free_ref(orig_ifinfo); 1351 return ret; 1352 } 1353 1354 1355 /** 1356 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if 1357 * @skb: the skb containing the OGM 1358 * @orig_node: the (cached) orig node for the originator of this OGM 1359 * @if_incoming: the interface where this packet was received 1360 * @if_outgoing: the interface for which the packet should be considered 1361 */ 1362 static void 1363 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset, 1364 struct batadv_orig_node *orig_node, 1365 struct batadv_hard_iface *if_incoming, 1366 struct batadv_hard_iface *if_outgoing) 1367 { 1368 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1369 struct batadv_neigh_node *router = NULL, *router_router = NULL; 1370 struct batadv_orig_node *orig_neigh_node; 1371 struct batadv_orig_ifinfo *orig_ifinfo; 1372 struct batadv_neigh_node *orig_neigh_router = NULL; 1373 struct batadv_neigh_ifinfo *router_ifinfo = NULL; 1374 struct batadv_ogm_packet *ogm_packet; 1375 enum batadv_dup_status dup_status; 1376 bool is_from_best_next_hop = false; 1377 bool is_single_hop_neigh = false; 1378 bool sameseq, similar_ttl; 1379 struct sk_buff *skb_priv; 1380 struct ethhdr *ethhdr; 1381 uint8_t *prev_sender; 1382 int is_bidirect; 1383 1384 /* create a private copy of the skb, as some functions change tq value 1385 * and/or flags. 1386 */ 1387 skb_priv = skb_copy(skb, GFP_ATOMIC); 1388 if (!skb_priv) 1389 return; 1390 1391 ethhdr = eth_hdr(skb_priv); 1392 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset); 1393 1394 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet, 1395 if_incoming, if_outgoing); 1396 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig)) 1397 is_single_hop_neigh = true; 1398 1399 if (dup_status == BATADV_PROTECTED) { 1400 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1401 "Drop packet: packet within seqno protection time (sender: %pM)\n", 1402 ethhdr->h_source); 1403 goto out; 1404 } 1405 1406 if (ogm_packet->tq == 0) { 1407 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1408 "Drop packet: originator packet with tq equal 0\n"); 1409 goto out; 1410 } 1411 1412 router = batadv_orig_router_get(orig_node, if_outgoing); 1413 if (router) { 1414 router_router = batadv_orig_router_get(router->orig_node, 1415 if_outgoing); 1416 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); 1417 } 1418 1419 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) && 1420 (batadv_compare_eth(router->addr, ethhdr->h_source))) 1421 is_from_best_next_hop = true; 1422 1423 prev_sender = ogm_packet->prev_sender; 1424 /* avoid temporary routing loops */ 1425 if (router && router_router && 1426 (batadv_compare_eth(router->addr, prev_sender)) && 1427 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) && 1428 (batadv_compare_eth(router->addr, router_router->addr))) { 1429 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1430 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", 1431 ethhdr->h_source); 1432 goto out; 1433 } 1434 1435 if (if_outgoing == BATADV_IF_DEFAULT) 1436 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node); 1437 1438 /* if sender is a direct neighbor the sender mac equals 1439 * originator mac 1440 */ 1441 if (is_single_hop_neigh) 1442 orig_neigh_node = orig_node; 1443 else 1444 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, 1445 ethhdr->h_source); 1446 1447 if (!orig_neigh_node) 1448 goto out; 1449 1450 /* Update nc_nodes of the originator */ 1451 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node, 1452 ogm_packet, is_single_hop_neigh); 1453 1454 orig_neigh_router = batadv_orig_router_get(orig_neigh_node, 1455 if_outgoing); 1456 1457 /* drop packet if sender is not a direct neighbor and if we 1458 * don't route towards it 1459 */ 1460 if (!is_single_hop_neigh && (!orig_neigh_router)) { 1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1462 "Drop packet: OGM via unknown neighbor!\n"); 1463 goto out_neigh; 1464 } 1465 1466 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node, 1467 ogm_packet, if_incoming, 1468 if_outgoing); 1469 1470 /* update ranking if it is not a duplicate or has the same 1471 * seqno and similar ttl as the non-duplicate 1472 */ 1473 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 1474 if (!orig_ifinfo) 1475 goto out_neigh; 1476 1477 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno); 1478 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl; 1479 1480 if (is_bidirect && ((dup_status == BATADV_NO_DUP) || 1481 (sameseq && similar_ttl))) { 1482 batadv_iv_ogm_orig_update(bat_priv, orig_node, 1483 orig_ifinfo, ethhdr, 1484 ogm_packet, if_incoming, 1485 if_outgoing, dup_status); 1486 } 1487 batadv_orig_ifinfo_free_ref(orig_ifinfo); 1488 1489 /* only forward for specific interface, not for the default one. */ 1490 if (if_outgoing == BATADV_IF_DEFAULT) 1491 goto out_neigh; 1492 1493 /* is single hop (direct) neighbor */ 1494 if (is_single_hop_neigh) { 1495 /* OGMs from secondary interfaces should only scheduled once 1496 * per interface where it has been received, not multiple times 1497 */ 1498 if ((ogm_packet->ttl <= 2) && 1499 (if_incoming != if_outgoing)) { 1500 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1501 "Drop packet: OGM from secondary interface and wrong outgoing interface\n"); 1502 goto out_neigh; 1503 } 1504 /* mark direct link on incoming interface */ 1505 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, 1506 is_single_hop_neigh, 1507 is_from_best_next_hop, if_incoming, 1508 if_outgoing); 1509 1510 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1511 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); 1512 goto out_neigh; 1513 } 1514 1515 /* multihop originator */ 1516 if (!is_bidirect) { 1517 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1518 "Drop packet: not received via bidirectional link\n"); 1519 goto out_neigh; 1520 } 1521 1522 if (dup_status == BATADV_NEIGH_DUP) { 1523 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1524 "Drop packet: duplicate packet received\n"); 1525 goto out_neigh; 1526 } 1527 1528 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1529 "Forwarding packet: rebroadcast originator packet\n"); 1530 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, 1531 is_single_hop_neigh, is_from_best_next_hop, 1532 if_incoming, if_outgoing); 1533 1534 out_neigh: 1535 if ((orig_neigh_node) && (!is_single_hop_neigh)) 1536 batadv_orig_node_free_ref(orig_neigh_node); 1537 out: 1538 if (router) 1539 batadv_neigh_node_free_ref(router); 1540 if (router_router) 1541 batadv_neigh_node_free_ref(router_router); 1542 if (orig_neigh_router) 1543 batadv_neigh_node_free_ref(orig_neigh_router); 1544 1545 kfree_skb(skb_priv); 1546 } 1547 1548 /** 1549 * batadv_iv_ogm_process - process an incoming batman iv OGM 1550 * @skb: the skb containing the OGM 1551 * @ogm_offset: offset to the OGM which should be processed (for aggregates) 1552 * @if_incoming: the interface where this packet was receved 1553 */ 1554 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset, 1555 struct batadv_hard_iface *if_incoming) 1556 { 1557 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1558 struct batadv_orig_node *orig_neigh_node, *orig_node; 1559 struct batadv_hard_iface *hard_iface; 1560 struct batadv_ogm_packet *ogm_packet; 1561 uint32_t if_incoming_seqno; 1562 bool has_directlink_flag; 1563 struct ethhdr *ethhdr; 1564 bool is_my_oldorig = false; 1565 bool is_my_addr = false; 1566 bool is_my_orig = false; 1567 1568 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset); 1569 ethhdr = eth_hdr(skb); 1570 1571 /* Silently drop when the batman packet is actually not a 1572 * correct packet. 1573 * 1574 * This might happen if a packet is padded (e.g. Ethernet has a 1575 * minimum frame length of 64 byte) and the aggregation interprets 1576 * it as an additional length. 1577 * 1578 * TODO: A more sane solution would be to have a bit in the 1579 * batadv_ogm_packet to detect whether the packet is the last 1580 * packet in an aggregation. Here we expect that the padding 1581 * is always zero (or not 0x01) 1582 */ 1583 if (ogm_packet->packet_type != BATADV_IV_OGM) 1584 return; 1585 1586 /* could be changed by schedule_own_packet() */ 1587 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno); 1588 1589 if (ogm_packet->flags & BATADV_DIRECTLINK) 1590 has_directlink_flag = true; 1591 else 1592 has_directlink_flag = false; 1593 1594 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1595 "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", 1596 ethhdr->h_source, if_incoming->net_dev->name, 1597 if_incoming->net_dev->dev_addr, ogm_packet->orig, 1598 ogm_packet->prev_sender, ntohl(ogm_packet->seqno), 1599 ogm_packet->tq, ogm_packet->ttl, 1600 ogm_packet->version, has_directlink_flag); 1601 1602 rcu_read_lock(); 1603 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 1604 if (hard_iface->if_status != BATADV_IF_ACTIVE) 1605 continue; 1606 1607 if (hard_iface->soft_iface != if_incoming->soft_iface) 1608 continue; 1609 1610 if (batadv_compare_eth(ethhdr->h_source, 1611 hard_iface->net_dev->dev_addr)) 1612 is_my_addr = true; 1613 1614 if (batadv_compare_eth(ogm_packet->orig, 1615 hard_iface->net_dev->dev_addr)) 1616 is_my_orig = true; 1617 1618 if (batadv_compare_eth(ogm_packet->prev_sender, 1619 hard_iface->net_dev->dev_addr)) 1620 is_my_oldorig = true; 1621 } 1622 rcu_read_unlock(); 1623 1624 if (is_my_addr) { 1625 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1626 "Drop packet: received my own broadcast (sender: %pM)\n", 1627 ethhdr->h_source); 1628 return; 1629 } 1630 1631 if (is_my_orig) { 1632 unsigned long *word; 1633 int offset; 1634 int32_t bit_pos; 1635 int16_t if_num; 1636 uint8_t *weight; 1637 1638 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, 1639 ethhdr->h_source); 1640 if (!orig_neigh_node) 1641 return; 1642 1643 /* neighbor has to indicate direct link and it has to 1644 * come via the corresponding interface 1645 * save packet seqno for bidirectional check 1646 */ 1647 if (has_directlink_flag && 1648 batadv_compare_eth(if_incoming->net_dev->dev_addr, 1649 ogm_packet->orig)) { 1650 if_num = if_incoming->if_num; 1651 offset = if_num * BATADV_NUM_WORDS; 1652 1653 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); 1654 word = &(orig_neigh_node->bat_iv.bcast_own[offset]); 1655 bit_pos = if_incoming_seqno - 2; 1656 bit_pos -= ntohl(ogm_packet->seqno); 1657 batadv_set_bit(word, bit_pos); 1658 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num]; 1659 *weight = bitmap_weight(word, 1660 BATADV_TQ_LOCAL_WINDOW_SIZE); 1661 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); 1662 } 1663 1664 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1665 "Drop packet: originator packet from myself (via neighbor)\n"); 1666 batadv_orig_node_free_ref(orig_neigh_node); 1667 return; 1668 } 1669 1670 if (is_my_oldorig) { 1671 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1672 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", 1673 ethhdr->h_source); 1674 return; 1675 } 1676 1677 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) { 1678 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1679 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", 1680 ethhdr->h_source); 1681 return; 1682 } 1683 1684 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig); 1685 if (!orig_node) 1686 return; 1687 1688 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, 1689 if_incoming, BATADV_IF_DEFAULT); 1690 1691 rcu_read_lock(); 1692 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 1693 if (hard_iface->if_status != BATADV_IF_ACTIVE) 1694 continue; 1695 1696 if (hard_iface->soft_iface != bat_priv->soft_iface) 1697 continue; 1698 1699 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, 1700 if_incoming, hard_iface); 1701 } 1702 rcu_read_unlock(); 1703 1704 batadv_orig_node_free_ref(orig_node); 1705 } 1706 1707 static int batadv_iv_ogm_receive(struct sk_buff *skb, 1708 struct batadv_hard_iface *if_incoming) 1709 { 1710 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1711 struct batadv_ogm_packet *ogm_packet; 1712 uint8_t *packet_pos; 1713 int ogm_offset; 1714 bool ret; 1715 1716 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); 1717 if (!ret) 1718 return NET_RX_DROP; 1719 1720 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface 1721 * that does not have B.A.T.M.A.N. IV enabled ? 1722 */ 1723 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit) 1724 return NET_RX_DROP; 1725 1726 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); 1727 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, 1728 skb->len + ETH_HLEN); 1729 1730 ogm_offset = 0; 1731 ogm_packet = (struct batadv_ogm_packet *)skb->data; 1732 1733 /* unpack the aggregated packets and process them one by one */ 1734 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb), 1735 ogm_packet->tvlv_len)) { 1736 batadv_iv_ogm_process(skb, ogm_offset, if_incoming); 1737 1738 ogm_offset += BATADV_OGM_HLEN; 1739 ogm_offset += ntohs(ogm_packet->tvlv_len); 1740 1741 packet_pos = skb->data + ogm_offset; 1742 ogm_packet = (struct batadv_ogm_packet *)packet_pos; 1743 } 1744 1745 kfree_skb(skb); 1746 return NET_RX_SUCCESS; 1747 } 1748 1749 /** 1750 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table 1751 * @orig_node: the orig_node for which the neighbors are printed 1752 * @if_outgoing: outgoing interface for these entries 1753 * @seq: debugfs table seq_file struct 1754 * 1755 * Must be called while holding an rcu lock. 1756 */ 1757 static void 1758 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node, 1759 struct batadv_hard_iface *if_outgoing, 1760 struct seq_file *seq) 1761 { 1762 struct batadv_neigh_node *neigh_node; 1763 struct batadv_neigh_ifinfo *n_ifinfo; 1764 1765 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { 1766 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); 1767 if (!n_ifinfo) 1768 continue; 1769 1770 seq_printf(seq, " %pM (%3i)", 1771 neigh_node->addr, 1772 n_ifinfo->bat_iv.tq_avg); 1773 1774 batadv_neigh_ifinfo_free_ref(n_ifinfo); 1775 } 1776 } 1777 1778 /** 1779 * batadv_iv_ogm_orig_print - print the originator table 1780 * @bat_priv: the bat priv with all the soft interface information 1781 * @seq: debugfs table seq_file struct 1782 * @if_outgoing: the outgoing interface for which this should be printed 1783 */ 1784 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, 1785 struct seq_file *seq, 1786 struct batadv_hard_iface *if_outgoing) 1787 { 1788 struct batadv_neigh_node *neigh_node; 1789 struct batadv_hashtable *hash = bat_priv->orig_hash; 1790 int last_seen_msecs, last_seen_secs; 1791 struct batadv_orig_node *orig_node; 1792 struct batadv_neigh_ifinfo *n_ifinfo; 1793 unsigned long last_seen_jiffies; 1794 struct hlist_head *head; 1795 int batman_count = 0; 1796 uint32_t i; 1797 1798 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", 1799 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, 1800 "Nexthop", "outgoingIF", "Potential nexthops"); 1801 1802 for (i = 0; i < hash->size; i++) { 1803 head = &hash->table[i]; 1804 1805 rcu_read_lock(); 1806 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 1807 neigh_node = batadv_orig_router_get(orig_node, 1808 if_outgoing); 1809 if (!neigh_node) 1810 continue; 1811 1812 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, 1813 if_outgoing); 1814 if (!n_ifinfo) 1815 goto next; 1816 1817 if (n_ifinfo->bat_iv.tq_avg == 0) 1818 goto next; 1819 1820 last_seen_jiffies = jiffies - orig_node->last_seen; 1821 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 1822 last_seen_secs = last_seen_msecs / 1000; 1823 last_seen_msecs = last_seen_msecs % 1000; 1824 1825 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", 1826 orig_node->orig, last_seen_secs, 1827 last_seen_msecs, n_ifinfo->bat_iv.tq_avg, 1828 neigh_node->addr, 1829 neigh_node->if_incoming->net_dev->name); 1830 1831 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing, 1832 seq); 1833 seq_puts(seq, "\n"); 1834 batman_count++; 1835 1836 next: 1837 batadv_neigh_node_free_ref(neigh_node); 1838 if (n_ifinfo) 1839 batadv_neigh_ifinfo_free_ref(n_ifinfo); 1840 } 1841 rcu_read_unlock(); 1842 } 1843 1844 if (batman_count == 0) 1845 seq_puts(seq, "No batman nodes in range ...\n"); 1846 } 1847 1848 /** 1849 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors 1850 * @neigh1: the first neighbor object of the comparison 1851 * @if_outgoing1: outgoing interface for the first neighbor 1852 * @neigh2: the second neighbor object of the comparison 1853 * @if_outgoing2: outgoing interface for the second neighbor 1854 * 1855 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is 1856 * lower, the same as or higher than the metric via neigh2 1857 */ 1858 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, 1859 struct batadv_hard_iface *if_outgoing1, 1860 struct batadv_neigh_node *neigh2, 1861 struct batadv_hard_iface *if_outgoing2) 1862 { 1863 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; 1864 uint8_t tq1, tq2; 1865 int diff; 1866 1867 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); 1868 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); 1869 1870 if (!neigh1_ifinfo || !neigh2_ifinfo) { 1871 diff = 0; 1872 goto out; 1873 } 1874 1875 tq1 = neigh1_ifinfo->bat_iv.tq_avg; 1876 tq2 = neigh2_ifinfo->bat_iv.tq_avg; 1877 diff = tq1 - tq2; 1878 1879 out: 1880 if (neigh1_ifinfo) 1881 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo); 1882 if (neigh2_ifinfo) 1883 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo); 1884 1885 return diff; 1886 } 1887 1888 /** 1889 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than 1890 * neigh2 from the metric prospective 1891 * @neigh1: the first neighbor object of the comparison 1892 * @if_outgoing: outgoing interface for the first neighbor 1893 * @neigh2: the second neighbor object of the comparison 1894 * @if_outgoing2: outgoing interface for the second neighbor 1895 1896 * Returns true if the metric via neigh1 is equally good or better than 1897 * the metric via neigh2, false otherwise. 1898 */ 1899 static bool 1900 batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1, 1901 struct batadv_hard_iface *if_outgoing1, 1902 struct batadv_neigh_node *neigh2, 1903 struct batadv_hard_iface *if_outgoing2) 1904 { 1905 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; 1906 uint8_t tq1, tq2; 1907 bool ret; 1908 1909 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); 1910 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); 1911 1912 /* we can't say that the metric is better */ 1913 if (!neigh1_ifinfo || !neigh2_ifinfo) { 1914 ret = false; 1915 goto out; 1916 } 1917 1918 tq1 = neigh1_ifinfo->bat_iv.tq_avg; 1919 tq2 = neigh2_ifinfo->bat_iv.tq_avg; 1920 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD; 1921 1922 out: 1923 if (neigh1_ifinfo) 1924 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo); 1925 if (neigh2_ifinfo) 1926 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo); 1927 1928 return ret; 1929 } 1930 1931 static struct batadv_algo_ops batadv_batman_iv __read_mostly = { 1932 .name = "BATMAN_IV", 1933 .bat_iface_enable = batadv_iv_ogm_iface_enable, 1934 .bat_iface_disable = batadv_iv_ogm_iface_disable, 1935 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac, 1936 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set, 1937 .bat_ogm_schedule = batadv_iv_ogm_schedule, 1938 .bat_ogm_emit = batadv_iv_ogm_emit, 1939 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp, 1940 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob, 1941 .bat_orig_print = batadv_iv_ogm_orig_print, 1942 .bat_orig_free = batadv_iv_ogm_orig_free, 1943 .bat_orig_add_if = batadv_iv_ogm_orig_add_if, 1944 .bat_orig_del_if = batadv_iv_ogm_orig_del_if, 1945 }; 1946 1947 int __init batadv_iv_init(void) 1948 { 1949 int ret; 1950 1951 /* batman originator packet */ 1952 ret = batadv_recv_handler_register(BATADV_IV_OGM, 1953 batadv_iv_ogm_receive); 1954 if (ret < 0) 1955 goto out; 1956 1957 ret = batadv_algo_register(&batadv_batman_iv); 1958 if (ret < 0) 1959 goto handler_unregister; 1960 1961 goto out; 1962 1963 handler_unregister: 1964 batadv_recv_handler_unregister(BATADV_IV_OGM); 1965 out: 1966 return ret; 1967 } 1968