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/mutex.h>
22 #include <linux/netdevice.h>
23 #include <linux/random.h>
24 #include <linux/rculist.h>
25 #include <linux/rcupdate.h>
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/stddef.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/workqueue.h>
33 #include <uapi/linux/batadv_packet.h>
36 #include "hard-interface.h"
39 #include "originator.h"
42 #include "translation-table.h"
46 * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node
47 * @bat_priv: the bat priv with all the soft interface information
48 * @addr: the address of the originator
50 * Return: the orig_node corresponding to the specified address. If such object
51 * does not exist it is allocated here. In case of allocation failure returns
54 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
57 struct batadv_orig_node *orig_node;
60 orig_node = batadv_orig_hash_find(bat_priv, addr);
64 orig_node = batadv_orig_node_new(bat_priv, addr);
68 kref_get(&orig_node->refcount);
69 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
70 batadv_choose_orig, orig_node,
71 &orig_node->hash_entry);
72 if (hash_added != 0) {
73 /* remove refcnt for newly created orig_node and hash entry */
74 batadv_orig_node_put(orig_node);
75 batadv_orig_node_put(orig_node);
83 * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer
84 * @hard_iface: the interface to use to send the OGM
86 static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
88 unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
90 /* msecs * [0.9, 1.1] */
91 msecs += prandom_u32() % (msecs / 5) - (msecs / 10);
92 queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq,
93 msecs_to_jiffies(msecs / 1000));
97 * batadv_v_ogm_start_timer() - restart the OGM sending timer
98 * @bat_priv: the bat priv with all the soft interface information
100 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
103 /* this function may be invoked in different contexts (ogm rescheduling
104 * or hard_iface activation), but the work timer should not be reset
106 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
109 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
110 msecs += prandom_u32() % (2 * BATADV_JITTER);
111 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
112 msecs_to_jiffies(msecs));
116 * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface
117 * @skb: the OGM to send
118 * @hard_iface: the interface to use to send the OGM
120 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
121 struct batadv_hard_iface *hard_iface)
123 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
125 if (hard_iface->if_status != BATADV_IF_ACTIVE)
128 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
129 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
130 skb->len + ETH_HLEN);
132 batadv_send_broadcast_skb(skb, hard_iface);
136 * batadv_v_ogm_len() - OGMv2 packet length
137 * @skb: the OGM to check
139 * Return: Length of the given OGMv2 packet, including tvlv length, excluding
140 * ethernet header length.
142 static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
144 struct batadv_ogm2_packet *ogm_packet;
146 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
147 return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
151 * batadv_v_ogm_queue_left() - check if given OGM still fits aggregation queue
152 * @skb: the OGM to check
153 * @hard_iface: the interface to use to send the OGM
155 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
157 * Return: True, if the given OGMv2 packet still fits, false otherwise.
159 static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
160 struct batadv_hard_iface *hard_iface)
162 unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu,
163 BATADV_MAX_AGGREGATION_BYTES);
164 unsigned int ogm_len = batadv_v_ogm_len(skb);
166 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
168 return hard_iface->bat_v.aggr_len + ogm_len <= max;
172 * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue
173 * @hard_iface: the interface holding the aggregation queue
175 * Empties the OGMv2 aggregation queue and frees all the skbs it contained.
177 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
179 static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
183 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
185 while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list)))
188 hard_iface->bat_v.aggr_len = 0;
192 * batadv_v_ogm_aggr_send() - flush & send aggregation queue
193 * @hard_iface: the interface with the aggregation queue to flush
195 * Aggregates all OGMv2 packets currently in the aggregation queue into a
196 * single OGMv2 packet and transmits this aggregate.
198 * The aggregation queue is empty after this call.
200 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
202 static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
204 unsigned int aggr_len = hard_iface->bat_v.aggr_len;
205 struct sk_buff *skb_aggr;
206 unsigned int ogm_len;
209 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
214 skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
216 batadv_v_ogm_aggr_list_free(hard_iface);
220 skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
221 skb_reset_network_header(skb_aggr);
223 while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list))) {
224 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
226 ogm_len = batadv_v_ogm_len(skb);
227 skb_put_data(skb_aggr, skb->data, ogm_len);
232 batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
236 * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface
237 * @skb: the OGM to queue
238 * @hard_iface: the interface to queue the OGM on
240 static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
241 struct batadv_hard_iface *hard_iface)
243 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
245 if (!atomic_read(&bat_priv->aggregated_ogms)) {
246 batadv_v_ogm_send_to_if(skb, hard_iface);
250 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
251 if (!batadv_v_ogm_queue_left(skb, hard_iface))
252 batadv_v_ogm_aggr_send(hard_iface);
254 hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
255 skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
256 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
260 * batadv_v_ogm_send_softif() - periodic worker broadcasting the own OGM
261 * @bat_priv: the bat priv with all the soft interface information
263 static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
265 struct batadv_hard_iface *hard_iface;
266 struct batadv_ogm2_packet *ogm_packet;
267 struct sk_buff *skb, *skb_tmp;
268 unsigned char *ogm_buff;
273 lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
275 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
278 ogm_buff = bat_priv->bat_v.ogm_buff;
279 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
280 /* tt changes have to be committed before the tvlv data is
281 * appended as it may alter the tt tvlv container
283 batadv_tt_local_commit_changes(bat_priv);
284 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
288 bat_priv->bat_v.ogm_buff = ogm_buff;
289 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
291 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
295 skb_reserve(skb, ETH_HLEN);
296 skb_put_data(skb, ogm_buff, ogm_buff_len);
298 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
299 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
300 atomic_inc(&bat_priv->bat_v.ogm_seqno);
301 ogm_packet->tvlv_len = htons(tvlv_len);
303 /* broadcast on every interface */
305 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
306 if (hard_iface->soft_iface != bat_priv->soft_iface)
309 if (!kref_get_unless_zero(&hard_iface->refcount))
312 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
317 case BATADV_HARDIF_BCAST_NORECIPIENT:
318 type = "no neighbor";
320 case BATADV_HARDIF_BCAST_DUPFWD:
321 type = "single neighbor is source";
323 case BATADV_HARDIF_BCAST_DUPORIG:
324 type = "single neighbor is originator";
330 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
331 hard_iface->net_dev->name, type);
333 batadv_hardif_put(hard_iface);
337 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
338 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
339 ogm_packet->orig, ntohl(ogm_packet->seqno),
340 ntohl(ogm_packet->throughput), ogm_packet->ttl,
341 hard_iface->net_dev->name,
342 hard_iface->net_dev->dev_addr);
344 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
345 skb_tmp = skb_clone(skb, GFP_ATOMIC);
347 batadv_hardif_put(hard_iface);
351 batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
352 batadv_hardif_put(hard_iface);
359 batadv_v_ogm_start_timer(bat_priv);
365 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
366 * @work: work queue item
368 static void batadv_v_ogm_send(struct work_struct *work)
370 struct batadv_priv_bat_v *bat_v;
371 struct batadv_priv *bat_priv;
373 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
374 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
376 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
377 batadv_v_ogm_send_softif(bat_priv);
378 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
382 * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
383 * @work: work queue item
385 * Emits aggregated OGM message in regular intervals.
387 void batadv_v_ogm_aggr_work(struct work_struct *work)
389 struct batadv_hard_iface_bat_v *batv;
390 struct batadv_hard_iface *hard_iface;
392 batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
393 hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
395 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
396 batadv_v_ogm_aggr_send(hard_iface);
397 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
399 batadv_v_ogm_start_queue_timer(hard_iface);
403 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
404 * @hard_iface: the interface to prepare
406 * Takes care of scheduling own OGM sending routine for this interface.
408 * Return: 0 on success or a negative error code otherwise
410 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
412 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
414 batadv_v_ogm_start_queue_timer(hard_iface);
415 batadv_v_ogm_start_timer(bat_priv);
421 * batadv_v_ogm_iface_disable() - release OGM interface private resources
422 * @hard_iface: interface for which the resources have to be released
424 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
426 cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
428 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
429 batadv_v_ogm_aggr_list_free(hard_iface);
430 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
434 * batadv_v_ogm_primary_iface_set() - set a new primary interface
435 * @primary_iface: the new primary interface
437 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
439 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
440 struct batadv_ogm2_packet *ogm_packet;
442 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
443 if (!bat_priv->bat_v.ogm_buff)
446 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
447 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
450 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
454 * batadv_v_forward_penalty() - apply a penalty to the throughput metric
455 * forwarded with B.A.T.M.A.N. V OGMs
456 * @bat_priv: the bat priv with all the soft interface information
457 * @if_incoming: the interface where the OGM has been received
458 * @if_outgoing: the interface where the OGM has to be forwarded to
459 * @throughput: the current throughput
461 * Apply a penalty on the current throughput metric value based on the
462 * characteristic of the interface where the OGM has been received. The return
463 * value is computed as follows:
464 * - throughput * 50% if the incoming and outgoing interface are the
465 * same WiFi interface and the throughput is above
467 * - throughput if the outgoing interface is the default
468 * interface (i.e. this OGM is processed for the
469 * internal table and not forwarded)
470 * - throughput * hop penalty otherwise
472 * Return: the penalised throughput metric.
474 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
475 struct batadv_hard_iface *if_incoming,
476 struct batadv_hard_iface *if_outgoing,
479 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
480 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
482 /* Don't apply hop penalty in default originator table. */
483 if (if_outgoing == BATADV_IF_DEFAULT)
486 /* Forwarding on the same WiFi interface cuts the throughput in half
487 * due to the store & forward characteristics of WIFI.
488 * Very low throughput values are the exception.
490 if (throughput > 10 &&
491 if_incoming == if_outgoing &&
492 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
493 return throughput / 2;
495 /* hop penalty of 255 equals 100% */
496 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
500 * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
502 * @bat_priv: the bat priv with all the soft interface information
503 * @ogm_received: previously received OGM to be forwarded
504 * @orig_node: the originator which has been updated
505 * @neigh_node: the neigh_node through with the OGM has been received
506 * @if_incoming: the interface on which this OGM was received on
507 * @if_outgoing: the interface to which the OGM has to be forwarded to
509 * Forward an OGM to an interface after having altered the throughput metric and
510 * the TTL value contained in it. The original OGM isn't modified.
512 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
513 const struct batadv_ogm2_packet *ogm_received,
514 struct batadv_orig_node *orig_node,
515 struct batadv_neigh_node *neigh_node,
516 struct batadv_hard_iface *if_incoming,
517 struct batadv_hard_iface *if_outgoing)
519 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
520 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
521 struct batadv_neigh_node *router = NULL;
522 struct batadv_ogm2_packet *ogm_forward;
523 unsigned char *skb_buff;
528 /* only forward for specific interfaces, not for the default one. */
529 if (if_outgoing == BATADV_IF_DEFAULT)
532 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
536 /* acquire possibly updated router */
537 router = batadv_orig_router_get(orig_node, if_outgoing);
539 /* strict rule: forward packets coming from the best next hop only */
540 if (neigh_node != router)
543 /* don't forward the same seqno twice on one interface */
544 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
547 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
549 if (ogm_received->ttl <= 1) {
550 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
554 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
558 tvlv_len = ntohs(ogm_received->tvlv_len);
560 packet_len = BATADV_OGM2_HLEN + tvlv_len;
561 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
562 ETH_HLEN + packet_len);
566 skb_reserve(skb, ETH_HLEN);
567 skb_buff = skb_put_data(skb, ogm_received, packet_len);
569 /* apply forward penalty */
570 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
571 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
574 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
575 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
576 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
577 ogm_forward->ttl, if_incoming->net_dev->name);
579 batadv_v_ogm_queue_on_if(skb, if_outgoing);
583 batadv_orig_ifinfo_put(orig_ifinfo);
585 batadv_neigh_node_put(router);
587 batadv_neigh_ifinfo_put(neigh_ifinfo);
591 * batadv_v_ogm_metric_update() - update route metric based on OGM
592 * @bat_priv: the bat priv with all the soft interface information
593 * @ogm2: OGM2 structure
594 * @orig_node: Originator structure for which the OGM has been received
595 * @neigh_node: the neigh_node through with the OGM has been received
596 * @if_incoming: the interface where this packet was received
597 * @if_outgoing: the interface for which the packet should be considered
600 * 1 if the OGM is new,
601 * 0 if it is not new but valid,
602 * <0 on error (e.g. old OGM)
604 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
605 const struct batadv_ogm2_packet *ogm2,
606 struct batadv_orig_node *orig_node,
607 struct batadv_neigh_node *neigh_node,
608 struct batadv_hard_iface *if_incoming,
609 struct batadv_hard_iface *if_outgoing)
611 struct batadv_orig_ifinfo *orig_ifinfo;
612 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
613 bool protection_started = false;
618 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
622 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
624 if (!hlist_empty(&orig_node->neigh_list) &&
625 batadv_window_protected(bat_priv, seq_diff,
627 &orig_ifinfo->batman_seqno_reset,
628 &protection_started)) {
629 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
630 "Drop packet: packet within window protection time from %pM\n",
632 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
633 "Last reset: %ld, %ld\n",
634 orig_ifinfo->batman_seqno_reset, jiffies);
638 /* drop packets with old seqnos, however accept the first packet after
639 * a host has been rebooted.
641 if (seq_diff < 0 && !protection_started)
644 neigh_node->last_seen = jiffies;
646 orig_node->last_seen = jiffies;
648 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
649 orig_ifinfo->last_ttl = ogm2->ttl;
651 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
655 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
657 ntohl(ogm2->throughput));
658 neigh_ifinfo->bat_v.throughput = path_throughput;
659 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
660 neigh_ifinfo->last_ttl = ogm2->ttl;
662 if (seq_diff > 0 || protection_started)
668 batadv_orig_ifinfo_put(orig_ifinfo);
670 batadv_neigh_ifinfo_put(neigh_ifinfo);
676 * batadv_v_ogm_route_update() - update routes based on OGM
677 * @bat_priv: the bat priv with all the soft interface information
678 * @ethhdr: the Ethernet header of the OGM2
679 * @ogm2: OGM2 structure
680 * @orig_node: Originator structure for which the OGM has been received
681 * @neigh_node: the neigh_node through with the OGM has been received
682 * @if_incoming: the interface where this packet was received
683 * @if_outgoing: the interface for which the packet should be considered
685 * Return: true if the packet should be forwarded, false otherwise
687 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
688 const struct ethhdr *ethhdr,
689 const struct batadv_ogm2_packet *ogm2,
690 struct batadv_orig_node *orig_node,
691 struct batadv_neigh_node *neigh_node,
692 struct batadv_hard_iface *if_incoming,
693 struct batadv_hard_iface *if_outgoing)
695 struct batadv_neigh_node *router = NULL;
696 struct batadv_orig_node *orig_neigh_node;
697 struct batadv_neigh_node *orig_neigh_router = NULL;
698 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
699 u32 router_throughput, neigh_throughput;
700 u32 router_last_seqno;
701 u32 neigh_last_seqno;
703 bool forward = false;
705 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
706 if (!orig_neigh_node)
709 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
712 /* drop packet if sender is not a direct neighbor and if we
713 * don't route towards it
715 router = batadv_orig_router_get(orig_node, if_outgoing);
716 if (router && router->orig_node != orig_node && !orig_neigh_router) {
717 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
718 "Drop packet: OGM via unknown neighbor!\n");
722 /* Mark the OGM to be considered for forwarding, and update routes
727 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
728 "Searching and updating originator entry of received packet\n");
730 /* if this neighbor already is our next hop there is nothing
733 if (router == neigh_node)
736 /* don't consider neighbours with worse throughput.
737 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
738 * the last received seqno from our best next hop.
741 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
742 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
744 /* if these are not allocated, something is wrong. */
745 if (!router_ifinfo || !neigh_ifinfo)
748 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
749 router_last_seqno = router_ifinfo->bat_v.last_seqno;
750 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
751 router_throughput = router_ifinfo->bat_v.throughput;
752 neigh_throughput = neigh_ifinfo->bat_v.throughput;
754 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
755 router_throughput >= neigh_throughput)
759 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
762 batadv_neigh_node_put(router);
763 if (orig_neigh_router)
764 batadv_neigh_node_put(orig_neigh_router);
766 batadv_orig_node_put(orig_neigh_node);
768 batadv_neigh_ifinfo_put(router_ifinfo);
770 batadv_neigh_ifinfo_put(neigh_ifinfo);
776 * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
777 * @bat_priv: the bat priv with all the soft interface information
778 * @ethhdr: the Ethernet header of the OGM2
779 * @ogm2: OGM2 structure
780 * @orig_node: Originator structure for which the OGM has been received
781 * @neigh_node: the neigh_node through with the OGM has been received
782 * @if_incoming: the interface where this packet was received
783 * @if_outgoing: the interface for which the packet should be considered
786 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
787 const struct ethhdr *ethhdr,
788 const struct batadv_ogm2_packet *ogm2,
789 struct batadv_orig_node *orig_node,
790 struct batadv_neigh_node *neigh_node,
791 struct batadv_hard_iface *if_incoming,
792 struct batadv_hard_iface *if_outgoing)
797 /* first, update the metric with according sanity checks */
798 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
799 neigh_node, if_incoming,
802 /* outdated sequence numbers are to be discarded */
806 /* only unknown & newer OGMs contain TVLVs we are interested in */
807 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
808 batadv_tvlv_containers_process(bat_priv, true, orig_node,
810 (unsigned char *)(ogm2 + 1),
811 ntohs(ogm2->tvlv_len));
813 /* if the metric update went through, update routes if needed */
814 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
815 neigh_node, if_incoming,
818 /* if the routes have been processed correctly, check and forward */
820 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
821 if_incoming, if_outgoing);
825 * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
826 * @buff_pos: current position in the skb
827 * @packet_len: total length of the skb
828 * @ogm2_packet: potential OGM2 in buffer
830 * Return: true if there is enough space for another OGM, false otherwise.
833 batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
834 const struct batadv_ogm2_packet *ogm2_packet)
836 int next_buff_pos = 0;
838 /* check if there is enough space for the header */
839 next_buff_pos += buff_pos + sizeof(*ogm2_packet);
840 if (next_buff_pos > packet_len)
843 /* check if there is enough space for the optional TVLV */
844 next_buff_pos += ntohs(ogm2_packet->tvlv_len);
846 return (next_buff_pos <= packet_len) &&
847 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
851 * batadv_v_ogm_process() - process an incoming batman v OGM
852 * @skb: the skb containing the OGM
853 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
854 * @if_incoming: the interface where this packet was receved
856 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
857 struct batadv_hard_iface *if_incoming)
859 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
860 struct ethhdr *ethhdr;
861 struct batadv_orig_node *orig_node = NULL;
862 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
863 struct batadv_neigh_node *neigh_node = NULL;
864 struct batadv_hard_iface *hard_iface;
865 struct batadv_ogm2_packet *ogm_packet;
866 u32 ogm_throughput, link_throughput, path_throughput;
869 ethhdr = eth_hdr(skb);
870 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
872 ogm_throughput = ntohl(ogm_packet->throughput);
874 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
875 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
876 ethhdr->h_source, if_incoming->net_dev->name,
877 if_incoming->net_dev->dev_addr, ogm_packet->orig,
878 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
879 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
881 /* If the throughput metric is 0, immediately drop the packet. No need
882 * to create orig_node / neigh_node for an unusable route.
884 if (ogm_throughput == 0) {
885 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
886 "Drop packet: originator packet with throughput metric of 0\n");
890 /* require ELP packets be to received from this neighbor first */
891 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
893 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
894 "Drop packet: OGM via unknown neighbor!\n");
898 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
902 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
907 /* Update the received throughput metric to match the link
909 * - If this OGM traveled one hop so far (emitted by single hop
910 * neighbor) the path throughput metric equals the link throughput.
911 * - For OGMs traversing more than hop the path throughput metric is
912 * the smaller of the path throughput and the link throughput.
914 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
915 path_throughput = min_t(u32, link_throughput, ogm_throughput);
916 ogm_packet->throughput = htonl(path_throughput);
918 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
919 neigh_node, if_incoming,
923 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
924 if (hard_iface->if_status != BATADV_IF_ACTIVE)
927 if (hard_iface->soft_iface != bat_priv->soft_iface)
930 if (!kref_get_unless_zero(&hard_iface->refcount))
933 ret = batadv_hardif_no_broadcast(hard_iface,
941 case BATADV_HARDIF_BCAST_NORECIPIENT:
942 type = "no neighbor";
944 case BATADV_HARDIF_BCAST_DUPFWD:
945 type = "single neighbor is source";
947 case BATADV_HARDIF_BCAST_DUPORIG:
948 type = "single neighbor is originator";
954 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
955 ogm_packet->orig, hard_iface->net_dev->name,
958 batadv_hardif_put(hard_iface);
962 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
963 orig_node, neigh_node,
964 if_incoming, hard_iface);
966 batadv_hardif_put(hard_iface);
971 batadv_orig_node_put(orig_node);
973 batadv_neigh_node_put(neigh_node);
975 batadv_hardif_neigh_put(hardif_neigh);
979 * batadv_v_ogm_packet_recv() - OGM2 receiving handler
980 * @skb: the received OGM
981 * @if_incoming: the interface where this OGM has been received
983 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
984 * (without freeing the skb) on failure
986 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
987 struct batadv_hard_iface *if_incoming)
989 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
990 struct batadv_ogm2_packet *ogm_packet;
991 struct ethhdr *ethhdr = eth_hdr(skb);
994 int ret = NET_RX_DROP;
996 /* did we receive a OGM2 packet on an interface that does not have
997 * B.A.T.M.A.N. V enabled ?
999 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
1002 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
1005 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1008 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1010 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
1013 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1014 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1015 skb->len + ETH_HLEN);
1018 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1020 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1022 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
1024 ogm_offset += BATADV_OGM2_HLEN;
1025 ogm_offset += ntohs(ogm_packet->tvlv_len);
1027 packet_pos = skb->data + ogm_offset;
1028 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
1031 ret = NET_RX_SUCCESS;
1034 if (ret == NET_RX_SUCCESS)
1043 * batadv_v_ogm_init() - initialise the OGM2 engine
1044 * @bat_priv: the bat priv with all the soft interface information
1046 * Return: 0 on success or a negative error code in case of failure
1048 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
1050 struct batadv_ogm2_packet *ogm_packet;
1051 unsigned char *ogm_buff;
1054 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1055 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
1059 bat_priv->bat_v.ogm_buff = ogm_buff;
1060 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
1061 ogm_packet->packet_type = BATADV_OGM2;
1062 ogm_packet->version = BATADV_COMPAT_VERSION;
1063 ogm_packet->ttl = BATADV_TTL;
1064 ogm_packet->flags = BATADV_NO_FLAGS;
1065 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
1067 /* randomize initial seqno to avoid collision */
1068 get_random_bytes(&random_seqno, sizeof(random_seqno));
1069 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
1070 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
1072 mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
1078 * batadv_v_ogm_free() - free OGM private resources
1079 * @bat_priv: the bat priv with all the soft interface information
1081 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1083 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
1085 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
1087 kfree(bat_priv->bat_v.ogm_buff);
1088 bat_priv->bat_v.ogm_buff = NULL;
1089 bat_priv->bat_v.ogm_buff_len = 0;
1091 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);