1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019 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/errno.h>
17 #include <linux/etherdevice.h>
18 #include <linux/gfp.h>
19 #include <linux/if_ether.h>
20 #include <linux/init.h>
21 #include <linux/jiffies.h>
22 #include <linux/kernel.h>
23 #include <linux/kref.h>
24 #include <linux/list.h>
25 #include <linux/netdevice.h>
26 #include <linux/netlink.h>
27 #include <linux/pkt_sched.h>
28 #include <linux/printk.h>
29 #include <linux/random.h>
30 #include <linux/rculist.h>
31 #include <linux/rcupdate.h>
32 #include <linux/seq_file.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/types.h>
39 #include <linux/workqueue.h>
40 #include <net/genetlink.h>
41 #include <net/netlink.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
47 #include "gateway_client.h"
48 #include "hard-interface.h"
52 #include "network-coding.h"
53 #include "originator.h"
56 #include "translation-table.h"
59 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
62 * enum batadv_dup_status - duplicate status
64 enum batadv_dup_status {
65 /** @BATADV_NO_DUP: the packet is no duplicate */
69 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
74 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
78 * @BATADV_PROTECTED: originator is currently protected (after reboot)
84 * batadv_ring_buffer_set() - update the ring buffer with the given value
85 * @lq_recv: pointer to the ring buffer
86 * @lq_index: index to store the value at
87 * @value: value to store in the ring buffer
89 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
91 lq_recv[*lq_index] = value;
92 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
96 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
97 * in the given ring buffer
98 * @lq_recv: pointer to the ring buffer
100 * Return: computed average value.
102 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
111 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
124 return (u8)(sum / count);
128 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
130 * @bat_priv: the bat priv with all the soft interface information
131 * @addr: mac address of the originator
133 * Return: the originator object corresponding to the passed mac address or NULL
135 * If the object does not exists it is created an initialised.
137 static struct batadv_orig_node *
138 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
140 struct batadv_orig_node *orig_node;
143 orig_node = batadv_orig_hash_find(bat_priv, addr);
147 orig_node = batadv_orig_node_new(bat_priv, addr);
151 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
153 kref_get(&orig_node->refcount);
154 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
155 batadv_choose_orig, orig_node,
156 &orig_node->hash_entry);
158 goto free_orig_node_hash;
163 /* reference for batadv_hash_add */
164 batadv_orig_node_put(orig_node);
165 /* reference from batadv_orig_node_new */
166 batadv_orig_node_put(orig_node);
171 static struct batadv_neigh_node *
172 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
173 const u8 *neigh_addr,
174 struct batadv_orig_node *orig_node,
175 struct batadv_orig_node *orig_neigh)
177 struct batadv_neigh_node *neigh_node;
179 neigh_node = batadv_neigh_node_get_or_create(orig_node,
180 hard_iface, neigh_addr);
184 neigh_node->orig_node = orig_neigh;
190 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
192 struct batadv_ogm_packet *batadv_ogm_packet;
193 unsigned char *ogm_buff;
196 /* randomize initial seqno to avoid collision */
197 get_random_bytes(&random_seqno, sizeof(random_seqno));
198 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
200 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
201 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
205 hard_iface->bat_iv.ogm_buff = ogm_buff;
207 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
208 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
209 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
210 batadv_ogm_packet->ttl = 2;
211 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
212 batadv_ogm_packet->reserved = 0;
213 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
218 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
220 kfree(hard_iface->bat_iv.ogm_buff);
221 hard_iface->bat_iv.ogm_buff = NULL;
224 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
226 struct batadv_ogm_packet *batadv_ogm_packet;
227 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
229 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
230 ether_addr_copy(batadv_ogm_packet->orig,
231 hard_iface->net_dev->dev_addr);
232 ether_addr_copy(batadv_ogm_packet->prev_sender,
233 hard_iface->net_dev->dev_addr);
237 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
239 struct batadv_ogm_packet *batadv_ogm_packet;
240 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
242 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
243 batadv_ogm_packet->ttl = BATADV_TTL;
246 /* when do we schedule our own ogm to be sent */
248 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
252 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
253 msecs += prandom_u32() % (2 * BATADV_JITTER);
255 return jiffies + msecs_to_jiffies(msecs);
258 /* when do we schedule a ogm packet to be sent */
259 static unsigned long batadv_iv_ogm_fwd_send_time(void)
261 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
264 /* apply hop penalty for a normal link */
265 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
267 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
270 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
271 new_tq /= BATADV_TQ_MAX_VALUE;
277 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
278 * @buff_pos: current position in the skb
279 * @packet_len: total length of the skb
280 * @ogm_packet: potential OGM in buffer
282 * Return: true if there is enough space for another OGM, false otherwise.
285 batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
286 const struct batadv_ogm_packet *ogm_packet)
288 int next_buff_pos = 0;
290 /* check if there is enough space for the header */
291 next_buff_pos += buff_pos + sizeof(*ogm_packet);
292 if (next_buff_pos > packet_len)
295 /* check if there is enough space for the optional TVLV */
296 next_buff_pos += ntohs(ogm_packet->tvlv_len);
298 return (next_buff_pos <= packet_len) &&
299 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
302 /* send a batman ogm to a given interface */
303 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
304 struct batadv_hard_iface *hard_iface)
306 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
310 struct batadv_ogm_packet *batadv_ogm_packet;
314 if (hard_iface->if_status != BATADV_IF_ACTIVE)
319 packet_pos = forw_packet->skb->data;
320 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
322 /* adjust all flags and log packets */
323 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
324 batadv_ogm_packet)) {
325 /* we might have aggregated direct link packets with an
326 * ordinary base packet
328 if (forw_packet->direct_link_flags & BIT(packet_num) &&
329 forw_packet->if_incoming == hard_iface)
330 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
332 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
334 if (packet_num > 0 || !forw_packet->own)
335 fwd_str = "Forwarding";
337 fwd_str = "Sending own";
339 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
340 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
341 fwd_str, (packet_num > 0 ? "aggregated " : ""),
342 batadv_ogm_packet->orig,
343 ntohl(batadv_ogm_packet->seqno),
344 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
345 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
347 hard_iface->net_dev->name,
348 hard_iface->net_dev->dev_addr);
350 buff_pos += BATADV_OGM_HLEN;
351 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
353 packet_pos = forw_packet->skb->data + buff_pos;
354 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
357 /* create clone because function is called more than once */
358 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
360 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
361 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
362 skb->len + ETH_HLEN);
363 batadv_send_broadcast_skb(skb, hard_iface);
367 /* send a batman ogm packet */
368 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
370 struct net_device *soft_iface;
372 if (!forw_packet->if_incoming) {
373 pr_err("Error - can't forward packet: incoming iface not specified\n");
377 soft_iface = forw_packet->if_incoming->soft_iface;
379 if (WARN_ON(!forw_packet->if_outgoing))
382 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
385 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
388 /* only for one specific outgoing interface */
389 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
393 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
394 * existing forward packet
395 * @new_bat_ogm_packet: OGM packet to be aggregated
396 * @bat_priv: the bat priv with all the soft interface information
397 * @packet_len: (total) length of the OGM
398 * @send_time: timestamp (jiffies) when the packet is to be sent
399 * @directlink: true if this is a direct link packet
400 * @if_incoming: interface where the packet was received
401 * @if_outgoing: interface for which the retransmission should be considered
402 * @forw_packet: the forwarded packet which should be checked
404 * Return: true if new_packet can be aggregated with forw_packet
407 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
408 struct batadv_priv *bat_priv,
409 int packet_len, unsigned long send_time,
411 const struct batadv_hard_iface *if_incoming,
412 const struct batadv_hard_iface *if_outgoing,
413 const struct batadv_forw_packet *forw_packet)
415 struct batadv_ogm_packet *batadv_ogm_packet;
416 int aggregated_bytes = forw_packet->packet_len + packet_len;
417 struct batadv_hard_iface *primary_if = NULL;
419 unsigned long aggregation_end_time;
421 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
422 aggregation_end_time = send_time;
423 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
425 /* we can aggregate the current packet to this aggregated packet
428 * - the send time is within our MAX_AGGREGATION_MS time
429 * - the resulting packet wont be bigger than
430 * MAX_AGGREGATION_BYTES
431 * otherwise aggregation is not possible
433 if (!time_before(send_time, forw_packet->send_time) ||
434 !time_after_eq(aggregation_end_time, forw_packet->send_time))
437 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
440 /* packet is not leaving on the same interface. */
441 if (forw_packet->if_outgoing != if_outgoing)
444 /* check aggregation compatibility
445 * -> direct link packets are broadcasted on
446 * their interface only
447 * -> aggregate packet if the current packet is
448 * a "global" packet as well as the base
451 primary_if = batadv_primary_if_get_selected(bat_priv);
455 /* packets without direct link flag and high TTL
456 * are flooded through the net
459 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
460 batadv_ogm_packet->ttl != 1 &&
462 /* own packets originating non-primary
463 * interfaces leave only that interface
465 (!forw_packet->own ||
466 forw_packet->if_incoming == primary_if)) {
471 /* if the incoming packet is sent via this one
472 * interface only - we still can aggregate
475 new_bat_ogm_packet->ttl == 1 &&
476 forw_packet->if_incoming == if_incoming &&
478 /* packets from direct neighbors or
479 * own secondary interface packets
480 * (= secondary interface packets in general)
482 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
484 forw_packet->if_incoming != primary_if))) {
491 batadv_hardif_put(primary_if);
496 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
498 * @packet_buff: pointer to the OGM
499 * @packet_len: (total) length of the OGM
500 * @send_time: timestamp (jiffies) when the packet is to be sent
501 * @direct_link: whether this OGM has direct link status
502 * @if_incoming: interface where the packet was received
503 * @if_outgoing: interface for which the retransmission should be considered
504 * @own_packet: true if it is a self-generated ogm
506 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
507 int packet_len, unsigned long send_time,
509 struct batadv_hard_iface *if_incoming,
510 struct batadv_hard_iface *if_outgoing,
513 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
514 struct batadv_forw_packet *forw_packet_aggr;
516 unsigned char *skb_buff;
517 unsigned int skb_size;
518 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
520 if (atomic_read(&bat_priv->aggregated_ogms) &&
521 packet_len < BATADV_MAX_AGGREGATION_BYTES)
522 skb_size = BATADV_MAX_AGGREGATION_BYTES;
524 skb_size = packet_len;
526 skb_size += ETH_HLEN;
528 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
532 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
533 queue_left, bat_priv, skb);
534 if (!forw_packet_aggr) {
539 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
540 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
542 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
543 forw_packet_aggr->packet_len = packet_len;
544 memcpy(skb_buff, packet_buff, packet_len);
546 forw_packet_aggr->own = own_packet;
547 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
548 forw_packet_aggr->send_time = send_time;
550 /* save packet direct link flag status */
552 forw_packet_aggr->direct_link_flags |= 1;
554 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
555 batadv_iv_send_outstanding_bat_ogm_packet);
557 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
560 /* aggregate a new packet into the existing ogm packet */
561 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
562 const unsigned char *packet_buff,
563 int packet_len, bool direct_link)
565 unsigned long new_direct_link_flag;
567 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
568 forw_packet_aggr->packet_len += packet_len;
569 forw_packet_aggr->num_packets++;
571 /* save packet direct link flag status */
573 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
574 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
579 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
580 * @bat_priv: the bat priv with all the soft interface information
581 * @packet_buff: pointer to the OGM
582 * @packet_len: (total) length of the OGM
583 * @if_incoming: interface where the packet was received
584 * @if_outgoing: interface for which the retransmission should be considered
585 * @own_packet: true if it is a self-generated ogm
586 * @send_time: timestamp (jiffies) when the packet is to be sent
588 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
589 unsigned char *packet_buff,
591 struct batadv_hard_iface *if_incoming,
592 struct batadv_hard_iface *if_outgoing,
593 int own_packet, unsigned long send_time)
595 /* _aggr -> pointer to the packet we want to aggregate with
596 * _pos -> pointer to the position in the queue
598 struct batadv_forw_packet *forw_packet_aggr = NULL;
599 struct batadv_forw_packet *forw_packet_pos = NULL;
600 struct batadv_ogm_packet *batadv_ogm_packet;
602 unsigned long max_aggregation_jiffies;
604 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
605 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
606 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
608 /* find position for the packet in the forward queue */
609 spin_lock_bh(&bat_priv->forw_bat_list_lock);
610 /* own packets are not to be aggregated */
611 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
612 hlist_for_each_entry(forw_packet_pos,
613 &bat_priv->forw_bat_list, list) {
614 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
615 bat_priv, packet_len,
616 send_time, direct_link,
620 forw_packet_aggr = forw_packet_pos;
626 /* nothing to aggregate with - either aggregation disabled or no
627 * suitable aggregation packet found
629 if (!forw_packet_aggr) {
630 /* the following section can run without the lock */
631 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
633 /* if we could not aggregate this packet with one of the others
634 * we hold it back for a while, so that it might be aggregated
637 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
638 send_time += max_aggregation_jiffies;
640 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
641 send_time, direct_link,
642 if_incoming, if_outgoing,
645 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
646 packet_len, direct_link);
647 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
651 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
652 const struct ethhdr *ethhdr,
653 struct batadv_ogm_packet *batadv_ogm_packet,
654 bool is_single_hop_neigh,
655 bool is_from_best_next_hop,
656 struct batadv_hard_iface *if_incoming,
657 struct batadv_hard_iface *if_outgoing)
659 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
662 if (batadv_ogm_packet->ttl <= 1) {
663 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
667 if (!is_from_best_next_hop) {
668 /* Mark the forwarded packet when it is not coming from our
669 * best next hop. We still need to forward the packet for our
670 * neighbor link quality detection to work in case the packet
671 * originated from a single hop neighbor. Otherwise we can
672 * simply drop the ogm.
674 if (is_single_hop_neigh)
675 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
680 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
682 batadv_ogm_packet->ttl--;
683 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
685 /* apply hop penalty */
686 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
689 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
690 "Forwarding packet: tq: %i, ttl: %i\n",
691 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
693 if (is_single_hop_neigh)
694 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
696 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
698 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
699 BATADV_OGM_HLEN + tvlv_len,
700 if_incoming, if_outgoing, 0,
701 batadv_iv_ogm_fwd_send_time());
705 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
706 * for the given interface
707 * @hard_iface: the interface for which the windows have to be shifted
710 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
712 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
713 struct batadv_hashtable *hash = bat_priv->orig_hash;
714 struct hlist_head *head;
715 struct batadv_orig_node *orig_node;
716 struct batadv_orig_ifinfo *orig_ifinfo;
721 for (i = 0; i < hash->size; i++) {
722 head = &hash->table[i];
725 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
726 hlist_for_each_entry_rcu(orig_ifinfo,
727 &orig_node->ifinfo_list,
729 if (orig_ifinfo->if_outgoing != hard_iface)
732 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
733 word = orig_ifinfo->bat_iv.bcast_own;
734 batadv_bit_get_packet(bat_priv, word, 1, 0);
735 w = &orig_ifinfo->bat_iv.bcast_own_sum;
736 *w = bitmap_weight(word,
737 BATADV_TQ_LOCAL_WINDOW_SIZE);
738 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
745 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
747 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
748 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
749 struct batadv_ogm_packet *batadv_ogm_packet;
750 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
751 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
754 unsigned long send_time;
756 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
757 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
760 /* the interface gets activated here to avoid race conditions between
761 * the moment of activating the interface in
762 * hardif_activate_interface() where the originator mac is set and
763 * outdated packets (especially uninitialized mac addresses) in the
766 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
767 hard_iface->if_status = BATADV_IF_ACTIVE;
769 primary_if = batadv_primary_if_get_selected(bat_priv);
771 if (hard_iface == primary_if) {
772 /* tt changes have to be committed before the tvlv data is
773 * appended as it may alter the tt tvlv container
775 batadv_tt_local_commit_changes(bat_priv);
776 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
781 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
782 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
784 /* change sequence number to network order */
785 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
786 batadv_ogm_packet->seqno = htonl(seqno);
787 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
789 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
791 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
793 if (hard_iface != primary_if) {
794 /* OGMs from secondary interfaces are only scheduled on their
795 * respective interfaces.
797 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
798 hard_iface, hard_iface, 1, send_time);
802 /* OGMs from primary interfaces are scheduled on all
806 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
807 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
810 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
813 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
814 *ogm_buff_len, hard_iface,
815 tmp_hard_iface, 1, send_time);
817 batadv_hardif_put(tmp_hard_iface);
823 batadv_hardif_put(primary_if);
827 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over iterface
828 * @orig_node: originator which reproadcasted the OGMs directly
829 * @if_outgoing: interface which transmitted the original OGM and received the
832 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
833 * an originator and directly (without intermediate hop) received by a specific
836 static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
837 struct batadv_hard_iface *if_outgoing)
839 struct batadv_orig_ifinfo *orig_ifinfo;
842 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
846 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
847 sum = orig_ifinfo->bat_iv.bcast_own_sum;
848 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
850 batadv_orig_ifinfo_put(orig_ifinfo);
856 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
858 * @bat_priv: the bat priv with all the soft interface information
859 * @orig_node: the orig node who originally emitted the ogm packet
860 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
861 * @ethhdr: Ethernet header of the OGM
862 * @batadv_ogm_packet: the ogm packet
863 * @if_incoming: interface where the packet was received
864 * @if_outgoing: interface for which the retransmission should be considered
865 * @dup_status: the duplicate status of this ogm packet.
868 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
869 struct batadv_orig_node *orig_node,
870 struct batadv_orig_ifinfo *orig_ifinfo,
871 const struct ethhdr *ethhdr,
872 const struct batadv_ogm_packet *batadv_ogm_packet,
873 struct batadv_hard_iface *if_incoming,
874 struct batadv_hard_iface *if_outgoing,
875 enum batadv_dup_status dup_status)
877 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
878 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
879 struct batadv_neigh_node *neigh_node = NULL;
880 struct batadv_neigh_node *tmp_neigh_node = NULL;
881 struct batadv_neigh_node *router = NULL;
882 u8 sum_orig, sum_neigh;
886 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
887 "%s(): Searching and updating originator entry of received packet\n",
891 hlist_for_each_entry_rcu(tmp_neigh_node,
892 &orig_node->neigh_list, list) {
893 neigh_addr = tmp_neigh_node->addr;
894 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
895 tmp_neigh_node->if_incoming == if_incoming &&
896 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
897 if (WARN(neigh_node, "too many matching neigh_nodes"))
898 batadv_neigh_node_put(neigh_node);
899 neigh_node = tmp_neigh_node;
903 if (dup_status != BATADV_NO_DUP)
906 /* only update the entry for this outgoing interface */
907 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
912 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
913 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
914 &neigh_ifinfo->bat_iv.tq_index, 0);
915 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
916 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
917 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
919 batadv_neigh_ifinfo_put(neigh_ifinfo);
924 struct batadv_orig_node *orig_tmp;
926 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
930 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
932 orig_node, orig_tmp);
934 batadv_orig_node_put(orig_tmp);
938 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
939 "Updating existing last-hop neighbor of originator\n");
943 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
947 neigh_node->last_seen = jiffies;
949 spin_lock_bh(&neigh_node->ifinfo_lock);
950 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
951 &neigh_ifinfo->bat_iv.tq_index,
952 batadv_ogm_packet->tq);
953 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
954 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
955 spin_unlock_bh(&neigh_node->ifinfo_lock);
957 if (dup_status == BATADV_NO_DUP) {
958 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
959 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
962 /* if this neighbor already is our next hop there is nothing
965 router = batadv_orig_router_get(orig_node, if_outgoing);
966 if (router == neigh_node)
970 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
974 /* if this neighbor does not offer a better TQ we won't
977 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
981 /* if the TQ is the same and the link not more symmetric we
982 * won't consider it either
985 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
986 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
987 router->if_incoming);
988 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
989 neigh_node->if_incoming);
990 if (sum_orig >= sum_neigh)
994 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1001 batadv_neigh_node_put(neigh_node);
1003 batadv_neigh_node_put(router);
1005 batadv_neigh_ifinfo_put(neigh_ifinfo);
1007 batadv_neigh_ifinfo_put(router_ifinfo);
1011 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1012 * @orig_node: the orig node who originally emitted the ogm packet
1013 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1014 * @batadv_ogm_packet: the ogm packet
1015 * @if_incoming: interface where the packet was received
1016 * @if_outgoing: interface for which the retransmission should be considered
1018 * Return: true if the link can be considered bidirectional, false otherwise
1020 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1021 struct batadv_orig_node *orig_neigh_node,
1022 struct batadv_ogm_packet *batadv_ogm_packet,
1023 struct batadv_hard_iface *if_incoming,
1024 struct batadv_hard_iface *if_outgoing)
1026 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1027 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1028 struct batadv_neigh_ifinfo *neigh_ifinfo;
1030 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1031 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1032 unsigned int tq_asym_penalty, inv_asym_penalty;
1033 unsigned int combined_tq;
1034 unsigned int tq_iface_penalty;
1037 /* find corresponding one hop neighbor */
1039 hlist_for_each_entry_rcu(tmp_neigh_node,
1040 &orig_neigh_node->neigh_list, list) {
1041 if (!batadv_compare_eth(tmp_neigh_node->addr,
1042 orig_neigh_node->orig))
1045 if (tmp_neigh_node->if_incoming != if_incoming)
1048 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1051 neigh_node = tmp_neigh_node;
1057 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1058 orig_neigh_node->orig,
1065 /* if orig_node is direct neighbor update neigh_node last_seen */
1066 if (orig_node == orig_neigh_node)
1067 neigh_node->last_seen = jiffies;
1069 orig_node->last_seen = jiffies;
1071 /* find packet count of corresponding one hop neighbor */
1072 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1073 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1075 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1076 batadv_neigh_ifinfo_put(neigh_ifinfo);
1081 /* pay attention to not get a value bigger than 100 % */
1082 if (orig_eq_count > neigh_rq_count)
1083 total_count = neigh_rq_count;
1085 total_count = orig_eq_count;
1087 /* if we have too few packets (too less data) we set tq_own to zero
1088 * if we receive too few packets it is not considered bidirectional
1090 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1091 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1094 /* neigh_node->real_packet_count is never zero as we
1095 * only purge old information when getting new
1098 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1100 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1101 * affect the nearly-symmetric links only a little, but
1102 * punishes asymmetric links more. This will give a value
1103 * between 0 and TQ_MAX_VALUE
1105 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1106 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1107 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1108 BATADV_TQ_LOCAL_WINDOW_SIZE *
1109 BATADV_TQ_LOCAL_WINDOW_SIZE;
1110 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1111 inv_asym_penalty /= neigh_rq_max_cube;
1112 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1114 /* penalize if the OGM is forwarded on the same interface. WiFi
1115 * interfaces and other half duplex devices suffer from throughput
1116 * drops as they can't send and receive at the same time.
1118 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1119 if (if_outgoing && if_incoming == if_outgoing &&
1120 batadv_is_wifi_hardif(if_outgoing))
1121 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1124 combined_tq = batadv_ogm_packet->tq *
1128 combined_tq /= BATADV_TQ_MAX_VALUE *
1129 BATADV_TQ_MAX_VALUE *
1130 BATADV_TQ_MAX_VALUE;
1131 batadv_ogm_packet->tq = combined_tq;
1133 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1134 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1135 orig_node->orig, orig_neigh_node->orig, total_count,
1136 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1137 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1138 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1140 /* if link has the minimum required transmission quality
1141 * consider it bidirectional
1143 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1148 batadv_neigh_node_put(neigh_node);
1153 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
1154 * adjust the sequence number and find out whether it is a duplicate
1155 * @ethhdr: ethernet header of the packet
1156 * @batadv_ogm_packet: OGM packet to be considered
1157 * @if_incoming: interface on which the OGM packet was received
1158 * @if_outgoing: interface for which the retransmission should be considered
1160 * Return: duplicate status as enum batadv_dup_status
1162 static enum batadv_dup_status
1163 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1164 const struct batadv_ogm_packet *batadv_ogm_packet,
1165 const struct batadv_hard_iface *if_incoming,
1166 struct batadv_hard_iface *if_outgoing)
1168 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1169 struct batadv_orig_node *orig_node;
1170 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1171 struct batadv_neigh_node *neigh_node;
1172 struct batadv_neigh_ifinfo *neigh_ifinfo;
1175 bool need_update = false;
1177 enum batadv_dup_status ret = BATADV_NO_DUP;
1178 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1181 unsigned long *bitmap;
1183 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1185 return BATADV_NO_DUP;
1187 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1188 if (WARN_ON(!orig_ifinfo)) {
1189 batadv_orig_node_put(orig_node);
1193 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1194 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1196 /* signalize caller that the packet is to be dropped. */
1197 if (!hlist_empty(&orig_node->neigh_list) &&
1198 batadv_window_protected(bat_priv, seq_diff,
1199 BATADV_TQ_LOCAL_WINDOW_SIZE,
1200 &orig_ifinfo->batman_seqno_reset, NULL)) {
1201 ret = BATADV_PROTECTED;
1206 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1207 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1212 neigh_addr = neigh_node->addr;
1213 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1214 orig_ifinfo->last_real_seqno,
1217 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1218 neigh_node->if_incoming == if_incoming) {
1221 ret = BATADV_NEIGH_DUP;
1224 if (is_dup && ret != BATADV_NEIGH_DUP)
1225 ret = BATADV_ORIG_DUP;
1228 /* if the window moved, set the update flag. */
1229 bitmap = neigh_ifinfo->bat_iv.real_bits;
1230 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1231 seq_diff, set_mark);
1233 packet_count = bitmap_weight(bitmap,
1234 BATADV_TQ_LOCAL_WINDOW_SIZE);
1235 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1236 batadv_neigh_ifinfo_put(neigh_ifinfo);
1241 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1242 "%s updating last_seqno: old %u, new %u\n",
1243 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1244 orig_ifinfo->last_real_seqno, seqno);
1245 orig_ifinfo->last_real_seqno = seqno;
1249 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1250 batadv_orig_node_put(orig_node);
1251 batadv_orig_ifinfo_put(orig_ifinfo);
1256 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1258 * @skb: the skb containing the OGM
1259 * @ogm_offset: offset from skb->data to start of ogm header
1260 * @orig_node: the (cached) orig node for the originator of this OGM
1261 * @if_incoming: the interface where this packet was received
1262 * @if_outgoing: the interface for which the packet should be considered
1265 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1266 struct batadv_orig_node *orig_node,
1267 struct batadv_hard_iface *if_incoming,
1268 struct batadv_hard_iface *if_outgoing)
1270 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1271 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1272 struct batadv_neigh_node *router = NULL;
1273 struct batadv_neigh_node *router_router = NULL;
1274 struct batadv_orig_node *orig_neigh_node;
1275 struct batadv_orig_ifinfo *orig_ifinfo;
1276 struct batadv_neigh_node *orig_neigh_router = NULL;
1277 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1278 struct batadv_ogm_packet *ogm_packet;
1279 enum batadv_dup_status dup_status;
1280 bool is_from_best_next_hop = false;
1281 bool is_single_hop_neigh = false;
1282 bool sameseq, similar_ttl;
1283 struct sk_buff *skb_priv;
1284 struct ethhdr *ethhdr;
1288 /* create a private copy of the skb, as some functions change tq value
1291 skb_priv = skb_copy(skb, GFP_ATOMIC);
1295 ethhdr = eth_hdr(skb_priv);
1296 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1298 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1299 if_incoming, if_outgoing);
1300 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1301 is_single_hop_neigh = true;
1303 if (dup_status == BATADV_PROTECTED) {
1304 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1305 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1310 if (ogm_packet->tq == 0) {
1311 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1312 "Drop packet: originator packet with tq equal 0\n");
1316 if (is_single_hop_neigh) {
1317 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1320 hardif_neigh->last_seen = jiffies;
1323 router = batadv_orig_router_get(orig_node, if_outgoing);
1325 router_router = batadv_orig_router_get(router->orig_node,
1327 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1330 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1331 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1332 is_from_best_next_hop = true;
1334 prev_sender = ogm_packet->prev_sender;
1335 /* avoid temporary routing loops */
1336 if (router && router_router &&
1337 (batadv_compare_eth(router->addr, prev_sender)) &&
1338 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1339 (batadv_compare_eth(router->addr, router_router->addr))) {
1340 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1341 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1346 if (if_outgoing == BATADV_IF_DEFAULT)
1347 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1349 /* if sender is a direct neighbor the sender mac equals
1352 if (is_single_hop_neigh)
1353 orig_neigh_node = orig_node;
1355 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1358 if (!orig_neigh_node)
1361 /* Update nc_nodes of the originator */
1362 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1363 ogm_packet, is_single_hop_neigh);
1365 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1368 /* drop packet if sender is not a direct neighbor and if we
1369 * don't route towards it
1371 if (!is_single_hop_neigh && !orig_neigh_router) {
1372 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1373 "Drop packet: OGM via unknown neighbor!\n");
1377 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1378 ogm_packet, if_incoming,
1381 /* update ranking if it is not a duplicate or has the same
1382 * seqno and similar ttl as the non-duplicate
1384 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1388 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1389 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1391 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1392 (sameseq && similar_ttl))) {
1393 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1394 orig_ifinfo, ethhdr,
1395 ogm_packet, if_incoming,
1396 if_outgoing, dup_status);
1398 batadv_orig_ifinfo_put(orig_ifinfo);
1400 /* only forward for specific interface, not for the default one. */
1401 if (if_outgoing == BATADV_IF_DEFAULT)
1404 /* is single hop (direct) neighbor */
1405 if (is_single_hop_neigh) {
1406 /* OGMs from secondary interfaces should only scheduled once
1407 * per interface where it has been received, not multiple times
1409 if (ogm_packet->ttl <= 2 &&
1410 if_incoming != if_outgoing) {
1411 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1412 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1415 /* mark direct link on incoming interface */
1416 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1417 is_single_hop_neigh,
1418 is_from_best_next_hop, if_incoming,
1421 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1422 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1426 /* multihop originator */
1428 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1429 "Drop packet: not received via bidirectional link\n");
1433 if (dup_status == BATADV_NEIGH_DUP) {
1434 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1435 "Drop packet: duplicate packet received\n");
1439 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1440 "Forwarding packet: rebroadcast originator packet\n");
1441 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1442 is_single_hop_neigh, is_from_best_next_hop,
1443 if_incoming, if_outgoing);
1446 if (orig_neigh_node && !is_single_hop_neigh)
1447 batadv_orig_node_put(orig_neigh_node);
1450 batadv_neigh_ifinfo_put(router_ifinfo);
1452 batadv_neigh_node_put(router);
1454 batadv_neigh_node_put(router_router);
1455 if (orig_neigh_router)
1456 batadv_neigh_node_put(orig_neigh_router);
1458 batadv_hardif_neigh_put(hardif_neigh);
1460 consume_skb(skb_priv);
1464 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1465 * @ogm_packet: rebroadcast OGM packet to process
1466 * @if_incoming: the interface where this packet was received
1467 * @orig_node: originator which reproadcasted the OGMs
1468 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1470 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1471 struct batadv_hard_iface *if_incoming,
1472 struct batadv_orig_node *orig_node,
1473 u32 if_incoming_seqno)
1475 struct batadv_orig_ifinfo *orig_ifinfo;
1479 /* neighbor has to indicate direct link and it has to
1480 * come via the corresponding interface
1482 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1485 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1489 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1493 /* save packet seqno for bidirectional check */
1494 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1495 bit_pos = if_incoming_seqno - 2;
1496 bit_pos -= ntohl(ogm_packet->seqno);
1497 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1498 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1499 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1500 BATADV_TQ_LOCAL_WINDOW_SIZE);
1501 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1503 batadv_orig_ifinfo_put(orig_ifinfo);
1507 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1508 * @skb: the skb containing the OGM
1509 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1510 * @if_incoming: the interface where this packet was receved
1512 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1513 struct batadv_hard_iface *if_incoming)
1515 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1516 struct batadv_orig_node *orig_neigh_node, *orig_node;
1517 struct batadv_hard_iface *hard_iface;
1518 struct batadv_ogm_packet *ogm_packet;
1519 u32 if_incoming_seqno;
1520 bool has_directlink_flag;
1521 struct ethhdr *ethhdr;
1522 bool is_my_oldorig = false;
1523 bool is_my_addr = false;
1524 bool is_my_orig = false;
1526 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1527 ethhdr = eth_hdr(skb);
1529 /* Silently drop when the batman packet is actually not a
1532 * This might happen if a packet is padded (e.g. Ethernet has a
1533 * minimum frame length of 64 byte) and the aggregation interprets
1534 * it as an additional length.
1536 * TODO: A more sane solution would be to have a bit in the
1537 * batadv_ogm_packet to detect whether the packet is the last
1538 * packet in an aggregation. Here we expect that the padding
1539 * is always zero (or not 0x01)
1541 if (ogm_packet->packet_type != BATADV_IV_OGM)
1544 /* could be changed by schedule_own_packet() */
1545 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1547 if (ogm_packet->flags & BATADV_DIRECTLINK)
1548 has_directlink_flag = true;
1550 has_directlink_flag = false;
1552 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1553 "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",
1554 ethhdr->h_source, if_incoming->net_dev->name,
1555 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1556 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1557 ogm_packet->tq, ogm_packet->ttl,
1558 ogm_packet->version, has_directlink_flag);
1561 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1562 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1565 if (hard_iface->soft_iface != if_incoming->soft_iface)
1568 if (batadv_compare_eth(ethhdr->h_source,
1569 hard_iface->net_dev->dev_addr))
1572 if (batadv_compare_eth(ogm_packet->orig,
1573 hard_iface->net_dev->dev_addr))
1576 if (batadv_compare_eth(ogm_packet->prev_sender,
1577 hard_iface->net_dev->dev_addr))
1578 is_my_oldorig = true;
1583 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1584 "Drop packet: received my own broadcast (sender: %pM)\n",
1590 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1592 if (!orig_neigh_node)
1595 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1596 orig_neigh_node, if_incoming_seqno);
1598 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1599 "Drop packet: originator packet from myself (via neighbor)\n");
1600 batadv_orig_node_put(orig_neigh_node);
1604 if (is_my_oldorig) {
1605 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1606 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1611 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1612 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1613 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1618 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1622 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1623 if_incoming, BATADV_IF_DEFAULT);
1626 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1627 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1630 if (hard_iface->soft_iface != bat_priv->soft_iface)
1633 if (!kref_get_unless_zero(&hard_iface->refcount))
1636 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1637 if_incoming, hard_iface);
1639 batadv_hardif_put(hard_iface);
1643 batadv_orig_node_put(orig_node);
1646 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1648 struct delayed_work *delayed_work;
1649 struct batadv_forw_packet *forw_packet;
1650 struct batadv_priv *bat_priv;
1651 bool dropped = false;
1653 delayed_work = to_delayed_work(work);
1654 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1656 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1658 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1663 batadv_iv_ogm_emit(forw_packet);
1665 /* we have to have at least one packet in the queue to determine the
1666 * queues wake up time unless we are shutting down.
1668 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1669 * primary interface should only be rescheduled once per period, but
1670 * this function will be called for the forw_packet instances of the
1671 * other secondary interfaces as well.
1673 if (forw_packet->own &&
1674 forw_packet->if_incoming == forw_packet->if_outgoing)
1675 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1678 /* do we get something for free()? */
1679 if (batadv_forw_packet_steal(forw_packet,
1680 &bat_priv->forw_bat_list_lock))
1681 batadv_forw_packet_free(forw_packet, dropped);
1684 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1685 struct batadv_hard_iface *if_incoming)
1687 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1688 struct batadv_ogm_packet *ogm_packet;
1692 int ret = NET_RX_DROP;
1694 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1698 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1699 * that does not have B.A.T.M.A.N. IV enabled ?
1701 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1704 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1705 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1706 skb->len + ETH_HLEN);
1709 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1711 /* unpack the aggregated packets and process them one by one */
1712 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1714 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1716 ogm_offset += BATADV_OGM_HLEN;
1717 ogm_offset += ntohs(ogm_packet->tvlv_len);
1719 packet_pos = skb->data + ogm_offset;
1720 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1723 ret = NET_RX_SUCCESS;
1726 if (ret == NET_RX_SUCCESS)
1734 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1736 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
1737 * @orig_node: the orig_node for which the neighbors are printed
1738 * @if_outgoing: outgoing interface for these entries
1739 * @seq: debugfs table seq_file struct
1741 * Must be called while holding an rcu lock.
1744 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1745 struct batadv_hard_iface *if_outgoing,
1746 struct seq_file *seq)
1748 struct batadv_neigh_node *neigh_node;
1749 struct batadv_neigh_ifinfo *n_ifinfo;
1751 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1752 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1756 seq_printf(seq, " %pM (%3i)",
1758 n_ifinfo->bat_iv.tq_avg);
1760 batadv_neigh_ifinfo_put(n_ifinfo);
1765 * batadv_iv_ogm_orig_print() - print the originator table
1766 * @bat_priv: the bat priv with all the soft interface information
1767 * @seq: debugfs table seq_file struct
1768 * @if_outgoing: the outgoing interface for which this should be printed
1770 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1771 struct seq_file *seq,
1772 struct batadv_hard_iface *if_outgoing)
1774 struct batadv_neigh_node *neigh_node;
1775 struct batadv_hashtable *hash = bat_priv->orig_hash;
1776 int last_seen_msecs, last_seen_secs;
1777 struct batadv_orig_node *orig_node;
1778 struct batadv_neigh_ifinfo *n_ifinfo;
1779 unsigned long last_seen_jiffies;
1780 struct hlist_head *head;
1781 int batman_count = 0;
1785 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1787 for (i = 0; i < hash->size; i++) {
1788 head = &hash->table[i];
1791 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1792 neigh_node = batadv_orig_router_get(orig_node,
1797 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1802 if (n_ifinfo->bat_iv.tq_avg == 0)
1805 last_seen_jiffies = jiffies - orig_node->last_seen;
1806 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1807 last_seen_secs = last_seen_msecs / 1000;
1808 last_seen_msecs = last_seen_msecs % 1000;
1810 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1811 orig_node->orig, last_seen_secs,
1812 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1814 neigh_node->if_incoming->net_dev->name);
1816 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1818 seq_putc(seq, '\n');
1822 batadv_neigh_node_put(neigh_node);
1824 batadv_neigh_ifinfo_put(n_ifinfo);
1829 if (batman_count == 0)
1830 seq_puts(seq, "No batman nodes in range ...\n");
1835 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1836 * given outgoing interface.
1837 * @neigh_node: Neighbour of interest
1838 * @if_outgoing: Outgoing interface of interest
1839 * @tq_avg: Pointer of where to store the TQ average
1841 * Return: False if no average TQ available, otherwise true.
1844 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1845 struct batadv_hard_iface *if_outgoing,
1848 struct batadv_neigh_ifinfo *n_ifinfo;
1850 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1854 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1855 batadv_neigh_ifinfo_put(n_ifinfo);
1861 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1863 * @msg: Netlink message to dump into
1864 * @portid: Port making netlink request
1865 * @seq: Sequence number of netlink message
1866 * @bat_priv: The bat priv with all the soft interface information
1867 * @if_outgoing: Limit dump to entries with this outgoing interface
1868 * @orig_node: Originator to dump
1869 * @neigh_node: Single hops neighbour
1870 * @best: Is the best originator
1872 * Return: Error code, or 0 on success
1875 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1876 struct batadv_priv *bat_priv,
1877 struct batadv_hard_iface *if_outgoing,
1878 struct batadv_orig_node *orig_node,
1879 struct batadv_neigh_node *neigh_node,
1884 unsigned int last_seen_msecs;
1886 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1888 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1891 if (if_outgoing != BATADV_IF_DEFAULT &&
1892 if_outgoing != neigh_node->if_incoming)
1895 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1896 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1900 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1902 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1903 neigh_node->addr) ||
1904 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1905 neigh_node->if_incoming->net_dev->ifindex) ||
1906 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1907 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1909 goto nla_put_failure;
1911 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1912 goto nla_put_failure;
1914 genlmsg_end(msg, hdr);
1918 genlmsg_cancel(msg, hdr);
1923 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
1924 * @msg: Netlink message to dump into
1925 * @portid: Port making netlink request
1926 * @seq: Sequence number of netlink message
1927 * @bat_priv: The bat priv with all the soft interface information
1928 * @if_outgoing: Limit dump to entries with this outgoing interface
1929 * @orig_node: Originator to dump
1930 * @sub_s: Number of sub entries to skip
1932 * This function assumes the caller holds rcu_read_lock().
1934 * Return: Error code, or 0 on success
1937 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1938 struct batadv_priv *bat_priv,
1939 struct batadv_hard_iface *if_outgoing,
1940 struct batadv_orig_node *orig_node, int *sub_s)
1942 struct batadv_neigh_node *neigh_node_best;
1943 struct batadv_neigh_node *neigh_node;
1948 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1949 if (!neigh_node_best)
1952 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1956 if (tq_avg_best == 0)
1959 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1963 best = (neigh_node == neigh_node_best);
1965 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1966 bat_priv, if_outgoing,
1967 orig_node, neigh_node,
1969 batadv_neigh_node_put(neigh_node_best);
1977 if (neigh_node_best)
1978 batadv_neigh_node_put(neigh_node_best);
1985 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
1987 * @msg: Netlink message to dump into
1988 * @portid: Port making netlink request
1989 * @seq: Sequence number of netlink message
1990 * @bat_priv: The bat priv with all the soft interface information
1991 * @if_outgoing: Limit dump to entries with this outgoing interface
1992 * @head: Bucket to be dumped
1993 * @idx_s: Number of entries to be skipped
1994 * @sub: Number of sub entries to be skipped
1996 * Return: Error code, or 0 on success
1999 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2000 struct batadv_priv *bat_priv,
2001 struct batadv_hard_iface *if_outgoing,
2002 struct hlist_head *head, int *idx_s, int *sub)
2004 struct batadv_orig_node *orig_node;
2008 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2012 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2013 if_outgoing, orig_node,
2028 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2029 * @msg: Netlink message to dump into
2030 * @cb: Control block containing additional options
2031 * @bat_priv: The bat priv with all the soft interface information
2032 * @if_outgoing: Limit dump to entries with this outgoing interface
2035 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2036 struct batadv_priv *bat_priv,
2037 struct batadv_hard_iface *if_outgoing)
2039 struct batadv_hashtable *hash = bat_priv->orig_hash;
2040 struct hlist_head *head;
2041 int bucket = cb->args[0];
2042 int idx = cb->args[1];
2043 int sub = cb->args[2];
2044 int portid = NETLINK_CB(cb->skb).portid;
2046 while (bucket < hash->size) {
2047 head = &hash->table[bucket];
2049 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2051 bat_priv, if_outgoing, head,
2058 cb->args[0] = bucket;
2063 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2065 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
2066 * @seq: neighbour table seq_file struct
2067 * @hardif_neigh: hardif neighbour information
2070 batadv_iv_hardif_neigh_print(struct seq_file *seq,
2071 struct batadv_hardif_neigh_node *hardif_neigh)
2073 int last_secs, last_msecs;
2075 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2076 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2078 seq_printf(seq, " %10s %pM %4i.%03is\n",
2079 hardif_neigh->if_incoming->net_dev->name,
2080 hardif_neigh->addr, last_secs, last_msecs);
2084 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
2085 * @bat_priv: the bat priv with all the soft interface information
2086 * @seq: neighbour table seq_file struct
2088 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2089 struct seq_file *seq)
2091 struct net_device *net_dev = (struct net_device *)seq->private;
2092 struct batadv_hardif_neigh_node *hardif_neigh;
2093 struct batadv_hard_iface *hard_iface;
2094 int batman_count = 0;
2096 seq_puts(seq, " IF Neighbor last-seen\n");
2099 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2100 if (hard_iface->soft_iface != net_dev)
2103 hlist_for_each_entry_rcu(hardif_neigh,
2104 &hard_iface->neigh_list, list) {
2105 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2111 if (batman_count == 0)
2112 seq_puts(seq, "No batman nodes in range ...\n");
2117 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2118 * @neigh1: the first neighbor object of the comparison
2119 * @if_outgoing1: outgoing interface for the first neighbor
2120 * @neigh2: the second neighbor object of the comparison
2121 * @if_outgoing2: outgoing interface for the second neighbor
2122 * @diff: pointer to integer receiving the calculated difference
2124 * The content of *@diff is only valid when this function returns true.
2125 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2126 * the same as or higher than the metric via neigh2
2128 * Return: true when the difference could be calculated, false otherwise
2130 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2131 struct batadv_hard_iface *if_outgoing1,
2132 struct batadv_neigh_node *neigh2,
2133 struct batadv_hard_iface *if_outgoing2,
2136 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2140 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2141 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2143 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2148 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2149 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2150 *diff = (int)tq1 - (int)tq2;
2154 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2156 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2162 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2163 * @msg: Netlink message to dump into
2164 * @portid: Port making netlink request
2165 * @seq: Sequence number of netlink message
2166 * @hardif_neigh: Neighbour to be dumped
2168 * Return: Error code, or 0 on success
2171 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2172 struct batadv_hardif_neigh_node *hardif_neigh)
2175 unsigned int last_seen_msecs;
2177 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2179 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2180 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2184 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2185 hardif_neigh->addr) ||
2186 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2187 hardif_neigh->if_incoming->net_dev->ifindex) ||
2188 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2190 goto nla_put_failure;
2192 genlmsg_end(msg, hdr);
2196 genlmsg_cancel(msg, hdr);
2201 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2203 * @msg: Netlink message to dump into
2204 * @portid: Port making netlink request
2205 * @seq: Sequence number of netlink message
2206 * @bat_priv: The bat priv with all the soft interface information
2207 * @hard_iface: Hard interface to dump the neighbours for
2208 * @idx_s: Number of entries to skip
2210 * This function assumes the caller holds rcu_read_lock().
2212 * Return: Error code, or 0 on success
2215 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2216 struct batadv_priv *bat_priv,
2217 struct batadv_hard_iface *hard_iface,
2220 struct batadv_hardif_neigh_node *hardif_neigh;
2223 hlist_for_each_entry_rcu(hardif_neigh,
2224 &hard_iface->neigh_list, list) {
2228 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2240 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2241 * @msg: Netlink message to dump into
2242 * @cb: Control block containing additional options
2243 * @bat_priv: The bat priv with all the soft interface information
2244 * @single_hardif: Limit dump to this hard interfaace
2247 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2248 struct batadv_priv *bat_priv,
2249 struct batadv_hard_iface *single_hardif)
2251 struct batadv_hard_iface *hard_iface;
2253 int i_hardif_s = cb->args[0];
2254 int idx = cb->args[1];
2255 int portid = NETLINK_CB(cb->skb).portid;
2258 if (single_hardif) {
2259 if (i_hardif_s == 0) {
2260 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2268 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2270 if (hard_iface->soft_iface != bat_priv->soft_iface)
2273 if (i_hardif++ < i_hardif_s)
2276 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2279 hard_iface, &idx)) {
2287 cb->args[0] = i_hardif;
2292 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2293 * @neigh1: the first neighbor object of the comparison
2294 * @if_outgoing1: outgoing interface for the first neighbor
2295 * @neigh2: the second neighbor object of the comparison
2296 * @if_outgoing2: outgoing interface for the second neighbor
2298 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2299 * lower, the same as or higher than the metric via neigh2
2301 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2302 struct batadv_hard_iface *if_outgoing1,
2303 struct batadv_neigh_node *neigh2,
2304 struct batadv_hard_iface *if_outgoing2)
2309 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2310 if_outgoing2, &diff);
2318 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2319 * than neigh2 from the metric prospective
2320 * @neigh1: the first neighbor object of the comparison
2321 * @if_outgoing1: outgoing interface for the first neighbor
2322 * @neigh2: the second neighbor object of the comparison
2323 * @if_outgoing2: outgoing interface for the second neighbor
2325 * Return: true if the metric via neigh1 is equally good or better than
2326 * the metric via neigh2, false otherwise.
2329 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2330 struct batadv_hard_iface *if_outgoing1,
2331 struct batadv_neigh_node *neigh2,
2332 struct batadv_hard_iface *if_outgoing2)
2337 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2338 if_outgoing2, &diff);
2342 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2346 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2348 /* begin scheduling originator messages on that interface */
2349 batadv_iv_ogm_schedule(hard_iface);
2353 * batadv_iv_init_sel_class() - initialize GW selection class
2354 * @bat_priv: the bat priv with all the soft interface information
2356 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2358 /* set default TQ difference threshold to 20 */
2359 atomic_set(&bat_priv->gw.sel_class, 20);
2362 static struct batadv_gw_node *
2363 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2365 struct batadv_neigh_node *router;
2366 struct batadv_neigh_ifinfo *router_ifinfo;
2367 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2368 u64 max_gw_factor = 0;
2369 u64 tmp_gw_factor = 0;
2372 struct batadv_orig_node *orig_node;
2375 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2376 orig_node = gw_node->orig_node;
2377 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2381 router_ifinfo = batadv_neigh_ifinfo_get(router,
2386 if (!kref_get_unless_zero(&gw_node->refcount))
2389 tq_avg = router_ifinfo->bat_iv.tq_avg;
2391 switch (atomic_read(&bat_priv->gw.sel_class)) {
2392 case 1: /* fast connection */
2393 tmp_gw_factor = tq_avg * tq_avg;
2394 tmp_gw_factor *= gw_node->bandwidth_down;
2395 tmp_gw_factor *= 100 * 100;
2396 tmp_gw_factor >>= 18;
2398 if (tmp_gw_factor > max_gw_factor ||
2399 (tmp_gw_factor == max_gw_factor &&
2402 batadv_gw_node_put(curr_gw);
2404 kref_get(&curr_gw->refcount);
2408 default: /* 2: stable connection (use best statistic)
2409 * 3: fast-switch (use best statistic but change as
2410 * soon as a better gateway appears)
2411 * XX: late-switch (use best statistic but change as
2412 * soon as a better gateway appears which has
2413 * $routing_class more tq points)
2415 if (tq_avg > max_tq) {
2417 batadv_gw_node_put(curr_gw);
2419 kref_get(&curr_gw->refcount);
2424 if (tq_avg > max_tq)
2427 if (tmp_gw_factor > max_gw_factor)
2428 max_gw_factor = tmp_gw_factor;
2430 batadv_gw_node_put(gw_node);
2433 batadv_neigh_node_put(router);
2435 batadv_neigh_ifinfo_put(router_ifinfo);
2442 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2443 struct batadv_orig_node *curr_gw_orig,
2444 struct batadv_orig_node *orig_node)
2446 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2447 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2448 struct batadv_neigh_node *router_gw = NULL;
2449 struct batadv_neigh_node *router_orig = NULL;
2450 u8 gw_tq_avg, orig_tq_avg;
2453 /* dynamic re-election is performed only on fast or late switch */
2454 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2457 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2463 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2465 if (!router_gw_ifinfo) {
2470 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2474 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2476 if (!router_orig_ifinfo)
2479 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2480 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2482 /* the TQ value has to be better */
2483 if (orig_tq_avg < gw_tq_avg)
2486 /* if the routing class is greater than 3 the value tells us how much
2487 * greater the TQ value of the new gateway must be
2489 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2490 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2493 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2494 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2495 gw_tq_avg, orig_tq_avg);
2499 if (router_gw_ifinfo)
2500 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2501 if (router_orig_ifinfo)
2502 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2504 batadv_neigh_node_put(router_gw);
2506 batadv_neigh_node_put(router_orig);
2511 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2512 /* fails if orig_node has no router */
2513 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2514 struct seq_file *seq,
2515 const struct batadv_gw_node *gw_node)
2517 struct batadv_gw_node *curr_gw;
2518 struct batadv_neigh_node *router;
2519 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2522 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2526 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2530 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2532 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2533 (curr_gw == gw_node ? "=>" : " "),
2534 gw_node->orig_node->orig,
2535 router_ifinfo->bat_iv.tq_avg, router->addr,
2536 router->if_incoming->net_dev->name,
2537 gw_node->bandwidth_down / 10,
2538 gw_node->bandwidth_down % 10,
2539 gw_node->bandwidth_up / 10,
2540 gw_node->bandwidth_up % 10);
2541 ret = seq_has_overflowed(seq) ? -1 : 0;
2544 batadv_gw_node_put(curr_gw);
2547 batadv_neigh_ifinfo_put(router_ifinfo);
2549 batadv_neigh_node_put(router);
2553 static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2554 struct seq_file *seq)
2556 struct batadv_gw_node *gw_node;
2560 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2563 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2564 /* fails if orig_node has no router */
2565 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2573 seq_puts(seq, "No gateways in range ...\n");
2578 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2579 * @msg: Netlink message to dump into
2580 * @portid: Port making netlink request
2581 * @cb: Control block containing additional options
2582 * @bat_priv: The bat priv with all the soft interface information
2583 * @gw_node: Gateway to be dumped
2585 * Return: Error code, or 0 on success
2587 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2588 struct netlink_callback *cb,
2589 struct batadv_priv *bat_priv,
2590 struct batadv_gw_node *gw_node)
2592 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2593 struct batadv_neigh_node *router;
2594 struct batadv_gw_node *curr_gw = NULL;
2598 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2602 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2606 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2608 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2609 &batadv_netlink_family, NLM_F_MULTI,
2610 BATADV_CMD_GET_GATEWAYS);
2616 genl_dump_check_consistent(cb, hdr);
2620 if (curr_gw == gw_node)
2621 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2622 genlmsg_cancel(msg, hdr);
2626 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2627 gw_node->orig_node->orig) ||
2628 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2629 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2631 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2632 router->if_incoming->net_dev->name) ||
2633 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2634 gw_node->bandwidth_down) ||
2635 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2636 gw_node->bandwidth_up)) {
2637 genlmsg_cancel(msg, hdr);
2641 genlmsg_end(msg, hdr);
2646 batadv_gw_node_put(curr_gw);
2648 batadv_neigh_ifinfo_put(router_ifinfo);
2650 batadv_neigh_node_put(router);
2655 * batadv_iv_gw_dump() - Dump gateways into a message
2656 * @msg: Netlink message to dump into
2657 * @cb: Control block containing additional options
2658 * @bat_priv: The bat priv with all the soft interface information
2660 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2661 struct batadv_priv *bat_priv)
2663 int portid = NETLINK_CB(cb->skb).portid;
2664 struct batadv_gw_node *gw_node;
2665 int idx_skip = cb->args[0];
2668 spin_lock_bh(&bat_priv->gw.list_lock);
2669 cb->seq = bat_priv->gw.generation << 1 | 1;
2671 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2672 if (idx++ < idx_skip)
2675 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2684 spin_unlock_bh(&bat_priv->gw.list_lock);
2686 cb->args[0] = idx_skip;
2689 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2690 .name = "BATMAN_IV",
2692 .enable = batadv_iv_ogm_iface_enable,
2693 .enabled = batadv_iv_iface_enabled,
2694 .disable = batadv_iv_ogm_iface_disable,
2695 .update_mac = batadv_iv_ogm_iface_update_mac,
2696 .primary_set = batadv_iv_ogm_primary_iface_set,
2699 .cmp = batadv_iv_ogm_neigh_cmp,
2700 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2701 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2702 .print = batadv_iv_neigh_print,
2704 .dump = batadv_iv_ogm_neigh_dump,
2707 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2708 .print = batadv_iv_ogm_orig_print,
2710 .dump = batadv_iv_ogm_orig_dump,
2713 .init_sel_class = batadv_iv_init_sel_class,
2714 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2715 .is_eligible = batadv_iv_gw_is_eligible,
2716 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2717 .print = batadv_iv_gw_print,
2719 .dump = batadv_iv_gw_dump,
2724 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2726 * Return: 0 on success or negative error number in case of failure
2728 int __init batadv_iv_init(void)
2732 /* batman originator packet */
2733 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2734 batadv_iv_ogm_receive);
2738 ret = batadv_algo_register(&batadv_batman_iv);
2740 goto handler_unregister;
2745 batadv_recv_handler_unregister(BATADV_IV_OGM);