18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2007-2020  B.A.T.M.A.N. contributors:
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Marek Lindner, Simon Wunderlich
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "send.h"
88c2ecf20Sopenharmony_ci#include "main.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/atomic.h>
118c2ecf20Sopenharmony_ci#include <linux/bug.h>
128c2ecf20Sopenharmony_ci#include <linux/byteorder/generic.h>
138c2ecf20Sopenharmony_ci#include <linux/errno.h>
148c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
158c2ecf20Sopenharmony_ci#include <linux/gfp.h>
168c2ecf20Sopenharmony_ci#include <linux/if.h>
178c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
188c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/kref.h>
218c2ecf20Sopenharmony_ci#include <linux/list.h>
228c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
238c2ecf20Sopenharmony_ci#include <linux/printk.h>
248c2ecf20Sopenharmony_ci#include <linux/rculist.h>
258c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
268c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
298c2ecf20Sopenharmony_ci#include <linux/stddef.h>
308c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "distributed-arp-table.h"
338c2ecf20Sopenharmony_ci#include "fragmentation.h"
348c2ecf20Sopenharmony_ci#include "gateway_client.h"
358c2ecf20Sopenharmony_ci#include "hard-interface.h"
368c2ecf20Sopenharmony_ci#include "log.h"
378c2ecf20Sopenharmony_ci#include "network-coding.h"
388c2ecf20Sopenharmony_ci#include "originator.h"
398c2ecf20Sopenharmony_ci#include "routing.h"
408c2ecf20Sopenharmony_ci#include "soft-interface.h"
418c2ecf20Sopenharmony_ci#include "translation-table.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void batadv_send_outstanding_bcast_packet(struct work_struct *work);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/**
468c2ecf20Sopenharmony_ci * batadv_send_skb_packet() - send an already prepared packet
478c2ecf20Sopenharmony_ci * @skb: the packet to send
488c2ecf20Sopenharmony_ci * @hard_iface: the interface to use to send the broadcast packet
498c2ecf20Sopenharmony_ci * @dst_addr: the payload destination
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Send out an already prepared packet to the given neighbor or broadcast it
528c2ecf20Sopenharmony_ci * using the specified interface. Either hard_iface or neigh_node must be not
538c2ecf20Sopenharmony_ci * NULL.
548c2ecf20Sopenharmony_ci * If neigh_node is NULL, then the packet is broadcasted using hard_iface,
558c2ecf20Sopenharmony_ci * otherwise it is sent as unicast to the given neighbor.
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * Regardless of the return value, the skb is consumed.
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * Return: A negative errno code is returned on a failure. A success does not
608c2ecf20Sopenharmony_ci * guarantee the frame will be transmitted as it may be dropped due
618c2ecf20Sopenharmony_ci * to congestion or traffic shaping.
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ciint batadv_send_skb_packet(struct sk_buff *skb,
648c2ecf20Sopenharmony_ci			   struct batadv_hard_iface *hard_iface,
658c2ecf20Sopenharmony_ci			   const u8 *dst_addr)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv;
688c2ecf20Sopenharmony_ci	struct ethhdr *ethhdr;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	bat_priv = netdev_priv(hard_iface->soft_iface);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (hard_iface->if_status != BATADV_IF_ACTIVE)
748c2ecf20Sopenharmony_ci		goto send_skb_err;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (unlikely(!hard_iface->net_dev))
778c2ecf20Sopenharmony_ci		goto send_skb_err;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (!(hard_iface->net_dev->flags & IFF_UP)) {
808c2ecf20Sopenharmony_ci		pr_warn("Interface %s is not up - can't send packet via that interface!\n",
818c2ecf20Sopenharmony_ci			hard_iface->net_dev->name);
828c2ecf20Sopenharmony_ci		goto send_skb_err;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* push to the ethernet header. */
868c2ecf20Sopenharmony_ci	if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
878c2ecf20Sopenharmony_ci		goto send_skb_err;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	skb_reset_mac_header(skb);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	ethhdr = eth_hdr(skb);
928c2ecf20Sopenharmony_ci	ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr);
938c2ecf20Sopenharmony_ci	ether_addr_copy(ethhdr->h_dest, dst_addr);
948c2ecf20Sopenharmony_ci	ethhdr->h_proto = htons(ETH_P_BATMAN);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	skb_set_network_header(skb, ETH_HLEN);
978c2ecf20Sopenharmony_ci	skb->protocol = htons(ETH_P_BATMAN);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	skb->dev = hard_iface->net_dev;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* Save a clone of the skb to use when decoding coded packets */
1028c2ecf20Sopenharmony_ci	batadv_nc_skb_store_for_decoding(bat_priv, skb);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* dev_queue_xmit() returns a negative result on error.	 However on
1058c2ecf20Sopenharmony_ci	 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
1068c2ecf20Sopenharmony_ci	 * (which is > 0). This will not be treated as an error.
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	ret = dev_queue_xmit(skb);
1098c2ecf20Sopenharmony_ci	return net_xmit_eval(ret);
1108c2ecf20Sopenharmony_cisend_skb_err:
1118c2ecf20Sopenharmony_ci	kfree_skb(skb);
1128c2ecf20Sopenharmony_ci	return NET_XMIT_DROP;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/**
1168c2ecf20Sopenharmony_ci * batadv_send_broadcast_skb() - Send broadcast packet via hard interface
1178c2ecf20Sopenharmony_ci * @skb: packet to be transmitted (with batadv header and no outer eth header)
1188c2ecf20Sopenharmony_ci * @hard_iface: outgoing interface
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * Return: A negative errno code is returned on a failure. A success does not
1218c2ecf20Sopenharmony_ci * guarantee the frame will be transmitted as it may be dropped due
1228c2ecf20Sopenharmony_ci * to congestion or traffic shaping.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ciint batadv_send_broadcast_skb(struct sk_buff *skb,
1258c2ecf20Sopenharmony_ci			      struct batadv_hard_iface *hard_iface)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	return batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/**
1318c2ecf20Sopenharmony_ci * batadv_send_unicast_skb() - Send unicast packet to neighbor
1328c2ecf20Sopenharmony_ci * @skb: packet to be transmitted (with batadv header and no outer eth header)
1338c2ecf20Sopenharmony_ci * @neigh: neighbor which is used as next hop to destination
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * Return: A negative errno code is returned on a failure. A success does not
1368c2ecf20Sopenharmony_ci * guarantee the frame will be transmitted as it may be dropped due
1378c2ecf20Sopenharmony_ci * to congestion or traffic shaping.
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_ciint batadv_send_unicast_skb(struct sk_buff *skb,
1408c2ecf20Sopenharmony_ci			    struct batadv_neigh_node *neigh)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1438c2ecf20Sopenharmony_ci	struct batadv_hardif_neigh_node *hardif_neigh;
1448c2ecf20Sopenharmony_ci#endif
1458c2ecf20Sopenharmony_ci	int ret;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	ret = batadv_send_skb_packet(skb, neigh->if_incoming, neigh->addr);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#ifdef CONFIG_BATMAN_ADV_BATMAN_V
1508c2ecf20Sopenharmony_ci	hardif_neigh = batadv_hardif_neigh_get(neigh->if_incoming, neigh->addr);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (hardif_neigh && ret != NET_XMIT_DROP)
1538c2ecf20Sopenharmony_ci		hardif_neigh->bat_v.last_unicast_tx = jiffies;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (hardif_neigh)
1568c2ecf20Sopenharmony_ci		batadv_hardif_neigh_put(hardif_neigh);
1578c2ecf20Sopenharmony_ci#endif
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return ret;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/**
1638c2ecf20Sopenharmony_ci * batadv_send_skb_to_orig() - Lookup next-hop and transmit skb.
1648c2ecf20Sopenharmony_ci * @skb: Packet to be transmitted.
1658c2ecf20Sopenharmony_ci * @orig_node: Final destination of the packet.
1668c2ecf20Sopenharmony_ci * @recv_if: Interface used when receiving the packet (can be NULL).
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * Looks up the best next-hop towards the passed originator and passes the
1698c2ecf20Sopenharmony_ci * skb on for preparation of MAC header. If the packet originated from this
1708c2ecf20Sopenharmony_ci * host, NULL can be passed as recv_if and no interface alternating is
1718c2ecf20Sopenharmony_ci * attempted.
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * Return: negative errno code on a failure, -EINPROGRESS if the skb is
1748c2ecf20Sopenharmony_ci * buffered for later transmit or the NET_XMIT status returned by the
1758c2ecf20Sopenharmony_ci * lower routine if the packet has been passed down.
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_ciint batadv_send_skb_to_orig(struct sk_buff *skb,
1788c2ecf20Sopenharmony_ci			    struct batadv_orig_node *orig_node,
1798c2ecf20Sopenharmony_ci			    struct batadv_hard_iface *recv_if)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv = orig_node->bat_priv;
1828c2ecf20Sopenharmony_ci	struct batadv_neigh_node *neigh_node;
1838c2ecf20Sopenharmony_ci	int ret;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* batadv_find_router() increases neigh_nodes refcount if found. */
1868c2ecf20Sopenharmony_ci	neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
1878c2ecf20Sopenharmony_ci	if (!neigh_node) {
1888c2ecf20Sopenharmony_ci		ret = -EINVAL;
1898c2ecf20Sopenharmony_ci		goto free_skb;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Check if the skb is too large to send in one piece and fragment
1938c2ecf20Sopenharmony_ci	 * it if needed.
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci	if (atomic_read(&bat_priv->fragmentation) &&
1968c2ecf20Sopenharmony_ci	    skb->len > neigh_node->if_incoming->net_dev->mtu) {
1978c2ecf20Sopenharmony_ci		/* Fragment and send packet. */
1988c2ecf20Sopenharmony_ci		ret = batadv_frag_send_packet(skb, orig_node, neigh_node);
1998c2ecf20Sopenharmony_ci		/* skb was consumed */
2008c2ecf20Sopenharmony_ci		skb = NULL;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		goto put_neigh_node;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* try to network code the packet, if it is received on an interface
2068c2ecf20Sopenharmony_ci	 * (i.e. being forwarded). If the packet originates from this node or if
2078c2ecf20Sopenharmony_ci	 * network coding fails, then send the packet as usual.
2088c2ecf20Sopenharmony_ci	 */
2098c2ecf20Sopenharmony_ci	if (recv_if && batadv_nc_skb_forward(skb, neigh_node))
2108c2ecf20Sopenharmony_ci		ret = -EINPROGRESS;
2118c2ecf20Sopenharmony_ci	else
2128c2ecf20Sopenharmony_ci		ret = batadv_send_unicast_skb(skb, neigh_node);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* skb was consumed */
2158c2ecf20Sopenharmony_ci	skb = NULL;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ciput_neigh_node:
2188c2ecf20Sopenharmony_ci	batadv_neigh_node_put(neigh_node);
2198c2ecf20Sopenharmony_cifree_skb:
2208c2ecf20Sopenharmony_ci	kfree_skb(skb);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return ret;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/**
2268c2ecf20Sopenharmony_ci * batadv_send_skb_push_fill_unicast() - extend the buffer and initialize the
2278c2ecf20Sopenharmony_ci *  common fields for unicast packets
2288c2ecf20Sopenharmony_ci * @skb: the skb carrying the unicast header to initialize
2298c2ecf20Sopenharmony_ci * @hdr_size: amount of bytes to push at the beginning of the skb
2308c2ecf20Sopenharmony_ci * @orig_node: the destination node
2318c2ecf20Sopenharmony_ci *
2328c2ecf20Sopenharmony_ci * Return: false if the buffer extension was not possible or true otherwise.
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_cistatic bool
2358c2ecf20Sopenharmony_cibatadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,
2368c2ecf20Sopenharmony_ci				  struct batadv_orig_node *orig_node)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct batadv_unicast_packet *unicast_packet;
2398c2ecf20Sopenharmony_ci	u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	if (batadv_skb_head_push(skb, hdr_size) < 0)
2428c2ecf20Sopenharmony_ci		return false;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	unicast_packet = (struct batadv_unicast_packet *)skb->data;
2458c2ecf20Sopenharmony_ci	unicast_packet->version = BATADV_COMPAT_VERSION;
2468c2ecf20Sopenharmony_ci	/* batman packet type: unicast */
2478c2ecf20Sopenharmony_ci	unicast_packet->packet_type = BATADV_UNICAST;
2488c2ecf20Sopenharmony_ci	/* set unicast ttl */
2498c2ecf20Sopenharmony_ci	unicast_packet->ttl = BATADV_TTL;
2508c2ecf20Sopenharmony_ci	/* copy the destination for faster routing */
2518c2ecf20Sopenharmony_ci	ether_addr_copy(unicast_packet->dest, orig_node->orig);
2528c2ecf20Sopenharmony_ci	/* set the destination tt version number */
2538c2ecf20Sopenharmony_ci	unicast_packet->ttvn = ttvn;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return true;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/**
2598c2ecf20Sopenharmony_ci * batadv_send_skb_prepare_unicast() - encapsulate an skb with a unicast header
2608c2ecf20Sopenharmony_ci * @skb: the skb containing the payload to encapsulate
2618c2ecf20Sopenharmony_ci * @orig_node: the destination node
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci * Return: false if the payload could not be encapsulated or true otherwise.
2648c2ecf20Sopenharmony_ci */
2658c2ecf20Sopenharmony_cistatic bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,
2668c2ecf20Sopenharmony_ci					    struct batadv_orig_node *orig_node)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	size_t uni_size = sizeof(struct batadv_unicast_packet);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/**
2748c2ecf20Sopenharmony_ci * batadv_send_skb_prepare_unicast_4addr() - encapsulate an skb with a
2758c2ecf20Sopenharmony_ci *  unicast 4addr header
2768c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
2778c2ecf20Sopenharmony_ci * @skb: the skb containing the payload to encapsulate
2788c2ecf20Sopenharmony_ci * @orig: the destination node
2798c2ecf20Sopenharmony_ci * @packet_subtype: the unicast 4addr packet subtype to use
2808c2ecf20Sopenharmony_ci *
2818c2ecf20Sopenharmony_ci * Return: false if the payload could not be encapsulated or true otherwise.
2828c2ecf20Sopenharmony_ci */
2838c2ecf20Sopenharmony_cibool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
2848c2ecf20Sopenharmony_ci					   struct sk_buff *skb,
2858c2ecf20Sopenharmony_ci					   struct batadv_orig_node *orig,
2868c2ecf20Sopenharmony_ci					   int packet_subtype)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct batadv_hard_iface *primary_if;
2898c2ecf20Sopenharmony_ci	struct batadv_unicast_4addr_packet *uc_4addr_packet;
2908c2ecf20Sopenharmony_ci	bool ret = false;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
2938c2ecf20Sopenharmony_ci	if (!primary_if)
2948c2ecf20Sopenharmony_ci		goto out;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Pull the header space and fill the unicast_packet substructure.
2978c2ecf20Sopenharmony_ci	 * We can do that because the first member of the uc_4addr_packet
2988c2ecf20Sopenharmony_ci	 * is of type struct unicast_packet
2998c2ecf20Sopenharmony_ci	 */
3008c2ecf20Sopenharmony_ci	if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),
3018c2ecf20Sopenharmony_ci					       orig))
3028c2ecf20Sopenharmony_ci		goto out;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
3058c2ecf20Sopenharmony_ci	uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;
3068c2ecf20Sopenharmony_ci	ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr);
3078c2ecf20Sopenharmony_ci	uc_4addr_packet->subtype = packet_subtype;
3088c2ecf20Sopenharmony_ci	uc_4addr_packet->reserved = 0;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	ret = true;
3118c2ecf20Sopenharmony_ciout:
3128c2ecf20Sopenharmony_ci	if (primary_if)
3138c2ecf20Sopenharmony_ci		batadv_hardif_put(primary_if);
3148c2ecf20Sopenharmony_ci	return ret;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci/**
3188c2ecf20Sopenharmony_ci * batadv_send_skb_unicast() - encapsulate and send an skb via unicast
3198c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
3208c2ecf20Sopenharmony_ci * @skb: payload to send
3218c2ecf20Sopenharmony_ci * @packet_type: the batman unicast packet type to use
3228c2ecf20Sopenharmony_ci * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
3238c2ecf20Sopenharmony_ci *  4addr packets)
3248c2ecf20Sopenharmony_ci * @orig_node: the originator to send the packet to
3258c2ecf20Sopenharmony_ci * @vid: the vid to be used to search the translation table
3268c2ecf20Sopenharmony_ci *
3278c2ecf20Sopenharmony_ci * Wrap the given skb into a batman-adv unicast or unicast-4addr header
3288c2ecf20Sopenharmony_ci * depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied
3298c2ecf20Sopenharmony_ci * as packet_type. Then send this frame to the given orig_node.
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
3328c2ecf20Sopenharmony_ci */
3338c2ecf20Sopenharmony_ciint batadv_send_skb_unicast(struct batadv_priv *bat_priv,
3348c2ecf20Sopenharmony_ci			    struct sk_buff *skb, int packet_type,
3358c2ecf20Sopenharmony_ci			    int packet_subtype,
3368c2ecf20Sopenharmony_ci			    struct batadv_orig_node *orig_node,
3378c2ecf20Sopenharmony_ci			    unsigned short vid)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct batadv_unicast_packet *unicast_packet;
3408c2ecf20Sopenharmony_ci	struct ethhdr *ethhdr;
3418c2ecf20Sopenharmony_ci	int ret = NET_XMIT_DROP;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (!orig_node)
3448c2ecf20Sopenharmony_ci		goto out;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	switch (packet_type) {
3478c2ecf20Sopenharmony_ci	case BATADV_UNICAST:
3488c2ecf20Sopenharmony_ci		if (!batadv_send_skb_prepare_unicast(skb, orig_node))
3498c2ecf20Sopenharmony_ci			goto out;
3508c2ecf20Sopenharmony_ci		break;
3518c2ecf20Sopenharmony_ci	case BATADV_UNICAST_4ADDR:
3528c2ecf20Sopenharmony_ci		if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,
3538c2ecf20Sopenharmony_ci							   orig_node,
3548c2ecf20Sopenharmony_ci							   packet_subtype))
3558c2ecf20Sopenharmony_ci			goto out;
3568c2ecf20Sopenharmony_ci		break;
3578c2ecf20Sopenharmony_ci	default:
3588c2ecf20Sopenharmony_ci		/* this function supports UNICAST and UNICAST_4ADDR only. It
3598c2ecf20Sopenharmony_ci		 * should never be invoked with any other packet type
3608c2ecf20Sopenharmony_ci		 */
3618c2ecf20Sopenharmony_ci		goto out;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* skb->data might have been reallocated by
3658c2ecf20Sopenharmony_ci	 * batadv_send_skb_prepare_unicast{,_4addr}()
3668c2ecf20Sopenharmony_ci	 */
3678c2ecf20Sopenharmony_ci	ethhdr = eth_hdr(skb);
3688c2ecf20Sopenharmony_ci	unicast_packet = (struct batadv_unicast_packet *)skb->data;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* inform the destination node that we are still missing a correct route
3718c2ecf20Sopenharmony_ci	 * for this client. The destination will receive this packet and will
3728c2ecf20Sopenharmony_ci	 * try to reroute it because the ttvn contained in the header is less
3738c2ecf20Sopenharmony_ci	 * than the current one
3748c2ecf20Sopenharmony_ci	 */
3758c2ecf20Sopenharmony_ci	if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))
3768c2ecf20Sopenharmony_ci		unicast_packet->ttvn = unicast_packet->ttvn - 1;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	ret = batadv_send_skb_to_orig(skb, orig_node, NULL);
3798c2ecf20Sopenharmony_ci	 /* skb was consumed */
3808c2ecf20Sopenharmony_ci	skb = NULL;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ciout:
3838c2ecf20Sopenharmony_ci	kfree_skb(skb);
3848c2ecf20Sopenharmony_ci	return ret;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/**
3888c2ecf20Sopenharmony_ci * batadv_send_skb_via_tt_generic() - send an skb via TT lookup
3898c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
3908c2ecf20Sopenharmony_ci * @skb: payload to send
3918c2ecf20Sopenharmony_ci * @packet_type: the batman unicast packet type to use
3928c2ecf20Sopenharmony_ci * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
3938c2ecf20Sopenharmony_ci *  4addr packets)
3948c2ecf20Sopenharmony_ci * @dst_hint: can be used to override the destination contained in the skb
3958c2ecf20Sopenharmony_ci * @vid: the vid to be used to search the translation table
3968c2ecf20Sopenharmony_ci *
3978c2ecf20Sopenharmony_ci * Look up the recipient node for the destination address in the ethernet
3988c2ecf20Sopenharmony_ci * header via the translation table. Wrap the given skb into a batman-adv
3998c2ecf20Sopenharmony_ci * unicast or unicast-4addr header depending on whether BATADV_UNICAST or
4008c2ecf20Sopenharmony_ci * BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame
4018c2ecf20Sopenharmony_ci * to the according destination node.
4028c2ecf20Sopenharmony_ci *
4038c2ecf20Sopenharmony_ci * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
4048c2ecf20Sopenharmony_ci */
4058c2ecf20Sopenharmony_ciint batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,
4068c2ecf20Sopenharmony_ci				   struct sk_buff *skb, int packet_type,
4078c2ecf20Sopenharmony_ci				   int packet_subtype, u8 *dst_hint,
4088c2ecf20Sopenharmony_ci				   unsigned short vid)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
4118c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node;
4128c2ecf20Sopenharmony_ci	u8 *src, *dst;
4138c2ecf20Sopenharmony_ci	int ret;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	src = ethhdr->h_source;
4168c2ecf20Sopenharmony_ci	dst = ethhdr->h_dest;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* if we got an hint! let's send the packet to this client (if any) */
4198c2ecf20Sopenharmony_ci	if (dst_hint) {
4208c2ecf20Sopenharmony_ci		src = NULL;
4218c2ecf20Sopenharmony_ci		dst = dst_hint;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci	orig_node = batadv_transtable_search(bat_priv, src, dst, vid);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	ret = batadv_send_skb_unicast(bat_priv, skb, packet_type,
4268c2ecf20Sopenharmony_ci				      packet_subtype, orig_node, vid);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (orig_node)
4298c2ecf20Sopenharmony_ci		batadv_orig_node_put(orig_node);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return ret;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci/**
4358c2ecf20Sopenharmony_ci * batadv_send_skb_via_gw() - send an skb via gateway lookup
4368c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
4378c2ecf20Sopenharmony_ci * @skb: payload to send
4388c2ecf20Sopenharmony_ci * @vid: the vid to be used to search the translation table
4398c2ecf20Sopenharmony_ci *
4408c2ecf20Sopenharmony_ci * Look up the currently selected gateway. Wrap the given skb into a batman-adv
4418c2ecf20Sopenharmony_ci * unicast header and send this frame to this gateway node.
4428c2ecf20Sopenharmony_ci *
4438c2ecf20Sopenharmony_ci * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
4448c2ecf20Sopenharmony_ci */
4458c2ecf20Sopenharmony_ciint batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,
4468c2ecf20Sopenharmony_ci			   unsigned short vid)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node;
4498c2ecf20Sopenharmony_ci	int ret;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	orig_node = batadv_gw_get_selected_orig(bat_priv);
4528c2ecf20Sopenharmony_ci	ret = batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST_4ADDR,
4538c2ecf20Sopenharmony_ci				      BATADV_P_DATA, orig_node, vid);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (orig_node)
4568c2ecf20Sopenharmony_ci		batadv_orig_node_put(orig_node);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return ret;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci/**
4628c2ecf20Sopenharmony_ci * batadv_forw_packet_free() - free a forwarding packet
4638c2ecf20Sopenharmony_ci * @forw_packet: The packet to free
4648c2ecf20Sopenharmony_ci * @dropped: whether the packet is freed because is dropped
4658c2ecf20Sopenharmony_ci *
4668c2ecf20Sopenharmony_ci * This frees a forwarding packet and releases any resources it might
4678c2ecf20Sopenharmony_ci * have claimed.
4688c2ecf20Sopenharmony_ci */
4698c2ecf20Sopenharmony_civoid batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,
4708c2ecf20Sopenharmony_ci			     bool dropped)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	if (dropped)
4738c2ecf20Sopenharmony_ci		kfree_skb(forw_packet->skb);
4748c2ecf20Sopenharmony_ci	else
4758c2ecf20Sopenharmony_ci		consume_skb(forw_packet->skb);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (forw_packet->if_incoming)
4788c2ecf20Sopenharmony_ci		batadv_hardif_put(forw_packet->if_incoming);
4798c2ecf20Sopenharmony_ci	if (forw_packet->if_outgoing)
4808c2ecf20Sopenharmony_ci		batadv_hardif_put(forw_packet->if_outgoing);
4818c2ecf20Sopenharmony_ci	if (forw_packet->queue_left)
4828c2ecf20Sopenharmony_ci		atomic_inc(forw_packet->queue_left);
4838c2ecf20Sopenharmony_ci	kfree(forw_packet);
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/**
4878c2ecf20Sopenharmony_ci * batadv_forw_packet_alloc() - allocate a forwarding packet
4888c2ecf20Sopenharmony_ci * @if_incoming: The (optional) if_incoming to be grabbed
4898c2ecf20Sopenharmony_ci * @if_outgoing: The (optional) if_outgoing to be grabbed
4908c2ecf20Sopenharmony_ci * @queue_left: The (optional) queue counter to decrease
4918c2ecf20Sopenharmony_ci * @bat_priv: The bat_priv for the mesh of this forw_packet
4928c2ecf20Sopenharmony_ci * @skb: The raw packet this forwarding packet shall contain
4938c2ecf20Sopenharmony_ci *
4948c2ecf20Sopenharmony_ci * Allocates a forwarding packet and tries to get a reference to the
4958c2ecf20Sopenharmony_ci * (optional) if_incoming, if_outgoing and queue_left. If queue_left
4968c2ecf20Sopenharmony_ci * is NULL then bat_priv is optional, too.
4978c2ecf20Sopenharmony_ci *
4988c2ecf20Sopenharmony_ci * Return: An allocated forwarding packet on success, NULL otherwise.
4998c2ecf20Sopenharmony_ci */
5008c2ecf20Sopenharmony_cistruct batadv_forw_packet *
5018c2ecf20Sopenharmony_cibatadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,
5028c2ecf20Sopenharmony_ci			 struct batadv_hard_iface *if_outgoing,
5038c2ecf20Sopenharmony_ci			 atomic_t *queue_left,
5048c2ecf20Sopenharmony_ci			 struct batadv_priv *bat_priv,
5058c2ecf20Sopenharmony_ci			 struct sk_buff *skb)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	struct batadv_forw_packet *forw_packet;
5088c2ecf20Sopenharmony_ci	const char *qname;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (queue_left && !batadv_atomic_dec_not_zero(queue_left)) {
5118c2ecf20Sopenharmony_ci		qname = "unknown";
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci		if (queue_left == &bat_priv->bcast_queue_left)
5148c2ecf20Sopenharmony_ci			qname = "bcast";
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci		if (queue_left == &bat_priv->batman_queue_left)
5178c2ecf20Sopenharmony_ci			qname = "batman";
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
5208c2ecf20Sopenharmony_ci			   "%s queue is full\n", qname);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci		return NULL;
5238c2ecf20Sopenharmony_ci	}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
5268c2ecf20Sopenharmony_ci	if (!forw_packet)
5278c2ecf20Sopenharmony_ci		goto err;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (if_incoming)
5308c2ecf20Sopenharmony_ci		kref_get(&if_incoming->refcount);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (if_outgoing)
5338c2ecf20Sopenharmony_ci		kref_get(&if_outgoing->refcount);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	INIT_HLIST_NODE(&forw_packet->list);
5368c2ecf20Sopenharmony_ci	INIT_HLIST_NODE(&forw_packet->cleanup_list);
5378c2ecf20Sopenharmony_ci	forw_packet->skb = skb;
5388c2ecf20Sopenharmony_ci	forw_packet->queue_left = queue_left;
5398c2ecf20Sopenharmony_ci	forw_packet->if_incoming = if_incoming;
5408c2ecf20Sopenharmony_ci	forw_packet->if_outgoing = if_outgoing;
5418c2ecf20Sopenharmony_ci	forw_packet->num_packets = 0;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	return forw_packet;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cierr:
5468c2ecf20Sopenharmony_ci	if (queue_left)
5478c2ecf20Sopenharmony_ci		atomic_inc(queue_left);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	return NULL;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci/**
5538c2ecf20Sopenharmony_ci * batadv_forw_packet_was_stolen() - check whether someone stole this packet
5548c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to check
5558c2ecf20Sopenharmony_ci *
5568c2ecf20Sopenharmony_ci * This function checks whether the given forwarding packet was claimed by
5578c2ecf20Sopenharmony_ci * someone else for free().
5588c2ecf20Sopenharmony_ci *
5598c2ecf20Sopenharmony_ci * Return: True if someone stole it, false otherwise.
5608c2ecf20Sopenharmony_ci */
5618c2ecf20Sopenharmony_cistatic bool
5628c2ecf20Sopenharmony_cibatadv_forw_packet_was_stolen(struct batadv_forw_packet *forw_packet)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	return !hlist_unhashed(&forw_packet->cleanup_list);
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci/**
5688c2ecf20Sopenharmony_ci * batadv_forw_packet_steal() - claim a forw_packet for free()
5698c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to steal
5708c2ecf20Sopenharmony_ci * @lock: a key to the store to steal from (e.g. forw_{bat,bcast}_list_lock)
5718c2ecf20Sopenharmony_ci *
5728c2ecf20Sopenharmony_ci * This function tries to steal a specific forw_packet from global
5738c2ecf20Sopenharmony_ci * visibility for the purpose of getting it for free(). That means
5748c2ecf20Sopenharmony_ci * the caller is *not* allowed to requeue it afterwards.
5758c2ecf20Sopenharmony_ci *
5768c2ecf20Sopenharmony_ci * Return: True if stealing was successful. False if someone else stole it
5778c2ecf20Sopenharmony_ci * before us.
5788c2ecf20Sopenharmony_ci */
5798c2ecf20Sopenharmony_cibool batadv_forw_packet_steal(struct batadv_forw_packet *forw_packet,
5808c2ecf20Sopenharmony_ci			      spinlock_t *lock)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	/* did purging routine steal it earlier? */
5838c2ecf20Sopenharmony_ci	spin_lock_bh(lock);
5848c2ecf20Sopenharmony_ci	if (batadv_forw_packet_was_stolen(forw_packet)) {
5858c2ecf20Sopenharmony_ci		spin_unlock_bh(lock);
5868c2ecf20Sopenharmony_ci		return false;
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	hlist_del_init(&forw_packet->list);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* Just to spot misuse of this function */
5928c2ecf20Sopenharmony_ci	hlist_add_fake(&forw_packet->cleanup_list);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	spin_unlock_bh(lock);
5958c2ecf20Sopenharmony_ci	return true;
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/**
5998c2ecf20Sopenharmony_ci * batadv_forw_packet_list_steal() - claim a list of forward packets for free()
6008c2ecf20Sopenharmony_ci * @forw_list: the to be stolen forward packets
6018c2ecf20Sopenharmony_ci * @cleanup_list: a backup pointer, to be able to dispose the packet later
6028c2ecf20Sopenharmony_ci * @hard_iface: the interface to steal forward packets from
6038c2ecf20Sopenharmony_ci *
6048c2ecf20Sopenharmony_ci * This function claims responsibility to free any forw_packet queued on the
6058c2ecf20Sopenharmony_ci * given hard_iface. If hard_iface is NULL forwarding packets on all hard
6068c2ecf20Sopenharmony_ci * interfaces will be claimed.
6078c2ecf20Sopenharmony_ci *
6088c2ecf20Sopenharmony_ci * The packets are being moved from the forw_list to the cleanup_list. This
6098c2ecf20Sopenharmony_ci * makes it possible for already running threads to notice the claim.
6108c2ecf20Sopenharmony_ci */
6118c2ecf20Sopenharmony_cistatic void
6128c2ecf20Sopenharmony_cibatadv_forw_packet_list_steal(struct hlist_head *forw_list,
6138c2ecf20Sopenharmony_ci			      struct hlist_head *cleanup_list,
6148c2ecf20Sopenharmony_ci			      const struct batadv_hard_iface *hard_iface)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	struct batadv_forw_packet *forw_packet;
6178c2ecf20Sopenharmony_ci	struct hlist_node *safe_tmp_node;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
6208c2ecf20Sopenharmony_ci				  forw_list, list) {
6218c2ecf20Sopenharmony_ci		/* if purge_outstanding_packets() was called with an argument
6228c2ecf20Sopenharmony_ci		 * we delete only packets belonging to the given interface
6238c2ecf20Sopenharmony_ci		 */
6248c2ecf20Sopenharmony_ci		if (hard_iface &&
6258c2ecf20Sopenharmony_ci		    forw_packet->if_incoming != hard_iface &&
6268c2ecf20Sopenharmony_ci		    forw_packet->if_outgoing != hard_iface)
6278c2ecf20Sopenharmony_ci			continue;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci		hlist_del(&forw_packet->list);
6308c2ecf20Sopenharmony_ci		hlist_add_head(&forw_packet->cleanup_list, cleanup_list);
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci/**
6358c2ecf20Sopenharmony_ci * batadv_forw_packet_list_free() - free a list of forward packets
6368c2ecf20Sopenharmony_ci * @head: a list of to be freed forw_packets
6378c2ecf20Sopenharmony_ci *
6388c2ecf20Sopenharmony_ci * This function cancels the scheduling of any packet in the provided list,
6398c2ecf20Sopenharmony_ci * waits for any possibly running packet forwarding thread to finish and
6408c2ecf20Sopenharmony_ci * finally, safely frees this forward packet.
6418c2ecf20Sopenharmony_ci *
6428c2ecf20Sopenharmony_ci * This function might sleep.
6438c2ecf20Sopenharmony_ci */
6448c2ecf20Sopenharmony_cistatic void batadv_forw_packet_list_free(struct hlist_head *head)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	struct batadv_forw_packet *forw_packet;
6478c2ecf20Sopenharmony_ci	struct hlist_node *safe_tmp_node;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	hlist_for_each_entry_safe(forw_packet, safe_tmp_node, head,
6508c2ecf20Sopenharmony_ci				  cleanup_list) {
6518c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&forw_packet->delayed_work);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		hlist_del(&forw_packet->cleanup_list);
6548c2ecf20Sopenharmony_ci		batadv_forw_packet_free(forw_packet, true);
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci/**
6598c2ecf20Sopenharmony_ci * batadv_forw_packet_queue() - try to queue a forwarding packet
6608c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to queue
6618c2ecf20Sopenharmony_ci * @lock: a key to the store (e.g. forw_{bat,bcast}_list_lock)
6628c2ecf20Sopenharmony_ci * @head: the shelve to queue it on (e.g. forw_{bat,bcast}_list)
6638c2ecf20Sopenharmony_ci * @send_time: timestamp (jiffies) when the packet is to be sent
6648c2ecf20Sopenharmony_ci *
6658c2ecf20Sopenharmony_ci * This function tries to (re)queue a forwarding packet. Requeuing
6668c2ecf20Sopenharmony_ci * is prevented if the according interface is shutting down
6678c2ecf20Sopenharmony_ci * (e.g. if batadv_forw_packet_list_steal() was called for this
6688c2ecf20Sopenharmony_ci * packet earlier).
6698c2ecf20Sopenharmony_ci *
6708c2ecf20Sopenharmony_ci * Calling batadv_forw_packet_queue() after a call to
6718c2ecf20Sopenharmony_ci * batadv_forw_packet_steal() is forbidden!
6728c2ecf20Sopenharmony_ci *
6738c2ecf20Sopenharmony_ci * Caller needs to ensure that forw_packet->delayed_work was initialized.
6748c2ecf20Sopenharmony_ci */
6758c2ecf20Sopenharmony_cistatic void batadv_forw_packet_queue(struct batadv_forw_packet *forw_packet,
6768c2ecf20Sopenharmony_ci				     spinlock_t *lock, struct hlist_head *head,
6778c2ecf20Sopenharmony_ci				     unsigned long send_time)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	spin_lock_bh(lock);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	/* did purging routine steal it from us? */
6828c2ecf20Sopenharmony_ci	if (batadv_forw_packet_was_stolen(forw_packet)) {
6838c2ecf20Sopenharmony_ci		/* If you got it for free() without trouble, then
6848c2ecf20Sopenharmony_ci		 * don't get back into the queue after stealing...
6858c2ecf20Sopenharmony_ci		 */
6868c2ecf20Sopenharmony_ci		WARN_ONCE(hlist_fake(&forw_packet->cleanup_list),
6878c2ecf20Sopenharmony_ci			  "Requeuing after batadv_forw_packet_steal() not allowed!\n");
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci		spin_unlock_bh(lock);
6908c2ecf20Sopenharmony_ci		return;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	hlist_del_init(&forw_packet->list);
6948c2ecf20Sopenharmony_ci	hlist_add_head(&forw_packet->list, head);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	queue_delayed_work(batadv_event_workqueue,
6978c2ecf20Sopenharmony_ci			   &forw_packet->delayed_work,
6988c2ecf20Sopenharmony_ci			   send_time - jiffies);
6998c2ecf20Sopenharmony_ci	spin_unlock_bh(lock);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci/**
7038c2ecf20Sopenharmony_ci * batadv_forw_packet_bcast_queue() - try to queue a broadcast packet
7048c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
7058c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to queue
7068c2ecf20Sopenharmony_ci * @send_time: timestamp (jiffies) when the packet is to be sent
7078c2ecf20Sopenharmony_ci *
7088c2ecf20Sopenharmony_ci * This function tries to (re)queue a broadcast packet.
7098c2ecf20Sopenharmony_ci *
7108c2ecf20Sopenharmony_ci * Caller needs to ensure that forw_packet->delayed_work was initialized.
7118c2ecf20Sopenharmony_ci */
7128c2ecf20Sopenharmony_cistatic void
7138c2ecf20Sopenharmony_cibatadv_forw_packet_bcast_queue(struct batadv_priv *bat_priv,
7148c2ecf20Sopenharmony_ci			       struct batadv_forw_packet *forw_packet,
7158c2ecf20Sopenharmony_ci			       unsigned long send_time)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bcast_list_lock,
7188c2ecf20Sopenharmony_ci				 &bat_priv->forw_bcast_list, send_time);
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci/**
7228c2ecf20Sopenharmony_ci * batadv_forw_packet_ogmv1_queue() - try to queue an OGMv1 packet
7238c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
7248c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to queue
7258c2ecf20Sopenharmony_ci * @send_time: timestamp (jiffies) when the packet is to be sent
7268c2ecf20Sopenharmony_ci *
7278c2ecf20Sopenharmony_ci * This function tries to (re)queue an OGMv1 packet.
7288c2ecf20Sopenharmony_ci *
7298c2ecf20Sopenharmony_ci * Caller needs to ensure that forw_packet->delayed_work was initialized.
7308c2ecf20Sopenharmony_ci */
7318c2ecf20Sopenharmony_civoid batadv_forw_packet_ogmv1_queue(struct batadv_priv *bat_priv,
7328c2ecf20Sopenharmony_ci				    struct batadv_forw_packet *forw_packet,
7338c2ecf20Sopenharmony_ci				    unsigned long send_time)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bat_list_lock,
7368c2ecf20Sopenharmony_ci				 &bat_priv->forw_bat_list, send_time);
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci/**
7408c2ecf20Sopenharmony_ci * batadv_add_bcast_packet_to_list() - queue broadcast packet for multiple sends
7418c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
7428c2ecf20Sopenharmony_ci * @skb: broadcast packet to add
7438c2ecf20Sopenharmony_ci * @delay: number of jiffies to wait before sending
7448c2ecf20Sopenharmony_ci * @own_packet: true if it is a self-generated broadcast packet
7458c2ecf20Sopenharmony_ci *
7468c2ecf20Sopenharmony_ci * add a broadcast packet to the queue and setup timers. broadcast packets
7478c2ecf20Sopenharmony_ci * are sent multiple times to increase probability for being received.
7488c2ecf20Sopenharmony_ci *
7498c2ecf20Sopenharmony_ci * The skb is not consumed, so the caller should make sure that the
7508c2ecf20Sopenharmony_ci * skb is freed.
7518c2ecf20Sopenharmony_ci *
7528c2ecf20Sopenharmony_ci * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.
7538c2ecf20Sopenharmony_ci */
7548c2ecf20Sopenharmony_ciint batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
7558c2ecf20Sopenharmony_ci				    const struct sk_buff *skb,
7568c2ecf20Sopenharmony_ci				    unsigned long delay,
7578c2ecf20Sopenharmony_ci				    bool own_packet)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	struct batadv_hard_iface *primary_if;
7608c2ecf20Sopenharmony_ci	struct batadv_forw_packet *forw_packet;
7618c2ecf20Sopenharmony_ci	struct batadv_bcast_packet *bcast_packet;
7628c2ecf20Sopenharmony_ci	struct sk_buff *newskb;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
7658c2ecf20Sopenharmony_ci	if (!primary_if)
7668c2ecf20Sopenharmony_ci		goto err;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	newskb = skb_copy(skb, GFP_ATOMIC);
7698c2ecf20Sopenharmony_ci	if (!newskb) {
7708c2ecf20Sopenharmony_ci		batadv_hardif_put(primary_if);
7718c2ecf20Sopenharmony_ci		goto err;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	forw_packet = batadv_forw_packet_alloc(primary_if, NULL,
7758c2ecf20Sopenharmony_ci					       &bat_priv->bcast_queue_left,
7768c2ecf20Sopenharmony_ci					       bat_priv, newskb);
7778c2ecf20Sopenharmony_ci	batadv_hardif_put(primary_if);
7788c2ecf20Sopenharmony_ci	if (!forw_packet)
7798c2ecf20Sopenharmony_ci		goto err_packet_free;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	/* as we have a copy now, it is safe to decrease the TTL */
7828c2ecf20Sopenharmony_ci	bcast_packet = (struct batadv_bcast_packet *)newskb->data;
7838c2ecf20Sopenharmony_ci	bcast_packet->ttl--;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	forw_packet->own = own_packet;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&forw_packet->delayed_work,
7888c2ecf20Sopenharmony_ci			  batadv_send_outstanding_bcast_packet);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	batadv_forw_packet_bcast_queue(bat_priv, forw_packet, jiffies + delay);
7918c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cierr_packet_free:
7948c2ecf20Sopenharmony_ci	kfree_skb(newskb);
7958c2ecf20Sopenharmony_cierr:
7968c2ecf20Sopenharmony_ci	return NETDEV_TX_BUSY;
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci/**
8008c2ecf20Sopenharmony_ci * batadv_forw_packet_bcasts_left() - check if a retransmission is necessary
8018c2ecf20Sopenharmony_ci * @forw_packet: the forwarding packet to check
8028c2ecf20Sopenharmony_ci * @hard_iface: the interface to check on
8038c2ecf20Sopenharmony_ci *
8048c2ecf20Sopenharmony_ci * Checks whether a given packet has any (re)transmissions left on the provided
8058c2ecf20Sopenharmony_ci * interface.
8068c2ecf20Sopenharmony_ci *
8078c2ecf20Sopenharmony_ci * hard_iface may be NULL: In that case the number of transmissions this skb had
8088c2ecf20Sopenharmony_ci * so far is compared with the maximum amount of retransmissions independent of
8098c2ecf20Sopenharmony_ci * any interface instead.
8108c2ecf20Sopenharmony_ci *
8118c2ecf20Sopenharmony_ci * Return: True if (re)transmissions are left, false otherwise.
8128c2ecf20Sopenharmony_ci */
8138c2ecf20Sopenharmony_cistatic bool
8148c2ecf20Sopenharmony_cibatadv_forw_packet_bcasts_left(struct batadv_forw_packet *forw_packet,
8158c2ecf20Sopenharmony_ci			       struct batadv_hard_iface *hard_iface)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	unsigned int max;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	if (hard_iface)
8208c2ecf20Sopenharmony_ci		max = hard_iface->num_bcasts;
8218c2ecf20Sopenharmony_ci	else
8228c2ecf20Sopenharmony_ci		max = BATADV_NUM_BCASTS_MAX;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	return BATADV_SKB_CB(forw_packet->skb)->num_bcasts < max;
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci/**
8288c2ecf20Sopenharmony_ci * batadv_forw_packet_bcasts_inc() - increment retransmission counter of a
8298c2ecf20Sopenharmony_ci *  packet
8308c2ecf20Sopenharmony_ci * @forw_packet: the packet to increase the counter for
8318c2ecf20Sopenharmony_ci */
8328c2ecf20Sopenharmony_cistatic void
8338c2ecf20Sopenharmony_cibatadv_forw_packet_bcasts_inc(struct batadv_forw_packet *forw_packet)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	BATADV_SKB_CB(forw_packet->skb)->num_bcasts++;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/**
8398c2ecf20Sopenharmony_ci * batadv_forw_packet_is_rebroadcast() - check packet for previous transmissions
8408c2ecf20Sopenharmony_ci * @forw_packet: the packet to check
8418c2ecf20Sopenharmony_ci *
8428c2ecf20Sopenharmony_ci * Return: True if this packet was transmitted before, false otherwise.
8438c2ecf20Sopenharmony_ci */
8448c2ecf20Sopenharmony_cibool batadv_forw_packet_is_rebroadcast(struct batadv_forw_packet *forw_packet)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	return BATADV_SKB_CB(forw_packet->skb)->num_bcasts > 0;
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic void batadv_send_outstanding_bcast_packet(struct work_struct *work)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	struct batadv_hard_iface *hard_iface;
8528c2ecf20Sopenharmony_ci	struct batadv_hardif_neigh_node *neigh_node;
8538c2ecf20Sopenharmony_ci	struct delayed_work *delayed_work;
8548c2ecf20Sopenharmony_ci	struct batadv_forw_packet *forw_packet;
8558c2ecf20Sopenharmony_ci	struct batadv_bcast_packet *bcast_packet;
8568c2ecf20Sopenharmony_ci	struct sk_buff *skb1;
8578c2ecf20Sopenharmony_ci	struct net_device *soft_iface;
8588c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv;
8598c2ecf20Sopenharmony_ci	unsigned long send_time = jiffies + msecs_to_jiffies(5);
8608c2ecf20Sopenharmony_ci	bool dropped = false;
8618c2ecf20Sopenharmony_ci	u8 *neigh_addr;
8628c2ecf20Sopenharmony_ci	u8 *orig_neigh;
8638c2ecf20Sopenharmony_ci	int ret = 0;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	delayed_work = to_delayed_work(work);
8668c2ecf20Sopenharmony_ci	forw_packet = container_of(delayed_work, struct batadv_forw_packet,
8678c2ecf20Sopenharmony_ci				   delayed_work);
8688c2ecf20Sopenharmony_ci	soft_iface = forw_packet->if_incoming->soft_iface;
8698c2ecf20Sopenharmony_ci	bat_priv = netdev_priv(soft_iface);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
8728c2ecf20Sopenharmony_ci		dropped = true;
8738c2ecf20Sopenharmony_ci		goto out;
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) {
8778c2ecf20Sopenharmony_ci		dropped = true;
8788c2ecf20Sopenharmony_ci		goto out;
8798c2ecf20Sopenharmony_ci	}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	bcast_packet = (struct batadv_bcast_packet *)forw_packet->skb->data;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	/* rebroadcast packet */
8848c2ecf20Sopenharmony_ci	rcu_read_lock();
8858c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
8868c2ecf20Sopenharmony_ci		if (hard_iface->soft_iface != soft_iface)
8878c2ecf20Sopenharmony_ci			continue;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci		if (!batadv_forw_packet_bcasts_left(forw_packet, hard_iface))
8908c2ecf20Sopenharmony_ci			continue;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci		if (forw_packet->own) {
8938c2ecf20Sopenharmony_ci			neigh_node = NULL;
8948c2ecf20Sopenharmony_ci		} else {
8958c2ecf20Sopenharmony_ci			neigh_addr = eth_hdr(forw_packet->skb)->h_source;
8968c2ecf20Sopenharmony_ci			neigh_node = batadv_hardif_neigh_get(hard_iface,
8978c2ecf20Sopenharmony_ci							     neigh_addr);
8988c2ecf20Sopenharmony_ci		}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci		orig_neigh = neigh_node ? neigh_node->orig : NULL;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci		ret = batadv_hardif_no_broadcast(hard_iface, bcast_packet->orig,
9038c2ecf20Sopenharmony_ci						 orig_neigh);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci		if (ret) {
9068c2ecf20Sopenharmony_ci			char *type;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci			switch (ret) {
9098c2ecf20Sopenharmony_ci			case BATADV_HARDIF_BCAST_NORECIPIENT:
9108c2ecf20Sopenharmony_ci				type = "no neighbor";
9118c2ecf20Sopenharmony_ci				break;
9128c2ecf20Sopenharmony_ci			case BATADV_HARDIF_BCAST_DUPFWD:
9138c2ecf20Sopenharmony_ci				type = "single neighbor is source";
9148c2ecf20Sopenharmony_ci				break;
9158c2ecf20Sopenharmony_ci			case BATADV_HARDIF_BCAST_DUPORIG:
9168c2ecf20Sopenharmony_ci				type = "single neighbor is originator";
9178c2ecf20Sopenharmony_ci				break;
9188c2ecf20Sopenharmony_ci			default:
9198c2ecf20Sopenharmony_ci				type = "unknown";
9208c2ecf20Sopenharmony_ci			}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci			batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "BCAST packet from orig %pM on %s suppressed: %s\n",
9238c2ecf20Sopenharmony_ci				   bcast_packet->orig,
9248c2ecf20Sopenharmony_ci				   hard_iface->net_dev->name, type);
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci			if (neigh_node)
9278c2ecf20Sopenharmony_ci				batadv_hardif_neigh_put(neigh_node);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci			continue;
9308c2ecf20Sopenharmony_ci		}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci		if (neigh_node)
9338c2ecf20Sopenharmony_ci			batadv_hardif_neigh_put(neigh_node);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci		if (!kref_get_unless_zero(&hard_iface->refcount))
9368c2ecf20Sopenharmony_ci			continue;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci		/* send a copy of the saved skb */
9398c2ecf20Sopenharmony_ci		skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
9408c2ecf20Sopenharmony_ci		if (skb1)
9418c2ecf20Sopenharmony_ci			batadv_send_broadcast_skb(skb1, hard_iface);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci		batadv_hardif_put(hard_iface);
9448c2ecf20Sopenharmony_ci	}
9458c2ecf20Sopenharmony_ci	rcu_read_unlock();
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	batadv_forw_packet_bcasts_inc(forw_packet);
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	/* if we still have some more bcasts to send */
9508c2ecf20Sopenharmony_ci	if (batadv_forw_packet_bcasts_left(forw_packet, NULL)) {
9518c2ecf20Sopenharmony_ci		batadv_forw_packet_bcast_queue(bat_priv, forw_packet,
9528c2ecf20Sopenharmony_ci					       send_time);
9538c2ecf20Sopenharmony_ci		return;
9548c2ecf20Sopenharmony_ci	}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ciout:
9578c2ecf20Sopenharmony_ci	/* do we get something for free()? */
9588c2ecf20Sopenharmony_ci	if (batadv_forw_packet_steal(forw_packet,
9598c2ecf20Sopenharmony_ci				     &bat_priv->forw_bcast_list_lock))
9608c2ecf20Sopenharmony_ci		batadv_forw_packet_free(forw_packet, dropped);
9618c2ecf20Sopenharmony_ci}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci/**
9648c2ecf20Sopenharmony_ci * batadv_purge_outstanding_packets() - stop/purge scheduled bcast/OGMv1 packets
9658c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
9668c2ecf20Sopenharmony_ci * @hard_iface: the hard interface to cancel and purge bcast/ogm packets on
9678c2ecf20Sopenharmony_ci *
9688c2ecf20Sopenharmony_ci * This method cancels and purges any broadcast and OGMv1 packet on the given
9698c2ecf20Sopenharmony_ci * hard_iface. If hard_iface is NULL, broadcast and OGMv1 packets on all hard
9708c2ecf20Sopenharmony_ci * interfaces will be canceled and purged.
9718c2ecf20Sopenharmony_ci *
9728c2ecf20Sopenharmony_ci * This function might sleep.
9738c2ecf20Sopenharmony_ci */
9748c2ecf20Sopenharmony_civoid
9758c2ecf20Sopenharmony_cibatadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
9768c2ecf20Sopenharmony_ci				 const struct batadv_hard_iface *hard_iface)
9778c2ecf20Sopenharmony_ci{
9788c2ecf20Sopenharmony_ci	struct hlist_head head = HLIST_HEAD_INIT;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	if (hard_iface)
9818c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
9828c2ecf20Sopenharmony_ci			   "%s(): %s\n",
9838c2ecf20Sopenharmony_ci			   __func__, hard_iface->net_dev->name);
9848c2ecf20Sopenharmony_ci	else
9858c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
9868c2ecf20Sopenharmony_ci			   "%s()\n", __func__);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	/* claim bcast list for free() */
9898c2ecf20Sopenharmony_ci	spin_lock_bh(&bat_priv->forw_bcast_list_lock);
9908c2ecf20Sopenharmony_ci	batadv_forw_packet_list_steal(&bat_priv->forw_bcast_list, &head,
9918c2ecf20Sopenharmony_ci				      hard_iface);
9928c2ecf20Sopenharmony_ci	spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	/* claim batman packet list for free() */
9958c2ecf20Sopenharmony_ci	spin_lock_bh(&bat_priv->forw_bat_list_lock);
9968c2ecf20Sopenharmony_ci	batadv_forw_packet_list_steal(&bat_priv->forw_bat_list, &head,
9978c2ecf20Sopenharmony_ci				      hard_iface);
9988c2ecf20Sopenharmony_ci	spin_unlock_bh(&bat_priv->forw_bat_list_lock);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* then cancel or wait for packet workers to finish and free */
10018c2ecf20Sopenharmony_ci	batadv_forw_packet_list_free(&head);
10028c2ecf20Sopenharmony_ci}
1003