1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "bat_iv_ogm.h"
22 #include <linux/atomic.h>
23 #include <linux/bitmap.h>
24 #include <linux/bitops.h>
25 #include <linux/bug.h>
26 #include <linux/byteorder/generic.h>
27 #include <linux/cache.h>
28 #include <linux/errno.h>
29 #include <linux/etherdevice.h>
30 #include <linux/gfp.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/jiffies.h>
34 #include <linux/kernel.h>
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/lockdep.h>
38 #include <linux/netdevice.h>
39 #include <linux/netlink.h>
40 #include <linux/pkt_sched.h>
41 #include <linux/printk.h>
42 #include <linux/random.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/types.h>
52 #include <linux/workqueue.h>
53 #include <net/genetlink.h>
54 #include <net/netlink.h>
55 #include <uapi/linux/batadv_packet.h>
56 #include <uapi/linux/batman_adv.h>
60 #include "gateway_client.h"
61 #include "hard-interface.h"
65 #include "network-coding.h"
66 #include "originator.h"
69 #include "translation-table.h"
72 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct
*work
);
75 * enum batadv_dup_status - duplicate status
77 enum batadv_dup_status
{
78 /** @BATADV_NO_DUP: the packet is no duplicate */
82 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
87 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
91 * @BATADV_PROTECTED: originator is currently protected (after reboot)
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
[])
124 while (i
< BATADV_TQ_GLOBAL_WINDOW_SIZE
) {
137 return (u8
)(sum
/ count
);
141 * batadv_iv_ogm_orig_free() - free the private resources allocated for this
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
)
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),
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
);
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
;
191 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
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
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
)
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
);
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
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
)
249 lockdep_assert_held(&orig_node
->bat_iv
.ogm_cnt_lock
);
251 data_ptr
= kmalloc_array(max_if_num
, sizeof(u8
), GFP_ATOMIC
);
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
;
292 batadv_iv_ogm_drop_bcast_own_entry(orig_node
, max_if_num
,
294 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node
, max_if_num
,
298 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
304 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
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
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
;
320 orig_node
= batadv_orig_hash_find(bat_priv
, addr
);
324 orig_node
= batadv_orig_node_new(bat_priv
, addr
);
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
)
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
)
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
);
345 goto free_orig_node_hash
;
350 batadv_orig_node_put(orig_node
);
352 batadv_orig_node_put(orig_node
);
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
);
370 neigh_node
->orig_node
= orig_neigh
;
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
;
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
);
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
;
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
);
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 */
434 batadv_iv_ogm_emit_send_time(const struct batadv_priv
*bat_priv
)
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
);
456 new_tq
= tq
* (BATADV_TQ_MAX_VALUE
- hop_penalty
);
457 new_tq
/= BATADV_TQ_MAX_VALUE
;
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
,
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
);
490 struct batadv_ogm_packet
*batadv_ogm_packet
;
494 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
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
;
512 batadv_ogm_packet
->flags
&= ~BATADV_DIRECTLINK
;
514 if (packet_num
> 0 || !forw_packet
->own
)
515 fwd_str
= "Forwarding";
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
) ?
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
);
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
);
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");
557 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
559 if (WARN_ON(!forw_packet
->if_outgoing
))
562 if (WARN_ON(forw_packet
->if_outgoing
->soft_iface
!= soft_iface
))
565 if (forw_packet
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
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
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
,
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
;
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
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
))
617 if (aggregated_bytes
> BATADV_MAX_AGGREGATION_BYTES
)
620 /* packet is not leaving on the same interface. */
621 if (forw_packet
->if_outgoing
!= if_outgoing
)
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
631 primary_if
= batadv_primary_if_get_selected(bat_priv
);
635 /* packets without direct link flag and high TTL
636 * are flooded through the net
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
)) {
651 /* if the incoming packet is sent via this one
652 * interface only - we still can aggregate
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
||
664 forw_packet
->if_incoming
!= primary_if
))) {
671 batadv_hardif_put(primary_if
);
676 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
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
,
689 struct batadv_hard_iface
*if_incoming
,
690 struct batadv_hard_iface
*if_outgoing
,
693 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
694 struct batadv_forw_packet
*forw_packet_aggr
;
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
;
704 skb_size
= packet_len
;
706 skb_size
+= ETH_HLEN
;
708 skb
= netdev_alloc_skb_ip_align(NULL
, skb_size
);
712 forw_packet_aggr
= batadv_forw_packet_alloc(if_incoming
, if_outgoing
,
713 queue_left
, bat_priv
, skb
);
714 if (!forw_packet_aggr
) {
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 */
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 */
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
,
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
;
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
,
800 forw_packet_aggr
= forw_packet_pos
;
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
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
,
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
);
842 if (batadv_ogm_packet
->ttl
<= 1) {
843 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
, "ttl exceeded\n");
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
;
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
,
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
;
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
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
;
902 for (i
= 0; i
< hash
->size
; i
++) {
903 head
= &hash
->table
[i
];
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
);
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
;
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
)
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
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
,
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
);
978 /* OGMs from primary interfaces are scheduled on all
982 list_for_each_entry_rcu(tmp_hard_iface
, &batadv_hardif_list
, list
) {
983 if (tmp_hard_iface
->soft_iface
!= hard_iface
->soft_iface
)
986 if (!kref_get_unless_zero(&tmp_hard_iface
->refcount
))
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
);
999 batadv_hardif_put(primary_if
);
1003 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
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.
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
;
1035 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1036 "%s(): Searching and updating originator entry of received packet\n",
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
;
1052 if (dup_status
!= BATADV_NO_DUP
)
1055 /* only update the entry for this outgoing interface */
1056 neigh_ifinfo
= batadv_neigh_ifinfo_get(tmp_neigh_node
,
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
;
1073 struct batadv_orig_node
*orig_tmp
;
1075 orig_tmp
= batadv_iv_ogm_orig_get(bat_priv
, ethhdr
->h_source
);
1079 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
1081 orig_node
, orig_tmp
);
1083 batadv_orig_node_put(orig_tmp
);
1087 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1088 "Updating existing last-hop neighbor of originator\n");
1092 neigh_ifinfo
= batadv_neigh_ifinfo_new(neigh_node
, if_outgoing
);
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
1114 router
= batadv_orig_router_get(orig_node
, if_outgoing
);
1115 if (router
== neigh_node
)
1119 router_ifinfo
= batadv_neigh_ifinfo_get(router
, if_outgoing
);
1123 /* if this neighbor does not offer a better TQ we won't
1126 if (router_ifinfo
->bat_iv
.tq_avg
> neigh_ifinfo
->bat_iv
.tq_avg
)
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
)
1151 batadv_update_route(bat_priv
, orig_node
, if_outgoing
, neigh_node
);
1158 batadv_neigh_node_put(neigh_node
);
1160 batadv_neigh_node_put(router
);
1162 batadv_neigh_ifinfo_put(neigh_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
;
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
;
1195 /* find corresponding one hop neighbor */
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
))
1203 if (tmp_neigh_node
->if_incoming
!= if_incoming
)
1206 if (!kref_get_unless_zero(&tmp_neigh_node
->refcount
))
1209 neigh_node
= tmp_neigh_node
;
1215 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
1216 orig_neigh_node
->orig
,
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
);
1235 neigh_rq_count
= neigh_ifinfo
->bat_iv
.real_packet_count
;
1236 batadv_neigh_ifinfo_put(neigh_ifinfo
);
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
;
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
)
1255 /* neigh_node->real_packet_count is never zero as we
1256 * only purge old information when getting new
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
,
1285 combined_tq
= batadv_ogm_packet
->tq
*
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
)
1309 batadv_neigh_node_put(neigh_node
);
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
;
1336 bool need_update
= false;
1338 enum batadv_dup_status ret
= BATADV_NO_DUP
;
1339 u32 seqno
= ntohl(batadv_ogm_packet
->seqno
);
1342 unsigned long *bitmap
;
1344 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, batadv_ogm_packet
->orig
);
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
);
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
;
1367 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
1368 neigh_ifinfo
= batadv_neigh_ifinfo_new(neigh_node
,
1373 neigh_addr
= neigh_node
->addr
;
1374 is_dup
= batadv_test_bit(neigh_ifinfo
->bat_iv
.real_bits
,
1375 orig_ifinfo
->last_real_seqno
,
1378 if (batadv_compare_eth(neigh_addr
, ethhdr
->h_source
) &&
1379 neigh_node
->if_incoming
== if_incoming
) {
1382 ret
= BATADV_NEIGH_DUP
;
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
);
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
;
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
);
1417 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
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
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
;
1449 /* create a private copy of the skb, as some functions change tq value
1452 skb_priv
= skb_copy(skb
, GFP_ATOMIC
);
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",
1471 if (ogm_packet
->tq
== 0) {
1472 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1473 "Drop packet: originator packet with tq equal 0\n");
1477 if (is_single_hop_neigh
) {
1478 hardif_neigh
= batadv_hardif_neigh_get(if_incoming
,
1481 hardif_neigh
->last_seen
= jiffies
;
1484 router
= batadv_orig_router_get(orig_node
, if_outgoing
);
1486 router_router
= batadv_orig_router_get(router
->orig_node
,
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",
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
1513 if (is_single_hop_neigh
)
1514 orig_neigh_node
= orig_node
;
1516 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1519 if (!orig_neigh_node
)
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
,
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");
1538 is_bidirect
= batadv_iv_ogm_calc_tq(orig_node
, orig_neigh_node
,
1539 ogm_packet
, if_incoming
,
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
);
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
)
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");
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
,
1582 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1583 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1587 /* multihop originator */
1589 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1590 "Drop packet: not received via bidirectional link\n");
1594 if (dup_status
== BATADV_NEIGH_DUP
) {
1595 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1596 "Drop packet: duplicate packet received\n");
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
);
1607 if (orig_neigh_node
&& !is_single_hop_neigh
)
1608 batadv_orig_node_put(orig_neigh_node
);
1611 batadv_neigh_ifinfo_put(router_ifinfo
);
1613 batadv_neigh_node_put(router
);
1615 batadv_neigh_node_put(router_router
);
1616 if (orig_neigh_router
)
1617 batadv_neigh_node_put(orig_neigh_router
);
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
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
)
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;
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
);
1679 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1680 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1683 if (hard_iface
->soft_iface
!= if_incoming
->soft_iface
)
1686 if (batadv_compare_eth(ethhdr
->h_source
,
1687 hard_iface
->net_dev
->dev_addr
))
1690 if (batadv_compare_eth(ogm_packet
->orig
,
1691 hard_iface
->net_dev
->dev_addr
))
1694 if (batadv_compare_eth(ogm_packet
->prev_sender
,
1695 hard_iface
->net_dev
->dev_addr
))
1696 is_my_oldorig
= true;
1701 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1702 "Drop packet: received my own broadcast (sender: %pM)\n",
1708 unsigned long *word
;
1711 unsigned int if_num
;
1714 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1716 if (!orig_neigh_node
)
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
);
1746 if (is_my_oldorig
) {
1747 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1748 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
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",
1760 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, ogm_packet
->orig
);
1764 batadv_iv_ogm_process_per_outif(skb
, ogm_offset
, orig_node
,
1765 if_incoming
, BATADV_IF_DEFAULT
);
1768 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1769 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1772 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1775 if (!kref_get_unless_zero(&hard_iface
->refcount
))
1778 batadv_iv_ogm_process_per_outif(skb
, ogm_offset
, orig_node
,
1779 if_incoming
, hard_iface
);
1781 batadv_hardif_put(hard_iface
);
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
,
1798 bat_priv
= netdev_priv(forw_packet
->if_incoming
->soft_iface
);
1800 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_DEACTIVATING
) {
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
);
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
;
1834 int ret
= NET_RX_DROP
;
1836 res
= batadv_check_management_packet(skb
, if_incoming
, BATADV_OGM_HLEN
);
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
)
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
);
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
;
1868 if (ret
== NET_RX_SUCCESS
)
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.
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
);
1898 seq_printf(seq
, " %pM (%3i)",
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;
1927 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1929 for (i
= 0; i
< hash
->size
; i
++) {
1930 head
= &hash
->table
[i
];
1933 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1934 neigh_node
= batadv_orig_router_get(orig_node
,
1939 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
1944 if (n_ifinfo
->bat_iv
.tq_avg
== 0)
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
,
1956 neigh_node
->if_incoming
->net_dev
->name
);
1958 batadv_iv_ogm_orig_print_neigh(orig_node
, if_outgoing
,
1960 seq_putc(seq
, '\n');
1964 batadv_neigh_node_put(neigh_node
);
1966 batadv_neigh_ifinfo_put(n_ifinfo
);
1971 if (batman_count
== 0)
1972 seq_puts(seq
, "No batman nodes in range ...\n");
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.
1986 batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node
*neigh_node
,
1987 struct batadv_hard_iface
*if_outgoing
,
1990 struct batadv_neigh_ifinfo
*n_ifinfo
;
1992 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
1996 *tq_avg
= n_ifinfo
->bat_iv
.tq_avg
;
1997 batadv_neigh_ifinfo_put(n_ifinfo
);
2003 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
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
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
,
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
))
2033 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
2034 if_outgoing
!= neigh_node
->if_incoming
)
2037 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
,
2038 NLM_F_MULTI
, BATADV_CMD_GET_ORIGINATORS
);
2042 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
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
,
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
);
2060 genlmsg_cancel(msg
, hdr
);
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
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
;
2090 neigh_node_best
= batadv_orig_router_get(orig_node
, if_outgoing
);
2091 if (!neigh_node_best
)
2094 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best
, if_outgoing
,
2098 if (tq_avg_best
== 0)
2101 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
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
,
2111 batadv_neigh_node_put(neigh_node_best
);
2119 if (neigh_node_best
)
2120 batadv_neigh_node_put(neigh_node_best
);
2127 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
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
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
;
2150 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
2154 if (batadv_iv_ogm_orig_dump_entry(msg
, portid
, seq
, bat_priv
,
2155 if_outgoing
, orig_node
,
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
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
,
2193 bat_priv
, if_outgoing
, head
,
2200 cb
->args
[0] = bucket
;
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
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");
2241 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
2242 if (hard_iface
->soft_iface
!= net_dev
)
2245 hlist_for_each_entry_rcu(hardif_neigh
,
2246 &hard_iface
->neigh_list
, list
) {
2247 batadv_iv_hardif_neigh_print(seq
, hardif_neigh
);
2253 if (batman_count
== 0)
2254 seq_puts(seq
, "No batman nodes in range ...\n");
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
,
2278 struct batadv_neigh_ifinfo
*neigh1_ifinfo
, *neigh2_ifinfo
;
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
) {
2290 tq1
= neigh1_ifinfo
->bat_iv
.tq_avg
;
2291 tq2
= neigh2_ifinfo
->bat_iv
.tq_avg
;
2292 *diff
= (int)tq1
- (int)tq2
;
2296 batadv_neigh_ifinfo_put(neigh1_ifinfo
);
2298 batadv_neigh_ifinfo_put(neigh2_ifinfo
);
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
2313 batadv_iv_ogm_neigh_dump_neigh(struct sk_buff
*msg
, u32 portid
, u32 seq
,
2314 struct batadv_hardif_neigh_node
*hardif_neigh
)
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
);
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
,
2332 goto nla_put_failure
;
2334 genlmsg_end(msg
, hdr
);
2338 genlmsg_cancel(msg
, hdr
);
2343 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
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
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
,
2362 struct batadv_hardif_neigh_node
*hardif_neigh
;
2365 hlist_for_each_entry_rcu(hardif_neigh
,
2366 &hard_iface
->neigh_list
, list
) {
2370 if (batadv_iv_ogm_neigh_dump_neigh(msg
, portid
, seq
,
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
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
;
2395 int i_hardif_s
= cb
->args
[0];
2396 int idx
= cb
->args
[1];
2397 int portid
= NETLINK_CB(cb
->skb
).portid
;
2400 if (single_hardif
) {
2401 if (i_hardif_s
== 0) {
2402 if (batadv_iv_ogm_neigh_dump_hardif(msg
, portid
,
2410 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
,
2412 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
2415 if (i_hardif
++ < i_hardif_s
)
2418 if (batadv_iv_ogm_neigh_dump_hardif(msg
, portid
,
2421 hard_iface
, &idx
)) {
2429 cb
->args
[0] = i_hardif
;
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
)
2451 ret
= batadv_iv_ogm_neigh_diff(neigh1
, if_outgoing1
, neigh2
,
2452 if_outgoing2
, &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.
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
)
2479 ret
= batadv_iv_ogm_neigh_diff(neigh1
, if_outgoing1
, neigh2
,
2480 if_outgoing2
, &diff
);
2484 ret
= diff
> -BATADV_TQ_SIMILARITY_THRESHOLD
;
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;
2514 struct batadv_orig_node
*orig_node
;
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
);
2523 router_ifinfo
= batadv_neigh_ifinfo_get(router
,
2528 if (!kref_get_unless_zero(&gw_node
->refcount
))
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
&&
2544 batadv_gw_node_put(curr_gw
);
2546 kref_get(&curr_gw
->refcount
);
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
) {
2559 batadv_gw_node_put(curr_gw
);
2561 kref_get(&curr_gw
->refcount
);
2566 if (tq_avg
> max_tq
)
2569 if (tmp_gw_factor
> max_gw_factor
)
2570 max_gw_factor
= tmp_gw_factor
;
2572 batadv_gw_node_put(gw_node
);
2575 batadv_neigh_node_put(router
);
2577 batadv_neigh_ifinfo_put(router_ifinfo
);
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
;
2595 /* dynamic re-election is performed only on fast or late switch */
2596 if (atomic_read(&bat_priv
->gw
.sel_class
) <= 2)
2599 router_gw
= batadv_orig_router_get(curr_gw_orig
, BATADV_IF_DEFAULT
);
2605 router_gw_ifinfo
= batadv_neigh_ifinfo_get(router_gw
,
2607 if (!router_gw_ifinfo
) {
2612 router_orig
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
2616 router_orig_ifinfo
= batadv_neigh_ifinfo_get(router_orig
,
2618 if (!router_orig_ifinfo
)
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
)
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
)))
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
);
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
);
2646 batadv_neigh_node_put(router_gw
);
2648 batadv_neigh_node_put(router_orig
);
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
;
2664 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
2668 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
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;
2686 batadv_gw_node_put(curr_gw
);
2689 batadv_neigh_ifinfo_put(router_ifinfo
);
2691 batadv_neigh_node_put(router
);
2695 static void batadv_iv_gw_print(struct batadv_priv
*bat_priv
,
2696 struct seq_file
*seq
)
2698 struct batadv_gw_node
*gw_node
;
2702 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
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)
2715 seq_puts(seq
, "No gateways in range ...\n");
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
;
2739 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
2743 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
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
);
2758 if (curr_gw
== gw_node
)
2759 if (nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
)) {
2760 genlmsg_cancel(msg
, hdr
);
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
,
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
);
2779 genlmsg_end(msg
, hdr
);
2784 batadv_neigh_ifinfo_put(router_ifinfo
);
2786 batadv_neigh_node_put(router
);
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];
2805 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
2806 if (idx
++ < idx_skip
)
2809 if (batadv_iv_gw_dump_entry(msg
, portid
, cb
->nlh
->nlmsg_seq
,
2810 bat_priv
, gw_node
)) {
2820 cb
->args
[0] = idx_skip
;
2823 static struct batadv_algo_ops batadv_batman_iv __read_mostly
= {
2824 .name
= "BATMAN_IV",
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
,
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
,
2838 .dump
= batadv_iv_ogm_neigh_dump
,
2841 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2842 .print
= batadv_iv_ogm_orig_print
,
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
,
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
,
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)
2869 /* batman originator packet */
2870 ret
= batadv_recv_handler_register(BATADV_IV_OGM
,
2871 batadv_iv_ogm_receive
);
2875 ret
= batadv_algo_register(&batadv_batman_iv
);
2877 goto handler_unregister
;
2882 batadv_recv_handler_unregister(BATADV_IV_OGM
);