bat_v.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2013-2020 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/kernel.h>
  15. #include <linux/kref.h>
  16. #include <linux/list.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/netlink.h>
  19. #include <linux/rculist.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/seq_file.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 "gateway_common.h"
  36. #include "hard-interface.h"
  37. #include "hash.h"
  38. #include "log.h"
  39. #include "netlink.h"
  40. #include "originator.h"
  41. static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
  42. {
  43. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  44. struct batadv_hard_iface *primary_if;
  45. primary_if = batadv_primary_if_get_selected(bat_priv);
  46. if (primary_if) {
  47. batadv_v_elp_iface_activate(primary_if, hard_iface);
  48. batadv_hardif_put(primary_if);
  49. }
  50. /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
  51. * set the interface as ACTIVE right away, without any risk of race
  52. * condition
  53. */
  54. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  55. hard_iface->if_status = BATADV_IF_ACTIVE;
  56. }
  57. static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
  58. {
  59. int ret;
  60. ret = batadv_v_elp_iface_enable(hard_iface);
  61. if (ret < 0)
  62. return ret;
  63. ret = batadv_v_ogm_iface_enable(hard_iface);
  64. if (ret < 0)
  65. batadv_v_elp_iface_disable(hard_iface);
  66. return ret;
  67. }
  68. static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
  69. {
  70. batadv_v_ogm_iface_disable(hard_iface);
  71. batadv_v_elp_iface_disable(hard_iface);
  72. }
  73. static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
  74. {
  75. batadv_v_elp_primary_iface_set(hard_iface);
  76. batadv_v_ogm_primary_iface_set(hard_iface);
  77. }
  78. /**
  79. * batadv_v_iface_update_mac() - react to hard-interface MAC address change
  80. * @hard_iface: the modified interface
  81. *
  82. * If the modified interface is the primary one, update the originator
  83. * address in the ELP and OGM messages to reflect the new MAC address.
  84. */
  85. static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
  86. {
  87. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  88. struct batadv_hard_iface *primary_if;
  89. primary_if = batadv_primary_if_get_selected(bat_priv);
  90. if (primary_if != hard_iface)
  91. goto out;
  92. batadv_v_primary_iface_set(hard_iface);
  93. out:
  94. if (primary_if)
  95. batadv_hardif_put(primary_if);
  96. }
  97. static void
  98. batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
  99. {
  100. ewma_throughput_init(&hardif_neigh->bat_v.throughput);
  101. INIT_WORK(&hardif_neigh->bat_v.metric_work,
  102. batadv_v_elp_throughput_metric_update);
  103. }
  104. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  105. /**
  106. * batadv_v_orig_print_neigh() - print neighbors for the originator table
  107. * @orig_node: the orig_node for which the neighbors are printed
  108. * @if_outgoing: outgoing interface for these entries
  109. * @seq: debugfs table seq_file struct
  110. *
  111. * Must be called while holding an rcu lock.
  112. */
  113. static void
  114. batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
  115. struct batadv_hard_iface *if_outgoing,
  116. struct seq_file *seq)
  117. {
  118. struct batadv_neigh_node *neigh_node;
  119. struct batadv_neigh_ifinfo *n_ifinfo;
  120. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  121. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  122. if (!n_ifinfo)
  123. continue;
  124. seq_printf(seq, " %pM (%9u.%1u)",
  125. neigh_node->addr,
  126. n_ifinfo->bat_v.throughput / 10,
  127. n_ifinfo->bat_v.throughput % 10);
  128. batadv_neigh_ifinfo_put(n_ifinfo);
  129. }
  130. }
  131. /**
  132. * batadv_v_hardif_neigh_print() - print a single ELP neighbour node
  133. * @seq: neighbour table seq_file struct
  134. * @hardif_neigh: hardif neighbour information
  135. */
  136. static void
  137. batadv_v_hardif_neigh_print(struct seq_file *seq,
  138. struct batadv_hardif_neigh_node *hardif_neigh)
  139. {
  140. int last_secs, last_msecs;
  141. u32 throughput;
  142. last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
  143. last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
  144. throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
  145. seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
  146. hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
  147. throughput % 10, hardif_neigh->if_incoming->net_dev->name);
  148. }
  149. /**
  150. * batadv_v_neigh_print() - print the single hop neighbour list
  151. * @bat_priv: the bat priv with all the soft interface information
  152. * @seq: neighbour table seq_file struct
  153. */
  154. static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
  155. struct seq_file *seq)
  156. {
  157. struct net_device *net_dev = (struct net_device *)seq->private;
  158. struct batadv_hardif_neigh_node *hardif_neigh;
  159. struct batadv_hard_iface *hard_iface;
  160. int batman_count = 0;
  161. seq_puts(seq,
  162. " Neighbor last-seen ( throughput) [ IF]\n");
  163. rcu_read_lock();
  164. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  165. if (hard_iface->soft_iface != net_dev)
  166. continue;
  167. hlist_for_each_entry_rcu(hardif_neigh,
  168. &hard_iface->neigh_list, list) {
  169. batadv_v_hardif_neigh_print(seq, hardif_neigh);
  170. batman_count++;
  171. }
  172. }
  173. rcu_read_unlock();
  174. if (batman_count == 0)
  175. seq_puts(seq, "No batman nodes in range ...\n");
  176. }
  177. #endif
  178. /**
  179. * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
  180. * @msg: Netlink message to dump into
  181. * @portid: Port making netlink request
  182. * @seq: Sequence number of netlink message
  183. * @hardif_neigh: Neighbour to dump
  184. *
  185. * Return: Error code, or 0 on success
  186. */
  187. static int
  188. batadv_v_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
  189. struct batadv_hardif_neigh_node *hardif_neigh)
  190. {
  191. void *hdr;
  192. unsigned int last_seen_msecs;
  193. u32 throughput;
  194. last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
  195. throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
  196. throughput = throughput * 100;
  197. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  198. BATADV_CMD_GET_NEIGHBORS);
  199. if (!hdr)
  200. return -ENOBUFS;
  201. if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  202. hardif_neigh->addr) ||
  203. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  204. hardif_neigh->if_incoming->net_dev->ifindex) ||
  205. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  206. last_seen_msecs) ||
  207. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput))
  208. goto nla_put_failure;
  209. genlmsg_end(msg, hdr);
  210. return 0;
  211. nla_put_failure:
  212. genlmsg_cancel(msg, hdr);
  213. return -EMSGSIZE;
  214. }
  215. /**
  216. * batadv_v_neigh_dump_hardif() - Dump the neighbours of a hard interface into
  217. * a message
  218. * @msg: Netlink message to dump into
  219. * @portid: Port making netlink request
  220. * @seq: Sequence number of netlink message
  221. * @bat_priv: The bat priv with all the soft interface information
  222. * @hard_iface: The hard interface to be dumped
  223. * @idx_s: Entries to be skipped
  224. *
  225. * This function assumes the caller holds rcu_read_lock().
  226. *
  227. * Return: Error code, or 0 on success
  228. */
  229. static int
  230. batadv_v_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
  231. struct batadv_priv *bat_priv,
  232. struct batadv_hard_iface *hard_iface,
  233. int *idx_s)
  234. {
  235. struct batadv_hardif_neigh_node *hardif_neigh;
  236. int idx = 0;
  237. hlist_for_each_entry_rcu(hardif_neigh,
  238. &hard_iface->neigh_list, list) {
  239. if (idx++ < *idx_s)
  240. continue;
  241. if (batadv_v_neigh_dump_neigh(msg, portid, seq, hardif_neigh)) {
  242. *idx_s = idx - 1;
  243. return -EMSGSIZE;
  244. }
  245. }
  246. *idx_s = 0;
  247. return 0;
  248. }
  249. /**
  250. * batadv_v_neigh_dump() - Dump the neighbours of a hard interface into a
  251. * message
  252. * @msg: Netlink message to dump into
  253. * @cb: Control block containing additional options
  254. * @bat_priv: The bat priv with all the soft interface information
  255. * @single_hardif: Limit dumping to this hard interface
  256. */
  257. static void
  258. batadv_v_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
  259. struct batadv_priv *bat_priv,
  260. struct batadv_hard_iface *single_hardif)
  261. {
  262. struct batadv_hard_iface *hard_iface;
  263. int i_hardif = 0;
  264. int i_hardif_s = cb->args[0];
  265. int idx = cb->args[1];
  266. int portid = NETLINK_CB(cb->skb).portid;
  267. rcu_read_lock();
  268. if (single_hardif) {
  269. if (i_hardif_s == 0) {
  270. if (batadv_v_neigh_dump_hardif(msg, portid,
  271. cb->nlh->nlmsg_seq,
  272. bat_priv, single_hardif,
  273. &idx) == 0)
  274. i_hardif++;
  275. }
  276. } else {
  277. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  278. if (hard_iface->soft_iface != bat_priv->soft_iface)
  279. continue;
  280. if (i_hardif++ < i_hardif_s)
  281. continue;
  282. if (batadv_v_neigh_dump_hardif(msg, portid,
  283. cb->nlh->nlmsg_seq,
  284. bat_priv, hard_iface,
  285. &idx)) {
  286. i_hardif--;
  287. break;
  288. }
  289. }
  290. }
  291. rcu_read_unlock();
  292. cb->args[0] = i_hardif;
  293. cb->args[1] = idx;
  294. }
  295. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  296. /**
  297. * batadv_v_orig_print() - print the originator table
  298. * @bat_priv: the bat priv with all the soft interface information
  299. * @seq: debugfs table seq_file struct
  300. * @if_outgoing: the outgoing interface for which this should be printed
  301. */
  302. static void batadv_v_orig_print(struct batadv_priv *bat_priv,
  303. struct seq_file *seq,
  304. struct batadv_hard_iface *if_outgoing)
  305. {
  306. struct batadv_neigh_node *neigh_node;
  307. struct batadv_hashtable *hash = bat_priv->orig_hash;
  308. int last_seen_msecs, last_seen_secs;
  309. struct batadv_orig_node *orig_node;
  310. struct batadv_neigh_ifinfo *n_ifinfo;
  311. unsigned long last_seen_jiffies;
  312. struct hlist_head *head;
  313. int batman_count = 0;
  314. u32 i;
  315. seq_puts(seq,
  316. " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
  317. for (i = 0; i < hash->size; i++) {
  318. head = &hash->table[i];
  319. rcu_read_lock();
  320. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  321. neigh_node = batadv_orig_router_get(orig_node,
  322. if_outgoing);
  323. if (!neigh_node)
  324. continue;
  325. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
  326. if_outgoing);
  327. if (!n_ifinfo)
  328. goto next;
  329. last_seen_jiffies = jiffies - orig_node->last_seen;
  330. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  331. last_seen_secs = last_seen_msecs / 1000;
  332. last_seen_msecs = last_seen_msecs % 1000;
  333. seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
  334. orig_node->orig, last_seen_secs,
  335. last_seen_msecs,
  336. n_ifinfo->bat_v.throughput / 10,
  337. n_ifinfo->bat_v.throughput % 10,
  338. neigh_node->addr,
  339. neigh_node->if_incoming->net_dev->name);
  340. batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
  341. seq_putc(seq, '\n');
  342. batman_count++;
  343. next:
  344. batadv_neigh_node_put(neigh_node);
  345. if (n_ifinfo)
  346. batadv_neigh_ifinfo_put(n_ifinfo);
  347. }
  348. rcu_read_unlock();
  349. }
  350. if (batman_count == 0)
  351. seq_puts(seq, "No batman nodes in range ...\n");
  352. }
  353. #endif
  354. /**
  355. * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
  356. * @msg: Netlink message to dump into
  357. * @portid: Port making netlink request
  358. * @seq: Sequence number of netlink message
  359. * @bat_priv: The bat priv with all the soft interface information
  360. * @if_outgoing: Limit dump to entries with this outgoing interface
  361. * @orig_node: Originator to dump
  362. * @neigh_node: Single hops neighbour
  363. * @best: Is the best originator
  364. *
  365. * Return: Error code, or 0 on success
  366. */
  367. static int
  368. batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  369. struct batadv_priv *bat_priv,
  370. struct batadv_hard_iface *if_outgoing,
  371. struct batadv_orig_node *orig_node,
  372. struct batadv_neigh_node *neigh_node,
  373. bool best)
  374. {
  375. struct batadv_neigh_ifinfo *n_ifinfo;
  376. unsigned int last_seen_msecs;
  377. u32 throughput;
  378. void *hdr;
  379. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  380. if (!n_ifinfo)
  381. return 0;
  382. throughput = n_ifinfo->bat_v.throughput * 100;
  383. batadv_neigh_ifinfo_put(n_ifinfo);
  384. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  385. if (if_outgoing != BATADV_IF_DEFAULT &&
  386. if_outgoing != neigh_node->if_incoming)
  387. return 0;
  388. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  389. BATADV_CMD_GET_ORIGINATORS);
  390. if (!hdr)
  391. return -ENOBUFS;
  392. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
  393. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  394. neigh_node->addr) ||
  395. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  396. neigh_node->if_incoming->net_dev->ifindex) ||
  397. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
  398. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  399. last_seen_msecs))
  400. goto nla_put_failure;
  401. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  402. goto nla_put_failure;
  403. genlmsg_end(msg, hdr);
  404. return 0;
  405. nla_put_failure:
  406. genlmsg_cancel(msg, hdr);
  407. return -EMSGSIZE;
  408. }
  409. /**
  410. * batadv_v_orig_dump_entry() - Dump an originator entry into a message
  411. * @msg: Netlink message to dump into
  412. * @portid: Port making netlink request
  413. * @seq: Sequence number of netlink message
  414. * @bat_priv: The bat priv with all the soft interface information
  415. * @if_outgoing: Limit dump to entries with this outgoing interface
  416. * @orig_node: Originator to dump
  417. * @sub_s: Number of sub entries to skip
  418. *
  419. * This function assumes the caller holds rcu_read_lock().
  420. *
  421. * Return: Error code, or 0 on success
  422. */
  423. static int
  424. batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  425. struct batadv_priv *bat_priv,
  426. struct batadv_hard_iface *if_outgoing,
  427. struct batadv_orig_node *orig_node, int *sub_s)
  428. {
  429. struct batadv_neigh_node *neigh_node_best;
  430. struct batadv_neigh_node *neigh_node;
  431. int sub = 0;
  432. bool best;
  433. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  434. if (!neigh_node_best)
  435. goto out;
  436. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  437. if (sub++ < *sub_s)
  438. continue;
  439. best = (neigh_node == neigh_node_best);
  440. if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
  441. if_outgoing, orig_node,
  442. neigh_node, best)) {
  443. batadv_neigh_node_put(neigh_node_best);
  444. *sub_s = sub - 1;
  445. return -EMSGSIZE;
  446. }
  447. }
  448. out:
  449. if (neigh_node_best)
  450. batadv_neigh_node_put(neigh_node_best);
  451. *sub_s = 0;
  452. return 0;
  453. }
  454. /**
  455. * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
  456. * @msg: Netlink message to dump into
  457. * @portid: Port making netlink request
  458. * @seq: Sequence number of netlink message
  459. * @bat_priv: The bat priv with all the soft interface information
  460. * @if_outgoing: Limit dump to entries with this outgoing interface
  461. * @head: Bucket to be dumped
  462. * @idx_s: Number of entries to be skipped
  463. * @sub: Number of sub entries to be skipped
  464. *
  465. * Return: Error code, or 0 on success
  466. */
  467. static int
  468. batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  469. struct batadv_priv *bat_priv,
  470. struct batadv_hard_iface *if_outgoing,
  471. struct hlist_head *head, int *idx_s, int *sub)
  472. {
  473. struct batadv_orig_node *orig_node;
  474. int idx = 0;
  475. rcu_read_lock();
  476. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  477. if (idx++ < *idx_s)
  478. continue;
  479. if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
  480. if_outgoing, orig_node, sub)) {
  481. rcu_read_unlock();
  482. *idx_s = idx - 1;
  483. return -EMSGSIZE;
  484. }
  485. }
  486. rcu_read_unlock();
  487. *idx_s = 0;
  488. *sub = 0;
  489. return 0;
  490. }
  491. /**
  492. * batadv_v_orig_dump() - Dump the originators into a message
  493. * @msg: Netlink message to dump into
  494. * @cb: Control block containing additional options
  495. * @bat_priv: The bat priv with all the soft interface information
  496. * @if_outgoing: Limit dump to entries with this outgoing interface
  497. */
  498. static void
  499. batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  500. struct batadv_priv *bat_priv,
  501. struct batadv_hard_iface *if_outgoing)
  502. {
  503. struct batadv_hashtable *hash = bat_priv->orig_hash;
  504. struct hlist_head *head;
  505. int bucket = cb->args[0];
  506. int idx = cb->args[1];
  507. int sub = cb->args[2];
  508. int portid = NETLINK_CB(cb->skb).portid;
  509. while (bucket < hash->size) {
  510. head = &hash->table[bucket];
  511. if (batadv_v_orig_dump_bucket(msg, portid,
  512. cb->nlh->nlmsg_seq,
  513. bat_priv, if_outgoing, head, &idx,
  514. &sub))
  515. break;
  516. bucket++;
  517. }
  518. cb->args[0] = bucket;
  519. cb->args[1] = idx;
  520. cb->args[2] = sub;
  521. }
  522. static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
  523. struct batadv_hard_iface *if_outgoing1,
  524. struct batadv_neigh_node *neigh2,
  525. struct batadv_hard_iface *if_outgoing2)
  526. {
  527. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  528. int ret = 0;
  529. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  530. if (!ifinfo1)
  531. goto err_ifinfo1;
  532. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  533. if (!ifinfo2)
  534. goto err_ifinfo2;
  535. ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
  536. batadv_neigh_ifinfo_put(ifinfo2);
  537. err_ifinfo2:
  538. batadv_neigh_ifinfo_put(ifinfo1);
  539. err_ifinfo1:
  540. return ret;
  541. }
  542. static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
  543. struct batadv_hard_iface *if_outgoing1,
  544. struct batadv_neigh_node *neigh2,
  545. struct batadv_hard_iface *if_outgoing2)
  546. {
  547. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  548. u32 threshold;
  549. bool ret = false;
  550. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  551. if (!ifinfo1)
  552. goto err_ifinfo1;
  553. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  554. if (!ifinfo2)
  555. goto err_ifinfo2;
  556. threshold = ifinfo1->bat_v.throughput / 4;
  557. threshold = ifinfo1->bat_v.throughput - threshold;
  558. ret = ifinfo2->bat_v.throughput > threshold;
  559. batadv_neigh_ifinfo_put(ifinfo2);
  560. err_ifinfo2:
  561. batadv_neigh_ifinfo_put(ifinfo1);
  562. err_ifinfo1:
  563. return ret;
  564. }
  565. /**
  566. * batadv_v_init_sel_class() - initialize GW selection class
  567. * @bat_priv: the bat priv with all the soft interface information
  568. */
  569. static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
  570. {
  571. /* set default throughput difference threshold to 5Mbps */
  572. atomic_set(&bat_priv->gw.sel_class, 50);
  573. }
  574. static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
  575. char *buff, size_t count)
  576. {
  577. u32 old_class, class;
  578. if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
  579. "B.A.T.M.A.N. V GW selection class",
  580. &class))
  581. return -EINVAL;
  582. old_class = atomic_read(&bat_priv->gw.sel_class);
  583. atomic_set(&bat_priv->gw.sel_class, class);
  584. if (old_class != class)
  585. batadv_gw_reselect(bat_priv);
  586. return count;
  587. }
  588. static ssize_t batadv_v_show_sel_class(struct batadv_priv *bat_priv, char *buff)
  589. {
  590. u32 class = atomic_read(&bat_priv->gw.sel_class);
  591. return sprintf(buff, "%u.%u MBit\n", class / 10, class % 10);
  592. }
  593. /**
  594. * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
  595. * @gw_node: the GW to retrieve the metric for
  596. * @bw: the pointer where the metric will be stored. The metric is computed as
  597. * the minimum between the GW advertised throughput and the path throughput to
  598. * it in the mesh
  599. *
  600. * Return: 0 on success, -1 on failure
  601. */
  602. static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
  603. {
  604. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  605. struct batadv_orig_node *orig_node;
  606. struct batadv_neigh_node *router;
  607. int ret = -1;
  608. orig_node = gw_node->orig_node;
  609. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  610. if (!router)
  611. goto out;
  612. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  613. if (!router_ifinfo)
  614. goto out;
  615. /* the GW metric is computed as the minimum between the path throughput
  616. * to reach the GW itself and the advertised bandwidth.
  617. * This gives us an approximation of the effective throughput that the
  618. * client can expect via this particular GW node
  619. */
  620. *bw = router_ifinfo->bat_v.throughput;
  621. *bw = min_t(u32, *bw, gw_node->bandwidth_down);
  622. ret = 0;
  623. out:
  624. if (router)
  625. batadv_neigh_node_put(router);
  626. if (router_ifinfo)
  627. batadv_neigh_ifinfo_put(router_ifinfo);
  628. return ret;
  629. }
  630. /**
  631. * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
  632. * @bat_priv: the bat priv with all the soft interface information
  633. *
  634. * Return: the GW node having the best GW-metric, NULL if no GW is known
  635. */
  636. static struct batadv_gw_node *
  637. batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  638. {
  639. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  640. u32 max_bw = 0, bw;
  641. rcu_read_lock();
  642. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  643. if (!kref_get_unless_zero(&gw_node->refcount))
  644. continue;
  645. if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
  646. goto next;
  647. if (curr_gw && bw <= max_bw)
  648. goto next;
  649. if (curr_gw)
  650. batadv_gw_node_put(curr_gw);
  651. curr_gw = gw_node;
  652. kref_get(&curr_gw->refcount);
  653. max_bw = bw;
  654. next:
  655. batadv_gw_node_put(gw_node);
  656. }
  657. rcu_read_unlock();
  658. return curr_gw;
  659. }
  660. /**
  661. * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
  662. * @bat_priv: the bat priv with all the soft interface information
  663. * @curr_gw_orig: originator representing the currently selected GW
  664. * @orig_node: the originator representing the new candidate
  665. *
  666. * Return: true if orig_node can be selected as current GW, false otherwise
  667. */
  668. static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
  669. struct batadv_orig_node *curr_gw_orig,
  670. struct batadv_orig_node *orig_node)
  671. {
  672. struct batadv_gw_node *curr_gw, *orig_gw = NULL;
  673. u32 gw_throughput, orig_throughput, threshold;
  674. bool ret = false;
  675. threshold = atomic_read(&bat_priv->gw.sel_class);
  676. curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
  677. if (!curr_gw) {
  678. ret = true;
  679. goto out;
  680. }
  681. if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
  682. ret = true;
  683. goto out;
  684. }
  685. orig_gw = batadv_gw_node_get(bat_priv, orig_node);
  686. if (!orig_gw)
  687. goto out;
  688. if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
  689. goto out;
  690. if (orig_throughput < gw_throughput)
  691. goto out;
  692. if ((orig_throughput - gw_throughput) < threshold)
  693. goto out;
  694. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  695. "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
  696. gw_throughput, orig_throughput);
  697. ret = true;
  698. out:
  699. if (curr_gw)
  700. batadv_gw_node_put(curr_gw);
  701. if (orig_gw)
  702. batadv_gw_node_put(orig_gw);
  703. return ret;
  704. }
  705. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  706. /* fails if orig_node has no router */
  707. static int batadv_v_gw_write_buffer_text(struct batadv_priv *bat_priv,
  708. struct seq_file *seq,
  709. const struct batadv_gw_node *gw_node)
  710. {
  711. struct batadv_gw_node *curr_gw;
  712. struct batadv_neigh_node *router;
  713. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  714. int ret = -1;
  715. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  716. if (!router)
  717. goto out;
  718. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  719. if (!router_ifinfo)
  720. goto out;
  721. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  722. seq_printf(seq, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
  723. (curr_gw == gw_node ? "=>" : " "),
  724. gw_node->orig_node->orig,
  725. router_ifinfo->bat_v.throughput / 10,
  726. router_ifinfo->bat_v.throughput % 10, router->addr,
  727. router->if_incoming->net_dev->name,
  728. gw_node->bandwidth_down / 10,
  729. gw_node->bandwidth_down % 10,
  730. gw_node->bandwidth_up / 10,
  731. gw_node->bandwidth_up % 10);
  732. ret = seq_has_overflowed(seq) ? -1 : 0;
  733. if (curr_gw)
  734. batadv_gw_node_put(curr_gw);
  735. out:
  736. if (router_ifinfo)
  737. batadv_neigh_ifinfo_put(router_ifinfo);
  738. if (router)
  739. batadv_neigh_node_put(router);
  740. return ret;
  741. }
  742. /**
  743. * batadv_v_gw_print() - print the gateway list
  744. * @bat_priv: the bat priv with all the soft interface information
  745. * @seq: gateway table seq_file struct
  746. */
  747. static void batadv_v_gw_print(struct batadv_priv *bat_priv,
  748. struct seq_file *seq)
  749. {
  750. struct batadv_gw_node *gw_node;
  751. int gw_count = 0;
  752. seq_puts(seq,
  753. " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
  754. rcu_read_lock();
  755. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  756. /* fails if orig_node has no router */
  757. if (batadv_v_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
  758. continue;
  759. gw_count++;
  760. }
  761. rcu_read_unlock();
  762. if (gw_count == 0)
  763. seq_puts(seq, "No gateways in range ...\n");
  764. }
  765. #endif
  766. /**
  767. * batadv_v_gw_dump_entry() - Dump a gateway into a message
  768. * @msg: Netlink message to dump into
  769. * @portid: Port making netlink request
  770. * @cb: Control block containing additional options
  771. * @bat_priv: The bat priv with all the soft interface information
  772. * @gw_node: Gateway to be dumped
  773. *
  774. * Return: Error code, or 0 on success
  775. */
  776. static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid,
  777. struct netlink_callback *cb,
  778. struct batadv_priv *bat_priv,
  779. struct batadv_gw_node *gw_node)
  780. {
  781. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  782. struct batadv_neigh_node *router;
  783. struct batadv_gw_node *curr_gw = NULL;
  784. int ret = 0;
  785. void *hdr;
  786. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  787. if (!router)
  788. goto out;
  789. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  790. if (!router_ifinfo)
  791. goto out;
  792. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  793. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  794. &batadv_netlink_family, NLM_F_MULTI,
  795. BATADV_CMD_GET_GATEWAYS);
  796. if (!hdr) {
  797. ret = -ENOBUFS;
  798. goto out;
  799. }
  800. genl_dump_check_consistent(cb, hdr);
  801. ret = -EMSGSIZE;
  802. if (curr_gw == gw_node) {
  803. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  804. genlmsg_cancel(msg, hdr);
  805. goto out;
  806. }
  807. }
  808. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  809. gw_node->orig_node->orig)) {
  810. genlmsg_cancel(msg, hdr);
  811. goto out;
  812. }
  813. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
  814. router_ifinfo->bat_v.throughput)) {
  815. genlmsg_cancel(msg, hdr);
  816. goto out;
  817. }
  818. if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
  819. genlmsg_cancel(msg, hdr);
  820. goto out;
  821. }
  822. if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  823. router->if_incoming->net_dev->name)) {
  824. genlmsg_cancel(msg, hdr);
  825. goto out;
  826. }
  827. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  828. gw_node->bandwidth_down)) {
  829. genlmsg_cancel(msg, hdr);
  830. goto out;
  831. }
  832. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
  833. genlmsg_cancel(msg, hdr);
  834. goto out;
  835. }
  836. genlmsg_end(msg, hdr);
  837. ret = 0;
  838. out:
  839. if (curr_gw)
  840. batadv_gw_node_put(curr_gw);
  841. if (router_ifinfo)
  842. batadv_neigh_ifinfo_put(router_ifinfo);
  843. if (router)
  844. batadv_neigh_node_put(router);
  845. return ret;
  846. }
  847. /**
  848. * batadv_v_gw_dump() - Dump gateways into a message
  849. * @msg: Netlink message to dump into
  850. * @cb: Control block containing additional options
  851. * @bat_priv: The bat priv with all the soft interface information
  852. */
  853. static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  854. struct batadv_priv *bat_priv)
  855. {
  856. int portid = NETLINK_CB(cb->skb).portid;
  857. struct batadv_gw_node *gw_node;
  858. int idx_skip = cb->args[0];
  859. int idx = 0;
  860. spin_lock_bh(&bat_priv->gw.list_lock);
  861. cb->seq = bat_priv->gw.generation << 1 | 1;
  862. hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
  863. if (idx++ < idx_skip)
  864. continue;
  865. if (batadv_v_gw_dump_entry(msg, portid, cb, bat_priv,
  866. gw_node)) {
  867. idx_skip = idx - 1;
  868. goto unlock;
  869. }
  870. }
  871. idx_skip = idx;
  872. unlock:
  873. spin_unlock_bh(&bat_priv->gw.list_lock);
  874. cb->args[0] = idx_skip;
  875. }
  876. static struct batadv_algo_ops batadv_batman_v __read_mostly = {
  877. .name = "BATMAN_V",
  878. .iface = {
  879. .activate = batadv_v_iface_activate,
  880. .enable = batadv_v_iface_enable,
  881. .disable = batadv_v_iface_disable,
  882. .update_mac = batadv_v_iface_update_mac,
  883. .primary_set = batadv_v_primary_iface_set,
  884. },
  885. .neigh = {
  886. .hardif_init = batadv_v_hardif_neigh_init,
  887. .cmp = batadv_v_neigh_cmp,
  888. .is_similar_or_better = batadv_v_neigh_is_sob,
  889. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  890. .print = batadv_v_neigh_print,
  891. #endif
  892. .dump = batadv_v_neigh_dump,
  893. },
  894. .orig = {
  895. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  896. .print = batadv_v_orig_print,
  897. #endif
  898. .dump = batadv_v_orig_dump,
  899. },
  900. .gw = {
  901. .init_sel_class = batadv_v_init_sel_class,
  902. .store_sel_class = batadv_v_store_sel_class,
  903. .show_sel_class = batadv_v_show_sel_class,
  904. .get_best_gw_node = batadv_v_gw_get_best_gw_node,
  905. .is_eligible = batadv_v_gw_is_eligible,
  906. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  907. .print = batadv_v_gw_print,
  908. #endif
  909. .dump = batadv_v_gw_dump,
  910. },
  911. };
  912. /**
  913. * batadv_v_hardif_init() - initialize the algorithm specific fields in the
  914. * hard-interface object
  915. * @hard_iface: the hard-interface to initialize
  916. */
  917. void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
  918. {
  919. /* enable link throughput auto-detection by setting the throughput
  920. * override to zero
  921. */
  922. atomic_set(&hard_iface->bat_v.throughput_override, 0);
  923. atomic_set(&hard_iface->bat_v.elp_interval, 500);
  924. hard_iface->bat_v.aggr_len = 0;
  925. skb_queue_head_init(&hard_iface->bat_v.aggr_list);
  926. INIT_DELAYED_WORK(&hard_iface->bat_v.aggr_wq,
  927. batadv_v_ogm_aggr_work);
  928. }
  929. /**
  930. * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
  931. * mesh
  932. * @bat_priv: the object representing the mesh interface to initialise
  933. *
  934. * Return: 0 on success or a negative error code otherwise
  935. */
  936. int batadv_v_mesh_init(struct batadv_priv *bat_priv)
  937. {
  938. int ret = 0;
  939. ret = batadv_v_ogm_init(bat_priv);
  940. if (ret < 0)
  941. return ret;
  942. return 0;
  943. }
  944. /**
  945. * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
  946. * @bat_priv: the object representing the mesh interface to free
  947. */
  948. void batadv_v_mesh_free(struct batadv_priv *bat_priv)
  949. {
  950. batadv_v_ogm_free(bat_priv);
  951. }
  952. /**
  953. * batadv_v_init() - B.A.T.M.A.N. V initialization function
  954. *
  955. * Description: Takes care of initializing all the subcomponents.
  956. * It is invoked upon module load only.
  957. *
  958. * Return: 0 on success or a negative error code otherwise
  959. */
  960. int __init batadv_v_init(void)
  961. {
  962. int ret;
  963. /* B.A.T.M.A.N. V echo location protocol packet */
  964. ret = batadv_recv_handler_register(BATADV_ELP,
  965. batadv_v_elp_packet_recv);
  966. if (ret < 0)
  967. return ret;
  968. ret = batadv_recv_handler_register(BATADV_OGM2,
  969. batadv_v_ogm_packet_recv);
  970. if (ret < 0)
  971. goto elp_unregister;
  972. ret = batadv_algo_register(&batadv_batman_v);
  973. if (ret < 0)
  974. goto ogm_unregister;
  975. return ret;
  976. ogm_unregister:
  977. batadv_recv_handler_unregister(BATADV_OGM2);
  978. elp_unregister:
  979. batadv_recv_handler_unregister(BATADV_ELP);
  980. return ret;
  981. }