bridge_loop_avoidance.c 69 KB

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