bridge_loop_avoidance.c 43 KB

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