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