1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "bat_iv_ogm.h"
22 #include <linux/atomic.h>
23 #include <linux/bitmap.h>
24 #include <linux/bitops.h>
25 #include <linux/bug.h>
26 #include <linux/byteorder/generic.h>
27 #include <linux/cache.h>
28 #include <linux/errno.h>
29 #include <linux/etherdevice.h>
30 #include <linux/gfp.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/jiffies.h>
34 #include <linux/kernel.h>
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/lockdep.h>
38 #include <linux/netdevice.h>
39 #include <linux/netlink.h>
40 #include <linux/pkt_sched.h>
41 #include <linux/printk.h>
42 #include <linux/random.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/types.h>
52 #include <linux/workqueue.h>
53 #include <net/genetlink.h>
54 #include <net/netlink.h>
55 #include <uapi/linux/batman_adv.h>
59 #include "gateway_client.h"
60 #include "hard-interface.h"
64 #include "network-coding.h"
65 #include "originator.h"
69 #include "translation-table.h"
72 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
75 * enum batadv_dup_status - duplicate status
76 * @BATADV_NO_DUP: the packet is no duplicate
77 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
79 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
80 * @BATADV_PROTECTED: originator is currently protected (after reboot)
82 enum batadv_dup_status {
90 * batadv_ring_buffer_set() - update the ring buffer with the given value
91 * @lq_recv: pointer to the ring buffer
92 * @lq_index: index to store the value at
93 * @value: value to store in the ring buffer
95 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
97 lq_recv[*lq_index] = value;
98 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
102 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
103 * in the given ring buffer
104 * @lq_recv: pointer to the ring buffer
106 * Return: computed average value.
108 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
117 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
130 return (u8)(sum / count);
134 * batadv_iv_ogm_orig_free() - free the private resources allocated for this
136 * @orig_node: the orig_node for which the resources have to be free'd
138 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
140 kfree(orig_node->bat_iv.bcast_own);
141 kfree(orig_node->bat_iv.bcast_own_sum);
145 * batadv_iv_ogm_orig_add_if() - change the private structures of the orig_node
146 * to include the new hard-interface
147 * @orig_node: the orig_node that has to be changed
148 * @max_if_num: the current amount of interfaces
150 * Return: 0 on success, a negative error code otherwise.
152 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
159 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
161 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
162 data_ptr = kmalloc_array(max_if_num,
163 BATADV_NUM_WORDS * sizeof(unsigned long),
168 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
169 kfree(orig_node->bat_iv.bcast_own);
170 orig_node->bat_iv.bcast_own = data_ptr;
172 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
176 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
177 (max_if_num - 1) * sizeof(u8));
178 kfree(orig_node->bat_iv.bcast_own_sum);
179 orig_node->bat_iv.bcast_own_sum = data_ptr;
184 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
190 * batadv_iv_ogm_drop_bcast_own_entry() - drop section of bcast_own
191 * @orig_node: the orig_node that has to be changed
192 * @max_if_num: the current amount of interfaces
193 * @del_if_num: the index of the interface being removed
196 batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
197 int max_if_num, int del_if_num)
203 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
205 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
206 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
208 /* use old buffer when new one could not be allocated */
209 data_ptr = orig_node->bat_iv.bcast_own;
211 /* copy first part */
212 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
214 /* copy second part */
215 if_offset = (del_if_num + 1) * chunk_size;
216 memmove((char *)data_ptr + del_if_num * chunk_size,
217 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
218 (max_if_num - del_if_num) * chunk_size);
220 /* bcast_own was shrunk down in new buffer; free old one */
221 if (orig_node->bat_iv.bcast_own != data_ptr) {
222 kfree(orig_node->bat_iv.bcast_own);
223 orig_node->bat_iv.bcast_own = data_ptr;
228 * batadv_iv_ogm_drop_bcast_own_sum_entry() - drop section of bcast_own_sum
229 * @orig_node: the orig_node that has to be changed
230 * @max_if_num: the current amount of interfaces
231 * @del_if_num: the index of the interface being removed
234 batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
235 int max_if_num, int del_if_num)
240 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
242 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
244 /* use old buffer when new one could not be allocated */
245 data_ptr = orig_node->bat_iv.bcast_own_sum;
247 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
248 del_if_num * sizeof(u8));
250 if_offset = (del_if_num + 1) * sizeof(u8);
251 memmove((char *)data_ptr + del_if_num * sizeof(u8),
252 orig_node->bat_iv.bcast_own_sum + if_offset,
253 (max_if_num - del_if_num) * sizeof(u8));
255 /* bcast_own_sum was shrunk down in new buffer; free old one */
256 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
257 kfree(orig_node->bat_iv.bcast_own_sum);
258 orig_node->bat_iv.bcast_own_sum = data_ptr;
263 * batadv_iv_ogm_orig_del_if() - change the private structures of the orig_node
264 * to exclude the removed interface
265 * @orig_node: the orig_node that has to be changed
266 * @max_if_num: the current amount of interfaces
267 * @del_if_num: the index of the interface being removed
269 * Return: 0 on success, a negative error code otherwise.
271 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
272 int max_if_num, int del_if_num)
274 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
276 if (max_if_num == 0) {
277 kfree(orig_node->bat_iv.bcast_own);
278 kfree(orig_node->bat_iv.bcast_own_sum);
279 orig_node->bat_iv.bcast_own = NULL;
280 orig_node->bat_iv.bcast_own_sum = NULL;
282 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
284 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
288 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
294 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
296 * @bat_priv: the bat priv with all the soft interface information
297 * @addr: mac address of the originator
299 * Return: the originator object corresponding to the passed mac address or NULL
301 * If the object does not exists it is created an initialised.
303 static struct batadv_orig_node *
304 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
306 struct batadv_orig_node *orig_node;
307 int size, hash_added;
309 orig_node = batadv_orig_hash_find(bat_priv, addr);
313 orig_node = batadv_orig_node_new(bat_priv, addr);
317 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
319 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
320 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
321 if (!orig_node->bat_iv.bcast_own)
324 size = bat_priv->num_ifaces * sizeof(u8);
325 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
326 if (!orig_node->bat_iv.bcast_own_sum)
329 kref_get(&orig_node->refcount);
330 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
331 batadv_choose_orig, orig_node,
332 &orig_node->hash_entry);
334 goto free_orig_node_hash;
339 batadv_orig_node_put(orig_node);
341 batadv_orig_node_put(orig_node);
346 static struct batadv_neigh_node *
347 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
348 const u8 *neigh_addr,
349 struct batadv_orig_node *orig_node,
350 struct batadv_orig_node *orig_neigh)
352 struct batadv_neigh_node *neigh_node;
354 neigh_node = batadv_neigh_node_get_or_create(orig_node,
355 hard_iface, neigh_addr);
359 neigh_node->orig_node = orig_neigh;
365 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
367 struct batadv_ogm_packet *batadv_ogm_packet;
368 unsigned char *ogm_buff;
371 /* randomize initial seqno to avoid collision */
372 get_random_bytes(&random_seqno, sizeof(random_seqno));
373 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
375 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
376 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
380 hard_iface->bat_iv.ogm_buff = ogm_buff;
382 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
383 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
384 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
385 batadv_ogm_packet->ttl = 2;
386 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
387 batadv_ogm_packet->reserved = 0;
388 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
393 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
395 kfree(hard_iface->bat_iv.ogm_buff);
396 hard_iface->bat_iv.ogm_buff = NULL;
399 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
401 struct batadv_ogm_packet *batadv_ogm_packet;
402 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
404 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
405 ether_addr_copy(batadv_ogm_packet->orig,
406 hard_iface->net_dev->dev_addr);
407 ether_addr_copy(batadv_ogm_packet->prev_sender,
408 hard_iface->net_dev->dev_addr);
412 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
414 struct batadv_ogm_packet *batadv_ogm_packet;
415 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
417 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
418 batadv_ogm_packet->ttl = BATADV_TTL;
421 /* when do we schedule our own ogm to be sent */
423 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
427 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
428 msecs += prandom_u32() % (2 * BATADV_JITTER);
430 return jiffies + msecs_to_jiffies(msecs);
433 /* when do we schedule a ogm packet to be sent */
434 static unsigned long batadv_iv_ogm_fwd_send_time(void)
436 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
439 /* apply hop penalty for a normal link */
440 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
442 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
445 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
446 new_tq /= BATADV_TQ_MAX_VALUE;
452 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
453 * @buff_pos: current position in the skb
454 * @packet_len: total length of the skb
455 * @tvlv_len: tvlv length of the previously considered OGM
457 * Return: true if there is enough space for another OGM, false otherwise.
459 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
462 int next_buff_pos = 0;
464 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
465 next_buff_pos += ntohs(tvlv_len);
467 return (next_buff_pos <= packet_len) &&
468 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
471 /* send a batman ogm to a given interface */
472 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
473 struct batadv_hard_iface *hard_iface)
475 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
479 struct batadv_ogm_packet *batadv_ogm_packet;
483 if (hard_iface->if_status != BATADV_IF_ACTIVE)
488 packet_pos = forw_packet->skb->data;
489 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
491 /* adjust all flags and log packets */
492 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
493 batadv_ogm_packet->tvlv_len)) {
494 /* we might have aggregated direct link packets with an
495 * ordinary base packet
497 if (forw_packet->direct_link_flags & BIT(packet_num) &&
498 forw_packet->if_incoming == hard_iface)
499 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
501 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
503 if (packet_num > 0 || !forw_packet->own)
504 fwd_str = "Forwarding";
506 fwd_str = "Sending own";
508 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
509 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
510 fwd_str, (packet_num > 0 ? "aggregated " : ""),
511 batadv_ogm_packet->orig,
512 ntohl(batadv_ogm_packet->seqno),
513 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
514 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
516 hard_iface->net_dev->name,
517 hard_iface->net_dev->dev_addr);
519 buff_pos += BATADV_OGM_HLEN;
520 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
522 packet_pos = forw_packet->skb->data + buff_pos;
523 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
526 /* create clone because function is called more than once */
527 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
529 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
530 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
531 skb->len + ETH_HLEN);
532 batadv_send_broadcast_skb(skb, hard_iface);
536 /* send a batman ogm packet */
537 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
539 struct net_device *soft_iface;
541 if (!forw_packet->if_incoming) {
542 pr_err("Error - can't forward packet: incoming iface not specified\n");
546 soft_iface = forw_packet->if_incoming->soft_iface;
548 if (WARN_ON(!forw_packet->if_outgoing))
551 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
554 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
557 /* only for one specific outgoing interface */
558 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
562 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
563 * existing forward packet
564 * @new_bat_ogm_packet: OGM packet to be aggregated
565 * @bat_priv: the bat priv with all the soft interface information
566 * @packet_len: (total) length of the OGM
567 * @send_time: timestamp (jiffies) when the packet is to be sent
568 * @directlink: true if this is a direct link packet
569 * @if_incoming: interface where the packet was received
570 * @if_outgoing: interface for which the retransmission should be considered
571 * @forw_packet: the forwarded packet which should be checked
573 * Return: true if new_packet can be aggregated with forw_packet
576 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
577 struct batadv_priv *bat_priv,
578 int packet_len, unsigned long send_time,
580 const struct batadv_hard_iface *if_incoming,
581 const struct batadv_hard_iface *if_outgoing,
582 const struct batadv_forw_packet *forw_packet)
584 struct batadv_ogm_packet *batadv_ogm_packet;
585 int aggregated_bytes = forw_packet->packet_len + packet_len;
586 struct batadv_hard_iface *primary_if = NULL;
588 unsigned long aggregation_end_time;
590 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
591 aggregation_end_time = send_time;
592 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
594 /* we can aggregate the current packet to this aggregated packet
597 * - the send time is within our MAX_AGGREGATION_MS time
598 * - the resulting packet wont be bigger than
599 * MAX_AGGREGATION_BYTES
600 * otherwise aggregation is not possible
602 if (!time_before(send_time, forw_packet->send_time) ||
603 !time_after_eq(aggregation_end_time, forw_packet->send_time))
606 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
609 /* packet is not leaving on the same interface. */
610 if (forw_packet->if_outgoing != if_outgoing)
613 /* check aggregation compatibility
614 * -> direct link packets are broadcasted on
615 * their interface only
616 * -> aggregate packet if the current packet is
617 * a "global" packet as well as the base
620 primary_if = batadv_primary_if_get_selected(bat_priv);
624 /* packets without direct link flag and high TTL
625 * are flooded through the net
628 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
629 batadv_ogm_packet->ttl != 1 &&
631 /* own packets originating non-primary
632 * interfaces leave only that interface
634 (!forw_packet->own ||
635 forw_packet->if_incoming == primary_if)) {
640 /* if the incoming packet is sent via this one
641 * interface only - we still can aggregate
644 new_bat_ogm_packet->ttl == 1 &&
645 forw_packet->if_incoming == if_incoming &&
647 /* packets from direct neighbors or
648 * own secondary interface packets
649 * (= secondary interface packets in general)
651 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
653 forw_packet->if_incoming != primary_if))) {
660 batadv_hardif_put(primary_if);
665 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
667 * @packet_buff: pointer to the OGM
668 * @packet_len: (total) length of the OGM
669 * @send_time: timestamp (jiffies) when the packet is to be sent
670 * @direct_link: whether this OGM has direct link status
671 * @if_incoming: interface where the packet was received
672 * @if_outgoing: interface for which the retransmission should be considered
673 * @own_packet: true if it is a self-generated ogm
675 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
676 int packet_len, unsigned long send_time,
678 struct batadv_hard_iface *if_incoming,
679 struct batadv_hard_iface *if_outgoing,
682 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
683 struct batadv_forw_packet *forw_packet_aggr;
685 unsigned char *skb_buff;
686 unsigned int skb_size;
687 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
689 if (atomic_read(&bat_priv->aggregated_ogms) &&
690 packet_len < BATADV_MAX_AGGREGATION_BYTES)
691 skb_size = BATADV_MAX_AGGREGATION_BYTES;
693 skb_size = packet_len;
695 skb_size += ETH_HLEN;
697 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
701 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
702 queue_left, bat_priv, skb);
703 if (!forw_packet_aggr) {
708 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
709 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
711 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
712 forw_packet_aggr->packet_len = packet_len;
713 memcpy(skb_buff, packet_buff, packet_len);
715 forw_packet_aggr->own = own_packet;
716 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
717 forw_packet_aggr->send_time = send_time;
719 /* save packet direct link flag status */
721 forw_packet_aggr->direct_link_flags |= 1;
723 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
724 batadv_iv_send_outstanding_bat_ogm_packet);
726 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
729 /* aggregate a new packet into the existing ogm packet */
730 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
731 const unsigned char *packet_buff,
732 int packet_len, bool direct_link)
734 unsigned long new_direct_link_flag;
736 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
737 forw_packet_aggr->packet_len += packet_len;
738 forw_packet_aggr->num_packets++;
740 /* save packet direct link flag status */
742 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
743 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
748 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
749 * @bat_priv: the bat priv with all the soft interface information
750 * @packet_buff: pointer to the OGM
751 * @packet_len: (total) length of the OGM
752 * @if_incoming: interface where the packet was received
753 * @if_outgoing: interface for which the retransmission should be considered
754 * @own_packet: true if it is a self-generated ogm
755 * @send_time: timestamp (jiffies) when the packet is to be sent
757 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
758 unsigned char *packet_buff,
760 struct batadv_hard_iface *if_incoming,
761 struct batadv_hard_iface *if_outgoing,
762 int own_packet, unsigned long send_time)
764 /* _aggr -> pointer to the packet we want to aggregate with
765 * _pos -> pointer to the position in the queue
767 struct batadv_forw_packet *forw_packet_aggr = NULL;
768 struct batadv_forw_packet *forw_packet_pos = NULL;
769 struct batadv_ogm_packet *batadv_ogm_packet;
771 unsigned long max_aggregation_jiffies;
773 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
774 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
775 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
777 /* find position for the packet in the forward queue */
778 spin_lock_bh(&bat_priv->forw_bat_list_lock);
779 /* own packets are not to be aggregated */
780 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
781 hlist_for_each_entry(forw_packet_pos,
782 &bat_priv->forw_bat_list, list) {
783 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
784 bat_priv, packet_len,
785 send_time, direct_link,
789 forw_packet_aggr = forw_packet_pos;
795 /* nothing to aggregate with - either aggregation disabled or no
796 * suitable aggregation packet found
798 if (!forw_packet_aggr) {
799 /* the following section can run without the lock */
800 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
802 /* if we could not aggregate this packet with one of the others
803 * we hold it back for a while, so that it might be aggregated
806 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
807 send_time += max_aggregation_jiffies;
809 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
810 send_time, direct_link,
811 if_incoming, if_outgoing,
814 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
815 packet_len, direct_link);
816 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
820 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
821 const struct ethhdr *ethhdr,
822 struct batadv_ogm_packet *batadv_ogm_packet,
823 bool is_single_hop_neigh,
824 bool is_from_best_next_hop,
825 struct batadv_hard_iface *if_incoming,
826 struct batadv_hard_iface *if_outgoing)
828 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
831 if (batadv_ogm_packet->ttl <= 1) {
832 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
836 if (!is_from_best_next_hop) {
837 /* Mark the forwarded packet when it is not coming from our
838 * best next hop. We still need to forward the packet for our
839 * neighbor link quality detection to work in case the packet
840 * originated from a single hop neighbor. Otherwise we can
841 * simply drop the ogm.
843 if (is_single_hop_neigh)
844 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
849 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
851 batadv_ogm_packet->ttl--;
852 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
854 /* apply hop penalty */
855 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
858 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
859 "Forwarding packet: tq: %i, ttl: %i\n",
860 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
862 if (is_single_hop_neigh)
863 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
865 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
867 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
868 BATADV_OGM_HLEN + tvlv_len,
869 if_incoming, if_outgoing, 0,
870 batadv_iv_ogm_fwd_send_time());
874 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
875 * for the given interface
876 * @hard_iface: the interface for which the windows have to be shifted
879 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
881 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
882 struct batadv_hashtable *hash = bat_priv->orig_hash;
883 struct hlist_head *head;
884 struct batadv_orig_node *orig_node;
891 for (i = 0; i < hash->size; i++) {
892 head = &hash->table[i];
895 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
896 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
897 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
898 word = &orig_node->bat_iv.bcast_own[word_index];
900 batadv_bit_get_packet(bat_priv, word, 1, 0);
901 if_num = hard_iface->if_num;
902 w = &orig_node->bat_iv.bcast_own_sum[if_num];
903 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
904 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
910 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
912 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
913 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
914 struct batadv_ogm_packet *batadv_ogm_packet;
915 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
916 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
919 unsigned long send_time;
921 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
922 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
925 /* the interface gets activated here to avoid race conditions between
926 * the moment of activating the interface in
927 * hardif_activate_interface() where the originator mac is set and
928 * outdated packets (especially uninitialized mac addresses) in the
931 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
932 hard_iface->if_status = BATADV_IF_ACTIVE;
934 primary_if = batadv_primary_if_get_selected(bat_priv);
936 if (hard_iface == primary_if) {
937 /* tt changes have to be committed before the tvlv data is
938 * appended as it may alter the tt tvlv container
940 batadv_tt_local_commit_changes(bat_priv);
941 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
946 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
947 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
949 /* change sequence number to network order */
950 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
951 batadv_ogm_packet->seqno = htonl(seqno);
952 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
954 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
956 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
958 if (hard_iface != primary_if) {
959 /* OGMs from secondary interfaces are only scheduled on their
960 * respective interfaces.
962 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
963 hard_iface, hard_iface, 1, send_time);
967 /* OGMs from primary interfaces are scheduled on all
971 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
972 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
975 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
978 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
979 *ogm_buff_len, hard_iface,
980 tmp_hard_iface, 1, send_time);
982 batadv_hardif_put(tmp_hard_iface);
988 batadv_hardif_put(primary_if);
992 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
994 * @bat_priv: the bat priv with all the soft interface information
995 * @orig_node: the orig node who originally emitted the ogm packet
996 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
997 * @ethhdr: Ethernet header of the OGM
998 * @batadv_ogm_packet: the ogm packet
999 * @if_incoming: interface where the packet was received
1000 * @if_outgoing: interface for which the retransmission should be considered
1001 * @dup_status: the duplicate status of this ogm packet.
1004 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1005 struct batadv_orig_node *orig_node,
1006 struct batadv_orig_ifinfo *orig_ifinfo,
1007 const struct ethhdr *ethhdr,
1008 const struct batadv_ogm_packet *batadv_ogm_packet,
1009 struct batadv_hard_iface *if_incoming,
1010 struct batadv_hard_iface *if_outgoing,
1011 enum batadv_dup_status dup_status)
1013 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1014 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1015 struct batadv_neigh_node *neigh_node = NULL;
1016 struct batadv_neigh_node *tmp_neigh_node = NULL;
1017 struct batadv_neigh_node *router = NULL;
1018 struct batadv_orig_node *orig_node_tmp;
1020 u8 sum_orig, sum_neigh;
1024 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1025 "%s(): Searching and updating originator entry of received packet\n",
1029 hlist_for_each_entry_rcu(tmp_neigh_node,
1030 &orig_node->neigh_list, list) {
1031 neigh_addr = tmp_neigh_node->addr;
1032 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1033 tmp_neigh_node->if_incoming == if_incoming &&
1034 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1035 if (WARN(neigh_node, "too many matching neigh_nodes"))
1036 batadv_neigh_node_put(neigh_node);
1037 neigh_node = tmp_neigh_node;
1041 if (dup_status != BATADV_NO_DUP)
1044 /* only update the entry for this outgoing interface */
1045 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1050 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1051 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1052 &neigh_ifinfo->bat_iv.tq_index, 0);
1053 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1054 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1055 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1057 batadv_neigh_ifinfo_put(neigh_ifinfo);
1058 neigh_ifinfo = NULL;
1062 struct batadv_orig_node *orig_tmp;
1064 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1068 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1070 orig_node, orig_tmp);
1072 batadv_orig_node_put(orig_tmp);
1076 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1077 "Updating existing last-hop neighbor of originator\n");
1081 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1085 neigh_node->last_seen = jiffies;
1087 spin_lock_bh(&neigh_node->ifinfo_lock);
1088 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1089 &neigh_ifinfo->bat_iv.tq_index,
1090 batadv_ogm_packet->tq);
1091 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1092 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1093 spin_unlock_bh(&neigh_node->ifinfo_lock);
1095 if (dup_status == BATADV_NO_DUP) {
1096 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1097 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1100 /* if this neighbor already is our next hop there is nothing
1103 router = batadv_orig_router_get(orig_node, if_outgoing);
1104 if (router == neigh_node)
1108 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1112 /* if this neighbor does not offer a better TQ we won't
1115 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1119 /* if the TQ is the same and the link not more symmetric we
1120 * won't consider it either
1122 if (router_ifinfo &&
1123 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1124 orig_node_tmp = router->orig_node;
1125 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1126 if_num = router->if_incoming->if_num;
1127 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1128 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1130 orig_node_tmp = neigh_node->orig_node;
1131 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1132 if_num = neigh_node->if_incoming->if_num;
1133 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1134 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1136 if (sum_orig >= sum_neigh)
1140 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1147 batadv_neigh_node_put(neigh_node);
1149 batadv_neigh_node_put(router);
1151 batadv_neigh_ifinfo_put(neigh_ifinfo);
1153 batadv_neigh_ifinfo_put(router_ifinfo);
1157 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1158 * @orig_node: the orig node who originally emitted the ogm packet
1159 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1160 * @batadv_ogm_packet: the ogm packet
1161 * @if_incoming: interface where the packet was received
1162 * @if_outgoing: interface for which the retransmission should be considered
1164 * Return: true if the link can be considered bidirectional, false otherwise
1166 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1167 struct batadv_orig_node *orig_neigh_node,
1168 struct batadv_ogm_packet *batadv_ogm_packet,
1169 struct batadv_hard_iface *if_incoming,
1170 struct batadv_hard_iface *if_outgoing)
1172 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1173 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1174 struct batadv_neigh_ifinfo *neigh_ifinfo;
1176 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1177 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1179 unsigned int tq_asym_penalty, inv_asym_penalty;
1180 unsigned int combined_tq;
1181 unsigned int tq_iface_penalty;
1184 /* find corresponding one hop neighbor */
1186 hlist_for_each_entry_rcu(tmp_neigh_node,
1187 &orig_neigh_node->neigh_list, list) {
1188 if (!batadv_compare_eth(tmp_neigh_node->addr,
1189 orig_neigh_node->orig))
1192 if (tmp_neigh_node->if_incoming != if_incoming)
1195 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1198 neigh_node = tmp_neigh_node;
1204 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1205 orig_neigh_node->orig,
1212 /* if orig_node is direct neighbor update neigh_node last_seen */
1213 if (orig_node == orig_neigh_node)
1214 neigh_node->last_seen = jiffies;
1216 orig_node->last_seen = jiffies;
1218 /* find packet count of corresponding one hop neighbor */
1219 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1220 if_num = if_incoming->if_num;
1221 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1222 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1224 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1225 batadv_neigh_ifinfo_put(neigh_ifinfo);
1229 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1231 /* pay attention to not get a value bigger than 100 % */
1232 if (orig_eq_count > neigh_rq_count)
1233 total_count = neigh_rq_count;
1235 total_count = orig_eq_count;
1237 /* if we have too few packets (too less data) we set tq_own to zero
1238 * if we receive too few packets it is not considered bidirectional
1240 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1241 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1244 /* neigh_node->real_packet_count is never zero as we
1245 * only purge old information when getting new
1248 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1250 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1251 * affect the nearly-symmetric links only a little, but
1252 * punishes asymmetric links more. This will give a value
1253 * between 0 and TQ_MAX_VALUE
1255 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1256 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1257 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1258 BATADV_TQ_LOCAL_WINDOW_SIZE *
1259 BATADV_TQ_LOCAL_WINDOW_SIZE;
1260 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1261 inv_asym_penalty /= neigh_rq_max_cube;
1262 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1264 /* penalize if the OGM is forwarded on the same interface. WiFi
1265 * interfaces and other half duplex devices suffer from throughput
1266 * drops as they can't send and receive at the same time.
1268 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1269 if (if_outgoing && if_incoming == if_outgoing &&
1270 batadv_is_wifi_hardif(if_outgoing))
1271 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1274 combined_tq = batadv_ogm_packet->tq *
1278 combined_tq /= BATADV_TQ_MAX_VALUE *
1279 BATADV_TQ_MAX_VALUE *
1280 BATADV_TQ_MAX_VALUE;
1281 batadv_ogm_packet->tq = combined_tq;
1283 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1284 "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",
1285 orig_node->orig, orig_neigh_node->orig, total_count,
1286 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1287 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1288 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1290 /* if link has the minimum required transmission quality
1291 * consider it bidirectional
1293 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1298 batadv_neigh_node_put(neigh_node);
1303 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
1304 * adjust the sequence number and find out whether it is a duplicate
1305 * @ethhdr: ethernet header of the packet
1306 * @batadv_ogm_packet: OGM packet to be considered
1307 * @if_incoming: interface on which the OGM packet was received
1308 * @if_outgoing: interface for which the retransmission should be considered
1310 * Return: duplicate status as enum batadv_dup_status
1312 static enum batadv_dup_status
1313 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1314 const struct batadv_ogm_packet *batadv_ogm_packet,
1315 const struct batadv_hard_iface *if_incoming,
1316 struct batadv_hard_iface *if_outgoing)
1318 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1319 struct batadv_orig_node *orig_node;
1320 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1321 struct batadv_neigh_node *neigh_node;
1322 struct batadv_neigh_ifinfo *neigh_ifinfo;
1325 bool need_update = false;
1327 enum batadv_dup_status ret = BATADV_NO_DUP;
1328 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1331 unsigned long *bitmap;
1333 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1335 return BATADV_NO_DUP;
1337 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1338 if (WARN_ON(!orig_ifinfo)) {
1339 batadv_orig_node_put(orig_node);
1343 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1344 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1346 /* signalize caller that the packet is to be dropped. */
1347 if (!hlist_empty(&orig_node->neigh_list) &&
1348 batadv_window_protected(bat_priv, seq_diff,
1349 BATADV_TQ_LOCAL_WINDOW_SIZE,
1350 &orig_ifinfo->batman_seqno_reset, NULL)) {
1351 ret = BATADV_PROTECTED;
1356 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1357 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1362 neigh_addr = neigh_node->addr;
1363 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1364 orig_ifinfo->last_real_seqno,
1367 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1368 neigh_node->if_incoming == if_incoming) {
1371 ret = BATADV_NEIGH_DUP;
1374 if (is_dup && ret != BATADV_NEIGH_DUP)
1375 ret = BATADV_ORIG_DUP;
1378 /* if the window moved, set the update flag. */
1379 bitmap = neigh_ifinfo->bat_iv.real_bits;
1380 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1381 seq_diff, set_mark);
1383 packet_count = bitmap_weight(bitmap,
1384 BATADV_TQ_LOCAL_WINDOW_SIZE);
1385 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1386 batadv_neigh_ifinfo_put(neigh_ifinfo);
1391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1392 "%s updating last_seqno: old %u, new %u\n",
1393 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1394 orig_ifinfo->last_real_seqno, seqno);
1395 orig_ifinfo->last_real_seqno = seqno;
1399 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1400 batadv_orig_node_put(orig_node);
1401 batadv_orig_ifinfo_put(orig_ifinfo);
1406 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1408 * @skb: the skb containing the OGM
1409 * @ogm_offset: offset from skb->data to start of ogm header
1410 * @orig_node: the (cached) orig node for the originator of this OGM
1411 * @if_incoming: the interface where this packet was received
1412 * @if_outgoing: the interface for which the packet should be considered
1415 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1416 struct batadv_orig_node *orig_node,
1417 struct batadv_hard_iface *if_incoming,
1418 struct batadv_hard_iface *if_outgoing)
1420 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1421 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1422 struct batadv_neigh_node *router = NULL;
1423 struct batadv_neigh_node *router_router = NULL;
1424 struct batadv_orig_node *orig_neigh_node;
1425 struct batadv_orig_ifinfo *orig_ifinfo;
1426 struct batadv_neigh_node *orig_neigh_router = NULL;
1427 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1428 struct batadv_ogm_packet *ogm_packet;
1429 enum batadv_dup_status dup_status;
1430 bool is_from_best_next_hop = false;
1431 bool is_single_hop_neigh = false;
1432 bool sameseq, similar_ttl;
1433 struct sk_buff *skb_priv;
1434 struct ethhdr *ethhdr;
1438 /* create a private copy of the skb, as some functions change tq value
1441 skb_priv = skb_copy(skb, GFP_ATOMIC);
1445 ethhdr = eth_hdr(skb_priv);
1446 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1448 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1449 if_incoming, if_outgoing);
1450 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1451 is_single_hop_neigh = true;
1453 if (dup_status == BATADV_PROTECTED) {
1454 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1455 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1460 if (ogm_packet->tq == 0) {
1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1462 "Drop packet: originator packet with tq equal 0\n");
1466 if (is_single_hop_neigh) {
1467 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1470 hardif_neigh->last_seen = jiffies;
1473 router = batadv_orig_router_get(orig_node, if_outgoing);
1475 router_router = batadv_orig_router_get(router->orig_node,
1477 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1480 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1481 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1482 is_from_best_next_hop = true;
1484 prev_sender = ogm_packet->prev_sender;
1485 /* avoid temporary routing loops */
1486 if (router && router_router &&
1487 (batadv_compare_eth(router->addr, prev_sender)) &&
1488 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1489 (batadv_compare_eth(router->addr, router_router->addr))) {
1490 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1491 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1496 if (if_outgoing == BATADV_IF_DEFAULT)
1497 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1499 /* if sender is a direct neighbor the sender mac equals
1502 if (is_single_hop_neigh)
1503 orig_neigh_node = orig_node;
1505 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1508 if (!orig_neigh_node)
1511 /* Update nc_nodes of the originator */
1512 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1513 ogm_packet, is_single_hop_neigh);
1515 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1518 /* drop packet if sender is not a direct neighbor and if we
1519 * don't route towards it
1521 if (!is_single_hop_neigh && !orig_neigh_router) {
1522 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1523 "Drop packet: OGM via unknown neighbor!\n");
1527 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1528 ogm_packet, if_incoming,
1531 /* update ranking if it is not a duplicate or has the same
1532 * seqno and similar ttl as the non-duplicate
1534 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1538 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1539 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1541 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1542 (sameseq && similar_ttl))) {
1543 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1544 orig_ifinfo, ethhdr,
1545 ogm_packet, if_incoming,
1546 if_outgoing, dup_status);
1548 batadv_orig_ifinfo_put(orig_ifinfo);
1550 /* only forward for specific interface, not for the default one. */
1551 if (if_outgoing == BATADV_IF_DEFAULT)
1554 /* is single hop (direct) neighbor */
1555 if (is_single_hop_neigh) {
1556 /* OGMs from secondary interfaces should only scheduled once
1557 * per interface where it has been received, not multiple times
1559 if (ogm_packet->ttl <= 2 &&
1560 if_incoming != if_outgoing) {
1561 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1562 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1565 /* mark direct link on incoming interface */
1566 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1567 is_single_hop_neigh,
1568 is_from_best_next_hop, if_incoming,
1571 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1572 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1576 /* multihop originator */
1578 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1579 "Drop packet: not received via bidirectional link\n");
1583 if (dup_status == BATADV_NEIGH_DUP) {
1584 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1585 "Drop packet: duplicate packet received\n");
1589 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1590 "Forwarding packet: rebroadcast originator packet\n");
1591 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1592 is_single_hop_neigh, is_from_best_next_hop,
1593 if_incoming, if_outgoing);
1596 if (orig_neigh_node && !is_single_hop_neigh)
1597 batadv_orig_node_put(orig_neigh_node);
1600 batadv_neigh_ifinfo_put(router_ifinfo);
1602 batadv_neigh_node_put(router);
1604 batadv_neigh_node_put(router_router);
1605 if (orig_neigh_router)
1606 batadv_neigh_node_put(orig_neigh_router);
1608 batadv_hardif_neigh_put(hardif_neigh);
1610 consume_skb(skb_priv);
1614 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1615 * @skb: the skb containing the OGM
1616 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1617 * @if_incoming: the interface where this packet was receved
1619 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1620 struct batadv_hard_iface *if_incoming)
1622 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1623 struct batadv_orig_node *orig_neigh_node, *orig_node;
1624 struct batadv_hard_iface *hard_iface;
1625 struct batadv_ogm_packet *ogm_packet;
1626 u32 if_incoming_seqno;
1627 bool has_directlink_flag;
1628 struct ethhdr *ethhdr;
1629 bool is_my_oldorig = false;
1630 bool is_my_addr = false;
1631 bool is_my_orig = false;
1633 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1634 ethhdr = eth_hdr(skb);
1636 /* Silently drop when the batman packet is actually not a
1639 * This might happen if a packet is padded (e.g. Ethernet has a
1640 * minimum frame length of 64 byte) and the aggregation interprets
1641 * it as an additional length.
1643 * TODO: A more sane solution would be to have a bit in the
1644 * batadv_ogm_packet to detect whether the packet is the last
1645 * packet in an aggregation. Here we expect that the padding
1646 * is always zero (or not 0x01)
1648 if (ogm_packet->packet_type != BATADV_IV_OGM)
1651 /* could be changed by schedule_own_packet() */
1652 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1654 if (ogm_packet->flags & BATADV_DIRECTLINK)
1655 has_directlink_flag = true;
1657 has_directlink_flag = false;
1659 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1660 "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",
1661 ethhdr->h_source, if_incoming->net_dev->name,
1662 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1663 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1664 ogm_packet->tq, ogm_packet->ttl,
1665 ogm_packet->version, has_directlink_flag);
1668 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1669 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1672 if (hard_iface->soft_iface != if_incoming->soft_iface)
1675 if (batadv_compare_eth(ethhdr->h_source,
1676 hard_iface->net_dev->dev_addr))
1679 if (batadv_compare_eth(ogm_packet->orig,
1680 hard_iface->net_dev->dev_addr))
1683 if (batadv_compare_eth(ogm_packet->prev_sender,
1684 hard_iface->net_dev->dev_addr))
1685 is_my_oldorig = true;
1690 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1691 "Drop packet: received my own broadcast (sender: %pM)\n",
1697 unsigned long *word;
1703 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1705 if (!orig_neigh_node)
1708 /* neighbor has to indicate direct link and it has to
1709 * come via the corresponding interface
1710 * save packet seqno for bidirectional check
1712 if (has_directlink_flag &&
1713 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1714 ogm_packet->orig)) {
1715 if_num = if_incoming->if_num;
1716 offset = if_num * BATADV_NUM_WORDS;
1718 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1719 word = &orig_neigh_node->bat_iv.bcast_own[offset];
1720 bit_pos = if_incoming_seqno - 2;
1721 bit_pos -= ntohl(ogm_packet->seqno);
1722 batadv_set_bit(word, bit_pos);
1723 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1724 *weight = bitmap_weight(word,
1725 BATADV_TQ_LOCAL_WINDOW_SIZE);
1726 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1729 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1730 "Drop packet: originator packet from myself (via neighbor)\n");
1731 batadv_orig_node_put(orig_neigh_node);
1735 if (is_my_oldorig) {
1736 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1737 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1742 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1743 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1744 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1749 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1753 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1754 if_incoming, BATADV_IF_DEFAULT);
1757 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1758 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1761 if (hard_iface->soft_iface != bat_priv->soft_iface)
1764 if (!kref_get_unless_zero(&hard_iface->refcount))
1767 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1768 if_incoming, hard_iface);
1770 batadv_hardif_put(hard_iface);
1774 batadv_orig_node_put(orig_node);
1777 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1779 struct delayed_work *delayed_work;
1780 struct batadv_forw_packet *forw_packet;
1781 struct batadv_priv *bat_priv;
1782 bool dropped = false;
1784 delayed_work = to_delayed_work(work);
1785 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1787 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1789 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1794 batadv_iv_ogm_emit(forw_packet);
1796 /* we have to have at least one packet in the queue to determine the
1797 * queues wake up time unless we are shutting down.
1799 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1800 * primary interface should only be rescheduled once per period, but
1801 * this function will be called for the forw_packet instances of the
1802 * other secondary interfaces as well.
1804 if (forw_packet->own &&
1805 forw_packet->if_incoming == forw_packet->if_outgoing)
1806 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1809 /* do we get something for free()? */
1810 if (batadv_forw_packet_steal(forw_packet,
1811 &bat_priv->forw_bat_list_lock))
1812 batadv_forw_packet_free(forw_packet, dropped);
1815 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1816 struct batadv_hard_iface *if_incoming)
1818 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1819 struct batadv_ogm_packet *ogm_packet;
1823 int ret = NET_RX_DROP;
1825 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1829 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1830 * that does not have B.A.T.M.A.N. IV enabled ?
1832 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1835 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1836 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1837 skb->len + ETH_HLEN);
1840 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1842 /* unpack the aggregated packets and process them one by one */
1843 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1844 ogm_packet->tvlv_len)) {
1845 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1847 ogm_offset += BATADV_OGM_HLEN;
1848 ogm_offset += ntohs(ogm_packet->tvlv_len);
1850 packet_pos = skb->data + ogm_offset;
1851 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1854 ret = NET_RX_SUCCESS;
1857 if (ret == NET_RX_SUCCESS)
1865 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1867 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
1868 * @orig_node: the orig_node for which the neighbors are printed
1869 * @if_outgoing: outgoing interface for these entries
1870 * @seq: debugfs table seq_file struct
1872 * Must be called while holding an rcu lock.
1875 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1876 struct batadv_hard_iface *if_outgoing,
1877 struct seq_file *seq)
1879 struct batadv_neigh_node *neigh_node;
1880 struct batadv_neigh_ifinfo *n_ifinfo;
1882 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1883 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1887 seq_printf(seq, " %pM (%3i)",
1889 n_ifinfo->bat_iv.tq_avg);
1891 batadv_neigh_ifinfo_put(n_ifinfo);
1896 * batadv_iv_ogm_orig_print() - print the originator table
1897 * @bat_priv: the bat priv with all the soft interface information
1898 * @seq: debugfs table seq_file struct
1899 * @if_outgoing: the outgoing interface for which this should be printed
1901 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1902 struct seq_file *seq,
1903 struct batadv_hard_iface *if_outgoing)
1905 struct batadv_neigh_node *neigh_node;
1906 struct batadv_hashtable *hash = bat_priv->orig_hash;
1907 int last_seen_msecs, last_seen_secs;
1908 struct batadv_orig_node *orig_node;
1909 struct batadv_neigh_ifinfo *n_ifinfo;
1910 unsigned long last_seen_jiffies;
1911 struct hlist_head *head;
1912 int batman_count = 0;
1916 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1918 for (i = 0; i < hash->size; i++) {
1919 head = &hash->table[i];
1922 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1923 neigh_node = batadv_orig_router_get(orig_node,
1928 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1933 if (n_ifinfo->bat_iv.tq_avg == 0)
1936 last_seen_jiffies = jiffies - orig_node->last_seen;
1937 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1938 last_seen_secs = last_seen_msecs / 1000;
1939 last_seen_msecs = last_seen_msecs % 1000;
1941 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1942 orig_node->orig, last_seen_secs,
1943 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1945 neigh_node->if_incoming->net_dev->name);
1947 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1949 seq_putc(seq, '\n');
1953 batadv_neigh_node_put(neigh_node);
1955 batadv_neigh_ifinfo_put(n_ifinfo);
1960 if (batman_count == 0)
1961 seq_puts(seq, "No batman nodes in range ...\n");
1966 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1967 * given outgoing interface.
1968 * @neigh_node: Neighbour of interest
1969 * @if_outgoing: Outgoing interface of interest
1970 * @tq_avg: Pointer of where to store the TQ average
1972 * Return: False if no average TQ available, otherwise true.
1975 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1976 struct batadv_hard_iface *if_outgoing,
1979 struct batadv_neigh_ifinfo *n_ifinfo;
1981 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1985 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1986 batadv_neigh_ifinfo_put(n_ifinfo);
1992 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
1994 * @msg: Netlink message to dump into
1995 * @portid: Port making netlink request
1996 * @seq: Sequence number of netlink message
1997 * @bat_priv: The bat priv with all the soft interface information
1998 * @if_outgoing: Limit dump to entries with this outgoing interface
1999 * @orig_node: Originator to dump
2000 * @neigh_node: Single hops neighbour
2001 * @best: Is the best originator
2003 * Return: Error code, or 0 on success
2006 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2007 struct batadv_priv *bat_priv,
2008 struct batadv_hard_iface *if_outgoing,
2009 struct batadv_orig_node *orig_node,
2010 struct batadv_neigh_node *neigh_node,
2015 unsigned int last_seen_msecs;
2017 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2019 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2022 if (if_outgoing != BATADV_IF_DEFAULT &&
2023 if_outgoing != neigh_node->if_incoming)
2026 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2027 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2031 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2033 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2034 neigh_node->addr) ||
2035 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2036 neigh_node->if_incoming->net_dev->ifindex) ||
2037 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2038 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2040 goto nla_put_failure;
2042 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2043 goto nla_put_failure;
2045 genlmsg_end(msg, hdr);
2049 genlmsg_cancel(msg, hdr);
2054 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
2055 * @msg: Netlink message to dump into
2056 * @portid: Port making netlink request
2057 * @seq: Sequence number of netlink message
2058 * @bat_priv: The bat priv with all the soft interface information
2059 * @if_outgoing: Limit dump to entries with this outgoing interface
2060 * @orig_node: Originator to dump
2061 * @sub_s: Number of sub entries to skip
2063 * This function assumes the caller holds rcu_read_lock().
2065 * Return: Error code, or 0 on success
2068 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2069 struct batadv_priv *bat_priv,
2070 struct batadv_hard_iface *if_outgoing,
2071 struct batadv_orig_node *orig_node, int *sub_s)
2073 struct batadv_neigh_node *neigh_node_best;
2074 struct batadv_neigh_node *neigh_node;
2079 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2080 if (!neigh_node_best)
2083 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2087 if (tq_avg_best == 0)
2090 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2094 best = (neigh_node == neigh_node_best);
2096 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2097 bat_priv, if_outgoing,
2098 orig_node, neigh_node,
2100 batadv_neigh_node_put(neigh_node_best);
2108 if (neigh_node_best)
2109 batadv_neigh_node_put(neigh_node_best);
2116 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
2118 * @msg: Netlink message to dump into
2119 * @portid: Port making netlink request
2120 * @seq: Sequence number of netlink message
2121 * @bat_priv: The bat priv with all the soft interface information
2122 * @if_outgoing: Limit dump to entries with this outgoing interface
2123 * @head: Bucket to be dumped
2124 * @idx_s: Number of entries to be skipped
2125 * @sub: Number of sub entries to be skipped
2127 * Return: Error code, or 0 on success
2130 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2131 struct batadv_priv *bat_priv,
2132 struct batadv_hard_iface *if_outgoing,
2133 struct hlist_head *head, int *idx_s, int *sub)
2135 struct batadv_orig_node *orig_node;
2139 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2143 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2144 if_outgoing, orig_node,
2159 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2160 * @msg: Netlink message to dump into
2161 * @cb: Control block containing additional options
2162 * @bat_priv: The bat priv with all the soft interface information
2163 * @if_outgoing: Limit dump to entries with this outgoing interface
2166 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2167 struct batadv_priv *bat_priv,
2168 struct batadv_hard_iface *if_outgoing)
2170 struct batadv_hashtable *hash = bat_priv->orig_hash;
2171 struct hlist_head *head;
2172 int bucket = cb->args[0];
2173 int idx = cb->args[1];
2174 int sub = cb->args[2];
2175 int portid = NETLINK_CB(cb->skb).portid;
2177 while (bucket < hash->size) {
2178 head = &hash->table[bucket];
2180 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2182 bat_priv, if_outgoing, head,
2189 cb->args[0] = bucket;
2194 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2196 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
2197 * @seq: neighbour table seq_file struct
2198 * @hardif_neigh: hardif neighbour information
2201 batadv_iv_hardif_neigh_print(struct seq_file *seq,
2202 struct batadv_hardif_neigh_node *hardif_neigh)
2204 int last_secs, last_msecs;
2206 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2207 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2209 seq_printf(seq, " %10s %pM %4i.%03is\n",
2210 hardif_neigh->if_incoming->net_dev->name,
2211 hardif_neigh->addr, last_secs, last_msecs);
2215 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
2216 * @bat_priv: the bat priv with all the soft interface information
2217 * @seq: neighbour table seq_file struct
2219 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2220 struct seq_file *seq)
2222 struct net_device *net_dev = (struct net_device *)seq->private;
2223 struct batadv_hardif_neigh_node *hardif_neigh;
2224 struct batadv_hard_iface *hard_iface;
2225 int batman_count = 0;
2227 seq_puts(seq, " IF Neighbor last-seen\n");
2230 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2231 if (hard_iface->soft_iface != net_dev)
2234 hlist_for_each_entry_rcu(hardif_neigh,
2235 &hard_iface->neigh_list, list) {
2236 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2242 if (batman_count == 0)
2243 seq_puts(seq, "No batman nodes in range ...\n");
2248 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2249 * @neigh1: the first neighbor object of the comparison
2250 * @if_outgoing1: outgoing interface for the first neighbor
2251 * @neigh2: the second neighbor object of the comparison
2252 * @if_outgoing2: outgoing interface for the second neighbor
2253 * @diff: pointer to integer receiving the calculated difference
2255 * The content of *@diff is only valid when this function returns true.
2256 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2257 * the same as or higher than the metric via neigh2
2259 * Return: true when the difference could be calculated, false otherwise
2261 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2262 struct batadv_hard_iface *if_outgoing1,
2263 struct batadv_neigh_node *neigh2,
2264 struct batadv_hard_iface *if_outgoing2,
2267 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2271 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2272 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2274 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2279 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2280 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2281 *diff = (int)tq1 - (int)tq2;
2285 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2287 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2293 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2294 * @msg: Netlink message to dump into
2295 * @portid: Port making netlink request
2296 * @seq: Sequence number of netlink message
2297 * @hardif_neigh: Neighbour to be dumped
2299 * Return: Error code, or 0 on success
2302 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2303 struct batadv_hardif_neigh_node *hardif_neigh)
2306 unsigned int last_seen_msecs;
2308 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2310 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2311 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2315 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2316 hardif_neigh->addr) ||
2317 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2318 hardif_neigh->if_incoming->net_dev->ifindex) ||
2319 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2321 goto nla_put_failure;
2323 genlmsg_end(msg, hdr);
2327 genlmsg_cancel(msg, hdr);
2332 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2334 * @msg: Netlink message to dump into
2335 * @portid: Port making netlink request
2336 * @seq: Sequence number of netlink message
2337 * @bat_priv: The bat priv with all the soft interface information
2338 * @hard_iface: Hard interface to dump the neighbours for
2339 * @idx_s: Number of entries to skip
2341 * This function assumes the caller holds rcu_read_lock().
2343 * Return: Error code, or 0 on success
2346 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2347 struct batadv_priv *bat_priv,
2348 struct batadv_hard_iface *hard_iface,
2351 struct batadv_hardif_neigh_node *hardif_neigh;
2354 hlist_for_each_entry_rcu(hardif_neigh,
2355 &hard_iface->neigh_list, list) {
2359 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2371 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2372 * @msg: Netlink message to dump into
2373 * @cb: Control block containing additional options
2374 * @bat_priv: The bat priv with all the soft interface information
2375 * @single_hardif: Limit dump to this hard interfaace
2378 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2379 struct batadv_priv *bat_priv,
2380 struct batadv_hard_iface *single_hardif)
2382 struct batadv_hard_iface *hard_iface;
2384 int i_hardif_s = cb->args[0];
2385 int idx = cb->args[1];
2386 int portid = NETLINK_CB(cb->skb).portid;
2389 if (single_hardif) {
2390 if (i_hardif_s == 0) {
2391 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2399 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2401 if (hard_iface->soft_iface != bat_priv->soft_iface)
2404 if (i_hardif++ < i_hardif_s)
2407 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2410 hard_iface, &idx)) {
2418 cb->args[0] = i_hardif;
2423 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2424 * @neigh1: the first neighbor object of the comparison
2425 * @if_outgoing1: outgoing interface for the first neighbor
2426 * @neigh2: the second neighbor object of the comparison
2427 * @if_outgoing2: outgoing interface for the second neighbor
2429 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2430 * lower, the same as or higher than the metric via neigh2
2432 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2433 struct batadv_hard_iface *if_outgoing1,
2434 struct batadv_neigh_node *neigh2,
2435 struct batadv_hard_iface *if_outgoing2)
2440 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2441 if_outgoing2, &diff);
2449 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2450 * than neigh2 from the metric prospective
2451 * @neigh1: the first neighbor object of the comparison
2452 * @if_outgoing1: outgoing interface for the first neighbor
2453 * @neigh2: the second neighbor object of the comparison
2454 * @if_outgoing2: outgoing interface for the second neighbor
2456 * Return: true if the metric via neigh1 is equally good or better than
2457 * the metric via neigh2, false otherwise.
2460 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2461 struct batadv_hard_iface *if_outgoing1,
2462 struct batadv_neigh_node *neigh2,
2463 struct batadv_hard_iface *if_outgoing2)
2468 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2469 if_outgoing2, &diff);
2473 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2477 static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2479 /* begin scheduling originator messages on that interface */
2480 batadv_iv_ogm_schedule(hard_iface);
2484 * batadv_iv_init_sel_class() - initialize GW selection class
2485 * @bat_priv: the bat priv with all the soft interface information
2487 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2489 /* set default TQ difference threshold to 20 */
2490 atomic_set(&bat_priv->gw.sel_class, 20);
2493 static struct batadv_gw_node *
2494 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2496 struct batadv_neigh_node *router;
2497 struct batadv_neigh_ifinfo *router_ifinfo;
2498 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2499 u64 max_gw_factor = 0;
2500 u64 tmp_gw_factor = 0;
2503 struct batadv_orig_node *orig_node;
2506 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2507 orig_node = gw_node->orig_node;
2508 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2512 router_ifinfo = batadv_neigh_ifinfo_get(router,
2517 if (!kref_get_unless_zero(&gw_node->refcount))
2520 tq_avg = router_ifinfo->bat_iv.tq_avg;
2522 switch (atomic_read(&bat_priv->gw.sel_class)) {
2523 case 1: /* fast connection */
2524 tmp_gw_factor = tq_avg * tq_avg;
2525 tmp_gw_factor *= gw_node->bandwidth_down;
2526 tmp_gw_factor *= 100 * 100;
2527 tmp_gw_factor >>= 18;
2529 if (tmp_gw_factor > max_gw_factor ||
2530 (tmp_gw_factor == max_gw_factor &&
2533 batadv_gw_node_put(curr_gw);
2535 kref_get(&curr_gw->refcount);
2539 default: /* 2: stable connection (use best statistic)
2540 * 3: fast-switch (use best statistic but change as
2541 * soon as a better gateway appears)
2542 * XX: late-switch (use best statistic but change as
2543 * soon as a better gateway appears which has
2544 * $routing_class more tq points)
2546 if (tq_avg > max_tq) {
2548 batadv_gw_node_put(curr_gw);
2550 kref_get(&curr_gw->refcount);
2555 if (tq_avg > max_tq)
2558 if (tmp_gw_factor > max_gw_factor)
2559 max_gw_factor = tmp_gw_factor;
2561 batadv_gw_node_put(gw_node);
2564 batadv_neigh_node_put(router);
2566 batadv_neigh_ifinfo_put(router_ifinfo);
2573 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2574 struct batadv_orig_node *curr_gw_orig,
2575 struct batadv_orig_node *orig_node)
2577 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2578 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2579 struct batadv_neigh_node *router_gw = NULL;
2580 struct batadv_neigh_node *router_orig = NULL;
2581 u8 gw_tq_avg, orig_tq_avg;
2584 /* dynamic re-election is performed only on fast or late switch */
2585 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2588 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2594 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2596 if (!router_gw_ifinfo) {
2601 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2605 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2607 if (!router_orig_ifinfo)
2610 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2611 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2613 /* the TQ value has to be better */
2614 if (orig_tq_avg < gw_tq_avg)
2617 /* if the routing class is greater than 3 the value tells us how much
2618 * greater the TQ value of the new gateway must be
2620 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2621 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2624 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2625 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2626 gw_tq_avg, orig_tq_avg);
2630 if (router_gw_ifinfo)
2631 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2632 if (router_orig_ifinfo)
2633 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2635 batadv_neigh_node_put(router_gw);
2637 batadv_neigh_node_put(router_orig);
2642 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2643 /* fails if orig_node has no router */
2644 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2645 struct seq_file *seq,
2646 const struct batadv_gw_node *gw_node)
2648 struct batadv_gw_node *curr_gw;
2649 struct batadv_neigh_node *router;
2650 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2653 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2657 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2661 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2663 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2664 (curr_gw == gw_node ? "=>" : " "),
2665 gw_node->orig_node->orig,
2666 router_ifinfo->bat_iv.tq_avg, router->addr,
2667 router->if_incoming->net_dev->name,
2668 gw_node->bandwidth_down / 10,
2669 gw_node->bandwidth_down % 10,
2670 gw_node->bandwidth_up / 10,
2671 gw_node->bandwidth_up % 10);
2672 ret = seq_has_overflowed(seq) ? -1 : 0;
2675 batadv_gw_node_put(curr_gw);
2678 batadv_neigh_ifinfo_put(router_ifinfo);
2680 batadv_neigh_node_put(router);
2684 static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2685 struct seq_file *seq)
2687 struct batadv_gw_node *gw_node;
2691 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2694 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2695 /* fails if orig_node has no router */
2696 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2704 seq_puts(seq, "No gateways in range ...\n");
2709 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2710 * @msg: Netlink message to dump into
2711 * @portid: Port making netlink request
2712 * @seq: Sequence number of netlink message
2713 * @bat_priv: The bat priv with all the soft interface information
2714 * @gw_node: Gateway to be dumped
2716 * Return: Error code, or 0 on success
2718 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2719 struct batadv_priv *bat_priv,
2720 struct batadv_gw_node *gw_node)
2722 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2723 struct batadv_neigh_node *router;
2724 struct batadv_gw_node *curr_gw;
2728 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2732 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2736 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2738 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2739 NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
2747 if (curr_gw == gw_node)
2748 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2749 genlmsg_cancel(msg, hdr);
2753 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2754 gw_node->orig_node->orig) ||
2755 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2756 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2758 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2759 router->if_incoming->net_dev->name) ||
2760 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2761 gw_node->bandwidth_down) ||
2762 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2763 gw_node->bandwidth_up)) {
2764 genlmsg_cancel(msg, hdr);
2768 genlmsg_end(msg, hdr);
2773 batadv_neigh_ifinfo_put(router_ifinfo);
2775 batadv_neigh_node_put(router);
2780 * batadv_iv_gw_dump() - Dump gateways into a message
2781 * @msg: Netlink message to dump into
2782 * @cb: Control block containing additional options
2783 * @bat_priv: The bat priv with all the soft interface information
2785 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2786 struct batadv_priv *bat_priv)
2788 int portid = NETLINK_CB(cb->skb).portid;
2789 struct batadv_gw_node *gw_node;
2790 int idx_skip = cb->args[0];
2794 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2795 if (idx++ < idx_skip)
2798 if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
2799 bat_priv, gw_node)) {
2809 cb->args[0] = idx_skip;
2812 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2813 .name = "BATMAN_IV",
2815 .activate = batadv_iv_iface_activate,
2816 .enable = batadv_iv_ogm_iface_enable,
2817 .disable = batadv_iv_ogm_iface_disable,
2818 .update_mac = batadv_iv_ogm_iface_update_mac,
2819 .primary_set = batadv_iv_ogm_primary_iface_set,
2822 .cmp = batadv_iv_ogm_neigh_cmp,
2823 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2824 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2825 .print = batadv_iv_neigh_print,
2827 .dump = batadv_iv_ogm_neigh_dump,
2830 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2831 .print = batadv_iv_ogm_orig_print,
2833 .dump = batadv_iv_ogm_orig_dump,
2834 .free = batadv_iv_ogm_orig_free,
2835 .add_if = batadv_iv_ogm_orig_add_if,
2836 .del_if = batadv_iv_ogm_orig_del_if,
2839 .init_sel_class = batadv_iv_init_sel_class,
2840 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2841 .is_eligible = batadv_iv_gw_is_eligible,
2842 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2843 .print = batadv_iv_gw_print,
2845 .dump = batadv_iv_gw_dump,
2849 int __init batadv_iv_init(void)
2853 /* batman originator packet */
2854 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2855 batadv_iv_ogm_receive);
2859 ret = batadv_algo_register(&batadv_batman_iv);
2861 goto handler_unregister;
2866 batadv_recv_handler_unregister(BATADV_IV_OGM);