1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "bat_iv_ogm.h"
21 #include <linux/atomic.h>
22 #include <linux/bitmap.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/init.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/netlink.h>
39 #include <linux/pkt_sched.h>
40 #include <linux/printk.h>
41 #include <linux/random.h>
42 #include <linux/rculist.h>
43 #include <linux/rcupdate.h>
44 #include <linux/seq_file.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/stddef.h>
49 #include <linux/string.h>
50 #include <linux/types.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/netlink.h>
54 #include <uapi/linux/batman_adv.h>
58 #include "gateway_client.h"
59 #include "hard-interface.h"
63 #include "network-coding.h"
64 #include "originator.h"
68 #include "translation-table.h"
71 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
74 * enum batadv_dup_status - duplicate status
75 * @BATADV_NO_DUP: the packet is no duplicate
76 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
78 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
79 * @BATADV_PROTECTED: originator is currently protected (after reboot)
81 enum batadv_dup_status {
89 * batadv_ring_buffer_set - update the ring buffer with the given value
90 * @lq_recv: pointer to the ring buffer
91 * @lq_index: index to store the value at
92 * @value: value to store in the ring buffer
94 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
96 lq_recv[*lq_index] = value;
97 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
101 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
102 * in the given ring buffer
103 * @lq_recv: pointer to the ring buffer
105 * Return: computed average value.
107 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
116 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
129 return (u8)(sum / count);
133 * batadv_iv_ogm_orig_free - free the private resources allocated for this
135 * @orig_node: the orig_node for which the resources have to be free'd
137 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
139 kfree(orig_node->bat_iv.bcast_own);
140 kfree(orig_node->bat_iv.bcast_own_sum);
144 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
145 * include the new hard-interface
146 * @orig_node: the orig_node that has to be changed
147 * @max_if_num: the current amount of interfaces
149 * Return: 0 on success, a negative error code otherwise.
151 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
158 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
160 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
161 data_ptr = kmalloc_array(max_if_num,
162 BATADV_NUM_WORDS * sizeof(unsigned long),
167 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
168 kfree(orig_node->bat_iv.bcast_own);
169 orig_node->bat_iv.bcast_own = data_ptr;
171 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
175 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
176 (max_if_num - 1) * sizeof(u8));
177 kfree(orig_node->bat_iv.bcast_own_sum);
178 orig_node->bat_iv.bcast_own_sum = data_ptr;
183 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
189 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
190 * @orig_node: the orig_node that has to be changed
191 * @max_if_num: the current amount of interfaces
192 * @del_if_num: the index of the interface being removed
195 batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
196 int max_if_num, int del_if_num)
202 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
204 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
205 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
207 /* use old buffer when new one could not be allocated */
208 data_ptr = orig_node->bat_iv.bcast_own;
210 /* copy first part */
211 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
213 /* copy second part */
214 if_offset = (del_if_num + 1) * chunk_size;
215 memmove((char *)data_ptr + del_if_num * chunk_size,
216 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
217 (max_if_num - del_if_num) * chunk_size);
219 /* bcast_own was shrunk down in new buffer; free old one */
220 if (orig_node->bat_iv.bcast_own != data_ptr) {
221 kfree(orig_node->bat_iv.bcast_own);
222 orig_node->bat_iv.bcast_own = data_ptr;
227 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
228 * @orig_node: the orig_node that has to be changed
229 * @max_if_num: the current amount of interfaces
230 * @del_if_num: the index of the interface being removed
233 batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
234 int max_if_num, int del_if_num)
239 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
241 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
243 /* use old buffer when new one could not be allocated */
244 data_ptr = orig_node->bat_iv.bcast_own_sum;
246 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
247 del_if_num * sizeof(u8));
249 if_offset = (del_if_num + 1) * sizeof(u8);
250 memmove((char *)data_ptr + del_if_num * sizeof(u8),
251 orig_node->bat_iv.bcast_own_sum + if_offset,
252 (max_if_num - del_if_num) * sizeof(u8));
254 /* bcast_own_sum was shrunk down in new buffer; free old one */
255 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
256 kfree(orig_node->bat_iv.bcast_own_sum);
257 orig_node->bat_iv.bcast_own_sum = data_ptr;
262 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
263 * exclude the removed interface
264 * @orig_node: the orig_node that has to be changed
265 * @max_if_num: the current amount of interfaces
266 * @del_if_num: the index of the interface being removed
268 * Return: 0 on success, a negative error code otherwise.
270 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
271 int max_if_num, int del_if_num)
273 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
275 if (max_if_num == 0) {
276 kfree(orig_node->bat_iv.bcast_own);
277 kfree(orig_node->bat_iv.bcast_own_sum);
278 orig_node->bat_iv.bcast_own = NULL;
279 orig_node->bat_iv.bcast_own_sum = NULL;
281 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
283 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
287 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
293 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
294 * @bat_priv: the bat priv with all the soft interface information
295 * @addr: mac address of the originator
297 * Return: the originator object corresponding to the passed mac address or NULL
299 * If the object does not exists it is created an initialised.
301 static struct batadv_orig_node *
302 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
304 struct batadv_orig_node *orig_node;
305 int size, hash_added;
307 orig_node = batadv_orig_hash_find(bat_priv, addr);
311 orig_node = batadv_orig_node_new(bat_priv, addr);
315 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
317 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
318 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
319 if (!orig_node->bat_iv.bcast_own)
322 size = bat_priv->num_ifaces * sizeof(u8);
323 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
324 if (!orig_node->bat_iv.bcast_own_sum)
327 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
328 batadv_choose_orig, orig_node,
329 &orig_node->hash_entry);
336 /* free twice, as batadv_orig_node_new sets refcount to 2 */
337 batadv_orig_node_put(orig_node);
338 batadv_orig_node_put(orig_node);
343 static struct batadv_neigh_node *
344 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
345 const u8 *neigh_addr,
346 struct batadv_orig_node *orig_node,
347 struct batadv_orig_node *orig_neigh)
349 struct batadv_neigh_node *neigh_node;
351 neigh_node = batadv_neigh_node_get_or_create(orig_node,
352 hard_iface, neigh_addr);
356 neigh_node->orig_node = orig_neigh;
362 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
364 struct batadv_ogm_packet *batadv_ogm_packet;
365 unsigned char *ogm_buff;
368 /* randomize initial seqno to avoid collision */
369 get_random_bytes(&random_seqno, sizeof(random_seqno));
370 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
372 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
373 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
377 hard_iface->bat_iv.ogm_buff = ogm_buff;
379 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
380 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
381 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
382 batadv_ogm_packet->ttl = 2;
383 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
384 batadv_ogm_packet->reserved = 0;
385 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
390 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
392 kfree(hard_iface->bat_iv.ogm_buff);
393 hard_iface->bat_iv.ogm_buff = NULL;
396 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
398 struct batadv_ogm_packet *batadv_ogm_packet;
399 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
401 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
402 ether_addr_copy(batadv_ogm_packet->orig,
403 hard_iface->net_dev->dev_addr);
404 ether_addr_copy(batadv_ogm_packet->prev_sender,
405 hard_iface->net_dev->dev_addr);
409 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
411 struct batadv_ogm_packet *batadv_ogm_packet;
412 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
414 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
415 batadv_ogm_packet->ttl = BATADV_TTL;
418 /* when do we schedule our own ogm to be sent */
420 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
424 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
425 msecs += prandom_u32() % (2 * BATADV_JITTER);
427 return jiffies + msecs_to_jiffies(msecs);
430 /* when do we schedule a ogm packet to be sent */
431 static unsigned long batadv_iv_ogm_fwd_send_time(void)
433 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
436 /* apply hop penalty for a normal link */
437 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
439 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
442 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
443 new_tq /= BATADV_TQ_MAX_VALUE;
449 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
450 * @buff_pos: current position in the skb
451 * @packet_len: total length of the skb
452 * @tvlv_len: tvlv length of the previously considered OGM
454 * Return: true if there is enough space for another OGM, false otherwise.
456 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
459 int next_buff_pos = 0;
461 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
462 next_buff_pos += ntohs(tvlv_len);
464 return (next_buff_pos <= packet_len) &&
465 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
468 /* send a batman ogm to a given interface */
469 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
470 struct batadv_hard_iface *hard_iface)
472 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
476 struct batadv_ogm_packet *batadv_ogm_packet;
480 if (hard_iface->if_status != BATADV_IF_ACTIVE)
485 packet_pos = forw_packet->skb->data;
486 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
488 /* adjust all flags and log packets */
489 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
490 batadv_ogm_packet->tvlv_len)) {
491 /* we might have aggregated direct link packets with an
492 * ordinary base packet
494 if (forw_packet->direct_link_flags & BIT(packet_num) &&
495 forw_packet->if_incoming == hard_iface)
496 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
498 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
500 if (packet_num > 0 || !forw_packet->own)
501 fwd_str = "Forwarding";
503 fwd_str = "Sending own";
505 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
506 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
507 fwd_str, (packet_num > 0 ? "aggregated " : ""),
508 batadv_ogm_packet->orig,
509 ntohl(batadv_ogm_packet->seqno),
510 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
511 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
513 hard_iface->net_dev->name,
514 hard_iface->net_dev->dev_addr);
516 buff_pos += BATADV_OGM_HLEN;
517 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
519 packet_pos = forw_packet->skb->data + buff_pos;
520 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
523 /* create clone because function is called more than once */
524 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
526 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
527 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
528 skb->len + ETH_HLEN);
529 batadv_send_broadcast_skb(skb, hard_iface);
533 /* send a batman ogm packet */
534 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
536 struct net_device *soft_iface;
538 if (!forw_packet->if_incoming) {
539 pr_err("Error - can't forward packet: incoming iface not specified\n");
543 soft_iface = forw_packet->if_incoming->soft_iface;
545 if (WARN_ON(!forw_packet->if_outgoing))
548 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
551 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
554 /* only for one specific outgoing interface */
555 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
559 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
560 * existing forward packet
561 * @new_bat_ogm_packet: OGM packet to be aggregated
562 * @bat_priv: the bat priv with all the soft interface information
563 * @packet_len: (total) length of the OGM
564 * @send_time: timestamp (jiffies) when the packet is to be sent
565 * @directlink: true if this is a direct link packet
566 * @if_incoming: interface where the packet was received
567 * @if_outgoing: interface for which the retransmission should be considered
568 * @forw_packet: the forwarded packet which should be checked
570 * Return: true if new_packet can be aggregated with forw_packet
573 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
574 struct batadv_priv *bat_priv,
575 int packet_len, unsigned long send_time,
577 const struct batadv_hard_iface *if_incoming,
578 const struct batadv_hard_iface *if_outgoing,
579 const struct batadv_forw_packet *forw_packet)
581 struct batadv_ogm_packet *batadv_ogm_packet;
582 int aggregated_bytes = forw_packet->packet_len + packet_len;
583 struct batadv_hard_iface *primary_if = NULL;
585 unsigned long aggregation_end_time;
587 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
588 aggregation_end_time = send_time;
589 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
591 /* we can aggregate the current packet to this aggregated packet
594 * - the send time is within our MAX_AGGREGATION_MS time
595 * - the resulting packet wont be bigger than
596 * MAX_AGGREGATION_BYTES
597 * otherwise aggregation is not possible
599 if (!time_before(send_time, forw_packet->send_time) ||
600 !time_after_eq(aggregation_end_time, forw_packet->send_time))
603 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
606 /* packet is not leaving on the same interface. */
607 if (forw_packet->if_outgoing != if_outgoing)
610 /* check aggregation compatibility
611 * -> direct link packets are broadcasted on
612 * their interface only
613 * -> aggregate packet if the current packet is
614 * a "global" packet as well as the base
617 primary_if = batadv_primary_if_get_selected(bat_priv);
621 /* packets without direct link flag and high TTL
622 * are flooded through the net
625 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
626 batadv_ogm_packet->ttl != 1 &&
628 /* own packets originating non-primary
629 * interfaces leave only that interface
631 (!forw_packet->own ||
632 forw_packet->if_incoming == primary_if)) {
637 /* if the incoming packet is sent via this one
638 * interface only - we still can aggregate
641 new_bat_ogm_packet->ttl == 1 &&
642 forw_packet->if_incoming == if_incoming &&
644 /* packets from direct neighbors or
645 * own secondary interface packets
646 * (= secondary interface packets in general)
648 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
650 forw_packet->if_incoming != primary_if))) {
657 batadv_hardif_put(primary_if);
662 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
664 * @packet_buff: pointer to the OGM
665 * @packet_len: (total) length of the OGM
666 * @send_time: timestamp (jiffies) when the packet is to be sent
667 * @direct_link: whether this OGM has direct link status
668 * @if_incoming: interface where the packet was received
669 * @if_outgoing: interface for which the retransmission should be considered
670 * @own_packet: true if it is a self-generated ogm
672 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
673 int packet_len, unsigned long send_time,
675 struct batadv_hard_iface *if_incoming,
676 struct batadv_hard_iface *if_outgoing,
679 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
680 struct batadv_forw_packet *forw_packet_aggr;
681 unsigned char *skb_buff;
682 unsigned int skb_size;
683 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
685 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
686 queue_left, bat_priv);
687 if (!forw_packet_aggr)
690 if (atomic_read(&bat_priv->aggregated_ogms) &&
691 packet_len < BATADV_MAX_AGGREGATION_BYTES)
692 skb_size = BATADV_MAX_AGGREGATION_BYTES;
694 skb_size = packet_len;
696 skb_size += ETH_HLEN;
698 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
699 if (!forw_packet_aggr->skb) {
700 batadv_forw_packet_free(forw_packet_aggr);
704 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
705 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
707 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
708 forw_packet_aggr->packet_len = packet_len;
709 memcpy(skb_buff, packet_buff, packet_len);
711 forw_packet_aggr->own = own_packet;
712 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
713 forw_packet_aggr->send_time = send_time;
715 /* save packet direct link flag status */
717 forw_packet_aggr->direct_link_flags |= 1;
719 /* add new packet to packet list */
720 spin_lock_bh(&bat_priv->forw_bat_list_lock);
721 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
722 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
724 /* start timer for this packet */
725 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
726 batadv_iv_send_outstanding_bat_ogm_packet);
727 queue_delayed_work(batadv_event_workqueue,
728 &forw_packet_aggr->delayed_work,
729 send_time - jiffies);
732 /* aggregate a new packet into the existing ogm packet */
733 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
734 const unsigned char *packet_buff,
735 int packet_len, bool direct_link)
737 unsigned char *skb_buff;
738 unsigned long new_direct_link_flag;
740 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
741 memcpy(skb_buff, packet_buff, packet_len);
742 forw_packet_aggr->packet_len += packet_len;
743 forw_packet_aggr->num_packets++;
745 /* save packet direct link flag status */
747 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
748 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
753 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
754 * @bat_priv: the bat priv with all the soft interface information
755 * @packet_buff: pointer to the OGM
756 * @packet_len: (total) length of the OGM
757 * @if_incoming: interface where the packet was received
758 * @if_outgoing: interface for which the retransmission should be considered
759 * @own_packet: true if it is a self-generated ogm
760 * @send_time: timestamp (jiffies) when the packet is to be sent
762 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
763 unsigned char *packet_buff,
765 struct batadv_hard_iface *if_incoming,
766 struct batadv_hard_iface *if_outgoing,
767 int own_packet, unsigned long send_time)
769 /* _aggr -> pointer to the packet we want to aggregate with
770 * _pos -> pointer to the position in the queue
772 struct batadv_forw_packet *forw_packet_aggr = NULL;
773 struct batadv_forw_packet *forw_packet_pos = NULL;
774 struct batadv_ogm_packet *batadv_ogm_packet;
776 unsigned long max_aggregation_jiffies;
778 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
779 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
780 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
782 /* find position for the packet in the forward queue */
783 spin_lock_bh(&bat_priv->forw_bat_list_lock);
784 /* own packets are not to be aggregated */
785 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
786 hlist_for_each_entry(forw_packet_pos,
787 &bat_priv->forw_bat_list, list) {
788 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
789 bat_priv, packet_len,
790 send_time, direct_link,
794 forw_packet_aggr = forw_packet_pos;
800 /* nothing to aggregate with - either aggregation disabled or no
801 * suitable aggregation packet found
803 if (!forw_packet_aggr) {
804 /* the following section can run without the lock */
805 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
807 /* if we could not aggregate this packet with one of the others
808 * we hold it back for a while, so that it might be aggregated
811 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
812 send_time += max_aggregation_jiffies;
814 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
815 send_time, direct_link,
816 if_incoming, if_outgoing,
819 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
820 packet_len, direct_link);
821 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
825 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
826 const struct ethhdr *ethhdr,
827 struct batadv_ogm_packet *batadv_ogm_packet,
828 bool is_single_hop_neigh,
829 bool is_from_best_next_hop,
830 struct batadv_hard_iface *if_incoming,
831 struct batadv_hard_iface *if_outgoing)
833 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
836 if (batadv_ogm_packet->ttl <= 1) {
837 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
841 if (!is_from_best_next_hop) {
842 /* Mark the forwarded packet when it is not coming from our
843 * best next hop. We still need to forward the packet for our
844 * neighbor link quality detection to work in case the packet
845 * originated from a single hop neighbor. Otherwise we can
846 * simply drop the ogm.
848 if (is_single_hop_neigh)
849 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
854 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
856 batadv_ogm_packet->ttl--;
857 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
859 /* apply hop penalty */
860 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
863 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
864 "Forwarding packet: tq: %i, ttl: %i\n",
865 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
867 if (is_single_hop_neigh)
868 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
870 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
872 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
873 BATADV_OGM_HLEN + tvlv_len,
874 if_incoming, if_outgoing, 0,
875 batadv_iv_ogm_fwd_send_time());
879 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
880 * the given interface
881 * @hard_iface: the interface for which the windows have to be shifted
884 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
886 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
887 struct batadv_hashtable *hash = bat_priv->orig_hash;
888 struct hlist_head *head;
889 struct batadv_orig_node *orig_node;
896 for (i = 0; i < hash->size; i++) {
897 head = &hash->table[i];
900 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
901 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
902 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
903 word = &orig_node->bat_iv.bcast_own[word_index];
905 batadv_bit_get_packet(bat_priv, word, 1, 0);
906 if_num = hard_iface->if_num;
907 w = &orig_node->bat_iv.bcast_own_sum[if_num];
908 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
909 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
915 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
917 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
918 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
919 struct batadv_ogm_packet *batadv_ogm_packet;
920 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
921 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
924 unsigned long send_time;
926 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
927 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
930 /* the interface gets activated here to avoid race conditions between
931 * the moment of activating the interface in
932 * hardif_activate_interface() where the originator mac is set and
933 * outdated packets (especially uninitialized mac addresses) in the
936 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
937 hard_iface->if_status = BATADV_IF_ACTIVE;
939 primary_if = batadv_primary_if_get_selected(bat_priv);
941 if (hard_iface == primary_if) {
942 /* tt changes have to be committed before the tvlv data is
943 * appended as it may alter the tt tvlv container
945 batadv_tt_local_commit_changes(bat_priv);
946 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
951 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
952 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
954 /* change sequence number to network order */
955 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
956 batadv_ogm_packet->seqno = htonl(seqno);
957 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
959 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
961 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
963 if (hard_iface != primary_if) {
964 /* OGMs from secondary interfaces are only scheduled on their
965 * respective interfaces.
967 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
968 hard_iface, hard_iface, 1, send_time);
972 /* OGMs from primary interfaces are scheduled on all
976 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
977 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
980 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
983 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
984 *ogm_buff_len, hard_iface,
985 tmp_hard_iface, 1, send_time);
987 batadv_hardif_put(tmp_hard_iface);
993 batadv_hardif_put(primary_if);
997 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
999 * @bat_priv: the bat priv with all the soft interface information
1000 * @orig_node: the orig node who originally emitted the ogm packet
1001 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
1002 * @ethhdr: Ethernet header of the OGM
1003 * @batadv_ogm_packet: the ogm packet
1004 * @if_incoming: interface where the packet was received
1005 * @if_outgoing: interface for which the retransmission should be considered
1006 * @dup_status: the duplicate status of this ogm packet.
1009 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1010 struct batadv_orig_node *orig_node,
1011 struct batadv_orig_ifinfo *orig_ifinfo,
1012 const struct ethhdr *ethhdr,
1013 const struct batadv_ogm_packet *batadv_ogm_packet,
1014 struct batadv_hard_iface *if_incoming,
1015 struct batadv_hard_iface *if_outgoing,
1016 enum batadv_dup_status dup_status)
1018 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1019 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1020 struct batadv_neigh_node *neigh_node = NULL;
1021 struct batadv_neigh_node *tmp_neigh_node = NULL;
1022 struct batadv_neigh_node *router = NULL;
1023 struct batadv_orig_node *orig_node_tmp;
1025 u8 sum_orig, sum_neigh;
1029 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1030 "update_originator(): Searching and updating originator entry of received packet\n");
1033 hlist_for_each_entry_rcu(tmp_neigh_node,
1034 &orig_node->neigh_list, list) {
1035 neigh_addr = tmp_neigh_node->addr;
1036 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1037 tmp_neigh_node->if_incoming == if_incoming &&
1038 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1039 if (WARN(neigh_node, "too many matching neigh_nodes"))
1040 batadv_neigh_node_put(neigh_node);
1041 neigh_node = tmp_neigh_node;
1045 if (dup_status != BATADV_NO_DUP)
1048 /* only update the entry for this outgoing interface */
1049 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1054 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1055 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1056 &neigh_ifinfo->bat_iv.tq_index, 0);
1057 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1058 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1059 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1061 batadv_neigh_ifinfo_put(neigh_ifinfo);
1062 neigh_ifinfo = NULL;
1066 struct batadv_orig_node *orig_tmp;
1068 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1072 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1074 orig_node, orig_tmp);
1076 batadv_orig_node_put(orig_tmp);
1080 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1081 "Updating existing last-hop neighbor of originator\n");
1085 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1089 neigh_node->last_seen = jiffies;
1091 spin_lock_bh(&neigh_node->ifinfo_lock);
1092 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1093 &neigh_ifinfo->bat_iv.tq_index,
1094 batadv_ogm_packet->tq);
1095 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1096 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1097 spin_unlock_bh(&neigh_node->ifinfo_lock);
1099 if (dup_status == BATADV_NO_DUP) {
1100 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1101 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1104 /* if this neighbor already is our next hop there is nothing
1107 router = batadv_orig_router_get(orig_node, if_outgoing);
1108 if (router == neigh_node)
1112 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1116 /* if this neighbor does not offer a better TQ we won't
1119 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1123 /* if the TQ is the same and the link not more symmetric we
1124 * won't consider it either
1126 if (router_ifinfo &&
1127 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1128 orig_node_tmp = router->orig_node;
1129 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1130 if_num = router->if_incoming->if_num;
1131 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1132 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1134 orig_node_tmp = neigh_node->orig_node;
1135 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1136 if_num = neigh_node->if_incoming->if_num;
1137 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1138 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1140 if (sum_orig >= sum_neigh)
1144 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1151 batadv_neigh_node_put(neigh_node);
1153 batadv_neigh_node_put(router);
1155 batadv_neigh_ifinfo_put(neigh_ifinfo);
1157 batadv_neigh_ifinfo_put(router_ifinfo);
1161 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1162 * @orig_node: the orig node who originally emitted the ogm packet
1163 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1164 * @batadv_ogm_packet: the ogm packet
1165 * @if_incoming: interface where the packet was received
1166 * @if_outgoing: interface for which the retransmission should be considered
1168 * Return: true if the link can be considered bidirectional, false otherwise
1170 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1171 struct batadv_orig_node *orig_neigh_node,
1172 struct batadv_ogm_packet *batadv_ogm_packet,
1173 struct batadv_hard_iface *if_incoming,
1174 struct batadv_hard_iface *if_outgoing)
1176 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1177 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1178 struct batadv_neigh_ifinfo *neigh_ifinfo;
1180 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1181 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1183 unsigned int tq_asym_penalty, inv_asym_penalty;
1184 unsigned int combined_tq;
1185 unsigned int tq_iface_penalty;
1188 /* find corresponding one hop neighbor */
1190 hlist_for_each_entry_rcu(tmp_neigh_node,
1191 &orig_neigh_node->neigh_list, list) {
1192 if (!batadv_compare_eth(tmp_neigh_node->addr,
1193 orig_neigh_node->orig))
1196 if (tmp_neigh_node->if_incoming != if_incoming)
1199 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1202 neigh_node = tmp_neigh_node;
1208 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1209 orig_neigh_node->orig,
1216 /* if orig_node is direct neighbor update neigh_node last_seen */
1217 if (orig_node == orig_neigh_node)
1218 neigh_node->last_seen = jiffies;
1220 orig_node->last_seen = jiffies;
1222 /* find packet count of corresponding one hop neighbor */
1223 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1224 if_num = if_incoming->if_num;
1225 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1226 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1228 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1229 batadv_neigh_ifinfo_put(neigh_ifinfo);
1233 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1235 /* pay attention to not get a value bigger than 100 % */
1236 if (orig_eq_count > neigh_rq_count)
1237 total_count = neigh_rq_count;
1239 total_count = orig_eq_count;
1241 /* if we have too few packets (too less data) we set tq_own to zero
1242 * if we receive too few packets it is not considered bidirectional
1244 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1245 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1248 /* neigh_node->real_packet_count is never zero as we
1249 * only purge old information when getting new
1252 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1254 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1255 * affect the nearly-symmetric links only a little, but
1256 * punishes asymmetric links more. This will give a value
1257 * between 0 and TQ_MAX_VALUE
1259 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1260 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1261 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1262 BATADV_TQ_LOCAL_WINDOW_SIZE *
1263 BATADV_TQ_LOCAL_WINDOW_SIZE;
1264 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1265 inv_asym_penalty /= neigh_rq_max_cube;
1266 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1268 /* penalize if the OGM is forwarded on the same interface. WiFi
1269 * interfaces and other half duplex devices suffer from throughput
1270 * drops as they can't send and receive at the same time.
1272 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1273 if (if_outgoing && (if_incoming == if_outgoing) &&
1274 batadv_is_wifi_netdev(if_outgoing->net_dev))
1275 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1278 combined_tq = batadv_ogm_packet->tq *
1282 combined_tq /= BATADV_TQ_MAX_VALUE *
1283 BATADV_TQ_MAX_VALUE *
1284 BATADV_TQ_MAX_VALUE;
1285 batadv_ogm_packet->tq = combined_tq;
1287 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1288 "bidirectional: orig = %-15pM neigh = %-15pM => 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",
1289 orig_node->orig, orig_neigh_node->orig, total_count,
1290 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1291 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1292 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1294 /* if link has the minimum required transmission quality
1295 * consider it bidirectional
1297 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1302 batadv_neigh_node_put(neigh_node);
1307 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1308 * adjust the sequence number and find out whether it is a duplicate
1309 * @ethhdr: ethernet header of the packet
1310 * @batadv_ogm_packet: OGM packet to be considered
1311 * @if_incoming: interface on which the OGM packet was received
1312 * @if_outgoing: interface for which the retransmission should be considered
1314 * Return: duplicate status as enum batadv_dup_status
1316 static enum batadv_dup_status
1317 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1318 const struct batadv_ogm_packet *batadv_ogm_packet,
1319 const struct batadv_hard_iface *if_incoming,
1320 struct batadv_hard_iface *if_outgoing)
1322 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1323 struct batadv_orig_node *orig_node;
1324 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1325 struct batadv_neigh_node *neigh_node;
1326 struct batadv_neigh_ifinfo *neigh_ifinfo;
1329 bool need_update = false;
1331 enum batadv_dup_status ret = BATADV_NO_DUP;
1332 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1335 unsigned long *bitmap;
1337 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1339 return BATADV_NO_DUP;
1341 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1342 if (WARN_ON(!orig_ifinfo)) {
1343 batadv_orig_node_put(orig_node);
1347 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1348 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1350 /* signalize caller that the packet is to be dropped. */
1351 if (!hlist_empty(&orig_node->neigh_list) &&
1352 batadv_window_protected(bat_priv, seq_diff,
1353 BATADV_TQ_LOCAL_WINDOW_SIZE,
1354 &orig_ifinfo->batman_seqno_reset, NULL)) {
1355 ret = BATADV_PROTECTED;
1360 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1361 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1366 neigh_addr = neigh_node->addr;
1367 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1368 orig_ifinfo->last_real_seqno,
1371 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1372 neigh_node->if_incoming == if_incoming) {
1375 ret = BATADV_NEIGH_DUP;
1378 if (is_dup && (ret != BATADV_NEIGH_DUP))
1379 ret = BATADV_ORIG_DUP;
1382 /* if the window moved, set the update flag. */
1383 bitmap = neigh_ifinfo->bat_iv.real_bits;
1384 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1385 seq_diff, set_mark);
1387 packet_count = bitmap_weight(bitmap,
1388 BATADV_TQ_LOCAL_WINDOW_SIZE);
1389 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1390 batadv_neigh_ifinfo_put(neigh_ifinfo);
1395 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1396 "%s updating last_seqno: old %u, new %u\n",
1397 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1398 orig_ifinfo->last_real_seqno, seqno);
1399 orig_ifinfo->last_real_seqno = seqno;
1403 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1404 batadv_orig_node_put(orig_node);
1405 batadv_orig_ifinfo_put(orig_ifinfo);
1410 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1411 * @skb: the skb containing the OGM
1412 * @ogm_offset: offset from skb->data to start of ogm header
1413 * @orig_node: the (cached) orig node for the originator of this OGM
1414 * @if_incoming: the interface where this packet was received
1415 * @if_outgoing: the interface for which the packet should be considered
1418 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1419 struct batadv_orig_node *orig_node,
1420 struct batadv_hard_iface *if_incoming,
1421 struct batadv_hard_iface *if_outgoing)
1423 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1424 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1425 struct batadv_neigh_node *router = NULL;
1426 struct batadv_neigh_node *router_router = NULL;
1427 struct batadv_orig_node *orig_neigh_node;
1428 struct batadv_orig_ifinfo *orig_ifinfo;
1429 struct batadv_neigh_node *orig_neigh_router = NULL;
1430 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1431 struct batadv_ogm_packet *ogm_packet;
1432 enum batadv_dup_status dup_status;
1433 bool is_from_best_next_hop = false;
1434 bool is_single_hop_neigh = false;
1435 bool sameseq, similar_ttl;
1436 struct sk_buff *skb_priv;
1437 struct ethhdr *ethhdr;
1441 /* create a private copy of the skb, as some functions change tq value
1444 skb_priv = skb_copy(skb, GFP_ATOMIC);
1448 ethhdr = eth_hdr(skb_priv);
1449 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1451 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1452 if_incoming, if_outgoing);
1453 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1454 is_single_hop_neigh = true;
1456 if (dup_status == BATADV_PROTECTED) {
1457 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1458 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1463 if (ogm_packet->tq == 0) {
1464 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1465 "Drop packet: originator packet with tq equal 0\n");
1469 if (is_single_hop_neigh) {
1470 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1473 hardif_neigh->last_seen = jiffies;
1476 router = batadv_orig_router_get(orig_node, if_outgoing);
1478 router_router = batadv_orig_router_get(router->orig_node,
1480 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1483 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1484 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1485 is_from_best_next_hop = true;
1487 prev_sender = ogm_packet->prev_sender;
1488 /* avoid temporary routing loops */
1489 if (router && router_router &&
1490 (batadv_compare_eth(router->addr, prev_sender)) &&
1491 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1492 (batadv_compare_eth(router->addr, router_router->addr))) {
1493 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1494 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1499 if (if_outgoing == BATADV_IF_DEFAULT)
1500 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1502 /* if sender is a direct neighbor the sender mac equals
1505 if (is_single_hop_neigh)
1506 orig_neigh_node = orig_node;
1508 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1511 if (!orig_neigh_node)
1514 /* Update nc_nodes of the originator */
1515 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1516 ogm_packet, is_single_hop_neigh);
1518 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1521 /* drop packet if sender is not a direct neighbor and if we
1522 * don't route towards it
1524 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1525 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1526 "Drop packet: OGM via unknown neighbor!\n");
1530 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1531 ogm_packet, if_incoming,
1534 /* update ranking if it is not a duplicate or has the same
1535 * seqno and similar ttl as the non-duplicate
1537 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1541 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1542 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1544 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1545 (sameseq && similar_ttl))) {
1546 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1547 orig_ifinfo, ethhdr,
1548 ogm_packet, if_incoming,
1549 if_outgoing, dup_status);
1551 batadv_orig_ifinfo_put(orig_ifinfo);
1553 /* only forward for specific interface, not for the default one. */
1554 if (if_outgoing == BATADV_IF_DEFAULT)
1557 /* is single hop (direct) neighbor */
1558 if (is_single_hop_neigh) {
1559 /* OGMs from secondary interfaces should only scheduled once
1560 * per interface where it has been received, not multiple times
1562 if ((ogm_packet->ttl <= 2) &&
1563 (if_incoming != if_outgoing)) {
1564 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1565 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1568 /* mark direct link on incoming interface */
1569 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1570 is_single_hop_neigh,
1571 is_from_best_next_hop, if_incoming,
1574 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1575 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1579 /* multihop originator */
1581 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1582 "Drop packet: not received via bidirectional link\n");
1586 if (dup_status == BATADV_NEIGH_DUP) {
1587 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1588 "Drop packet: duplicate packet received\n");
1592 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1593 "Forwarding packet: rebroadcast originator packet\n");
1594 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1595 is_single_hop_neigh, is_from_best_next_hop,
1596 if_incoming, if_outgoing);
1599 if ((orig_neigh_node) && (!is_single_hop_neigh))
1600 batadv_orig_node_put(orig_neigh_node);
1603 batadv_neigh_ifinfo_put(router_ifinfo);
1605 batadv_neigh_node_put(router);
1607 batadv_neigh_node_put(router_router);
1608 if (orig_neigh_router)
1609 batadv_neigh_node_put(orig_neigh_router);
1611 batadv_hardif_neigh_put(hardif_neigh);
1613 kfree_skb(skb_priv);
1617 * batadv_iv_ogm_process - process an incoming batman iv OGM
1618 * @skb: the skb containing the OGM
1619 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1620 * @if_incoming: the interface where this packet was receved
1622 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1623 struct batadv_hard_iface *if_incoming)
1625 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1626 struct batadv_orig_node *orig_neigh_node, *orig_node;
1627 struct batadv_hard_iface *hard_iface;
1628 struct batadv_ogm_packet *ogm_packet;
1629 u32 if_incoming_seqno;
1630 bool has_directlink_flag;
1631 struct ethhdr *ethhdr;
1632 bool is_my_oldorig = false;
1633 bool is_my_addr = false;
1634 bool is_my_orig = false;
1636 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1637 ethhdr = eth_hdr(skb);
1639 /* Silently drop when the batman packet is actually not a
1642 * This might happen if a packet is padded (e.g. Ethernet has a
1643 * minimum frame length of 64 byte) and the aggregation interprets
1644 * it as an additional length.
1646 * TODO: A more sane solution would be to have a bit in the
1647 * batadv_ogm_packet to detect whether the packet is the last
1648 * packet in an aggregation. Here we expect that the padding
1649 * is always zero (or not 0x01)
1651 if (ogm_packet->packet_type != BATADV_IV_OGM)
1654 /* could be changed by schedule_own_packet() */
1655 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1657 if (ogm_packet->flags & BATADV_DIRECTLINK)
1658 has_directlink_flag = true;
1660 has_directlink_flag = false;
1662 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1663 "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",
1664 ethhdr->h_source, if_incoming->net_dev->name,
1665 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1666 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1667 ogm_packet->tq, ogm_packet->ttl,
1668 ogm_packet->version, has_directlink_flag);
1671 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1672 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1675 if (hard_iface->soft_iface != if_incoming->soft_iface)
1678 if (batadv_compare_eth(ethhdr->h_source,
1679 hard_iface->net_dev->dev_addr))
1682 if (batadv_compare_eth(ogm_packet->orig,
1683 hard_iface->net_dev->dev_addr))
1686 if (batadv_compare_eth(ogm_packet->prev_sender,
1687 hard_iface->net_dev->dev_addr))
1688 is_my_oldorig = true;
1693 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1694 "Drop packet: received my own broadcast (sender: %pM)\n",
1700 unsigned long *word;
1706 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1708 if (!orig_neigh_node)
1711 /* neighbor has to indicate direct link and it has to
1712 * come via the corresponding interface
1713 * save packet seqno for bidirectional check
1715 if (has_directlink_flag &&
1716 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1717 ogm_packet->orig)) {
1718 if_num = if_incoming->if_num;
1719 offset = if_num * BATADV_NUM_WORDS;
1721 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1722 word = &orig_neigh_node->bat_iv.bcast_own[offset];
1723 bit_pos = if_incoming_seqno - 2;
1724 bit_pos -= ntohl(ogm_packet->seqno);
1725 batadv_set_bit(word, bit_pos);
1726 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1727 *weight = bitmap_weight(word,
1728 BATADV_TQ_LOCAL_WINDOW_SIZE);
1729 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1732 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1733 "Drop packet: originator packet from myself (via neighbor)\n");
1734 batadv_orig_node_put(orig_neigh_node);
1738 if (is_my_oldorig) {
1739 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1740 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1745 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1746 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1747 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1752 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1756 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1757 if_incoming, BATADV_IF_DEFAULT);
1760 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1761 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1764 if (hard_iface->soft_iface != bat_priv->soft_iface)
1767 if (!kref_get_unless_zero(&hard_iface->refcount))
1770 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1771 if_incoming, hard_iface);
1773 batadv_hardif_put(hard_iface);
1777 batadv_orig_node_put(orig_node);
1780 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1782 struct delayed_work *delayed_work;
1783 struct batadv_forw_packet *forw_packet;
1784 struct batadv_priv *bat_priv;
1786 delayed_work = to_delayed_work(work);
1787 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1789 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1790 spin_lock_bh(&bat_priv->forw_bat_list_lock);
1791 hlist_del(&forw_packet->list);
1792 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
1794 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1797 batadv_iv_ogm_emit(forw_packet);
1799 /* we have to have at least one packet in the queue to determine the
1800 * queues wake up time unless we are shutting down.
1802 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1803 * primary interface should only be rescheduled once per period, but
1804 * this function will be called for the forw_packet instances of the
1805 * other secondary interfaces as well.
1807 if (forw_packet->own &&
1808 forw_packet->if_incoming == forw_packet->if_outgoing)
1809 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1812 batadv_forw_packet_free(forw_packet);
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;
1824 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1828 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1829 * that does not have B.A.T.M.A.N. IV enabled ?
1831 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1834 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1835 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1836 skb->len + ETH_HLEN);
1839 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1841 /* unpack the aggregated packets and process them one by one */
1842 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1843 ogm_packet->tvlv_len)) {
1844 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1846 ogm_offset += BATADV_OGM_HLEN;
1847 ogm_offset += ntohs(ogm_packet->tvlv_len);
1849 packet_pos = skb->data + ogm_offset;
1850 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1854 return NET_RX_SUCCESS;
1858 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1859 * @orig_node: the orig_node for which the neighbors are printed
1860 * @if_outgoing: outgoing interface for these entries
1861 * @seq: debugfs table seq_file struct
1863 * Must be called while holding an rcu lock.
1866 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1867 struct batadv_hard_iface *if_outgoing,
1868 struct seq_file *seq)
1870 struct batadv_neigh_node *neigh_node;
1871 struct batadv_neigh_ifinfo *n_ifinfo;
1873 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1874 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1878 seq_printf(seq, " %pM (%3i)",
1880 n_ifinfo->bat_iv.tq_avg);
1882 batadv_neigh_ifinfo_put(n_ifinfo);
1887 * batadv_iv_ogm_orig_print - print the originator table
1888 * @bat_priv: the bat priv with all the soft interface information
1889 * @seq: debugfs table seq_file struct
1890 * @if_outgoing: the outgoing interface for which this should be printed
1892 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1893 struct seq_file *seq,
1894 struct batadv_hard_iface *if_outgoing)
1896 struct batadv_neigh_node *neigh_node;
1897 struct batadv_hashtable *hash = bat_priv->orig_hash;
1898 int last_seen_msecs, last_seen_secs;
1899 struct batadv_orig_node *orig_node;
1900 struct batadv_neigh_ifinfo *n_ifinfo;
1901 unsigned long last_seen_jiffies;
1902 struct hlist_head *head;
1903 int batman_count = 0;
1907 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1909 for (i = 0; i < hash->size; i++) {
1910 head = &hash->table[i];
1913 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1914 neigh_node = batadv_orig_router_get(orig_node,
1919 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1924 if (n_ifinfo->bat_iv.tq_avg == 0)
1927 last_seen_jiffies = jiffies - orig_node->last_seen;
1928 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1929 last_seen_secs = last_seen_msecs / 1000;
1930 last_seen_msecs = last_seen_msecs % 1000;
1932 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1933 orig_node->orig, last_seen_secs,
1934 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1936 neigh_node->if_incoming->net_dev->name);
1938 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1940 seq_puts(seq, "\n");
1944 batadv_neigh_node_put(neigh_node);
1946 batadv_neigh_ifinfo_put(n_ifinfo);
1951 if (batman_count == 0)
1952 seq_puts(seq, "No batman nodes in range ...\n");
1956 * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
1957 * given outgoing interface.
1958 * @neigh_node: Neighbour of interest
1959 * @if_outgoing: Outgoing interface of interest
1960 * @tq_avg: Pointer of where to store the TQ average
1962 * Return: False if no average TQ available, otherwise true.
1965 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1966 struct batadv_hard_iface *if_outgoing,
1969 struct batadv_neigh_ifinfo *n_ifinfo;
1971 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1975 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1976 batadv_neigh_ifinfo_put(n_ifinfo);
1982 * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
1984 * @msg: Netlink message to dump into
1985 * @portid: Port making netlink request
1986 * @seq: Sequence number of netlink message
1987 * @bat_priv: The bat priv with all the soft interface information
1988 * @if_outgoing: Limit dump to entries with this outgoing interface
1989 * @orig_node: Originator to dump
1990 * @neigh_node: Single hops neighbour
1991 * @best: Is the best originator
1993 * Return: Error code, or 0 on success
1996 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1997 struct batadv_priv *bat_priv,
1998 struct batadv_hard_iface *if_outgoing,
1999 struct batadv_orig_node *orig_node,
2000 struct batadv_neigh_node *neigh_node,
2005 unsigned int last_seen_msecs;
2007 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2009 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2012 if (if_outgoing != BATADV_IF_DEFAULT &&
2013 if_outgoing != neigh_node->if_incoming)
2016 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2017 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2021 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2023 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2024 neigh_node->addr) ||
2025 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2026 neigh_node->if_incoming->net_dev->ifindex) ||
2027 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2028 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2030 goto nla_put_failure;
2032 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2033 goto nla_put_failure;
2035 genlmsg_end(msg, hdr);
2039 genlmsg_cancel(msg, hdr);
2044 * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
2045 * @msg: Netlink message to dump into
2046 * @portid: Port making netlink request
2047 * @seq: Sequence number of netlink message
2048 * @bat_priv: The bat priv with all the soft interface information
2049 * @if_outgoing: Limit dump to entries with this outgoing interface
2050 * @orig_node: Originator to dump
2051 * @sub_s: Number of sub entries to skip
2053 * This function assumes the caller holds rcu_read_lock().
2055 * Return: Error code, or 0 on success
2058 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2059 struct batadv_priv *bat_priv,
2060 struct batadv_hard_iface *if_outgoing,
2061 struct batadv_orig_node *orig_node, int *sub_s)
2063 struct batadv_neigh_node *neigh_node_best;
2064 struct batadv_neigh_node *neigh_node;
2069 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2070 if (!neigh_node_best)
2073 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2077 if (tq_avg_best == 0)
2080 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2084 best = (neigh_node == neigh_node_best);
2086 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2087 bat_priv, if_outgoing,
2088 orig_node, neigh_node,
2090 batadv_neigh_node_put(neigh_node_best);
2098 if (neigh_node_best)
2099 batadv_neigh_node_put(neigh_node_best);
2106 * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
2108 * @msg: Netlink message to dump into
2109 * @portid: Port making netlink request
2110 * @seq: Sequence number of netlink message
2111 * @bat_priv: The bat priv with all the soft interface information
2112 * @if_outgoing: Limit dump to entries with this outgoing interface
2113 * @head: Bucket to be dumped
2114 * @idx_s: Number of entries to be skipped
2115 * @sub: Number of sub entries to be skipped
2117 * Return: Error code, or 0 on success
2120 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2121 struct batadv_priv *bat_priv,
2122 struct batadv_hard_iface *if_outgoing,
2123 struct hlist_head *head, int *idx_s, int *sub)
2125 struct batadv_orig_node *orig_node;
2129 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2133 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2134 if_outgoing, orig_node,
2149 * batadv_iv_ogm_orig_dump - Dump the originators into a message
2150 * @msg: Netlink message to dump into
2151 * @cb: Control block containing additional options
2152 * @bat_priv: The bat priv with all the soft interface information
2153 * @if_outgoing: Limit dump to entries with this outgoing interface
2156 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2157 struct batadv_priv *bat_priv,
2158 struct batadv_hard_iface *if_outgoing)
2160 struct batadv_hashtable *hash = bat_priv->orig_hash;
2161 struct hlist_head *head;
2162 int bucket = cb->args[0];
2163 int idx = cb->args[1];
2164 int sub = cb->args[2];
2165 int portid = NETLINK_CB(cb->skb).portid;
2167 while (bucket < hash->size) {
2168 head = &hash->table[bucket];
2170 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2172 bat_priv, if_outgoing, head,
2179 cb->args[0] = bucket;
2185 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
2186 * @seq: neighbour table seq_file struct
2187 * @hardif_neigh: hardif neighbour information
2190 batadv_iv_hardif_neigh_print(struct seq_file *seq,
2191 struct batadv_hardif_neigh_node *hardif_neigh)
2193 int last_secs, last_msecs;
2195 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2196 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2198 seq_printf(seq, " %10s %pM %4i.%03is\n",
2199 hardif_neigh->if_incoming->net_dev->name,
2200 hardif_neigh->addr, last_secs, last_msecs);
2204 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
2205 * @bat_priv: the bat priv with all the soft interface information
2206 * @seq: neighbour table seq_file struct
2208 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2209 struct seq_file *seq)
2211 struct net_device *net_dev = (struct net_device *)seq->private;
2212 struct batadv_hardif_neigh_node *hardif_neigh;
2213 struct batadv_hard_iface *hard_iface;
2214 int batman_count = 0;
2216 seq_puts(seq, " IF Neighbor last-seen\n");
2219 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2220 if (hard_iface->soft_iface != net_dev)
2223 hlist_for_each_entry_rcu(hardif_neigh,
2224 &hard_iface->neigh_list, list) {
2225 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2231 if (batman_count == 0)
2232 seq_puts(seq, "No batman nodes in range ...\n");
2236 * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors
2237 * @neigh1: the first neighbor object of the comparison
2238 * @if_outgoing1: outgoing interface for the first neighbor
2239 * @neigh2: the second neighbor object of the comparison
2240 * @if_outgoing2: outgoing interface for the second neighbor
2241 * @diff: pointer to integer receiving the calculated difference
2243 * The content of *@diff is only valid when this function returns true.
2244 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2245 * the same as or higher than the metric via neigh2
2247 * Return: true when the difference could be calculated, false otherwise
2249 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2250 struct batadv_hard_iface *if_outgoing1,
2251 struct batadv_neigh_node *neigh2,
2252 struct batadv_hard_iface *if_outgoing2,
2255 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2259 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2260 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2262 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2267 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2268 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2269 *diff = (int)tq1 - (int)tq2;
2273 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2275 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2281 * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
2282 * @msg: Netlink message to dump into
2283 * @portid: Port making netlink request
2284 * @seq: Sequence number of netlink message
2285 * @hardif_neigh: Neighbour to be dumped
2287 * Return: Error code, or 0 on success
2290 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2291 struct batadv_hardif_neigh_node *hardif_neigh)
2294 unsigned int last_seen_msecs;
2296 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2298 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2299 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2303 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2304 hardif_neigh->addr) ||
2305 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2306 hardif_neigh->if_incoming->net_dev->ifindex) ||
2307 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2309 goto nla_put_failure;
2311 genlmsg_end(msg, hdr);
2315 genlmsg_cancel(msg, hdr);
2320 * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
2322 * @msg: Netlink message to dump into
2323 * @portid: Port making netlink request
2324 * @seq: Sequence number of netlink message
2325 * @bat_priv: The bat priv with all the soft interface information
2326 * @hard_iface: Hard interface to dump the neighbours for
2327 * @idx_s: Number of entries to skip
2329 * This function assumes the caller holds rcu_read_lock().
2331 * Return: Error code, or 0 on success
2334 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2335 struct batadv_priv *bat_priv,
2336 struct batadv_hard_iface *hard_iface,
2339 struct batadv_hardif_neigh_node *hardif_neigh;
2342 hlist_for_each_entry_rcu(hardif_neigh,
2343 &hard_iface->neigh_list, list) {
2347 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2359 * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
2360 * @msg: Netlink message to dump into
2361 * @cb: Control block containing additional options
2362 * @bat_priv: The bat priv with all the soft interface information
2363 * @single_hardif: Limit dump to this hard interfaace
2366 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2367 struct batadv_priv *bat_priv,
2368 struct batadv_hard_iface *single_hardif)
2370 struct batadv_hard_iface *hard_iface;
2372 int i_hardif_s = cb->args[0];
2373 int idx = cb->args[1];
2374 int portid = NETLINK_CB(cb->skb).portid;
2377 if (single_hardif) {
2378 if (i_hardif_s == 0) {
2379 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2387 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2389 if (hard_iface->soft_iface != bat_priv->soft_iface)
2392 if (i_hardif++ < i_hardif_s)
2395 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2398 hard_iface, &idx)) {
2406 cb->args[0] = i_hardif;
2411 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2412 * @neigh1: the first neighbor object of the comparison
2413 * @if_outgoing1: outgoing interface for the first neighbor
2414 * @neigh2: the second neighbor object of the comparison
2415 * @if_outgoing2: outgoing interface for the second neighbor
2417 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2418 * lower, the same as or higher than the metric via neigh2
2420 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2421 struct batadv_hard_iface *if_outgoing1,
2422 struct batadv_neigh_node *neigh2,
2423 struct batadv_hard_iface *if_outgoing2)
2428 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2429 if_outgoing2, &diff);
2437 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2438 * than neigh2 from the metric prospective
2439 * @neigh1: the first neighbor object of the comparison
2440 * @if_outgoing1: outgoing interface for the first neighbor
2441 * @neigh2: the second neighbor object of the comparison
2442 * @if_outgoing2: outgoing interface for the second neighbor
2444 * Return: true if the metric via neigh1 is equally good or better than
2445 * the metric via neigh2, false otherwise.
2448 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2449 struct batadv_hard_iface *if_outgoing1,
2450 struct batadv_neigh_node *neigh2,
2451 struct batadv_hard_iface *if_outgoing2)
2456 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2457 if_outgoing2, &diff);
2461 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2465 static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2467 /* begin scheduling originator messages on that interface */
2468 batadv_iv_ogm_schedule(hard_iface);
2471 static struct batadv_gw_node *
2472 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2474 struct batadv_neigh_node *router;
2475 struct batadv_neigh_ifinfo *router_ifinfo;
2476 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2477 u64 max_gw_factor = 0;
2478 u64 tmp_gw_factor = 0;
2481 struct batadv_orig_node *orig_node;
2484 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
2485 orig_node = gw_node->orig_node;
2486 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2490 router_ifinfo = batadv_neigh_ifinfo_get(router,
2495 if (!kref_get_unless_zero(&gw_node->refcount))
2498 tq_avg = router_ifinfo->bat_iv.tq_avg;
2500 switch (atomic_read(&bat_priv->gw.sel_class)) {
2501 case 1: /* fast connection */
2502 tmp_gw_factor = tq_avg * tq_avg;
2503 tmp_gw_factor *= gw_node->bandwidth_down;
2504 tmp_gw_factor *= 100 * 100;
2505 tmp_gw_factor >>= 18;
2507 if ((tmp_gw_factor > max_gw_factor) ||
2508 ((tmp_gw_factor == max_gw_factor) &&
2509 (tq_avg > max_tq))) {
2511 batadv_gw_node_put(curr_gw);
2513 kref_get(&curr_gw->refcount);
2517 default: /* 2: stable connection (use best statistic)
2518 * 3: fast-switch (use best statistic but change as
2519 * soon as a better gateway appears)
2520 * XX: late-switch (use best statistic but change as
2521 * soon as a better gateway appears which has
2522 * $routing_class more tq points)
2524 if (tq_avg > max_tq) {
2526 batadv_gw_node_put(curr_gw);
2528 kref_get(&curr_gw->refcount);
2533 if (tq_avg > max_tq)
2536 if (tmp_gw_factor > max_gw_factor)
2537 max_gw_factor = tmp_gw_factor;
2539 batadv_gw_node_put(gw_node);
2542 batadv_neigh_node_put(router);
2544 batadv_neigh_ifinfo_put(router_ifinfo);
2551 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2552 struct batadv_orig_node *curr_gw_orig,
2553 struct batadv_orig_node *orig_node)
2555 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2556 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2557 struct batadv_neigh_node *router_gw = NULL;
2558 struct batadv_neigh_node *router_orig = NULL;
2559 u8 gw_tq_avg, orig_tq_avg;
2562 /* dynamic re-election is performed only on fast or late switch */
2563 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2566 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2572 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2574 if (!router_gw_ifinfo) {
2579 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2583 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2585 if (!router_orig_ifinfo)
2588 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2589 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2591 /* the TQ value has to be better */
2592 if (orig_tq_avg < gw_tq_avg)
2595 /* if the routing class is greater than 3 the value tells us how much
2596 * greater the TQ value of the new gateway must be
2598 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2599 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2602 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2603 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2604 gw_tq_avg, orig_tq_avg);
2608 if (router_gw_ifinfo)
2609 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2610 if (router_orig_ifinfo)
2611 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2613 batadv_neigh_node_put(router_gw);
2615 batadv_neigh_node_put(router_orig);
2620 /* fails if orig_node has no router */
2621 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2622 struct seq_file *seq,
2623 const struct batadv_gw_node *gw_node)
2625 struct batadv_gw_node *curr_gw;
2626 struct batadv_neigh_node *router;
2627 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2630 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2634 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2638 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2640 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2641 (curr_gw == gw_node ? "=>" : " "),
2642 gw_node->orig_node->orig,
2643 router_ifinfo->bat_iv.tq_avg, router->addr,
2644 router->if_incoming->net_dev->name,
2645 gw_node->bandwidth_down / 10,
2646 gw_node->bandwidth_down % 10,
2647 gw_node->bandwidth_up / 10,
2648 gw_node->bandwidth_up % 10);
2649 ret = seq_has_overflowed(seq) ? -1 : 0;
2652 batadv_gw_node_put(curr_gw);
2655 batadv_neigh_ifinfo_put(router_ifinfo);
2657 batadv_neigh_node_put(router);
2661 static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2662 struct seq_file *seq)
2664 struct batadv_gw_node *gw_node;
2668 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2671 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
2672 /* fails if orig_node has no router */
2673 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2681 seq_puts(seq, "No gateways in range ...\n");
2684 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2685 .name = "BATMAN_IV",
2687 .activate = batadv_iv_iface_activate,
2688 .enable = batadv_iv_ogm_iface_enable,
2689 .disable = batadv_iv_ogm_iface_disable,
2690 .update_mac = batadv_iv_ogm_iface_update_mac,
2691 .primary_set = batadv_iv_ogm_primary_iface_set,
2694 .cmp = batadv_iv_ogm_neigh_cmp,
2695 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2696 .print = batadv_iv_neigh_print,
2697 .dump = batadv_iv_ogm_neigh_dump,
2700 .print = batadv_iv_ogm_orig_print,
2701 .dump = batadv_iv_ogm_orig_dump,
2702 .free = batadv_iv_ogm_orig_free,
2703 .add_if = batadv_iv_ogm_orig_add_if,
2704 .del_if = batadv_iv_ogm_orig_del_if,
2707 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2708 .is_eligible = batadv_iv_gw_is_eligible,
2709 .print = batadv_iv_gw_print,
2713 int __init batadv_iv_init(void)
2717 /* batman originator packet */
2718 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2719 batadv_iv_ogm_receive);
2723 ret = batadv_algo_register(&batadv_batman_iv);
2725 goto handler_unregister;
2730 batadv_recv_handler_unregister(BATADV_IV_OGM);