bridge_loop_avoidance.c 48 KB

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