bridge_loop_avoidance.c 46 KB

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