xref: /linux/net/batman-adv/bat_v.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 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/kref.h>
17 #include <linux/limits.h>
18 #include <linux/list.h>
19 #include <linux/minmax.h>
20 #include <linux/netdevice.h>
21 #include <linux/netlink.h>
22 #include <linux/rculist.h>
23 #include <linux/rcupdate.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 "hard-interface.h"
39 #include "hash.h"
40 #include "log.h"
41 #include "netlink.h"
42 #include "originator.h"
43 
batadv_v_iface_activate(struct batadv_hard_iface * hard_iface)44 static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
45 {
46 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
47 	struct batadv_hard_iface *primary_if;
48 
49 	primary_if = batadv_primary_if_get_selected(bat_priv);
50 
51 	if (primary_if) {
52 		batadv_v_elp_iface_activate(primary_if, hard_iface);
53 		batadv_hardif_put(primary_if);
54 	}
55 
56 	/* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
57 	 * set the interface as ACTIVE right away, without any risk of race
58 	 * condition
59 	 */
60 	if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
61 		hard_iface->if_status = BATADV_IF_ACTIVE;
62 }
63 
batadv_v_iface_enable(struct batadv_hard_iface * hard_iface)64 static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
65 {
66 	int ret;
67 
68 	ret = batadv_v_elp_iface_enable(hard_iface);
69 	if (ret < 0)
70 		return ret;
71 
72 	ret = batadv_v_ogm_iface_enable(hard_iface);
73 	if (ret < 0)
74 		batadv_v_elp_iface_disable(hard_iface);
75 
76 	return ret;
77 }
78 
batadv_v_iface_disable(struct batadv_hard_iface * hard_iface)79 static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
80 {
81 	batadv_v_ogm_iface_disable(hard_iface);
82 	batadv_v_elp_iface_disable(hard_iface);
83 }
84 
batadv_v_primary_iface_set(struct batadv_hard_iface * hard_iface)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  */
batadv_v_iface_update_mac(struct batadv_hard_iface * hard_iface)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->mesh_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 	batadv_hardif_put(primary_if);
110 }
111 
112 static void
batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node * hardif_neigh)113 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
114 {
115 	ewma_throughput_init(&hardif_neigh->bat_v.throughput);
116 }
117 
118 /**
119  * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
120  * @msg: Netlink message to dump into
121  * @portid: Port making netlink request
122  * @seq: Sequence number of netlink message
123  * @hardif_neigh: Neighbour to dump
124  *
125  * Return: Error code, or 0 on success
126  */
127 static int
batadv_v_neigh_dump_neigh(struct sk_buff * msg,u32 portid,u32 seq,struct batadv_hardif_neigh_node * hardif_neigh)128 batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
129 			  struct batadv_hardif_neigh_node *hardif_neigh)
130 {
131 	void *hdr;
132 	unsigned int last_seen_msecs;
133 	u32 throughput;
134 
135 	last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
136 	throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
137 	throughput = throughput * 100;
138 
139 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
140 			  BATADV_CMD_GET_NEIGHBORS);
141 	if (!hdr)
142 		return -ENOBUFS;
143 
144 	if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
145 		    hardif_neigh->addr) ||
146 	    nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
147 			   hardif_neigh->if_incoming->net_dev->name) ||
148 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
149 			hardif_neigh->if_incoming->net_dev->ifindex) ||
150 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
151 			last_seen_msecs) ||
152 	    nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
153 		goto nla_put_failure;
154 
155 	genlmsg_end(msg, hdr);
156 	return 0;
157 
158  nla_put_failure:
159 	genlmsg_cancel(msg, hdr);
160 	return -EMSGSIZE;
161 }
162 
163 /**
164  * batadv_v_neigh_dump_hardif() - Dump the  neighbours of a hard interface into
165  *  a message
166  * @msg: Netlink message to dump into
167  * @portid: Port making netlink request
168  * @seq: Sequence number of netlink message
169  * @bat_priv: The bat priv with all the mesh interface information
170  * @hard_iface: The hard interface to be dumped
171  * @idx_s: Entries to be skipped
172  *
173  * This function assumes the caller holds rcu_read_lock().
174  *
175  * Return: Error code, or 0 on success
176  */
177 static int
batadv_v_neigh_dump_hardif(struct sk_buff * msg,u32 portid,u32 seq,struct batadv_priv * bat_priv,struct batadv_hard_iface * hard_iface,int * idx_s)178 batadv_v_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
179 			   struct batadv_priv *bat_priv,
180 			   struct batadv_hard_iface *hard_iface,
181 			   int *idx_s)
182 {
183 	struct batadv_hardif_neigh_node *hardif_neigh;
184 	int idx = 0;
185 
186 	hlist_for_each_entry_rcu(hardif_neigh,
187 				 &hard_iface->neigh_list, list) {
188 		if (idx++ < *idx_s)
189 			continue;
190 
191 		if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
192 			*idx_s = idx - 1;
193 			return -EMSGSIZE;
194 		}
195 	}
196 
197 	*idx_s = 0;
198 	return 0;
199 }
200 
201 /**
202  * batadv_v_neigh_dump() - Dump the neighbours of a hard interface  into a
203  *  message
204  * @msg: Netlink message to dump into
205  * @cb: Control block containing additional options
206  * @bat_priv: The bat priv with all the mesh interface information
207  * @single_hardif: Limit dumping to this hard interface
208  */
209 static void
batadv_v_neigh_dump(struct sk_buff * msg,struct netlink_callback * cb,struct batadv_priv * bat_priv,struct batadv_hard_iface * single_hardif)210 batadv_v_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
211 		    struct batadv_priv *bat_priv,
212 		    struct batadv_hard_iface *single_hardif)
213 {
214 	struct batadv_hard_iface *hard_iface;
215 	struct list_head *iter;
216 	int i_hardif = 0;
217 	int i_hardif_s = cb->args[0];
218 	int idx = cb->args[1];
219 	int portid = NETLINK_CB(cb->skb).portid;
220 
221 	rcu_read_lock();
222 	if (single_hardif) {
223 		if (i_hardif_s == 0) {
224 			if (batadv_v_neigh_dump_hardif(msg, portid,
225 						       cb->nlh->nlmsg_seq,
226 						       bat_priv, single_hardif,
227 						       &idx) == 0)
228 				i_hardif++;
229 		}
230 	} else {
231 		netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
232 			if (i_hardif++ < i_hardif_s)
233 				continue;
234 
235 			if (batadv_v_neigh_dump_hardif(msg, portid,
236 						       cb->nlh->nlmsg_seq,
237 						       bat_priv, hard_iface,
238 						       &idx)) {
239 				i_hardif--;
240 				break;
241 			}
242 		}
243 	}
244 	rcu_read_unlock();
245 
246 	cb->args[0] = i_hardif;
247 	cb->args[1] = idx;
248 }
249 
250 /**
251  * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
252  * @msg: Netlink message to dump into
253  * @portid: Port making netlink request
254  * @seq: Sequence number of netlink message
255  * @bat_priv: The bat priv with all the mesh interface information
256  * @if_outgoing: Limit dump to entries with this outgoing interface
257  * @orig_node: Originator to dump
258  * @neigh_node: Single hops neighbour
259  * @best: Is the best originator
260  *
261  * Return: Error code, or 0 on success
262  */
263 static int
batadv_v_orig_dump_subentry(struct sk_buff * msg,u32 portid,u32 seq,struct batadv_priv * bat_priv,struct batadv_hard_iface * if_outgoing,struct batadv_orig_node * orig_node,struct batadv_neigh_node * neigh_node,bool best)264 batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
265 			    struct batadv_priv *bat_priv,
266 			    struct batadv_hard_iface *if_outgoing,
267 			    struct batadv_orig_node *orig_node,
268 			    struct batadv_neigh_node *neigh_node,
269 			    bool best)
270 {
271 	struct batadv_neigh_ifinfo *n_ifinfo;
272 	unsigned int last_seen_msecs;
273 	u32 throughput;
274 	void *hdr;
275 
276 	n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
277 	if (!n_ifinfo)
278 		return 0;
279 
280 	throughput = n_ifinfo->bat_v.throughput * 100;
281 
282 	batadv_neigh_ifinfo_put(n_ifinfo);
283 
284 	last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
285 
286 	if (if_outgoing != BATADV_IF_DEFAULT &&
287 	    if_outgoing != neigh_node->if_incoming)
288 		return 0;
289 
290 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
291 			  BATADV_CMD_GET_ORIGINATORS);
292 	if (!hdr)
293 		return -ENOBUFS;
294 
295 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
296 	    nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
297 		    neigh_node->addr) ||
298 	    nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
299 			   neigh_node->if_incoming->net_dev->name) ||
300 	    nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
301 			neigh_node->if_incoming->net_dev->ifindex) ||
302 	    nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
303 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
304 			last_seen_msecs))
305 		goto nla_put_failure;
306 
307 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
308 		goto nla_put_failure;
309 
310 	genlmsg_end(msg, hdr);
311 	return 0;
312 
313  nla_put_failure:
314 	genlmsg_cancel(msg, hdr);
315 	return -EMSGSIZE;
316 }
317 
318 /**
319  * batadv_v_orig_dump_entry() - Dump an originator entry into a message
320  * @msg: Netlink message to dump into
321  * @portid: Port making netlink request
322  * @seq: Sequence number of netlink message
323  * @bat_priv: The bat priv with all the mesh interface information
324  * @if_outgoing: Limit dump to entries with this outgoing interface
325  * @orig_node: Originator to dump
326  * @sub_s: Number of sub entries to skip
327  *
328  * This function assumes the caller holds rcu_read_lock().
329  *
330  * Return: Error code, or 0 on success
331  */
332 static int
batadv_v_orig_dump_entry(struct sk_buff * msg,u32 portid,u32 seq,struct batadv_priv * bat_priv,struct batadv_hard_iface * if_outgoing,struct batadv_orig_node * orig_node,int * sub_s)333 batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
334 			 struct batadv_priv *bat_priv,
335 			 struct batadv_hard_iface *if_outgoing,
336 			 struct batadv_orig_node *orig_node, int *sub_s)
337 {
338 	struct batadv_neigh_node *neigh_node_best;
339 	struct batadv_neigh_node *neigh_node;
340 	int sub = 0;
341 	bool best;
342 
343 	neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
344 	if (!neigh_node_best)
345 		goto out;
346 
347 	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
348 		if (sub++ < *sub_s)
349 			continue;
350 
351 		best = (neigh_node == neigh_node_best);
352 
353 		if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
354 						if_outgoing, orig_node,
355 						neigh_node, best)) {
356 			batadv_neigh_node_put(neigh_node_best);
357 
358 			*sub_s = sub - 1;
359 			return -EMSGSIZE;
360 		}
361 	}
362 
363  out:
364 	batadv_neigh_node_put(neigh_node_best);
365 
366 	*sub_s = 0;
367 	return 0;
368 }
369 
370 /**
371  * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
372  * @msg: Netlink message to dump into
373  * @portid: Port making netlink request
374  * @seq: Sequence number of netlink message
375  * @bat_priv: The bat priv with all the mesh interface information
376  * @if_outgoing: Limit dump to entries with this outgoing interface
377  * @head: Bucket to be dumped
378  * @idx_s: Number of entries to be skipped
379  * @sub: Number of sub entries to be skipped
380  *
381  * Return: Error code, or 0 on success
382  */
383 static int
batadv_v_orig_dump_bucket(struct sk_buff * msg,u32 portid,u32 seq,struct batadv_priv * bat_priv,struct batadv_hard_iface * if_outgoing,struct hlist_head * head,int * idx_s,int * sub)384 batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
385 			  struct batadv_priv *bat_priv,
386 			  struct batadv_hard_iface *if_outgoing,
387 			  struct hlist_head *head, int *idx_s, int *sub)
388 {
389 	struct batadv_orig_node *orig_node;
390 	int idx = 0;
391 
392 	rcu_read_lock();
393 	hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
394 		if (idx++ < *idx_s)
395 			continue;
396 
397 		if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
398 					     if_outgoing, orig_node, sub)) {
399 			rcu_read_unlock();
400 			*idx_s = idx - 1;
401 			return -EMSGSIZE;
402 		}
403 	}
404 	rcu_read_unlock();
405 
406 	*idx_s = 0;
407 	*sub = 0;
408 	return 0;
409 }
410 
411 /**
412  * batadv_v_orig_dump() - Dump the originators into a message
413  * @msg: Netlink message to dump into
414  * @cb: Control block containing additional options
415  * @bat_priv: The bat priv with all the mesh interface information
416  * @if_outgoing: Limit dump to entries with this outgoing interface
417  */
418 static void
batadv_v_orig_dump(struct sk_buff * msg,struct netlink_callback * cb,struct batadv_priv * bat_priv,struct batadv_hard_iface * if_outgoing)419 batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
420 		   struct batadv_priv *bat_priv,
421 		   struct batadv_hard_iface *if_outgoing)
422 {
423 	struct batadv_hashtable *hash = bat_priv->orig_hash;
424 	struct hlist_head *head;
425 	int bucket = cb->args[0];
426 	int idx = cb->args[1];
427 	int sub = cb->args[2];
428 	int portid = NETLINK_CB(cb->skb).portid;
429 
430 	while (bucket < hash->size) {
431 		head = &hash->table[bucket];
432 
433 		if (batadv_v_orig_dump_bucket(msg, portid,
434 					      cb->nlh->nlmsg_seq,
435 					      bat_priv, if_outgoing, head, &idx,
436 					      &sub))
437 			break;
438 
439 		bucket++;
440 	}
441 
442 	cb->args[0] = bucket;
443 	cb->args[1] = idx;
444 	cb->args[2] = sub;
445 }
446 
batadv_v_neigh_cmp(struct batadv_neigh_node * neigh1,struct batadv_hard_iface * if_outgoing1,struct batadv_neigh_node * neigh2,struct batadv_hard_iface * if_outgoing2)447 static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
448 			      struct batadv_hard_iface *if_outgoing1,
449 			      struct batadv_neigh_node *neigh2,
450 			      struct batadv_hard_iface *if_outgoing2)
451 {
452 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
453 	int ret = 0;
454 
455 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
456 	if (!ifinfo1)
457 		goto err_ifinfo1;
458 
459 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
460 	if (!ifinfo2)
461 		goto err_ifinfo2;
462 
463 	ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
464 
465 	batadv_neigh_ifinfo_put(ifinfo2);
466 err_ifinfo2:
467 	batadv_neigh_ifinfo_put(ifinfo1);
468 err_ifinfo1:
469 	return ret;
470 }
471 
batadv_v_neigh_is_sob(struct batadv_neigh_node * neigh1,struct batadv_hard_iface * if_outgoing1,struct batadv_neigh_node * neigh2,struct batadv_hard_iface * if_outgoing2)472 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
473 				  struct batadv_hard_iface *if_outgoing1,
474 				  struct batadv_neigh_node *neigh2,
475 				  struct batadv_hard_iface *if_outgoing2)
476 {
477 	struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
478 	u32 threshold;
479 	bool ret = false;
480 
481 	ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
482 	if (!ifinfo1)
483 		goto err_ifinfo1;
484 
485 	ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
486 	if (!ifinfo2)
487 		goto err_ifinfo2;
488 
489 	threshold = ifinfo1->bat_v.throughput / 4;
490 	threshold = ifinfo1->bat_v.throughput - threshold;
491 
492 	ret = ifinfo2->bat_v.throughput > threshold;
493 
494 	batadv_neigh_ifinfo_put(ifinfo2);
495 err_ifinfo2:
496 	batadv_neigh_ifinfo_put(ifinfo1);
497 err_ifinfo1:
498 	return ret;
499 }
500 
501 /**
502  * batadv_v_init_sel_class() - initialize GW selection class
503  * @bat_priv: the bat priv with all the mesh interface information
504  */
batadv_v_init_sel_class(struct batadv_priv * bat_priv)505 static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
506 {
507 	/* set default throughput difference threshold to 5Mbps */
508 	atomic_set(&bat_priv->gw.sel_class, 50);
509 }
510 
511 /**
512  * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
513  * @gw_node: the GW to retrieve the metric for
514  * @bw: the pointer where the metric will be stored. The metric is computed as
515  *  the minimum between the GW advertised throughput and the path throughput to
516  *  it in the mesh
517  *
518  * Return: 0 on success, -1 on failure
519  */
batadv_v_gw_throughput_get(struct batadv_gw_node * gw_node,u32 * bw)520 static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
521 {
522 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
523 	struct batadv_orig_node *orig_node;
524 	struct batadv_neigh_node *router;
525 	int ret = -1;
526 
527 	orig_node = gw_node->orig_node;
528 	router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
529 	if (!router)
530 		goto out;
531 
532 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
533 	if (!router_ifinfo)
534 		goto out;
535 
536 	/* the GW metric is computed as the minimum between the path throughput
537 	 * to reach the GW itself and the advertised bandwidth.
538 	 * This gives us an approximation of the effective throughput that the
539 	 * client can expect via this particular GW node
540 	 */
541 	*bw = router_ifinfo->bat_v.throughput;
542 	*bw = min_t(u32, *bw, gw_node->bandwidth_down);
543 
544 	ret = 0;
545 out:
546 	batadv_neigh_node_put(router);
547 	batadv_neigh_ifinfo_put(router_ifinfo);
548 
549 	return ret;
550 }
551 
552 /**
553  * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
554  * @bat_priv: the bat priv with all the mesh interface information
555  *
556  * Return: the GW node having the best GW-metric, NULL if no GW is known
557  */
558 static struct batadv_gw_node *
batadv_v_gw_get_best_gw_node(struct batadv_priv * bat_priv)559 batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
560 {
561 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
562 	u32 max_bw = 0, bw;
563 
564 	rcu_read_lock();
565 	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
566 		if (!kref_get_unless_zero(&gw_node->refcount))
567 			continue;
568 
569 		if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
570 			goto next;
571 
572 		if (curr_gw && bw <= max_bw)
573 			goto next;
574 
575 		batadv_gw_node_put(curr_gw);
576 
577 		curr_gw = gw_node;
578 		kref_get(&curr_gw->refcount);
579 		max_bw = bw;
580 
581 next:
582 		batadv_gw_node_put(gw_node);
583 	}
584 	rcu_read_unlock();
585 
586 	return curr_gw;
587 }
588 
589 /**
590  * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
591  * @bat_priv: the bat priv with all the mesh interface information
592  * @curr_gw_orig: originator representing the currently selected GW
593  * @orig_node: the originator representing the new candidate
594  *
595  * Return: true if orig_node can be selected as current GW, false otherwise
596  */
batadv_v_gw_is_eligible(struct batadv_priv * bat_priv,struct batadv_orig_node * curr_gw_orig,struct batadv_orig_node * orig_node)597 static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
598 				    struct batadv_orig_node *curr_gw_orig,
599 				    struct batadv_orig_node *orig_node)
600 {
601 	struct batadv_gw_node *curr_gw, *orig_gw = NULL;
602 	u32 gw_throughput, orig_throughput, threshold;
603 	bool ret = false;
604 
605 	threshold = atomic_read(&bat_priv->gw.sel_class);
606 
607 	curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
608 	if (!curr_gw) {
609 		ret = true;
610 		goto out;
611 	}
612 
613 	if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
614 		ret = true;
615 		goto out;
616 	}
617 
618 	orig_gw = batadv_gw_node_get(bat_priv, orig_node);
619 	if (!orig_gw)
620 		goto out;
621 
622 	if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
623 		goto out;
624 
625 	if (orig_throughput < gw_throughput)
626 		goto out;
627 
628 	if ((orig_throughput - gw_throughput) < threshold)
629 		goto out;
630 
631 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
632 		   "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
633 		   gw_throughput, orig_throughput);
634 
635 	ret = true;
636 out:
637 	batadv_gw_node_put(curr_gw);
638 	batadv_gw_node_put(orig_gw);
639 
640 	return ret;
641 }
642 
643 /**
644  * batadv_v_gw_dump_entry() - Dump a gateway into a message
645  * @msg: Netlink message to dump into
646  * @portid: Port making netlink request
647  * @cb: Control block containing additional options
648  * @bat_priv: The bat priv with all the mesh interface information
649  * @gw_node: Gateway to be dumped
650  *
651  * Return: Error code, or 0 on success
652  */
batadv_v_gw_dump_entry(struct sk_buff * msg,u32 portid,struct netlink_callback * cb,struct batadv_priv * bat_priv,struct batadv_gw_node * gw_node)653 static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
654 				  struct netlink_callback *cb,
655 				  struct batadv_priv *bat_priv,
656 				  struct batadv_gw_node *gw_node)
657 {
658 	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
659 	struct batadv_neigh_node *router;
660 	struct batadv_gw_node *curr_gw = NULL;
661 	int ret = 0;
662 	void *hdr;
663 
664 	router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
665 	if (!router)
666 		goto out;
667 
668 	router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
669 	if (!router_ifinfo)
670 		goto out;
671 
672 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
673 
674 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
675 			  &batadv_netlink_family, NLM_F_MULTI,
676 			  BATADV_CMD_GET_GATEWAYS);
677 	if (!hdr) {
678 		ret = -ENOBUFS;
679 		goto out;
680 	}
681 
682 	genl_dump_check_consistent(cb, hdr);
683 
684 	ret = -EMSGSIZE;
685 
686 	if (curr_gw == gw_node) {
687 		if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
688 			genlmsg_cancel(msg, hdr);
689 			goto out;
690 		}
691 	}
692 
693 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
694 		    gw_node->orig_node->orig)) {
695 		genlmsg_cancel(msg, hdr);
696 		goto out;
697 	}
698 
699 	if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
700 			router_ifinfo->bat_v.throughput)) {
701 		genlmsg_cancel(msg, hdr);
702 		goto out;
703 	}
704 
705 	if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
706 		genlmsg_cancel(msg, hdr);
707 		goto out;
708 	}
709 
710 	if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
711 			   router->if_incoming->net_dev->name)) {
712 		genlmsg_cancel(msg, hdr);
713 		goto out;
714 	}
715 
716 	if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
717 			router->if_incoming->net_dev->ifindex)) {
718 		genlmsg_cancel(msg, hdr);
719 		goto out;
720 	}
721 
722 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
723 			gw_node->bandwidth_down)) {
724 		genlmsg_cancel(msg, hdr);
725 		goto out;
726 	}
727 
728 	if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
729 		genlmsg_cancel(msg, hdr);
730 		goto out;
731 	}
732 
733 	genlmsg_end(msg, hdr);
734 	ret = 0;
735 
736 out:
737 	batadv_gw_node_put(curr_gw);
738 	batadv_neigh_ifinfo_put(router_ifinfo);
739 	batadv_neigh_node_put(router);
740 	return ret;
741 }
742 
743 /**
744  * batadv_v_gw_dump() - Dump gateways into a message
745  * @msg: Netlink message to dump into
746  * @cb: Control block containing additional options
747  * @bat_priv: The bat priv with all the mesh interface information
748  */
batadv_v_gw_dump(struct sk_buff * msg,struct netlink_callback * cb,struct batadv_priv * bat_priv)749 static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
750 			     struct batadv_priv *bat_priv)
751 {
752 	int portid = NETLINK_CB(cb->skb).portid;
753 	struct batadv_gw_node *gw_node;
754 	int idx_skip = cb->args[0];
755 	int idx = 0;
756 
757 	spin_lock_bh(&bat_priv->gw.list_lock);
758 	cb->seq = bat_priv->gw.generation << 1 | 1;
759 
760 	hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
761 		if (idx++ < idx_skip)
762 			continue;
763 
764 		if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
765 					   gw_node)) {
766 			idx_skip = idx - 1;
767 			goto unlock;
768 		}
769 	}
770 
771 	idx_skip = idx;
772 unlock:
773 	spin_unlock_bh(&bat_priv->gw.list_lock);
774 
775 	cb->args[0] = idx_skip;
776 }
777 
778 static struct batadv_algo_ops batadv_batman_v __read_mostly = {
779 	.name = "BATMAN_V",
780 	.iface = {
781 		.activate = batadv_v_iface_activate,
782 		.enable = batadv_v_iface_enable,
783 		.disable = batadv_v_iface_disable,
784 		.update_mac = batadv_v_iface_update_mac,
785 		.primary_set = batadv_v_primary_iface_set,
786 	},
787 	.neigh = {
788 		.hardif_init = batadv_v_hardif_neigh_init,
789 		.cmp = batadv_v_neigh_cmp,
790 		.is_similar_or_better = batadv_v_neigh_is_sob,
791 		.dump = batadv_v_neigh_dump,
792 	},
793 	.orig = {
794 		.dump = batadv_v_orig_dump,
795 	},
796 	.gw = {
797 		.init_sel_class = batadv_v_init_sel_class,
798 		.sel_class_max = U32_MAX,
799 		.get_best_gw_node = batadv_v_gw_get_best_gw_node,
800 		.is_eligible = batadv_v_gw_is_eligible,
801 		.dump = batadv_v_gw_dump,
802 	},
803 };
804 
805 /**
806  * batadv_v_hardif_init() - initialize the algorithm specific fields in the
807  *  hard-interface object
808  * @hard_iface: the hard-interface to initialize
809  */
batadv_v_hardif_init(struct batadv_hard_iface * hard_iface)810 void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
811 {
812 	/* enable link throughput auto-detection by setting the throughput
813 	 * override to zero
814 	 */
815 	atomic_set(&hard_iface->bat_v.throughput_override, 0);
816 	atomic_set(&hard_iface->bat_v.elp_interval, 500);
817 
818 	hard_iface->bat_v.aggr_len = 0;
819 	skb_queue_head_init(&hard_iface->bat_v.aggr_list);
820 	INIT_DELAYED_WORK(&hard_iface->bat_v.aggr_wq,
821 			  batadv_v_ogm_aggr_work);
822 }
823 
824 /**
825  * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
826  *  mesh
827  * @bat_priv: the object representing the mesh interface to initialise
828  *
829  * Return: 0 on success or a negative error code otherwise
830  */
batadv_v_mesh_init(struct batadv_priv * bat_priv)831 int batadv_v_mesh_init(struct batadv_priv *bat_priv)
832 {
833 	int ret = 0;
834 
835 	ret = batadv_v_ogm_init(bat_priv);
836 	if (ret < 0)
837 		return ret;
838 
839 	return 0;
840 }
841 
842 /**
843  * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
844  * @bat_priv: the object representing the mesh interface to free
845  */
batadv_v_mesh_free(struct batadv_priv * bat_priv)846 void batadv_v_mesh_free(struct batadv_priv *bat_priv)
847 {
848 	batadv_v_ogm_free(bat_priv);
849 }
850 
851 /**
852  * batadv_v_init() - B.A.T.M.A.N. V initialization function
853  *
854  * Description: Takes care of initializing all the subcomponents.
855  * It is invoked upon module load only.
856  *
857  * Return: 0 on success or a negative error code otherwise
858  */
batadv_v_init(void)859 int __init batadv_v_init(void)
860 {
861 	int ret;
862 
863 	/* B.A.T.M.A.N. V echo location protocol packet  */
864 	ret = batadv_recv_handler_register(BATADV_ELP,
865 					   batadv_v_elp_packet_recv);
866 	if (ret < 0)
867 		return ret;
868 
869 	ret = batadv_recv_handler_register(BATADV_OGM2,
870 					   batadv_v_ogm_packet_recv);
871 	if (ret < 0)
872 		goto elp_unregister;
873 
874 	ret = batadv_algo_register(&batadv_batman_v);
875 	if (ret < 0)
876 		goto ogm_unregister;
877 
878 	return ret;
879 
880 ogm_unregister:
881 	batadv_recv_handler_unregister(BATADV_OGM2);
882 
883 elp_unregister:
884 	batadv_recv_handler_unregister(BATADV_ELP);
885 
886 	return ret;
887 }
888