originator.c 45 KB

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