Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / batman-adv / bat_iv_ogm.c
blob99abeadf416e6cf2b392bd833179cff333aa29c8
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"
20 #include "main.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/batadv_packet.h>
56 #include <uapi/linux/batman_adv.h>
58 #include "bat_algo.h"
59 #include "bitarray.h"
60 #include "gateway_client.h"
61 #include "hard-interface.h"
62 #include "hash.h"
63 #include "log.h"
64 #include "netlink.h"
65 #include "network-coding.h"
66 #include "originator.h"
67 #include "routing.h"
68 #include "send.h"
69 #include "translation-table.h"
70 #include "tvlv.h"
72 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
74 /**
75 * enum batadv_dup_status - duplicate status
77 enum batadv_dup_status {
78 /** @BATADV_NO_DUP: the packet is no duplicate */
79 BATADV_NO_DUP = 0,
81 /**
82 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
83 * the neighbor)
85 BATADV_ORIG_DUP,
87 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
88 BATADV_NEIGH_DUP,
90 /**
91 * @BATADV_PROTECTED: originator is currently protected (after reboot)
93 BATADV_PROTECTED,
96 /**
97 * batadv_ring_buffer_set() - update the ring buffer with the given value
98 * @lq_recv: pointer to the ring buffer
99 * @lq_index: index to store the value at
100 * @value: value to store in the ring buffer
102 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
104 lq_recv[*lq_index] = value;
105 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
109 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
110 * in the given ring buffer
111 * @lq_recv: pointer to the ring buffer
113 * Return: computed average value.
115 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
117 const u8 *ptr;
118 u16 count = 0;
119 u16 i = 0;
120 u16 sum = 0;
122 ptr = lq_recv;
124 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
125 if (*ptr != 0) {
126 count++;
127 sum += *ptr;
130 i++;
131 ptr++;
134 if (count == 0)
135 return 0;
137 return (u8)(sum / count);
141 * batadv_iv_ogm_orig_free() - free the private resources allocated for this
142 * orig_node
143 * @orig_node: the orig_node for which the resources have to be free'd
145 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
147 kfree(orig_node->bat_iv.bcast_own);
148 kfree(orig_node->bat_iv.bcast_own_sum);
152 * batadv_iv_ogm_orig_add_if() - change the private structures of the orig_node
153 * to include the new hard-interface
154 * @orig_node: the orig_node that has to be changed
155 * @max_if_num: the current amount of interfaces
157 * Return: 0 on success, a negative error code otherwise.
159 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
160 unsigned int max_if_num)
162 void *data_ptr;
163 size_t old_size;
164 int ret = -ENOMEM;
166 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
168 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
169 data_ptr = kmalloc_array(max_if_num,
170 BATADV_NUM_WORDS * sizeof(unsigned long),
171 GFP_ATOMIC);
172 if (!data_ptr)
173 goto unlock;
175 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
176 kfree(orig_node->bat_iv.bcast_own);
177 orig_node->bat_iv.bcast_own = data_ptr;
179 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
180 if (!data_ptr)
181 goto unlock;
183 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
184 (max_if_num - 1) * sizeof(u8));
185 kfree(orig_node->bat_iv.bcast_own_sum);
186 orig_node->bat_iv.bcast_own_sum = data_ptr;
188 ret = 0;
190 unlock:
191 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
193 return ret;
197 * batadv_iv_ogm_drop_bcast_own_entry() - drop section of bcast_own
198 * @orig_node: the orig_node that has to be changed
199 * @max_if_num: the current amount of interfaces
200 * @del_if_num: the index of the interface being removed
202 static void
203 batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
204 unsigned int max_if_num,
205 unsigned int del_if_num)
207 size_t chunk_size;
208 size_t if_offset;
209 void *data_ptr;
211 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
213 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
214 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
215 if (!data_ptr)
216 /* use old buffer when new one could not be allocated */
217 data_ptr = orig_node->bat_iv.bcast_own;
219 /* copy first part */
220 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
222 /* copy second part */
223 if_offset = (del_if_num + 1) * chunk_size;
224 memmove((char *)data_ptr + del_if_num * chunk_size,
225 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
226 (max_if_num - del_if_num) * chunk_size);
228 /* bcast_own was shrunk down in new buffer; free old one */
229 if (orig_node->bat_iv.bcast_own != data_ptr) {
230 kfree(orig_node->bat_iv.bcast_own);
231 orig_node->bat_iv.bcast_own = data_ptr;
236 * batadv_iv_ogm_drop_bcast_own_sum_entry() - drop section of bcast_own_sum
237 * @orig_node: the orig_node that has to be changed
238 * @max_if_num: the current amount of interfaces
239 * @del_if_num: the index of the interface being removed
241 static void
242 batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
243 unsigned int max_if_num,
244 unsigned int del_if_num)
246 size_t if_offset;
247 void *data_ptr;
249 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
251 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
252 if (!data_ptr)
253 /* use old buffer when new one could not be allocated */
254 data_ptr = orig_node->bat_iv.bcast_own_sum;
256 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
257 del_if_num * sizeof(u8));
259 if_offset = (del_if_num + 1) * sizeof(u8);
260 memmove((char *)data_ptr + del_if_num * sizeof(u8),
261 orig_node->bat_iv.bcast_own_sum + if_offset,
262 (max_if_num - del_if_num) * sizeof(u8));
264 /* bcast_own_sum was shrunk down in new buffer; free old one */
265 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
266 kfree(orig_node->bat_iv.bcast_own_sum);
267 orig_node->bat_iv.bcast_own_sum = data_ptr;
272 * batadv_iv_ogm_orig_del_if() - change the private structures of the orig_node
273 * to exclude the removed interface
274 * @orig_node: the orig_node that has to be changed
275 * @max_if_num: the current amount of interfaces
276 * @del_if_num: the index of the interface being removed
278 * Return: 0 on success, a negative error code otherwise.
280 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
281 unsigned int max_if_num,
282 unsigned int del_if_num)
284 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
286 if (max_if_num == 0) {
287 kfree(orig_node->bat_iv.bcast_own);
288 kfree(orig_node->bat_iv.bcast_own_sum);
289 orig_node->bat_iv.bcast_own = NULL;
290 orig_node->bat_iv.bcast_own_sum = NULL;
291 } else {
292 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
293 del_if_num);
294 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
295 del_if_num);
298 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
300 return 0;
304 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
305 * originator
306 * @bat_priv: the bat priv with all the soft interface information
307 * @addr: mac address of the originator
309 * Return: the originator object corresponding to the passed mac address or NULL
310 * on failure.
311 * If the object does not exists it is created an initialised.
313 static struct batadv_orig_node *
314 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
316 struct batadv_orig_node *orig_node;
317 int hash_added;
318 size_t size;
320 orig_node = batadv_orig_hash_find(bat_priv, addr);
321 if (orig_node)
322 return orig_node;
324 orig_node = batadv_orig_node_new(bat_priv, addr);
325 if (!orig_node)
326 return NULL;
328 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
330 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
331 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
332 if (!orig_node->bat_iv.bcast_own)
333 goto free_orig_node;
335 size = bat_priv->num_ifaces * sizeof(u8);
336 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
337 if (!orig_node->bat_iv.bcast_own_sum)
338 goto free_orig_node;
340 kref_get(&orig_node->refcount);
341 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
342 batadv_choose_orig, orig_node,
343 &orig_node->hash_entry);
344 if (hash_added != 0)
345 goto free_orig_node_hash;
347 return orig_node;
349 free_orig_node_hash:
350 batadv_orig_node_put(orig_node);
351 free_orig_node:
352 batadv_orig_node_put(orig_node);
354 return NULL;
357 static struct batadv_neigh_node *
358 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
359 const u8 *neigh_addr,
360 struct batadv_orig_node *orig_node,
361 struct batadv_orig_node *orig_neigh)
363 struct batadv_neigh_node *neigh_node;
365 neigh_node = batadv_neigh_node_get_or_create(orig_node,
366 hard_iface, neigh_addr);
367 if (!neigh_node)
368 goto out;
370 neigh_node->orig_node = orig_neigh;
372 out:
373 return neigh_node;
376 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
378 struct batadv_ogm_packet *batadv_ogm_packet;
379 unsigned char *ogm_buff;
380 u32 random_seqno;
382 /* randomize initial seqno to avoid collision */
383 get_random_bytes(&random_seqno, sizeof(random_seqno));
384 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
386 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
387 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
388 if (!ogm_buff)
389 return -ENOMEM;
391 hard_iface->bat_iv.ogm_buff = ogm_buff;
393 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
394 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
395 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
396 batadv_ogm_packet->ttl = 2;
397 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
398 batadv_ogm_packet->reserved = 0;
399 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
401 return 0;
404 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
406 kfree(hard_iface->bat_iv.ogm_buff);
407 hard_iface->bat_iv.ogm_buff = NULL;
410 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
412 struct batadv_ogm_packet *batadv_ogm_packet;
413 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
415 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
416 ether_addr_copy(batadv_ogm_packet->orig,
417 hard_iface->net_dev->dev_addr);
418 ether_addr_copy(batadv_ogm_packet->prev_sender,
419 hard_iface->net_dev->dev_addr);
422 static void
423 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
425 struct batadv_ogm_packet *batadv_ogm_packet;
426 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
428 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
429 batadv_ogm_packet->ttl = BATADV_TTL;
432 /* when do we schedule our own ogm to be sent */
433 static unsigned long
434 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
436 unsigned int msecs;
438 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
439 msecs += prandom_u32() % (2 * BATADV_JITTER);
441 return jiffies + msecs_to_jiffies(msecs);
444 /* when do we schedule a ogm packet to be sent */
445 static unsigned long batadv_iv_ogm_fwd_send_time(void)
447 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
450 /* apply hop penalty for a normal link */
451 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
453 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
454 int new_tq;
456 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
457 new_tq /= BATADV_TQ_MAX_VALUE;
459 return new_tq;
463 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
464 * @buff_pos: current position in the skb
465 * @packet_len: total length of the skb
466 * @tvlv_len: tvlv length of the previously considered OGM
468 * Return: true if there is enough space for another OGM, false otherwise.
470 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
471 __be16 tvlv_len)
473 int next_buff_pos = 0;
475 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
476 next_buff_pos += ntohs(tvlv_len);
478 return (next_buff_pos <= packet_len) &&
479 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
482 /* send a batman ogm to a given interface */
483 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
484 struct batadv_hard_iface *hard_iface)
486 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
487 const char *fwd_str;
488 u8 packet_num;
489 s16 buff_pos;
490 struct batadv_ogm_packet *batadv_ogm_packet;
491 struct sk_buff *skb;
492 u8 *packet_pos;
494 if (hard_iface->if_status != BATADV_IF_ACTIVE)
495 return;
497 packet_num = 0;
498 buff_pos = 0;
499 packet_pos = forw_packet->skb->data;
500 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
502 /* adjust all flags and log packets */
503 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
504 batadv_ogm_packet->tvlv_len)) {
505 /* we might have aggregated direct link packets with an
506 * ordinary base packet
508 if (forw_packet->direct_link_flags & BIT(packet_num) &&
509 forw_packet->if_incoming == hard_iface)
510 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
511 else
512 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
514 if (packet_num > 0 || !forw_packet->own)
515 fwd_str = "Forwarding";
516 else
517 fwd_str = "Sending own";
519 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
520 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
521 fwd_str, (packet_num > 0 ? "aggregated " : ""),
522 batadv_ogm_packet->orig,
523 ntohl(batadv_ogm_packet->seqno),
524 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
525 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
526 "on" : "off"),
527 hard_iface->net_dev->name,
528 hard_iface->net_dev->dev_addr);
530 buff_pos += BATADV_OGM_HLEN;
531 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
532 packet_num++;
533 packet_pos = forw_packet->skb->data + buff_pos;
534 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
537 /* create clone because function is called more than once */
538 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
539 if (skb) {
540 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
541 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
542 skb->len + ETH_HLEN);
543 batadv_send_broadcast_skb(skb, hard_iface);
547 /* send a batman ogm packet */
548 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
550 struct net_device *soft_iface;
552 if (!forw_packet->if_incoming) {
553 pr_err("Error - can't forward packet: incoming iface not specified\n");
554 return;
557 soft_iface = forw_packet->if_incoming->soft_iface;
559 if (WARN_ON(!forw_packet->if_outgoing))
560 return;
562 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
563 return;
565 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
566 return;
568 /* only for one specific outgoing interface */
569 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
573 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
574 * existing forward packet
575 * @new_bat_ogm_packet: OGM packet to be aggregated
576 * @bat_priv: the bat priv with all the soft interface information
577 * @packet_len: (total) length of the OGM
578 * @send_time: timestamp (jiffies) when the packet is to be sent
579 * @directlink: true if this is a direct link packet
580 * @if_incoming: interface where the packet was received
581 * @if_outgoing: interface for which the retransmission should be considered
582 * @forw_packet: the forwarded packet which should be checked
584 * Return: true if new_packet can be aggregated with forw_packet
586 static bool
587 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
588 struct batadv_priv *bat_priv,
589 int packet_len, unsigned long send_time,
590 bool directlink,
591 const struct batadv_hard_iface *if_incoming,
592 const struct batadv_hard_iface *if_outgoing,
593 const struct batadv_forw_packet *forw_packet)
595 struct batadv_ogm_packet *batadv_ogm_packet;
596 int aggregated_bytes = forw_packet->packet_len + packet_len;
597 struct batadv_hard_iface *primary_if = NULL;
598 bool res = false;
599 unsigned long aggregation_end_time;
601 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
602 aggregation_end_time = send_time;
603 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
605 /* we can aggregate the current packet to this aggregated packet
606 * if:
608 * - the send time is within our MAX_AGGREGATION_MS time
609 * - the resulting packet wont be bigger than
610 * MAX_AGGREGATION_BYTES
611 * otherwise aggregation is not possible
613 if (!time_before(send_time, forw_packet->send_time) ||
614 !time_after_eq(aggregation_end_time, forw_packet->send_time))
615 return false;
617 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
618 return false;
620 /* packet is not leaving on the same interface. */
621 if (forw_packet->if_outgoing != if_outgoing)
622 return false;
624 /* check aggregation compatibility
625 * -> direct link packets are broadcasted on
626 * their interface only
627 * -> aggregate packet if the current packet is
628 * a "global" packet as well as the base
629 * packet
631 primary_if = batadv_primary_if_get_selected(bat_priv);
632 if (!primary_if)
633 return false;
635 /* packets without direct link flag and high TTL
636 * are flooded through the net
638 if (!directlink &&
639 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
640 batadv_ogm_packet->ttl != 1 &&
642 /* own packets originating non-primary
643 * interfaces leave only that interface
645 (!forw_packet->own ||
646 forw_packet->if_incoming == primary_if)) {
647 res = true;
648 goto out;
651 /* if the incoming packet is sent via this one
652 * interface only - we still can aggregate
654 if (directlink &&
655 new_bat_ogm_packet->ttl == 1 &&
656 forw_packet->if_incoming == if_incoming &&
658 /* packets from direct neighbors or
659 * own secondary interface packets
660 * (= secondary interface packets in general)
662 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
663 (forw_packet->own &&
664 forw_packet->if_incoming != primary_if))) {
665 res = true;
666 goto out;
669 out:
670 if (primary_if)
671 batadv_hardif_put(primary_if);
672 return res;
676 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
677 * packet to it.
678 * @packet_buff: pointer to the OGM
679 * @packet_len: (total) length of the OGM
680 * @send_time: timestamp (jiffies) when the packet is to be sent
681 * @direct_link: whether this OGM has direct link status
682 * @if_incoming: interface where the packet was received
683 * @if_outgoing: interface for which the retransmission should be considered
684 * @own_packet: true if it is a self-generated ogm
686 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
687 int packet_len, unsigned long send_time,
688 bool direct_link,
689 struct batadv_hard_iface *if_incoming,
690 struct batadv_hard_iface *if_outgoing,
691 int own_packet)
693 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
694 struct batadv_forw_packet *forw_packet_aggr;
695 struct sk_buff *skb;
696 unsigned char *skb_buff;
697 unsigned int skb_size;
698 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
700 if (atomic_read(&bat_priv->aggregated_ogms) &&
701 packet_len < BATADV_MAX_AGGREGATION_BYTES)
702 skb_size = BATADV_MAX_AGGREGATION_BYTES;
703 else
704 skb_size = packet_len;
706 skb_size += ETH_HLEN;
708 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
709 if (!skb)
710 return;
712 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
713 queue_left, bat_priv, skb);
714 if (!forw_packet_aggr) {
715 kfree_skb(skb);
716 return;
719 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
720 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
722 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
723 forw_packet_aggr->packet_len = packet_len;
724 memcpy(skb_buff, packet_buff, packet_len);
726 forw_packet_aggr->own = own_packet;
727 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
728 forw_packet_aggr->send_time = send_time;
730 /* save packet direct link flag status */
731 if (direct_link)
732 forw_packet_aggr->direct_link_flags |= 1;
734 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
735 batadv_iv_send_outstanding_bat_ogm_packet);
737 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
740 /* aggregate a new packet into the existing ogm packet */
741 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
742 const unsigned char *packet_buff,
743 int packet_len, bool direct_link)
745 unsigned long new_direct_link_flag;
747 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
748 forw_packet_aggr->packet_len += packet_len;
749 forw_packet_aggr->num_packets++;
751 /* save packet direct link flag status */
752 if (direct_link) {
753 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
754 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
759 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
760 * @bat_priv: the bat priv with all the soft interface information
761 * @packet_buff: pointer to the OGM
762 * @packet_len: (total) length of the OGM
763 * @if_incoming: interface where the packet was received
764 * @if_outgoing: interface for which the retransmission should be considered
765 * @own_packet: true if it is a self-generated ogm
766 * @send_time: timestamp (jiffies) when the packet is to be sent
768 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
769 unsigned char *packet_buff,
770 int packet_len,
771 struct batadv_hard_iface *if_incoming,
772 struct batadv_hard_iface *if_outgoing,
773 int own_packet, unsigned long send_time)
775 /* _aggr -> pointer to the packet we want to aggregate with
776 * _pos -> pointer to the position in the queue
778 struct batadv_forw_packet *forw_packet_aggr = NULL;
779 struct batadv_forw_packet *forw_packet_pos = NULL;
780 struct batadv_ogm_packet *batadv_ogm_packet;
781 bool direct_link;
782 unsigned long max_aggregation_jiffies;
784 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
785 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
786 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
788 /* find position for the packet in the forward queue */
789 spin_lock_bh(&bat_priv->forw_bat_list_lock);
790 /* own packets are not to be aggregated */
791 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
792 hlist_for_each_entry(forw_packet_pos,
793 &bat_priv->forw_bat_list, list) {
794 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
795 bat_priv, packet_len,
796 send_time, direct_link,
797 if_incoming,
798 if_outgoing,
799 forw_packet_pos)) {
800 forw_packet_aggr = forw_packet_pos;
801 break;
806 /* nothing to aggregate with - either aggregation disabled or no
807 * suitable aggregation packet found
809 if (!forw_packet_aggr) {
810 /* the following section can run without the lock */
811 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
813 /* if we could not aggregate this packet with one of the others
814 * we hold it back for a while, so that it might be aggregated
815 * later on
817 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
818 send_time += max_aggregation_jiffies;
820 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
821 send_time, direct_link,
822 if_incoming, if_outgoing,
823 own_packet);
824 } else {
825 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
826 packet_len, direct_link);
827 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
831 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
832 const struct ethhdr *ethhdr,
833 struct batadv_ogm_packet *batadv_ogm_packet,
834 bool is_single_hop_neigh,
835 bool is_from_best_next_hop,
836 struct batadv_hard_iface *if_incoming,
837 struct batadv_hard_iface *if_outgoing)
839 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
840 u16 tvlv_len;
842 if (batadv_ogm_packet->ttl <= 1) {
843 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
844 return;
847 if (!is_from_best_next_hop) {
848 /* Mark the forwarded packet when it is not coming from our
849 * best next hop. We still need to forward the packet for our
850 * neighbor link quality detection to work in case the packet
851 * originated from a single hop neighbor. Otherwise we can
852 * simply drop the ogm.
854 if (is_single_hop_neigh)
855 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
856 else
857 return;
860 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
862 batadv_ogm_packet->ttl--;
863 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
865 /* apply hop penalty */
866 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
867 bat_priv);
869 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
870 "Forwarding packet: tq: %i, ttl: %i\n",
871 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
873 if (is_single_hop_neigh)
874 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
875 else
876 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
878 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
879 BATADV_OGM_HLEN + tvlv_len,
880 if_incoming, if_outgoing, 0,
881 batadv_iv_ogm_fwd_send_time());
885 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
886 * for the given interface
887 * @hard_iface: the interface for which the windows have to be shifted
889 static void
890 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
892 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
893 struct batadv_hashtable *hash = bat_priv->orig_hash;
894 struct hlist_head *head;
895 struct batadv_orig_node *orig_node;
896 unsigned long *word;
897 u32 i;
898 size_t word_index;
899 u8 *w;
900 unsigned int if_num;
902 for (i = 0; i < hash->size; i++) {
903 head = &hash->table[i];
905 rcu_read_lock();
906 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
907 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
908 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
909 word = &orig_node->bat_iv.bcast_own[word_index];
911 batadv_bit_get_packet(bat_priv, word, 1, 0);
912 if_num = hard_iface->if_num;
913 w = &orig_node->bat_iv.bcast_own_sum[if_num];
914 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
915 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
917 rcu_read_unlock();
921 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
923 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
924 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
925 struct batadv_ogm_packet *batadv_ogm_packet;
926 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
927 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
928 u32 seqno;
929 u16 tvlv_len = 0;
930 unsigned long send_time;
932 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
933 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
934 return;
936 /* the interface gets activated here to avoid race conditions between
937 * the moment of activating the interface in
938 * hardif_activate_interface() where the originator mac is set and
939 * outdated packets (especially uninitialized mac addresses) in the
940 * packet queue
942 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
943 hard_iface->if_status = BATADV_IF_ACTIVE;
945 primary_if = batadv_primary_if_get_selected(bat_priv);
947 if (hard_iface == primary_if) {
948 /* tt changes have to be committed before the tvlv data is
949 * appended as it may alter the tt tvlv container
951 batadv_tt_local_commit_changes(bat_priv);
952 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
953 ogm_buff_len,
954 BATADV_OGM_HLEN);
957 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
958 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
960 /* change sequence number to network order */
961 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
962 batadv_ogm_packet->seqno = htonl(seqno);
963 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
965 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
967 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
969 if (hard_iface != primary_if) {
970 /* OGMs from secondary interfaces are only scheduled on their
971 * respective interfaces.
973 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
974 hard_iface, hard_iface, 1, send_time);
975 goto out;
978 /* OGMs from primary interfaces are scheduled on all
979 * interfaces.
981 rcu_read_lock();
982 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
983 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
984 continue;
986 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
987 continue;
989 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
990 *ogm_buff_len, hard_iface,
991 tmp_hard_iface, 1, send_time);
993 batadv_hardif_put(tmp_hard_iface);
995 rcu_read_unlock();
997 out:
998 if (primary_if)
999 batadv_hardif_put(primary_if);
1003 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
1004 * originator
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @orig_node: the orig node who originally emitted the ogm packet
1007 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
1008 * @ethhdr: Ethernet header of the OGM
1009 * @batadv_ogm_packet: the ogm packet
1010 * @if_incoming: interface where the packet was received
1011 * @if_outgoing: interface for which the retransmission should be considered
1012 * @dup_status: the duplicate status of this ogm packet.
1014 static void
1015 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1016 struct batadv_orig_node *orig_node,
1017 struct batadv_orig_ifinfo *orig_ifinfo,
1018 const struct ethhdr *ethhdr,
1019 const struct batadv_ogm_packet *batadv_ogm_packet,
1020 struct batadv_hard_iface *if_incoming,
1021 struct batadv_hard_iface *if_outgoing,
1022 enum batadv_dup_status dup_status)
1024 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1025 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1026 struct batadv_neigh_node *neigh_node = NULL;
1027 struct batadv_neigh_node *tmp_neigh_node = NULL;
1028 struct batadv_neigh_node *router = NULL;
1029 struct batadv_orig_node *orig_node_tmp;
1030 unsigned int if_num;
1031 u8 sum_orig, sum_neigh;
1032 u8 *neigh_addr;
1033 u8 tq_avg;
1035 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1036 "%s(): Searching and updating originator entry of received packet\n",
1037 __func__);
1039 rcu_read_lock();
1040 hlist_for_each_entry_rcu(tmp_neigh_node,
1041 &orig_node->neigh_list, list) {
1042 neigh_addr = tmp_neigh_node->addr;
1043 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1044 tmp_neigh_node->if_incoming == if_incoming &&
1045 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1046 if (WARN(neigh_node, "too many matching neigh_nodes"))
1047 batadv_neigh_node_put(neigh_node);
1048 neigh_node = tmp_neigh_node;
1049 continue;
1052 if (dup_status != BATADV_NO_DUP)
1053 continue;
1055 /* only update the entry for this outgoing interface */
1056 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1057 if_outgoing);
1058 if (!neigh_ifinfo)
1059 continue;
1061 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1062 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1063 &neigh_ifinfo->bat_iv.tq_index, 0);
1064 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1065 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1066 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1068 batadv_neigh_ifinfo_put(neigh_ifinfo);
1069 neigh_ifinfo = NULL;
1072 if (!neigh_node) {
1073 struct batadv_orig_node *orig_tmp;
1075 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1076 if (!orig_tmp)
1077 goto unlock;
1079 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1080 ethhdr->h_source,
1081 orig_node, orig_tmp);
1083 batadv_orig_node_put(orig_tmp);
1084 if (!neigh_node)
1085 goto unlock;
1086 } else {
1087 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1088 "Updating existing last-hop neighbor of originator\n");
1091 rcu_read_unlock();
1092 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1093 if (!neigh_ifinfo)
1094 goto out;
1096 neigh_node->last_seen = jiffies;
1098 spin_lock_bh(&neigh_node->ifinfo_lock);
1099 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1100 &neigh_ifinfo->bat_iv.tq_index,
1101 batadv_ogm_packet->tq);
1102 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1103 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1104 spin_unlock_bh(&neigh_node->ifinfo_lock);
1106 if (dup_status == BATADV_NO_DUP) {
1107 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1108 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1111 /* if this neighbor already is our next hop there is nothing
1112 * to change
1114 router = batadv_orig_router_get(orig_node, if_outgoing);
1115 if (router == neigh_node)
1116 goto out;
1118 if (router) {
1119 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1120 if (!router_ifinfo)
1121 goto out;
1123 /* if this neighbor does not offer a better TQ we won't
1124 * consider it
1126 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1127 goto out;
1130 /* if the TQ is the same and the link not more symmetric we
1131 * won't consider it either
1133 if (router_ifinfo &&
1134 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1135 orig_node_tmp = router->orig_node;
1136 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1137 if_num = router->if_incoming->if_num;
1138 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1139 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1141 orig_node_tmp = neigh_node->orig_node;
1142 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1143 if_num = neigh_node->if_incoming->if_num;
1144 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1145 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1147 if (sum_orig >= sum_neigh)
1148 goto out;
1151 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1152 goto out;
1154 unlock:
1155 rcu_read_unlock();
1156 out:
1157 if (neigh_node)
1158 batadv_neigh_node_put(neigh_node);
1159 if (router)
1160 batadv_neigh_node_put(router);
1161 if (neigh_ifinfo)
1162 batadv_neigh_ifinfo_put(neigh_ifinfo);
1163 if (router_ifinfo)
1164 batadv_neigh_ifinfo_put(router_ifinfo);
1168 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
1169 * @orig_node: the orig node who originally emitted the ogm packet
1170 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1171 * @batadv_ogm_packet: the ogm packet
1172 * @if_incoming: interface where the packet was received
1173 * @if_outgoing: interface for which the retransmission should be considered
1175 * Return: true if the link can be considered bidirectional, false otherwise
1177 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1178 struct batadv_orig_node *orig_neigh_node,
1179 struct batadv_ogm_packet *batadv_ogm_packet,
1180 struct batadv_hard_iface *if_incoming,
1181 struct batadv_hard_iface *if_outgoing)
1183 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1184 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1185 struct batadv_neigh_ifinfo *neigh_ifinfo;
1186 u8 total_count;
1187 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1188 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1189 unsigned int if_num;
1190 unsigned int tq_asym_penalty, inv_asym_penalty;
1191 unsigned int combined_tq;
1192 unsigned int tq_iface_penalty;
1193 bool ret = false;
1195 /* find corresponding one hop neighbor */
1196 rcu_read_lock();
1197 hlist_for_each_entry_rcu(tmp_neigh_node,
1198 &orig_neigh_node->neigh_list, list) {
1199 if (!batadv_compare_eth(tmp_neigh_node->addr,
1200 orig_neigh_node->orig))
1201 continue;
1203 if (tmp_neigh_node->if_incoming != if_incoming)
1204 continue;
1206 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1207 continue;
1209 neigh_node = tmp_neigh_node;
1210 break;
1212 rcu_read_unlock();
1214 if (!neigh_node)
1215 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1216 orig_neigh_node->orig,
1217 orig_neigh_node,
1218 orig_neigh_node);
1220 if (!neigh_node)
1221 goto out;
1223 /* if orig_node is direct neighbor update neigh_node last_seen */
1224 if (orig_node == orig_neigh_node)
1225 neigh_node->last_seen = jiffies;
1227 orig_node->last_seen = jiffies;
1229 /* find packet count of corresponding one hop neighbor */
1230 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1231 if_num = if_incoming->if_num;
1232 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1233 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1234 if (neigh_ifinfo) {
1235 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1236 batadv_neigh_ifinfo_put(neigh_ifinfo);
1237 } else {
1238 neigh_rq_count = 0;
1240 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1242 /* pay attention to not get a value bigger than 100 % */
1243 if (orig_eq_count > neigh_rq_count)
1244 total_count = neigh_rq_count;
1245 else
1246 total_count = orig_eq_count;
1248 /* if we have too few packets (too less data) we set tq_own to zero
1249 * if we receive too few packets it is not considered bidirectional
1251 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1252 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1253 tq_own = 0;
1254 else
1255 /* neigh_node->real_packet_count is never zero as we
1256 * only purge old information when getting new
1257 * information
1259 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1261 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1262 * affect the nearly-symmetric links only a little, but
1263 * punishes asymmetric links more. This will give a value
1264 * between 0 and TQ_MAX_VALUE
1266 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1267 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1268 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1269 BATADV_TQ_LOCAL_WINDOW_SIZE *
1270 BATADV_TQ_LOCAL_WINDOW_SIZE;
1271 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1272 inv_asym_penalty /= neigh_rq_max_cube;
1273 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1275 /* penalize if the OGM is forwarded on the same interface. WiFi
1276 * interfaces and other half duplex devices suffer from throughput
1277 * drops as they can't send and receive at the same time.
1279 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1280 if (if_outgoing && if_incoming == if_outgoing &&
1281 batadv_is_wifi_hardif(if_outgoing))
1282 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1283 bat_priv);
1285 combined_tq = batadv_ogm_packet->tq *
1286 tq_own *
1287 tq_asym_penalty *
1288 tq_iface_penalty;
1289 combined_tq /= BATADV_TQ_MAX_VALUE *
1290 BATADV_TQ_MAX_VALUE *
1291 BATADV_TQ_MAX_VALUE;
1292 batadv_ogm_packet->tq = combined_tq;
1294 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1295 "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",
1296 orig_node->orig, orig_neigh_node->orig, total_count,
1297 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1298 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1299 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1301 /* if link has the minimum required transmission quality
1302 * consider it bidirectional
1304 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1305 ret = true;
1307 out:
1308 if (neigh_node)
1309 batadv_neigh_node_put(neigh_node);
1310 return ret;
1314 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
1315 * adjust the sequence number and find out whether it is a duplicate
1316 * @ethhdr: ethernet header of the packet
1317 * @batadv_ogm_packet: OGM packet to be considered
1318 * @if_incoming: interface on which the OGM packet was received
1319 * @if_outgoing: interface for which the retransmission should be considered
1321 * Return: duplicate status as enum batadv_dup_status
1323 static enum batadv_dup_status
1324 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1325 const struct batadv_ogm_packet *batadv_ogm_packet,
1326 const struct batadv_hard_iface *if_incoming,
1327 struct batadv_hard_iface *if_outgoing)
1329 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1330 struct batadv_orig_node *orig_node;
1331 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1332 struct batadv_neigh_node *neigh_node;
1333 struct batadv_neigh_ifinfo *neigh_ifinfo;
1334 bool is_dup;
1335 s32 seq_diff;
1336 bool need_update = false;
1337 int set_mark;
1338 enum batadv_dup_status ret = BATADV_NO_DUP;
1339 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1340 u8 *neigh_addr;
1341 u8 packet_count;
1342 unsigned long *bitmap;
1344 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1345 if (!orig_node)
1346 return BATADV_NO_DUP;
1348 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1349 if (WARN_ON(!orig_ifinfo)) {
1350 batadv_orig_node_put(orig_node);
1351 return 0;
1354 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1355 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1357 /* signalize caller that the packet is to be dropped. */
1358 if (!hlist_empty(&orig_node->neigh_list) &&
1359 batadv_window_protected(bat_priv, seq_diff,
1360 BATADV_TQ_LOCAL_WINDOW_SIZE,
1361 &orig_ifinfo->batman_seqno_reset, NULL)) {
1362 ret = BATADV_PROTECTED;
1363 goto out;
1366 rcu_read_lock();
1367 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1368 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1369 if_outgoing);
1370 if (!neigh_ifinfo)
1371 continue;
1373 neigh_addr = neigh_node->addr;
1374 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1375 orig_ifinfo->last_real_seqno,
1376 seqno);
1378 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1379 neigh_node->if_incoming == if_incoming) {
1380 set_mark = 1;
1381 if (is_dup)
1382 ret = BATADV_NEIGH_DUP;
1383 } else {
1384 set_mark = 0;
1385 if (is_dup && ret != BATADV_NEIGH_DUP)
1386 ret = BATADV_ORIG_DUP;
1389 /* if the window moved, set the update flag. */
1390 bitmap = neigh_ifinfo->bat_iv.real_bits;
1391 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1392 seq_diff, set_mark);
1394 packet_count = bitmap_weight(bitmap,
1395 BATADV_TQ_LOCAL_WINDOW_SIZE);
1396 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1397 batadv_neigh_ifinfo_put(neigh_ifinfo);
1399 rcu_read_unlock();
1401 if (need_update) {
1402 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1403 "%s updating last_seqno: old %u, new %u\n",
1404 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1405 orig_ifinfo->last_real_seqno, seqno);
1406 orig_ifinfo->last_real_seqno = seqno;
1409 out:
1410 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1411 batadv_orig_node_put(orig_node);
1412 batadv_orig_ifinfo_put(orig_ifinfo);
1413 return ret;
1417 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1418 * interface
1419 * @skb: the skb containing the OGM
1420 * @ogm_offset: offset from skb->data to start of ogm header
1421 * @orig_node: the (cached) orig node for the originator of this OGM
1422 * @if_incoming: the interface where this packet was received
1423 * @if_outgoing: the interface for which the packet should be considered
1425 static void
1426 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1427 struct batadv_orig_node *orig_node,
1428 struct batadv_hard_iface *if_incoming,
1429 struct batadv_hard_iface *if_outgoing)
1431 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1432 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1433 struct batadv_neigh_node *router = NULL;
1434 struct batadv_neigh_node *router_router = NULL;
1435 struct batadv_orig_node *orig_neigh_node;
1436 struct batadv_orig_ifinfo *orig_ifinfo;
1437 struct batadv_neigh_node *orig_neigh_router = NULL;
1438 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1439 struct batadv_ogm_packet *ogm_packet;
1440 enum batadv_dup_status dup_status;
1441 bool is_from_best_next_hop = false;
1442 bool is_single_hop_neigh = false;
1443 bool sameseq, similar_ttl;
1444 struct sk_buff *skb_priv;
1445 struct ethhdr *ethhdr;
1446 u8 *prev_sender;
1447 bool is_bidirect;
1449 /* create a private copy of the skb, as some functions change tq value
1450 * and/or flags.
1452 skb_priv = skb_copy(skb, GFP_ATOMIC);
1453 if (!skb_priv)
1454 return;
1456 ethhdr = eth_hdr(skb_priv);
1457 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1459 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1460 if_incoming, if_outgoing);
1461 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1462 is_single_hop_neigh = true;
1464 if (dup_status == BATADV_PROTECTED) {
1465 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1466 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1467 ethhdr->h_source);
1468 goto out;
1471 if (ogm_packet->tq == 0) {
1472 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1473 "Drop packet: originator packet with tq equal 0\n");
1474 goto out;
1477 if (is_single_hop_neigh) {
1478 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1479 ethhdr->h_source);
1480 if (hardif_neigh)
1481 hardif_neigh->last_seen = jiffies;
1484 router = batadv_orig_router_get(orig_node, if_outgoing);
1485 if (router) {
1486 router_router = batadv_orig_router_get(router->orig_node,
1487 if_outgoing);
1488 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1491 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1492 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1493 is_from_best_next_hop = true;
1495 prev_sender = ogm_packet->prev_sender;
1496 /* avoid temporary routing loops */
1497 if (router && router_router &&
1498 (batadv_compare_eth(router->addr, prev_sender)) &&
1499 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1500 (batadv_compare_eth(router->addr, router_router->addr))) {
1501 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1502 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1503 ethhdr->h_source);
1504 goto out;
1507 if (if_outgoing == BATADV_IF_DEFAULT)
1508 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1510 /* if sender is a direct neighbor the sender mac equals
1511 * originator mac
1513 if (is_single_hop_neigh)
1514 orig_neigh_node = orig_node;
1515 else
1516 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1517 ethhdr->h_source);
1519 if (!orig_neigh_node)
1520 goto out;
1522 /* Update nc_nodes of the originator */
1523 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1524 ogm_packet, is_single_hop_neigh);
1526 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1527 if_outgoing);
1529 /* drop packet if sender is not a direct neighbor and if we
1530 * don't route towards it
1532 if (!is_single_hop_neigh && !orig_neigh_router) {
1533 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1534 "Drop packet: OGM via unknown neighbor!\n");
1535 goto out_neigh;
1538 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1539 ogm_packet, if_incoming,
1540 if_outgoing);
1542 /* update ranking if it is not a duplicate or has the same
1543 * seqno and similar ttl as the non-duplicate
1545 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1546 if (!orig_ifinfo)
1547 goto out_neigh;
1549 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1550 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1552 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
1553 (sameseq && similar_ttl))) {
1554 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1555 orig_ifinfo, ethhdr,
1556 ogm_packet, if_incoming,
1557 if_outgoing, dup_status);
1559 batadv_orig_ifinfo_put(orig_ifinfo);
1561 /* only forward for specific interface, not for the default one. */
1562 if (if_outgoing == BATADV_IF_DEFAULT)
1563 goto out_neigh;
1565 /* is single hop (direct) neighbor */
1566 if (is_single_hop_neigh) {
1567 /* OGMs from secondary interfaces should only scheduled once
1568 * per interface where it has been received, not multiple times
1570 if (ogm_packet->ttl <= 2 &&
1571 if_incoming != if_outgoing) {
1572 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1573 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1574 goto out_neigh;
1576 /* mark direct link on incoming interface */
1577 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1578 is_single_hop_neigh,
1579 is_from_best_next_hop, if_incoming,
1580 if_outgoing);
1582 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1583 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1584 goto out_neigh;
1587 /* multihop originator */
1588 if (!is_bidirect) {
1589 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1590 "Drop packet: not received via bidirectional link\n");
1591 goto out_neigh;
1594 if (dup_status == BATADV_NEIGH_DUP) {
1595 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1596 "Drop packet: duplicate packet received\n");
1597 goto out_neigh;
1600 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1601 "Forwarding packet: rebroadcast originator packet\n");
1602 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1603 is_single_hop_neigh, is_from_best_next_hop,
1604 if_incoming, if_outgoing);
1606 out_neigh:
1607 if (orig_neigh_node && !is_single_hop_neigh)
1608 batadv_orig_node_put(orig_neigh_node);
1609 out:
1610 if (router_ifinfo)
1611 batadv_neigh_ifinfo_put(router_ifinfo);
1612 if (router)
1613 batadv_neigh_node_put(router);
1614 if (router_router)
1615 batadv_neigh_node_put(router_router);
1616 if (orig_neigh_router)
1617 batadv_neigh_node_put(orig_neigh_router);
1618 if (hardif_neigh)
1619 batadv_hardif_neigh_put(hardif_neigh);
1621 consume_skb(skb_priv);
1625 * batadv_iv_ogm_process() - process an incoming batman iv OGM
1626 * @skb: the skb containing the OGM
1627 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1628 * @if_incoming: the interface where this packet was receved
1630 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1631 struct batadv_hard_iface *if_incoming)
1633 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1634 struct batadv_orig_node *orig_neigh_node, *orig_node;
1635 struct batadv_hard_iface *hard_iface;
1636 struct batadv_ogm_packet *ogm_packet;
1637 u32 if_incoming_seqno;
1638 bool has_directlink_flag;
1639 struct ethhdr *ethhdr;
1640 bool is_my_oldorig = false;
1641 bool is_my_addr = false;
1642 bool is_my_orig = false;
1644 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1645 ethhdr = eth_hdr(skb);
1647 /* Silently drop when the batman packet is actually not a
1648 * correct packet.
1650 * This might happen if a packet is padded (e.g. Ethernet has a
1651 * minimum frame length of 64 byte) and the aggregation interprets
1652 * it as an additional length.
1654 * TODO: A more sane solution would be to have a bit in the
1655 * batadv_ogm_packet to detect whether the packet is the last
1656 * packet in an aggregation. Here we expect that the padding
1657 * is always zero (or not 0x01)
1659 if (ogm_packet->packet_type != BATADV_IV_OGM)
1660 return;
1662 /* could be changed by schedule_own_packet() */
1663 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1665 if (ogm_packet->flags & BATADV_DIRECTLINK)
1666 has_directlink_flag = true;
1667 else
1668 has_directlink_flag = false;
1670 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1671 "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",
1672 ethhdr->h_source, if_incoming->net_dev->name,
1673 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1674 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1675 ogm_packet->tq, ogm_packet->ttl,
1676 ogm_packet->version, has_directlink_flag);
1678 rcu_read_lock();
1679 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1680 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1681 continue;
1683 if (hard_iface->soft_iface != if_incoming->soft_iface)
1684 continue;
1686 if (batadv_compare_eth(ethhdr->h_source,
1687 hard_iface->net_dev->dev_addr))
1688 is_my_addr = true;
1690 if (batadv_compare_eth(ogm_packet->orig,
1691 hard_iface->net_dev->dev_addr))
1692 is_my_orig = true;
1694 if (batadv_compare_eth(ogm_packet->prev_sender,
1695 hard_iface->net_dev->dev_addr))
1696 is_my_oldorig = true;
1698 rcu_read_unlock();
1700 if (is_my_addr) {
1701 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1702 "Drop packet: received my own broadcast (sender: %pM)\n",
1703 ethhdr->h_source);
1704 return;
1707 if (is_my_orig) {
1708 unsigned long *word;
1709 size_t offset;
1710 s32 bit_pos;
1711 unsigned int if_num;
1712 u8 *weight;
1714 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1715 ethhdr->h_source);
1716 if (!orig_neigh_node)
1717 return;
1719 /* neighbor has to indicate direct link and it has to
1720 * come via the corresponding interface
1721 * save packet seqno for bidirectional check
1723 if (has_directlink_flag &&
1724 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1725 ogm_packet->orig)) {
1726 if_num = if_incoming->if_num;
1727 offset = if_num * BATADV_NUM_WORDS;
1729 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1730 word = &orig_neigh_node->bat_iv.bcast_own[offset];
1731 bit_pos = if_incoming_seqno - 2;
1732 bit_pos -= ntohl(ogm_packet->seqno);
1733 batadv_set_bit(word, bit_pos);
1734 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1735 *weight = bitmap_weight(word,
1736 BATADV_TQ_LOCAL_WINDOW_SIZE);
1737 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1740 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1741 "Drop packet: originator packet from myself (via neighbor)\n");
1742 batadv_orig_node_put(orig_neigh_node);
1743 return;
1746 if (is_my_oldorig) {
1747 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1748 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1749 ethhdr->h_source);
1750 return;
1753 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1754 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1755 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1756 ethhdr->h_source);
1757 return;
1760 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1761 if (!orig_node)
1762 return;
1764 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1765 if_incoming, BATADV_IF_DEFAULT);
1767 rcu_read_lock();
1768 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1769 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1770 continue;
1772 if (hard_iface->soft_iface != bat_priv->soft_iface)
1773 continue;
1775 if (!kref_get_unless_zero(&hard_iface->refcount))
1776 continue;
1778 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1779 if_incoming, hard_iface);
1781 batadv_hardif_put(hard_iface);
1783 rcu_read_unlock();
1785 batadv_orig_node_put(orig_node);
1788 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1790 struct delayed_work *delayed_work;
1791 struct batadv_forw_packet *forw_packet;
1792 struct batadv_priv *bat_priv;
1793 bool dropped = false;
1795 delayed_work = to_delayed_work(work);
1796 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1797 delayed_work);
1798 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1800 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1801 dropped = true;
1802 goto out;
1805 batadv_iv_ogm_emit(forw_packet);
1807 /* we have to have at least one packet in the queue to determine the
1808 * queues wake up time unless we are shutting down.
1810 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1811 * primary interface should only be rescheduled once per period, but
1812 * this function will be called for the forw_packet instances of the
1813 * other secondary interfaces as well.
1815 if (forw_packet->own &&
1816 forw_packet->if_incoming == forw_packet->if_outgoing)
1817 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1819 out:
1820 /* do we get something for free()? */
1821 if (batadv_forw_packet_steal(forw_packet,
1822 &bat_priv->forw_bat_list_lock))
1823 batadv_forw_packet_free(forw_packet, dropped);
1826 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1827 struct batadv_hard_iface *if_incoming)
1829 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1830 struct batadv_ogm_packet *ogm_packet;
1831 u8 *packet_pos;
1832 int ogm_offset;
1833 bool res;
1834 int ret = NET_RX_DROP;
1836 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1837 if (!res)
1838 goto free_skb;
1840 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1841 * that does not have B.A.T.M.A.N. IV enabled ?
1843 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
1844 goto free_skb;
1846 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1847 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1848 skb->len + ETH_HLEN);
1850 ogm_offset = 0;
1851 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1853 /* unpack the aggregated packets and process them one by one */
1854 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1855 ogm_packet->tvlv_len)) {
1856 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1858 ogm_offset += BATADV_OGM_HLEN;
1859 ogm_offset += ntohs(ogm_packet->tvlv_len);
1861 packet_pos = skb->data + ogm_offset;
1862 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1865 ret = NET_RX_SUCCESS;
1867 free_skb:
1868 if (ret == NET_RX_SUCCESS)
1869 consume_skb(skb);
1870 else
1871 kfree_skb(skb);
1873 return ret;
1876 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1878 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
1879 * @orig_node: the orig_node for which the neighbors are printed
1880 * @if_outgoing: outgoing interface for these entries
1881 * @seq: debugfs table seq_file struct
1883 * Must be called while holding an rcu lock.
1885 static void
1886 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1887 struct batadv_hard_iface *if_outgoing,
1888 struct seq_file *seq)
1890 struct batadv_neigh_node *neigh_node;
1891 struct batadv_neigh_ifinfo *n_ifinfo;
1893 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1894 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1895 if (!n_ifinfo)
1896 continue;
1898 seq_printf(seq, " %pM (%3i)",
1899 neigh_node->addr,
1900 n_ifinfo->bat_iv.tq_avg);
1902 batadv_neigh_ifinfo_put(n_ifinfo);
1907 * batadv_iv_ogm_orig_print() - print the originator table
1908 * @bat_priv: the bat priv with all the soft interface information
1909 * @seq: debugfs table seq_file struct
1910 * @if_outgoing: the outgoing interface for which this should be printed
1912 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1913 struct seq_file *seq,
1914 struct batadv_hard_iface *if_outgoing)
1916 struct batadv_neigh_node *neigh_node;
1917 struct batadv_hashtable *hash = bat_priv->orig_hash;
1918 int last_seen_msecs, last_seen_secs;
1919 struct batadv_orig_node *orig_node;
1920 struct batadv_neigh_ifinfo *n_ifinfo;
1921 unsigned long last_seen_jiffies;
1922 struct hlist_head *head;
1923 int batman_count = 0;
1924 u32 i;
1926 seq_puts(seq,
1927 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1929 for (i = 0; i < hash->size; i++) {
1930 head = &hash->table[i];
1932 rcu_read_lock();
1933 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1934 neigh_node = batadv_orig_router_get(orig_node,
1935 if_outgoing);
1936 if (!neigh_node)
1937 continue;
1939 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1940 if_outgoing);
1941 if (!n_ifinfo)
1942 goto next;
1944 if (n_ifinfo->bat_iv.tq_avg == 0)
1945 goto next;
1947 last_seen_jiffies = jiffies - orig_node->last_seen;
1948 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1949 last_seen_secs = last_seen_msecs / 1000;
1950 last_seen_msecs = last_seen_msecs % 1000;
1952 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1953 orig_node->orig, last_seen_secs,
1954 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1955 neigh_node->addr,
1956 neigh_node->if_incoming->net_dev->name);
1958 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1959 seq);
1960 seq_putc(seq, '\n');
1961 batman_count++;
1963 next:
1964 batadv_neigh_node_put(neigh_node);
1965 if (n_ifinfo)
1966 batadv_neigh_ifinfo_put(n_ifinfo);
1968 rcu_read_unlock();
1971 if (batman_count == 0)
1972 seq_puts(seq, "No batman nodes in range ...\n");
1974 #endif
1977 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
1978 * given outgoing interface.
1979 * @neigh_node: Neighbour of interest
1980 * @if_outgoing: Outgoing interface of interest
1981 * @tq_avg: Pointer of where to store the TQ average
1983 * Return: False if no average TQ available, otherwise true.
1985 static bool
1986 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1987 struct batadv_hard_iface *if_outgoing,
1988 u8 *tq_avg)
1990 struct batadv_neigh_ifinfo *n_ifinfo;
1992 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1993 if (!n_ifinfo)
1994 return false;
1996 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1997 batadv_neigh_ifinfo_put(n_ifinfo);
1999 return true;
2003 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
2004 * message
2005 * @msg: Netlink message to dump into
2006 * @portid: Port making netlink request
2007 * @seq: Sequence number of netlink message
2008 * @bat_priv: The bat priv with all the soft interface information
2009 * @if_outgoing: Limit dump to entries with this outgoing interface
2010 * @orig_node: Originator to dump
2011 * @neigh_node: Single hops neighbour
2012 * @best: Is the best originator
2014 * Return: Error code, or 0 on success
2016 static int
2017 batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2018 struct batadv_priv *bat_priv,
2019 struct batadv_hard_iface *if_outgoing,
2020 struct batadv_orig_node *orig_node,
2021 struct batadv_neigh_node *neigh_node,
2022 bool best)
2024 void *hdr;
2025 u8 tq_avg;
2026 unsigned int last_seen_msecs;
2028 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2030 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2031 return 0;
2033 if (if_outgoing != BATADV_IF_DEFAULT &&
2034 if_outgoing != neigh_node->if_incoming)
2035 return 0;
2037 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2038 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2039 if (!hdr)
2040 return -ENOBUFS;
2042 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2043 orig_node->orig) ||
2044 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2045 neigh_node->addr) ||
2046 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2047 neigh_node->if_incoming->net_dev->ifindex) ||
2048 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2049 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2050 last_seen_msecs))
2051 goto nla_put_failure;
2053 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2054 goto nla_put_failure;
2056 genlmsg_end(msg, hdr);
2057 return 0;
2059 nla_put_failure:
2060 genlmsg_cancel(msg, hdr);
2061 return -EMSGSIZE;
2065 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
2066 * @msg: Netlink message to dump into
2067 * @portid: Port making netlink request
2068 * @seq: Sequence number of netlink message
2069 * @bat_priv: The bat priv with all the soft interface information
2070 * @if_outgoing: Limit dump to entries with this outgoing interface
2071 * @orig_node: Originator to dump
2072 * @sub_s: Number of sub entries to skip
2074 * This function assumes the caller holds rcu_read_lock().
2076 * Return: Error code, or 0 on success
2078 static int
2079 batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2080 struct batadv_priv *bat_priv,
2081 struct batadv_hard_iface *if_outgoing,
2082 struct batadv_orig_node *orig_node, int *sub_s)
2084 struct batadv_neigh_node *neigh_node_best;
2085 struct batadv_neigh_node *neigh_node;
2086 int sub = 0;
2087 bool best;
2088 u8 tq_avg_best;
2090 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2091 if (!neigh_node_best)
2092 goto out;
2094 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2095 &tq_avg_best))
2096 goto out;
2098 if (tq_avg_best == 0)
2099 goto out;
2101 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2102 if (sub++ < *sub_s)
2103 continue;
2105 best = (neigh_node == neigh_node_best);
2107 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2108 bat_priv, if_outgoing,
2109 orig_node, neigh_node,
2110 best)) {
2111 batadv_neigh_node_put(neigh_node_best);
2113 *sub_s = sub - 1;
2114 return -EMSGSIZE;
2118 out:
2119 if (neigh_node_best)
2120 batadv_neigh_node_put(neigh_node_best);
2122 *sub_s = 0;
2123 return 0;
2127 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
2128 * message
2129 * @msg: Netlink message to dump into
2130 * @portid: Port making netlink request
2131 * @seq: Sequence number of netlink message
2132 * @bat_priv: The bat priv with all the soft interface information
2133 * @if_outgoing: Limit dump to entries with this outgoing interface
2134 * @head: Bucket to be dumped
2135 * @idx_s: Number of entries to be skipped
2136 * @sub: Number of sub entries to be skipped
2138 * Return: Error code, or 0 on success
2140 static int
2141 batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2142 struct batadv_priv *bat_priv,
2143 struct batadv_hard_iface *if_outgoing,
2144 struct hlist_head *head, int *idx_s, int *sub)
2146 struct batadv_orig_node *orig_node;
2147 int idx = 0;
2149 rcu_read_lock();
2150 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2151 if (idx++ < *idx_s)
2152 continue;
2154 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2155 if_outgoing, orig_node,
2156 sub)) {
2157 rcu_read_unlock();
2158 *idx_s = idx - 1;
2159 return -EMSGSIZE;
2162 rcu_read_unlock();
2164 *idx_s = 0;
2165 *sub = 0;
2166 return 0;
2170 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
2171 * @msg: Netlink message to dump into
2172 * @cb: Control block containing additional options
2173 * @bat_priv: The bat priv with all the soft interface information
2174 * @if_outgoing: Limit dump to entries with this outgoing interface
2176 static void
2177 batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2178 struct batadv_priv *bat_priv,
2179 struct batadv_hard_iface *if_outgoing)
2181 struct batadv_hashtable *hash = bat_priv->orig_hash;
2182 struct hlist_head *head;
2183 int bucket = cb->args[0];
2184 int idx = cb->args[1];
2185 int sub = cb->args[2];
2186 int portid = NETLINK_CB(cb->skb).portid;
2188 while (bucket < hash->size) {
2189 head = &hash->table[bucket];
2191 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2192 cb->nlh->nlmsg_seq,
2193 bat_priv, if_outgoing, head,
2194 &idx, &sub))
2195 break;
2197 bucket++;
2200 cb->args[0] = bucket;
2201 cb->args[1] = idx;
2202 cb->args[2] = sub;
2205 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2207 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
2208 * @seq: neighbour table seq_file struct
2209 * @hardif_neigh: hardif neighbour information
2211 static void
2212 batadv_iv_hardif_neigh_print(struct seq_file *seq,
2213 struct batadv_hardif_neigh_node *hardif_neigh)
2215 int last_secs, last_msecs;
2217 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2218 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2220 seq_printf(seq, " %10s %pM %4i.%03is\n",
2221 hardif_neigh->if_incoming->net_dev->name,
2222 hardif_neigh->addr, last_secs, last_msecs);
2226 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
2227 * @bat_priv: the bat priv with all the soft interface information
2228 * @seq: neighbour table seq_file struct
2230 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2231 struct seq_file *seq)
2233 struct net_device *net_dev = (struct net_device *)seq->private;
2234 struct batadv_hardif_neigh_node *hardif_neigh;
2235 struct batadv_hard_iface *hard_iface;
2236 int batman_count = 0;
2238 seq_puts(seq, " IF Neighbor last-seen\n");
2240 rcu_read_lock();
2241 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2242 if (hard_iface->soft_iface != net_dev)
2243 continue;
2245 hlist_for_each_entry_rcu(hardif_neigh,
2246 &hard_iface->neigh_list, list) {
2247 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2248 batman_count++;
2251 rcu_read_unlock();
2253 if (batman_count == 0)
2254 seq_puts(seq, "No batman nodes in range ...\n");
2256 #endif
2259 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
2260 * @neigh1: the first neighbor object of the comparison
2261 * @if_outgoing1: outgoing interface for the first neighbor
2262 * @neigh2: the second neighbor object of the comparison
2263 * @if_outgoing2: outgoing interface for the second neighbor
2264 * @diff: pointer to integer receiving the calculated difference
2266 * The content of *@diff is only valid when this function returns true.
2267 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2268 * the same as or higher than the metric via neigh2
2270 * Return: true when the difference could be calculated, false otherwise
2272 static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2273 struct batadv_hard_iface *if_outgoing1,
2274 struct batadv_neigh_node *neigh2,
2275 struct batadv_hard_iface *if_outgoing2,
2276 int *diff)
2278 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2279 u8 tq1, tq2;
2280 bool ret = true;
2282 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2283 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2285 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2286 ret = false;
2287 goto out;
2290 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2291 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2292 *diff = (int)tq1 - (int)tq2;
2294 out:
2295 if (neigh1_ifinfo)
2296 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2297 if (neigh2_ifinfo)
2298 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2300 return ret;
2304 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
2305 * @msg: Netlink message to dump into
2306 * @portid: Port making netlink request
2307 * @seq: Sequence number of netlink message
2308 * @hardif_neigh: Neighbour to be dumped
2310 * Return: Error code, or 0 on success
2312 static int
2313 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2314 struct batadv_hardif_neigh_node *hardif_neigh)
2316 void *hdr;
2317 unsigned int last_seen_msecs;
2319 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2321 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2322 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2323 if (!hdr)
2324 return -ENOBUFS;
2326 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2327 hardif_neigh->addr) ||
2328 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2329 hardif_neigh->if_incoming->net_dev->ifindex) ||
2330 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2331 last_seen_msecs))
2332 goto nla_put_failure;
2334 genlmsg_end(msg, hdr);
2335 return 0;
2337 nla_put_failure:
2338 genlmsg_cancel(msg, hdr);
2339 return -EMSGSIZE;
2343 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
2344 * into a message
2345 * @msg: Netlink message to dump into
2346 * @portid: Port making netlink request
2347 * @seq: Sequence number of netlink message
2348 * @bat_priv: The bat priv with all the soft interface information
2349 * @hard_iface: Hard interface to dump the neighbours for
2350 * @idx_s: Number of entries to skip
2352 * This function assumes the caller holds rcu_read_lock().
2354 * Return: Error code, or 0 on success
2356 static int
2357 batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2358 struct batadv_priv *bat_priv,
2359 struct batadv_hard_iface *hard_iface,
2360 int *idx_s)
2362 struct batadv_hardif_neigh_node *hardif_neigh;
2363 int idx = 0;
2365 hlist_for_each_entry_rcu(hardif_neigh,
2366 &hard_iface->neigh_list, list) {
2367 if (idx++ < *idx_s)
2368 continue;
2370 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2371 hardif_neigh)) {
2372 *idx_s = idx - 1;
2373 return -EMSGSIZE;
2377 *idx_s = 0;
2378 return 0;
2382 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
2383 * @msg: Netlink message to dump into
2384 * @cb: Control block containing additional options
2385 * @bat_priv: The bat priv with all the soft interface information
2386 * @single_hardif: Limit dump to this hard interfaace
2388 static void
2389 batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2390 struct batadv_priv *bat_priv,
2391 struct batadv_hard_iface *single_hardif)
2393 struct batadv_hard_iface *hard_iface;
2394 int i_hardif = 0;
2395 int i_hardif_s = cb->args[0];
2396 int idx = cb->args[1];
2397 int portid = NETLINK_CB(cb->skb).portid;
2399 rcu_read_lock();
2400 if (single_hardif) {
2401 if (i_hardif_s == 0) {
2402 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2403 cb->nlh->nlmsg_seq,
2404 bat_priv,
2405 single_hardif,
2406 &idx) == 0)
2407 i_hardif++;
2409 } else {
2410 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2411 list) {
2412 if (hard_iface->soft_iface != bat_priv->soft_iface)
2413 continue;
2415 if (i_hardif++ < i_hardif_s)
2416 continue;
2418 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2419 cb->nlh->nlmsg_seq,
2420 bat_priv,
2421 hard_iface, &idx)) {
2422 i_hardif--;
2423 break;
2427 rcu_read_unlock();
2429 cb->args[0] = i_hardif;
2430 cb->args[1] = idx;
2434 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
2435 * @neigh1: the first neighbor object of the comparison
2436 * @if_outgoing1: outgoing interface for the first neighbor
2437 * @neigh2: the second neighbor object of the comparison
2438 * @if_outgoing2: outgoing interface for the second neighbor
2440 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2441 * lower, the same as or higher than the metric via neigh2
2443 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2444 struct batadv_hard_iface *if_outgoing1,
2445 struct batadv_neigh_node *neigh2,
2446 struct batadv_hard_iface *if_outgoing2)
2448 bool ret;
2449 int diff;
2451 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2452 if_outgoing2, &diff);
2453 if (!ret)
2454 return 0;
2456 return diff;
2460 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
2461 * than neigh2 from the metric prospective
2462 * @neigh1: the first neighbor object of the comparison
2463 * @if_outgoing1: outgoing interface for the first neighbor
2464 * @neigh2: the second neighbor object of the comparison
2465 * @if_outgoing2: outgoing interface for the second neighbor
2467 * Return: true if the metric via neigh1 is equally good or better than
2468 * the metric via neigh2, false otherwise.
2470 static bool
2471 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2472 struct batadv_hard_iface *if_outgoing1,
2473 struct batadv_neigh_node *neigh2,
2474 struct batadv_hard_iface *if_outgoing2)
2476 bool ret;
2477 int diff;
2479 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2480 if_outgoing2, &diff);
2481 if (!ret)
2482 return false;
2484 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
2485 return ret;
2488 static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2490 /* begin scheduling originator messages on that interface */
2491 batadv_iv_ogm_schedule(hard_iface);
2495 * batadv_iv_init_sel_class() - initialize GW selection class
2496 * @bat_priv: the bat priv with all the soft interface information
2498 static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2500 /* set default TQ difference threshold to 20 */
2501 atomic_set(&bat_priv->gw.sel_class, 20);
2504 static struct batadv_gw_node *
2505 batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2507 struct batadv_neigh_node *router;
2508 struct batadv_neigh_ifinfo *router_ifinfo;
2509 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2510 u64 max_gw_factor = 0;
2511 u64 tmp_gw_factor = 0;
2512 u8 max_tq = 0;
2513 u8 tq_avg;
2514 struct batadv_orig_node *orig_node;
2516 rcu_read_lock();
2517 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2518 orig_node = gw_node->orig_node;
2519 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2520 if (!router)
2521 continue;
2523 router_ifinfo = batadv_neigh_ifinfo_get(router,
2524 BATADV_IF_DEFAULT);
2525 if (!router_ifinfo)
2526 goto next;
2528 if (!kref_get_unless_zero(&gw_node->refcount))
2529 goto next;
2531 tq_avg = router_ifinfo->bat_iv.tq_avg;
2533 switch (atomic_read(&bat_priv->gw.sel_class)) {
2534 case 1: /* fast connection */
2535 tmp_gw_factor = tq_avg * tq_avg;
2536 tmp_gw_factor *= gw_node->bandwidth_down;
2537 tmp_gw_factor *= 100 * 100;
2538 tmp_gw_factor >>= 18;
2540 if (tmp_gw_factor > max_gw_factor ||
2541 (tmp_gw_factor == max_gw_factor &&
2542 tq_avg > max_tq)) {
2543 if (curr_gw)
2544 batadv_gw_node_put(curr_gw);
2545 curr_gw = gw_node;
2546 kref_get(&curr_gw->refcount);
2548 break;
2550 default: /* 2: stable connection (use best statistic)
2551 * 3: fast-switch (use best statistic but change as
2552 * soon as a better gateway appears)
2553 * XX: late-switch (use best statistic but change as
2554 * soon as a better gateway appears which has
2555 * $routing_class more tq points)
2557 if (tq_avg > max_tq) {
2558 if (curr_gw)
2559 batadv_gw_node_put(curr_gw);
2560 curr_gw = gw_node;
2561 kref_get(&curr_gw->refcount);
2563 break;
2566 if (tq_avg > max_tq)
2567 max_tq = tq_avg;
2569 if (tmp_gw_factor > max_gw_factor)
2570 max_gw_factor = tmp_gw_factor;
2572 batadv_gw_node_put(gw_node);
2574 next:
2575 batadv_neigh_node_put(router);
2576 if (router_ifinfo)
2577 batadv_neigh_ifinfo_put(router_ifinfo);
2579 rcu_read_unlock();
2581 return curr_gw;
2584 static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2585 struct batadv_orig_node *curr_gw_orig,
2586 struct batadv_orig_node *orig_node)
2588 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2589 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2590 struct batadv_neigh_node *router_gw = NULL;
2591 struct batadv_neigh_node *router_orig = NULL;
2592 u8 gw_tq_avg, orig_tq_avg;
2593 bool ret = false;
2595 /* dynamic re-election is performed only on fast or late switch */
2596 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2597 return false;
2599 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2600 if (!router_gw) {
2601 ret = true;
2602 goto out;
2605 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2606 BATADV_IF_DEFAULT);
2607 if (!router_gw_ifinfo) {
2608 ret = true;
2609 goto out;
2612 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2613 if (!router_orig)
2614 goto out;
2616 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2617 BATADV_IF_DEFAULT);
2618 if (!router_orig_ifinfo)
2619 goto out;
2621 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2622 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2624 /* the TQ value has to be better */
2625 if (orig_tq_avg < gw_tq_avg)
2626 goto out;
2628 /* if the routing class is greater than 3 the value tells us how much
2629 * greater the TQ value of the new gateway must be
2631 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2632 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2633 goto out;
2635 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2636 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2637 gw_tq_avg, orig_tq_avg);
2639 ret = true;
2640 out:
2641 if (router_gw_ifinfo)
2642 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2643 if (router_orig_ifinfo)
2644 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2645 if (router_gw)
2646 batadv_neigh_node_put(router_gw);
2647 if (router_orig)
2648 batadv_neigh_node_put(router_orig);
2650 return ret;
2653 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2654 /* fails if orig_node has no router */
2655 static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2656 struct seq_file *seq,
2657 const struct batadv_gw_node *gw_node)
2659 struct batadv_gw_node *curr_gw;
2660 struct batadv_neigh_node *router;
2661 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2662 int ret = -1;
2664 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2665 if (!router)
2666 goto out;
2668 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2669 if (!router_ifinfo)
2670 goto out;
2672 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2674 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2675 (curr_gw == gw_node ? "=>" : " "),
2676 gw_node->orig_node->orig,
2677 router_ifinfo->bat_iv.tq_avg, router->addr,
2678 router->if_incoming->net_dev->name,
2679 gw_node->bandwidth_down / 10,
2680 gw_node->bandwidth_down % 10,
2681 gw_node->bandwidth_up / 10,
2682 gw_node->bandwidth_up % 10);
2683 ret = seq_has_overflowed(seq) ? -1 : 0;
2685 if (curr_gw)
2686 batadv_gw_node_put(curr_gw);
2687 out:
2688 if (router_ifinfo)
2689 batadv_neigh_ifinfo_put(router_ifinfo);
2690 if (router)
2691 batadv_neigh_node_put(router);
2692 return ret;
2695 static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2696 struct seq_file *seq)
2698 struct batadv_gw_node *gw_node;
2699 int gw_count = 0;
2701 seq_puts(seq,
2702 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2704 rcu_read_lock();
2705 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2706 /* fails if orig_node has no router */
2707 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2708 continue;
2710 gw_count++;
2712 rcu_read_unlock();
2714 if (gw_count == 0)
2715 seq_puts(seq, "No gateways in range ...\n");
2717 #endif
2720 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
2721 * @msg: Netlink message to dump into
2722 * @portid: Port making netlink request
2723 * @seq: Sequence number of netlink message
2724 * @bat_priv: The bat priv with all the soft interface information
2725 * @gw_node: Gateway to be dumped
2727 * Return: Error code, or 0 on success
2729 static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2730 struct batadv_priv *bat_priv,
2731 struct batadv_gw_node *gw_node)
2733 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2734 struct batadv_neigh_node *router;
2735 struct batadv_gw_node *curr_gw;
2736 int ret = 0;
2737 void *hdr;
2739 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2740 if (!router)
2741 goto out;
2743 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2744 if (!router_ifinfo)
2745 goto out;
2747 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2749 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2750 NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
2751 if (!hdr) {
2752 ret = -ENOBUFS;
2753 goto out;
2756 ret = -EMSGSIZE;
2758 if (curr_gw == gw_node)
2759 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2760 genlmsg_cancel(msg, hdr);
2761 goto out;
2764 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2765 gw_node->orig_node->orig) ||
2766 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2767 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2768 router->addr) ||
2769 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2770 router->if_incoming->net_dev->name) ||
2771 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2772 gw_node->bandwidth_down) ||
2773 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2774 gw_node->bandwidth_up)) {
2775 genlmsg_cancel(msg, hdr);
2776 goto out;
2779 genlmsg_end(msg, hdr);
2780 ret = 0;
2782 out:
2783 if (router_ifinfo)
2784 batadv_neigh_ifinfo_put(router_ifinfo);
2785 if (router)
2786 batadv_neigh_node_put(router);
2787 return ret;
2791 * batadv_iv_gw_dump() - Dump gateways into a message
2792 * @msg: Netlink message to dump into
2793 * @cb: Control block containing additional options
2794 * @bat_priv: The bat priv with all the soft interface information
2796 static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2797 struct batadv_priv *bat_priv)
2799 int portid = NETLINK_CB(cb->skb).portid;
2800 struct batadv_gw_node *gw_node;
2801 int idx_skip = cb->args[0];
2802 int idx = 0;
2804 rcu_read_lock();
2805 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
2806 if (idx++ < idx_skip)
2807 continue;
2809 if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
2810 bat_priv, gw_node)) {
2811 idx_skip = idx - 1;
2812 goto unlock;
2816 idx_skip = idx;
2817 unlock:
2818 rcu_read_unlock();
2820 cb->args[0] = idx_skip;
2823 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2824 .name = "BATMAN_IV",
2825 .iface = {
2826 .activate = batadv_iv_iface_activate,
2827 .enable = batadv_iv_ogm_iface_enable,
2828 .disable = batadv_iv_ogm_iface_disable,
2829 .update_mac = batadv_iv_ogm_iface_update_mac,
2830 .primary_set = batadv_iv_ogm_primary_iface_set,
2832 .neigh = {
2833 .cmp = batadv_iv_ogm_neigh_cmp,
2834 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2835 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2836 .print = batadv_iv_neigh_print,
2837 #endif
2838 .dump = batadv_iv_ogm_neigh_dump,
2840 .orig = {
2841 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2842 .print = batadv_iv_ogm_orig_print,
2843 #endif
2844 .dump = batadv_iv_ogm_orig_dump,
2845 .free = batadv_iv_ogm_orig_free,
2846 .add_if = batadv_iv_ogm_orig_add_if,
2847 .del_if = batadv_iv_ogm_orig_del_if,
2849 .gw = {
2850 .init_sel_class = batadv_iv_init_sel_class,
2851 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2852 .is_eligible = batadv_iv_gw_is_eligible,
2853 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2854 .print = batadv_iv_gw_print,
2855 #endif
2856 .dump = batadv_iv_gw_dump,
2861 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2863 * Return: 0 on success or negative error number in case of failure
2865 int __init batadv_iv_init(void)
2867 int ret;
2869 /* batman originator packet */
2870 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2871 batadv_iv_ogm_receive);
2872 if (ret < 0)
2873 goto out;
2875 ret = batadv_algo_register(&batadv_batman_iv);
2876 if (ret < 0)
2877 goto handler_unregister;
2879 goto out;
2881 handler_unregister:
2882 batadv_recv_handler_unregister(BATADV_IV_OGM);
2883 out:
2884 return ret;