1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2019 B.A.T.M.A.N. contributors:
4 * Linus Lüssing, Marek Lindner
10 #include <linux/atomic.h>
11 #include <linux/cache.h>
12 #include <linux/errno.h>
13 #include <linux/if_ether.h>
14 #include <linux/init.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/netlink.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/seq_file.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/stddef.h>
27 #include <linux/types.h>
28 #include <linux/workqueue.h>
29 #include <net/genetlink.h>
30 #include <net/netlink.h>
31 #include <uapi/linux/batadv_packet.h>
32 #include <uapi/linux/batman_adv.h>
35 #include "bat_v_elp.h"
36 #include "bat_v_ogm.h"
37 #include "gateway_client.h"
38 #include "gateway_common.h"
39 #include "hard-interface.h"
43 #include "originator.h"
45 static void batadv_v_iface_activate(struct batadv_hard_iface
*hard_iface
)
47 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
48 struct batadv_hard_iface
*primary_if
;
50 primary_if
= batadv_primary_if_get_selected(bat_priv
);
53 batadv_v_elp_iface_activate(primary_if
, hard_iface
);
54 batadv_hardif_put(primary_if
);
57 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
58 * set the interface as ACTIVE right away, without any risk of race
61 if (hard_iface
->if_status
== BATADV_IF_TO_BE_ACTIVATED
)
62 hard_iface
->if_status
= BATADV_IF_ACTIVE
;
65 static int batadv_v_iface_enable(struct batadv_hard_iface
*hard_iface
)
69 ret
= batadv_v_elp_iface_enable(hard_iface
);
73 ret
= batadv_v_ogm_iface_enable(hard_iface
);
75 batadv_v_elp_iface_disable(hard_iface
);
80 static void batadv_v_iface_disable(struct batadv_hard_iface
*hard_iface
)
82 batadv_v_elp_iface_disable(hard_iface
);
85 static void batadv_v_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
87 batadv_v_elp_primary_iface_set(hard_iface
);
88 batadv_v_ogm_primary_iface_set(hard_iface
);
92 * batadv_v_iface_update_mac() - react to hard-interface MAC address change
93 * @hard_iface: the modified interface
95 * If the modified interface is the primary one, update the originator
96 * address in the ELP and OGM messages to reflect the new MAC address.
98 static void batadv_v_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
100 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
101 struct batadv_hard_iface
*primary_if
;
103 primary_if
= batadv_primary_if_get_selected(bat_priv
);
104 if (primary_if
!= hard_iface
)
107 batadv_v_primary_iface_set(hard_iface
);
110 batadv_hardif_put(primary_if
);
114 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node
*hardif_neigh
)
116 ewma_throughput_init(&hardif_neigh
->bat_v
.throughput
);
117 INIT_WORK(&hardif_neigh
->bat_v
.metric_work
,
118 batadv_v_elp_throughput_metric_update
);
121 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
123 * batadv_v_orig_print_neigh() - print neighbors for the originator table
124 * @orig_node: the orig_node for which the neighbors are printed
125 * @if_outgoing: outgoing interface for these entries
126 * @seq: debugfs table seq_file struct
128 * Must be called while holding an rcu lock.
131 batadv_v_orig_print_neigh(struct batadv_orig_node
*orig_node
,
132 struct batadv_hard_iface
*if_outgoing
,
133 struct seq_file
*seq
)
135 struct batadv_neigh_node
*neigh_node
;
136 struct batadv_neigh_ifinfo
*n_ifinfo
;
138 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
139 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
143 seq_printf(seq
, " %pM (%9u.%1u)",
145 n_ifinfo
->bat_v
.throughput
/ 10,
146 n_ifinfo
->bat_v
.throughput
% 10);
148 batadv_neigh_ifinfo_put(n_ifinfo
);
153 * batadv_v_hardif_neigh_print() - print a single ELP neighbour node
154 * @seq: neighbour table seq_file struct
155 * @hardif_neigh: hardif neighbour information
158 batadv_v_hardif_neigh_print(struct seq_file
*seq
,
159 struct batadv_hardif_neigh_node
*hardif_neigh
)
161 int last_secs
, last_msecs
;
164 last_secs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) / 1000;
165 last_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) % 1000;
166 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
168 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
169 hardif_neigh
->addr
, last_secs
, last_msecs
, throughput
/ 10,
170 throughput
% 10, hardif_neigh
->if_incoming
->net_dev
->name
);
174 * batadv_v_neigh_print() - print the single hop neighbour list
175 * @bat_priv: the bat priv with all the soft interface information
176 * @seq: neighbour table seq_file struct
178 static void batadv_v_neigh_print(struct batadv_priv
*bat_priv
,
179 struct seq_file
*seq
)
181 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
182 struct batadv_hardif_neigh_node
*hardif_neigh
;
183 struct batadv_hard_iface
*hard_iface
;
184 int batman_count
= 0;
187 " Neighbor last-seen ( throughput) [ IF]\n");
190 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
191 if (hard_iface
->soft_iface
!= net_dev
)
194 hlist_for_each_entry_rcu(hardif_neigh
,
195 &hard_iface
->neigh_list
, list
) {
196 batadv_v_hardif_neigh_print(seq
, hardif_neigh
);
202 if (batman_count
== 0)
203 seq_puts(seq
, "No batman nodes in range ...\n");
208 * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
209 * @msg: Netlink message to dump into
210 * @portid: Port making netlink request
211 * @seq: Sequence number of netlink message
212 * @hardif_neigh: Neighbour to dump
214 * Return: Error code, or 0 on success
217 batadv_v_neigh_dump_neigh(struct sk_buff
*msg
, u32 portid
, u32 seq
,
218 struct batadv_hardif_neigh_node
*hardif_neigh
)
221 unsigned int last_seen_msecs
;
224 last_seen_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
);
225 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
226 throughput
= throughput
* 100;
228 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
229 BATADV_CMD_GET_NEIGHBORS
);
233 if (nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
234 hardif_neigh
->addr
) ||
235 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
236 hardif_neigh
->if_incoming
->net_dev
->ifindex
) ||
237 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
239 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
))
240 goto nla_put_failure
;
242 genlmsg_end(msg
, hdr
);
246 genlmsg_cancel(msg
, hdr
);
251 * batadv_v_neigh_dump_hardif() - Dump the neighbours of a hard interface into
253 * @msg: Netlink message to dump into
254 * @portid: Port making netlink request
255 * @seq: Sequence number of netlink message
256 * @bat_priv: The bat priv with all the soft interface information
257 * @hard_iface: The hard interface to be dumped
258 * @idx_s: Entries to be skipped
260 * This function assumes the caller holds rcu_read_lock().
262 * Return: Error code, or 0 on success
265 batadv_v_neigh_dump_hardif(struct sk_buff
*msg
, u32 portid
, u32 seq
,
266 struct batadv_priv
*bat_priv
,
267 struct batadv_hard_iface
*hard_iface
,
270 struct batadv_hardif_neigh_node
*hardif_neigh
;
273 hlist_for_each_entry_rcu(hardif_neigh
,
274 &hard_iface
->neigh_list
, list
) {
278 if (batadv_v_neigh_dump_neigh(msg
, portid
, seq
, hardif_neigh
)) {
289 * batadv_v_neigh_dump() - Dump the neighbours of a hard interface into a
291 * @msg: Netlink message to dump into
292 * @cb: Control block containing additional options
293 * @bat_priv: The bat priv with all the soft interface information
294 * @single_hardif: Limit dumping to this hard interface
297 batadv_v_neigh_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
298 struct batadv_priv
*bat_priv
,
299 struct batadv_hard_iface
*single_hardif
)
301 struct batadv_hard_iface
*hard_iface
;
303 int i_hardif_s
= cb
->args
[0];
304 int idx
= cb
->args
[1];
305 int portid
= NETLINK_CB(cb
->skb
).portid
;
309 if (i_hardif_s
== 0) {
310 if (batadv_v_neigh_dump_hardif(msg
, portid
,
312 bat_priv
, single_hardif
,
317 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
318 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
321 if (i_hardif
++ < i_hardif_s
)
324 if (batadv_v_neigh_dump_hardif(msg
, portid
,
326 bat_priv
, hard_iface
,
335 cb
->args
[0] = i_hardif
;
339 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
341 * batadv_v_orig_print() - print the originator table
342 * @bat_priv: the bat priv with all the soft interface information
343 * @seq: debugfs table seq_file struct
344 * @if_outgoing: the outgoing interface for which this should be printed
346 static void batadv_v_orig_print(struct batadv_priv
*bat_priv
,
347 struct seq_file
*seq
,
348 struct batadv_hard_iface
*if_outgoing
)
350 struct batadv_neigh_node
*neigh_node
;
351 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
352 int last_seen_msecs
, last_seen_secs
;
353 struct batadv_orig_node
*orig_node
;
354 struct batadv_neigh_ifinfo
*n_ifinfo
;
355 unsigned long last_seen_jiffies
;
356 struct hlist_head
*head
;
357 int batman_count
= 0;
361 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
363 for (i
= 0; i
< hash
->size
; i
++) {
364 head
= &hash
->table
[i
];
367 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
368 neigh_node
= batadv_orig_router_get(orig_node
,
373 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
378 last_seen_jiffies
= jiffies
- orig_node
->last_seen
;
379 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
380 last_seen_secs
= last_seen_msecs
/ 1000;
381 last_seen_msecs
= last_seen_msecs
% 1000;
383 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
384 orig_node
->orig
, last_seen_secs
,
386 n_ifinfo
->bat_v
.throughput
/ 10,
387 n_ifinfo
->bat_v
.throughput
% 10,
389 neigh_node
->if_incoming
->net_dev
->name
);
391 batadv_v_orig_print_neigh(orig_node
, if_outgoing
, seq
);
396 batadv_neigh_node_put(neigh_node
);
398 batadv_neigh_ifinfo_put(n_ifinfo
);
403 if (batman_count
== 0)
404 seq_puts(seq
, "No batman nodes in range ...\n");
409 * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
410 * @msg: Netlink message to dump into
411 * @portid: Port making netlink request
412 * @seq: Sequence number of netlink message
413 * @bat_priv: The bat priv with all the soft interface information
414 * @if_outgoing: Limit dump to entries with this outgoing interface
415 * @orig_node: Originator to dump
416 * @neigh_node: Single hops neighbour
417 * @best: Is the best originator
419 * Return: Error code, or 0 on success
422 batadv_v_orig_dump_subentry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
423 struct batadv_priv
*bat_priv
,
424 struct batadv_hard_iface
*if_outgoing
,
425 struct batadv_orig_node
*orig_node
,
426 struct batadv_neigh_node
*neigh_node
,
429 struct batadv_neigh_ifinfo
*n_ifinfo
;
430 unsigned int last_seen_msecs
;
434 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
438 throughput
= n_ifinfo
->bat_v
.throughput
* 100;
440 batadv_neigh_ifinfo_put(n_ifinfo
);
442 last_seen_msecs
= jiffies_to_msecs(jiffies
- orig_node
->last_seen
);
444 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
445 if_outgoing
!= neigh_node
->if_incoming
)
448 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
449 BATADV_CMD_GET_ORIGINATORS
);
453 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
, orig_node
->orig
) ||
454 nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
456 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
457 neigh_node
->if_incoming
->net_dev
->ifindex
) ||
458 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
) ||
459 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
461 goto nla_put_failure
;
463 if (best
&& nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
))
464 goto nla_put_failure
;
466 genlmsg_end(msg
, hdr
);
470 genlmsg_cancel(msg
, hdr
);
475 * batadv_v_orig_dump_entry() - Dump an originator entry into a message
476 * @msg: Netlink message to dump into
477 * @portid: Port making netlink request
478 * @seq: Sequence number of netlink message
479 * @bat_priv: The bat priv with all the soft interface information
480 * @if_outgoing: Limit dump to entries with this outgoing interface
481 * @orig_node: Originator to dump
482 * @sub_s: Number of sub entries to skip
484 * This function assumes the caller holds rcu_read_lock().
486 * Return: Error code, or 0 on success
489 batadv_v_orig_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
490 struct batadv_priv
*bat_priv
,
491 struct batadv_hard_iface
*if_outgoing
,
492 struct batadv_orig_node
*orig_node
, int *sub_s
)
494 struct batadv_neigh_node
*neigh_node_best
;
495 struct batadv_neigh_node
*neigh_node
;
499 neigh_node_best
= batadv_orig_router_get(orig_node
, if_outgoing
);
500 if (!neigh_node_best
)
503 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
507 best
= (neigh_node
== neigh_node_best
);
509 if (batadv_v_orig_dump_subentry(msg
, portid
, seq
, bat_priv
,
510 if_outgoing
, orig_node
,
512 batadv_neigh_node_put(neigh_node_best
);
521 batadv_neigh_node_put(neigh_node_best
);
528 * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
529 * @msg: Netlink message to dump into
530 * @portid: Port making netlink request
531 * @seq: Sequence number of netlink message
532 * @bat_priv: The bat priv with all the soft interface information
533 * @if_outgoing: Limit dump to entries with this outgoing interface
534 * @head: Bucket to be dumped
535 * @idx_s: Number of entries to be skipped
536 * @sub: Number of sub entries to be skipped
538 * Return: Error code, or 0 on success
541 batadv_v_orig_dump_bucket(struct sk_buff
*msg
, u32 portid
, u32 seq
,
542 struct batadv_priv
*bat_priv
,
543 struct batadv_hard_iface
*if_outgoing
,
544 struct hlist_head
*head
, int *idx_s
, int *sub
)
546 struct batadv_orig_node
*orig_node
;
550 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
554 if (batadv_v_orig_dump_entry(msg
, portid
, seq
, bat_priv
,
555 if_outgoing
, orig_node
, sub
)) {
569 * batadv_v_orig_dump() - Dump the originators into a message
570 * @msg: Netlink message to dump into
571 * @cb: Control block containing additional options
572 * @bat_priv: The bat priv with all the soft interface information
573 * @if_outgoing: Limit dump to entries with this outgoing interface
576 batadv_v_orig_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
577 struct batadv_priv
*bat_priv
,
578 struct batadv_hard_iface
*if_outgoing
)
580 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
581 struct hlist_head
*head
;
582 int bucket
= cb
->args
[0];
583 int idx
= cb
->args
[1];
584 int sub
= cb
->args
[2];
585 int portid
= NETLINK_CB(cb
->skb
).portid
;
587 while (bucket
< hash
->size
) {
588 head
= &hash
->table
[bucket
];
590 if (batadv_v_orig_dump_bucket(msg
, portid
,
592 bat_priv
, if_outgoing
, head
, &idx
,
599 cb
->args
[0] = bucket
;
604 static int batadv_v_neigh_cmp(struct batadv_neigh_node
*neigh1
,
605 struct batadv_hard_iface
*if_outgoing1
,
606 struct batadv_neigh_node
*neigh2
,
607 struct batadv_hard_iface
*if_outgoing2
)
609 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
612 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
616 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
620 ret
= ifinfo1
->bat_v
.throughput
- ifinfo2
->bat_v
.throughput
;
622 batadv_neigh_ifinfo_put(ifinfo2
);
624 batadv_neigh_ifinfo_put(ifinfo1
);
629 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node
*neigh1
,
630 struct batadv_hard_iface
*if_outgoing1
,
631 struct batadv_neigh_node
*neigh2
,
632 struct batadv_hard_iface
*if_outgoing2
)
634 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
638 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
642 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
646 threshold
= ifinfo1
->bat_v
.throughput
/ 4;
647 threshold
= ifinfo1
->bat_v
.throughput
- threshold
;
649 ret
= ifinfo2
->bat_v
.throughput
> threshold
;
651 batadv_neigh_ifinfo_put(ifinfo2
);
653 batadv_neigh_ifinfo_put(ifinfo1
);
659 * batadv_v_init_sel_class() - initialize GW selection class
660 * @bat_priv: the bat priv with all the soft interface information
662 static void batadv_v_init_sel_class(struct batadv_priv
*bat_priv
)
664 /* set default throughput difference threshold to 5Mbps */
665 atomic_set(&bat_priv
->gw
.sel_class
, 50);
668 static ssize_t
batadv_v_store_sel_class(struct batadv_priv
*bat_priv
,
669 char *buff
, size_t count
)
671 u32 old_class
, class;
673 if (!batadv_parse_throughput(bat_priv
->soft_iface
, buff
,
674 "B.A.T.M.A.N. V GW selection class",
678 old_class
= atomic_read(&bat_priv
->gw
.sel_class
);
679 atomic_set(&bat_priv
->gw
.sel_class
, class);
681 if (old_class
!= class)
682 batadv_gw_reselect(bat_priv
);
687 static ssize_t
batadv_v_show_sel_class(struct batadv_priv
*bat_priv
, char *buff
)
689 u32
class = atomic_read(&bat_priv
->gw
.sel_class
);
691 return sprintf(buff
, "%u.%u MBit\n", class / 10, class % 10);
695 * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
696 * @gw_node: the GW to retrieve the metric for
697 * @bw: the pointer where the metric will be stored. The metric is computed as
698 * the minimum between the GW advertised throughput and the path throughput to
701 * Return: 0 on success, -1 on failure
703 static int batadv_v_gw_throughput_get(struct batadv_gw_node
*gw_node
, u32
*bw
)
705 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
706 struct batadv_orig_node
*orig_node
;
707 struct batadv_neigh_node
*router
;
710 orig_node
= gw_node
->orig_node
;
711 router
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
715 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
719 /* the GW metric is computed as the minimum between the path throughput
720 * to reach the GW itself and the advertised bandwidth.
721 * This gives us an approximation of the effective throughput that the
722 * client can expect via this particular GW node
724 *bw
= router_ifinfo
->bat_v
.throughput
;
725 *bw
= min_t(u32
, *bw
, gw_node
->bandwidth_down
);
730 batadv_neigh_node_put(router
);
732 batadv_neigh_ifinfo_put(router_ifinfo
);
738 * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
739 * @bat_priv: the bat priv with all the soft interface information
741 * Return: the GW node having the best GW-metric, NULL if no GW is known
743 static struct batadv_gw_node
*
744 batadv_v_gw_get_best_gw_node(struct batadv_priv
*bat_priv
)
746 struct batadv_gw_node
*gw_node
, *curr_gw
= NULL
;
750 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
751 if (!kref_get_unless_zero(&gw_node
->refcount
))
754 if (batadv_v_gw_throughput_get(gw_node
, &bw
) < 0)
757 if (curr_gw
&& bw
<= max_bw
)
761 batadv_gw_node_put(curr_gw
);
764 kref_get(&curr_gw
->refcount
);
768 batadv_gw_node_put(gw_node
);
776 * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
777 * @bat_priv: the bat priv with all the soft interface information
778 * @curr_gw_orig: originator representing the currently selected GW
779 * @orig_node: the originator representing the new candidate
781 * Return: true if orig_node can be selected as current GW, false otherwise
783 static bool batadv_v_gw_is_eligible(struct batadv_priv
*bat_priv
,
784 struct batadv_orig_node
*curr_gw_orig
,
785 struct batadv_orig_node
*orig_node
)
787 struct batadv_gw_node
*curr_gw
, *orig_gw
= NULL
;
788 u32 gw_throughput
, orig_throughput
, threshold
;
791 threshold
= atomic_read(&bat_priv
->gw
.sel_class
);
793 curr_gw
= batadv_gw_node_get(bat_priv
, curr_gw_orig
);
799 if (batadv_v_gw_throughput_get(curr_gw
, &gw_throughput
) < 0) {
804 orig_gw
= batadv_gw_node_get(bat_priv
, orig_node
);
808 if (batadv_v_gw_throughput_get(orig_gw
, &orig_throughput
) < 0)
811 if (orig_throughput
< gw_throughput
)
814 if ((orig_throughput
- gw_throughput
) < threshold
)
817 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
818 "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
819 gw_throughput
, orig_throughput
);
824 batadv_gw_node_put(curr_gw
);
826 batadv_gw_node_put(orig_gw
);
831 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
832 /* fails if orig_node has no router */
833 static int batadv_v_gw_write_buffer_text(struct batadv_priv
*bat_priv
,
834 struct seq_file
*seq
,
835 const struct batadv_gw_node
*gw_node
)
837 struct batadv_gw_node
*curr_gw
;
838 struct batadv_neigh_node
*router
;
839 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
842 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
846 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
850 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
852 seq_printf(seq
, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
853 (curr_gw
== gw_node
? "=>" : " "),
854 gw_node
->orig_node
->orig
,
855 router_ifinfo
->bat_v
.throughput
/ 10,
856 router_ifinfo
->bat_v
.throughput
% 10, router
->addr
,
857 router
->if_incoming
->net_dev
->name
,
858 gw_node
->bandwidth_down
/ 10,
859 gw_node
->bandwidth_down
% 10,
860 gw_node
->bandwidth_up
/ 10,
861 gw_node
->bandwidth_up
% 10);
862 ret
= seq_has_overflowed(seq
) ? -1 : 0;
865 batadv_gw_node_put(curr_gw
);
868 batadv_neigh_ifinfo_put(router_ifinfo
);
870 batadv_neigh_node_put(router
);
875 * batadv_v_gw_print() - print the gateway list
876 * @bat_priv: the bat priv with all the soft interface information
877 * @seq: gateway table seq_file struct
879 static void batadv_v_gw_print(struct batadv_priv
*bat_priv
,
880 struct seq_file
*seq
)
882 struct batadv_gw_node
*gw_node
;
886 " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
889 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
890 /* fails if orig_node has no router */
891 if (batadv_v_gw_write_buffer_text(bat_priv
, seq
, gw_node
) < 0)
899 seq_puts(seq
, "No gateways in range ...\n");
904 * batadv_v_gw_dump_entry() - Dump a gateway into a message
905 * @msg: Netlink message to dump into
906 * @portid: Port making netlink request
907 * @cb: Control block containing additional options
908 * @bat_priv: The bat priv with all the soft interface information
909 * @gw_node: Gateway to be dumped
911 * Return: Error code, or 0 on success
913 static int batadv_v_gw_dump_entry(struct sk_buff
*msg
, u32 portid
,
914 struct netlink_callback
*cb
,
915 struct batadv_priv
*bat_priv
,
916 struct batadv_gw_node
*gw_node
)
918 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
919 struct batadv_neigh_node
*router
;
920 struct batadv_gw_node
*curr_gw
= NULL
;
924 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
928 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
932 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
934 hdr
= genlmsg_put(msg
, portid
, cb
->nlh
->nlmsg_seq
,
935 &batadv_netlink_family
, NLM_F_MULTI
,
936 BATADV_CMD_GET_GATEWAYS
);
942 genl_dump_check_consistent(cb
, hdr
);
946 if (curr_gw
== gw_node
) {
947 if (nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
)) {
948 genlmsg_cancel(msg
, hdr
);
953 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
954 gw_node
->orig_node
->orig
)) {
955 genlmsg_cancel(msg
, hdr
);
959 if (nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
,
960 router_ifinfo
->bat_v
.throughput
)) {
961 genlmsg_cancel(msg
, hdr
);
965 if (nla_put(msg
, BATADV_ATTR_ROUTER
, ETH_ALEN
, router
->addr
)) {
966 genlmsg_cancel(msg
, hdr
);
970 if (nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
971 router
->if_incoming
->net_dev
->name
)) {
972 genlmsg_cancel(msg
, hdr
);
976 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_DOWN
,
977 gw_node
->bandwidth_down
)) {
978 genlmsg_cancel(msg
, hdr
);
982 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_UP
, gw_node
->bandwidth_up
)) {
983 genlmsg_cancel(msg
, hdr
);
987 genlmsg_end(msg
, hdr
);
992 batadv_gw_node_put(curr_gw
);
994 batadv_neigh_ifinfo_put(router_ifinfo
);
996 batadv_neigh_node_put(router
);
1001 * batadv_v_gw_dump() - Dump gateways into a message
1002 * @msg: Netlink message to dump into
1003 * @cb: Control block containing additional options
1004 * @bat_priv: The bat priv with all the soft interface information
1006 static void batadv_v_gw_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
1007 struct batadv_priv
*bat_priv
)
1009 int portid
= NETLINK_CB(cb
->skb
).portid
;
1010 struct batadv_gw_node
*gw_node
;
1011 int idx_skip
= cb
->args
[0];
1014 spin_lock_bh(&bat_priv
->gw
.list_lock
);
1015 cb
->seq
= bat_priv
->gw
.generation
<< 1 | 1;
1017 hlist_for_each_entry(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
1018 if (idx
++ < idx_skip
)
1021 if (batadv_v_gw_dump_entry(msg
, portid
, cb
, bat_priv
,
1030 spin_unlock_bh(&bat_priv
->gw
.list_lock
);
1032 cb
->args
[0] = idx_skip
;
1035 static struct batadv_algo_ops batadv_batman_v __read_mostly
= {
1038 .activate
= batadv_v_iface_activate
,
1039 .enable
= batadv_v_iface_enable
,
1040 .disable
= batadv_v_iface_disable
,
1041 .update_mac
= batadv_v_iface_update_mac
,
1042 .primary_set
= batadv_v_primary_iface_set
,
1045 .hardif_init
= batadv_v_hardif_neigh_init
,
1046 .cmp
= batadv_v_neigh_cmp
,
1047 .is_similar_or_better
= batadv_v_neigh_is_sob
,
1048 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1049 .print
= batadv_v_neigh_print
,
1051 .dump
= batadv_v_neigh_dump
,
1054 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1055 .print
= batadv_v_orig_print
,
1057 .dump
= batadv_v_orig_dump
,
1060 .init_sel_class
= batadv_v_init_sel_class
,
1061 .store_sel_class
= batadv_v_store_sel_class
,
1062 .show_sel_class
= batadv_v_show_sel_class
,
1063 .get_best_gw_node
= batadv_v_gw_get_best_gw_node
,
1064 .is_eligible
= batadv_v_gw_is_eligible
,
1065 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1066 .print
= batadv_v_gw_print
,
1068 .dump
= batadv_v_gw_dump
,
1073 * batadv_v_hardif_init() - initialize the algorithm specific fields in the
1074 * hard-interface object
1075 * @hard_iface: the hard-interface to initialize
1077 void batadv_v_hardif_init(struct batadv_hard_iface
*hard_iface
)
1079 /* enable link throughput auto-detection by setting the throughput
1082 atomic_set(&hard_iface
->bat_v
.throughput_override
, 0);
1083 atomic_set(&hard_iface
->bat_v
.elp_interval
, 500);
1087 * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
1089 * @bat_priv: the object representing the mesh interface to initialise
1091 * Return: 0 on success or a negative error code otherwise
1093 int batadv_v_mesh_init(struct batadv_priv
*bat_priv
)
1097 ret
= batadv_v_ogm_init(bat_priv
);
1105 * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
1106 * @bat_priv: the object representing the mesh interface to free
1108 void batadv_v_mesh_free(struct batadv_priv
*bat_priv
)
1110 batadv_v_ogm_free(bat_priv
);
1114 * batadv_v_init() - B.A.T.M.A.N. V initialization function
1116 * Description: Takes care of initializing all the subcomponents.
1117 * It is invoked upon module load only.
1119 * Return: 0 on success or a negative error code otherwise
1121 int __init
batadv_v_init(void)
1125 /* B.A.T.M.A.N. V echo location protocol packet */
1126 ret
= batadv_recv_handler_register(BATADV_ELP
,
1127 batadv_v_elp_packet_recv
);
1131 ret
= batadv_recv_handler_register(BATADV_OGM2
,
1132 batadv_v_ogm_packet_recv
);
1134 goto elp_unregister
;
1136 ret
= batadv_algo_register(&batadv_batman_v
);
1138 goto ogm_unregister
;
1143 batadv_recv_handler_unregister(BATADV_OGM2
);
1146 batadv_recv_handler_unregister(BATADV_ELP
);