bridge_loop_avoidance.c 46 KB

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