bat_v_elp.c 17 KB

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