bridge_loop_avoidance.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. /* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * 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 "bridge_loop_avoidance.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/compiler.h>
  22. #include <linux/crc16.h>
  23. #include <linux/errno.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/fs.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/jhash.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kref.h>
  33. #include <linux/list.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/netlink.h>
  37. #include <linux/rculist.h>
  38. #include <linux/rcupdate.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/stddef.h>
  44. #include <linux/string.h>
  45. #include <linux/workqueue.h>
  46. #include <net/arp.h>
  47. #include <net/genetlink.h>
  48. #include <net/netlink.h>
  49. #include <net/sock.h>
  50. #include <uapi/linux/batman_adv.h>
  51. #include "hard-interface.h"
  52. #include "hash.h"
  53. #include "log.h"
  54. #include "netlink.h"
  55. #include "originator.h"
  56. #include "packet.h"
  57. #include "soft-interface.h"
  58. #include "sysfs.h"
  59. #include "translation-table.h"
  60. static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
  61. static void batadv_bla_periodic_work(struct work_struct *work);
  62. static void
  63. batadv_bla_send_announce(struct batadv_priv *bat_priv,
  64. struct batadv_bla_backbone_gw *backbone_gw);
  65. /**
  66. * batadv_choose_claim - choose the right bucket for a claim.
  67. * @data: data to hash
  68. * @size: size of the hash table
  69. *
  70. * Return: the hash index of the claim
  71. */
  72. static inline u32 batadv_choose_claim(const void *data, u32 size)
  73. {
  74. struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  75. u32 hash = 0;
  76. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  77. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  78. return hash % size;
  79. }
  80. /**
  81. * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
  82. * @data: data to hash
  83. * @size: size of the hash table
  84. *
  85. * Return: the hash index of the backbone gateway
  86. */
  87. static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
  88. {
  89. const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  90. u32 hash = 0;
  91. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  92. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  93. return hash % size;
  94. }
  95. /**
  96. * batadv_compare_backbone_gw - compare address and vid of two backbone gws
  97. * @node: list node of the first entry to compare
  98. * @data2: pointer to the second backbone gateway
  99. *
  100. * Return: true if the backbones have the same data, false otherwise
  101. */
  102. static bool batadv_compare_backbone_gw(const struct hlist_node *node,
  103. const void *data2)
  104. {
  105. const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
  106. hash_entry);
  107. const struct batadv_bla_backbone_gw *gw1 = data1;
  108. const struct batadv_bla_backbone_gw *gw2 = data2;
  109. if (!batadv_compare_eth(gw1->orig, gw2->orig))
  110. return false;
  111. if (gw1->vid != gw2->vid)
  112. return false;
  113. return true;
  114. }
  115. /**
  116. * batadv_compare_claim - compare address and vid of two claims
  117. * @node: list node of the first entry to compare
  118. * @data2: pointer to the second claims
  119. *
  120. * Return: true if the claim have the same data, 0 otherwise
  121. */
  122. static bool batadv_compare_claim(const struct hlist_node *node,
  123. const void *data2)
  124. {
  125. const void *data1 = container_of(node, struct batadv_bla_claim,
  126. hash_entry);
  127. const struct batadv_bla_claim *cl1 = data1;
  128. const struct batadv_bla_claim *cl2 = data2;
  129. if (!batadv_compare_eth(cl1->addr, cl2->addr))
  130. return false;
  131. if (cl1->vid != cl2->vid)
  132. return false;
  133. return true;
  134. }
  135. /**
  136. * batadv_backbone_gw_release - release backbone gw from lists and queue for
  137. * free after rcu grace period
  138. * @ref: kref pointer of the backbone gw
  139. */
  140. static void batadv_backbone_gw_release(struct kref *ref)
  141. {
  142. struct batadv_bla_backbone_gw *backbone_gw;
  143. backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
  144. refcount);
  145. kfree_rcu(backbone_gw, rcu);
  146. }
  147. /**
  148. * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
  149. * release it
  150. * @backbone_gw: backbone gateway to be free'd
  151. */
  152. static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
  153. {
  154. kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
  155. }
  156. /**
  157. * batadv_claim_release - release claim from lists and queue for free after rcu
  158. * grace period
  159. * @ref: kref pointer of the claim
  160. */
  161. static void batadv_claim_release(struct kref *ref)
  162. {
  163. struct batadv_bla_claim *claim;
  164. struct batadv_bla_backbone_gw *old_backbone_gw;
  165. claim = container_of(ref, struct batadv_bla_claim, refcount);
  166. spin_lock_bh(&claim->backbone_lock);
  167. old_backbone_gw = claim->backbone_gw;
  168. claim->backbone_gw = NULL;
  169. spin_unlock_bh(&claim->backbone_lock);
  170. spin_lock_bh(&old_backbone_gw->crc_lock);
  171. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  172. spin_unlock_bh(&old_backbone_gw->crc_lock);
  173. batadv_backbone_gw_put(old_backbone_gw);
  174. kfree_rcu(claim, rcu);
  175. }
  176. /**
  177. * batadv_claim_put - decrement the claim refcounter and possibly
  178. * release it
  179. * @claim: claim to be free'd
  180. */
  181. static void batadv_claim_put(struct batadv_bla_claim *claim)
  182. {
  183. kref_put(&claim->refcount, batadv_claim_release);
  184. }
  185. /**
  186. * batadv_claim_hash_find - looks for a claim in the claim hash
  187. * @bat_priv: the bat priv with all the soft interface information
  188. * @data: search data (may be local/static data)
  189. *
  190. * Return: claim if found or NULL otherwise.
  191. */
  192. static struct batadv_bla_claim *
  193. batadv_claim_hash_find(struct batadv_priv *bat_priv,
  194. struct batadv_bla_claim *data)
  195. {
  196. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  197. struct hlist_head *head;
  198. struct batadv_bla_claim *claim;
  199. struct batadv_bla_claim *claim_tmp = NULL;
  200. int index;
  201. if (!hash)
  202. return NULL;
  203. index = batadv_choose_claim(data, hash->size);
  204. head = &hash->table[index];
  205. rcu_read_lock();
  206. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  207. if (!batadv_compare_claim(&claim->hash_entry, data))
  208. continue;
  209. if (!kref_get_unless_zero(&claim->refcount))
  210. continue;
  211. claim_tmp = claim;
  212. break;
  213. }
  214. rcu_read_unlock();
  215. return claim_tmp;
  216. }
  217. /**
  218. * batadv_backbone_hash_find - looks for a backbone gateway in the hash
  219. * @bat_priv: the bat priv with all the soft interface information
  220. * @addr: the address of the originator
  221. * @vid: the VLAN ID
  222. *
  223. * Return: backbone gateway if found or NULL otherwise
  224. */
  225. static struct batadv_bla_backbone_gw *
  226. batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
  227. unsigned short vid)
  228. {
  229. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  230. struct hlist_head *head;
  231. struct batadv_bla_backbone_gw search_entry, *backbone_gw;
  232. struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
  233. int index;
  234. if (!hash)
  235. return NULL;
  236. ether_addr_copy(search_entry.orig, addr);
  237. search_entry.vid = vid;
  238. index = batadv_choose_backbone_gw(&search_entry, hash->size);
  239. head = &hash->table[index];
  240. rcu_read_lock();
  241. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  242. if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
  243. &search_entry))
  244. continue;
  245. if (!kref_get_unless_zero(&backbone_gw->refcount))
  246. continue;
  247. backbone_gw_tmp = backbone_gw;
  248. break;
  249. }
  250. rcu_read_unlock();
  251. return backbone_gw_tmp;
  252. }
  253. /**
  254. * batadv_bla_del_backbone_claims - delete all claims for a backbone
  255. * @backbone_gw: backbone gateway where the claims should be removed
  256. */
  257. static void
  258. batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
  259. {
  260. struct batadv_hashtable *hash;
  261. struct hlist_node *node_tmp;
  262. struct hlist_head *head;
  263. struct batadv_bla_claim *claim;
  264. int i;
  265. spinlock_t *list_lock; /* protects write access to the hash lists */
  266. hash = backbone_gw->bat_priv->bla.claim_hash;
  267. if (!hash)
  268. return;
  269. for (i = 0; i < hash->size; i++) {
  270. head = &hash->table[i];
  271. list_lock = &hash->list_locks[i];
  272. spin_lock_bh(list_lock);
  273. hlist_for_each_entry_safe(claim, node_tmp,
  274. head, hash_entry) {
  275. if (claim->backbone_gw != backbone_gw)
  276. continue;
  277. batadv_claim_put(claim);
  278. hlist_del_rcu(&claim->hash_entry);
  279. }
  280. spin_unlock_bh(list_lock);
  281. }
  282. /* all claims gone, initialize CRC */
  283. spin_lock_bh(&backbone_gw->crc_lock);
  284. backbone_gw->crc = BATADV_BLA_CRC_INIT;
  285. spin_unlock_bh(&backbone_gw->crc_lock);
  286. }
  287. /**
  288. * batadv_bla_send_claim - sends a claim frame according to the provided info
  289. * @bat_priv: the bat priv with all the soft interface information
  290. * @mac: the mac address to be announced within the claim
  291. * @vid: the VLAN ID
  292. * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
  293. */
  294. static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
  295. unsigned short vid, int claimtype)
  296. {
  297. struct sk_buff *skb;
  298. struct ethhdr *ethhdr;
  299. struct batadv_hard_iface *primary_if;
  300. struct net_device *soft_iface;
  301. u8 *hw_src;
  302. struct batadv_bla_claim_dst local_claim_dest;
  303. __be32 zeroip = 0;
  304. primary_if = batadv_primary_if_get_selected(bat_priv);
  305. if (!primary_if)
  306. return;
  307. memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
  308. sizeof(local_claim_dest));
  309. local_claim_dest.type = claimtype;
  310. soft_iface = primary_if->soft_iface;
  311. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  312. /* IP DST: 0.0.0.0 */
  313. zeroip,
  314. primary_if->soft_iface,
  315. /* IP SRC: 0.0.0.0 */
  316. zeroip,
  317. /* Ethernet DST: Broadcast */
  318. NULL,
  319. /* Ethernet SRC/HW SRC: originator mac */
  320. primary_if->net_dev->dev_addr,
  321. /* HW DST: FF:43:05:XX:YY:YY
  322. * with XX = claim type
  323. * and YY:YY = group id
  324. */
  325. (u8 *)&local_claim_dest);
  326. if (!skb)
  327. goto out;
  328. ethhdr = (struct ethhdr *)skb->data;
  329. hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
  330. /* now we pretend that the client would have sent this ... */
  331. switch (claimtype) {
  332. case BATADV_CLAIM_TYPE_CLAIM:
  333. /* normal claim frame
  334. * set Ethernet SRC to the clients mac
  335. */
  336. ether_addr_copy(ethhdr->h_source, mac);
  337. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  338. "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
  339. BATADV_PRINT_VID(vid));
  340. break;
  341. case BATADV_CLAIM_TYPE_UNCLAIM:
  342. /* unclaim frame
  343. * set HW SRC to the clients mac
  344. */
  345. ether_addr_copy(hw_src, mac);
  346. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  347. "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
  348. BATADV_PRINT_VID(vid));
  349. break;
  350. case BATADV_CLAIM_TYPE_ANNOUNCE:
  351. /* announcement frame
  352. * set HW SRC to the special mac containg the crc
  353. */
  354. ether_addr_copy(hw_src, mac);
  355. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  356. "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
  357. ethhdr->h_source, BATADV_PRINT_VID(vid));
  358. break;
  359. case BATADV_CLAIM_TYPE_REQUEST:
  360. /* request frame
  361. * set HW SRC and header destination to the receiving backbone
  362. * gws mac
  363. */
  364. ether_addr_copy(hw_src, mac);
  365. ether_addr_copy(ethhdr->h_dest, mac);
  366. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  367. "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
  368. ethhdr->h_source, ethhdr->h_dest,
  369. BATADV_PRINT_VID(vid));
  370. break;
  371. case BATADV_CLAIM_TYPE_LOOPDETECT:
  372. ether_addr_copy(ethhdr->h_source, mac);
  373. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  374. "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
  375. ethhdr->h_source, ethhdr->h_dest,
  376. BATADV_PRINT_VID(vid));
  377. break;
  378. }
  379. if (vid & BATADV_VLAN_HAS_TAG) {
  380. skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
  381. vid & VLAN_VID_MASK);
  382. if (!skb)
  383. goto out;
  384. }
  385. skb_reset_mac_header(skb);
  386. skb->protocol = eth_type_trans(skb, soft_iface);
  387. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  388. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  389. skb->len + ETH_HLEN);
  390. soft_iface->last_rx = jiffies;
  391. netif_rx(skb);
  392. out:
  393. if (primary_if)
  394. batadv_hardif_put(primary_if);
  395. }
  396. /**
  397. * batadv_bla_loopdetect_report - worker for reporting the loop
  398. * @work: work queue item
  399. *
  400. * Throws an uevent, as the loopdetect check function can't do that itself
  401. * since the kernel may sleep while throwing uevents.
  402. */
  403. static void batadv_bla_loopdetect_report(struct work_struct *work)
  404. {
  405. struct batadv_bla_backbone_gw *backbone_gw;
  406. struct batadv_priv *bat_priv;
  407. char vid_str[6] = { '\0' };
  408. backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
  409. report_work);
  410. bat_priv = backbone_gw->bat_priv;
  411. batadv_info(bat_priv->soft_iface,
  412. "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
  413. BATADV_PRINT_VID(backbone_gw->vid));
  414. snprintf(vid_str, sizeof(vid_str), "%d",
  415. BATADV_PRINT_VID(backbone_gw->vid));
  416. vid_str[sizeof(vid_str) - 1] = 0;
  417. batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
  418. vid_str);
  419. batadv_backbone_gw_put(backbone_gw);
  420. }
  421. /**
  422. * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
  423. * @bat_priv: the bat priv with all the soft interface information
  424. * @orig: the mac address of the originator
  425. * @vid: the VLAN ID
  426. * @own_backbone: set if the requested backbone is local
  427. *
  428. * Return: the (possibly created) backbone gateway or NULL on error
  429. */
  430. static struct batadv_bla_backbone_gw *
  431. batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
  432. unsigned short vid, bool own_backbone)
  433. {
  434. struct batadv_bla_backbone_gw *entry;
  435. struct batadv_orig_node *orig_node;
  436. int hash_added;
  437. entry = batadv_backbone_hash_find(bat_priv, orig, vid);
  438. if (entry)
  439. return entry;
  440. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  441. "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
  442. orig, BATADV_PRINT_VID(vid));
  443. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  444. if (!entry)
  445. return NULL;
  446. entry->vid = vid;
  447. entry->lasttime = jiffies;
  448. entry->crc = BATADV_BLA_CRC_INIT;
  449. entry->bat_priv = bat_priv;
  450. spin_lock_init(&entry->crc_lock);
  451. atomic_set(&entry->request_sent, 0);
  452. atomic_set(&entry->wait_periods, 0);
  453. ether_addr_copy(entry->orig, orig);
  454. INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
  455. kref_init(&entry->refcount);
  456. kref_get(&entry->refcount);
  457. hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
  458. batadv_compare_backbone_gw,
  459. batadv_choose_backbone_gw, entry,
  460. &entry->hash_entry);
  461. if (unlikely(hash_added != 0)) {
  462. /* hash failed, free the structure */
  463. kfree(entry);
  464. return NULL;
  465. }
  466. /* this is a gateway now, remove any TT entry on this VLAN */
  467. orig_node = batadv_orig_hash_find(bat_priv, orig);
  468. if (orig_node) {
  469. batadv_tt_global_del_orig(bat_priv, orig_node, vid,
  470. "became a backbone gateway");
  471. batadv_orig_node_put(orig_node);
  472. }
  473. if (own_backbone) {
  474. batadv_bla_send_announce(bat_priv, entry);
  475. /* this will be decreased in the worker thread */
  476. atomic_inc(&entry->request_sent);
  477. atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
  478. atomic_inc(&bat_priv->bla.num_requests);
  479. }
  480. return entry;
  481. }
  482. /**
  483. * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
  484. * @bat_priv: the bat priv with all the soft interface information
  485. * @primary_if: the selected primary interface
  486. * @vid: VLAN identifier
  487. *
  488. * update or add the own backbone gw to make sure we announce
  489. * where we receive other backbone gws
  490. */
  491. static void
  492. batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
  493. struct batadv_hard_iface *primary_if,
  494. unsigned short vid)
  495. {
  496. struct batadv_bla_backbone_gw *backbone_gw;
  497. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  498. primary_if->net_dev->dev_addr,
  499. vid, true);
  500. if (unlikely(!backbone_gw))
  501. return;
  502. backbone_gw->lasttime = jiffies;
  503. batadv_backbone_gw_put(backbone_gw);
  504. }
  505. /**
  506. * batadv_bla_answer_request - answer a bla request by sending own claims
  507. * @bat_priv: the bat priv with all the soft interface information
  508. * @primary_if: interface where the request came on
  509. * @vid: the vid where the request came on
  510. *
  511. * Repeat all of our own claims, and finally send an ANNOUNCE frame
  512. * to allow the requester another check if the CRC is correct now.
  513. */
  514. static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
  515. struct batadv_hard_iface *primary_if,
  516. unsigned short vid)
  517. {
  518. struct hlist_head *head;
  519. struct batadv_hashtable *hash;
  520. struct batadv_bla_claim *claim;
  521. struct batadv_bla_backbone_gw *backbone_gw;
  522. int i;
  523. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  524. "bla_answer_request(): received a claim request, send all of our own claims again\n");
  525. backbone_gw = batadv_backbone_hash_find(bat_priv,
  526. primary_if->net_dev->dev_addr,
  527. vid);
  528. if (!backbone_gw)
  529. return;
  530. hash = bat_priv->bla.claim_hash;
  531. for (i = 0; i < hash->size; i++) {
  532. head = &hash->table[i];
  533. rcu_read_lock();
  534. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  535. /* only own claims are interesting */
  536. if (claim->backbone_gw != backbone_gw)
  537. continue;
  538. batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
  539. BATADV_CLAIM_TYPE_CLAIM);
  540. }
  541. rcu_read_unlock();
  542. }
  543. /* finally, send an announcement frame */
  544. batadv_bla_send_announce(bat_priv, backbone_gw);
  545. batadv_backbone_gw_put(backbone_gw);
  546. }
  547. /**
  548. * batadv_bla_send_request - send a request to repeat claims
  549. * @backbone_gw: the backbone gateway from whom we are out of sync
  550. *
  551. * When the crc is wrong, ask the backbone gateway for a full table update.
  552. * After the request, it will repeat all of his own claims and finally
  553. * send an announcement claim with which we can check again.
  554. */
  555. static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
  556. {
  557. /* first, remove all old entries */
  558. batadv_bla_del_backbone_claims(backbone_gw);
  559. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  560. "Sending REQUEST to %pM\n", backbone_gw->orig);
  561. /* send request */
  562. batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
  563. backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
  564. /* no local broadcasts should be sent or received, for now. */
  565. if (!atomic_read(&backbone_gw->request_sent)) {
  566. atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
  567. atomic_set(&backbone_gw->request_sent, 1);
  568. }
  569. }
  570. /**
  571. * batadv_bla_send_announce - Send an announcement frame
  572. * @bat_priv: the bat priv with all the soft interface information
  573. * @backbone_gw: our backbone gateway which should be announced
  574. */
  575. static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
  576. struct batadv_bla_backbone_gw *backbone_gw)
  577. {
  578. u8 mac[ETH_ALEN];
  579. __be16 crc;
  580. memcpy(mac, batadv_announce_mac, 4);
  581. spin_lock_bh(&backbone_gw->crc_lock);
  582. crc = htons(backbone_gw->crc);
  583. spin_unlock_bh(&backbone_gw->crc_lock);
  584. memcpy(&mac[4], &crc, 2);
  585. batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
  586. BATADV_CLAIM_TYPE_ANNOUNCE);
  587. }
  588. /**
  589. * batadv_bla_add_claim - Adds a claim in the claim hash
  590. * @bat_priv: the bat priv with all the soft interface information
  591. * @mac: the mac address of the claim
  592. * @vid: the VLAN ID of the frame
  593. * @backbone_gw: the backbone gateway which claims it
  594. */
  595. static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
  596. const u8 *mac, const unsigned short vid,
  597. struct batadv_bla_backbone_gw *backbone_gw)
  598. {
  599. struct batadv_bla_backbone_gw *old_backbone_gw;
  600. struct batadv_bla_claim *claim;
  601. struct batadv_bla_claim search_claim;
  602. bool remove_crc = false;
  603. int hash_added;
  604. ether_addr_copy(search_claim.addr, mac);
  605. search_claim.vid = vid;
  606. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  607. /* create a new claim entry if it does not exist yet. */
  608. if (!claim) {
  609. claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
  610. if (!claim)
  611. return;
  612. ether_addr_copy(claim->addr, mac);
  613. spin_lock_init(&claim->backbone_lock);
  614. claim->vid = vid;
  615. claim->lasttime = jiffies;
  616. kref_get(&backbone_gw->refcount);
  617. claim->backbone_gw = backbone_gw;
  618. kref_init(&claim->refcount);
  619. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  620. "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
  621. mac, BATADV_PRINT_VID(vid));
  622. kref_get(&claim->refcount);
  623. hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
  624. batadv_compare_claim,
  625. batadv_choose_claim, claim,
  626. &claim->hash_entry);
  627. if (unlikely(hash_added != 0)) {
  628. /* only local changes happened. */
  629. kfree(claim);
  630. return;
  631. }
  632. } else {
  633. claim->lasttime = jiffies;
  634. if (claim->backbone_gw == backbone_gw)
  635. /* no need to register a new backbone */
  636. goto claim_free_ref;
  637. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  638. "bla_add_claim(): changing ownership for %pM, vid %d\n",
  639. mac, BATADV_PRINT_VID(vid));
  640. remove_crc = true;
  641. }
  642. /* replace backbone_gw atomically and adjust reference counters */
  643. spin_lock_bh(&claim->backbone_lock);
  644. old_backbone_gw = claim->backbone_gw;
  645. kref_get(&backbone_gw->refcount);
  646. claim->backbone_gw = backbone_gw;
  647. spin_unlock_bh(&claim->backbone_lock);
  648. if (remove_crc) {
  649. /* remove claim address from old backbone_gw */
  650. spin_lock_bh(&old_backbone_gw->crc_lock);
  651. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  652. spin_unlock_bh(&old_backbone_gw->crc_lock);
  653. }
  654. batadv_backbone_gw_put(old_backbone_gw);
  655. /* add claim address to new backbone_gw */
  656. spin_lock_bh(&backbone_gw->crc_lock);
  657. backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  658. spin_unlock_bh(&backbone_gw->crc_lock);
  659. backbone_gw->lasttime = jiffies;
  660. claim_free_ref:
  661. batadv_claim_put(claim);
  662. }
  663. /**
  664. * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
  665. * claim
  666. * @claim: claim whose backbone_gw should be returned
  667. *
  668. * Return: valid reference to claim::backbone_gw
  669. */
  670. static struct batadv_bla_backbone_gw *
  671. batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
  672. {
  673. struct batadv_bla_backbone_gw *backbone_gw;
  674. spin_lock_bh(&claim->backbone_lock);
  675. backbone_gw = claim->backbone_gw;
  676. kref_get(&backbone_gw->refcount);
  677. spin_unlock_bh(&claim->backbone_lock);
  678. return backbone_gw;
  679. }
  680. /**
  681. * batadv_bla_del_claim - delete a claim from the claim hash
  682. * @bat_priv: the bat priv with all the soft interface information
  683. * @mac: mac address of the claim to be removed
  684. * @vid: VLAN id for the claim to be removed
  685. */
  686. static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
  687. const u8 *mac, const unsigned short vid)
  688. {
  689. struct batadv_bla_claim search_claim, *claim;
  690. ether_addr_copy(search_claim.addr, mac);
  691. search_claim.vid = vid;
  692. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  693. if (!claim)
  694. return;
  695. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
  696. mac, BATADV_PRINT_VID(vid));
  697. batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
  698. batadv_choose_claim, claim);
  699. batadv_claim_put(claim); /* reference from the hash is gone */
  700. /* don't need the reference from hash_find() anymore */
  701. batadv_claim_put(claim);
  702. }
  703. /**
  704. * batadv_handle_announce - check for ANNOUNCE frame
  705. * @bat_priv: the bat priv with all the soft interface information
  706. * @an_addr: announcement mac address (ARP Sender HW address)
  707. * @backbone_addr: originator address of the sender (Ethernet source MAC)
  708. * @vid: the VLAN ID of the frame
  709. *
  710. * Return: true if handled
  711. */
  712. static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
  713. u8 *backbone_addr, unsigned short vid)
  714. {
  715. struct batadv_bla_backbone_gw *backbone_gw;
  716. u16 backbone_crc, crc;
  717. if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
  718. return false;
  719. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  720. false);
  721. if (unlikely(!backbone_gw))
  722. return true;
  723. /* handle as ANNOUNCE frame */
  724. backbone_gw->lasttime = jiffies;
  725. crc = ntohs(*((__be16 *)(&an_addr[4])));
  726. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  727. "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
  728. BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
  729. spin_lock_bh(&backbone_gw->crc_lock);
  730. backbone_crc = backbone_gw->crc;
  731. spin_unlock_bh(&backbone_gw->crc_lock);
  732. if (backbone_crc != crc) {
  733. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  734. "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
  735. backbone_gw->orig,
  736. BATADV_PRINT_VID(backbone_gw->vid),
  737. backbone_crc, crc);
  738. batadv_bla_send_request(backbone_gw);
  739. } else {
  740. /* if we have sent a request and the crc was OK,
  741. * we can allow traffic again.
  742. */
  743. if (atomic_read(&backbone_gw->request_sent)) {
  744. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  745. atomic_set(&backbone_gw->request_sent, 0);
  746. }
  747. }
  748. batadv_backbone_gw_put(backbone_gw);
  749. return true;
  750. }
  751. /**
  752. * batadv_handle_request - check for REQUEST frame
  753. * @bat_priv: the bat priv with all the soft interface information
  754. * @primary_if: the primary hard interface of this batman soft interface
  755. * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
  756. * @ethhdr: ethernet header of a packet
  757. * @vid: the VLAN ID of the frame
  758. *
  759. * Return: true if handled
  760. */
  761. static bool batadv_handle_request(struct batadv_priv *bat_priv,
  762. struct batadv_hard_iface *primary_if,
  763. u8 *backbone_addr, struct ethhdr *ethhdr,
  764. unsigned short vid)
  765. {
  766. /* check for REQUEST frame */
  767. if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
  768. return false;
  769. /* sanity check, this should not happen on a normal switch,
  770. * we ignore it in this case.
  771. */
  772. if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
  773. return true;
  774. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  775. "handle_request(): REQUEST vid %d (sent by %pM)...\n",
  776. BATADV_PRINT_VID(vid), ethhdr->h_source);
  777. batadv_bla_answer_request(bat_priv, primary_if, vid);
  778. return true;
  779. }
  780. /**
  781. * batadv_handle_unclaim - check for UNCLAIM frame
  782. * @bat_priv: the bat priv with all the soft interface information
  783. * @primary_if: the primary hard interface of this batman soft interface
  784. * @backbone_addr: originator address of the backbone (Ethernet source)
  785. * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
  786. * @vid: the VLAN ID of the frame
  787. *
  788. * Return: true if handled
  789. */
  790. static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
  791. struct batadv_hard_iface *primary_if,
  792. u8 *backbone_addr, u8 *claim_addr,
  793. unsigned short vid)
  794. {
  795. struct batadv_bla_backbone_gw *backbone_gw;
  796. /* unclaim in any case if it is our own */
  797. if (primary_if && batadv_compare_eth(backbone_addr,
  798. primary_if->net_dev->dev_addr))
  799. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  800. BATADV_CLAIM_TYPE_UNCLAIM);
  801. backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
  802. if (!backbone_gw)
  803. return true;
  804. /* this must be an UNCLAIM frame */
  805. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  806. "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
  807. claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
  808. batadv_bla_del_claim(bat_priv, claim_addr, vid);
  809. batadv_backbone_gw_put(backbone_gw);
  810. return true;
  811. }
  812. /**
  813. * batadv_handle_claim - check for CLAIM frame
  814. * @bat_priv: the bat priv with all the soft interface information
  815. * @primary_if: the primary hard interface of this batman soft interface
  816. * @backbone_addr: originator address of the backbone (Ethernet Source)
  817. * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
  818. * @vid: the VLAN ID of the frame
  819. *
  820. * Return: true if handled
  821. */
  822. static bool batadv_handle_claim(struct batadv_priv *bat_priv,
  823. struct batadv_hard_iface *primary_if,
  824. u8 *backbone_addr, u8 *claim_addr,
  825. unsigned short vid)
  826. {
  827. struct batadv_bla_backbone_gw *backbone_gw;
  828. /* register the gateway if not yet available, and add the claim. */
  829. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  830. false);
  831. if (unlikely(!backbone_gw))
  832. return true;
  833. /* this must be a CLAIM frame */
  834. batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
  835. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  836. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  837. BATADV_CLAIM_TYPE_CLAIM);
  838. /* TODO: we could call something like tt_local_del() here. */
  839. batadv_backbone_gw_put(backbone_gw);
  840. return true;
  841. }
  842. /**
  843. * batadv_check_claim_group - check for claim group membership
  844. * @bat_priv: the bat priv with all the soft interface information
  845. * @primary_if: the primary interface of this batman interface
  846. * @hw_src: the Hardware source in the ARP Header
  847. * @hw_dst: the Hardware destination in the ARP Header
  848. * @ethhdr: pointer to the Ethernet header of the claim frame
  849. *
  850. * checks if it is a claim packet and if its on the same group.
  851. * This function also applies the group ID of the sender
  852. * if it is in the same mesh.
  853. *
  854. * Return:
  855. * 2 - if it is a claim packet and on the same group
  856. * 1 - if is a claim packet from another group
  857. * 0 - if it is not a claim packet
  858. */
  859. static int batadv_check_claim_group(struct batadv_priv *bat_priv,
  860. struct batadv_hard_iface *primary_if,
  861. u8 *hw_src, u8 *hw_dst,
  862. struct ethhdr *ethhdr)
  863. {
  864. u8 *backbone_addr;
  865. struct batadv_orig_node *orig_node;
  866. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  867. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  868. bla_dst_own = &bat_priv->bla.claim_dest;
  869. /* if announcement packet, use the source,
  870. * otherwise assume it is in the hw_src
  871. */
  872. switch (bla_dst->type) {
  873. case BATADV_CLAIM_TYPE_CLAIM:
  874. backbone_addr = hw_src;
  875. break;
  876. case BATADV_CLAIM_TYPE_REQUEST:
  877. case BATADV_CLAIM_TYPE_ANNOUNCE:
  878. case BATADV_CLAIM_TYPE_UNCLAIM:
  879. backbone_addr = ethhdr->h_source;
  880. break;
  881. default:
  882. return 0;
  883. }
  884. /* don't accept claim frames from ourselves */
  885. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  886. return 0;
  887. /* if its already the same group, it is fine. */
  888. if (bla_dst->group == bla_dst_own->group)
  889. return 2;
  890. /* lets see if this originator is in our mesh */
  891. orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
  892. /* dont accept claims from gateways which are not in
  893. * the same mesh or group.
  894. */
  895. if (!orig_node)
  896. return 1;
  897. /* if our mesh friends mac is bigger, use it for ourselves. */
  898. if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
  899. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  900. "taking other backbones claim group: %#.4x\n",
  901. ntohs(bla_dst->group));
  902. bla_dst_own->group = bla_dst->group;
  903. }
  904. batadv_orig_node_put(orig_node);
  905. return 2;
  906. }
  907. /**
  908. * batadv_bla_process_claim - Check if this is a claim frame, and process it
  909. * @bat_priv: the bat priv with all the soft interface information
  910. * @primary_if: the primary hard interface of this batman soft interface
  911. * @skb: the frame to be checked
  912. *
  913. * Return: true if it was a claim frame, otherwise return false to
  914. * tell the callee that it can use the frame on its own.
  915. */
  916. static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
  917. struct batadv_hard_iface *primary_if,
  918. struct sk_buff *skb)
  919. {
  920. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  921. u8 *hw_src, *hw_dst;
  922. struct vlan_hdr *vhdr, vhdr_buf;
  923. struct ethhdr *ethhdr;
  924. struct arphdr *arphdr;
  925. unsigned short vid;
  926. int vlan_depth = 0;
  927. __be16 proto;
  928. int headlen;
  929. int ret;
  930. vid = batadv_get_vid(skb, 0);
  931. ethhdr = eth_hdr(skb);
  932. proto = ethhdr->h_proto;
  933. headlen = ETH_HLEN;
  934. if (vid & BATADV_VLAN_HAS_TAG) {
  935. /* Traverse the VLAN/Ethertypes.
  936. *
  937. * At this point it is known that the first protocol is a VLAN
  938. * header, so start checking at the encapsulated protocol.
  939. *
  940. * The depth of the VLAN headers is recorded to drop BLA claim
  941. * frames encapsulated into multiple VLAN headers (QinQ).
  942. */
  943. do {
  944. vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
  945. &vhdr_buf);
  946. if (!vhdr)
  947. return false;
  948. proto = vhdr->h_vlan_encapsulated_proto;
  949. headlen += VLAN_HLEN;
  950. vlan_depth++;
  951. } while (proto == htons(ETH_P_8021Q));
  952. }
  953. if (proto != htons(ETH_P_ARP))
  954. return false; /* not a claim frame */
  955. /* this must be a ARP frame. check if it is a claim. */
  956. if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
  957. return false;
  958. /* pskb_may_pull() may have modified the pointers, get ethhdr again */
  959. ethhdr = eth_hdr(skb);
  960. arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
  961. /* Check whether the ARP frame carries a valid
  962. * IP information
  963. */
  964. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  965. return false;
  966. if (arphdr->ar_pro != htons(ETH_P_IP))
  967. return false;
  968. if (arphdr->ar_hln != ETH_ALEN)
  969. return false;
  970. if (arphdr->ar_pln != 4)
  971. return false;
  972. hw_src = (u8 *)arphdr + sizeof(struct arphdr);
  973. hw_dst = hw_src + ETH_ALEN + 4;
  974. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  975. bla_dst_own = &bat_priv->bla.claim_dest;
  976. /* check if it is a claim frame in general */
  977. if (memcmp(bla_dst->magic, bla_dst_own->magic,
  978. sizeof(bla_dst->magic)) != 0)
  979. return false;
  980. /* check if there is a claim frame encapsulated deeper in (QinQ) and
  981. * drop that, as this is not supported by BLA but should also not be
  982. * sent via the mesh.
  983. */
  984. if (vlan_depth > 1)
  985. return true;
  986. /* Let the loopdetect frames on the mesh in any case. */
  987. if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
  988. return false;
  989. /* check if it is a claim frame. */
  990. ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
  991. ethhdr);
  992. if (ret == 1)
  993. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  994. "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  995. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
  996. hw_dst);
  997. if (ret < 2)
  998. return !!ret;
  999. /* become a backbone gw ourselves on this vlan if not happened yet */
  1000. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1001. /* check for the different types of claim frames ... */
  1002. switch (bla_dst->type) {
  1003. case BATADV_CLAIM_TYPE_CLAIM:
  1004. if (batadv_handle_claim(bat_priv, primary_if, hw_src,
  1005. ethhdr->h_source, vid))
  1006. return true;
  1007. break;
  1008. case BATADV_CLAIM_TYPE_UNCLAIM:
  1009. if (batadv_handle_unclaim(bat_priv, primary_if,
  1010. ethhdr->h_source, hw_src, vid))
  1011. return true;
  1012. break;
  1013. case BATADV_CLAIM_TYPE_ANNOUNCE:
  1014. if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
  1015. vid))
  1016. return true;
  1017. break;
  1018. case BATADV_CLAIM_TYPE_REQUEST:
  1019. if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
  1020. vid))
  1021. return true;
  1022. break;
  1023. }
  1024. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1025. "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  1026. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
  1027. return true;
  1028. }
  1029. /**
  1030. * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
  1031. * immediately
  1032. * @bat_priv: the bat priv with all the soft interface information
  1033. * @now: whether the whole hash shall be wiped now
  1034. *
  1035. * Check when we last heard from other nodes, and remove them in case of
  1036. * a time out, or clean all backbone gws if now is set.
  1037. */
  1038. static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
  1039. {
  1040. struct batadv_bla_backbone_gw *backbone_gw;
  1041. struct hlist_node *node_tmp;
  1042. struct hlist_head *head;
  1043. struct batadv_hashtable *hash;
  1044. spinlock_t *list_lock; /* protects write access to the hash lists */
  1045. int i;
  1046. hash = bat_priv->bla.backbone_hash;
  1047. if (!hash)
  1048. return;
  1049. for (i = 0; i < hash->size; i++) {
  1050. head = &hash->table[i];
  1051. list_lock = &hash->list_locks[i];
  1052. spin_lock_bh(list_lock);
  1053. hlist_for_each_entry_safe(backbone_gw, node_tmp,
  1054. head, hash_entry) {
  1055. if (now)
  1056. goto purge_now;
  1057. if (!batadv_has_timed_out(backbone_gw->lasttime,
  1058. BATADV_BLA_BACKBONE_TIMEOUT))
  1059. continue;
  1060. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  1061. "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
  1062. backbone_gw->orig);
  1063. purge_now:
  1064. /* don't wait for the pending request anymore */
  1065. if (atomic_read(&backbone_gw->request_sent))
  1066. atomic_dec(&bat_priv->bla.num_requests);
  1067. batadv_bla_del_backbone_claims(backbone_gw);
  1068. hlist_del_rcu(&backbone_gw->hash_entry);
  1069. batadv_backbone_gw_put(backbone_gw);
  1070. }
  1071. spin_unlock_bh(list_lock);
  1072. }
  1073. }
  1074. /**
  1075. * batadv_bla_purge_claims - Remove claims after a timeout or immediately
  1076. * @bat_priv: the bat priv with all the soft interface information
  1077. * @primary_if: the selected primary interface, may be NULL if now is set
  1078. * @now: whether the whole hash shall be wiped now
  1079. *
  1080. * Check when we heard last time from our own claims, and remove them in case of
  1081. * a time out, or clean all claims if now is set
  1082. */
  1083. static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
  1084. struct batadv_hard_iface *primary_if,
  1085. int now)
  1086. {
  1087. struct batadv_bla_backbone_gw *backbone_gw;
  1088. struct batadv_bla_claim *claim;
  1089. struct hlist_head *head;
  1090. struct batadv_hashtable *hash;
  1091. int i;
  1092. hash = bat_priv->bla.claim_hash;
  1093. if (!hash)
  1094. return;
  1095. for (i = 0; i < hash->size; i++) {
  1096. head = &hash->table[i];
  1097. rcu_read_lock();
  1098. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1099. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1100. if (now)
  1101. goto purge_now;
  1102. if (!batadv_compare_eth(backbone_gw->orig,
  1103. primary_if->net_dev->dev_addr))
  1104. goto skip;
  1105. if (!batadv_has_timed_out(claim->lasttime,
  1106. BATADV_BLA_CLAIM_TIMEOUT))
  1107. goto skip;
  1108. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1109. "bla_purge_claims(): %pM, vid %d, time out\n",
  1110. claim->addr, claim->vid);
  1111. purge_now:
  1112. batadv_handle_unclaim(bat_priv, primary_if,
  1113. backbone_gw->orig,
  1114. claim->addr, claim->vid);
  1115. skip:
  1116. batadv_backbone_gw_put(backbone_gw);
  1117. }
  1118. rcu_read_unlock();
  1119. }
  1120. }
  1121. /**
  1122. * batadv_bla_update_orig_address - Update the backbone gateways when the own
  1123. * originator address changes
  1124. * @bat_priv: the bat priv with all the soft interface information
  1125. * @primary_if: the new selected primary_if
  1126. * @oldif: the old primary interface, may be NULL
  1127. */
  1128. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  1129. struct batadv_hard_iface *primary_if,
  1130. struct batadv_hard_iface *oldif)
  1131. {
  1132. struct batadv_bla_backbone_gw *backbone_gw;
  1133. struct hlist_head *head;
  1134. struct batadv_hashtable *hash;
  1135. __be16 group;
  1136. int i;
  1137. /* reset bridge loop avoidance group id */
  1138. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  1139. bat_priv->bla.claim_dest.group = group;
  1140. /* purge everything when bridge loop avoidance is turned off */
  1141. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1142. oldif = NULL;
  1143. if (!oldif) {
  1144. batadv_bla_purge_claims(bat_priv, NULL, 1);
  1145. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1146. return;
  1147. }
  1148. hash = bat_priv->bla.backbone_hash;
  1149. if (!hash)
  1150. return;
  1151. for (i = 0; i < hash->size; i++) {
  1152. head = &hash->table[i];
  1153. rcu_read_lock();
  1154. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1155. /* own orig still holds the old value. */
  1156. if (!batadv_compare_eth(backbone_gw->orig,
  1157. oldif->net_dev->dev_addr))
  1158. continue;
  1159. ether_addr_copy(backbone_gw->orig,
  1160. primary_if->net_dev->dev_addr);
  1161. /* send an announce frame so others will ask for our
  1162. * claims and update their tables.
  1163. */
  1164. batadv_bla_send_announce(bat_priv, backbone_gw);
  1165. }
  1166. rcu_read_unlock();
  1167. }
  1168. }
  1169. /**
  1170. * batadv_bla_send_loopdetect - send a loopdetect frame
  1171. * @bat_priv: the bat priv with all the soft interface information
  1172. * @backbone_gw: the backbone gateway for which a loop should be detected
  1173. *
  1174. * To detect loops that the bridge loop avoidance can't handle, send a loop
  1175. * detection packet on the backbone. Unlike other BLA frames, this frame will
  1176. * be allowed on the mesh by other nodes. If it is received on the mesh, this
  1177. * indicates that there is a loop.
  1178. */
  1179. static void
  1180. batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
  1181. struct batadv_bla_backbone_gw *backbone_gw)
  1182. {
  1183. batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
  1184. backbone_gw->vid);
  1185. batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
  1186. backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
  1187. }
  1188. /**
  1189. * batadv_bla_status_update - purge bla interfaces if necessary
  1190. * @net_dev: the soft interface net device
  1191. */
  1192. void batadv_bla_status_update(struct net_device *net_dev)
  1193. {
  1194. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1195. struct batadv_hard_iface *primary_if;
  1196. primary_if = batadv_primary_if_get_selected(bat_priv);
  1197. if (!primary_if)
  1198. return;
  1199. /* this function already purges everything when bla is disabled,
  1200. * so just call that one.
  1201. */
  1202. batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
  1203. batadv_hardif_put(primary_if);
  1204. }
  1205. /**
  1206. * batadv_bla_periodic_work - performs periodic bla work
  1207. * @work: kernel work struct
  1208. *
  1209. * periodic work to do:
  1210. * * purge structures when they are too old
  1211. * * send announcements
  1212. */
  1213. static void batadv_bla_periodic_work(struct work_struct *work)
  1214. {
  1215. struct delayed_work *delayed_work;
  1216. struct batadv_priv *bat_priv;
  1217. struct batadv_priv_bla *priv_bla;
  1218. struct hlist_head *head;
  1219. struct batadv_bla_backbone_gw *backbone_gw;
  1220. struct batadv_hashtable *hash;
  1221. struct batadv_hard_iface *primary_if;
  1222. bool send_loopdetect = false;
  1223. int i;
  1224. delayed_work = to_delayed_work(work);
  1225. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  1226. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  1227. primary_if = batadv_primary_if_get_selected(bat_priv);
  1228. if (!primary_if)
  1229. goto out;
  1230. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  1231. batadv_bla_purge_backbone_gw(bat_priv, 0);
  1232. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1233. goto out;
  1234. if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
  1235. /* set a new random mac address for the next bridge loop
  1236. * detection frames. Set the locally administered bit to avoid
  1237. * collisions with users mac addresses.
  1238. */
  1239. random_ether_addr(bat_priv->bla.loopdetect_addr);
  1240. bat_priv->bla.loopdetect_addr[0] = 0xba;
  1241. bat_priv->bla.loopdetect_addr[1] = 0xbe;
  1242. bat_priv->bla.loopdetect_lasttime = jiffies;
  1243. atomic_set(&bat_priv->bla.loopdetect_next,
  1244. BATADV_BLA_LOOPDETECT_PERIODS);
  1245. /* mark for sending loop detect on all VLANs */
  1246. send_loopdetect = true;
  1247. }
  1248. hash = bat_priv->bla.backbone_hash;
  1249. if (!hash)
  1250. goto out;
  1251. for (i = 0; i < hash->size; i++) {
  1252. head = &hash->table[i];
  1253. rcu_read_lock();
  1254. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1255. if (!batadv_compare_eth(backbone_gw->orig,
  1256. primary_if->net_dev->dev_addr))
  1257. continue;
  1258. backbone_gw->lasttime = jiffies;
  1259. batadv_bla_send_announce(bat_priv, backbone_gw);
  1260. if (send_loopdetect)
  1261. batadv_bla_send_loopdetect(bat_priv,
  1262. backbone_gw);
  1263. /* request_sent is only set after creation to avoid
  1264. * problems when we are not yet known as backbone gw
  1265. * in the backbone.
  1266. *
  1267. * We can reset this now after we waited some periods
  1268. * to give bridge forward delays and bla group forming
  1269. * some grace time.
  1270. */
  1271. if (atomic_read(&backbone_gw->request_sent) == 0)
  1272. continue;
  1273. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  1274. continue;
  1275. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  1276. atomic_set(&backbone_gw->request_sent, 0);
  1277. }
  1278. rcu_read_unlock();
  1279. }
  1280. out:
  1281. if (primary_if)
  1282. batadv_hardif_put(primary_if);
  1283. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1284. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1285. }
  1286. /* The hash for claim and backbone hash receive the same key because they
  1287. * are getting initialized by hash_new with the same key. Reinitializing
  1288. * them with to different keys to allow nested locking without generating
  1289. * lockdep warnings
  1290. */
  1291. static struct lock_class_key batadv_claim_hash_lock_class_key;
  1292. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  1293. /**
  1294. * batadv_bla_init - initialize all bla structures
  1295. * @bat_priv: the bat priv with all the soft interface information
  1296. *
  1297. * Return: 0 on success, < 0 on error.
  1298. */
  1299. int batadv_bla_init(struct batadv_priv *bat_priv)
  1300. {
  1301. int i;
  1302. u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1303. struct batadv_hard_iface *primary_if;
  1304. u16 crc;
  1305. unsigned long entrytime;
  1306. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1307. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1308. /* setting claim destination address */
  1309. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1310. bat_priv->bla.claim_dest.type = 0;
  1311. primary_if = batadv_primary_if_get_selected(bat_priv);
  1312. if (primary_if) {
  1313. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1314. bat_priv->bla.claim_dest.group = htons(crc);
  1315. batadv_hardif_put(primary_if);
  1316. } else {
  1317. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1318. }
  1319. /* initialize the duplicate list */
  1320. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1321. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1322. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1323. bat_priv->bla.bcast_duplist_curr = 0;
  1324. atomic_set(&bat_priv->bla.loopdetect_next,
  1325. BATADV_BLA_LOOPDETECT_PERIODS);
  1326. if (bat_priv->bla.claim_hash)
  1327. return 0;
  1328. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1329. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1330. if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
  1331. return -ENOMEM;
  1332. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1333. &batadv_claim_hash_lock_class_key);
  1334. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1335. &batadv_backbone_hash_lock_class_key);
  1336. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1337. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  1338. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1339. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1340. return 0;
  1341. }
  1342. /**
  1343. * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
  1344. * @bat_priv: the bat priv with all the soft interface information
  1345. * @skb: contains the bcast_packet to be checked
  1346. *
  1347. * check if it is on our broadcast list. Another gateway might
  1348. * have sent the same packet because it is connected to the same backbone,
  1349. * so we have to remove this duplicate.
  1350. *
  1351. * This is performed by checking the CRC, which will tell us
  1352. * with a good chance that it is the same packet. If it is furthermore
  1353. * sent by another host, drop it. We allow equal packets from
  1354. * the same host however as this might be intended.
  1355. *
  1356. * Return: true if a packet is in the duplicate list, false otherwise.
  1357. */
  1358. bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
  1359. struct sk_buff *skb)
  1360. {
  1361. int i, curr;
  1362. __be32 crc;
  1363. struct batadv_bcast_packet *bcast_packet;
  1364. struct batadv_bcast_duplist_entry *entry;
  1365. bool ret = false;
  1366. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1367. /* calculate the crc ... */
  1368. crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
  1369. spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
  1370. for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
  1371. curr = (bat_priv->bla.bcast_duplist_curr + i);
  1372. curr %= BATADV_DUPLIST_SIZE;
  1373. entry = &bat_priv->bla.bcast_duplist[curr];
  1374. /* we can stop searching if the entry is too old ;
  1375. * later entries will be even older
  1376. */
  1377. if (batadv_has_timed_out(entry->entrytime,
  1378. BATADV_DUPLIST_TIMEOUT))
  1379. break;
  1380. if (entry->crc != crc)
  1381. continue;
  1382. if (batadv_compare_eth(entry->orig, bcast_packet->orig))
  1383. continue;
  1384. /* this entry seems to match: same crc, not too old,
  1385. * and from another gw. therefore return true to forbid it.
  1386. */
  1387. ret = true;
  1388. goto out;
  1389. }
  1390. /* not found, add a new entry (overwrite the oldest entry)
  1391. * and allow it, its the first occurrence.
  1392. */
  1393. curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
  1394. curr %= BATADV_DUPLIST_SIZE;
  1395. entry = &bat_priv->bla.bcast_duplist[curr];
  1396. entry->crc = crc;
  1397. entry->entrytime = jiffies;
  1398. ether_addr_copy(entry->orig, bcast_packet->orig);
  1399. bat_priv->bla.bcast_duplist_curr = curr;
  1400. out:
  1401. spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
  1402. return ret;
  1403. }
  1404. /**
  1405. * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
  1406. * the VLAN identified by vid.
  1407. * @bat_priv: the bat priv with all the soft interface information
  1408. * @orig: originator mac address
  1409. * @vid: VLAN identifier
  1410. *
  1411. * Return: true if orig is a backbone for this vid, false otherwise.
  1412. */
  1413. bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
  1414. unsigned short vid)
  1415. {
  1416. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1417. struct hlist_head *head;
  1418. struct batadv_bla_backbone_gw *backbone_gw;
  1419. int i;
  1420. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1421. return false;
  1422. if (!hash)
  1423. return false;
  1424. for (i = 0; i < hash->size; i++) {
  1425. head = &hash->table[i];
  1426. rcu_read_lock();
  1427. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1428. if (batadv_compare_eth(backbone_gw->orig, orig) &&
  1429. backbone_gw->vid == vid) {
  1430. rcu_read_unlock();
  1431. return true;
  1432. }
  1433. }
  1434. rcu_read_unlock();
  1435. }
  1436. return false;
  1437. }
  1438. /**
  1439. * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
  1440. * @skb: the frame to be checked
  1441. * @orig_node: the orig_node of the frame
  1442. * @hdr_size: maximum length of the frame
  1443. *
  1444. * Return: true if the orig_node is also a gateway on the soft interface,
  1445. * otherwise it returns false.
  1446. */
  1447. bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1448. struct batadv_orig_node *orig_node, int hdr_size)
  1449. {
  1450. struct batadv_bla_backbone_gw *backbone_gw;
  1451. unsigned short vid;
  1452. if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
  1453. return false;
  1454. /* first, find out the vid. */
  1455. if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
  1456. return false;
  1457. vid = batadv_get_vid(skb, hdr_size);
  1458. /* see if this originator is a backbone gw for this VLAN */
  1459. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1460. orig_node->orig, vid);
  1461. if (!backbone_gw)
  1462. return false;
  1463. batadv_backbone_gw_put(backbone_gw);
  1464. return true;
  1465. }
  1466. /**
  1467. * batadv_bla_free - free all bla structures
  1468. * @bat_priv: the bat priv with all the soft interface information
  1469. *
  1470. * for softinterface free or module unload
  1471. */
  1472. void batadv_bla_free(struct batadv_priv *bat_priv)
  1473. {
  1474. struct batadv_hard_iface *primary_if;
  1475. cancel_delayed_work_sync(&bat_priv->bla.work);
  1476. primary_if = batadv_primary_if_get_selected(bat_priv);
  1477. if (bat_priv->bla.claim_hash) {
  1478. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1479. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1480. bat_priv->bla.claim_hash = NULL;
  1481. }
  1482. if (bat_priv->bla.backbone_hash) {
  1483. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1484. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1485. bat_priv->bla.backbone_hash = NULL;
  1486. }
  1487. if (primary_if)
  1488. batadv_hardif_put(primary_if);
  1489. }
  1490. /**
  1491. * batadv_bla_loopdetect_check - check and handle a detected loop
  1492. * @bat_priv: the bat priv with all the soft interface information
  1493. * @skb: the packet to check
  1494. * @primary_if: interface where the request came on
  1495. * @vid: the VLAN ID of the frame
  1496. *
  1497. * Checks if this packet is a loop detect frame which has been sent by us,
  1498. * throw an uevent and log the event if that is the case.
  1499. *
  1500. * Return: true if it is a loop detect frame which is to be dropped, false
  1501. * otherwise.
  1502. */
  1503. static bool
  1504. batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1505. struct batadv_hard_iface *primary_if,
  1506. unsigned short vid)
  1507. {
  1508. struct batadv_bla_backbone_gw *backbone_gw;
  1509. struct ethhdr *ethhdr;
  1510. ethhdr = eth_hdr(skb);
  1511. /* Only check for the MAC address and skip more checks here for
  1512. * performance reasons - this function is on the hotpath, after all.
  1513. */
  1514. if (!batadv_compare_eth(ethhdr->h_source,
  1515. bat_priv->bla.loopdetect_addr))
  1516. return false;
  1517. /* If the packet came too late, don't forward it on the mesh
  1518. * but don't consider that as loop. It might be a coincidence.
  1519. */
  1520. if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
  1521. BATADV_BLA_LOOPDETECT_TIMEOUT))
  1522. return true;
  1523. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  1524. primary_if->net_dev->dev_addr,
  1525. vid, true);
  1526. if (unlikely(!backbone_gw))
  1527. return true;
  1528. queue_work(batadv_event_workqueue, &backbone_gw->report_work);
  1529. /* backbone_gw is unreferenced in the report work function function */
  1530. return true;
  1531. }
  1532. /**
  1533. * batadv_bla_rx - check packets coming from the mesh.
  1534. * @bat_priv: the bat priv with all the soft interface information
  1535. * @skb: the frame to be checked
  1536. * @vid: the VLAN ID of the frame
  1537. * @is_bcast: the packet came in a broadcast packet type.
  1538. *
  1539. * batadv_bla_rx avoidance checks if:
  1540. * * we have to race for a claim
  1541. * * if the frame is allowed on the LAN
  1542. *
  1543. * in these cases, the skb is further handled by this function
  1544. *
  1545. * Return: true if handled, otherwise it returns false and the caller shall
  1546. * further process the skb.
  1547. */
  1548. bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1549. unsigned short vid, bool is_bcast)
  1550. {
  1551. struct batadv_bla_backbone_gw *backbone_gw;
  1552. struct ethhdr *ethhdr;
  1553. struct batadv_bla_claim search_claim, *claim = NULL;
  1554. struct batadv_hard_iface *primary_if;
  1555. bool own_claim;
  1556. bool ret;
  1557. ethhdr = eth_hdr(skb);
  1558. primary_if = batadv_primary_if_get_selected(bat_priv);
  1559. if (!primary_if)
  1560. goto handled;
  1561. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1562. goto allow;
  1563. if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
  1564. goto handled;
  1565. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1566. /* don't allow broadcasts while requests are in flight */
  1567. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
  1568. goto handled;
  1569. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1570. search_claim.vid = vid;
  1571. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1572. if (!claim) {
  1573. /* possible optimization: race for a claim */
  1574. /* No claim exists yet, claim it for us!
  1575. */
  1576. batadv_handle_claim(bat_priv, primary_if,
  1577. primary_if->net_dev->dev_addr,
  1578. ethhdr->h_source, vid);
  1579. goto allow;
  1580. }
  1581. /* if it is our own claim ... */
  1582. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1583. own_claim = batadv_compare_eth(backbone_gw->orig,
  1584. primary_if->net_dev->dev_addr);
  1585. batadv_backbone_gw_put(backbone_gw);
  1586. if (own_claim) {
  1587. /* ... allow it in any case */
  1588. claim->lasttime = jiffies;
  1589. goto allow;
  1590. }
  1591. /* if it is a broadcast ... */
  1592. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
  1593. /* ... drop it. the responsible gateway is in charge.
  1594. *
  1595. * We need to check is_bcast because with the gateway
  1596. * feature, broadcasts (like DHCP requests) may be sent
  1597. * using a unicast packet type.
  1598. */
  1599. goto handled;
  1600. } else {
  1601. /* seems the client considers us as its best gateway.
  1602. * send a claim and update the claim table
  1603. * immediately.
  1604. */
  1605. batadv_handle_claim(bat_priv, primary_if,
  1606. primary_if->net_dev->dev_addr,
  1607. ethhdr->h_source, vid);
  1608. goto allow;
  1609. }
  1610. allow:
  1611. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1612. ret = false;
  1613. goto out;
  1614. handled:
  1615. kfree_skb(skb);
  1616. ret = true;
  1617. out:
  1618. if (primary_if)
  1619. batadv_hardif_put(primary_if);
  1620. if (claim)
  1621. batadv_claim_put(claim);
  1622. return ret;
  1623. }
  1624. /**
  1625. * batadv_bla_tx - check packets going into the mesh
  1626. * @bat_priv: the bat priv with all the soft interface information
  1627. * @skb: the frame to be checked
  1628. * @vid: the VLAN ID of the frame
  1629. *
  1630. * batadv_bla_tx checks if:
  1631. * * a claim was received which has to be processed
  1632. * * the frame is allowed on the mesh
  1633. *
  1634. * in these cases, the skb is further handled by this function.
  1635. *
  1636. * This call might reallocate skb data.
  1637. *
  1638. * Return: true if handled, otherwise it returns false and the caller shall
  1639. * further process the skb.
  1640. */
  1641. bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1642. unsigned short vid)
  1643. {
  1644. struct ethhdr *ethhdr;
  1645. struct batadv_bla_claim search_claim, *claim = NULL;
  1646. struct batadv_bla_backbone_gw *backbone_gw;
  1647. struct batadv_hard_iface *primary_if;
  1648. bool client_roamed;
  1649. bool ret = false;
  1650. primary_if = batadv_primary_if_get_selected(bat_priv);
  1651. if (!primary_if)
  1652. goto out;
  1653. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1654. goto allow;
  1655. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1656. goto handled;
  1657. ethhdr = eth_hdr(skb);
  1658. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1659. /* don't allow broadcasts while requests are in flight */
  1660. if (is_multicast_ether_addr(ethhdr->h_dest))
  1661. goto handled;
  1662. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1663. search_claim.vid = vid;
  1664. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1665. /* if no claim exists, allow it. */
  1666. if (!claim)
  1667. goto allow;
  1668. /* check if we are responsible. */
  1669. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1670. client_roamed = batadv_compare_eth(backbone_gw->orig,
  1671. primary_if->net_dev->dev_addr);
  1672. batadv_backbone_gw_put(backbone_gw);
  1673. if (client_roamed) {
  1674. /* if yes, the client has roamed and we have
  1675. * to unclaim it.
  1676. */
  1677. if (batadv_has_timed_out(claim->lasttime, 100)) {
  1678. /* only unclaim if the last claim entry is
  1679. * older than 100 ms to make sure we really
  1680. * have a roaming client here.
  1681. */
  1682. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_tx(): Roaming client %pM detected. Unclaim it.\n",
  1683. ethhdr->h_source);
  1684. batadv_handle_unclaim(bat_priv, primary_if,
  1685. primary_if->net_dev->dev_addr,
  1686. ethhdr->h_source, vid);
  1687. goto allow;
  1688. } else {
  1689. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_tx(): Race for claim %pM detected. Drop packet.\n",
  1690. ethhdr->h_source);
  1691. goto handled;
  1692. }
  1693. }
  1694. /* check if it is a multicast/broadcast frame */
  1695. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1696. /* drop it. the responsible gateway has forwarded it into
  1697. * the backbone network.
  1698. */
  1699. goto handled;
  1700. } else {
  1701. /* we must allow it. at least if we are
  1702. * responsible for the DESTINATION.
  1703. */
  1704. goto allow;
  1705. }
  1706. allow:
  1707. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1708. ret = false;
  1709. goto out;
  1710. handled:
  1711. ret = true;
  1712. out:
  1713. if (primary_if)
  1714. batadv_hardif_put(primary_if);
  1715. if (claim)
  1716. batadv_claim_put(claim);
  1717. return ret;
  1718. }
  1719. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1720. /**
  1721. * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
  1722. * @seq: seq file to print on
  1723. * @offset: not used
  1724. *
  1725. * Return: always 0
  1726. */
  1727. int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
  1728. {
  1729. struct net_device *net_dev = (struct net_device *)seq->private;
  1730. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1731. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  1732. struct batadv_bla_backbone_gw *backbone_gw;
  1733. struct batadv_bla_claim *claim;
  1734. struct batadv_hard_iface *primary_if;
  1735. struct hlist_head *head;
  1736. u16 backbone_crc;
  1737. u32 i;
  1738. bool is_own;
  1739. u8 *primary_addr;
  1740. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1741. if (!primary_if)
  1742. goto out;
  1743. primary_addr = primary_if->net_dev->dev_addr;
  1744. seq_printf(seq,
  1745. "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1746. net_dev->name, primary_addr,
  1747. ntohs(bat_priv->bla.claim_dest.group));
  1748. seq_puts(seq,
  1749. " Client VID Originator [o] (CRC )\n");
  1750. for (i = 0; i < hash->size; i++) {
  1751. head = &hash->table[i];
  1752. rcu_read_lock();
  1753. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1754. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1755. is_own = batadv_compare_eth(backbone_gw->orig,
  1756. primary_addr);
  1757. spin_lock_bh(&backbone_gw->crc_lock);
  1758. backbone_crc = backbone_gw->crc;
  1759. spin_unlock_bh(&backbone_gw->crc_lock);
  1760. seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
  1761. claim->addr, BATADV_PRINT_VID(claim->vid),
  1762. backbone_gw->orig,
  1763. (is_own ? 'x' : ' '),
  1764. backbone_crc);
  1765. batadv_backbone_gw_put(backbone_gw);
  1766. }
  1767. rcu_read_unlock();
  1768. }
  1769. out:
  1770. if (primary_if)
  1771. batadv_hardif_put(primary_if);
  1772. return 0;
  1773. }
  1774. #endif
  1775. /**
  1776. * batadv_bla_claim_dump_entry - dump one entry of the claim table
  1777. * to a netlink socket
  1778. * @msg: buffer for the message
  1779. * @portid: netlink port
  1780. * @seq: Sequence number of netlink message
  1781. * @primary_if: primary interface
  1782. * @claim: entry to dump
  1783. *
  1784. * Return: 0 or error code.
  1785. */
  1786. static int
  1787. batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1788. struct batadv_hard_iface *primary_if,
  1789. struct batadv_bla_claim *claim)
  1790. {
  1791. u8 *primary_addr = primary_if->net_dev->dev_addr;
  1792. u16 backbone_crc;
  1793. bool is_own;
  1794. void *hdr;
  1795. int ret = -EINVAL;
  1796. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1797. NLM_F_MULTI, BATADV_CMD_GET_BLA_CLAIM);
  1798. if (!hdr) {
  1799. ret = -ENOBUFS;
  1800. goto out;
  1801. }
  1802. is_own = batadv_compare_eth(claim->backbone_gw->orig,
  1803. primary_addr);
  1804. spin_lock_bh(&claim->backbone_gw->crc_lock);
  1805. backbone_crc = claim->backbone_gw->crc;
  1806. spin_unlock_bh(&claim->backbone_gw->crc_lock);
  1807. if (is_own)
  1808. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  1809. genlmsg_cancel(msg, hdr);
  1810. goto out;
  1811. }
  1812. if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
  1813. nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
  1814. nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  1815. claim->backbone_gw->orig) ||
  1816. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  1817. backbone_crc)) {
  1818. genlmsg_cancel(msg, hdr);
  1819. goto out;
  1820. }
  1821. genlmsg_end(msg, hdr);
  1822. ret = 0;
  1823. out:
  1824. return ret;
  1825. }
  1826. /**
  1827. * batadv_bla_claim_dump_bucket - dump one bucket of the claim table
  1828. * to a netlink socket
  1829. * @msg: buffer for the message
  1830. * @portid: netlink port
  1831. * @seq: Sequence number of netlink message
  1832. * @primary_if: primary interface
  1833. * @head: bucket to dump
  1834. * @idx_skip: How many entries to skip
  1835. *
  1836. * Return: always 0.
  1837. */
  1838. static int
  1839. batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1840. struct batadv_hard_iface *primary_if,
  1841. struct hlist_head *head, int *idx_skip)
  1842. {
  1843. struct batadv_bla_claim *claim;
  1844. int idx = 0;
  1845. int ret = 0;
  1846. rcu_read_lock();
  1847. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1848. if (idx++ < *idx_skip)
  1849. continue;
  1850. ret = batadv_bla_claim_dump_entry(msg, portid, seq,
  1851. primary_if, claim);
  1852. if (ret) {
  1853. *idx_skip = idx - 1;
  1854. goto unlock;
  1855. }
  1856. }
  1857. *idx_skip = 0;
  1858. unlock:
  1859. rcu_read_unlock();
  1860. return ret;
  1861. }
  1862. /**
  1863. * batadv_bla_claim_dump - dump claim table to a netlink socket
  1864. * @msg: buffer for the message
  1865. * @cb: callback structure containing arguments
  1866. *
  1867. * Return: message length.
  1868. */
  1869. int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1870. {
  1871. struct batadv_hard_iface *primary_if = NULL;
  1872. int portid = NETLINK_CB(cb->skb).portid;
  1873. struct net *net = sock_net(cb->skb->sk);
  1874. struct net_device *soft_iface;
  1875. struct batadv_hashtable *hash;
  1876. struct batadv_priv *bat_priv;
  1877. int bucket = cb->args[0];
  1878. struct hlist_head *head;
  1879. int idx = cb->args[1];
  1880. int ifindex;
  1881. int ret = 0;
  1882. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  1883. BATADV_ATTR_MESH_IFINDEX);
  1884. if (!ifindex)
  1885. return -EINVAL;
  1886. soft_iface = dev_get_by_index(net, ifindex);
  1887. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1888. ret = -ENODEV;
  1889. goto out;
  1890. }
  1891. bat_priv = netdev_priv(soft_iface);
  1892. hash = bat_priv->bla.claim_hash;
  1893. primary_if = batadv_primary_if_get_selected(bat_priv);
  1894. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1895. ret = -ENOENT;
  1896. goto out;
  1897. }
  1898. while (bucket < hash->size) {
  1899. head = &hash->table[bucket];
  1900. if (batadv_bla_claim_dump_bucket(msg, portid,
  1901. cb->nlh->nlmsg_seq,
  1902. primary_if, head, &idx))
  1903. break;
  1904. bucket++;
  1905. }
  1906. cb->args[0] = bucket;
  1907. cb->args[1] = idx;
  1908. ret = msg->len;
  1909. out:
  1910. if (primary_if)
  1911. batadv_hardif_put(primary_if);
  1912. if (soft_iface)
  1913. dev_put(soft_iface);
  1914. return ret;
  1915. }
  1916. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1917. /**
  1918. * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
  1919. * file
  1920. * @seq: seq file to print on
  1921. * @offset: not used
  1922. *
  1923. * Return: always 0
  1924. */
  1925. int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
  1926. {
  1927. struct net_device *net_dev = (struct net_device *)seq->private;
  1928. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1929. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1930. struct batadv_bla_backbone_gw *backbone_gw;
  1931. struct batadv_hard_iface *primary_if;
  1932. struct hlist_head *head;
  1933. int secs, msecs;
  1934. u16 backbone_crc;
  1935. u32 i;
  1936. bool is_own;
  1937. u8 *primary_addr;
  1938. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1939. if (!primary_if)
  1940. goto out;
  1941. primary_addr = primary_if->net_dev->dev_addr;
  1942. seq_printf(seq,
  1943. "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1944. net_dev->name, primary_addr,
  1945. ntohs(bat_priv->bla.claim_dest.group));
  1946. seq_puts(seq, " Originator VID last seen (CRC )\n");
  1947. for (i = 0; i < hash->size; i++) {
  1948. head = &hash->table[i];
  1949. rcu_read_lock();
  1950. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1951. msecs = jiffies_to_msecs(jiffies -
  1952. backbone_gw->lasttime);
  1953. secs = msecs / 1000;
  1954. msecs = msecs % 1000;
  1955. is_own = batadv_compare_eth(backbone_gw->orig,
  1956. primary_addr);
  1957. if (is_own)
  1958. continue;
  1959. spin_lock_bh(&backbone_gw->crc_lock);
  1960. backbone_crc = backbone_gw->crc;
  1961. spin_unlock_bh(&backbone_gw->crc_lock);
  1962. seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
  1963. backbone_gw->orig,
  1964. BATADV_PRINT_VID(backbone_gw->vid), secs,
  1965. msecs, backbone_crc);
  1966. }
  1967. rcu_read_unlock();
  1968. }
  1969. out:
  1970. if (primary_if)
  1971. batadv_hardif_put(primary_if);
  1972. return 0;
  1973. }
  1974. #endif
  1975. /**
  1976. * batadv_bla_backbone_dump_entry - dump one entry of the backbone table
  1977. * to a netlink socket
  1978. * @msg: buffer for the message
  1979. * @portid: netlink port
  1980. * @seq: Sequence number of netlink message
  1981. * @primary_if: primary interface
  1982. * @backbone_gw: entry to dump
  1983. *
  1984. * Return: 0 or error code.
  1985. */
  1986. static int
  1987. batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1988. struct batadv_hard_iface *primary_if,
  1989. struct batadv_bla_backbone_gw *backbone_gw)
  1990. {
  1991. u8 *primary_addr = primary_if->net_dev->dev_addr;
  1992. u16 backbone_crc;
  1993. bool is_own;
  1994. int msecs;
  1995. void *hdr;
  1996. int ret = -EINVAL;
  1997. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1998. NLM_F_MULTI, BATADV_CMD_GET_BLA_BACKBONE);
  1999. if (!hdr) {
  2000. ret = -ENOBUFS;
  2001. goto out;
  2002. }
  2003. is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
  2004. spin_lock_bh(&backbone_gw->crc_lock);
  2005. backbone_crc = backbone_gw->crc;
  2006. spin_unlock_bh(&backbone_gw->crc_lock);
  2007. msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
  2008. if (is_own)
  2009. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  2010. genlmsg_cancel(msg, hdr);
  2011. goto out;
  2012. }
  2013. if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  2014. backbone_gw->orig) ||
  2015. nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
  2016. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  2017. backbone_crc) ||
  2018. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
  2019. genlmsg_cancel(msg, hdr);
  2020. goto out;
  2021. }
  2022. genlmsg_end(msg, hdr);
  2023. ret = 0;
  2024. out:
  2025. return ret;
  2026. }
  2027. /**
  2028. * batadv_bla_backbone_dump_bucket - dump one bucket of the backbone table
  2029. * to a netlink socket
  2030. * @msg: buffer for the message
  2031. * @portid: netlink port
  2032. * @seq: Sequence number of netlink message
  2033. * @primary_if: primary interface
  2034. * @head: bucket to dump
  2035. * @idx_skip: How many entries to skip
  2036. *
  2037. * Return: always 0.
  2038. */
  2039. static int
  2040. batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  2041. struct batadv_hard_iface *primary_if,
  2042. struct hlist_head *head, int *idx_skip)
  2043. {
  2044. struct batadv_bla_backbone_gw *backbone_gw;
  2045. int idx = 0;
  2046. int ret = 0;
  2047. rcu_read_lock();
  2048. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  2049. if (idx++ < *idx_skip)
  2050. continue;
  2051. ret = batadv_bla_backbone_dump_entry(msg, portid, seq,
  2052. primary_if, backbone_gw);
  2053. if (ret) {
  2054. *idx_skip = idx - 1;
  2055. goto unlock;
  2056. }
  2057. }
  2058. *idx_skip = 0;
  2059. unlock:
  2060. rcu_read_unlock();
  2061. return ret;
  2062. }
  2063. /**
  2064. * batadv_bla_backbone_dump - dump backbone table to a netlink socket
  2065. * @msg: buffer for the message
  2066. * @cb: callback structure containing arguments
  2067. *
  2068. * Return: message length.
  2069. */
  2070. int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
  2071. {
  2072. struct batadv_hard_iface *primary_if = NULL;
  2073. int portid = NETLINK_CB(cb->skb).portid;
  2074. struct net *net = sock_net(cb->skb->sk);
  2075. struct net_device *soft_iface;
  2076. struct batadv_hashtable *hash;
  2077. struct batadv_priv *bat_priv;
  2078. int bucket = cb->args[0];
  2079. struct hlist_head *head;
  2080. int idx = cb->args[1];
  2081. int ifindex;
  2082. int ret = 0;
  2083. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  2084. BATADV_ATTR_MESH_IFINDEX);
  2085. if (!ifindex)
  2086. return -EINVAL;
  2087. soft_iface = dev_get_by_index(net, ifindex);
  2088. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  2089. ret = -ENODEV;
  2090. goto out;
  2091. }
  2092. bat_priv = netdev_priv(soft_iface);
  2093. hash = bat_priv->bla.backbone_hash;
  2094. primary_if = batadv_primary_if_get_selected(bat_priv);
  2095. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  2096. ret = -ENOENT;
  2097. goto out;
  2098. }
  2099. while (bucket < hash->size) {
  2100. head = &hash->table[bucket];
  2101. if (batadv_bla_backbone_dump_bucket(msg, portid,
  2102. cb->nlh->nlmsg_seq,
  2103. primary_if, head, &idx))
  2104. break;
  2105. bucket++;
  2106. }
  2107. cb->args[0] = bucket;
  2108. cb->args[1] = idx;
  2109. ret = msg->len;
  2110. out:
  2111. if (primary_if)
  2112. batadv_hardif_put(primary_if);
  2113. if (soft_iface)
  2114. dev_put(soft_iface);
  2115. return ret;
  2116. }