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_elp.h"
8 #include "main.h"
9
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/byteorder/generic.h>
13 #include <linux/compiler.h>
14 #include <linux/container_of.h>
15 #include <linux/errno.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/gfp.h>
19 #include <linux/if_ether.h>
20 #include <linux/jiffies.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 #include <linux/minmax.h>
24 #include <linux/netdevice.h>
25 #include <linux/nl80211.h>
26 #include <linux/random.h>
27 #include <linux/rculist.h>
28 #include <linux/rcupdate.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/skbuff.h>
31 #include <linux/slab.h>
32 #include <linux/stddef.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
35 #include <linux/workqueue.h>
36 #include <net/cfg80211.h>
37 #include <uapi/linux/batadv_packet.h>
38
39 #include "bat_v_ogm.h"
40 #include "hard-interface.h"
41 #include "log.h"
42 #include "originator.h"
43 #include "routing.h"
44 #include "send.h"
45
46 /**
47 * struct batadv_v_metric_queue_entry - list of hardif neighbors which require
48 * and metric update
49 */
50 struct batadv_v_metric_queue_entry {
51 /** @hardif_neigh: hardif neighbor scheduled for metric update */
52 struct batadv_hardif_neigh_node *hardif_neigh;
53
54 /** @list: list node for metric_queue */
55 struct list_head list;
56 };
57
58 /**
59 * batadv_v_elp_start_timer() - restart timer for ELP periodic work
60 * @hard_iface: the interface for which the timer has to be reset
61 */
batadv_v_elp_start_timer(struct batadv_hard_iface * hard_iface)62 static void batadv_v_elp_start_timer(struct batadv_hard_iface *hard_iface)
63 {
64 unsigned int msecs;
65
66 msecs = READ_ONCE(hard_iface->bat_v.elp_interval) - BATADV_JITTER;
67 msecs += get_random_u32_below(2 * BATADV_JITTER);
68
69 queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.elp_wq,
70 msecs_to_jiffies(msecs));
71 }
72
73 /**
74 * batadv_v_elp_get_throughput() - get the throughput towards a neighbour
75 * @neigh: the neighbour for which the throughput has to be obtained
76 * @pthroughput: calculated throughput towards the given neighbour in multiples
77 * of 100kpbs (a value of '1' equals 0.1Mbps, '10' equals 1Mbps, etc).
78 *
79 * Return: true when value behind @pthroughput was set
80 */
batadv_v_elp_get_throughput(struct batadv_hardif_neigh_node * neigh,u32 * pthroughput)81 static bool batadv_v_elp_get_throughput(struct batadv_hardif_neigh_node *neigh,
82 u32 *pthroughput)
83 {
84 struct batadv_hard_iface *hard_iface = neigh->if_incoming;
85 struct net_device *mesh_iface = hard_iface->mesh_iface;
86 struct ethtool_link_ksettings link_settings;
87 struct net_device *real_netdev;
88 struct station_info sinfo;
89 u32 wifi_flags;
90 u32 throughput;
91 int ret;
92
93 /* if the user specified a customised value for this interface, then
94 * return it directly
95 */
96 throughput = READ_ONCE(hard_iface->bat_v.throughput_override);
97 if (throughput != 0) {
98 *pthroughput = throughput;
99 return true;
100 }
101
102 /* if this is a wireless device, then ask its throughput through
103 * cfg80211 API
104 */
105 wifi_flags = batadv_hardif_get_wifi_flags(hard_iface);
106 if (batadv_is_wifi(wifi_flags)) {
107 if (!batadv_is_cfg80211(wifi_flags))
108 /* unsupported WiFi driver version */
109 goto default_throughput;
110
111 /* only use rtnl_trylock because the elp worker will be cancelled while
112 * the rntl_lock is held. the disable_delayed_work_sync() would otherwise
113 * wait forever when the elp work_item was started and it is then also
114 * trying to rtnl_lock
115 */
116 if (!rtnl_trylock())
117 return false;
118 real_netdev = __batadv_get_real_netdev(hard_iface->net_dev);
119 rtnl_unlock();
120 if (!real_netdev)
121 goto default_throughput;
122
123 ret = cfg80211_get_station(real_netdev, neigh->addr, &sinfo);
124
125 if (!ret) {
126 /* free the TID stats immediately */
127 cfg80211_sinfo_release_content(&sinfo);
128 }
129
130 dev_put(real_netdev);
131 if (ret == -ENOENT) {
132 /* Node is not associated anymore! It would be
133 * possible to delete this neighbor. For now set
134 * the throughput metric to 0.
135 */
136 *pthroughput = 0;
137 return true;
138 }
139 if (ret)
140 goto default_throughput;
141
142 if (sinfo.filled & BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT)) {
143 *pthroughput = sinfo.expected_throughput / 100;
144 return true;
145 }
146
147 /* try to estimate the expected throughput based on reported tx
148 * rates
149 */
150 if (sinfo.filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
151 *pthroughput = cfg80211_calculate_bitrate(&sinfo.txrate) / 3;
152 return true;
153 }
154
155 goto default_throughput;
156 }
157
158 /* only use rtnl_trylock because the elp worker will be cancelled while
159 * the rntl_lock is held. the disable_delayed_work_sync() would otherwise
160 * wait forever when the elp work_item was started and it is then also
161 * trying to rtnl_lock
162 */
163 if (!rtnl_trylock())
164 return false;
165
166 /* if not a wifi interface, check if this device provides data via
167 * ethtool (e.g. an Ethernet adapter)
168 */
169 ret = __ethtool_get_link_ksettings(hard_iface->net_dev, &link_settings);
170 rtnl_unlock();
171 if (ret == 0) {
172 /* link characteristics might change over time */
173 if (link_settings.base.duplex == DUPLEX_FULL)
174 hard_iface->bat_v.flags |= BATADV_FULL_DUPLEX;
175 else
176 hard_iface->bat_v.flags &= ~BATADV_FULL_DUPLEX;
177
178 throughput = link_settings.base.speed;
179 if (throughput && throughput != SPEED_UNKNOWN) {
180 *pthroughput = throughput * 10;
181 return true;
182 }
183 }
184
185 default_throughput:
186 if (!(hard_iface->bat_v.flags & BATADV_WARNING_DEFAULT)) {
187 batadv_info(mesh_iface,
188 "WiFi driver or ethtool info does not provide information about link speeds on interface %s, therefore defaulting to hardcoded throughput values of %u.%1u Mbps. Consider overriding the throughput manually or checking your driver.\n",
189 hard_iface->net_dev->name,
190 BATADV_THROUGHPUT_DEFAULT_VALUE / 10,
191 BATADV_THROUGHPUT_DEFAULT_VALUE % 10);
192 hard_iface->bat_v.flags |= BATADV_WARNING_DEFAULT;
193 }
194
195 /* if none of the above cases apply, return the base_throughput */
196 *pthroughput = BATADV_THROUGHPUT_DEFAULT_VALUE;
197 return true;
198 }
199
200 /**
201 * batadv_v_elp_throughput_metric_update() - worker updating the throughput
202 * metric of a single hop neighbour
203 * @neigh: the neighbour to probe
204 */
205 static void
batadv_v_elp_throughput_metric_update(struct batadv_hardif_neigh_node * neigh)206 batadv_v_elp_throughput_metric_update(struct batadv_hardif_neigh_node *neigh)
207 {
208 u32 throughput;
209 bool valid;
210
211 valid = batadv_v_elp_get_throughput(neigh, &throughput);
212 if (!valid)
213 return;
214
215 ewma_throughput_add(&neigh->bat_v.throughput, throughput);
216 }
217
218 /**
219 * batadv_v_elp_wifi_neigh_probe() - send link probing packets to a neighbour
220 * @neigh: the neighbour to probe
221 *
222 * Sends a predefined number of unicast wifi packets to a given neighbour in
223 * order to trigger the throughput estimation on this link by the RC algorithm.
224 * Packets are sent only if there is not enough payload unicast traffic towards
225 * this neighbour..
226 *
227 * Return: True on success and false in case of error during skb preparation.
228 */
229 static bool
batadv_v_elp_wifi_neigh_probe(struct batadv_hardif_neigh_node * neigh)230 batadv_v_elp_wifi_neigh_probe(struct batadv_hardif_neigh_node *neigh)
231 {
232 struct batadv_hard_iface *hard_iface = neigh->if_incoming;
233 struct batadv_priv *bat_priv;
234 unsigned long last_tx_diff;
235 struct sk_buff *skb;
236 int elp_skb_len;
237 int probe_len;
238 int i;
239
240 bat_priv = netdev_priv(hard_iface->mesh_iface);
241
242 /* this probing routine is for Wifi neighbours only */
243 if (!batadv_is_wifi_hardif(hard_iface))
244 return true;
245
246 /* probe the neighbor only if no unicast packets have been sent
247 * to it in the last 100 milliseconds: this is the rate control
248 * algorithm sampling interval (minstrel). In this way, if not
249 * enough traffic has been sent to the neighbor, batman-adv can
250 * generate 2 probe packets and push the RC algorithm to perform
251 * the sampling
252 */
253 last_tx_diff = jiffies_to_msecs(jiffies - neigh->bat_v.last_unicast_tx);
254 if (last_tx_diff <= BATADV_ELP_PROBE_MAX_TX_DIFF)
255 return true;
256
257 probe_len = max_t(int, sizeof(struct batadv_elp_packet),
258 BATADV_ELP_MIN_PROBE_SIZE);
259
260 for (i = 0; i < BATADV_ELP_PROBES_PER_NODE; i++) {
261 elp_skb_len = hard_iface->bat_v.elp_skb->len;
262 skb = skb_copy_expand(hard_iface->bat_v.elp_skb, 0,
263 probe_len - elp_skb_len,
264 GFP_ATOMIC);
265 if (!skb)
266 return false;
267
268 /* Tell the skb to get as big as the allocated space (we want
269 * the packet to be exactly of that size to make the link
270 * throughput estimation effective.
271 */
272 skb_put_zero(skb, probe_len - hard_iface->bat_v.elp_skb->len);
273
274 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
275 "Sending unicast (probe) ELP packet on interface %s to %pM\n",
276 hard_iface->net_dev->name, neigh->addr);
277
278 batadv_send_skb_packet(skb, hard_iface, neigh->addr);
279 }
280
281 return true;
282 }
283
284 /**
285 * batadv_v_elp_periodic_work() - ELP periodic task per interface
286 * @work: work queue item
287 *
288 * Emits broadcast ELP messages in regular intervals.
289 */
batadv_v_elp_periodic_work(struct work_struct * work)290 static void batadv_v_elp_periodic_work(struct work_struct *work)
291 {
292 struct batadv_v_metric_queue_entry *metric_entry;
293 struct batadv_v_metric_queue_entry *metric_safe;
294 struct batadv_hardif_neigh_node *hardif_neigh;
295 struct batadv_hard_iface_bat_v *bat_v;
296 struct batadv_hard_iface *hard_iface;
297 struct batadv_elp_packet *elp_packet;
298 struct list_head metric_queue;
299 struct batadv_priv *bat_priv;
300 struct sk_buff *skb;
301 u32 elp_interval;
302
303 bat_v = container_of(work, struct batadv_hard_iface_bat_v, elp_wq.work);
304 hard_iface = container_of(bat_v, struct batadv_hard_iface, bat_v);
305 bat_priv = netdev_priv(hard_iface->mesh_iface);
306
307 if (READ_ONCE(bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
308 goto out;
309
310 /* we are in the process of shutting this interface down */
311 if (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
312 goto out;
313
314 /* the interface was enabled but may not be ready yet */
315 if (hard_iface->if_status != BATADV_IF_ACTIVE)
316 goto restart_timer;
317
318 skb = skb_copy(hard_iface->bat_v.elp_skb, GFP_ATOMIC);
319 if (!skb)
320 goto restart_timer;
321
322 elp_packet = (struct batadv_elp_packet *)skb->data;
323 elp_packet->seqno = htonl(atomic_read(&hard_iface->bat_v.elp_seqno));
324 elp_interval = READ_ONCE(hard_iface->bat_v.elp_interval);
325 elp_packet->elp_interval = htonl(elp_interval);
326
327 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
328 "Sending broadcast ELP packet on interface %s, seqno %u\n",
329 hard_iface->net_dev->name,
330 atomic_read(&hard_iface->bat_v.elp_seqno));
331
332 batadv_send_broadcast_skb(skb, hard_iface);
333
334 atomic_inc(&hard_iface->bat_v.elp_seqno);
335
336 INIT_LIST_HEAD(&metric_queue);
337
338 /* The throughput metric is updated on each sent packet. This way, if a
339 * node is dead and no longer sends packets, batman-adv is still able to
340 * react timely to its death.
341 *
342 * The throughput metric is updated by following these steps:
343 * 1) if the hard_iface is wifi => send a number of unicast ELPs for
344 * probing/sampling to each neighbor
345 * 2) update the throughput metric value of each neighbor (note that the
346 * value retrieved in this step might be 100ms old because the
347 * probing packets at point 1) could still be in the HW queue)
348 */
349 rcu_read_lock();
350 hlist_for_each_entry_rcu(hardif_neigh, &hard_iface->neigh_list, list) {
351 if (!batadv_v_elp_wifi_neigh_probe(hardif_neigh))
352 /* if something goes wrong while probing, better to stop
353 * sending packets immediately and reschedule the task
354 */
355 break;
356
357 if (!kref_get_unless_zero(&hardif_neigh->refcount))
358 continue;
359
360 /* Reading the estimated throughput from cfg80211 is a task that
361 * may sleep and that is not allowed in an rcu protected
362 * context. Therefore add it to metric_queue and process it
363 * outside rcu protected context.
364 */
365 metric_entry = kzalloc_obj(*metric_entry, GFP_ATOMIC);
366 if (!metric_entry) {
367 batadv_hardif_neigh_put(hardif_neigh);
368 continue;
369 }
370
371 metric_entry->hardif_neigh = hardif_neigh;
372 list_add(&metric_entry->list, &metric_queue);
373 }
374 rcu_read_unlock();
375
376 list_for_each_entry_safe(metric_entry, metric_safe, &metric_queue, list) {
377 batadv_v_elp_throughput_metric_update(metric_entry->hardif_neigh);
378
379 batadv_hardif_neigh_put(metric_entry->hardif_neigh);
380 list_del(&metric_entry->list);
381 kfree(metric_entry);
382 }
383
384 restart_timer:
385 batadv_v_elp_start_timer(hard_iface);
386 out:
387 return;
388 }
389
390 /**
391 * batadv_v_elp_iface_enable() - setup the ELP interface private resources
392 * @hard_iface: interface for which the data has to be prepared
393 *
394 * Return: 0 on success or a -ENOMEM in case of failure.
395 */
batadv_v_elp_iface_enable(struct batadv_hard_iface * hard_iface)396 int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface)
397 {
398 static const size_t tvlv_padding = sizeof(__be32);
399 struct batadv_elp_packet *elp_packet;
400 unsigned char *elp_buff;
401 int res = -ENOMEM;
402 u32 random_seqno;
403 size_t size;
404
405 size = ETH_HLEN + NET_IP_ALIGN + BATADV_ELP_HLEN + tvlv_padding;
406 hard_iface->bat_v.elp_skb = dev_alloc_skb(size);
407 if (!hard_iface->bat_v.elp_skb)
408 goto out;
409
410 skb_reserve(hard_iface->bat_v.elp_skb, ETH_HLEN + NET_IP_ALIGN);
411 elp_buff = skb_put_zero(hard_iface->bat_v.elp_skb,
412 BATADV_ELP_HLEN + tvlv_padding);
413 elp_packet = (struct batadv_elp_packet *)elp_buff;
414
415 elp_packet->packet_type = BATADV_ELP;
416 elp_packet->version = BATADV_COMPAT_VERSION;
417
418 /* randomize initial seqno to avoid collision */
419 get_random_bytes(&random_seqno, sizeof(random_seqno));
420 atomic_set(&hard_iface->bat_v.elp_seqno, random_seqno);
421
422 /* assume full-duplex by default */
423 hard_iface->bat_v.flags |= BATADV_FULL_DUPLEX;
424
425 /* warn the user (again) if there is no throughput data is available */
426 hard_iface->bat_v.flags &= ~BATADV_WARNING_DEFAULT;
427
428 if (batadv_is_wifi_hardif(hard_iface))
429 hard_iface->bat_v.flags &= ~BATADV_FULL_DUPLEX;
430
431 INIT_DELAYED_WORK(&hard_iface->bat_v.elp_wq,
432 batadv_v_elp_periodic_work);
433 batadv_v_elp_start_timer(hard_iface);
434 res = 0;
435
436 out:
437 return res;
438 }
439
440 /**
441 * batadv_v_elp_iface_disable() - release ELP interface private resources
442 * @hard_iface: interface for which the resources have to be released
443 */
batadv_v_elp_iface_disable(struct batadv_hard_iface * hard_iface)444 void batadv_v_elp_iface_disable(struct batadv_hard_iface *hard_iface)
445 {
446 disable_delayed_work_sync(&hard_iface->bat_v.elp_wq);
447
448 dev_kfree_skb(hard_iface->bat_v.elp_skb);
449 hard_iface->bat_v.elp_skb = NULL;
450 }
451
452 /**
453 * batadv_v_elp_iface_activate() - update the ELP buffer belonging to the given
454 * hard-interface
455 * @primary_iface: the new primary interface
456 * @hard_iface: interface holding the to-be-updated buffer
457 */
batadv_v_elp_iface_activate(struct batadv_hard_iface * primary_iface,struct batadv_hard_iface * hard_iface)458 void batadv_v_elp_iface_activate(struct batadv_hard_iface *primary_iface,
459 struct batadv_hard_iface *hard_iface)
460 {
461 struct batadv_elp_packet *elp_packet;
462 struct sk_buff *skb;
463
464 if (!hard_iface->bat_v.elp_skb)
465 return;
466
467 skb = hard_iface->bat_v.elp_skb;
468 elp_packet = (struct batadv_elp_packet *)skb->data;
469 ether_addr_copy(elp_packet->orig,
470 primary_iface->net_dev->dev_addr);
471 }
472
473 /**
474 * batadv_v_elp_primary_iface_set() - change internal data to reflect the new
475 * primary interface
476 * @primary_iface: the new primary interface
477 */
batadv_v_elp_primary_iface_set(struct batadv_hard_iface * primary_iface)478 void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface)
479 {
480 struct batadv_hard_iface *hard_iface;
481 struct list_head *iter;
482
483 /* update orig field of every elp iface belonging to this mesh */
484 rcu_read_lock();
485 netdev_for_each_lower_private_rcu(primary_iface->mesh_iface, hard_iface, iter)
486 batadv_v_elp_iface_activate(primary_iface, hard_iface);
487 rcu_read_unlock();
488 }
489
490 /**
491 * batadv_v_elp_neigh_update() - update an ELP neighbour node
492 * @bat_priv: the bat priv with all the mesh interface information
493 * @neigh_addr: the neighbour interface address
494 * @if_incoming: the interface the packet was received through
495 * @elp_packet: the received ELP packet
496 *
497 * Updates the ELP neighbour node state with the data received within the new
498 * ELP packet.
499 */
batadv_v_elp_neigh_update(struct batadv_priv * bat_priv,u8 * neigh_addr,struct batadv_hard_iface * if_incoming,struct batadv_elp_packet * elp_packet)500 static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv,
501 u8 *neigh_addr,
502 struct batadv_hard_iface *if_incoming,
503 struct batadv_elp_packet *elp_packet)
504
505 {
506 struct batadv_hardif_neigh_node *hardif_neigh;
507 struct batadv_orig_node *orig_neigh;
508 struct batadv_neigh_node *neigh;
509 s32 elp_latest_seqno;
510 s32 seqno_diff;
511
512 orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig);
513 if (!orig_neigh)
514 return;
515
516 neigh = batadv_neigh_node_get_or_create(orig_neigh,
517 if_incoming, neigh_addr);
518 if (!neigh)
519 goto orig_free;
520
521 hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr);
522 if (!hardif_neigh)
523 goto neigh_free;
524
525 elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno;
526 seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno;
527
528 /* known or older sequence numbers are ignored. However always adopt
529 * if the router seems to have been restarted.
530 */
531 if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE)
532 goto hardif_free;
533
534 neigh->last_seen = jiffies;
535 hardif_neigh->last_seen = jiffies;
536 hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno);
537 hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval);
538
539 hardif_free:
540 batadv_hardif_neigh_put(hardif_neigh);
541 neigh_free:
542 batadv_neigh_node_put(neigh);
543 orig_free:
544 batadv_orig_node_put(orig_neigh);
545 }
546
547 /**
548 * batadv_v_elp_packet_recv() - main ELP packet handler
549 * @skb: the received packet
550 * @if_incoming: the interface this packet was received through
551 *
552 * Return: NET_RX_SUCCESS and consumes the skb if the packet was properly
553 * processed or NET_RX_DROP in case of failure.
554 */
batadv_v_elp_packet_recv(struct sk_buff * skb,struct batadv_hard_iface * if_incoming)555 int batadv_v_elp_packet_recv(struct sk_buff *skb,
556 struct batadv_hard_iface *if_incoming)
557 {
558 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
559 struct batadv_elp_packet *elp_packet;
560 struct batadv_hard_iface *primary_if;
561 struct ethhdr *ethhdr;
562 int ret = NET_RX_DROP;
563 bool res;
564
565 res = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN);
566 if (!res)
567 goto free_skb;
568
569 ethhdr = eth_hdr(skb);
570 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
571 goto free_skb;
572
573 /* did we receive a B.A.T.M.A.N. V ELP packet on an interface
574 * that does not have B.A.T.M.A.N. V ELP enabled ?
575 */
576 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
577 goto free_skb;
578
579 elp_packet = (struct batadv_elp_packet *)skb->data;
580
581 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
582 "Received ELP packet from %pM seqno %u ORIG: %pM\n",
583 ethhdr->h_source, ntohl(elp_packet->seqno),
584 elp_packet->orig);
585
586 primary_if = batadv_primary_if_get_selected(bat_priv);
587 if (!primary_if)
588 goto free_skb;
589
590 batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming,
591 elp_packet);
592
593 ret = NET_RX_SUCCESS;
594 batadv_hardif_put(primary_if);
595
596 free_skb:
597 if (ret == NET_RX_SUCCESS)
598 consume_skb(skb);
599 else
600 kfree_skb(skb);
601
602 return ret;
603 }
604