bridge_loop_avoidance.c 40 KB

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