bridge_loop_avoidance.c 45 KB

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