bridge_loop_avoidance.c 48 KB

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