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