bridge_loop_avoidance.c 41 KB

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