// 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 "network-coding.h"37#include "originator.h"38#include "routing.h"39#include "translation-table.h"4041static void batadv_send_outstanding_bcast_packet(struct work_struct *work);4243/**44* batadv_send_skb_packet() - send an already prepared packet45* @skb: the packet to send46* @hard_iface: the interface to use to send the broadcast packet47* @dst_addr: the payload destination48*49* Send out an already prepared packet to the given neighbor or broadcast it50* using the specified interface. Either hard_iface or neigh_node must be not51* 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 not58* guarantee the frame will be transmitted as it may be dropped due59* to congestion or traffic shaping.60*/61int batadv_send_skb_packet(struct sk_buff *skb,62struct batadv_hard_iface *hard_iface,63const u8 *dst_addr)64{65struct batadv_priv *bat_priv;66struct ethhdr *ethhdr;67int ret;6869bat_priv = netdev_priv(hard_iface->mesh_iface);7071if (hard_iface->if_status != BATADV_IF_ACTIVE)72goto send_skb_err;7374if (unlikely(!hard_iface->net_dev))75goto send_skb_err;7677if (!(hard_iface->net_dev->flags & IFF_UP)) {78pr_warn("Interface %s is not up - can't send packet via that interface!\n",79hard_iface->net_dev->name);80goto send_skb_err;81}8283/* push to the ethernet header. */84if (batadv_skb_head_push(skb, ETH_HLEN) < 0)85goto send_skb_err;8687skb_reset_mac_header(skb);8889ethhdr = eth_hdr(skb);90ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr);91ether_addr_copy(ethhdr->h_dest, dst_addr);92ethhdr->h_proto = htons(ETH_P_BATMAN);9394skb_set_network_header(skb, ETH_HLEN);95skb->protocol = htons(ETH_P_BATMAN);9697skb->dev = hard_iface->net_dev;9899/* Save a clone of the skb to use when decoding coded packets */100batadv_nc_skb_store_for_decoding(bat_priv, skb);101102/* dev_queue_xmit() returns a negative result on error. However on103* congestion and traffic shaping, it drops and returns NET_XMIT_DROP104* (which is > 0). This will not be treated as an error.105*/106ret = dev_queue_xmit(skb);107return net_xmit_eval(ret);108send_skb_err:109kfree_skb(skb);110return NET_XMIT_DROP;111}112113/**114* batadv_send_broadcast_skb() - Send broadcast packet via hard interface115* @skb: packet to be transmitted (with batadv header and no outer eth header)116* @hard_iface: outgoing interface117*118* Return: A negative errno code is returned on a failure. A success does not119* guarantee the frame will be transmitted as it may be dropped due120* to congestion or traffic shaping.121*/122int batadv_send_broadcast_skb(struct sk_buff *skb,123struct batadv_hard_iface *hard_iface)124{125static const u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};126127return batadv_send_skb_packet(skb, hard_iface, broadcast_addr);128}129130/**131* batadv_send_unicast_skb() - Send unicast packet to neighbor132* @skb: packet to be transmitted (with batadv header and no outer eth header)133* @neigh: neighbor which is used as next hop to destination134*135* Return: A negative errno code is returned on a failure. A success does not136* guarantee the frame will be transmitted as it may be dropped due137* to congestion or traffic shaping.138*/139int batadv_send_unicast_skb(struct sk_buff *skb,140struct batadv_neigh_node *neigh)141{142#ifdef CONFIG_BATMAN_ADV_BATMAN_V143struct batadv_hardif_neigh_node *hardif_neigh;144#endif145int ret;146147ret = batadv_send_skb_packet(skb, neigh->if_incoming, neigh->addr);148149#ifdef CONFIG_BATMAN_ADV_BATMAN_V150hardif_neigh = batadv_hardif_neigh_get(neigh->if_incoming, neigh->addr);151152if (hardif_neigh && ret != NET_XMIT_DROP)153hardif_neigh->bat_v.last_unicast_tx = jiffies;154155batadv_hardif_neigh_put(hardif_neigh);156#endif157158return ret;159}160161/**162* batadv_send_skb_to_orig() - Lookup next-hop and transmit skb.163* @skb: Packet to be transmitted.164* @orig_node: Final destination of the packet.165* @recv_if: Interface used when receiving the packet (can be NULL).166*167* Looks up the best next-hop towards the passed originator and passes the168* skb on for preparation of MAC header. If the packet originated from this169* host, NULL can be passed as recv_if and no interface alternating is170* attempted.171*172* Return: negative errno code on a failure, -EINPROGRESS if the skb is173* buffered for later transmit or the NET_XMIT status returned by the174* lower routine if the packet has been passed down.175*/176int batadv_send_skb_to_orig(struct sk_buff *skb,177struct batadv_orig_node *orig_node,178struct batadv_hard_iface *recv_if)179{180struct batadv_priv *bat_priv = orig_node->bat_priv;181struct batadv_neigh_node *neigh_node;182int ret;183184/* batadv_find_router() increases neigh_nodes refcount if found. */185neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);186if (!neigh_node) {187ret = -EINVAL;188goto free_skb;189}190191/* Check if the skb is too large to send in one piece and fragment192* it if needed.193*/194if (atomic_read(&bat_priv->fragmentation) &&195skb->len > neigh_node->if_incoming->net_dev->mtu) {196/* Fragment and send packet. */197ret = batadv_frag_send_packet(skb, orig_node, neigh_node);198/* skb was consumed */199skb = NULL;200201goto put_neigh_node;202}203204/* try to network code the packet, if it is received on an interface205* (i.e. being forwarded). If the packet originates from this node or if206* network coding fails, then send the packet as usual.207*/208if (recv_if && batadv_nc_skb_forward(skb, neigh_node))209ret = -EINPROGRESS;210else211ret = batadv_send_unicast_skb(skb, neigh_node);212213/* skb was consumed */214skb = NULL;215216put_neigh_node:217batadv_neigh_node_put(neigh_node);218free_skb:219kfree_skb(skb);220221return ret;222}223224/**225* batadv_send_skb_push_fill_unicast() - extend the buffer and initialize the226* common fields for unicast packets227* @skb: the skb carrying the unicast header to initialize228* @hdr_size: amount of bytes to push at the beginning of the skb229* @orig_node: the destination node230*231* Return: false if the buffer extension was not possible or true otherwise.232*/233static bool234batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,235struct batadv_orig_node *orig_node)236{237struct batadv_unicast_packet *unicast_packet;238u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn);239240if (batadv_skb_head_push(skb, hdr_size) < 0)241return false;242243unicast_packet = (struct batadv_unicast_packet *)skb->data;244unicast_packet->version = BATADV_COMPAT_VERSION;245/* batman packet type: unicast */246unicast_packet->packet_type = BATADV_UNICAST;247/* set unicast ttl */248unicast_packet->ttl = BATADV_TTL;249/* copy the destination for faster routing */250ether_addr_copy(unicast_packet->dest, orig_node->orig);251/* set the destination tt version number */252unicast_packet->ttvn = ttvn;253254return true;255}256257/**258* batadv_send_skb_prepare_unicast() - encapsulate an skb with a unicast header259* @skb: the skb containing the payload to encapsulate260* @orig_node: the destination node261*262* Return: false if the payload could not be encapsulated or true otherwise.263*/264static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,265struct batadv_orig_node *orig_node)266{267size_t uni_size = sizeof(struct batadv_unicast_packet);268269return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);270}271272/**273* batadv_send_skb_prepare_unicast_4addr() - encapsulate an skb with a274* unicast 4addr header275* @bat_priv: the bat priv with all the mesh interface information276* @skb: the skb containing the payload to encapsulate277* @orig: the destination node278* @packet_subtype: the unicast 4addr packet subtype to use279*280* Return: false if the payload could not be encapsulated or true otherwise.281*/282bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,283struct sk_buff *skb,284struct batadv_orig_node *orig,285int packet_subtype)286{287struct batadv_hard_iface *primary_if;288struct batadv_unicast_4addr_packet *uc_4addr_packet;289bool ret = false;290291primary_if = batadv_primary_if_get_selected(bat_priv);292if (!primary_if)293goto out;294295/* Pull the header space and fill the unicast_packet substructure.296* We can do that because the first member of the uc_4addr_packet297* is of type struct unicast_packet298*/299if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),300orig))301goto out;302303uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;304uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;305ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr);306uc_4addr_packet->subtype = packet_subtype;307uc_4addr_packet->reserved = 0;308309ret = true;310out:311batadv_hardif_put(primary_if);312return ret;313}314315/**316* batadv_send_skb_unicast() - encapsulate and send an skb via unicast317* @bat_priv: the bat priv with all the mesh interface information318* @skb: payload to send319* @packet_type: the batman unicast packet type to use320* @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast321* 4addr packets)322* @orig_node: the originator to send the packet to323* @vid: the vid to be used to search the translation table324*325* Wrap the given skb into a batman-adv unicast or unicast-4addr header326* depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied327* as packet_type. Then send this frame to the given orig_node.328*329* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.330*/331int batadv_send_skb_unicast(struct batadv_priv *bat_priv,332struct sk_buff *skb, int packet_type,333int packet_subtype,334struct batadv_orig_node *orig_node,335unsigned short vid)336{337struct batadv_unicast_packet *unicast_packet;338struct ethhdr *ethhdr;339int ret = NET_XMIT_DROP;340341if (!orig_node)342goto out;343344switch (packet_type) {345case BATADV_UNICAST:346if (!batadv_send_skb_prepare_unicast(skb, orig_node))347goto out;348break;349case BATADV_UNICAST_4ADDR:350if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,351orig_node,352packet_subtype))353goto out;354break;355default:356/* this function supports UNICAST and UNICAST_4ADDR only. It357* should never be invoked with any other packet type358*/359goto out;360}361362/* skb->data might have been reallocated by363* batadv_send_skb_prepare_unicast{,_4addr}()364*/365ethhdr = eth_hdr(skb);366unicast_packet = (struct batadv_unicast_packet *)skb->data;367368/* inform the destination node that we are still missing a correct route369* for this client. The destination will receive this packet and will370* try to reroute it because the ttvn contained in the header is less371* than the current one372*/373if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))374unicast_packet->ttvn = unicast_packet->ttvn - 1;375376ret = batadv_send_skb_to_orig(skb, orig_node, NULL);377/* skb was consumed */378skb = NULL;379380out:381kfree_skb(skb);382return ret;383}384385/**386* batadv_send_skb_via_tt_generic() - send an skb via TT lookup387* @bat_priv: the bat priv with all the mesh interface information388* @skb: payload to send389* @packet_type: the batman unicast packet type to use390* @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast391* 4addr packets)392* @dst_hint: can be used to override the destination contained in the skb393* @vid: the vid to be used to search the translation table394*395* Look up the recipient node for the destination address in the ethernet396* header via the translation table. Wrap the given skb into a batman-adv397* unicast or unicast-4addr header depending on whether BATADV_UNICAST or398* BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame399* to the according destination node.400*401* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.402*/403int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,404struct sk_buff *skb, int packet_type,405int packet_subtype, u8 *dst_hint,406unsigned short vid)407{408struct ethhdr *ethhdr = (struct ethhdr *)skb->data;409struct batadv_orig_node *orig_node;410u8 *src, *dst;411int ret;412413src = ethhdr->h_source;414dst = ethhdr->h_dest;415416/* if we got an hint! let's send the packet to this client (if any) */417if (dst_hint) {418src = NULL;419dst = dst_hint;420}421orig_node = batadv_transtable_search(bat_priv, src, dst, vid);422423ret = batadv_send_skb_unicast(bat_priv, skb, packet_type,424packet_subtype, orig_node, vid);425426batadv_orig_node_put(orig_node);427428return ret;429}430431/**432* batadv_send_skb_via_gw() - send an skb via gateway lookup433* @bat_priv: the bat priv with all the mesh interface information434* @skb: payload to send435* @vid: the vid to be used to search the translation table436*437* Look up the currently selected gateway. Wrap the given skb into a batman-adv438* unicast header and send this frame to this gateway node.439*440* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.441*/442int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,443unsigned short vid)444{445struct batadv_orig_node *orig_node;446int ret;447448orig_node = batadv_gw_get_selected_orig(bat_priv);449ret = batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST_4ADDR,450BATADV_P_DATA, orig_node, vid);451452batadv_orig_node_put(orig_node);453454return ret;455}456457/**458* batadv_forw_packet_free() - free a forwarding packet459* @forw_packet: The packet to free460* @dropped: whether the packet is freed because is dropped461*462* This frees a forwarding packet and releases any resources it might463* have claimed.464*/465void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,466bool dropped)467{468if (dropped)469kfree_skb(forw_packet->skb);470else471consume_skb(forw_packet->skb);472473batadv_hardif_put(forw_packet->if_incoming);474batadv_hardif_put(forw_packet->if_outgoing);475if (forw_packet->queue_left)476atomic_inc(forw_packet->queue_left);477kfree(forw_packet);478}479480/**481* batadv_forw_packet_alloc() - allocate a forwarding packet482* @if_incoming: The (optional) if_incoming to be grabbed483* @if_outgoing: The (optional) if_outgoing to be grabbed484* @queue_left: The (optional) queue counter to decrease485* @bat_priv: The bat_priv for the mesh of this forw_packet486* @skb: The raw packet this forwarding packet shall contain487*488* Allocates a forwarding packet and tries to get a reference to the489* (optional) if_incoming, if_outgoing and queue_left. If queue_left490* is NULL then bat_priv is optional, too.491*492* Return: An allocated forwarding packet on success, NULL otherwise.493*/494struct batadv_forw_packet *495batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,496struct batadv_hard_iface *if_outgoing,497atomic_t *queue_left,498struct batadv_priv *bat_priv,499struct sk_buff *skb)500{501struct batadv_forw_packet *forw_packet;502const char *qname;503504if (queue_left && !batadv_atomic_dec_not_zero(queue_left)) {505qname = "unknown";506507if (queue_left == &bat_priv->bcast_queue_left)508qname = "bcast";509510if (queue_left == &bat_priv->batman_queue_left)511qname = "batman";512513batadv_dbg(BATADV_DBG_BATMAN, bat_priv,514"%s queue is full\n", qname);515516return NULL;517}518519forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);520if (!forw_packet)521goto err;522523if (if_incoming)524kref_get(&if_incoming->refcount);525526if (if_outgoing)527kref_get(&if_outgoing->refcount);528529INIT_HLIST_NODE(&forw_packet->list);530INIT_HLIST_NODE(&forw_packet->cleanup_list);531forw_packet->skb = skb;532forw_packet->queue_left = queue_left;533forw_packet->if_incoming = if_incoming;534forw_packet->if_outgoing = if_outgoing;535forw_packet->num_packets = 1;536537return forw_packet;538539err:540if (queue_left)541atomic_inc(queue_left);542543return NULL;544}545546/**547* batadv_forw_packet_was_stolen() - check whether someone stole this packet548* @forw_packet: the forwarding packet to check549*550* This function checks whether the given forwarding packet was claimed by551* someone else for free().552*553* Return: True if someone stole it, false otherwise.554*/555static bool556batadv_forw_packet_was_stolen(struct batadv_forw_packet *forw_packet)557{558return !hlist_unhashed(&forw_packet->cleanup_list);559}560561/**562* batadv_forw_packet_steal() - claim a forw_packet for free()563* @forw_packet: the forwarding packet to steal564* @lock: a key to the store to steal from (e.g. forw_{bat,bcast}_list_lock)565*566* This function tries to steal a specific forw_packet from global567* visibility for the purpose of getting it for free(). That means568* the caller is *not* allowed to requeue it afterwards.569*570* Return: True if stealing was successful. False if someone else stole it571* before us.572*/573bool batadv_forw_packet_steal(struct batadv_forw_packet *forw_packet,574spinlock_t *lock)575{576/* did purging routine steal it earlier? */577spin_lock_bh(lock);578if (batadv_forw_packet_was_stolen(forw_packet)) {579spin_unlock_bh(lock);580return false;581}582583hlist_del_init(&forw_packet->list);584585/* Just to spot misuse of this function */586hlist_add_fake(&forw_packet->cleanup_list);587588spin_unlock_bh(lock);589return true;590}591592/**593* batadv_forw_packet_list_steal() - claim a list of forward packets for free()594* @forw_list: the to be stolen forward packets595* @cleanup_list: a backup pointer, to be able to dispose the packet later596* @hard_iface: the interface to steal forward packets from597*598* This function claims responsibility to free any forw_packet queued on the599* given hard_iface. If hard_iface is NULL forwarding packets on all hard600* interfaces will be claimed.601*602* The packets are being moved from the forw_list to the cleanup_list. This603* makes it possible for already running threads to notice the claim.604*/605static void606batadv_forw_packet_list_steal(struct hlist_head *forw_list,607struct hlist_head *cleanup_list,608const struct batadv_hard_iface *hard_iface)609{610struct batadv_forw_packet *forw_packet;611struct hlist_node *safe_tmp_node;612613hlist_for_each_entry_safe(forw_packet, safe_tmp_node,614forw_list, list) {615/* if purge_outstanding_packets() was called with an argument616* we delete only packets belonging to the given interface617*/618if (hard_iface &&619forw_packet->if_incoming != hard_iface &&620forw_packet->if_outgoing != hard_iface)621continue;622623hlist_del(&forw_packet->list);624hlist_add_head(&forw_packet->cleanup_list, cleanup_list);625}626}627628/**629* batadv_forw_packet_list_free() - free a list of forward packets630* @head: a list of to be freed forw_packets631*632* This function cancels the scheduling of any packet in the provided list,633* waits for any possibly running packet forwarding thread to finish and634* finally, safely frees this forward packet.635*636* This function might sleep.637*/638static void batadv_forw_packet_list_free(struct hlist_head *head)639{640struct batadv_forw_packet *forw_packet;641struct hlist_node *safe_tmp_node;642643hlist_for_each_entry_safe(forw_packet, safe_tmp_node, head,644cleanup_list) {645cancel_delayed_work_sync(&forw_packet->delayed_work);646647hlist_del(&forw_packet->cleanup_list);648batadv_forw_packet_free(forw_packet, true);649}650}651652/**653* batadv_forw_packet_queue() - try to queue a forwarding packet654* @forw_packet: the forwarding packet to queue655* @lock: a key to the store (e.g. forw_{bat,bcast}_list_lock)656* @head: the shelve to queue it on (e.g. forw_{bat,bcast}_list)657* @send_time: timestamp (jiffies) when the packet is to be sent658*659* This function tries to (re)queue a forwarding packet. Requeuing660* is prevented if the according interface is shutting down661* (e.g. if batadv_forw_packet_list_steal() was called for this662* packet earlier).663*664* Calling batadv_forw_packet_queue() after a call to665* batadv_forw_packet_steal() is forbidden!666*667* Caller needs to ensure that forw_packet->delayed_work was initialized.668*/669static void batadv_forw_packet_queue(struct batadv_forw_packet *forw_packet,670spinlock_t *lock, struct hlist_head *head,671unsigned long send_time)672{673spin_lock_bh(lock);674675/* did purging routine steal it from us? */676if (batadv_forw_packet_was_stolen(forw_packet)) {677/* If you got it for free() without trouble, then678* don't get back into the queue after stealing...679*/680WARN_ONCE(hlist_fake(&forw_packet->cleanup_list),681"Requeuing after batadv_forw_packet_steal() not allowed!\n");682683spin_unlock_bh(lock);684return;685}686687hlist_del_init(&forw_packet->list);688hlist_add_head(&forw_packet->list, head);689690queue_delayed_work(batadv_event_workqueue,691&forw_packet->delayed_work,692send_time - jiffies);693spin_unlock_bh(lock);694}695696/**697* batadv_forw_packet_bcast_queue() - try to queue a broadcast packet698* @bat_priv: the bat priv with all the mesh interface information699* @forw_packet: the forwarding packet to queue700* @send_time: timestamp (jiffies) when the packet is to be sent701*702* This function tries to (re)queue a broadcast packet.703*704* Caller needs to ensure that forw_packet->delayed_work was initialized.705*/706static void707batadv_forw_packet_bcast_queue(struct batadv_priv *bat_priv,708struct batadv_forw_packet *forw_packet,709unsigned long send_time)710{711batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bcast_list_lock,712&bat_priv->forw_bcast_list, send_time);713}714715/**716* batadv_forw_packet_ogmv1_queue() - try to queue an OGMv1 packet717* @bat_priv: the bat priv with all the mesh interface information718* @forw_packet: the forwarding packet to queue719* @send_time: timestamp (jiffies) when the packet is to be sent720*721* This function tries to (re)queue an OGMv1 packet.722*723* Caller needs to ensure that forw_packet->delayed_work was initialized.724*/725void batadv_forw_packet_ogmv1_queue(struct batadv_priv *bat_priv,726struct batadv_forw_packet *forw_packet,727unsigned long send_time)728{729batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bat_list_lock,730&bat_priv->forw_bat_list, send_time);731}732733/**734* batadv_forw_bcast_packet_to_list() - queue broadcast packet for transmissions735* @bat_priv: the bat priv with all the mesh interface information736* @skb: broadcast packet to add737* @delay: number of jiffies to wait before sending738* @own_packet: true if it is a self-generated broadcast packet739* @if_in: the interface where the packet was received on740* @if_out: the outgoing interface to queue on741*742* Adds a broadcast packet to the queue and sets up timers. Broadcast packets743* are sent multiple times to increase probability for being received.744*745* This call clones the given skb, hence the caller needs to take into746* account that the data segment of the original skb might not be747* modifiable anymore.748*749* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.750*/751static int batadv_forw_bcast_packet_to_list(struct batadv_priv *bat_priv,752struct sk_buff *skb,753unsigned long delay,754bool own_packet,755struct batadv_hard_iface *if_in,756struct batadv_hard_iface *if_out)757{758struct batadv_forw_packet *forw_packet;759unsigned long send_time = jiffies;760struct sk_buff *newskb;761762newskb = skb_clone(skb, GFP_ATOMIC);763if (!newskb)764goto err;765766forw_packet = batadv_forw_packet_alloc(if_in, if_out,767&bat_priv->bcast_queue_left,768bat_priv, newskb);769if (!forw_packet)770goto err_packet_free;771772forw_packet->own = own_packet;773774INIT_DELAYED_WORK(&forw_packet->delayed_work,775batadv_send_outstanding_bcast_packet);776777send_time += delay ? delay : msecs_to_jiffies(5);778779batadv_forw_packet_bcast_queue(bat_priv, forw_packet, send_time);780return NETDEV_TX_OK;781782err_packet_free:783kfree_skb(newskb);784err:785return NETDEV_TX_BUSY;786}787788/**789* batadv_forw_bcast_packet_if() - forward and queue a broadcast packet790* @bat_priv: the bat priv with all the mesh interface information791* @skb: broadcast packet to add792* @delay: number of jiffies to wait before sending793* @own_packet: true if it is a self-generated broadcast packet794* @if_in: the interface where the packet was received on795* @if_out: the outgoing interface to forward to796*797* Transmits a broadcast packet on the specified interface either immediately798* or if a delay is given after that. Furthermore, queues additional799* retransmissions if this interface is a wireless one.800*801* This call clones the given skb, hence the caller needs to take into802* account that the data segment of the original skb might not be803* modifiable anymore.804*805* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.806*/807static int batadv_forw_bcast_packet_if(struct batadv_priv *bat_priv,808struct sk_buff *skb,809unsigned long delay,810bool own_packet,811struct batadv_hard_iface *if_in,812struct batadv_hard_iface *if_out)813{814unsigned int num_bcasts = if_out->num_bcasts;815struct sk_buff *newskb;816int ret = NETDEV_TX_OK;817818if (!delay) {819newskb = skb_clone(skb, GFP_ATOMIC);820if (!newskb)821return NETDEV_TX_BUSY;822823batadv_send_broadcast_skb(newskb, if_out);824num_bcasts--;825}826827/* delayed broadcast or rebroadcasts? */828if (num_bcasts >= 1) {829BATADV_SKB_CB(skb)->num_bcasts = num_bcasts;830831ret = batadv_forw_bcast_packet_to_list(bat_priv, skb, delay,832own_packet, if_in,833if_out);834}835836return ret;837}838839/**840* batadv_send_no_broadcast() - check whether (re)broadcast is necessary841* @bat_priv: the bat priv with all the mesh interface information842* @skb: broadcast packet to check843* @own_packet: true if it is a self-generated broadcast packet844* @if_out: the outgoing interface checked and considered for (re)broadcast845*846* Return: False if a packet needs to be (re)broadcasted on the given interface,847* true otherwise.848*/849static bool batadv_send_no_broadcast(struct batadv_priv *bat_priv,850struct sk_buff *skb, bool own_packet,851struct batadv_hard_iface *if_out)852{853struct batadv_hardif_neigh_node *neigh_node = NULL;854struct batadv_bcast_packet *bcast_packet;855u8 *orig_neigh;856u8 *neigh_addr;857char *type;858int ret;859860if (!own_packet) {861neigh_addr = eth_hdr(skb)->h_source;862neigh_node = batadv_hardif_neigh_get(if_out,863neigh_addr);864}865866bcast_packet = (struct batadv_bcast_packet *)skb->data;867orig_neigh = neigh_node ? neigh_node->orig : NULL;868869ret = batadv_hardif_no_broadcast(if_out, bcast_packet->orig,870orig_neigh);871872batadv_hardif_neigh_put(neigh_node);873874/* ok, may broadcast */875if (!ret)876return false;877878/* no broadcast */879switch (ret) {880case BATADV_HARDIF_BCAST_NORECIPIENT:881type = "no neighbor";882break;883case BATADV_HARDIF_BCAST_DUPFWD:884type = "single neighbor is source";885break;886case BATADV_HARDIF_BCAST_DUPORIG:887type = "single neighbor is originator";888break;889default:890type = "unknown";891}892893batadv_dbg(BATADV_DBG_BATMAN, bat_priv,894"BCAST packet from orig %pM on %s suppressed: %s\n",895bcast_packet->orig,896if_out->net_dev->name, type);897898return true;899}900901/**902* __batadv_forw_bcast_packet() - forward and queue a broadcast packet903* @bat_priv: the bat priv with all the mesh interface information904* @skb: broadcast packet to add905* @delay: number of jiffies to wait before sending906* @own_packet: true if it is a self-generated broadcast packet907*908* Transmits a broadcast packet either immediately or if a delay is given909* after that. Furthermore, queues additional retransmissions on wireless910* interfaces.911*912* This call clones the given skb, hence the caller needs to take into913* account that the data segment of the given skb might not be914* modifiable anymore.915*916* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.917*/918static int __batadv_forw_bcast_packet(struct batadv_priv *bat_priv,919struct sk_buff *skb,920unsigned long delay,921bool own_packet)922{923struct batadv_hard_iface *hard_iface;924struct batadv_hard_iface *primary_if;925struct list_head *iter;926int ret = NETDEV_TX_OK;927928primary_if = batadv_primary_if_get_selected(bat_priv);929if (!primary_if)930return NETDEV_TX_BUSY;931932rcu_read_lock();933netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {934if (!kref_get_unless_zero(&hard_iface->refcount))935continue;936937if (batadv_send_no_broadcast(bat_priv, skb, own_packet,938hard_iface)) {939batadv_hardif_put(hard_iface);940continue;941}942943ret = batadv_forw_bcast_packet_if(bat_priv, skb, delay,944own_packet, primary_if,945hard_iface);946batadv_hardif_put(hard_iface);947948if (ret == NETDEV_TX_BUSY)949break;950}951rcu_read_unlock();952953batadv_hardif_put(primary_if);954return ret;955}956957/**958* batadv_forw_bcast_packet() - forward and queue a broadcast packet959* @bat_priv: the bat priv with all the mesh interface information960* @skb: broadcast packet to add961* @delay: number of jiffies to wait before sending962* @own_packet: true if it is a self-generated broadcast packet963*964* Transmits a broadcast packet either immediately or if a delay is given965* after that. Furthermore, queues additional retransmissions on wireless966* interfaces.967*968* Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.969*/970int batadv_forw_bcast_packet(struct batadv_priv *bat_priv,971struct sk_buff *skb,972unsigned long delay,973bool own_packet)974{975return __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);976}977978/**979* batadv_send_bcast_packet() - send and queue a broadcast packet980* @bat_priv: the bat priv with all the mesh interface information981* @skb: broadcast packet to add982* @delay: number of jiffies to wait before sending983* @own_packet: true if it is a self-generated broadcast packet984*985* Transmits a broadcast packet either immediately or if a delay is given986* after that. Furthermore, queues additional retransmissions on wireless987* interfaces.988*989* Consumes the provided skb.990*/991void batadv_send_bcast_packet(struct batadv_priv *bat_priv,992struct sk_buff *skb,993unsigned long delay,994bool own_packet)995{996__batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);997consume_skb(skb);998}9991000/**1001* batadv_forw_packet_bcasts_left() - check if a retransmission is necessary1002* @forw_packet: the forwarding packet to check1003*1004* Checks whether a given packet has any (re)transmissions left on the provided1005* interface.1006*1007* hard_iface may be NULL: In that case the number of transmissions this skb had1008* so far is compared with the maximum amount of retransmissions independent of1009* any interface instead.1010*1011* Return: True if (re)transmissions are left, false otherwise.1012*/1013static bool1014batadv_forw_packet_bcasts_left(struct batadv_forw_packet *forw_packet)1015{1016return BATADV_SKB_CB(forw_packet->skb)->num_bcasts;1017}10181019/**1020* batadv_forw_packet_bcasts_dec() - decrement retransmission counter of a1021* packet1022* @forw_packet: the packet to decrease the counter for1023*/1024static void1025batadv_forw_packet_bcasts_dec(struct batadv_forw_packet *forw_packet)1026{1027BATADV_SKB_CB(forw_packet->skb)->num_bcasts--;1028}10291030/**1031* batadv_forw_packet_is_rebroadcast() - check packet for previous transmissions1032* @forw_packet: the packet to check1033*1034* Return: True if this packet was transmitted before, false otherwise.1035*/1036bool batadv_forw_packet_is_rebroadcast(struct batadv_forw_packet *forw_packet)1037{1038unsigned char num_bcasts = BATADV_SKB_CB(forw_packet->skb)->num_bcasts;10391040return num_bcasts != forw_packet->if_outgoing->num_bcasts;1041}10421043/**1044* batadv_send_outstanding_bcast_packet() - transmit a queued broadcast packet1045* @work: work queue item1046*1047* Transmits a queued broadcast packet and if necessary reschedules it.1048*/1049static void batadv_send_outstanding_bcast_packet(struct work_struct *work)1050{1051unsigned long send_time = jiffies + msecs_to_jiffies(5);1052struct batadv_forw_packet *forw_packet;1053struct delayed_work *delayed_work;1054struct batadv_priv *bat_priv;1055struct sk_buff *skb1;1056bool dropped = false;10571058delayed_work = to_delayed_work(work);1059forw_packet = container_of(delayed_work, struct batadv_forw_packet,1060delayed_work);1061bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);10621063if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {1064dropped = true;1065goto out;1066}10671068if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) {1069dropped = true;1070goto out;1071}10721073/* send a copy of the saved skb */1074skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);1075if (!skb1)1076goto out;10771078batadv_send_broadcast_skb(skb1, forw_packet->if_outgoing);1079batadv_forw_packet_bcasts_dec(forw_packet);10801081if (batadv_forw_packet_bcasts_left(forw_packet)) {1082batadv_forw_packet_bcast_queue(bat_priv, forw_packet,1083send_time);1084return;1085}10861087out:1088/* do we get something for free()? */1089if (batadv_forw_packet_steal(forw_packet,1090&bat_priv->forw_bcast_list_lock))1091batadv_forw_packet_free(forw_packet, dropped);1092}10931094/**1095* batadv_purge_outstanding_packets() - stop/purge scheduled bcast/OGMv1 packets1096* @bat_priv: the bat priv with all the mesh interface information1097* @hard_iface: the hard interface to cancel and purge bcast/ogm packets on1098*1099* This method cancels and purges any broadcast and OGMv1 packet on the given1100* hard_iface. If hard_iface is NULL, broadcast and OGMv1 packets on all hard1101* interfaces will be canceled and purged.1102*1103* This function might sleep.1104*/1105void1106batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,1107const struct batadv_hard_iface *hard_iface)1108{1109struct hlist_head head = HLIST_HEAD_INIT;11101111if (hard_iface)1112batadv_dbg(BATADV_DBG_BATMAN, bat_priv,1113"%s(): %s\n",1114__func__, hard_iface->net_dev->name);1115else1116batadv_dbg(BATADV_DBG_BATMAN, bat_priv,1117"%s()\n", __func__);11181119/* claim bcast list for free() */1120spin_lock_bh(&bat_priv->forw_bcast_list_lock);1121batadv_forw_packet_list_steal(&bat_priv->forw_bcast_list, &head,1122hard_iface);1123spin_unlock_bh(&bat_priv->forw_bcast_list_lock);11241125/* claim batman packet list for free() */1126spin_lock_bh(&bat_priv->forw_bat_list_lock);1127batadv_forw_packet_list_steal(&bat_priv->forw_bat_list, &head,1128hard_iface);1129spin_unlock_bh(&bat_priv->forw_bat_list_lock);11301131/* then cancel or wait for packet workers to finish and free */1132batadv_forw_packet_list_free(&head);1133}113411351136