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 * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
196 static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
200 lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
202 while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list))) {
203 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
204 batadv_v_ogm_send_to_if(skb, hard_iface);
209 * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface
210 * @skb: the OGM to queue
211 * @hard_iface: the interface to queue the OGM on
213 static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
214 struct batadv_hard_iface *hard_iface)
216 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
218 if (!atomic_read(&bat_priv->aggregated_ogms)) {
219 batadv_v_ogm_send_to_if(skb, hard_iface);
223 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
224 if (!batadv_v_ogm_queue_left(skb, hard_iface))
225 batadv_v_ogm_aggr_send(hard_iface);
227 hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
228 skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
229 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
233 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
234 * @work: work queue item
236 static void batadv_v_ogm_send(struct work_struct *work)
238 struct batadv_hard_iface *hard_iface;
239 struct batadv_priv_bat_v *bat_v;
240 struct batadv_priv *bat_priv;
241 struct batadv_ogm2_packet *ogm_packet;
242 struct sk_buff *skb, *skb_tmp;
243 unsigned char *ogm_buff;
248 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
249 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
251 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
254 ogm_buff = bat_priv->bat_v.ogm_buff;
255 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
256 /* tt changes have to be committed before the tvlv data is
257 * appended as it may alter the tt tvlv container
259 batadv_tt_local_commit_changes(bat_priv);
260 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
264 bat_priv->bat_v.ogm_buff = ogm_buff;
265 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
267 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
271 skb_reserve(skb, ETH_HLEN);
272 skb_put_data(skb, ogm_buff, ogm_buff_len);
274 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
275 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
276 atomic_inc(&bat_priv->bat_v.ogm_seqno);
277 ogm_packet->tvlv_len = htons(tvlv_len);
279 /* broadcast on every interface */
281 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
282 if (hard_iface->soft_iface != bat_priv->soft_iface)
285 if (!kref_get_unless_zero(&hard_iface->refcount))
288 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
293 case BATADV_HARDIF_BCAST_NORECIPIENT:
294 type = "no neighbor";
296 case BATADV_HARDIF_BCAST_DUPFWD:
297 type = "single neighbor is source";
299 case BATADV_HARDIF_BCAST_DUPORIG:
300 type = "single neighbor is originator";
306 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
307 hard_iface->net_dev->name, type);
309 batadv_hardif_put(hard_iface);
313 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
314 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
315 ogm_packet->orig, ntohl(ogm_packet->seqno),
316 ntohl(ogm_packet->throughput), ogm_packet->ttl,
317 hard_iface->net_dev->name,
318 hard_iface->net_dev->dev_addr);
320 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
321 skb_tmp = skb_clone(skb, GFP_ATOMIC);
323 batadv_hardif_put(hard_iface);
327 batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
328 batadv_hardif_put(hard_iface);
335 batadv_v_ogm_start_timer(bat_priv);
341 * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
342 * @work: work queue item
344 * Emits aggregated OGM message in regular intervals.
346 void batadv_v_ogm_aggr_work(struct work_struct *work)
348 struct batadv_hard_iface_bat_v *batv;
349 struct batadv_hard_iface *hard_iface;
351 batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
352 hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
354 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
355 batadv_v_ogm_aggr_send(hard_iface);
356 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
358 batadv_v_ogm_start_queue_timer(hard_iface);
362 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
363 * @hard_iface: the interface to prepare
365 * Takes care of scheduling own OGM sending routine for this interface.
367 * Return: 0 on success or a negative error code otherwise
369 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
371 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
373 batadv_v_ogm_start_queue_timer(hard_iface);
374 batadv_v_ogm_start_timer(bat_priv);
380 * batadv_v_ogm_iface_disable() - release OGM interface private resources
381 * @hard_iface: interface for which the resources have to be released
383 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
385 cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
387 spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
388 batadv_v_ogm_aggr_list_free(hard_iface);
389 spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
393 * batadv_v_ogm_primary_iface_set() - set a new primary interface
394 * @primary_iface: the new primary interface
396 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
398 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
399 struct batadv_ogm2_packet *ogm_packet;
401 if (!bat_priv->bat_v.ogm_buff)
404 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
405 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
409 * batadv_v_forward_penalty() - apply a penalty to the throughput metric
410 * forwarded with B.A.T.M.A.N. V OGMs
411 * @bat_priv: the bat priv with all the soft interface information
412 * @if_incoming: the interface where the OGM has been received
413 * @if_outgoing: the interface where the OGM has to be forwarded to
414 * @throughput: the current throughput
416 * Apply a penalty on the current throughput metric value based on the
417 * characteristic of the interface where the OGM has been received. The return
418 * value is computed as follows:
419 * - throughput * 50% if the incoming and outgoing interface are the
420 * same WiFi interface and the throughput is above
422 * - throughput if the outgoing interface is the default
423 * interface (i.e. this OGM is processed for the
424 * internal table and not forwarded)
425 * - throughput * hop penalty otherwise
427 * Return: the penalised throughput metric.
429 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
430 struct batadv_hard_iface *if_incoming,
431 struct batadv_hard_iface *if_outgoing,
434 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
435 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
437 /* Don't apply hop penalty in default originator table. */
438 if (if_outgoing == BATADV_IF_DEFAULT)
441 /* Forwarding on the same WiFi interface cuts the throughput in half
442 * due to the store & forward characteristics of WIFI.
443 * Very low throughput values are the exception.
445 if (throughput > 10 &&
446 if_incoming == if_outgoing &&
447 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
448 return throughput / 2;
450 /* hop penalty of 255 equals 100% */
451 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
455 * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
457 * @bat_priv: the bat priv with all the soft interface information
458 * @ogm_received: previously received OGM to be forwarded
459 * @orig_node: the originator which has been updated
460 * @neigh_node: the neigh_node through with the OGM has been received
461 * @if_incoming: the interface on which this OGM was received on
462 * @if_outgoing: the interface to which the OGM has to be forwarded to
464 * Forward an OGM to an interface after having altered the throughput metric and
465 * the TTL value contained in it. The original OGM isn't modified.
467 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
468 const struct batadv_ogm2_packet *ogm_received,
469 struct batadv_orig_node *orig_node,
470 struct batadv_neigh_node *neigh_node,
471 struct batadv_hard_iface *if_incoming,
472 struct batadv_hard_iface *if_outgoing)
474 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
475 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
476 struct batadv_neigh_node *router = NULL;
477 struct batadv_ogm2_packet *ogm_forward;
478 unsigned char *skb_buff;
483 /* only forward for specific interfaces, not for the default one. */
484 if (if_outgoing == BATADV_IF_DEFAULT)
487 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
491 /* acquire possibly updated router */
492 router = batadv_orig_router_get(orig_node, if_outgoing);
494 /* strict rule: forward packets coming from the best next hop only */
495 if (neigh_node != router)
498 /* don't forward the same seqno twice on one interface */
499 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
502 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
504 if (ogm_received->ttl <= 1) {
505 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
509 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
513 tvlv_len = ntohs(ogm_received->tvlv_len);
515 packet_len = BATADV_OGM2_HLEN + tvlv_len;
516 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
517 ETH_HLEN + packet_len);
521 skb_reserve(skb, ETH_HLEN);
522 skb_buff = skb_put_data(skb, ogm_received, packet_len);
524 /* apply forward penalty */
525 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
526 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
529 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
530 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
531 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
532 ogm_forward->ttl, if_incoming->net_dev->name);
534 batadv_v_ogm_queue_on_if(skb, if_outgoing);
538 batadv_orig_ifinfo_put(orig_ifinfo);
540 batadv_neigh_node_put(router);
542 batadv_neigh_ifinfo_put(neigh_ifinfo);
546 * batadv_v_ogm_metric_update() - update route metric based on OGM
547 * @bat_priv: the bat priv with all the soft interface information
548 * @ogm2: OGM2 structure
549 * @orig_node: Originator structure for which the OGM has been received
550 * @neigh_node: the neigh_node through with the OGM has been received
551 * @if_incoming: the interface where this packet was received
552 * @if_outgoing: the interface for which the packet should be considered
555 * 1 if the OGM is new,
556 * 0 if it is not new but valid,
557 * <0 on error (e.g. old OGM)
559 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
560 const struct batadv_ogm2_packet *ogm2,
561 struct batadv_orig_node *orig_node,
562 struct batadv_neigh_node *neigh_node,
563 struct batadv_hard_iface *if_incoming,
564 struct batadv_hard_iface *if_outgoing)
566 struct batadv_orig_ifinfo *orig_ifinfo;
567 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
568 bool protection_started = false;
573 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
577 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
579 if (!hlist_empty(&orig_node->neigh_list) &&
580 batadv_window_protected(bat_priv, seq_diff,
582 &orig_ifinfo->batman_seqno_reset,
583 &protection_started)) {
584 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
585 "Drop packet: packet within window protection time from %pM\n",
587 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
588 "Last reset: %ld, %ld\n",
589 orig_ifinfo->batman_seqno_reset, jiffies);
593 /* drop packets with old seqnos, however accept the first packet after
594 * a host has been rebooted.
596 if (seq_diff < 0 && !protection_started)
599 neigh_node->last_seen = jiffies;
601 orig_node->last_seen = jiffies;
603 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
604 orig_ifinfo->last_ttl = ogm2->ttl;
606 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
610 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
612 ntohl(ogm2->throughput));
613 neigh_ifinfo->bat_v.throughput = path_throughput;
614 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
615 neigh_ifinfo->last_ttl = ogm2->ttl;
617 if (seq_diff > 0 || protection_started)
623 batadv_orig_ifinfo_put(orig_ifinfo);
625 batadv_neigh_ifinfo_put(neigh_ifinfo);
631 * batadv_v_ogm_route_update() - update routes based on OGM
632 * @bat_priv: the bat priv with all the soft interface information
633 * @ethhdr: the Ethernet header of the OGM2
634 * @ogm2: OGM2 structure
635 * @orig_node: Originator structure for which the OGM has been received
636 * @neigh_node: the neigh_node through with the OGM has been received
637 * @if_incoming: the interface where this packet was received
638 * @if_outgoing: the interface for which the packet should be considered
640 * Return: true if the packet should be forwarded, false otherwise
642 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
643 const struct ethhdr *ethhdr,
644 const struct batadv_ogm2_packet *ogm2,
645 struct batadv_orig_node *orig_node,
646 struct batadv_neigh_node *neigh_node,
647 struct batadv_hard_iface *if_incoming,
648 struct batadv_hard_iface *if_outgoing)
650 struct batadv_neigh_node *router = NULL;
651 struct batadv_orig_node *orig_neigh_node;
652 struct batadv_neigh_node *orig_neigh_router = NULL;
653 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
654 u32 router_throughput, neigh_throughput;
655 u32 router_last_seqno;
656 u32 neigh_last_seqno;
658 bool forward = false;
660 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
661 if (!orig_neigh_node)
664 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
667 /* drop packet if sender is not a direct neighbor and if we
668 * don't route towards it
670 router = batadv_orig_router_get(orig_node, if_outgoing);
671 if (router && router->orig_node != orig_node && !orig_neigh_router) {
672 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
673 "Drop packet: OGM via unknown neighbor!\n");
677 /* Mark the OGM to be considered for forwarding, and update routes
682 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
683 "Searching and updating originator entry of received packet\n");
685 /* if this neighbor already is our next hop there is nothing
688 if (router == neigh_node)
691 /* don't consider neighbours with worse throughput.
692 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
693 * the last received seqno from our best next hop.
696 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
697 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
699 /* if these are not allocated, something is wrong. */
700 if (!router_ifinfo || !neigh_ifinfo)
703 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
704 router_last_seqno = router_ifinfo->bat_v.last_seqno;
705 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
706 router_throughput = router_ifinfo->bat_v.throughput;
707 neigh_throughput = neigh_ifinfo->bat_v.throughput;
709 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
710 router_throughput >= neigh_throughput)
714 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
717 batadv_neigh_node_put(router);
718 if (orig_neigh_router)
719 batadv_neigh_node_put(orig_neigh_router);
721 batadv_orig_node_put(orig_neigh_node);
723 batadv_neigh_ifinfo_put(router_ifinfo);
725 batadv_neigh_ifinfo_put(neigh_ifinfo);
731 * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
732 * @bat_priv: the bat priv with all the soft interface information
733 * @ethhdr: the Ethernet header of the OGM2
734 * @ogm2: OGM2 structure
735 * @orig_node: Originator structure for which the OGM has been received
736 * @neigh_node: the neigh_node through with the OGM has been received
737 * @if_incoming: the interface where this packet was received
738 * @if_outgoing: the interface for which the packet should be considered
741 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
742 const struct ethhdr *ethhdr,
743 const struct batadv_ogm2_packet *ogm2,
744 struct batadv_orig_node *orig_node,
745 struct batadv_neigh_node *neigh_node,
746 struct batadv_hard_iface *if_incoming,
747 struct batadv_hard_iface *if_outgoing)
752 /* first, update the metric with according sanity checks */
753 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
754 neigh_node, if_incoming,
757 /* outdated sequence numbers are to be discarded */
761 /* only unknown & newer OGMs contain TVLVs we are interested in */
762 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
763 batadv_tvlv_containers_process(bat_priv, true, orig_node,
765 (unsigned char *)(ogm2 + 1),
766 ntohs(ogm2->tvlv_len));
768 /* if the metric update went through, update routes if needed */
769 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
770 neigh_node, if_incoming,
773 /* if the routes have been processed correctly, check and forward */
775 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
776 if_incoming, if_outgoing);
780 * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
781 * @buff_pos: current position in the skb
782 * @packet_len: total length of the skb
783 * @tvlv_len: tvlv length of the previously considered OGM
785 * Return: true if there is enough space for another OGM, false otherwise.
787 static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
790 int next_buff_pos = 0;
792 next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
793 next_buff_pos += ntohs(tvlv_len);
795 return (next_buff_pos <= packet_len) &&
796 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
800 * batadv_v_ogm_process() - process an incoming batman v OGM
801 * @skb: the skb containing the OGM
802 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
803 * @if_incoming: the interface where this packet was receved
805 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
806 struct batadv_hard_iface *if_incoming)
808 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
809 struct ethhdr *ethhdr;
810 struct batadv_orig_node *orig_node = NULL;
811 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
812 struct batadv_neigh_node *neigh_node = NULL;
813 struct batadv_hard_iface *hard_iface;
814 struct batadv_ogm2_packet *ogm_packet;
815 u32 ogm_throughput, link_throughput, path_throughput;
818 ethhdr = eth_hdr(skb);
819 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
821 ogm_throughput = ntohl(ogm_packet->throughput);
823 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
824 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
825 ethhdr->h_source, if_incoming->net_dev->name,
826 if_incoming->net_dev->dev_addr, ogm_packet->orig,
827 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
828 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
830 /* If the throughput metric is 0, immediately drop the packet. No need
831 * to create orig_node / neigh_node for an unusable route.
833 if (ogm_throughput == 0) {
834 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
835 "Drop packet: originator packet with throughput metric of 0\n");
839 /* require ELP packets be to received from this neighbor first */
840 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
842 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
843 "Drop packet: OGM via unknown neighbor!\n");
847 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
851 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
856 /* Update the received throughput metric to match the link
858 * - If this OGM traveled one hop so far (emitted by single hop
859 * neighbor) the path throughput metric equals the link throughput.
860 * - For OGMs traversing more than hop the path throughput metric is
861 * the smaller of the path throughput and the link throughput.
863 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
864 path_throughput = min_t(u32, link_throughput, ogm_throughput);
865 ogm_packet->throughput = htonl(path_throughput);
867 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
868 neigh_node, if_incoming,
872 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
873 if (hard_iface->if_status != BATADV_IF_ACTIVE)
876 if (hard_iface->soft_iface != bat_priv->soft_iface)
879 if (!kref_get_unless_zero(&hard_iface->refcount))
882 ret = batadv_hardif_no_broadcast(hard_iface,
890 case BATADV_HARDIF_BCAST_NORECIPIENT:
891 type = "no neighbor";
893 case BATADV_HARDIF_BCAST_DUPFWD:
894 type = "single neighbor is source";
896 case BATADV_HARDIF_BCAST_DUPORIG:
897 type = "single neighbor is originator";
903 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
904 ogm_packet->orig, hard_iface->net_dev->name,
907 batadv_hardif_put(hard_iface);
911 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
912 orig_node, neigh_node,
913 if_incoming, hard_iface);
915 batadv_hardif_put(hard_iface);
920 batadv_orig_node_put(orig_node);
922 batadv_neigh_node_put(neigh_node);
924 batadv_hardif_neigh_put(hardif_neigh);
928 * batadv_v_ogm_packet_recv() - OGM2 receiving handler
929 * @skb: the received OGM
930 * @if_incoming: the interface where this OGM has been received
932 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
933 * (without freeing the skb) on failure
935 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
936 struct batadv_hard_iface *if_incoming)
938 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
939 struct batadv_ogm2_packet *ogm_packet;
940 struct ethhdr *ethhdr = eth_hdr(skb);
943 int ret = NET_RX_DROP;
945 /* did we receive a OGM2 packet on an interface that does not have
946 * B.A.T.M.A.N. V enabled ?
948 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
951 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
954 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
957 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
959 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
962 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
963 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
964 skb->len + ETH_HLEN);
967 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
969 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
970 ogm_packet->tvlv_len)) {
971 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
973 ogm_offset += BATADV_OGM2_HLEN;
974 ogm_offset += ntohs(ogm_packet->tvlv_len);
976 packet_pos = skb->data + ogm_offset;
977 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
980 ret = NET_RX_SUCCESS;
983 if (ret == NET_RX_SUCCESS)
992 * batadv_v_ogm_init() - initialise the OGM2 engine
993 * @bat_priv: the bat priv with all the soft interface information
995 * Return: 0 on success or a negative error code in case of failure
997 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
999 struct batadv_ogm2_packet *ogm_packet;
1000 unsigned char *ogm_buff;
1003 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1004 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
1008 bat_priv->bat_v.ogm_buff = ogm_buff;
1009 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
1010 ogm_packet->packet_type = BATADV_OGM2;
1011 ogm_packet->version = BATADV_COMPAT_VERSION;
1012 ogm_packet->ttl = BATADV_TTL;
1013 ogm_packet->flags = BATADV_NO_FLAGS;
1014 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
1016 /* randomize initial seqno to avoid collision */
1017 get_random_bytes(&random_seqno, sizeof(random_seqno));
1018 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
1019 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
1025 * batadv_v_ogm_free() - free OGM private resources
1026 * @bat_priv: the bat priv with all the soft interface information
1028 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1030 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
1032 kfree(bat_priv->bat_v.ogm_buff);
1033 bat_priv->bat_v.ogm_buff = NULL;
1034 bat_priv->bat_v.ogm_buff_len = 0;