originator.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /* Copyright (C) 2009-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  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 "originator.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/errno.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/fs.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kref.h>
  26. #include <linux/list.h>
  27. #include <linux/lockdep.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/netlink.h>
  30. #include <linux/rculist.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/workqueue.h>
  36. #include <net/sock.h>
  37. #include <uapi/linux/batman_adv.h>
  38. #include "bat_algo.h"
  39. #include "distributed-arp-table.h"
  40. #include "fragmentation.h"
  41. #include "gateway_client.h"
  42. #include "hard-interface.h"
  43. #include "hash.h"
  44. #include "log.h"
  45. #include "multicast.h"
  46. #include "netlink.h"
  47. #include "network-coding.h"
  48. #include "routing.h"
  49. #include "soft-interface.h"
  50. #include "translation-table.h"
  51. /* hash class keys */
  52. static struct lock_class_key batadv_orig_hash_lock_class_key;
  53. static void batadv_purge_orig(struct work_struct *work);
  54. /**
  55. * batadv_compare_orig - comparing function used in the originator hash table
  56. * @node: node in the local table
  57. * @data2: second object to compare the node to
  58. *
  59. * Return: true if they are the same originator
  60. */
  61. bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
  62. {
  63. const void *data1 = container_of(node, struct batadv_orig_node,
  64. hash_entry);
  65. return batadv_compare_eth(data1, data2);
  66. }
  67. /**
  68. * batadv_orig_node_vlan_get - get an orig_node_vlan object
  69. * @orig_node: the originator serving the VLAN
  70. * @vid: the VLAN identifier
  71. *
  72. * Return: the vlan object identified by vid and belonging to orig_node or NULL
  73. * if it does not exist.
  74. */
  75. struct batadv_orig_node_vlan *
  76. batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
  77. unsigned short vid)
  78. {
  79. struct batadv_orig_node_vlan *vlan = NULL, *tmp;
  80. rcu_read_lock();
  81. hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
  82. if (tmp->vid != vid)
  83. continue;
  84. if (!kref_get_unless_zero(&tmp->refcount))
  85. continue;
  86. vlan = tmp;
  87. break;
  88. }
  89. rcu_read_unlock();
  90. return vlan;
  91. }
  92. /**
  93. * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
  94. * object
  95. * @orig_node: the originator serving the VLAN
  96. * @vid: the VLAN identifier
  97. *
  98. * Return: NULL in case of failure or the vlan object identified by vid and
  99. * belonging to orig_node otherwise. The object is created and added to the list
  100. * if it does not exist.
  101. *
  102. * The object is returned with refcounter increased by 1.
  103. */
  104. struct batadv_orig_node_vlan *
  105. batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
  106. unsigned short vid)
  107. {
  108. struct batadv_orig_node_vlan *vlan;
  109. spin_lock_bh(&orig_node->vlan_list_lock);
  110. /* first look if an object for this vid already exists */
  111. vlan = batadv_orig_node_vlan_get(orig_node, vid);
  112. if (vlan)
  113. goto out;
  114. vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
  115. if (!vlan)
  116. goto out;
  117. kref_init(&vlan->refcount);
  118. vlan->vid = vid;
  119. kref_get(&vlan->refcount);
  120. hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
  121. out:
  122. spin_unlock_bh(&orig_node->vlan_list_lock);
  123. return vlan;
  124. }
  125. /**
  126. * batadv_orig_node_vlan_release - release originator-vlan object from lists
  127. * and queue for free after rcu grace period
  128. * @ref: kref pointer of the originator-vlan object
  129. */
  130. static void batadv_orig_node_vlan_release(struct kref *ref)
  131. {
  132. struct batadv_orig_node_vlan *orig_vlan;
  133. orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
  134. kfree_rcu(orig_vlan, rcu);
  135. }
  136. /**
  137. * batadv_orig_node_vlan_put - decrement the refcounter and possibly release
  138. * the originator-vlan object
  139. * @orig_vlan: the originator-vlan object to release
  140. */
  141. void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
  142. {
  143. kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
  144. }
  145. int batadv_originator_init(struct batadv_priv *bat_priv)
  146. {
  147. if (bat_priv->orig_hash)
  148. return 0;
  149. bat_priv->orig_hash = batadv_hash_new(1024);
  150. if (!bat_priv->orig_hash)
  151. goto err;
  152. batadv_hash_set_lock_class(bat_priv->orig_hash,
  153. &batadv_orig_hash_lock_class_key);
  154. INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
  155. queue_delayed_work(batadv_event_workqueue,
  156. &bat_priv->orig_work,
  157. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  158. return 0;
  159. err:
  160. return -ENOMEM;
  161. }
  162. /**
  163. * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
  164. * free after rcu grace period
  165. * @ref: kref pointer of the neigh_ifinfo
  166. */
  167. static void batadv_neigh_ifinfo_release(struct kref *ref)
  168. {
  169. struct batadv_neigh_ifinfo *neigh_ifinfo;
  170. neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
  171. if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
  172. batadv_hardif_put(neigh_ifinfo->if_outgoing);
  173. kfree_rcu(neigh_ifinfo, rcu);
  174. }
  175. /**
  176. * batadv_neigh_ifinfo_put - decrement the refcounter and possibly release
  177. * the neigh_ifinfo
  178. * @neigh_ifinfo: the neigh_ifinfo object to release
  179. */
  180. void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
  181. {
  182. kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
  183. }
  184. /**
  185. * batadv_hardif_neigh_release - release hardif neigh node from lists and
  186. * queue for free after rcu grace period
  187. * @ref: kref pointer of the neigh_node
  188. */
  189. static void batadv_hardif_neigh_release(struct kref *ref)
  190. {
  191. struct batadv_hardif_neigh_node *hardif_neigh;
  192. hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
  193. refcount);
  194. spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
  195. hlist_del_init_rcu(&hardif_neigh->list);
  196. spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
  197. batadv_hardif_put(hardif_neigh->if_incoming);
  198. kfree_rcu(hardif_neigh, rcu);
  199. }
  200. /**
  201. * batadv_hardif_neigh_put - decrement the hardif neighbors refcounter
  202. * and possibly release it
  203. * @hardif_neigh: hardif neigh neighbor to free
  204. */
  205. void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
  206. {
  207. kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
  208. }
  209. /**
  210. * batadv_neigh_node_release - release neigh_node from lists and queue for
  211. * free after rcu grace period
  212. * @ref: kref pointer of the neigh_node
  213. */
  214. static void batadv_neigh_node_release(struct kref *ref)
  215. {
  216. struct hlist_node *node_tmp;
  217. struct batadv_neigh_node *neigh_node;
  218. struct batadv_neigh_ifinfo *neigh_ifinfo;
  219. neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
  220. hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
  221. &neigh_node->ifinfo_list, list) {
  222. batadv_neigh_ifinfo_put(neigh_ifinfo);
  223. }
  224. batadv_hardif_neigh_put(neigh_node->hardif_neigh);
  225. batadv_hardif_put(neigh_node->if_incoming);
  226. kfree_rcu(neigh_node, rcu);
  227. }
  228. /**
  229. * batadv_neigh_node_put - decrement the neighbors refcounter and possibly
  230. * release it
  231. * @neigh_node: neigh neighbor to free
  232. */
  233. void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
  234. {
  235. kref_put(&neigh_node->refcount, batadv_neigh_node_release);
  236. }
  237. /**
  238. * batadv_orig_router_get - router to the originator depending on iface
  239. * @orig_node: the orig node for the router
  240. * @if_outgoing: the interface where the payload packet has been received or
  241. * the OGM should be sent to
  242. *
  243. * Return: the neighbor which should be router for this orig_node/iface.
  244. *
  245. * The object is returned with refcounter increased by 1.
  246. */
  247. struct batadv_neigh_node *
  248. batadv_orig_router_get(struct batadv_orig_node *orig_node,
  249. const struct batadv_hard_iface *if_outgoing)
  250. {
  251. struct batadv_orig_ifinfo *orig_ifinfo;
  252. struct batadv_neigh_node *router = NULL;
  253. rcu_read_lock();
  254. hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
  255. if (orig_ifinfo->if_outgoing != if_outgoing)
  256. continue;
  257. router = rcu_dereference(orig_ifinfo->router);
  258. break;
  259. }
  260. if (router && !kref_get_unless_zero(&router->refcount))
  261. router = NULL;
  262. rcu_read_unlock();
  263. return router;
  264. }
  265. /**
  266. * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
  267. * @orig_node: the orig node to be queried
  268. * @if_outgoing: the interface for which the ifinfo should be acquired
  269. *
  270. * Return: the requested orig_ifinfo or NULL if not found.
  271. *
  272. * The object is returned with refcounter increased by 1.
  273. */
  274. struct batadv_orig_ifinfo *
  275. batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
  276. struct batadv_hard_iface *if_outgoing)
  277. {
  278. struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
  279. rcu_read_lock();
  280. hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
  281. list) {
  282. if (tmp->if_outgoing != if_outgoing)
  283. continue;
  284. if (!kref_get_unless_zero(&tmp->refcount))
  285. continue;
  286. orig_ifinfo = tmp;
  287. break;
  288. }
  289. rcu_read_unlock();
  290. return orig_ifinfo;
  291. }
  292. /**
  293. * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
  294. * @orig_node: the orig node to be queried
  295. * @if_outgoing: the interface for which the ifinfo should be acquired
  296. *
  297. * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
  298. * interface otherwise. The object is created and added to the list
  299. * if it does not exist.
  300. *
  301. * The object is returned with refcounter increased by 1.
  302. */
  303. struct batadv_orig_ifinfo *
  304. batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
  305. struct batadv_hard_iface *if_outgoing)
  306. {
  307. struct batadv_orig_ifinfo *orig_ifinfo;
  308. unsigned long reset_time;
  309. spin_lock_bh(&orig_node->neigh_list_lock);
  310. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
  311. if (orig_ifinfo)
  312. goto out;
  313. orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
  314. if (!orig_ifinfo)
  315. goto out;
  316. if (if_outgoing != BATADV_IF_DEFAULT)
  317. kref_get(&if_outgoing->refcount);
  318. reset_time = jiffies - 1;
  319. reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
  320. orig_ifinfo->batman_seqno_reset = reset_time;
  321. orig_ifinfo->if_outgoing = if_outgoing;
  322. INIT_HLIST_NODE(&orig_ifinfo->list);
  323. kref_init(&orig_ifinfo->refcount);
  324. kref_get(&orig_ifinfo->refcount);
  325. hlist_add_head_rcu(&orig_ifinfo->list,
  326. &orig_node->ifinfo_list);
  327. out:
  328. spin_unlock_bh(&orig_node->neigh_list_lock);
  329. return orig_ifinfo;
  330. }
  331. /**
  332. * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
  333. * @neigh: the neigh node to be queried
  334. * @if_outgoing: the interface for which the ifinfo should be acquired
  335. *
  336. * The object is returned with refcounter increased by 1.
  337. *
  338. * Return: the requested neigh_ifinfo or NULL if not found
  339. */
  340. struct batadv_neigh_ifinfo *
  341. batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
  342. struct batadv_hard_iface *if_outgoing)
  343. {
  344. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
  345. *tmp_neigh_ifinfo;
  346. rcu_read_lock();
  347. hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
  348. list) {
  349. if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
  350. continue;
  351. if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
  352. continue;
  353. neigh_ifinfo = tmp_neigh_ifinfo;
  354. break;
  355. }
  356. rcu_read_unlock();
  357. return neigh_ifinfo;
  358. }
  359. /**
  360. * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
  361. * @neigh: the neigh node to be queried
  362. * @if_outgoing: the interface for which the ifinfo should be acquired
  363. *
  364. * Return: NULL in case of failure or the neigh_ifinfo object for the
  365. * if_outgoing interface otherwise. The object is created and added to the list
  366. * if it does not exist.
  367. *
  368. * The object is returned with refcounter increased by 1.
  369. */
  370. struct batadv_neigh_ifinfo *
  371. batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
  372. struct batadv_hard_iface *if_outgoing)
  373. {
  374. struct batadv_neigh_ifinfo *neigh_ifinfo;
  375. spin_lock_bh(&neigh->ifinfo_lock);
  376. neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
  377. if (neigh_ifinfo)
  378. goto out;
  379. neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
  380. if (!neigh_ifinfo)
  381. goto out;
  382. if (if_outgoing)
  383. kref_get(&if_outgoing->refcount);
  384. INIT_HLIST_NODE(&neigh_ifinfo->list);
  385. kref_init(&neigh_ifinfo->refcount);
  386. neigh_ifinfo->if_outgoing = if_outgoing;
  387. kref_get(&neigh_ifinfo->refcount);
  388. hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
  389. out:
  390. spin_unlock_bh(&neigh->ifinfo_lock);
  391. return neigh_ifinfo;
  392. }
  393. /**
  394. * batadv_neigh_node_get - retrieve a neighbour from the list
  395. * @orig_node: originator which the neighbour belongs to
  396. * @hard_iface: the interface where this neighbour is connected to
  397. * @addr: the address of the neighbour
  398. *
  399. * Looks for and possibly returns a neighbour belonging to this originator list
  400. * which is connected through the provided hard interface.
  401. *
  402. * Return: neighbor when found. Othwerwise NULL
  403. */
  404. static struct batadv_neigh_node *
  405. batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
  406. const struct batadv_hard_iface *hard_iface,
  407. const u8 *addr)
  408. {
  409. struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
  410. rcu_read_lock();
  411. hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
  412. if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
  413. continue;
  414. if (tmp_neigh_node->if_incoming != hard_iface)
  415. continue;
  416. if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
  417. continue;
  418. res = tmp_neigh_node;
  419. break;
  420. }
  421. rcu_read_unlock();
  422. return res;
  423. }
  424. /**
  425. * batadv_hardif_neigh_create - create a hardif neighbour node
  426. * @hard_iface: the interface this neighbour is connected to
  427. * @neigh_addr: the interface address of the neighbour to retrieve
  428. * @orig_node: originator object representing the neighbour
  429. *
  430. * Return: the hardif neighbour node if found or created or NULL otherwise.
  431. */
  432. static struct batadv_hardif_neigh_node *
  433. batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
  434. const u8 *neigh_addr,
  435. struct batadv_orig_node *orig_node)
  436. {
  437. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  438. struct batadv_hardif_neigh_node *hardif_neigh;
  439. spin_lock_bh(&hard_iface->neigh_list_lock);
  440. /* check if neighbor hasn't been added in the meantime */
  441. hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
  442. if (hardif_neigh)
  443. goto out;
  444. hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
  445. if (!hardif_neigh)
  446. goto out;
  447. kref_get(&hard_iface->refcount);
  448. INIT_HLIST_NODE(&hardif_neigh->list);
  449. ether_addr_copy(hardif_neigh->addr, neigh_addr);
  450. ether_addr_copy(hardif_neigh->orig, orig_node->orig);
  451. hardif_neigh->if_incoming = hard_iface;
  452. hardif_neigh->last_seen = jiffies;
  453. kref_init(&hardif_neigh->refcount);
  454. if (bat_priv->algo_ops->neigh.hardif_init)
  455. bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
  456. hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
  457. out:
  458. spin_unlock_bh(&hard_iface->neigh_list_lock);
  459. return hardif_neigh;
  460. }
  461. /**
  462. * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
  463. * node
  464. * @hard_iface: the interface this neighbour is connected to
  465. * @neigh_addr: the interface address of the neighbour to retrieve
  466. * @orig_node: originator object representing the neighbour
  467. *
  468. * Return: the hardif neighbour node if found or created or NULL otherwise.
  469. */
  470. static struct batadv_hardif_neigh_node *
  471. batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
  472. const u8 *neigh_addr,
  473. struct batadv_orig_node *orig_node)
  474. {
  475. struct batadv_hardif_neigh_node *hardif_neigh;
  476. /* first check without locking to avoid the overhead */
  477. hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
  478. if (hardif_neigh)
  479. return hardif_neigh;
  480. return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
  481. }
  482. /**
  483. * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
  484. * @hard_iface: the interface where this neighbour is connected to
  485. * @neigh_addr: the address of the neighbour
  486. *
  487. * Looks for and possibly returns a neighbour belonging to this hard interface.
  488. *
  489. * Return: neighbor when found. Othwerwise NULL
  490. */
  491. struct batadv_hardif_neigh_node *
  492. batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
  493. const u8 *neigh_addr)
  494. {
  495. struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
  496. rcu_read_lock();
  497. hlist_for_each_entry_rcu(tmp_hardif_neigh,
  498. &hard_iface->neigh_list, list) {
  499. if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
  500. continue;
  501. if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
  502. continue;
  503. hardif_neigh = tmp_hardif_neigh;
  504. break;
  505. }
  506. rcu_read_unlock();
  507. return hardif_neigh;
  508. }
  509. /**
  510. * batadv_neigh_node_create - create a neigh node object
  511. * @orig_node: originator object representing the neighbour
  512. * @hard_iface: the interface where the neighbour is connected to
  513. * @neigh_addr: the mac address of the neighbour interface
  514. *
  515. * Allocates a new neigh_node object and initialises all the generic fields.
  516. *
  517. * Return: the neighbour node if found or created or NULL otherwise.
  518. */
  519. static struct batadv_neigh_node *
  520. batadv_neigh_node_create(struct batadv_orig_node *orig_node,
  521. struct batadv_hard_iface *hard_iface,
  522. const u8 *neigh_addr)
  523. {
  524. struct batadv_neigh_node *neigh_node;
  525. struct batadv_hardif_neigh_node *hardif_neigh = NULL;
  526. spin_lock_bh(&orig_node->neigh_list_lock);
  527. neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
  528. if (neigh_node)
  529. goto out;
  530. hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
  531. neigh_addr, orig_node);
  532. if (!hardif_neigh)
  533. goto out;
  534. neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
  535. if (!neigh_node)
  536. goto out;
  537. INIT_HLIST_NODE(&neigh_node->list);
  538. INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
  539. spin_lock_init(&neigh_node->ifinfo_lock);
  540. kref_get(&hard_iface->refcount);
  541. ether_addr_copy(neigh_node->addr, neigh_addr);
  542. neigh_node->if_incoming = hard_iface;
  543. neigh_node->orig_node = orig_node;
  544. neigh_node->last_seen = jiffies;
  545. /* increment unique neighbor refcount */
  546. kref_get(&hardif_neigh->refcount);
  547. neigh_node->hardif_neigh = hardif_neigh;
  548. /* extra reference for return */
  549. kref_init(&neigh_node->refcount);
  550. kref_get(&neigh_node->refcount);
  551. hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
  552. batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
  553. "Creating new neighbor %pM for orig_node %pM on interface %s\n",
  554. neigh_addr, orig_node->orig, hard_iface->net_dev->name);
  555. out:
  556. spin_unlock_bh(&orig_node->neigh_list_lock);
  557. if (hardif_neigh)
  558. batadv_hardif_neigh_put(hardif_neigh);
  559. return neigh_node;
  560. }
  561. /**
  562. * batadv_neigh_node_get_or_create - retrieve or create a neigh node object
  563. * @orig_node: originator object representing the neighbour
  564. * @hard_iface: the interface where the neighbour is connected to
  565. * @neigh_addr: the mac address of the neighbour interface
  566. *
  567. * Return: the neighbour node if found or created or NULL otherwise.
  568. */
  569. struct batadv_neigh_node *
  570. batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
  571. struct batadv_hard_iface *hard_iface,
  572. const u8 *neigh_addr)
  573. {
  574. struct batadv_neigh_node *neigh_node;
  575. /* first check without locking to avoid the overhead */
  576. neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
  577. if (neigh_node)
  578. return neigh_node;
  579. return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
  580. }
  581. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  582. /**
  583. * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
  584. * @seq: neighbour table seq_file struct
  585. * @offset: not used
  586. *
  587. * Return: always 0
  588. */
  589. int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
  590. {
  591. struct net_device *net_dev = (struct net_device *)seq->private;
  592. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  593. struct batadv_hard_iface *primary_if;
  594. primary_if = batadv_seq_print_text_primary_if_get(seq);
  595. if (!primary_if)
  596. return 0;
  597. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
  598. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  599. primary_if->net_dev->dev_addr, net_dev->name,
  600. bat_priv->algo_ops->name);
  601. batadv_hardif_put(primary_if);
  602. if (!bat_priv->algo_ops->neigh.print) {
  603. seq_puts(seq,
  604. "No printing function for this routing protocol\n");
  605. return 0;
  606. }
  607. bat_priv->algo_ops->neigh.print(bat_priv, seq);
  608. return 0;
  609. }
  610. #endif
  611. /**
  612. * batadv_hardif_neigh_dump - Dump to netlink the neighbor infos for a specific
  613. * outgoing interface
  614. * @msg: message to dump into
  615. * @cb: parameters for the dump
  616. *
  617. * Return: 0 or error value
  618. */
  619. int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
  620. {
  621. struct net *net = sock_net(cb->skb->sk);
  622. struct net_device *soft_iface;
  623. struct net_device *hard_iface = NULL;
  624. struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
  625. struct batadv_priv *bat_priv;
  626. struct batadv_hard_iface *primary_if = NULL;
  627. int ret;
  628. int ifindex, hard_ifindex;
  629. ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
  630. if (!ifindex)
  631. return -EINVAL;
  632. soft_iface = dev_get_by_index(net, ifindex);
  633. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  634. ret = -ENODEV;
  635. goto out;
  636. }
  637. bat_priv = netdev_priv(soft_iface);
  638. primary_if = batadv_primary_if_get_selected(bat_priv);
  639. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  640. ret = -ENOENT;
  641. goto out;
  642. }
  643. hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
  644. BATADV_ATTR_HARD_IFINDEX);
  645. if (hard_ifindex) {
  646. hard_iface = dev_get_by_index(net, hard_ifindex);
  647. if (hard_iface)
  648. hardif = batadv_hardif_get_by_netdev(hard_iface);
  649. if (!hardif) {
  650. ret = -ENODEV;
  651. goto out;
  652. }
  653. if (hardif->soft_iface != soft_iface) {
  654. ret = -ENOENT;
  655. goto out;
  656. }
  657. }
  658. if (!bat_priv->algo_ops->neigh.dump) {
  659. ret = -EOPNOTSUPP;
  660. goto out;
  661. }
  662. bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
  663. ret = msg->len;
  664. out:
  665. if (hardif)
  666. batadv_hardif_put(hardif);
  667. if (hard_iface)
  668. dev_put(hard_iface);
  669. if (primary_if)
  670. batadv_hardif_put(primary_if);
  671. if (soft_iface)
  672. dev_put(soft_iface);
  673. return ret;
  674. }
  675. /**
  676. * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
  677. * free after rcu grace period
  678. * @ref: kref pointer of the orig_ifinfo
  679. */
  680. static void batadv_orig_ifinfo_release(struct kref *ref)
  681. {
  682. struct batadv_orig_ifinfo *orig_ifinfo;
  683. struct batadv_neigh_node *router;
  684. orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
  685. if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
  686. batadv_hardif_put(orig_ifinfo->if_outgoing);
  687. /* this is the last reference to this object */
  688. router = rcu_dereference_protected(orig_ifinfo->router, true);
  689. if (router)
  690. batadv_neigh_node_put(router);
  691. kfree_rcu(orig_ifinfo, rcu);
  692. }
  693. /**
  694. * batadv_orig_ifinfo_put - decrement the refcounter and possibly release
  695. * the orig_ifinfo
  696. * @orig_ifinfo: the orig_ifinfo object to release
  697. */
  698. void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
  699. {
  700. kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
  701. }
  702. /**
  703. * batadv_orig_node_free_rcu - free the orig_node
  704. * @rcu: rcu pointer of the orig_node
  705. */
  706. static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
  707. {
  708. struct batadv_orig_node *orig_node;
  709. orig_node = container_of(rcu, struct batadv_orig_node, rcu);
  710. batadv_mcast_purge_orig(orig_node);
  711. batadv_frag_purge_orig(orig_node, NULL);
  712. if (orig_node->bat_priv->algo_ops->orig.free)
  713. orig_node->bat_priv->algo_ops->orig.free(orig_node);
  714. kfree(orig_node->tt_buff);
  715. kfree(orig_node);
  716. }
  717. /**
  718. * batadv_orig_node_release - release orig_node from lists and queue for
  719. * free after rcu grace period
  720. * @ref: kref pointer of the orig_node
  721. */
  722. static void batadv_orig_node_release(struct kref *ref)
  723. {
  724. struct hlist_node *node_tmp;
  725. struct batadv_neigh_node *neigh_node;
  726. struct batadv_orig_node *orig_node;
  727. struct batadv_orig_ifinfo *orig_ifinfo;
  728. struct batadv_orig_node_vlan *vlan;
  729. struct batadv_orig_ifinfo *last_candidate;
  730. orig_node = container_of(ref, struct batadv_orig_node, refcount);
  731. spin_lock_bh(&orig_node->neigh_list_lock);
  732. /* for all neighbors towards this originator ... */
  733. hlist_for_each_entry_safe(neigh_node, node_tmp,
  734. &orig_node->neigh_list, list) {
  735. hlist_del_rcu(&neigh_node->list);
  736. batadv_neigh_node_put(neigh_node);
  737. }
  738. hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
  739. &orig_node->ifinfo_list, list) {
  740. hlist_del_rcu(&orig_ifinfo->list);
  741. batadv_orig_ifinfo_put(orig_ifinfo);
  742. }
  743. last_candidate = orig_node->last_bonding_candidate;
  744. orig_node->last_bonding_candidate = NULL;
  745. spin_unlock_bh(&orig_node->neigh_list_lock);
  746. if (last_candidate)
  747. batadv_orig_ifinfo_put(last_candidate);
  748. spin_lock_bh(&orig_node->vlan_list_lock);
  749. hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
  750. hlist_del_rcu(&vlan->list);
  751. batadv_orig_node_vlan_put(vlan);
  752. }
  753. spin_unlock_bh(&orig_node->vlan_list_lock);
  754. /* Free nc_nodes */
  755. batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
  756. call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
  757. }
  758. /**
  759. * batadv_orig_node_put - decrement the orig node refcounter and possibly
  760. * release it
  761. * @orig_node: the orig node to free
  762. */
  763. void batadv_orig_node_put(struct batadv_orig_node *orig_node)
  764. {
  765. kref_put(&orig_node->refcount, batadv_orig_node_release);
  766. }
  767. void batadv_originator_free(struct batadv_priv *bat_priv)
  768. {
  769. struct batadv_hashtable *hash = bat_priv->orig_hash;
  770. struct hlist_node *node_tmp;
  771. struct hlist_head *head;
  772. spinlock_t *list_lock; /* spinlock to protect write access */
  773. struct batadv_orig_node *orig_node;
  774. u32 i;
  775. if (!hash)
  776. return;
  777. cancel_delayed_work_sync(&bat_priv->orig_work);
  778. bat_priv->orig_hash = NULL;
  779. for (i = 0; i < hash->size; i++) {
  780. head = &hash->table[i];
  781. list_lock = &hash->list_locks[i];
  782. spin_lock_bh(list_lock);
  783. hlist_for_each_entry_safe(orig_node, node_tmp,
  784. head, hash_entry) {
  785. hlist_del_rcu(&orig_node->hash_entry);
  786. batadv_orig_node_put(orig_node);
  787. }
  788. spin_unlock_bh(list_lock);
  789. }
  790. batadv_hash_destroy(hash);
  791. }
  792. /**
  793. * batadv_orig_node_new - creates a new orig_node
  794. * @bat_priv: the bat priv with all the soft interface information
  795. * @addr: the mac address of the originator
  796. *
  797. * Creates a new originator object and initialise all the generic fields.
  798. * The new object is not added to the originator list.
  799. *
  800. * Return: the newly created object or NULL on failure.
  801. */
  802. struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
  803. const u8 *addr)
  804. {
  805. struct batadv_orig_node *orig_node;
  806. struct batadv_orig_node_vlan *vlan;
  807. unsigned long reset_time;
  808. int i;
  809. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  810. "Creating new originator: %pM\n", addr);
  811. orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
  812. if (!orig_node)
  813. return NULL;
  814. INIT_HLIST_HEAD(&orig_node->neigh_list);
  815. INIT_HLIST_HEAD(&orig_node->vlan_list);
  816. INIT_HLIST_HEAD(&orig_node->ifinfo_list);
  817. spin_lock_init(&orig_node->bcast_seqno_lock);
  818. spin_lock_init(&orig_node->neigh_list_lock);
  819. spin_lock_init(&orig_node->tt_buff_lock);
  820. spin_lock_init(&orig_node->tt_lock);
  821. spin_lock_init(&orig_node->vlan_list_lock);
  822. batadv_nc_init_orig(orig_node);
  823. /* extra reference for return */
  824. kref_init(&orig_node->refcount);
  825. orig_node->bat_priv = bat_priv;
  826. ether_addr_copy(orig_node->orig, addr);
  827. batadv_dat_init_orig_node_addr(orig_node);
  828. atomic_set(&orig_node->last_ttvn, 0);
  829. orig_node->tt_buff = NULL;
  830. orig_node->tt_buff_len = 0;
  831. orig_node->last_seen = jiffies;
  832. reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
  833. orig_node->bcast_seqno_reset = reset_time;
  834. #ifdef CONFIG_BATMAN_ADV_MCAST
  835. orig_node->mcast_flags = BATADV_NO_FLAGS;
  836. INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
  837. INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
  838. INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
  839. spin_lock_init(&orig_node->mcast_handler_lock);
  840. #endif
  841. /* create a vlan object for the "untagged" LAN */
  842. vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
  843. if (!vlan)
  844. goto free_orig_node;
  845. /* batadv_orig_node_vlan_new() increases the refcounter.
  846. * Immediately release vlan since it is not needed anymore in this
  847. * context
  848. */
  849. batadv_orig_node_vlan_put(vlan);
  850. for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
  851. INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
  852. spin_lock_init(&orig_node->fragments[i].lock);
  853. orig_node->fragments[i].size = 0;
  854. }
  855. return orig_node;
  856. free_orig_node:
  857. kfree(orig_node);
  858. return NULL;
  859. }
  860. /**
  861. * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
  862. * @bat_priv: the bat priv with all the soft interface information
  863. * @neigh: orig node which is to be checked
  864. */
  865. static void
  866. batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
  867. struct batadv_neigh_node *neigh)
  868. {
  869. struct batadv_neigh_ifinfo *neigh_ifinfo;
  870. struct batadv_hard_iface *if_outgoing;
  871. struct hlist_node *node_tmp;
  872. spin_lock_bh(&neigh->ifinfo_lock);
  873. /* for all ifinfo objects for this neighinator */
  874. hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
  875. &neigh->ifinfo_list, list) {
  876. if_outgoing = neigh_ifinfo->if_outgoing;
  877. /* always keep the default interface */
  878. if (if_outgoing == BATADV_IF_DEFAULT)
  879. continue;
  880. /* don't purge if the interface is not (going) down */
  881. if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
  882. (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
  883. (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
  884. continue;
  885. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  886. "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
  887. neigh->addr, if_outgoing->net_dev->name);
  888. hlist_del_rcu(&neigh_ifinfo->list);
  889. batadv_neigh_ifinfo_put(neigh_ifinfo);
  890. }
  891. spin_unlock_bh(&neigh->ifinfo_lock);
  892. }
  893. /**
  894. * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
  895. * @bat_priv: the bat priv with all the soft interface information
  896. * @orig_node: orig node which is to be checked
  897. *
  898. * Return: true if any ifinfo entry was purged, false otherwise.
  899. */
  900. static bool
  901. batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
  902. struct batadv_orig_node *orig_node)
  903. {
  904. struct batadv_orig_ifinfo *orig_ifinfo;
  905. struct batadv_hard_iface *if_outgoing;
  906. struct hlist_node *node_tmp;
  907. bool ifinfo_purged = false;
  908. spin_lock_bh(&orig_node->neigh_list_lock);
  909. /* for all ifinfo objects for this originator */
  910. hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
  911. &orig_node->ifinfo_list, list) {
  912. if_outgoing = orig_ifinfo->if_outgoing;
  913. /* always keep the default interface */
  914. if (if_outgoing == BATADV_IF_DEFAULT)
  915. continue;
  916. /* don't purge if the interface is not (going) down */
  917. if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
  918. (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
  919. (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
  920. continue;
  921. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  922. "router/ifinfo purge: originator %pM, iface: %s\n",
  923. orig_node->orig, if_outgoing->net_dev->name);
  924. ifinfo_purged = true;
  925. hlist_del_rcu(&orig_ifinfo->list);
  926. batadv_orig_ifinfo_put(orig_ifinfo);
  927. if (orig_node->last_bonding_candidate == orig_ifinfo) {
  928. orig_node->last_bonding_candidate = NULL;
  929. batadv_orig_ifinfo_put(orig_ifinfo);
  930. }
  931. }
  932. spin_unlock_bh(&orig_node->neigh_list_lock);
  933. return ifinfo_purged;
  934. }
  935. /**
  936. * batadv_purge_orig_neighbors - purges neighbors from originator
  937. * @bat_priv: the bat priv with all the soft interface information
  938. * @orig_node: orig node which is to be checked
  939. *
  940. * Return: true if any neighbor was purged, false otherwise
  941. */
  942. static bool
  943. batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
  944. struct batadv_orig_node *orig_node)
  945. {
  946. struct hlist_node *node_tmp;
  947. struct batadv_neigh_node *neigh_node;
  948. bool neigh_purged = false;
  949. unsigned long last_seen;
  950. struct batadv_hard_iface *if_incoming;
  951. spin_lock_bh(&orig_node->neigh_list_lock);
  952. /* for all neighbors towards this originator ... */
  953. hlist_for_each_entry_safe(neigh_node, node_tmp,
  954. &orig_node->neigh_list, list) {
  955. last_seen = neigh_node->last_seen;
  956. if_incoming = neigh_node->if_incoming;
  957. if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
  958. (if_incoming->if_status == BATADV_IF_INACTIVE) ||
  959. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  960. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
  961. if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
  962. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  963. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
  964. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  965. "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
  966. orig_node->orig, neigh_node->addr,
  967. if_incoming->net_dev->name);
  968. else
  969. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  970. "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
  971. orig_node->orig, neigh_node->addr,
  972. jiffies_to_msecs(last_seen));
  973. neigh_purged = true;
  974. hlist_del_rcu(&neigh_node->list);
  975. batadv_neigh_node_put(neigh_node);
  976. } else {
  977. /* only necessary if not the whole neighbor is to be
  978. * deleted, but some interface has been removed.
  979. */
  980. batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
  981. }
  982. }
  983. spin_unlock_bh(&orig_node->neigh_list_lock);
  984. return neigh_purged;
  985. }
  986. /**
  987. * batadv_find_best_neighbor - finds the best neighbor after purging
  988. * @bat_priv: the bat priv with all the soft interface information
  989. * @orig_node: orig node which is to be checked
  990. * @if_outgoing: the interface for which the metric should be compared
  991. *
  992. * Return: the current best neighbor, with refcount increased.
  993. */
  994. static struct batadv_neigh_node *
  995. batadv_find_best_neighbor(struct batadv_priv *bat_priv,
  996. struct batadv_orig_node *orig_node,
  997. struct batadv_hard_iface *if_outgoing)
  998. {
  999. struct batadv_neigh_node *best = NULL, *neigh;
  1000. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  1001. rcu_read_lock();
  1002. hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
  1003. if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
  1004. if_outgoing) <= 0))
  1005. continue;
  1006. if (!kref_get_unless_zero(&neigh->refcount))
  1007. continue;
  1008. if (best)
  1009. batadv_neigh_node_put(best);
  1010. best = neigh;
  1011. }
  1012. rcu_read_unlock();
  1013. return best;
  1014. }
  1015. /**
  1016. * batadv_purge_orig_node - purges obsolete information from an orig_node
  1017. * @bat_priv: the bat priv with all the soft interface information
  1018. * @orig_node: orig node which is to be checked
  1019. *
  1020. * This function checks if the orig_node or substructures of it have become
  1021. * obsolete, and purges this information if that's the case.
  1022. *
  1023. * Return: true if the orig_node is to be removed, false otherwise.
  1024. */
  1025. static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
  1026. struct batadv_orig_node *orig_node)
  1027. {
  1028. struct batadv_neigh_node *best_neigh_node;
  1029. struct batadv_hard_iface *hard_iface;
  1030. bool changed_ifinfo, changed_neigh;
  1031. if (batadv_has_timed_out(orig_node->last_seen,
  1032. 2 * BATADV_PURGE_TIMEOUT)) {
  1033. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1034. "Originator timeout: originator %pM, last_seen %u\n",
  1035. orig_node->orig,
  1036. jiffies_to_msecs(orig_node->last_seen));
  1037. return true;
  1038. }
  1039. changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
  1040. changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
  1041. if (!changed_ifinfo && !changed_neigh)
  1042. return false;
  1043. /* first for NULL ... */
  1044. best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
  1045. BATADV_IF_DEFAULT);
  1046. batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
  1047. best_neigh_node);
  1048. if (best_neigh_node)
  1049. batadv_neigh_node_put(best_neigh_node);
  1050. /* ... then for all other interfaces. */
  1051. rcu_read_lock();
  1052. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1053. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1054. continue;
  1055. if (hard_iface->soft_iface != bat_priv->soft_iface)
  1056. continue;
  1057. if (!kref_get_unless_zero(&hard_iface->refcount))
  1058. continue;
  1059. best_neigh_node = batadv_find_best_neighbor(bat_priv,
  1060. orig_node,
  1061. hard_iface);
  1062. batadv_update_route(bat_priv, orig_node, hard_iface,
  1063. best_neigh_node);
  1064. if (best_neigh_node)
  1065. batadv_neigh_node_put(best_neigh_node);
  1066. batadv_hardif_put(hard_iface);
  1067. }
  1068. rcu_read_unlock();
  1069. return false;
  1070. }
  1071. static void _batadv_purge_orig(struct batadv_priv *bat_priv)
  1072. {
  1073. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1074. struct hlist_node *node_tmp;
  1075. struct hlist_head *head;
  1076. spinlock_t *list_lock; /* spinlock to protect write access */
  1077. struct batadv_orig_node *orig_node;
  1078. u32 i;
  1079. if (!hash)
  1080. return;
  1081. /* for all origins... */
  1082. for (i = 0; i < hash->size; i++) {
  1083. head = &hash->table[i];
  1084. list_lock = &hash->list_locks[i];
  1085. spin_lock_bh(list_lock);
  1086. hlist_for_each_entry_safe(orig_node, node_tmp,
  1087. head, hash_entry) {
  1088. if (batadv_purge_orig_node(bat_priv, orig_node)) {
  1089. batadv_gw_node_delete(bat_priv, orig_node);
  1090. hlist_del_rcu(&orig_node->hash_entry);
  1091. batadv_tt_global_del_orig(orig_node->bat_priv,
  1092. orig_node, -1,
  1093. "originator timed out");
  1094. batadv_orig_node_put(orig_node);
  1095. continue;
  1096. }
  1097. batadv_frag_purge_orig(orig_node,
  1098. batadv_frag_check_entry);
  1099. }
  1100. spin_unlock_bh(list_lock);
  1101. }
  1102. batadv_gw_election(bat_priv);
  1103. }
  1104. static void batadv_purge_orig(struct work_struct *work)
  1105. {
  1106. struct delayed_work *delayed_work;
  1107. struct batadv_priv *bat_priv;
  1108. delayed_work = to_delayed_work(work);
  1109. bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
  1110. _batadv_purge_orig(bat_priv);
  1111. queue_delayed_work(batadv_event_workqueue,
  1112. &bat_priv->orig_work,
  1113. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  1114. }
  1115. void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
  1116. {
  1117. _batadv_purge_orig(bat_priv);
  1118. }
  1119. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1120. int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
  1121. {
  1122. struct net_device *net_dev = (struct net_device *)seq->private;
  1123. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1124. struct batadv_hard_iface *primary_if;
  1125. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1126. if (!primary_if)
  1127. return 0;
  1128. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
  1129. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  1130. primary_if->net_dev->dev_addr, net_dev->name,
  1131. bat_priv->algo_ops->name);
  1132. batadv_hardif_put(primary_if);
  1133. if (!bat_priv->algo_ops->orig.print) {
  1134. seq_puts(seq,
  1135. "No printing function for this routing protocol\n");
  1136. return 0;
  1137. }
  1138. bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
  1139. return 0;
  1140. }
  1141. /**
  1142. * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
  1143. * outgoing interface
  1144. * @seq: debugfs table seq_file struct
  1145. * @offset: not used
  1146. *
  1147. * Return: 0
  1148. */
  1149. int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
  1150. {
  1151. struct net_device *net_dev = (struct net_device *)seq->private;
  1152. struct batadv_hard_iface *hard_iface;
  1153. struct batadv_priv *bat_priv;
  1154. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  1155. if (!hard_iface || !hard_iface->soft_iface) {
  1156. seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
  1157. goto out;
  1158. }
  1159. bat_priv = netdev_priv(hard_iface->soft_iface);
  1160. if (!bat_priv->algo_ops->orig.print) {
  1161. seq_puts(seq,
  1162. "No printing function for this routing protocol\n");
  1163. goto out;
  1164. }
  1165. if (hard_iface->if_status != BATADV_IF_ACTIVE) {
  1166. seq_puts(seq, "Interface not active\n");
  1167. goto out;
  1168. }
  1169. seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
  1170. BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
  1171. hard_iface->net_dev->dev_addr,
  1172. hard_iface->soft_iface->name, bat_priv->algo_ops->name);
  1173. bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
  1174. out:
  1175. if (hard_iface)
  1176. batadv_hardif_put(hard_iface);
  1177. return 0;
  1178. }
  1179. #endif
  1180. /**
  1181. * batadv_orig_dump - Dump to netlink the originator infos for a specific
  1182. * outgoing interface
  1183. * @msg: message to dump into
  1184. * @cb: parameters for the dump
  1185. *
  1186. * Return: 0 or error value
  1187. */
  1188. int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1189. {
  1190. struct net *net = sock_net(cb->skb->sk);
  1191. struct net_device *soft_iface;
  1192. struct net_device *hard_iface = NULL;
  1193. struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
  1194. struct batadv_priv *bat_priv;
  1195. struct batadv_hard_iface *primary_if = NULL;
  1196. int ret;
  1197. int ifindex, hard_ifindex;
  1198. ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
  1199. if (!ifindex)
  1200. return -EINVAL;
  1201. soft_iface = dev_get_by_index(net, ifindex);
  1202. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1203. ret = -ENODEV;
  1204. goto out;
  1205. }
  1206. bat_priv = netdev_priv(soft_iface);
  1207. primary_if = batadv_primary_if_get_selected(bat_priv);
  1208. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1209. ret = -ENOENT;
  1210. goto out;
  1211. }
  1212. hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
  1213. BATADV_ATTR_HARD_IFINDEX);
  1214. if (hard_ifindex) {
  1215. hard_iface = dev_get_by_index(net, hard_ifindex);
  1216. if (hard_iface)
  1217. hardif = batadv_hardif_get_by_netdev(hard_iface);
  1218. if (!hardif) {
  1219. ret = -ENODEV;
  1220. goto out;
  1221. }
  1222. if (hardif->soft_iface != soft_iface) {
  1223. ret = -ENOENT;
  1224. goto out;
  1225. }
  1226. }
  1227. if (!bat_priv->algo_ops->orig.dump) {
  1228. ret = -EOPNOTSUPP;
  1229. goto out;
  1230. }
  1231. bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
  1232. ret = msg->len;
  1233. out:
  1234. if (hardif)
  1235. batadv_hardif_put(hardif);
  1236. if (hard_iface)
  1237. dev_put(hard_iface);
  1238. if (primary_if)
  1239. batadv_hardif_put(primary_if);
  1240. if (soft_iface)
  1241. dev_put(soft_iface);
  1242. return ret;
  1243. }
  1244. int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
  1245. int max_if_num)
  1246. {
  1247. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  1248. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  1249. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1250. struct hlist_head *head;
  1251. struct batadv_orig_node *orig_node;
  1252. u32 i;
  1253. int ret;
  1254. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  1255. * if_num
  1256. */
  1257. for (i = 0; i < hash->size; i++) {
  1258. head = &hash->table[i];
  1259. rcu_read_lock();
  1260. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1261. ret = 0;
  1262. if (bao->orig.add_if)
  1263. ret = bao->orig.add_if(orig_node, max_if_num);
  1264. if (ret == -ENOMEM)
  1265. goto err;
  1266. }
  1267. rcu_read_unlock();
  1268. }
  1269. return 0;
  1270. err:
  1271. rcu_read_unlock();
  1272. return -ENOMEM;
  1273. }
  1274. int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
  1275. int max_if_num)
  1276. {
  1277. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  1278. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1279. struct hlist_head *head;
  1280. struct batadv_hard_iface *hard_iface_tmp;
  1281. struct batadv_orig_node *orig_node;
  1282. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  1283. u32 i;
  1284. int ret;
  1285. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  1286. * if_num
  1287. */
  1288. for (i = 0; i < hash->size; i++) {
  1289. head = &hash->table[i];
  1290. rcu_read_lock();
  1291. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1292. ret = 0;
  1293. if (bao->orig.del_if)
  1294. ret = bao->orig.del_if(orig_node, max_if_num,
  1295. hard_iface->if_num);
  1296. if (ret == -ENOMEM)
  1297. goto err;
  1298. }
  1299. rcu_read_unlock();
  1300. }
  1301. /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
  1302. rcu_read_lock();
  1303. list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
  1304. if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
  1305. continue;
  1306. if (hard_iface == hard_iface_tmp)
  1307. continue;
  1308. if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
  1309. continue;
  1310. if (hard_iface_tmp->if_num > hard_iface->if_num)
  1311. hard_iface_tmp->if_num--;
  1312. }
  1313. rcu_read_unlock();
  1314. hard_iface->if_num = -1;
  1315. return 0;
  1316. err:
  1317. rcu_read_unlock();
  1318. return -ENOMEM;
  1319. }