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 * @tvlv_len: tvlv length of the previously considered OGM
282 * Return: true if there is enough space for another OGM, false otherwise.
284 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
287 int next_buff_pos = 0;
289 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
290 next_buff_pos += ntohs(tvlv_len);
292 return (next_buff_pos <= packet_len) &&
293 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
296 /* send a batman ogm to a given interface */
297 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
298 struct batadv_hard_iface *hard_iface)
300 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
304 struct batadv_ogm_packet *batadv_ogm_packet;
308 if (hard_iface->if_status != BATADV_IF_ACTIVE)
313 packet_pos = forw_packet->skb->data;
314 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
316 /* adjust all flags and log packets */
317 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
318 batadv_ogm_packet->tvlv_len)) {
319 /* we might have aggregated direct link packets with an
320 * ordinary base packet
322 if (forw_packet->direct_link_flags & BIT(packet_num) &&
323 forw_packet->if_incoming == hard_iface)
324 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
326 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
328 if (packet_num > 0 || !forw_packet->own)
329 fwd_str = "Forwarding";
331 fwd_str = "Sending own";
333 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
334 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
335 fwd_str, (packet_num > 0 ? "aggregated " : ""),
336 batadv_ogm_packet->orig,
337 ntohl(batadv_ogm_packet->seqno),
338 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
339 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
341 hard_iface->net_dev->name,
342 hard_iface->net_dev->dev_addr);
344 buff_pos += BATADV_OGM_HLEN;
345 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
347 packet_pos = forw_packet->skb->data + buff_pos;
348 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
351 /* create clone because function is called more than once */
352 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
354 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
355 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
356 skb->len + ETH_HLEN);
357 batadv_send_broadcast_skb(skb, hard_iface);
361 /* send a batman ogm packet */
362 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
364 struct net_device *soft_iface;
366 if (!forw_packet->if_incoming) {
367 pr_err("Error - can't forward packet: incoming iface not specified\n");
371 soft_iface = forw_packet->if_incoming->soft_iface;
373 if (WARN_ON(!forw_packet->if_outgoing))
376 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
379 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
382 /* only for one specific outgoing interface */
383 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
387 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
388 * existing forward packet
389 * @new_bat_ogm_packet: OGM packet to be aggregated
390 * @bat_priv: the bat priv with all the soft interface information
391 * @packet_len: (total) length of the OGM
392 * @send_time: timestamp (jiffies) when the packet is to be sent
393 * @directlink: true if this is a direct link packet
394 * @if_incoming: interface where the packet was received
395 * @if_outgoing: interface for which the retransmission should be considered
396 * @forw_packet: the forwarded packet which should be checked
398 * Return: true if new_packet can be aggregated with forw_packet
401 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
402 struct batadv_priv *bat_priv,
403 int packet_len, unsigned long send_time,
405 const struct batadv_hard_iface *if_incoming,
406 const struct batadv_hard_iface *if_outgoing,
407 const struct batadv_forw_packet *forw_packet)
409 struct batadv_ogm_packet *batadv_ogm_packet;
410 int aggregated_bytes = forw_packet->packet_len + packet_len;
411 struct batadv_hard_iface *primary_if = NULL;
413 unsigned long aggregation_end_time;
415 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
416 aggregation_end_time = send_time;
417 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
419 /* we can aggregate the current packet to this aggregated packet
422 * - the send time is within our MAX_AGGREGATION_MS time
423 * - the resulting packet wont be bigger than
424 * MAX_AGGREGATION_BYTES
425 * otherwise aggregation is not possible
427 if (!time_before(send_time, forw_packet->send_time) ||
428 !time_after_eq(aggregation_end_time, forw_packet->send_time))
431 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
434 /* packet is not leaving on the same interface. */
435 if (forw_packet->if_outgoing != if_outgoing)
438 /* check aggregation compatibility
439 * -> direct link packets are broadcasted on
440 * their interface only
441 * -> aggregate packet if the current packet is
442 * a "global" packet as well as the base
445 primary_if = batadv_primary_if_get_selected(bat_priv);
449 /* packets without direct link flag and high TTL
450 * are flooded through the net
453 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
454 batadv_ogm_packet->ttl != 1 &&
456 /* own packets originating non-primary
457 * interfaces leave only that interface
459 (!forw_packet->own ||
460 forw_packet->if_incoming == primary_if)) {
465 /* if the incoming packet is sent via this one
466 * interface only - we still can aggregate
469 new_bat_ogm_packet->ttl == 1 &&
470 forw_packet->if_incoming == if_incoming &&
472 /* packets from direct neighbors or
473 * own secondary interface packets
474 * (= secondary interface packets in general)
476 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
478 forw_packet->if_incoming != primary_if))) {
485 batadv_hardif_put(primary_if);
490 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
492 * @packet_buff: pointer to the OGM
493 * @packet_len: (total) length of the OGM
494 * @send_time: timestamp (jiffies) when the packet is to be sent
495 * @direct_link: whether this OGM has direct link status
496 * @if_incoming: interface where the packet was received
497 * @if_outgoing: interface for which the retransmission should be considered
498 * @own_packet: true if it is a self-generated ogm
500 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
501 int packet_len, unsigned long send_time,
503 struct batadv_hard_iface *if_incoming,
504 struct batadv_hard_iface *if_outgoing,
507 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
508 struct batadv_forw_packet *forw_packet_aggr;
510 unsigned char *skb_buff;
511 unsigned int skb_size;
512 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
514 if (atomic_read(&bat_priv->aggregated_ogms) &&
515 packet_len < BATADV_MAX_AGGREGATION_BYTES)
516 skb_size = BATADV_MAX_AGGREGATION_BYTES;
518 skb_size = packet_len;
520 skb_size += ETH_HLEN;
522 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
526 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
527 queue_left, bat_priv, skb);
528 if (!forw_packet_aggr) {
533 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
534 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
536 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
537 forw_packet_aggr->packet_len = packet_len;
538 memcpy(skb_buff, packet_buff, packet_len);
540 forw_packet_aggr->own = own_packet;
541 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
542 forw_packet_aggr->send_time = send_time;
544 /* save packet direct link flag status */
546 forw_packet_aggr->direct_link_flags |= 1;
548 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
549 batadv_iv_send_outstanding_bat_ogm_packet);
551 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
554 /* aggregate a new packet into the existing ogm packet */
555 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
556 const unsigned char *packet_buff,
557 int packet_len, bool direct_link)
559 unsigned long new_direct_link_flag;
561 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
562 forw_packet_aggr->packet_len += packet_len;
563 forw_packet_aggr->num_packets++;
565 /* save packet direct link flag status */
567 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
568 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
573 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
574 * @bat_priv: the bat priv with all the soft interface information
575 * @packet_buff: pointer to the OGM
576 * @packet_len: (total) length of the OGM
577 * @if_incoming: interface where the packet was received
578 * @if_outgoing: interface for which the retransmission should be considered
579 * @own_packet: true if it is a self-generated ogm
580 * @send_time: timestamp (jiffies) when the packet is to be sent
582 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
583 unsigned char *packet_buff,
585 struct batadv_hard_iface *if_incoming,
586 struct batadv_hard_iface *if_outgoing,
587 int own_packet, unsigned long send_time)
589 /* _aggr -> pointer to the packet we want to aggregate with
590 * _pos -> pointer to the position in the queue
592 struct batadv_forw_packet *forw_packet_aggr = NULL;
593 struct batadv_forw_packet *forw_packet_pos = NULL;
594 struct batadv_ogm_packet *batadv_ogm_packet;
596 unsigned long max_aggregation_jiffies;
598 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
599 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
600 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
602 /* find position for the packet in the forward queue */
603 spin_lock_bh(&bat_priv->forw_bat_list_lock);
604 /* own packets are not to be aggregated */
605 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
606 hlist_for_each_entry(forw_packet_pos,
607 &bat_priv->forw_bat_list, list) {
608 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
609 bat_priv, packet_len,
610 send_time, direct_link,
614 forw_packet_aggr = forw_packet_pos;
620 /* nothing to aggregate with - either aggregation disabled or no
621 * suitable aggregation packet found
623 if (!forw_packet_aggr) {
624 /* the following section can run without the lock */
625 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
627 /* if we could not aggregate this packet with one of the others
628 * we hold it back for a while, so that it might be aggregated
631 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
632 send_time += max_aggregation_jiffies;
634 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
635 send_time, direct_link,
636 if_incoming, if_outgoing,
639 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
640 packet_len, direct_link);
641 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
645 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
646 const struct ethhdr *ethhdr,
647 struct batadv_ogm_packet *batadv_ogm_packet,
648 bool is_single_hop_neigh,
649 bool is_from_best_next_hop,
650 struct batadv_hard_iface *if_incoming,
651 struct batadv_hard_iface *if_outgoing)
653 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
656 if (batadv_ogm_packet->ttl <= 1) {
657 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
661 if (!is_from_best_next_hop) {
662 /* Mark the forwarded packet when it is not coming from our
663 * best next hop. We still need to forward the packet for our
664 * neighbor link quality detection to work in case the packet
665 * originated from a single hop neighbor. Otherwise we can
666 * simply drop the ogm.
668 if (is_single_hop_neigh)
669 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
674 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
676 batadv_ogm_packet->ttl--;
677 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
679 /* apply hop penalty */
680 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
683 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
684 "Forwarding packet: tq: %i, ttl: %i\n",
685 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
687 if (is_single_hop_neigh)
688 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
690 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
692 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
693 BATADV_OGM_HLEN + tvlv_len,
694 if_incoming, if_outgoing, 0,
695 batadv_iv_ogm_fwd_send_time());
699 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
700 * for the given interface
701 * @hard_iface: the interface for which the windows have to be shifted
704 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
706 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
707 struct batadv_hashtable *hash = bat_priv->orig_hash;
708 struct hlist_head *head;
709 struct batadv_orig_node *orig_node;
710 struct batadv_orig_ifinfo *orig_ifinfo;
715 for (i = 0; i < hash->size; i++) {
716 head = &hash->table[i];
719 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
720 hlist_for_each_entry_rcu(orig_ifinfo,
721 &orig_node->ifinfo_list,
723 if (orig_ifinfo->if_outgoing != hard_iface)
726 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
727 word = orig_ifinfo->bat_iv.bcast_own;
728 batadv_bit_get_packet(bat_priv, word, 1, 0);
729 w = &orig_ifinfo->bat_iv.bcast_own_sum;
730 *w = bitmap_weight(word,
731 BATADV_TQ_LOCAL_WINDOW_SIZE);
732 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
739 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
741 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
742 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
743 struct batadv_ogm_packet *batadv_ogm_packet;
744 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
745 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
748 unsigned long send_time;
750 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
751 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
754 /* the interface gets activated here to avoid race conditions between
755 * the moment of activating the interface in
756 * hardif_activate_interface() where the originator mac is set and
757 * outdated packets (especially uninitialized mac addresses) in the
760 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
761 hard_iface->if_status = BATADV_IF_ACTIVE;
763 primary_if = batadv_primary_if_get_selected(bat_priv);
765 if (hard_iface == primary_if) {
766 /* tt changes have to be committed before the tvlv data is
767 * appended as it may alter the tt tvlv container
769 batadv_tt_local_commit_changes(bat_priv);
770 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
775 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
776 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
778 /* change sequence number to network order */
779 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
780 batadv_ogm_packet->seqno = htonl(seqno);
781 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
783 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
785 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
787 if (hard_iface != primary_if) {
788 /* OGMs from secondary interfaces are only scheduled on their
789 * respective interfaces.
791 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
792 hard_iface, hard_iface, 1, send_time);
796 /* OGMs from primary interfaces are scheduled on all
800 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
801 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
804 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
807 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
808 *ogm_buff_len, hard_iface,
809 tmp_hard_iface, 1, send_time);
811 batadv_hardif_put(tmp_hard_iface);
817 batadv_hardif_put(primary_if);
821 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over iterface
822 * @orig_node: originator which reproadcasted the OGMs directly
823 * @if_outgoing: interface which transmitted the original OGM and received the
826 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
827 * an originator and directly (without intermediate hop) received by a specific
830 static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
831 struct batadv_hard_iface *if_outgoing)
833 struct batadv_orig_ifinfo *orig_ifinfo;
836 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
840 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
841 sum = orig_ifinfo->bat_iv.bcast_own_sum;
842 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
844 batadv_orig_ifinfo_put(orig_ifinfo);
850 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
852 * @bat_priv: the bat priv with all the soft interface information
853 * @orig_node: the orig node who originally emitted the ogm packet
854 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
855 * @ethhdr: Ethernet header of the OGM
856 * @batadv_ogm_packet: the ogm packet
857 * @if_incoming: interface where the packet was received
858 * @if_outgoing: interface for which the retransmission should be considered
859 * @dup_status: the duplicate status of this ogm packet.
862 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
863 struct batadv_orig_node *orig_node,
864 struct batadv_orig_ifinfo *orig_ifinfo,
865 const struct ethhdr *ethhdr,
866 const struct batadv_ogm_packet *batadv_ogm_packet,
867 struct batadv_hard_iface *if_incoming,
868 struct batadv_hard_iface *if_outgoing,
869 enum batadv_dup_status dup_status)
871 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
872 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
873 struct batadv_neigh_node *neigh_node = NULL;
874 struct batadv_neigh_node *tmp_neigh_node = NULL;
875 struct batadv_neigh_node *router = NULL;
876 u8 sum_orig, sum_neigh;
880 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
881 "%s(): Searching and updating originator entry of received packet\n",
885 hlist_for_each_entry_rcu(tmp_neigh_node,
886 &orig_node->neigh_list, list) {
887 neigh_addr = tmp_neigh_node->addr;
888 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
889 tmp_neigh_node->if_incoming == if_incoming &&
890 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
891 if (WARN(neigh_node, "too many matching neigh_nodes"))
892 batadv_neigh_node_put(neigh_node);
893 neigh_node = tmp_neigh_node;
897 if (dup_status != BATADV_NO_DUP)
900 /* only update the entry for this outgoing interface */
901 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
906 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
907 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
908 &neigh_ifinfo->bat_iv.tq_index, 0);
909 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
910 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
911 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
913 batadv_neigh_ifinfo_put(neigh_ifinfo);
918 struct batadv_orig_node *orig_tmp;
920 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
924 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
926 orig_node, orig_tmp);
928 batadv_orig_node_put(orig_tmp);
932 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
933 "Updating existing last-hop neighbor of originator\n");
937 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
941 neigh_node->last_seen = jiffies;
943 spin_lock_bh(&neigh_node->ifinfo_lock);
944 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
945 &neigh_ifinfo->bat_iv.tq_index,
946 batadv_ogm_packet->tq);
947 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
948 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
949 spin_unlock_bh(&neigh_node->ifinfo_lock);
951 if (dup_status == BATADV_NO_DUP) {
952 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
953 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
956 /* if this neighbor already is our next hop there is nothing
959 router = batadv_orig_router_get(orig_node, if_outgoing);
960 if (router == neigh_node)
964 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
968 /* if this neighbor does not offer a better TQ we won't
971 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
975 /* if the TQ is the same and the link not more symmetric we
976 * won't consider it either
979 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
980 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
981 router->if_incoming);
982 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
983 neigh_node->if_incoming);
984 if (sum_orig >= sum_neigh)
988 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
995 batadv_neigh_node_put(neigh_node);
997 batadv_neigh_node_put(router);
999 batadv_neigh_ifinfo_put(neigh_ifinfo);
1001 batadv_neigh_ifinfo_put(router_ifinfo);
1005 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1006 * @orig_node: the orig node who originally emitted the ogm packet
1007 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1008 * @batadv_ogm_packet: the ogm packet
1009 * @if_incoming: interface where the packet was received
1010 * @if_outgoing: interface for which the retransmission should be considered
1012 * Return: true if the link can be considered bidirectional, false otherwise
1014 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1015 struct batadv_orig_node *orig_neigh_node,
1016 struct batadv_ogm_packet *batadv_ogm_packet,
1017 struct batadv_hard_iface *if_incoming,
1018 struct batadv_hard_iface *if_outgoing)
1020 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1021 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1022 struct batadv_neigh_ifinfo *neigh_ifinfo;
1024 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1025 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1026 unsigned int tq_asym_penalty, inv_asym_penalty;
1027 unsigned int combined_tq;
1028 unsigned int tq_iface_penalty;
1031 /* find corresponding one hop neighbor */
1033 hlist_for_each_entry_rcu(tmp_neigh_node,
1034 &orig_neigh_node->neigh_list, list) {
1035 if (!batadv_compare_eth(tmp_neigh_node->addr,
1036 orig_neigh_node->orig))
1039 if (tmp_neigh_node->if_incoming != if_incoming)
1042 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1045 neigh_node = tmp_neigh_node;
1051 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1052 orig_neigh_node->orig,
1059 /* if orig_node is direct neighbor update neigh_node last_seen */
1060 if (orig_node == orig_neigh_node)
1061 neigh_node->last_seen = jiffies;
1063 orig_node->last_seen = jiffies;
1065 /* find packet count of corresponding one hop neighbor */
1066 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
1067 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1069 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1070 batadv_neigh_ifinfo_put(neigh_ifinfo);
1075 /* pay attention to not get a value bigger than 100 % */
1076 if (orig_eq_count > neigh_rq_count)
1077 total_count = neigh_rq_count;
1079 total_count = orig_eq_count;
1081 /* if we have too few packets (too less data) we set tq_own to zero
1082 * if we receive too few packets it is not considered bidirectional
1084 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1085 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1088 /* neigh_node->real_packet_count is never zero as we
1089 * only purge old information when getting new
1092 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1094 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1095 * affect the nearly-symmetric links only a little, but
1096 * punishes asymmetric links more. This will give a value
1097 * between 0 and TQ_MAX_VALUE
1099 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1100 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1101 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1102 BATADV_TQ_LOCAL_WINDOW_SIZE *
1103 BATADV_TQ_LOCAL_WINDOW_SIZE;
1104 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1105 inv_asym_penalty /= neigh_rq_max_cube;
1106 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1108 /* penalize if the OGM is forwarded on the same interface. WiFi
1109 * interfaces and other half duplex devices suffer from throughput
1110 * drops as they can't send and receive at the same time.
1112 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1113 if (if_outgoing && if_incoming == if_outgoing &&
1114 batadv_is_wifi_hardif(if_outgoing))
1115 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1118 combined_tq = batadv_ogm_packet->tq *
1122 combined_tq /= BATADV_TQ_MAX_VALUE *
1123 BATADV_TQ_MAX_VALUE *
1124 BATADV_TQ_MAX_VALUE;
1125 batadv_ogm_packet->tq = combined_tq;
1127 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1128 "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",
1129 orig_node->orig, orig_neigh_node->orig, total_count,
1130 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1131 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1132 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1134 /* if link has the minimum required transmission quality
1135 * consider it bidirectional
1137 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1142 batadv_neigh_node_put(neigh_node);
1147 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
1148 * adjust the sequence number and find out whether it is a duplicate
1149 * @ethhdr: ethernet header of the packet
1150 * @batadv_ogm_packet: OGM packet to be considered
1151 * @if_incoming: interface on which the OGM packet was received
1152 * @if_outgoing: interface for which the retransmission should be considered
1154 * Return: duplicate status as enum batadv_dup_status
1156 static enum batadv_dup_status
1157 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1158 const struct batadv_ogm_packet *batadv_ogm_packet,
1159 const struct batadv_hard_iface *if_incoming,
1160 struct batadv_hard_iface *if_outgoing)
1162 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1163 struct batadv_orig_node *orig_node;
1164 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1165 struct batadv_neigh_node *neigh_node;
1166 struct batadv_neigh_ifinfo *neigh_ifinfo;
1169 bool need_update = false;
1171 enum batadv_dup_status ret = BATADV_NO_DUP;
1172 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1175 unsigned long *bitmap;
1177 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1179 return BATADV_NO_DUP;
1181 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1182 if (WARN_ON(!orig_ifinfo)) {
1183 batadv_orig_node_put(orig_node);
1187 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1188 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1190 /* signalize caller that the packet is to be dropped. */
1191 if (!hlist_empty(&orig_node->neigh_list) &&
1192 batadv_window_protected(bat_priv, seq_diff,
1193 BATADV_TQ_LOCAL_WINDOW_SIZE,
1194 &orig_ifinfo->batman_seqno_reset, NULL)) {
1195 ret = BATADV_PROTECTED;
1200 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1201 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1206 neigh_addr = neigh_node->addr;
1207 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1208 orig_ifinfo->last_real_seqno,
1211 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1212 neigh_node->if_incoming == if_incoming) {
1215 ret = BATADV_NEIGH_DUP;
1218 if (is_dup && ret != BATADV_NEIGH_DUP)
1219 ret = BATADV_ORIG_DUP;
1222 /* if the window moved, set the update flag. */
1223 bitmap = neigh_ifinfo->bat_iv.real_bits;
1224 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1225 seq_diff, set_mark);
1227 packet_count = bitmap_weight(bitmap,
1228 BATADV_TQ_LOCAL_WINDOW_SIZE);
1229 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1230 batadv_neigh_ifinfo_put(neigh_ifinfo);
1235 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1236 "%s updating last_seqno: old %u, new %u\n",
1237 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1238 orig_ifinfo->last_real_seqno, seqno);
1239 orig_ifinfo->last_real_seqno = seqno;
1243 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1244 batadv_orig_node_put(orig_node);
1245 batadv_orig_ifinfo_put(orig_ifinfo);
1250 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1252 * @skb: the skb containing the OGM
1253 * @ogm_offset: offset from skb->data to start of ogm header
1254 * @orig_node: the (cached) orig node for the originator of this OGM
1255 * @if_incoming: the interface where this packet was received
1256 * @if_outgoing: the interface for which the packet should be considered
1259 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1260 struct batadv_orig_node *orig_node,
1261 struct batadv_hard_iface *if_incoming,
1262 struct batadv_hard_iface *if_outgoing)
1264 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1265 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1266 struct batadv_neigh_node *router = NULL;
1267 struct batadv_neigh_node *router_router = NULL;
1268 struct batadv_orig_node *orig_neigh_node;
1269 struct batadv_orig_ifinfo *orig_ifinfo;
1270 struct batadv_neigh_node *orig_neigh_router = NULL;
1271 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1272 struct batadv_ogm_packet *ogm_packet;
1273 enum batadv_dup_status dup_status;
1274 bool is_from_best_next_hop = false;
1275 bool is_single_hop_neigh = false;
1276 bool sameseq, similar_ttl;
1277 struct sk_buff *skb_priv;
1278 struct ethhdr *ethhdr;
1282 /* create a private copy of the skb, as some functions change tq value
1285 skb_priv = skb_copy(skb, GFP_ATOMIC);
1289 ethhdr = eth_hdr(skb_priv);
1290 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1292 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1293 if_incoming, if_outgoing);
1294 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1295 is_single_hop_neigh = true;
1297 if (dup_status == BATADV_PROTECTED) {
1298 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1299 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1304 if (ogm_packet->tq == 0) {
1305 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1306 "Drop packet: originator packet with tq equal 0\n");
1310 if (is_single_hop_neigh) {
1311 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1314 hardif_neigh->last_seen = jiffies;
1317 router = batadv_orig_router_get(orig_node, if_outgoing);
1319 router_router = batadv_orig_router_get(router->orig_node,
1321 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1324 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1325 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1326 is_from_best_next_hop = true;
1328 prev_sender = ogm_packet->prev_sender;
1329 /* avoid temporary routing loops */
1330 if (router && router_router &&
1331 (batadv_compare_eth(router->addr, prev_sender)) &&
1332 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1333 (batadv_compare_eth(router->addr, router_router->addr))) {
1334 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1335 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1340 if (if_outgoing == BATADV_IF_DEFAULT)
1341 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1343 /* if sender is a direct neighbor the sender mac equals
1346 if (is_single_hop_neigh)
1347 orig_neigh_node = orig_node;
1349 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1352 if (!orig_neigh_node)
1355 /* Update nc_nodes of the originator */
1356 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1357 ogm_packet, is_single_hop_neigh);
1359 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1362 /* drop packet if sender is not a direct neighbor and if we
1363 * don't route towards it
1365 if (!is_single_hop_neigh && !orig_neigh_router) {
1366 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1367 "Drop packet: OGM via unknown neighbor!\n");
1371 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1372 ogm_packet, if_incoming,
1375 /* update ranking if it is not a duplicate or has the same
1376 * seqno and similar ttl as the non-duplicate
1378 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1382 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1383 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1385 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1386 (sameseq && similar_ttl))) {
1387 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1388 orig_ifinfo, ethhdr,
1389 ogm_packet, if_incoming,
1390 if_outgoing, dup_status);
1392 batadv_orig_ifinfo_put(orig_ifinfo);
1394 /* only forward for specific interface, not for the default one. */
1395 if (if_outgoing == BATADV_IF_DEFAULT)
1398 /* is single hop (direct) neighbor */
1399 if (is_single_hop_neigh) {
1400 /* OGMs from secondary interfaces should only scheduled once
1401 * per interface where it has been received, not multiple times
1403 if (ogm_packet->ttl <= 2 &&
1404 if_incoming != if_outgoing) {
1405 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1406 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1409 /* mark direct link on incoming interface */
1410 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1411 is_single_hop_neigh,
1412 is_from_best_next_hop, if_incoming,
1415 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1416 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1420 /* multihop originator */
1422 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1423 "Drop packet: not received via bidirectional link\n");
1427 if (dup_status == BATADV_NEIGH_DUP) {
1428 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1429 "Drop packet: duplicate packet received\n");
1433 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1434 "Forwarding packet: rebroadcast originator packet\n");
1435 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1436 is_single_hop_neigh, is_from_best_next_hop,
1437 if_incoming, if_outgoing);
1440 if (orig_neigh_node && !is_single_hop_neigh)
1441 batadv_orig_node_put(orig_neigh_node);
1444 batadv_neigh_ifinfo_put(router_ifinfo);
1446 batadv_neigh_node_put(router);
1448 batadv_neigh_node_put(router_router);
1449 if (orig_neigh_router)
1450 batadv_neigh_node_put(orig_neigh_router);
1452 batadv_hardif_neigh_put(hardif_neigh);
1454 consume_skb(skb_priv);
1458 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1459 * @ogm_packet: rebroadcast OGM packet to process
1460 * @if_incoming: the interface where this packet was received
1461 * @orig_node: originator which reproadcasted the OGMs
1462 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1464 static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1465 struct batadv_hard_iface *if_incoming,
1466 struct batadv_orig_node *orig_node,
1467 u32 if_incoming_seqno)
1469 struct batadv_orig_ifinfo *orig_ifinfo;
1473 /* neighbor has to indicate direct link and it has to
1474 * come via the corresponding interface
1476 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1479 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1483 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1487 /* save packet seqno for bidirectional check */
1488 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1489 bit_pos = if_incoming_seqno - 2;
1490 bit_pos -= ntohl(ogm_packet->seqno);
1491 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1492 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1493 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1494 BATADV_TQ_LOCAL_WINDOW_SIZE);
1495 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1497 batadv_orig_ifinfo_put(orig_ifinfo);
1501 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1502 * @skb: the skb containing the OGM
1503 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1504 * @if_incoming: the interface where this packet was receved
1506 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1507 struct batadv_hard_iface *if_incoming)
1509 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1510 struct batadv_orig_node *orig_neigh_node, *orig_node;
1511 struct batadv_hard_iface *hard_iface;
1512 struct batadv_ogm_packet *ogm_packet;
1513 u32 if_incoming_seqno;
1514 bool has_directlink_flag;
1515 struct ethhdr *ethhdr;
1516 bool is_my_oldorig = false;
1517 bool is_my_addr = false;
1518 bool is_my_orig = false;
1520 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1521 ethhdr = eth_hdr(skb);
1523 /* Silently drop when the batman packet is actually not a
1526 * This might happen if a packet is padded (e.g. Ethernet has a
1527 * minimum frame length of 64 byte) and the aggregation interprets
1528 * it as an additional length.
1530 * TODO: A more sane solution would be to have a bit in the
1531 * batadv_ogm_packet to detect whether the packet is the last
1532 * packet in an aggregation. Here we expect that the padding
1533 * is always zero (or not 0x01)
1535 if (ogm_packet->packet_type != BATADV_IV_OGM)
1538 /* could be changed by schedule_own_packet() */
1539 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1541 if (ogm_packet->flags & BATADV_DIRECTLINK)
1542 has_directlink_flag = true;
1544 has_directlink_flag = false;
1546 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1547 "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",
1548 ethhdr->h_source, if_incoming->net_dev->name,
1549 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1550 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1551 ogm_packet->tq, ogm_packet->ttl,
1552 ogm_packet->version, has_directlink_flag);
1555 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1556 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1559 if (hard_iface->soft_iface != if_incoming->soft_iface)
1562 if (batadv_compare_eth(ethhdr->h_source,
1563 hard_iface->net_dev->dev_addr))
1566 if (batadv_compare_eth(ogm_packet->orig,
1567 hard_iface->net_dev->dev_addr))
1570 if (batadv_compare_eth(ogm_packet->prev_sender,
1571 hard_iface->net_dev->dev_addr))
1572 is_my_oldorig = true;
1577 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1578 "Drop packet: received my own broadcast (sender: %pM)\n",
1584 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1586 if (!orig_neigh_node)
1589 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1590 orig_neigh_node, if_incoming_seqno);
1592 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1593 "Drop packet: originator packet from myself (via neighbor)\n");
1594 batadv_orig_node_put(orig_neigh_node);
1598 if (is_my_oldorig) {
1599 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1600 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1605 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1606 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1607 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1612 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1616 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1617 if_incoming, BATADV_IF_DEFAULT);
1620 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1621 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1624 if (hard_iface->soft_iface != bat_priv->soft_iface)
1627 if (!kref_get_unless_zero(&hard_iface->refcount))
1630 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1631 if_incoming, hard_iface);
1633 batadv_hardif_put(hard_iface);
1637 batadv_orig_node_put(orig_node);
1640 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1642 struct delayed_work *delayed_work;
1643 struct batadv_forw_packet *forw_packet;
1644 struct batadv_priv *bat_priv;
1645 bool dropped = false;
1647 delayed_work = to_delayed_work(work);
1648 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1650 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1652 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1657 batadv_iv_ogm_emit(forw_packet);
1659 /* we have to have at least one packet in the queue to determine the
1660 * queues wake up time unless we are shutting down.
1662 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1663 * primary interface should only be rescheduled once per period, but
1664 * this function will be called for the forw_packet instances of the
1665 * other secondary interfaces as well.
1667 if (forw_packet->own &&
1668 forw_packet->if_incoming == forw_packet->if_outgoing)
1669 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1672 /* do we get something for free()? */
1673 if (batadv_forw_packet_steal(forw_packet,
1674 &bat_priv->forw_bat_list_lock))
1675 batadv_forw_packet_free(forw_packet, dropped);
1678 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1679 struct batadv_hard_iface *if_incoming)
1681 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1682 struct batadv_ogm_packet *ogm_packet;
1686 int ret = NET_RX_DROP;
1688 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1692 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1693 * that does not have B.A.T.M.A.N. IV enabled ?
1695 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1698 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1699 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1700 skb->len + ETH_HLEN);
1703 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1705 /* unpack the aggregated packets and process them one by one */
1706 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1707 ogm_packet->tvlv_len)) {
1708 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1710 ogm_offset += BATADV_OGM_HLEN;
1711 ogm_offset += ntohs(ogm_packet->tvlv_len);
1713 packet_pos = skb->data + ogm_offset;
1714 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1717 ret = NET_RX_SUCCESS;
1720 if (ret == NET_RX_SUCCESS)
1728 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1730 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
1731 * @orig_node: the orig_node for which the neighbors are printed
1732 * @if_outgoing: outgoing interface for these entries
1733 * @seq: debugfs table seq_file struct
1735 * Must be called while holding an rcu lock.
1738 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1739 struct batadv_hard_iface *if_outgoing,
1740 struct seq_file *seq)
1742 struct batadv_neigh_node *neigh_node;
1743 struct batadv_neigh_ifinfo *n_ifinfo;
1745 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1746 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1750 seq_printf(seq, " %pM (%3i)",
1752 n_ifinfo->bat_iv.tq_avg);
1754 batadv_neigh_ifinfo_put(n_ifinfo);
1759 * batadv_iv_ogm_orig_print() - print the originator table
1760 * @bat_priv: the bat priv with all the soft interface information
1761 * @seq: debugfs table seq_file struct
1762 * @if_outgoing: the outgoing interface for which this should be printed
1764 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1765 struct seq_file *seq,
1766 struct batadv_hard_iface *if_outgoing)
1768 struct batadv_neigh_node *neigh_node;
1769 struct batadv_hashtable *hash = bat_priv->orig_hash;
1770 int last_seen_msecs, last_seen_secs;
1771 struct batadv_orig_node *orig_node;
1772 struct batadv_neigh_ifinfo *n_ifinfo;
1773 unsigned long last_seen_jiffies;
1774 struct hlist_head *head;
1775 int batman_count = 0;
1779 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1781 for (i = 0; i < hash->size; i++) {
1782 head = &hash->table[i];
1785 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1786 neigh_node = batadv_orig_router_get(orig_node,
1791 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1796 if (n_ifinfo->bat_iv.tq_avg == 0)
1799 last_seen_jiffies = jiffies - orig_node->last_seen;
1800 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1801 last_seen_secs = last_seen_msecs / 1000;
1802 last_seen_msecs = last_seen_msecs % 1000;
1804 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1805 orig_node->orig, last_seen_secs,
1806 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1808 neigh_node->if_incoming->net_dev->name);
1810 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1812 seq_putc(seq, '\n');
1816 batadv_neigh_node_put(neigh_node);
1818 batadv_neigh_ifinfo_put(n_ifinfo);
1823 if (batman_count == 0)
1824 seq_puts(seq, "No batman nodes in range ...\n");
1829 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1830 * given outgoing interface.
1831 * @neigh_node: Neighbour of interest
1832 * @if_outgoing: Outgoing interface of interest
1833 * @tq_avg: Pointer of where to store the TQ average
1835 * Return: False if no average TQ available, otherwise true.
1838 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1839 struct batadv_hard_iface *if_outgoing,
1842 struct batadv_neigh_ifinfo *n_ifinfo;
1844 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1848 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1849 batadv_neigh_ifinfo_put(n_ifinfo);
1855 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1857 * @msg: Netlink message to dump into
1858 * @portid: Port making netlink request
1859 * @seq: Sequence number of netlink message
1860 * @bat_priv: The bat priv with all the soft interface information
1861 * @if_outgoing: Limit dump to entries with this outgoing interface
1862 * @orig_node: Originator to dump
1863 * @neigh_node: Single hops neighbour
1864 * @best: Is the best originator
1866 * Return: Error code, or 0 on success
1869 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1870 struct batadv_priv *bat_priv,
1871 struct batadv_hard_iface *if_outgoing,
1872 struct batadv_orig_node *orig_node,
1873 struct batadv_neigh_node *neigh_node,
1878 unsigned int last_seen_msecs;
1880 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1882 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1885 if (if_outgoing != BATADV_IF_DEFAULT &&
1886 if_outgoing != neigh_node->if_incoming)
1889 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1890 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1894 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1896 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1897 neigh_node->addr) ||
1898 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1899 neigh_node->if_incoming->net_dev->ifindex) ||
1900 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1901 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1903 goto nla_put_failure;
1905 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1906 goto nla_put_failure;
1908 genlmsg_end(msg, hdr);
1912 genlmsg_cancel(msg, hdr);
1917 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
1918 * @msg: Netlink message to dump into
1919 * @portid: Port making netlink request
1920 * @seq: Sequence number of netlink message
1921 * @bat_priv: The bat priv with all the soft interface information
1922 * @if_outgoing: Limit dump to entries with this outgoing interface
1923 * @orig_node: Originator to dump
1924 * @sub_s: Number of sub entries to skip
1926 * This function assumes the caller holds rcu_read_lock().
1928 * Return: Error code, or 0 on success
1931 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1932 struct batadv_priv *bat_priv,
1933 struct batadv_hard_iface *if_outgoing,
1934 struct batadv_orig_node *orig_node, int *sub_s)
1936 struct batadv_neigh_node *neigh_node_best;
1937 struct batadv_neigh_node *neigh_node;
1942 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1943 if (!neigh_node_best)
1946 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1950 if (tq_avg_best == 0)
1953 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1957 best = (neigh_node == neigh_node_best);
1959 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1960 bat_priv, if_outgoing,
1961 orig_node, neigh_node,
1963 batadv_neigh_node_put(neigh_node_best);
1971 if (neigh_node_best)
1972 batadv_neigh_node_put(neigh_node_best);
1979 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
1981 * @msg: Netlink message to dump into
1982 * @portid: Port making netlink request
1983 * @seq: Sequence number of netlink message
1984 * @bat_priv: The bat priv with all the soft interface information
1985 * @if_outgoing: Limit dump to entries with this outgoing interface
1986 * @head: Bucket to be dumped
1987 * @idx_s: Number of entries to be skipped
1988 * @sub: Number of sub entries to be skipped
1990 * Return: Error code, or 0 on success
1993 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1994 struct batadv_priv *bat_priv,
1995 struct batadv_hard_iface *if_outgoing,
1996 struct hlist_head *head, int *idx_s, int *sub)
1998 struct batadv_orig_node *orig_node;
2002 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2006 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2007 if_outgoing, orig_node,
2022 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2023 * @msg: Netlink message to dump into
2024 * @cb: Control block containing additional options
2025 * @bat_priv: The bat priv with all the soft interface information
2026 * @if_outgoing: Limit dump to entries with this outgoing interface
2029 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2030 struct batadv_priv *bat_priv,
2031 struct batadv_hard_iface *if_outgoing)
2033 struct batadv_hashtable *hash = bat_priv->orig_hash;
2034 struct hlist_head *head;
2035 int bucket = cb->args[0];
2036 int idx = cb->args[1];
2037 int sub = cb->args[2];
2038 int portid = NETLINK_CB(cb->skb).portid;
2040 while (bucket < hash->size) {
2041 head = &hash->table[bucket];
2043 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2045 bat_priv, if_outgoing, head,
2052 cb->args[0] = bucket;
2057 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2059 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
2060 * @seq: neighbour table seq_file struct
2061 * @hardif_neigh: hardif neighbour information
2064 batadv_iv_hardif_neigh_print(struct seq_file *seq,
2065 struct batadv_hardif_neigh_node *hardif_neigh)
2067 int last_secs, last_msecs;
2069 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2070 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2072 seq_printf(seq, " %10s %pM %4i.%03is\n",
2073 hardif_neigh->if_incoming->net_dev->name,
2074 hardif_neigh->addr, last_secs, last_msecs);
2078 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
2079 * @bat_priv: the bat priv with all the soft interface information
2080 * @seq: neighbour table seq_file struct
2082 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2083 struct seq_file *seq)
2085 struct net_device *net_dev = (struct net_device *)seq->private;
2086 struct batadv_hardif_neigh_node *hardif_neigh;
2087 struct batadv_hard_iface *hard_iface;
2088 int batman_count = 0;
2090 seq_puts(seq, " IF Neighbor last-seen\n");
2093 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2094 if (hard_iface->soft_iface != net_dev)
2097 hlist_for_each_entry_rcu(hardif_neigh,
2098 &hard_iface->neigh_list, list) {
2099 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2105 if (batman_count == 0)
2106 seq_puts(seq, "No batman nodes in range ...\n");
2111 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2112 * @neigh1: the first neighbor object of the comparison
2113 * @if_outgoing1: outgoing interface for the first neighbor
2114 * @neigh2: the second neighbor object of the comparison
2115 * @if_outgoing2: outgoing interface for the second neighbor
2116 * @diff: pointer to integer receiving the calculated difference
2118 * The content of *@diff is only valid when this function returns true.
2119 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2120 * the same as or higher than the metric via neigh2
2122 * Return: true when the difference could be calculated, false otherwise
2124 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2125 struct batadv_hard_iface *if_outgoing1,
2126 struct batadv_neigh_node *neigh2,
2127 struct batadv_hard_iface *if_outgoing2,
2130 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2134 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2135 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2137 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2142 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2143 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2144 *diff = (int)tq1 - (int)tq2;
2148 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2150 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2156 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2157 * @msg: Netlink message to dump into
2158 * @portid: Port making netlink request
2159 * @seq: Sequence number of netlink message
2160 * @hardif_neigh: Neighbour to be dumped
2162 * Return: Error code, or 0 on success
2165 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2166 struct batadv_hardif_neigh_node *hardif_neigh)
2169 unsigned int last_seen_msecs;
2171 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2173 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2174 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2178 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2179 hardif_neigh->addr) ||
2180 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2181 hardif_neigh->if_incoming->net_dev->ifindex) ||
2182 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2184 goto nla_put_failure;
2186 genlmsg_end(msg, hdr);
2190 genlmsg_cancel(msg, hdr);
2195 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2197 * @msg: Netlink message to dump into
2198 * @portid: Port making netlink request
2199 * @seq: Sequence number of netlink message
2200 * @bat_priv: The bat priv with all the soft interface information
2201 * @hard_iface: Hard interface to dump the neighbours for
2202 * @idx_s: Number of entries to skip
2204 * This function assumes the caller holds rcu_read_lock().
2206 * Return: Error code, or 0 on success
2209 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2210 struct batadv_priv *bat_priv,
2211 struct batadv_hard_iface *hard_iface,
2214 struct batadv_hardif_neigh_node *hardif_neigh;
2217 hlist_for_each_entry_rcu(hardif_neigh,
2218 &hard_iface->neigh_list, list) {
2222 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2234 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2235 * @msg: Netlink message to dump into
2236 * @cb: Control block containing additional options
2237 * @bat_priv: The bat priv with all the soft interface information
2238 * @single_hardif: Limit dump to this hard interfaace
2241 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2242 struct batadv_priv *bat_priv,
2243 struct batadv_hard_iface *single_hardif)
2245 struct batadv_hard_iface *hard_iface;
2247 int i_hardif_s = cb->args[0];
2248 int idx = cb->args[1];
2249 int portid = NETLINK_CB(cb->skb).portid;
2252 if (single_hardif) {
2253 if (i_hardif_s == 0) {
2254 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2262 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2264 if (hard_iface->soft_iface != bat_priv->soft_iface)
2267 if (i_hardif++ < i_hardif_s)
2270 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2273 hard_iface, &idx)) {
2281 cb->args[0] = i_hardif;
2286 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2287 * @neigh1: the first neighbor object of the comparison
2288 * @if_outgoing1: outgoing interface for the first neighbor
2289 * @neigh2: the second neighbor object of the comparison
2290 * @if_outgoing2: outgoing interface for the second neighbor
2292 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2293 * lower, the same as or higher than the metric via neigh2
2295 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2296 struct batadv_hard_iface *if_outgoing1,
2297 struct batadv_neigh_node *neigh2,
2298 struct batadv_hard_iface *if_outgoing2)
2303 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2304 if_outgoing2, &diff);
2312 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2313 * than neigh2 from the metric prospective
2314 * @neigh1: the first neighbor object of the comparison
2315 * @if_outgoing1: outgoing interface for the first neighbor
2316 * @neigh2: the second neighbor object of the comparison
2317 * @if_outgoing2: outgoing interface for the second neighbor
2319 * Return: true if the metric via neigh1 is equally good or better than
2320 * the metric via neigh2, false otherwise.
2323 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2324 struct batadv_hard_iface *if_outgoing1,
2325 struct batadv_neigh_node *neigh2,
2326 struct batadv_hard_iface *if_outgoing2)
2331 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2332 if_outgoing2, &diff);
2336 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2340 static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
2342 /* begin scheduling originator messages on that interface */
2343 batadv_iv_ogm_schedule(hard_iface);
2347 * batadv_iv_init_sel_class() - initialize GW selection class
2348 * @bat_priv: the bat priv with all the soft interface information
2350 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2352 /* set default TQ difference threshold to 20 */
2353 atomic_set(&bat_priv->gw.sel_class, 20);
2356 static struct batadv_gw_node *
2357 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2359 struct batadv_neigh_node *router;
2360 struct batadv_neigh_ifinfo *router_ifinfo;
2361 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2362 u64 max_gw_factor = 0;
2363 u64 tmp_gw_factor = 0;
2366 struct batadv_orig_node *orig_node;
2369 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2370 orig_node = gw_node->orig_node;
2371 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2375 router_ifinfo = batadv_neigh_ifinfo_get(router,
2380 if (!kref_get_unless_zero(&gw_node->refcount))
2383 tq_avg = router_ifinfo->bat_iv.tq_avg;
2385 switch (atomic_read(&bat_priv->gw.sel_class)) {
2386 case 1: /* fast connection */
2387 tmp_gw_factor = tq_avg * tq_avg;
2388 tmp_gw_factor *= gw_node->bandwidth_down;
2389 tmp_gw_factor *= 100 * 100;
2390 tmp_gw_factor >>= 18;
2392 if (tmp_gw_factor > max_gw_factor ||
2393 (tmp_gw_factor == max_gw_factor &&
2396 batadv_gw_node_put(curr_gw);
2398 kref_get(&curr_gw->refcount);
2402 default: /* 2: stable connection (use best statistic)
2403 * 3: fast-switch (use best statistic but change as
2404 * soon as a better gateway appears)
2405 * XX: late-switch (use best statistic but change as
2406 * soon as a better gateway appears which has
2407 * $routing_class more tq points)
2409 if (tq_avg > max_tq) {
2411 batadv_gw_node_put(curr_gw);
2413 kref_get(&curr_gw->refcount);
2418 if (tq_avg > max_tq)
2421 if (tmp_gw_factor > max_gw_factor)
2422 max_gw_factor = tmp_gw_factor;
2424 batadv_gw_node_put(gw_node);
2427 batadv_neigh_node_put(router);
2429 batadv_neigh_ifinfo_put(router_ifinfo);
2436 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2437 struct batadv_orig_node *curr_gw_orig,
2438 struct batadv_orig_node *orig_node)
2440 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2441 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2442 struct batadv_neigh_node *router_gw = NULL;
2443 struct batadv_neigh_node *router_orig = NULL;
2444 u8 gw_tq_avg, orig_tq_avg;
2447 /* dynamic re-election is performed only on fast or late switch */
2448 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2451 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2457 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2459 if (!router_gw_ifinfo) {
2464 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2468 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2470 if (!router_orig_ifinfo)
2473 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2474 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2476 /* the TQ value has to be better */
2477 if (orig_tq_avg < gw_tq_avg)
2480 /* if the routing class is greater than 3 the value tells us how much
2481 * greater the TQ value of the new gateway must be
2483 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2484 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2487 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2488 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2489 gw_tq_avg, orig_tq_avg);
2493 if (router_gw_ifinfo)
2494 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2495 if (router_orig_ifinfo)
2496 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2498 batadv_neigh_node_put(router_gw);
2500 batadv_neigh_node_put(router_orig);
2505 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2506 /* fails if orig_node has no router */
2507 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2508 struct seq_file *seq,
2509 const struct batadv_gw_node *gw_node)
2511 struct batadv_gw_node *curr_gw;
2512 struct batadv_neigh_node *router;
2513 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2516 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2520 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2524 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2526 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2527 (curr_gw == gw_node ? "=>" : " "),
2528 gw_node->orig_node->orig,
2529 router_ifinfo->bat_iv.tq_avg, router->addr,
2530 router->if_incoming->net_dev->name,
2531 gw_node->bandwidth_down / 10,
2532 gw_node->bandwidth_down % 10,
2533 gw_node->bandwidth_up / 10,
2534 gw_node->bandwidth_up % 10);
2535 ret = seq_has_overflowed(seq) ? -1 : 0;
2538 batadv_gw_node_put(curr_gw);
2541 batadv_neigh_ifinfo_put(router_ifinfo);
2543 batadv_neigh_node_put(router);
2547 static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2548 struct seq_file *seq)
2550 struct batadv_gw_node *gw_node;
2554 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2557 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2558 /* fails if orig_node has no router */
2559 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2567 seq_puts(seq, "No gateways in range ...\n");
2572 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2573 * @msg: Netlink message to dump into
2574 * @portid: Port making netlink request
2575 * @cb: Control block containing additional options
2576 * @bat_priv: The bat priv with all the soft interface information
2577 * @gw_node: Gateway to be dumped
2579 * Return: Error code, or 0 on success
2581 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2582 struct netlink_callback *cb,
2583 struct batadv_priv *bat_priv,
2584 struct batadv_gw_node *gw_node)
2586 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2587 struct batadv_neigh_node *router;
2588 struct batadv_gw_node *curr_gw = NULL;
2592 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2596 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2600 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2602 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2603 &batadv_netlink_family, NLM_F_MULTI,
2604 BATADV_CMD_GET_GATEWAYS);
2610 genl_dump_check_consistent(cb, hdr);
2614 if (curr_gw == gw_node)
2615 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2616 genlmsg_cancel(msg, hdr);
2620 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2621 gw_node->orig_node->orig) ||
2622 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2623 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2625 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2626 router->if_incoming->net_dev->name) ||
2627 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2628 gw_node->bandwidth_down) ||
2629 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2630 gw_node->bandwidth_up)) {
2631 genlmsg_cancel(msg, hdr);
2635 genlmsg_end(msg, hdr);
2640 batadv_gw_node_put(curr_gw);
2642 batadv_neigh_ifinfo_put(router_ifinfo);
2644 batadv_neigh_node_put(router);
2649 * batadv_iv_gw_dump() - Dump gateways into a message
2650 * @msg: Netlink message to dump into
2651 * @cb: Control block containing additional options
2652 * @bat_priv: The bat priv with all the soft interface information
2654 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2655 struct batadv_priv *bat_priv)
2657 int portid = NETLINK_CB(cb->skb).portid;
2658 struct batadv_gw_node *gw_node;
2659 int idx_skip = cb->args[0];
2662 spin_lock_bh(&bat_priv->gw.list_lock);
2663 cb->seq = bat_priv->gw.generation << 1 | 1;
2665 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
2666 if (idx++ < idx_skip)
2669 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2678 spin_unlock_bh(&bat_priv->gw.list_lock);
2680 cb->args[0] = idx_skip;
2683 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2684 .name = "BATMAN_IV",
2686 .enable = batadv_iv_ogm_iface_enable,
2687 .enabled = batadv_iv_iface_enabled,
2688 .disable = batadv_iv_ogm_iface_disable,
2689 .update_mac = batadv_iv_ogm_iface_update_mac,
2690 .primary_set = batadv_iv_ogm_primary_iface_set,
2693 .cmp = batadv_iv_ogm_neigh_cmp,
2694 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2695 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2696 .print = batadv_iv_neigh_print,
2698 .dump = batadv_iv_ogm_neigh_dump,
2701 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2702 .print = batadv_iv_ogm_orig_print,
2704 .dump = batadv_iv_ogm_orig_dump,
2707 .init_sel_class = batadv_iv_init_sel_class,
2708 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2709 .is_eligible = batadv_iv_gw_is_eligible,
2710 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2711 .print = batadv_iv_gw_print,
2713 .dump = batadv_iv_gw_dump,
2718 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2720 * Return: 0 on success or negative error number in case of failure
2722 int __init batadv_iv_init(void)
2726 /* batman originator packet */
2727 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2728 batadv_iv_ogm_receive);
2732 ret = batadv_algo_register(&batadv_batman_iv);
2734 goto handler_unregister;
2739 batadv_recv_handler_unregister(BATADV_IV_OGM);