originator.c 37 KB

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