bridge_loop_avoidance.c 69 KB

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