bat_v.c 30 KB

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