originator.c 43 KB

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