bridge_loop_avoidance.c 47 KB

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