bat_v_elp.c 17 KB

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