xref: /linux/net/batman-adv/bat_v.c (revision 3213486f2e442831e324cc6201a2f9e924ecc235)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2019  B.A.T.M.A.N. contributors:
3  *
4  * Linus Lüssing, Marek Lindner
5  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 #include "bat_v.h"
20 #include "main.h"
21 
22 #include <linux/atomic.h>
23 #include <linux/cache.h>
24 #include <linux/errno.h>
25 #include <linux/if_ether.h>
26 #include <linux/init.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/list.h>
31 #include <linux/netdevice.h>
32 #include <linux/netlink.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/seq_file.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/types.h>
39 #include <linux/workqueue.h>
40 #include <net/genetlink.h>
41 #include <net/netlink.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
44 
45 #include "bat_algo.h"
46 #include "bat_v_elp.h"
47 #include "bat_v_ogm.h"
48 #include "gateway_client.h"
49 #include "gateway_common.h"
50 #include "hard-interface.h"
51 #include "hash.h"
52 #include "log.h"
53 #include "netlink.h"
54 #include "originator.h"
55 
56 struct sk_buff;
57 
58 static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
59 {
60 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
61 	struct batadv_hard_iface *primary_if;
62 
63 	primary_if = batadv_primary_if_get_selected(bat_priv);
64 
65 	if (primary_if) {
66 		batadv_v_elp_iface_activate(primary_if, hard_iface);
67 		batadv_hardif_put(primary_if);
68 	}
69 
70 	/* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
71 	 * set the interface as ACTIVE right away, without any risk of race
72 	 * condition
73 	 */
74 	if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
75 		hard_iface->if_status = BATADV_IF_ACTIVE;
76 }
77 
78 static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
79 {
80 	int ret;
81 
82 	ret = batadv_v_elp_iface_enable(hard_iface);
83 	if (ret < 0)
84 		return ret;
85 
86 	ret = batadv_v_ogm_iface_enable(hard_iface);
87 	if (ret < 0)
88 		batadv_v_elp_iface_disable(hard_iface);
89 
90 	return ret;
91 }
92 
93 static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
94 {
95 	batadv_v_elp_iface_disable(hard_iface);
96 }
97 
98 static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
99 {
100 	batadv_v_elp_primary_iface_set(hard_iface);
101 	batadv_v_ogm_primary_iface_set(hard_iface);
102 }
103 
104 /**
105  * batadv_v_iface_update_mac() - react to hard-interface MAC address change
106  * @hard_iface: the modified interface
107  *
108  * If the modified interface is the primary one, update the originator
109  * address in the ELP and OGM messages to reflect the new MAC address.
110  */
111 static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
112 {
113 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
114 	struct batadv_hard_iface *primary_if;
115 
116 	primary_if = batadv_primary_if_get_selected(bat_priv);
117 	if (primary_if != hard_iface)
118 		goto out;
119 
120 	batadv_v_primary_iface_set(hard_iface);
121 out:
122 	if (primary_if)
123 		batadv_hardif_put(primary_if);
124 }
125 
126 static void
127 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
128 {
129 	ewma_throughput_init(&hardif_neigh->bat_v.throughput);
130 	INIT_WORK(&hardif_neigh->bat_v.metric_work,
131 		  batadv_v_elp_throughput_metric_update);
132 }
133 
134 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
135 /**
136  * batadv_v_orig_print_neigh() - print neighbors for the originator table
137  * @orig_node: the orig_node for which the neighbors are printed
138  * @if_outgoing: outgoing interface for these entries
139  * @seq: debugfs table seq_file struct
140  *
141  * Must be called while holding an rcu lock.
142  */
143 static void
144 batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
145 			  struct batadv_hard_iface *if_outgoing,
146 			  struct seq_file *seq)
147 {
148 	struct batadv_neigh_node *neigh_node;
149 	struct batadv_neigh_ifinfo *n_ifinfo;
150 
151 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
152 		n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
153 		if (!n_ifinfo)
154 			continue;
155 
156 		seq_printf(seq, " %pM (%9u.%1u)",
157 			   neigh_node->addr,
158 			   n_ifinfo->bat_v.throughput / 10,
159 			   n_ifinfo->bat_v.throughput % 10);
160 
161 		batadv_neigh_ifinfo_put(n_ifinfo);
162 	}
163 }
164 
165 /**
166  * batadv_v_hardif_neigh_print() - print a single ELP neighbour node
167  * @seq: neighbour table seq_file struct
168  * @hardif_neigh: hardif neighbour information
169  */
170 static void
171 batadv_v_hardif_neigh_print(struct seq_file *seq,
172 			    struct batadv_hardif_neigh_node *hardif_neigh)
173 {
174 	int last_secs, last_msecs;
175 	u32 throughput;
176 
177 	last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
178 	last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
179 	throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
180 
181 	seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
182 		   hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
183 		   throughput % 10, hardif_neigh->if_incoming->net_dev->name);
184 }
185 
186 /**
187  * batadv_v_neigh_print() - print the single hop neighbour list
188  * @bat_priv: the bat priv with all the soft interface information
189  * @seq: neighbour table seq_file struct
190  */
191 static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
192 				 struct seq_file *seq)
193 {
194 	struct net_device *net_dev = (struct net_device *)seq->private;
195 	struct batadv_hardif_neigh_node *hardif_neigh;
196 	struct batadv_hard_iface *hard_iface;
197 	int batman_count = 0;
198 
199 	seq_puts(seq,
200 		 "  Neighbor        last-seen ( throughput) [        IF]\n");
201 
202 	rcu_read_lock();
203 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
204 		if (hard_iface->soft_iface != net_dev)
205 			continue;
206 
207 		hlist_for_each_entry_rcu(hardif_neigh,
208 					 &hard_iface->neigh_list, list) {
209 			batadv_v_hardif_neigh_print(seq, hardif_neigh);
210 			batman_count++;
211 		}
212 	}
213 	rcu_read_unlock();
214 
215 	if (batman_count == 0)
216 		seq_puts(seq, "No batman nodes in range ...\n");
217 }
218 #endif
219 
220 /**
221  * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
222  * @msg: Netlink message to dump into
223  * @portid: Port making netlink request
224  * @seq: Sequence number of netlink message
225  * @hardif_neigh: Neighbour to dump
226  *
227  * Return: Error code, or 0 on success
228  */
229 static int
230 batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
231 			  struct batadv_hardif_neigh_node *hardif_neigh)
232 {
233 	void *hdr;
234 	unsigned int last_seen_msecs;
235 	u32 throughput;
236 
237 	last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
238 	throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
239 	throughput = throughput * 100;
240 
241 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
242 			  BATADV_CMD_GET_NEIGHBORS);
243 	if (!hdr)
244 		return -ENOBUFS;
245 
246 	if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
247 		    hardif_neigh->addr) ||
248 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
249 			hardif_neigh->if_incoming->net_dev->ifindex) ||
250 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
251 			last_seen_msecs) ||
252 	    nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
253 		goto nla_put_failure;
254 
255 	genlmsg_end(msg, hdr);
256 	return 0;
257 
258  nla_put_failure:
259 	genlmsg_cancel(msg, hdr);
260 	return -EMSGSIZE;
261 }
262 
263 /**
264  * batadv_v_neigh_dump_hardif() - Dump the  neighbours of a hard interface into
265  *  a message
266  * @msg: Netlink message to dump into
267  * @portid: Port making netlink request
268  * @seq: Sequence number of netlink message
269  * @bat_priv: The bat priv with all the soft interface information
270  * @hard_iface: The hard interface to be dumped
271  * @idx_s: Entries to be skipped
272  *
273  * This function assumes the caller holds rcu_read_lock().
274  *
275  * Return: Error code, or 0 on success
276  */
277 static int
278 batadv_v_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
279 			   struct batadv_priv *bat_priv,
280 			   struct batadv_hard_iface *hard_iface,
281 			   int *idx_s)
282 {
283 	struct batadv_hardif_neigh_node *hardif_neigh;
284 	int idx = 0;
285 
286 	hlist_for_each_entry_rcu(hardif_neigh,
287 				 &hard_iface->neigh_list, list) {
288 		if (idx++ < *idx_s)
289 			continue;
290 
291 		if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
292 			*idx_s = idx - 1;
293 			return -EMSGSIZE;
294 		}
295 	}
296 
297 	*idx_s = 0;
298 	return 0;
299 }
300 
301 /**
302  * batadv_v_neigh_dump() - Dump the neighbours of a hard interface  into a
303  *  message
304  * @msg: Netlink message to dump into
305  * @cb: Control block containing additional options
306  * @bat_priv: The bat priv with all the soft interface information
307  * @single_hardif: Limit dumping to this hard interface
308  */
309 static void
310 batadv_v_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
311 		    struct batadv_priv *bat_priv,
312 		    struct batadv_hard_iface *single_hardif)
313 {
314 	struct batadv_hard_iface *hard_iface;
315 	int i_hardif = 0;
316 	int i_hardif_s = cb->args[0];
317 	int idx = cb->args[1];
318 	int portid = NETLINK_CB(cb->skb).portid;
319 
320 	rcu_read_lock();
321 	if (single_hardif) {
322 		if (i_hardif_s == 0) {
323 			if (batadv_v_neigh_dump_hardif(msg, portid,
324 						       cb->nlh->nlmsg_seq,
325 						       bat_priv, single_hardif,
326 						       &idx) == 0)
327 				i_hardif++;
328 		}
329 	} else {
330 		list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
331 			if (hard_iface->soft_iface != bat_priv->soft_iface)
332 				continue;
333 
334 			if (i_hardif++ < i_hardif_s)
335 				continue;
336 
337 			if (batadv_v_neigh_dump_hardif(msg, portid,
338 						       cb->nlh->nlmsg_seq,
339 						       bat_priv, hard_iface,
340 						       &idx)) {
341 				i_hardif--;
342 				break;
343 			}
344 		}
345 	}
346 	rcu_read_unlock();
347 
348 	cb->args[0] = i_hardif;
349 	cb->args[1] = idx;
350 }
351 
352 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
353 /**
354  * batadv_v_orig_print() - print the originator table
355  * @bat_priv: the bat priv with all the soft interface information
356  * @seq: debugfs table seq_file struct
357  * @if_outgoing: the outgoing interface for which this should be printed
358  */
359 static void batadv_v_orig_print(struct batadv_priv *bat_priv,
360 				struct seq_file *seq,
361 				struct batadv_hard_iface *if_outgoing)
362 {
363 	struct batadv_neigh_node *neigh_node;
364 	struct batadv_hashtable *hash = bat_priv->orig_hash;
365 	int last_seen_msecs, last_seen_secs;
366 	struct batadv_orig_node *orig_node;
367 	struct batadv_neigh_ifinfo *n_ifinfo;
368 	unsigned long last_seen_jiffies;
369 	struct hlist_head *head;
370 	int batman_count = 0;
371 	u32 i;
372 
373 	seq_puts(seq,
374 		 "  Originator      last-seen ( throughput)           Nexthop [outgoingIF]:   Potential nexthops ...\n");
375 
376 	for (i = 0; i < hash->size; i++) {
377 		head = &hash->table[i];
378 
379 		rcu_read_lock();
380 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
381 			neigh_node = batadv_orig_router_get(orig_node,
382 							    if_outgoing);
383 			if (!neigh_node)
384 				continue;
385 
386 			n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
387 							   if_outgoing);
388 			if (!n_ifinfo)
389 				goto next;
390 
391 			last_seen_jiffies = jiffies - orig_node->last_seen;
392 			last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
393 			last_seen_secs = last_seen_msecs / 1000;
394 			last_seen_msecs = last_seen_msecs % 1000;
395 
396 			seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
397 				   orig_node->orig, last_seen_secs,
398 				   last_seen_msecs,
399 				   n_ifinfo->bat_v.throughput / 10,
400 				   n_ifinfo->bat_v.throughput % 10,
401 				   neigh_node->addr,
402 				   neigh_node->if_incoming->net_dev->name);
403 
404 			batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
405 			seq_putc(seq, '\n');
406 			batman_count++;
407 
408 next:
409 			batadv_neigh_node_put(neigh_node);
410 			if (n_ifinfo)
411 				batadv_neigh_ifinfo_put(n_ifinfo);
412 		}
413 		rcu_read_unlock();
414 	}
415 
416 	if (batman_count == 0)
417 		seq_puts(seq, "No batman nodes in range ...\n");
418 }
419 #endif
420 
421 /**
422  * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
423  * @msg: Netlink message to dump into
424  * @portid: Port making netlink request
425  * @seq: Sequence number of netlink message
426  * @bat_priv: The bat priv with all the soft interface information
427  * @if_outgoing: Limit dump to entries with this outgoing interface
428  * @orig_node: Originator to dump
429  * @neigh_node: Single hops neighbour
430  * @best: Is the best originator
431  *
432  * Return: Error code, or 0 on success
433  */
434 static int
435 batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
436 			    struct batadv_priv *bat_priv,
437 			    struct batadv_hard_iface *if_outgoing,
438 			    struct batadv_orig_node *orig_node,
439 			    struct batadv_neigh_node *neigh_node,
440 			    bool best)
441 {
442 	struct batadv_neigh_ifinfo *n_ifinfo;
443 	unsigned int last_seen_msecs;
444 	u32 throughput;
445 	void *hdr;
446 
447 	n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
448 	if (!n_ifinfo)
449 		return 0;
450 
451 	throughput = n_ifinfo->bat_v.throughput * 100;
452 
453 	batadv_neigh_ifinfo_put(n_ifinfo);
454 
455 	last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
456 
457 	if (if_outgoing != BATADV_IF_DEFAULT &&
458 	    if_outgoing != neigh_node->if_incoming)
459 		return 0;
460 
461 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
462 			  BATADV_CMD_GET_ORIGINATORS);
463 	if (!hdr)
464 		return -ENOBUFS;
465 
466 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
467 	    nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
468 		    neigh_node->addr) ||
469 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
470 			neigh_node->if_incoming->net_dev->ifindex) ||
471 	    nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
472 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
473 			last_seen_msecs))
474 		goto nla_put_failure;
475 
476 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
477 		goto nla_put_failure;
478 
479 	genlmsg_end(msg, hdr);
480 	return 0;
481 
482  nla_put_failure:
483 	genlmsg_cancel(msg, hdr);
484 	return -EMSGSIZE;
485 }
486 
487 /**
488  * batadv_v_orig_dump_entry() - Dump an originator entry into a message
489  * @msg: Netlink message to dump into
490  * @portid: Port making netlink request
491  * @seq: Sequence number of netlink message
492  * @bat_priv: The bat priv with all the soft interface information
493  * @if_outgoing: Limit dump to entries with this outgoing interface
494  * @orig_node: Originator to dump
495  * @sub_s: Number of sub entries to skip
496  *
497  * This function assumes the caller holds rcu_read_lock().
498  *
499  * Return: Error code, or 0 on success
500  */
501 static int
502 batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
503 			 struct batadv_priv *bat_priv,
504 			 struct batadv_hard_iface *if_outgoing,
505 			 struct batadv_orig_node *orig_node, int *sub_s)
506 {
507 	struct batadv_neigh_node *neigh_node_best;
508 	struct batadv_neigh_node *neigh_node;
509 	int sub = 0;
510 	bool best;
511 
512 	neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
513 	if (!neigh_node_best)
514 		goto out;
515 
516 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
517 		if (sub++ < *sub_s)
518 			continue;
519 
520 		best = (neigh_node == neigh_node_best);
521 
522 		if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
523 						if_outgoing, orig_node,
524 						neigh_node, best)) {
525 			batadv_neigh_node_put(neigh_node_best);
526 
527 			*sub_s = sub - 1;
528 			return -EMSGSIZE;
529 		}
530 	}
531 
532  out:
533 	if (neigh_node_best)
534 		batadv_neigh_node_put(neigh_node_best);
535 
536 	*sub_s = 0;
537 	return 0;
538 }
539 
540 /**
541  * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
542  * @msg: Netlink message to dump into
543  * @portid: Port making netlink request
544  * @seq: Sequence number of netlink message
545  * @bat_priv: The bat priv with all the soft interface information
546  * @if_outgoing: Limit dump to entries with this outgoing interface
547  * @head: Bucket to be dumped
548  * @idx_s: Number of entries to be skipped
549  * @sub: Number of sub entries to be skipped
550  *
551  * Return: Error code, or 0 on success
552  */
553 static int
554 batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
555 			  struct batadv_priv *bat_priv,
556 			  struct batadv_hard_iface *if_outgoing,
557 			  struct hlist_head *head, int *idx_s, int *sub)
558 {
559 	struct batadv_orig_node *orig_node;
560 	int idx = 0;
561 
562 	rcu_read_lock();
563 	hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
564 		if (idx++ < *idx_s)
565 			continue;
566 
567 		if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
568 					     if_outgoing, orig_node, sub)) {
569 			rcu_read_unlock();
570 			*idx_s = idx - 1;
571 			return -EMSGSIZE;
572 		}
573 	}
574 	rcu_read_unlock();
575 
576 	*idx_s = 0;
577 	*sub = 0;
578 	return 0;
579 }
580 
581 /**
582  * batadv_v_orig_dump() - Dump the originators into a message
583  * @msg: Netlink message to dump into
584  * @cb: Control block containing additional options
585  * @bat_priv: The bat priv with all the soft interface information
586  * @if_outgoing: Limit dump to entries with this outgoing interface
587  */
588 static void
589 batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
590 		   struct batadv_priv *bat_priv,
591 		   struct batadv_hard_iface *if_outgoing)
592 {
593 	struct batadv_hashtable *hash = bat_priv->orig_hash;
594 	struct hlist_head *head;
595 	int bucket = cb->args[0];
596 	int idx = cb->args[1];
597 	int sub = cb->args[2];
598 	int portid = NETLINK_CB(cb->skb).portid;
599 
600 	while (bucket < hash->size) {
601 		head = &hash->table[bucket];
602 
603 		if (batadv_v_orig_dump_bucket(msg, portid,
604 					      cb->nlh->nlmsg_seq,
605 					      bat_priv, if_outgoing, head, &idx,
606 					      &sub))
607 			break;
608 
609 		bucket++;
610 	}
611 
612 	cb->args[0] = bucket;
613 	cb->args[1] = idx;
614 	cb->args[2] = sub;
615 }
616 
617 static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
618 			      struct batadv_hard_iface *if_outgoing1,
619 			      struct batadv_neigh_node *neigh2,
620 			      struct batadv_hard_iface *if_outgoing2)
621 {
622 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
623 	int ret = 0;
624 
625 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
626 	if (!ifinfo1)
627 		goto err_ifinfo1;
628 
629 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
630 	if (!ifinfo2)
631 		goto err_ifinfo2;
632 
633 	ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
634 
635 	batadv_neigh_ifinfo_put(ifinfo2);
636 err_ifinfo2:
637 	batadv_neigh_ifinfo_put(ifinfo1);
638 err_ifinfo1:
639 	return ret;
640 }
641 
642 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
643 				  struct batadv_hard_iface *if_outgoing1,
644 				  struct batadv_neigh_node *neigh2,
645 				  struct batadv_hard_iface *if_outgoing2)
646 {
647 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
648 	u32 threshold;
649 	bool ret = false;
650 
651 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
652 	if (!ifinfo1)
653 		goto err_ifinfo1;
654 
655 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
656 	if (!ifinfo2)
657 		goto err_ifinfo2;
658 
659 	threshold = ifinfo1->bat_v.throughput / 4;
660 	threshold = ifinfo1->bat_v.throughput - threshold;
661 
662 	ret = ifinfo2->bat_v.throughput > threshold;
663 
664 	batadv_neigh_ifinfo_put(ifinfo2);
665 err_ifinfo2:
666 	batadv_neigh_ifinfo_put(ifinfo1);
667 err_ifinfo1:
668 	return ret;
669 }
670 
671 /**
672  * batadv_v_init_sel_class() - initialize GW selection class
673  * @bat_priv: the bat priv with all the soft interface information
674  */
675 static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
676 {
677 	/* set default throughput difference threshold to 5Mbps */
678 	atomic_set(&bat_priv->gw.sel_class, 50);
679 }
680 
681 static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
682 					char *buff, size_t count)
683 {
684 	u32 old_class, class;
685 
686 	if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
687 				     "B.A.T.M.A.N. V GW selection class",
688 				     &class))
689 		return -EINVAL;
690 
691 	old_class = atomic_read(&bat_priv->gw.sel_class);
692 	atomic_set(&bat_priv->gw.sel_class, class);
693 
694 	if (old_class != class)
695 		batadv_gw_reselect(bat_priv);
696 
697 	return count;
698 }
699 
700 static ssize_t batadv_v_show_sel_class(struct batadv_priv *bat_priv, char *buff)
701 {
702 	u32 class = atomic_read(&bat_priv->gw.sel_class);
703 
704 	return sprintf(buff, "%u.%u MBit\n", class / 10, class % 10);
705 }
706 
707 /**
708  * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
709  * @gw_node: the GW to retrieve the metric for
710  * @bw: the pointer where the metric will be stored. The metric is computed as
711  *  the minimum between the GW advertised throughput and the path throughput to
712  *  it in the mesh
713  *
714  * Return: 0 on success, -1 on failure
715  */
716 static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
717 {
718 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
719 	struct batadv_orig_node *orig_node;
720 	struct batadv_neigh_node *router;
721 	int ret = -1;
722 
723 	orig_node = gw_node->orig_node;
724 	router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
725 	if (!router)
726 		goto out;
727 
728 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
729 	if (!router_ifinfo)
730 		goto out;
731 
732 	/* the GW metric is computed as the minimum between the path throughput
733 	 * to reach the GW itself and the advertised bandwidth.
734 	 * This gives us an approximation of the effective throughput that the
735 	 * client can expect via this particular GW node
736 	 */
737 	*bw = router_ifinfo->bat_v.throughput;
738 	*bw = min_t(u32, *bw, gw_node->bandwidth_down);
739 
740 	ret = 0;
741 out:
742 	if (router)
743 		batadv_neigh_node_put(router);
744 	if (router_ifinfo)
745 		batadv_neigh_ifinfo_put(router_ifinfo);
746 
747 	return ret;
748 }
749 
750 /**
751  * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
752  * @bat_priv: the bat priv with all the soft interface information
753  *
754  * Return: the GW node having the best GW-metric, NULL if no GW is known
755  */
756 static struct batadv_gw_node *
757 batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
758 {
759 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
760 	u32 max_bw = 0, bw;
761 
762 	rcu_read_lock();
763 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
764 		if (!kref_get_unless_zero(&gw_node->refcount))
765 			continue;
766 
767 		if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
768 			goto next;
769 
770 		if (curr_gw && bw <= max_bw)
771 			goto next;
772 
773 		if (curr_gw)
774 			batadv_gw_node_put(curr_gw);
775 
776 		curr_gw = gw_node;
777 		kref_get(&curr_gw->refcount);
778 		max_bw = bw;
779 
780 next:
781 		batadv_gw_node_put(gw_node);
782 	}
783 	rcu_read_unlock();
784 
785 	return curr_gw;
786 }
787 
788 /**
789  * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
790  * @bat_priv: the bat priv with all the soft interface information
791  * @curr_gw_orig: originator representing the currently selected GW
792  * @orig_node: the originator representing the new candidate
793  *
794  * Return: true if orig_node can be selected as current GW, false otherwise
795  */
796 static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
797 				    struct batadv_orig_node *curr_gw_orig,
798 				    struct batadv_orig_node *orig_node)
799 {
800 	struct batadv_gw_node *curr_gw, *orig_gw = NULL;
801 	u32 gw_throughput, orig_throughput, threshold;
802 	bool ret = false;
803 
804 	threshold = atomic_read(&bat_priv->gw.sel_class);
805 
806 	curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
807 	if (!curr_gw) {
808 		ret = true;
809 		goto out;
810 	}
811 
812 	if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
813 		ret = true;
814 		goto out;
815 	}
816 
817 	orig_gw = batadv_gw_node_get(bat_priv, orig_node);
818 	if (!orig_gw)
819 		goto out;
820 
821 	if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
822 		goto out;
823 
824 	if (orig_throughput < gw_throughput)
825 		goto out;
826 
827 	if ((orig_throughput - gw_throughput) < threshold)
828 		goto out;
829 
830 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
831 		   "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
832 		   gw_throughput, orig_throughput);
833 
834 	ret = true;
835 out:
836 	if (curr_gw)
837 		batadv_gw_node_put(curr_gw);
838 	if (orig_gw)
839 		batadv_gw_node_put(orig_gw);
840 
841 	return ret;
842 }
843 
844 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
845 /* fails if orig_node has no router */
846 static int batadv_v_gw_write_buffer_text(struct batadv_priv *bat_priv,
847 					 struct seq_file *seq,
848 					 const struct batadv_gw_node *gw_node)
849 {
850 	struct batadv_gw_node *curr_gw;
851 	struct batadv_neigh_node *router;
852 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
853 	int ret = -1;
854 
855 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
856 	if (!router)
857 		goto out;
858 
859 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
860 	if (!router_ifinfo)
861 		goto out;
862 
863 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
864 
865 	seq_printf(seq, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
866 		   (curr_gw == gw_node ? "=>" : "  "),
867 		   gw_node->orig_node->orig,
868 		   router_ifinfo->bat_v.throughput / 10,
869 		   router_ifinfo->bat_v.throughput % 10, router->addr,
870 		   router->if_incoming->net_dev->name,
871 		   gw_node->bandwidth_down / 10,
872 		   gw_node->bandwidth_down % 10,
873 		   gw_node->bandwidth_up / 10,
874 		   gw_node->bandwidth_up % 10);
875 	ret = seq_has_overflowed(seq) ? -1 : 0;
876 
877 	if (curr_gw)
878 		batadv_gw_node_put(curr_gw);
879 out:
880 	if (router_ifinfo)
881 		batadv_neigh_ifinfo_put(router_ifinfo);
882 	if (router)
883 		batadv_neigh_node_put(router);
884 	return ret;
885 }
886 
887 /**
888  * batadv_v_gw_print() - print the gateway list
889  * @bat_priv: the bat priv with all the soft interface information
890  * @seq: gateway table seq_file struct
891  */
892 static void batadv_v_gw_print(struct batadv_priv *bat_priv,
893 			      struct seq_file *seq)
894 {
895 	struct batadv_gw_node *gw_node;
896 	int gw_count = 0;
897 
898 	seq_puts(seq,
899 		 "      Gateway        ( throughput)           Nexthop [outgoingIF]: advertised uplink bandwidth\n");
900 
901 	rcu_read_lock();
902 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
903 		/* fails if orig_node has no router */
904 		if (batadv_v_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
905 			continue;
906 
907 		gw_count++;
908 	}
909 	rcu_read_unlock();
910 
911 	if (gw_count == 0)
912 		seq_puts(seq, "No gateways in range ...\n");
913 }
914 #endif
915 
916 /**
917  * batadv_v_gw_dump_entry() - Dump a gateway into a message
918  * @msg: Netlink message to dump into
919  * @portid: Port making netlink request
920  * @cb: Control block containing additional options
921  * @bat_priv: The bat priv with all the soft interface information
922  * @gw_node: Gateway to be dumped
923  *
924  * Return: Error code, or 0 on success
925  */
926 static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
927 				  struct netlink_callback *cb,
928 				  struct batadv_priv *bat_priv,
929 				  struct batadv_gw_node *gw_node)
930 {
931 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
932 	struct batadv_neigh_node *router;
933 	struct batadv_gw_node *curr_gw = NULL;
934 	int ret = 0;
935 	void *hdr;
936 
937 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
938 	if (!router)
939 		goto out;
940 
941 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
942 	if (!router_ifinfo)
943 		goto out;
944 
945 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
946 
947 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
948 			  &batadv_netlink_family, NLM_F_MULTI,
949 			  BATADV_CMD_GET_GATEWAYS);
950 	if (!hdr) {
951 		ret = -ENOBUFS;
952 		goto out;
953 	}
954 
955 	genl_dump_check_consistent(cb, hdr);
956 
957 	ret = -EMSGSIZE;
958 
959 	if (curr_gw == gw_node) {
960 		if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
961 			genlmsg_cancel(msg, hdr);
962 			goto out;
963 		}
964 	}
965 
966 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
967 		    gw_node->orig_node->orig)) {
968 		genlmsg_cancel(msg, hdr);
969 		goto out;
970 	}
971 
972 	if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
973 			router_ifinfo->bat_v.throughput)) {
974 		genlmsg_cancel(msg, hdr);
975 		goto out;
976 	}
977 
978 	if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
979 		genlmsg_cancel(msg, hdr);
980 		goto out;
981 	}
982 
983 	if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
984 			   router->if_incoming->net_dev->name)) {
985 		genlmsg_cancel(msg, hdr);
986 		goto out;
987 	}
988 
989 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
990 			gw_node->bandwidth_down)) {
991 		genlmsg_cancel(msg, hdr);
992 		goto out;
993 	}
994 
995 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
996 		genlmsg_cancel(msg, hdr);
997 		goto out;
998 	}
999 
1000 	genlmsg_end(msg, hdr);
1001 	ret = 0;
1002 
1003 out:
1004 	if (curr_gw)
1005 		batadv_gw_node_put(curr_gw);
1006 	if (router_ifinfo)
1007 		batadv_neigh_ifinfo_put(router_ifinfo);
1008 	if (router)
1009 		batadv_neigh_node_put(router);
1010 	return ret;
1011 }
1012 
1013 /**
1014  * batadv_v_gw_dump() - Dump gateways into a message
1015  * @msg: Netlink message to dump into
1016  * @cb: Control block containing additional options
1017  * @bat_priv: The bat priv with all the soft interface information
1018  */
1019 static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
1020 			     struct batadv_priv *bat_priv)
1021 {
1022 	int portid = NETLINK_CB(cb->skb).portid;
1023 	struct batadv_gw_node *gw_node;
1024 	int idx_skip = cb->args[0];
1025 	int idx = 0;
1026 
1027 	spin_lock_bh(&bat_priv->gw.list_lock);
1028 	cb->seq = bat_priv->gw.generation << 1 | 1;
1029 
1030 	hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
1031 		if (idx++ < idx_skip)
1032 			continue;
1033 
1034 		if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
1035 					   gw_node)) {
1036 			idx_skip = idx - 1;
1037 			goto unlock;
1038 		}
1039 	}
1040 
1041 	idx_skip = idx;
1042 unlock:
1043 	spin_unlock_bh(&bat_priv->gw.list_lock);
1044 
1045 	cb->args[0] = idx_skip;
1046 }
1047 
1048 static struct batadv_algo_ops batadv_batman_v __read_mostly = {
1049 	.name = "BATMAN_V",
1050 	.iface = {
1051 		.activate = batadv_v_iface_activate,
1052 		.enable = batadv_v_iface_enable,
1053 		.disable = batadv_v_iface_disable,
1054 		.update_mac = batadv_v_iface_update_mac,
1055 		.primary_set = batadv_v_primary_iface_set,
1056 	},
1057 	.neigh = {
1058 		.hardif_init = batadv_v_hardif_neigh_init,
1059 		.cmp = batadv_v_neigh_cmp,
1060 		.is_similar_or_better = batadv_v_neigh_is_sob,
1061 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1062 		.print = batadv_v_neigh_print,
1063 #endif
1064 		.dump = batadv_v_neigh_dump,
1065 	},
1066 	.orig = {
1067 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1068 		.print = batadv_v_orig_print,
1069 #endif
1070 		.dump = batadv_v_orig_dump,
1071 	},
1072 	.gw = {
1073 		.init_sel_class = batadv_v_init_sel_class,
1074 		.store_sel_class = batadv_v_store_sel_class,
1075 		.show_sel_class = batadv_v_show_sel_class,
1076 		.get_best_gw_node = batadv_v_gw_get_best_gw_node,
1077 		.is_eligible = batadv_v_gw_is_eligible,
1078 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1079 		.print = batadv_v_gw_print,
1080 #endif
1081 		.dump = batadv_v_gw_dump,
1082 	},
1083 };
1084 
1085 /**
1086  * batadv_v_hardif_init() - initialize the algorithm specific fields in the
1087  *  hard-interface object
1088  * @hard_iface: the hard-interface to initialize
1089  */
1090 void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
1091 {
1092 	/* enable link throughput auto-detection by setting the throughput
1093 	 * override to zero
1094 	 */
1095 	atomic_set(&hard_iface->bat_v.throughput_override, 0);
1096 	atomic_set(&hard_iface->bat_v.elp_interval, 500);
1097 }
1098 
1099 /**
1100  * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
1101  *  mesh
1102  * @bat_priv: the object representing the mesh interface to initialise
1103  *
1104  * Return: 0 on success or a negative error code otherwise
1105  */
1106 int batadv_v_mesh_init(struct batadv_priv *bat_priv)
1107 {
1108 	int ret = 0;
1109 
1110 	ret = batadv_v_ogm_init(bat_priv);
1111 	if (ret < 0)
1112 		return ret;
1113 
1114 	return 0;
1115 }
1116 
1117 /**
1118  * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
1119  * @bat_priv: the object representing the mesh interface to free
1120  */
1121 void batadv_v_mesh_free(struct batadv_priv *bat_priv)
1122 {
1123 	batadv_v_ogm_free(bat_priv);
1124 }
1125 
1126 /**
1127  * batadv_v_init() - B.A.T.M.A.N. V initialization function
1128  *
1129  * Description: Takes care of initializing all the subcomponents.
1130  * It is invoked upon module load only.
1131  *
1132  * Return: 0 on success or a negative error code otherwise
1133  */
1134 int __init batadv_v_init(void)
1135 {
1136 	int ret;
1137 
1138 	/* B.A.T.M.A.N. V echo location protocol packet  */
1139 	ret = batadv_recv_handler_register(BATADV_ELP,
1140 					   batadv_v_elp_packet_recv);
1141 	if (ret < 0)
1142 		return ret;
1143 
1144 	ret = batadv_recv_handler_register(BATADV_OGM2,
1145 					   batadv_v_ogm_packet_recv);
1146 	if (ret < 0)
1147 		goto elp_unregister;
1148 
1149 	ret = batadv_algo_register(&batadv_batman_v);
1150 	if (ret < 0)
1151 		goto ogm_unregister;
1152 
1153 	return ret;
1154 
1155 ogm_unregister:
1156 	batadv_recv_handler_unregister(BATADV_OGM2);
1157 
1158 elp_unregister:
1159 	batadv_recv_handler_unregister(BATADV_ELP);
1160 
1161 	return ret;
1162 }
1163