bridge_loop_avoidance.c 46 KB

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