bridge_loop_avoidance.c 47 KB

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