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