bridge_loop_avoidance.c 40 KB

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