1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2019 B.A.T.M.A.N. contributors:
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>
35 #include "hard-interface.h"
38 #include "originator.h"
41 #include "translation-table.h"
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
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
53 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
56 struct batadv_orig_node *orig_node;
59 orig_node = batadv_orig_hash_find(bat_priv, addr);
63 orig_node = batadv_orig_node_new(bat_priv, addr);
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);
82 * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer
83 * @hard_iface: the interface to use to send the OGM
85 static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
87 unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
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));
96 * batadv_v_ogm_start_timer() - restart the OGM sending timer
97 * @bat_priv: the bat priv with all the soft interface information
99 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
102 /* this function may be invoked in different contexts (ogm rescheduling
103 * or hard_iface activation), but the work timer should not be reset
105 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
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));
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
119 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
120 struct batadv_hard_iface *hard_iface)
122 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
124 if (hard_iface->if_status != BATADV_IF_ACTIVE)
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);
131 batadv_send_broadcast_skb(skb, hard_iface);
135 * batadv_v_ogm_len() - OGMv2 packet length
136 * @skb: the OGM to check
138 * Return: Length of the given OGMv2 packet, including tvlv length, excluding
139 * ethernet header length.
141 static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
143 struct batadv_ogm2_packet *ogm_packet;
145 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
146 return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
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
154 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
156 * Return: True, if the given OGMv2 packet still fits, false otherwise.
158 static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
159 struct batadv_hard_iface *hard_iface)
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);
165 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
167 return hard_iface->bat_v.aggr_len + ogm_len <= max;
171 * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue
172 * @hard_iface: the interface holding the aggregation queue
174 * Empties the OGMv2 aggregation queue and frees all the skbs it contained.
176 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
178 static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
182 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
184 while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list)))
187 hard_iface->bat_v.aggr_len = 0;
191 * batadv_v_ogm_aggr_send() - flush & send aggregation queue
192 * @hard_iface: the interface with the aggregation queue to flush
194 * Aggregates all OGMv2 packets currently in the aggregation queue into a
195 * single OGMv2 packet and transmits this aggregate.
197 * The aggregation queue is empty after this call.
199 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
201 static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
203 unsigned int aggr_len = hard_iface->bat_v.aggr_len;
204 struct sk_buff *skb_aggr;
205 unsigned int ogm_len;
208 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
213 skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
215 batadv_v_ogm_aggr_list_free(hard_iface);
219 skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
220 skb_reset_network_header(skb_aggr);
222 while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list))) {
223 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
225 ogm_len = batadv_v_ogm_len(skb);
226 skb_put_data(skb_aggr, skb->data, ogm_len);
231 batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
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
239 static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
240 struct batadv_hard_iface *hard_iface)
242 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
244 if (!atomic_read(&bat_priv->aggregated_ogms)) {
245 batadv_v_ogm_send_to_if(skb, hard_iface);
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);
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);
259 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
260 * @work: work queue item
262 static void batadv_v_ogm_send(struct work_struct *work)
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;
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);
277 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
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
285 batadv_tt_local_commit_changes(bat_priv);
286 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
290 bat_priv->bat_v.ogm_buff = ogm_buff;
291 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
293 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
297 skb_reserve(skb, ETH_HLEN);
298 skb_put_data(skb, ogm_buff, ogm_buff_len);
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);
305 /* broadcast on every interface */
307 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
308 if (hard_iface->soft_iface != bat_priv->soft_iface)
311 if (!kref_get_unless_zero(&hard_iface->refcount))
314 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
319 case BATADV_HARDIF_BCAST_NORECIPIENT:
320 type = "no neighbor";
322 case BATADV_HARDIF_BCAST_DUPFWD:
323 type = "single neighbor is source";
325 case BATADV_HARDIF_BCAST_DUPORIG:
326 type = "single neighbor is originator";
332 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
333 hard_iface->net_dev->name, type);
335 batadv_hardif_put(hard_iface);
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);
346 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
347 skb_tmp = skb_clone(skb, GFP_ATOMIC);
349 batadv_hardif_put(hard_iface);
353 batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
354 batadv_hardif_put(hard_iface);
361 batadv_v_ogm_start_timer(bat_priv);
367 * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
368 * @work: work queue item
370 * Emits aggregated OGM message in regular intervals.
372 void batadv_v_ogm_aggr_work(struct work_struct *work)
374 struct batadv_hard_iface_bat_v *batv;
375 struct batadv_hard_iface *hard_iface;
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);
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);
384 batadv_v_ogm_start_queue_timer(hard_iface);
388 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
389 * @hard_iface: the interface to prepare
391 * Takes care of scheduling own OGM sending routine for this interface.
393 * Return: 0 on success or a negative error code otherwise
395 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
397 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
399 batadv_v_ogm_start_queue_timer(hard_iface);
400 batadv_v_ogm_start_timer(bat_priv);
406 * batadv_v_ogm_iface_disable() - release OGM interface private resources
407 * @hard_iface: interface for which the resources have to be released
409 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
411 cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
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);
419 * batadv_v_ogm_primary_iface_set() - set a new primary interface
420 * @primary_iface: the new primary interface
422 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
424 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
425 struct batadv_ogm2_packet *ogm_packet;
427 if (!bat_priv->bat_v.ogm_buff)
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);
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
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
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
453 * Return: the penalised throughput metric.
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,
460 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
461 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
463 /* Don't apply hop penalty in default originator table. */
464 if (if_outgoing == BATADV_IF_DEFAULT)
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.
471 if (throughput > 10 &&
472 if_incoming == if_outgoing &&
473 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
474 return throughput / 2;
476 /* hop penalty of 255 equals 100% */
477 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
481 * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
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
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.
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)
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;
509 /* only forward for specific interfaces, not for the default one. */
510 if (if_outgoing == BATADV_IF_DEFAULT)
513 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
517 /* acquire possibly updated router */
518 router = batadv_orig_router_get(orig_node, if_outgoing);
520 /* strict rule: forward packets coming from the best next hop only */
521 if (neigh_node != router)
524 /* don't forward the same seqno twice on one interface */
525 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
528 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
530 if (ogm_received->ttl <= 1) {
531 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
535 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
539 tvlv_len = ntohs(ogm_received->tvlv_len);
541 packet_len = BATADV_OGM2_HLEN + tvlv_len;
542 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
543 ETH_HLEN + packet_len);
547 skb_reserve(skb, ETH_HLEN);
548 skb_buff = skb_put_data(skb, ogm_received, packet_len);
550 /* apply forward penalty */
551 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
552 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
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);
560 batadv_v_ogm_queue_on_if(skb, if_outgoing);
564 batadv_orig_ifinfo_put(orig_ifinfo);
566 batadv_neigh_node_put(router);
568 batadv_neigh_ifinfo_put(neigh_ifinfo);
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
581 * 1 if the OGM is new,
582 * 0 if it is not new but valid,
583 * <0 on error (e.g. old OGM)
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)
592 struct batadv_orig_ifinfo *orig_ifinfo;
593 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
594 bool protection_started = false;
599 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
603 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
605 if (!hlist_empty(&orig_node->neigh_list) &&
606 batadv_window_protected(bat_priv, seq_diff,
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",
613 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
614 "Last reset: %ld, %ld\n",
615 orig_ifinfo->batman_seqno_reset, jiffies);
619 /* drop packets with old seqnos, however accept the first packet after
620 * a host has been rebooted.
622 if (seq_diff < 0 && !protection_started)
625 neigh_node->last_seen = jiffies;
627 orig_node->last_seen = jiffies;
629 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
630 orig_ifinfo->last_ttl = ogm2->ttl;
632 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
636 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
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;
643 if (seq_diff > 0 || protection_started)
649 batadv_orig_ifinfo_put(orig_ifinfo);
651 batadv_neigh_ifinfo_put(neigh_ifinfo);
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
666 * Return: true if the packet should be forwarded, false otherwise
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)
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;
684 bool forward = false;
686 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
687 if (!orig_neigh_node)
690 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
693 /* drop packet if sender is not a direct neighbor and if we
694 * don't route towards it
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");
703 /* Mark the OGM to be considered for forwarding, and update routes
708 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
709 "Searching and updating originator entry of received packet\n");
711 /* if this neighbor already is our next hop there is nothing
714 if (router == neigh_node)
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.
722 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
723 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
725 /* if these are not allocated, something is wrong. */
726 if (!router_ifinfo || !neigh_ifinfo)
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;
735 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
736 router_throughput >= neigh_throughput)
740 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
743 batadv_neigh_node_put(router);
744 if (orig_neigh_router)
745 batadv_neigh_node_put(orig_neigh_router);
747 batadv_orig_node_put(orig_neigh_node);
749 batadv_neigh_ifinfo_put(router_ifinfo);
751 batadv_neigh_ifinfo_put(neigh_ifinfo);
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
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)
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,
783 /* outdated sequence numbers are to be discarded */
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,
791 (unsigned char *)(ogm2 + 1),
792 ntohs(ogm2->tvlv_len));
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,
799 /* if the routes have been processed correctly, check and forward */
801 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
802 if_incoming, if_outgoing);
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
811 * Return: true if there is enough space for another OGM, false otherwise.
814 batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
815 const struct batadv_ogm2_packet *ogm2_packet)
817 int next_buff_pos = 0;
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)
824 /* check if there is enough space for the optional TVLV */
825 next_buff_pos += ntohs(ogm2_packet->tvlv_len);
827 return (next_buff_pos <= packet_len) &&
828 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
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
837 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
838 struct batadv_hard_iface *if_incoming)
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;
850 ethhdr = eth_hdr(skb);
851 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
853 ogm_throughput = ntohl(ogm_packet->throughput);
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));
862 /* If the throughput metric is 0, immediately drop the packet. No need
863 * to create orig_node / neigh_node for an unusable route.
865 if (ogm_throughput == 0) {
866 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
867 "Drop packet: originator packet with throughput metric of 0\n");
871 /* require ELP packets be to received from this neighbor first */
872 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
874 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
875 "Drop packet: OGM via unknown neighbor!\n");
879 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
883 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
888 /* Update the received throughput metric to match the link
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.
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);
899 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
900 neigh_node, if_incoming,
904 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
905 if (hard_iface->if_status != BATADV_IF_ACTIVE)
908 if (hard_iface->soft_iface != bat_priv->soft_iface)
911 if (!kref_get_unless_zero(&hard_iface->refcount))
914 ret = batadv_hardif_no_broadcast(hard_iface,
922 case BATADV_HARDIF_BCAST_NORECIPIENT:
923 type = "no neighbor";
925 case BATADV_HARDIF_BCAST_DUPFWD:
926 type = "single neighbor is source";
928 case BATADV_HARDIF_BCAST_DUPORIG:
929 type = "single neighbor is originator";
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,
939 batadv_hardif_put(hard_iface);
943 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
944 orig_node, neigh_node,
945 if_incoming, hard_iface);
947 batadv_hardif_put(hard_iface);
952 batadv_orig_node_put(orig_node);
954 batadv_neigh_node_put(neigh_node);
956 batadv_hardif_neigh_put(hardif_neigh);
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
964 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
965 * (without freeing the skb) on failure
967 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
968 struct batadv_hard_iface *if_incoming)
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);
975 int ret = NET_RX_DROP;
977 /* did we receive a OGM2 packet on an interface that does not have
978 * B.A.T.M.A.N. V enabled ?
980 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
983 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
986 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
989 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
991 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
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);
999 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1001 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1003 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
1005 ogm_offset += BATADV_OGM2_HLEN;
1006 ogm_offset += ntohs(ogm_packet->tvlv_len);
1008 packet_pos = skb->data + ogm_offset;
1009 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
1012 ret = NET_RX_SUCCESS;
1015 if (ret == NET_RX_SUCCESS)
1024 * batadv_v_ogm_init() - initialise the OGM2 engine
1025 * @bat_priv: the bat priv with all the soft interface information
1027 * Return: 0 on success or a negative error code in case of failure
1029 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
1031 struct batadv_ogm2_packet *ogm_packet;
1032 unsigned char *ogm_buff;
1035 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1036 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
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);
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);
1057 * batadv_v_ogm_free() - free OGM private resources
1058 * @bat_priv: the bat priv with all the soft interface information
1060 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1062 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
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;