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