bridge_loop_avoidance.c 35 KB

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