bridge_loop_avoidance.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  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. "%s(): CLAIM %pM on vid %d\n", __func__, 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. "%s(): UNCLAIM %pM on vid %d\n", __func__, 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. "%s(): ANNOUNCE of %pM on vid %d\n", __func__,
  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. "%s(): REQUEST of %pM to %pM on vid %d\n", __func__,
  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. "%s(): LOOPDETECT of %pM to %pM on vid %d\n",
  375. __func__, 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. "%s(): not found (%pM, %d), creating new entry\n", __func__,
  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. "%s(): received a claim request, send all of our own claims again\n",
  524. __func__);
  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. "%s(): adding new entry %pM, vid %d to hash ...\n",
  621. __func__, 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. "%s(): changing ownership for %pM, vid %d to gw %pM\n",
  639. __func__, mac, batadv_print_vid(vid),
  640. backbone_gw->orig);
  641. remove_crc = true;
  642. }
  643. /* replace backbone_gw atomically and adjust reference counters */
  644. spin_lock_bh(&claim->backbone_lock);
  645. old_backbone_gw = claim->backbone_gw;
  646. kref_get(&backbone_gw->refcount);
  647. claim->backbone_gw = backbone_gw;
  648. spin_unlock_bh(&claim->backbone_lock);
  649. if (remove_crc) {
  650. /* remove claim address from old backbone_gw */
  651. spin_lock_bh(&old_backbone_gw->crc_lock);
  652. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  653. spin_unlock_bh(&old_backbone_gw->crc_lock);
  654. }
  655. batadv_backbone_gw_put(old_backbone_gw);
  656. /* add claim address to new backbone_gw */
  657. spin_lock_bh(&backbone_gw->crc_lock);
  658. backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  659. spin_unlock_bh(&backbone_gw->crc_lock);
  660. backbone_gw->lasttime = jiffies;
  661. claim_free_ref:
  662. batadv_claim_put(claim);
  663. }
  664. /**
  665. * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
  666. * claim
  667. * @claim: claim whose backbone_gw should be returned
  668. *
  669. * Return: valid reference to claim::backbone_gw
  670. */
  671. static struct batadv_bla_backbone_gw *
  672. batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
  673. {
  674. struct batadv_bla_backbone_gw *backbone_gw;
  675. spin_lock_bh(&claim->backbone_lock);
  676. backbone_gw = claim->backbone_gw;
  677. kref_get(&backbone_gw->refcount);
  678. spin_unlock_bh(&claim->backbone_lock);
  679. return backbone_gw;
  680. }
  681. /**
  682. * batadv_bla_del_claim - delete a claim from the claim hash
  683. * @bat_priv: the bat priv with all the soft interface information
  684. * @mac: mac address of the claim to be removed
  685. * @vid: VLAN id for the claim to be removed
  686. */
  687. static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
  688. const u8 *mac, const unsigned short vid)
  689. {
  690. struct batadv_bla_claim search_claim, *claim;
  691. ether_addr_copy(search_claim.addr, mac);
  692. search_claim.vid = vid;
  693. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  694. if (!claim)
  695. return;
  696. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,
  697. mac, batadv_print_vid(vid));
  698. batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
  699. batadv_choose_claim, claim);
  700. batadv_claim_put(claim); /* reference from the hash is gone */
  701. /* don't need the reference from hash_find() anymore */
  702. batadv_claim_put(claim);
  703. }
  704. /**
  705. * batadv_handle_announce - check for ANNOUNCE frame
  706. * @bat_priv: the bat priv with all the soft interface information
  707. * @an_addr: announcement mac address (ARP Sender HW address)
  708. * @backbone_addr: originator address of the sender (Ethernet source MAC)
  709. * @vid: the VLAN ID of the frame
  710. *
  711. * Return: true if handled
  712. */
  713. static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
  714. u8 *backbone_addr, unsigned short vid)
  715. {
  716. struct batadv_bla_backbone_gw *backbone_gw;
  717. u16 backbone_crc, crc;
  718. if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
  719. return false;
  720. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  721. false);
  722. if (unlikely(!backbone_gw))
  723. return true;
  724. /* handle as ANNOUNCE frame */
  725. backbone_gw->lasttime = jiffies;
  726. crc = ntohs(*((__be16 *)(&an_addr[4])));
  727. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  728. "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
  729. __func__, batadv_print_vid(vid), backbone_gw->orig, crc);
  730. spin_lock_bh(&backbone_gw->crc_lock);
  731. backbone_crc = backbone_gw->crc;
  732. spin_unlock_bh(&backbone_gw->crc_lock);
  733. if (backbone_crc != crc) {
  734. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  735. "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
  736. __func__, backbone_gw->orig,
  737. batadv_print_vid(backbone_gw->vid),
  738. backbone_crc, crc);
  739. batadv_bla_send_request(backbone_gw);
  740. } else {
  741. /* if we have sent a request and the crc was OK,
  742. * we can allow traffic again.
  743. */
  744. if (atomic_read(&backbone_gw->request_sent)) {
  745. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  746. atomic_set(&backbone_gw->request_sent, 0);
  747. }
  748. }
  749. batadv_backbone_gw_put(backbone_gw);
  750. return true;
  751. }
  752. /**
  753. * batadv_handle_request - check for REQUEST frame
  754. * @bat_priv: the bat priv with all the soft interface information
  755. * @primary_if: the primary hard interface of this batman soft interface
  756. * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
  757. * @ethhdr: ethernet header of a packet
  758. * @vid: the VLAN ID of the frame
  759. *
  760. * Return: true if handled
  761. */
  762. static bool batadv_handle_request(struct batadv_priv *bat_priv,
  763. struct batadv_hard_iface *primary_if,
  764. u8 *backbone_addr, struct ethhdr *ethhdr,
  765. unsigned short vid)
  766. {
  767. /* check for REQUEST frame */
  768. if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
  769. return false;
  770. /* sanity check, this should not happen on a normal switch,
  771. * we ignore it in this case.
  772. */
  773. if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
  774. return true;
  775. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  776. "%s(): REQUEST vid %d (sent by %pM)...\n",
  777. __func__, batadv_print_vid(vid), ethhdr->h_source);
  778. batadv_bla_answer_request(bat_priv, primary_if, vid);
  779. return true;
  780. }
  781. /**
  782. * batadv_handle_unclaim - check for UNCLAIM frame
  783. * @bat_priv: the bat priv with all the soft interface information
  784. * @primary_if: the primary hard interface of this batman soft interface
  785. * @backbone_addr: originator address of the backbone (Ethernet source)
  786. * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
  787. * @vid: the VLAN ID of the frame
  788. *
  789. * Return: true if handled
  790. */
  791. static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
  792. struct batadv_hard_iface *primary_if,
  793. u8 *backbone_addr, u8 *claim_addr,
  794. unsigned short vid)
  795. {
  796. struct batadv_bla_backbone_gw *backbone_gw;
  797. /* unclaim in any case if it is our own */
  798. if (primary_if && batadv_compare_eth(backbone_addr,
  799. primary_if->net_dev->dev_addr))
  800. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  801. BATADV_CLAIM_TYPE_UNCLAIM);
  802. backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
  803. if (!backbone_gw)
  804. return true;
  805. /* this must be an UNCLAIM frame */
  806. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  807. "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,
  808. claim_addr, batadv_print_vid(vid), backbone_gw->orig);
  809. batadv_bla_del_claim(bat_priv, claim_addr, vid);
  810. batadv_backbone_gw_put(backbone_gw);
  811. return true;
  812. }
  813. /**
  814. * batadv_handle_claim - check for CLAIM frame
  815. * @bat_priv: the bat priv with all the soft interface information
  816. * @primary_if: the primary hard interface of this batman soft interface
  817. * @backbone_addr: originator address of the backbone (Ethernet Source)
  818. * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
  819. * @vid: the VLAN ID of the frame
  820. *
  821. * Return: true if handled
  822. */
  823. static bool batadv_handle_claim(struct batadv_priv *bat_priv,
  824. struct batadv_hard_iface *primary_if,
  825. u8 *backbone_addr, u8 *claim_addr,
  826. unsigned short vid)
  827. {
  828. struct batadv_bla_backbone_gw *backbone_gw;
  829. /* register the gateway if not yet available, and add the claim. */
  830. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  831. false);
  832. if (unlikely(!backbone_gw))
  833. return true;
  834. /* this must be a CLAIM frame */
  835. batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
  836. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  837. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  838. BATADV_CLAIM_TYPE_CLAIM);
  839. /* TODO: we could call something like tt_local_del() here. */
  840. batadv_backbone_gw_put(backbone_gw);
  841. return true;
  842. }
  843. /**
  844. * batadv_check_claim_group - check for claim group membership
  845. * @bat_priv: the bat priv with all the soft interface information
  846. * @primary_if: the primary interface of this batman interface
  847. * @hw_src: the Hardware source in the ARP Header
  848. * @hw_dst: the Hardware destination in the ARP Header
  849. * @ethhdr: pointer to the Ethernet header of the claim frame
  850. *
  851. * checks if it is a claim packet and if its on the same group.
  852. * This function also applies the group ID of the sender
  853. * if it is in the same mesh.
  854. *
  855. * Return:
  856. * 2 - if it is a claim packet and on the same group
  857. * 1 - if is a claim packet from another group
  858. * 0 - if it is not a claim packet
  859. */
  860. static int batadv_check_claim_group(struct batadv_priv *bat_priv,
  861. struct batadv_hard_iface *primary_if,
  862. u8 *hw_src, u8 *hw_dst,
  863. struct ethhdr *ethhdr)
  864. {
  865. u8 *backbone_addr;
  866. struct batadv_orig_node *orig_node;
  867. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  868. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  869. bla_dst_own = &bat_priv->bla.claim_dest;
  870. /* if announcement packet, use the source,
  871. * otherwise assume it is in the hw_src
  872. */
  873. switch (bla_dst->type) {
  874. case BATADV_CLAIM_TYPE_CLAIM:
  875. backbone_addr = hw_src;
  876. break;
  877. case BATADV_CLAIM_TYPE_REQUEST:
  878. case BATADV_CLAIM_TYPE_ANNOUNCE:
  879. case BATADV_CLAIM_TYPE_UNCLAIM:
  880. backbone_addr = ethhdr->h_source;
  881. break;
  882. default:
  883. return 0;
  884. }
  885. /* don't accept claim frames from ourselves */
  886. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  887. return 0;
  888. /* if its already the same group, it is fine. */
  889. if (bla_dst->group == bla_dst_own->group)
  890. return 2;
  891. /* lets see if this originator is in our mesh */
  892. orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
  893. /* dont accept claims from gateways which are not in
  894. * the same mesh or group.
  895. */
  896. if (!orig_node)
  897. return 1;
  898. /* if our mesh friends mac is bigger, use it for ourselves. */
  899. if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
  900. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  901. "taking other backbones claim group: %#.4x\n",
  902. ntohs(bla_dst->group));
  903. bla_dst_own->group = bla_dst->group;
  904. }
  905. batadv_orig_node_put(orig_node);
  906. return 2;
  907. }
  908. /**
  909. * batadv_bla_process_claim - Check if this is a claim frame, and process it
  910. * @bat_priv: the bat priv with all the soft interface information
  911. * @primary_if: the primary hard interface of this batman soft interface
  912. * @skb: the frame to be checked
  913. *
  914. * Return: true if it was a claim frame, otherwise return false to
  915. * tell the callee that it can use the frame on its own.
  916. */
  917. static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
  918. struct batadv_hard_iface *primary_if,
  919. struct sk_buff *skb)
  920. {
  921. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  922. u8 *hw_src, *hw_dst;
  923. struct vlan_hdr *vhdr, vhdr_buf;
  924. struct ethhdr *ethhdr;
  925. struct arphdr *arphdr;
  926. unsigned short vid;
  927. int vlan_depth = 0;
  928. __be16 proto;
  929. int headlen;
  930. int ret;
  931. vid = batadv_get_vid(skb, 0);
  932. ethhdr = eth_hdr(skb);
  933. proto = ethhdr->h_proto;
  934. headlen = ETH_HLEN;
  935. if (vid & BATADV_VLAN_HAS_TAG) {
  936. /* Traverse the VLAN/Ethertypes.
  937. *
  938. * At this point it is known that the first protocol is a VLAN
  939. * header, so start checking at the encapsulated protocol.
  940. *
  941. * The depth of the VLAN headers is recorded to drop BLA claim
  942. * frames encapsulated into multiple VLAN headers (QinQ).
  943. */
  944. do {
  945. vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
  946. &vhdr_buf);
  947. if (!vhdr)
  948. return false;
  949. proto = vhdr->h_vlan_encapsulated_proto;
  950. headlen += VLAN_HLEN;
  951. vlan_depth++;
  952. } while (proto == htons(ETH_P_8021Q));
  953. }
  954. if (proto != htons(ETH_P_ARP))
  955. return false; /* not a claim frame */
  956. /* this must be a ARP frame. check if it is a claim. */
  957. if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
  958. return false;
  959. /* pskb_may_pull() may have modified the pointers, get ethhdr again */
  960. ethhdr = eth_hdr(skb);
  961. arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
  962. /* Check whether the ARP frame carries a valid
  963. * IP information
  964. */
  965. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  966. return false;
  967. if (arphdr->ar_pro != htons(ETH_P_IP))
  968. return false;
  969. if (arphdr->ar_hln != ETH_ALEN)
  970. return false;
  971. if (arphdr->ar_pln != 4)
  972. return false;
  973. hw_src = (u8 *)arphdr + sizeof(struct arphdr);
  974. hw_dst = hw_src + ETH_ALEN + 4;
  975. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  976. bla_dst_own = &bat_priv->bla.claim_dest;
  977. /* check if it is a claim frame in general */
  978. if (memcmp(bla_dst->magic, bla_dst_own->magic,
  979. sizeof(bla_dst->magic)) != 0)
  980. return false;
  981. /* check if there is a claim frame encapsulated deeper in (QinQ) and
  982. * drop that, as this is not supported by BLA but should also not be
  983. * sent via the mesh.
  984. */
  985. if (vlan_depth > 1)
  986. return true;
  987. /* Let the loopdetect frames on the mesh in any case. */
  988. if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
  989. return false;
  990. /* check if it is a claim frame. */
  991. ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
  992. ethhdr);
  993. if (ret == 1)
  994. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  995. "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  996. __func__, ethhdr->h_source, batadv_print_vid(vid),
  997. hw_src, hw_dst);
  998. if (ret < 2)
  999. return !!ret;
  1000. /* become a backbone gw ourselves on this vlan if not happened yet */
  1001. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1002. /* check for the different types of claim frames ... */
  1003. switch (bla_dst->type) {
  1004. case BATADV_CLAIM_TYPE_CLAIM:
  1005. if (batadv_handle_claim(bat_priv, primary_if, hw_src,
  1006. ethhdr->h_source, vid))
  1007. return true;
  1008. break;
  1009. case BATADV_CLAIM_TYPE_UNCLAIM:
  1010. if (batadv_handle_unclaim(bat_priv, primary_if,
  1011. ethhdr->h_source, hw_src, vid))
  1012. return true;
  1013. break;
  1014. case BATADV_CLAIM_TYPE_ANNOUNCE:
  1015. if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
  1016. vid))
  1017. return true;
  1018. break;
  1019. case BATADV_CLAIM_TYPE_REQUEST:
  1020. if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
  1021. vid))
  1022. return true;
  1023. break;
  1024. }
  1025. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1026. "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  1027. __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,
  1028. hw_dst);
  1029. return true;
  1030. }
  1031. /**
  1032. * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
  1033. * immediately
  1034. * @bat_priv: the bat priv with all the soft interface information
  1035. * @now: whether the whole hash shall be wiped now
  1036. *
  1037. * Check when we last heard from other nodes, and remove them in case of
  1038. * a time out, or clean all backbone gws if now is set.
  1039. */
  1040. static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
  1041. {
  1042. struct batadv_bla_backbone_gw *backbone_gw;
  1043. struct hlist_node *node_tmp;
  1044. struct hlist_head *head;
  1045. struct batadv_hashtable *hash;
  1046. spinlock_t *list_lock; /* protects write access to the hash lists */
  1047. int i;
  1048. hash = bat_priv->bla.backbone_hash;
  1049. if (!hash)
  1050. return;
  1051. for (i = 0; i < hash->size; i++) {
  1052. head = &hash->table[i];
  1053. list_lock = &hash->list_locks[i];
  1054. spin_lock_bh(list_lock);
  1055. hlist_for_each_entry_safe(backbone_gw, node_tmp,
  1056. head, hash_entry) {
  1057. if (now)
  1058. goto purge_now;
  1059. if (!batadv_has_timed_out(backbone_gw->lasttime,
  1060. BATADV_BLA_BACKBONE_TIMEOUT))
  1061. continue;
  1062. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  1063. "%s(): backbone gw %pM timed out\n",
  1064. __func__, backbone_gw->orig);
  1065. purge_now:
  1066. /* don't wait for the pending request anymore */
  1067. if (atomic_read(&backbone_gw->request_sent))
  1068. atomic_dec(&bat_priv->bla.num_requests);
  1069. batadv_bla_del_backbone_claims(backbone_gw);
  1070. hlist_del_rcu(&backbone_gw->hash_entry);
  1071. batadv_backbone_gw_put(backbone_gw);
  1072. }
  1073. spin_unlock_bh(list_lock);
  1074. }
  1075. }
  1076. /**
  1077. * batadv_bla_purge_claims - Remove claims after a timeout or immediately
  1078. * @bat_priv: the bat priv with all the soft interface information
  1079. * @primary_if: the selected primary interface, may be NULL if now is set
  1080. * @now: whether the whole hash shall be wiped now
  1081. *
  1082. * Check when we heard last time from our own claims, and remove them in case of
  1083. * a time out, or clean all claims if now is set
  1084. */
  1085. static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
  1086. struct batadv_hard_iface *primary_if,
  1087. int now)
  1088. {
  1089. struct batadv_bla_backbone_gw *backbone_gw;
  1090. struct batadv_bla_claim *claim;
  1091. struct hlist_head *head;
  1092. struct batadv_hashtable *hash;
  1093. int i;
  1094. hash = bat_priv->bla.claim_hash;
  1095. if (!hash)
  1096. return;
  1097. for (i = 0; i < hash->size; i++) {
  1098. head = &hash->table[i];
  1099. rcu_read_lock();
  1100. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1101. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1102. if (now)
  1103. goto purge_now;
  1104. if (!batadv_compare_eth(backbone_gw->orig,
  1105. primary_if->net_dev->dev_addr))
  1106. goto skip;
  1107. if (!batadv_has_timed_out(claim->lasttime,
  1108. BATADV_BLA_CLAIM_TIMEOUT))
  1109. goto skip;
  1110. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1111. "%s(): timed out.\n", __func__);
  1112. purge_now:
  1113. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1114. "%s(): %pM, vid %d\n", __func__,
  1115. claim->addr, claim->vid);
  1116. batadv_handle_unclaim(bat_priv, primary_if,
  1117. backbone_gw->orig,
  1118. claim->addr, claim->vid);
  1119. skip:
  1120. batadv_backbone_gw_put(backbone_gw);
  1121. }
  1122. rcu_read_unlock();
  1123. }
  1124. }
  1125. /**
  1126. * batadv_bla_update_orig_address - Update the backbone gateways when the own
  1127. * originator address changes
  1128. * @bat_priv: the bat priv with all the soft interface information
  1129. * @primary_if: the new selected primary_if
  1130. * @oldif: the old primary interface, may be NULL
  1131. */
  1132. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  1133. struct batadv_hard_iface *primary_if,
  1134. struct batadv_hard_iface *oldif)
  1135. {
  1136. struct batadv_bla_backbone_gw *backbone_gw;
  1137. struct hlist_head *head;
  1138. struct batadv_hashtable *hash;
  1139. __be16 group;
  1140. int i;
  1141. /* reset bridge loop avoidance group id */
  1142. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  1143. bat_priv->bla.claim_dest.group = group;
  1144. /* purge everything when bridge loop avoidance is turned off */
  1145. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1146. oldif = NULL;
  1147. if (!oldif) {
  1148. batadv_bla_purge_claims(bat_priv, NULL, 1);
  1149. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1150. return;
  1151. }
  1152. hash = bat_priv->bla.backbone_hash;
  1153. if (!hash)
  1154. return;
  1155. for (i = 0; i < hash->size; i++) {
  1156. head = &hash->table[i];
  1157. rcu_read_lock();
  1158. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1159. /* own orig still holds the old value. */
  1160. if (!batadv_compare_eth(backbone_gw->orig,
  1161. oldif->net_dev->dev_addr))
  1162. continue;
  1163. ether_addr_copy(backbone_gw->orig,
  1164. primary_if->net_dev->dev_addr);
  1165. /* send an announce frame so others will ask for our
  1166. * claims and update their tables.
  1167. */
  1168. batadv_bla_send_announce(bat_priv, backbone_gw);
  1169. }
  1170. rcu_read_unlock();
  1171. }
  1172. }
  1173. /**
  1174. * batadv_bla_send_loopdetect - send a loopdetect frame
  1175. * @bat_priv: the bat priv with all the soft interface information
  1176. * @backbone_gw: the backbone gateway for which a loop should be detected
  1177. *
  1178. * To detect loops that the bridge loop avoidance can't handle, send a loop
  1179. * detection packet on the backbone. Unlike other BLA frames, this frame will
  1180. * be allowed on the mesh by other nodes. If it is received on the mesh, this
  1181. * indicates that there is a loop.
  1182. */
  1183. static void
  1184. batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
  1185. struct batadv_bla_backbone_gw *backbone_gw)
  1186. {
  1187. batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
  1188. backbone_gw->vid);
  1189. batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
  1190. backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
  1191. }
  1192. /**
  1193. * batadv_bla_status_update - purge bla interfaces if necessary
  1194. * @net_dev: the soft interface net device
  1195. */
  1196. void batadv_bla_status_update(struct net_device *net_dev)
  1197. {
  1198. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1199. struct batadv_hard_iface *primary_if;
  1200. primary_if = batadv_primary_if_get_selected(bat_priv);
  1201. if (!primary_if)
  1202. return;
  1203. /* this function already purges everything when bla is disabled,
  1204. * so just call that one.
  1205. */
  1206. batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
  1207. batadv_hardif_put(primary_if);
  1208. }
  1209. /**
  1210. * batadv_bla_periodic_work - performs periodic bla work
  1211. * @work: kernel work struct
  1212. *
  1213. * periodic work to do:
  1214. * * purge structures when they are too old
  1215. * * send announcements
  1216. */
  1217. static void batadv_bla_periodic_work(struct work_struct *work)
  1218. {
  1219. struct delayed_work *delayed_work;
  1220. struct batadv_priv *bat_priv;
  1221. struct batadv_priv_bla *priv_bla;
  1222. struct hlist_head *head;
  1223. struct batadv_bla_backbone_gw *backbone_gw;
  1224. struct batadv_hashtable *hash;
  1225. struct batadv_hard_iface *primary_if;
  1226. bool send_loopdetect = false;
  1227. int i;
  1228. delayed_work = to_delayed_work(work);
  1229. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  1230. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  1231. primary_if = batadv_primary_if_get_selected(bat_priv);
  1232. if (!primary_if)
  1233. goto out;
  1234. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  1235. batadv_bla_purge_backbone_gw(bat_priv, 0);
  1236. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1237. goto out;
  1238. if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
  1239. /* set a new random mac address for the next bridge loop
  1240. * detection frames. Set the locally administered bit to avoid
  1241. * collisions with users mac addresses.
  1242. */
  1243. random_ether_addr(bat_priv->bla.loopdetect_addr);
  1244. bat_priv->bla.loopdetect_addr[0] = 0xba;
  1245. bat_priv->bla.loopdetect_addr[1] = 0xbe;
  1246. bat_priv->bla.loopdetect_lasttime = jiffies;
  1247. atomic_set(&bat_priv->bla.loopdetect_next,
  1248. BATADV_BLA_LOOPDETECT_PERIODS);
  1249. /* mark for sending loop detect on all VLANs */
  1250. send_loopdetect = true;
  1251. }
  1252. hash = bat_priv->bla.backbone_hash;
  1253. if (!hash)
  1254. goto out;
  1255. for (i = 0; i < hash->size; i++) {
  1256. head = &hash->table[i];
  1257. rcu_read_lock();
  1258. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1259. if (!batadv_compare_eth(backbone_gw->orig,
  1260. primary_if->net_dev->dev_addr))
  1261. continue;
  1262. backbone_gw->lasttime = jiffies;
  1263. batadv_bla_send_announce(bat_priv, backbone_gw);
  1264. if (send_loopdetect)
  1265. batadv_bla_send_loopdetect(bat_priv,
  1266. backbone_gw);
  1267. /* request_sent is only set after creation to avoid
  1268. * problems when we are not yet known as backbone gw
  1269. * in the backbone.
  1270. *
  1271. * We can reset this now after we waited some periods
  1272. * to give bridge forward delays and bla group forming
  1273. * some grace time.
  1274. */
  1275. if (atomic_read(&backbone_gw->request_sent) == 0)
  1276. continue;
  1277. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  1278. continue;
  1279. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  1280. atomic_set(&backbone_gw->request_sent, 0);
  1281. }
  1282. rcu_read_unlock();
  1283. }
  1284. out:
  1285. if (primary_if)
  1286. batadv_hardif_put(primary_if);
  1287. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1288. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1289. }
  1290. /* The hash for claim and backbone hash receive the same key because they
  1291. * are getting initialized by hash_new with the same key. Reinitializing
  1292. * them with to different keys to allow nested locking without generating
  1293. * lockdep warnings
  1294. */
  1295. static struct lock_class_key batadv_claim_hash_lock_class_key;
  1296. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  1297. /**
  1298. * batadv_bla_init - initialize all bla structures
  1299. * @bat_priv: the bat priv with all the soft interface information
  1300. *
  1301. * Return: 0 on success, < 0 on error.
  1302. */
  1303. int batadv_bla_init(struct batadv_priv *bat_priv)
  1304. {
  1305. int i;
  1306. u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1307. struct batadv_hard_iface *primary_if;
  1308. u16 crc;
  1309. unsigned long entrytime;
  1310. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1311. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1312. /* setting claim destination address */
  1313. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1314. bat_priv->bla.claim_dest.type = 0;
  1315. primary_if = batadv_primary_if_get_selected(bat_priv);
  1316. if (primary_if) {
  1317. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1318. bat_priv->bla.claim_dest.group = htons(crc);
  1319. batadv_hardif_put(primary_if);
  1320. } else {
  1321. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1322. }
  1323. /* initialize the duplicate list */
  1324. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1325. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1326. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1327. bat_priv->bla.bcast_duplist_curr = 0;
  1328. atomic_set(&bat_priv->bla.loopdetect_next,
  1329. BATADV_BLA_LOOPDETECT_PERIODS);
  1330. if (bat_priv->bla.claim_hash)
  1331. return 0;
  1332. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1333. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1334. if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
  1335. return -ENOMEM;
  1336. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1337. &batadv_claim_hash_lock_class_key);
  1338. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1339. &batadv_backbone_hash_lock_class_key);
  1340. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1341. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  1342. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1343. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1344. return 0;
  1345. }
  1346. /**
  1347. * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
  1348. * @bat_priv: the bat priv with all the soft interface information
  1349. * @skb: contains the bcast_packet to be checked
  1350. *
  1351. * check if it is on our broadcast list. Another gateway might
  1352. * have sent the same packet because it is connected to the same backbone,
  1353. * so we have to remove this duplicate.
  1354. *
  1355. * This is performed by checking the CRC, which will tell us
  1356. * with a good chance that it is the same packet. If it is furthermore
  1357. * sent by another host, drop it. We allow equal packets from
  1358. * the same host however as this might be intended.
  1359. *
  1360. * Return: true if a packet is in the duplicate list, false otherwise.
  1361. */
  1362. bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
  1363. struct sk_buff *skb)
  1364. {
  1365. int i, curr;
  1366. __be32 crc;
  1367. struct batadv_bcast_packet *bcast_packet;
  1368. struct batadv_bcast_duplist_entry *entry;
  1369. bool ret = false;
  1370. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1371. /* calculate the crc ... */
  1372. crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
  1373. spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
  1374. for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
  1375. curr = (bat_priv->bla.bcast_duplist_curr + i);
  1376. curr %= BATADV_DUPLIST_SIZE;
  1377. entry = &bat_priv->bla.bcast_duplist[curr];
  1378. /* we can stop searching if the entry is too old ;
  1379. * later entries will be even older
  1380. */
  1381. if (batadv_has_timed_out(entry->entrytime,
  1382. BATADV_DUPLIST_TIMEOUT))
  1383. break;
  1384. if (entry->crc != crc)
  1385. continue;
  1386. if (batadv_compare_eth(entry->orig, bcast_packet->orig))
  1387. continue;
  1388. /* this entry seems to match: same crc, not too old,
  1389. * and from another gw. therefore return true to forbid it.
  1390. */
  1391. ret = true;
  1392. goto out;
  1393. }
  1394. /* not found, add a new entry (overwrite the oldest entry)
  1395. * and allow it, its the first occurrence.
  1396. */
  1397. curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
  1398. curr %= BATADV_DUPLIST_SIZE;
  1399. entry = &bat_priv->bla.bcast_duplist[curr];
  1400. entry->crc = crc;
  1401. entry->entrytime = jiffies;
  1402. ether_addr_copy(entry->orig, bcast_packet->orig);
  1403. bat_priv->bla.bcast_duplist_curr = curr;
  1404. out:
  1405. spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
  1406. return ret;
  1407. }
  1408. /**
  1409. * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
  1410. * the VLAN identified by vid.
  1411. * @bat_priv: the bat priv with all the soft interface information
  1412. * @orig: originator mac address
  1413. * @vid: VLAN identifier
  1414. *
  1415. * Return: true if orig is a backbone for this vid, false otherwise.
  1416. */
  1417. bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
  1418. unsigned short vid)
  1419. {
  1420. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1421. struct hlist_head *head;
  1422. struct batadv_bla_backbone_gw *backbone_gw;
  1423. int i;
  1424. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1425. return false;
  1426. if (!hash)
  1427. return false;
  1428. for (i = 0; i < hash->size; i++) {
  1429. head = &hash->table[i];
  1430. rcu_read_lock();
  1431. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1432. if (batadv_compare_eth(backbone_gw->orig, orig) &&
  1433. backbone_gw->vid == vid) {
  1434. rcu_read_unlock();
  1435. return true;
  1436. }
  1437. }
  1438. rcu_read_unlock();
  1439. }
  1440. return false;
  1441. }
  1442. /**
  1443. * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
  1444. * @skb: the frame to be checked
  1445. * @orig_node: the orig_node of the frame
  1446. * @hdr_size: maximum length of the frame
  1447. *
  1448. * Return: true if the orig_node is also a gateway on the soft interface,
  1449. * otherwise it returns false.
  1450. */
  1451. bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1452. struct batadv_orig_node *orig_node, int hdr_size)
  1453. {
  1454. struct batadv_bla_backbone_gw *backbone_gw;
  1455. unsigned short vid;
  1456. if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
  1457. return false;
  1458. /* first, find out the vid. */
  1459. if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
  1460. return false;
  1461. vid = batadv_get_vid(skb, hdr_size);
  1462. /* see if this originator is a backbone gw for this VLAN */
  1463. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1464. orig_node->orig, vid);
  1465. if (!backbone_gw)
  1466. return false;
  1467. batadv_backbone_gw_put(backbone_gw);
  1468. return true;
  1469. }
  1470. /**
  1471. * batadv_bla_free - free all bla structures
  1472. * @bat_priv: the bat priv with all the soft interface information
  1473. *
  1474. * for softinterface free or module unload
  1475. */
  1476. void batadv_bla_free(struct batadv_priv *bat_priv)
  1477. {
  1478. struct batadv_hard_iface *primary_if;
  1479. cancel_delayed_work_sync(&bat_priv->bla.work);
  1480. primary_if = batadv_primary_if_get_selected(bat_priv);
  1481. if (bat_priv->bla.claim_hash) {
  1482. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1483. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1484. bat_priv->bla.claim_hash = NULL;
  1485. }
  1486. if (bat_priv->bla.backbone_hash) {
  1487. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1488. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1489. bat_priv->bla.backbone_hash = NULL;
  1490. }
  1491. if (primary_if)
  1492. batadv_hardif_put(primary_if);
  1493. }
  1494. /**
  1495. * batadv_bla_loopdetect_check - check and handle a detected loop
  1496. * @bat_priv: the bat priv with all the soft interface information
  1497. * @skb: the packet to check
  1498. * @primary_if: interface where the request came on
  1499. * @vid: the VLAN ID of the frame
  1500. *
  1501. * Checks if this packet is a loop detect frame which has been sent by us,
  1502. * throw an uevent and log the event if that is the case.
  1503. *
  1504. * Return: true if it is a loop detect frame which is to be dropped, false
  1505. * otherwise.
  1506. */
  1507. static bool
  1508. batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1509. struct batadv_hard_iface *primary_if,
  1510. unsigned short vid)
  1511. {
  1512. struct batadv_bla_backbone_gw *backbone_gw;
  1513. struct ethhdr *ethhdr;
  1514. ethhdr = eth_hdr(skb);
  1515. /* Only check for the MAC address and skip more checks here for
  1516. * performance reasons - this function is on the hotpath, after all.
  1517. */
  1518. if (!batadv_compare_eth(ethhdr->h_source,
  1519. bat_priv->bla.loopdetect_addr))
  1520. return false;
  1521. /* If the packet came too late, don't forward it on the mesh
  1522. * but don't consider that as loop. It might be a coincidence.
  1523. */
  1524. if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
  1525. BATADV_BLA_LOOPDETECT_TIMEOUT))
  1526. return true;
  1527. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  1528. primary_if->net_dev->dev_addr,
  1529. vid, true);
  1530. if (unlikely(!backbone_gw))
  1531. return true;
  1532. queue_work(batadv_event_workqueue, &backbone_gw->report_work);
  1533. /* backbone_gw is unreferenced in the report work function function */
  1534. return true;
  1535. }
  1536. /**
  1537. * batadv_bla_rx - check packets coming from the mesh.
  1538. * @bat_priv: the bat priv with all the soft interface information
  1539. * @skb: the frame to be checked
  1540. * @vid: the VLAN ID of the frame
  1541. * @is_bcast: the packet came in a broadcast packet type.
  1542. *
  1543. * batadv_bla_rx avoidance checks if:
  1544. * * we have to race for a claim
  1545. * * if the frame is allowed on the LAN
  1546. *
  1547. * in these cases, the skb is further handled by this function
  1548. *
  1549. * Return: true if handled, otherwise it returns false and the caller shall
  1550. * further process the skb.
  1551. */
  1552. bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1553. unsigned short vid, bool is_bcast)
  1554. {
  1555. struct batadv_bla_backbone_gw *backbone_gw;
  1556. struct ethhdr *ethhdr;
  1557. struct batadv_bla_claim search_claim, *claim = NULL;
  1558. struct batadv_hard_iface *primary_if;
  1559. bool own_claim;
  1560. bool ret;
  1561. ethhdr = eth_hdr(skb);
  1562. primary_if = batadv_primary_if_get_selected(bat_priv);
  1563. if (!primary_if)
  1564. goto handled;
  1565. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1566. goto allow;
  1567. if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
  1568. goto handled;
  1569. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1570. /* don't allow broadcasts while requests are in flight */
  1571. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
  1572. goto handled;
  1573. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1574. search_claim.vid = vid;
  1575. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1576. if (!claim) {
  1577. /* possible optimization: race for a claim */
  1578. /* No claim exists yet, claim it for us!
  1579. */
  1580. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1581. "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
  1582. __func__, ethhdr->h_source,
  1583. batadv_is_my_client(bat_priv,
  1584. ethhdr->h_source, vid) ?
  1585. "yes" : "no");
  1586. batadv_handle_claim(bat_priv, primary_if,
  1587. primary_if->net_dev->dev_addr,
  1588. ethhdr->h_source, vid);
  1589. goto allow;
  1590. }
  1591. /* if it is our own claim ... */
  1592. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1593. own_claim = batadv_compare_eth(backbone_gw->orig,
  1594. primary_if->net_dev->dev_addr);
  1595. batadv_backbone_gw_put(backbone_gw);
  1596. if (own_claim) {
  1597. /* ... allow it in any case */
  1598. claim->lasttime = jiffies;
  1599. goto allow;
  1600. }
  1601. /* if it is a broadcast ... */
  1602. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
  1603. /* ... drop it. the responsible gateway is in charge.
  1604. *
  1605. * We need to check is_bcast because with the gateway
  1606. * feature, broadcasts (like DHCP requests) may be sent
  1607. * using a unicast packet type.
  1608. */
  1609. goto handled;
  1610. } else {
  1611. /* seems the client considers us as its best gateway.
  1612. * send a claim and update the claim table
  1613. * immediately.
  1614. */
  1615. batadv_handle_claim(bat_priv, primary_if,
  1616. primary_if->net_dev->dev_addr,
  1617. ethhdr->h_source, vid);
  1618. goto allow;
  1619. }
  1620. allow:
  1621. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1622. ret = false;
  1623. goto out;
  1624. handled:
  1625. kfree_skb(skb);
  1626. ret = true;
  1627. out:
  1628. if (primary_if)
  1629. batadv_hardif_put(primary_if);
  1630. if (claim)
  1631. batadv_claim_put(claim);
  1632. return ret;
  1633. }
  1634. /**
  1635. * batadv_bla_tx - check packets going into the mesh
  1636. * @bat_priv: the bat priv with all the soft interface information
  1637. * @skb: the frame to be checked
  1638. * @vid: the VLAN ID of the frame
  1639. *
  1640. * batadv_bla_tx checks if:
  1641. * * a claim was received which has to be processed
  1642. * * the frame is allowed on the mesh
  1643. *
  1644. * in these cases, the skb is further handled by this function.
  1645. *
  1646. * This call might reallocate skb data.
  1647. *
  1648. * Return: true if handled, otherwise it returns false and the caller shall
  1649. * further process the skb.
  1650. */
  1651. bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1652. unsigned short vid)
  1653. {
  1654. struct ethhdr *ethhdr;
  1655. struct batadv_bla_claim search_claim, *claim = NULL;
  1656. struct batadv_bla_backbone_gw *backbone_gw;
  1657. struct batadv_hard_iface *primary_if;
  1658. bool client_roamed;
  1659. bool ret = false;
  1660. primary_if = batadv_primary_if_get_selected(bat_priv);
  1661. if (!primary_if)
  1662. goto out;
  1663. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1664. goto allow;
  1665. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1666. goto handled;
  1667. ethhdr = eth_hdr(skb);
  1668. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1669. /* don't allow broadcasts while requests are in flight */
  1670. if (is_multicast_ether_addr(ethhdr->h_dest))
  1671. goto handled;
  1672. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1673. search_claim.vid = vid;
  1674. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1675. /* if no claim exists, allow it. */
  1676. if (!claim)
  1677. goto allow;
  1678. /* check if we are responsible. */
  1679. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1680. client_roamed = batadv_compare_eth(backbone_gw->orig,
  1681. primary_if->net_dev->dev_addr);
  1682. batadv_backbone_gw_put(backbone_gw);
  1683. if (client_roamed) {
  1684. /* if yes, the client has roamed and we have
  1685. * to unclaim it.
  1686. */
  1687. if (batadv_has_timed_out(claim->lasttime, 100)) {
  1688. /* only unclaim if the last claim entry is
  1689. * older than 100 ms to make sure we really
  1690. * have a roaming client here.
  1691. */
  1692. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
  1693. __func__, ethhdr->h_source);
  1694. batadv_handle_unclaim(bat_priv, primary_if,
  1695. primary_if->net_dev->dev_addr,
  1696. ethhdr->h_source, vid);
  1697. goto allow;
  1698. } else {
  1699. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
  1700. __func__, ethhdr->h_source);
  1701. goto handled;
  1702. }
  1703. }
  1704. /* check if it is a multicast/broadcast frame */
  1705. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1706. /* drop it. the responsible gateway has forwarded it into
  1707. * the backbone network.
  1708. */
  1709. goto handled;
  1710. } else {
  1711. /* we must allow it. at least if we are
  1712. * responsible for the DESTINATION.
  1713. */
  1714. goto allow;
  1715. }
  1716. allow:
  1717. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1718. ret = false;
  1719. goto out;
  1720. handled:
  1721. ret = true;
  1722. out:
  1723. if (primary_if)
  1724. batadv_hardif_put(primary_if);
  1725. if (claim)
  1726. batadv_claim_put(claim);
  1727. return ret;
  1728. }
  1729. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1730. /**
  1731. * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
  1732. * @seq: seq file to print on
  1733. * @offset: not used
  1734. *
  1735. * Return: always 0
  1736. */
  1737. int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
  1738. {
  1739. struct net_device *net_dev = (struct net_device *)seq->private;
  1740. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1741. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  1742. struct batadv_bla_backbone_gw *backbone_gw;
  1743. struct batadv_bla_claim *claim;
  1744. struct batadv_hard_iface *primary_if;
  1745. struct hlist_head *head;
  1746. u16 backbone_crc;
  1747. u32 i;
  1748. bool is_own;
  1749. u8 *primary_addr;
  1750. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1751. if (!primary_if)
  1752. goto out;
  1753. primary_addr = primary_if->net_dev->dev_addr;
  1754. seq_printf(seq,
  1755. "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1756. net_dev->name, primary_addr,
  1757. ntohs(bat_priv->bla.claim_dest.group));
  1758. seq_puts(seq,
  1759. " Client VID Originator [o] (CRC )\n");
  1760. for (i = 0; i < hash->size; i++) {
  1761. head = &hash->table[i];
  1762. rcu_read_lock();
  1763. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1764. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1765. is_own = batadv_compare_eth(backbone_gw->orig,
  1766. primary_addr);
  1767. spin_lock_bh(&backbone_gw->crc_lock);
  1768. backbone_crc = backbone_gw->crc;
  1769. spin_unlock_bh(&backbone_gw->crc_lock);
  1770. seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
  1771. claim->addr, batadv_print_vid(claim->vid),
  1772. backbone_gw->orig,
  1773. (is_own ? 'x' : ' '),
  1774. backbone_crc);
  1775. batadv_backbone_gw_put(backbone_gw);
  1776. }
  1777. rcu_read_unlock();
  1778. }
  1779. out:
  1780. if (primary_if)
  1781. batadv_hardif_put(primary_if);
  1782. return 0;
  1783. }
  1784. #endif
  1785. /**
  1786. * batadv_bla_claim_dump_entry - dump one entry of the claim table
  1787. * to a netlink socket
  1788. * @msg: buffer for the message
  1789. * @portid: netlink port
  1790. * @seq: Sequence number of netlink message
  1791. * @primary_if: primary interface
  1792. * @claim: entry to dump
  1793. *
  1794. * Return: 0 or error code.
  1795. */
  1796. static int
  1797. batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1798. struct batadv_hard_iface *primary_if,
  1799. struct batadv_bla_claim *claim)
  1800. {
  1801. u8 *primary_addr = primary_if->net_dev->dev_addr;
  1802. u16 backbone_crc;
  1803. bool is_own;
  1804. void *hdr;
  1805. int ret = -EINVAL;
  1806. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1807. NLM_F_MULTI, BATADV_CMD_GET_BLA_CLAIM);
  1808. if (!hdr) {
  1809. ret = -ENOBUFS;
  1810. goto out;
  1811. }
  1812. is_own = batadv_compare_eth(claim->backbone_gw->orig,
  1813. primary_addr);
  1814. spin_lock_bh(&claim->backbone_gw->crc_lock);
  1815. backbone_crc = claim->backbone_gw->crc;
  1816. spin_unlock_bh(&claim->backbone_gw->crc_lock);
  1817. if (is_own)
  1818. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  1819. genlmsg_cancel(msg, hdr);
  1820. goto out;
  1821. }
  1822. if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
  1823. nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
  1824. nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  1825. claim->backbone_gw->orig) ||
  1826. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  1827. backbone_crc)) {
  1828. genlmsg_cancel(msg, hdr);
  1829. goto out;
  1830. }
  1831. genlmsg_end(msg, hdr);
  1832. ret = 0;
  1833. out:
  1834. return ret;
  1835. }
  1836. /**
  1837. * batadv_bla_claim_dump_bucket - dump one bucket of the claim table
  1838. * to a netlink socket
  1839. * @msg: buffer for the message
  1840. * @portid: netlink port
  1841. * @seq: Sequence number of netlink message
  1842. * @primary_if: primary interface
  1843. * @head: bucket to dump
  1844. * @idx_skip: How many entries to skip
  1845. *
  1846. * Return: always 0.
  1847. */
  1848. static int
  1849. batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1850. struct batadv_hard_iface *primary_if,
  1851. struct hlist_head *head, int *idx_skip)
  1852. {
  1853. struct batadv_bla_claim *claim;
  1854. int idx = 0;
  1855. rcu_read_lock();
  1856. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1857. if (idx++ < *idx_skip)
  1858. continue;
  1859. if (batadv_bla_claim_dump_entry(msg, portid, seq,
  1860. primary_if, claim)) {
  1861. *idx_skip = idx - 1;
  1862. goto unlock;
  1863. }
  1864. }
  1865. *idx_skip = idx;
  1866. unlock:
  1867. rcu_read_unlock();
  1868. return 0;
  1869. }
  1870. /**
  1871. * batadv_bla_claim_dump - dump claim table to a netlink socket
  1872. * @msg: buffer for the message
  1873. * @cb: callback structure containing arguments
  1874. *
  1875. * Return: message length.
  1876. */
  1877. int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1878. {
  1879. struct batadv_hard_iface *primary_if = NULL;
  1880. int portid = NETLINK_CB(cb->skb).portid;
  1881. struct net *net = sock_net(cb->skb->sk);
  1882. struct net_device *soft_iface;
  1883. struct batadv_hashtable *hash;
  1884. struct batadv_priv *bat_priv;
  1885. int bucket = cb->args[0];
  1886. struct hlist_head *head;
  1887. int idx = cb->args[1];
  1888. int ifindex;
  1889. int ret = 0;
  1890. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  1891. BATADV_ATTR_MESH_IFINDEX);
  1892. if (!ifindex)
  1893. return -EINVAL;
  1894. soft_iface = dev_get_by_index(net, ifindex);
  1895. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1896. ret = -ENODEV;
  1897. goto out;
  1898. }
  1899. bat_priv = netdev_priv(soft_iface);
  1900. hash = bat_priv->bla.claim_hash;
  1901. primary_if = batadv_primary_if_get_selected(bat_priv);
  1902. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1903. ret = -ENOENT;
  1904. goto out;
  1905. }
  1906. while (bucket < hash->size) {
  1907. head = &hash->table[bucket];
  1908. if (batadv_bla_claim_dump_bucket(msg, portid,
  1909. cb->nlh->nlmsg_seq,
  1910. primary_if, head, &idx))
  1911. break;
  1912. bucket++;
  1913. }
  1914. cb->args[0] = bucket;
  1915. cb->args[1] = idx;
  1916. ret = msg->len;
  1917. out:
  1918. if (primary_if)
  1919. batadv_hardif_put(primary_if);
  1920. if (soft_iface)
  1921. dev_put(soft_iface);
  1922. return ret;
  1923. }
  1924. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1925. /**
  1926. * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
  1927. * file
  1928. * @seq: seq file to print on
  1929. * @offset: not used
  1930. *
  1931. * Return: always 0
  1932. */
  1933. int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
  1934. {
  1935. struct net_device *net_dev = (struct net_device *)seq->private;
  1936. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1937. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1938. struct batadv_bla_backbone_gw *backbone_gw;
  1939. struct batadv_hard_iface *primary_if;
  1940. struct hlist_head *head;
  1941. int secs, msecs;
  1942. u16 backbone_crc;
  1943. u32 i;
  1944. bool is_own;
  1945. u8 *primary_addr;
  1946. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1947. if (!primary_if)
  1948. goto out;
  1949. primary_addr = primary_if->net_dev->dev_addr;
  1950. seq_printf(seq,
  1951. "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1952. net_dev->name, primary_addr,
  1953. ntohs(bat_priv->bla.claim_dest.group));
  1954. seq_puts(seq, " Originator VID last seen (CRC )\n");
  1955. for (i = 0; i < hash->size; i++) {
  1956. head = &hash->table[i];
  1957. rcu_read_lock();
  1958. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1959. msecs = jiffies_to_msecs(jiffies -
  1960. backbone_gw->lasttime);
  1961. secs = msecs / 1000;
  1962. msecs = msecs % 1000;
  1963. is_own = batadv_compare_eth(backbone_gw->orig,
  1964. primary_addr);
  1965. if (is_own)
  1966. continue;
  1967. spin_lock_bh(&backbone_gw->crc_lock);
  1968. backbone_crc = backbone_gw->crc;
  1969. spin_unlock_bh(&backbone_gw->crc_lock);
  1970. seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
  1971. backbone_gw->orig,
  1972. batadv_print_vid(backbone_gw->vid), secs,
  1973. msecs, backbone_crc);
  1974. }
  1975. rcu_read_unlock();
  1976. }
  1977. out:
  1978. if (primary_if)
  1979. batadv_hardif_put(primary_if);
  1980. return 0;
  1981. }
  1982. #endif
  1983. /**
  1984. * batadv_bla_backbone_dump_entry - dump one entry of the backbone table
  1985. * to a netlink socket
  1986. * @msg: buffer for the message
  1987. * @portid: netlink port
  1988. * @seq: Sequence number of netlink message
  1989. * @primary_if: primary interface
  1990. * @backbone_gw: entry to dump
  1991. *
  1992. * Return: 0 or error code.
  1993. */
  1994. static int
  1995. batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1996. struct batadv_hard_iface *primary_if,
  1997. struct batadv_bla_backbone_gw *backbone_gw)
  1998. {
  1999. u8 *primary_addr = primary_if->net_dev->dev_addr;
  2000. u16 backbone_crc;
  2001. bool is_own;
  2002. int msecs;
  2003. void *hdr;
  2004. int ret = -EINVAL;
  2005. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  2006. NLM_F_MULTI, BATADV_CMD_GET_BLA_BACKBONE);
  2007. if (!hdr) {
  2008. ret = -ENOBUFS;
  2009. goto out;
  2010. }
  2011. is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
  2012. spin_lock_bh(&backbone_gw->crc_lock);
  2013. backbone_crc = backbone_gw->crc;
  2014. spin_unlock_bh(&backbone_gw->crc_lock);
  2015. msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
  2016. if (is_own)
  2017. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  2018. genlmsg_cancel(msg, hdr);
  2019. goto out;
  2020. }
  2021. if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  2022. backbone_gw->orig) ||
  2023. nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
  2024. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  2025. backbone_crc) ||
  2026. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
  2027. genlmsg_cancel(msg, hdr);
  2028. goto out;
  2029. }
  2030. genlmsg_end(msg, hdr);
  2031. ret = 0;
  2032. out:
  2033. return ret;
  2034. }
  2035. /**
  2036. * batadv_bla_backbone_dump_bucket - dump one bucket of the backbone table
  2037. * to a netlink socket
  2038. * @msg: buffer for the message
  2039. * @portid: netlink port
  2040. * @seq: Sequence number of netlink message
  2041. * @primary_if: primary interface
  2042. * @head: bucket to dump
  2043. * @idx_skip: How many entries to skip
  2044. *
  2045. * Return: always 0.
  2046. */
  2047. static int
  2048. batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  2049. struct batadv_hard_iface *primary_if,
  2050. struct hlist_head *head, int *idx_skip)
  2051. {
  2052. struct batadv_bla_backbone_gw *backbone_gw;
  2053. int idx = 0;
  2054. rcu_read_lock();
  2055. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  2056. if (idx++ < *idx_skip)
  2057. continue;
  2058. if (batadv_bla_backbone_dump_entry(msg, portid, seq,
  2059. primary_if, backbone_gw)) {
  2060. *idx_skip = idx - 1;
  2061. goto unlock;
  2062. }
  2063. }
  2064. *idx_skip = idx;
  2065. unlock:
  2066. rcu_read_unlock();
  2067. return 0;
  2068. }
  2069. /**
  2070. * batadv_bla_backbone_dump - dump backbone table to a netlink socket
  2071. * @msg: buffer for the message
  2072. * @cb: callback structure containing arguments
  2073. *
  2074. * Return: message length.
  2075. */
  2076. int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
  2077. {
  2078. struct batadv_hard_iface *primary_if = NULL;
  2079. int portid = NETLINK_CB(cb->skb).portid;
  2080. struct net *net = sock_net(cb->skb->sk);
  2081. struct net_device *soft_iface;
  2082. struct batadv_hashtable *hash;
  2083. struct batadv_priv *bat_priv;
  2084. int bucket = cb->args[0];
  2085. struct hlist_head *head;
  2086. int idx = cb->args[1];
  2087. int ifindex;
  2088. int ret = 0;
  2089. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  2090. BATADV_ATTR_MESH_IFINDEX);
  2091. if (!ifindex)
  2092. return -EINVAL;
  2093. soft_iface = dev_get_by_index(net, ifindex);
  2094. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  2095. ret = -ENODEV;
  2096. goto out;
  2097. }
  2098. bat_priv = netdev_priv(soft_iface);
  2099. hash = bat_priv->bla.backbone_hash;
  2100. primary_if = batadv_primary_if_get_selected(bat_priv);
  2101. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  2102. ret = -ENOENT;
  2103. goto out;
  2104. }
  2105. while (bucket < hash->size) {
  2106. head = &hash->table[bucket];
  2107. if (batadv_bla_backbone_dump_bucket(msg, portid,
  2108. cb->nlh->nlmsg_seq,
  2109. primary_if, head, &idx))
  2110. break;
  2111. bucket++;
  2112. }
  2113. cb->args[0] = bucket;
  2114. cb->args[1] = idx;
  2115. ret = msg->len;
  2116. out:
  2117. if (primary_if)
  2118. batadv_hardif_put(primary_if);
  2119. if (soft_iface)
  2120. dev_put(soft_iface);
  2121. return ret;
  2122. }
  2123. #ifdef CONFIG_BATMAN_ADV_DAT
  2124. /**
  2125. * batadv_bla_check_claim - check if address is claimed
  2126. *
  2127. * @bat_priv: the bat priv with all the soft interface information
  2128. * @addr: mac address of which the claim status is checked
  2129. * @vid: the VLAN ID
  2130. *
  2131. * addr is checked if this address is claimed by the local device itself.
  2132. *
  2133. * Return: true if bla is disabled or the mac is claimed by the device,
  2134. * false if the device addr is already claimed by another gateway
  2135. */
  2136. bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
  2137. u8 *addr, unsigned short vid)
  2138. {
  2139. struct batadv_bla_claim search_claim;
  2140. struct batadv_bla_claim *claim = NULL;
  2141. struct batadv_hard_iface *primary_if = NULL;
  2142. bool ret = true;
  2143. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  2144. return ret;
  2145. primary_if = batadv_primary_if_get_selected(bat_priv);
  2146. if (!primary_if)
  2147. return ret;
  2148. /* First look if the mac address is claimed */
  2149. ether_addr_copy(search_claim.addr, addr);
  2150. search_claim.vid = vid;
  2151. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  2152. /* If there is a claim and we are not owner of the claim,
  2153. * return false.
  2154. */
  2155. if (claim) {
  2156. if (!batadv_compare_eth(claim->backbone_gw->orig,
  2157. primary_if->net_dev->dev_addr))
  2158. ret = false;
  2159. batadv_claim_put(claim);
  2160. }
  2161. batadv_hardif_put(primary_if);
  2162. return ret;
  2163. }
  2164. #endif