bridge_loop_avoidance.c 73 KB

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