bridge_loop_avoidance.c 42 KB

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