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