bat_v.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /* Copyright (C) 2013-2017 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_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
  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. /**
  577. * batadv_v_init_sel_class - initialize GW selection class
  578. * @bat_priv: the bat priv with all the soft interface information
  579. */
  580. static void batadv_v_init_sel_class(struct batadv_priv *bat_priv)
  581. {
  582. /* set default throughput difference threshold to 5Mbps */
  583. atomic_set(&bat_priv->gw.sel_class, 50);
  584. }
  585. static ssize_t batadv_v_store_sel_class(struct batadv_priv *bat_priv,
  586. char *buff, size_t count)
  587. {
  588. u32 old_class, class;
  589. if (!batadv_parse_throughput(bat_priv->soft_iface, buff,
  590. "B.A.T.M.A.N. V GW selection class",
  591. &class))
  592. return -EINVAL;
  593. old_class = atomic_read(&bat_priv->gw.sel_class);
  594. atomic_set(&bat_priv->gw.sel_class, class);
  595. if (old_class != class)
  596. batadv_gw_reselect(bat_priv);
  597. return count;
  598. }
  599. static ssize_t batadv_v_show_sel_class(struct batadv_priv *bat_priv, char *buff)
  600. {
  601. u32 class = atomic_read(&bat_priv->gw.sel_class);
  602. return sprintf(buff, "%u.%u MBit\n", class / 10, class % 10);
  603. }
  604. /**
  605. * batadv_v_gw_throughput_get - retrieve the GW-bandwidth for a given GW
  606. * @gw_node: the GW to retrieve the metric for
  607. * @bw: the pointer where the metric will be stored. The metric is computed as
  608. * the minimum between the GW advertised throughput and the path throughput to
  609. * it in the mesh
  610. *
  611. * Return: 0 on success, -1 on failure
  612. */
  613. static int batadv_v_gw_throughput_get(struct batadv_gw_node *gw_node, u32 *bw)
  614. {
  615. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  616. struct batadv_orig_node *orig_node;
  617. struct batadv_neigh_node *router;
  618. int ret = -1;
  619. orig_node = gw_node->orig_node;
  620. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  621. if (!router)
  622. goto out;
  623. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  624. if (!router_ifinfo)
  625. goto out;
  626. /* the GW metric is computed as the minimum between the path throughput
  627. * to reach the GW itself and the advertised bandwidth.
  628. * This gives us an approximation of the effective throughput that the
  629. * client can expect via this particular GW node
  630. */
  631. *bw = router_ifinfo->bat_v.throughput;
  632. *bw = min_t(u32, *bw, gw_node->bandwidth_down);
  633. ret = 0;
  634. out:
  635. if (router)
  636. batadv_neigh_node_put(router);
  637. if (router_ifinfo)
  638. batadv_neigh_ifinfo_put(router_ifinfo);
  639. return ret;
  640. }
  641. /**
  642. * batadv_v_gw_get_best_gw_node - retrieve the best GW node
  643. * @bat_priv: the bat priv with all the soft interface information
  644. *
  645. * Return: the GW node having the best GW-metric, NULL if no GW is known
  646. */
  647. static struct batadv_gw_node *
  648. batadv_v_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  649. {
  650. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  651. u32 max_bw = 0, bw;
  652. rcu_read_lock();
  653. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  654. if (!kref_get_unless_zero(&gw_node->refcount))
  655. continue;
  656. if (batadv_v_gw_throughput_get(gw_node, &bw) < 0)
  657. goto next;
  658. if (curr_gw && (bw <= max_bw))
  659. goto next;
  660. if (curr_gw)
  661. batadv_gw_node_put(curr_gw);
  662. curr_gw = gw_node;
  663. kref_get(&curr_gw->refcount);
  664. max_bw = bw;
  665. next:
  666. batadv_gw_node_put(gw_node);
  667. }
  668. rcu_read_unlock();
  669. return curr_gw;
  670. }
  671. /**
  672. * batadv_v_gw_is_eligible - check if a originator would be selected as GW
  673. * @bat_priv: the bat priv with all the soft interface information
  674. * @curr_gw_orig: originator representing the currently selected GW
  675. * @orig_node: the originator representing the new candidate
  676. *
  677. * Return: true if orig_node can be selected as current GW, false otherwise
  678. */
  679. static bool batadv_v_gw_is_eligible(struct batadv_priv *bat_priv,
  680. struct batadv_orig_node *curr_gw_orig,
  681. struct batadv_orig_node *orig_node)
  682. {
  683. struct batadv_gw_node *curr_gw, *orig_gw = NULL;
  684. u32 gw_throughput, orig_throughput, threshold;
  685. bool ret = false;
  686. threshold = atomic_read(&bat_priv->gw.sel_class);
  687. curr_gw = batadv_gw_node_get(bat_priv, curr_gw_orig);
  688. if (!curr_gw) {
  689. ret = true;
  690. goto out;
  691. }
  692. if (batadv_v_gw_throughput_get(curr_gw, &gw_throughput) < 0) {
  693. ret = true;
  694. goto out;
  695. }
  696. orig_gw = batadv_gw_node_get(bat_priv, orig_node);
  697. if (!orig_node)
  698. goto out;
  699. if (batadv_v_gw_throughput_get(orig_gw, &orig_throughput) < 0)
  700. goto out;
  701. if (orig_throughput < gw_throughput)
  702. goto out;
  703. if ((orig_throughput - gw_throughput) < threshold)
  704. goto out;
  705. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  706. "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
  707. gw_throughput, orig_throughput);
  708. ret = true;
  709. out:
  710. if (curr_gw)
  711. batadv_gw_node_put(curr_gw);
  712. if (orig_gw)
  713. batadv_gw_node_put(orig_gw);
  714. return ret;
  715. }
  716. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  717. /* fails if orig_node has no router */
  718. static int batadv_v_gw_write_buffer_text(struct batadv_priv *bat_priv,
  719. struct seq_file *seq,
  720. const struct batadv_gw_node *gw_node)
  721. {
  722. struct batadv_gw_node *curr_gw;
  723. struct batadv_neigh_node *router;
  724. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  725. int ret = -1;
  726. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  727. if (!router)
  728. goto out;
  729. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  730. if (!router_ifinfo)
  731. goto out;
  732. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  733. seq_printf(seq, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
  734. (curr_gw == gw_node ? "=>" : " "),
  735. gw_node->orig_node->orig,
  736. router_ifinfo->bat_v.throughput / 10,
  737. router_ifinfo->bat_v.throughput % 10, router->addr,
  738. router->if_incoming->net_dev->name,
  739. gw_node->bandwidth_down / 10,
  740. gw_node->bandwidth_down % 10,
  741. gw_node->bandwidth_up / 10,
  742. gw_node->bandwidth_up % 10);
  743. ret = seq_has_overflowed(seq) ? -1 : 0;
  744. if (curr_gw)
  745. batadv_gw_node_put(curr_gw);
  746. out:
  747. if (router_ifinfo)
  748. batadv_neigh_ifinfo_put(router_ifinfo);
  749. if (router)
  750. batadv_neigh_node_put(router);
  751. return ret;
  752. }
  753. /**
  754. * batadv_v_gw_print - print the gateway list
  755. * @bat_priv: the bat priv with all the soft interface information
  756. * @seq: gateway table seq_file struct
  757. */
  758. static void batadv_v_gw_print(struct batadv_priv *bat_priv,
  759. struct seq_file *seq)
  760. {
  761. struct batadv_gw_node *gw_node;
  762. int gw_count = 0;
  763. seq_puts(seq,
  764. " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
  765. rcu_read_lock();
  766. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  767. /* fails if orig_node has no router */
  768. if (batadv_v_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
  769. continue;
  770. gw_count++;
  771. }
  772. rcu_read_unlock();
  773. if (gw_count == 0)
  774. seq_puts(seq, "No gateways in range ...\n");
  775. }
  776. #endif
  777. /**
  778. * batadv_v_gw_dump_entry - Dump a gateway into a message
  779. * @msg: Netlink message to dump into
  780. * @portid: Port making netlink request
  781. * @seq: Sequence number of netlink message
  782. * @bat_priv: The bat priv with all the soft interface information
  783. * @gw_node: Gateway to be dumped
  784. *
  785. * Return: Error code, or 0 on success
  786. */
  787. static int batadv_v_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  788. struct batadv_priv *bat_priv,
  789. struct batadv_gw_node *gw_node)
  790. {
  791. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  792. struct batadv_neigh_node *router;
  793. struct batadv_gw_node *curr_gw;
  794. int ret = -EINVAL;
  795. void *hdr;
  796. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  797. if (!router)
  798. goto out;
  799. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  800. if (!router_ifinfo)
  801. goto out;
  802. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  803. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  804. NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
  805. if (!hdr) {
  806. ret = -ENOBUFS;
  807. goto out;
  808. }
  809. ret = -EMSGSIZE;
  810. if (curr_gw == gw_node) {
  811. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  812. genlmsg_cancel(msg, hdr);
  813. goto out;
  814. }
  815. }
  816. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  817. gw_node->orig_node->orig)) {
  818. genlmsg_cancel(msg, hdr);
  819. goto out;
  820. }
  821. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT,
  822. router_ifinfo->bat_v.throughput)) {
  823. genlmsg_cancel(msg, hdr);
  824. goto out;
  825. }
  826. if (nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN, router->addr)) {
  827. genlmsg_cancel(msg, hdr);
  828. goto out;
  829. }
  830. if (nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  831. router->if_incoming->net_dev->name)) {
  832. genlmsg_cancel(msg, hdr);
  833. goto out;
  834. }
  835. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  836. gw_node->bandwidth_down)) {
  837. genlmsg_cancel(msg, hdr);
  838. goto out;
  839. }
  840. if (nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP, gw_node->bandwidth_up)) {
  841. genlmsg_cancel(msg, hdr);
  842. goto out;
  843. }
  844. genlmsg_end(msg, hdr);
  845. ret = 0;
  846. out:
  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. }