xref: /linux/net/batman-adv/bat_v.c (revision 088e88be5a380cc4e81963a9a02815da465d144f)
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 
7 #include "bat_v.h"
8 #include "main.h"
9 
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>
33 
34 #include "bat_algo.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"
40 #include "hash.h"
41 #include "log.h"
42 #include "netlink.h"
43 #include "originator.h"
44 
45 static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
46 {
47 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
48 	struct batadv_hard_iface *primary_if;
49 
50 	primary_if = batadv_primary_if_get_selected(bat_priv);
51 
52 	if (primary_if) {
53 		batadv_v_elp_iface_activate(primary_if, hard_iface);
54 		batadv_hardif_put(primary_if);
55 	}
56 
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
59 	 * condition
60 	 */
61 	if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
62 		hard_iface->if_status = BATADV_IF_ACTIVE;
63 }
64 
65 static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
66 {
67 	int ret;
68 
69 	ret = batadv_v_elp_iface_enable(hard_iface);
70 	if (ret < 0)
71 		return ret;
72 
73 	ret = batadv_v_ogm_iface_enable(hard_iface);
74 	if (ret < 0)
75 		batadv_v_elp_iface_disable(hard_iface);
76 
77 	return ret;
78 }
79 
80 static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
81 {
82 	batadv_v_elp_iface_disable(hard_iface);
83 }
84 
85 static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
86 {
87 	batadv_v_elp_primary_iface_set(hard_iface);
88 	batadv_v_ogm_primary_iface_set(hard_iface);
89 }
90 
91 /**
92  * batadv_v_iface_update_mac() - react to hard-interface MAC address change
93  * @hard_iface: the modified interface
94  *
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.
97  */
98 static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
99 {
100 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
101 	struct batadv_hard_iface *primary_if;
102 
103 	primary_if = batadv_primary_if_get_selected(bat_priv);
104 	if (primary_if != hard_iface)
105 		goto out;
106 
107 	batadv_v_primary_iface_set(hard_iface);
108 out:
109 	if (primary_if)
110 		batadv_hardif_put(primary_if);
111 }
112 
113 static void
114 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
115 {
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);
119 }
120 
121 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
122 /**
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
127  *
128  * Must be called while holding an rcu lock.
129  */
130 static void
131 batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
132 			  struct batadv_hard_iface *if_outgoing,
133 			  struct seq_file *seq)
134 {
135 	struct batadv_neigh_node *neigh_node;
136 	struct batadv_neigh_ifinfo *n_ifinfo;
137 
138 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
139 		n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
140 		if (!n_ifinfo)
141 			continue;
142 
143 		seq_printf(seq, " %pM (%9u.%1u)",
144 			   neigh_node->addr,
145 			   n_ifinfo->bat_v.throughput / 10,
146 			   n_ifinfo->bat_v.throughput % 10);
147 
148 		batadv_neigh_ifinfo_put(n_ifinfo);
149 	}
150 }
151 
152 /**
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
156  */
157 static void
158 batadv_v_hardif_neigh_print(struct seq_file *seq,
159 			    struct batadv_hardif_neigh_node *hardif_neigh)
160 {
161 	int last_secs, last_msecs;
162 	u32 throughput;
163 
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);
167 
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);
171 }
172 
173 /**
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
177  */
178 static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
179 				 struct seq_file *seq)
180 {
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;
185 
186 	seq_puts(seq,
187 		 "  Neighbor        last-seen ( throughput) [        IF]\n");
188 
189 	rcu_read_lock();
190 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
191 		if (hard_iface->soft_iface != net_dev)
192 			continue;
193 
194 		hlist_for_each_entry_rcu(hardif_neigh,
195 					 &hard_iface->neigh_list, list) {
196 			batadv_v_hardif_neigh_print(seq, hardif_neigh);
197 			batman_count++;
198 		}
199 	}
200 	rcu_read_unlock();
201 
202 	if (batman_count == 0)
203 		seq_puts(seq, "No batman nodes in range ...\n");
204 }
205 #endif
206 
207 /**
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
213  *
214  * Return: Error code, or 0 on success
215  */
216 static int
217 batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
218 			  struct batadv_hardif_neigh_node *hardif_neigh)
219 {
220 	void *hdr;
221 	unsigned int last_seen_msecs;
222 	u32 throughput;
223 
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;
227 
228 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
229 			  BATADV_CMD_GET_NEIGHBORS);
230 	if (!hdr)
231 		return -ENOBUFS;
232 
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,
238 			last_seen_msecs) ||
239 	    nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
240 		goto nla_put_failure;
241 
242 	genlmsg_end(msg, hdr);
243 	return 0;
244 
245  nla_put_failure:
246 	genlmsg_cancel(msg, hdr);
247 	return -EMSGSIZE;
248 }
249 
250 /**
251  * batadv_v_neigh_dump_hardif() - Dump the  neighbours of a hard interface into
252  *  a message
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
259  *
260  * This function assumes the caller holds rcu_read_lock().
261  *
262  * Return: Error code, or 0 on success
263  */
264 static int
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,
268 			   int *idx_s)
269 {
270 	struct batadv_hardif_neigh_node *hardif_neigh;
271 	int idx = 0;
272 
273 	hlist_for_each_entry_rcu(hardif_neigh,
274 				 &hard_iface->neigh_list, list) {
275 		if (idx++ < *idx_s)
276 			continue;
277 
278 		if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
279 			*idx_s = idx - 1;
280 			return -EMSGSIZE;
281 		}
282 	}
283 
284 	*idx_s = 0;
285 	return 0;
286 }
287 
288 /**
289  * batadv_v_neigh_dump() - Dump the neighbours of a hard interface  into a
290  *  message
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
295  */
296 static void
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)
300 {
301 	struct batadv_hard_iface *hard_iface;
302 	int i_hardif = 0;
303 	int i_hardif_s = cb->args[0];
304 	int idx = cb->args[1];
305 	int portid = NETLINK_CB(cb->skb).portid;
306 
307 	rcu_read_lock();
308 	if (single_hardif) {
309 		if (i_hardif_s == 0) {
310 			if (batadv_v_neigh_dump_hardif(msg, portid,
311 						       cb->nlh->nlmsg_seq,
312 						       bat_priv, single_hardif,
313 						       &idx) == 0)
314 				i_hardif++;
315 		}
316 	} else {
317 		list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
318 			if (hard_iface->soft_iface != bat_priv->soft_iface)
319 				continue;
320 
321 			if (i_hardif++ < i_hardif_s)
322 				continue;
323 
324 			if (batadv_v_neigh_dump_hardif(msg, portid,
325 						       cb->nlh->nlmsg_seq,
326 						       bat_priv, hard_iface,
327 						       &idx)) {
328 				i_hardif--;
329 				break;
330 			}
331 		}
332 	}
333 	rcu_read_unlock();
334 
335 	cb->args[0] = i_hardif;
336 	cb->args[1] = idx;
337 }
338 
339 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
340 /**
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
345  */
346 static void batadv_v_orig_print(struct batadv_priv *bat_priv,
347 				struct seq_file *seq,
348 				struct batadv_hard_iface *if_outgoing)
349 {
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;
358 	u32 i;
359 
360 	seq_puts(seq,
361 		 "  Originator      last-seen ( throughput)           Nexthop [outgoingIF]:   Potential nexthops ...\n");
362 
363 	for (i = 0; i < hash->size; i++) {
364 		head = &hash->table[i];
365 
366 		rcu_read_lock();
367 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
368 			neigh_node = batadv_orig_router_get(orig_node,
369 							    if_outgoing);
370 			if (!neigh_node)
371 				continue;
372 
373 			n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
374 							   if_outgoing);
375 			if (!n_ifinfo)
376 				goto next;
377 
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;
382 
383 			seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
384 				   orig_node->orig, last_seen_secs,
385 				   last_seen_msecs,
386 				   n_ifinfo->bat_v.throughput / 10,
387 				   n_ifinfo->bat_v.throughput % 10,
388 				   neigh_node->addr,
389 				   neigh_node->if_incoming->net_dev->name);
390 
391 			batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
392 			seq_putc(seq, '\n');
393 			batman_count++;
394 
395 next:
396 			batadv_neigh_node_put(neigh_node);
397 			if (n_ifinfo)
398 				batadv_neigh_ifinfo_put(n_ifinfo);
399 		}
400 		rcu_read_unlock();
401 	}
402 
403 	if (batman_count == 0)
404 		seq_puts(seq, "No batman nodes in range ...\n");
405 }
406 #endif
407 
408 /**
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
418  *
419  * Return: Error code, or 0 on success
420  */
421 static int
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,
427 			    bool best)
428 {
429 	struct batadv_neigh_ifinfo *n_ifinfo;
430 	unsigned int last_seen_msecs;
431 	u32 throughput;
432 	void *hdr;
433 
434 	n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
435 	if (!n_ifinfo)
436 		return 0;
437 
438 	throughput = n_ifinfo->bat_v.throughput * 100;
439 
440 	batadv_neigh_ifinfo_put(n_ifinfo);
441 
442 	last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
443 
444 	if (if_outgoing != BATADV_IF_DEFAULT &&
445 	    if_outgoing != neigh_node->if_incoming)
446 		return 0;
447 
448 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
449 			  BATADV_CMD_GET_ORIGINATORS);
450 	if (!hdr)
451 		return -ENOBUFS;
452 
453 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
454 	    nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
455 		    neigh_node->addr) ||
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,
460 			last_seen_msecs))
461 		goto nla_put_failure;
462 
463 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
464 		goto nla_put_failure;
465 
466 	genlmsg_end(msg, hdr);
467 	return 0;
468 
469  nla_put_failure:
470 	genlmsg_cancel(msg, hdr);
471 	return -EMSGSIZE;
472 }
473 
474 /**
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
483  *
484  * This function assumes the caller holds rcu_read_lock().
485  *
486  * Return: Error code, or 0 on success
487  */
488 static int
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)
493 {
494 	struct batadv_neigh_node *neigh_node_best;
495 	struct batadv_neigh_node *neigh_node;
496 	int sub = 0;
497 	bool best;
498 
499 	neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
500 	if (!neigh_node_best)
501 		goto out;
502 
503 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
504 		if (sub++ < *sub_s)
505 			continue;
506 
507 		best = (neigh_node == neigh_node_best);
508 
509 		if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
510 						if_outgoing, orig_node,
511 						neigh_node, best)) {
512 			batadv_neigh_node_put(neigh_node_best);
513 
514 			*sub_s = sub - 1;
515 			return -EMSGSIZE;
516 		}
517 	}
518 
519  out:
520 	if (neigh_node_best)
521 		batadv_neigh_node_put(neigh_node_best);
522 
523 	*sub_s = 0;
524 	return 0;
525 }
526 
527 /**
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
537  *
538  * Return: Error code, or 0 on success
539  */
540 static int
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)
545 {
546 	struct batadv_orig_node *orig_node;
547 	int idx = 0;
548 
549 	rcu_read_lock();
550 	hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
551 		if (idx++ < *idx_s)
552 			continue;
553 
554 		if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
555 					     if_outgoing, orig_node, sub)) {
556 			rcu_read_unlock();
557 			*idx_s = idx - 1;
558 			return -EMSGSIZE;
559 		}
560 	}
561 	rcu_read_unlock();
562 
563 	*idx_s = 0;
564 	*sub = 0;
565 	return 0;
566 }
567 
568 /**
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
574  */
575 static void
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)
579 {
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;
586 
587 	while (bucket < hash->size) {
588 		head = &hash->table[bucket];
589 
590 		if (batadv_v_orig_dump_bucket(msg, portid,
591 					      cb->nlh->nlmsg_seq,
592 					      bat_priv, if_outgoing, head, &idx,
593 					      &sub))
594 			break;
595 
596 		bucket++;
597 	}
598 
599 	cb->args[0] = bucket;
600 	cb->args[1] = idx;
601 	cb->args[2] = sub;
602 }
603 
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)
608 {
609 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
610 	int ret = 0;
611 
612 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
613 	if (!ifinfo1)
614 		goto err_ifinfo1;
615 
616 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
617 	if (!ifinfo2)
618 		goto err_ifinfo2;
619 
620 	ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
621 
622 	batadv_neigh_ifinfo_put(ifinfo2);
623 err_ifinfo2:
624 	batadv_neigh_ifinfo_put(ifinfo1);
625 err_ifinfo1:
626 	return ret;
627 }
628 
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)
633 {
634 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
635 	u32 threshold;
636 	bool ret = false;
637 
638 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
639 	if (!ifinfo1)
640 		goto err_ifinfo1;
641 
642 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
643 	if (!ifinfo2)
644 		goto err_ifinfo2;
645 
646 	threshold = ifinfo1->bat_v.throughput / 4;
647 	threshold = ifinfo1->bat_v.throughput - threshold;
648 
649 	ret = ifinfo2->bat_v.throughput > threshold;
650 
651 	batadv_neigh_ifinfo_put(ifinfo2);
652 err_ifinfo2:
653 	batadv_neigh_ifinfo_put(ifinfo1);
654 err_ifinfo1:
655 	return ret;
656 }
657 
658 /**
659  * batadv_v_init_sel_class() - initialize GW selection class
660  * @bat_priv: the bat priv with all the soft interface information
661  */
662 static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
663 {
664 	/* set default throughput difference threshold to 5Mbps */
665 	atomic_set(&bat_priv->gw.sel_class, 50);
666 }
667 
668 static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
669 					char *buff, size_t count)
670 {
671 	u32 old_class, class;
672 
673 	if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
674 				     "B.A.T.M.A.N. V GW selection class",
675 				     &class))
676 		return -EINVAL;
677 
678 	old_class = atomic_read(&bat_priv->gw.sel_class);
679 	atomic_set(&bat_priv->gw.sel_class, class);
680 
681 	if (old_class != class)
682 		batadv_gw_reselect(bat_priv);
683 
684 	return count;
685 }
686 
687 static ssize_t batadv_v_show_sel_class(struct batadv_priv *bat_priv, char *buff)
688 {
689 	u32 class = atomic_read(&bat_priv->gw.sel_class);
690 
691 	return sprintf(buff, "%u.%u MBit\n", class / 10, class % 10);
692 }
693 
694 /**
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
699  *  it in the mesh
700  *
701  * Return: 0 on success, -1 on failure
702  */
703 static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
704 {
705 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
706 	struct batadv_orig_node *orig_node;
707 	struct batadv_neigh_node *router;
708 	int ret = -1;
709 
710 	orig_node = gw_node->orig_node;
711 	router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
712 	if (!router)
713 		goto out;
714 
715 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
716 	if (!router_ifinfo)
717 		goto out;
718 
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
723 	 */
724 	*bw = router_ifinfo->bat_v.throughput;
725 	*bw = min_t(u32, *bw, gw_node->bandwidth_down);
726 
727 	ret = 0;
728 out:
729 	if (router)
730 		batadv_neigh_node_put(router);
731 	if (router_ifinfo)
732 		batadv_neigh_ifinfo_put(router_ifinfo);
733 
734 	return ret;
735 }
736 
737 /**
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
740  *
741  * Return: the GW node having the best GW-metric, NULL if no GW is known
742  */
743 static struct batadv_gw_node *
744 batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
745 {
746 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
747 	u32 max_bw = 0, bw;
748 
749 	rcu_read_lock();
750 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
751 		if (!kref_get_unless_zero(&gw_node->refcount))
752 			continue;
753 
754 		if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
755 			goto next;
756 
757 		if (curr_gw && bw <= max_bw)
758 			goto next;
759 
760 		if (curr_gw)
761 			batadv_gw_node_put(curr_gw);
762 
763 		curr_gw = gw_node;
764 		kref_get(&curr_gw->refcount);
765 		max_bw = bw;
766 
767 next:
768 		batadv_gw_node_put(gw_node);
769 	}
770 	rcu_read_unlock();
771 
772 	return curr_gw;
773 }
774 
775 /**
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
780  *
781  * Return: true if orig_node can be selected as current GW, false otherwise
782  */
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)
786 {
787 	struct batadv_gw_node *curr_gw, *orig_gw = NULL;
788 	u32 gw_throughput, orig_throughput, threshold;
789 	bool ret = false;
790 
791 	threshold = atomic_read(&bat_priv->gw.sel_class);
792 
793 	curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
794 	if (!curr_gw) {
795 		ret = true;
796 		goto out;
797 	}
798 
799 	if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
800 		ret = true;
801 		goto out;
802 	}
803 
804 	orig_gw = batadv_gw_node_get(bat_priv, orig_node);
805 	if (!orig_gw)
806 		goto out;
807 
808 	if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
809 		goto out;
810 
811 	if (orig_throughput < gw_throughput)
812 		goto out;
813 
814 	if ((orig_throughput - gw_throughput) < threshold)
815 		goto out;
816 
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);
820 
821 	ret = true;
822 out:
823 	if (curr_gw)
824 		batadv_gw_node_put(curr_gw);
825 	if (orig_gw)
826 		batadv_gw_node_put(orig_gw);
827 
828 	return ret;
829 }
830 
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)
836 {
837 	struct batadv_gw_node *curr_gw;
838 	struct batadv_neigh_node *router;
839 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
840 	int ret = -1;
841 
842 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
843 	if (!router)
844 		goto out;
845 
846 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
847 	if (!router_ifinfo)
848 		goto out;
849 
850 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
851 
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;
863 
864 	if (curr_gw)
865 		batadv_gw_node_put(curr_gw);
866 out:
867 	if (router_ifinfo)
868 		batadv_neigh_ifinfo_put(router_ifinfo);
869 	if (router)
870 		batadv_neigh_node_put(router);
871 	return ret;
872 }
873 
874 /**
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
878  */
879 static void batadv_v_gw_print(struct batadv_priv *bat_priv,
880 			      struct seq_file *seq)
881 {
882 	struct batadv_gw_node *gw_node;
883 	int gw_count = 0;
884 
885 	seq_puts(seq,
886 		 "      Gateway        ( throughput)           Nexthop [outgoingIF]: advertised uplink bandwidth\n");
887 
888 	rcu_read_lock();
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)
892 			continue;
893 
894 		gw_count++;
895 	}
896 	rcu_read_unlock();
897 
898 	if (gw_count == 0)
899 		seq_puts(seq, "No gateways in range ...\n");
900 }
901 #endif
902 
903 /**
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
910  *
911  * Return: Error code, or 0 on success
912  */
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)
917 {
918 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
919 	struct batadv_neigh_node *router;
920 	struct batadv_gw_node *curr_gw = NULL;
921 	int ret = 0;
922 	void *hdr;
923 
924 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
925 	if (!router)
926 		goto out;
927 
928 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
929 	if (!router_ifinfo)
930 		goto out;
931 
932 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
933 
934 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
935 			  &batadv_netlink_family, NLM_F_MULTI,
936 			  BATADV_CMD_GET_GATEWAYS);
937 	if (!hdr) {
938 		ret = -ENOBUFS;
939 		goto out;
940 	}
941 
942 	genl_dump_check_consistent(cb, hdr);
943 
944 	ret = -EMSGSIZE;
945 
946 	if (curr_gw == gw_node) {
947 		if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
948 			genlmsg_cancel(msg, hdr);
949 			goto out;
950 		}
951 	}
952 
953 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
954 		    gw_node->orig_node->orig)) {
955 		genlmsg_cancel(msg, hdr);
956 		goto out;
957 	}
958 
959 	if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
960 			router_ifinfo->bat_v.throughput)) {
961 		genlmsg_cancel(msg, hdr);
962 		goto out;
963 	}
964 
965 	if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
966 		genlmsg_cancel(msg, hdr);
967 		goto out;
968 	}
969 
970 	if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
971 			   router->if_incoming->net_dev->name)) {
972 		genlmsg_cancel(msg, hdr);
973 		goto out;
974 	}
975 
976 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
977 			gw_node->bandwidth_down)) {
978 		genlmsg_cancel(msg, hdr);
979 		goto out;
980 	}
981 
982 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
983 		genlmsg_cancel(msg, hdr);
984 		goto out;
985 	}
986 
987 	genlmsg_end(msg, hdr);
988 	ret = 0;
989 
990 out:
991 	if (curr_gw)
992 		batadv_gw_node_put(curr_gw);
993 	if (router_ifinfo)
994 		batadv_neigh_ifinfo_put(router_ifinfo);
995 	if (router)
996 		batadv_neigh_node_put(router);
997 	return ret;
998 }
999 
1000 /**
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
1005  */
1006 static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
1007 			     struct batadv_priv *bat_priv)
1008 {
1009 	int portid = NETLINK_CB(cb->skb).portid;
1010 	struct batadv_gw_node *gw_node;
1011 	int idx_skip = cb->args[0];
1012 	int idx = 0;
1013 
1014 	spin_lock_bh(&bat_priv->gw.list_lock);
1015 	cb->seq = bat_priv->gw.generation << 1 | 1;
1016 
1017 	hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
1018 		if (idx++ < idx_skip)
1019 			continue;
1020 
1021 		if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
1022 					   gw_node)) {
1023 			idx_skip = idx - 1;
1024 			goto unlock;
1025 		}
1026 	}
1027 
1028 	idx_skip = idx;
1029 unlock:
1030 	spin_unlock_bh(&bat_priv->gw.list_lock);
1031 
1032 	cb->args[0] = idx_skip;
1033 }
1034 
1035 static struct batadv_algo_ops batadv_batman_v __read_mostly = {
1036 	.name = "BATMAN_V",
1037 	.iface = {
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,
1043 	},
1044 	.neigh = {
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,
1050 #endif
1051 		.dump = batadv_v_neigh_dump,
1052 	},
1053 	.orig = {
1054 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1055 		.print = batadv_v_orig_print,
1056 #endif
1057 		.dump = batadv_v_orig_dump,
1058 	},
1059 	.gw = {
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,
1067 #endif
1068 		.dump = batadv_v_gw_dump,
1069 	},
1070 };
1071 
1072 /**
1073  * batadv_v_hardif_init() - initialize the algorithm specific fields in the
1074  *  hard-interface object
1075  * @hard_iface: the hard-interface to initialize
1076  */
1077 void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
1078 {
1079 	/* enable link throughput auto-detection by setting the throughput
1080 	 * override to zero
1081 	 */
1082 	atomic_set(&hard_iface->bat_v.throughput_override, 0);
1083 	atomic_set(&hard_iface->bat_v.elp_interval, 500);
1084 }
1085 
1086 /**
1087  * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
1088  *  mesh
1089  * @bat_priv: the object representing the mesh interface to initialise
1090  *
1091  * Return: 0 on success or a negative error code otherwise
1092  */
1093 int batadv_v_mesh_init(struct batadv_priv *bat_priv)
1094 {
1095 	int ret = 0;
1096 
1097 	ret = batadv_v_ogm_init(bat_priv);
1098 	if (ret < 0)
1099 		return ret;
1100 
1101 	return 0;
1102 }
1103 
1104 /**
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
1107  */
1108 void batadv_v_mesh_free(struct batadv_priv *bat_priv)
1109 {
1110 	batadv_v_ogm_free(bat_priv);
1111 }
1112 
1113 /**
1114  * batadv_v_init() - B.A.T.M.A.N. V initialization function
1115  *
1116  * Description: Takes care of initializing all the subcomponents.
1117  * It is invoked upon module load only.
1118  *
1119  * Return: 0 on success or a negative error code otherwise
1120  */
1121 int __init batadv_v_init(void)
1122 {
1123 	int ret;
1124 
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);
1128 	if (ret < 0)
1129 		return ret;
1130 
1131 	ret = batadv_recv_handler_register(BATADV_OGM2,
1132 					   batadv_v_ogm_packet_recv);
1133 	if (ret < 0)
1134 		goto elp_unregister;
1135 
1136 	ret = batadv_algo_register(&batadv_batman_v);
1137 	if (ret < 0)
1138 		goto ogm_unregister;
1139 
1140 	return ret;
1141 
1142 ogm_unregister:
1143 	batadv_recv_handler_unregister(BATADV_OGM2);
1144 
1145 elp_unregister:
1146 	batadv_recv_handler_unregister(BATADV_ELP);
1147 
1148 	return ret;
1149 }
1150