bat_v_elp.c 17 KB

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