originator.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /* Copyright (C) 2009-2014 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 "main.h"
  18. #include "distributed-arp-table.h"
  19. #include "originator.h"
  20. #include "hash.h"
  21. #include "translation-table.h"
  22. #include "routing.h"
  23. #include "gateway_client.h"
  24. #include "hard-interface.h"
  25. #include "soft-interface.h"
  26. #include "bridge_loop_avoidance.h"
  27. #include "network-coding.h"
  28. #include "fragmentation.h"
  29. #include "multicast.h"
  30. /* hash class keys */
  31. static struct lock_class_key batadv_orig_hash_lock_class_key;
  32. static void batadv_purge_orig(struct work_struct *work);
  33. /* returns 1 if they are the same originator */
  34. int batadv_compare_orig(const struct hlist_node *node, const void *data2)
  35. {
  36. const void *data1 = container_of(node, struct batadv_orig_node,
  37. hash_entry);
  38. return batadv_compare_eth(data1, data2);
  39. }
  40. /**
  41. * batadv_orig_node_vlan_get - get an orig_node_vlan object
  42. * @orig_node: the originator serving the VLAN
  43. * @vid: the VLAN identifier
  44. *
  45. * Returns the vlan object identified by vid and belonging to orig_node or NULL
  46. * if it does not exist.
  47. */
  48. struct batadv_orig_node_vlan *
  49. batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
  50. unsigned short vid)
  51. {
  52. struct batadv_orig_node_vlan *vlan = NULL, *tmp;
  53. rcu_read_lock();
  54. list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
  55. if (tmp->vid != vid)
  56. continue;
  57. if (!atomic_inc_not_zero(&tmp->refcount))
  58. continue;
  59. vlan = tmp;
  60. break;
  61. }
  62. rcu_read_unlock();
  63. return vlan;
  64. }
  65. /**
  66. * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
  67. * object
  68. * @orig_node: the originator serving the VLAN
  69. * @vid: the VLAN identifier
  70. *
  71. * Returns NULL in case of failure or the vlan object identified by vid and
  72. * belonging to orig_node otherwise. The object is created and added to the list
  73. * if it does not exist.
  74. *
  75. * The object is returned with refcounter increased by 1.
  76. */
  77. struct batadv_orig_node_vlan *
  78. batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
  79. unsigned short vid)
  80. {
  81. struct batadv_orig_node_vlan *vlan;
  82. spin_lock_bh(&orig_node->vlan_list_lock);
  83. /* first look if an object for this vid already exists */
  84. vlan = batadv_orig_node_vlan_get(orig_node, vid);
  85. if (vlan)
  86. goto out;
  87. vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
  88. if (!vlan)
  89. goto out;
  90. atomic_set(&vlan->refcount, 2);
  91. vlan->vid = vid;
  92. list_add_rcu(&vlan->list, &orig_node->vlan_list);
  93. out:
  94. spin_unlock_bh(&orig_node->vlan_list_lock);
  95. return vlan;
  96. }
  97. /**
  98. * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
  99. * the originator-vlan object
  100. * @orig_vlan: the originator-vlan object to release
  101. */
  102. void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
  103. {
  104. if (atomic_dec_and_test(&orig_vlan->refcount))
  105. kfree_rcu(orig_vlan, rcu);
  106. }
  107. int batadv_originator_init(struct batadv_priv *bat_priv)
  108. {
  109. if (bat_priv->orig_hash)
  110. return 0;
  111. bat_priv->orig_hash = batadv_hash_new(1024);
  112. if (!bat_priv->orig_hash)
  113. goto err;
  114. batadv_hash_set_lock_class(bat_priv->orig_hash,
  115. &batadv_orig_hash_lock_class_key);
  116. INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
  117. queue_delayed_work(batadv_event_workqueue,
  118. &bat_priv->orig_work,
  119. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  120. return 0;
  121. err:
  122. return -ENOMEM;
  123. }
  124. /**
  125. * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
  126. * @rcu: rcu pointer of the neigh_ifinfo object
  127. */
  128. static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
  129. {
  130. struct batadv_neigh_ifinfo *neigh_ifinfo;
  131. neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
  132. if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
  133. batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
  134. kfree(neigh_ifinfo);
  135. }
  136. /**
  137. * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
  138. * the neigh_ifinfo (without rcu callback)
  139. * @neigh_ifinfo: the neigh_ifinfo object to release
  140. */
  141. static void
  142. batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
  143. {
  144. if (atomic_dec_and_test(&neigh_ifinfo->refcount))
  145. batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
  146. }
  147. /**
  148. * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
  149. * the neigh_ifinfo
  150. * @neigh_ifinfo: the neigh_ifinfo object to release
  151. */
  152. void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
  153. {
  154. if (atomic_dec_and_test(&neigh_ifinfo->refcount))
  155. call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
  156. }
  157. /**
  158. * batadv_neigh_node_free_rcu - free the neigh_node
  159. * @rcu: rcu pointer of the neigh_node
  160. */
  161. static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
  162. {
  163. struct hlist_node *node_tmp;
  164. struct batadv_neigh_node *neigh_node;
  165. struct batadv_neigh_ifinfo *neigh_ifinfo;
  166. neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
  167. hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
  168. &neigh_node->ifinfo_list, list) {
  169. batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
  170. }
  171. batadv_hardif_free_ref_now(neigh_node->if_incoming);
  172. kfree(neigh_node);
  173. }
  174. /**
  175. * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
  176. * and possibly free it (without rcu callback)
  177. * @neigh_node: neigh neighbor to free
  178. */
  179. static void
  180. batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
  181. {
  182. if (atomic_dec_and_test(&neigh_node->refcount))
  183. batadv_neigh_node_free_rcu(&neigh_node->rcu);
  184. }
  185. /**
  186. * batadv_neigh_node_free_ref - decrement the neighbors refcounter
  187. * and possibly free it
  188. * @neigh_node: neigh neighbor to free
  189. */
  190. void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
  191. {
  192. if (atomic_dec_and_test(&neigh_node->refcount))
  193. call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
  194. }
  195. /**
  196. * batadv_orig_node_get_router - router to the originator depending on iface
  197. * @orig_node: the orig node for the router
  198. * @if_outgoing: the interface where the payload packet has been received or
  199. * the OGM should be sent to
  200. *
  201. * Returns the neighbor which should be router for this orig_node/iface.
  202. *
  203. * The object is returned with refcounter increased by 1.
  204. */
  205. struct batadv_neigh_node *
  206. batadv_orig_router_get(struct batadv_orig_node *orig_node,
  207. const struct batadv_hard_iface *if_outgoing)
  208. {
  209. struct batadv_orig_ifinfo *orig_ifinfo;
  210. struct batadv_neigh_node *router = NULL;
  211. rcu_read_lock();
  212. hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
  213. if (orig_ifinfo->if_outgoing != if_outgoing)
  214. continue;
  215. router = rcu_dereference(orig_ifinfo->router);
  216. break;
  217. }
  218. if (router && !atomic_inc_not_zero(&router->refcount))
  219. router = NULL;
  220. rcu_read_unlock();
  221. return router;
  222. }
  223. /**
  224. * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
  225. * @orig_node: the orig node to be queried
  226. * @if_outgoing: the interface for which the ifinfo should be acquired
  227. *
  228. * Returns the requested orig_ifinfo or NULL if not found.
  229. *
  230. * The object is returned with refcounter increased by 1.
  231. */
  232. struct batadv_orig_ifinfo *
  233. batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
  234. struct batadv_hard_iface *if_outgoing)
  235. {
  236. struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
  237. rcu_read_lock();
  238. hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
  239. list) {
  240. if (tmp->if_outgoing != if_outgoing)
  241. continue;
  242. if (!atomic_inc_not_zero(&tmp->refcount))
  243. continue;
  244. orig_ifinfo = tmp;
  245. break;
  246. }
  247. rcu_read_unlock();
  248. return orig_ifinfo;
  249. }
  250. /**
  251. * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
  252. * @orig_node: the orig node to be queried
  253. * @if_outgoing: the interface for which the ifinfo should be acquired
  254. *
  255. * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
  256. * interface otherwise. The object is created and added to the list
  257. * if it does not exist.
  258. *
  259. * The object is returned with refcounter increased by 1.
  260. */
  261. struct batadv_orig_ifinfo *
  262. batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
  263. struct batadv_hard_iface *if_outgoing)
  264. {
  265. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  266. unsigned long reset_time;
  267. spin_lock_bh(&orig_node->neigh_list_lock);
  268. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
  269. if (orig_ifinfo)
  270. goto out;
  271. orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
  272. if (!orig_ifinfo)
  273. goto out;
  274. if (if_outgoing != BATADV_IF_DEFAULT &&
  275. !atomic_inc_not_zero(&if_outgoing->refcount)) {
  276. kfree(orig_ifinfo);
  277. orig_ifinfo = NULL;
  278. goto out;
  279. }
  280. reset_time = jiffies - 1;
  281. reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
  282. orig_ifinfo->batman_seqno_reset = reset_time;
  283. orig_ifinfo->if_outgoing = if_outgoing;
  284. INIT_HLIST_NODE(&orig_ifinfo->list);
  285. atomic_set(&orig_ifinfo->refcount, 2);
  286. hlist_add_head_rcu(&orig_ifinfo->list,
  287. &orig_node->ifinfo_list);
  288. out:
  289. spin_unlock_bh(&orig_node->neigh_list_lock);
  290. return orig_ifinfo;
  291. }
  292. /**
  293. * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
  294. * @neigh_node: the neigh node to be queried
  295. * @if_outgoing: the interface for which the ifinfo should be acquired
  296. *
  297. * The object is returned with refcounter increased by 1.
  298. *
  299. * Returns the requested neigh_ifinfo or NULL if not found
  300. */
  301. struct batadv_neigh_ifinfo *
  302. batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
  303. struct batadv_hard_iface *if_outgoing)
  304. {
  305. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
  306. *tmp_neigh_ifinfo;
  307. rcu_read_lock();
  308. hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
  309. list) {
  310. if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
  311. continue;
  312. if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
  313. continue;
  314. neigh_ifinfo = tmp_neigh_ifinfo;
  315. break;
  316. }
  317. rcu_read_unlock();
  318. return neigh_ifinfo;
  319. }
  320. /**
  321. * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
  322. * @neigh_node: the neigh node to be queried
  323. * @if_outgoing: the interface for which the ifinfo should be acquired
  324. *
  325. * Returns NULL in case of failure or the neigh_ifinfo object for the
  326. * if_outgoing interface otherwise. The object is created and added to the list
  327. * if it does not exist.
  328. *
  329. * The object is returned with refcounter increased by 1.
  330. */
  331. struct batadv_neigh_ifinfo *
  332. batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
  333. struct batadv_hard_iface *if_outgoing)
  334. {
  335. struct batadv_neigh_ifinfo *neigh_ifinfo;
  336. spin_lock_bh(&neigh->ifinfo_lock);
  337. neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
  338. if (neigh_ifinfo)
  339. goto out;
  340. neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
  341. if (!neigh_ifinfo)
  342. goto out;
  343. if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
  344. kfree(neigh_ifinfo);
  345. neigh_ifinfo = NULL;
  346. goto out;
  347. }
  348. INIT_HLIST_NODE(&neigh_ifinfo->list);
  349. atomic_set(&neigh_ifinfo->refcount, 2);
  350. neigh_ifinfo->if_outgoing = if_outgoing;
  351. hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
  352. out:
  353. spin_unlock_bh(&neigh->ifinfo_lock);
  354. return neigh_ifinfo;
  355. }
  356. /**
  357. * batadv_neigh_node_new - create and init a new neigh_node object
  358. * @hard_iface: the interface where the neighbour is connected to
  359. * @neigh_addr: the mac address of the neighbour interface
  360. * @orig_node: originator object representing the neighbour
  361. *
  362. * Allocates a new neigh_node object and initialises all the generic fields.
  363. * Returns the new object or NULL on failure.
  364. */
  365. struct batadv_neigh_node *
  366. batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
  367. const uint8_t *neigh_addr,
  368. struct batadv_orig_node *orig_node)
  369. {
  370. struct batadv_neigh_node *neigh_node;
  371. neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
  372. if (!neigh_node)
  373. goto out;
  374. INIT_HLIST_NODE(&neigh_node->list);
  375. INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
  376. spin_lock_init(&neigh_node->ifinfo_lock);
  377. ether_addr_copy(neigh_node->addr, neigh_addr);
  378. neigh_node->if_incoming = hard_iface;
  379. neigh_node->orig_node = orig_node;
  380. /* extra reference for return */
  381. atomic_set(&neigh_node->refcount, 2);
  382. out:
  383. return neigh_node;
  384. }
  385. /**
  386. * batadv_neigh_node_get - retrieve a neighbour from the list
  387. * @orig_node: originator which the neighbour belongs to
  388. * @hard_iface: the interface where this neighbour is connected to
  389. * @addr: the address of the neighbour
  390. *
  391. * Looks for and possibly returns a neighbour belonging to this originator list
  392. * which is connected through the provided hard interface.
  393. * Returns NULL if the neighbour is not found.
  394. */
  395. struct batadv_neigh_node *
  396. batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
  397. const struct batadv_hard_iface *hard_iface,
  398. const uint8_t *addr)
  399. {
  400. struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
  401. rcu_read_lock();
  402. hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
  403. if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
  404. continue;
  405. if (tmp_neigh_node->if_incoming != hard_iface)
  406. continue;
  407. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  408. continue;
  409. res = tmp_neigh_node;
  410. break;
  411. }
  412. rcu_read_unlock();
  413. return res;
  414. }
  415. /**
  416. * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
  417. * @rcu: rcu pointer of the orig_ifinfo object
  418. */
  419. static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
  420. {
  421. struct batadv_orig_ifinfo *orig_ifinfo;
  422. struct batadv_neigh_node *router;
  423. orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
  424. if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
  425. batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
  426. /* this is the last reference to this object */
  427. router = rcu_dereference_protected(orig_ifinfo->router, true);
  428. if (router)
  429. batadv_neigh_node_free_ref_now(router);
  430. kfree(orig_ifinfo);
  431. }
  432. /**
  433. * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
  434. * the orig_ifinfo (without rcu callback)
  435. * @orig_ifinfo: the orig_ifinfo object to release
  436. */
  437. static void
  438. batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
  439. {
  440. if (atomic_dec_and_test(&orig_ifinfo->refcount))
  441. batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
  442. }
  443. /**
  444. * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
  445. * the orig_ifinfo
  446. * @orig_ifinfo: the orig_ifinfo object to release
  447. */
  448. void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
  449. {
  450. if (atomic_dec_and_test(&orig_ifinfo->refcount))
  451. call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
  452. }
  453. static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
  454. {
  455. struct hlist_node *node_tmp;
  456. struct batadv_neigh_node *neigh_node;
  457. struct batadv_orig_node *orig_node;
  458. struct batadv_orig_ifinfo *orig_ifinfo;
  459. orig_node = container_of(rcu, struct batadv_orig_node, rcu);
  460. spin_lock_bh(&orig_node->neigh_list_lock);
  461. /* for all neighbors towards this originator ... */
  462. hlist_for_each_entry_safe(neigh_node, node_tmp,
  463. &orig_node->neigh_list, list) {
  464. hlist_del_rcu(&neigh_node->list);
  465. batadv_neigh_node_free_ref_now(neigh_node);
  466. }
  467. hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
  468. &orig_node->ifinfo_list, list) {
  469. hlist_del_rcu(&orig_ifinfo->list);
  470. batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
  471. }
  472. spin_unlock_bh(&orig_node->neigh_list_lock);
  473. batadv_mcast_purge_orig(orig_node);
  474. /* Free nc_nodes */
  475. batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
  476. batadv_frag_purge_orig(orig_node, NULL);
  477. batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
  478. "originator timed out");
  479. if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
  480. orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
  481. kfree(orig_node->tt_buff);
  482. kfree(orig_node);
  483. }
  484. /**
  485. * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
  486. * schedule an rcu callback for freeing it
  487. * @orig_node: the orig node to free
  488. */
  489. void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
  490. {
  491. if (atomic_dec_and_test(&orig_node->refcount))
  492. call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
  493. }
  494. /**
  495. * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
  496. * possibly free it (without rcu callback)
  497. * @orig_node: the orig node to free
  498. */
  499. void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
  500. {
  501. if (atomic_dec_and_test(&orig_node->refcount))
  502. batadv_orig_node_free_rcu(&orig_node->rcu);
  503. }
  504. void batadv_originator_free(struct batadv_priv *bat_priv)
  505. {
  506. struct batadv_hashtable *hash = bat_priv->orig_hash;
  507. struct hlist_node *node_tmp;
  508. struct hlist_head *head;
  509. spinlock_t *list_lock; /* spinlock to protect write access */
  510. struct batadv_orig_node *orig_node;
  511. uint32_t i;
  512. if (!hash)
  513. return;
  514. cancel_delayed_work_sync(&bat_priv->orig_work);
  515. bat_priv->orig_hash = NULL;
  516. for (i = 0; i < hash->size; i++) {
  517. head = &hash->table[i];
  518. list_lock = &hash->list_locks[i];
  519. spin_lock_bh(list_lock);
  520. hlist_for_each_entry_safe(orig_node, node_tmp,
  521. head, hash_entry) {
  522. hlist_del_rcu(&orig_node->hash_entry);
  523. batadv_orig_node_free_ref(orig_node);
  524. }
  525. spin_unlock_bh(list_lock);
  526. }
  527. batadv_hash_destroy(hash);
  528. }
  529. /**
  530. * batadv_orig_node_new - creates a new orig_node
  531. * @bat_priv: the bat priv with all the soft interface information
  532. * @addr: the mac address of the originator
  533. *
  534. * Creates a new originator object and initialise all the generic fields.
  535. * The new object is not added to the originator list.
  536. * Returns the newly created object or NULL on failure.
  537. */
  538. struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
  539. const uint8_t *addr)
  540. {
  541. struct batadv_orig_node *orig_node;
  542. struct batadv_orig_node_vlan *vlan;
  543. unsigned long reset_time;
  544. int i;
  545. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  546. "Creating new originator: %pM\n", addr);
  547. orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
  548. if (!orig_node)
  549. return NULL;
  550. INIT_HLIST_HEAD(&orig_node->neigh_list);
  551. INIT_LIST_HEAD(&orig_node->vlan_list);
  552. INIT_HLIST_HEAD(&orig_node->ifinfo_list);
  553. spin_lock_init(&orig_node->bcast_seqno_lock);
  554. spin_lock_init(&orig_node->neigh_list_lock);
  555. spin_lock_init(&orig_node->tt_buff_lock);
  556. spin_lock_init(&orig_node->tt_lock);
  557. spin_lock_init(&orig_node->vlan_list_lock);
  558. batadv_nc_init_orig(orig_node);
  559. /* extra reference for return */
  560. atomic_set(&orig_node->refcount, 2);
  561. orig_node->bat_priv = bat_priv;
  562. ether_addr_copy(orig_node->orig, addr);
  563. batadv_dat_init_orig_node_addr(orig_node);
  564. atomic_set(&orig_node->last_ttvn, 0);
  565. orig_node->tt_buff = NULL;
  566. orig_node->tt_buff_len = 0;
  567. reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
  568. orig_node->bcast_seqno_reset = reset_time;
  569. #ifdef CONFIG_BATMAN_ADV_MCAST
  570. orig_node->mcast_flags = BATADV_NO_FLAGS;
  571. #endif
  572. /* create a vlan object for the "untagged" LAN */
  573. vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
  574. if (!vlan)
  575. goto free_orig_node;
  576. /* batadv_orig_node_vlan_new() increases the refcounter.
  577. * Immediately release vlan since it is not needed anymore in this
  578. * context
  579. */
  580. batadv_orig_node_vlan_free_ref(vlan);
  581. for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
  582. INIT_HLIST_HEAD(&orig_node->fragments[i].head);
  583. spin_lock_init(&orig_node->fragments[i].lock);
  584. orig_node->fragments[i].size = 0;
  585. }
  586. return orig_node;
  587. free_orig_node:
  588. kfree(orig_node);
  589. return NULL;
  590. }
  591. /**
  592. * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
  593. * @bat_priv: the bat priv with all the soft interface information
  594. * @neigh: orig node which is to be checked
  595. */
  596. static void
  597. batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
  598. struct batadv_neigh_node *neigh)
  599. {
  600. struct batadv_neigh_ifinfo *neigh_ifinfo;
  601. struct batadv_hard_iface *if_outgoing;
  602. struct hlist_node *node_tmp;
  603. spin_lock_bh(&neigh->ifinfo_lock);
  604. /* for all ifinfo objects for this neighinator */
  605. hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
  606. &neigh->ifinfo_list, list) {
  607. if_outgoing = neigh_ifinfo->if_outgoing;
  608. /* always keep the default interface */
  609. if (if_outgoing == BATADV_IF_DEFAULT)
  610. continue;
  611. /* don't purge if the interface is not (going) down */
  612. if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
  613. (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
  614. (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
  615. continue;
  616. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  617. "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
  618. neigh->addr, if_outgoing->net_dev->name);
  619. hlist_del_rcu(&neigh_ifinfo->list);
  620. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  621. }
  622. spin_unlock_bh(&neigh->ifinfo_lock);
  623. }
  624. /**
  625. * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
  626. * @bat_priv: the bat priv with all the soft interface information
  627. * @orig_node: orig node which is to be checked
  628. *
  629. * Returns true if any ifinfo entry was purged, false otherwise.
  630. */
  631. static bool
  632. batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
  633. struct batadv_orig_node *orig_node)
  634. {
  635. struct batadv_orig_ifinfo *orig_ifinfo;
  636. struct batadv_hard_iface *if_outgoing;
  637. struct hlist_node *node_tmp;
  638. bool ifinfo_purged = false;
  639. spin_lock_bh(&orig_node->neigh_list_lock);
  640. /* for all ifinfo objects for this originator */
  641. hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
  642. &orig_node->ifinfo_list, list) {
  643. if_outgoing = orig_ifinfo->if_outgoing;
  644. /* always keep the default interface */
  645. if (if_outgoing == BATADV_IF_DEFAULT)
  646. continue;
  647. /* don't purge if the interface is not (going) down */
  648. if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
  649. (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
  650. (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
  651. continue;
  652. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  653. "router/ifinfo purge: originator %pM, iface: %s\n",
  654. orig_node->orig, if_outgoing->net_dev->name);
  655. ifinfo_purged = true;
  656. hlist_del_rcu(&orig_ifinfo->list);
  657. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  658. if (orig_node->last_bonding_candidate == orig_ifinfo) {
  659. orig_node->last_bonding_candidate = NULL;
  660. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  661. }
  662. }
  663. spin_unlock_bh(&orig_node->neigh_list_lock);
  664. return ifinfo_purged;
  665. }
  666. /**
  667. * batadv_purge_orig_neighbors - purges neighbors from originator
  668. * @bat_priv: the bat priv with all the soft interface information
  669. * @orig_node: orig node which is to be checked
  670. *
  671. * Returns true if any neighbor was purged, false otherwise
  672. */
  673. static bool
  674. batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
  675. struct batadv_orig_node *orig_node)
  676. {
  677. struct hlist_node *node_tmp;
  678. struct batadv_neigh_node *neigh_node;
  679. bool neigh_purged = false;
  680. unsigned long last_seen;
  681. struct batadv_hard_iface *if_incoming;
  682. spin_lock_bh(&orig_node->neigh_list_lock);
  683. /* for all neighbors towards this originator ... */
  684. hlist_for_each_entry_safe(neigh_node, node_tmp,
  685. &orig_node->neigh_list, list) {
  686. last_seen = neigh_node->last_seen;
  687. if_incoming = neigh_node->if_incoming;
  688. if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
  689. (if_incoming->if_status == BATADV_IF_INACTIVE) ||
  690. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  691. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
  692. if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
  693. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  694. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
  695. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  696. "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
  697. orig_node->orig, neigh_node->addr,
  698. if_incoming->net_dev->name);
  699. else
  700. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  701. "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
  702. orig_node->orig, neigh_node->addr,
  703. jiffies_to_msecs(last_seen));
  704. neigh_purged = true;
  705. hlist_del_rcu(&neigh_node->list);
  706. batadv_neigh_node_free_ref(neigh_node);
  707. } else {
  708. /* only necessary if not the whole neighbor is to be
  709. * deleted, but some interface has been removed.
  710. */
  711. batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
  712. }
  713. }
  714. spin_unlock_bh(&orig_node->neigh_list_lock);
  715. return neigh_purged;
  716. }
  717. /**
  718. * batadv_find_best_neighbor - finds the best neighbor after purging
  719. * @bat_priv: the bat priv with all the soft interface information
  720. * @orig_node: orig node which is to be checked
  721. * @if_outgoing: the interface for which the metric should be compared
  722. *
  723. * Returns the current best neighbor, with refcount increased.
  724. */
  725. static struct batadv_neigh_node *
  726. batadv_find_best_neighbor(struct batadv_priv *bat_priv,
  727. struct batadv_orig_node *orig_node,
  728. struct batadv_hard_iface *if_outgoing)
  729. {
  730. struct batadv_neigh_node *best = NULL, *neigh;
  731. struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
  732. rcu_read_lock();
  733. hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
  734. if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
  735. best, if_outgoing) <= 0))
  736. continue;
  737. if (!atomic_inc_not_zero(&neigh->refcount))
  738. continue;
  739. if (best)
  740. batadv_neigh_node_free_ref(best);
  741. best = neigh;
  742. }
  743. rcu_read_unlock();
  744. return best;
  745. }
  746. /**
  747. * batadv_purge_orig_node - purges obsolete information from an orig_node
  748. * @bat_priv: the bat priv with all the soft interface information
  749. * @orig_node: orig node which is to be checked
  750. *
  751. * This function checks if the orig_node or substructures of it have become
  752. * obsolete, and purges this information if that's the case.
  753. *
  754. * Returns true if the orig_node is to be removed, false otherwise.
  755. */
  756. static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
  757. struct batadv_orig_node *orig_node)
  758. {
  759. struct batadv_neigh_node *best_neigh_node;
  760. struct batadv_hard_iface *hard_iface;
  761. bool changed_ifinfo, changed_neigh;
  762. if (batadv_has_timed_out(orig_node->last_seen,
  763. 2 * BATADV_PURGE_TIMEOUT)) {
  764. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  765. "Originator timeout: originator %pM, last_seen %u\n",
  766. orig_node->orig,
  767. jiffies_to_msecs(orig_node->last_seen));
  768. return true;
  769. }
  770. changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
  771. changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
  772. if (!changed_ifinfo && !changed_neigh)
  773. return false;
  774. /* first for NULL ... */
  775. best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
  776. BATADV_IF_DEFAULT);
  777. batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
  778. best_neigh_node);
  779. if (best_neigh_node)
  780. batadv_neigh_node_free_ref(best_neigh_node);
  781. /* ... then for all other interfaces. */
  782. rcu_read_lock();
  783. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  784. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  785. continue;
  786. if (hard_iface->soft_iface != bat_priv->soft_iface)
  787. continue;
  788. best_neigh_node = batadv_find_best_neighbor(bat_priv,
  789. orig_node,
  790. hard_iface);
  791. batadv_update_route(bat_priv, orig_node, hard_iface,
  792. best_neigh_node);
  793. if (best_neigh_node)
  794. batadv_neigh_node_free_ref(best_neigh_node);
  795. }
  796. rcu_read_unlock();
  797. return false;
  798. }
  799. static void _batadv_purge_orig(struct batadv_priv *bat_priv)
  800. {
  801. struct batadv_hashtable *hash = bat_priv->orig_hash;
  802. struct hlist_node *node_tmp;
  803. struct hlist_head *head;
  804. spinlock_t *list_lock; /* spinlock to protect write access */
  805. struct batadv_orig_node *orig_node;
  806. uint32_t i;
  807. if (!hash)
  808. return;
  809. /* for all origins... */
  810. for (i = 0; i < hash->size; i++) {
  811. head = &hash->table[i];
  812. list_lock = &hash->list_locks[i];
  813. spin_lock_bh(list_lock);
  814. hlist_for_each_entry_safe(orig_node, node_tmp,
  815. head, hash_entry) {
  816. if (batadv_purge_orig_node(bat_priv, orig_node)) {
  817. batadv_gw_node_delete(bat_priv, orig_node);
  818. hlist_del_rcu(&orig_node->hash_entry);
  819. batadv_orig_node_free_ref(orig_node);
  820. continue;
  821. }
  822. batadv_frag_purge_orig(orig_node,
  823. batadv_frag_check_entry);
  824. }
  825. spin_unlock_bh(list_lock);
  826. }
  827. batadv_gw_node_purge(bat_priv);
  828. batadv_gw_election(bat_priv);
  829. }
  830. static void batadv_purge_orig(struct work_struct *work)
  831. {
  832. struct delayed_work *delayed_work;
  833. struct batadv_priv *bat_priv;
  834. delayed_work = container_of(work, struct delayed_work, work);
  835. bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
  836. _batadv_purge_orig(bat_priv);
  837. queue_delayed_work(batadv_event_workqueue,
  838. &bat_priv->orig_work,
  839. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  840. }
  841. void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
  842. {
  843. _batadv_purge_orig(bat_priv);
  844. }
  845. int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
  846. {
  847. struct net_device *net_dev = (struct net_device *)seq->private;
  848. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  849. struct batadv_hard_iface *primary_if;
  850. primary_if = batadv_seq_print_text_primary_if_get(seq);
  851. if (!primary_if)
  852. return 0;
  853. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
  854. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  855. primary_if->net_dev->dev_addr, net_dev->name,
  856. bat_priv->bat_algo_ops->name);
  857. batadv_hardif_free_ref(primary_if);
  858. if (!bat_priv->bat_algo_ops->bat_orig_print) {
  859. seq_puts(seq,
  860. "No printing function for this routing protocol\n");
  861. return 0;
  862. }
  863. bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
  864. BATADV_IF_DEFAULT);
  865. return 0;
  866. }
  867. /**
  868. * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
  869. * outgoing interface
  870. * @seq: debugfs table seq_file struct
  871. * @offset: not used
  872. *
  873. * Returns 0
  874. */
  875. int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
  876. {
  877. struct net_device *net_dev = (struct net_device *)seq->private;
  878. struct batadv_hard_iface *hard_iface;
  879. struct batadv_priv *bat_priv;
  880. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  881. if (!hard_iface || !hard_iface->soft_iface) {
  882. seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
  883. goto out;
  884. }
  885. bat_priv = netdev_priv(hard_iface->soft_iface);
  886. if (!bat_priv->bat_algo_ops->bat_orig_print) {
  887. seq_puts(seq,
  888. "No printing function for this routing protocol\n");
  889. goto out;
  890. }
  891. if (hard_iface->if_status != BATADV_IF_ACTIVE) {
  892. seq_puts(seq, "Interface not active\n");
  893. goto out;
  894. }
  895. seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
  896. BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
  897. hard_iface->net_dev->dev_addr,
  898. hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
  899. bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
  900. out:
  901. if (hard_iface)
  902. batadv_hardif_free_ref(hard_iface);
  903. return 0;
  904. }
  905. int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
  906. int max_if_num)
  907. {
  908. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  909. struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
  910. struct batadv_hashtable *hash = bat_priv->orig_hash;
  911. struct hlist_head *head;
  912. struct batadv_orig_node *orig_node;
  913. uint32_t i;
  914. int ret;
  915. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  916. * if_num
  917. */
  918. for (i = 0; i < hash->size; i++) {
  919. head = &hash->table[i];
  920. rcu_read_lock();
  921. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  922. ret = 0;
  923. if (bao->bat_orig_add_if)
  924. ret = bao->bat_orig_add_if(orig_node,
  925. max_if_num);
  926. if (ret == -ENOMEM)
  927. goto err;
  928. }
  929. rcu_read_unlock();
  930. }
  931. return 0;
  932. err:
  933. rcu_read_unlock();
  934. return -ENOMEM;
  935. }
  936. int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
  937. int max_if_num)
  938. {
  939. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  940. struct batadv_hashtable *hash = bat_priv->orig_hash;
  941. struct hlist_head *head;
  942. struct batadv_hard_iface *hard_iface_tmp;
  943. struct batadv_orig_node *orig_node;
  944. struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
  945. uint32_t i;
  946. int ret;
  947. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  948. * if_num
  949. */
  950. for (i = 0; i < hash->size; i++) {
  951. head = &hash->table[i];
  952. rcu_read_lock();
  953. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  954. ret = 0;
  955. if (bao->bat_orig_del_if)
  956. ret = bao->bat_orig_del_if(orig_node,
  957. max_if_num,
  958. hard_iface->if_num);
  959. if (ret == -ENOMEM)
  960. goto err;
  961. }
  962. rcu_read_unlock();
  963. }
  964. /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
  965. rcu_read_lock();
  966. list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
  967. if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
  968. continue;
  969. if (hard_iface == hard_iface_tmp)
  970. continue;
  971. if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
  972. continue;
  973. if (hard_iface_tmp->if_num > hard_iface->if_num)
  974. hard_iface_tmp->if_num--;
  975. }
  976. rcu_read_unlock();
  977. hard_iface->if_num = -1;
  978. return 0;
  979. err:
  980. rcu_read_unlock();
  981. return -ENOMEM;
  982. }