bridge_loop_avoidance.c 42 KB

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