bridge_loop_avoidance.c 41 KB

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