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