bridge_loop_avoidance.c 73 KB

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