xref: /openbmc/linux/net/batman-adv/bat_v_ogm.c (revision 8795a739)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2019  B.A.T.M.A.N. contributors:
3  *
4  * Antonio Quartulli
5  */
6 
7 #include "bat_v_ogm.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/errno.h>
13 #include <linux/etherdevice.h>
14 #include <linux/gfp.h>
15 #include <linux/if_ether.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/kref.h>
19 #include <linux/list.h>
20 #include <linux/lockdep.h>
21 #include <linux/netdevice.h>
22 #include <linux/random.h>
23 #include <linux/rculist.h>
24 #include <linux/rcupdate.h>
25 #include <linux/skbuff.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/stddef.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <linux/workqueue.h>
32 #include <uapi/linux/batadv_packet.h>
33 
34 #include "bat_algo.h"
35 #include "hard-interface.h"
36 #include "hash.h"
37 #include "log.h"
38 #include "originator.h"
39 #include "routing.h"
40 #include "send.h"
41 #include "translation-table.h"
42 #include "tvlv.h"
43 
44 /**
45  * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node
46  * @bat_priv: the bat priv with all the soft interface information
47  * @addr: the address of the originator
48  *
49  * Return: the orig_node corresponding to the specified address. If such object
50  * does not exist it is allocated here. In case of allocation failure returns
51  * NULL.
52  */
53 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
54 					       const u8 *addr)
55 {
56 	struct batadv_orig_node *orig_node;
57 	int hash_added;
58 
59 	orig_node = batadv_orig_hash_find(bat_priv, addr);
60 	if (orig_node)
61 		return orig_node;
62 
63 	orig_node = batadv_orig_node_new(bat_priv, addr);
64 	if (!orig_node)
65 		return NULL;
66 
67 	kref_get(&orig_node->refcount);
68 	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
69 				     batadv_choose_orig, orig_node,
70 				     &orig_node->hash_entry);
71 	if (hash_added != 0) {
72 		/* remove refcnt for newly created orig_node and hash entry */
73 		batadv_orig_node_put(orig_node);
74 		batadv_orig_node_put(orig_node);
75 		orig_node = NULL;
76 	}
77 
78 	return orig_node;
79 }
80 
81 /**
82  * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer
83  * @hard_iface: the interface to use to send the OGM
84  */
85 static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
86 {
87 	unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
88 
89 	/* msecs * [0.9, 1.1] */
90 	msecs += prandom_u32() % (msecs / 5) - (msecs / 10);
91 	queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq,
92 			   msecs_to_jiffies(msecs / 1000));
93 }
94 
95 /**
96  * batadv_v_ogm_start_timer() - restart the OGM sending timer
97  * @bat_priv: the bat priv with all the soft interface information
98  */
99 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
100 {
101 	unsigned long msecs;
102 	/* this function may be invoked in different contexts (ogm rescheduling
103 	 * or hard_iface activation), but the work timer should not be reset
104 	 */
105 	if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
106 		return;
107 
108 	msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
109 	msecs += prandom_u32() % (2 * BATADV_JITTER);
110 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
111 			   msecs_to_jiffies(msecs));
112 }
113 
114 /**
115  * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface
116  * @skb: the OGM to send
117  * @hard_iface: the interface to use to send the OGM
118  */
119 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
120 				    struct batadv_hard_iface *hard_iface)
121 {
122 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
123 
124 	if (hard_iface->if_status != BATADV_IF_ACTIVE)
125 		return;
126 
127 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
128 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
129 			   skb->len + ETH_HLEN);
130 
131 	batadv_send_broadcast_skb(skb, hard_iface);
132 }
133 
134 /**
135  * batadv_v_ogm_len() - OGMv2 packet length
136  * @skb: the OGM to check
137  *
138  * Return: Length of the given OGMv2 packet, including tvlv length, excluding
139  * ethernet header length.
140  */
141 static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
142 {
143 	struct batadv_ogm2_packet *ogm_packet;
144 
145 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
146 	return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
147 }
148 
149 /**
150  * batadv_v_ogm_queue_left() - check if given OGM still fits aggregation queue
151  * @skb: the OGM to check
152  * @hard_iface: the interface to use to send the OGM
153  *
154  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
155  *
156  * Return: True, if the given OGMv2 packet still fits, false otherwise.
157  */
158 static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
159 				    struct batadv_hard_iface *hard_iface)
160 {
161 	unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu,
162 				 BATADV_MAX_AGGREGATION_BYTES);
163 	unsigned int ogm_len = batadv_v_ogm_len(skb);
164 
165 	lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
166 
167 	return hard_iface->bat_v.aggr_len + ogm_len <= max;
168 }
169 
170 /**
171  * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue
172  * @hard_iface: the interface holding the aggregation queue
173  *
174  * Empties the OGMv2 aggregation queue and frees all the skbs it contained.
175  *
176  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
177  */
178 static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
179 {
180 	struct sk_buff *skb;
181 
182 	lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
183 
184 	while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list)))
185 		kfree_skb(skb);
186 
187 	hard_iface->bat_v.aggr_len = 0;
188 }
189 
190 /**
191  * batadv_v_ogm_aggr_send() - flush & send aggregation queue
192  * @hard_iface: the interface with the aggregation queue to flush
193  *
194  * Aggregates all OGMv2 packets currently in the aggregation queue into a
195  * single OGMv2 packet and transmits this aggregate.
196  *
197  * The aggregation queue is empty after this call.
198  *
199  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
200  */
201 static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
202 {
203 	unsigned int aggr_len = hard_iface->bat_v.aggr_len;
204 	struct sk_buff *skb_aggr;
205 	unsigned int ogm_len;
206 	struct sk_buff *skb;
207 
208 	lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
209 
210 	if (!aggr_len)
211 		return;
212 
213 	skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
214 	if (!skb_aggr) {
215 		batadv_v_ogm_aggr_list_free(hard_iface);
216 		return;
217 	}
218 
219 	skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
220 	skb_reset_network_header(skb_aggr);
221 
222 	while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list))) {
223 		hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
224 
225 		ogm_len = batadv_v_ogm_len(skb);
226 		skb_put_data(skb_aggr, skb->data, ogm_len);
227 
228 		consume_skb(skb);
229 	}
230 
231 	batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
232 }
233 
234 /**
235  * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface
236  * @skb: the OGM to queue
237  * @hard_iface: the interface to queue the OGM on
238  */
239 static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
240 				     struct batadv_hard_iface *hard_iface)
241 {
242 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
243 
244 	if (!atomic_read(&bat_priv->aggregated_ogms)) {
245 		batadv_v_ogm_send_to_if(skb, hard_iface);
246 		return;
247 	}
248 
249 	spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
250 	if (!batadv_v_ogm_queue_left(skb, hard_iface))
251 		batadv_v_ogm_aggr_send(hard_iface);
252 
253 	hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
254 	skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
255 	spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
256 }
257 
258 /**
259  * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
260  * @work: work queue item
261  */
262 static void batadv_v_ogm_send(struct work_struct *work)
263 {
264 	struct batadv_hard_iface *hard_iface;
265 	struct batadv_priv_bat_v *bat_v;
266 	struct batadv_priv *bat_priv;
267 	struct batadv_ogm2_packet *ogm_packet;
268 	struct sk_buff *skb, *skb_tmp;
269 	unsigned char *ogm_buff;
270 	int ogm_buff_len;
271 	u16 tvlv_len = 0;
272 	int ret;
273 
274 	bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
275 	bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
276 
277 	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
278 		goto out;
279 
280 	ogm_buff = bat_priv->bat_v.ogm_buff;
281 	ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
282 	/* tt changes have to be committed before the tvlv data is
283 	 * appended as it may alter the tt tvlv container
284 	 */
285 	batadv_tt_local_commit_changes(bat_priv);
286 	tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
287 						    &ogm_buff_len,
288 						    BATADV_OGM2_HLEN);
289 
290 	bat_priv->bat_v.ogm_buff = ogm_buff;
291 	bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
292 
293 	skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
294 	if (!skb)
295 		goto reschedule;
296 
297 	skb_reserve(skb, ETH_HLEN);
298 	skb_put_data(skb, ogm_buff, ogm_buff_len);
299 
300 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
301 	ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
302 	atomic_inc(&bat_priv->bat_v.ogm_seqno);
303 	ogm_packet->tvlv_len = htons(tvlv_len);
304 
305 	/* broadcast on every interface */
306 	rcu_read_lock();
307 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
308 		if (hard_iface->soft_iface != bat_priv->soft_iface)
309 			continue;
310 
311 		if (!kref_get_unless_zero(&hard_iface->refcount))
312 			continue;
313 
314 		ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
315 		if (ret) {
316 			char *type;
317 
318 			switch (ret) {
319 			case BATADV_HARDIF_BCAST_NORECIPIENT:
320 				type = "no neighbor";
321 				break;
322 			case BATADV_HARDIF_BCAST_DUPFWD:
323 				type = "single neighbor is source";
324 				break;
325 			case BATADV_HARDIF_BCAST_DUPORIG:
326 				type = "single neighbor is originator";
327 				break;
328 			default:
329 				type = "unknown";
330 			}
331 
332 			batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
333 				   hard_iface->net_dev->name, type);
334 
335 			batadv_hardif_put(hard_iface);
336 			continue;
337 		}
338 
339 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
340 			   "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
341 			   ogm_packet->orig, ntohl(ogm_packet->seqno),
342 			   ntohl(ogm_packet->throughput), ogm_packet->ttl,
343 			   hard_iface->net_dev->name,
344 			   hard_iface->net_dev->dev_addr);
345 
346 		/* this skb gets consumed by batadv_v_ogm_send_to_if() */
347 		skb_tmp = skb_clone(skb, GFP_ATOMIC);
348 		if (!skb_tmp) {
349 			batadv_hardif_put(hard_iface);
350 			break;
351 		}
352 
353 		batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
354 		batadv_hardif_put(hard_iface);
355 	}
356 	rcu_read_unlock();
357 
358 	consume_skb(skb);
359 
360 reschedule:
361 	batadv_v_ogm_start_timer(bat_priv);
362 out:
363 	return;
364 }
365 
366 /**
367  * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
368  * @work: work queue item
369  *
370  * Emits aggregated OGM message in regular intervals.
371  */
372 void batadv_v_ogm_aggr_work(struct work_struct *work)
373 {
374 	struct batadv_hard_iface_bat_v *batv;
375 	struct batadv_hard_iface *hard_iface;
376 
377 	batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
378 	hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
379 
380 	spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
381 	batadv_v_ogm_aggr_send(hard_iface);
382 	spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
383 
384 	batadv_v_ogm_start_queue_timer(hard_iface);
385 }
386 
387 /**
388  * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
389  * @hard_iface: the interface to prepare
390  *
391  * Takes care of scheduling own OGM sending routine for this interface.
392  *
393  * Return: 0 on success or a negative error code otherwise
394  */
395 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
396 {
397 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
398 
399 	batadv_v_ogm_start_queue_timer(hard_iface);
400 	batadv_v_ogm_start_timer(bat_priv);
401 
402 	return 0;
403 }
404 
405 /**
406  * batadv_v_ogm_iface_disable() - release OGM interface private resources
407  * @hard_iface: interface for which the resources have to be released
408  */
409 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
410 {
411 	cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
412 
413 	spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
414 	batadv_v_ogm_aggr_list_free(hard_iface);
415 	spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
416 }
417 
418 /**
419  * batadv_v_ogm_primary_iface_set() - set a new primary interface
420  * @primary_iface: the new primary interface
421  */
422 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
423 {
424 	struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
425 	struct batadv_ogm2_packet *ogm_packet;
426 
427 	if (!bat_priv->bat_v.ogm_buff)
428 		return;
429 
430 	ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
431 	ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
432 }
433 
434 /**
435  * batadv_v_forward_penalty() - apply a penalty to the throughput metric
436  *  forwarded with B.A.T.M.A.N. V OGMs
437  * @bat_priv: the bat priv with all the soft interface information
438  * @if_incoming: the interface where the OGM has been received
439  * @if_outgoing: the interface where the OGM has to be forwarded to
440  * @throughput: the current throughput
441  *
442  * Apply a penalty on the current throughput metric value based on the
443  * characteristic of the interface where the OGM has been received. The return
444  * value is computed as follows:
445  * - throughput * 50%          if the incoming and outgoing interface are the
446  *                             same WiFi interface and the throughput is above
447  *                             1MBit/s
448  * - throughput                if the outgoing interface is the default
449  *                             interface (i.e. this OGM is processed for the
450  *                             internal table and not forwarded)
451  * - throughput * hop penalty  otherwise
452  *
453  * Return: the penalised throughput metric.
454  */
455 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
456 				    struct batadv_hard_iface *if_incoming,
457 				    struct batadv_hard_iface *if_outgoing,
458 				    u32 throughput)
459 {
460 	int hop_penalty = atomic_read(&bat_priv->hop_penalty);
461 	int hop_penalty_max = BATADV_TQ_MAX_VALUE;
462 
463 	/* Don't apply hop penalty in default originator table. */
464 	if (if_outgoing == BATADV_IF_DEFAULT)
465 		return throughput;
466 
467 	/* Forwarding on the same WiFi interface cuts the throughput in half
468 	 * due to the store & forward characteristics of WIFI.
469 	 * Very low throughput values are the exception.
470 	 */
471 	if (throughput > 10 &&
472 	    if_incoming == if_outgoing &&
473 	    !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
474 		return throughput / 2;
475 
476 	/* hop penalty of 255 equals 100% */
477 	return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
478 }
479 
480 /**
481  * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
482  *  outgoing interface
483  * @bat_priv: the bat priv with all the soft interface information
484  * @ogm_received: previously received OGM to be forwarded
485  * @orig_node: the originator which has been updated
486  * @neigh_node: the neigh_node through with the OGM has been received
487  * @if_incoming: the interface on which this OGM was received on
488  * @if_outgoing: the interface to which the OGM has to be forwarded to
489  *
490  * Forward an OGM to an interface after having altered the throughput metric and
491  * the TTL value contained in it. The original OGM isn't modified.
492  */
493 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
494 				 const struct batadv_ogm2_packet *ogm_received,
495 				 struct batadv_orig_node *orig_node,
496 				 struct batadv_neigh_node *neigh_node,
497 				 struct batadv_hard_iface *if_incoming,
498 				 struct batadv_hard_iface *if_outgoing)
499 {
500 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
501 	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
502 	struct batadv_neigh_node *router = NULL;
503 	struct batadv_ogm2_packet *ogm_forward;
504 	unsigned char *skb_buff;
505 	struct sk_buff *skb;
506 	size_t packet_len;
507 	u16 tvlv_len;
508 
509 	/* only forward for specific interfaces, not for the default one. */
510 	if (if_outgoing == BATADV_IF_DEFAULT)
511 		goto out;
512 
513 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
514 	if (!orig_ifinfo)
515 		goto out;
516 
517 	/* acquire possibly updated router */
518 	router = batadv_orig_router_get(orig_node, if_outgoing);
519 
520 	/* strict rule: forward packets coming from the best next hop only */
521 	if (neigh_node != router)
522 		goto out;
523 
524 	/* don't forward the same seqno twice on one interface */
525 	if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
526 		goto out;
527 
528 	orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
529 
530 	if (ogm_received->ttl <= 1) {
531 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
532 		goto out;
533 	}
534 
535 	neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
536 	if (!neigh_ifinfo)
537 		goto out;
538 
539 	tvlv_len = ntohs(ogm_received->tvlv_len);
540 
541 	packet_len = BATADV_OGM2_HLEN + tvlv_len;
542 	skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
543 					ETH_HLEN + packet_len);
544 	if (!skb)
545 		goto out;
546 
547 	skb_reserve(skb, ETH_HLEN);
548 	skb_buff = skb_put_data(skb, ogm_received, packet_len);
549 
550 	/* apply forward penalty */
551 	ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
552 	ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
553 	ogm_forward->ttl--;
554 
555 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
556 		   "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
557 		   if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
558 		   ogm_forward->ttl, if_incoming->net_dev->name);
559 
560 	batadv_v_ogm_queue_on_if(skb, if_outgoing);
561 
562 out:
563 	if (orig_ifinfo)
564 		batadv_orig_ifinfo_put(orig_ifinfo);
565 	if (router)
566 		batadv_neigh_node_put(router);
567 	if (neigh_ifinfo)
568 		batadv_neigh_ifinfo_put(neigh_ifinfo);
569 }
570 
571 /**
572  * batadv_v_ogm_metric_update() - update route metric based on OGM
573  * @bat_priv: the bat priv with all the soft interface information
574  * @ogm2: OGM2 structure
575  * @orig_node: Originator structure for which the OGM has been received
576  * @neigh_node: the neigh_node through with the OGM has been received
577  * @if_incoming: the interface where this packet was received
578  * @if_outgoing: the interface for which the packet should be considered
579  *
580  * Return:
581  *  1  if the OGM is new,
582  *  0  if it is not new but valid,
583  *  <0 on error (e.g. old OGM)
584  */
585 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
586 				      const struct batadv_ogm2_packet *ogm2,
587 				      struct batadv_orig_node *orig_node,
588 				      struct batadv_neigh_node *neigh_node,
589 				      struct batadv_hard_iface *if_incoming,
590 				      struct batadv_hard_iface *if_outgoing)
591 {
592 	struct batadv_orig_ifinfo *orig_ifinfo;
593 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
594 	bool protection_started = false;
595 	int ret = -EINVAL;
596 	u32 path_throughput;
597 	s32 seq_diff;
598 
599 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
600 	if (!orig_ifinfo)
601 		goto out;
602 
603 	seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
604 
605 	if (!hlist_empty(&orig_node->neigh_list) &&
606 	    batadv_window_protected(bat_priv, seq_diff,
607 				    BATADV_OGM_MAX_AGE,
608 				    &orig_ifinfo->batman_seqno_reset,
609 				    &protection_started)) {
610 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
611 			   "Drop packet: packet within window protection time from %pM\n",
612 			   ogm2->orig);
613 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
614 			   "Last reset: %ld, %ld\n",
615 			   orig_ifinfo->batman_seqno_reset, jiffies);
616 		goto out;
617 	}
618 
619 	/* drop packets with old seqnos, however accept the first packet after
620 	 * a host has been rebooted.
621 	 */
622 	if (seq_diff < 0 && !protection_started)
623 		goto out;
624 
625 	neigh_node->last_seen = jiffies;
626 
627 	orig_node->last_seen = jiffies;
628 
629 	orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
630 	orig_ifinfo->last_ttl = ogm2->ttl;
631 
632 	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
633 	if (!neigh_ifinfo)
634 		goto out;
635 
636 	path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
637 						   if_outgoing,
638 						   ntohl(ogm2->throughput));
639 	neigh_ifinfo->bat_v.throughput = path_throughput;
640 	neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
641 	neigh_ifinfo->last_ttl = ogm2->ttl;
642 
643 	if (seq_diff > 0 || protection_started)
644 		ret = 1;
645 	else
646 		ret = 0;
647 out:
648 	if (orig_ifinfo)
649 		batadv_orig_ifinfo_put(orig_ifinfo);
650 	if (neigh_ifinfo)
651 		batadv_neigh_ifinfo_put(neigh_ifinfo);
652 
653 	return ret;
654 }
655 
656 /**
657  * batadv_v_ogm_route_update() - update routes based on OGM
658  * @bat_priv: the bat priv with all the soft interface information
659  * @ethhdr: the Ethernet header of the OGM2
660  * @ogm2: OGM2 structure
661  * @orig_node: Originator structure for which the OGM has been received
662  * @neigh_node: the neigh_node through with the OGM has been received
663  * @if_incoming: the interface where this packet was received
664  * @if_outgoing: the interface for which the packet should be considered
665  *
666  * Return: true if the packet should be forwarded, false otherwise
667  */
668 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
669 				      const struct ethhdr *ethhdr,
670 				      const struct batadv_ogm2_packet *ogm2,
671 				      struct batadv_orig_node *orig_node,
672 				      struct batadv_neigh_node *neigh_node,
673 				      struct batadv_hard_iface *if_incoming,
674 				      struct batadv_hard_iface *if_outgoing)
675 {
676 	struct batadv_neigh_node *router = NULL;
677 	struct batadv_orig_node *orig_neigh_node;
678 	struct batadv_neigh_node *orig_neigh_router = NULL;
679 	struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
680 	u32 router_throughput, neigh_throughput;
681 	u32 router_last_seqno;
682 	u32 neigh_last_seqno;
683 	s32 neigh_seq_diff;
684 	bool forward = false;
685 
686 	orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
687 	if (!orig_neigh_node)
688 		goto out;
689 
690 	orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
691 						   if_outgoing);
692 
693 	/* drop packet if sender is not a direct neighbor and if we
694 	 * don't route towards it
695 	 */
696 	router = batadv_orig_router_get(orig_node, if_outgoing);
697 	if (router && router->orig_node != orig_node && !orig_neigh_router) {
698 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
699 			   "Drop packet: OGM via unknown neighbor!\n");
700 		goto out;
701 	}
702 
703 	/* Mark the OGM to be considered for forwarding, and update routes
704 	 * if needed.
705 	 */
706 	forward = true;
707 
708 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
709 		   "Searching and updating originator entry of received packet\n");
710 
711 	/* if this neighbor already is our next hop there is nothing
712 	 * to change
713 	 */
714 	if (router == neigh_node)
715 		goto out;
716 
717 	/* don't consider neighbours with worse throughput.
718 	 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
719 	 * the last received seqno from our best next hop.
720 	 */
721 	if (router) {
722 		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
723 		neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
724 
725 		/* if these are not allocated, something is wrong. */
726 		if (!router_ifinfo || !neigh_ifinfo)
727 			goto out;
728 
729 		neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
730 		router_last_seqno = router_ifinfo->bat_v.last_seqno;
731 		neigh_seq_diff = neigh_last_seqno - router_last_seqno;
732 		router_throughput = router_ifinfo->bat_v.throughput;
733 		neigh_throughput = neigh_ifinfo->bat_v.throughput;
734 
735 		if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
736 		    router_throughput >= neigh_throughput)
737 			goto out;
738 	}
739 
740 	batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
741 out:
742 	if (router)
743 		batadv_neigh_node_put(router);
744 	if (orig_neigh_router)
745 		batadv_neigh_node_put(orig_neigh_router);
746 	if (orig_neigh_node)
747 		batadv_orig_node_put(orig_neigh_node);
748 	if (router_ifinfo)
749 		batadv_neigh_ifinfo_put(router_ifinfo);
750 	if (neigh_ifinfo)
751 		batadv_neigh_ifinfo_put(neigh_ifinfo);
752 
753 	return forward;
754 }
755 
756 /**
757  * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
758  * @bat_priv: the bat priv with all the soft interface information
759  * @ethhdr: the Ethernet header of the OGM2
760  * @ogm2: OGM2 structure
761  * @orig_node: Originator structure for which the OGM has been received
762  * @neigh_node: the neigh_node through with the OGM has been received
763  * @if_incoming: the interface where this packet was received
764  * @if_outgoing: the interface for which the packet should be considered
765  */
766 static void
767 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
768 			       const struct ethhdr *ethhdr,
769 			       const struct batadv_ogm2_packet *ogm2,
770 			       struct batadv_orig_node *orig_node,
771 			       struct batadv_neigh_node *neigh_node,
772 			       struct batadv_hard_iface *if_incoming,
773 			       struct batadv_hard_iface *if_outgoing)
774 {
775 	int seqno_age;
776 	bool forward;
777 
778 	/* first, update the metric with according sanity checks */
779 	seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
780 					       neigh_node, if_incoming,
781 					       if_outgoing);
782 
783 	/* outdated sequence numbers are to be discarded */
784 	if (seqno_age < 0)
785 		return;
786 
787 	/* only unknown & newer OGMs contain TVLVs we are interested in */
788 	if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
789 		batadv_tvlv_containers_process(bat_priv, true, orig_node,
790 					       NULL, NULL,
791 					       (unsigned char *)(ogm2 + 1),
792 					       ntohs(ogm2->tvlv_len));
793 
794 	/* if the metric update went through, update routes if needed */
795 	forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
796 					    neigh_node, if_incoming,
797 					    if_outgoing);
798 
799 	/* if the routes have been processed correctly, check and forward */
800 	if (forward)
801 		batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
802 				     if_incoming, if_outgoing);
803 }
804 
805 /**
806  * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
807  * @buff_pos: current position in the skb
808  * @packet_len: total length of the skb
809  * @ogm2_packet: potential OGM2 in buffer
810  *
811  * Return: true if there is enough space for another OGM, false otherwise.
812  */
813 static bool
814 batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
815 			 const struct batadv_ogm2_packet *ogm2_packet)
816 {
817 	int next_buff_pos = 0;
818 
819 	/* check if there is enough space for the header */
820 	next_buff_pos += buff_pos + sizeof(*ogm2_packet);
821 	if (next_buff_pos > packet_len)
822 		return false;
823 
824 	/* check if there is enough space for the optional TVLV */
825 	next_buff_pos += ntohs(ogm2_packet->tvlv_len);
826 
827 	return (next_buff_pos <= packet_len) &&
828 	       (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
829 }
830 
831 /**
832  * batadv_v_ogm_process() - process an incoming batman v OGM
833  * @skb: the skb containing the OGM
834  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
835  * @if_incoming: the interface where this packet was receved
836  */
837 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
838 				 struct batadv_hard_iface *if_incoming)
839 {
840 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
841 	struct ethhdr *ethhdr;
842 	struct batadv_orig_node *orig_node = NULL;
843 	struct batadv_hardif_neigh_node *hardif_neigh = NULL;
844 	struct batadv_neigh_node *neigh_node = NULL;
845 	struct batadv_hard_iface *hard_iface;
846 	struct batadv_ogm2_packet *ogm_packet;
847 	u32 ogm_throughput, link_throughput, path_throughput;
848 	int ret;
849 
850 	ethhdr = eth_hdr(skb);
851 	ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
852 
853 	ogm_throughput = ntohl(ogm_packet->throughput);
854 
855 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
856 		   "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
857 		   ethhdr->h_source, if_incoming->net_dev->name,
858 		   if_incoming->net_dev->dev_addr, ogm_packet->orig,
859 		   ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
860 		   ogm_packet->version, ntohs(ogm_packet->tvlv_len));
861 
862 	/* If the throughput metric is 0, immediately drop the packet. No need
863 	 * to create orig_node / neigh_node for an unusable route.
864 	 */
865 	if (ogm_throughput == 0) {
866 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
867 			   "Drop packet: originator packet with throughput metric of 0\n");
868 		return;
869 	}
870 
871 	/* require ELP packets be to received from this neighbor first */
872 	hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
873 	if (!hardif_neigh) {
874 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
875 			   "Drop packet: OGM via unknown neighbor!\n");
876 		goto out;
877 	}
878 
879 	orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
880 	if (!orig_node)
881 		return;
882 
883 	neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
884 						     ethhdr->h_source);
885 	if (!neigh_node)
886 		goto out;
887 
888 	/* Update the received throughput metric to match the link
889 	 * characteristic:
890 	 *  - If this OGM traveled one hop so far (emitted by single hop
891 	 *    neighbor) the path throughput metric equals the link throughput.
892 	 *  - For OGMs traversing more than hop the path throughput metric is
893 	 *    the smaller of the path throughput and the link throughput.
894 	 */
895 	link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
896 	path_throughput = min_t(u32, link_throughput, ogm_throughput);
897 	ogm_packet->throughput = htonl(path_throughput);
898 
899 	batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
900 				       neigh_node, if_incoming,
901 				       BATADV_IF_DEFAULT);
902 
903 	rcu_read_lock();
904 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
905 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
906 			continue;
907 
908 		if (hard_iface->soft_iface != bat_priv->soft_iface)
909 			continue;
910 
911 		if (!kref_get_unless_zero(&hard_iface->refcount))
912 			continue;
913 
914 		ret = batadv_hardif_no_broadcast(hard_iface,
915 						 ogm_packet->orig,
916 						 hardif_neigh->orig);
917 
918 		if (ret) {
919 			char *type;
920 
921 			switch (ret) {
922 			case BATADV_HARDIF_BCAST_NORECIPIENT:
923 				type = "no neighbor";
924 				break;
925 			case BATADV_HARDIF_BCAST_DUPFWD:
926 				type = "single neighbor is source";
927 				break;
928 			case BATADV_HARDIF_BCAST_DUPORIG:
929 				type = "single neighbor is originator";
930 				break;
931 			default:
932 				type = "unknown";
933 			}
934 
935 			batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
936 				   ogm_packet->orig, hard_iface->net_dev->name,
937 				   type);
938 
939 			batadv_hardif_put(hard_iface);
940 			continue;
941 		}
942 
943 		batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
944 					       orig_node, neigh_node,
945 					       if_incoming, hard_iface);
946 
947 		batadv_hardif_put(hard_iface);
948 	}
949 	rcu_read_unlock();
950 out:
951 	if (orig_node)
952 		batadv_orig_node_put(orig_node);
953 	if (neigh_node)
954 		batadv_neigh_node_put(neigh_node);
955 	if (hardif_neigh)
956 		batadv_hardif_neigh_put(hardif_neigh);
957 }
958 
959 /**
960  * batadv_v_ogm_packet_recv() - OGM2 receiving handler
961  * @skb: the received OGM
962  * @if_incoming: the interface where this OGM has been received
963  *
964  * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
965  * (without freeing the skb) on failure
966  */
967 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
968 			     struct batadv_hard_iface *if_incoming)
969 {
970 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
971 	struct batadv_ogm2_packet *ogm_packet;
972 	struct ethhdr *ethhdr = eth_hdr(skb);
973 	int ogm_offset;
974 	u8 *packet_pos;
975 	int ret = NET_RX_DROP;
976 
977 	/* did we receive a OGM2 packet on an interface that does not have
978 	 * B.A.T.M.A.N. V enabled ?
979 	 */
980 	if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
981 		goto free_skb;
982 
983 	if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
984 		goto free_skb;
985 
986 	if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
987 		goto free_skb;
988 
989 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
990 
991 	if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
992 		goto free_skb;
993 
994 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
995 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
996 			   skb->len + ETH_HLEN);
997 
998 	ogm_offset = 0;
999 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1000 
1001 	while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1002 					ogm_packet)) {
1003 		batadv_v_ogm_process(skb, ogm_offset, if_incoming);
1004 
1005 		ogm_offset += BATADV_OGM2_HLEN;
1006 		ogm_offset += ntohs(ogm_packet->tvlv_len);
1007 
1008 		packet_pos = skb->data + ogm_offset;
1009 		ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
1010 	}
1011 
1012 	ret = NET_RX_SUCCESS;
1013 
1014 free_skb:
1015 	if (ret == NET_RX_SUCCESS)
1016 		consume_skb(skb);
1017 	else
1018 		kfree_skb(skb);
1019 
1020 	return ret;
1021 }
1022 
1023 /**
1024  * batadv_v_ogm_init() - initialise the OGM2 engine
1025  * @bat_priv: the bat priv with all the soft interface information
1026  *
1027  * Return: 0 on success or a negative error code in case of failure
1028  */
1029 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
1030 {
1031 	struct batadv_ogm2_packet *ogm_packet;
1032 	unsigned char *ogm_buff;
1033 	u32 random_seqno;
1034 
1035 	bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1036 	ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
1037 	if (!ogm_buff)
1038 		return -ENOMEM;
1039 
1040 	bat_priv->bat_v.ogm_buff = ogm_buff;
1041 	ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
1042 	ogm_packet->packet_type = BATADV_OGM2;
1043 	ogm_packet->version = BATADV_COMPAT_VERSION;
1044 	ogm_packet->ttl = BATADV_TTL;
1045 	ogm_packet->flags = BATADV_NO_FLAGS;
1046 	ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
1047 
1048 	/* randomize initial seqno to avoid collision */
1049 	get_random_bytes(&random_seqno, sizeof(random_seqno));
1050 	atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
1051 	INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
1052 
1053 	return 0;
1054 }
1055 
1056 /**
1057  * batadv_v_ogm_free() - free OGM private resources
1058  * @bat_priv: the bat priv with all the soft interface information
1059  */
1060 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1061 {
1062 	cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
1063 
1064 	kfree(bat_priv->bat_v.ogm_buff);
1065 	bat_priv->bat_v.ogm_buff = NULL;
1066 	bat_priv->bat_v.ogm_buff_len = 0;
1067 }
1068