bridge_loop_avoidance.c 44 KB

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