// SPDX-License-Identifier: GPL-2.01/* Copyright (C) B.A.T.M.A.N. contributors:2*3* Marek Lindner, Simon Wunderlich4*/56#include "send.h"7#include "main.h"89#include <linux/atomic.h>10#include <linux/bug.h>11#include <linux/byteorder/generic.h>12#include <linux/container_of.h>13#include <linux/errno.h>14#include <linux/etherdevice.h>15#include <linux/gfp.h>16#include <linux/if.h>17#include <linux/if_ether.h>18#include <linux/jiffies.h>19#include <linux/kref.h>20#include <linux/list.h>21#include <linux/netdevice.h>22#include <linux/printk.h>23#include <linux/rcupdate.h>24#include <linux/skbuff.h>25#include <linux/slab.h>26#include <linux/spinlock.h>27#include <linux/stddef.h>28#include <linux/workqueue.h>2930#include "distributed-arp-table.h"31#include "fragmentation.h"32#include "gateway_client.h"33#include "hard-interface.h"34#include "log.h"35#include "mesh-interface.h"36#include "originator.h"37#include "routing.h"38#include "translation-table.h"3940static void batadv_send_outstanding_bcast_packet(struct work_struct *work);4142/**43* batadv_send_skb_packet() - send an already prepared packet44* @skb: the packet to send45* @hard_iface: the interface to use to send the broadcast packet46* @dst_addr: the payload destination47*48* Send out an already prepared packet to the given neighbor or broadcast it49* using the specified interface. Either hard_iface or neigh_node must be not50* NULL.51* If neigh_node is NULL, then the packet is broadcasted using hard_iface,52* otherwise it is sent as unicast to the given neighbor.53*54* Regardless of the return value, the skb is consumed.55*56* Return: A negative errno code is returned on a failure. A success does not57* guarantee the frame will be transmitted as it may be dropped due58* to congestion or traffic shaping.59*/60int batadv_send_skb_packet(struct sk_buff *skb,61struct batadv_hard_iface *hard_iface,62const u8 *dst_addr)63{64struct ethhdr *ethhdr;65int ret;6667if (hard_iface->if_status != BATADV_IF_ACTIVE)68goto send_skb_err;6970if (unlikely(!hard_iface->net_dev))71goto send_skb_err;7273if (!(hard_iface->net_dev->flags & IFF_UP)) {74pr_warn("Interface %s is not up - can't send packet via that interface!\n",75hard_iface->net_dev->name);76goto send_skb_err;77}7879/* push to the ethernet header. */80if (batadv_skb_head_push(skb, ETH_HLEN) < 0)81goto send_skb_err;8283skb_reset_mac_header(skb);8485ethhdr = eth_hdr(skb);86ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr);87ether_addr_copy(ethhdr->h_dest, dst_addr);88ethhdr->h_proto = htons(ETH_P_BATMAN);8990skb_set_network_header(skb, ETH_HLEN);91skb->protocol = htons(ETH_P_BATMAN);9293skb->dev = hard_iface->net_dev;9495/* dev_queue_xmit() returns a negative result on error. However on96* congestion and traffic shaping, it drops and returns NET_XMIT_DROP97* (which is > 0). This will not be treated as an error.98*/99ret = dev_queue_xmit(skb);100return net_xmit_eval(ret);101send_skb_err:102kfree_skb(skb);103return NET_XMIT_DROP;104}105106/**107* batadv_send_broadcast_skb() - Send broadcast packet via hard interface108* @skb: packet to be transmitted (with batadv header and no outer eth header)109* @hard_iface: outgoing interface110*111* Return: A negative errno code is returned on a failure. A success does not112* guarantee the frame will be transmitted as it may be dropped due113* to congestion or traffic shaping.114*/115int batadv_send_broadcast_skb(struct sk_buff *skb,116struct batadv_hard_iface *hard_iface)117{118static const u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};119120return batadv_send_skb_packet(skb, hard_iface, broadcast_addr);121}122123/**124* batadv_send_unicast_skb() - Send unicast packet to neighbor125* @skb: packet to be transmitted (with batadv header and no outer eth header)126* @neigh: neighbor which is used as next hop to destination127*128* Return: A negative errno code is returned on a failure. A success does not129* guarantee the frame will be transmitted as it may be dropped due130* to congestion or traffic shaping.131*/132int batadv_send_unicast_skb(struct sk_buff *skb,133struct batadv_neigh_node *neigh)134{135#ifdef CONFIG_BATMAN_ADV_BATMAN_V136struct batadv_hardif_neigh_node *hardif_neigh;137#endif138int ret;139140ret = batadv_send_skb_packet(skb, neigh->if_incoming, neigh->addr);141142#ifdef CONFIG_BATMAN_ADV_BATMAN_V143hardif_neigh = batadv_hardif_neigh_get(neigh->if_incoming, neigh->addr);144145if (hardif_neigh && ret != NET_XMIT_DROP)146hardif_neigh->bat_v.last_unicast_tx = jiffies;147148batadv_hardif_neigh_put(hardif_neigh);149#endif150151return ret;152}153154/**155* batadv_send_skb_to_orig() - Lookup next-hop and transmit skb.156* @skb: Packet to be transmitted.157* @orig_node: Final destination of the packet.158* @recv_if: Interface used when receiving the packet (can be NULL).159*160* Looks up the best next-hop towards the passed originator and passes the161* skb on for preparation of MAC header. If the packet originated from this162* host, NULL can be passed as recv_if and no interface alternating is163* attempted.164*165* Return: negative errno code on a failure, -EINPROGRESS if the skb is166* buffered for later transmit or the NET_XMIT status returned by the167* lower routine if the packet has been passed down.168*/169int batadv_send_skb_to_orig(struct sk_buff *skb,170struct batadv_orig_node *orig_node,171struct batadv_hard_iface *recv_if)172{173struct batadv_priv *bat_priv = orig_node->bat_priv;174struct batadv_neigh_node *neigh_node;175int ret;176177/* batadv_find_router() increases neigh_nodes refcount if found. */178neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);179if (!neigh_node) {180ret = -EINVAL;181goto free_skb;182}183184/* Check if the skb is too large to send in one piece and fragment185* it if needed.186*/187if (atomic_read(&bat_priv->fragmentation) &&188skb->len > neigh_node->if_incoming->net_dev->mtu) {189/* Fragment and send packet. */190ret = batadv_frag_send_packet(skb, orig_node, neigh_node);191/* skb was consumed */192skb = NULL;193194goto put_neigh_node;195}196197ret = batadv_send_unicast_skb(skb, neigh_node);198199/* skb was consumed */200skb = NULL;201202put_neigh_node:203batadv_neigh_node_put(neigh_node);204free_skb:205kfree_skb(skb);206207return ret;208}209210/**211* batadv_send_skb_push_fill_unicast() - extend the buffer and initialize the212* common fields for unicast packets213* @skb: the skb carrying the unicast header to initialize214* @hdr_size: amount of bytes to push at the beginning of the skb215* @orig_node: the destination node216*217* Return: false if the buffer extension was not possible or true otherwise.218*/219static bool220batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,221struct batadv_orig_node *orig_node)222{223struct batadv_unicast_packet *unicast_packet;224u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn);225226if (batadv_skb_head_push(skb, hdr_size) < 0)227return false;228229unicast_packet = (struct batadv_unicast_packet *)skb->data;230unicast_packet->version = BATADV_COMPAT_VERSION;231/* batman packet type: unicast */232unicast_packet->packet_type = BATADV_UNICAST;233/* set unicast ttl */234unicast_packet->ttl = BATADV_TTL;235/* copy the destination for faster routing */236ether_addr_copy(unicast_packet->dest, orig_node->orig);237/* set the destination tt version number */238unicast_packet->ttvn = ttvn;239240return true;241}242243/**244* batadv_send_skb_prepare_unicast() - encapsulate an skb with a unicast header245* @skb: the skb containing the payload to encapsulate246* @orig_node: the destination node247*248* Return: false if the payload could not be encapsulated or true otherwise.249*/250static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,251struct batadv_orig_node *orig_node)252{253size_t uni_size = sizeof(struct batadv_unicast_packet);254255return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);256}257258/**259* batadv_send_skb_prepare_unicast_4addr() - encapsulate an skb with a260* unicast 4addr header261* @bat_priv: the bat priv with all the mesh interface information262* @skb: the skb containing the payload to encapsulate263* @orig: the destination node264* @packet_subtype: the unicast 4addr packet subtype to use265*266* Return: false if the payload could not be encapsulated or true otherwise.267*/268bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,269struct sk_buff *skb,270struct batadv_orig_node *orig,271int packet_subtype)272{273struct batadv_hard_iface *primary_if;274struct batadv_unicast_4addr_packet *uc_4addr_packet;275bool ret = false;276277primary_if = batadv_primary_if_get_selected(bat_priv);278if (!primary_if)279goto out;280281/* Pull the header space and fill the unicast_packet substructure.282* We can do that because the first member of the uc_4addr_packet283* is of type struct unicast_packet284*/285if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),286orig))287goto out;288289uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;290uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;291ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr);292uc_4addr_packet->subtype = packet_subtype;293uc_4addr_packet->reserved = 0;294295ret = true;296out:297batadv_hardif_put(primary_if);298return ret;299}300301/**302* batadv_send_skb_unicast() - encapsulate and send an skb via unicast303* @bat_priv: the bat priv with all the mesh interface information304* @skb: payload to send305* @packet_type: the batman unicast packet type to use306* @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast307* 4addr packets)308* @orig_node: the originator to send the packet to309* @vid: the vid to be used to search the translation table310*311* Wrap the given skb into a batman-adv unicast or unicast-4addr header312* depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied313* as packet_type. Then send this frame to the given orig_node.314*315* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.316*/317int batadv_send_skb_unicast(struct batadv_priv *bat_priv,318struct sk_buff *skb, int packet_type,319int packet_subtype,320struct batadv_orig_node *orig_node,321unsigned short vid)322{323struct batadv_unicast_packet *unicast_packet;324struct ethhdr *ethhdr;325int ret = NET_XMIT_DROP;326327if (!orig_node)328goto out;329330switch (packet_type) {331case BATADV_UNICAST:332if (!batadv_send_skb_prepare_unicast(skb, orig_node))333goto out;334break;335case BATADV_UNICAST_4ADDR:336if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,337orig_node,338packet_subtype))339goto out;340break;341default:342/* this function supports UNICAST and UNICAST_4ADDR only. It343* should never be invoked with any other packet type344*/345goto out;346}347348/* skb->data might have been reallocated by349* batadv_send_skb_prepare_unicast{,_4addr}()350*/351ethhdr = eth_hdr(skb);352unicast_packet = (struct batadv_unicast_packet *)skb->data;353354/* inform the destination node that we are still missing a correct route355* for this client. The destination will receive this packet and will356* try to reroute it because the ttvn contained in the header is less357* than the current one358*/359if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))360unicast_packet->ttvn = unicast_packet->ttvn - 1;361362ret = batadv_send_skb_to_orig(skb, orig_node, NULL);363/* skb was consumed */364skb = NULL;365366out:367kfree_skb(skb);368return ret;369}370371/**372* batadv_send_skb_via_tt_generic() - send an skb via TT lookup373* @bat_priv: the bat priv with all the mesh interface information374* @skb: payload to send375* @packet_type: the batman unicast packet type to use376* @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast377* 4addr packets)378* @dst_hint: can be used to override the destination contained in the skb379* @vid: the vid to be used to search the translation table380*381* Look up the recipient node for the destination address in the ethernet382* header via the translation table. Wrap the given skb into a batman-adv383* unicast or unicast-4addr header depending on whether BATADV_UNICAST or384* BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame385* to the according destination node.386*387* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.388*/389int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,390struct sk_buff *skb, int packet_type,391int packet_subtype, u8 *dst_hint,392unsigned short vid)393{394struct ethhdr *ethhdr = (struct ethhdr *)skb->data;395struct batadv_orig_node *orig_node;396u8 *src, *dst;397int ret;398399src = ethhdr->h_source;400dst = ethhdr->h_dest;401402/* if we got an hint! let's send the packet to this client (if any) */403if (dst_hint) {404src = NULL;405dst = dst_hint;406}407orig_node = batadv_transtable_search(bat_priv, src, dst, vid);408409ret = batadv_send_skb_unicast(bat_priv, skb, packet_type,410packet_subtype, orig_node, vid);411412batadv_orig_node_put(orig_node);413414return ret;415}416417/**418* batadv_send_skb_via_gw() - send an skb via gateway lookup419* @bat_priv: the bat priv with all the mesh interface information420* @skb: payload to send421* @vid: the vid to be used to search the translation table422*423* Look up the currently selected gateway. Wrap the given skb into a batman-adv424* unicast header and send this frame to this gateway node.425*426* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.427*/428int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,429unsigned short vid)430{431struct batadv_orig_node *orig_node;432int ret;433434orig_node = batadv_gw_get_selected_orig(bat_priv);435ret = batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST_4ADDR,436BATADV_P_DATA, orig_node, vid);437438batadv_orig_node_put(orig_node);439440return ret;441}442443/**444* batadv_forw_packet_free() - free a forwarding packet445* @forw_packet: The packet to free446* @dropped: whether the packet is freed because is dropped447*448* This frees a forwarding packet and releases any resources it might449* have claimed.450*/451void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,452bool dropped)453{454if (dropped)455kfree_skb(forw_packet->skb);456else457consume_skb(forw_packet->skb);458459batadv_hardif_put(forw_packet->if_incoming);460batadv_hardif_put(forw_packet->if_outgoing);461if (forw_packet->queue_left)462atomic_inc(forw_packet->queue_left);463kfree(forw_packet);464}465466/**467* batadv_forw_packet_alloc() - allocate a forwarding packet468* @if_incoming: The (optional) if_incoming to be grabbed469* @if_outgoing: The (optional) if_outgoing to be grabbed470* @queue_left: The (optional) queue counter to decrease471* @bat_priv: The bat_priv for the mesh of this forw_packet472* @skb: The raw packet this forwarding packet shall contain473*474* Allocates a forwarding packet and tries to get a reference to the475* (optional) if_incoming, if_outgoing and queue_left. If queue_left476* is NULL then bat_priv is optional, too.477*478* Return: An allocated forwarding packet on success, NULL otherwise.479*/480struct batadv_forw_packet *481batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,482struct batadv_hard_iface *if_outgoing,483atomic_t *queue_left,484struct batadv_priv *bat_priv,485struct sk_buff *skb)486{487struct batadv_forw_packet *forw_packet;488const char *qname;489490if (queue_left && !batadv_atomic_dec_not_zero(queue_left)) {491qname = "unknown";492493if (queue_left == &bat_priv->bcast_queue_left)494qname = "bcast";495496if (queue_left == &bat_priv->batman_queue_left)497qname = "batman";498499batadv_dbg(BATADV_DBG_BATMAN, bat_priv,500"%s queue is full\n", qname);501502return NULL;503}504505forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);506if (!forw_packet)507goto err;508509if (if_incoming)510kref_get(&if_incoming->refcount);511512if (if_outgoing)513kref_get(&if_outgoing->refcount);514515INIT_HLIST_NODE(&forw_packet->list);516INIT_HLIST_NODE(&forw_packet->cleanup_list);517forw_packet->skb = skb;518forw_packet->queue_left = queue_left;519forw_packet->if_incoming = if_incoming;520forw_packet->if_outgoing = if_outgoing;521forw_packet->num_packets = 1;522523return forw_packet;524525err:526if (queue_left)527atomic_inc(queue_left);528529return NULL;530}531532/**533* batadv_forw_packet_was_stolen() - check whether someone stole this packet534* @forw_packet: the forwarding packet to check535*536* This function checks whether the given forwarding packet was claimed by537* someone else for free().538*539* Return: True if someone stole it, false otherwise.540*/541static bool542batadv_forw_packet_was_stolen(struct batadv_forw_packet *forw_packet)543{544return !hlist_unhashed(&forw_packet->cleanup_list);545}546547/**548* batadv_forw_packet_steal() - claim a forw_packet for free()549* @forw_packet: the forwarding packet to steal550* @lock: a key to the store to steal from (e.g. forw_{bat,bcast}_list_lock)551*552* This function tries to steal a specific forw_packet from global553* visibility for the purpose of getting it for free(). That means554* the caller is *not* allowed to requeue it afterwards.555*556* Return: True if stealing was successful. False if someone else stole it557* before us.558*/559bool batadv_forw_packet_steal(struct batadv_forw_packet *forw_packet,560spinlock_t *lock)561{562/* did purging routine steal it earlier? */563spin_lock_bh(lock);564if (batadv_forw_packet_was_stolen(forw_packet)) {565spin_unlock_bh(lock);566return false;567}568569hlist_del_init(&forw_packet->list);570571/* Just to spot misuse of this function */572hlist_add_fake(&forw_packet->cleanup_list);573574spin_unlock_bh(lock);575return true;576}577578/**579* batadv_forw_packet_list_steal() - claim a list of forward packets for free()580* @forw_list: the to be stolen forward packets581* @cleanup_list: a backup pointer, to be able to dispose the packet later582* @hard_iface: the interface to steal forward packets from583*584* This function claims responsibility to free any forw_packet queued on the585* given hard_iface. If hard_iface is NULL forwarding packets on all hard586* interfaces will be claimed.587*588* The packets are being moved from the forw_list to the cleanup_list. This589* makes it possible for already running threads to notice the claim.590*/591static void592batadv_forw_packet_list_steal(struct hlist_head *forw_list,593struct hlist_head *cleanup_list,594const struct batadv_hard_iface *hard_iface)595{596struct batadv_forw_packet *forw_packet;597struct hlist_node *safe_tmp_node;598599hlist_for_each_entry_safe(forw_packet, safe_tmp_node,600forw_list, list) {601/* if purge_outstanding_packets() was called with an argument602* we delete only packets belonging to the given interface603*/604if (hard_iface &&605forw_packet->if_incoming != hard_iface &&606forw_packet->if_outgoing != hard_iface)607continue;608609hlist_del(&forw_packet->list);610hlist_add_head(&forw_packet->cleanup_list, cleanup_list);611}612}613614/**615* batadv_forw_packet_list_free() - free a list of forward packets616* @head: a list of to be freed forw_packets617*618* This function cancels the scheduling of any packet in the provided list,619* waits for any possibly running packet forwarding thread to finish and620* finally, safely frees this forward packet.621*622* This function might sleep.623*/624static void batadv_forw_packet_list_free(struct hlist_head *head)625{626struct batadv_forw_packet *forw_packet;627struct hlist_node *safe_tmp_node;628629hlist_for_each_entry_safe(forw_packet, safe_tmp_node, head,630cleanup_list) {631cancel_delayed_work_sync(&forw_packet->delayed_work);632633hlist_del(&forw_packet->cleanup_list);634batadv_forw_packet_free(forw_packet, true);635}636}637638/**639* batadv_forw_packet_queue() - try to queue a forwarding packet640* @forw_packet: the forwarding packet to queue641* @lock: a key to the store (e.g. forw_{bat,bcast}_list_lock)642* @head: the shelve to queue it on (e.g. forw_{bat,bcast}_list)643* @send_time: timestamp (jiffies) when the packet is to be sent644*645* This function tries to (re)queue a forwarding packet. Requeuing646* is prevented if the according interface is shutting down647* (e.g. if batadv_forw_packet_list_steal() was called for this648* packet earlier).649*650* Calling batadv_forw_packet_queue() after a call to651* batadv_forw_packet_steal() is forbidden!652*653* Caller needs to ensure that forw_packet->delayed_work was initialized.654*/655static void batadv_forw_packet_queue(struct batadv_forw_packet *forw_packet,656spinlock_t *lock, struct hlist_head *head,657unsigned long send_time)658{659spin_lock_bh(lock);660661/* did purging routine steal it from us? */662if (batadv_forw_packet_was_stolen(forw_packet)) {663/* If you got it for free() without trouble, then664* don't get back into the queue after stealing...665*/666WARN_ONCE(hlist_fake(&forw_packet->cleanup_list),667"Requeuing after batadv_forw_packet_steal() not allowed!\n");668669spin_unlock_bh(lock);670return;671}672673hlist_del_init(&forw_packet->list);674hlist_add_head(&forw_packet->list, head);675676queue_delayed_work(batadv_event_workqueue,677&forw_packet->delayed_work,678send_time - jiffies);679spin_unlock_bh(lock);680}681682/**683* batadv_forw_packet_bcast_queue() - try to queue a broadcast packet684* @bat_priv: the bat priv with all the mesh interface information685* @forw_packet: the forwarding packet to queue686* @send_time: timestamp (jiffies) when the packet is to be sent687*688* This function tries to (re)queue a broadcast packet.689*690* Caller needs to ensure that forw_packet->delayed_work was initialized.691*/692static void693batadv_forw_packet_bcast_queue(struct batadv_priv *bat_priv,694struct batadv_forw_packet *forw_packet,695unsigned long send_time)696{697batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bcast_list_lock,698&bat_priv->forw_bcast_list, send_time);699}700701/**702* batadv_forw_packet_ogmv1_queue() - try to queue an OGMv1 packet703* @bat_priv: the bat priv with all the mesh interface information704* @forw_packet: the forwarding packet to queue705* @send_time: timestamp (jiffies) when the packet is to be sent706*707* This function tries to (re)queue an OGMv1 packet.708*709* Caller needs to ensure that forw_packet->delayed_work was initialized.710*/711void batadv_forw_packet_ogmv1_queue(struct batadv_priv *bat_priv,712struct batadv_forw_packet *forw_packet,713unsigned long send_time)714{715batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bat_list_lock,716&bat_priv->forw_bat_list, send_time);717}718719/**720* batadv_forw_bcast_packet_to_list() - queue broadcast packet for transmissions721* @bat_priv: the bat priv with all the mesh interface information722* @skb: broadcast packet to add723* @delay: number of jiffies to wait before sending724* @own_packet: true if it is a self-generated broadcast packet725* @if_in: the interface where the packet was received on726* @if_out: the outgoing interface to queue on727*728* Adds a broadcast packet to the queue and sets up timers. Broadcast packets729* are sent multiple times to increase probability for being received.730*731* This call clones the given skb, hence the caller needs to take into732* account that the data segment of the original skb might not be733* modifiable anymore.734*735* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.736*/737static int batadv_forw_bcast_packet_to_list(struct batadv_priv *bat_priv,738struct sk_buff *skb,739unsigned long delay,740bool own_packet,741struct batadv_hard_iface *if_in,742struct batadv_hard_iface *if_out)743{744struct batadv_forw_packet *forw_packet;745unsigned long send_time = jiffies;746struct sk_buff *newskb;747748newskb = skb_clone(skb, GFP_ATOMIC);749if (!newskb)750goto err;751752forw_packet = batadv_forw_packet_alloc(if_in, if_out,753&bat_priv->bcast_queue_left,754bat_priv, newskb);755if (!forw_packet)756goto err_packet_free;757758forw_packet->own = own_packet;759760INIT_DELAYED_WORK(&forw_packet->delayed_work,761batadv_send_outstanding_bcast_packet);762763send_time += delay ? delay : msecs_to_jiffies(5);764765batadv_forw_packet_bcast_queue(bat_priv, forw_packet, send_time);766return NETDEV_TX_OK;767768err_packet_free:769kfree_skb(newskb);770err:771return NETDEV_TX_BUSY;772}773774/**775* batadv_forw_bcast_packet_if() - forward and queue a broadcast packet776* @bat_priv: the bat priv with all the mesh interface information777* @skb: broadcast packet to add778* @delay: number of jiffies to wait before sending779* @own_packet: true if it is a self-generated broadcast packet780* @if_in: the interface where the packet was received on781* @if_out: the outgoing interface to forward to782*783* Transmits a broadcast packet on the specified interface either immediately784* or if a delay is given after that. Furthermore, queues additional785* retransmissions if this interface is a wireless one.786*787* This call clones the given skb, hence the caller needs to take into788* account that the data segment of the original skb might not be789* modifiable anymore.790*791* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.792*/793static int batadv_forw_bcast_packet_if(struct batadv_priv *bat_priv,794struct sk_buff *skb,795unsigned long delay,796bool own_packet,797struct batadv_hard_iface *if_in,798struct batadv_hard_iface *if_out)799{800unsigned int num_bcasts = if_out->num_bcasts;801struct sk_buff *newskb;802int ret = NETDEV_TX_OK;803804if (!delay) {805newskb = skb_clone(skb, GFP_ATOMIC);806if (!newskb)807return NETDEV_TX_BUSY;808809batadv_send_broadcast_skb(newskb, if_out);810num_bcasts--;811}812813/* delayed broadcast or rebroadcasts? */814if (num_bcasts >= 1) {815BATADV_SKB_CB(skb)->num_bcasts = num_bcasts;816817ret = batadv_forw_bcast_packet_to_list(bat_priv, skb, delay,818own_packet, if_in,819if_out);820}821822return ret;823}824825/**826* batadv_send_no_broadcast() - check whether (re)broadcast is necessary827* @bat_priv: the bat priv with all the mesh interface information828* @skb: broadcast packet to check829* @own_packet: true if it is a self-generated broadcast packet830* @if_out: the outgoing interface checked and considered for (re)broadcast831*832* Return: False if a packet needs to be (re)broadcasted on the given interface,833* true otherwise.834*/835static bool batadv_send_no_broadcast(struct batadv_priv *bat_priv,836struct sk_buff *skb, bool own_packet,837struct batadv_hard_iface *if_out)838{839struct batadv_hardif_neigh_node *neigh_node = NULL;840struct batadv_bcast_packet *bcast_packet;841u8 *orig_neigh;842u8 *neigh_addr;843char *type;844int ret;845846if (!own_packet) {847neigh_addr = eth_hdr(skb)->h_source;848neigh_node = batadv_hardif_neigh_get(if_out,849neigh_addr);850}851852bcast_packet = (struct batadv_bcast_packet *)skb->data;853orig_neigh = neigh_node ? neigh_node->orig : NULL;854855ret = batadv_hardif_no_broadcast(if_out, bcast_packet->orig,856orig_neigh);857858batadv_hardif_neigh_put(neigh_node);859860/* ok, may broadcast */861if (!ret)862return false;863864/* no broadcast */865switch (ret) {866case BATADV_HARDIF_BCAST_NORECIPIENT:867type = "no neighbor";868break;869case BATADV_HARDIF_BCAST_DUPFWD:870type = "single neighbor is source";871break;872case BATADV_HARDIF_BCAST_DUPORIG:873type = "single neighbor is originator";874break;875default:876type = "unknown";877}878879batadv_dbg(BATADV_DBG_BATMAN, bat_priv,880"BCAST packet from orig %pM on %s suppressed: %s\n",881bcast_packet->orig,882if_out->net_dev->name, type);883884return true;885}886887/**888* __batadv_forw_bcast_packet() - forward and queue a broadcast packet889* @bat_priv: the bat priv with all the mesh interface information890* @skb: broadcast packet to add891* @delay: number of jiffies to wait before sending892* @own_packet: true if it is a self-generated broadcast packet893*894* Transmits a broadcast packet either immediately or if a delay is given895* after that. Furthermore, queues additional retransmissions on wireless896* interfaces.897*898* This call clones the given skb, hence the caller needs to take into899* account that the data segment of the given skb might not be900* modifiable anymore.901*902* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.903*/904static int __batadv_forw_bcast_packet(struct batadv_priv *bat_priv,905struct sk_buff *skb,906unsigned long delay,907bool own_packet)908{909struct batadv_hard_iface *hard_iface;910struct batadv_hard_iface *primary_if;911struct list_head *iter;912int ret = NETDEV_TX_OK;913914primary_if = batadv_primary_if_get_selected(bat_priv);915if (!primary_if)916return NETDEV_TX_BUSY;917918rcu_read_lock();919netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {920if (!kref_get_unless_zero(&hard_iface->refcount))921continue;922923if (batadv_send_no_broadcast(bat_priv, skb, own_packet,924hard_iface)) {925batadv_hardif_put(hard_iface);926continue;927}928929ret = batadv_forw_bcast_packet_if(bat_priv, skb, delay,930own_packet, primary_if,931hard_iface);932batadv_hardif_put(hard_iface);933934if (ret == NETDEV_TX_BUSY)935break;936}937rcu_read_unlock();938939batadv_hardif_put(primary_if);940return ret;941}942943/**944* batadv_forw_bcast_packet() - forward and queue a broadcast packet945* @bat_priv: the bat priv with all the mesh interface information946* @skb: broadcast packet to add947* @delay: number of jiffies to wait before sending948* @own_packet: true if it is a self-generated broadcast packet949*950* Transmits a broadcast packet either immediately or if a delay is given951* after that. Furthermore, queues additional retransmissions on wireless952* interfaces.953*954* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.955*/956int batadv_forw_bcast_packet(struct batadv_priv *bat_priv,957struct sk_buff *skb,958unsigned long delay,959bool own_packet)960{961return __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);962}963964/**965* batadv_send_bcast_packet() - send and queue a broadcast packet966* @bat_priv: the bat priv with all the mesh interface information967* @skb: broadcast packet to add968* @delay: number of jiffies to wait before sending969* @own_packet: true if it is a self-generated broadcast packet970*971* Transmits a broadcast packet either immediately or if a delay is given972* after that. Furthermore, queues additional retransmissions on wireless973* interfaces.974*975* Consumes the provided skb.976*/977void batadv_send_bcast_packet(struct batadv_priv *bat_priv,978struct sk_buff *skb,979unsigned long delay,980bool own_packet)981{982__batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);983consume_skb(skb);984}985986/**987* batadv_forw_packet_bcasts_left() - check if a retransmission is necessary988* @forw_packet: the forwarding packet to check989*990* Checks whether a given packet has any (re)transmissions left on the provided991* interface.992*993* hard_iface may be NULL: In that case the number of transmissions this skb had994* so far is compared with the maximum amount of retransmissions independent of995* any interface instead.996*997* Return: True if (re)transmissions are left, false otherwise.998*/999static bool1000batadv_forw_packet_bcasts_left(struct batadv_forw_packet *forw_packet)1001{1002return BATADV_SKB_CB(forw_packet->skb)->num_bcasts;1003}10041005/**1006* batadv_forw_packet_bcasts_dec() - decrement retransmission counter of a1007* packet1008* @forw_packet: the packet to decrease the counter for1009*/1010static void1011batadv_forw_packet_bcasts_dec(struct batadv_forw_packet *forw_packet)1012{1013BATADV_SKB_CB(forw_packet->skb)->num_bcasts--;1014}10151016/**1017* batadv_forw_packet_is_rebroadcast() - check packet for previous transmissions1018* @forw_packet: the packet to check1019*1020* Return: True if this packet was transmitted before, false otherwise.1021*/1022bool batadv_forw_packet_is_rebroadcast(struct batadv_forw_packet *forw_packet)1023{1024unsigned char num_bcasts = BATADV_SKB_CB(forw_packet->skb)->num_bcasts;10251026return num_bcasts != forw_packet->if_outgoing->num_bcasts;1027}10281029/**1030* batadv_send_outstanding_bcast_packet() - transmit a queued broadcast packet1031* @work: work queue item1032*1033* Transmits a queued broadcast packet and if necessary reschedules it.1034*/1035static void batadv_send_outstanding_bcast_packet(struct work_struct *work)1036{1037unsigned long send_time = jiffies + msecs_to_jiffies(5);1038struct batadv_forw_packet *forw_packet;1039struct delayed_work *delayed_work;1040struct batadv_priv *bat_priv;1041struct sk_buff *skb1;1042bool dropped = false;10431044delayed_work = to_delayed_work(work);1045forw_packet = container_of(delayed_work, struct batadv_forw_packet,1046delayed_work);1047bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);10481049if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {1050dropped = true;1051goto out;1052}10531054if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) {1055dropped = true;1056goto out;1057}10581059/* send a copy of the saved skb */1060skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);1061if (!skb1)1062goto out;10631064batadv_send_broadcast_skb(skb1, forw_packet->if_outgoing);1065batadv_forw_packet_bcasts_dec(forw_packet);10661067if (batadv_forw_packet_bcasts_left(forw_packet)) {1068batadv_forw_packet_bcast_queue(bat_priv, forw_packet,1069send_time);1070return;1071}10721073out:1074/* do we get something for free()? */1075if (batadv_forw_packet_steal(forw_packet,1076&bat_priv->forw_bcast_list_lock))1077batadv_forw_packet_free(forw_packet, dropped);1078}10791080/**1081* batadv_purge_outstanding_packets() - stop/purge scheduled bcast/OGMv1 packets1082* @bat_priv: the bat priv with all the mesh interface information1083* @hard_iface: the hard interface to cancel and purge bcast/ogm packets on1084*1085* This method cancels and purges any broadcast and OGMv1 packet on the given1086* hard_iface. If hard_iface is NULL, broadcast and OGMv1 packets on all hard1087* interfaces will be canceled and purged.1088*1089* This function might sleep.1090*/1091void1092batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,1093const struct batadv_hard_iface *hard_iface)1094{1095struct hlist_head head = HLIST_HEAD_INIT;10961097if (hard_iface)1098batadv_dbg(BATADV_DBG_BATMAN, bat_priv,1099"%s(): %s\n",1100__func__, hard_iface->net_dev->name);1101else1102batadv_dbg(BATADV_DBG_BATMAN, bat_priv,1103"%s()\n", __func__);11041105/* claim bcast list for free() */1106spin_lock_bh(&bat_priv->forw_bcast_list_lock);1107batadv_forw_packet_list_steal(&bat_priv->forw_bcast_list, &head,1108hard_iface);1109spin_unlock_bh(&bat_priv->forw_bcast_list_lock);11101111/* claim batman packet list for free() */1112spin_lock_bh(&bat_priv->forw_bat_list_lock);1113batadv_forw_packet_list_steal(&bat_priv->forw_bat_list, &head,1114hard_iface);1115spin_unlock_bh(&bat_priv->forw_bat_list_lock);11161117/* then cancel or wait for packet workers to finish and free */1118batadv_forw_packet_list_free(&head);1119}112011211122