bridge_loop_avoidance.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /* Copyright (C) 2011-2012 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 batadv_bla_send_announce(struct batadv_priv *bat_priv,
  34. struct batadv_backbone_gw *backbone_gw);
  35. /* return the index of the claim */
  36. static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
  37. {
  38. struct batadv_claim *claim = (struct batadv_claim *)data;
  39. uint32_t hash = 0;
  40. hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
  41. hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
  42. hash += (hash << 3);
  43. hash ^= (hash >> 11);
  44. hash += (hash << 15);
  45. return hash % size;
  46. }
  47. /* return the index of the backbone gateway */
  48. static inline uint32_t batadv_choose_backbone_gw(const void *data,
  49. uint32_t size)
  50. {
  51. struct batadv_claim *claim = (struct batadv_claim *)data;
  52. uint32_t hash = 0;
  53. hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
  54. hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
  55. hash += (hash << 3);
  56. hash ^= (hash >> 11);
  57. hash += (hash << 15);
  58. return hash % size;
  59. }
  60. /* compares address and vid of two backbone gws */
  61. static int batadv_compare_backbone_gw(const struct hlist_node *node,
  62. const void *data2)
  63. {
  64. const void *data1 = container_of(node, struct batadv_backbone_gw,
  65. hash_entry);
  66. const struct batadv_backbone_gw *gw1 = data1, *gw2 = data2;
  67. if (!batadv_compare_eth(gw1->orig, gw2->orig))
  68. return 0;
  69. if (gw1->vid != gw2->vid)
  70. return 0;
  71. return 1;
  72. }
  73. /* compares address and vid of two claims */
  74. static int batadv_compare_claim(const struct hlist_node *node,
  75. const void *data2)
  76. {
  77. const void *data1 = container_of(node, struct batadv_claim,
  78. hash_entry);
  79. const struct batadv_claim *cl1 = data1, *cl2 = data2;
  80. if (!batadv_compare_eth(cl1->addr, cl2->addr))
  81. return 0;
  82. if (cl1->vid != cl2->vid)
  83. return 0;
  84. return 1;
  85. }
  86. /* free a backbone gw */
  87. static void batadv_backbone_gw_free_ref(struct batadv_backbone_gw *backbone_gw)
  88. {
  89. if (atomic_dec_and_test(&backbone_gw->refcount))
  90. kfree_rcu(backbone_gw, rcu);
  91. }
  92. /* finally deinitialize the claim */
  93. static void batadv_claim_free_rcu(struct rcu_head *rcu)
  94. {
  95. struct batadv_claim *claim;
  96. claim = container_of(rcu, struct batadv_claim, rcu);
  97. batadv_backbone_gw_free_ref(claim->backbone_gw);
  98. kfree(claim);
  99. }
  100. /* free a claim, call claim_free_rcu if its the last reference */
  101. static void batadv_claim_free_ref(struct batadv_claim *claim)
  102. {
  103. if (atomic_dec_and_test(&claim->refcount))
  104. call_rcu(&claim->rcu, batadv_claim_free_rcu);
  105. }
  106. /* @bat_priv: the bat priv with all the soft interface information
  107. * @data: search data (may be local/static data)
  108. *
  109. * looks for a claim in the hash, and returns it if found
  110. * or NULL otherwise.
  111. */
  112. static struct batadv_claim *batadv_claim_hash_find(struct batadv_priv *bat_priv,
  113. struct batadv_claim *data)
  114. {
  115. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  116. struct hlist_head *head;
  117. struct hlist_node *node;
  118. struct batadv_claim *claim;
  119. struct batadv_claim *claim_tmp = NULL;
  120. int index;
  121. if (!hash)
  122. return NULL;
  123. index = batadv_choose_claim(data, hash->size);
  124. head = &hash->table[index];
  125. rcu_read_lock();
  126. hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
  127. if (!batadv_compare_claim(&claim->hash_entry, data))
  128. continue;
  129. if (!atomic_inc_not_zero(&claim->refcount))
  130. continue;
  131. claim_tmp = claim;
  132. break;
  133. }
  134. rcu_read_unlock();
  135. return claim_tmp;
  136. }
  137. /**
  138. * batadv_backbone_hash_find - looks for a claim in the hash
  139. * @bat_priv: the bat priv with all the soft interface information
  140. * @addr: the address of the originator
  141. * @vid: the VLAN ID
  142. *
  143. * Returns claim if found or NULL otherwise.
  144. */
  145. static struct batadv_backbone_gw *
  146. batadv_backbone_hash_find(struct batadv_priv *bat_priv,
  147. uint8_t *addr, short vid)
  148. {
  149. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  150. struct hlist_head *head;
  151. struct hlist_node *node;
  152. struct batadv_backbone_gw search_entry, *backbone_gw;
  153. struct batadv_backbone_gw *backbone_gw_tmp = NULL;
  154. int index;
  155. if (!hash)
  156. return NULL;
  157. memcpy(search_entry.orig, addr, ETH_ALEN);
  158. search_entry.vid = vid;
  159. index = batadv_choose_backbone_gw(&search_entry, hash->size);
  160. head = &hash->table[index];
  161. rcu_read_lock();
  162. hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
  163. if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
  164. &search_entry))
  165. continue;
  166. if (!atomic_inc_not_zero(&backbone_gw->refcount))
  167. continue;
  168. backbone_gw_tmp = backbone_gw;
  169. break;
  170. }
  171. rcu_read_unlock();
  172. return backbone_gw_tmp;
  173. }
  174. /* delete all claims for a backbone */
  175. static void
  176. batadv_bla_del_backbone_claims(struct batadv_backbone_gw *backbone_gw)
  177. {
  178. struct batadv_hashtable *hash;
  179. struct hlist_node *node, *node_tmp;
  180. struct hlist_head *head;
  181. struct batadv_claim *claim;
  182. int i;
  183. spinlock_t *list_lock; /* protects write access to the hash lists */
  184. hash = backbone_gw->bat_priv->bla.claim_hash;
  185. if (!hash)
  186. return;
  187. for (i = 0; i < hash->size; i++) {
  188. head = &hash->table[i];
  189. list_lock = &hash->list_locks[i];
  190. spin_lock_bh(list_lock);
  191. hlist_for_each_entry_safe(claim, node, node_tmp,
  192. head, hash_entry) {
  193. if (claim->backbone_gw != backbone_gw)
  194. continue;
  195. batadv_claim_free_ref(claim);
  196. hlist_del_rcu(node);
  197. }
  198. spin_unlock_bh(list_lock);
  199. }
  200. /* all claims gone, intialize CRC */
  201. backbone_gw->crc = BATADV_BLA_CRC_INIT;
  202. }
  203. /**
  204. * batadv_bla_send_claim - sends a claim frame according to the provided info
  205. * @bat_priv: the bat priv with all the soft interface information
  206. * @orig: the mac address to be announced within the claim
  207. * @vid: the VLAN ID
  208. * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
  209. */
  210. static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
  211. short vid, int claimtype)
  212. {
  213. struct sk_buff *skb;
  214. struct ethhdr *ethhdr;
  215. struct batadv_hard_iface *primary_if;
  216. struct net_device *soft_iface;
  217. uint8_t *hw_src;
  218. struct batadv_bla_claim_dst local_claim_dest;
  219. __be32 zeroip = 0;
  220. primary_if = batadv_primary_if_get_selected(bat_priv);
  221. if (!primary_if)
  222. return;
  223. memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
  224. sizeof(local_claim_dest));
  225. local_claim_dest.type = claimtype;
  226. soft_iface = primary_if->soft_iface;
  227. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  228. /* IP DST: 0.0.0.0 */
  229. zeroip,
  230. primary_if->soft_iface,
  231. /* IP SRC: 0.0.0.0 */
  232. zeroip,
  233. /* Ethernet DST: Broadcast */
  234. NULL,
  235. /* Ethernet SRC/HW SRC: originator mac */
  236. primary_if->net_dev->dev_addr,
  237. /* HW DST: FF:43:05:XX:YY:YY
  238. * with XX = claim type
  239. * and YY:YY = group id
  240. */
  241. (uint8_t *)&local_claim_dest);
  242. if (!skb)
  243. goto out;
  244. ethhdr = (struct ethhdr *)skb->data;
  245. hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
  246. /* now we pretend that the client would have sent this ... */
  247. switch (claimtype) {
  248. case BATADV_CLAIM_TYPE_CLAIM:
  249. /* normal claim frame
  250. * set Ethernet SRC to the clients mac
  251. */
  252. memcpy(ethhdr->h_source, mac, ETH_ALEN);
  253. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  254. "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
  255. break;
  256. case BATADV_CLAIM_TYPE_UNCLAIM:
  257. /* unclaim frame
  258. * set HW SRC to the clients mac
  259. */
  260. memcpy(hw_src, mac, ETH_ALEN);
  261. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  262. "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
  263. vid);
  264. break;
  265. case BATADV_CLAIM_TYPE_ANNOUNCE:
  266. /* announcement frame
  267. * set HW SRC to the special mac containg the crc
  268. */
  269. memcpy(hw_src, mac, ETH_ALEN);
  270. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  271. "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
  272. ethhdr->h_source, vid);
  273. break;
  274. case BATADV_CLAIM_TYPE_REQUEST:
  275. /* request frame
  276. * set HW SRC and header destination to the receiving backbone
  277. * gws mac
  278. */
  279. memcpy(hw_src, mac, ETH_ALEN);
  280. memcpy(ethhdr->h_dest, mac, ETH_ALEN);
  281. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  282. "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
  283. ethhdr->h_source, ethhdr->h_dest, vid);
  284. break;
  285. }
  286. if (vid != -1)
  287. skb = vlan_insert_tag(skb, vid);
  288. skb_reset_mac_header(skb);
  289. skb->protocol = eth_type_trans(skb, soft_iface);
  290. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  291. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  292. skb->len + ETH_HLEN);
  293. soft_iface->last_rx = jiffies;
  294. netif_rx(skb);
  295. out:
  296. if (primary_if)
  297. batadv_hardif_free_ref(primary_if);
  298. }
  299. /**
  300. * batadv_bla_get_backbone_gw
  301. * @bat_priv: the bat priv with all the soft interface information
  302. * @orig: the mac address of the originator
  303. * @vid: the VLAN ID
  304. *
  305. * searches for the backbone gw or creates a new one if it could not
  306. * be found.
  307. */
  308. static struct batadv_backbone_gw *
  309. batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
  310. short vid, bool own_backbone)
  311. {
  312. struct batadv_backbone_gw *entry;
  313. struct batadv_orig_node *orig_node;
  314. int hash_added;
  315. entry = batadv_backbone_hash_find(bat_priv, orig, vid);
  316. if (entry)
  317. return entry;
  318. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  319. "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
  320. orig, vid);
  321. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  322. if (!entry)
  323. return NULL;
  324. entry->vid = vid;
  325. entry->lasttime = jiffies;
  326. entry->crc = BATADV_BLA_CRC_INIT;
  327. entry->bat_priv = bat_priv;
  328. atomic_set(&entry->request_sent, 0);
  329. atomic_set(&entry->wait_periods, 0);
  330. memcpy(entry->orig, orig, ETH_ALEN);
  331. /* one for the hash, one for returning */
  332. atomic_set(&entry->refcount, 2);
  333. hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
  334. batadv_compare_backbone_gw,
  335. batadv_choose_backbone_gw, entry,
  336. &entry->hash_entry);
  337. if (unlikely(hash_added != 0)) {
  338. /* hash failed, free the structure */
  339. kfree(entry);
  340. return NULL;
  341. }
  342. /* this is a gateway now, remove any tt entries */
  343. orig_node = batadv_orig_hash_find(bat_priv, orig);
  344. if (orig_node) {
  345. batadv_tt_global_del_orig(bat_priv, orig_node,
  346. "became a backbone gateway");
  347. batadv_orig_node_free_ref(orig_node);
  348. }
  349. if (own_backbone) {
  350. batadv_bla_send_announce(bat_priv, entry);
  351. /* this will be decreased in the worker thread */
  352. atomic_inc(&entry->request_sent);
  353. atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
  354. atomic_inc(&bat_priv->bla.num_requests);
  355. }
  356. return entry;
  357. }
  358. /* update or add the own backbone gw to make sure we announce
  359. * where we receive other backbone gws
  360. */
  361. static void
  362. batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
  363. struct batadv_hard_iface *primary_if,
  364. short vid)
  365. {
  366. struct batadv_backbone_gw *backbone_gw;
  367. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  368. primary_if->net_dev->dev_addr,
  369. vid, true);
  370. if (unlikely(!backbone_gw))
  371. return;
  372. backbone_gw->lasttime = jiffies;
  373. batadv_backbone_gw_free_ref(backbone_gw);
  374. }
  375. /* @bat_priv: the bat priv with all the soft interface information
  376. * @vid: the vid where the request came on
  377. *
  378. * Repeat all of our own claims, and finally send an ANNOUNCE frame
  379. * to allow the requester another check if the CRC is correct now.
  380. */
  381. static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
  382. struct batadv_hard_iface *primary_if,
  383. short vid)
  384. {
  385. struct hlist_node *node;
  386. struct hlist_head *head;
  387. struct batadv_hashtable *hash;
  388. struct batadv_claim *claim;
  389. struct batadv_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, node, 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_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_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_backbone_gw *backbone_gw)
  463. {
  464. struct batadv_claim *claim;
  465. struct batadv_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_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_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 = %04x\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 = %04x, sent = %04x)\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_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_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: %04x\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_backbone_gw *backbone_gw;
  799. struct hlist_node *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, 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(node);
  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_claim *claim;
  846. struct hlist_node *node;
  847. struct hlist_head *head;
  848. struct batadv_hashtable *hash;
  849. int i;
  850. hash = bat_priv->bla.claim_hash;
  851. if (!hash)
  852. return;
  853. for (i = 0; i < hash->size; i++) {
  854. head = &hash->table[i];
  855. rcu_read_lock();
  856. hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
  857. if (now)
  858. goto purge_now;
  859. if (!batadv_compare_eth(claim->backbone_gw->orig,
  860. primary_if->net_dev->dev_addr))
  861. continue;
  862. if (!batadv_has_timed_out(claim->lasttime,
  863. BATADV_BLA_CLAIM_TIMEOUT))
  864. continue;
  865. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  866. "bla_purge_claims(): %pM, vid %d, time out\n",
  867. claim->addr, claim->vid);
  868. purge_now:
  869. batadv_handle_unclaim(bat_priv, primary_if,
  870. claim->backbone_gw->orig,
  871. claim->addr, claim->vid);
  872. }
  873. rcu_read_unlock();
  874. }
  875. }
  876. /**
  877. * batadv_bla_update_orig_address
  878. * @bat_priv: the bat priv with all the soft interface information
  879. * @primary_if: the new selected primary_if
  880. * @oldif: the old primary interface, may be NULL
  881. *
  882. * Update the backbone gateways when the own orig address changes.
  883. */
  884. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  885. struct batadv_hard_iface *primary_if,
  886. struct batadv_hard_iface *oldif)
  887. {
  888. struct batadv_backbone_gw *backbone_gw;
  889. struct hlist_node *node;
  890. struct hlist_head *head;
  891. struct batadv_hashtable *hash;
  892. __be16 group;
  893. int i;
  894. /* reset bridge loop avoidance group id */
  895. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  896. bat_priv->bla.claim_dest.group = group;
  897. if (!oldif) {
  898. batadv_bla_purge_claims(bat_priv, NULL, 1);
  899. batadv_bla_purge_backbone_gw(bat_priv, 1);
  900. return;
  901. }
  902. hash = bat_priv->bla.backbone_hash;
  903. if (!hash)
  904. return;
  905. for (i = 0; i < hash->size; i++) {
  906. head = &hash->table[i];
  907. rcu_read_lock();
  908. hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
  909. /* own orig still holds the old value. */
  910. if (!batadv_compare_eth(backbone_gw->orig,
  911. oldif->net_dev->dev_addr))
  912. continue;
  913. memcpy(backbone_gw->orig,
  914. primary_if->net_dev->dev_addr, ETH_ALEN);
  915. /* send an announce frame so others will ask for our
  916. * claims and update their tables.
  917. */
  918. batadv_bla_send_announce(bat_priv, backbone_gw);
  919. }
  920. rcu_read_unlock();
  921. }
  922. }
  923. /* (re)start the timer */
  924. static void batadv_bla_start_timer(struct batadv_priv *bat_priv)
  925. {
  926. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  927. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  928. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  929. }
  930. /* periodic work to do:
  931. * * purge structures when they are too old
  932. * * send announcements
  933. */
  934. static void batadv_bla_periodic_work(struct work_struct *work)
  935. {
  936. struct delayed_work *delayed_work;
  937. struct batadv_priv *bat_priv;
  938. struct batadv_priv_bla *priv_bla;
  939. struct hlist_node *node;
  940. struct hlist_head *head;
  941. struct batadv_backbone_gw *backbone_gw;
  942. struct batadv_hashtable *hash;
  943. struct batadv_hard_iface *primary_if;
  944. int i;
  945. delayed_work = container_of(work, struct delayed_work, work);
  946. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  947. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  948. primary_if = batadv_primary_if_get_selected(bat_priv);
  949. if (!primary_if)
  950. goto out;
  951. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  952. batadv_bla_purge_backbone_gw(bat_priv, 0);
  953. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  954. goto out;
  955. hash = bat_priv->bla.backbone_hash;
  956. if (!hash)
  957. goto out;
  958. for (i = 0; i < hash->size; i++) {
  959. head = &hash->table[i];
  960. rcu_read_lock();
  961. hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
  962. if (!batadv_compare_eth(backbone_gw->orig,
  963. primary_if->net_dev->dev_addr))
  964. continue;
  965. backbone_gw->lasttime = jiffies;
  966. batadv_bla_send_announce(bat_priv, backbone_gw);
  967. /* request_sent is only set after creation to avoid
  968. * problems when we are not yet known as backbone gw
  969. * in the backbone.
  970. *
  971. * We can reset this now after we waited some periods
  972. * to give bridge forward delays and bla group forming
  973. * some grace time.
  974. */
  975. if (atomic_read(&backbone_gw->request_sent) == 0)
  976. continue;
  977. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  978. continue;
  979. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  980. atomic_set(&backbone_gw->request_sent, 0);
  981. }
  982. rcu_read_unlock();
  983. }
  984. out:
  985. if (primary_if)
  986. batadv_hardif_free_ref(primary_if);
  987. batadv_bla_start_timer(bat_priv);
  988. }
  989. /* The hash for claim and backbone hash receive the same key because they
  990. * are getting initialized by hash_new with the same key. Reinitializing
  991. * them with to different keys to allow nested locking without generating
  992. * lockdep warnings
  993. */
  994. static struct lock_class_key batadv_claim_hash_lock_class_key;
  995. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  996. /* initialize all bla structures */
  997. int batadv_bla_init(struct batadv_priv *bat_priv)
  998. {
  999. int i;
  1000. uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1001. struct batadv_hard_iface *primary_if;
  1002. uint16_t crc;
  1003. unsigned long entrytime;
  1004. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1005. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1006. /* setting claim destination address */
  1007. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1008. bat_priv->bla.claim_dest.type = 0;
  1009. primary_if = batadv_primary_if_get_selected(bat_priv);
  1010. if (primary_if) {
  1011. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1012. bat_priv->bla.claim_dest.group = htons(crc);
  1013. batadv_hardif_free_ref(primary_if);
  1014. } else {
  1015. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1016. }
  1017. /* initialize the duplicate list */
  1018. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1019. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1020. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1021. bat_priv->bla.bcast_duplist_curr = 0;
  1022. if (bat_priv->bla.claim_hash)
  1023. return 0;
  1024. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1025. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1026. if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
  1027. return -ENOMEM;
  1028. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1029. &batadv_claim_hash_lock_class_key);
  1030. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1031. &batadv_backbone_hash_lock_class_key);
  1032. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1033. batadv_bla_start_timer(bat_priv);
  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. *
  1098. * check if the originator is a gateway for any VLAN ID.
  1099. *
  1100. * returns 1 if it is found, 0 otherwise
  1101. */
  1102. int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
  1103. {
  1104. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1105. struct hlist_head *head;
  1106. struct hlist_node *node;
  1107. struct batadv_backbone_gw *backbone_gw;
  1108. int i;
  1109. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1110. return 0;
  1111. if (!hash)
  1112. return 0;
  1113. for (i = 0; i < hash->size; i++) {
  1114. head = &hash->table[i];
  1115. rcu_read_lock();
  1116. hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
  1117. if (batadv_compare_eth(backbone_gw->orig, orig)) {
  1118. rcu_read_unlock();
  1119. return 1;
  1120. }
  1121. }
  1122. rcu_read_unlock();
  1123. }
  1124. return 0;
  1125. }
  1126. /**
  1127. * batadv_bla_is_backbone_gw
  1128. * @skb: the frame to be checked
  1129. * @orig_node: the orig_node of the frame
  1130. * @hdr_size: maximum length of the frame
  1131. *
  1132. * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
  1133. * if the orig_node is also a gateway on the soft interface, otherwise it
  1134. * returns 0.
  1135. */
  1136. int batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1137. struct batadv_orig_node *orig_node, int hdr_size)
  1138. {
  1139. struct ethhdr *ethhdr;
  1140. struct vlan_ethhdr *vhdr;
  1141. struct batadv_backbone_gw *backbone_gw;
  1142. short vid = -1;
  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. ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
  1149. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  1150. if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
  1151. return 0;
  1152. vhdr = (struct vlan_ethhdr *)(skb->data + hdr_size);
  1153. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  1154. }
  1155. /* see if this originator is a backbone gw for this VLAN */
  1156. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1157. orig_node->orig, vid);
  1158. if (!backbone_gw)
  1159. return 0;
  1160. batadv_backbone_gw_free_ref(backbone_gw);
  1161. return 1;
  1162. }
  1163. /* free all bla structures (for softinterface free or module unload) */
  1164. void batadv_bla_free(struct batadv_priv *bat_priv)
  1165. {
  1166. struct batadv_hard_iface *primary_if;
  1167. cancel_delayed_work_sync(&bat_priv->bla.work);
  1168. primary_if = batadv_primary_if_get_selected(bat_priv);
  1169. if (bat_priv->bla.claim_hash) {
  1170. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1171. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1172. bat_priv->bla.claim_hash = NULL;
  1173. }
  1174. if (bat_priv->bla.backbone_hash) {
  1175. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1176. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1177. bat_priv->bla.backbone_hash = NULL;
  1178. }
  1179. if (primary_if)
  1180. batadv_hardif_free_ref(primary_if);
  1181. }
  1182. /**
  1183. * batadv_bla_rx
  1184. * @bat_priv: the bat priv with all the soft interface information
  1185. * @skb: the frame to be checked
  1186. * @vid: the VLAN ID of the frame
  1187. * @is_bcast: the packet came in a broadcast packet type.
  1188. *
  1189. * bla_rx avoidance checks if:
  1190. * * we have to race for a claim
  1191. * * if the frame is allowed on the LAN
  1192. *
  1193. * in these cases, the skb is further handled by this function and
  1194. * returns 1, otherwise it returns 0 and the caller shall further
  1195. * process the skb.
  1196. */
  1197. int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid,
  1198. bool is_bcast)
  1199. {
  1200. struct ethhdr *ethhdr;
  1201. struct batadv_claim search_claim, *claim = NULL;
  1202. struct batadv_hard_iface *primary_if;
  1203. int ret;
  1204. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1205. primary_if = batadv_primary_if_get_selected(bat_priv);
  1206. if (!primary_if)
  1207. goto handled;
  1208. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1209. goto allow;
  1210. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1211. /* don't allow broadcasts while requests are in flight */
  1212. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
  1213. goto handled;
  1214. memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
  1215. search_claim.vid = vid;
  1216. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1217. if (!claim) {
  1218. /* possible optimization: race for a claim */
  1219. /* No claim exists yet, claim it for us!
  1220. */
  1221. batadv_handle_claim(bat_priv, primary_if,
  1222. primary_if->net_dev->dev_addr,
  1223. ethhdr->h_source, vid);
  1224. goto allow;
  1225. }
  1226. /* if it is our own claim ... */
  1227. if (batadv_compare_eth(claim->backbone_gw->orig,
  1228. primary_if->net_dev->dev_addr)) {
  1229. /* ... allow it in any case */
  1230. claim->lasttime = jiffies;
  1231. goto allow;
  1232. }
  1233. /* if it is a broadcast ... */
  1234. if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
  1235. /* ... drop it. the responsible gateway is in charge.
  1236. *
  1237. * We need to check is_bcast because with the gateway
  1238. * feature, broadcasts (like DHCP requests) may be sent
  1239. * using a unicast packet type.
  1240. */
  1241. goto handled;
  1242. } else {
  1243. /* seems the client considers us as its best gateway.
  1244. * send a claim and update the claim table
  1245. * immediately.
  1246. */
  1247. batadv_handle_claim(bat_priv, primary_if,
  1248. primary_if->net_dev->dev_addr,
  1249. ethhdr->h_source, vid);
  1250. goto allow;
  1251. }
  1252. allow:
  1253. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1254. ret = 0;
  1255. goto out;
  1256. handled:
  1257. kfree_skb(skb);
  1258. ret = 1;
  1259. out:
  1260. if (primary_if)
  1261. batadv_hardif_free_ref(primary_if);
  1262. if (claim)
  1263. batadv_claim_free_ref(claim);
  1264. return ret;
  1265. }
  1266. /**
  1267. * batadv_bla_tx
  1268. * @bat_priv: the bat priv with all the soft interface information
  1269. * @skb: the frame to be checked
  1270. * @vid: the VLAN ID of the frame
  1271. *
  1272. * bla_tx checks if:
  1273. * * a claim was received which has to be processed
  1274. * * the frame is allowed on the mesh
  1275. *
  1276. * in these cases, the skb is further handled by this function and
  1277. * returns 1, otherwise it returns 0 and the caller shall further
  1278. * process the skb.
  1279. */
  1280. int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid)
  1281. {
  1282. struct ethhdr *ethhdr;
  1283. struct batadv_claim search_claim, *claim = NULL;
  1284. struct batadv_hard_iface *primary_if;
  1285. int ret = 0;
  1286. primary_if = batadv_primary_if_get_selected(bat_priv);
  1287. if (!primary_if)
  1288. goto out;
  1289. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1290. goto allow;
  1291. /* in VLAN case, the mac header might not be set. */
  1292. skb_reset_mac_header(skb);
  1293. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1294. goto handled;
  1295. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1296. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1297. /* don't allow broadcasts while requests are in flight */
  1298. if (is_multicast_ether_addr(ethhdr->h_dest))
  1299. goto handled;
  1300. memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
  1301. search_claim.vid = vid;
  1302. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1303. /* if no claim exists, allow it. */
  1304. if (!claim)
  1305. goto allow;
  1306. /* check if we are responsible. */
  1307. if (batadv_compare_eth(claim->backbone_gw->orig,
  1308. primary_if->net_dev->dev_addr)) {
  1309. /* if yes, the client has roamed and we have
  1310. * to unclaim it.
  1311. */
  1312. batadv_handle_unclaim(bat_priv, primary_if,
  1313. primary_if->net_dev->dev_addr,
  1314. ethhdr->h_source, vid);
  1315. goto allow;
  1316. }
  1317. /* check if it is a multicast/broadcast frame */
  1318. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1319. /* drop it. the responsible gateway has forwarded it into
  1320. * the backbone network.
  1321. */
  1322. goto handled;
  1323. } else {
  1324. /* we must allow it. at least if we are
  1325. * responsible for the DESTINATION.
  1326. */
  1327. goto allow;
  1328. }
  1329. allow:
  1330. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1331. ret = 0;
  1332. goto out;
  1333. handled:
  1334. ret = 1;
  1335. out:
  1336. if (primary_if)
  1337. batadv_hardif_free_ref(primary_if);
  1338. if (claim)
  1339. batadv_claim_free_ref(claim);
  1340. return ret;
  1341. }
  1342. int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
  1343. {
  1344. struct net_device *net_dev = (struct net_device *)seq->private;
  1345. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1346. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  1347. struct batadv_claim *claim;
  1348. struct batadv_hard_iface *primary_if;
  1349. struct hlist_node *node;
  1350. struct hlist_head *head;
  1351. uint32_t i;
  1352. bool is_own;
  1353. uint8_t *primary_addr;
  1354. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1355. if (!primary_if)
  1356. goto out;
  1357. primary_addr = primary_if->net_dev->dev_addr;
  1358. seq_printf(seq,
  1359. "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
  1360. net_dev->name, primary_addr,
  1361. ntohs(bat_priv->bla.claim_dest.group));
  1362. seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
  1363. "Client", "VID", "Originator", "CRC");
  1364. for (i = 0; i < hash->size; i++) {
  1365. head = &hash->table[i];
  1366. rcu_read_lock();
  1367. hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
  1368. is_own = batadv_compare_eth(claim->backbone_gw->orig,
  1369. primary_addr);
  1370. seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
  1371. claim->addr, claim->vid,
  1372. claim->backbone_gw->orig,
  1373. (is_own ? 'x' : ' '),
  1374. claim->backbone_gw->crc);
  1375. }
  1376. rcu_read_unlock();
  1377. }
  1378. out:
  1379. if (primary_if)
  1380. batadv_hardif_free_ref(primary_if);
  1381. return 0;
  1382. }
  1383. int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
  1384. {
  1385. struct net_device *net_dev = (struct net_device *)seq->private;
  1386. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1387. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1388. struct batadv_backbone_gw *backbone_gw;
  1389. struct batadv_hard_iface *primary_if;
  1390. struct hlist_node *node;
  1391. struct hlist_head *head;
  1392. int secs, msecs;
  1393. uint32_t i;
  1394. bool is_own;
  1395. uint8_t *primary_addr;
  1396. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1397. if (!primary_if)
  1398. goto out;
  1399. primary_addr = primary_if->net_dev->dev_addr;
  1400. seq_printf(seq,
  1401. "Backbones announced for the mesh %s (orig %pM, group id %04x)\n",
  1402. net_dev->name, primary_addr,
  1403. ntohs(bat_priv->bla.claim_dest.group));
  1404. seq_printf(seq, " %-17s %-5s %-9s (%-4s)\n",
  1405. "Originator", "VID", "last seen", "CRC");
  1406. for (i = 0; i < hash->size; i++) {
  1407. head = &hash->table[i];
  1408. rcu_read_lock();
  1409. hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
  1410. msecs = jiffies_to_msecs(jiffies -
  1411. backbone_gw->lasttime);
  1412. secs = msecs / 1000;
  1413. msecs = msecs % 1000;
  1414. is_own = batadv_compare_eth(backbone_gw->orig,
  1415. primary_addr);
  1416. if (is_own)
  1417. continue;
  1418. seq_printf(seq,
  1419. " * %pM on % 5d % 4i.%03is (%04x)\n",
  1420. backbone_gw->orig, backbone_gw->vid,
  1421. secs, msecs, backbone_gw->crc);
  1422. }
  1423. rcu_read_unlock();
  1424. }
  1425. out:
  1426. if (primary_if)
  1427. batadv_hardif_free_ref(primary_if);
  1428. return 0;
  1429. }