bridge_loop_avoidance.c 41 KB

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