bat_v.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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;
  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 (router_ifinfo)
  839. batadv_neigh_ifinfo_put(router_ifinfo);
  840. if (router)
  841. batadv_neigh_node_put(router);
  842. return ret;
  843. }
  844. /**
  845. * batadv_v_gw_dump - Dump gateways into a message
  846. * @msg: Netlink message to dump into
  847. * @cb: Control block containing additional options
  848. * @bat_priv: The bat priv with all the soft interface information
  849. */
  850. static void batadv_v_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  851. struct batadv_priv *bat_priv)
  852. {
  853. int portid = NETLINK_CB(cb->skb).portid;
  854. struct batadv_gw_node *gw_node;
  855. int idx_skip = cb->args[0];
  856. int idx = 0;
  857. rcu_read_lock();
  858. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  859. if (idx++ < idx_skip)
  860. continue;
  861. if (batadv_v_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  862. bat_priv, gw_node)) {
  863. idx_skip = idx - 1;
  864. goto unlock;
  865. }
  866. }
  867. idx_skip = idx;
  868. unlock:
  869. rcu_read_unlock();
  870. cb->args[0] = idx_skip;
  871. }
  872. static struct batadv_algo_ops batadv_batman_v __read_mostly = {
  873. .name = "BATMAN_V",
  874. .iface = {
  875. .activate = batadv_v_iface_activate,
  876. .enable = batadv_v_iface_enable,
  877. .disable = batadv_v_iface_disable,
  878. .update_mac = batadv_v_iface_update_mac,
  879. .primary_set = batadv_v_primary_iface_set,
  880. },
  881. .neigh = {
  882. .hardif_init = batadv_v_hardif_neigh_init,
  883. .cmp = batadv_v_neigh_cmp,
  884. .is_similar_or_better = batadv_v_neigh_is_sob,
  885. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  886. .print = batadv_v_neigh_print,
  887. #endif
  888. .dump = batadv_v_neigh_dump,
  889. },
  890. .orig = {
  891. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  892. .print = batadv_v_orig_print,
  893. #endif
  894. .dump = batadv_v_orig_dump,
  895. },
  896. .gw = {
  897. .store_sel_class = batadv_v_store_sel_class,
  898. .show_sel_class = batadv_v_show_sel_class,
  899. .get_best_gw_node = batadv_v_gw_get_best_gw_node,
  900. .is_eligible = batadv_v_gw_is_eligible,
  901. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  902. .print = batadv_v_gw_print,
  903. #endif
  904. .dump = batadv_v_gw_dump,
  905. },
  906. };
  907. /**
  908. * batadv_v_hardif_init - initialize the algorithm specific fields in the
  909. * hard-interface object
  910. * @hard_iface: the hard-interface to initialize
  911. */
  912. void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
  913. {
  914. /* enable link throughput auto-detection by setting the throughput
  915. * override to zero
  916. */
  917. atomic_set(&hard_iface->bat_v.throughput_override, 0);
  918. atomic_set(&hard_iface->bat_v.elp_interval, 500);
  919. }
  920. /**
  921. * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
  922. * mesh
  923. * @bat_priv: the object representing the mesh interface to initialise
  924. *
  925. * Return: 0 on success or a negative error code otherwise
  926. */
  927. int batadv_v_mesh_init(struct batadv_priv *bat_priv)
  928. {
  929. int ret = 0;
  930. ret = batadv_v_ogm_init(bat_priv);
  931. if (ret < 0)
  932. return ret;
  933. /* set default throughput difference threshold to 5Mbps */
  934. atomic_set(&bat_priv->gw.sel_class, 50);
  935. return 0;
  936. }
  937. /**
  938. * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
  939. * @bat_priv: the object representing the mesh interface to free
  940. */
  941. void batadv_v_mesh_free(struct batadv_priv *bat_priv)
  942. {
  943. batadv_v_ogm_free(bat_priv);
  944. }
  945. /**
  946. * batadv_v_init - B.A.T.M.A.N. V initialization function
  947. *
  948. * Description: Takes care of initializing all the subcomponents.
  949. * It is invoked upon module load only.
  950. *
  951. * Return: 0 on success or a negative error code otherwise
  952. */
  953. int __init batadv_v_init(void)
  954. {
  955. int ret;
  956. /* B.A.T.M.A.N. V echo location protocol packet */
  957. ret = batadv_recv_handler_register(BATADV_ELP,
  958. batadv_v_elp_packet_recv);
  959. if (ret < 0)
  960. return ret;
  961. ret = batadv_recv_handler_register(BATADV_OGM2,
  962. batadv_v_ogm_packet_recv);
  963. if (ret < 0)
  964. goto elp_unregister;
  965. ret = batadv_algo_register(&batadv_batman_v);
  966. if (ret < 0)
  967. goto ogm_unregister;
  968. return ret;
  969. ogm_unregister:
  970. batadv_recv_handler_unregister(BATADV_OGM2);
  971. elp_unregister:
  972. batadv_recv_handler_unregister(BATADV_ELP);
  973. return ret;
  974. }