bridge_loop_avoidance.c 47 KB

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