bridge_loop_avoidance.c 46 KB

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