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