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