bridge_loop_avoidance.c 48 KB

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