xref: /linux/net/batman-adv/bat_iv_ogm.c (revision 32a6799255525d6ea4da0f7e9e0e521ad9560a46)
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 "bat_iv_ogm.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/bitmap.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/byteorder/generic.h>
15 #include <linux/cache.h>
16 #include <linux/compiler.h>
17 #include <linux/container_of.h>
18 #include <linux/errno.h>
19 #include <linux/etherdevice.h>
20 #include <linux/gfp.h>
21 #include <linux/if_ether.h>
22 #include <linux/init.h>
23 #include <linux/jiffies.h>
24 #include <linux/kref.h>
25 #include <linux/list.h>
26 #include <linux/lockdep.h>
27 #include <linux/minmax.h>
28 #include <linux/mutex.h>
29 #include <linux/netdevice.h>
30 #include <linux/netlink.h>
31 #include <linux/pkt_sched.h>
32 #include <linux/printk.h>
33 #include <linux/random.h>
34 #include <linux/rculist.h>
35 #include <linux/rcupdate.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/stddef.h>
40 #include <linux/string.h>
41 #include <linux/string_choices.h>
42 #include <linux/types.h>
43 #include <linux/workqueue.h>
44 #include <net/genetlink.h>
45 #include <net/netlink.h>
46 #include <uapi/linux/batadv_packet.h>
47 #include <uapi/linux/batman_adv.h>
48 
49 #include "bat_algo.h"
50 #include "bitarray.h"
51 #include "gateway_client.h"
52 #include "hard-interface.h"
53 #include "hash.h"
54 #include "log.h"
55 #include "netlink.h"
56 #include "originator.h"
57 #include "routing.h"
58 #include "send.h"
59 #include "translation-table.h"
60 #include "tvlv.h"
61 
62 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
63 
64 /**
65  * enum batadv_dup_status - duplicate status
66  */
67 enum batadv_dup_status {
68 	/** @BATADV_NO_DUP: the packet is no duplicate */
69 	BATADV_NO_DUP = 0,
70 
71 	/**
72 	 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
73 	 *  the neighbor)
74 	 */
75 	BATADV_ORIG_DUP,
76 
77 	/** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
78 	BATADV_NEIGH_DUP,
79 
80 	/**
81 	 * @BATADV_PROTECTED: originator is currently protected (after reboot)
82 	 */
83 	BATADV_PROTECTED,
84 };
85 
86 /**
87  * batadv_ring_buffer_set() - update the ring buffer with the given value
88  * @lq_recv: pointer to the ring buffer
89  * @lq_index: index to store the value at
90  * @value: value to store in the ring buffer
91  */
92 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
93 {
94 	lq_recv[*lq_index] = value;
95 	*lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
96 }
97 
98 /**
99  * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
100  * in the given ring buffer
101  * @lq_recv: pointer to the ring buffer
102  *
103  * Return: computed average value.
104  */
105 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
106 {
107 	const u8 *ptr;
108 	u16 count = 0;
109 	u16 i = 0;
110 	u16 sum = 0;
111 
112 	ptr = lq_recv;
113 
114 	while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
115 		if (*ptr != 0) {
116 			count++;
117 			sum += *ptr;
118 		}
119 
120 		i++;
121 		ptr++;
122 	}
123 
124 	if (count == 0)
125 		return 0;
126 
127 	return (u8)(sum / count);
128 }
129 
130 /**
131  * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
132  *  originator
133  * @bat_priv: the bat priv with all the mesh interface information
134  * @addr: mac address of the originator
135  *
136  * Return: the originator object corresponding to the passed mac address or NULL
137  * on failure.
138  * If the object does not exist, it is created and initialised.
139  */
140 static struct batadv_orig_node *
141 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
142 {
143 	struct batadv_orig_node *orig_node;
144 	int hash_added;
145 
146 	orig_node = batadv_orig_hash_find(bat_priv, addr);
147 	if (orig_node)
148 		return orig_node;
149 
150 	orig_node = batadv_orig_node_new(bat_priv, addr);
151 	if (!orig_node)
152 		return NULL;
153 
154 	spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
155 
156 	kref_get(&orig_node->refcount);
157 	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
158 				     batadv_choose_orig, orig_node,
159 				     &orig_node->hash_entry);
160 	if (hash_added != 0)
161 		goto free_orig_node_hash;
162 
163 	return orig_node;
164 
165 free_orig_node_hash:
166 	/* reference for batadv_hash_add */
167 	batadv_orig_node_put(orig_node);
168 	/* reference from batadv_orig_node_new */
169 	batadv_orig_node_put(orig_node);
170 
171 	return NULL;
172 }
173 
174 static struct batadv_neigh_node *
175 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
176 			const u8 *neigh_addr,
177 			struct batadv_orig_node *orig_node)
178 {
179 	struct batadv_neigh_node *neigh_node;
180 
181 	neigh_node = batadv_neigh_node_get_or_create(orig_node,
182 						     hard_iface, neigh_addr);
183 	return neigh_node;
184 }
185 
186 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
187 {
188 	struct batadv_ogm_packet *batadv_ogm_packet;
189 	unsigned char *ogm_buff;
190 	u32 random_seqno;
191 
192 	mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
193 
194 	/* randomize initial seqno to avoid collision */
195 	get_random_bytes(&random_seqno, sizeof(random_seqno));
196 	atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
197 
198 	hard_iface->bat_iv.ogm_buff.len = BATADV_OGM_HLEN;
199 	hard_iface->bat_iv.ogm_buff.capacity = BATADV_OGM_HLEN;
200 	hard_iface->bat_iv.ogm_buff.header_length = BATADV_OGM_HLEN;
201 
202 	ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff.capacity, GFP_ATOMIC);
203 	if (!ogm_buff) {
204 		mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
205 		return -ENOMEM;
206 	}
207 
208 	hard_iface->bat_iv.ogm_buff.buf = ogm_buff;
209 
210 	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
211 	batadv_ogm_packet->packet_type = BATADV_IV_OGM;
212 	batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
213 	batadv_ogm_packet->ttl = 2;
214 	batadv_ogm_packet->flags = BATADV_NO_FLAGS;
215 	batadv_ogm_packet->reserved = 0;
216 	batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
217 
218 	mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
219 
220 	return 0;
221 }
222 
223 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
224 {
225 	mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
226 
227 	kfree(hard_iface->bat_iv.ogm_buff.buf);
228 	memset(&hard_iface->bat_iv.ogm_buff, 0,
229 	       sizeof(hard_iface->bat_iv.ogm_buff));
230 
231 	mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
232 
233 	disable_delayed_work_sync(&hard_iface->bat_iv.reschedule_work);
234 }
235 
236 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
237 {
238 	struct batadv_ogm_packet *batadv_ogm_packet;
239 	void *ogm_buff;
240 
241 	mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
242 
243 	ogm_buff = hard_iface->bat_iv.ogm_buff.buf;
244 	if (!ogm_buff)
245 		goto unlock;
246 
247 	batadv_ogm_packet = ogm_buff;
248 	ether_addr_copy(batadv_ogm_packet->orig,
249 			hard_iface->net_dev->dev_addr);
250 	ether_addr_copy(batadv_ogm_packet->prev_sender,
251 			hard_iface->net_dev->dev_addr);
252 
253 unlock:
254 	mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
255 }
256 
257 static void
258 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
259 {
260 	struct batadv_ogm_packet *batadv_ogm_packet;
261 	void *ogm_buff;
262 
263 	mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
264 
265 	ogm_buff = hard_iface->bat_iv.ogm_buff.buf;
266 	if (!ogm_buff)
267 		goto unlock;
268 
269 	batadv_ogm_packet = ogm_buff;
270 	batadv_ogm_packet->ttl = BATADV_TTL;
271 
272 unlock:
273 	mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
274 }
275 
276 /* when do we schedule our own ogm to be sent */
277 static unsigned long
278 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
279 {
280 	unsigned int msecs;
281 
282 	msecs = READ_ONCE(bat_priv->orig_interval) - BATADV_JITTER;
283 	msecs += get_random_u32_below(2 * BATADV_JITTER);
284 
285 	return jiffies + msecs_to_jiffies(msecs);
286 }
287 
288 /* when do we schedule a ogm packet to be sent */
289 static unsigned long batadv_iv_ogm_fwd_send_time(void)
290 {
291 	return jiffies + msecs_to_jiffies(get_random_u32_below(BATADV_JITTER / 2));
292 }
293 
294 /* apply hop penalty for a normal link */
295 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
296 {
297 	int hop_penalty = READ_ONCE(bat_priv->hop_penalty);
298 	int new_tq;
299 
300 	new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
301 	new_tq /= BATADV_TQ_MAX_VALUE;
302 
303 	return new_tq;
304 }
305 
306 /**
307  * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
308  * @buff_pos: current position in the skb
309  * @packet_len: total length of the skb
310  * @ogm_packet: potential OGM in buffer
311  *
312  * Return: true if there is enough space for another OGM, false otherwise.
313  */
314 static bool
315 batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
316 			  const struct batadv_ogm_packet *ogm_packet)
317 {
318 	int next_buff_pos = 0;
319 	u16 tvlv_len;
320 
321 	/* check if there is enough space for the header */
322 	next_buff_pos += buff_pos + sizeof(*ogm_packet);
323 	if (next_buff_pos > packet_len)
324 		return false;
325 
326 	tvlv_len = ntohs(ogm_packet->tvlv_len);
327 
328 	/* the fields of an aggregated OGM are accessed assuming (at least)
329 	 * 2-byte alignment, so a following OGM must start at an even offset.
330 	 */
331 	if (tvlv_len & 1)
332 		return false;
333 
334 	/* check if there is enough space for the optional TVLV */
335 	next_buff_pos += tvlv_len;
336 
337 	return next_buff_pos <= packet_len;
338 }
339 
340 /* send a batman ogm to a given interface */
341 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
342 				     struct batadv_hard_iface *hard_iface)
343 {
344 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
345 	const char *fwd_str;
346 	u8 packet_num;
347 	int buff_pos;
348 	struct batadv_ogm_packet *batadv_ogm_packet;
349 	struct sk_buff *skb;
350 	u8 *packet_pos;
351 
352 	if (hard_iface->if_status != BATADV_IF_ACTIVE)
353 		return;
354 
355 	packet_num = 0;
356 	buff_pos = 0;
357 	packet_pos = forw_packet->skb->data;
358 	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
359 
360 	/* adjust all flags and log packets */
361 	while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
362 					 batadv_ogm_packet)) {
363 		/* we might have aggregated direct link packets with an
364 		 * ordinary base packet
365 		 */
366 		if (test_bit(packet_num, forw_packet->direct_link_flags) &&
367 		    forw_packet->if_incoming == hard_iface)
368 			batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
369 		else
370 			batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
371 
372 		if (packet_num > 0 || !forw_packet->own)
373 			fwd_str = "Forwarding";
374 		else
375 			fwd_str = "Sending own";
376 
377 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
378 			   "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
379 			   fwd_str, (packet_num > 0 ? "aggregated " : ""),
380 			   batadv_ogm_packet->orig,
381 			   ntohl(batadv_ogm_packet->seqno),
382 			   batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
383 			   str_on_off(batadv_ogm_packet->flags & BATADV_DIRECTLINK),
384 			   hard_iface->net_dev->name,
385 			   hard_iface->net_dev->dev_addr);
386 
387 		buff_pos += BATADV_OGM_HLEN;
388 		buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
389 		packet_num++;
390 		packet_pos = forw_packet->skb->data + buff_pos;
391 		batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
392 	}
393 
394 	/* create clone because function is called more than once */
395 	skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
396 	if (skb) {
397 		batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
398 		batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
399 				   skb->len + ETH_HLEN);
400 		batadv_send_broadcast_skb(skb, hard_iface);
401 	}
402 }
403 
404 /* send a batman ogm packet */
405 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
406 {
407 	struct net_device *mesh_iface;
408 
409 	if (!forw_packet->if_incoming) {
410 		pr_err("Error - can't forward packet: incoming iface not specified\n");
411 		return;
412 	}
413 
414 	mesh_iface = forw_packet->if_incoming->mesh_iface;
415 
416 	if (WARN_ON(!forw_packet->if_outgoing))
417 		return;
418 
419 	if (forw_packet->if_outgoing->mesh_iface != mesh_iface) {
420 		pr_warn("%s: mesh interface switch for queued OGM\n", __func__);
421 		return;
422 	}
423 
424 	if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
425 		return;
426 
427 	/* only for one specific outgoing interface */
428 	batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
429 }
430 
431 /**
432  * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
433  *  existing forward packet
434  * @new_bat_ogm_packet: OGM packet to be aggregated
435  * @bat_priv: the bat priv with all the mesh interface information
436  * @packet_len: (total) length of the OGM
437  * @send_time: timestamp (jiffies) when the packet is to be sent
438  * @directlink: true if this is a direct link packet
439  * @if_incoming: interface where the packet was received
440  * @if_outgoing: interface for which the retransmission should be considered
441  * @forw_packet: the forwarded packet which should be checked
442  *
443  * Return: true if new_packet can be aggregated with forw_packet
444  */
445 static bool
446 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
447 			    struct batadv_priv *bat_priv,
448 			    int packet_len, unsigned long send_time,
449 			    bool directlink,
450 			    const struct batadv_hard_iface *if_incoming,
451 			    const struct batadv_hard_iface *if_outgoing,
452 			    const struct batadv_forw_packet *forw_packet)
453 {
454 	struct batadv_ogm_packet *batadv_ogm_packet;
455 	unsigned int aggregated_bytes = forw_packet->packet_len + packet_len;
456 	struct batadv_hard_iface *primary_if = NULL;
457 	u8 packet_num = forw_packet->num_packets;
458 	bool res = false;
459 	unsigned long aggregation_end_time;
460 	unsigned int max_bytes;
461 
462 	batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
463 	aggregation_end_time = send_time;
464 	aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
465 
466 	max_bytes = min_t(unsigned int, if_outgoing->net_dev->mtu,
467 			  BATADV_MAX_AGGREGATION_BYTES);
468 
469 	/* we can aggregate the current packet to this aggregated packet
470 	 * if:
471 	 *
472 	 * - the send time is within our MAX_AGGREGATION_MS time
473 	 * - the resulting packet won't be bigger than
474 	 *   MAX_AGGREGATION_BYTES and MTU of the outgoing interface
475 	 * - the number of packets is lower than MAX_AGGREGATION_PACKETS
476 	 * otherwise aggregation is not possible
477 	 */
478 	if (!time_before(send_time, forw_packet->send_time) ||
479 	    !time_after_eq(aggregation_end_time, forw_packet->send_time))
480 		return false;
481 
482 	if (aggregated_bytes > max_bytes)
483 		return false;
484 
485 	if (skb_tailroom(forw_packet->skb) < packet_len)
486 		return false;
487 
488 	if (packet_num >= BATADV_MAX_AGGREGATION_PACKETS)
489 		return false;
490 
491 	/* packet is not leaving on the same interface. */
492 	if (forw_packet->if_outgoing != if_outgoing)
493 		return false;
494 
495 	/* check aggregation compatibility
496 	 * -> direct link packets are broadcasted on
497 	 *    their interface only
498 	 * -> aggregate packet if the current packet is
499 	 *    a "global" packet as well as the base
500 	 *    packet
501 	 */
502 	primary_if = batadv_primary_if_get_selected(bat_priv);
503 	if (!primary_if)
504 		return false;
505 
506 	/* packets without direct link flag and high TTL
507 	 * are flooded through the net
508 	 */
509 	if (!directlink &&
510 	    !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
511 	    batadv_ogm_packet->ttl != 1 &&
512 
513 	    /* own packets originating non-primary
514 	     * interfaces leave only that interface
515 	     */
516 	    (!forw_packet->own ||
517 	     forw_packet->if_incoming == primary_if)) {
518 		res = true;
519 		goto out;
520 	}
521 
522 	/* if the incoming packet is sent via this one
523 	 * interface only - we still can aggregate
524 	 */
525 	if (directlink &&
526 	    new_bat_ogm_packet->ttl == 1 &&
527 	    forw_packet->if_incoming == if_incoming &&
528 
529 	    /* packets from direct neighbors or
530 	     * own secondary interface packets
531 	     * (= secondary interface packets in general)
532 	     */
533 	    (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
534 	     (forw_packet->own &&
535 	      forw_packet->if_incoming != primary_if))) {
536 		res = true;
537 		goto out;
538 	}
539 
540 out:
541 	batadv_hardif_put(primary_if);
542 	return res;
543 }
544 
545 /**
546  * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
547  *  packet to it.
548  * @packet_buff: pointer to the OGM
549  * @packet_len: (total) length of the OGM
550  * @send_time: timestamp (jiffies) when the packet is to be sent
551  * @direct_link: whether this OGM has direct link status
552  * @if_incoming: interface where the packet was received
553  * @if_outgoing: interface for which the retransmission should be considered
554  * @own_packet: true if it is a self-generated ogm
555  *
556  * Return: whether forward packet was scheduled
557  */
558 static bool batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
559 					int packet_len, unsigned long send_time,
560 					bool direct_link,
561 					struct batadv_hard_iface *if_incoming,
562 					struct batadv_hard_iface *if_outgoing,
563 					int own_packet)
564 {
565 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
566 	struct batadv_forw_packet *forw_packet_aggr;
567 	struct sk_buff *skb;
568 	unsigned char *skb_buff;
569 	unsigned int skb_size;
570 	atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
571 
572 	if (READ_ONCE(bat_priv->aggregated_ogms))
573 		skb_size = max_t(unsigned int, BATADV_MAX_AGGREGATION_BYTES,
574 				 packet_len);
575 	else
576 		skb_size = packet_len;
577 
578 	skb_size += ETH_HLEN;
579 
580 	skb = netdev_alloc_skb_ip_align(NULL, skb_size);
581 	if (!skb)
582 		return false;
583 
584 	forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
585 						    queue_left, bat_priv, skb);
586 	if (!forw_packet_aggr) {
587 		kfree_skb(skb);
588 		return false;
589 	}
590 
591 	forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
592 	skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
593 
594 	skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
595 	forw_packet_aggr->packet_len = packet_len;
596 	memcpy(skb_buff, packet_buff, packet_len);
597 
598 	forw_packet_aggr->own = own_packet;
599 	bitmap_zero(forw_packet_aggr->direct_link_flags,
600 		    BATADV_MAX_AGGREGATION_PACKETS);
601 	forw_packet_aggr->send_time = send_time;
602 
603 	/* save packet direct link flag status */
604 	if (direct_link)
605 		set_bit(0, forw_packet_aggr->direct_link_flags);
606 
607 	INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
608 			  batadv_iv_send_outstanding_bat_ogm_packet);
609 
610 	batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
611 
612 	return true;
613 }
614 
615 /* aggregate a new packet into the existing ogm packet */
616 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
617 				    const unsigned char *packet_buff,
618 				    int packet_len, bool direct_link)
619 {
620 	skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
621 	forw_packet_aggr->packet_len += packet_len;
622 
623 	/* save packet direct link flag status */
624 	if (direct_link)
625 		set_bit(forw_packet_aggr->num_packets,
626 			forw_packet_aggr->direct_link_flags);
627 
628 	forw_packet_aggr->num_packets++;
629 }
630 
631 /**
632  * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
633  * @bat_priv: the bat priv with all the mesh interface information
634  * @packet_buff: pointer to the OGM
635  * @packet_len: (total) length of the OGM
636  * @if_incoming: interface where the packet was received
637  * @if_outgoing: interface for which the retransmission should be considered
638  * @own_packet: true if it is a self-generated ogm
639  * @send_time: timestamp (jiffies) when the packet is to be sent
640  *
641  * Return: whether forward packet was scheduled
642  */
643 static bool batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
644 				    unsigned char *packet_buff,
645 				    int packet_len,
646 				    struct batadv_hard_iface *if_incoming,
647 				    struct batadv_hard_iface *if_outgoing,
648 				    int own_packet, unsigned long send_time)
649 {
650 	/* _aggr -> pointer to the packet we want to aggregate with
651 	 * _pos -> pointer to the position in the queue
652 	 */
653 	struct batadv_forw_packet *forw_packet_aggr = NULL;
654 	struct batadv_forw_packet *forw_packet_pos = NULL;
655 	struct batadv_ogm_packet *batadv_ogm_packet;
656 	bool direct_link;
657 	unsigned long max_aggregation_jiffies;
658 	bool aggregated_ogms;
659 
660 	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
661 	direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
662 	max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
663 
664 	/* find position for the packet in the forward queue */
665 	spin_lock_bh(&bat_priv->forw_bat_list_lock);
666 	aggregated_ogms = READ_ONCE(bat_priv->aggregated_ogms);
667 
668 	/* own packets are not to be aggregated */
669 	if (aggregated_ogms && !own_packet) {
670 		hlist_for_each_entry(forw_packet_pos,
671 				     &bat_priv->forw_bat_list, list) {
672 			if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
673 							bat_priv, packet_len,
674 							send_time, direct_link,
675 							if_incoming,
676 							if_outgoing,
677 							forw_packet_pos)) {
678 				forw_packet_aggr = forw_packet_pos;
679 				break;
680 			}
681 		}
682 	}
683 
684 	/* nothing to aggregate with - either aggregation disabled or no
685 	 * suitable aggregation packet found
686 	 */
687 	if (!forw_packet_aggr) {
688 		/* the following section can run without the lock */
689 		spin_unlock_bh(&bat_priv->forw_bat_list_lock);
690 
691 		/* if we could not aggregate this packet with one of the others
692 		 * we hold it back for a while, so that it might be aggregated
693 		 * later on
694 		 */
695 		if (!own_packet && aggregated_ogms)
696 			send_time += max_aggregation_jiffies;
697 
698 		return batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
699 						   send_time, direct_link,
700 						   if_incoming, if_outgoing,
701 						   own_packet);
702 	} else {
703 		batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
704 					packet_len, direct_link);
705 		spin_unlock_bh(&bat_priv->forw_bat_list_lock);
706 
707 		return true;
708 	}
709 }
710 
711 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
712 				  const struct ethhdr *ethhdr,
713 				  struct batadv_ogm_packet *batadv_ogm_packet,
714 				  bool is_single_hop_neigh,
715 				  bool is_from_best_next_hop,
716 				  struct batadv_hard_iface *if_incoming,
717 				  struct batadv_hard_iface *if_outgoing)
718 {
719 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
720 	u16 tvlv_len;
721 
722 	if (batadv_ogm_packet->ttl <= 1) {
723 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
724 		return;
725 	}
726 
727 	if (!is_from_best_next_hop) {
728 		/* Mark the forwarded packet when it is not coming from our
729 		 * best next hop. We still need to forward the packet for our
730 		 * neighbor link quality detection to work in case the packet
731 		 * originated from a single hop neighbor. Otherwise we can
732 		 * simply drop the ogm.
733 		 */
734 		if (is_single_hop_neigh)
735 			batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
736 		else
737 			return;
738 	}
739 
740 	tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
741 
742 	batadv_ogm_packet->ttl--;
743 	ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
744 
745 	/* apply hop penalty */
746 	batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
747 						   bat_priv);
748 
749 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
750 		   "Forwarding packet: tq: %i, ttl: %i\n",
751 		   batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
752 
753 	if (is_single_hop_neigh)
754 		batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
755 	else
756 		batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
757 
758 	batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
759 				BATADV_OGM_HLEN + tvlv_len,
760 				if_incoming, if_outgoing, 0,
761 				batadv_iv_ogm_fwd_send_time());
762 }
763 
764 /**
765  * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
766  *  for the given interface
767  * @hard_iface: the interface for which the windows have to be shifted
768  */
769 static void
770 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
771 {
772 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
773 	struct batadv_hashtable *hash = bat_priv->orig_hash;
774 	struct hlist_head *head;
775 	struct batadv_orig_node *orig_node;
776 	struct batadv_orig_ifinfo *orig_ifinfo;
777 	unsigned long *word;
778 	u32 i;
779 	u8 *w;
780 
781 	for (i = 0; i < hash->size; i++) {
782 		head = &hash->table[i];
783 
784 		rcu_read_lock();
785 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
786 			hlist_for_each_entry_rcu(orig_ifinfo,
787 						 &orig_node->ifinfo_list,
788 						 list) {
789 				if (orig_ifinfo->if_outgoing != hard_iface)
790 					continue;
791 
792 				spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
793 				word = orig_ifinfo->bat_iv.bcast_own;
794 				batadv_bit_get_packet(bat_priv, word, 1, 0);
795 				w = &orig_ifinfo->bat_iv.bcast_own_sum;
796 				*w = bitmap_weight(word,
797 						   BATADV_TQ_LOCAL_WINDOW_SIZE);
798 				spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
799 			}
800 		}
801 		rcu_read_unlock();
802 	}
803 }
804 
805 /**
806  * batadv_iv_ogm_schedule_buff() - schedule submission of hardif ogm buffer
807  * @hard_iface: interface whose ogm buffer should be transmitted
808  */
809 static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
810 {
811 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
812 	struct batadv_ogm_buf *ogm_buff = &hard_iface->bat_iv.ogm_buff;
813 	struct batadv_ogm_packet *batadv_ogm_packet;
814 	struct batadv_hard_iface *primary_if, *tmp_hard_iface;
815 	struct list_head *iter;
816 	u32 seqno;
817 	u16 tvlv_len = 0;
818 	unsigned long send_time;
819 	bool reschedule = false;
820 	bool scheduled;
821 	int ret;
822 
823 	lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
824 
825 	/* interface already disabled by batadv_iv_ogm_iface_disable */
826 	if (!ogm_buff->buf)
827 		return;
828 
829 	/* the interface gets activated here to avoid race conditions between
830 	 * the moment of activating the interface in
831 	 * hardif_activate_interface() where the originator mac is set and
832 	 * outdated packets (especially uninitialized mac addresses) in the
833 	 * packet queue
834 	 */
835 	if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
836 		hard_iface->if_status = BATADV_IF_ACTIVE;
837 
838 	primary_if = batadv_primary_if_get_selected(bat_priv);
839 
840 	if (hard_iface == primary_if) {
841 		/* tt changes have to be committed before the tvlv data is
842 		 * appended as it may alter the tt tvlv container
843 		 */
844 		batadv_tt_local_commit_changes(bat_priv);
845 		ret = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff);
846 		if (ret < 0) {
847 			reschedule = true;
848 			goto out;
849 		}
850 
851 		tvlv_len = ret;
852 	}
853 
854 	batadv_ogm_packet = ogm_buff->buf;
855 	batadv_ogm_packet->tvlv_len = htons(tvlv_len);
856 
857 	/* change sequence number to network order */
858 	seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
859 	batadv_ogm_packet->seqno = htonl(seqno);
860 	atomic_inc(&hard_iface->bat_iv.ogm_seqno);
861 
862 	batadv_iv_ogm_slide_own_bcast_window(hard_iface);
863 
864 	send_time = batadv_iv_ogm_emit_send_time(bat_priv);
865 
866 	if (hard_iface != primary_if) {
867 		/* OGMs from secondary interfaces are only scheduled on their
868 		 * respective interfaces.
869 		 */
870 		scheduled = batadv_iv_ogm_queue_add(bat_priv, ogm_buff->buf, ogm_buff->len,
871 						    hard_iface, hard_iface, 1, send_time);
872 		if (!scheduled)
873 			reschedule = true;
874 
875 		goto out;
876 	}
877 
878 	/* OGMs from primary interfaces are scheduled on all
879 	 * interfaces.
880 	 */
881 	rcu_read_lock();
882 	netdev_for_each_lower_private_rcu(hard_iface->mesh_iface, tmp_hard_iface, iter) {
883 		if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
884 			continue;
885 
886 		scheduled = batadv_iv_ogm_queue_add(bat_priv, ogm_buff->buf,
887 						    ogm_buff->len, hard_iface,
888 						    tmp_hard_iface, 1, send_time);
889 		batadv_hardif_put(tmp_hard_iface);
890 
891 		if (!scheduled && tmp_hard_iface == hard_iface)
892 			reschedule = true;
893 	}
894 	rcu_read_unlock();
895 
896 out:
897 	if (reschedule) {
898 		/* there was a failure scheduling the own forward packet.
899 		 * as result, the batadv_iv_send_outstanding_bat_ogm_packet()
900 		 * work item is no longer scheduled. it is therefore necessary
901 		 * to reschedule it manually
902 		 */
903 		queue_delayed_work(batadv_event_workqueue,
904 				   &hard_iface->bat_iv.reschedule_work,
905 				   msecs_to_jiffies(READ_ONCE(bat_priv->orig_interval)));
906 	}
907 
908 	batadv_hardif_put(primary_if);
909 }
910 
911 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
912 {
913 	if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
914 	    hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
915 		return;
916 
917 	mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
918 	batadv_iv_ogm_schedule_buff(hard_iface);
919 	mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
920 }
921 
922 static void batadv_iv_ogm_reschedule(struct work_struct *work)
923 {
924 	struct delayed_work *delayed_work = to_delayed_work(work);
925 	struct batadv_hard_iface *hard_iface;
926 
927 	hard_iface = container_of(delayed_work,
928 				  struct batadv_hard_iface,
929 				  bat_iv.reschedule_work);
930 	batadv_iv_ogm_schedule(hard_iface);
931 }
932 
933 /**
934  * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over interface
935  * @orig_node: originator which reproadcasted the OGMs directly
936  * @if_outgoing: interface which transmitted the original OGM and received the
937  *  direct rebroadcast
938  *
939  * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
940  *  an originator and directly (without intermediate hop) received by a specific
941  *  interface
942  */
943 static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
944 				    struct batadv_hard_iface *if_outgoing)
945 {
946 	struct batadv_orig_ifinfo *orig_ifinfo;
947 	u8 sum;
948 
949 	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
950 	if (!orig_ifinfo)
951 		return 0;
952 
953 	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
954 	sum = orig_ifinfo->bat_iv.bcast_own_sum;
955 	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
956 
957 	batadv_orig_ifinfo_put(orig_ifinfo);
958 
959 	return sum;
960 }
961 
962 /**
963  * batadv_iv_ogm_neigh_ifinfo_sum() - Get bcast_own sum for a last-hop neighbor
964  * @bat_priv: the bat priv with all the mesh interface information
965  * @neigh_node: last-hop neighbor of an originator
966  *
967  * Return: Number of replied (rebroadcasted) OGMs for the originator currently
968  * announced by the neighbor. Returns 0 if the neighbor's originator entry is
969  * not available anymore.
970  */
971 static u8 batadv_iv_ogm_neigh_ifinfo_sum(struct batadv_priv *bat_priv,
972 					 const struct batadv_neigh_node *neigh_node)
973 {
974 	struct batadv_orig_node *orig_neigh;
975 	u8 sum;
976 
977 	orig_neigh = batadv_orig_hash_find(bat_priv, neigh_node->addr);
978 	if (!orig_neigh)
979 		return 0;
980 
981 	sum = batadv_iv_orig_ifinfo_sum(orig_neigh, neigh_node->if_incoming);
982 	batadv_orig_node_put(orig_neigh);
983 
984 	return sum;
985 }
986 
987 /**
988  * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
989  *  originator
990  * @bat_priv: the bat priv with all the mesh interface information
991  * @orig_node: the orig node who originally emitted the ogm packet
992  * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
993  * @ethhdr: Ethernet header of the OGM
994  * @batadv_ogm_packet: the ogm packet
995  * @if_incoming: interface where the packet was received
996  * @if_outgoing: interface for which the retransmission should be considered
997  * @dup_status: the duplicate status of this ogm packet.
998  */
999 static void
1000 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1001 			  struct batadv_orig_node *orig_node,
1002 			  struct batadv_orig_ifinfo *orig_ifinfo,
1003 			  const struct ethhdr *ethhdr,
1004 			  const struct batadv_ogm_packet *batadv_ogm_packet,
1005 			  struct batadv_hard_iface *if_incoming,
1006 			  struct batadv_hard_iface *if_outgoing,
1007 			  enum batadv_dup_status dup_status)
1008 {
1009 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1010 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1011 	struct batadv_neigh_node *neigh_node = NULL;
1012 	struct batadv_neigh_node *tmp_neigh_node = NULL;
1013 	struct batadv_neigh_node *router = NULL;
1014 	u8 sum_orig, sum_neigh;
1015 	u8 *neigh_addr;
1016 	u8 tq_avg;
1017 
1018 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1019 		   "%s(): Searching and updating originator entry of received packet\n",
1020 		   __func__);
1021 
1022 	rcu_read_lock();
1023 	hlist_for_each_entry_rcu(tmp_neigh_node,
1024 				 &orig_node->neigh_list, list) {
1025 		neigh_addr = tmp_neigh_node->addr;
1026 		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1027 		    tmp_neigh_node->if_incoming == if_incoming &&
1028 		    kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1029 			if (WARN(neigh_node, "too many matching neigh_nodes"))
1030 				batadv_neigh_node_put(neigh_node);
1031 			neigh_node = tmp_neigh_node;
1032 			continue;
1033 		}
1034 
1035 		if (dup_status != BATADV_NO_DUP)
1036 			continue;
1037 
1038 		/* only update the entry for this outgoing interface */
1039 		neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1040 						       if_outgoing);
1041 		if (!neigh_ifinfo)
1042 			continue;
1043 
1044 		spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1045 		batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1046 				       &neigh_ifinfo->bat_iv.tq_index, 0);
1047 		tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1048 		neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1049 		spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1050 
1051 		batadv_neigh_ifinfo_put(neigh_ifinfo);
1052 		neigh_ifinfo = NULL;
1053 	}
1054 
1055 	if (!neigh_node) {
1056 		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1057 						     ethhdr->h_source,
1058 						     orig_node);
1059 		if (!neigh_node)
1060 			goto unlock;
1061 	} else {
1062 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1063 			   "Updating existing last-hop neighbor of originator\n");
1064 	}
1065 
1066 	rcu_read_unlock();
1067 	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1068 	if (!neigh_ifinfo)
1069 		goto out;
1070 
1071 	neigh_node->last_seen = jiffies;
1072 
1073 	spin_lock_bh(&neigh_node->ifinfo_lock);
1074 	batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1075 			       &neigh_ifinfo->bat_iv.tq_index,
1076 			       batadv_ogm_packet->tq);
1077 	tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1078 	neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1079 	spin_unlock_bh(&neigh_node->ifinfo_lock);
1080 
1081 	if (dup_status == BATADV_NO_DUP) {
1082 		orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1083 		neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1084 	}
1085 
1086 	/* if this neighbor already is our next hop there is nothing
1087 	 * to change
1088 	 */
1089 	router = batadv_orig_router_get(orig_node, if_outgoing);
1090 	if (router == neigh_node)
1091 		goto out;
1092 
1093 	if (router) {
1094 		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1095 		if (!router_ifinfo)
1096 			goto out;
1097 
1098 		/* if this neighbor does not offer a better TQ we won't
1099 		 * consider it
1100 		 */
1101 		if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1102 			goto out;
1103 	}
1104 
1105 	/* if the TQ is the same and the link not more symmetric we
1106 	 * won't consider it either
1107 	 */
1108 	if (router_ifinfo &&
1109 	    neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1110 		sum_orig = batadv_iv_ogm_neigh_ifinfo_sum(bat_priv, router);
1111 		sum_neigh = batadv_iv_ogm_neigh_ifinfo_sum(bat_priv,
1112 							   neigh_node);
1113 		if (sum_orig >= sum_neigh)
1114 			goto out;
1115 	}
1116 
1117 	batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1118 	goto out;
1119 
1120 unlock:
1121 	rcu_read_unlock();
1122 out:
1123 	batadv_neigh_node_put(neigh_node);
1124 	batadv_neigh_node_put(router);
1125 	batadv_neigh_ifinfo_put(neigh_ifinfo);
1126 	batadv_neigh_ifinfo_put(router_ifinfo);
1127 }
1128 
1129 /**
1130  * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1131  * @orig_node: the orig node who originally emitted the ogm packet
1132  * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1133  * @batadv_ogm_packet: the ogm packet
1134  * @if_incoming: interface where the packet was received
1135  * @if_outgoing: interface for which the retransmission should be considered
1136  *
1137  * Return: true if the link can be considered bidirectional, false otherwise
1138  */
1139 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1140 				  struct batadv_orig_node *orig_neigh_node,
1141 				  struct batadv_ogm_packet *batadv_ogm_packet,
1142 				  struct batadv_hard_iface *if_incoming,
1143 				  struct batadv_hard_iface *if_outgoing)
1144 {
1145 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1146 	struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1147 	struct batadv_neigh_ifinfo *neigh_ifinfo;
1148 	u8 total_count;
1149 	u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1150 	unsigned int tq_iface_hop_penalty = BATADV_TQ_MAX_VALUE;
1151 	unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1152 	unsigned int tq_asym_penalty, inv_asym_penalty;
1153 	unsigned int combined_tq;
1154 	bool ret = false;
1155 
1156 	/* find corresponding one hop neighbor */
1157 	rcu_read_lock();
1158 	hlist_for_each_entry_rcu(tmp_neigh_node,
1159 				 &orig_neigh_node->neigh_list, list) {
1160 		if (!batadv_compare_eth(tmp_neigh_node->addr,
1161 					orig_neigh_node->orig))
1162 			continue;
1163 
1164 		if (tmp_neigh_node->if_incoming != if_incoming)
1165 			continue;
1166 
1167 		if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1168 			continue;
1169 
1170 		neigh_node = tmp_neigh_node;
1171 		break;
1172 	}
1173 	rcu_read_unlock();
1174 
1175 	if (!neigh_node)
1176 		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1177 						     orig_neigh_node->orig,
1178 						     orig_neigh_node);
1179 
1180 	if (!neigh_node)
1181 		goto out;
1182 
1183 	/* if orig_node is direct neighbor update neigh_node last_seen */
1184 	if (orig_node == orig_neigh_node)
1185 		neigh_node->last_seen = jiffies;
1186 
1187 	orig_node->last_seen = jiffies;
1188 
1189 	/* find packet count of corresponding one hop neighbor */
1190 	orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1191 	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1192 	if (neigh_ifinfo) {
1193 		neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1194 		batadv_neigh_ifinfo_put(neigh_ifinfo);
1195 	} else {
1196 		neigh_rq_count = 0;
1197 	}
1198 
1199 	/* pay attention to not get a value bigger than 100 % */
1200 	if (orig_eq_count > neigh_rq_count)
1201 		total_count = neigh_rq_count;
1202 	else
1203 		total_count = orig_eq_count;
1204 
1205 	/* if we have too few packets (too less data) we set tq_own to zero
1206 	 * if we receive too few packets it is not considered bidirectional
1207 	 */
1208 	if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1209 	    neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1210 		tq_own = 0;
1211 	else
1212 		/* neigh_node->real_packet_count is never zero as we
1213 		 * only purge old information when getting new
1214 		 * information
1215 		 */
1216 		tq_own = (BATADV_TQ_MAX_VALUE * total_count) /	neigh_rq_count;
1217 
1218 	/* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1219 	 * affect the nearly-symmetric links only a little, but
1220 	 * punishes asymmetric links more.  This will give a value
1221 	 * between 0 and TQ_MAX_VALUE
1222 	 */
1223 	neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1224 	neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1225 	neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1226 			    BATADV_TQ_LOCAL_WINDOW_SIZE *
1227 			    BATADV_TQ_LOCAL_WINDOW_SIZE;
1228 	inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1229 	inv_asym_penalty /= neigh_rq_max_cube;
1230 	tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1231 	tq_iface_hop_penalty -= READ_ONCE(if_incoming->hop_penalty);
1232 
1233 	/* penalize if the OGM is forwarded on the same interface. WiFi
1234 	 * interfaces and other half duplex devices suffer from throughput
1235 	 * drops as they can't send and receive at the same time.
1236 	 */
1237 	if (if_outgoing && if_incoming == if_outgoing &&
1238 	    batadv_is_wifi_hardif(if_outgoing))
1239 		tq_iface_hop_penalty = batadv_hop_penalty(tq_iface_hop_penalty,
1240 							  bat_priv);
1241 
1242 	combined_tq = batadv_ogm_packet->tq *
1243 		      tq_own *
1244 		      tq_asym_penalty *
1245 		      tq_iface_hop_penalty;
1246 	combined_tq /= BATADV_TQ_MAX_VALUE *
1247 		       BATADV_TQ_MAX_VALUE *
1248 		       BATADV_TQ_MAX_VALUE;
1249 	batadv_ogm_packet->tq = combined_tq;
1250 
1251 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1252 		   "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_hop_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1253 		   orig_node->orig, orig_neigh_node->orig, total_count,
1254 		   neigh_rq_count, tq_own, tq_asym_penalty,
1255 		   tq_iface_hop_penalty, batadv_ogm_packet->tq,
1256 		   if_incoming->net_dev->name,
1257 		   if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1258 
1259 	/* if link has the minimum required transmission quality
1260 	 * consider it bidirectional
1261 	 */
1262 	if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1263 		ret = true;
1264 
1265 out:
1266 	batadv_neigh_node_put(neigh_node);
1267 	return ret;
1268 }
1269 
1270 /**
1271  * batadv_iv_ogm_update_seqnos() -  process a batman packet for all interfaces,
1272  *  adjust the sequence number and find out whether it is a duplicate
1273  * @ethhdr: ethernet header of the packet
1274  * @batadv_ogm_packet: OGM packet to be considered
1275  * @if_incoming: interface on which the OGM packet was received
1276  * @if_outgoing: interface for which the retransmission should be considered
1277  *
1278  * Return: duplicate status as enum batadv_dup_status
1279  */
1280 static enum batadv_dup_status
1281 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1282 			    const struct batadv_ogm_packet *batadv_ogm_packet,
1283 			    const struct batadv_hard_iface *if_incoming,
1284 			    struct batadv_hard_iface *if_outgoing)
1285 {
1286 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1287 	struct batadv_orig_node *orig_node;
1288 	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1289 	struct batadv_neigh_node *neigh_node;
1290 	struct batadv_neigh_ifinfo *neigh_ifinfo;
1291 	bool is_dup;
1292 	s32 seq_diff;
1293 	bool need_update = false;
1294 	int set_mark;
1295 	enum batadv_dup_status ret = BATADV_NO_DUP;
1296 	u32 seqno = ntohl(batadv_ogm_packet->seqno);
1297 	u8 *neigh_addr;
1298 	u8 packet_count;
1299 	unsigned long *bitmap;
1300 
1301 	orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1302 	if (!orig_node)
1303 		return BATADV_NO_DUP;
1304 
1305 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1306 	if (WARN_ON(!orig_ifinfo)) {
1307 		batadv_orig_node_put(orig_node);
1308 		return 0;
1309 	}
1310 
1311 	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1312 	seq_diff = seqno - orig_ifinfo->last_real_seqno;
1313 
1314 	/* signalize caller that the packet is to be dropped. */
1315 	if (!hlist_empty(&orig_node->neigh_list) &&
1316 	    batadv_window_protected(bat_priv, seq_diff,
1317 				    BATADV_TQ_LOCAL_WINDOW_SIZE,
1318 				    &orig_ifinfo->batman_seqno_reset, NULL)) {
1319 		ret = BATADV_PROTECTED;
1320 		goto out;
1321 	}
1322 
1323 	rcu_read_lock();
1324 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1325 		neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1326 						       if_outgoing);
1327 		if (!neigh_ifinfo)
1328 			continue;
1329 
1330 		neigh_addr = neigh_node->addr;
1331 		is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1332 					 orig_ifinfo->last_real_seqno,
1333 					 seqno);
1334 
1335 		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1336 		    neigh_node->if_incoming == if_incoming) {
1337 			set_mark = 1;
1338 			if (is_dup)
1339 				ret = BATADV_NEIGH_DUP;
1340 		} else {
1341 			set_mark = 0;
1342 			if (is_dup && ret != BATADV_NEIGH_DUP)
1343 				ret = BATADV_ORIG_DUP;
1344 		}
1345 
1346 		/* if the window moved, set the update flag. */
1347 		bitmap = neigh_ifinfo->bat_iv.real_bits;
1348 		need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1349 						     seq_diff, set_mark);
1350 
1351 		packet_count = bitmap_weight(bitmap,
1352 					     BATADV_TQ_LOCAL_WINDOW_SIZE);
1353 		neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1354 		batadv_neigh_ifinfo_put(neigh_ifinfo);
1355 	}
1356 	rcu_read_unlock();
1357 
1358 	if (need_update) {
1359 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1360 			   "%s updating last_seqno: old %u, new %u\n",
1361 			   if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1362 			   orig_ifinfo->last_real_seqno, seqno);
1363 		orig_ifinfo->last_real_seqno = seqno;
1364 	}
1365 
1366 out:
1367 	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1368 	batadv_orig_node_put(orig_node);
1369 	batadv_orig_ifinfo_put(orig_ifinfo);
1370 	return ret;
1371 }
1372 
1373 /**
1374  * batadv_orig_to_direct_router() - get direct next hop neighbor to an orig address
1375  * @bat_priv: the bat priv with all the mesh interface information
1376  * @orig_addr: the originator MAC address to search the best next hop router for
1377  * @if_outgoing: the interface where the OGM should be sent to
1378  *
1379  * Return: A neighbor node which is the best router towards the given originator
1380  * address. Bonding candidates are ignored.
1381  */
1382 static struct batadv_neigh_node *
1383 batadv_orig_to_direct_router(struct batadv_priv *bat_priv, u8 *orig_addr,
1384 			     struct batadv_hard_iface *if_outgoing)
1385 {
1386 	struct batadv_neigh_node *neigh_node;
1387 	struct batadv_orig_node *orig_node;
1388 
1389 	orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
1390 	if (!orig_node)
1391 		return NULL;
1392 
1393 	neigh_node = batadv_orig_router_get(orig_node, if_outgoing);
1394 	batadv_orig_node_put(orig_node);
1395 
1396 	return neigh_node;
1397 }
1398 
1399 /**
1400  * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1401  *  interface
1402  * @skb: the skb containing the OGM
1403  * @ogm_offset: offset from skb->data to start of ogm header
1404  * @orig_node: the (cached) orig node for the originator of this OGM
1405  * @if_incoming: the interface where this packet was received
1406  * @if_outgoing: the interface for which the packet should be considered
1407  */
1408 static void
1409 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1410 				struct batadv_orig_node *orig_node,
1411 				struct batadv_hard_iface *if_incoming,
1412 				struct batadv_hard_iface *if_outgoing)
1413 {
1414 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1415 	struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1416 	struct batadv_neigh_node *router = NULL;
1417 	struct batadv_neigh_node *router_router = NULL;
1418 	struct batadv_orig_node *orig_neigh_node;
1419 	struct batadv_orig_ifinfo *orig_ifinfo;
1420 	struct batadv_neigh_node *orig_neigh_router = NULL;
1421 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1422 	struct batadv_ogm_packet *ogm_packet;
1423 	enum batadv_dup_status dup_status;
1424 	bool is_from_best_next_hop = false;
1425 	bool is_single_hop_neigh = false;
1426 	bool sameseq, similar_ttl;
1427 	struct sk_buff *skb_priv;
1428 	struct ethhdr *ethhdr;
1429 	u8 *prev_sender;
1430 	bool is_bidirect;
1431 
1432 	/* create a private copy of the skb, as some functions change tq value
1433 	 * and/or flags.
1434 	 */
1435 	skb_priv = skb_copy(skb, GFP_ATOMIC);
1436 	if (!skb_priv)
1437 		return;
1438 
1439 	ethhdr = eth_hdr(skb_priv);
1440 	ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1441 
1442 	dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1443 						 if_incoming, if_outgoing);
1444 	if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1445 		is_single_hop_neigh = true;
1446 
1447 	if (dup_status == BATADV_PROTECTED) {
1448 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1449 			   "Drop packet: packet within seqno protection time (sender: %pM)\n",
1450 			   ethhdr->h_source);
1451 		goto out;
1452 	}
1453 
1454 	if (ogm_packet->tq == 0) {
1455 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1456 			   "Drop packet: originator packet with tq equal 0\n");
1457 		goto out;
1458 	}
1459 
1460 	if (is_single_hop_neigh) {
1461 		hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1462 						       ethhdr->h_source);
1463 		if (hardif_neigh)
1464 			hardif_neigh->last_seen = jiffies;
1465 	}
1466 
1467 	router = batadv_orig_router_get(orig_node, if_outgoing);
1468 	if (router) {
1469 		router_router = batadv_orig_to_direct_router(bat_priv,
1470 							     router->addr,
1471 							     if_outgoing);
1472 		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1473 	}
1474 
1475 	if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1476 	    (batadv_compare_eth(router->addr, ethhdr->h_source)))
1477 		is_from_best_next_hop = true;
1478 
1479 	prev_sender = ogm_packet->prev_sender;
1480 	/* avoid temporary routing loops */
1481 	if (router && router_router &&
1482 	    (batadv_compare_eth(router->addr, prev_sender)) &&
1483 	    !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1484 	    (batadv_compare_eth(router->addr, router_router->addr))) {
1485 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1486 			   "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1487 			   ethhdr->h_source);
1488 		goto out;
1489 	}
1490 
1491 	if (if_outgoing == BATADV_IF_DEFAULT)
1492 		batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1493 
1494 	/* if sender is a direct neighbor the sender mac equals
1495 	 * originator mac
1496 	 */
1497 	if (is_single_hop_neigh)
1498 		orig_neigh_node = orig_node;
1499 	else
1500 		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1501 							 ethhdr->h_source);
1502 
1503 	if (!orig_neigh_node)
1504 		goto out;
1505 
1506 	orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1507 						   if_outgoing);
1508 
1509 	/* drop packet if sender is not a direct neighbor and if we
1510 	 * don't route towards it
1511 	 */
1512 	if (!is_single_hop_neigh && !orig_neigh_router) {
1513 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1514 			   "Drop packet: OGM via unknown neighbor!\n");
1515 		goto out_neigh;
1516 	}
1517 
1518 	is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1519 					    ogm_packet, if_incoming,
1520 					    if_outgoing);
1521 
1522 	/* update ranking if it is not a duplicate or has the same
1523 	 * seqno and similar ttl as the non-duplicate
1524 	 */
1525 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1526 	if (!orig_ifinfo)
1527 		goto out_neigh;
1528 
1529 	sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1530 	similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1531 
1532 	if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1533 			    (sameseq && similar_ttl))) {
1534 		batadv_iv_ogm_orig_update(bat_priv, orig_node,
1535 					  orig_ifinfo, ethhdr,
1536 					  ogm_packet, if_incoming,
1537 					  if_outgoing, dup_status);
1538 	}
1539 	batadv_orig_ifinfo_put(orig_ifinfo);
1540 
1541 	/* only forward for specific interface, not for the default one. */
1542 	if (if_outgoing == BATADV_IF_DEFAULT)
1543 		goto out_neigh;
1544 
1545 	/* is single hop (direct) neighbor */
1546 	if (is_single_hop_neigh) {
1547 		/* OGMs from secondary interfaces should only scheduled once
1548 		 * per interface where it has been received, not multiple times
1549 		 */
1550 		if (ogm_packet->ttl <= 2 &&
1551 		    if_incoming != if_outgoing) {
1552 			batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1553 				   "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1554 			goto out_neigh;
1555 		}
1556 		/* mark direct link on incoming interface */
1557 		batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1558 				      is_single_hop_neigh,
1559 				      is_from_best_next_hop, if_incoming,
1560 				      if_outgoing);
1561 
1562 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1563 			   "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1564 		goto out_neigh;
1565 	}
1566 
1567 	/* multihop originator */
1568 	if (!is_bidirect) {
1569 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1570 			   "Drop packet: not received via bidirectional link\n");
1571 		goto out_neigh;
1572 	}
1573 
1574 	if (dup_status == BATADV_NEIGH_DUP) {
1575 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1576 			   "Drop packet: duplicate packet received\n");
1577 		goto out_neigh;
1578 	}
1579 
1580 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1581 		   "Forwarding packet: rebroadcast originator packet\n");
1582 	batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1583 			      is_single_hop_neigh, is_from_best_next_hop,
1584 			      if_incoming, if_outgoing);
1585 
1586 out_neigh:
1587 	if (orig_neigh_node && !is_single_hop_neigh)
1588 		batadv_orig_node_put(orig_neigh_node);
1589 out:
1590 	batadv_neigh_ifinfo_put(router_ifinfo);
1591 	batadv_neigh_node_put(router);
1592 	batadv_neigh_node_put(router_router);
1593 	batadv_neigh_node_put(orig_neigh_router);
1594 	batadv_hardif_neigh_put(hardif_neigh);
1595 
1596 	consume_skb(skb_priv);
1597 }
1598 
1599 /**
1600  * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1601  * @ogm_packet: rebroadcast OGM packet to process
1602  * @if_incoming: the interface where this packet was received
1603  * @orig_node: originator which reproadcasted the OGMs
1604  * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1605  */
1606 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1607 					struct batadv_hard_iface *if_incoming,
1608 					struct batadv_orig_node *orig_node,
1609 					u32 if_incoming_seqno)
1610 {
1611 	struct batadv_orig_ifinfo *orig_ifinfo;
1612 	s32 bit_pos;
1613 	u8 *weight;
1614 
1615 	/* neighbor has to indicate direct link and it has to
1616 	 * come via the corresponding interface
1617 	 */
1618 	if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1619 		return;
1620 
1621 	if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1622 				ogm_packet->orig))
1623 		return;
1624 
1625 	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1626 	if (!orig_ifinfo)
1627 		return;
1628 
1629 	/* save packet seqno for bidirectional check */
1630 	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1631 	bit_pos = if_incoming_seqno - 2;
1632 	bit_pos -= ntohl(ogm_packet->seqno);
1633 	batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1634 	weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1635 	*weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1636 				BATADV_TQ_LOCAL_WINDOW_SIZE);
1637 	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1638 
1639 	batadv_orig_ifinfo_put(orig_ifinfo);
1640 }
1641 
1642 /**
1643  * batadv_iv_ogm_process() - process an incoming batman iv OGM
1644  * @skb: the skb containing the OGM
1645  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1646  * @if_incoming: the interface where this packet was received
1647  */
1648 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1649 				  struct batadv_hard_iface *if_incoming)
1650 {
1651 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1652 	struct batadv_orig_node *orig_neigh_node, *orig_node;
1653 	struct batadv_hard_iface *hard_iface;
1654 	struct batadv_ogm_packet *ogm_packet;
1655 	u32 if_incoming_seqno;
1656 	bool has_directlink_flag;
1657 	struct ethhdr *ethhdr;
1658 	bool is_my_oldorig = false;
1659 	bool is_my_addr = false;
1660 	bool is_my_orig = false;
1661 	struct list_head *iter;
1662 
1663 	ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1664 	ethhdr = eth_hdr(skb);
1665 
1666 	/* Silently drop when the batman packet is actually not a
1667 	 * correct packet.
1668 	 *
1669 	 * This might happen if a packet is padded (e.g. Ethernet has a
1670 	 * minimum frame length of 64 byte) and the aggregation interprets
1671 	 * it as an additional length.
1672 	 *
1673 	 * TODO: A more sane solution would be to have a bit in the
1674 	 * batadv_ogm_packet to detect whether the packet is the last
1675 	 * packet in an aggregation.  Here we expect that the padding
1676 	 * is always zero (or not 0x01)
1677 	 */
1678 	if (ogm_packet->packet_type != BATADV_IV_OGM)
1679 		return;
1680 
1681 	/* could be changed by schedule_own_packet() */
1682 	if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1683 
1684 	if (ogm_packet->flags & BATADV_DIRECTLINK)
1685 		has_directlink_flag = true;
1686 	else
1687 		has_directlink_flag = false;
1688 
1689 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1690 		   "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1691 		   ethhdr->h_source, if_incoming->net_dev->name,
1692 		   if_incoming->net_dev->dev_addr, ogm_packet->orig,
1693 		   ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1694 		   ogm_packet->tq, ogm_packet->ttl,
1695 		   ogm_packet->version, has_directlink_flag);
1696 
1697 	rcu_read_lock();
1698 
1699 	netdev_for_each_lower_private_rcu(if_incoming->mesh_iface, hard_iface, iter) {
1700 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
1701 			continue;
1702 
1703 		if (batadv_compare_eth(ethhdr->h_source,
1704 				       hard_iface->net_dev->dev_addr))
1705 			is_my_addr = true;
1706 
1707 		if (batadv_compare_eth(ogm_packet->orig,
1708 				       hard_iface->net_dev->dev_addr))
1709 			is_my_orig = true;
1710 
1711 		if (batadv_compare_eth(ogm_packet->prev_sender,
1712 				       hard_iface->net_dev->dev_addr))
1713 			is_my_oldorig = true;
1714 	}
1715 	rcu_read_unlock();
1716 
1717 	if (is_my_addr) {
1718 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1719 			   "Drop packet: received my own broadcast (sender: %pM)\n",
1720 			   ethhdr->h_source);
1721 		return;
1722 	}
1723 
1724 	if (is_my_orig) {
1725 		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1726 							 ethhdr->h_source);
1727 		if (!orig_neigh_node)
1728 			return;
1729 
1730 		batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1731 					    orig_neigh_node, if_incoming_seqno);
1732 
1733 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1734 			   "Drop packet: originator packet from myself (via neighbor)\n");
1735 		batadv_orig_node_put(orig_neigh_node);
1736 		return;
1737 	}
1738 
1739 	if (is_my_oldorig) {
1740 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1741 			   "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1742 			   ethhdr->h_source);
1743 		return;
1744 	}
1745 
1746 	if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1747 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1748 			   "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1749 			   ethhdr->h_source);
1750 		return;
1751 	}
1752 
1753 	orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1754 	if (!orig_node)
1755 		return;
1756 
1757 	batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1758 					if_incoming, BATADV_IF_DEFAULT);
1759 
1760 	rcu_read_lock();
1761 	netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
1762 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
1763 			continue;
1764 
1765 		if (!kref_get_unless_zero(&hard_iface->refcount))
1766 			continue;
1767 
1768 		batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1769 						if_incoming, hard_iface);
1770 
1771 		batadv_hardif_put(hard_iface);
1772 	}
1773 	rcu_read_unlock();
1774 
1775 	batadv_orig_node_put(orig_node);
1776 }
1777 
1778 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1779 {
1780 	struct delayed_work *delayed_work;
1781 	struct batadv_forw_packet *forw_packet;
1782 	struct batadv_priv *bat_priv;
1783 	bool dropped = false;
1784 
1785 	delayed_work = to_delayed_work(work);
1786 	forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1787 				   delayed_work);
1788 	bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);
1789 
1790 	if (READ_ONCE(bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1791 		dropped = true;
1792 		goto out;
1793 	}
1794 
1795 	batadv_iv_ogm_emit(forw_packet);
1796 
1797 	/* we have to have at least one packet in the queue to determine the
1798 	 * queues wake up time unless we are shutting down.
1799 	 *
1800 	 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1801 	 * primary interface should only be rescheduled once per period, but
1802 	 * this function will be called for the forw_packet instances of the
1803 	 * other secondary interfaces as well.
1804 	 */
1805 	if (forw_packet->own &&
1806 	    forw_packet->if_incoming == forw_packet->if_outgoing)
1807 		batadv_iv_ogm_schedule(forw_packet->if_incoming);
1808 
1809 out:
1810 	/* do we get something for free()? */
1811 	if (batadv_forw_packet_steal(forw_packet,
1812 				     &bat_priv->forw_bat_list_lock))
1813 		batadv_forw_packet_free(forw_packet, dropped);
1814 }
1815 
1816 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1817 				 struct batadv_hard_iface *if_incoming)
1818 {
1819 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1820 	struct batadv_ogm_packet *ogm_packet;
1821 	u8 *packet_pos;
1822 	int ogm_offset;
1823 	bool res;
1824 	int ret = NET_RX_DROP;
1825 
1826 	res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1827 	if (!res)
1828 		goto free_skb;
1829 
1830 	/* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1831 	 * that does not have B.A.T.M.A.N. IV enabled ?
1832 	 */
1833 	if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1834 		goto free_skb;
1835 
1836 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1837 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1838 			   skb->len + ETH_HLEN);
1839 
1840 	ogm_offset = 0;
1841 	ogm_packet = (struct batadv_ogm_packet *)skb->data;
1842 
1843 	/* unpack the aggregated packets and process them one by one */
1844 	while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1845 					 ogm_packet)) {
1846 		batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1847 
1848 		ogm_offset += BATADV_OGM_HLEN;
1849 		ogm_offset += ntohs(ogm_packet->tvlv_len);
1850 
1851 		packet_pos = skb->data + ogm_offset;
1852 		ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1853 	}
1854 
1855 	ret = NET_RX_SUCCESS;
1856 
1857 free_skb:
1858 	if (ret == NET_RX_SUCCESS)
1859 		consume_skb(skb);
1860 	else
1861 		kfree_skb(skb);
1862 
1863 	return ret;
1864 }
1865 
1866 /**
1867  * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1868  *  given outgoing interface.
1869  * @neigh_node: Neighbour of interest
1870  * @if_outgoing: Outgoing interface of interest
1871  * @tq_avg: Pointer of where to store the TQ average
1872  *
1873  * Return: False if no average TQ available, otherwise true.
1874  */
1875 static bool
1876 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1877 			       struct batadv_hard_iface *if_outgoing,
1878 			       u8 *tq_avg)
1879 {
1880 	struct batadv_neigh_ifinfo *n_ifinfo;
1881 
1882 	n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1883 	if (!n_ifinfo)
1884 		return false;
1885 
1886 	*tq_avg = n_ifinfo->bat_iv.tq_avg;
1887 	batadv_neigh_ifinfo_put(n_ifinfo);
1888 
1889 	return true;
1890 }
1891 
1892 /**
1893  * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1894  *  message
1895  * @msg: Netlink message to dump into
1896  * @portid: Port making netlink request
1897  * @seq: Sequence number of netlink message
1898  * @bat_priv: The bat priv with all the mesh interface information
1899  * @if_outgoing: Limit dump to entries with this outgoing interface
1900  * @orig_node: Originator to dump
1901  * @neigh_node: Single hops neighbour
1902  * @best: Is the best originator
1903  *
1904  * Return: Error code, or 0 on success
1905  */
1906 static int
1907 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1908 				 struct batadv_priv *bat_priv,
1909 				 struct batadv_hard_iface *if_outgoing,
1910 				 struct batadv_orig_node *orig_node,
1911 				 struct batadv_neigh_node *neigh_node,
1912 				 bool best)
1913 {
1914 	void *hdr;
1915 	u8 tq_avg;
1916 	unsigned int last_seen_msecs;
1917 
1918 	last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1919 
1920 	if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1921 		return 0;
1922 
1923 	if (if_outgoing != BATADV_IF_DEFAULT &&
1924 	    if_outgoing != neigh_node->if_incoming)
1925 		return 0;
1926 
1927 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1928 			  NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1929 	if (!hdr)
1930 		return -ENOBUFS;
1931 
1932 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1933 		    orig_node->orig) ||
1934 	    nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1935 		    neigh_node->addr) ||
1936 	    nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
1937 			   neigh_node->if_incoming->net_dev->name) ||
1938 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1939 			neigh_node->if_incoming->net_dev->ifindex) ||
1940 	    nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1941 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1942 			last_seen_msecs))
1943 		goto nla_put_failure;
1944 
1945 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1946 		goto nla_put_failure;
1947 
1948 	genlmsg_end(msg, hdr);
1949 	return 0;
1950 
1951  nla_put_failure:
1952 	genlmsg_cancel(msg, hdr);
1953 	return -EMSGSIZE;
1954 }
1955 
1956 /**
1957  * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
1958  * @msg: Netlink message to dump into
1959  * @portid: Port making netlink request
1960  * @seq: Sequence number of netlink message
1961  * @bat_priv: The bat priv with all the mesh interface information
1962  * @if_outgoing: Limit dump to entries with this outgoing interface
1963  * @orig_node: Originator to dump
1964  * @sub_s: Number of sub entries to skip
1965  *
1966  * This function assumes the caller holds rcu_read_lock().
1967  *
1968  * Return: Error code, or 0 on success
1969  */
1970 static int
1971 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1972 			      struct batadv_priv *bat_priv,
1973 			      struct batadv_hard_iface *if_outgoing,
1974 			      struct batadv_orig_node *orig_node, int *sub_s)
1975 {
1976 	struct batadv_neigh_node *neigh_node_best;
1977 	struct batadv_neigh_node *neigh_node;
1978 	int sub = 0;
1979 	bool best;
1980 	u8 tq_avg_best;
1981 
1982 	neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1983 	if (!neigh_node_best)
1984 		goto out;
1985 
1986 	if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1987 					    &tq_avg_best))
1988 		goto out;
1989 
1990 	if (tq_avg_best == 0)
1991 		goto out;
1992 
1993 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1994 		if (sub++ < *sub_s)
1995 			continue;
1996 
1997 		best = (neigh_node == neigh_node_best);
1998 
1999 		if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2000 						     bat_priv, if_outgoing,
2001 						     orig_node, neigh_node,
2002 						     best)) {
2003 			batadv_neigh_node_put(neigh_node_best);
2004 
2005 			*sub_s = sub - 1;
2006 			return -EMSGSIZE;
2007 		}
2008 	}
2009 
2010  out:
2011 	batadv_neigh_node_put(neigh_node_best);
2012 
2013 	*sub_s = 0;
2014 	return 0;
2015 }
2016 
2017 /**
2018  * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
2019  *  message
2020  * @msg: Netlink message to dump into
2021  * @portid: Port making netlink request
2022  * @seq: Sequence number of netlink message
2023  * @bat_priv: The bat priv with all the mesh interface information
2024  * @if_outgoing: Limit dump to entries with this outgoing interface
2025  * @head: Bucket to be dumped
2026  * @idx_s: Number of entries to be skipped
2027  * @sub: Number of sub entries to be skipped
2028  *
2029  * Return: Error code, or 0 on success
2030  */
2031 static int
2032 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2033 			       struct batadv_priv *bat_priv,
2034 			       struct batadv_hard_iface *if_outgoing,
2035 			       struct hlist_head *head, int *idx_s, int *sub)
2036 {
2037 	struct batadv_orig_node *orig_node;
2038 	int idx = 0;
2039 
2040 	rcu_read_lock();
2041 	hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2042 		if (idx++ < *idx_s)
2043 			continue;
2044 
2045 		if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2046 						  if_outgoing, orig_node,
2047 						  sub)) {
2048 			rcu_read_unlock();
2049 			*idx_s = idx - 1;
2050 			return -EMSGSIZE;
2051 		}
2052 	}
2053 	rcu_read_unlock();
2054 
2055 	*idx_s = 0;
2056 	*sub = 0;
2057 	return 0;
2058 }
2059 
2060 /**
2061  * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2062  * @msg: Netlink message to dump into
2063  * @cb: Control block containing additional options
2064  * @bat_priv: The bat priv with all the mesh interface information
2065  * @if_outgoing: Limit dump to entries with this outgoing interface
2066  */
2067 static void
2068 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2069 			struct batadv_priv *bat_priv,
2070 			struct batadv_hard_iface *if_outgoing)
2071 {
2072 	struct batadv_hashtable *hash = bat_priv->orig_hash;
2073 	struct hlist_head *head;
2074 	int bucket = cb->args[0];
2075 	int idx = cb->args[1];
2076 	int sub = cb->args[2];
2077 	int portid = NETLINK_CB(cb->skb).portid;
2078 
2079 	while (bucket < hash->size) {
2080 		head = &hash->table[bucket];
2081 
2082 		if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2083 						   cb->nlh->nlmsg_seq,
2084 						   bat_priv, if_outgoing, head,
2085 						   &idx, &sub))
2086 			break;
2087 
2088 		bucket++;
2089 	}
2090 
2091 	cb->args[0] = bucket;
2092 	cb->args[1] = idx;
2093 	cb->args[2] = sub;
2094 }
2095 
2096 /**
2097  * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2098  * @neigh1: the first neighbor object of the comparison
2099  * @if_outgoing1: outgoing interface for the first neighbor
2100  * @neigh2: the second neighbor object of the comparison
2101  * @if_outgoing2: outgoing interface for the second neighbor
2102  * @diff: pointer to integer receiving the calculated difference
2103  *
2104  * The content of *@diff is only valid when this function returns true.
2105  * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2106  * the same as or higher than the metric via neigh2
2107  *
2108  * Return: true when the difference could be calculated, false otherwise
2109  */
2110 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2111 				     struct batadv_hard_iface *if_outgoing1,
2112 				     struct batadv_neigh_node *neigh2,
2113 				     struct batadv_hard_iface *if_outgoing2,
2114 				     int *diff)
2115 {
2116 	struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2117 	u8 tq1, tq2;
2118 	bool ret = true;
2119 
2120 	neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2121 	neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2122 
2123 	if (!neigh1_ifinfo || !neigh2_ifinfo) {
2124 		ret = false;
2125 		goto out;
2126 	}
2127 
2128 	tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2129 	tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2130 	*diff = (int)tq1 - (int)tq2;
2131 
2132 out:
2133 	batadv_neigh_ifinfo_put(neigh1_ifinfo);
2134 	batadv_neigh_ifinfo_put(neigh2_ifinfo);
2135 
2136 	return ret;
2137 }
2138 
2139 /**
2140  * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2141  * @msg: Netlink message to dump into
2142  * @portid: Port making netlink request
2143  * @seq: Sequence number of netlink message
2144  * @hardif_neigh: Neighbour to be dumped
2145  *
2146  * Return: Error code, or 0 on success
2147  */
2148 static int
2149 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2150 			       struct batadv_hardif_neigh_node *hardif_neigh)
2151 {
2152 	void *hdr;
2153 	unsigned int last_seen_msecs;
2154 
2155 	last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2156 
2157 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2158 			  NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2159 	if (!hdr)
2160 		return -ENOBUFS;
2161 
2162 	if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2163 		    hardif_neigh->addr) ||
2164 	    nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2165 			   hardif_neigh->if_incoming->net_dev->name) ||
2166 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2167 			hardif_neigh->if_incoming->net_dev->ifindex) ||
2168 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2169 			last_seen_msecs))
2170 		goto nla_put_failure;
2171 
2172 	genlmsg_end(msg, hdr);
2173 	return 0;
2174 
2175  nla_put_failure:
2176 	genlmsg_cancel(msg, hdr);
2177 	return -EMSGSIZE;
2178 }
2179 
2180 /**
2181  * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2182  *  into a message
2183  * @msg: Netlink message to dump into
2184  * @portid: Port making netlink request
2185  * @seq: Sequence number of netlink message
2186  * @bat_priv: The bat priv with all the mesh interface information
2187  * @hard_iface: Hard interface to dump the neighbours for
2188  * @idx_s: Number of entries to skip
2189  *
2190  * This function assumes the caller holds rcu_read_lock().
2191  *
2192  * Return: Error code, or 0 on success
2193  */
2194 static int
2195 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2196 				struct batadv_priv *bat_priv,
2197 				struct batadv_hard_iface *hard_iface,
2198 				int *idx_s)
2199 {
2200 	struct batadv_hardif_neigh_node *hardif_neigh;
2201 	int idx = 0;
2202 
2203 	hlist_for_each_entry_rcu(hardif_neigh,
2204 				 &hard_iface->neigh_list, list) {
2205 		if (idx++ < *idx_s)
2206 			continue;
2207 
2208 		if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2209 						   hardif_neigh)) {
2210 			*idx_s = idx - 1;
2211 			return -EMSGSIZE;
2212 		}
2213 	}
2214 
2215 	*idx_s = 0;
2216 	return 0;
2217 }
2218 
2219 /**
2220  * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2221  * @msg: Netlink message to dump into
2222  * @cb: Control block containing additional options
2223  * @bat_priv: The bat priv with all the mesh interface information
2224  * @single_hardif: Limit dump to this hard interface
2225  */
2226 static void
2227 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2228 			 struct batadv_priv *bat_priv,
2229 			 struct batadv_hard_iface *single_hardif)
2230 {
2231 	struct batadv_hard_iface *hard_iface;
2232 	struct list_head *iter;
2233 	int i_hardif = 0;
2234 	int i_hardif_s = cb->args[0];
2235 	int idx = cb->args[1];
2236 	int portid = NETLINK_CB(cb->skb).portid;
2237 
2238 	rcu_read_lock();
2239 	if (single_hardif) {
2240 		if (i_hardif_s == 0) {
2241 			if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2242 							    cb->nlh->nlmsg_seq,
2243 							    bat_priv,
2244 							    single_hardif,
2245 							    &idx) == 0)
2246 				i_hardif++;
2247 		}
2248 	} else {
2249 		netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
2250 			if (i_hardif++ < i_hardif_s)
2251 				continue;
2252 
2253 			if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2254 							    cb->nlh->nlmsg_seq,
2255 							    bat_priv,
2256 							    hard_iface, &idx)) {
2257 				i_hardif--;
2258 				break;
2259 			}
2260 		}
2261 	}
2262 	rcu_read_unlock();
2263 
2264 	cb->args[0] = i_hardif;
2265 	cb->args[1] = idx;
2266 }
2267 
2268 /**
2269  * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2270  * @neigh1: the first neighbor object of the comparison
2271  * @if_outgoing1: outgoing interface for the first neighbor
2272  * @neigh2: the second neighbor object of the comparison
2273  * @if_outgoing2: outgoing interface for the second neighbor
2274  *
2275  * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2276  * lower, the same as or higher than the metric via neigh2
2277  */
2278 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2279 				   struct batadv_hard_iface *if_outgoing1,
2280 				   struct batadv_neigh_node *neigh2,
2281 				   struct batadv_hard_iface *if_outgoing2)
2282 {
2283 	bool ret;
2284 	int diff;
2285 
2286 	ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2287 				       if_outgoing2, &diff);
2288 	if (!ret)
2289 		return 0;
2290 
2291 	return diff;
2292 }
2293 
2294 /**
2295  * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2296  *  than neigh2 from the metric prospective
2297  * @neigh1: the first neighbor object of the comparison
2298  * @if_outgoing1: outgoing interface for the first neighbor
2299  * @neigh2: the second neighbor object of the comparison
2300  * @if_outgoing2: outgoing interface for the second neighbor
2301  *
2302  * Return: true if the metric via neigh1 is equally good or better than
2303  * the metric via neigh2, false otherwise.
2304  */
2305 static bool
2306 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2307 			   struct batadv_hard_iface *if_outgoing1,
2308 			   struct batadv_neigh_node *neigh2,
2309 			   struct batadv_hard_iface *if_outgoing2)
2310 {
2311 	bool ret;
2312 	int diff;
2313 
2314 	ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2315 				       if_outgoing2, &diff);
2316 	if (!ret)
2317 		return false;
2318 
2319 	ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2320 	return ret;
2321 }
2322 
2323 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2324 {
2325 	INIT_DELAYED_WORK(&hard_iface->bat_iv.reschedule_work, batadv_iv_ogm_reschedule);
2326 
2327 	/* begin scheduling originator messages on that interface */
2328 	batadv_iv_ogm_schedule(hard_iface);
2329 }
2330 
2331 /**
2332  * batadv_iv_init_sel_class() - initialize GW selection class
2333  * @bat_priv: the bat priv with all the mesh interface information
2334  */
2335 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2336 {
2337 	/* set default TQ difference threshold to 20 */
2338 	WRITE_ONCE(bat_priv->gw.sel_class, 20);
2339 }
2340 
2341 static struct batadv_gw_node *
2342 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2343 {
2344 	struct batadv_neigh_node *router;
2345 	struct batadv_neigh_ifinfo *router_ifinfo;
2346 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
2347 	u64 max_gw_factor = 0;
2348 	u64 tmp_gw_factor = 0;
2349 	u8 max_tq = 0;
2350 	u8 tq_avg;
2351 	struct batadv_orig_node *orig_node;
2352 
2353 	rcu_read_lock();
2354 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2355 		orig_node = gw_node->orig_node;
2356 		router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2357 		if (!router)
2358 			continue;
2359 
2360 		router_ifinfo = batadv_neigh_ifinfo_get(router,
2361 							BATADV_IF_DEFAULT);
2362 		if (!router_ifinfo)
2363 			goto next;
2364 
2365 		if (!kref_get_unless_zero(&gw_node->refcount))
2366 			goto next;
2367 
2368 		tq_avg = router_ifinfo->bat_iv.tq_avg;
2369 
2370 		switch (READ_ONCE(bat_priv->gw.sel_class)) {
2371 		case 1: /* fast connection */
2372 			tmp_gw_factor = tq_avg * tq_avg;
2373 			tmp_gw_factor *= gw_node->bandwidth_down;
2374 			tmp_gw_factor *= 100 * 100;
2375 			tmp_gw_factor >>= 18;
2376 
2377 			if (tmp_gw_factor > max_gw_factor ||
2378 			    (tmp_gw_factor == max_gw_factor &&
2379 			     tq_avg > max_tq)) {
2380 				batadv_gw_node_put(curr_gw);
2381 				curr_gw = gw_node;
2382 				kref_get(&curr_gw->refcount);
2383 			}
2384 			break;
2385 
2386 		default: /* 2:  stable connection (use best statistic)
2387 			  * 3:  fast-switch (use best statistic but change as
2388 			  *     soon as a better gateway appears)
2389 			  * XX: late-switch (use best statistic but change as
2390 			  *     soon as a better gateway appears which has
2391 			  *     $routing_class more tq points)
2392 			  */
2393 			if (tq_avg > max_tq) {
2394 				batadv_gw_node_put(curr_gw);
2395 				curr_gw = gw_node;
2396 				kref_get(&curr_gw->refcount);
2397 			}
2398 			break;
2399 		}
2400 
2401 		if (tq_avg > max_tq)
2402 			max_tq = tq_avg;
2403 
2404 		if (tmp_gw_factor > max_gw_factor)
2405 			max_gw_factor = tmp_gw_factor;
2406 
2407 		batadv_gw_node_put(gw_node);
2408 
2409 next:
2410 		batadv_neigh_node_put(router);
2411 		batadv_neigh_ifinfo_put(router_ifinfo);
2412 	}
2413 	rcu_read_unlock();
2414 
2415 	return curr_gw;
2416 }
2417 
2418 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2419 				     struct batadv_orig_node *curr_gw_orig,
2420 				     struct batadv_orig_node *orig_node)
2421 {
2422 	struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2423 	struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2424 	u32 sel_class = READ_ONCE(bat_priv->gw.sel_class);
2425 	struct batadv_neigh_node *router_gw = NULL;
2426 	struct batadv_neigh_node *router_orig = NULL;
2427 	u8 gw_tq_avg, orig_tq_avg;
2428 	bool ret = false;
2429 
2430 	/* dynamic re-election is performed only on fast or late switch */
2431 	if (sel_class <= 2)
2432 		return false;
2433 
2434 	router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2435 	if (!router_gw) {
2436 		ret = true;
2437 		goto out;
2438 	}
2439 
2440 	router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2441 						   BATADV_IF_DEFAULT);
2442 	if (!router_gw_ifinfo) {
2443 		ret = true;
2444 		goto out;
2445 	}
2446 
2447 	router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2448 	if (!router_orig)
2449 		goto out;
2450 
2451 	router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2452 						     BATADV_IF_DEFAULT);
2453 	if (!router_orig_ifinfo)
2454 		goto out;
2455 
2456 	gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2457 	orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2458 
2459 	/* the TQ value has to be better */
2460 	if (orig_tq_avg < gw_tq_avg)
2461 		goto out;
2462 
2463 	/* if the routing class is greater than 3 the value tells us how much
2464 	 * greater the TQ value of the new gateway must be
2465 	 */
2466 	if (sel_class > 3 && orig_tq_avg - gw_tq_avg < sel_class)
2467 		goto out;
2468 
2469 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2470 		   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2471 		   gw_tq_avg, orig_tq_avg);
2472 
2473 	ret = true;
2474 out:
2475 	batadv_neigh_ifinfo_put(router_gw_ifinfo);
2476 	batadv_neigh_ifinfo_put(router_orig_ifinfo);
2477 	batadv_neigh_node_put(router_gw);
2478 	batadv_neigh_node_put(router_orig);
2479 
2480 	return ret;
2481 }
2482 
2483 /**
2484  * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2485  * @msg: Netlink message to dump into
2486  * @portid: Port making netlink request
2487  * @cb: Control block containing additional options
2488  * @bat_priv: The bat priv with all the mesh interface information
2489  * @gw_node: Gateway to be dumped
2490  *
2491  * Return: Error code, or 0 on success
2492  */
2493 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2494 				   struct netlink_callback *cb,
2495 				   struct batadv_priv *bat_priv,
2496 				   struct batadv_gw_node *gw_node)
2497 {
2498 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2499 	struct batadv_neigh_node *router;
2500 	struct batadv_gw_node *curr_gw = NULL;
2501 	int ret = 0;
2502 	void *hdr;
2503 
2504 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2505 	if (!router)
2506 		goto out;
2507 
2508 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2509 	if (!router_ifinfo)
2510 		goto out;
2511 
2512 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2513 
2514 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2515 			  &batadv_netlink_family, NLM_F_MULTI,
2516 			  BATADV_CMD_GET_GATEWAYS);
2517 	if (!hdr) {
2518 		ret = -ENOBUFS;
2519 		goto out;
2520 	}
2521 
2522 	genl_dump_check_consistent(cb, hdr);
2523 
2524 	ret = -EMSGSIZE;
2525 
2526 	if (curr_gw == gw_node)
2527 		if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2528 			genlmsg_cancel(msg, hdr);
2529 			goto out;
2530 		}
2531 
2532 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2533 		    gw_node->orig_node->orig) ||
2534 	    nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2535 	    nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2536 		    router->addr) ||
2537 	    nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2538 			   router->if_incoming->net_dev->name) ||
2539 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2540 			router->if_incoming->net_dev->ifindex) ||
2541 	    nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2542 			gw_node->bandwidth_down) ||
2543 	    nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2544 			gw_node->bandwidth_up)) {
2545 		genlmsg_cancel(msg, hdr);
2546 		goto out;
2547 	}
2548 
2549 	genlmsg_end(msg, hdr);
2550 	ret = 0;
2551 
2552 out:
2553 	batadv_gw_node_put(curr_gw);
2554 	batadv_neigh_ifinfo_put(router_ifinfo);
2555 	batadv_neigh_node_put(router);
2556 	return ret;
2557 }
2558 
2559 /**
2560  * batadv_iv_gw_dump() - Dump gateways into a message
2561  * @msg: Netlink message to dump into
2562  * @cb: Control block containing additional options
2563  * @bat_priv: The bat priv with all the mesh interface information
2564  */
2565 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2566 			      struct batadv_priv *bat_priv)
2567 {
2568 	int portid = NETLINK_CB(cb->skb).portid;
2569 	struct batadv_gw_node *gw_node;
2570 	int idx_skip = cb->args[0];
2571 	int idx = 0;
2572 
2573 	spin_lock_bh(&bat_priv->gw.list_lock);
2574 	cb->seq = bat_priv->gw.generation << 1 | 1;
2575 
2576 	hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2577 		if (idx++ < idx_skip)
2578 			continue;
2579 
2580 		if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2581 					    gw_node)) {
2582 			idx_skip = idx - 1;
2583 			goto unlock;
2584 		}
2585 	}
2586 
2587 	idx_skip = idx;
2588 unlock:
2589 	spin_unlock_bh(&bat_priv->gw.list_lock);
2590 
2591 	cb->args[0] = idx_skip;
2592 }
2593 
2594 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2595 	.name = "BATMAN_IV",
2596 	.iface = {
2597 		.enable = batadv_iv_ogm_iface_enable,
2598 		.enabled = batadv_iv_iface_enabled,
2599 		.disable = batadv_iv_ogm_iface_disable,
2600 		.update_mac = batadv_iv_ogm_iface_update_mac,
2601 		.primary_set = batadv_iv_ogm_primary_iface_set,
2602 	},
2603 	.neigh = {
2604 		.cmp = batadv_iv_ogm_neigh_cmp,
2605 		.is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2606 		.dump = batadv_iv_ogm_neigh_dump,
2607 	},
2608 	.orig = {
2609 		.dump = batadv_iv_ogm_orig_dump,
2610 	},
2611 	.gw = {
2612 		.init_sel_class = batadv_iv_init_sel_class,
2613 		.sel_class_max = BATADV_TQ_MAX_VALUE,
2614 		.get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2615 		.is_eligible = batadv_iv_gw_is_eligible,
2616 		.dump = batadv_iv_gw_dump,
2617 	},
2618 };
2619 
2620 /**
2621  * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2622  *
2623  * Return: 0 on success or negative error number in case of failure
2624  */
2625 int __init batadv_iv_init(void)
2626 {
2627 	int ret;
2628 
2629 	/* batman originator packet */
2630 	ret = batadv_recv_handler_register(BATADV_IV_OGM,
2631 					   batadv_iv_ogm_receive);
2632 	if (ret < 0)
2633 		goto out;
2634 
2635 	ret = batadv_algo_register(&batadv_batman_iv);
2636 	if (ret < 0)
2637 		goto handler_unregister;
2638 
2639 	goto out;
2640 
2641 handler_unregister:
2642 	batadv_recv_handler_unregister(BATADV_IV_OGM);
2643 out:
2644 	return ret;
2645 }
2646