bridge_loop_avoidance.c 46 KB

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