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