1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
7 #include "bat_iv_ogm.h"
10 #include <linux/atomic.h>
11 #include <linux/bitmap.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/byteorder/generic.h>
15 #include <linux/cache.h>
16 #include <linux/container_of.h>
17 #include <linux/errno.h>
18 #include <linux/etherdevice.h>
19 #include <linux/gfp.h>
20 #include <linux/if_ether.h>
21 #include <linux/init.h>
22 #include <linux/jiffies.h>
23 #include <linux/kref.h>
24 #include <linux/list.h>
25 #include <linux/lockdep.h>
26 #include <linux/minmax.h>
27 #include <linux/mutex.h>
28 #include <linux/netdevice.h>
29 #include <linux/netlink.h>
30 #include <linux/pkt_sched.h>
31 #include <linux/printk.h>
32 #include <linux/random.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/skbuff.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/stddef.h>
39 #include <linux/string.h>
40 #include <linux/string_choices.h>
41 #include <linux/types.h>
42 #include <linux/workqueue.h>
43 #include <net/genetlink.h>
44 #include <net/netlink.h>
45 #include <uapi/linux/batadv_packet.h>
46 #include <uapi/linux/batman_adv.h>
50 #include "gateway_client.h"
51 #include "hard-interface.h"
55 #include "network-coding.h"
56 #include "originator.h"
59 #include "translation-table.h"
62 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
65 * enum batadv_dup_status - duplicate status
67 enum batadv_dup_status {
68 /** @BATADV_NO_DUP: the packet is no duplicate */
72 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
77 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
81 * @BATADV_PROTECTED: originator is currently protected (after reboot)
87 * batadv_ring_buffer_set() - update the ring buffer with the given value
88 * @lq_recv: pointer to the ring buffer
89 * @lq_index: index to store the value at
90 * @value: value to store in the ring buffer
92 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
94 lq_recv[*lq_index] = value;
95 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
99 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
100 * in the given ring buffer
101 * @lq_recv: pointer to the ring buffer
103 * Return: computed average value.
105 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
114 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
127 return (u8)(sum / count);
131 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
133 * @bat_priv: the bat priv with all the mesh interface information
134 * @addr: mac address of the originator
136 * Return: the originator object corresponding to the passed mac address or NULL
138 * If the object does not exist, it is created and initialised.
140 static struct batadv_orig_node *
141 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
143 struct batadv_orig_node *orig_node;
146 orig_node = batadv_orig_hash_find(bat_priv, addr);
150 orig_node = batadv_orig_node_new(bat_priv, addr);
154 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
156 kref_get(&orig_node->refcount);
157 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
158 batadv_choose_orig, orig_node,
159 &orig_node->hash_entry);
161 goto free_orig_node_hash;
166 /* reference for batadv_hash_add */
167 batadv_orig_node_put(orig_node);
168 /* reference from batadv_orig_node_new */
169 batadv_orig_node_put(orig_node);
174 static struct batadv_neigh_node *
175 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
176 const u8 *neigh_addr,
177 struct batadv_orig_node *orig_node,
178 struct batadv_orig_node *orig_neigh)
180 struct batadv_neigh_node *neigh_node;
182 neigh_node = batadv_neigh_node_get_or_create(orig_node,
183 hard_iface, neigh_addr);
187 neigh_node->orig_node = orig_neigh;
193 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
195 struct batadv_ogm_packet *batadv_ogm_packet;
196 unsigned char *ogm_buff;
199 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
201 /* randomize initial seqno to avoid collision */
202 get_random_bytes(&random_seqno, sizeof(random_seqno));
203 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
205 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
206 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
208 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
212 hard_iface->bat_iv.ogm_buff = ogm_buff;
214 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
215 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
216 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
217 batadv_ogm_packet->ttl = 2;
218 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
219 batadv_ogm_packet->reserved = 0;
220 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
222 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
227 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
229 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
231 kfree(hard_iface->bat_iv.ogm_buff);
232 hard_iface->bat_iv.ogm_buff = NULL;
234 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
237 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
239 struct batadv_ogm_packet *batadv_ogm_packet;
242 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
244 ogm_buff = hard_iface->bat_iv.ogm_buff;
248 batadv_ogm_packet = ogm_buff;
249 ether_addr_copy(batadv_ogm_packet->orig,
250 hard_iface->net_dev->dev_addr);
251 ether_addr_copy(batadv_ogm_packet->prev_sender,
252 hard_iface->net_dev->dev_addr);
255 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
259 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
261 struct batadv_ogm_packet *batadv_ogm_packet;
264 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
266 ogm_buff = hard_iface->bat_iv.ogm_buff;
270 batadv_ogm_packet = ogm_buff;
271 batadv_ogm_packet->ttl = BATADV_TTL;
274 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
277 /* when do we schedule our own ogm to be sent */
279 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
283 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
284 msecs += get_random_u32_below(2 * BATADV_JITTER);
286 return jiffies + msecs_to_jiffies(msecs);
289 /* when do we schedule a ogm packet to be sent */
290 static unsigned long batadv_iv_ogm_fwd_send_time(void)
292 return jiffies + msecs_to_jiffies(get_random_u32_below(BATADV_JITTER / 2));
295 /* apply hop penalty for a normal link */
296 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
298 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
301 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
302 new_tq /= BATADV_TQ_MAX_VALUE;
308 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
309 * @buff_pos: current position in the skb
310 * @packet_len: total length of the skb
311 * @ogm_packet: potential OGM in buffer
313 * Return: true if there is enough space for another OGM, false otherwise.
316 batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
317 const struct batadv_ogm_packet *ogm_packet)
319 int next_buff_pos = 0;
321 /* check if there is enough space for the header */
322 next_buff_pos += buff_pos + sizeof(*ogm_packet);
323 if (next_buff_pos > packet_len)
326 /* check if there is enough space for the optional TVLV */
327 next_buff_pos += ntohs(ogm_packet->tvlv_len);
329 return next_buff_pos <= packet_len;
332 /* send a batman ogm to a given interface */
333 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
334 struct batadv_hard_iface *hard_iface)
336 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
340 struct batadv_ogm_packet *batadv_ogm_packet;
344 if (hard_iface->if_status != BATADV_IF_ACTIVE)
349 packet_pos = forw_packet->skb->data;
350 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
352 /* adjust all flags and log packets */
353 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
354 batadv_ogm_packet)) {
355 /* we might have aggregated direct link packets with an
356 * ordinary base packet
358 if (test_bit(packet_num, forw_packet->direct_link_flags) &&
359 forw_packet->if_incoming == hard_iface)
360 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
362 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
364 if (packet_num > 0 || !forw_packet->own)
365 fwd_str = "Forwarding";
367 fwd_str = "Sending own";
369 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
370 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
371 fwd_str, (packet_num > 0 ? "aggregated " : ""),
372 batadv_ogm_packet->orig,
373 ntohl(batadv_ogm_packet->seqno),
374 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
375 str_on_off(batadv_ogm_packet->flags & BATADV_DIRECTLINK),
376 hard_iface->net_dev->name,
377 hard_iface->net_dev->dev_addr);
379 buff_pos += BATADV_OGM_HLEN;
380 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
382 packet_pos = forw_packet->skb->data + buff_pos;
383 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
386 /* create clone because function is called more than once */
387 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
389 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
390 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
391 skb->len + ETH_HLEN);
392 batadv_send_broadcast_skb(skb, hard_iface);
396 /* send a batman ogm packet */
397 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
399 struct net_device *mesh_iface;
401 if (!forw_packet->if_incoming) {
402 pr_err("Error - can't forward packet: incoming iface not specified\n");
406 mesh_iface = forw_packet->if_incoming->mesh_iface;
408 if (WARN_ON(!forw_packet->if_outgoing))
411 if (forw_packet->if_outgoing->mesh_iface != mesh_iface) {
412 pr_warn("%s: mesh interface switch for queued OGM\n", __func__);
416 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
419 /* only for one specific outgoing interface */
420 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
424 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
425 * existing forward packet
426 * @new_bat_ogm_packet: OGM packet to be aggregated
427 * @bat_priv: the bat priv with all the mesh interface information
428 * @packet_len: (total) length of the OGM
429 * @send_time: timestamp (jiffies) when the packet is to be sent
430 * @directlink: true if this is a direct link packet
431 * @if_incoming: interface where the packet was received
432 * @if_outgoing: interface for which the retransmission should be considered
433 * @forw_packet: the forwarded packet which should be checked
435 * Return: true if new_packet can be aggregated with forw_packet
438 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
439 struct batadv_priv *bat_priv,
440 int packet_len, unsigned long send_time,
442 const struct batadv_hard_iface *if_incoming,
443 const struct batadv_hard_iface *if_outgoing,
444 const struct batadv_forw_packet *forw_packet)
446 struct batadv_ogm_packet *batadv_ogm_packet;
447 unsigned int aggregated_bytes = forw_packet->packet_len + packet_len;
448 struct batadv_hard_iface *primary_if = NULL;
449 u8 packet_num = forw_packet->num_packets;
451 unsigned long aggregation_end_time;
452 unsigned int max_bytes;
454 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
455 aggregation_end_time = send_time;
456 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
458 max_bytes = min_t(unsigned int, if_outgoing->net_dev->mtu,
459 BATADV_MAX_AGGREGATION_BYTES);
461 /* we can aggregate the current packet to this aggregated packet
464 * - the send time is within our MAX_AGGREGATION_MS time
465 * - the resulting packet won't be bigger than
466 * MAX_AGGREGATION_BYTES and MTU of the outgoing interface
467 * - the number of packets is lower than MAX_AGGREGATION_PACKETS
468 * otherwise aggregation is not possible
470 if (!time_before(send_time, forw_packet->send_time) ||
471 !time_after_eq(aggregation_end_time, forw_packet->send_time))
474 if (aggregated_bytes > max_bytes)
477 if (packet_num >= BATADV_MAX_AGGREGATION_PACKETS)
480 /* packet is not leaving on the same interface. */
481 if (forw_packet->if_outgoing != if_outgoing)
484 /* check aggregation compatibility
485 * -> direct link packets are broadcasted on
486 * their interface only
487 * -> aggregate packet if the current packet is
488 * a "global" packet as well as the base
491 primary_if = batadv_primary_if_get_selected(bat_priv);
495 /* packets without direct link flag and high TTL
496 * are flooded through the net
499 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
500 batadv_ogm_packet->ttl != 1 &&
502 /* own packets originating non-primary
503 * interfaces leave only that interface
505 (!forw_packet->own ||
506 forw_packet->if_incoming == primary_if)) {
511 /* if the incoming packet is sent via this one
512 * interface only - we still can aggregate
515 new_bat_ogm_packet->ttl == 1 &&
516 forw_packet->if_incoming == if_incoming &&
518 /* packets from direct neighbors or
519 * own secondary interface packets
520 * (= secondary interface packets in general)
522 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
524 forw_packet->if_incoming != primary_if))) {
530 batadv_hardif_put(primary_if);
535 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
537 * @packet_buff: pointer to the OGM
538 * @packet_len: (total) length of the OGM
539 * @send_time: timestamp (jiffies) when the packet is to be sent
540 * @direct_link: whether this OGM has direct link status
541 * @if_incoming: interface where the packet was received
542 * @if_outgoing: interface for which the retransmission should be considered
543 * @own_packet: true if it is a self-generated ogm
545 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
546 int packet_len, unsigned long send_time,
548 struct batadv_hard_iface *if_incoming,
549 struct batadv_hard_iface *if_outgoing,
552 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
553 struct batadv_forw_packet *forw_packet_aggr;
555 unsigned char *skb_buff;
556 unsigned int skb_size;
557 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
559 if (atomic_read(&bat_priv->aggregated_ogms))
560 skb_size = max_t(unsigned int, BATADV_MAX_AGGREGATION_BYTES,
563 skb_size = packet_len;
565 skb_size += ETH_HLEN;
567 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
571 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
572 queue_left, bat_priv, skb);
573 if (!forw_packet_aggr) {
578 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
579 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
581 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
582 forw_packet_aggr->packet_len = packet_len;
583 memcpy(skb_buff, packet_buff, packet_len);
585 forw_packet_aggr->own = own_packet;
586 bitmap_zero(forw_packet_aggr->direct_link_flags,
587 BATADV_MAX_AGGREGATION_PACKETS);
588 forw_packet_aggr->send_time = send_time;
590 /* save packet direct link flag status */
592 set_bit(0, forw_packet_aggr->direct_link_flags);
594 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
595 batadv_iv_send_outstanding_bat_ogm_packet);
597 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
600 /* aggregate a new packet into the existing ogm packet */
601 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
602 const unsigned char *packet_buff,
603 int packet_len, bool direct_link)
605 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
606 forw_packet_aggr->packet_len += packet_len;
608 /* save packet direct link flag status */
610 set_bit(forw_packet_aggr->num_packets,
611 forw_packet_aggr->direct_link_flags);
613 forw_packet_aggr->num_packets++;
617 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
618 * @bat_priv: the bat priv with all the mesh interface information
619 * @packet_buff: pointer to the OGM
620 * @packet_len: (total) length of the OGM
621 * @if_incoming: interface where the packet was received
622 * @if_outgoing: interface for which the retransmission should be considered
623 * @own_packet: true if it is a self-generated ogm
624 * @send_time: timestamp (jiffies) when the packet is to be sent
626 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
627 unsigned char *packet_buff,
629 struct batadv_hard_iface *if_incoming,
630 struct batadv_hard_iface *if_outgoing,
631 int own_packet, unsigned long send_time)
633 /* _aggr -> pointer to the packet we want to aggregate with
634 * _pos -> pointer to the position in the queue
636 struct batadv_forw_packet *forw_packet_aggr = NULL;
637 struct batadv_forw_packet *forw_packet_pos = NULL;
638 struct batadv_ogm_packet *batadv_ogm_packet;
640 unsigned long max_aggregation_jiffies;
642 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
643 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
644 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
646 /* find position for the packet in the forward queue */
647 spin_lock_bh(&bat_priv->forw_bat_list_lock);
648 /* own packets are not to be aggregated */
649 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
650 hlist_for_each_entry(forw_packet_pos,
651 &bat_priv->forw_bat_list, list) {
652 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
653 bat_priv, packet_len,
654 send_time, direct_link,
658 forw_packet_aggr = forw_packet_pos;
664 /* nothing to aggregate with - either aggregation disabled or no
665 * suitable aggregation packet found
667 if (!forw_packet_aggr) {
668 /* the following section can run without the lock */
669 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
671 /* if we could not aggregate this packet with one of the others
672 * we hold it back for a while, so that it might be aggregated
675 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
676 send_time += max_aggregation_jiffies;
678 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
679 send_time, direct_link,
680 if_incoming, if_outgoing,
683 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
684 packet_len, direct_link);
685 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
689 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
690 const struct ethhdr *ethhdr,
691 struct batadv_ogm_packet *batadv_ogm_packet,
692 bool is_single_hop_neigh,
693 bool is_from_best_next_hop,
694 struct batadv_hard_iface *if_incoming,
695 struct batadv_hard_iface *if_outgoing)
697 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
700 if (batadv_ogm_packet->ttl <= 1) {
701 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
705 if (!is_from_best_next_hop) {
706 /* Mark the forwarded packet when it is not coming from our
707 * best next hop. We still need to forward the packet for our
708 * neighbor link quality detection to work in case the packet
709 * originated from a single hop neighbor. Otherwise we can
710 * simply drop the ogm.
712 if (is_single_hop_neigh)
713 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
718 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
720 batadv_ogm_packet->ttl--;
721 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
723 /* apply hop penalty */
724 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
727 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
728 "Forwarding packet: tq: %i, ttl: %i\n",
729 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
731 if (is_single_hop_neigh)
732 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
734 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
736 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
737 BATADV_OGM_HLEN + tvlv_len,
738 if_incoming, if_outgoing, 0,
739 batadv_iv_ogm_fwd_send_time());
743 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
744 * for the given interface
745 * @hard_iface: the interface for which the windows have to be shifted
748 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
750 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
751 struct batadv_hashtable *hash = bat_priv->orig_hash;
752 struct hlist_head *head;
753 struct batadv_orig_node *orig_node;
754 struct batadv_orig_ifinfo *orig_ifinfo;
759 for (i = 0; i < hash->size; i++) {
760 head = &hash->table[i];
763 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
764 hlist_for_each_entry_rcu(orig_ifinfo,
765 &orig_node->ifinfo_list,
767 if (orig_ifinfo->if_outgoing != hard_iface)
770 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
771 word = orig_ifinfo->bat_iv.bcast_own;
772 batadv_bit_get_packet(bat_priv, word, 1, 0);
773 w = &orig_ifinfo->bat_iv.bcast_own_sum;
774 *w = bitmap_weight(word,
775 BATADV_TQ_LOCAL_WINDOW_SIZE);
776 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
784 * batadv_iv_ogm_schedule_buff() - schedule submission of hardif ogm buffer
785 * @hard_iface: interface whose ogm buffer should be transmitted
787 static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
789 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
790 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
791 struct batadv_ogm_packet *batadv_ogm_packet;
792 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
793 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
796 unsigned long send_time;
798 lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
800 /* interface already disabled by batadv_iv_ogm_iface_disable */
804 /* the interface gets activated here to avoid race conditions between
805 * the moment of activating the interface in
806 * hardif_activate_interface() where the originator mac is set and
807 * outdated packets (especially uninitialized mac addresses) in the
810 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
811 hard_iface->if_status = BATADV_IF_ACTIVE;
813 primary_if = batadv_primary_if_get_selected(bat_priv);
815 if (hard_iface == primary_if) {
816 /* tt changes have to be committed before the tvlv data is
817 * appended as it may alter the tt tvlv container
819 batadv_tt_local_commit_changes(bat_priv);
820 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
825 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
826 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
828 /* change sequence number to network order */
829 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
830 batadv_ogm_packet->seqno = htonl(seqno);
831 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
833 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
835 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
837 if (hard_iface != primary_if) {
838 /* OGMs from secondary interfaces are only scheduled on their
839 * respective interfaces.
841 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
842 hard_iface, hard_iface, 1, send_time);
846 /* OGMs from primary interfaces are scheduled on all
850 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
851 if (tmp_hard_iface->mesh_iface != hard_iface->mesh_iface)
854 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
857 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
858 *ogm_buff_len, hard_iface,
859 tmp_hard_iface, 1, send_time);
861 batadv_hardif_put(tmp_hard_iface);
866 batadv_hardif_put(primary_if);
869 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
871 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
872 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
875 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
876 batadv_iv_ogm_schedule_buff(hard_iface);
877 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
881 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over interface
882 * @orig_node: originator which reproadcasted the OGMs directly
883 * @if_outgoing: interface which transmitted the original OGM and received the
886 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
887 * an originator and directly (without intermediate hop) received by a specific
890 static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
891 struct batadv_hard_iface *if_outgoing)
893 struct batadv_orig_ifinfo *orig_ifinfo;
896 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
900 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
901 sum = orig_ifinfo->bat_iv.bcast_own_sum;
902 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
904 batadv_orig_ifinfo_put(orig_ifinfo);
910 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
912 * @bat_priv: the bat priv with all the mesh interface information
913 * @orig_node: the orig node who originally emitted the ogm packet
914 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
915 * @ethhdr: Ethernet header of the OGM
916 * @batadv_ogm_packet: the ogm packet
917 * @if_incoming: interface where the packet was received
918 * @if_outgoing: interface for which the retransmission should be considered
919 * @dup_status: the duplicate status of this ogm packet.
922 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
923 struct batadv_orig_node *orig_node,
924 struct batadv_orig_ifinfo *orig_ifinfo,
925 const struct ethhdr *ethhdr,
926 const struct batadv_ogm_packet *batadv_ogm_packet,
927 struct batadv_hard_iface *if_incoming,
928 struct batadv_hard_iface *if_outgoing,
929 enum batadv_dup_status dup_status)
931 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
932 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
933 struct batadv_neigh_node *neigh_node = NULL;
934 struct batadv_neigh_node *tmp_neigh_node = NULL;
935 struct batadv_neigh_node *router = NULL;
936 u8 sum_orig, sum_neigh;
940 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
941 "%s(): Searching and updating originator entry of received packet\n",
945 hlist_for_each_entry_rcu(tmp_neigh_node,
946 &orig_node->neigh_list, list) {
947 neigh_addr = tmp_neigh_node->addr;
948 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
949 tmp_neigh_node->if_incoming == if_incoming &&
950 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
951 if (WARN(neigh_node, "too many matching neigh_nodes"))
952 batadv_neigh_node_put(neigh_node);
953 neigh_node = tmp_neigh_node;
957 if (dup_status != BATADV_NO_DUP)
960 /* only update the entry for this outgoing interface */
961 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
966 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
967 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
968 &neigh_ifinfo->bat_iv.tq_index, 0);
969 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
970 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
971 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
973 batadv_neigh_ifinfo_put(neigh_ifinfo);
978 struct batadv_orig_node *orig_tmp;
980 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
984 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
986 orig_node, orig_tmp);
988 batadv_orig_node_put(orig_tmp);
992 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
993 "Updating existing last-hop neighbor of originator\n");
997 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1001 neigh_node->last_seen = jiffies;
1003 spin_lock_bh(&neigh_node->ifinfo_lock);
1004 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1005 &neigh_ifinfo->bat_iv.tq_index,
1006 batadv_ogm_packet->tq);
1007 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1008 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1009 spin_unlock_bh(&neigh_node->ifinfo_lock);
1011 if (dup_status == BATADV_NO_DUP) {
1012 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1013 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1016 /* if this neighbor already is our next hop there is nothing
1019 router = batadv_orig_router_get(orig_node, if_outgoing);
1020 if (router == neigh_node)
1024 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1028 /* if this neighbor does not offer a better TQ we won't
1031 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1035 /* if the TQ is the same and the link not more symmetric we
1036 * won't consider it either
1038 if (router_ifinfo &&
1039 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1040 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
1041 router->if_incoming);
1042 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
1043 neigh_node->if_incoming);
1044 if (sum_orig >= sum_neigh)
1048 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1054 batadv_neigh_node_put(neigh_node);
1055 batadv_neigh_node_put(router);
1056 batadv_neigh_ifinfo_put(neigh_ifinfo);
1057 batadv_neigh_ifinfo_put(router_ifinfo);
1061 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1062 * @orig_node: the orig node who originally emitted the ogm packet
1063 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1064 * @batadv_ogm_packet: the ogm packet
1065 * @if_incoming: interface where the packet was received
1066 * @if_outgoing: interface for which the retransmission should be considered
1068 * Return: true if the link can be considered bidirectional, false otherwise
1070 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1071 struct batadv_orig_node *orig_neigh_node,
1072 struct batadv_ogm_packet *batadv_ogm_packet,
1073 struct batadv_hard_iface *if_incoming,
1074 struct batadv_hard_iface *if_outgoing)
1076 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1077 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1078 struct batadv_neigh_ifinfo *neigh_ifinfo;
1080 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1081 unsigned int tq_iface_hop_penalty = BATADV_TQ_MAX_VALUE;
1082 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1083 unsigned int tq_asym_penalty, inv_asym_penalty;
1084 unsigned int combined_tq;
1087 /* find corresponding one hop neighbor */
1089 hlist_for_each_entry_rcu(tmp_neigh_node,
1090 &orig_neigh_node->neigh_list, list) {
1091 if (!batadv_compare_eth(tmp_neigh_node->addr,
1092 orig_neigh_node->orig))
1095 if (tmp_neigh_node->if_incoming != if_incoming)
1098 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1101 neigh_node = tmp_neigh_node;
1107 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1108 orig_neigh_node->orig,
1115 /* if orig_node is direct neighbor update neigh_node last_seen */
1116 if (orig_node == orig_neigh_node)
1117 neigh_node->last_seen = jiffies;
1119 orig_node->last_seen = jiffies;
1121 /* find packet count of corresponding one hop neighbor */
1122 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1123 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1125 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1126 batadv_neigh_ifinfo_put(neigh_ifinfo);
1131 /* pay attention to not get a value bigger than 100 % */
1132 if (orig_eq_count > neigh_rq_count)
1133 total_count = neigh_rq_count;
1135 total_count = orig_eq_count;
1137 /* if we have too few packets (too less data) we set tq_own to zero
1138 * if we receive too few packets it is not considered bidirectional
1140 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1141 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1144 /* neigh_node->real_packet_count is never zero as we
1145 * only purge old information when getting new
1148 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1150 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1151 * affect the nearly-symmetric links only a little, but
1152 * punishes asymmetric links more. This will give a value
1153 * between 0 and TQ_MAX_VALUE
1155 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1156 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1157 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1158 BATADV_TQ_LOCAL_WINDOW_SIZE *
1159 BATADV_TQ_LOCAL_WINDOW_SIZE;
1160 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1161 inv_asym_penalty /= neigh_rq_max_cube;
1162 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1163 tq_iface_hop_penalty -= atomic_read(&if_incoming->hop_penalty);
1165 /* penalize if the OGM is forwarded on the same interface. WiFi
1166 * interfaces and other half duplex devices suffer from throughput
1167 * drops as they can't send and receive at the same time.
1169 if (if_outgoing && if_incoming == if_outgoing &&
1170 batadv_is_wifi_hardif(if_outgoing))
1171 tq_iface_hop_penalty = batadv_hop_penalty(tq_iface_hop_penalty,
1174 combined_tq = batadv_ogm_packet->tq *
1177 tq_iface_hop_penalty;
1178 combined_tq /= BATADV_TQ_MAX_VALUE *
1179 BATADV_TQ_MAX_VALUE *
1180 BATADV_TQ_MAX_VALUE;
1181 batadv_ogm_packet->tq = combined_tq;
1183 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1184 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_hop_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1185 orig_node->orig, orig_neigh_node->orig, total_count,
1186 neigh_rq_count, tq_own, tq_asym_penalty,
1187 tq_iface_hop_penalty, batadv_ogm_packet->tq,
1188 if_incoming->net_dev->name,
1189 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1191 /* if link has the minimum required transmission quality
1192 * consider it bidirectional
1194 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1198 batadv_neigh_node_put(neigh_node);
1203 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
1204 * adjust the sequence number and find out whether it is a duplicate
1205 * @ethhdr: ethernet header of the packet
1206 * @batadv_ogm_packet: OGM packet to be considered
1207 * @if_incoming: interface on which the OGM packet was received
1208 * @if_outgoing: interface for which the retransmission should be considered
1210 * Return: duplicate status as enum batadv_dup_status
1212 static enum batadv_dup_status
1213 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1214 const struct batadv_ogm_packet *batadv_ogm_packet,
1215 const struct batadv_hard_iface *if_incoming,
1216 struct batadv_hard_iface *if_outgoing)
1218 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1219 struct batadv_orig_node *orig_node;
1220 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1221 struct batadv_neigh_node *neigh_node;
1222 struct batadv_neigh_ifinfo *neigh_ifinfo;
1225 bool need_update = false;
1227 enum batadv_dup_status ret = BATADV_NO_DUP;
1228 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1231 unsigned long *bitmap;
1233 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1235 return BATADV_NO_DUP;
1237 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1238 if (WARN_ON(!orig_ifinfo)) {
1239 batadv_orig_node_put(orig_node);
1243 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1244 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1246 /* signalize caller that the packet is to be dropped. */
1247 if (!hlist_empty(&orig_node->neigh_list) &&
1248 batadv_window_protected(bat_priv, seq_diff,
1249 BATADV_TQ_LOCAL_WINDOW_SIZE,
1250 &orig_ifinfo->batman_seqno_reset, NULL)) {
1251 ret = BATADV_PROTECTED;
1256 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1257 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1262 neigh_addr = neigh_node->addr;
1263 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1264 orig_ifinfo->last_real_seqno,
1267 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1268 neigh_node->if_incoming == if_incoming) {
1271 ret = BATADV_NEIGH_DUP;
1274 if (is_dup && ret != BATADV_NEIGH_DUP)
1275 ret = BATADV_ORIG_DUP;
1278 /* if the window moved, set the update flag. */
1279 bitmap = neigh_ifinfo->bat_iv.real_bits;
1280 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1281 seq_diff, set_mark);
1283 packet_count = bitmap_weight(bitmap,
1284 BATADV_TQ_LOCAL_WINDOW_SIZE);
1285 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1286 batadv_neigh_ifinfo_put(neigh_ifinfo);
1291 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1292 "%s updating last_seqno: old %u, new %u\n",
1293 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1294 orig_ifinfo->last_real_seqno, seqno);
1295 orig_ifinfo->last_real_seqno = seqno;
1299 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1300 batadv_orig_node_put(orig_node);
1301 batadv_orig_ifinfo_put(orig_ifinfo);
1306 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1308 * @skb: the skb containing the OGM
1309 * @ogm_offset: offset from skb->data to start of ogm header
1310 * @orig_node: the (cached) orig node for the originator of this OGM
1311 * @if_incoming: the interface where this packet was received
1312 * @if_outgoing: the interface for which the packet should be considered
1315 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1316 struct batadv_orig_node *orig_node,
1317 struct batadv_hard_iface *if_incoming,
1318 struct batadv_hard_iface *if_outgoing)
1320 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1321 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1322 struct batadv_neigh_node *router = NULL;
1323 struct batadv_neigh_node *router_router = NULL;
1324 struct batadv_orig_node *orig_neigh_node;
1325 struct batadv_orig_ifinfo *orig_ifinfo;
1326 struct batadv_neigh_node *orig_neigh_router = NULL;
1327 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1328 struct batadv_ogm_packet *ogm_packet;
1329 enum batadv_dup_status dup_status;
1330 bool is_from_best_next_hop = false;
1331 bool is_single_hop_neigh = false;
1332 bool sameseq, similar_ttl;
1333 struct sk_buff *skb_priv;
1334 struct ethhdr *ethhdr;
1338 /* create a private copy of the skb, as some functions change tq value
1341 skb_priv = skb_copy(skb, GFP_ATOMIC);
1345 ethhdr = eth_hdr(skb_priv);
1346 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1348 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1349 if_incoming, if_outgoing);
1350 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1351 is_single_hop_neigh = true;
1353 if (dup_status == BATADV_PROTECTED) {
1354 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1355 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1360 if (ogm_packet->tq == 0) {
1361 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1362 "Drop packet: originator packet with tq equal 0\n");
1366 if (is_single_hop_neigh) {
1367 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1370 hardif_neigh->last_seen = jiffies;
1373 router = batadv_orig_router_get(orig_node, if_outgoing);
1375 router_router = batadv_orig_router_get(router->orig_node,
1377 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1380 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1381 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1382 is_from_best_next_hop = true;
1384 prev_sender = ogm_packet->prev_sender;
1385 /* avoid temporary routing loops */
1386 if (router && router_router &&
1387 (batadv_compare_eth(router->addr, prev_sender)) &&
1388 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1389 (batadv_compare_eth(router->addr, router_router->addr))) {
1390 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1391 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1396 if (if_outgoing == BATADV_IF_DEFAULT)
1397 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1399 /* if sender is a direct neighbor the sender mac equals
1402 if (is_single_hop_neigh)
1403 orig_neigh_node = orig_node;
1405 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1408 if (!orig_neigh_node)
1411 /* Update nc_nodes of the originator */
1412 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1413 ogm_packet, is_single_hop_neigh);
1415 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1418 /* drop packet if sender is not a direct neighbor and if we
1419 * don't route towards it
1421 if (!is_single_hop_neigh && !orig_neigh_router) {
1422 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1423 "Drop packet: OGM via unknown neighbor!\n");
1427 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1428 ogm_packet, if_incoming,
1431 /* update ranking if it is not a duplicate or has the same
1432 * seqno and similar ttl as the non-duplicate
1434 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1438 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1439 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1441 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1442 (sameseq && similar_ttl))) {
1443 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1444 orig_ifinfo, ethhdr,
1445 ogm_packet, if_incoming,
1446 if_outgoing, dup_status);
1448 batadv_orig_ifinfo_put(orig_ifinfo);
1450 /* only forward for specific interface, not for the default one. */
1451 if (if_outgoing == BATADV_IF_DEFAULT)
1454 /* is single hop (direct) neighbor */
1455 if (is_single_hop_neigh) {
1456 /* OGMs from secondary interfaces should only scheduled once
1457 * per interface where it has been received, not multiple times
1459 if (ogm_packet->ttl <= 2 &&
1460 if_incoming != if_outgoing) {
1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1462 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1465 /* mark direct link on incoming interface */
1466 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1467 is_single_hop_neigh,
1468 is_from_best_next_hop, if_incoming,
1471 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1472 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1476 /* multihop originator */
1478 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1479 "Drop packet: not received via bidirectional link\n");
1483 if (dup_status == BATADV_NEIGH_DUP) {
1484 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1485 "Drop packet: duplicate packet received\n");
1489 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1490 "Forwarding packet: rebroadcast originator packet\n");
1491 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1492 is_single_hop_neigh, is_from_best_next_hop,
1493 if_incoming, if_outgoing);
1496 if (orig_neigh_node && !is_single_hop_neigh)
1497 batadv_orig_node_put(orig_neigh_node);
1499 batadv_neigh_ifinfo_put(router_ifinfo);
1500 batadv_neigh_node_put(router);
1501 batadv_neigh_node_put(router_router);
1502 batadv_neigh_node_put(orig_neigh_router);
1503 batadv_hardif_neigh_put(hardif_neigh);
1505 consume_skb(skb_priv);
1509 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1510 * @ogm_packet: rebroadcast OGM packet to process
1511 * @if_incoming: the interface where this packet was received
1512 * @orig_node: originator which reproadcasted the OGMs
1513 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1515 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1516 struct batadv_hard_iface *if_incoming,
1517 struct batadv_orig_node *orig_node,
1518 u32 if_incoming_seqno)
1520 struct batadv_orig_ifinfo *orig_ifinfo;
1524 /* neighbor has to indicate direct link and it has to
1525 * come via the corresponding interface
1527 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1530 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1534 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1538 /* save packet seqno for bidirectional check */
1539 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1540 bit_pos = if_incoming_seqno - 2;
1541 bit_pos -= ntohl(ogm_packet->seqno);
1542 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1543 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1544 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1545 BATADV_TQ_LOCAL_WINDOW_SIZE);
1546 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1548 batadv_orig_ifinfo_put(orig_ifinfo);
1552 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1553 * @skb: the skb containing the OGM
1554 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1555 * @if_incoming: the interface where this packet was received
1557 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1558 struct batadv_hard_iface *if_incoming)
1560 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1561 struct batadv_orig_node *orig_neigh_node, *orig_node;
1562 struct batadv_hard_iface *hard_iface;
1563 struct batadv_ogm_packet *ogm_packet;
1564 u32 if_incoming_seqno;
1565 bool has_directlink_flag;
1566 struct ethhdr *ethhdr;
1567 bool is_my_oldorig = false;
1568 bool is_my_addr = false;
1569 bool is_my_orig = false;
1571 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1572 ethhdr = eth_hdr(skb);
1574 /* Silently drop when the batman packet is actually not a
1577 * This might happen if a packet is padded (e.g. Ethernet has a
1578 * minimum frame length of 64 byte) and the aggregation interprets
1579 * it as an additional length.
1581 * TODO: A more sane solution would be to have a bit in the
1582 * batadv_ogm_packet to detect whether the packet is the last
1583 * packet in an aggregation. Here we expect that the padding
1584 * is always zero (or not 0x01)
1586 if (ogm_packet->packet_type != BATADV_IV_OGM)
1589 /* could be changed by schedule_own_packet() */
1590 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1592 if (ogm_packet->flags & BATADV_DIRECTLINK)
1593 has_directlink_flag = true;
1595 has_directlink_flag = false;
1597 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1598 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1599 ethhdr->h_source, if_incoming->net_dev->name,
1600 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1601 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1602 ogm_packet->tq, ogm_packet->ttl,
1603 ogm_packet->version, has_directlink_flag);
1606 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1607 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1610 if (hard_iface->mesh_iface != if_incoming->mesh_iface)
1613 if (batadv_compare_eth(ethhdr->h_source,
1614 hard_iface->net_dev->dev_addr))
1617 if (batadv_compare_eth(ogm_packet->orig,
1618 hard_iface->net_dev->dev_addr))
1621 if (batadv_compare_eth(ogm_packet->prev_sender,
1622 hard_iface->net_dev->dev_addr))
1623 is_my_oldorig = true;
1628 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1629 "Drop packet: received my own broadcast (sender: %pM)\n",
1635 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1637 if (!orig_neigh_node)
1640 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1641 orig_neigh_node, if_incoming_seqno);
1643 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1644 "Drop packet: originator packet from myself (via neighbor)\n");
1645 batadv_orig_node_put(orig_neigh_node);
1649 if (is_my_oldorig) {
1650 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1651 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1656 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1657 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1658 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1663 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1667 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1668 if_incoming, BATADV_IF_DEFAULT);
1671 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1672 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1675 if (hard_iface->mesh_iface != bat_priv->mesh_iface)
1678 if (!kref_get_unless_zero(&hard_iface->refcount))
1681 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1682 if_incoming, hard_iface);
1684 batadv_hardif_put(hard_iface);
1688 batadv_orig_node_put(orig_node);
1691 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1693 struct delayed_work *delayed_work;
1694 struct batadv_forw_packet *forw_packet;
1695 struct batadv_priv *bat_priv;
1696 bool dropped = false;
1698 delayed_work = to_delayed_work(work);
1699 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1701 bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);
1703 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1708 batadv_iv_ogm_emit(forw_packet);
1710 /* we have to have at least one packet in the queue to determine the
1711 * queues wake up time unless we are shutting down.
1713 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1714 * primary interface should only be rescheduled once per period, but
1715 * this function will be called for the forw_packet instances of the
1716 * other secondary interfaces as well.
1718 if (forw_packet->own &&
1719 forw_packet->if_incoming == forw_packet->if_outgoing)
1720 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1723 /* do we get something for free()? */
1724 if (batadv_forw_packet_steal(forw_packet,
1725 &bat_priv->forw_bat_list_lock))
1726 batadv_forw_packet_free(forw_packet, dropped);
1729 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1730 struct batadv_hard_iface *if_incoming)
1732 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
1733 struct batadv_ogm_packet *ogm_packet;
1737 int ret = NET_RX_DROP;
1739 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1743 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1744 * that does not have B.A.T.M.A.N. IV enabled ?
1746 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1749 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1750 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1751 skb->len + ETH_HLEN);
1754 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1756 /* unpack the aggregated packets and process them one by one */
1757 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1759 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1761 ogm_offset += BATADV_OGM_HLEN;
1762 ogm_offset += ntohs(ogm_packet->tvlv_len);
1764 packet_pos = skb->data + ogm_offset;
1765 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1768 ret = NET_RX_SUCCESS;
1771 if (ret == NET_RX_SUCCESS)
1780 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1781 * given outgoing interface.
1782 * @neigh_node: Neighbour of interest
1783 * @if_outgoing: Outgoing interface of interest
1784 * @tq_avg: Pointer of where to store the TQ average
1786 * Return: False if no average TQ available, otherwise true.
1789 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1790 struct batadv_hard_iface *if_outgoing,
1793 struct batadv_neigh_ifinfo *n_ifinfo;
1795 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1799 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1800 batadv_neigh_ifinfo_put(n_ifinfo);
1806 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1808 * @msg: Netlink message to dump into
1809 * @portid: Port making netlink request
1810 * @seq: Sequence number of netlink message
1811 * @bat_priv: The bat priv with all the mesh interface information
1812 * @if_outgoing: Limit dump to entries with this outgoing interface
1813 * @orig_node: Originator to dump
1814 * @neigh_node: Single hops neighbour
1815 * @best: Is the best originator
1817 * Return: Error code, or 0 on success
1820 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1821 struct batadv_priv *bat_priv,
1822 struct batadv_hard_iface *if_outgoing,
1823 struct batadv_orig_node *orig_node,
1824 struct batadv_neigh_node *neigh_node,
1829 unsigned int last_seen_msecs;
1831 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1833 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1836 if (if_outgoing != BATADV_IF_DEFAULT &&
1837 if_outgoing != neigh_node->if_incoming)
1840 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1841 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1845 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1847 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1848 neigh_node->addr) ||
1849 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
1850 neigh_node->if_incoming->net_dev->name) ||
1851 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1852 neigh_node->if_incoming->net_dev->ifindex) ||
1853 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1854 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1856 goto nla_put_failure;
1858 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1859 goto nla_put_failure;
1861 genlmsg_end(msg, hdr);
1865 genlmsg_cancel(msg, hdr);
1870 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
1871 * @msg: Netlink message to dump into
1872 * @portid: Port making netlink request
1873 * @seq: Sequence number of netlink message
1874 * @bat_priv: The bat priv with all the mesh interface information
1875 * @if_outgoing: Limit dump to entries with this outgoing interface
1876 * @orig_node: Originator to dump
1877 * @sub_s: Number of sub entries to skip
1879 * This function assumes the caller holds rcu_read_lock().
1881 * Return: Error code, or 0 on success
1884 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1885 struct batadv_priv *bat_priv,
1886 struct batadv_hard_iface *if_outgoing,
1887 struct batadv_orig_node *orig_node, int *sub_s)
1889 struct batadv_neigh_node *neigh_node_best;
1890 struct batadv_neigh_node *neigh_node;
1895 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1896 if (!neigh_node_best)
1899 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1903 if (tq_avg_best == 0)
1906 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1910 best = (neigh_node == neigh_node_best);
1912 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1913 bat_priv, if_outgoing,
1914 orig_node, neigh_node,
1916 batadv_neigh_node_put(neigh_node_best);
1924 batadv_neigh_node_put(neigh_node_best);
1931 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
1933 * @msg: Netlink message to dump into
1934 * @portid: Port making netlink request
1935 * @seq: Sequence number of netlink message
1936 * @bat_priv: The bat priv with all the mesh interface information
1937 * @if_outgoing: Limit dump to entries with this outgoing interface
1938 * @head: Bucket to be dumped
1939 * @idx_s: Number of entries to be skipped
1940 * @sub: Number of sub entries to be skipped
1942 * Return: Error code, or 0 on success
1945 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1946 struct batadv_priv *bat_priv,
1947 struct batadv_hard_iface *if_outgoing,
1948 struct hlist_head *head, int *idx_s, int *sub)
1950 struct batadv_orig_node *orig_node;
1954 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1958 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
1959 if_outgoing, orig_node,
1974 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
1975 * @msg: Netlink message to dump into
1976 * @cb: Control block containing additional options
1977 * @bat_priv: The bat priv with all the mesh interface information
1978 * @if_outgoing: Limit dump to entries with this outgoing interface
1981 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
1982 struct batadv_priv *bat_priv,
1983 struct batadv_hard_iface *if_outgoing)
1985 struct batadv_hashtable *hash = bat_priv->orig_hash;
1986 struct hlist_head *head;
1987 int bucket = cb->args[0];
1988 int idx = cb->args[1];
1989 int sub = cb->args[2];
1990 int portid = NETLINK_CB(cb->skb).portid;
1992 while (bucket < hash->size) {
1993 head = &hash->table[bucket];
1995 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
1997 bat_priv, if_outgoing, head,
2004 cb->args[0] = bucket;
2010 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2011 * @neigh1: the first neighbor object of the comparison
2012 * @if_outgoing1: outgoing interface for the first neighbor
2013 * @neigh2: the second neighbor object of the comparison
2014 * @if_outgoing2: outgoing interface for the second neighbor
2015 * @diff: pointer to integer receiving the calculated difference
2017 * The content of *@diff is only valid when this function returns true.
2018 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2019 * the same as or higher than the metric via neigh2
2021 * Return: true when the difference could be calculated, false otherwise
2023 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2024 struct batadv_hard_iface *if_outgoing1,
2025 struct batadv_neigh_node *neigh2,
2026 struct batadv_hard_iface *if_outgoing2,
2029 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2033 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2034 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2036 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2041 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2042 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2043 *diff = (int)tq1 - (int)tq2;
2046 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2047 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2053 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2054 * @msg: Netlink message to dump into
2055 * @portid: Port making netlink request
2056 * @seq: Sequence number of netlink message
2057 * @hardif_neigh: Neighbour to be dumped
2059 * Return: Error code, or 0 on success
2062 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2063 struct batadv_hardif_neigh_node *hardif_neigh)
2066 unsigned int last_seen_msecs;
2068 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2070 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2071 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2075 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2076 hardif_neigh->addr) ||
2077 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2078 hardif_neigh->if_incoming->net_dev->name) ||
2079 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2080 hardif_neigh->if_incoming->net_dev->ifindex) ||
2081 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2083 goto nla_put_failure;
2085 genlmsg_end(msg, hdr);
2089 genlmsg_cancel(msg, hdr);
2094 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2096 * @msg: Netlink message to dump into
2097 * @portid: Port making netlink request
2098 * @seq: Sequence number of netlink message
2099 * @bat_priv: The bat priv with all the mesh interface information
2100 * @hard_iface: Hard interface to dump the neighbours for
2101 * @idx_s: Number of entries to skip
2103 * This function assumes the caller holds rcu_read_lock().
2105 * Return: Error code, or 0 on success
2108 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2109 struct batadv_priv *bat_priv,
2110 struct batadv_hard_iface *hard_iface,
2113 struct batadv_hardif_neigh_node *hardif_neigh;
2116 hlist_for_each_entry_rcu(hardif_neigh,
2117 &hard_iface->neigh_list, list) {
2121 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2133 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2134 * @msg: Netlink message to dump into
2135 * @cb: Control block containing additional options
2136 * @bat_priv: The bat priv with all the mesh interface information
2137 * @single_hardif: Limit dump to this hard interface
2140 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2141 struct batadv_priv *bat_priv,
2142 struct batadv_hard_iface *single_hardif)
2144 struct batadv_hard_iface *hard_iface;
2146 int i_hardif_s = cb->args[0];
2147 int idx = cb->args[1];
2148 int portid = NETLINK_CB(cb->skb).portid;
2151 if (single_hardif) {
2152 if (i_hardif_s == 0) {
2153 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2161 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2163 if (hard_iface->mesh_iface != bat_priv->mesh_iface)
2166 if (i_hardif++ < i_hardif_s)
2169 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2172 hard_iface, &idx)) {
2180 cb->args[0] = i_hardif;
2185 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2186 * @neigh1: the first neighbor object of the comparison
2187 * @if_outgoing1: outgoing interface for the first neighbor
2188 * @neigh2: the second neighbor object of the comparison
2189 * @if_outgoing2: outgoing interface for the second neighbor
2191 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2192 * lower, the same as or higher than the metric via neigh2
2194 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2195 struct batadv_hard_iface *if_outgoing1,
2196 struct batadv_neigh_node *neigh2,
2197 struct batadv_hard_iface *if_outgoing2)
2202 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2203 if_outgoing2, &diff);
2211 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2212 * than neigh2 from the metric prospective
2213 * @neigh1: the first neighbor object of the comparison
2214 * @if_outgoing1: outgoing interface for the first neighbor
2215 * @neigh2: the second neighbor object of the comparison
2216 * @if_outgoing2: outgoing interface for the second neighbor
2218 * Return: true if the metric via neigh1 is equally good or better than
2219 * the metric via neigh2, false otherwise.
2222 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2223 struct batadv_hard_iface *if_outgoing1,
2224 struct batadv_neigh_node *neigh2,
2225 struct batadv_hard_iface *if_outgoing2)
2230 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2231 if_outgoing2, &diff);
2235 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2239 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2241 /* begin scheduling originator messages on that interface */
2242 batadv_iv_ogm_schedule(hard_iface);
2246 * batadv_iv_init_sel_class() - initialize GW selection class
2247 * @bat_priv: the bat priv with all the mesh interface information
2249 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2251 /* set default TQ difference threshold to 20 */
2252 atomic_set(&bat_priv->gw.sel_class, 20);
2255 static struct batadv_gw_node *
2256 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2258 struct batadv_neigh_node *router;
2259 struct batadv_neigh_ifinfo *router_ifinfo;
2260 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2261 u64 max_gw_factor = 0;
2262 u64 tmp_gw_factor = 0;
2265 struct batadv_orig_node *orig_node;
2268 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2269 orig_node = gw_node->orig_node;
2270 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2274 router_ifinfo = batadv_neigh_ifinfo_get(router,
2279 if (!kref_get_unless_zero(&gw_node->refcount))
2282 tq_avg = router_ifinfo->bat_iv.tq_avg;
2284 switch (atomic_read(&bat_priv->gw.sel_class)) {
2285 case 1: /* fast connection */
2286 tmp_gw_factor = tq_avg * tq_avg;
2287 tmp_gw_factor *= gw_node->bandwidth_down;
2288 tmp_gw_factor *= 100 * 100;
2289 tmp_gw_factor >>= 18;
2291 if (tmp_gw_factor > max_gw_factor ||
2292 (tmp_gw_factor == max_gw_factor &&
2294 batadv_gw_node_put(curr_gw);
2296 kref_get(&curr_gw->refcount);
2300 default: /* 2: stable connection (use best statistic)
2301 * 3: fast-switch (use best statistic but change as
2302 * soon as a better gateway appears)
2303 * XX: late-switch (use best statistic but change as
2304 * soon as a better gateway appears which has
2305 * $routing_class more tq points)
2307 if (tq_avg > max_tq) {
2308 batadv_gw_node_put(curr_gw);
2310 kref_get(&curr_gw->refcount);
2315 if (tq_avg > max_tq)
2318 if (tmp_gw_factor > max_gw_factor)
2319 max_gw_factor = tmp_gw_factor;
2321 batadv_gw_node_put(gw_node);
2324 batadv_neigh_node_put(router);
2325 batadv_neigh_ifinfo_put(router_ifinfo);
2332 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2333 struct batadv_orig_node *curr_gw_orig,
2334 struct batadv_orig_node *orig_node)
2336 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2337 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2338 struct batadv_neigh_node *router_gw = NULL;
2339 struct batadv_neigh_node *router_orig = NULL;
2340 u8 gw_tq_avg, orig_tq_avg;
2343 /* dynamic re-election is performed only on fast or late switch */
2344 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2347 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2353 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2355 if (!router_gw_ifinfo) {
2360 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2364 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2366 if (!router_orig_ifinfo)
2369 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2370 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2372 /* the TQ value has to be better */
2373 if (orig_tq_avg < gw_tq_avg)
2376 /* if the routing class is greater than 3 the value tells us how much
2377 * greater the TQ value of the new gateway must be
2379 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2380 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2383 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2384 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2385 gw_tq_avg, orig_tq_avg);
2389 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2390 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2391 batadv_neigh_node_put(router_gw);
2392 batadv_neigh_node_put(router_orig);
2398 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2399 * @msg: Netlink message to dump into
2400 * @portid: Port making netlink request
2401 * @cb: Control block containing additional options
2402 * @bat_priv: The bat priv with all the mesh interface information
2403 * @gw_node: Gateway to be dumped
2405 * Return: Error code, or 0 on success
2407 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2408 struct netlink_callback *cb,
2409 struct batadv_priv *bat_priv,
2410 struct batadv_gw_node *gw_node)
2412 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2413 struct batadv_neigh_node *router;
2414 struct batadv_gw_node *curr_gw = NULL;
2418 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2422 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2426 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2428 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2429 &batadv_netlink_family, NLM_F_MULTI,
2430 BATADV_CMD_GET_GATEWAYS);
2436 genl_dump_check_consistent(cb, hdr);
2440 if (curr_gw == gw_node)
2441 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2442 genlmsg_cancel(msg, hdr);
2446 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2447 gw_node->orig_node->orig) ||
2448 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2449 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2451 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2452 router->if_incoming->net_dev->name) ||
2453 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2454 router->if_incoming->net_dev->ifindex) ||
2455 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2456 gw_node->bandwidth_down) ||
2457 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2458 gw_node->bandwidth_up)) {
2459 genlmsg_cancel(msg, hdr);
2463 genlmsg_end(msg, hdr);
2467 batadv_gw_node_put(curr_gw);
2468 batadv_neigh_ifinfo_put(router_ifinfo);
2469 batadv_neigh_node_put(router);
2474 * batadv_iv_gw_dump() - Dump gateways into a message
2475 * @msg: Netlink message to dump into
2476 * @cb: Control block containing additional options
2477 * @bat_priv: The bat priv with all the mesh interface information
2479 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2480 struct batadv_priv *bat_priv)
2482 int portid = NETLINK_CB(cb->skb).portid;
2483 struct batadv_gw_node *gw_node;
2484 int idx_skip = cb->args[0];
2487 spin_lock_bh(&bat_priv->gw.list_lock);
2488 cb->seq = bat_priv->gw.generation << 1 | 1;
2490 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2491 if (idx++ < idx_skip)
2494 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2503 spin_unlock_bh(&bat_priv->gw.list_lock);
2505 cb->args[0] = idx_skip;
2508 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2509 .name = "BATMAN_IV",
2511 .enable = batadv_iv_ogm_iface_enable,
2512 .enabled = batadv_iv_iface_enabled,
2513 .disable = batadv_iv_ogm_iface_disable,
2514 .update_mac = batadv_iv_ogm_iface_update_mac,
2515 .primary_set = batadv_iv_ogm_primary_iface_set,
2518 .cmp = batadv_iv_ogm_neigh_cmp,
2519 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2520 .dump = batadv_iv_ogm_neigh_dump,
2523 .dump = batadv_iv_ogm_orig_dump,
2526 .init_sel_class = batadv_iv_init_sel_class,
2527 .sel_class_max = BATADV_TQ_MAX_VALUE,
2528 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2529 .is_eligible = batadv_iv_gw_is_eligible,
2530 .dump = batadv_iv_gw_dump,
2535 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2537 * Return: 0 on success or negative error number in case of failure
2539 int __init batadv_iv_init(void)
2543 /* batman originator packet */
2544 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2545 batadv_iv_ogm_receive);
2549 ret = batadv_algo_register(&batadv_batman_iv);
2551 goto handler_unregister;
2556 batadv_recv_handler_unregister(BATADV_IV_OGM);