bat_v.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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->soft_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->soft_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 soft 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 soft 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. int i_hardif = 0;
  188. int i_hardif_s = cb->args[0];
  189. int idx = cb->args[1];
  190. int portid = NETLINK_CB(cb->skb).portid;
  191. rcu_read_lock();
  192. if (single_hardif) {
  193. if (i_hardif_s == 0) {
  194. if (batadv_v_neigh_dump_hardif(msg, portid,
  195. cb->nlh->nlmsg_seq,
  196. bat_priv, single_hardif,
  197. &idx) == 0)
  198. i_hardif++;
  199. }
  200. } else {
  201. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  202. if (hard_iface->soft_iface != bat_priv->soft_iface)
  203. continue;
  204. if (i_hardif++ < i_hardif_s)
  205. continue;
  206. if (batadv_v_neigh_dump_hardif(msg, portid,
  207. cb->nlh->nlmsg_seq,
  208. bat_priv, hard_iface,
  209. &idx)) {
  210. i_hardif--;
  211. break;
  212. }
  213. }
  214. }
  215. rcu_read_unlock();
  216. cb->args[0] = i_hardif;
  217. cb->args[1] = idx;
  218. }
  219. /**
  220. * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
  221. * @msg: Netlink message to dump into
  222. * @portid: Port making netlink request
  223. * @seq: Sequence number of netlink message
  224. * @bat_priv: The bat priv with all the soft interface information
  225. * @if_outgoing: Limit dump to entries with this outgoing interface
  226. * @orig_node: Originator to dump
  227. * @neigh_node: Single hops neighbour
  228. * @best: Is the best originator
  229. *
  230. * Return: Error code, or 0 on success
  231. */
  232. static int
  233. batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  234. struct batadv_priv *bat_priv,
  235. struct batadv_hard_iface *if_outgoing,
  236. struct batadv_orig_node *orig_node,
  237. struct batadv_neigh_node *neigh_node,
  238. bool best)
  239. {
  240. struct batadv_neigh_ifinfo *n_ifinfo;
  241. unsigned int last_seen_msecs;
  242. u32 throughput;
  243. void *hdr;
  244. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  245. if (!n_ifinfo)
  246. return 0;
  247. throughput = n_ifinfo->bat_v.throughput * 100;
  248. batadv_neigh_ifinfo_put(n_ifinfo);
  249. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  250. if (if_outgoing != BATADV_IF_DEFAULT &&
  251. if_outgoing != neigh_node->if_incoming)
  252. return 0;
  253. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  254. BATADV_CMD_GET_ORIGINATORS);
  255. if (!hdr)
  256. return -ENOBUFS;
  257. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
  258. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  259. neigh_node->addr) ||
  260. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  261. neigh_node->if_incoming->net_dev->name) ||
  262. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  263. neigh_node->if_incoming->net_dev->ifindex) ||
  264. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
  265. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  266. last_seen_msecs))
  267. goto nla_put_failure;
  268. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  269. goto nla_put_failure;
  270. genlmsg_end(msg, hdr);
  271. return 0;
  272. nla_put_failure:
  273. genlmsg_cancel(msg, hdr);
  274. return -EMSGSIZE;
  275. }
  276. /**
  277. * batadv_v_orig_dump_entry() - Dump an originator entry into a message
  278. * @msg: Netlink message to dump into
  279. * @portid: Port making netlink request
  280. * @seq: Sequence number of netlink message
  281. * @bat_priv: The bat priv with all the soft interface information
  282. * @if_outgoing: Limit dump to entries with this outgoing interface
  283. * @orig_node: Originator to dump
  284. * @sub_s: Number of sub entries to skip
  285. *
  286. * This function assumes the caller holds rcu_read_lock().
  287. *
  288. * Return: Error code, or 0 on success
  289. */
  290. static int
  291. batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  292. struct batadv_priv *bat_priv,
  293. struct batadv_hard_iface *if_outgoing,
  294. struct batadv_orig_node *orig_node, int *sub_s)
  295. {
  296. struct batadv_neigh_node *neigh_node_best;
  297. struct batadv_neigh_node *neigh_node;
  298. int sub = 0;
  299. bool best;
  300. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  301. if (!neigh_node_best)
  302. goto out;
  303. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  304. if (sub++ < *sub_s)
  305. continue;
  306. best = (neigh_node == neigh_node_best);
  307. if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
  308. if_outgoing, orig_node,
  309. neigh_node, best)) {
  310. batadv_neigh_node_put(neigh_node_best);
  311. *sub_s = sub - 1;
  312. return -EMSGSIZE;
  313. }
  314. }
  315. out:
  316. batadv_neigh_node_put(neigh_node_best);
  317. *sub_s = 0;
  318. return 0;
  319. }
  320. /**
  321. * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
  322. * @msg: Netlink message to dump into
  323. * @portid: Port making netlink request
  324. * @seq: Sequence number of netlink message
  325. * @bat_priv: The bat priv with all the soft interface information
  326. * @if_outgoing: Limit dump to entries with this outgoing interface
  327. * @head: Bucket to be dumped
  328. * @idx_s: Number of entries to be skipped
  329. * @sub: Number of sub entries to be skipped
  330. *
  331. * Return: Error code, or 0 on success
  332. */
  333. static int
  334. batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  335. struct batadv_priv *bat_priv,
  336. struct batadv_hard_iface *if_outgoing,
  337. struct hlist_head *head, int *idx_s, int *sub)
  338. {
  339. struct batadv_orig_node *orig_node;
  340. int idx = 0;
  341. rcu_read_lock();
  342. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  343. if (idx++ < *idx_s)
  344. continue;
  345. if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
  346. if_outgoing, orig_node, sub)) {
  347. rcu_read_unlock();
  348. *idx_s = idx - 1;
  349. return -EMSGSIZE;
  350. }
  351. }
  352. rcu_read_unlock();
  353. *idx_s = 0;
  354. *sub = 0;
  355. return 0;
  356. }
  357. /**
  358. * batadv_v_orig_dump() - Dump the originators into a message
  359. * @msg: Netlink message to dump into
  360. * @cb: Control block containing additional options
  361. * @bat_priv: The bat priv with all the soft interface information
  362. * @if_outgoing: Limit dump to entries with this outgoing interface
  363. */
  364. static void
  365. batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  366. struct batadv_priv *bat_priv,
  367. struct batadv_hard_iface *if_outgoing)
  368. {
  369. struct batadv_hashtable *hash = bat_priv->orig_hash;
  370. struct hlist_head *head;
  371. int bucket = cb->args[0];
  372. int idx = cb->args[1];
  373. int sub = cb->args[2];
  374. int portid = NETLINK_CB(cb->skb).portid;
  375. while (bucket < hash->size) {
  376. head = &hash->table[bucket];
  377. if (batadv_v_orig_dump_bucket(msg, portid,
  378. cb->nlh->nlmsg_seq,
  379. bat_priv, if_outgoing, head, &idx,
  380. &sub))
  381. break;
  382. bucket++;
  383. }
  384. cb->args[0] = bucket;
  385. cb->args[1] = idx;
  386. cb->args[2] = sub;
  387. }
  388. static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
  389. struct batadv_hard_iface *if_outgoing1,
  390. struct batadv_neigh_node *neigh2,
  391. struct batadv_hard_iface *if_outgoing2)
  392. {
  393. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  394. int ret = 0;
  395. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  396. if (!ifinfo1)
  397. goto err_ifinfo1;
  398. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  399. if (!ifinfo2)
  400. goto err_ifinfo2;
  401. ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
  402. batadv_neigh_ifinfo_put(ifinfo2);
  403. err_ifinfo2:
  404. batadv_neigh_ifinfo_put(ifinfo1);
  405. err_ifinfo1:
  406. return ret;
  407. }
  408. static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
  409. struct batadv_hard_iface *if_outgoing1,
  410. struct batadv_neigh_node *neigh2,
  411. struct batadv_hard_iface *if_outgoing2)
  412. {
  413. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  414. u32 threshold;
  415. bool ret = false;
  416. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  417. if (!ifinfo1)
  418. goto err_ifinfo1;
  419. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  420. if (!ifinfo2)
  421. goto err_ifinfo2;
  422. threshold = ifinfo1->bat_v.throughput / 4;
  423. threshold = ifinfo1->bat_v.throughput - threshold;
  424. ret = ifinfo2->bat_v.throughput > threshold;
  425. batadv_neigh_ifinfo_put(ifinfo2);
  426. err_ifinfo2:
  427. batadv_neigh_ifinfo_put(ifinfo1);
  428. err_ifinfo1:
  429. return ret;
  430. }
  431. /**
  432. * batadv_v_init_sel_class() - initialize GW selection class
  433. * @bat_priv: the bat priv with all the soft interface information
  434. */
  435. static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
  436. {
  437. /* set default throughput difference threshold to 5Mbps */
  438. atomic_set(&bat_priv->gw.sel_class, 50);
  439. }
  440. /**
  441. * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
  442. * @gw_node: the GW to retrieve the metric for
  443. * @bw: the pointer where the metric will be stored. The metric is computed as
  444. * the minimum between the GW advertised throughput and the path throughput to
  445. * it in the mesh
  446. *
  447. * Return: 0 on success, -1 on failure
  448. */
  449. static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
  450. {
  451. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  452. struct batadv_orig_node *orig_node;
  453. struct batadv_neigh_node *router;
  454. int ret = -1;
  455. orig_node = gw_node->orig_node;
  456. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  457. if (!router)
  458. goto out;
  459. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  460. if (!router_ifinfo)
  461. goto out;
  462. /* the GW metric is computed as the minimum between the path throughput
  463. * to reach the GW itself and the advertised bandwidth.
  464. * This gives us an approximation of the effective throughput that the
  465. * client can expect via this particular GW node
  466. */
  467. *bw = router_ifinfo->bat_v.throughput;
  468. *bw = min_t(u32, *bw, gw_node->bandwidth_down);
  469. ret = 0;
  470. out:
  471. batadv_neigh_node_put(router);
  472. batadv_neigh_ifinfo_put(router_ifinfo);
  473. return ret;
  474. }
  475. /**
  476. * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
  477. * @bat_priv: the bat priv with all the soft interface information
  478. *
  479. * Return: the GW node having the best GW-metric, NULL if no GW is known
  480. */
  481. static struct batadv_gw_node *
  482. batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  483. {
  484. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  485. u32 max_bw = 0, bw;
  486. rcu_read_lock();
  487. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  488. if (!kref_get_unless_zero(&gw_node->refcount))
  489. continue;
  490. if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
  491. goto next;
  492. if (curr_gw && bw <= max_bw)
  493. goto next;
  494. batadv_gw_node_put(curr_gw);
  495. curr_gw = gw_node;
  496. kref_get(&curr_gw->refcount);
  497. max_bw = bw;
  498. next:
  499. batadv_gw_node_put(gw_node);
  500. }
  501. rcu_read_unlock();
  502. return curr_gw;
  503. }
  504. /**
  505. * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
  506. * @bat_priv: the bat priv with all the soft interface information
  507. * @curr_gw_orig: originator representing the currently selected GW
  508. * @orig_node: the originator representing the new candidate
  509. *
  510. * Return: true if orig_node can be selected as current GW, false otherwise
  511. */
  512. static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
  513. struct batadv_orig_node *curr_gw_orig,
  514. struct batadv_orig_node *orig_node)
  515. {
  516. struct batadv_gw_node *curr_gw, *orig_gw = NULL;
  517. u32 gw_throughput, orig_throughput, threshold;
  518. bool ret = false;
  519. threshold = atomic_read(&bat_priv->gw.sel_class);
  520. curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
  521. if (!curr_gw) {
  522. ret = true;
  523. goto out;
  524. }
  525. if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
  526. ret = true;
  527. goto out;
  528. }
  529. orig_gw = batadv_gw_node_get(bat_priv, orig_node);
  530. if (!orig_gw)
  531. goto out;
  532. if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
  533. goto out;
  534. if (orig_throughput < gw_throughput)
  535. goto out;
  536. if ((orig_throughput - gw_throughput) < threshold)
  537. goto out;
  538. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  539. "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
  540. gw_throughput, orig_throughput);
  541. ret = true;
  542. out:
  543. batadv_gw_node_put(curr_gw);
  544. batadv_gw_node_put(orig_gw);
  545. return ret;
  546. }
  547. /**
  548. * batadv_v_gw_dump_entry() - Dump a gateway into a message
  549. * @msg: Netlink message to dump into
  550. * @portid: Port making netlink request
  551. * @cb: Control block containing additional options
  552. * @bat_priv: The bat priv with all the soft interface information
  553. * @gw_node: Gateway to be dumped
  554. *
  555. * Return: Error code, or 0 on success
  556. */
  557. static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
  558. struct netlink_callback *cb,
  559. struct batadv_priv *bat_priv,
  560. struct batadv_gw_node *gw_node)
  561. {
  562. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  563. struct batadv_neigh_node *router;
  564. struct batadv_gw_node *curr_gw = NULL;
  565. int ret = 0;
  566. void *hdr;
  567. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  568. if (!router)
  569. goto out;
  570. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  571. if (!router_ifinfo)
  572. goto out;
  573. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  574. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  575. &batadv_netlink_family, NLM_F_MULTI,
  576. BATADV_CMD_GET_GATEWAYS);
  577. if (!hdr) {
  578. ret = -ENOBUFS;
  579. goto out;
  580. }
  581. genl_dump_check_consistent(cb, hdr);
  582. ret = -EMSGSIZE;
  583. if (curr_gw == gw_node) {
  584. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  585. genlmsg_cancel(msg, hdr);
  586. goto out;
  587. }
  588. }
  589. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  590. gw_node->orig_node->orig)) {
  591. genlmsg_cancel(msg, hdr);
  592. goto out;
  593. }
  594. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
  595. router_ifinfo->bat_v.throughput)) {
  596. genlmsg_cancel(msg, hdr);
  597. goto out;
  598. }
  599. if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
  600. genlmsg_cancel(msg, hdr);
  601. goto out;
  602. }
  603. if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  604. router->if_incoming->net_dev->name)) {
  605. genlmsg_cancel(msg, hdr);
  606. goto out;
  607. }
  608. if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  609. router->if_incoming->net_dev->ifindex)) {
  610. genlmsg_cancel(msg, hdr);
  611. goto out;
  612. }
  613. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  614. gw_node->bandwidth_down)) {
  615. genlmsg_cancel(msg, hdr);
  616. goto out;
  617. }
  618. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
  619. genlmsg_cancel(msg, hdr);
  620. goto out;
  621. }
  622. genlmsg_end(msg, hdr);
  623. ret = 0;
  624. out:
  625. batadv_gw_node_put(curr_gw);
  626. batadv_neigh_ifinfo_put(router_ifinfo);
  627. batadv_neigh_node_put(router);
  628. return ret;
  629. }
  630. /**
  631. * batadv_v_gw_dump() - Dump gateways into a message
  632. * @msg: Netlink message to dump into
  633. * @cb: Control block containing additional options
  634. * @bat_priv: The bat priv with all the soft interface information
  635. */
  636. static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  637. struct batadv_priv *bat_priv)
  638. {
  639. int portid = NETLINK_CB(cb->skb).portid;
  640. struct batadv_gw_node *gw_node;
  641. int idx_skip = cb->args[0];
  642. int idx = 0;
  643. spin_lock_bh(&bat_priv->gw.list_lock);
  644. cb->seq = bat_priv->gw.generation << 1 | 1;
  645. hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
  646. if (idx++ < idx_skip)
  647. continue;
  648. if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
  649. gw_node)) {
  650. idx_skip = idx - 1;
  651. goto unlock;
  652. }
  653. }
  654. idx_skip = idx;
  655. unlock:
  656. spin_unlock_bh(&bat_priv->gw.list_lock);
  657. cb->args[0] = idx_skip;
  658. }
  659. static struct batadv_algo_ops batadv_batman_v __read_mostly = {
  660. .name = "BATMAN_V",
  661. .iface = {
  662. .activate = batadv_v_iface_activate,
  663. .enable = batadv_v_iface_enable,
  664. .disable = batadv_v_iface_disable,
  665. .update_mac = batadv_v_iface_update_mac,
  666. .primary_set = batadv_v_primary_iface_set,
  667. },
  668. .neigh = {
  669. .hardif_init = batadv_v_hardif_neigh_init,
  670. .cmp = batadv_v_neigh_cmp,
  671. .is_similar_or_better = batadv_v_neigh_is_sob,
  672. .dump = batadv_v_neigh_dump,
  673. },
  674. .orig = {
  675. .dump = batadv_v_orig_dump,
  676. },
  677. .gw = {
  678. .init_sel_class = batadv_v_init_sel_class,
  679. .sel_class_max = U32_MAX,
  680. .get_best_gw_node = batadv_v_gw_get_best_gw_node,
  681. .is_eligible = batadv_v_gw_is_eligible,
  682. .dump = batadv_v_gw_dump,
  683. },
  684. };
  685. /**
  686. * batadv_v_hardif_init() - initialize the algorithm specific fields in the
  687. * hard-interface object
  688. * @hard_iface: the hard-interface to initialize
  689. */
  690. void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
  691. {
  692. /* enable link throughput auto-detection by setting the throughput
  693. * override to zero
  694. */
  695. atomic_set(&hard_iface->bat_v.throughput_override, 0);
  696. atomic_set(&hard_iface->bat_v.elp_interval, 500);
  697. hard_iface->bat_v.aggr_len = 0;
  698. skb_queue_head_init(&hard_iface->bat_v.aggr_list);
  699. INIT_DELAYED_WORK(&hard_iface->bat_v.aggr_wq,
  700. batadv_v_ogm_aggr_work);
  701. }
  702. /**
  703. * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
  704. * mesh
  705. * @bat_priv: the object representing the mesh interface to initialise
  706. *
  707. * Return: 0 on success or a negative error code otherwise
  708. */
  709. int batadv_v_mesh_init(struct batadv_priv *bat_priv)
  710. {
  711. int ret = 0;
  712. ret = batadv_v_ogm_init(bat_priv);
  713. if (ret < 0)
  714. return ret;
  715. return 0;
  716. }
  717. /**
  718. * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
  719. * @bat_priv: the object representing the mesh interface to free
  720. */
  721. void batadv_v_mesh_free(struct batadv_priv *bat_priv)
  722. {
  723. batadv_v_ogm_free(bat_priv);
  724. }
  725. /**
  726. * batadv_v_init() - B.A.T.M.A.N. V initialization function
  727. *
  728. * Description: Takes care of initializing all the subcomponents.
  729. * It is invoked upon module load only.
  730. *
  731. * Return: 0 on success or a negative error code otherwise
  732. */
  733. int __init batadv_v_init(void)
  734. {
  735. int ret;
  736. /* B.A.T.M.A.N. V echo location protocol packet */
  737. ret = batadv_recv_handler_register(BATADV_ELP,
  738. batadv_v_elp_packet_recv);
  739. if (ret < 0)
  740. return ret;
  741. ret = batadv_recv_handler_register(BATADV_OGM2,
  742. batadv_v_ogm_packet_recv);
  743. if (ret < 0)
  744. goto elp_unregister;
  745. ret = batadv_algo_register(&batadv_batman_v);
  746. if (ret < 0)
  747. goto ogm_unregister;
  748. return ret;
  749. ogm_unregister:
  750. batadv_recv_handler_unregister(BATADV_OGM2);
  751. elp_unregister:
  752. batadv_recv_handler_unregister(BATADV_ELP);
  753. return ret;
  754. }