bat_v.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. #include "bat_v.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/cache.h>
  10. #include <linux/errno.h>
  11. #include <linux/if_ether.h>
  12. #include <linux/init.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/kref.h>
  15. #include <linux/limits.h>
  16. #include <linux/list.h>
  17. #include <linux/minmax.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/netlink.h>
  20. #include <linux/rculist.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/stddef.h>
  25. #include <linux/types.h>
  26. #include <linux/workqueue.h>
  27. #include <net/genetlink.h>
  28. #include <net/netlink.h>
  29. #include <uapi/linux/batadv_packet.h>
  30. #include <uapi/linux/batman_adv.h>
  31. #include "bat_algo.h"
  32. #include "bat_v_elp.h"
  33. #include "bat_v_ogm.h"
  34. #include "gateway_client.h"
  35. #include "hard-interface.h"
  36. #include "hash.h"
  37. #include "log.h"
  38. #include "netlink.h"
  39. #include "originator.h"
  40. static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
  41. {
  42. struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
  43. struct batadv_hard_iface *primary_if;
  44. primary_if = batadv_primary_if_get_selected(bat_priv);
  45. if (primary_if) {
  46. batadv_v_elp_iface_activate(primary_if, hard_iface);
  47. batadv_hardif_put(primary_if);
  48. }
  49. /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
  50. * set the interface as ACTIVE right away, without any risk of race
  51. * condition
  52. */
  53. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  54. hard_iface->if_status = BATADV_IF_ACTIVE;
  55. }
  56. static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
  57. {
  58. int ret;
  59. ret = batadv_v_elp_iface_enable(hard_iface);
  60. if (ret < 0)
  61. return ret;
  62. ret = batadv_v_ogm_iface_enable(hard_iface);
  63. if (ret < 0)
  64. batadv_v_elp_iface_disable(hard_iface);
  65. return ret;
  66. }
  67. static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
  68. {
  69. batadv_v_ogm_iface_disable(hard_iface);
  70. batadv_v_elp_iface_disable(hard_iface);
  71. }
  72. static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
  73. {
  74. batadv_v_elp_primary_iface_set(hard_iface);
  75. batadv_v_ogm_primary_iface_set(hard_iface);
  76. }
  77. /**
  78. * batadv_v_iface_update_mac() - react to hard-interface MAC address change
  79. * @hard_iface: the modified interface
  80. *
  81. * If the modified interface is the primary one, update the originator
  82. * address in the ELP and OGM messages to reflect the new MAC address.
  83. */
  84. static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
  85. {
  86. struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
  87. struct batadv_hard_iface *primary_if;
  88. primary_if = batadv_primary_if_get_selected(bat_priv);
  89. if (primary_if != hard_iface)
  90. goto out;
  91. batadv_v_primary_iface_set(hard_iface);
  92. out:
  93. batadv_hardif_put(primary_if);
  94. }
  95. static void
  96. batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
  97. {
  98. ewma_throughput_init(&hardif_neigh->bat_v.throughput);
  99. }
  100. /**
  101. * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
  102. * @msg: Netlink message to dump into
  103. * @portid: Port making netlink request
  104. * @seq: Sequence number of netlink message
  105. * @hardif_neigh: Neighbour to dump
  106. *
  107. * Return: Error code, or 0 on success
  108. */
  109. static int
  110. batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
  111. struct batadv_hardif_neigh_node *hardif_neigh)
  112. {
  113. void *hdr;
  114. unsigned int last_seen_msecs;
  115. u32 throughput;
  116. last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
  117. throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
  118. throughput = throughput * 100;
  119. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  120. BATADV_CMD_GET_NEIGHBORS);
  121. if (!hdr)
  122. return -ENOBUFS;
  123. if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  124. hardif_neigh->addr) ||
  125. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  126. hardif_neigh->if_incoming->net_dev->name) ||
  127. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  128. hardif_neigh->if_incoming->net_dev->ifindex) ||
  129. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  130. last_seen_msecs) ||
  131. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
  132. goto nla_put_failure;
  133. genlmsg_end(msg, hdr);
  134. return 0;
  135. nla_put_failure:
  136. genlmsg_cancel(msg, hdr);
  137. return -EMSGSIZE;
  138. }
  139. /**
  140. * batadv_v_neigh_dump_hardif() - Dump the neighbours of a hard interface into
  141. * a message
  142. * @msg: Netlink message to dump into
  143. * @portid: Port making netlink request
  144. * @seq: Sequence number of netlink message
  145. * @bat_priv: The bat priv with all the mesh interface information
  146. * @hard_iface: The hard interface to be dumped
  147. * @idx_s: Entries to be skipped
  148. *
  149. * This function assumes the caller holds rcu_read_lock().
  150. *
  151. * Return: Error code, or 0 on success
  152. */
  153. static int
  154. batadv_v_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
  155. struct batadv_priv *bat_priv,
  156. struct batadv_hard_iface *hard_iface,
  157. int *idx_s)
  158. {
  159. struct batadv_hardif_neigh_node *hardif_neigh;
  160. int idx = 0;
  161. hlist_for_each_entry_rcu(hardif_neigh,
  162. &hard_iface->neigh_list, list) {
  163. if (idx++ < *idx_s)
  164. continue;
  165. if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
  166. *idx_s = idx - 1;
  167. return -EMSGSIZE;
  168. }
  169. }
  170. *idx_s = 0;
  171. return 0;
  172. }
  173. /**
  174. * batadv_v_neigh_dump() - Dump the neighbours of a hard interface into a
  175. * message
  176. * @msg: Netlink message to dump into
  177. * @cb: Control block containing additional options
  178. * @bat_priv: The bat priv with all the mesh interface information
  179. * @single_hardif: Limit dumping to this hard interface
  180. */
  181. static void
  182. batadv_v_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
  183. struct batadv_priv *bat_priv,
  184. struct batadv_hard_iface *single_hardif)
  185. {
  186. struct batadv_hard_iface *hard_iface;
  187. struct list_head *iter;
  188. int i_hardif = 0;
  189. int i_hardif_s = cb->args[0];
  190. int idx = cb->args[1];
  191. int portid = NETLINK_CB(cb->skb).portid;
  192. rcu_read_lock();
  193. if (single_hardif) {
  194. if (i_hardif_s == 0) {
  195. if (batadv_v_neigh_dump_hardif(msg, portid,
  196. cb->nlh->nlmsg_seq,
  197. bat_priv, single_hardif,
  198. &idx) == 0)
  199. i_hardif++;
  200. }
  201. } else {
  202. netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
  203. if (i_hardif++ < i_hardif_s)
  204. continue;
  205. if (batadv_v_neigh_dump_hardif(msg, portid,
  206. cb->nlh->nlmsg_seq,
  207. bat_priv, hard_iface,
  208. &idx)) {
  209. i_hardif--;
  210. break;
  211. }
  212. }
  213. }
  214. rcu_read_unlock();
  215. cb->args[0] = i_hardif;
  216. cb->args[1] = idx;
  217. }
  218. /**
  219. * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
  220. * @msg: Netlink message to dump into
  221. * @portid: Port making netlink request
  222. * @seq: Sequence number of netlink message
  223. * @bat_priv: The bat priv with all the mesh interface information
  224. * @if_outgoing: Limit dump to entries with this outgoing interface
  225. * @orig_node: Originator to dump
  226. * @neigh_node: Single hops neighbour
  227. * @best: Is the best originator
  228. *
  229. * Return: Error code, or 0 on success
  230. */
  231. static int
  232. batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  233. struct batadv_priv *bat_priv,
  234. struct batadv_hard_iface *if_outgoing,
  235. struct batadv_orig_node *orig_node,
  236. struct batadv_neigh_node *neigh_node,
  237. bool best)
  238. {
  239. struct batadv_neigh_ifinfo *n_ifinfo;
  240. unsigned int last_seen_msecs;
  241. u32 throughput;
  242. void *hdr;
  243. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  244. if (!n_ifinfo)
  245. return 0;
  246. throughput = n_ifinfo->bat_v.throughput * 100;
  247. batadv_neigh_ifinfo_put(n_ifinfo);
  248. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  249. if (if_outgoing != BATADV_IF_DEFAULT &&
  250. if_outgoing != neigh_node->if_incoming)
  251. return 0;
  252. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  253. BATADV_CMD_GET_ORIGINATORS);
  254. if (!hdr)
  255. return -ENOBUFS;
  256. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
  257. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  258. neigh_node->addr) ||
  259. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  260. neigh_node->if_incoming->net_dev->name) ||
  261. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  262. neigh_node->if_incoming->net_dev->ifindex) ||
  263. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
  264. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  265. last_seen_msecs))
  266. goto nla_put_failure;
  267. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  268. goto nla_put_failure;
  269. genlmsg_end(msg, hdr);
  270. return 0;
  271. nla_put_failure:
  272. genlmsg_cancel(msg, hdr);
  273. return -EMSGSIZE;
  274. }
  275. /**
  276. * batadv_v_orig_dump_entry() - Dump an originator entry into a message
  277. * @msg: Netlink message to dump into
  278. * @portid: Port making netlink request
  279. * @seq: Sequence number of netlink message
  280. * @bat_priv: The bat priv with all the mesh interface information
  281. * @if_outgoing: Limit dump to entries with this outgoing interface
  282. * @orig_node: Originator to dump
  283. * @sub_s: Number of sub entries to skip
  284. *
  285. * This function assumes the caller holds rcu_read_lock().
  286. *
  287. * Return: Error code, or 0 on success
  288. */
  289. static int
  290. batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  291. struct batadv_priv *bat_priv,
  292. struct batadv_hard_iface *if_outgoing,
  293. struct batadv_orig_node *orig_node, int *sub_s)
  294. {
  295. struct batadv_neigh_node *neigh_node_best;
  296. struct batadv_neigh_node *neigh_node;
  297. int sub = 0;
  298. bool best;
  299. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  300. if (!neigh_node_best)
  301. goto out;
  302. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  303. if (sub++ < *sub_s)
  304. continue;
  305. best = (neigh_node == neigh_node_best);
  306. if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
  307. if_outgoing, orig_node,
  308. neigh_node, best)) {
  309. batadv_neigh_node_put(neigh_node_best);
  310. *sub_s = sub - 1;
  311. return -EMSGSIZE;
  312. }
  313. }
  314. out:
  315. batadv_neigh_node_put(neigh_node_best);
  316. *sub_s = 0;
  317. return 0;
  318. }
  319. /**
  320. * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
  321. * @msg: Netlink message to dump into
  322. * @portid: Port making netlink request
  323. * @seq: Sequence number of netlink message
  324. * @bat_priv: The bat priv with all the mesh interface information
  325. * @if_outgoing: Limit dump to entries with this outgoing interface
  326. * @head: Bucket to be dumped
  327. * @idx_s: Number of entries to be skipped
  328. * @sub: Number of sub entries to be skipped
  329. *
  330. * Return: Error code, or 0 on success
  331. */
  332. static int
  333. batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  334. struct batadv_priv *bat_priv,
  335. struct batadv_hard_iface *if_outgoing,
  336. struct hlist_head *head, int *idx_s, int *sub)
  337. {
  338. struct batadv_orig_node *orig_node;
  339. int idx = 0;
  340. rcu_read_lock();
  341. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  342. if (idx++ < *idx_s)
  343. continue;
  344. if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
  345. if_outgoing, orig_node, sub)) {
  346. rcu_read_unlock();
  347. *idx_s = idx - 1;
  348. return -EMSGSIZE;
  349. }
  350. }
  351. rcu_read_unlock();
  352. *idx_s = 0;
  353. *sub = 0;
  354. return 0;
  355. }
  356. /**
  357. * batadv_v_orig_dump() - Dump the originators into a message
  358. * @msg: Netlink message to dump into
  359. * @cb: Control block containing additional options
  360. * @bat_priv: The bat priv with all the mesh interface information
  361. * @if_outgoing: Limit dump to entries with this outgoing interface
  362. */
  363. static void
  364. batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  365. struct batadv_priv *bat_priv,
  366. struct batadv_hard_iface *if_outgoing)
  367. {
  368. struct batadv_hashtable *hash = bat_priv->orig_hash;
  369. struct hlist_head *head;
  370. int bucket = cb->args[0];
  371. int idx = cb->args[1];
  372. int sub = cb->args[2];
  373. int portid = NETLINK_CB(cb->skb).portid;
  374. while (bucket < hash->size) {
  375. head = &hash->table[bucket];
  376. if (batadv_v_orig_dump_bucket(msg, portid,
  377. cb->nlh->nlmsg_seq,
  378. bat_priv, if_outgoing, head, &idx,
  379. &sub))
  380. break;
  381. bucket++;
  382. }
  383. cb->args[0] = bucket;
  384. cb->args[1] = idx;
  385. cb->args[2] = sub;
  386. }
  387. static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
  388. struct batadv_hard_iface *if_outgoing1,
  389. struct batadv_neigh_node *neigh2,
  390. struct batadv_hard_iface *if_outgoing2)
  391. {
  392. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  393. int ret = 0;
  394. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  395. if (!ifinfo1)
  396. goto err_ifinfo1;
  397. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  398. if (!ifinfo2)
  399. goto err_ifinfo2;
  400. ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
  401. batadv_neigh_ifinfo_put(ifinfo2);
  402. err_ifinfo2:
  403. batadv_neigh_ifinfo_put(ifinfo1);
  404. err_ifinfo1:
  405. return ret;
  406. }
  407. static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
  408. struct batadv_hard_iface *if_outgoing1,
  409. struct batadv_neigh_node *neigh2,
  410. struct batadv_hard_iface *if_outgoing2)
  411. {
  412. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  413. u32 threshold;
  414. bool ret = false;
  415. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  416. if (!ifinfo1)
  417. goto err_ifinfo1;
  418. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  419. if (!ifinfo2)
  420. goto err_ifinfo2;
  421. threshold = ifinfo1->bat_v.throughput / 4;
  422. threshold = ifinfo1->bat_v.throughput - threshold;
  423. ret = ifinfo2->bat_v.throughput > threshold;
  424. batadv_neigh_ifinfo_put(ifinfo2);
  425. err_ifinfo2:
  426. batadv_neigh_ifinfo_put(ifinfo1);
  427. err_ifinfo1:
  428. return ret;
  429. }
  430. /**
  431. * batadv_v_init_sel_class() - initialize GW selection class
  432. * @bat_priv: the bat priv with all the mesh interface information
  433. */
  434. static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
  435. {
  436. /* set default throughput difference threshold to 5Mbps */
  437. atomic_set(&bat_priv->gw.sel_class, 50);
  438. }
  439. /**
  440. * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
  441. * @gw_node: the GW to retrieve the metric for
  442. * @bw: the pointer where the metric will be stored. The metric is computed as
  443. * the minimum between the GW advertised throughput and the path throughput to
  444. * it in the mesh
  445. *
  446. * Return: 0 on success, -1 on failure
  447. */
  448. static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
  449. {
  450. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  451. struct batadv_orig_node *orig_node;
  452. struct batadv_neigh_node *router;
  453. int ret = -1;
  454. orig_node = gw_node->orig_node;
  455. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  456. if (!router)
  457. goto out;
  458. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  459. if (!router_ifinfo)
  460. goto out;
  461. /* the GW metric is computed as the minimum between the path throughput
  462. * to reach the GW itself and the advertised bandwidth.
  463. * This gives us an approximation of the effective throughput that the
  464. * client can expect via this particular GW node
  465. */
  466. *bw = router_ifinfo->bat_v.throughput;
  467. *bw = min_t(u32, *bw, gw_node->bandwidth_down);
  468. ret = 0;
  469. out:
  470. batadv_neigh_node_put(router);
  471. batadv_neigh_ifinfo_put(router_ifinfo);
  472. return ret;
  473. }
  474. /**
  475. * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
  476. * @bat_priv: the bat priv with all the mesh interface information
  477. *
  478. * Return: the GW node having the best GW-metric, NULL if no GW is known
  479. */
  480. static struct batadv_gw_node *
  481. batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  482. {
  483. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  484. u32 max_bw = 0, bw;
  485. rcu_read_lock();
  486. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  487. if (!kref_get_unless_zero(&gw_node->refcount))
  488. continue;
  489. if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
  490. goto next;
  491. if (curr_gw && bw <= max_bw)
  492. goto next;
  493. batadv_gw_node_put(curr_gw);
  494. curr_gw = gw_node;
  495. kref_get(&curr_gw->refcount);
  496. max_bw = bw;
  497. next:
  498. batadv_gw_node_put(gw_node);
  499. }
  500. rcu_read_unlock();
  501. return curr_gw;
  502. }
  503. /**
  504. * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
  505. * @bat_priv: the bat priv with all the mesh interface information
  506. * @curr_gw_orig: originator representing the currently selected GW
  507. * @orig_node: the originator representing the new candidate
  508. *
  509. * Return: true if orig_node can be selected as current GW, false otherwise
  510. */
  511. static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
  512. struct batadv_orig_node *curr_gw_orig,
  513. struct batadv_orig_node *orig_node)
  514. {
  515. struct batadv_gw_node *curr_gw, *orig_gw = NULL;
  516. u32 gw_throughput, orig_throughput, threshold;
  517. bool ret = false;
  518. threshold = atomic_read(&bat_priv->gw.sel_class);
  519. curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
  520. if (!curr_gw) {
  521. ret = true;
  522. goto out;
  523. }
  524. if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
  525. ret = true;
  526. goto out;
  527. }
  528. orig_gw = batadv_gw_node_get(bat_priv, orig_node);
  529. if (!orig_gw)
  530. goto out;
  531. if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
  532. goto out;
  533. if (orig_throughput < gw_throughput)
  534. goto out;
  535. if ((orig_throughput - gw_throughput) < threshold)
  536. goto out;
  537. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  538. "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
  539. gw_throughput, orig_throughput);
  540. ret = true;
  541. out:
  542. batadv_gw_node_put(curr_gw);
  543. batadv_gw_node_put(orig_gw);
  544. return ret;
  545. }
  546. /**
  547. * batadv_v_gw_dump_entry() - Dump a gateway into a message
  548. * @msg: Netlink message to dump into
  549. * @portid: Port making netlink request
  550. * @cb: Control block containing additional options
  551. * @bat_priv: The bat priv with all the mesh interface information
  552. * @gw_node: Gateway to be dumped
  553. *
  554. * Return: Error code, or 0 on success
  555. */
  556. static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
  557. struct netlink_callback *cb,
  558. struct batadv_priv *bat_priv,
  559. struct batadv_gw_node *gw_node)
  560. {
  561. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  562. struct batadv_neigh_node *router;
  563. struct batadv_gw_node *curr_gw = NULL;
  564. int ret = 0;
  565. void *hdr;
  566. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  567. if (!router)
  568. goto out;
  569. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  570. if (!router_ifinfo)
  571. goto out;
  572. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  573. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  574. &batadv_netlink_family, NLM_F_MULTI,
  575. BATADV_CMD_GET_GATEWAYS);
  576. if (!hdr) {
  577. ret = -ENOBUFS;
  578. goto out;
  579. }
  580. genl_dump_check_consistent(cb, hdr);
  581. ret = -EMSGSIZE;
  582. if (curr_gw == gw_node) {
  583. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  584. genlmsg_cancel(msg, hdr);
  585. goto out;
  586. }
  587. }
  588. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  589. gw_node->orig_node->orig)) {
  590. genlmsg_cancel(msg, hdr);
  591. goto out;
  592. }
  593. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
  594. router_ifinfo->bat_v.throughput)) {
  595. genlmsg_cancel(msg, hdr);
  596. goto out;
  597. }
  598. if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
  599. genlmsg_cancel(msg, hdr);
  600. goto out;
  601. }
  602. if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  603. router->if_incoming->net_dev->name)) {
  604. genlmsg_cancel(msg, hdr);
  605. goto out;
  606. }
  607. if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  608. router->if_incoming->net_dev->ifindex)) {
  609. genlmsg_cancel(msg, hdr);
  610. goto out;
  611. }
  612. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  613. gw_node->bandwidth_down)) {
  614. genlmsg_cancel(msg, hdr);
  615. goto out;
  616. }
  617. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
  618. genlmsg_cancel(msg, hdr);
  619. goto out;
  620. }
  621. genlmsg_end(msg, hdr);
  622. ret = 0;
  623. out:
  624. batadv_gw_node_put(curr_gw);
  625. batadv_neigh_ifinfo_put(router_ifinfo);
  626. batadv_neigh_node_put(router);
  627. return ret;
  628. }
  629. /**
  630. * batadv_v_gw_dump() - Dump gateways into a message
  631. * @msg: Netlink message to dump into
  632. * @cb: Control block containing additional options
  633. * @bat_priv: The bat priv with all the mesh interface information
  634. */
  635. static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  636. struct batadv_priv *bat_priv)
  637. {
  638. int portid = NETLINK_CB(cb->skb).portid;
  639. struct batadv_gw_node *gw_node;
  640. int idx_skip = cb->args[0];
  641. int idx = 0;
  642. spin_lock_bh(&bat_priv->gw.list_lock);
  643. cb->seq = bat_priv->gw.generation << 1 | 1;
  644. hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
  645. if (idx++ < idx_skip)
  646. continue;
  647. if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
  648. gw_node)) {
  649. idx_skip = idx - 1;
  650. goto unlock;
  651. }
  652. }
  653. idx_skip = idx;
  654. unlock:
  655. spin_unlock_bh(&bat_priv->gw.list_lock);
  656. cb->args[0] = idx_skip;
  657. }
  658. static struct batadv_algo_ops batadv_batman_v __read_mostly = {
  659. .name = "BATMAN_V",
  660. .iface = {
  661. .activate = batadv_v_iface_activate,
  662. .enable = batadv_v_iface_enable,
  663. .disable = batadv_v_iface_disable,
  664. .update_mac = batadv_v_iface_update_mac,
  665. .primary_set = batadv_v_primary_iface_set,
  666. },
  667. .neigh = {
  668. .hardif_init = batadv_v_hardif_neigh_init,
  669. .cmp = batadv_v_neigh_cmp,
  670. .is_similar_or_better = batadv_v_neigh_is_sob,
  671. .dump = batadv_v_neigh_dump,
  672. },
  673. .orig = {
  674. .dump = batadv_v_orig_dump,
  675. },
  676. .gw = {
  677. .init_sel_class = batadv_v_init_sel_class,
  678. .sel_class_max = U32_MAX,
  679. .get_best_gw_node = batadv_v_gw_get_best_gw_node,
  680. .is_eligible = batadv_v_gw_is_eligible,
  681. .dump = batadv_v_gw_dump,
  682. },
  683. };
  684. /**
  685. * batadv_v_hardif_init() - initialize the algorithm specific fields in the
  686. * hard-interface object
  687. * @hard_iface: the hard-interface to initialize
  688. */
  689. void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
  690. {
  691. /* enable link throughput auto-detection by setting the throughput
  692. * override to zero
  693. */
  694. atomic_set(&hard_iface->bat_v.throughput_override, 0);
  695. atomic_set(&hard_iface->bat_v.elp_interval, 500);
  696. hard_iface->bat_v.aggr_len = 0;
  697. skb_queue_head_init(&hard_iface->bat_v.aggr_list);
  698. INIT_DELAYED_WORK(&hard_iface->bat_v.aggr_wq,
  699. batadv_v_ogm_aggr_work);
  700. }
  701. /**
  702. * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
  703. * mesh
  704. * @bat_priv: the object representing the mesh interface to initialise
  705. *
  706. * Return: 0 on success or a negative error code otherwise
  707. */
  708. int batadv_v_mesh_init(struct batadv_priv *bat_priv)
  709. {
  710. int ret = 0;
  711. ret = batadv_v_ogm_init(bat_priv);
  712. if (ret < 0)
  713. return ret;
  714. return 0;
  715. }
  716. /**
  717. * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
  718. * @bat_priv: the object representing the mesh interface to free
  719. */
  720. void batadv_v_mesh_free(struct batadv_priv *bat_priv)
  721. {
  722. batadv_v_ogm_free(bat_priv);
  723. }
  724. /**
  725. * batadv_v_init() - B.A.T.M.A.N. V initialization function
  726. *
  727. * Description: Takes care of initializing all the subcomponents.
  728. * It is invoked upon module load only.
  729. *
  730. * Return: 0 on success or a negative error code otherwise
  731. */
  732. int __init batadv_v_init(void)
  733. {
  734. int ret;
  735. /* B.A.T.M.A.N. V echo location protocol packet */
  736. ret = batadv_recv_handler_register(BATADV_ELP,
  737. batadv_v_elp_packet_recv);
  738. if (ret < 0)
  739. return ret;
  740. ret = batadv_recv_handler_register(BATADV_OGM2,
  741. batadv_v_ogm_packet_recv);
  742. if (ret < 0)
  743. goto elp_unregister;
  744. ret = batadv_algo_register(&batadv_batman_v);
  745. if (ret < 0)
  746. goto ogm_unregister;
  747. return ret;
  748. ogm_unregister:
  749. batadv_recv_handler_unregister(BATADV_OGM2);
  750. elp_unregister:
  751. batadv_recv_handler_unregister(BATADV_ELP);
  752. return ret;
  753. }