bridge_loop_avoidance.c 68 KB

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