bridge_loop_avoidance.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /* Copyright (C) 2011-2015 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/list.h>
  33. #include <linux/lockdep.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/rculist.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/stddef.h>
  42. #include <linux/string.h>
  43. #include <linux/workqueue.h>
  44. #include <net/arp.h>
  45. #include "hard-interface.h"
  46. #include "hash.h"
  47. #include "originator.h"
  48. #include "packet.h"
  49. #include "translation-table.h"
  50. static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
  51. static void batadv_bla_periodic_work(struct work_struct *work);
  52. static void
  53. batadv_bla_send_announce(struct batadv_priv *bat_priv,
  54. struct batadv_bla_backbone_gw *backbone_gw);
  55. /* return the index of the claim */
  56. static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
  57. {
  58. struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  59. uint32_t hash = 0;
  60. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  61. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  62. return hash % size;
  63. }
  64. /* return the index of the backbone gateway */
  65. static inline uint32_t batadv_choose_backbone_gw(const void *data,
  66. uint32_t size)
  67. {
  68. const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
  69. uint32_t hash = 0;
  70. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  71. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  72. return hash % size;
  73. }
  74. /* compares address and vid of two backbone gws */
  75. static int batadv_compare_backbone_gw(const struct hlist_node *node,
  76. const void *data2)
  77. {
  78. const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
  79. hash_entry);
  80. const struct batadv_bla_backbone_gw *gw1 = data1, *gw2 = data2;
  81. if (!batadv_compare_eth(gw1->orig, gw2->orig))
  82. return 0;
  83. if (gw1->vid != gw2->vid)
  84. return 0;
  85. return 1;
  86. }
  87. /* compares address and vid of two claims */
  88. static int batadv_compare_claim(const struct hlist_node *node,
  89. const void *data2)
  90. {
  91. const void *data1 = container_of(node, struct batadv_bla_claim,
  92. hash_entry);
  93. const struct batadv_bla_claim *cl1 = data1, *cl2 = data2;
  94. if (!batadv_compare_eth(cl1->addr, cl2->addr))
  95. return 0;
  96. if (cl1->vid != cl2->vid)
  97. return 0;
  98. return 1;
  99. }
  100. /* free a backbone gw */
  101. static void
  102. batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
  103. {
  104. if (atomic_dec_and_test(&backbone_gw->refcount))
  105. kfree_rcu(backbone_gw, rcu);
  106. }
  107. /* finally deinitialize the claim */
  108. static void batadv_claim_free_rcu(struct rcu_head *rcu)
  109. {
  110. struct batadv_bla_claim *claim;
  111. claim = container_of(rcu, struct batadv_bla_claim, rcu);
  112. batadv_backbone_gw_free_ref(claim->backbone_gw);
  113. kfree(claim);
  114. }
  115. /* free a claim, call claim_free_rcu if its the last reference */
  116. static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
  117. {
  118. if (atomic_dec_and_test(&claim->refcount))
  119. call_rcu(&claim->rcu, batadv_claim_free_rcu);
  120. }
  121. /**
  122. * batadv_claim_hash_find
  123. * @bat_priv: the bat priv with all the soft interface information
  124. * @data: search data (may be local/static data)
  125. *
  126. * looks for a claim in the hash, and returns it if found
  127. * or NULL otherwise.
  128. */
  129. static struct batadv_bla_claim
  130. *batadv_claim_hash_find(struct batadv_priv *bat_priv,
  131. struct batadv_bla_claim *data)
  132. {
  133. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  134. struct hlist_head *head;
  135. struct batadv_bla_claim *claim;
  136. struct batadv_bla_claim *claim_tmp = NULL;
  137. int index;
  138. if (!hash)
  139. return NULL;
  140. index = batadv_choose_claim(data, hash->size);
  141. head = &hash->table[index];
  142. rcu_read_lock();
  143. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  144. if (!batadv_compare_claim(&claim->hash_entry, data))
  145. continue;
  146. if (!atomic_inc_not_zero(&claim->refcount))
  147. continue;
  148. claim_tmp = claim;
  149. break;
  150. }
  151. rcu_read_unlock();
  152. return claim_tmp;
  153. }
  154. /**
  155. * batadv_backbone_hash_find - looks for a claim in the hash
  156. * @bat_priv: the bat priv with all the soft interface information
  157. * @addr: the address of the originator
  158. * @vid: the VLAN ID
  159. *
  160. * Returns claim if found or NULL otherwise.
  161. */
  162. static struct batadv_bla_backbone_gw *
  163. batadv_backbone_hash_find(struct batadv_priv *bat_priv,
  164. uint8_t *addr, unsigned short vid)
  165. {
  166. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  167. struct hlist_head *head;
  168. struct batadv_bla_backbone_gw search_entry, *backbone_gw;
  169. struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
  170. int index;
  171. if (!hash)
  172. return NULL;
  173. ether_addr_copy(search_entry.orig, addr);
  174. search_entry.vid = vid;
  175. index = batadv_choose_backbone_gw(&search_entry, hash->size);
  176. head = &hash->table[index];
  177. rcu_read_lock();
  178. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  179. if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
  180. &search_entry))
  181. continue;
  182. if (!atomic_inc_not_zero(&backbone_gw->refcount))
  183. continue;
  184. backbone_gw_tmp = backbone_gw;
  185. break;
  186. }
  187. rcu_read_unlock();
  188. return backbone_gw_tmp;
  189. }
  190. /* delete all claims for a backbone */
  191. static void
  192. batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
  193. {
  194. struct batadv_hashtable *hash;
  195. struct hlist_node *node_tmp;
  196. struct hlist_head *head;
  197. struct batadv_bla_claim *claim;
  198. int i;
  199. spinlock_t *list_lock; /* protects write access to the hash lists */
  200. hash = backbone_gw->bat_priv->bla.claim_hash;
  201. if (!hash)
  202. return;
  203. for (i = 0; i < hash->size; i++) {
  204. head = &hash->table[i];
  205. list_lock = &hash->list_locks[i];
  206. spin_lock_bh(list_lock);
  207. hlist_for_each_entry_safe(claim, node_tmp,
  208. head, hash_entry) {
  209. if (claim->backbone_gw != backbone_gw)
  210. continue;
  211. batadv_claim_free_ref(claim);
  212. hlist_del_rcu(&claim->hash_entry);
  213. }
  214. spin_unlock_bh(list_lock);
  215. }
  216. /* all claims gone, initialize CRC */
  217. backbone_gw->crc = BATADV_BLA_CRC_INIT;
  218. }
  219. /**
  220. * batadv_bla_send_claim - sends a claim frame according to the provided info
  221. * @bat_priv: the bat priv with all the soft interface information
  222. * @mac: the mac address to be announced within the claim
  223. * @vid: the VLAN ID
  224. * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
  225. */
  226. static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
  227. unsigned short vid, int claimtype)
  228. {
  229. struct sk_buff *skb;
  230. struct ethhdr *ethhdr;
  231. struct batadv_hard_iface *primary_if;
  232. struct net_device *soft_iface;
  233. uint8_t *hw_src;
  234. struct batadv_bla_claim_dst local_claim_dest;
  235. __be32 zeroip = 0;
  236. primary_if = batadv_primary_if_get_selected(bat_priv);
  237. if (!primary_if)
  238. return;
  239. memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
  240. sizeof(local_claim_dest));
  241. local_claim_dest.type = claimtype;
  242. soft_iface = primary_if->soft_iface;
  243. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  244. /* IP DST: 0.0.0.0 */
  245. zeroip,
  246. primary_if->soft_iface,
  247. /* IP SRC: 0.0.0.0 */
  248. zeroip,
  249. /* Ethernet DST: Broadcast */
  250. NULL,
  251. /* Ethernet SRC/HW SRC: originator mac */
  252. primary_if->net_dev->dev_addr,
  253. /* HW DST: FF:43:05:XX:YY:YY
  254. * with XX = claim type
  255. * and YY:YY = group id
  256. */
  257. (uint8_t *)&local_claim_dest);
  258. if (!skb)
  259. goto out;
  260. ethhdr = (struct ethhdr *)skb->data;
  261. hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
  262. /* now we pretend that the client would have sent this ... */
  263. switch (claimtype) {
  264. case BATADV_CLAIM_TYPE_CLAIM:
  265. /* normal claim frame
  266. * set Ethernet SRC to the clients mac
  267. */
  268. ether_addr_copy(ethhdr->h_source, mac);
  269. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  270. "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
  271. BATADV_PRINT_VID(vid));
  272. break;
  273. case BATADV_CLAIM_TYPE_UNCLAIM:
  274. /* unclaim frame
  275. * set HW SRC to the clients mac
  276. */
  277. ether_addr_copy(hw_src, mac);
  278. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  279. "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
  280. BATADV_PRINT_VID(vid));
  281. break;
  282. case BATADV_CLAIM_TYPE_ANNOUNCE:
  283. /* announcement frame
  284. * set HW SRC to the special mac containg the crc
  285. */
  286. ether_addr_copy(hw_src, mac);
  287. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  288. "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
  289. ethhdr->h_source, BATADV_PRINT_VID(vid));
  290. break;
  291. case BATADV_CLAIM_TYPE_REQUEST:
  292. /* request frame
  293. * set HW SRC and header destination to the receiving backbone
  294. * gws mac
  295. */
  296. ether_addr_copy(hw_src, mac);
  297. ether_addr_copy(ethhdr->h_dest, mac);
  298. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  299. "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
  300. ethhdr->h_source, ethhdr->h_dest,
  301. BATADV_PRINT_VID(vid));
  302. break;
  303. }
  304. if (vid & BATADV_VLAN_HAS_TAG)
  305. skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
  306. vid & VLAN_VID_MASK);
  307. skb_reset_mac_header(skb);
  308. skb->protocol = eth_type_trans(skb, soft_iface);
  309. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  310. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  311. skb->len + ETH_HLEN);
  312. soft_iface->last_rx = jiffies;
  313. netif_rx(skb);
  314. out:
  315. if (primary_if)
  316. batadv_hardif_free_ref(primary_if);
  317. }
  318. /**
  319. * batadv_bla_get_backbone_gw
  320. * @bat_priv: the bat priv with all the soft interface information
  321. * @orig: the mac address of the originator
  322. * @vid: the VLAN ID
  323. * @own_backbone: set if the requested backbone is local
  324. *
  325. * searches for the backbone gw or creates a new one if it could not
  326. * be found.
  327. */
  328. static struct batadv_bla_backbone_gw *
  329. batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
  330. unsigned short vid, bool own_backbone)
  331. {
  332. struct batadv_bla_backbone_gw *entry;
  333. struct batadv_orig_node *orig_node;
  334. int hash_added;
  335. entry = batadv_backbone_hash_find(bat_priv, orig, vid);
  336. if (entry)
  337. return entry;
  338. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  339. "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
  340. orig, BATADV_PRINT_VID(vid));
  341. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  342. if (!entry)
  343. return NULL;
  344. entry->vid = vid;
  345. entry->lasttime = jiffies;
  346. entry->crc = BATADV_BLA_CRC_INIT;
  347. entry->bat_priv = bat_priv;
  348. atomic_set(&entry->request_sent, 0);
  349. atomic_set(&entry->wait_periods, 0);
  350. ether_addr_copy(entry->orig, orig);
  351. /* one for the hash, one for returning */
  352. atomic_set(&entry->refcount, 2);
  353. hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
  354. batadv_compare_backbone_gw,
  355. batadv_choose_backbone_gw, entry,
  356. &entry->hash_entry);
  357. if (unlikely(hash_added != 0)) {
  358. /* hash failed, free the structure */
  359. kfree(entry);
  360. return NULL;
  361. }
  362. /* this is a gateway now, remove any TT entry on this VLAN */
  363. orig_node = batadv_orig_hash_find(bat_priv, orig);
  364. if (orig_node) {
  365. batadv_tt_global_del_orig(bat_priv, orig_node, vid,
  366. "became a backbone gateway");
  367. batadv_orig_node_free_ref(orig_node);
  368. }
  369. if (own_backbone) {
  370. batadv_bla_send_announce(bat_priv, entry);
  371. /* this will be decreased in the worker thread */
  372. atomic_inc(&entry->request_sent);
  373. atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
  374. atomic_inc(&bat_priv->bla.num_requests);
  375. }
  376. return entry;
  377. }
  378. /* update or add the own backbone gw to make sure we announce
  379. * where we receive other backbone gws
  380. */
  381. static void
  382. batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
  383. struct batadv_hard_iface *primary_if,
  384. unsigned short vid)
  385. {
  386. struct batadv_bla_backbone_gw *backbone_gw;
  387. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  388. primary_if->net_dev->dev_addr,
  389. vid, true);
  390. if (unlikely(!backbone_gw))
  391. return;
  392. backbone_gw->lasttime = jiffies;
  393. batadv_backbone_gw_free_ref(backbone_gw);
  394. }
  395. /**
  396. * batadv_bla_answer_request - answer a bla request by sending own claims
  397. * @bat_priv: the bat priv with all the soft interface information
  398. * @primary_if: interface where the request came on
  399. * @vid: the vid where the request came on
  400. *
  401. * Repeat all of our own claims, and finally send an ANNOUNCE frame
  402. * to allow the requester another check if the CRC is correct now.
  403. */
  404. static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
  405. struct batadv_hard_iface *primary_if,
  406. unsigned short vid)
  407. {
  408. struct hlist_head *head;
  409. struct batadv_hashtable *hash;
  410. struct batadv_bla_claim *claim;
  411. struct batadv_bla_backbone_gw *backbone_gw;
  412. int i;
  413. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  414. "bla_answer_request(): received a claim request, send all of our own claims again\n");
  415. backbone_gw = batadv_backbone_hash_find(bat_priv,
  416. primary_if->net_dev->dev_addr,
  417. vid);
  418. if (!backbone_gw)
  419. return;
  420. hash = bat_priv->bla.claim_hash;
  421. for (i = 0; i < hash->size; i++) {
  422. head = &hash->table[i];
  423. rcu_read_lock();
  424. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  425. /* only own claims are interesting */
  426. if (claim->backbone_gw != backbone_gw)
  427. continue;
  428. batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
  429. BATADV_CLAIM_TYPE_CLAIM);
  430. }
  431. rcu_read_unlock();
  432. }
  433. /* finally, send an announcement frame */
  434. batadv_bla_send_announce(bat_priv, backbone_gw);
  435. batadv_backbone_gw_free_ref(backbone_gw);
  436. }
  437. /**
  438. * batadv_bla_send_request - send a request to repeat claims
  439. * @backbone_gw: the backbone gateway from whom we are out of sync
  440. *
  441. * When the crc is wrong, ask the backbone gateway for a full table update.
  442. * After the request, it will repeat all of his own claims and finally
  443. * send an announcement claim with which we can check again.
  444. */
  445. static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
  446. {
  447. /* first, remove all old entries */
  448. batadv_bla_del_backbone_claims(backbone_gw);
  449. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  450. "Sending REQUEST to %pM\n", backbone_gw->orig);
  451. /* send request */
  452. batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
  453. backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
  454. /* no local broadcasts should be sent or received, for now. */
  455. if (!atomic_read(&backbone_gw->request_sent)) {
  456. atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
  457. atomic_set(&backbone_gw->request_sent, 1);
  458. }
  459. }
  460. /**
  461. * batadv_bla_send_announce
  462. * @bat_priv: the bat priv with all the soft interface information
  463. * @backbone_gw: our backbone gateway which should be announced
  464. *
  465. * This function sends an announcement. It is called from multiple
  466. * places.
  467. */
  468. static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
  469. struct batadv_bla_backbone_gw *backbone_gw)
  470. {
  471. uint8_t mac[ETH_ALEN];
  472. __be16 crc;
  473. memcpy(mac, batadv_announce_mac, 4);
  474. crc = htons(backbone_gw->crc);
  475. memcpy(&mac[4], &crc, 2);
  476. batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
  477. BATADV_CLAIM_TYPE_ANNOUNCE);
  478. }
  479. /**
  480. * batadv_bla_add_claim - Adds a claim in the claim hash
  481. * @bat_priv: the bat priv with all the soft interface information
  482. * @mac: the mac address of the claim
  483. * @vid: the VLAN ID of the frame
  484. * @backbone_gw: the backbone gateway which claims it
  485. */
  486. static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
  487. const uint8_t *mac, const unsigned short vid,
  488. struct batadv_bla_backbone_gw *backbone_gw)
  489. {
  490. struct batadv_bla_claim *claim;
  491. struct batadv_bla_claim search_claim;
  492. int hash_added;
  493. ether_addr_copy(search_claim.addr, mac);
  494. search_claim.vid = vid;
  495. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  496. /* create a new claim entry if it does not exist yet. */
  497. if (!claim) {
  498. claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
  499. if (!claim)
  500. return;
  501. ether_addr_copy(claim->addr, mac);
  502. claim->vid = vid;
  503. claim->lasttime = jiffies;
  504. claim->backbone_gw = backbone_gw;
  505. atomic_set(&claim->refcount, 2);
  506. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  507. "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
  508. mac, BATADV_PRINT_VID(vid));
  509. hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
  510. batadv_compare_claim,
  511. batadv_choose_claim, claim,
  512. &claim->hash_entry);
  513. if (unlikely(hash_added != 0)) {
  514. /* only local changes happened. */
  515. kfree(claim);
  516. return;
  517. }
  518. } else {
  519. claim->lasttime = jiffies;
  520. if (claim->backbone_gw == backbone_gw)
  521. /* no need to register a new backbone */
  522. goto claim_free_ref;
  523. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  524. "bla_add_claim(): changing ownership for %pM, vid %d\n",
  525. mac, BATADV_PRINT_VID(vid));
  526. claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  527. batadv_backbone_gw_free_ref(claim->backbone_gw);
  528. }
  529. /* set (new) backbone gw */
  530. atomic_inc(&backbone_gw->refcount);
  531. claim->backbone_gw = backbone_gw;
  532. backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  533. backbone_gw->lasttime = jiffies;
  534. claim_free_ref:
  535. batadv_claim_free_ref(claim);
  536. }
  537. /* Delete a claim from the claim hash which has the
  538. * given mac address and vid.
  539. */
  540. static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
  541. const uint8_t *mac, const unsigned short vid)
  542. {
  543. struct batadv_bla_claim search_claim, *claim;
  544. ether_addr_copy(search_claim.addr, mac);
  545. search_claim.vid = vid;
  546. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  547. if (!claim)
  548. return;
  549. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
  550. mac, BATADV_PRINT_VID(vid));
  551. batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
  552. batadv_choose_claim, claim);
  553. batadv_claim_free_ref(claim); /* reference from the hash is gone */
  554. claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  555. /* don't need the reference from hash_find() anymore */
  556. batadv_claim_free_ref(claim);
  557. }
  558. /* check for ANNOUNCE frame, return 1 if handled */
  559. static int batadv_handle_announce(struct batadv_priv *bat_priv,
  560. uint8_t *an_addr, uint8_t *backbone_addr,
  561. unsigned short vid)
  562. {
  563. struct batadv_bla_backbone_gw *backbone_gw;
  564. uint16_t crc;
  565. if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
  566. return 0;
  567. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  568. false);
  569. if (unlikely(!backbone_gw))
  570. return 1;
  571. /* handle as ANNOUNCE frame */
  572. backbone_gw->lasttime = jiffies;
  573. crc = ntohs(*((__be16 *)(&an_addr[4])));
  574. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  575. "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
  576. BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
  577. if (backbone_gw->crc != crc) {
  578. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  579. "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
  580. backbone_gw->orig,
  581. BATADV_PRINT_VID(backbone_gw->vid),
  582. backbone_gw->crc, crc);
  583. batadv_bla_send_request(backbone_gw);
  584. } else {
  585. /* if we have sent a request and the crc was OK,
  586. * we can allow traffic again.
  587. */
  588. if (atomic_read(&backbone_gw->request_sent)) {
  589. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  590. atomic_set(&backbone_gw->request_sent, 0);
  591. }
  592. }
  593. batadv_backbone_gw_free_ref(backbone_gw);
  594. return 1;
  595. }
  596. /* check for REQUEST frame, return 1 if handled */
  597. static int batadv_handle_request(struct batadv_priv *bat_priv,
  598. struct batadv_hard_iface *primary_if,
  599. uint8_t *backbone_addr,
  600. struct ethhdr *ethhdr, unsigned short vid)
  601. {
  602. /* check for REQUEST frame */
  603. if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
  604. return 0;
  605. /* sanity check, this should not happen on a normal switch,
  606. * we ignore it in this case.
  607. */
  608. if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
  609. return 1;
  610. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  611. "handle_request(): REQUEST vid %d (sent by %pM)...\n",
  612. BATADV_PRINT_VID(vid), ethhdr->h_source);
  613. batadv_bla_answer_request(bat_priv, primary_if, vid);
  614. return 1;
  615. }
  616. /* check for UNCLAIM frame, return 1 if handled */
  617. static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
  618. struct batadv_hard_iface *primary_if,
  619. uint8_t *backbone_addr,
  620. uint8_t *claim_addr, unsigned short vid)
  621. {
  622. struct batadv_bla_backbone_gw *backbone_gw;
  623. /* unclaim in any case if it is our own */
  624. if (primary_if && batadv_compare_eth(backbone_addr,
  625. primary_if->net_dev->dev_addr))
  626. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  627. BATADV_CLAIM_TYPE_UNCLAIM);
  628. backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
  629. if (!backbone_gw)
  630. return 1;
  631. /* this must be an UNCLAIM frame */
  632. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  633. "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
  634. claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
  635. batadv_bla_del_claim(bat_priv, claim_addr, vid);
  636. batadv_backbone_gw_free_ref(backbone_gw);
  637. return 1;
  638. }
  639. /* check for CLAIM frame, return 1 if handled */
  640. static int batadv_handle_claim(struct batadv_priv *bat_priv,
  641. struct batadv_hard_iface *primary_if,
  642. uint8_t *backbone_addr, uint8_t *claim_addr,
  643. unsigned short vid)
  644. {
  645. struct batadv_bla_backbone_gw *backbone_gw;
  646. /* register the gateway if not yet available, and add the claim. */
  647. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  648. false);
  649. if (unlikely(!backbone_gw))
  650. return 1;
  651. /* this must be a CLAIM frame */
  652. batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
  653. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  654. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  655. BATADV_CLAIM_TYPE_CLAIM);
  656. /* TODO: we could call something like tt_local_del() here. */
  657. batadv_backbone_gw_free_ref(backbone_gw);
  658. return 1;
  659. }
  660. /**
  661. * batadv_check_claim_group
  662. * @bat_priv: the bat priv with all the soft interface information
  663. * @primary_if: the primary interface of this batman interface
  664. * @hw_src: the Hardware source in the ARP Header
  665. * @hw_dst: the Hardware destination in the ARP Header
  666. * @ethhdr: pointer to the Ethernet header of the claim frame
  667. *
  668. * checks if it is a claim packet and if its on the same group.
  669. * This function also applies the group ID of the sender
  670. * if it is in the same mesh.
  671. *
  672. * returns:
  673. * 2 - if it is a claim packet and on the same group
  674. * 1 - if is a claim packet from another group
  675. * 0 - if it is not a claim packet
  676. */
  677. static int batadv_check_claim_group(struct batadv_priv *bat_priv,
  678. struct batadv_hard_iface *primary_if,
  679. uint8_t *hw_src, uint8_t *hw_dst,
  680. struct ethhdr *ethhdr)
  681. {
  682. uint8_t *backbone_addr;
  683. struct batadv_orig_node *orig_node;
  684. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  685. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  686. bla_dst_own = &bat_priv->bla.claim_dest;
  687. /* if announcement packet, use the source,
  688. * otherwise assume it is in the hw_src
  689. */
  690. switch (bla_dst->type) {
  691. case BATADV_CLAIM_TYPE_CLAIM:
  692. backbone_addr = hw_src;
  693. break;
  694. case BATADV_CLAIM_TYPE_REQUEST:
  695. case BATADV_CLAIM_TYPE_ANNOUNCE:
  696. case BATADV_CLAIM_TYPE_UNCLAIM:
  697. backbone_addr = ethhdr->h_source;
  698. break;
  699. default:
  700. return 0;
  701. }
  702. /* don't accept claim frames from ourselves */
  703. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  704. return 0;
  705. /* if its already the same group, it is fine. */
  706. if (bla_dst->group == bla_dst_own->group)
  707. return 2;
  708. /* lets see if this originator is in our mesh */
  709. orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
  710. /* dont accept claims from gateways which are not in
  711. * the same mesh or group.
  712. */
  713. if (!orig_node)
  714. return 1;
  715. /* if our mesh friends mac is bigger, use it for ourselves. */
  716. if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
  717. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  718. "taking other backbones claim group: %#.4x\n",
  719. ntohs(bla_dst->group));
  720. bla_dst_own->group = bla_dst->group;
  721. }
  722. batadv_orig_node_free_ref(orig_node);
  723. return 2;
  724. }
  725. /**
  726. * batadv_bla_process_claim
  727. * @bat_priv: the bat priv with all the soft interface information
  728. * @primary_if: the primary hard interface of this batman soft interface
  729. * @skb: the frame to be checked
  730. *
  731. * Check if this is a claim frame, and process it accordingly.
  732. *
  733. * returns 1 if it was a claim frame, otherwise return 0 to
  734. * tell the callee that it can use the frame on its own.
  735. */
  736. static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
  737. struct batadv_hard_iface *primary_if,
  738. struct sk_buff *skb)
  739. {
  740. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  741. uint8_t *hw_src, *hw_dst;
  742. struct vlan_hdr *vhdr, vhdr_buf;
  743. struct ethhdr *ethhdr;
  744. struct arphdr *arphdr;
  745. unsigned short vid;
  746. int vlan_depth = 0;
  747. __be16 proto;
  748. int headlen;
  749. int ret;
  750. vid = batadv_get_vid(skb, 0);
  751. ethhdr = eth_hdr(skb);
  752. proto = ethhdr->h_proto;
  753. headlen = ETH_HLEN;
  754. if (vid & BATADV_VLAN_HAS_TAG) {
  755. /* Traverse the VLAN/Ethertypes.
  756. *
  757. * At this point it is known that the first protocol is a VLAN
  758. * header, so start checking at the encapsulated protocol.
  759. *
  760. * The depth of the VLAN headers is recorded to drop BLA claim
  761. * frames encapsulated into multiple VLAN headers (QinQ).
  762. */
  763. do {
  764. vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
  765. &vhdr_buf);
  766. if (!vhdr)
  767. return 0;
  768. proto = vhdr->h_vlan_encapsulated_proto;
  769. headlen += VLAN_HLEN;
  770. vlan_depth++;
  771. } while (proto == htons(ETH_P_8021Q));
  772. }
  773. if (proto != htons(ETH_P_ARP))
  774. return 0; /* not a claim frame */
  775. /* this must be a ARP frame. check if it is a claim. */
  776. if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
  777. return 0;
  778. /* pskb_may_pull() may have modified the pointers, get ethhdr again */
  779. ethhdr = eth_hdr(skb);
  780. arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
  781. /* Check whether the ARP frame carries a valid
  782. * IP information
  783. */
  784. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  785. return 0;
  786. if (arphdr->ar_pro != htons(ETH_P_IP))
  787. return 0;
  788. if (arphdr->ar_hln != ETH_ALEN)
  789. return 0;
  790. if (arphdr->ar_pln != 4)
  791. return 0;
  792. hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
  793. hw_dst = hw_src + ETH_ALEN + 4;
  794. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  795. bla_dst_own = &bat_priv->bla.claim_dest;
  796. /* check if it is a claim frame in general */
  797. if (memcmp(bla_dst->magic, bla_dst_own->magic,
  798. sizeof(bla_dst->magic)) != 0)
  799. return 0;
  800. /* check if there is a claim frame encapsulated deeper in (QinQ) and
  801. * drop that, as this is not supported by BLA but should also not be
  802. * sent via the mesh.
  803. */
  804. if (vlan_depth > 1)
  805. return 1;
  806. /* check if it is a claim frame. */
  807. ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
  808. ethhdr);
  809. if (ret == 1)
  810. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  811. "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  812. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
  813. hw_dst);
  814. if (ret < 2)
  815. return ret;
  816. /* become a backbone gw ourselves on this vlan if not happened yet */
  817. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  818. /* check for the different types of claim frames ... */
  819. switch (bla_dst->type) {
  820. case BATADV_CLAIM_TYPE_CLAIM:
  821. if (batadv_handle_claim(bat_priv, primary_if, hw_src,
  822. ethhdr->h_source, vid))
  823. return 1;
  824. break;
  825. case BATADV_CLAIM_TYPE_UNCLAIM:
  826. if (batadv_handle_unclaim(bat_priv, primary_if,
  827. ethhdr->h_source, hw_src, vid))
  828. return 1;
  829. break;
  830. case BATADV_CLAIM_TYPE_ANNOUNCE:
  831. if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
  832. vid))
  833. return 1;
  834. break;
  835. case BATADV_CLAIM_TYPE_REQUEST:
  836. if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
  837. vid))
  838. return 1;
  839. break;
  840. }
  841. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  842. "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",
  843. ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
  844. return 1;
  845. }
  846. /* Check when we last heard from other nodes, and remove them in case of
  847. * a time out, or clean all backbone gws if now is set.
  848. */
  849. static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
  850. {
  851. struct batadv_bla_backbone_gw *backbone_gw;
  852. struct hlist_node *node_tmp;
  853. struct hlist_head *head;
  854. struct batadv_hashtable *hash;
  855. spinlock_t *list_lock; /* protects write access to the hash lists */
  856. int i;
  857. hash = bat_priv->bla.backbone_hash;
  858. if (!hash)
  859. return;
  860. for (i = 0; i < hash->size; i++) {
  861. head = &hash->table[i];
  862. list_lock = &hash->list_locks[i];
  863. spin_lock_bh(list_lock);
  864. hlist_for_each_entry_safe(backbone_gw, node_tmp,
  865. head, hash_entry) {
  866. if (now)
  867. goto purge_now;
  868. if (!batadv_has_timed_out(backbone_gw->lasttime,
  869. BATADV_BLA_BACKBONE_TIMEOUT))
  870. continue;
  871. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  872. "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
  873. backbone_gw->orig);
  874. purge_now:
  875. /* don't wait for the pending request anymore */
  876. if (atomic_read(&backbone_gw->request_sent))
  877. atomic_dec(&bat_priv->bla.num_requests);
  878. batadv_bla_del_backbone_claims(backbone_gw);
  879. hlist_del_rcu(&backbone_gw->hash_entry);
  880. batadv_backbone_gw_free_ref(backbone_gw);
  881. }
  882. spin_unlock_bh(list_lock);
  883. }
  884. }
  885. /**
  886. * batadv_bla_purge_claims
  887. * @bat_priv: the bat priv with all the soft interface information
  888. * @primary_if: the selected primary interface, may be NULL if now is set
  889. * @now: whether the whole hash shall be wiped now
  890. *
  891. * Check when we heard last time from our own claims, and remove them in case of
  892. * a time out, or clean all claims if now is set
  893. */
  894. static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
  895. struct batadv_hard_iface *primary_if,
  896. int now)
  897. {
  898. struct batadv_bla_claim *claim;
  899. struct hlist_head *head;
  900. struct batadv_hashtable *hash;
  901. int i;
  902. hash = bat_priv->bla.claim_hash;
  903. if (!hash)
  904. return;
  905. for (i = 0; i < hash->size; i++) {
  906. head = &hash->table[i];
  907. rcu_read_lock();
  908. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  909. if (now)
  910. goto purge_now;
  911. if (!batadv_compare_eth(claim->backbone_gw->orig,
  912. primary_if->net_dev->dev_addr))
  913. continue;
  914. if (!batadv_has_timed_out(claim->lasttime,
  915. BATADV_BLA_CLAIM_TIMEOUT))
  916. continue;
  917. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  918. "bla_purge_claims(): %pM, vid %d, time out\n",
  919. claim->addr, claim->vid);
  920. purge_now:
  921. batadv_handle_unclaim(bat_priv, primary_if,
  922. claim->backbone_gw->orig,
  923. claim->addr, claim->vid);
  924. }
  925. rcu_read_unlock();
  926. }
  927. }
  928. /**
  929. * batadv_bla_update_orig_address
  930. * @bat_priv: the bat priv with all the soft interface information
  931. * @primary_if: the new selected primary_if
  932. * @oldif: the old primary interface, may be NULL
  933. *
  934. * Update the backbone gateways when the own orig address changes.
  935. */
  936. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  937. struct batadv_hard_iface *primary_if,
  938. struct batadv_hard_iface *oldif)
  939. {
  940. struct batadv_bla_backbone_gw *backbone_gw;
  941. struct hlist_head *head;
  942. struct batadv_hashtable *hash;
  943. __be16 group;
  944. int i;
  945. /* reset bridge loop avoidance group id */
  946. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  947. bat_priv->bla.claim_dest.group = group;
  948. /* purge everything when bridge loop avoidance is turned off */
  949. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  950. oldif = NULL;
  951. if (!oldif) {
  952. batadv_bla_purge_claims(bat_priv, NULL, 1);
  953. batadv_bla_purge_backbone_gw(bat_priv, 1);
  954. return;
  955. }
  956. hash = bat_priv->bla.backbone_hash;
  957. if (!hash)
  958. return;
  959. for (i = 0; i < hash->size; i++) {
  960. head = &hash->table[i];
  961. rcu_read_lock();
  962. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  963. /* own orig still holds the old value. */
  964. if (!batadv_compare_eth(backbone_gw->orig,
  965. oldif->net_dev->dev_addr))
  966. continue;
  967. ether_addr_copy(backbone_gw->orig,
  968. primary_if->net_dev->dev_addr);
  969. /* send an announce frame so others will ask for our
  970. * claims and update their tables.
  971. */
  972. batadv_bla_send_announce(bat_priv, backbone_gw);
  973. }
  974. rcu_read_unlock();
  975. }
  976. }
  977. /* periodic work to do:
  978. * * purge structures when they are too old
  979. * * send announcements
  980. */
  981. static void batadv_bla_periodic_work(struct work_struct *work)
  982. {
  983. struct delayed_work *delayed_work;
  984. struct batadv_priv *bat_priv;
  985. struct batadv_priv_bla *priv_bla;
  986. struct hlist_head *head;
  987. struct batadv_bla_backbone_gw *backbone_gw;
  988. struct batadv_hashtable *hash;
  989. struct batadv_hard_iface *primary_if;
  990. int i;
  991. delayed_work = container_of(work, struct delayed_work, work);
  992. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  993. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  994. primary_if = batadv_primary_if_get_selected(bat_priv);
  995. if (!primary_if)
  996. goto out;
  997. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  998. batadv_bla_purge_backbone_gw(bat_priv, 0);
  999. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1000. goto out;
  1001. hash = bat_priv->bla.backbone_hash;
  1002. if (!hash)
  1003. goto out;
  1004. for (i = 0; i < hash->size; i++) {
  1005. head = &hash->table[i];
  1006. rcu_read_lock();
  1007. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1008. if (!batadv_compare_eth(backbone_gw->orig,
  1009. primary_if->net_dev->dev_addr))
  1010. continue;
  1011. backbone_gw->lasttime = jiffies;
  1012. batadv_bla_send_announce(bat_priv, backbone_gw);
  1013. /* request_sent is only set after creation to avoid
  1014. * problems when we are not yet known as backbone gw
  1015. * in the backbone.
  1016. *
  1017. * We can reset this now after we waited some periods
  1018. * to give bridge forward delays and bla group forming
  1019. * some grace time.
  1020. */
  1021. if (atomic_read(&backbone_gw->request_sent) == 0)
  1022. continue;
  1023. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  1024. continue;
  1025. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  1026. atomic_set(&backbone_gw->request_sent, 0);
  1027. }
  1028. rcu_read_unlock();
  1029. }
  1030. out:
  1031. if (primary_if)
  1032. batadv_hardif_free_ref(primary_if);
  1033. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1034. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1035. }
  1036. /* The hash for claim and backbone hash receive the same key because they
  1037. * are getting initialized by hash_new with the same key. Reinitializing
  1038. * them with to different keys to allow nested locking without generating
  1039. * lockdep warnings
  1040. */
  1041. static struct lock_class_key batadv_claim_hash_lock_class_key;
  1042. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  1043. /* initialize all bla structures */
  1044. int batadv_bla_init(struct batadv_priv *bat_priv)
  1045. {
  1046. int i;
  1047. uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1048. struct batadv_hard_iface *primary_if;
  1049. uint16_t crc;
  1050. unsigned long entrytime;
  1051. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1052. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1053. /* setting claim destination address */
  1054. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1055. bat_priv->bla.claim_dest.type = 0;
  1056. primary_if = batadv_primary_if_get_selected(bat_priv);
  1057. if (primary_if) {
  1058. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1059. bat_priv->bla.claim_dest.group = htons(crc);
  1060. batadv_hardif_free_ref(primary_if);
  1061. } else {
  1062. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1063. }
  1064. /* initialize the duplicate list */
  1065. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1066. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1067. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1068. bat_priv->bla.bcast_duplist_curr = 0;
  1069. if (bat_priv->bla.claim_hash)
  1070. return 0;
  1071. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1072. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1073. if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
  1074. return -ENOMEM;
  1075. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1076. &batadv_claim_hash_lock_class_key);
  1077. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1078. &batadv_backbone_hash_lock_class_key);
  1079. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1080. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  1081. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1082. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1083. return 0;
  1084. }
  1085. /**
  1086. * batadv_bla_check_bcast_duplist
  1087. * @bat_priv: the bat priv with all the soft interface information
  1088. * @skb: contains the bcast_packet to be checked
  1089. *
  1090. * check if it is on our broadcast list. Another gateway might
  1091. * have sent the same packet because it is connected to the same backbone,
  1092. * so we have to remove this duplicate.
  1093. *
  1094. * This is performed by checking the CRC, which will tell us
  1095. * with a good chance that it is the same packet. If it is furthermore
  1096. * sent by another host, drop it. We allow equal packets from
  1097. * the same host however as this might be intended.
  1098. */
  1099. int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
  1100. struct sk_buff *skb)
  1101. {
  1102. int i, curr, ret = 0;
  1103. __be32 crc;
  1104. struct batadv_bcast_packet *bcast_packet;
  1105. struct batadv_bcast_duplist_entry *entry;
  1106. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1107. /* calculate the crc ... */
  1108. crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
  1109. spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
  1110. for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
  1111. curr = (bat_priv->bla.bcast_duplist_curr + i);
  1112. curr %= BATADV_DUPLIST_SIZE;
  1113. entry = &bat_priv->bla.bcast_duplist[curr];
  1114. /* we can stop searching if the entry is too old ;
  1115. * later entries will be even older
  1116. */
  1117. if (batadv_has_timed_out(entry->entrytime,
  1118. BATADV_DUPLIST_TIMEOUT))
  1119. break;
  1120. if (entry->crc != crc)
  1121. continue;
  1122. if (batadv_compare_eth(entry->orig, bcast_packet->orig))
  1123. continue;
  1124. /* this entry seems to match: same crc, not too old,
  1125. * and from another gw. therefore return 1 to forbid it.
  1126. */
  1127. ret = 1;
  1128. goto out;
  1129. }
  1130. /* not found, add a new entry (overwrite the oldest entry)
  1131. * and allow it, its the first occurrence.
  1132. */
  1133. curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
  1134. curr %= BATADV_DUPLIST_SIZE;
  1135. entry = &bat_priv->bla.bcast_duplist[curr];
  1136. entry->crc = crc;
  1137. entry->entrytime = jiffies;
  1138. ether_addr_copy(entry->orig, bcast_packet->orig);
  1139. bat_priv->bla.bcast_duplist_curr = curr;
  1140. out:
  1141. spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
  1142. return ret;
  1143. }
  1144. /**
  1145. * batadv_bla_is_backbone_gw_orig
  1146. * @bat_priv: the bat priv with all the soft interface information
  1147. * @orig: originator mac address
  1148. * @vid: VLAN identifier
  1149. *
  1150. * Check if the originator is a gateway for the VLAN identified by vid.
  1151. *
  1152. * Returns true if orig is a backbone for this vid, false otherwise.
  1153. */
  1154. bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
  1155. unsigned short vid)
  1156. {
  1157. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1158. struct hlist_head *head;
  1159. struct batadv_bla_backbone_gw *backbone_gw;
  1160. int i;
  1161. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1162. return false;
  1163. if (!hash)
  1164. return false;
  1165. for (i = 0; i < hash->size; i++) {
  1166. head = &hash->table[i];
  1167. rcu_read_lock();
  1168. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1169. if (batadv_compare_eth(backbone_gw->orig, orig) &&
  1170. backbone_gw->vid == vid) {
  1171. rcu_read_unlock();
  1172. return true;
  1173. }
  1174. }
  1175. rcu_read_unlock();
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * batadv_bla_is_backbone_gw
  1181. * @skb: the frame to be checked
  1182. * @orig_node: the orig_node of the frame
  1183. * @hdr_size: maximum length of the frame
  1184. *
  1185. * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
  1186. * if the orig_node is also a gateway on the soft interface, otherwise it
  1187. * returns 0.
  1188. */
  1189. int batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1190. struct batadv_orig_node *orig_node, int hdr_size)
  1191. {
  1192. struct batadv_bla_backbone_gw *backbone_gw;
  1193. unsigned short vid;
  1194. if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
  1195. return 0;
  1196. /* first, find out the vid. */
  1197. if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
  1198. return 0;
  1199. vid = batadv_get_vid(skb, hdr_size);
  1200. /* see if this originator is a backbone gw for this VLAN */
  1201. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1202. orig_node->orig, vid);
  1203. if (!backbone_gw)
  1204. return 0;
  1205. batadv_backbone_gw_free_ref(backbone_gw);
  1206. return 1;
  1207. }
  1208. /* free all bla structures (for softinterface free or module unload) */
  1209. void batadv_bla_free(struct batadv_priv *bat_priv)
  1210. {
  1211. struct batadv_hard_iface *primary_if;
  1212. cancel_delayed_work_sync(&bat_priv->bla.work);
  1213. primary_if = batadv_primary_if_get_selected(bat_priv);
  1214. if (bat_priv->bla.claim_hash) {
  1215. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1216. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1217. bat_priv->bla.claim_hash = NULL;
  1218. }
  1219. if (bat_priv->bla.backbone_hash) {
  1220. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1221. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1222. bat_priv->bla.backbone_hash = NULL;
  1223. }
  1224. if (primary_if)
  1225. batadv_hardif_free_ref(primary_if);
  1226. }
  1227. /**
  1228. * batadv_bla_rx
  1229. * @bat_priv: the bat priv with all the soft interface information
  1230. * @skb: the frame to be checked
  1231. * @vid: the VLAN ID of the frame
  1232. * @is_bcast: the packet came in a broadcast packet type.
  1233. *
  1234. * bla_rx avoidance checks if:
  1235. * * we have to race for a claim
  1236. * * if the frame is allowed on the LAN
  1237. *
  1238. * in these cases, the skb is further handled by this function and
  1239. * returns 1, otherwise it returns 0 and the caller shall further
  1240. * process the skb.
  1241. */
  1242. int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1243. unsigned short vid, bool is_bcast)
  1244. {
  1245. struct ethhdr *ethhdr;
  1246. struct batadv_bla_claim search_claim, *claim = NULL;
  1247. struct batadv_hard_iface *primary_if;
  1248. int ret;
  1249. ethhdr = eth_hdr(skb);
  1250. primary_if = batadv_primary_if_get_selected(bat_priv);
  1251. if (!primary_if)
  1252. goto handled;
  1253. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1254. goto allow;
  1255. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1256. /* don't allow broadcasts while requests are in flight */
  1257. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
  1258. goto handled;
  1259. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1260. search_claim.vid = vid;
  1261. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1262. if (!claim) {
  1263. /* possible optimization: race for a claim */
  1264. /* No claim exists yet, claim it for us!
  1265. */
  1266. batadv_handle_claim(bat_priv, primary_if,
  1267. primary_if->net_dev->dev_addr,
  1268. ethhdr->h_source, vid);
  1269. goto allow;
  1270. }
  1271. /* if it is our own claim ... */
  1272. if (batadv_compare_eth(claim->backbone_gw->orig,
  1273. primary_if->net_dev->dev_addr)) {
  1274. /* ... allow it in any case */
  1275. claim->lasttime = jiffies;
  1276. goto allow;
  1277. }
  1278. /* if it is a broadcast ... */
  1279. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
  1280. /* ... drop it. the responsible gateway is in charge.
  1281. *
  1282. * We need to check is_bcast because with the gateway
  1283. * feature, broadcasts (like DHCP requests) may be sent
  1284. * using a unicast packet type.
  1285. */
  1286. goto handled;
  1287. } else {
  1288. /* seems the client considers us as its best gateway.
  1289. * send a claim and update the claim table
  1290. * immediately.
  1291. */
  1292. batadv_handle_claim(bat_priv, primary_if,
  1293. primary_if->net_dev->dev_addr,
  1294. ethhdr->h_source, vid);
  1295. goto allow;
  1296. }
  1297. allow:
  1298. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1299. ret = 0;
  1300. goto out;
  1301. handled:
  1302. kfree_skb(skb);
  1303. ret = 1;
  1304. out:
  1305. if (primary_if)
  1306. batadv_hardif_free_ref(primary_if);
  1307. if (claim)
  1308. batadv_claim_free_ref(claim);
  1309. return ret;
  1310. }
  1311. /**
  1312. * batadv_bla_tx
  1313. * @bat_priv: the bat priv with all the soft interface information
  1314. * @skb: the frame to be checked
  1315. * @vid: the VLAN ID of the frame
  1316. *
  1317. * bla_tx checks if:
  1318. * * a claim was received which has to be processed
  1319. * * the frame is allowed on the mesh
  1320. *
  1321. * in these cases, the skb is further handled by this function and
  1322. * returns 1, otherwise it returns 0 and the caller shall further
  1323. * process the skb.
  1324. *
  1325. * This call might reallocate skb data.
  1326. */
  1327. int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1328. unsigned short vid)
  1329. {
  1330. struct ethhdr *ethhdr;
  1331. struct batadv_bla_claim search_claim, *claim = NULL;
  1332. struct batadv_hard_iface *primary_if;
  1333. int ret = 0;
  1334. primary_if = batadv_primary_if_get_selected(bat_priv);
  1335. if (!primary_if)
  1336. goto out;
  1337. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1338. goto allow;
  1339. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1340. goto handled;
  1341. ethhdr = eth_hdr(skb);
  1342. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1343. /* don't allow broadcasts while requests are in flight */
  1344. if (is_multicast_ether_addr(ethhdr->h_dest))
  1345. goto handled;
  1346. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1347. search_claim.vid = vid;
  1348. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1349. /* if no claim exists, allow it. */
  1350. if (!claim)
  1351. goto allow;
  1352. /* check if we are responsible. */
  1353. if (batadv_compare_eth(claim->backbone_gw->orig,
  1354. primary_if->net_dev->dev_addr)) {
  1355. /* if yes, the client has roamed and we have
  1356. * to unclaim it.
  1357. */
  1358. batadv_handle_unclaim(bat_priv, primary_if,
  1359. primary_if->net_dev->dev_addr,
  1360. ethhdr->h_source, vid);
  1361. goto allow;
  1362. }
  1363. /* check if it is a multicast/broadcast frame */
  1364. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1365. /* drop it. the responsible gateway has forwarded it into
  1366. * the backbone network.
  1367. */
  1368. goto handled;
  1369. } else {
  1370. /* we must allow it. at least if we are
  1371. * responsible for the DESTINATION.
  1372. */
  1373. goto allow;
  1374. }
  1375. allow:
  1376. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1377. ret = 0;
  1378. goto out;
  1379. handled:
  1380. ret = 1;
  1381. out:
  1382. if (primary_if)
  1383. batadv_hardif_free_ref(primary_if);
  1384. if (claim)
  1385. batadv_claim_free_ref(claim);
  1386. return ret;
  1387. }
  1388. int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
  1389. {
  1390. struct net_device *net_dev = (struct net_device *)seq->private;
  1391. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1392. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  1393. struct batadv_bla_claim *claim;
  1394. struct batadv_hard_iface *primary_if;
  1395. struct hlist_head *head;
  1396. uint32_t i;
  1397. bool is_own;
  1398. uint8_t *primary_addr;
  1399. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1400. if (!primary_if)
  1401. goto out;
  1402. primary_addr = primary_if->net_dev->dev_addr;
  1403. seq_printf(seq,
  1404. "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1405. net_dev->name, primary_addr,
  1406. ntohs(bat_priv->bla.claim_dest.group));
  1407. seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
  1408. "Client", "VID", "Originator", "CRC");
  1409. for (i = 0; i < hash->size; i++) {
  1410. head = &hash->table[i];
  1411. rcu_read_lock();
  1412. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1413. is_own = batadv_compare_eth(claim->backbone_gw->orig,
  1414. primary_addr);
  1415. seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
  1416. claim->addr, BATADV_PRINT_VID(claim->vid),
  1417. claim->backbone_gw->orig,
  1418. (is_own ? 'x' : ' '),
  1419. claim->backbone_gw->crc);
  1420. }
  1421. rcu_read_unlock();
  1422. }
  1423. out:
  1424. if (primary_if)
  1425. batadv_hardif_free_ref(primary_if);
  1426. return 0;
  1427. }
  1428. int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
  1429. {
  1430. struct net_device *net_dev = (struct net_device *)seq->private;
  1431. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1432. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1433. struct batadv_bla_backbone_gw *backbone_gw;
  1434. struct batadv_hard_iface *primary_if;
  1435. struct hlist_head *head;
  1436. int secs, msecs;
  1437. uint32_t i;
  1438. bool is_own;
  1439. uint8_t *primary_addr;
  1440. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1441. if (!primary_if)
  1442. goto out;
  1443. primary_addr = primary_if->net_dev->dev_addr;
  1444. seq_printf(seq,
  1445. "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
  1446. net_dev->name, primary_addr,
  1447. ntohs(bat_priv->bla.claim_dest.group));
  1448. seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
  1449. "Originator", "VID", "last seen", "CRC");
  1450. for (i = 0; i < hash->size; i++) {
  1451. head = &hash->table[i];
  1452. rcu_read_lock();
  1453. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1454. msecs = jiffies_to_msecs(jiffies -
  1455. backbone_gw->lasttime);
  1456. secs = msecs / 1000;
  1457. msecs = msecs % 1000;
  1458. is_own = batadv_compare_eth(backbone_gw->orig,
  1459. primary_addr);
  1460. if (is_own)
  1461. continue;
  1462. seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
  1463. backbone_gw->orig,
  1464. BATADV_PRINT_VID(backbone_gw->vid), secs,
  1465. msecs, backbone_gw->crc);
  1466. }
  1467. rcu_read_unlock();
  1468. }
  1469. out:
  1470. if (primary_if)
  1471. batadv_hardif_free_ref(primary_if);
  1472. return 0;
  1473. }