bridge_loop_avoidance.c 46 KB

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