bridge_loop_avoidance.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bridge_loop_avoidance.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/compiler.h>
  22. #include <linux/crc16.h>
  23. #include <linux/errno.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/fs.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/jhash.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kref.h>
  33. #include <linux/list.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/rculist.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/stddef.h>
  43. #include <linux/string.h>
  44. #include <linux/workqueue.h>
  45. #include <net/arp.h>
  46. #include "hard-interface.h"
  47. #include "hash.h"
  48. #include "originator.h"
  49. #include "packet.h"
  50. #include "sysfs.h"
  51. #include "translation-table.h"
  52. static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
  53. static void batadv_bla_periodic_work(struct work_struct *work);
  54. static void
  55. batadv_bla_send_announce(struct batadv_priv *bat_priv,
  56. struct batadv_bla_backbone_gw *backbone_gw);
  57. /**
  58. * batadv_choose_claim - choose the right bucket for a claim.
  59. * @data: data to hash
  60. * @size: size of the hash table
  61. *
  62. * Return: the hash index of the claim
  63. */
  64. static inline u32 batadv_choose_claim(const void *data, u32 size)
  65. {
  66. struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  67. u32 hash = 0;
  68. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  69. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  70. return hash % size;
  71. }
  72. /**
  73. * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
  74. * @data: data to hash
  75. * @size: size of the hash table
  76. *
  77. * Return: the hash index of the backbone gateway
  78. */
  79. static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
  80. {
  81. const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  82. u32 hash = 0;
  83. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  84. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  85. return hash % size;
  86. }
  87. /**
  88. * batadv_compare_backbone_gw - compare address and vid of two backbone gws
  89. * @node: list node of the first entry to compare
  90. * @data2: pointer to the second backbone gateway
  91. *
  92. * Return: true if the backbones have the same data, false otherwise
  93. */
  94. static bool batadv_compare_backbone_gw(const struct hlist_node *node,
  95. const void *data2)
  96. {
  97. const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
  98. hash_entry);
  99. const struct batadv_bla_backbone_gw *gw1 = data1;
  100. const struct batadv_bla_backbone_gw *gw2 = data2;
  101. if (!batadv_compare_eth(gw1->orig, gw2->orig))
  102. return false;
  103. if (gw1->vid != gw2->vid)
  104. return false;
  105. return true;
  106. }
  107. /**
  108. * batadv_compare_claim - compare address and vid of two claims
  109. * @node: list node of the first entry to compare
  110. * @data2: pointer to the second claims
  111. *
  112. * Return: true if the claim have the same data, 0 otherwise
  113. */
  114. static bool batadv_compare_claim(const struct hlist_node *node,
  115. const void *data2)
  116. {
  117. const void *data1 = container_of(node, struct batadv_bla_claim,
  118. hash_entry);
  119. const struct batadv_bla_claim *cl1 = data1;
  120. const struct batadv_bla_claim *cl2 = data2;
  121. if (!batadv_compare_eth(cl1->addr, cl2->addr))
  122. return false;
  123. if (cl1->vid != cl2->vid)
  124. return false;
  125. return true;
  126. }
  127. /**
  128. * batadv_backbone_gw_release - release backbone gw from lists and queue for
  129. * free after rcu grace period
  130. * @ref: kref pointer of the backbone gw
  131. */
  132. static void batadv_backbone_gw_release(struct kref *ref)
  133. {
  134. struct batadv_bla_backbone_gw *backbone_gw;
  135. backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
  136. refcount);
  137. kfree_rcu(backbone_gw, rcu);
  138. }
  139. /**
  140. * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
  141. * release it
  142. * @backbone_gw: backbone gateway to be free'd
  143. */
  144. static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
  145. {
  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 rcu
  150. * 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
  170. * release it
  171. * @claim: claim to be free'd
  172. */
  173. static void batadv_claim_put(struct batadv_bla_claim *claim)
  174. {
  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, 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, 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. "bla_send_claim(): CLAIM %pM on vid %d\n", 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. "bla_send_claim(): UNCLAIM %pM on vid %d\n", 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 containg the crc
  345. */
  346. ether_addr_copy(hw_src, mac);
  347. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  348. "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
  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. "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
  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. "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
  367. 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. soft_iface->last_rx = jiffies;
  383. netif_rx(skb);
  384. out:
  385. if (primary_if)
  386. batadv_hardif_put(primary_if);
  387. }
  388. /**
  389. * batadv_bla_loopdetect_report - worker for reporting the loop
  390. * @work: work queue item
  391. *
  392. * Throws an uevent, as the loopdetect check function can't do that itself
  393. * since the kernel may sleep while throwing uevents.
  394. */
  395. static void batadv_bla_loopdetect_report(struct work_struct *work)
  396. {
  397. struct batadv_bla_backbone_gw *backbone_gw;
  398. struct batadv_priv *bat_priv;
  399. char vid_str[6] = { '\0' };
  400. backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
  401. report_work);
  402. bat_priv = backbone_gw->bat_priv;
  403. batadv_info(bat_priv->soft_iface,
  404. "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
  405. BATADV_PRINT_VID(backbone_gw->vid));
  406. snprintf(vid_str, sizeof(vid_str), "%d",
  407. BATADV_PRINT_VID(backbone_gw->vid));
  408. vid_str[sizeof(vid_str) - 1] = 0;
  409. batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
  410. vid_str);
  411. batadv_backbone_gw_put(backbone_gw);
  412. }
  413. /**
  414. * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
  415. * @bat_priv: the bat priv with all the soft interface information
  416. * @orig: the mac address of the originator
  417. * @vid: the VLAN ID
  418. * @own_backbone: set if the requested backbone is local
  419. *
  420. * Return: the (possibly created) backbone gateway or NULL on error
  421. */
  422. static struct batadv_bla_backbone_gw *
  423. batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
  424. unsigned short vid, bool own_backbone)
  425. {
  426. struct batadv_bla_backbone_gw *entry;
  427. struct batadv_orig_node *orig_node;
  428. int hash_added;
  429. entry = batadv_backbone_hash_find(bat_priv, orig, vid);
  430. if (entry)
  431. return entry;
  432. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  433. "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
  434. orig, BATADV_PRINT_VID(vid));
  435. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  436. if (!entry)
  437. return NULL;
  438. entry->vid = vid;
  439. entry->lasttime = jiffies;
  440. entry->crc = BATADV_BLA_CRC_INIT;
  441. entry->bat_priv = bat_priv;
  442. spin_lock_init(&entry->crc_lock);
  443. atomic_set(&entry->request_sent, 0);
  444. atomic_set(&entry->wait_periods, 0);
  445. ether_addr_copy(entry->orig, orig);
  446. INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
  447. /* one for the hash, one for returning */
  448. kref_init(&entry->refcount);
  449. kref_get(&entry->refcount);
  450. hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
  451. batadv_compare_backbone_gw,
  452. batadv_choose_backbone_gw, entry,
  453. &entry->hash_entry);
  454. if (unlikely(hash_added != 0)) {
  455. /* hash failed, free the structure */
  456. kfree(entry);
  457. return NULL;
  458. }
  459. /* this is a gateway now, remove any TT entry on this VLAN */
  460. orig_node = batadv_orig_hash_find(bat_priv, orig);
  461. if (orig_node) {
  462. batadv_tt_global_del_orig(bat_priv, orig_node, vid,
  463. "became a backbone gateway");
  464. batadv_orig_node_put(orig_node);
  465. }
  466. if (own_backbone) {
  467. batadv_bla_send_announce(bat_priv, entry);
  468. /* this will be decreased in the worker thread */
  469. atomic_inc(&entry->request_sent);
  470. atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
  471. atomic_inc(&bat_priv->bla.num_requests);
  472. }
  473. return entry;
  474. }
  475. /**
  476. * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
  477. * @bat_priv: the bat priv with all the soft interface information
  478. * @primary_if: the selected primary interface
  479. * @vid: VLAN identifier
  480. *
  481. * update or add the own backbone gw to make sure we announce
  482. * where we receive other backbone gws
  483. */
  484. static void
  485. batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
  486. struct batadv_hard_iface *primary_if,
  487. unsigned short vid)
  488. {
  489. struct batadv_bla_backbone_gw *backbone_gw;
  490. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  491. primary_if->net_dev->dev_addr,
  492. vid, true);
  493. if (unlikely(!backbone_gw))
  494. return;
  495. backbone_gw->lasttime = jiffies;
  496. batadv_backbone_gw_put(backbone_gw);
  497. }
  498. /**
  499. * batadv_bla_answer_request - answer a bla request by sending own claims
  500. * @bat_priv: the bat priv with all the soft interface information
  501. * @primary_if: interface where the request came on
  502. * @vid: the vid where the request came on
  503. *
  504. * Repeat all of our own claims, and finally send an ANNOUNCE frame
  505. * to allow the requester another check if the CRC is correct now.
  506. */
  507. static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
  508. struct batadv_hard_iface *primary_if,
  509. unsigned short vid)
  510. {
  511. struct hlist_head *head;
  512. struct batadv_hashtable *hash;
  513. struct batadv_bla_claim *claim;
  514. struct batadv_bla_backbone_gw *backbone_gw;
  515. int i;
  516. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  517. "bla_answer_request(): received a claim request, send all of our own claims again\n");
  518. backbone_gw = batadv_backbone_hash_find(bat_priv,
  519. primary_if->net_dev->dev_addr,
  520. vid);
  521. if (!backbone_gw)
  522. return;
  523. hash = bat_priv->bla.claim_hash;
  524. for (i = 0; i < hash->size; i++) {
  525. head = &hash->table[i];
  526. rcu_read_lock();
  527. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  528. /* only own claims are interesting */
  529. if (claim->backbone_gw != backbone_gw)
  530. continue;
  531. batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
  532. BATADV_CLAIM_TYPE_CLAIM);
  533. }
  534. rcu_read_unlock();
  535. }
  536. /* finally, send an announcement frame */
  537. batadv_bla_send_announce(bat_priv, backbone_gw);
  538. batadv_backbone_gw_put(backbone_gw);
  539. }
  540. /**
  541. * batadv_bla_send_request - send a request to repeat claims
  542. * @backbone_gw: the backbone gateway from whom we are out of sync
  543. *
  544. * When the crc is wrong, ask the backbone gateway for a full table update.
  545. * After the request, it will repeat all of his own claims and finally
  546. * send an announcement claim with which we can check again.
  547. */
  548. static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
  549. {
  550. /* first, remove all old entries */
  551. batadv_bla_del_backbone_claims(backbone_gw);
  552. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  553. "Sending REQUEST to %pM\n", backbone_gw->orig);
  554. /* send request */
  555. batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
  556. backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
  557. /* no local broadcasts should be sent or received, for now. */
  558. if (!atomic_read(&backbone_gw->request_sent)) {
  559. atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
  560. atomic_set(&backbone_gw->request_sent, 1);
  561. }
  562. }
  563. /**
  564. * batadv_bla_send_announce - Send an announcement frame
  565. * @bat_priv: the bat priv with all the soft interface information
  566. * @backbone_gw: our backbone gateway which should be announced
  567. */
  568. static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
  569. struct batadv_bla_backbone_gw *backbone_gw)
  570. {
  571. u8 mac[ETH_ALEN];
  572. __be16 crc;
  573. memcpy(mac, batadv_announce_mac, 4);
  574. spin_lock_bh(&backbone_gw->crc_lock);
  575. crc = htons(backbone_gw->crc);
  576. spin_unlock_bh(&backbone_gw->crc_lock);
  577. memcpy(&mac[4], &crc, 2);
  578. batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
  579. BATADV_CLAIM_TYPE_ANNOUNCE);
  580. }
  581. /**
  582. * batadv_bla_add_claim - Adds a claim in the claim hash
  583. * @bat_priv: the bat priv with all the soft interface information
  584. * @mac: the mac address of the claim
  585. * @vid: the VLAN ID of the frame
  586. * @backbone_gw: the backbone gateway which claims it
  587. */
  588. static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
  589. const u8 *mac, const unsigned short vid,
  590. struct batadv_bla_backbone_gw *backbone_gw)
  591. {
  592. struct batadv_bla_backbone_gw *old_backbone_gw;
  593. struct batadv_bla_claim *claim;
  594. struct batadv_bla_claim search_claim;
  595. bool remove_crc = false;
  596. int hash_added;
  597. ether_addr_copy(search_claim.addr, mac);
  598. search_claim.vid = vid;
  599. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  600. /* create a new claim entry if it does not exist yet. */
  601. if (!claim) {
  602. claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
  603. if (!claim)
  604. return;
  605. ether_addr_copy(claim->addr, mac);
  606. spin_lock_init(&claim->backbone_lock);
  607. claim->vid = vid;
  608. claim->lasttime = jiffies;
  609. kref_get(&backbone_gw->refcount);
  610. claim->backbone_gw = backbone_gw;
  611. kref_init(&claim->refcount);
  612. kref_get(&claim->refcount);
  613. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  614. "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
  615. mac, BATADV_PRINT_VID(vid));
  616. hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
  617. batadv_compare_claim,
  618. batadv_choose_claim, claim,
  619. &claim->hash_entry);
  620. if (unlikely(hash_added != 0)) {
  621. /* only local changes happened. */
  622. kfree(claim);
  623. return;
  624. }
  625. } else {
  626. claim->lasttime = jiffies;
  627. if (claim->backbone_gw == backbone_gw)
  628. /* no need to register a new backbone */
  629. goto claim_free_ref;
  630. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  631. "bla_add_claim(): changing ownership for %pM, vid %d\n",
  632. mac, BATADV_PRINT_VID(vid));
  633. remove_crc = true;
  634. }
  635. /* replace backbone_gw atomically and adjust reference counters */
  636. spin_lock_bh(&claim->backbone_lock);
  637. old_backbone_gw = claim->backbone_gw;
  638. kref_get(&backbone_gw->refcount);
  639. claim->backbone_gw = backbone_gw;
  640. spin_unlock_bh(&claim->backbone_lock);
  641. if (remove_crc) {
  642. /* remove claim address from old backbone_gw */
  643. spin_lock_bh(&old_backbone_gw->crc_lock);
  644. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  645. spin_unlock_bh(&old_backbone_gw->crc_lock);
  646. }
  647. batadv_backbone_gw_put(old_backbone_gw);
  648. /* add claim address to new backbone_gw */
  649. spin_lock_bh(&backbone_gw->crc_lock);
  650. backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  651. spin_unlock_bh(&backbone_gw->crc_lock);
  652. backbone_gw->lasttime = jiffies;
  653. claim_free_ref:
  654. batadv_claim_put(claim);
  655. }
  656. /**
  657. * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
  658. * claim
  659. * @claim: claim whose backbone_gw should be returned
  660. *
  661. * Return: valid reference to claim::backbone_gw
  662. */
  663. static struct batadv_bla_backbone_gw *
  664. batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
  665. {
  666. struct batadv_bla_backbone_gw *backbone_gw;
  667. spin_lock_bh(&claim->backbone_lock);
  668. backbone_gw = claim->backbone_gw;
  669. kref_get(&backbone_gw->refcount);
  670. spin_unlock_bh(&claim->backbone_lock);
  671. return backbone_gw;
  672. }
  673. /**
  674. * batadv_bla_del_claim - delete a claim from the claim hash
  675. * @bat_priv: the bat priv with all the soft interface information
  676. * @mac: mac address of the claim to be removed
  677. * @vid: VLAN id for the claim to be removed
  678. */
  679. static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
  680. const u8 *mac, const unsigned short vid)
  681. {
  682. struct batadv_bla_claim search_claim, *claim;
  683. ether_addr_copy(search_claim.addr, mac);
  684. search_claim.vid = vid;
  685. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  686. if (!claim)
  687. return;
  688. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
  689. mac, BATADV_PRINT_VID(vid));
  690. batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
  691. batadv_choose_claim, claim);
  692. batadv_claim_put(claim); /* reference from the hash is gone */
  693. /* don't need the reference from hash_find() anymore */
  694. batadv_claim_put(claim);
  695. }
  696. /**
  697. * batadv_handle_announce - check for ANNOUNCE frame
  698. * @bat_priv: the bat priv with all the soft interface information
  699. * @an_addr: announcement mac address (ARP Sender HW address)
  700. * @backbone_addr: originator address of the sender (Ethernet source MAC)
  701. * @vid: the VLAN ID of the frame
  702. *
  703. * Return: true if handled
  704. */
  705. static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
  706. u8 *backbone_addr, unsigned short vid)
  707. {
  708. struct batadv_bla_backbone_gw *backbone_gw;
  709. u16 backbone_crc, crc;
  710. if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
  711. return false;
  712. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  713. false);
  714. if (unlikely(!backbone_gw))
  715. return true;
  716. /* handle as ANNOUNCE frame */
  717. backbone_gw->lasttime = jiffies;
  718. crc = ntohs(*((__be16 *)(&an_addr[4])));
  719. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  720. "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
  721. BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
  722. spin_lock_bh(&backbone_gw->crc_lock);
  723. backbone_crc = backbone_gw->crc;
  724. spin_unlock_bh(&backbone_gw->crc_lock);
  725. if (backbone_crc != crc) {
  726. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  727. "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
  728. backbone_gw->orig,
  729. BATADV_PRINT_VID(backbone_gw->vid),
  730. backbone_crc, crc);
  731. batadv_bla_send_request(backbone_gw);
  732. } else {
  733. /* if we have sent a request and the crc was OK,
  734. * we can allow traffic again.
  735. */
  736. if (atomic_read(&backbone_gw->request_sent)) {
  737. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  738. atomic_set(&backbone_gw->request_sent, 0);
  739. }
  740. }
  741. batadv_backbone_gw_put(backbone_gw);
  742. return true;
  743. }
  744. /**
  745. * batadv_handle_request - check for REQUEST frame
  746. * @bat_priv: the bat priv with all the soft interface information
  747. * @primary_if: the primary hard interface of this batman soft interface
  748. * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
  749. * @ethhdr: ethernet header of a packet
  750. * @vid: the VLAN ID of the frame
  751. *
  752. * Return: true if handled
  753. */
  754. static bool batadv_handle_request(struct batadv_priv *bat_priv,
  755. struct batadv_hard_iface *primary_if,
  756. u8 *backbone_addr, struct ethhdr *ethhdr,
  757. unsigned short vid)
  758. {
  759. /* check for REQUEST frame */
  760. if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
  761. return false;
  762. /* sanity check, this should not happen on a normal switch,
  763. * we ignore it in this case.
  764. */
  765. if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
  766. return true;
  767. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  768. "handle_request(): REQUEST vid %d (sent by %pM)...\n",
  769. BATADV_PRINT_VID(vid), ethhdr->h_source);
  770. batadv_bla_answer_request(bat_priv, primary_if, vid);
  771. return true;
  772. }
  773. /**
  774. * batadv_handle_unclaim - check for UNCLAIM frame
  775. * @bat_priv: the bat priv with all the soft interface information
  776. * @primary_if: the primary hard interface of this batman soft interface
  777. * @backbone_addr: originator address of the backbone (Ethernet source)
  778. * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
  779. * @vid: the VLAN ID of the frame
  780. *
  781. * Return: true if handled
  782. */
  783. static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
  784. struct batadv_hard_iface *primary_if,
  785. u8 *backbone_addr, u8 *claim_addr,
  786. unsigned short vid)
  787. {
  788. struct batadv_bla_backbone_gw *backbone_gw;
  789. /* unclaim in any case if it is our own */
  790. if (primary_if && batadv_compare_eth(backbone_addr,
  791. primary_if->net_dev->dev_addr))
  792. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  793. BATADV_CLAIM_TYPE_UNCLAIM);
  794. backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
  795. if (!backbone_gw)
  796. return true;
  797. /* this must be an UNCLAIM frame */
  798. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  799. "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
  800. claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
  801. batadv_bla_del_claim(bat_priv, claim_addr, vid);
  802. batadv_backbone_gw_put(backbone_gw);
  803. return true;
  804. }
  805. /**
  806. * batadv_handle_claim - check for CLAIM frame
  807. * @bat_priv: the bat priv with all the soft interface information
  808. * @primary_if: the primary hard interface of this batman soft interface
  809. * @backbone_addr: originator address of the backbone (Ethernet Source)
  810. * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
  811. * @vid: the VLAN ID of the frame
  812. *
  813. * Return: true if handled
  814. */
  815. static bool batadv_handle_claim(struct batadv_priv *bat_priv,
  816. struct batadv_hard_iface *primary_if,
  817. u8 *backbone_addr, u8 *claim_addr,
  818. unsigned short vid)
  819. {
  820. struct batadv_bla_backbone_gw *backbone_gw;
  821. /* register the gateway if not yet available, and add the claim. */
  822. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  823. false);
  824. if (unlikely(!backbone_gw))
  825. return true;
  826. /* this must be a CLAIM frame */
  827. batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
  828. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  829. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  830. BATADV_CLAIM_TYPE_CLAIM);
  831. /* TODO: we could call something like tt_local_del() here. */
  832. batadv_backbone_gw_put(backbone_gw);
  833. return true;
  834. }
  835. /**
  836. * batadv_check_claim_group - check for claim group membership
  837. * @bat_priv: the bat priv with all the soft interface information
  838. * @primary_if: the primary interface of this batman interface
  839. * @hw_src: the Hardware source in the ARP Header
  840. * @hw_dst: the Hardware destination in the ARP Header
  841. * @ethhdr: pointer to the Ethernet header of the claim frame
  842. *
  843. * checks if it is a claim packet and if its on the same group.
  844. * This function also applies the group ID of the sender
  845. * if it is in the same mesh.
  846. *
  847. * Return:
  848. * 2 - if it is a claim packet and on the same group
  849. * 1 - if is a claim packet from another group
  850. * 0 - if it is not a claim packet
  851. */
  852. static int batadv_check_claim_group(struct batadv_priv *bat_priv,
  853. struct batadv_hard_iface *primary_if,
  854. u8 *hw_src, u8 *hw_dst,
  855. struct ethhdr *ethhdr)
  856. {
  857. u8 *backbone_addr;
  858. struct batadv_orig_node *orig_node;
  859. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  860. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  861. bla_dst_own = &bat_priv->bla.claim_dest;
  862. /* if announcement packet, use the source,
  863. * otherwise assume it is in the hw_src
  864. */
  865. switch (bla_dst->type) {
  866. case BATADV_CLAIM_TYPE_CLAIM:
  867. backbone_addr = hw_src;
  868. break;
  869. case BATADV_CLAIM_TYPE_REQUEST:
  870. case BATADV_CLAIM_TYPE_ANNOUNCE:
  871. case BATADV_CLAIM_TYPE_UNCLAIM:
  872. backbone_addr = ethhdr->h_source;
  873. break;
  874. default:
  875. return 0;
  876. }
  877. /* don't accept claim frames from ourselves */
  878. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  879. return 0;
  880. /* if its already the same group, it is fine. */
  881. if (bla_dst->group == bla_dst_own->group)
  882. return 2;
  883. /* lets see if this originator is in our mesh */
  884. orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
  885. /* dont accept claims from gateways which are not in
  886. * the same mesh or group.
  887. */
  888. if (!orig_node)
  889. return 1;
  890. /* if our mesh friends mac is bigger, use it for ourselves. */
  891. if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
  892. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  893. "taking other backbones claim group: %#.4x\n",
  894. ntohs(bla_dst->group));
  895. bla_dst_own->group = bla_dst->group;
  896. }
  897. batadv_orig_node_put(orig_node);
  898. return 2;
  899. }
  900. /**
  901. * batadv_bla_process_claim - Check if this is a claim frame, and process it
  902. * @bat_priv: the bat priv with all the soft interface information
  903. * @primary_if: the primary hard interface of this batman soft interface
  904. * @skb: the frame to be checked
  905. *
  906. * Return: true if it was a claim frame, otherwise return false to
  907. * tell the callee that it can use the frame on its own.
  908. */
  909. static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
  910. struct batadv_hard_iface *primary_if,
  911. struct sk_buff *skb)
  912. {
  913. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  914. u8 *hw_src, *hw_dst;
  915. struct vlan_hdr *vhdr, vhdr_buf;
  916. struct ethhdr *ethhdr;
  917. struct arphdr *arphdr;
  918. unsigned short vid;
  919. int vlan_depth = 0;
  920. __be16 proto;
  921. int headlen;
  922. int ret;
  923. vid = batadv_get_vid(skb, 0);
  924. ethhdr = eth_hdr(skb);
  925. proto = ethhdr->h_proto;
  926. headlen = ETH_HLEN;
  927. if (vid & BATADV_VLAN_HAS_TAG) {
  928. /* Traverse the VLAN/Ethertypes.
  929. *
  930. * At this point it is known that the first protocol is a VLAN
  931. * header, so start checking at the encapsulated protocol.
  932. *
  933. * The depth of the VLAN headers is recorded to drop BLA claim
  934. * frames encapsulated into multiple VLAN headers (QinQ).
  935. */
  936. do {
  937. vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
  938. &vhdr_buf);
  939. if (!vhdr)
  940. return false;
  941. proto = vhdr->h_vlan_encapsulated_proto;
  942. headlen += VLAN_HLEN;
  943. vlan_depth++;
  944. } while (proto == htons(ETH_P_8021Q));
  945. }
  946. if (proto != htons(ETH_P_ARP))
  947. return false; /* not a claim frame */
  948. /* this must be a ARP frame. check if it is a claim. */
  949. if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
  950. return false;
  951. /* pskb_may_pull() may have modified the pointers, get ethhdr again */
  952. ethhdr = eth_hdr(skb);
  953. arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
  954. /* Check whether the ARP frame carries a valid
  955. * IP information
  956. */
  957. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  958. return false;
  959. if (arphdr->ar_pro != htons(ETH_P_IP))
  960. return false;
  961. if (arphdr->ar_hln != ETH_ALEN)
  962. return false;
  963. if (arphdr->ar_pln != 4)
  964. return false;
  965. hw_src = (u8 *)arphdr + sizeof(struct arphdr);
  966. hw_dst = hw_src + ETH_ALEN + 4;
  967. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  968. bla_dst_own = &bat_priv->bla.claim_dest;
  969. /* check if it is a claim frame in general */
  970. if (memcmp(bla_dst->magic, bla_dst_own->magic,
  971. sizeof(bla_dst->magic)) != 0)
  972. return false;
  973. /* check if there is a claim frame encapsulated deeper in (QinQ) and
  974. * drop that, as this is not supported by BLA but should also not be
  975. * sent via the mesh.
  976. */
  977. if (vlan_depth > 1)
  978. return true;
  979. /* Let the loopdetect frames on the mesh in any case. */
  980. if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
  981. return 0;
  982. /* check if it is a claim frame. */
  983. ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
  984. ethhdr);
  985. if (ret == 1)
  986. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  987. "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  988. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
  989. hw_dst);
  990. if (ret < 2)
  991. return !!ret;
  992. /* become a backbone gw ourselves on this vlan if not happened yet */
  993. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  994. /* check for the different types of claim frames ... */
  995. switch (bla_dst->type) {
  996. case BATADV_CLAIM_TYPE_CLAIM:
  997. if (batadv_handle_claim(bat_priv, primary_if, hw_src,
  998. ethhdr->h_source, vid))
  999. return true;
  1000. break;
  1001. case BATADV_CLAIM_TYPE_UNCLAIM:
  1002. if (batadv_handle_unclaim(bat_priv, primary_if,
  1003. ethhdr->h_source, hw_src, vid))
  1004. return true;
  1005. break;
  1006. case BATADV_CLAIM_TYPE_ANNOUNCE:
  1007. if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
  1008. vid))
  1009. return true;
  1010. break;
  1011. case BATADV_CLAIM_TYPE_REQUEST:
  1012. if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
  1013. vid))
  1014. return true;
  1015. break;
  1016. }
  1017. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1018. "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  1019. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
  1020. return true;
  1021. }
  1022. /**
  1023. * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
  1024. * immediately
  1025. * @bat_priv: the bat priv with all the soft interface information
  1026. * @now: whether the whole hash shall be wiped now
  1027. *
  1028. * Check when we last heard from other nodes, and remove them in case of
  1029. * a time out, or clean all backbone gws if now is set.
  1030. */
  1031. static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
  1032. {
  1033. struct batadv_bla_backbone_gw *backbone_gw;
  1034. struct hlist_node *node_tmp;
  1035. struct hlist_head *head;
  1036. struct batadv_hashtable *hash;
  1037. spinlock_t *list_lock; /* protects write access to the hash lists */
  1038. int i;
  1039. hash = bat_priv->bla.backbone_hash;
  1040. if (!hash)
  1041. return;
  1042. for (i = 0; i < hash->size; i++) {
  1043. head = &hash->table[i];
  1044. list_lock = &hash->list_locks[i];
  1045. spin_lock_bh(list_lock);
  1046. hlist_for_each_entry_safe(backbone_gw, node_tmp,
  1047. head, hash_entry) {
  1048. if (now)
  1049. goto purge_now;
  1050. if (!batadv_has_timed_out(backbone_gw->lasttime,
  1051. BATADV_BLA_BACKBONE_TIMEOUT))
  1052. continue;
  1053. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  1054. "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
  1055. backbone_gw->orig);
  1056. purge_now:
  1057. /* don't wait for the pending request anymore */
  1058. if (atomic_read(&backbone_gw->request_sent))
  1059. atomic_dec(&bat_priv->bla.num_requests);
  1060. batadv_bla_del_backbone_claims(backbone_gw);
  1061. hlist_del_rcu(&backbone_gw->hash_entry);
  1062. batadv_backbone_gw_put(backbone_gw);
  1063. }
  1064. spin_unlock_bh(list_lock);
  1065. }
  1066. }
  1067. /**
  1068. * batadv_bla_purge_claims - Remove claims after a timeout or immediately
  1069. * @bat_priv: the bat priv with all the soft interface information
  1070. * @primary_if: the selected primary interface, may be NULL if now is set
  1071. * @now: whether the whole hash shall be wiped now
  1072. *
  1073. * Check when we heard last time from our own claims, and remove them in case of
  1074. * a time out, or clean all claims if now is set
  1075. */
  1076. static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
  1077. struct batadv_hard_iface *primary_if,
  1078. int now)
  1079. {
  1080. struct batadv_bla_backbone_gw *backbone_gw;
  1081. struct batadv_bla_claim *claim;
  1082. struct hlist_head *head;
  1083. struct batadv_hashtable *hash;
  1084. int i;
  1085. hash = bat_priv->bla.claim_hash;
  1086. if (!hash)
  1087. return;
  1088. for (i = 0; i < hash->size; i++) {
  1089. head = &hash->table[i];
  1090. rcu_read_lock();
  1091. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1092. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1093. if (now)
  1094. goto purge_now;
  1095. if (!batadv_compare_eth(backbone_gw->orig,
  1096. primary_if->net_dev->dev_addr))
  1097. goto skip;
  1098. if (!batadv_has_timed_out(claim->lasttime,
  1099. BATADV_BLA_CLAIM_TIMEOUT))
  1100. goto skip;
  1101. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1102. "bla_purge_claims(): %pM, vid %d, time out\n",
  1103. claim->addr, claim->vid);
  1104. purge_now:
  1105. batadv_handle_unclaim(bat_priv, primary_if,
  1106. backbone_gw->orig,
  1107. claim->addr, claim->vid);
  1108. skip:
  1109. batadv_backbone_gw_put(backbone_gw);
  1110. }
  1111. rcu_read_unlock();
  1112. }
  1113. }
  1114. /**
  1115. * batadv_bla_update_orig_address - Update the backbone gateways when the own
  1116. * originator address changes
  1117. * @bat_priv: the bat priv with all the soft interface information
  1118. * @primary_if: the new selected primary_if
  1119. * @oldif: the old primary interface, may be NULL
  1120. */
  1121. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  1122. struct batadv_hard_iface *primary_if,
  1123. struct batadv_hard_iface *oldif)
  1124. {
  1125. struct batadv_bla_backbone_gw *backbone_gw;
  1126. struct hlist_head *head;
  1127. struct batadv_hashtable *hash;
  1128. __be16 group;
  1129. int i;
  1130. /* reset bridge loop avoidance group id */
  1131. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  1132. bat_priv->bla.claim_dest.group = group;
  1133. /* purge everything when bridge loop avoidance is turned off */
  1134. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1135. oldif = NULL;
  1136. if (!oldif) {
  1137. batadv_bla_purge_claims(bat_priv, NULL, 1);
  1138. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1139. return;
  1140. }
  1141. hash = bat_priv->bla.backbone_hash;
  1142. if (!hash)
  1143. return;
  1144. for (i = 0; i < hash->size; i++) {
  1145. head = &hash->table[i];
  1146. rcu_read_lock();
  1147. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1148. /* own orig still holds the old value. */
  1149. if (!batadv_compare_eth(backbone_gw->orig,
  1150. oldif->net_dev->dev_addr))
  1151. continue;
  1152. ether_addr_copy(backbone_gw->orig,
  1153. primary_if->net_dev->dev_addr);
  1154. /* send an announce frame so others will ask for our
  1155. * claims and update their tables.
  1156. */
  1157. batadv_bla_send_announce(bat_priv, backbone_gw);
  1158. }
  1159. rcu_read_unlock();
  1160. }
  1161. }
  1162. /**
  1163. * batadv_bla_send_loopdetect - send a loopdetect frame
  1164. * @bat_priv: the bat priv with all the soft interface information
  1165. * @backbone_gw: the backbone gateway for which a loop should be detected
  1166. *
  1167. * To detect loops that the bridge loop avoidance can't handle, send a loop
  1168. * detection packet on the backbone. Unlike other BLA frames, this frame will
  1169. * be allowed on the mesh by other nodes. If it is received on the mesh, this
  1170. * indicates that there is a loop.
  1171. */
  1172. static void
  1173. batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
  1174. struct batadv_bla_backbone_gw *backbone_gw)
  1175. {
  1176. batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
  1177. backbone_gw->vid);
  1178. batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
  1179. backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
  1180. }
  1181. /**
  1182. * batadv_bla_status_update - purge bla interfaces if necessary
  1183. * @net_dev: the soft interface net device
  1184. */
  1185. void batadv_bla_status_update(struct net_device *net_dev)
  1186. {
  1187. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1188. struct batadv_hard_iface *primary_if;
  1189. primary_if = batadv_primary_if_get_selected(bat_priv);
  1190. if (!primary_if)
  1191. return;
  1192. /* this function already purges everything when bla is disabled,
  1193. * so just call that one.
  1194. */
  1195. batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
  1196. batadv_hardif_put(primary_if);
  1197. }
  1198. /**
  1199. * batadv_bla_periodic_work - performs periodic bla work
  1200. * @work: kernel work struct
  1201. *
  1202. * periodic work to do:
  1203. * * purge structures when they are too old
  1204. * * send announcements
  1205. */
  1206. static void batadv_bla_periodic_work(struct work_struct *work)
  1207. {
  1208. struct delayed_work *delayed_work;
  1209. struct batadv_priv *bat_priv;
  1210. struct batadv_priv_bla *priv_bla;
  1211. struct hlist_head *head;
  1212. struct batadv_bla_backbone_gw *backbone_gw;
  1213. struct batadv_hashtable *hash;
  1214. struct batadv_hard_iface *primary_if;
  1215. bool send_loopdetect = false;
  1216. int i;
  1217. delayed_work = to_delayed_work(work);
  1218. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  1219. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  1220. primary_if = batadv_primary_if_get_selected(bat_priv);
  1221. if (!primary_if)
  1222. goto out;
  1223. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  1224. batadv_bla_purge_backbone_gw(bat_priv, 0);
  1225. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1226. goto out;
  1227. if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
  1228. /* set a new random mac address for the next bridge loop
  1229. * detection frames. Set the locally administered bit to avoid
  1230. * collisions with users mac addresses.
  1231. */
  1232. random_ether_addr(bat_priv->bla.loopdetect_addr);
  1233. bat_priv->bla.loopdetect_addr[0] = 0xba;
  1234. bat_priv->bla.loopdetect_addr[1] = 0xbe;
  1235. bat_priv->bla.loopdetect_lasttime = jiffies;
  1236. atomic_set(&bat_priv->bla.loopdetect_next,
  1237. BATADV_BLA_LOOPDETECT_PERIODS);
  1238. /* mark for sending loop detect on all VLANs */
  1239. send_loopdetect = true;
  1240. }
  1241. hash = bat_priv->bla.backbone_hash;
  1242. if (!hash)
  1243. goto out;
  1244. for (i = 0; i < hash->size; i++) {
  1245. head = &hash->table[i];
  1246. rcu_read_lock();
  1247. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1248. if (!batadv_compare_eth(backbone_gw->orig,
  1249. primary_if->net_dev->dev_addr))
  1250. continue;
  1251. backbone_gw->lasttime = jiffies;
  1252. batadv_bla_send_announce(bat_priv, backbone_gw);
  1253. if (send_loopdetect)
  1254. batadv_bla_send_loopdetect(bat_priv,
  1255. backbone_gw);
  1256. /* request_sent is only set after creation to avoid
  1257. * problems when we are not yet known as backbone gw
  1258. * in the backbone.
  1259. *
  1260. * We can reset this now after we waited some periods
  1261. * to give bridge forward delays and bla group forming
  1262. * some grace time.
  1263. */
  1264. if (atomic_read(&backbone_gw->request_sent) == 0)
  1265. continue;
  1266. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  1267. continue;
  1268. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  1269. atomic_set(&backbone_gw->request_sent, 0);
  1270. }
  1271. rcu_read_unlock();
  1272. }
  1273. out:
  1274. if (primary_if)
  1275. batadv_hardif_put(primary_if);
  1276. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1277. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1278. }
  1279. /* The hash for claim and backbone hash receive the same key because they
  1280. * are getting initialized by hash_new with the same key. Reinitializing
  1281. * them with to different keys to allow nested locking without generating
  1282. * lockdep warnings
  1283. */
  1284. static struct lock_class_key batadv_claim_hash_lock_class_key;
  1285. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  1286. /**
  1287. * batadv_bla_init - initialize all bla structures
  1288. * @bat_priv: the bat priv with all the soft interface information
  1289. *
  1290. * Return: 0 on success, < 0 on error.
  1291. */
  1292. int batadv_bla_init(struct batadv_priv *bat_priv)
  1293. {
  1294. int i;
  1295. u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1296. struct batadv_hard_iface *primary_if;
  1297. u16 crc;
  1298. unsigned long entrytime;
  1299. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1300. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1301. /* setting claim destination address */
  1302. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1303. bat_priv->bla.claim_dest.type = 0;
  1304. primary_if = batadv_primary_if_get_selected(bat_priv);
  1305. if (primary_if) {
  1306. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1307. bat_priv->bla.claim_dest.group = htons(crc);
  1308. batadv_hardif_put(primary_if);
  1309. } else {
  1310. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1311. }
  1312. /* initialize the duplicate list */
  1313. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1314. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1315. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1316. bat_priv->bla.bcast_duplist_curr = 0;
  1317. atomic_set(&bat_priv->bla.loopdetect_next,
  1318. BATADV_BLA_LOOPDETECT_PERIODS);
  1319. if (bat_priv->bla.claim_hash)
  1320. return 0;
  1321. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1322. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1323. if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
  1324. return -ENOMEM;
  1325. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1326. &batadv_claim_hash_lock_class_key);
  1327. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1328. &batadv_backbone_hash_lock_class_key);
  1329. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1330. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  1331. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1332. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1333. return 0;
  1334. }
  1335. /**
  1336. * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
  1337. * @bat_priv: the bat priv with all the soft interface information
  1338. * @skb: contains the bcast_packet to be checked
  1339. *
  1340. * check if it is on our broadcast list. Another gateway might
  1341. * have sent the same packet because it is connected to the same backbone,
  1342. * so we have to remove this duplicate.
  1343. *
  1344. * This is performed by checking the CRC, which will tell us
  1345. * with a good chance that it is the same packet. If it is furthermore
  1346. * sent by another host, drop it. We allow equal packets from
  1347. * the same host however as this might be intended.
  1348. *
  1349. * Return: true if a packet is in the duplicate list, false otherwise.
  1350. */
  1351. bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
  1352. struct sk_buff *skb)
  1353. {
  1354. int i, curr;
  1355. __be32 crc;
  1356. struct batadv_bcast_packet *bcast_packet;
  1357. struct batadv_bcast_duplist_entry *entry;
  1358. bool ret = false;
  1359. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1360. /* calculate the crc ... */
  1361. crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
  1362. spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
  1363. for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
  1364. curr = (bat_priv->bla.bcast_duplist_curr + i);
  1365. curr %= BATADV_DUPLIST_SIZE;
  1366. entry = &bat_priv->bla.bcast_duplist[curr];
  1367. /* we can stop searching if the entry is too old ;
  1368. * later entries will be even older
  1369. */
  1370. if (batadv_has_timed_out(entry->entrytime,
  1371. BATADV_DUPLIST_TIMEOUT))
  1372. break;
  1373. if (entry->crc != crc)
  1374. continue;
  1375. if (batadv_compare_eth(entry->orig, bcast_packet->orig))
  1376. continue;
  1377. /* this entry seems to match: same crc, not too old,
  1378. * and from another gw. therefore return true to forbid it.
  1379. */
  1380. ret = true;
  1381. goto out;
  1382. }
  1383. /* not found, add a new entry (overwrite the oldest entry)
  1384. * and allow it, its the first occurrence.
  1385. */
  1386. curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
  1387. curr %= BATADV_DUPLIST_SIZE;
  1388. entry = &bat_priv->bla.bcast_duplist[curr];
  1389. entry->crc = crc;
  1390. entry->entrytime = jiffies;
  1391. ether_addr_copy(entry->orig, bcast_packet->orig);
  1392. bat_priv->bla.bcast_duplist_curr = curr;
  1393. out:
  1394. spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
  1395. return ret;
  1396. }
  1397. /**
  1398. * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
  1399. * the VLAN identified by vid.
  1400. * @bat_priv: the bat priv with all the soft interface information
  1401. * @orig: originator mac address
  1402. * @vid: VLAN identifier
  1403. *
  1404. * Return: true if orig is a backbone for this vid, false otherwise.
  1405. */
  1406. bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
  1407. unsigned short vid)
  1408. {
  1409. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1410. struct hlist_head *head;
  1411. struct batadv_bla_backbone_gw *backbone_gw;
  1412. int i;
  1413. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1414. return false;
  1415. if (!hash)
  1416. return false;
  1417. for (i = 0; i < hash->size; i++) {
  1418. head = &hash->table[i];
  1419. rcu_read_lock();
  1420. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1421. if (batadv_compare_eth(backbone_gw->orig, orig) &&
  1422. backbone_gw->vid == vid) {
  1423. rcu_read_unlock();
  1424. return true;
  1425. }
  1426. }
  1427. rcu_read_unlock();
  1428. }
  1429. return false;
  1430. }
  1431. /**
  1432. * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
  1433. * @skb: the frame to be checked
  1434. * @orig_node: the orig_node of the frame
  1435. * @hdr_size: maximum length of the frame
  1436. *
  1437. * Return: true if the orig_node is also a gateway on the soft interface,
  1438. * otherwise it returns false.
  1439. */
  1440. bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1441. struct batadv_orig_node *orig_node, int hdr_size)
  1442. {
  1443. struct batadv_bla_backbone_gw *backbone_gw;
  1444. unsigned short vid;
  1445. if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
  1446. return false;
  1447. /* first, find out the vid. */
  1448. if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
  1449. return false;
  1450. vid = batadv_get_vid(skb, hdr_size);
  1451. /* see if this originator is a backbone gw for this VLAN */
  1452. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1453. orig_node->orig, vid);
  1454. if (!backbone_gw)
  1455. return false;
  1456. batadv_backbone_gw_put(backbone_gw);
  1457. return true;
  1458. }
  1459. /**
  1460. * batadv_bla_free - free all bla structures
  1461. * @bat_priv: the bat priv with all the soft interface information
  1462. *
  1463. * for softinterface free or module unload
  1464. */
  1465. void batadv_bla_free(struct batadv_priv *bat_priv)
  1466. {
  1467. struct batadv_hard_iface *primary_if;
  1468. cancel_delayed_work_sync(&bat_priv->bla.work);
  1469. primary_if = batadv_primary_if_get_selected(bat_priv);
  1470. if (bat_priv->bla.claim_hash) {
  1471. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1472. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1473. bat_priv->bla.claim_hash = NULL;
  1474. }
  1475. if (bat_priv->bla.backbone_hash) {
  1476. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1477. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1478. bat_priv->bla.backbone_hash = NULL;
  1479. }
  1480. if (primary_if)
  1481. batadv_hardif_put(primary_if);
  1482. }
  1483. /**
  1484. * batadv_bla_loopdetect_check - check and handle a detected loop
  1485. * @bat_priv: the bat priv with all the soft interface information
  1486. * @skb: the packet to check
  1487. * @primary_if: interface where the request came on
  1488. * @vid: the VLAN ID of the frame
  1489. *
  1490. * Checks if this packet is a loop detect frame which has been sent by us,
  1491. * throw an uevent and log the event if that is the case.
  1492. *
  1493. * Return: true if it is a loop detect frame which is to be dropped, false
  1494. * otherwise.
  1495. */
  1496. static bool
  1497. batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1498. struct batadv_hard_iface *primary_if,
  1499. unsigned short vid)
  1500. {
  1501. struct batadv_bla_backbone_gw *backbone_gw;
  1502. struct ethhdr *ethhdr;
  1503. ethhdr = eth_hdr(skb);
  1504. /* Only check for the MAC address and skip more checks here for
  1505. * performance reasons - this function is on the hotpath, after all.
  1506. */
  1507. if (!batadv_compare_eth(ethhdr->h_source,
  1508. bat_priv->bla.loopdetect_addr))
  1509. return false;
  1510. /* If the packet came too late, don't forward it on the mesh
  1511. * but don't consider that as loop. It might be a coincidence.
  1512. */
  1513. if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
  1514. BATADV_BLA_LOOPDETECT_TIMEOUT))
  1515. return true;
  1516. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  1517. primary_if->net_dev->dev_addr,
  1518. vid, true);
  1519. if (unlikely(!backbone_gw))
  1520. return true;
  1521. queue_work(batadv_event_workqueue, &backbone_gw->report_work);
  1522. /* backbone_gw is unreferenced in the report work function function */
  1523. return true;
  1524. }
  1525. /**
  1526. * batadv_bla_rx - check packets coming from the mesh.
  1527. * @bat_priv: the bat priv with all the soft interface information
  1528. * @skb: the frame to be checked
  1529. * @vid: the VLAN ID of the frame
  1530. * @is_bcast: the packet came in a broadcast packet type.
  1531. *
  1532. * batadv_bla_rx avoidance checks if:
  1533. * * we have to race for a claim
  1534. * * if the frame is allowed on the LAN
  1535. *
  1536. * in these cases, the skb is further handled by this function
  1537. *
  1538. * Return: true if handled, otherwise it returns false and the caller shall
  1539. * further process the skb.
  1540. */
  1541. bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1542. unsigned short vid, bool is_bcast)
  1543. {
  1544. struct batadv_bla_backbone_gw *backbone_gw;
  1545. struct ethhdr *ethhdr;
  1546. struct batadv_bla_claim search_claim, *claim = NULL;
  1547. struct batadv_hard_iface *primary_if;
  1548. bool own_claim;
  1549. bool ret;
  1550. ethhdr = eth_hdr(skb);
  1551. primary_if = batadv_primary_if_get_selected(bat_priv);
  1552. if (!primary_if)
  1553. goto handled;
  1554. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1555. goto allow;
  1556. if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
  1557. goto handled;
  1558. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1559. /* don't allow broadcasts while requests are in flight */
  1560. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
  1561. goto handled;
  1562. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1563. search_claim.vid = vid;
  1564. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1565. if (!claim) {
  1566. /* possible optimization: race for a claim */
  1567. /* No claim exists yet, claim it for us!
  1568. */
  1569. batadv_handle_claim(bat_priv, primary_if,
  1570. primary_if->net_dev->dev_addr,
  1571. ethhdr->h_source, vid);
  1572. goto allow;
  1573. }
  1574. /* if it is our own claim ... */
  1575. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1576. own_claim = batadv_compare_eth(backbone_gw->orig,
  1577. primary_if->net_dev->dev_addr);
  1578. batadv_backbone_gw_put(backbone_gw);
  1579. if (own_claim) {
  1580. /* ... allow it in any case */
  1581. claim->lasttime = jiffies;
  1582. goto allow;
  1583. }
  1584. /* if it is a broadcast ... */
  1585. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
  1586. /* ... drop it. the responsible gateway is in charge.
  1587. *
  1588. * We need to check is_bcast because with the gateway
  1589. * feature, broadcasts (like DHCP requests) may be sent
  1590. * using a unicast packet type.
  1591. */
  1592. goto handled;
  1593. } else {
  1594. /* seems the client considers us as its best gateway.
  1595. * send a claim and update the claim table
  1596. * immediately.
  1597. */
  1598. batadv_handle_claim(bat_priv, primary_if,
  1599. primary_if->net_dev->dev_addr,
  1600. ethhdr->h_source, vid);
  1601. goto allow;
  1602. }
  1603. allow:
  1604. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1605. ret = false;
  1606. goto out;
  1607. handled:
  1608. kfree_skb(skb);
  1609. ret = true;
  1610. out:
  1611. if (primary_if)
  1612. batadv_hardif_put(primary_if);
  1613. if (claim)
  1614. batadv_claim_put(claim);
  1615. return ret;
  1616. }
  1617. /**
  1618. * batadv_bla_tx - check packets going into the mesh
  1619. * @bat_priv: the bat priv with all the soft interface information
  1620. * @skb: the frame to be checked
  1621. * @vid: the VLAN ID of the frame
  1622. *
  1623. * batadv_bla_tx checks if:
  1624. * * a claim was received which has to be processed
  1625. * * the frame is allowed on the mesh
  1626. *
  1627. * in these cases, the skb is further handled by this function.
  1628. *
  1629. * This call might reallocate skb data.
  1630. *
  1631. * Return: true if handled, otherwise it returns false and the caller shall
  1632. * further process the skb.
  1633. */
  1634. bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1635. unsigned short vid)
  1636. {
  1637. struct ethhdr *ethhdr;
  1638. struct batadv_bla_claim search_claim, *claim = NULL;
  1639. struct batadv_bla_backbone_gw *backbone_gw;
  1640. struct batadv_hard_iface *primary_if;
  1641. bool client_roamed;
  1642. bool ret = false;
  1643. primary_if = batadv_primary_if_get_selected(bat_priv);
  1644. if (!primary_if)
  1645. goto out;
  1646. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1647. goto allow;
  1648. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1649. goto handled;
  1650. ethhdr = eth_hdr(skb);
  1651. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1652. /* don't allow broadcasts while requests are in flight */
  1653. if (is_multicast_ether_addr(ethhdr->h_dest))
  1654. goto handled;
  1655. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1656. search_claim.vid = vid;
  1657. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1658. /* if no claim exists, allow it. */
  1659. if (!claim)
  1660. goto allow;
  1661. /* check if we are responsible. */
  1662. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1663. client_roamed = batadv_compare_eth(backbone_gw->orig,
  1664. primary_if->net_dev->dev_addr);
  1665. batadv_backbone_gw_put(backbone_gw);
  1666. if (client_roamed) {
  1667. /* if yes, the client has roamed and we have
  1668. * to unclaim it.
  1669. */
  1670. batadv_handle_unclaim(bat_priv, primary_if,
  1671. primary_if->net_dev->dev_addr,
  1672. ethhdr->h_source, vid);
  1673. goto allow;
  1674. }
  1675. /* check if it is a multicast/broadcast frame */
  1676. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1677. /* drop it. the responsible gateway has forwarded it into
  1678. * the backbone network.
  1679. */
  1680. goto handled;
  1681. } else {
  1682. /* we must allow it. at least if we are
  1683. * responsible for the DESTINATION.
  1684. */
  1685. goto allow;
  1686. }
  1687. allow:
  1688. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1689. ret = false;
  1690. goto out;
  1691. handled:
  1692. ret = true;
  1693. out:
  1694. if (primary_if)
  1695. batadv_hardif_put(primary_if);
  1696. if (claim)
  1697. batadv_claim_put(claim);
  1698. return ret;
  1699. }
  1700. /**
  1701. * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
  1702. * @seq: seq file to print on
  1703. * @offset: not used
  1704. *
  1705. * Return: always 0
  1706. */
  1707. int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
  1708. {
  1709. struct net_device *net_dev = (struct net_device *)seq->private;
  1710. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1711. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  1712. struct batadv_bla_backbone_gw *backbone_gw;
  1713. struct batadv_bla_claim *claim;
  1714. struct batadv_hard_iface *primary_if;
  1715. struct hlist_head *head;
  1716. u16 backbone_crc;
  1717. u32 i;
  1718. bool is_own;
  1719. u8 *primary_addr;
  1720. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1721. if (!primary_if)
  1722. goto out;
  1723. primary_addr = primary_if->net_dev->dev_addr;
  1724. seq_printf(seq,
  1725. "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1726. net_dev->name, primary_addr,
  1727. ntohs(bat_priv->bla.claim_dest.group));
  1728. seq_puts(seq,
  1729. " Client VID Originator [o] (CRC )\n");
  1730. for (i = 0; i < hash->size; i++) {
  1731. head = &hash->table[i];
  1732. rcu_read_lock();
  1733. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1734. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1735. is_own = batadv_compare_eth(backbone_gw->orig,
  1736. primary_addr);
  1737. spin_lock_bh(&backbone_gw->crc_lock);
  1738. backbone_crc = backbone_gw->crc;
  1739. spin_unlock_bh(&backbone_gw->crc_lock);
  1740. seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
  1741. claim->addr, BATADV_PRINT_VID(claim->vid),
  1742. backbone_gw->orig,
  1743. (is_own ? 'x' : ' '),
  1744. backbone_crc);
  1745. batadv_backbone_gw_put(backbone_gw);
  1746. }
  1747. rcu_read_unlock();
  1748. }
  1749. out:
  1750. if (primary_if)
  1751. batadv_hardif_put(primary_if);
  1752. return 0;
  1753. }
  1754. /**
  1755. * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
  1756. * file
  1757. * @seq: seq file to print on
  1758. * @offset: not used
  1759. *
  1760. * Return: always 0
  1761. */
  1762. int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
  1763. {
  1764. struct net_device *net_dev = (struct net_device *)seq->private;
  1765. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1766. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1767. struct batadv_bla_backbone_gw *backbone_gw;
  1768. struct batadv_hard_iface *primary_if;
  1769. struct hlist_head *head;
  1770. int secs, msecs;
  1771. u16 backbone_crc;
  1772. u32 i;
  1773. bool is_own;
  1774. u8 *primary_addr;
  1775. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1776. if (!primary_if)
  1777. goto out;
  1778. primary_addr = primary_if->net_dev->dev_addr;
  1779. seq_printf(seq,
  1780. "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1781. net_dev->name, primary_addr,
  1782. ntohs(bat_priv->bla.claim_dest.group));
  1783. seq_puts(seq, " Originator VID last seen (CRC )\n");
  1784. for (i = 0; i < hash->size; i++) {
  1785. head = &hash->table[i];
  1786. rcu_read_lock();
  1787. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1788. msecs = jiffies_to_msecs(jiffies -
  1789. backbone_gw->lasttime);
  1790. secs = msecs / 1000;
  1791. msecs = msecs % 1000;
  1792. is_own = batadv_compare_eth(backbone_gw->orig,
  1793. primary_addr);
  1794. if (is_own)
  1795. continue;
  1796. spin_lock_bh(&backbone_gw->crc_lock);
  1797. backbone_crc = backbone_gw->crc;
  1798. spin_unlock_bh(&backbone_gw->crc_lock);
  1799. seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
  1800. backbone_gw->orig,
  1801. BATADV_PRINT_VID(backbone_gw->vid), secs,
  1802. msecs, backbone_crc);
  1803. }
  1804. rcu_read_unlock();
  1805. }
  1806. out:
  1807. if (primary_if)
  1808. batadv_hardif_put(primary_if);
  1809. return 0;
  1810. }