ieee80211_rx.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*
  2. * Original code based Host AP (software wireless LAN access point) driver
  3. * for Intersil Prism2/2.5/3 - hostap.o module, common routines
  4. *
  5. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. * <jkmaline@cc.hut.fi>
  7. * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
  8. * Copyright (c) 2004-2005, Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation. See README and COPYING for
  13. * more details.
  14. */
  15. #include <linux/compiler.h>
  16. #include <linux/config.h>
  17. #include <linux/errno.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/in6.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/slab.h>
  28. #include <linux/tcp.h>
  29. #include <linux/types.h>
  30. #include <linux/wireless.h>
  31. #include <linux/etherdevice.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/ctype.h>
  34. #include <net/ieee80211.h>
  35. static void ieee80211_monitor_rx(struct ieee80211_device *ieee,
  36. struct sk_buff *skb,
  37. struct ieee80211_rx_stats *rx_stats)
  38. {
  39. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  40. u16 fc = le16_to_cpu(hdr->frame_ctl);
  41. skb->dev = ieee->dev;
  42. skb->mac.raw = skb->data;
  43. skb_pull(skb, ieee80211_get_hdrlen(fc));
  44. skb->pkt_type = PACKET_OTHERHOST;
  45. skb->protocol = __constant_htons(ETH_P_80211_RAW);
  46. memset(skb->cb, 0, sizeof(skb->cb));
  47. netif_rx(skb);
  48. }
  49. /* Called only as a tasklet (software IRQ) */
  50. static struct ieee80211_frag_entry *ieee80211_frag_cache_find(struct
  51. ieee80211_device
  52. *ieee,
  53. unsigned int seq,
  54. unsigned int frag,
  55. u8 * src,
  56. u8 * dst)
  57. {
  58. struct ieee80211_frag_entry *entry;
  59. int i;
  60. for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
  61. entry = &ieee->frag_cache[i];
  62. if (entry->skb != NULL &&
  63. time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  64. IEEE80211_DEBUG_FRAG("expiring fragment cache entry "
  65. "seq=%u last_frag=%u\n",
  66. entry->seq, entry->last_frag);
  67. dev_kfree_skb_any(entry->skb);
  68. entry->skb = NULL;
  69. }
  70. if (entry->skb != NULL && entry->seq == seq &&
  71. (entry->last_frag + 1 == frag || frag == -1) &&
  72. !compare_ether_addr(entry->src_addr, src) &&
  73. !compare_ether_addr(entry->dst_addr, dst))
  74. return entry;
  75. }
  76. return NULL;
  77. }
  78. /* Called only as a tasklet (software IRQ) */
  79. static struct sk_buff *ieee80211_frag_cache_get(struct ieee80211_device *ieee,
  80. struct ieee80211_hdr_4addr *hdr)
  81. {
  82. struct sk_buff *skb = NULL;
  83. u16 sc;
  84. unsigned int frag, seq;
  85. struct ieee80211_frag_entry *entry;
  86. sc = le16_to_cpu(hdr->seq_ctl);
  87. frag = WLAN_GET_SEQ_FRAG(sc);
  88. seq = WLAN_GET_SEQ_SEQ(sc);
  89. if (frag == 0) {
  90. /* Reserve enough space to fit maximum frame length */
  91. skb = dev_alloc_skb(ieee->dev->mtu +
  92. sizeof(struct ieee80211_hdr_4addr) +
  93. 8 /* LLC */ +
  94. 2 /* alignment */ +
  95. 8 /* WEP */ + ETH_ALEN /* WDS */ );
  96. if (skb == NULL)
  97. return NULL;
  98. entry = &ieee->frag_cache[ieee->frag_next_idx];
  99. ieee->frag_next_idx++;
  100. if (ieee->frag_next_idx >= IEEE80211_FRAG_CACHE_LEN)
  101. ieee->frag_next_idx = 0;
  102. if (entry->skb != NULL)
  103. dev_kfree_skb_any(entry->skb);
  104. entry->first_frag_time = jiffies;
  105. entry->seq = seq;
  106. entry->last_frag = frag;
  107. entry->skb = skb;
  108. memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
  109. memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
  110. } else {
  111. /* received a fragment of a frame for which the head fragment
  112. * should have already been received */
  113. entry = ieee80211_frag_cache_find(ieee, seq, frag, hdr->addr2,
  114. hdr->addr1);
  115. if (entry != NULL) {
  116. entry->last_frag = frag;
  117. skb = entry->skb;
  118. }
  119. }
  120. return skb;
  121. }
  122. /* Called only as a tasklet (software IRQ) */
  123. static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
  124. struct ieee80211_hdr_4addr *hdr)
  125. {
  126. u16 sc;
  127. unsigned int seq;
  128. struct ieee80211_frag_entry *entry;
  129. sc = le16_to_cpu(hdr->seq_ctl);
  130. seq = WLAN_GET_SEQ_SEQ(sc);
  131. entry = ieee80211_frag_cache_find(ieee, seq, -1, hdr->addr2,
  132. hdr->addr1);
  133. if (entry == NULL) {
  134. IEEE80211_DEBUG_FRAG("could not invalidate fragment cache "
  135. "entry (seq=%u)\n", seq);
  136. return -1;
  137. }
  138. entry->skb = NULL;
  139. return 0;
  140. }
  141. #ifdef NOT_YET
  142. /* ieee80211_rx_frame_mgtmt
  143. *
  144. * Responsible for handling management control frames
  145. *
  146. * Called by ieee80211_rx */
  147. static int
  148. ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
  149. struct ieee80211_rx_stats *rx_stats, u16 type,
  150. u16 stype)
  151. {
  152. if (ieee->iw_mode == IW_MODE_MASTER) {
  153. printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
  154. ieee->dev->name);
  155. return 0;
  156. /*
  157. hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr_4addr *)
  158. skb->data);*/
  159. }
  160. if (ieee->hostapd && type == WLAN_FC_TYPE_MGMT) {
  161. if (stype == WLAN_FC_STYPE_BEACON &&
  162. ieee->iw_mode == IW_MODE_MASTER) {
  163. struct sk_buff *skb2;
  164. /* Process beacon frames also in kernel driver to
  165. * update STA(AP) table statistics */
  166. skb2 = skb_clone(skb, GFP_ATOMIC);
  167. if (skb2)
  168. hostap_rx(skb2->dev, skb2, rx_stats);
  169. }
  170. /* send management frames to the user space daemon for
  171. * processing */
  172. ieee->apdevstats.rx_packets++;
  173. ieee->apdevstats.rx_bytes += skb->len;
  174. prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
  175. return 0;
  176. }
  177. if (ieee->iw_mode == IW_MODE_MASTER) {
  178. if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
  179. printk(KERN_DEBUG "%s: unknown management frame "
  180. "(type=0x%02x, stype=0x%02x) dropped\n",
  181. skb->dev->name, type, stype);
  182. return -1;
  183. }
  184. hostap_rx(skb->dev, skb, rx_stats);
  185. return 0;
  186. }
  187. printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
  188. "received in non-Host AP mode\n", skb->dev->name);
  189. return -1;
  190. }
  191. #endif
  192. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  193. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  194. static unsigned char rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  195. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  196. static unsigned char bridge_tunnel_header[] =
  197. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  198. /* No encapsulation header if EtherType < 0x600 (=length) */
  199. /* Called by ieee80211_rx_frame_decrypt */
  200. static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
  201. struct sk_buff *skb)
  202. {
  203. struct net_device *dev = ieee->dev;
  204. u16 fc, ethertype;
  205. struct ieee80211_hdr_3addr *hdr;
  206. u8 *pos;
  207. if (skb->len < 24)
  208. return 0;
  209. hdr = (struct ieee80211_hdr_3addr *)skb->data;
  210. fc = le16_to_cpu(hdr->frame_ctl);
  211. /* check that the frame is unicast frame to us */
  212. if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  213. IEEE80211_FCTL_TODS &&
  214. !compare_ether_addr(hdr->addr1, dev->dev_addr) &&
  215. !compare_ether_addr(hdr->addr3, dev->dev_addr)) {
  216. /* ToDS frame with own addr BSSID and DA */
  217. } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  218. IEEE80211_FCTL_FROMDS &&
  219. !compare_ether_addr(hdr->addr1, dev->dev_addr)) {
  220. /* FromDS frame with own addr as DA */
  221. } else
  222. return 0;
  223. if (skb->len < 24 + 8)
  224. return 0;
  225. /* check for port access entity Ethernet type */
  226. pos = skb->data + 24;
  227. ethertype = (pos[6] << 8) | pos[7];
  228. if (ethertype == ETH_P_PAE)
  229. return 1;
  230. return 0;
  231. }
  232. /* Called only as a tasklet (software IRQ), by ieee80211_rx */
  233. static int
  234. ieee80211_rx_frame_decrypt(struct ieee80211_device *ieee, struct sk_buff *skb,
  235. struct ieee80211_crypt_data *crypt)
  236. {
  237. struct ieee80211_hdr_3addr *hdr;
  238. int res, hdrlen;
  239. if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
  240. return 0;
  241. hdr = (struct ieee80211_hdr_3addr *)skb->data;
  242. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  243. atomic_inc(&crypt->refcnt);
  244. res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
  245. atomic_dec(&crypt->refcnt);
  246. if (res < 0) {
  247. IEEE80211_DEBUG_DROP("decryption failed (SA=" MAC_FMT
  248. ") res=%d\n", MAC_ARG(hdr->addr2), res);
  249. if (res == -2)
  250. IEEE80211_DEBUG_DROP("Decryption failed ICV "
  251. "mismatch (key %d)\n",
  252. skb->data[hdrlen + 3] >> 6);
  253. ieee->ieee_stats.rx_discards_undecryptable++;
  254. return -1;
  255. }
  256. return res;
  257. }
  258. /* Called only as a tasklet (software IRQ), by ieee80211_rx */
  259. static int
  260. ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device *ieee,
  261. struct sk_buff *skb, int keyidx,
  262. struct ieee80211_crypt_data *crypt)
  263. {
  264. struct ieee80211_hdr_3addr *hdr;
  265. int res, hdrlen;
  266. if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
  267. return 0;
  268. hdr = (struct ieee80211_hdr_3addr *)skb->data;
  269. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
  270. atomic_inc(&crypt->refcnt);
  271. res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
  272. atomic_dec(&crypt->refcnt);
  273. if (res < 0) {
  274. printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
  275. " (SA=" MAC_FMT " keyidx=%d)\n",
  276. ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
  277. return -1;
  278. }
  279. return 0;
  280. }
  281. /* All received frames are sent to this function. @skb contains the frame in
  282. * IEEE 802.11 format, i.e., in the format it was sent over air.
  283. * This function is called only as a tasklet (software IRQ). */
  284. int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
  285. struct ieee80211_rx_stats *rx_stats)
  286. {
  287. struct net_device *dev = ieee->dev;
  288. struct ieee80211_hdr_4addr *hdr;
  289. size_t hdrlen;
  290. u16 fc, type, stype, sc;
  291. struct net_device_stats *stats;
  292. unsigned int frag;
  293. u8 *payload;
  294. u16 ethertype;
  295. #ifdef NOT_YET
  296. struct net_device *wds = NULL;
  297. struct sk_buff *skb2 = NULL;
  298. struct net_device *wds = NULL;
  299. int frame_authorized = 0;
  300. int from_assoc_ap = 0;
  301. void *sta = NULL;
  302. #endif
  303. u8 dst[ETH_ALEN];
  304. u8 src[ETH_ALEN];
  305. struct ieee80211_crypt_data *crypt = NULL;
  306. int keyidx = 0;
  307. int can_be_decrypted = 0;
  308. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  309. stats = &ieee->stats;
  310. if (skb->len < 10) {
  311. printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
  312. goto rx_dropped;
  313. }
  314. fc = le16_to_cpu(hdr->frame_ctl);
  315. type = WLAN_FC_GET_TYPE(fc);
  316. stype = WLAN_FC_GET_STYPE(fc);
  317. sc = le16_to_cpu(hdr->seq_ctl);
  318. frag = WLAN_GET_SEQ_FRAG(sc);
  319. hdrlen = ieee80211_get_hdrlen(fc);
  320. /* Put this code here so that we avoid duplicating it in all
  321. * Rx paths. - Jean II */
  322. #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
  323. #ifdef CONFIG_NET_RADIO
  324. /* If spy monitoring on */
  325. if (ieee->spy_data.spy_number > 0) {
  326. struct iw_quality wstats;
  327. wstats.updated = 0;
  328. if (rx_stats->mask & IEEE80211_STATMASK_RSSI) {
  329. wstats.level = rx_stats->rssi;
  330. wstats.updated |= IW_QUAL_LEVEL_UPDATED;
  331. } else
  332. wstats.updated |= IW_QUAL_LEVEL_INVALID;
  333. if (rx_stats->mask & IEEE80211_STATMASK_NOISE) {
  334. wstats.noise = rx_stats->noise;
  335. wstats.updated |= IW_QUAL_NOISE_UPDATED;
  336. } else
  337. wstats.updated |= IW_QUAL_NOISE_INVALID;
  338. if (rx_stats->mask & IEEE80211_STATMASK_SIGNAL) {
  339. wstats.qual = rx_stats->signal;
  340. wstats.updated |= IW_QUAL_QUAL_UPDATED;
  341. } else
  342. wstats.updated |= IW_QUAL_QUAL_INVALID;
  343. /* Update spy records */
  344. wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
  345. }
  346. #endif /* CONFIG_NET_RADIO */
  347. #endif /* IW_WIRELESS_SPY */
  348. #ifdef NOT_YET
  349. hostap_update_rx_stats(local->ap, hdr, rx_stats);
  350. #endif
  351. if (ieee->iw_mode == IW_MODE_MONITOR) {
  352. ieee80211_monitor_rx(ieee, skb, rx_stats);
  353. stats->rx_packets++;
  354. stats->rx_bytes += skb->len;
  355. return 1;
  356. }
  357. can_be_decrypted = (is_multicast_ether_addr(hdr->addr1) ||
  358. is_broadcast_ether_addr(hdr->addr2)) ?
  359. ieee->host_mc_decrypt : ieee->host_decrypt;
  360. if (can_be_decrypted) {
  361. int idx = 0;
  362. if (skb->len >= hdrlen + 3) {
  363. /* Top two-bits of byte 3 are the key index */
  364. idx = skb->data[hdrlen + 3] >> 6;
  365. }
  366. /* ieee->crypt[] is WEP_KEY (4) in length. Given that idx
  367. * is only allowed 2-bits of storage, no value of idx can
  368. * be provided via above code that would result in idx
  369. * being out of range */
  370. crypt = ieee->crypt[idx];
  371. #ifdef NOT_YET
  372. sta = NULL;
  373. /* Use station specific key to override default keys if the
  374. * receiver address is a unicast address ("individual RA"). If
  375. * bcrx_sta_key parameter is set, station specific key is used
  376. * even with broad/multicast targets (this is against IEEE
  377. * 802.11, but makes it easier to use different keys with
  378. * stations that do not support WEP key mapping). */
  379. if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
  380. (void)hostap_handle_sta_crypto(local, hdr, &crypt,
  381. &sta);
  382. #endif
  383. /* allow NULL decrypt to indicate an station specific override
  384. * for default encryption */
  385. if (crypt && (crypt->ops == NULL ||
  386. crypt->ops->decrypt_mpdu == NULL))
  387. crypt = NULL;
  388. if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
  389. /* This seems to be triggered by some (multicast?)
  390. * frames from other than current BSS, so just drop the
  391. * frames silently instead of filling system log with
  392. * these reports. */
  393. IEEE80211_DEBUG_DROP("Decryption failed (not set)"
  394. " (SA=" MAC_FMT ")\n",
  395. MAC_ARG(hdr->addr2));
  396. ieee->ieee_stats.rx_discards_undecryptable++;
  397. goto rx_dropped;
  398. }
  399. }
  400. #ifdef NOT_YET
  401. if (type != WLAN_FC_TYPE_DATA) {
  402. if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
  403. fc & IEEE80211_FCTL_PROTECTED && ieee->host_decrypt &&
  404. (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0) {
  405. printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
  406. "from " MAC_FMT "\n", dev->name,
  407. MAC_ARG(hdr->addr2));
  408. /* TODO: could inform hostapd about this so that it
  409. * could send auth failure report */
  410. goto rx_dropped;
  411. }
  412. if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
  413. goto rx_dropped;
  414. else
  415. goto rx_exit;
  416. }
  417. #endif
  418. /* Data frame - extract src/dst addresses */
  419. if (skb->len < IEEE80211_3ADDR_LEN)
  420. goto rx_dropped;
  421. switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
  422. case IEEE80211_FCTL_FROMDS:
  423. memcpy(dst, hdr->addr1, ETH_ALEN);
  424. memcpy(src, hdr->addr3, ETH_ALEN);
  425. break;
  426. case IEEE80211_FCTL_TODS:
  427. memcpy(dst, hdr->addr3, ETH_ALEN);
  428. memcpy(src, hdr->addr2, ETH_ALEN);
  429. break;
  430. case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
  431. if (skb->len < IEEE80211_4ADDR_LEN)
  432. goto rx_dropped;
  433. memcpy(dst, hdr->addr3, ETH_ALEN);
  434. memcpy(src, hdr->addr4, ETH_ALEN);
  435. break;
  436. case 0:
  437. memcpy(dst, hdr->addr1, ETH_ALEN);
  438. memcpy(src, hdr->addr2, ETH_ALEN);
  439. break;
  440. }
  441. #ifdef NOT_YET
  442. if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
  443. goto rx_dropped;
  444. if (wds) {
  445. skb->dev = dev = wds;
  446. stats = hostap_get_stats(dev);
  447. }
  448. if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
  449. (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  450. IEEE80211_FCTL_FROMDS && ieee->stadev
  451. && !compare_ether_addr(hdr->addr2, ieee->assoc_ap_addr)) {
  452. /* Frame from BSSID of the AP for which we are a client */
  453. skb->dev = dev = ieee->stadev;
  454. stats = hostap_get_stats(dev);
  455. from_assoc_ap = 1;
  456. }
  457. #endif
  458. dev->last_rx = jiffies;
  459. #ifdef NOT_YET
  460. if ((ieee->iw_mode == IW_MODE_MASTER ||
  461. ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
  462. switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
  463. wds != NULL)) {
  464. case AP_RX_CONTINUE_NOT_AUTHORIZED:
  465. frame_authorized = 0;
  466. break;
  467. case AP_RX_CONTINUE:
  468. frame_authorized = 1;
  469. break;
  470. case AP_RX_DROP:
  471. goto rx_dropped;
  472. case AP_RX_EXIT:
  473. goto rx_exit;
  474. }
  475. }
  476. #endif
  477. /* Nullfunc frames may have PS-bit set, so they must be passed to
  478. * hostap_handle_sta_rx() before being dropped here. */
  479. stype &= ~IEEE80211_STYPE_QOS_DATA;
  480. if (stype != IEEE80211_STYPE_DATA &&
  481. stype != IEEE80211_STYPE_DATA_CFACK &&
  482. stype != IEEE80211_STYPE_DATA_CFPOLL &&
  483. stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
  484. if (stype != IEEE80211_STYPE_NULLFUNC)
  485. IEEE80211_DEBUG_DROP("RX: dropped data frame "
  486. "with no data (type=0x%02x, "
  487. "subtype=0x%02x, len=%d)\n",
  488. type, stype, skb->len);
  489. goto rx_dropped;
  490. }
  491. /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
  492. if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
  493. (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
  494. goto rx_dropped;
  495. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  496. /* skb: hdr + (possibly fragmented) plaintext payload */
  497. // PR: FIXME: hostap has additional conditions in the "if" below:
  498. // ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  499. if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
  500. int flen;
  501. struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
  502. IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
  503. if (!frag_skb) {
  504. IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
  505. "Rx cannot get skb from fragment "
  506. "cache (morefrag=%d seq=%u frag=%u)\n",
  507. (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
  508. WLAN_GET_SEQ_SEQ(sc), frag);
  509. goto rx_dropped;
  510. }
  511. flen = skb->len;
  512. if (frag != 0)
  513. flen -= hdrlen;
  514. if (frag_skb->tail + flen > frag_skb->end) {
  515. printk(KERN_WARNING "%s: host decrypted and "
  516. "reassembled frame did not fit skb\n",
  517. dev->name);
  518. ieee80211_frag_cache_invalidate(ieee, hdr);
  519. goto rx_dropped;
  520. }
  521. if (frag == 0) {
  522. /* copy first fragment (including full headers) into
  523. * beginning of the fragment cache skb */
  524. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  525. } else {
  526. /* append frame payload to the end of the fragment
  527. * cache skb */
  528. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  529. flen);
  530. }
  531. dev_kfree_skb_any(skb);
  532. skb = NULL;
  533. if (fc & IEEE80211_FCTL_MOREFRAGS) {
  534. /* more fragments expected - leave the skb in fragment
  535. * cache for now; it will be delivered to upper layers
  536. * after all fragments have been received */
  537. goto rx_exit;
  538. }
  539. /* this was the last fragment and the frame will be
  540. * delivered, so remove skb from fragment cache */
  541. skb = frag_skb;
  542. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  543. ieee80211_frag_cache_invalidate(ieee, hdr);
  544. }
  545. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  546. * encrypted/authenticated */
  547. if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
  548. ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
  549. goto rx_dropped;
  550. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  551. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
  552. if ( /*ieee->ieee802_1x && */
  553. ieee80211_is_eapol_frame(ieee, skb)) {
  554. /* pass unencrypted EAPOL frames even if encryption is
  555. * configured */
  556. } else {
  557. IEEE80211_DEBUG_DROP("encryption configured, but RX "
  558. "frame not encrypted (SA=" MAC_FMT
  559. ")\n", MAC_ARG(hdr->addr2));
  560. goto rx_dropped;
  561. }
  562. }
  563. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
  564. !ieee80211_is_eapol_frame(ieee, skb)) {
  565. IEEE80211_DEBUG_DROP("dropped unencrypted RX data "
  566. "frame from " MAC_FMT
  567. " (drop_unencrypted=1)\n",
  568. MAC_ARG(hdr->addr2));
  569. goto rx_dropped;
  570. }
  571. /* skb: hdr + (possible reassembled) full plaintext payload */
  572. payload = skb->data + hdrlen;
  573. ethertype = (payload[6] << 8) | payload[7];
  574. #ifdef NOT_YET
  575. /* If IEEE 802.1X is used, check whether the port is authorized to send
  576. * the received frame. */
  577. if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
  578. if (ethertype == ETH_P_PAE) {
  579. printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
  580. dev->name);
  581. if (ieee->hostapd && ieee->apdev) {
  582. /* Send IEEE 802.1X frames to the user
  583. * space daemon for processing */
  584. prism2_rx_80211(ieee->apdev, skb, rx_stats,
  585. PRISM2_RX_MGMT);
  586. ieee->apdevstats.rx_packets++;
  587. ieee->apdevstats.rx_bytes += skb->len;
  588. goto rx_exit;
  589. }
  590. } else if (!frame_authorized) {
  591. printk(KERN_DEBUG "%s: dropped frame from "
  592. "unauthorized port (IEEE 802.1X): "
  593. "ethertype=0x%04x\n", dev->name, ethertype);
  594. goto rx_dropped;
  595. }
  596. }
  597. #endif
  598. /* convert hdr + possible LLC headers into Ethernet header */
  599. if (skb->len - hdrlen >= 8 &&
  600. ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
  601. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  602. memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
  603. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  604. * replace EtherType */
  605. skb_pull(skb, hdrlen + SNAP_SIZE);
  606. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  607. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  608. } else {
  609. u16 len;
  610. /* Leave Ethernet header part of hdr and full payload */
  611. skb_pull(skb, hdrlen);
  612. len = htons(skb->len);
  613. memcpy(skb_push(skb, 2), &len, 2);
  614. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  615. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  616. }
  617. #ifdef NOT_YET
  618. if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  619. IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
  620. /* Non-standard frame: get addr4 from its bogus location after
  621. * the payload */
  622. memcpy(skb->data + ETH_ALEN,
  623. skb->data + skb->len - ETH_ALEN, ETH_ALEN);
  624. skb_trim(skb, skb->len - ETH_ALEN);
  625. }
  626. #endif
  627. stats->rx_packets++;
  628. stats->rx_bytes += skb->len;
  629. #ifdef NOT_YET
  630. if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
  631. if (dst[0] & 0x01) {
  632. /* copy multicast frame both to the higher layers and
  633. * to the wireless media */
  634. ieee->ap->bridged_multicast++;
  635. skb2 = skb_clone(skb, GFP_ATOMIC);
  636. if (skb2 == NULL)
  637. printk(KERN_DEBUG "%s: skb_clone failed for "
  638. "multicast frame\n", dev->name);
  639. } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
  640. /* send frame directly to the associated STA using
  641. * wireless media and not passing to higher layers */
  642. ieee->ap->bridged_unicast++;
  643. skb2 = skb;
  644. skb = NULL;
  645. }
  646. }
  647. if (skb2 != NULL) {
  648. /* send to wireless media */
  649. skb2->protocol = __constant_htons(ETH_P_802_3);
  650. skb2->mac.raw = skb2->nh.raw = skb2->data;
  651. /* skb2->nh.raw = skb2->data + ETH_HLEN; */
  652. skb2->dev = dev;
  653. dev_queue_xmit(skb2);
  654. }
  655. #endif
  656. if (skb) {
  657. skb->protocol = eth_type_trans(skb, dev);
  658. memset(skb->cb, 0, sizeof(skb->cb));
  659. skb->dev = dev;
  660. skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
  661. netif_rx(skb);
  662. }
  663. rx_exit:
  664. #ifdef NOT_YET
  665. if (sta)
  666. hostap_handle_sta_release(sta);
  667. #endif
  668. return 1;
  669. rx_dropped:
  670. stats->rx_dropped++;
  671. /* Returning 0 indicates to caller that we have not handled the SKB--
  672. * so it is still allocated and can be used again by underlying
  673. * hardware as a DMA target */
  674. return 0;
  675. }
  676. #define MGMT_FRAME_FIXED_PART_LENGTH 0x24
  677. static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
  678. /*
  679. * Make ther structure we read from the beacon packet has
  680. * the right values
  681. */
  682. static int ieee80211_verify_qos_info(struct ieee80211_qos_information_element
  683. *info_element, int sub_type)
  684. {
  685. if (info_element->qui_subtype != sub_type)
  686. return -1;
  687. if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
  688. return -1;
  689. if (info_element->qui_type != QOS_OUI_TYPE)
  690. return -1;
  691. if (info_element->version != QOS_VERSION_1)
  692. return -1;
  693. return 0;
  694. }
  695. /*
  696. * Parse a QoS parameter element
  697. */
  698. static int ieee80211_read_qos_param_element(struct ieee80211_qos_parameter_info
  699. *element_param, struct ieee80211_info_element
  700. *info_element)
  701. {
  702. int ret = 0;
  703. u16 size = sizeof(struct ieee80211_qos_parameter_info) - 2;
  704. if ((info_element == NULL) || (element_param == NULL))
  705. return -1;
  706. if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
  707. memcpy(element_param->info_element.qui, info_element->data,
  708. info_element->len);
  709. element_param->info_element.elementID = info_element->id;
  710. element_param->info_element.length = info_element->len;
  711. } else
  712. ret = -1;
  713. if (ret == 0)
  714. ret = ieee80211_verify_qos_info(&element_param->info_element,
  715. QOS_OUI_PARAM_SUB_TYPE);
  716. return ret;
  717. }
  718. /*
  719. * Parse a QoS information element
  720. */
  721. static int ieee80211_read_qos_info_element(struct
  722. ieee80211_qos_information_element
  723. *element_info, struct ieee80211_info_element
  724. *info_element)
  725. {
  726. int ret = 0;
  727. u16 size = sizeof(struct ieee80211_qos_information_element) - 2;
  728. if (element_info == NULL)
  729. return -1;
  730. if (info_element == NULL)
  731. return -1;
  732. if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
  733. memcpy(element_info->qui, info_element->data,
  734. info_element->len);
  735. element_info->elementID = info_element->id;
  736. element_info->length = info_element->len;
  737. } else
  738. ret = -1;
  739. if (ret == 0)
  740. ret = ieee80211_verify_qos_info(element_info,
  741. QOS_OUI_INFO_SUB_TYPE);
  742. return ret;
  743. }
  744. /*
  745. * Write QoS parameters from the ac parameters.
  746. */
  747. static int ieee80211_qos_convert_ac_to_parameters(struct
  748. ieee80211_qos_parameter_info
  749. *param_elm, struct
  750. ieee80211_qos_parameters
  751. *qos_param)
  752. {
  753. int rc = 0;
  754. int i;
  755. struct ieee80211_qos_ac_parameter *ac_params;
  756. u32 txop;
  757. u8 cw_min;
  758. u8 cw_max;
  759. for (i = 0; i < QOS_QUEUE_NUM; i++) {
  760. ac_params = &(param_elm->ac_params_record[i]);
  761. qos_param->aifs[i] = (ac_params->aci_aifsn) & 0x0F;
  762. qos_param->aifs[i] -= (qos_param->aifs[i] < 2) ? 0 : 2;
  763. cw_min = ac_params->ecw_min_max & 0x0F;
  764. qos_param->cw_min[i] = (u16) ((1 << cw_min) - 1);
  765. cw_max = (ac_params->ecw_min_max & 0xF0) >> 4;
  766. qos_param->cw_max[i] = (u16) ((1 << cw_max) - 1);
  767. qos_param->flag[i] =
  768. (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
  769. txop = le16_to_cpu(ac_params->tx_op_limit) * 32;
  770. qos_param->tx_op_limit[i] = (u16) txop;
  771. }
  772. return rc;
  773. }
  774. /*
  775. * we have a generic data element which it may contain QoS information or
  776. * parameters element. check the information element length to decide
  777. * which type to read
  778. */
  779. static int ieee80211_parse_qos_info_param_IE(struct ieee80211_info_element
  780. *info_element,
  781. struct ieee80211_network *network)
  782. {
  783. int rc = 0;
  784. struct ieee80211_qos_parameters *qos_param = NULL;
  785. struct ieee80211_qos_information_element qos_info_element;
  786. rc = ieee80211_read_qos_info_element(&qos_info_element, info_element);
  787. if (rc == 0) {
  788. network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
  789. network->flags |= NETWORK_HAS_QOS_INFORMATION;
  790. } else {
  791. struct ieee80211_qos_parameter_info param_element;
  792. rc = ieee80211_read_qos_param_element(&param_element,
  793. info_element);
  794. if (rc == 0) {
  795. qos_param = &(network->qos_data.parameters);
  796. ieee80211_qos_convert_ac_to_parameters(&param_element,
  797. qos_param);
  798. network->flags |= NETWORK_HAS_QOS_PARAMETERS;
  799. network->qos_data.param_count =
  800. param_element.info_element.ac_info & 0x0F;
  801. }
  802. }
  803. if (rc == 0) {
  804. IEEE80211_DEBUG_QOS("QoS is supported\n");
  805. network->qos_data.supported = 1;
  806. }
  807. return rc;
  808. }
  809. static int ieee80211_parse_info_param(struct ieee80211_info_element
  810. *info_element, u16 length,
  811. struct ieee80211_network *network)
  812. {
  813. u8 i;
  814. #ifdef CONFIG_IEEE80211_DEBUG
  815. char rates_str[64];
  816. char *p;
  817. #endif
  818. while (length >= sizeof(*info_element)) {
  819. if (sizeof(*info_element) + info_element->len > length) {
  820. IEEE80211_DEBUG_MGMT("Info elem: parse failed: "
  821. "info_element->len + 2 > left : "
  822. "info_element->len+2=%zd left=%d, id=%d.\n",
  823. info_element->len +
  824. sizeof(*info_element),
  825. length, info_element->id);
  826. return 1;
  827. }
  828. switch (info_element->id) {
  829. case MFIE_TYPE_SSID:
  830. if (ieee80211_is_empty_essid(info_element->data,
  831. info_element->len)) {
  832. network->flags |= NETWORK_EMPTY_ESSID;
  833. break;
  834. }
  835. network->ssid_len = min(info_element->len,
  836. (u8) IW_ESSID_MAX_SIZE);
  837. memcpy(network->ssid, info_element->data,
  838. network->ssid_len);
  839. if (network->ssid_len < IW_ESSID_MAX_SIZE)
  840. memset(network->ssid + network->ssid_len, 0,
  841. IW_ESSID_MAX_SIZE - network->ssid_len);
  842. IEEE80211_DEBUG_MGMT("MFIE_TYPE_SSID: '%s' len=%d.\n",
  843. network->ssid, network->ssid_len);
  844. break;
  845. case MFIE_TYPE_RATES:
  846. #ifdef CONFIG_IEEE80211_DEBUG
  847. p = rates_str;
  848. #endif
  849. network->rates_len = min(info_element->len,
  850. MAX_RATES_LENGTH);
  851. for (i = 0; i < network->rates_len; i++) {
  852. network->rates[i] = info_element->data[i];
  853. #ifdef CONFIG_IEEE80211_DEBUG
  854. p += snprintf(p, sizeof(rates_str) -
  855. (p - rates_str), "%02X ",
  856. network->rates[i]);
  857. #endif
  858. if (ieee80211_is_ofdm_rate
  859. (info_element->data[i])) {
  860. network->flags |= NETWORK_HAS_OFDM;
  861. if (info_element->data[i] &
  862. IEEE80211_BASIC_RATE_MASK)
  863. network->flags &=
  864. ~NETWORK_HAS_CCK;
  865. }
  866. }
  867. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES: '%s' (%d)\n",
  868. rates_str, network->rates_len);
  869. break;
  870. case MFIE_TYPE_RATES_EX:
  871. #ifdef CONFIG_IEEE80211_DEBUG
  872. p = rates_str;
  873. #endif
  874. network->rates_ex_len = min(info_element->len,
  875. MAX_RATES_EX_LENGTH);
  876. for (i = 0; i < network->rates_ex_len; i++) {
  877. network->rates_ex[i] = info_element->data[i];
  878. #ifdef CONFIG_IEEE80211_DEBUG
  879. p += snprintf(p, sizeof(rates_str) -
  880. (p - rates_str), "%02X ",
  881. network->rates[i]);
  882. #endif
  883. if (ieee80211_is_ofdm_rate
  884. (info_element->data[i])) {
  885. network->flags |= NETWORK_HAS_OFDM;
  886. if (info_element->data[i] &
  887. IEEE80211_BASIC_RATE_MASK)
  888. network->flags &=
  889. ~NETWORK_HAS_CCK;
  890. }
  891. }
  892. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
  893. rates_str, network->rates_ex_len);
  894. break;
  895. case MFIE_TYPE_DS_SET:
  896. IEEE80211_DEBUG_MGMT("MFIE_TYPE_DS_SET: %d\n",
  897. info_element->data[0]);
  898. network->channel = info_element->data[0];
  899. break;
  900. case MFIE_TYPE_FH_SET:
  901. IEEE80211_DEBUG_MGMT("MFIE_TYPE_FH_SET: ignored\n");
  902. break;
  903. case MFIE_TYPE_CF_SET:
  904. IEEE80211_DEBUG_MGMT("MFIE_TYPE_CF_SET: ignored\n");
  905. break;
  906. case MFIE_TYPE_TIM:
  907. IEEE80211_DEBUG_MGMT("MFIE_TYPE_TIM: ignored\n");
  908. break;
  909. case MFIE_TYPE_ERP_INFO:
  910. network->erp_value = info_element->data[0];
  911. IEEE80211_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
  912. network->erp_value);
  913. break;
  914. case MFIE_TYPE_IBSS_SET:
  915. network->atim_window = info_element->data[0];
  916. IEEE80211_DEBUG_MGMT("MFIE_TYPE_IBSS_SET: %d\n",
  917. network->atim_window);
  918. break;
  919. case MFIE_TYPE_CHALLENGE:
  920. IEEE80211_DEBUG_MGMT("MFIE_TYPE_CHALLENGE: ignored\n");
  921. break;
  922. case MFIE_TYPE_GENERIC:
  923. IEEE80211_DEBUG_MGMT("MFIE_TYPE_GENERIC: %d bytes\n",
  924. info_element->len);
  925. if (!ieee80211_parse_qos_info_param_IE(info_element,
  926. network))
  927. break;
  928. if (info_element->len >= 4 &&
  929. info_element->data[0] == 0x00 &&
  930. info_element->data[1] == 0x50 &&
  931. info_element->data[2] == 0xf2 &&
  932. info_element->data[3] == 0x01) {
  933. network->wpa_ie_len = min(info_element->len + 2,
  934. MAX_WPA_IE_LEN);
  935. memcpy(network->wpa_ie, info_element,
  936. network->wpa_ie_len);
  937. }
  938. break;
  939. case MFIE_TYPE_RSN:
  940. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RSN: %d bytes\n",
  941. info_element->len);
  942. network->rsn_ie_len = min(info_element->len + 2,
  943. MAX_WPA_IE_LEN);
  944. memcpy(network->rsn_ie, info_element,
  945. network->rsn_ie_len);
  946. break;
  947. case MFIE_TYPE_QOS_PARAMETER:
  948. printk(KERN_ERR
  949. "QoS Error need to parse QOS_PARAMETER IE\n");
  950. break;
  951. default:
  952. IEEE80211_DEBUG_MGMT("unsupported IE %d\n",
  953. info_element->id);
  954. break;
  955. }
  956. length -= sizeof(*info_element) + info_element->len;
  957. info_element =
  958. (struct ieee80211_info_element *)&info_element->
  959. data[info_element->len];
  960. }
  961. return 0;
  962. }
  963. static int ieee80211_handle_assoc_resp(struct ieee80211_device *ieee, struct ieee80211_assoc_response
  964. *frame, struct ieee80211_rx_stats *stats)
  965. {
  966. struct ieee80211_network network_resp;
  967. struct ieee80211_network *network = &network_resp;
  968. struct net_device *dev = ieee->dev;
  969. network->flags = 0;
  970. network->qos_data.active = 0;
  971. network->qos_data.supported = 0;
  972. network->qos_data.param_count = 0;
  973. network->qos_data.old_param_count = 0;
  974. //network->atim_window = le16_to_cpu(frame->aid) & (0x3FFF);
  975. network->atim_window = le16_to_cpu(frame->aid);
  976. network->listen_interval = le16_to_cpu(frame->status);
  977. memcpy(network->bssid, frame->header.addr3, ETH_ALEN);
  978. network->capability = le16_to_cpu(frame->capability);
  979. network->last_scanned = jiffies;
  980. network->rates_len = network->rates_ex_len = 0;
  981. network->last_associate = 0;
  982. network->ssid_len = 0;
  983. network->erp_value =
  984. (network->capability & WLAN_CAPABILITY_IBSS) ? 0x3 : 0x0;
  985. if (stats->freq == IEEE80211_52GHZ_BAND) {
  986. /* for A band (No DS info) */
  987. network->channel = stats->received_channel;
  988. } else
  989. network->flags |= NETWORK_HAS_CCK;
  990. network->wpa_ie_len = 0;
  991. network->rsn_ie_len = 0;
  992. if (ieee80211_parse_info_param
  993. (frame->info_element, stats->len - sizeof(*frame), network))
  994. return 1;
  995. network->mode = 0;
  996. if (stats->freq == IEEE80211_52GHZ_BAND)
  997. network->mode = IEEE_A;
  998. else {
  999. if (network->flags & NETWORK_HAS_OFDM)
  1000. network->mode |= IEEE_G;
  1001. if (network->flags & NETWORK_HAS_CCK)
  1002. network->mode |= IEEE_B;
  1003. }
  1004. if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
  1005. network->flags |= NETWORK_EMPTY_ESSID;
  1006. memcpy(&network->stats, stats, sizeof(network->stats));
  1007. if (ieee->handle_assoc_response != NULL)
  1008. ieee->handle_assoc_response(dev, frame, network);
  1009. return 0;
  1010. }
  1011. /***************************************************/
  1012. static int ieee80211_network_init(struct ieee80211_device *ieee, struct ieee80211_probe_response
  1013. *beacon,
  1014. struct ieee80211_network *network,
  1015. struct ieee80211_rx_stats *stats)
  1016. {
  1017. network->qos_data.active = 0;
  1018. network->qos_data.supported = 0;
  1019. network->qos_data.param_count = 0;
  1020. network->qos_data.old_param_count = 0;
  1021. /* Pull out fixed field data */
  1022. memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
  1023. network->capability = le16_to_cpu(beacon->capability);
  1024. network->last_scanned = jiffies;
  1025. network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
  1026. network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
  1027. network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
  1028. /* Where to pull this? beacon->listen_interval; */
  1029. network->listen_interval = 0x0A;
  1030. network->rates_len = network->rates_ex_len = 0;
  1031. network->last_associate = 0;
  1032. network->ssid_len = 0;
  1033. network->flags = 0;
  1034. network->atim_window = 0;
  1035. network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
  1036. 0x3 : 0x0;
  1037. if (stats->freq == IEEE80211_52GHZ_BAND) {
  1038. /* for A band (No DS info) */
  1039. network->channel = stats->received_channel;
  1040. } else
  1041. network->flags |= NETWORK_HAS_CCK;
  1042. network->wpa_ie_len = 0;
  1043. network->rsn_ie_len = 0;
  1044. if (ieee80211_parse_info_param
  1045. (beacon->info_element, stats->len - sizeof(*beacon), network))
  1046. return 1;
  1047. network->mode = 0;
  1048. if (stats->freq == IEEE80211_52GHZ_BAND)
  1049. network->mode = IEEE_A;
  1050. else {
  1051. if (network->flags & NETWORK_HAS_OFDM)
  1052. network->mode |= IEEE_G;
  1053. if (network->flags & NETWORK_HAS_CCK)
  1054. network->mode |= IEEE_B;
  1055. }
  1056. if (network->mode == 0) {
  1057. IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
  1058. "network.\n",
  1059. escape_essid(network->ssid,
  1060. network->ssid_len),
  1061. MAC_ARG(network->bssid));
  1062. return 1;
  1063. }
  1064. if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
  1065. network->flags |= NETWORK_EMPTY_ESSID;
  1066. memcpy(&network->stats, stats, sizeof(network->stats));
  1067. return 0;
  1068. }
  1069. static inline int is_same_network(struct ieee80211_network *src,
  1070. struct ieee80211_network *dst)
  1071. {
  1072. /* A network is only a duplicate if the channel, BSSID, and ESSID
  1073. * all match. We treat all <hidden> with the same BSSID and channel
  1074. * as one network */
  1075. return ((src->ssid_len == dst->ssid_len) &&
  1076. (src->channel == dst->channel) &&
  1077. !compare_ether_addr(src->bssid, dst->bssid) &&
  1078. !memcmp(src->ssid, dst->ssid, src->ssid_len));
  1079. }
  1080. static void update_network(struct ieee80211_network *dst,
  1081. struct ieee80211_network *src)
  1082. {
  1083. int qos_active;
  1084. u8 old_param;
  1085. memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
  1086. dst->capability = src->capability;
  1087. memcpy(dst->rates, src->rates, src->rates_len);
  1088. dst->rates_len = src->rates_len;
  1089. memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
  1090. dst->rates_ex_len = src->rates_ex_len;
  1091. dst->mode = src->mode;
  1092. dst->flags = src->flags;
  1093. dst->time_stamp[0] = src->time_stamp[0];
  1094. dst->time_stamp[1] = src->time_stamp[1];
  1095. dst->beacon_interval = src->beacon_interval;
  1096. dst->listen_interval = src->listen_interval;
  1097. dst->atim_window = src->atim_window;
  1098. dst->erp_value = src->erp_value;
  1099. memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
  1100. dst->wpa_ie_len = src->wpa_ie_len;
  1101. memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
  1102. dst->rsn_ie_len = src->rsn_ie_len;
  1103. dst->last_scanned = jiffies;
  1104. qos_active = src->qos_data.active;
  1105. old_param = dst->qos_data.old_param_count;
  1106. if (dst->flags & NETWORK_HAS_QOS_MASK)
  1107. memcpy(&dst->qos_data, &src->qos_data,
  1108. sizeof(struct ieee80211_qos_data));
  1109. else {
  1110. dst->qos_data.supported = src->qos_data.supported;
  1111. dst->qos_data.param_count = src->qos_data.param_count;
  1112. }
  1113. if (dst->qos_data.supported == 1) {
  1114. if (dst->ssid_len)
  1115. IEEE80211_DEBUG_QOS
  1116. ("QoS the network %s is QoS supported\n",
  1117. dst->ssid);
  1118. else
  1119. IEEE80211_DEBUG_QOS
  1120. ("QoS the network is QoS supported\n");
  1121. }
  1122. dst->qos_data.active = qos_active;
  1123. dst->qos_data.old_param_count = old_param;
  1124. /* dst->last_associate is not overwritten */
  1125. }
  1126. static inline int is_beacon(int fc)
  1127. {
  1128. return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
  1129. }
  1130. static void ieee80211_process_probe_response(struct ieee80211_device
  1131. *ieee, struct
  1132. ieee80211_probe_response
  1133. *beacon, struct ieee80211_rx_stats
  1134. *stats)
  1135. {
  1136. struct net_device *dev = ieee->dev;
  1137. struct ieee80211_network network;
  1138. struct ieee80211_network *target;
  1139. struct ieee80211_network *oldest = NULL;
  1140. #ifdef CONFIG_IEEE80211_DEBUG
  1141. struct ieee80211_info_element *info_element = beacon->info_element;
  1142. #endif
  1143. unsigned long flags;
  1144. IEEE80211_DEBUG_SCAN("'%s' (" MAC_FMT
  1145. "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
  1146. escape_essid(info_element->data,
  1147. info_element->len),
  1148. MAC_ARG(beacon->header.addr3),
  1149. (beacon->capability & (1 << 0xf)) ? '1' : '0',
  1150. (beacon->capability & (1 << 0xe)) ? '1' : '0',
  1151. (beacon->capability & (1 << 0xd)) ? '1' : '0',
  1152. (beacon->capability & (1 << 0xc)) ? '1' : '0',
  1153. (beacon->capability & (1 << 0xb)) ? '1' : '0',
  1154. (beacon->capability & (1 << 0xa)) ? '1' : '0',
  1155. (beacon->capability & (1 << 0x9)) ? '1' : '0',
  1156. (beacon->capability & (1 << 0x8)) ? '1' : '0',
  1157. (beacon->capability & (1 << 0x7)) ? '1' : '0',
  1158. (beacon->capability & (1 << 0x6)) ? '1' : '0',
  1159. (beacon->capability & (1 << 0x5)) ? '1' : '0',
  1160. (beacon->capability & (1 << 0x4)) ? '1' : '0',
  1161. (beacon->capability & (1 << 0x3)) ? '1' : '0',
  1162. (beacon->capability & (1 << 0x2)) ? '1' : '0',
  1163. (beacon->capability & (1 << 0x1)) ? '1' : '0',
  1164. (beacon->capability & (1 << 0x0)) ? '1' : '0');
  1165. if (ieee80211_network_init(ieee, beacon, &network, stats)) {
  1166. IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
  1167. escape_essid(info_element->data,
  1168. info_element->len),
  1169. MAC_ARG(beacon->header.addr3),
  1170. is_beacon(le16_to_cpu
  1171. (beacon->header.
  1172. frame_ctl)) ?
  1173. "BEACON" : "PROBE RESPONSE");
  1174. return;
  1175. }
  1176. /* The network parsed correctly -- so now we scan our known networks
  1177. * to see if we can find it in our list.
  1178. *
  1179. * NOTE: This search is definitely not optimized. Once its doing
  1180. * the "right thing" we'll optimize it for efficiency if
  1181. * necessary */
  1182. /* Search for this entry in the list and update it if it is
  1183. * already there. */
  1184. spin_lock_irqsave(&ieee->lock, flags);
  1185. list_for_each_entry(target, &ieee->network_list, list) {
  1186. if (is_same_network(target, &network))
  1187. break;
  1188. if ((oldest == NULL) ||
  1189. (target->last_scanned < oldest->last_scanned))
  1190. oldest = target;
  1191. }
  1192. /* If we didn't find a match, then get a new network slot to initialize
  1193. * with this beacon's information */
  1194. if (&target->list == &ieee->network_list) {
  1195. if (list_empty(&ieee->network_free_list)) {
  1196. /* If there are no more slots, expire the oldest */
  1197. list_del(&oldest->list);
  1198. target = oldest;
  1199. IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
  1200. "network list.\n",
  1201. escape_essid(target->ssid,
  1202. target->ssid_len),
  1203. MAC_ARG(target->bssid));
  1204. } else {
  1205. /* Otherwise just pull from the free list */
  1206. target = list_entry(ieee->network_free_list.next,
  1207. struct ieee80211_network, list);
  1208. list_del(ieee->network_free_list.next);
  1209. }
  1210. #ifdef CONFIG_IEEE80211_DEBUG
  1211. IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
  1212. escape_essid(network.ssid,
  1213. network.ssid_len),
  1214. MAC_ARG(network.bssid),
  1215. is_beacon(le16_to_cpu
  1216. (beacon->header.
  1217. frame_ctl)) ?
  1218. "BEACON" : "PROBE RESPONSE");
  1219. #endif
  1220. memcpy(target, &network, sizeof(*target));
  1221. list_add_tail(&target->list, &ieee->network_list);
  1222. } else {
  1223. IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
  1224. escape_essid(target->ssid,
  1225. target->ssid_len),
  1226. MAC_ARG(target->bssid),
  1227. is_beacon(le16_to_cpu
  1228. (beacon->header.
  1229. frame_ctl)) ?
  1230. "BEACON" : "PROBE RESPONSE");
  1231. update_network(target, &network);
  1232. }
  1233. spin_unlock_irqrestore(&ieee->lock, flags);
  1234. if (is_beacon(le16_to_cpu(beacon->header.frame_ctl))) {
  1235. if (ieee->handle_beacon != NULL)
  1236. ieee->handle_beacon(dev, beacon, &network);
  1237. } else {
  1238. if (ieee->handle_probe_response != NULL)
  1239. ieee->handle_probe_response(dev, beacon, &network);
  1240. }
  1241. }
  1242. void ieee80211_rx_mgt(struct ieee80211_device *ieee,
  1243. struct ieee80211_hdr_4addr *header,
  1244. struct ieee80211_rx_stats *stats)
  1245. {
  1246. switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
  1247. case IEEE80211_STYPE_ASSOC_RESP:
  1248. IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
  1249. WLAN_FC_GET_STYPE(le16_to_cpu
  1250. (header->frame_ctl)));
  1251. ieee80211_handle_assoc_resp(ieee,
  1252. (struct ieee80211_assoc_response *)
  1253. header, stats);
  1254. break;
  1255. case IEEE80211_STYPE_REASSOC_RESP:
  1256. IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
  1257. WLAN_FC_GET_STYPE(le16_to_cpu
  1258. (header->frame_ctl)));
  1259. break;
  1260. case IEEE80211_STYPE_PROBE_REQ:
  1261. IEEE80211_DEBUG_MGMT("received auth (%d)\n",
  1262. WLAN_FC_GET_STYPE(le16_to_cpu
  1263. (header->frame_ctl)));
  1264. if (ieee->handle_probe_request != NULL)
  1265. ieee->handle_probe_request(ieee->dev,
  1266. (struct
  1267. ieee80211_probe_request *)
  1268. header, stats);
  1269. break;
  1270. case IEEE80211_STYPE_PROBE_RESP:
  1271. IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
  1272. WLAN_FC_GET_STYPE(le16_to_cpu
  1273. (header->frame_ctl)));
  1274. IEEE80211_DEBUG_SCAN("Probe response\n");
  1275. ieee80211_process_probe_response(ieee,
  1276. (struct
  1277. ieee80211_probe_response *)
  1278. header, stats);
  1279. break;
  1280. case IEEE80211_STYPE_BEACON:
  1281. IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
  1282. WLAN_FC_GET_STYPE(le16_to_cpu
  1283. (header->frame_ctl)));
  1284. IEEE80211_DEBUG_SCAN("Beacon\n");
  1285. ieee80211_process_probe_response(ieee,
  1286. (struct
  1287. ieee80211_probe_response *)
  1288. header, stats);
  1289. break;
  1290. case IEEE80211_STYPE_AUTH:
  1291. IEEE80211_DEBUG_MGMT("received auth (%d)\n",
  1292. WLAN_FC_GET_STYPE(le16_to_cpu
  1293. (header->frame_ctl)));
  1294. if (ieee->handle_auth != NULL)
  1295. ieee->handle_auth(ieee->dev,
  1296. (struct ieee80211_auth *)header);
  1297. break;
  1298. case IEEE80211_STYPE_DISASSOC:
  1299. if (ieee->handle_disassoc != NULL)
  1300. ieee->handle_disassoc(ieee->dev,
  1301. (struct ieee80211_disassoc *)
  1302. header);
  1303. break;
  1304. case IEEE80211_STYPE_DEAUTH:
  1305. printk("DEAUTH from AP\n");
  1306. if (ieee->handle_deauth != NULL)
  1307. ieee->handle_deauth(ieee->dev, (struct ieee80211_auth *)
  1308. header);
  1309. break;
  1310. default:
  1311. IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
  1312. WLAN_FC_GET_STYPE(le16_to_cpu
  1313. (header->frame_ctl)));
  1314. IEEE80211_WARNING("%s: Unknown management packet: %d\n",
  1315. ieee->dev->name,
  1316. WLAN_FC_GET_STYPE(le16_to_cpu
  1317. (header->frame_ctl)));
  1318. break;
  1319. }
  1320. }
  1321. EXPORT_SYMBOL(ieee80211_rx_mgt);
  1322. EXPORT_SYMBOL(ieee80211_rx);