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