1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Antonio Quartulli 5 */ 6 7 #include "bat_v_ogm.h" 8 #include "main.h" 9 10 #include <linux/atomic.h> 11 #include <linux/bug.h> 12 #include <linux/byteorder/generic.h> 13 #include <linux/compiler.h> 14 #include <linux/container_of.h> 15 #include <linux/errno.h> 16 #include <linux/etherdevice.h> 17 #include <linux/gfp.h> 18 #include <linux/if_ether.h> 19 #include <linux/jiffies.h> 20 #include <linux/kref.h> 21 #include <linux/list.h> 22 #include <linux/lockdep.h> 23 #include <linux/minmax.h> 24 #include <linux/mutex.h> 25 #include <linux/netdevice.h> 26 #include <linux/random.h> 27 #include <linux/rcupdate.h> 28 #include <linux/skbuff.h> 29 #include <linux/slab.h> 30 #include <linux/spinlock.h> 31 #include <linux/stddef.h> 32 #include <linux/string.h> 33 #include <linux/types.h> 34 #include <linux/workqueue.h> 35 #include <uapi/linux/batadv_packet.h> 36 37 #include "hard-interface.h" 38 #include "hash.h" 39 #include "log.h" 40 #include "originator.h" 41 #include "routing.h" 42 #include "send.h" 43 #include "translation-table.h" 44 #include "tvlv.h" 45 46 /** 47 * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node 48 * @bat_priv: the bat priv with all the mesh interface information 49 * @addr: the address of the originator 50 * 51 * Return: the orig_node corresponding to the specified address. If such an 52 * object does not exist, it is allocated here. In case of allocation failure 53 * returns NULL. 54 */ 55 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv, 56 const u8 *addr) 57 { 58 struct batadv_orig_node *orig_node; 59 int hash_added; 60 61 orig_node = batadv_orig_hash_find(bat_priv, addr); 62 if (orig_node) 63 return orig_node; 64 65 orig_node = batadv_orig_node_new(bat_priv, addr); 66 if (!orig_node) 67 return NULL; 68 69 kref_get(&orig_node->refcount); 70 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, 71 batadv_choose_orig, orig_node, 72 &orig_node->hash_entry); 73 if (hash_added != 0) { 74 /* remove refcnt for newly created orig_node and hash entry */ 75 batadv_orig_node_put(orig_node); 76 batadv_orig_node_put(orig_node); 77 orig_node = NULL; 78 } 79 80 return orig_node; 81 } 82 83 /** 84 * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer 85 * @hard_iface: the interface to use to send the OGM 86 */ 87 static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface) 88 { 89 unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000; 90 91 /* msecs * [0.9, 1.1] */ 92 msecs += get_random_u32_below(msecs / 5) - (msecs / 10); 93 queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq, 94 msecs_to_jiffies(msecs / 1000)); 95 } 96 97 /** 98 * batadv_v_ogm_start_timer() - restart the OGM sending timer 99 * @bat_priv: the bat priv with all the mesh interface information 100 */ 101 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv) 102 { 103 unsigned long msecs; 104 /* this function may be invoked in different contexts (ogm rescheduling 105 * or hard_iface activation), but the work timer should not be reset 106 */ 107 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq)) 108 return; 109 110 msecs = READ_ONCE(bat_priv->orig_interval) - BATADV_JITTER; 111 msecs += get_random_u32_below(2 * BATADV_JITTER); 112 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq, 113 msecs_to_jiffies(msecs)); 114 } 115 116 /** 117 * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface 118 * @bat_priv: the bat priv with all the mesh interface information 119 * @skb: the OGM to send 120 * @hard_iface: the interface to use to send the OGM 121 */ 122 static void batadv_v_ogm_send_to_if(struct batadv_priv *bat_priv, 123 struct sk_buff *skb, 124 struct batadv_hard_iface *hard_iface) 125 { 126 if (hard_iface->if_status != BATADV_IF_ACTIVE) { 127 kfree_skb(skb); 128 return; 129 } 130 131 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX); 132 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES, 133 skb->len + ETH_HLEN); 134 135 batadv_send_broadcast_skb(skb, hard_iface); 136 } 137 138 /** 139 * batadv_v_ogm_len() - OGMv2 packet length 140 * @skb: the OGM to check 141 * 142 * Return: Length of the given OGMv2 packet, including tvlv length, excluding 143 * ethernet header length. 144 */ 145 static unsigned int batadv_v_ogm_len(struct sk_buff *skb) 146 { 147 struct batadv_ogm2_packet *ogm_packet; 148 149 ogm_packet = (struct batadv_ogm2_packet *)skb->data; 150 return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len); 151 } 152 153 /** 154 * batadv_v_ogm_queue_left() - check if given OGM still fits aggregation queue 155 * @skb: the OGM to check 156 * @hard_iface: the interface to use to send the OGM 157 * 158 * Caller needs to hold the hard_iface->bat_v.aggr_list.lock. 159 * 160 * Return: True, if the given OGMv2 packet still fits, false otherwise. 161 */ 162 static bool batadv_v_ogm_queue_left(struct sk_buff *skb, 163 struct batadv_hard_iface *hard_iface) 164 { 165 unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu, 166 BATADV_MAX_AGGREGATION_BYTES); 167 unsigned int ogm_len = batadv_v_ogm_len(skb); 168 169 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock); 170 171 return hard_iface->bat_v.aggr_len + ogm_len <= max; 172 } 173 174 /** 175 * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue 176 * @hard_iface: the interface holding the aggregation queue 177 * 178 * Empties the OGMv2 aggregation queue and frees all the skbs it contains. 179 * 180 * Caller needs to hold the hard_iface->bat_v.aggr_list.lock. 181 */ 182 static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface) 183 { 184 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock); 185 186 __skb_queue_purge(&hard_iface->bat_v.aggr_list); 187 hard_iface->bat_v.aggr_len = 0; 188 } 189 190 /** 191 * batadv_v_ogm_aggr_send() - flush & send aggregation queue 192 * @bat_priv: the bat priv with all the mesh interface information 193 * @hard_iface: the interface with the aggregation queue to flush 194 * 195 * Aggregates all OGMv2 packets currently in the aggregation queue into a 196 * single OGMv2 packet and transmits this aggregate. 197 * 198 * The aggregation queue is empty after this call. 199 * 200 * Caller needs to hold the hard_iface->bat_v.aggr_list.lock. 201 */ 202 static void batadv_v_ogm_aggr_send(struct batadv_priv *bat_priv, 203 struct batadv_hard_iface *hard_iface) 204 { 205 unsigned int aggr_len = hard_iface->bat_v.aggr_len; 206 struct sk_buff *skb_aggr; 207 unsigned int ogm_len; 208 struct sk_buff *skb; 209 210 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock); 211 212 if (!aggr_len) 213 return; 214 215 skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN); 216 if (!skb_aggr) { 217 batadv_v_ogm_aggr_list_free(hard_iface); 218 return; 219 } 220 221 skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN); 222 skb_reset_network_header(skb_aggr); 223 224 while ((skb = __skb_dequeue(&hard_iface->bat_v.aggr_list))) { 225 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb); 226 227 ogm_len = batadv_v_ogm_len(skb); 228 skb_put_data(skb_aggr, skb->data, ogm_len); 229 230 consume_skb(skb); 231 } 232 233 batadv_v_ogm_send_to_if(bat_priv, skb_aggr, hard_iface); 234 } 235 236 /** 237 * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface 238 * @bat_priv: the bat priv with all the mesh interface information 239 * @skb: the OGM to queue 240 * @hard_iface: the interface to queue the OGM on 241 */ 242 static void batadv_v_ogm_queue_on_if(struct batadv_priv *bat_priv, 243 struct sk_buff *skb, 244 struct batadv_hard_iface *hard_iface) 245 { 246 if (hard_iface->mesh_iface != bat_priv->mesh_iface) { 247 kfree_skb(skb); 248 return; 249 } 250 251 if (!READ_ONCE(bat_priv->aggregated_ogms)) { 252 batadv_v_ogm_send_to_if(bat_priv, skb, hard_iface); 253 return; 254 } 255 256 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock); 257 if (!batadv_v_ogm_queue_left(skb, hard_iface)) 258 batadv_v_ogm_aggr_send(bat_priv, hard_iface); 259 260 hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb); 261 __skb_queue_tail(&hard_iface->bat_v.aggr_list, skb); 262 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock); 263 } 264 265 /** 266 * batadv_v_ogm_send_meshif() - periodic worker broadcasting the own OGM 267 * @bat_priv: the bat priv with all the mesh interface information 268 */ 269 static void batadv_v_ogm_send_meshif(struct batadv_priv *bat_priv) 270 { 271 struct batadv_hard_iface *hard_iface; 272 struct batadv_ogm2_packet *ogm_packet; 273 struct batadv_ogm_buf *ogm_buff; 274 struct sk_buff *skb, *skb_tmp; 275 struct list_head *iter; 276 u16 tvlv_len; 277 int ret; 278 279 lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex); 280 281 if (READ_ONCE(bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) 282 goto out; 283 284 ogm_buff = &bat_priv->bat_v.ogm_buff; 285 286 /* tt changes have to be committed before the tvlv data is 287 * appended as it may alter the tt tvlv container 288 */ 289 batadv_tt_local_commit_changes(bat_priv); 290 ret = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff); 291 if (ret < 0) 292 goto reschedule; 293 294 tvlv_len = ret; 295 296 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff->len); 297 if (!skb) 298 goto reschedule; 299 300 skb_reserve(skb, ETH_HLEN); 301 skb_put_data(skb, ogm_buff->buf, ogm_buff->len); 302 303 ogm_packet = (struct batadv_ogm2_packet *)skb->data; 304 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno)); 305 atomic_inc(&bat_priv->bat_v.ogm_seqno); 306 ogm_packet->tvlv_len = htons(tvlv_len); 307 308 /* broadcast on every interface */ 309 rcu_read_lock(); 310 netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) { 311 if (!kref_get_unless_zero(&hard_iface->refcount)) 312 continue; 313 314 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL); 315 if (ret) { 316 char *type; 317 318 switch (ret) { 319 case BATADV_HARDIF_BCAST_NORECIPIENT: 320 type = "no neighbor"; 321 break; 322 case BATADV_HARDIF_BCAST_DUPFWD: 323 type = "single neighbor is source"; 324 break; 325 case BATADV_HARDIF_BCAST_DUPORIG: 326 type = "single neighbor is originator"; 327 break; 328 default: 329 type = "unknown"; 330 } 331 332 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n", 333 hard_iface->net_dev->name, type); 334 335 batadv_hardif_put(hard_iface); 336 continue; 337 } 338 339 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 340 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n", 341 ogm_packet->orig, ntohl(ogm_packet->seqno), 342 ntohl(ogm_packet->throughput), ogm_packet->ttl, 343 hard_iface->net_dev->name, 344 hard_iface->net_dev->dev_addr); 345 346 /* this skb gets consumed by batadv_v_ogm_send_to_if() */ 347 skb_tmp = skb_clone(skb, GFP_ATOMIC); 348 if (!skb_tmp) { 349 batadv_hardif_put(hard_iface); 350 break; 351 } 352 353 batadv_v_ogm_queue_on_if(bat_priv, skb_tmp, hard_iface); 354 batadv_hardif_put(hard_iface); 355 } 356 rcu_read_unlock(); 357 358 consume_skb(skb); 359 360 reschedule: 361 batadv_v_ogm_start_timer(bat_priv); 362 out: 363 return; 364 } 365 366 /** 367 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM 368 * @work: work queue item 369 */ 370 static void batadv_v_ogm_send(struct work_struct *work) 371 { 372 struct batadv_priv_bat_v *bat_v; 373 struct batadv_priv *bat_priv; 374 375 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work); 376 bat_priv = container_of(bat_v, struct batadv_priv, bat_v); 377 378 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex); 379 batadv_v_ogm_send_meshif(bat_priv); 380 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex); 381 } 382 383 /** 384 * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface 385 * @work: work queue item 386 * 387 * Emits aggregated OGM messages in regular intervals. 388 */ 389 void batadv_v_ogm_aggr_work(struct work_struct *work) 390 { 391 struct batadv_hard_iface_bat_v *batv; 392 struct batadv_hard_iface *hard_iface; 393 struct batadv_priv *bat_priv; 394 395 batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work); 396 hard_iface = container_of(batv, struct batadv_hard_iface, bat_v); 397 bat_priv = netdev_priv(hard_iface->mesh_iface); 398 399 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock); 400 batadv_v_ogm_aggr_send(bat_priv, hard_iface); 401 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock); 402 403 batadv_v_ogm_start_queue_timer(hard_iface); 404 } 405 406 /** 407 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V 408 * @hard_iface: the interface to prepare 409 * 410 * Takes care of scheduling its own OGM sending routine for this interface. 411 * 412 * Return: 0 on success or a negative error code otherwise 413 */ 414 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface) 415 { 416 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface); 417 418 enable_delayed_work(&hard_iface->bat_v.aggr_wq); 419 420 batadv_v_ogm_start_queue_timer(hard_iface); 421 batadv_v_ogm_start_timer(bat_priv); 422 423 return 0; 424 } 425 426 /** 427 * batadv_v_ogm_iface_disable() - release OGM interface private resources 428 * @hard_iface: interface for which the resources have to be released 429 */ 430 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface) 431 { 432 disable_delayed_work_sync(&hard_iface->bat_v.aggr_wq); 433 434 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock); 435 batadv_v_ogm_aggr_list_free(hard_iface); 436 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock); 437 } 438 439 /** 440 * batadv_v_ogm_primary_iface_set() - set a new primary interface 441 * @primary_iface: the new primary interface 442 */ 443 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface) 444 { 445 struct batadv_priv *bat_priv = netdev_priv(primary_iface->mesh_iface); 446 struct batadv_ogm2_packet *ogm_packet; 447 448 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex); 449 if (!bat_priv->bat_v.ogm_buff.buf) 450 goto unlock; 451 452 ogm_packet = bat_priv->bat_v.ogm_buff.buf; 453 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr); 454 455 unlock: 456 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex); 457 } 458 459 /** 460 * batadv_v_forward_penalty() - apply a penalty to the throughput metric 461 * forwarded with B.A.T.M.A.N. V OGMs 462 * @bat_priv: the bat priv with all the mesh interface information 463 * @if_incoming: the interface where the OGM has been received 464 * @if_outgoing: the interface where the OGM has to be forwarded to 465 * @throughput: the current throughput 466 * 467 * Apply a penalty on the current throughput metric value based on the 468 * characteristic of the interface where the OGM has been received. 469 * 470 * Initially the per hardif hop penalty is applied to the throughput. After 471 * that the return value is then computed as follows: 472 * - throughput * 50% if the incoming and outgoing interface are the 473 * same WiFi interface and the throughput is above 474 * 1MBit/s 475 * - throughput if the outgoing interface is the default 476 * interface (i.e. this OGM is processed for the 477 * internal table and not forwarded) 478 * - throughput * node hop penalty otherwise 479 * 480 * Return: the penalised throughput metric. 481 */ 482 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv, 483 struct batadv_hard_iface *if_incoming, 484 struct batadv_hard_iface *if_outgoing, 485 u32 throughput) 486 { 487 u32 if_hop_penalty = READ_ONCE(if_incoming->hop_penalty); 488 u32 hop_penalty = READ_ONCE(bat_priv->hop_penalty); 489 u32 hop_penalty_max = BATADV_TQ_MAX_VALUE; 490 491 /* Apply per hardif hop penalty */ 492 throughput = throughput * (hop_penalty_max - if_hop_penalty) / 493 hop_penalty_max; 494 495 /* Don't apply hop penalty in default originator table. */ 496 if (if_outgoing == BATADV_IF_DEFAULT) 497 return throughput; 498 499 /* Forwarding on the same WiFi interface cuts the throughput in half 500 * due to the store & forward characteristics of WIFI. 501 * Very low throughput values are the exception. 502 */ 503 if (throughput > 10 && 504 if_incoming == if_outgoing && 505 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX)) 506 return throughput / 2; 507 508 /* hop penalty of 255 equals 100% */ 509 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max; 510 } 511 512 /** 513 * batadv_v_ogm_forward() - check conditions and forward an OGM to the given 514 * outgoing interface 515 * @bat_priv: the bat priv with all the mesh interface information 516 * @ogm_received: previously received OGM to be forwarded 517 * @orig_node: the originator which has been updated 518 * @neigh_node: the neigh_node through which the OGM has been received 519 * @if_incoming: the interface on which this OGM was received on 520 * @if_outgoing: the interface to which the OGM has to be forwarded to 521 * 522 * Forward an OGM to an interface after having altered the throughput metric and 523 * the TTL value contained in it. The original OGM isn't modified. 524 */ 525 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv, 526 const struct batadv_ogm2_packet *ogm_received, 527 struct batadv_orig_node *orig_node, 528 struct batadv_neigh_node *neigh_node, 529 struct batadv_hard_iface *if_incoming, 530 struct batadv_hard_iface *if_outgoing) 531 { 532 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL; 533 struct batadv_orig_ifinfo *orig_ifinfo = NULL; 534 struct batadv_neigh_node *router = NULL; 535 struct batadv_ogm2_packet *ogm_forward; 536 unsigned char *skb_buff; 537 struct sk_buff *skb; 538 size_t packet_len; 539 u16 tvlv_len; 540 541 /* only forward for specific interfaces, not for the default one. */ 542 if (if_outgoing == BATADV_IF_DEFAULT) 543 goto out; 544 545 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 546 if (!orig_ifinfo) 547 goto out; 548 549 /* acquire possibly updated router */ 550 router = batadv_orig_router_get(orig_node, if_outgoing); 551 552 /* strict rule: forward packets coming from the best next hop only */ 553 if (neigh_node != router) 554 goto out; 555 556 /* don't forward the same seqno twice on one interface */ 557 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno)) 558 goto out; 559 560 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno); 561 562 if (ogm_received->ttl <= 1) { 563 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); 564 goto out; 565 } 566 567 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); 568 if (!neigh_ifinfo) 569 goto out; 570 571 tvlv_len = ntohs(ogm_received->tvlv_len); 572 573 packet_len = BATADV_OGM2_HLEN + tvlv_len; 574 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev, 575 ETH_HLEN + packet_len); 576 if (!skb) 577 goto out; 578 579 skb_reserve(skb, ETH_HLEN); 580 skb_buff = skb_put_data(skb, ogm_received, packet_len); 581 582 /* apply forward penalty */ 583 ogm_forward = (struct batadv_ogm2_packet *)skb_buff; 584 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput); 585 ogm_forward->ttl--; 586 587 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 588 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n", 589 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput), 590 ogm_forward->ttl, if_incoming->net_dev->name); 591 592 batadv_v_ogm_queue_on_if(bat_priv, skb, if_outgoing); 593 594 out: 595 batadv_orig_ifinfo_put(orig_ifinfo); 596 batadv_neigh_node_put(router); 597 batadv_neigh_ifinfo_put(neigh_ifinfo); 598 } 599 600 /** 601 * batadv_v_ogm_metric_update() - update route metric based on OGM 602 * @bat_priv: the bat priv with all the mesh interface information 603 * @ogm2: OGM2 structure 604 * @orig_node: Originator structure for which the OGM has been received 605 * @neigh_node: the neigh_node through which the OGM has been received 606 * @if_incoming: the interface where this packet was received 607 * @if_outgoing: the interface for which the packet should be considered 608 * 609 * Return: 610 * 1 if the OGM is new, 611 * 0 if it is not new but valid, 612 * <0 on error (e.g. old OGM) 613 */ 614 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv, 615 const struct batadv_ogm2_packet *ogm2, 616 struct batadv_orig_node *orig_node, 617 struct batadv_neigh_node *neigh_node, 618 struct batadv_hard_iface *if_incoming, 619 struct batadv_hard_iface *if_outgoing) 620 { 621 struct batadv_orig_ifinfo *orig_ifinfo; 622 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL; 623 bool protection_started = false; 624 int ret = -EINVAL; 625 u32 path_throughput; 626 s32 seq_diff; 627 628 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); 629 if (!orig_ifinfo) 630 goto out; 631 632 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno; 633 634 if (!hlist_empty(&orig_node->neigh_list) && 635 batadv_window_protected(bat_priv, seq_diff, 636 BATADV_OGM_MAX_AGE, 637 &orig_ifinfo->batman_seqno_reset, 638 &protection_started)) { 639 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 640 "Drop packet: packet within window protection time from %pM\n", 641 ogm2->orig); 642 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 643 "Last reset: %ld, %ld\n", 644 orig_ifinfo->batman_seqno_reset, jiffies); 645 goto out; 646 } 647 648 /* drop packets with old seqnos, however accept the first packet after 649 * a host has been rebooted. 650 */ 651 if (seq_diff < 0 && !protection_started) 652 goto out; 653 654 neigh_node->last_seen = jiffies; 655 656 orig_node->last_seen = jiffies; 657 658 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno); 659 orig_ifinfo->last_ttl = ogm2->ttl; 660 661 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing); 662 if (!neigh_ifinfo) 663 goto out; 664 665 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming, 666 if_outgoing, 667 ntohl(ogm2->throughput)); 668 neigh_ifinfo->bat_v.throughput = path_throughput; 669 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno); 670 neigh_ifinfo->last_ttl = ogm2->ttl; 671 672 if (seq_diff > 0 || protection_started) 673 ret = 1; 674 else 675 ret = 0; 676 out: 677 batadv_orig_ifinfo_put(orig_ifinfo); 678 batadv_neigh_ifinfo_put(neigh_ifinfo); 679 680 return ret; 681 } 682 683 /** 684 * batadv_v_ogm_route_update() - update routes based on OGM 685 * @bat_priv: the bat priv with all the mesh interface information 686 * @ethhdr: the Ethernet header of the OGM2 687 * @ogm2: OGM2 structure 688 * @orig_node: Originator structure for which the OGM has been received 689 * @neigh_node: the neigh_node through which the OGM has been received 690 * @if_incoming: the interface where this packet was received 691 * @if_outgoing: the interface for which the packet should be considered 692 * 693 * Return: true if the packet should be forwarded, false otherwise 694 */ 695 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv, 696 const struct ethhdr *ethhdr, 697 const struct batadv_ogm2_packet *ogm2, 698 struct batadv_orig_node *orig_node, 699 struct batadv_neigh_node *neigh_node, 700 struct batadv_hard_iface *if_incoming, 701 struct batadv_hard_iface *if_outgoing) 702 { 703 struct batadv_neigh_node *router = NULL; 704 struct batadv_orig_node *orig_neigh_node; 705 struct batadv_neigh_node *orig_neigh_router = NULL; 706 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL; 707 u32 router_throughput, neigh_throughput; 708 u32 router_last_seqno; 709 u32 neigh_last_seqno; 710 s32 neigh_seq_diff; 711 bool forward = false; 712 713 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source); 714 if (!orig_neigh_node) 715 goto out; 716 717 orig_neigh_router = batadv_orig_router_get(orig_neigh_node, 718 if_outgoing); 719 720 /* drop packet if sender is not a direct neighbor and if we 721 * don't route towards it 722 */ 723 router = batadv_orig_router_get(orig_node, if_outgoing); 724 if (router && ACCESS_PRIVATE(router, orig_node_id) != orig_node && !orig_neigh_router) { 725 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 726 "Drop packet: OGM via unknown neighbor!\n"); 727 goto out; 728 } 729 730 /* Mark the OGM to be considered for forwarding, and update routes 731 * if needed. 732 */ 733 forward = true; 734 735 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 736 "Searching and updating originator entry of received packet\n"); 737 738 /* if this neighbor already is our next hop there is nothing 739 * to change 740 */ 741 if (router == neigh_node) 742 goto out; 743 744 /* don't consider neighbours with worse throughput. 745 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than 746 * the last received seqno from our best next hop. 747 */ 748 if (router) { 749 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); 750 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); 751 752 /* if these are not allocated, something is wrong. */ 753 if (!router_ifinfo || !neigh_ifinfo) 754 goto out; 755 756 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno; 757 router_last_seqno = router_ifinfo->bat_v.last_seqno; 758 neigh_seq_diff = neigh_last_seqno - router_last_seqno; 759 router_throughput = router_ifinfo->bat_v.throughput; 760 neigh_throughput = neigh_ifinfo->bat_v.throughput; 761 762 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF && 763 router_throughput >= neigh_throughput) 764 goto out; 765 } 766 767 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node); 768 out: 769 batadv_neigh_node_put(router); 770 batadv_neigh_node_put(orig_neigh_router); 771 batadv_orig_node_put(orig_neigh_node); 772 batadv_neigh_ifinfo_put(router_ifinfo); 773 batadv_neigh_ifinfo_put(neigh_ifinfo); 774 775 return forward; 776 } 777 778 /** 779 * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if 780 * @bat_priv: the bat priv with all the mesh interface information 781 * @ethhdr: the Ethernet header of the OGM2 782 * @ogm2: OGM2 structure 783 * @orig_node: Originator structure for which the OGM has been received 784 * @neigh_node: the neigh_node through which the OGM has been received 785 * @if_incoming: the interface where this packet was received 786 * @if_outgoing: the interface for which the packet should be considered 787 */ 788 static void 789 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv, 790 const struct ethhdr *ethhdr, 791 const struct batadv_ogm2_packet *ogm2, 792 struct batadv_orig_node *orig_node, 793 struct batadv_neigh_node *neigh_node, 794 struct batadv_hard_iface *if_incoming, 795 struct batadv_hard_iface *if_outgoing) 796 { 797 int seqno_age; 798 bool forward; 799 800 /* first, update the metric with according sanity checks */ 801 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node, 802 neigh_node, if_incoming, 803 if_outgoing); 804 805 /* outdated sequence numbers are to be discarded */ 806 if (seqno_age < 0) 807 return; 808 809 /* only unknown & newer OGMs contain TVLVs we are interested in */ 810 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT) 811 batadv_tvlv_containers_process(bat_priv, BATADV_OGM2, orig_node, 812 NULL, 813 (unsigned char *)(ogm2 + 1), 814 ntohs(ogm2->tvlv_len)); 815 816 /* if the metric update went through, update routes if needed */ 817 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node, 818 neigh_node, if_incoming, 819 if_outgoing); 820 821 /* if the routes have been processed correctly, check and forward */ 822 if (forward) 823 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node, 824 if_incoming, if_outgoing); 825 } 826 827 /** 828 * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated 829 * @buff_pos: current position in the skb 830 * @packet_len: total length of the skb 831 * @ogm2_packet: potential OGM2 in buffer 832 * 833 * Return: true if there is enough space for another OGM, false otherwise. 834 */ 835 static bool 836 batadv_v_ogm_aggr_packet(int buff_pos, int packet_len, 837 const struct batadv_ogm2_packet *ogm2_packet) 838 { 839 int next_buff_pos = 0; 840 841 /* check if there is enough space for the header */ 842 next_buff_pos += buff_pos + sizeof(*ogm2_packet); 843 if (next_buff_pos > packet_len) 844 return false; 845 846 /* check if there is enough space for the optional TVLV */ 847 next_buff_pos += ntohs(ogm2_packet->tvlv_len); 848 849 return next_buff_pos <= packet_len; 850 } 851 852 /** 853 * batadv_v_ogm_process() - process an incoming batman v OGM 854 * @skb: the skb containing the OGM 855 * @ogm_offset: offset to the OGM which should be processed (for aggregates) 856 * @if_incoming: the interface where this packet was received 857 */ 858 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset, 859 struct batadv_hard_iface *if_incoming) 860 { 861 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface); 862 struct ethhdr *ethhdr; 863 struct batadv_orig_node *orig_node = NULL; 864 struct batadv_hardif_neigh_node *hardif_neigh = NULL; 865 struct batadv_neigh_node *neigh_node = NULL; 866 struct batadv_hard_iface *hard_iface; 867 struct batadv_ogm2_packet *ogm_packet; 868 u32 ogm_throughput, link_throughput, path_throughput; 869 struct list_head *iter; 870 int ret; 871 872 ethhdr = eth_hdr(skb); 873 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset); 874 875 ogm_throughput = ntohl(ogm_packet->throughput); 876 877 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 878 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n", 879 ethhdr->h_source, if_incoming->net_dev->name, 880 if_incoming->net_dev->dev_addr, ogm_packet->orig, 881 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl, 882 ogm_packet->version, ntohs(ogm_packet->tvlv_len)); 883 884 if (batadv_is_my_mac(bat_priv, ogm_packet->orig)) { 885 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 886 "Drop packet: originator packet from ourself\n"); 887 return; 888 } 889 890 /* If the throughput metric is 0, immediately drop the packet. No need 891 * to create orig_node / neigh_node for an unusable route. 892 */ 893 if (ogm_throughput == 0) { 894 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 895 "Drop packet: originator packet with throughput metric of 0\n"); 896 return; 897 } 898 899 /* require ELP packets be to received from this neighbor first */ 900 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source); 901 if (!hardif_neigh) { 902 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 903 "Drop packet: OGM via unknown neighbor!\n"); 904 goto out; 905 } 906 907 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig); 908 if (!orig_node) 909 goto out; 910 911 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming, 912 ethhdr->h_source); 913 if (!neigh_node) 914 goto out; 915 916 /* Update the received throughput metric to match the link 917 * characteristic: 918 * - If this OGM traveled one hop so far (emitted by single hop 919 * neighbor) the path throughput metric equals the link throughput. 920 * - For OGMs traversing more than hop the path throughput metric is 921 * the smaller of the path throughput and the link throughput. 922 */ 923 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput); 924 path_throughput = min_t(u32, link_throughput, ogm_throughput); 925 ogm_packet->throughput = htonl(path_throughput); 926 927 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node, 928 neigh_node, if_incoming, 929 BATADV_IF_DEFAULT); 930 931 rcu_read_lock(); 932 netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) { 933 if (hard_iface->if_status != BATADV_IF_ACTIVE) 934 continue; 935 936 if (!kref_get_unless_zero(&hard_iface->refcount)) 937 continue; 938 939 ret = batadv_hardif_no_broadcast(hard_iface, 940 ogm_packet->orig, 941 hardif_neigh->orig); 942 943 if (ret) { 944 char *type; 945 946 switch (ret) { 947 case BATADV_HARDIF_BCAST_NORECIPIENT: 948 type = "no neighbor"; 949 break; 950 case BATADV_HARDIF_BCAST_DUPFWD: 951 type = "single neighbor is source"; 952 break; 953 case BATADV_HARDIF_BCAST_DUPORIG: 954 type = "single neighbor is originator"; 955 break; 956 default: 957 type = "unknown"; 958 } 959 960 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n", 961 ogm_packet->orig, hard_iface->net_dev->name, 962 type); 963 964 batadv_hardif_put(hard_iface); 965 continue; 966 } 967 968 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, 969 orig_node, neigh_node, 970 if_incoming, hard_iface); 971 972 batadv_hardif_put(hard_iface); 973 } 974 rcu_read_unlock(); 975 out: 976 batadv_orig_node_put(orig_node); 977 batadv_neigh_node_put(neigh_node); 978 batadv_hardif_neigh_put(hardif_neigh); 979 } 980 981 /** 982 * batadv_v_ogm_packet_recv() - OGM2 receiving handler 983 * @skb: the received OGM 984 * @if_incoming: the interface where this OGM has been received 985 * 986 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP 987 * (freeing the skb) on failure 988 */ 989 int batadv_v_ogm_packet_recv(struct sk_buff *skb, 990 struct batadv_hard_iface *if_incoming) 991 { 992 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface); 993 struct batadv_ogm2_packet *ogm_packet; 994 struct ethhdr *ethhdr; 995 int ogm_offset; 996 u8 *packet_pos; 997 int ret = NET_RX_DROP; 998 999 /* did we receive a OGM2 packet on an interface that does not have 1000 * B.A.T.M.A.N. V enabled ? 1001 */ 1002 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0) 1003 goto free_skb; 1004 1005 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN)) 1006 goto free_skb; 1007 1008 ethhdr = eth_hdr(skb); 1009 if (batadv_is_my_mac(bat_priv, ethhdr->h_source)) 1010 goto free_skb; 1011 1012 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); 1013 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, 1014 skb->len + ETH_HLEN); 1015 1016 ogm_offset = 0; 1017 ogm_packet = (struct batadv_ogm2_packet *)skb->data; 1018 1019 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb), 1020 ogm_packet)) { 1021 batadv_v_ogm_process(skb, ogm_offset, if_incoming); 1022 1023 ogm_offset += BATADV_OGM2_HLEN; 1024 ogm_offset += ntohs(ogm_packet->tvlv_len); 1025 1026 packet_pos = skb->data + ogm_offset; 1027 ogm_packet = (struct batadv_ogm2_packet *)packet_pos; 1028 } 1029 1030 ret = NET_RX_SUCCESS; 1031 1032 free_skb: 1033 if (ret == NET_RX_SUCCESS) 1034 consume_skb(skb); 1035 else 1036 kfree_skb(skb); 1037 1038 return ret; 1039 } 1040 1041 /** 1042 * batadv_v_ogm_init() - initialise the OGM2 engine 1043 * @bat_priv: the bat priv with all the mesh interface information 1044 * 1045 * Return: 0 on success or a negative error code in case of failure 1046 */ 1047 int batadv_v_ogm_init(struct batadv_priv *bat_priv) 1048 { 1049 struct batadv_ogm2_packet *ogm_packet; 1050 unsigned char *ogm_buff; 1051 u32 random_seqno; 1052 1053 bat_priv->bat_v.ogm_buff.len = BATADV_OGM2_HLEN; 1054 bat_priv->bat_v.ogm_buff.capacity = BATADV_OGM2_HLEN; 1055 bat_priv->bat_v.ogm_buff.header_length = BATADV_OGM2_HLEN; 1056 1057 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff.capacity, GFP_ATOMIC); 1058 if (!ogm_buff) 1059 return -ENOMEM; 1060 1061 bat_priv->bat_v.ogm_buff.buf = ogm_buff; 1062 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff; 1063 ogm_packet->packet_type = BATADV_OGM2; 1064 ogm_packet->version = BATADV_COMPAT_VERSION; 1065 ogm_packet->ttl = BATADV_TTL; 1066 ogm_packet->flags = BATADV_NO_FLAGS; 1067 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE); 1068 1069 /* randomize initial seqno to avoid collision */ 1070 get_random_bytes(&random_seqno, sizeof(random_seqno)); 1071 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno); 1072 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send); 1073 1074 mutex_init(&bat_priv->bat_v.ogm_buff_mutex); 1075 1076 return 0; 1077 } 1078 1079 /** 1080 * batadv_v_ogm_free() - free OGM private resources 1081 * @bat_priv: the bat priv with all the mesh interface information 1082 */ 1083 void batadv_v_ogm_free(struct batadv_priv *bat_priv) 1084 { 1085 disable_delayed_work_sync(&bat_priv->bat_v.ogm_wq); 1086 1087 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex); 1088 1089 kfree(bat_priv->bat_v.ogm_buff.buf); 1090 memset(&bat_priv->bat_v.ogm_buff, 0, sizeof(bat_priv->bat_v.ogm_buff)); 1091 1092 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex); 1093 } 1094