originator.c 40 KB

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