1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner, Simon Wunderlich 5 */ 6 7 #include "send.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/container_of.h> 14 #include <linux/errno.h> 15 #include <linux/etherdevice.h> 16 #include <linux/gfp.h> 17 #include <linux/if.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/netdevice.h> 23 #include <linux/printk.h> 24 #include <linux/rcupdate.h> 25 #include <linux/skbuff.h> 26 #include <linux/slab.h> 27 #include <linux/spinlock.h> 28 #include <linux/stddef.h> 29 #include <linux/workqueue.h> 30 31 #include "distributed-arp-table.h" 32 #include "fragmentation.h" 33 #include "gateway_client.h" 34 #include "hard-interface.h" 35 #include "log.h" 36 #include "mesh-interface.h" 37 #include "originator.h" 38 #include "routing.h" 39 #include "translation-table.h" 40 41 static void batadv_send_outstanding_bcast_packet(struct work_struct *work); 42 43 /** 44 * batadv_send_skb_packet() - send an already prepared packet 45 * @skb: the packet to send 46 * @hard_iface: the interface to use to send the broadcast packet 47 * @dst_addr: the payload destination 48 * 49 * Send out an already prepared packet to the given neighbor or broadcast it 50 * using the specified interface. Either hard_iface or neigh_node must be not 51 * NULL. 52 * If neigh_node is NULL, then the packet is broadcasted using hard_iface, 53 * otherwise it is sent as unicast to the given neighbor. 54 * 55 * Regardless of the return value, the skb is consumed. 56 * 57 * Return: A negative errno code is returned on a failure. A success does not 58 * guarantee the frame will be transmitted as it may be dropped due 59 * to congestion or traffic shaping. 60 */ 61 int batadv_send_skb_packet(struct sk_buff *skb, 62 struct batadv_hard_iface *hard_iface, 63 const u8 *dst_addr) 64 { 65 struct ethhdr *ethhdr; 66 int ret; 67 68 if (hard_iface->if_status != BATADV_IF_ACTIVE) 69 goto send_skb_err; 70 71 if (unlikely(!hard_iface->net_dev)) 72 goto send_skb_err; 73 74 if (!(hard_iface->net_dev->flags & IFF_UP)) { 75 pr_warn("Interface %s is not up - can't send packet via that interface!\n", 76 hard_iface->net_dev->name); 77 goto send_skb_err; 78 } 79 80 /* push to the ethernet header. */ 81 if (batadv_skb_head_push(skb, ETH_HLEN) < 0) 82 goto send_skb_err; 83 84 skb_reset_mac_header(skb); 85 86 ethhdr = eth_hdr(skb); 87 ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr); 88 ether_addr_copy(ethhdr->h_dest, dst_addr); 89 ethhdr->h_proto = htons(ETH_P_BATMAN); 90 91 skb_set_network_header(skb, ETH_HLEN); 92 skb->protocol = htons(ETH_P_BATMAN); 93 94 skb->dev = hard_iface->net_dev; 95 96 /* dev_queue_xmit() returns a negative result on error. However on 97 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP 98 * (which is > 0). This will not be treated as an error. 99 */ 100 ret = dev_queue_xmit(skb); 101 return net_xmit_eval(ret); 102 send_skb_err: 103 kfree_skb(skb); 104 return NET_XMIT_DROP; 105 } 106 107 /** 108 * batadv_send_broadcast_skb() - Send broadcast packet via hard interface 109 * @skb: packet to be transmitted (with batadv header and no outer eth header) 110 * @hard_iface: outgoing interface 111 * 112 * Return: A negative errno code is returned on a failure. A success does not 113 * guarantee the frame will be transmitted as it may be dropped due 114 * to congestion or traffic shaping. 115 */ 116 int batadv_send_broadcast_skb(struct sk_buff *skb, 117 struct batadv_hard_iface *hard_iface) 118 { 119 static const u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 120 121 return batadv_send_skb_packet(skb, hard_iface, broadcast_addr); 122 } 123 124 /** 125 * batadv_send_unicast_skb() - Send unicast packet to neighbor 126 * @skb: packet to be transmitted (with batadv header and no outer eth header) 127 * @neigh: neighbor which is used as next hop to destination 128 * 129 * Return: A negative errno code is returned on a failure. A success does not 130 * guarantee the frame will be transmitted as it may be dropped due 131 * to congestion or traffic shaping. 132 */ 133 int batadv_send_unicast_skb(struct sk_buff *skb, 134 struct batadv_neigh_node *neigh) 135 { 136 #ifdef CONFIG_BATMAN_ADV_BATMAN_V 137 struct batadv_hardif_neigh_node *hardif_neigh; 138 #endif 139 int ret; 140 141 ret = batadv_send_skb_packet(skb, neigh->if_incoming, neigh->addr); 142 143 #ifdef CONFIG_BATMAN_ADV_BATMAN_V 144 hardif_neigh = batadv_hardif_neigh_get(neigh->if_incoming, neigh->addr); 145 146 if (hardif_neigh && ret != NET_XMIT_DROP) 147 hardif_neigh->bat_v.last_unicast_tx = jiffies; 148 149 batadv_hardif_neigh_put(hardif_neigh); 150 #endif 151 152 return ret; 153 } 154 155 /** 156 * batadv_send_skb_to_orig() - Lookup next-hop and transmit skb. 157 * @skb: Packet to be transmitted. 158 * @orig_node: Final destination of the packet. 159 * @recv_if: Interface used when receiving the packet (can be NULL). 160 * 161 * Looks up the best next-hop towards the passed originator and passes the 162 * skb on for preparation of MAC header. If the packet originated from this 163 * host, NULL can be passed as recv_if and no interface alternating is 164 * attempted. 165 * 166 * Return: negative errno code on a failure, -EINPROGRESS if the skb is 167 * buffered for later transmit or the NET_XMIT status returned by the 168 * lower routine if the packet has been passed down. 169 */ 170 int batadv_send_skb_to_orig(struct sk_buff *skb, 171 struct batadv_orig_node *orig_node, 172 struct batadv_hard_iface *recv_if) 173 { 174 struct batadv_priv *bat_priv = orig_node->bat_priv; 175 struct batadv_neigh_node *neigh_node; 176 int ret; 177 178 /* batadv_find_router() increases neigh_nodes refcount if found. */ 179 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if); 180 if (!neigh_node) { 181 ret = -EINVAL; 182 goto free_skb; 183 } 184 185 /* Check if the skb is too large to send in one piece and fragment 186 * it if needed. 187 */ 188 if (atomic_read(&bat_priv->fragmentation) && 189 skb->len > neigh_node->if_incoming->net_dev->mtu) { 190 /* Fragment and send packet. */ 191 ret = batadv_frag_send_packet(skb, orig_node, neigh_node); 192 /* skb was consumed */ 193 skb = NULL; 194 195 goto put_neigh_node; 196 } 197 198 ret = batadv_send_unicast_skb(skb, neigh_node); 199 200 /* skb was consumed */ 201 skb = NULL; 202 203 put_neigh_node: 204 batadv_neigh_node_put(neigh_node); 205 free_skb: 206 kfree_skb(skb); 207 208 return ret; 209 } 210 211 /** 212 * batadv_send_skb_push_fill_unicast() - extend the buffer and initialize the 213 * common fields for unicast packets 214 * @skb: the skb carrying the unicast header to initialize 215 * @hdr_size: amount of bytes to push at the beginning of the skb 216 * @orig_node: the destination node 217 * 218 * Return: false if the buffer extension was not possible or true otherwise. 219 */ 220 static bool 221 batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size, 222 struct batadv_orig_node *orig_node) 223 { 224 struct batadv_unicast_packet *unicast_packet; 225 u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn); 226 227 if (batadv_skb_head_push(skb, hdr_size) < 0) 228 return false; 229 230 unicast_packet = (struct batadv_unicast_packet *)skb->data; 231 unicast_packet->version = BATADV_COMPAT_VERSION; 232 /* batman packet type: unicast */ 233 unicast_packet->packet_type = BATADV_UNICAST; 234 /* set unicast ttl */ 235 unicast_packet->ttl = BATADV_TTL; 236 /* copy the destination for faster routing */ 237 ether_addr_copy(unicast_packet->dest, orig_node->orig); 238 /* set the destination tt version number */ 239 unicast_packet->ttvn = ttvn; 240 241 return true; 242 } 243 244 /** 245 * batadv_send_skb_prepare_unicast() - encapsulate an skb with a unicast header 246 * @skb: the skb containing the payload to encapsulate 247 * @orig_node: the destination node 248 * 249 * Return: false if the payload could not be encapsulated or true otherwise. 250 */ 251 static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb, 252 struct batadv_orig_node *orig_node) 253 { 254 size_t uni_size = sizeof(struct batadv_unicast_packet); 255 256 return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node); 257 } 258 259 /** 260 * batadv_send_skb_prepare_unicast_4addr() - encapsulate an skb with a 261 * unicast 4addr header 262 * @bat_priv: the bat priv with all the mesh interface information 263 * @skb: the skb containing the payload to encapsulate 264 * @orig: the destination node 265 * @packet_subtype: the unicast 4addr packet subtype to use 266 * 267 * Return: false if the payload could not be encapsulated or true otherwise. 268 */ 269 bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv, 270 struct sk_buff *skb, 271 struct batadv_orig_node *orig, 272 int packet_subtype) 273 { 274 struct batadv_hard_iface *primary_if; 275 struct batadv_unicast_4addr_packet *uc_4addr_packet; 276 bool ret = false; 277 278 primary_if = batadv_primary_if_get_selected(bat_priv); 279 if (!primary_if) 280 goto out; 281 282 /* Pull the header space and fill the unicast_packet substructure. 283 * We can do that because the first member of the uc_4addr_packet 284 * is of type struct unicast_packet 285 */ 286 if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet), 287 orig)) 288 goto out; 289 290 uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; 291 uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR; 292 ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr); 293 uc_4addr_packet->subtype = packet_subtype; 294 uc_4addr_packet->reserved = 0; 295 296 ret = true; 297 out: 298 batadv_hardif_put(primary_if); 299 return ret; 300 } 301 302 /** 303 * batadv_send_skb_unicast() - encapsulate and send an skb via unicast 304 * @bat_priv: the bat priv with all the mesh interface information 305 * @skb: payload to send 306 * @packet_type: the batman unicast packet type to use 307 * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast 308 * 4addr packets) 309 * @orig_node: the originator to send the packet to 310 * @vid: the vid to be used to search the translation table 311 * 312 * Wrap the given skb into a batman-adv unicast or unicast-4addr header 313 * depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied 314 * as packet_type. Then send this frame to the given orig_node. 315 * 316 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise. 317 */ 318 int batadv_send_skb_unicast(struct batadv_priv *bat_priv, 319 struct sk_buff *skb, int packet_type, 320 int packet_subtype, 321 struct batadv_orig_node *orig_node, 322 unsigned short vid) 323 { 324 struct batadv_unicast_packet *unicast_packet; 325 struct ethhdr *ethhdr; 326 int ret = NET_XMIT_DROP; 327 328 if (!orig_node) 329 goto out; 330 331 switch (packet_type) { 332 case BATADV_UNICAST: 333 if (!batadv_send_skb_prepare_unicast(skb, orig_node)) 334 goto out; 335 break; 336 case BATADV_UNICAST_4ADDR: 337 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb, 338 orig_node, 339 packet_subtype)) 340 goto out; 341 break; 342 default: 343 /* this function supports UNICAST and UNICAST_4ADDR only. It 344 * should never be invoked with any other packet type 345 */ 346 goto out; 347 } 348 349 /* skb->data might have been reallocated by 350 * batadv_send_skb_prepare_unicast{,_4addr}() 351 */ 352 ethhdr = eth_hdr(skb); 353 unicast_packet = (struct batadv_unicast_packet *)skb->data; 354 355 /* inform the destination node that we are still missing a correct route 356 * for this client. The destination will receive this packet and will 357 * try to reroute it because the ttvn contained in the header is less 358 * than the current one 359 */ 360 if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) 361 unicast_packet->ttvn = unicast_packet->ttvn - 1; 362 363 ret = batadv_send_skb_to_orig(skb, orig_node, NULL); 364 /* skb was consumed */ 365 skb = NULL; 366 367 out: 368 kfree_skb(skb); 369 return ret; 370 } 371 372 /** 373 * batadv_send_skb_via_tt_generic() - send an skb via TT lookup 374 * @bat_priv: the bat priv with all the mesh interface information 375 * @skb: payload to send 376 * @packet_type: the batman unicast packet type to use 377 * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast 378 * 4addr packets) 379 * @dst_hint: can be used to override the destination contained in the skb 380 * @vid: the vid to be used to search the translation table 381 * 382 * Look up the recipient node for the destination address in the ethernet 383 * header via the translation table. Wrap the given skb into a batman-adv 384 * unicast or unicast-4addr header depending on whether BATADV_UNICAST or 385 * BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame 386 * to the according destination node. 387 * 388 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise. 389 */ 390 int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv, 391 struct sk_buff *skb, int packet_type, 392 int packet_subtype, u8 *dst_hint, 393 unsigned short vid) 394 { 395 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 396 struct batadv_orig_node *orig_node; 397 u8 *src, *dst; 398 int ret; 399 400 src = ethhdr->h_source; 401 dst = ethhdr->h_dest; 402 403 /* if we got an hint! let's send the packet to this client (if any) */ 404 if (dst_hint) { 405 src = NULL; 406 dst = dst_hint; 407 } 408 orig_node = batadv_transtable_search(bat_priv, src, dst, vid); 409 410 ret = batadv_send_skb_unicast(bat_priv, skb, packet_type, 411 packet_subtype, orig_node, vid); 412 413 batadv_orig_node_put(orig_node); 414 415 return ret; 416 } 417 418 /** 419 * batadv_send_skb_via_gw() - send an skb via gateway lookup 420 * @bat_priv: the bat priv with all the mesh interface information 421 * @skb: payload to send 422 * @vid: the vid to be used to search the translation table 423 * 424 * Look up the currently selected gateway. Wrap the given skb into a batman-adv 425 * unicast header and send this frame to this gateway node. 426 * 427 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise. 428 */ 429 int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb, 430 unsigned short vid) 431 { 432 struct batadv_orig_node *orig_node; 433 int ret; 434 435 orig_node = batadv_gw_get_selected_orig(bat_priv); 436 ret = batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST_4ADDR, 437 BATADV_P_DATA, orig_node, vid); 438 439 batadv_orig_node_put(orig_node); 440 441 return ret; 442 } 443 444 /** 445 * batadv_forw_packet_free() - free a forwarding packet 446 * @forw_packet: The packet to free 447 * @dropped: whether the packet is freed because is dropped 448 * 449 * This frees a forwarding packet and releases any resources it might 450 * have claimed. 451 */ 452 void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet, 453 bool dropped) 454 { 455 if (dropped) 456 kfree_skb(forw_packet->skb); 457 else 458 consume_skb(forw_packet->skb); 459 460 batadv_hardif_put(forw_packet->if_incoming); 461 batadv_hardif_put(forw_packet->if_outgoing); 462 if (forw_packet->queue_left) 463 atomic_inc(forw_packet->queue_left); 464 kfree(forw_packet); 465 } 466 467 /** 468 * batadv_forw_packet_alloc() - allocate a forwarding packet 469 * @if_incoming: The (optional) if_incoming to be grabbed 470 * @if_outgoing: The (optional) if_outgoing to be grabbed 471 * @queue_left: The (optional) queue counter to decrease 472 * @bat_priv: The bat_priv for the mesh of this forw_packet 473 * @skb: The raw packet this forwarding packet shall contain 474 * 475 * Allocates a forwarding packet and tries to get a reference to the 476 * (optional) if_incoming, if_outgoing and queue_left. If queue_left 477 * is NULL then bat_priv is optional, too. 478 * 479 * Return: An allocated forwarding packet on success, NULL otherwise. 480 */ 481 struct batadv_forw_packet * 482 batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming, 483 struct batadv_hard_iface *if_outgoing, 484 atomic_t *queue_left, 485 struct batadv_priv *bat_priv, 486 struct sk_buff *skb) 487 { 488 struct batadv_forw_packet *forw_packet; 489 const char *qname; 490 491 if (queue_left && !batadv_atomic_dec_not_zero(queue_left)) { 492 qname = "unknown"; 493 494 if (queue_left == &bat_priv->bcast_queue_left) 495 qname = "bcast"; 496 497 if (queue_left == &bat_priv->batman_queue_left) 498 qname = "batman"; 499 500 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 501 "%s queue is full\n", qname); 502 503 return NULL; 504 } 505 506 forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC); 507 if (!forw_packet) 508 goto err; 509 510 if (if_incoming) 511 kref_get(&if_incoming->refcount); 512 513 if (if_outgoing) 514 kref_get(&if_outgoing->refcount); 515 516 INIT_HLIST_NODE(&forw_packet->list); 517 INIT_HLIST_NODE(&forw_packet->cleanup_list); 518 forw_packet->skb = skb; 519 forw_packet->queue_left = queue_left; 520 forw_packet->if_incoming = if_incoming; 521 forw_packet->if_outgoing = if_outgoing; 522 forw_packet->num_packets = 1; 523 524 return forw_packet; 525 526 err: 527 if (queue_left) 528 atomic_inc(queue_left); 529 530 return NULL; 531 } 532 533 /** 534 * batadv_forw_packet_was_stolen() - check whether someone stole this packet 535 * @forw_packet: the forwarding packet to check 536 * 537 * This function checks whether the given forwarding packet was claimed by 538 * someone else for free(). 539 * 540 * Return: True if someone stole it, false otherwise. 541 */ 542 static bool 543 batadv_forw_packet_was_stolen(struct batadv_forw_packet *forw_packet) 544 { 545 return !hlist_unhashed(&forw_packet->cleanup_list); 546 } 547 548 /** 549 * batadv_forw_packet_steal() - claim a forw_packet for free() 550 * @forw_packet: the forwarding packet to steal 551 * @lock: a key to the store to steal from (e.g. forw_{bat,bcast}_list_lock) 552 * 553 * This function tries to steal a specific forw_packet from global 554 * visibility for the purpose of getting it for free(). That means 555 * the caller is *not* allowed to requeue it afterwards. 556 * 557 * Return: True if stealing was successful. False if someone else stole it 558 * before us. 559 */ 560 bool batadv_forw_packet_steal(struct batadv_forw_packet *forw_packet, 561 spinlock_t *lock) 562 { 563 /* did purging routine steal it earlier? */ 564 spin_lock_bh(lock); 565 if (batadv_forw_packet_was_stolen(forw_packet)) { 566 spin_unlock_bh(lock); 567 return false; 568 } 569 570 hlist_del_init(&forw_packet->list); 571 572 /* Just to spot misuse of this function */ 573 hlist_add_fake(&forw_packet->cleanup_list); 574 575 spin_unlock_bh(lock); 576 return true; 577 } 578 579 /** 580 * batadv_forw_packet_list_steal() - claim a list of forward packets for free() 581 * @forw_list: the to be stolen forward packets 582 * @cleanup_list: a backup pointer, to be able to dispose the packet later 583 * @hard_iface: the interface to steal forward packets from 584 * 585 * This function claims responsibility to free any forw_packet queued on the 586 * given hard_iface. If hard_iface is NULL forwarding packets on all hard 587 * interfaces will be claimed. 588 * 589 * The packets are being moved from the forw_list to the cleanup_list. This 590 * makes it possible for already running threads to notice the claim. 591 */ 592 static void 593 batadv_forw_packet_list_steal(struct hlist_head *forw_list, 594 struct hlist_head *cleanup_list, 595 const struct batadv_hard_iface *hard_iface) 596 { 597 struct batadv_forw_packet *forw_packet; 598 struct hlist_node *safe_tmp_node; 599 600 hlist_for_each_entry_safe(forw_packet, safe_tmp_node, 601 forw_list, list) { 602 /* if purge_outstanding_packets() was called with an argument 603 * we delete only packets belonging to the given interface 604 */ 605 if (hard_iface && 606 forw_packet->if_incoming != hard_iface && 607 forw_packet->if_outgoing != hard_iface) 608 continue; 609 610 hlist_del(&forw_packet->list); 611 hlist_add_head(&forw_packet->cleanup_list, cleanup_list); 612 } 613 } 614 615 /** 616 * batadv_forw_packet_list_free() - free a list of forward packets 617 * @head: a list of to be freed forw_packets 618 * 619 * This function cancels the scheduling of any packet in the provided list, 620 * waits for any possibly running packet forwarding thread to finish and 621 * finally, safely frees this forward packet. 622 * 623 * This function might sleep. 624 */ 625 static void batadv_forw_packet_list_free(struct hlist_head *head) 626 { 627 struct batadv_forw_packet *forw_packet; 628 struct hlist_node *safe_tmp_node; 629 630 hlist_for_each_entry_safe(forw_packet, safe_tmp_node, head, 631 cleanup_list) { 632 cancel_delayed_work_sync(&forw_packet->delayed_work); 633 634 hlist_del(&forw_packet->cleanup_list); 635 batadv_forw_packet_free(forw_packet, true); 636 } 637 } 638 639 /** 640 * batadv_forw_packet_queue() - try to queue a forwarding packet 641 * @forw_packet: the forwarding packet to queue 642 * @lock: a key to the store (e.g. forw_{bat,bcast}_list_lock) 643 * @head: the shelve to queue it on (e.g. forw_{bat,bcast}_list) 644 * @send_time: timestamp (jiffies) when the packet is to be sent 645 * 646 * This function tries to (re)queue a forwarding packet. Requeuing 647 * is prevented if the according interface is shutting down 648 * (e.g. if batadv_forw_packet_list_steal() was called for this 649 * packet earlier). 650 * 651 * Calling batadv_forw_packet_queue() after a call to 652 * batadv_forw_packet_steal() is forbidden! 653 * 654 * Caller needs to ensure that forw_packet->delayed_work was initialized. 655 */ 656 static void batadv_forw_packet_queue(struct batadv_forw_packet *forw_packet, 657 spinlock_t *lock, struct hlist_head *head, 658 unsigned long send_time) 659 { 660 spin_lock_bh(lock); 661 662 /* did purging routine steal it from us? */ 663 if (batadv_forw_packet_was_stolen(forw_packet)) { 664 /* If you got it for free() without trouble, then 665 * don't get back into the queue after stealing... 666 */ 667 WARN_ONCE(hlist_fake(&forw_packet->cleanup_list), 668 "Requeuing after batadv_forw_packet_steal() not allowed!\n"); 669 670 spin_unlock_bh(lock); 671 return; 672 } 673 674 hlist_del_init(&forw_packet->list); 675 hlist_add_head(&forw_packet->list, head); 676 677 queue_delayed_work(batadv_event_workqueue, 678 &forw_packet->delayed_work, 679 send_time - jiffies); 680 spin_unlock_bh(lock); 681 } 682 683 /** 684 * batadv_forw_packet_bcast_queue() - try to queue a broadcast packet 685 * @bat_priv: the bat priv with all the mesh interface information 686 * @forw_packet: the forwarding packet to queue 687 * @send_time: timestamp (jiffies) when the packet is to be sent 688 * 689 * This function tries to (re)queue a broadcast packet. 690 * 691 * Caller needs to ensure that forw_packet->delayed_work was initialized. 692 */ 693 static void 694 batadv_forw_packet_bcast_queue(struct batadv_priv *bat_priv, 695 struct batadv_forw_packet *forw_packet, 696 unsigned long send_time) 697 { 698 batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bcast_list_lock, 699 &bat_priv->forw_bcast_list, send_time); 700 } 701 702 /** 703 * batadv_forw_packet_ogmv1_queue() - try to queue an OGMv1 packet 704 * @bat_priv: the bat priv with all the mesh interface information 705 * @forw_packet: the forwarding packet to queue 706 * @send_time: timestamp (jiffies) when the packet is to be sent 707 * 708 * This function tries to (re)queue an OGMv1 packet. 709 * 710 * Caller needs to ensure that forw_packet->delayed_work was initialized. 711 */ 712 void batadv_forw_packet_ogmv1_queue(struct batadv_priv *bat_priv, 713 struct batadv_forw_packet *forw_packet, 714 unsigned long send_time) 715 { 716 batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bat_list_lock, 717 &bat_priv->forw_bat_list, send_time); 718 } 719 720 /** 721 * batadv_forw_bcast_packet_to_list() - queue broadcast packet for transmissions 722 * @bat_priv: the bat priv with all the mesh interface information 723 * @skb: broadcast packet to add 724 * @delay: number of jiffies to wait before sending 725 * @own_packet: true if it is a self-generated broadcast packet 726 * @if_in: the interface where the packet was received on 727 * @if_out: the outgoing interface to queue on 728 * 729 * Adds a broadcast packet to the queue and sets up timers. Broadcast packets 730 * are sent multiple times to increase probability for being received. 731 * 732 * This call clones the given skb, hence the caller needs to take into 733 * account that the data segment of the original skb might not be 734 * modifiable anymore. 735 * 736 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors. 737 */ 738 static int batadv_forw_bcast_packet_to_list(struct batadv_priv *bat_priv, 739 struct sk_buff *skb, 740 unsigned long delay, 741 bool own_packet, 742 struct batadv_hard_iface *if_in, 743 struct batadv_hard_iface *if_out) 744 { 745 struct batadv_forw_packet *forw_packet; 746 unsigned long send_time = jiffies; 747 struct sk_buff *newskb; 748 749 newskb = skb_clone(skb, GFP_ATOMIC); 750 if (!newskb) 751 goto err; 752 753 forw_packet = batadv_forw_packet_alloc(if_in, if_out, 754 &bat_priv->bcast_queue_left, 755 bat_priv, newskb); 756 if (!forw_packet) 757 goto err_packet_free; 758 759 forw_packet->own = own_packet; 760 761 INIT_DELAYED_WORK(&forw_packet->delayed_work, 762 batadv_send_outstanding_bcast_packet); 763 764 send_time += delay ? delay : msecs_to_jiffies(5); 765 766 batadv_forw_packet_bcast_queue(bat_priv, forw_packet, send_time); 767 return NETDEV_TX_OK; 768 769 err_packet_free: 770 kfree_skb(newskb); 771 err: 772 return NETDEV_TX_BUSY; 773 } 774 775 /** 776 * batadv_forw_bcast_packet_if() - forward and queue a broadcast packet 777 * @bat_priv: the bat priv with all the mesh interface information 778 * @skb: broadcast packet to add 779 * @delay: number of jiffies to wait before sending 780 * @own_packet: true if it is a self-generated broadcast packet 781 * @if_in: the interface where the packet was received on 782 * @if_out: the outgoing interface to forward to 783 * 784 * Transmits a broadcast packet on the specified interface either immediately 785 * or if a delay is given after that. Furthermore, queues additional 786 * retransmissions if this interface is a wireless one. 787 * 788 * This call clones the given skb, hence the caller needs to take into 789 * account that the data segment of the original skb might not be 790 * modifiable anymore. 791 * 792 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors. 793 */ 794 static int batadv_forw_bcast_packet_if(struct batadv_priv *bat_priv, 795 struct sk_buff *skb, 796 unsigned long delay, 797 bool own_packet, 798 struct batadv_hard_iface *if_in, 799 struct batadv_hard_iface *if_out) 800 { 801 unsigned int num_bcasts = if_out->num_bcasts; 802 struct sk_buff *newskb; 803 int ret = NETDEV_TX_OK; 804 805 if (!delay) { 806 newskb = skb_clone(skb, GFP_ATOMIC); 807 if (!newskb) 808 return NETDEV_TX_BUSY; 809 810 batadv_send_broadcast_skb(newskb, if_out); 811 num_bcasts--; 812 } 813 814 /* delayed broadcast or rebroadcasts? */ 815 if (num_bcasts >= 1) { 816 BATADV_SKB_CB(skb)->num_bcasts = num_bcasts; 817 818 ret = batadv_forw_bcast_packet_to_list(bat_priv, skb, delay, 819 own_packet, if_in, 820 if_out); 821 } 822 823 return ret; 824 } 825 826 /** 827 * batadv_send_no_broadcast() - check whether (re)broadcast is necessary 828 * @bat_priv: the bat priv with all the mesh interface information 829 * @skb: broadcast packet to check 830 * @own_packet: true if it is a self-generated broadcast packet 831 * @if_out: the outgoing interface checked and considered for (re)broadcast 832 * 833 * Return: False if a packet needs to be (re)broadcasted on the given interface, 834 * true otherwise. 835 */ 836 static bool batadv_send_no_broadcast(struct batadv_priv *bat_priv, 837 struct sk_buff *skb, bool own_packet, 838 struct batadv_hard_iface *if_out) 839 { 840 struct batadv_hardif_neigh_node *neigh_node = NULL; 841 struct batadv_bcast_packet *bcast_packet; 842 u8 *orig_neigh; 843 u8 *neigh_addr; 844 char *type; 845 int ret; 846 847 if (!own_packet) { 848 neigh_addr = eth_hdr(skb)->h_source; 849 neigh_node = batadv_hardif_neigh_get(if_out, 850 neigh_addr); 851 } 852 853 bcast_packet = (struct batadv_bcast_packet *)skb->data; 854 orig_neigh = neigh_node ? neigh_node->orig : NULL; 855 856 ret = batadv_hardif_no_broadcast(if_out, bcast_packet->orig, 857 orig_neigh); 858 859 batadv_hardif_neigh_put(neigh_node); 860 861 /* ok, may broadcast */ 862 if (!ret) 863 return false; 864 865 /* no broadcast */ 866 switch (ret) { 867 case BATADV_HARDIF_BCAST_NORECIPIENT: 868 type = "no neighbor"; 869 break; 870 case BATADV_HARDIF_BCAST_DUPFWD: 871 type = "single neighbor is source"; 872 break; 873 case BATADV_HARDIF_BCAST_DUPORIG: 874 type = "single neighbor is originator"; 875 break; 876 default: 877 type = "unknown"; 878 } 879 880 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 881 "BCAST packet from orig %pM on %s suppressed: %s\n", 882 bcast_packet->orig, 883 if_out->net_dev->name, type); 884 885 return true; 886 } 887 888 /** 889 * __batadv_forw_bcast_packet() - forward and queue a broadcast packet 890 * @bat_priv: the bat priv with all the mesh interface information 891 * @skb: broadcast packet to add 892 * @delay: number of jiffies to wait before sending 893 * @own_packet: true if it is a self-generated broadcast packet 894 * 895 * Transmits a broadcast packet either immediately or if a delay is given 896 * after that. Furthermore, queues additional retransmissions on wireless 897 * interfaces. 898 * 899 * This call clones the given skb, hence the caller needs to take into 900 * account that the data segment of the given skb might not be 901 * modifiable anymore. 902 * 903 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors. 904 */ 905 static int __batadv_forw_bcast_packet(struct batadv_priv *bat_priv, 906 struct sk_buff *skb, 907 unsigned long delay, 908 bool own_packet) 909 { 910 struct batadv_hard_iface *hard_iface; 911 struct batadv_hard_iface *primary_if; 912 struct list_head *iter; 913 int ret = NETDEV_TX_OK; 914 915 primary_if = batadv_primary_if_get_selected(bat_priv); 916 if (!primary_if) 917 return NETDEV_TX_BUSY; 918 919 rcu_read_lock(); 920 netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) { 921 if (!kref_get_unless_zero(&hard_iface->refcount)) 922 continue; 923 924 if (batadv_send_no_broadcast(bat_priv, skb, own_packet, 925 hard_iface)) { 926 batadv_hardif_put(hard_iface); 927 continue; 928 } 929 930 ret = batadv_forw_bcast_packet_if(bat_priv, skb, delay, 931 own_packet, primary_if, 932 hard_iface); 933 batadv_hardif_put(hard_iface); 934 935 if (ret == NETDEV_TX_BUSY) 936 break; 937 } 938 rcu_read_unlock(); 939 940 batadv_hardif_put(primary_if); 941 return ret; 942 } 943 944 /** 945 * batadv_forw_bcast_packet() - forward and queue a broadcast packet 946 * @bat_priv: the bat priv with all the mesh interface information 947 * @skb: broadcast packet to add 948 * @delay: number of jiffies to wait before sending 949 * @own_packet: true if it is a self-generated broadcast packet 950 * 951 * Transmits a broadcast packet either immediately or if a delay is given 952 * after that. Furthermore, queues additional retransmissions on wireless 953 * interfaces. 954 * 955 * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors. 956 */ 957 int batadv_forw_bcast_packet(struct batadv_priv *bat_priv, 958 struct sk_buff *skb, 959 unsigned long delay, 960 bool own_packet) 961 { 962 return __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet); 963 } 964 965 /** 966 * batadv_send_bcast_packet() - send and queue a broadcast packet 967 * @bat_priv: the bat priv with all the mesh interface information 968 * @skb: broadcast packet to add 969 * @delay: number of jiffies to wait before sending 970 * @own_packet: true if it is a self-generated broadcast packet 971 * 972 * Transmits a broadcast packet either immediately or if a delay is given 973 * after that. Furthermore, queues additional retransmissions on wireless 974 * interfaces. 975 * 976 * Consumes the provided skb. 977 */ 978 void batadv_send_bcast_packet(struct batadv_priv *bat_priv, 979 struct sk_buff *skb, 980 unsigned long delay, 981 bool own_packet) 982 { 983 __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet); 984 consume_skb(skb); 985 } 986 987 /** 988 * batadv_forw_packet_bcasts_left() - check if a retransmission is necessary 989 * @forw_packet: the forwarding packet to check 990 * 991 * Checks whether a given packet has any (re)transmissions left on the provided 992 * interface. 993 * 994 * hard_iface may be NULL: In that case the number of transmissions this skb had 995 * so far is compared with the maximum amount of retransmissions independent of 996 * any interface instead. 997 * 998 * Return: True if (re)transmissions are left, false otherwise. 999 */ 1000 static bool 1001 batadv_forw_packet_bcasts_left(struct batadv_forw_packet *forw_packet) 1002 { 1003 return BATADV_SKB_CB(forw_packet->skb)->num_bcasts; 1004 } 1005 1006 /** 1007 * batadv_forw_packet_bcasts_dec() - decrement retransmission counter of a 1008 * packet 1009 * @forw_packet: the packet to decrease the counter for 1010 */ 1011 static void 1012 batadv_forw_packet_bcasts_dec(struct batadv_forw_packet *forw_packet) 1013 { 1014 BATADV_SKB_CB(forw_packet->skb)->num_bcasts--; 1015 } 1016 1017 /** 1018 * batadv_forw_packet_is_rebroadcast() - check packet for previous transmissions 1019 * @forw_packet: the packet to check 1020 * 1021 * Return: True if this packet was transmitted before, false otherwise. 1022 */ 1023 bool batadv_forw_packet_is_rebroadcast(struct batadv_forw_packet *forw_packet) 1024 { 1025 unsigned char num_bcasts = BATADV_SKB_CB(forw_packet->skb)->num_bcasts; 1026 1027 return num_bcasts != forw_packet->if_outgoing->num_bcasts; 1028 } 1029 1030 /** 1031 * batadv_send_outstanding_bcast_packet() - transmit a queued broadcast packet 1032 * @work: work queue item 1033 * 1034 * Transmits a queued broadcast packet and if necessary reschedules it. 1035 */ 1036 static void batadv_send_outstanding_bcast_packet(struct work_struct *work) 1037 { 1038 unsigned long send_time = jiffies + msecs_to_jiffies(5); 1039 struct batadv_forw_packet *forw_packet; 1040 struct delayed_work *delayed_work; 1041 struct batadv_priv *bat_priv; 1042 struct sk_buff *skb1; 1043 bool dropped = false; 1044 1045 delayed_work = to_delayed_work(work); 1046 forw_packet = container_of(delayed_work, struct batadv_forw_packet, 1047 delayed_work); 1048 bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface); 1049 1050 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) { 1051 dropped = true; 1052 goto out; 1053 } 1054 1055 if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) { 1056 dropped = true; 1057 goto out; 1058 } 1059 1060 /* send a copy of the saved skb */ 1061 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC); 1062 if (!skb1) 1063 goto out; 1064 1065 batadv_send_broadcast_skb(skb1, forw_packet->if_outgoing); 1066 batadv_forw_packet_bcasts_dec(forw_packet); 1067 1068 if (batadv_forw_packet_bcasts_left(forw_packet)) { 1069 batadv_forw_packet_bcast_queue(bat_priv, forw_packet, 1070 send_time); 1071 return; 1072 } 1073 1074 out: 1075 /* do we get something for free()? */ 1076 if (batadv_forw_packet_steal(forw_packet, 1077 &bat_priv->forw_bcast_list_lock)) 1078 batadv_forw_packet_free(forw_packet, dropped); 1079 } 1080 1081 /** 1082 * batadv_purge_outstanding_packets() - stop/purge scheduled bcast/OGMv1 packets 1083 * @bat_priv: the bat priv with all the mesh interface information 1084 * @hard_iface: the hard interface to cancel and purge bcast/ogm packets on 1085 * 1086 * This method cancels and purges any broadcast and OGMv1 packet on the given 1087 * hard_iface. If hard_iface is NULL, broadcast and OGMv1 packets on all hard 1088 * interfaces will be canceled and purged. 1089 * 1090 * This function might sleep. 1091 */ 1092 void 1093 batadv_purge_outstanding_packets(struct batadv_priv *bat_priv, 1094 const struct batadv_hard_iface *hard_iface) 1095 { 1096 struct hlist_head head = HLIST_HEAD_INIT; 1097 1098 if (hard_iface) 1099 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1100 "%s(): %s\n", 1101 __func__, hard_iface->net_dev->name); 1102 else 1103 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1104 "%s()\n", __func__); 1105 1106 /* claim bcast list for free() */ 1107 spin_lock_bh(&bat_priv->forw_bcast_list_lock); 1108 batadv_forw_packet_list_steal(&bat_priv->forw_bcast_list, &head, 1109 hard_iface); 1110 spin_unlock_bh(&bat_priv->forw_bcast_list_lock); 1111 1112 /* claim batman packet list for free() */ 1113 spin_lock_bh(&bat_priv->forw_bat_list_lock); 1114 batadv_forw_packet_list_steal(&bat_priv->forw_bat_list, &head, 1115 hard_iface); 1116 spin_unlock_bh(&bat_priv->forw_bat_list_lock); 1117 1118 /* then cancel or wait for packet workers to finish and free */ 1119 batadv_forw_packet_list_free(&head); 1120 } 1121