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