bat_v.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Linus Lüssing, Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bat_v.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/bug.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/batman_adv.h>
  39. #include "bat_algo.h"
  40. #include "bat_v_elp.h"
  41. #include "bat_v_ogm.h"
  42. #include "gateway_client.h"
  43. #include "gateway_common.h"
  44. #include "hard-interface.h"
  45. #include "hash.h"
  46. #include "log.h"
  47. #include "netlink.h"
  48. #include "originator.h"
  49. #include "packet.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_puts(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
  365. * message
  366. * @msg: Netlink message to dump into
  367. * @portid: Port making netlink request
  368. * @seq: Sequence number of netlink message
  369. * @bat_priv: The bat priv with all the soft interface information
  370. * @if_outgoing: Limit dump to entries with this outgoing interface
  371. * @orig_node: Originator to dump
  372. * @neigh_node: Single hops neighbour
  373. * @best: Is the best originator
  374. *
  375. * Return: Error code, or 0 on success
  376. */
  377. static int
  378. batadv_v_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  379. struct batadv_priv *bat_priv,
  380. struct batadv_hard_iface *if_outgoing,
  381. struct batadv_orig_node *orig_node,
  382. struct batadv_neigh_node *neigh_node,
  383. bool best)
  384. {
  385. struct batadv_neigh_ifinfo *n_ifinfo;
  386. unsigned int last_seen_msecs;
  387. u32 throughput;
  388. void *hdr;
  389. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  390. if (!n_ifinfo)
  391. return 0;
  392. throughput = n_ifinfo->bat_v.throughput * 100;
  393. batadv_neigh_ifinfo_put(n_ifinfo);
  394. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  395. if (if_outgoing != BATADV_IF_DEFAULT &&
  396. if_outgoing != neigh_node->if_incoming)
  397. return 0;
  398. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
  399. BATADV_CMD_GET_ORIGINATORS);
  400. if (!hdr)
  401. return -ENOBUFS;
  402. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, orig_node->orig) ||
  403. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  404. neigh_node->addr) ||
  405. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  406. neigh_node->if_incoming->net_dev->ifindex) ||
  407. nla_put_u32(msg, BATADV_ATTR_THROUGHPUT, throughput) ||
  408. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  409. last_seen_msecs))
  410. goto nla_put_failure;
  411. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  412. goto nla_put_failure;
  413. genlmsg_end(msg, hdr);
  414. return 0;
  415. nla_put_failure:
  416. genlmsg_cancel(msg, hdr);
  417. return -EMSGSIZE;
  418. }
  419. /**
  420. * batadv_v_orig_dump_entry - Dump an originator entry into a message
  421. * @msg: Netlink message to dump into
  422. * @portid: Port making netlink request
  423. * @seq: Sequence number of netlink message
  424. * @bat_priv: The bat priv with all the soft interface information
  425. * @if_outgoing: Limit dump to entries with this outgoing interface
  426. * @orig_node: Originator to dump
  427. * @sub_s: Number of sub entries to skip
  428. *
  429. * This function assumes the caller holds rcu_read_lock().
  430. *
  431. * Return: Error code, or 0 on success
  432. */
  433. static int
  434. batadv_v_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  435. struct batadv_priv *bat_priv,
  436. struct batadv_hard_iface *if_outgoing,
  437. struct batadv_orig_node *orig_node, int *sub_s)
  438. {
  439. struct batadv_neigh_node *neigh_node_best;
  440. struct batadv_neigh_node *neigh_node;
  441. int sub = 0;
  442. bool best;
  443. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  444. if (!neigh_node_best)
  445. goto out;
  446. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  447. if (sub++ < *sub_s)
  448. continue;
  449. best = (neigh_node == neigh_node_best);
  450. if (batadv_v_orig_dump_subentry(msg, portid, seq, bat_priv,
  451. if_outgoing, orig_node,
  452. neigh_node, best)) {
  453. batadv_neigh_node_put(neigh_node_best);
  454. *sub_s = sub - 1;
  455. return -EMSGSIZE;
  456. }
  457. }
  458. out:
  459. if (neigh_node_best)
  460. batadv_neigh_node_put(neigh_node_best);
  461. *sub_s = 0;
  462. return 0;
  463. }
  464. /**
  465. * batadv_v_orig_dump_bucket - Dump an originator bucket into a
  466. * message
  467. * @msg: Netlink message to dump into
  468. * @portid: Port making netlink request
  469. * @seq: Sequence number of netlink message
  470. * @bat_priv: The bat priv with all the soft interface information
  471. * @if_outgoing: Limit dump to entries with this outgoing interface
  472. * @head: Bucket to be dumped
  473. * @idx_s: Number of entries to be skipped
  474. * @sub: Number of sub entries to be skipped
  475. *
  476. * Return: Error code, or 0 on success
  477. */
  478. static int
  479. batadv_v_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  480. struct batadv_priv *bat_priv,
  481. struct batadv_hard_iface *if_outgoing,
  482. struct hlist_head *head, int *idx_s, int *sub)
  483. {
  484. struct batadv_orig_node *orig_node;
  485. int idx = 0;
  486. rcu_read_lock();
  487. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  488. if (idx++ < *idx_s)
  489. continue;
  490. if (batadv_v_orig_dump_entry(msg, portid, seq, bat_priv,
  491. if_outgoing, orig_node, sub)) {
  492. rcu_read_unlock();
  493. *idx_s = idx - 1;
  494. return -EMSGSIZE;
  495. }
  496. }
  497. rcu_read_unlock();
  498. *idx_s = 0;
  499. *sub = 0;
  500. return 0;
  501. }
  502. /**
  503. * batadv_v_orig_dump - Dump the originators into a message
  504. * @msg: Netlink message to dump into
  505. * @cb: Control block containing additional options
  506. * @bat_priv: The bat priv with all the soft interface information
  507. * @if_outgoing: Limit dump to entries with this outgoing interface
  508. */
  509. static void
  510. batadv_v_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  511. struct batadv_priv *bat_priv,
  512. struct batadv_hard_iface *if_outgoing)
  513. {
  514. struct batadv_hashtable *hash = bat_priv->orig_hash;
  515. struct hlist_head *head;
  516. int bucket = cb->args[0];
  517. int idx = cb->args[1];
  518. int sub = cb->args[2];
  519. int portid = NETLINK_CB(cb->skb).portid;
  520. while (bucket < hash->size) {
  521. head = &hash->table[bucket];
  522. if (batadv_v_orig_dump_bucket(msg, portid,
  523. cb->nlh->nlmsg_seq,
  524. bat_priv, if_outgoing, head, &idx,
  525. &sub))
  526. break;
  527. bucket++;
  528. }
  529. cb->args[0] = bucket;
  530. cb->args[1] = idx;
  531. cb->args[2] = sub;
  532. }
  533. static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
  534. struct batadv_hard_iface *if_outgoing1,
  535. struct batadv_neigh_node *neigh2,
  536. struct batadv_hard_iface *if_outgoing2)
  537. {
  538. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  539. int ret = 0;
  540. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  541. if (WARN_ON(!ifinfo1))
  542. goto err_ifinfo1;
  543. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  544. if (WARN_ON(!ifinfo2))
  545. goto err_ifinfo2;
  546. ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
  547. batadv_neigh_ifinfo_put(ifinfo2);
  548. err_ifinfo2:
  549. batadv_neigh_ifinfo_put(ifinfo1);
  550. err_ifinfo1:
  551. return ret;
  552. }
  553. static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
  554. struct batadv_hard_iface *if_outgoing1,
  555. struct batadv_neigh_node *neigh2,
  556. struct batadv_hard_iface *if_outgoing2)
  557. {
  558. struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
  559. u32 threshold;
  560. bool ret = false;
  561. ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  562. if (WARN_ON(!ifinfo1))
  563. goto err_ifinfo1;
  564. ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  565. if (WARN_ON(!ifinfo2))
  566. goto err_ifinfo2;
  567. threshold = ifinfo1->bat_v.throughput / 4;
  568. threshold = ifinfo1->bat_v.throughput - threshold;
  569. ret = ifinfo2->bat_v.throughput > threshold;
  570. batadv_neigh_ifinfo_put(ifinfo2);
  571. err_ifinfo2:
  572. batadv_neigh_ifinfo_put(ifinfo1);
  573. err_ifinfo1:
  574. return ret;
  575. }
  576. static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
  577. char *buff, size_t count)
  578. {
  579. u32 old_class, class;
  580. if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
  581. "B.A.T.M.A.N. V GW selection class",
  582. &class))
  583. return -EINVAL;
  584. old_class = atomic_read(&bat_priv->gw.sel_class);
  585. atomic_set(&bat_priv->gw.sel_class, class);
  586. if (old_class != class)
  587. batadv_gw_reselect(bat_priv);
  588. return count;
  589. }
  590. static ssize_t batadv_v_show_sel_class(struct batadv_priv *bat_priv, char *buff)
  591. {
  592. u32 class = atomic_read(&bat_priv->gw.sel_class);
  593. return sprintf(buff, "%u.%u MBit\n", class / 10, class % 10);
  594. }
  595. /**
  596. * batadv_v_gw_throughput_get - retrieve the GW-bandwidth for a given GW
  597. * @gw_node: the GW to retrieve the metric for
  598. * @bw: the pointer where the metric will be stored. The metric is computed as
  599. * the minimum between the GW advertised throughput and the path throughput to
  600. * it in the mesh
  601. *
  602. * Return: 0 on success, -1 on failure
  603. */
  604. static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
  605. {
  606. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  607. struct batadv_orig_node *orig_node;
  608. struct batadv_neigh_node *router;
  609. int ret = -1;
  610. orig_node = gw_node->orig_node;
  611. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  612. if (!router)
  613. goto out;
  614. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  615. if (!router_ifinfo)
  616. goto out;
  617. /* the GW metric is computed as the minimum between the path throughput
  618. * to reach the GW itself and the advertised bandwidth.
  619. * This gives us an approximation of the effective throughput that the
  620. * client can expect via this particular GW node
  621. */
  622. *bw = router_ifinfo->bat_v.throughput;
  623. *bw = min_t(u32, *bw, gw_node->bandwidth_down);
  624. ret = 0;
  625. out:
  626. if (router)
  627. batadv_neigh_node_put(router);
  628. if (router_ifinfo)
  629. batadv_neigh_ifinfo_put(router_ifinfo);
  630. return ret;
  631. }
  632. /**
  633. * batadv_v_gw_get_best_gw_node - retrieve the best GW node
  634. * @bat_priv: the bat priv with all the soft interface information
  635. *
  636. * Return: the GW node having the best GW-metric, NULL if no GW is known
  637. */
  638. static struct batadv_gw_node *
  639. batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  640. {
  641. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  642. u32 max_bw = 0, bw;
  643. rcu_read_lock();
  644. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  645. if (!kref_get_unless_zero(&gw_node->refcount))
  646. continue;
  647. if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
  648. goto next;
  649. if (curr_gw && (bw <= max_bw))
  650. goto next;
  651. if (curr_gw)
  652. batadv_gw_node_put(curr_gw);
  653. curr_gw = gw_node;
  654. kref_get(&curr_gw->refcount);
  655. max_bw = bw;
  656. next:
  657. batadv_gw_node_put(gw_node);
  658. }
  659. rcu_read_unlock();
  660. return curr_gw;
  661. }
  662. /**
  663. * batadv_v_gw_is_eligible - check if a originator would be selected as GW
  664. * @bat_priv: the bat priv with all the soft interface information
  665. * @curr_gw_orig: originator representing the currently selected GW
  666. * @orig_node: the originator representing the new candidate
  667. *
  668. * Return: true if orig_node can be selected as current GW, false otherwise
  669. */
  670. static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
  671. struct batadv_orig_node *curr_gw_orig,
  672. struct batadv_orig_node *orig_node)
  673. {
  674. struct batadv_gw_node *curr_gw = NULL, *orig_gw = NULL;
  675. u32 gw_throughput, orig_throughput, threshold;
  676. bool ret = false;
  677. threshold = atomic_read(&bat_priv->gw.sel_class);
  678. curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
  679. if (!curr_gw) {
  680. ret = true;
  681. goto out;
  682. }
  683. if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
  684. ret = true;
  685. goto out;
  686. }
  687. orig_gw = batadv_gw_node_get(bat_priv, orig_node);
  688. if (!orig_node)
  689. goto out;
  690. if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
  691. goto out;
  692. if (orig_throughput < gw_throughput)
  693. goto out;
  694. if ((orig_throughput - gw_throughput) < threshold)
  695. goto out;
  696. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  697. "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
  698. gw_throughput, orig_throughput);
  699. ret = true;
  700. out:
  701. if (curr_gw)
  702. batadv_gw_node_put(curr_gw);
  703. if (orig_gw)
  704. batadv_gw_node_put(orig_gw);
  705. return ret;
  706. }
  707. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  708. /* fails if orig_node has no router */
  709. static int batadv_v_gw_write_buffer_text(struct batadv_priv *bat_priv,
  710. struct seq_file *seq,
  711. const struct batadv_gw_node *gw_node)
  712. {
  713. struct batadv_gw_node *curr_gw;
  714. struct batadv_neigh_node *router;
  715. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  716. int ret = -1;
  717. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  718. if (!router)
  719. goto out;
  720. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  721. if (!router_ifinfo)
  722. goto out;
  723. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  724. seq_printf(seq, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
  725. (curr_gw == gw_node ? "=>" : " "),
  726. gw_node->orig_node->orig,
  727. router_ifinfo->bat_v.throughput / 10,
  728. router_ifinfo->bat_v.throughput % 10, router->addr,
  729. router->if_incoming->net_dev->name,
  730. gw_node->bandwidth_down / 10,
  731. gw_node->bandwidth_down % 10,
  732. gw_node->bandwidth_up / 10,
  733. gw_node->bandwidth_up % 10);
  734. ret = seq_has_overflowed(seq) ? -1 : 0;
  735. if (curr_gw)
  736. batadv_gw_node_put(curr_gw);
  737. out:
  738. if (router_ifinfo)
  739. batadv_neigh_ifinfo_put(router_ifinfo);
  740. if (router)
  741. batadv_neigh_node_put(router);
  742. return ret;
  743. }
  744. /**
  745. * batadv_v_gw_print - print the gateway list
  746. * @bat_priv: the bat priv with all the soft interface information
  747. * @seq: gateway table seq_file struct
  748. */
  749. static void batadv_v_gw_print(struct batadv_priv *bat_priv,
  750. struct seq_file *seq)
  751. {
  752. struct batadv_gw_node *gw_node;
  753. int gw_count = 0;
  754. seq_puts(seq,
  755. " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
  756. rcu_read_lock();
  757. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  758. /* fails if orig_node has no router */
  759. if (batadv_v_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
  760. continue;
  761. gw_count++;
  762. }
  763. rcu_read_unlock();
  764. if (gw_count == 0)
  765. seq_puts(seq, "No gateways in range ...\n");
  766. }
  767. #endif
  768. /**
  769. * batadv_v_gw_dump_entry - Dump a gateway into a message
  770. * @msg: Netlink message to dump into
  771. * @portid: Port making netlink request
  772. * @seq: Sequence number of netlink message
  773. * @bat_priv: The bat priv with all the soft interface information
  774. * @gw_node: Gateway to be dumped
  775. *
  776. * Return: Error code, or 0 on success
  777. */
  778. static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  779. struct batadv_priv *bat_priv,
  780. struct batadv_gw_node *gw_node)
  781. {
  782. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  783. struct batadv_neigh_node *router;
  784. struct batadv_gw_node *curr_gw = NULL;
  785. int ret = 0;
  786. void *hdr;
  787. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  788. if (!router)
  789. goto out;
  790. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  791. if (!router_ifinfo)
  792. goto out;
  793. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  794. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  795. NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
  796. if (!hdr) {
  797. ret = -ENOBUFS;
  798. goto out;
  799. }
  800. ret = -EMSGSIZE;
  801. if (curr_gw == gw_node) {
  802. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  803. genlmsg_cancel(msg, hdr);
  804. goto out;
  805. }
  806. }
  807. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  808. gw_node->orig_node->orig)) {
  809. genlmsg_cancel(msg, hdr);
  810. goto out;
  811. }
  812. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
  813. router_ifinfo->bat_v.throughput)) {
  814. genlmsg_cancel(msg, hdr);
  815. goto out;
  816. }
  817. if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
  818. genlmsg_cancel(msg, hdr);
  819. goto out;
  820. }
  821. if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  822. router->if_incoming->net_dev->name)) {
  823. genlmsg_cancel(msg, hdr);
  824. goto out;
  825. }
  826. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  827. gw_node->bandwidth_down)) {
  828. genlmsg_cancel(msg, hdr);
  829. goto out;
  830. }
  831. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
  832. genlmsg_cancel(msg, hdr);
  833. goto out;
  834. }
  835. genlmsg_end(msg, hdr);
  836. ret = 0;
  837. out:
  838. if (curr_gw)
  839. batadv_gw_node_put(curr_gw);
  840. if (router_ifinfo)
  841. batadv_neigh_ifinfo_put(router_ifinfo);
  842. if (router)
  843. batadv_neigh_node_put(router);
  844. return ret;
  845. }
  846. /**
  847. * batadv_v_gw_dump - Dump gateways into a message
  848. * @msg: Netlink message to dump into
  849. * @cb: Control block containing additional options
  850. * @bat_priv: The bat priv with all the soft interface information
  851. */
  852. static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  853. struct batadv_priv *bat_priv)
  854. {
  855. int portid = NETLINK_CB(cb->skb).portid;
  856. struct batadv_gw_node *gw_node;
  857. int idx_skip = cb->args[0];
  858. int idx = 0;
  859. rcu_read_lock();
  860. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  861. if (idx++ < idx_skip)
  862. continue;
  863. if (batadv_v_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  864. bat_priv, gw_node)) {
  865. idx_skip = idx - 1;
  866. goto unlock;
  867. }
  868. }
  869. idx_skip = idx;
  870. unlock:
  871. rcu_read_unlock();
  872. cb->args[0] = idx_skip;
  873. }
  874. static struct batadv_algo_ops batadv_batman_v __read_mostly = {
  875. .name = "BATMAN_V",
  876. .iface = {
  877. .activate = batadv_v_iface_activate,
  878. .enable = batadv_v_iface_enable,
  879. .disable = batadv_v_iface_disable,
  880. .update_mac = batadv_v_iface_update_mac,
  881. .primary_set = batadv_v_primary_iface_set,
  882. },
  883. .neigh = {
  884. .hardif_init = batadv_v_hardif_neigh_init,
  885. .cmp = batadv_v_neigh_cmp,
  886. .is_similar_or_better = batadv_v_neigh_is_sob,
  887. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  888. .print = batadv_v_neigh_print,
  889. #endif
  890. .dump = batadv_v_neigh_dump,
  891. },
  892. .orig = {
  893. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  894. .print = batadv_v_orig_print,
  895. #endif
  896. .dump = batadv_v_orig_dump,
  897. },
  898. .gw = {
  899. .store_sel_class = batadv_v_store_sel_class,
  900. .show_sel_class = batadv_v_show_sel_class,
  901. .get_best_gw_node = batadv_v_gw_get_best_gw_node,
  902. .is_eligible = batadv_v_gw_is_eligible,
  903. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  904. .print = batadv_v_gw_print,
  905. #endif
  906. .dump = batadv_v_gw_dump,
  907. },
  908. };
  909. /**
  910. * batadv_v_hardif_init - initialize the algorithm specific fields in the
  911. * hard-interface object
  912. * @hard_iface: the hard-interface to initialize
  913. */
  914. void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
  915. {
  916. /* enable link throughput auto-detection by setting the throughput
  917. * override to zero
  918. */
  919. atomic_set(&hard_iface->bat_v.throughput_override, 0);
  920. atomic_set(&hard_iface->bat_v.elp_interval, 500);
  921. }
  922. /**
  923. * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
  924. * mesh
  925. * @bat_priv: the object representing the mesh interface to initialise
  926. *
  927. * Return: 0 on success or a negative error code otherwise
  928. */
  929. int batadv_v_mesh_init(struct batadv_priv *bat_priv)
  930. {
  931. int ret = 0;
  932. ret = batadv_v_ogm_init(bat_priv);
  933. if (ret < 0)
  934. return ret;
  935. /* set default throughput difference threshold to 5Mbps */
  936. atomic_set(&bat_priv->gw.sel_class, 50);
  937. return 0;
  938. }
  939. /**
  940. * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
  941. * @bat_priv: the object representing the mesh interface to free
  942. */
  943. void batadv_v_mesh_free(struct batadv_priv *bat_priv)
  944. {
  945. batadv_v_ogm_free(bat_priv);
  946. }
  947. /**
  948. * batadv_v_init - B.A.T.M.A.N. V initialization function
  949. *
  950. * Description: Takes care of initializing all the subcomponents.
  951. * It is invoked upon module load only.
  952. *
  953. * Return: 0 on success or a negative error code otherwise
  954. */
  955. int __init batadv_v_init(void)
  956. {
  957. int ret;
  958. /* B.A.T.M.A.N. V echo location protocol packet */
  959. ret = batadv_recv_handler_register(BATADV_ELP,
  960. batadv_v_elp_packet_recv);
  961. if (ret < 0)
  962. return ret;
  963. ret = batadv_recv_handler_register(BATADV_OGM2,
  964. batadv_v_ogm_packet_recv);
  965. if (ret < 0)
  966. goto elp_unregister;
  967. ret = batadv_algo_register(&batadv_batman_v);
  968. if (ret < 0)
  969. goto ogm_unregister;
  970. return ret;
  971. ogm_unregister:
  972. batadv_recv_handler_unregister(BATADV_OGM2);
  973. elp_unregister:
  974. batadv_recv_handler_unregister(BATADV_ELP);
  975. return ret;
  976. }