ieee80211_rx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  308. stats = &ieee->stats;
  309. if (skb->len < 10) {
  310. printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
  311. goto rx_dropped;
  312. }
  313. fc = le16_to_cpu(hdr->frame_ctl);
  314. type = WLAN_FC_GET_TYPE(fc);
  315. stype = WLAN_FC_GET_STYPE(fc);
  316. sc = le16_to_cpu(hdr->seq_ctl);
  317. frag = WLAN_GET_SEQ_FRAG(sc);
  318. hdrlen = ieee80211_get_hdrlen(fc);
  319. /* Put this code here so that we avoid duplicating it in all
  320. * Rx paths. - Jean II */
  321. #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
  322. #ifdef CONFIG_NET_RADIO
  323. /* If spy monitoring on */
  324. if (ieee->spy_data.spy_number > 0) {
  325. struct iw_quality wstats;
  326. wstats.updated = 0;
  327. if (rx_stats->mask & IEEE80211_STATMASK_RSSI) {
  328. wstats.level = rx_stats->rssi;
  329. wstats.updated |= IW_QUAL_LEVEL_UPDATED;
  330. } else
  331. wstats.updated |= IW_QUAL_LEVEL_INVALID;
  332. if (rx_stats->mask & IEEE80211_STATMASK_NOISE) {
  333. wstats.noise = rx_stats->noise;
  334. wstats.updated |= IW_QUAL_NOISE_UPDATED;
  335. } else
  336. wstats.updated |= IW_QUAL_NOISE_INVALID;
  337. if (rx_stats->mask & IEEE80211_STATMASK_SIGNAL) {
  338. wstats.qual = rx_stats->signal;
  339. wstats.updated |= IW_QUAL_QUAL_UPDATED;
  340. } else
  341. wstats.updated |= IW_QUAL_QUAL_INVALID;
  342. /* Update spy records */
  343. wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
  344. }
  345. #endif /* CONFIG_NET_RADIO */
  346. #endif /* IW_WIRELESS_SPY */
  347. #ifdef NOT_YET
  348. hostap_update_rx_stats(local->ap, hdr, rx_stats);
  349. #endif
  350. if (ieee->iw_mode == IW_MODE_MONITOR) {
  351. ieee80211_monitor_rx(ieee, skb, rx_stats);
  352. stats->rx_packets++;
  353. stats->rx_bytes += skb->len;
  354. return 1;
  355. }
  356. if (is_multicast_ether_addr(hdr->addr1)
  357. ? ieee->host_mc_decrypt : ieee->host_decrypt) {
  358. int idx = 0;
  359. if (skb->len >= hdrlen + 3)
  360. idx = skb->data[hdrlen + 3] >> 6;
  361. crypt = ieee->crypt[idx];
  362. #ifdef NOT_YET
  363. sta = NULL;
  364. /* Use station specific key to override default keys if the
  365. * receiver address is a unicast address ("individual RA"). If
  366. * bcrx_sta_key parameter is set, station specific key is used
  367. * even with broad/multicast targets (this is against IEEE
  368. * 802.11, but makes it easier to use different keys with
  369. * stations that do not support WEP key mapping). */
  370. if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
  371. (void)hostap_handle_sta_crypto(local, hdr, &crypt,
  372. &sta);
  373. #endif
  374. /* allow NULL decrypt to indicate an station specific override
  375. * for default encryption */
  376. if (crypt && (crypt->ops == NULL ||
  377. crypt->ops->decrypt_mpdu == NULL))
  378. crypt = NULL;
  379. if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
  380. /* This seems to be triggered by some (multicast?)
  381. * frames from other than current BSS, so just drop the
  382. * frames silently instead of filling system log with
  383. * these reports. */
  384. IEEE80211_DEBUG_DROP("Decryption failed (not set)"
  385. " (SA=" MAC_FMT ")\n",
  386. MAC_ARG(hdr->addr2));
  387. ieee->ieee_stats.rx_discards_undecryptable++;
  388. goto rx_dropped;
  389. }
  390. }
  391. #ifdef NOT_YET
  392. if (type != WLAN_FC_TYPE_DATA) {
  393. if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
  394. fc & IEEE80211_FCTL_PROTECTED && ieee->host_decrypt &&
  395. (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0) {
  396. printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
  397. "from " MAC_FMT "\n", dev->name,
  398. MAC_ARG(hdr->addr2));
  399. /* TODO: could inform hostapd about this so that it
  400. * could send auth failure report */
  401. goto rx_dropped;
  402. }
  403. if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
  404. goto rx_dropped;
  405. else
  406. goto rx_exit;
  407. }
  408. #endif
  409. /* Data frame - extract src/dst addresses */
  410. if (skb->len < IEEE80211_3ADDR_LEN)
  411. goto rx_dropped;
  412. switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
  413. case IEEE80211_FCTL_FROMDS:
  414. memcpy(dst, hdr->addr1, ETH_ALEN);
  415. memcpy(src, hdr->addr3, ETH_ALEN);
  416. break;
  417. case IEEE80211_FCTL_TODS:
  418. memcpy(dst, hdr->addr3, ETH_ALEN);
  419. memcpy(src, hdr->addr2, ETH_ALEN);
  420. break;
  421. case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
  422. if (skb->len < IEEE80211_4ADDR_LEN)
  423. goto rx_dropped;
  424. memcpy(dst, hdr->addr3, ETH_ALEN);
  425. memcpy(src, hdr->addr4, ETH_ALEN);
  426. break;
  427. case 0:
  428. memcpy(dst, hdr->addr1, ETH_ALEN);
  429. memcpy(src, hdr->addr2, ETH_ALEN);
  430. break;
  431. }
  432. #ifdef NOT_YET
  433. if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
  434. goto rx_dropped;
  435. if (wds) {
  436. skb->dev = dev = wds;
  437. stats = hostap_get_stats(dev);
  438. }
  439. if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
  440. (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  441. IEEE80211_FCTL_FROMDS && ieee->stadev
  442. && !compare_ether_addr(hdr->addr2, ieee->assoc_ap_addr)) {
  443. /* Frame from BSSID of the AP for which we are a client */
  444. skb->dev = dev = ieee->stadev;
  445. stats = hostap_get_stats(dev);
  446. from_assoc_ap = 1;
  447. }
  448. #endif
  449. dev->last_rx = jiffies;
  450. #ifdef NOT_YET
  451. if ((ieee->iw_mode == IW_MODE_MASTER ||
  452. ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
  453. switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
  454. wds != NULL)) {
  455. case AP_RX_CONTINUE_NOT_AUTHORIZED:
  456. frame_authorized = 0;
  457. break;
  458. case AP_RX_CONTINUE:
  459. frame_authorized = 1;
  460. break;
  461. case AP_RX_DROP:
  462. goto rx_dropped;
  463. case AP_RX_EXIT:
  464. goto rx_exit;
  465. }
  466. }
  467. #endif
  468. /* Nullfunc frames may have PS-bit set, so they must be passed to
  469. * hostap_handle_sta_rx() before being dropped here. */
  470. stype &= ~IEEE80211_STYPE_QOS_DATA;
  471. if (stype != IEEE80211_STYPE_DATA &&
  472. stype != IEEE80211_STYPE_DATA_CFACK &&
  473. stype != IEEE80211_STYPE_DATA_CFPOLL &&
  474. stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
  475. if (stype != IEEE80211_STYPE_NULLFUNC)
  476. IEEE80211_DEBUG_DROP("RX: dropped data frame "
  477. "with no data (type=0x%02x, "
  478. "subtype=0x%02x, len=%d)\n",
  479. type, stype, skb->len);
  480. goto rx_dropped;
  481. }
  482. /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
  483. if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  484. (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
  485. goto rx_dropped;
  486. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  487. /* skb: hdr + (possibly fragmented) plaintext payload */
  488. // PR: FIXME: hostap has additional conditions in the "if" below:
  489. // ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  490. if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
  491. int flen;
  492. struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
  493. IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
  494. if (!frag_skb) {
  495. IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
  496. "Rx cannot get skb from fragment "
  497. "cache (morefrag=%d seq=%u frag=%u)\n",
  498. (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
  499. WLAN_GET_SEQ_SEQ(sc), frag);
  500. goto rx_dropped;
  501. }
  502. flen = skb->len;
  503. if (frag != 0)
  504. flen -= hdrlen;
  505. if (frag_skb->tail + flen > frag_skb->end) {
  506. printk(KERN_WARNING "%s: host decrypted and "
  507. "reassembled frame did not fit skb\n",
  508. dev->name);
  509. ieee80211_frag_cache_invalidate(ieee, hdr);
  510. goto rx_dropped;
  511. }
  512. if (frag == 0) {
  513. /* copy first fragment (including full headers) into
  514. * beginning of the fragment cache skb */
  515. memcpy(skb_put(frag_skb, flen), skb->data, flen);
  516. } else {
  517. /* append frame payload to the end of the fragment
  518. * cache skb */
  519. memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
  520. flen);
  521. }
  522. dev_kfree_skb_any(skb);
  523. skb = NULL;
  524. if (fc & IEEE80211_FCTL_MOREFRAGS) {
  525. /* more fragments expected - leave the skb in fragment
  526. * cache for now; it will be delivered to upper layers
  527. * after all fragments have been received */
  528. goto rx_exit;
  529. }
  530. /* this was the last fragment and the frame will be
  531. * delivered, so remove skb from fragment cache */
  532. skb = frag_skb;
  533. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  534. ieee80211_frag_cache_invalidate(ieee, hdr);
  535. }
  536. /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
  537. * encrypted/authenticated */
  538. if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
  539. ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
  540. goto rx_dropped;
  541. hdr = (struct ieee80211_hdr_4addr *)skb->data;
  542. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
  543. if ( /*ieee->ieee802_1x && */
  544. ieee80211_is_eapol_frame(ieee, skb)) {
  545. /* pass unencrypted EAPOL frames even if encryption is
  546. * configured */
  547. } else {
  548. IEEE80211_DEBUG_DROP("encryption configured, but RX "
  549. "frame not encrypted (SA=" MAC_FMT
  550. ")\n", MAC_ARG(hdr->addr2));
  551. goto rx_dropped;
  552. }
  553. }
  554. if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
  555. !ieee80211_is_eapol_frame(ieee, skb)) {
  556. IEEE80211_DEBUG_DROP("dropped unencrypted RX data "
  557. "frame from " MAC_FMT
  558. " (drop_unencrypted=1)\n",
  559. MAC_ARG(hdr->addr2));
  560. goto rx_dropped;
  561. }
  562. /* skb: hdr + (possible reassembled) full plaintext payload */
  563. payload = skb->data + hdrlen;
  564. ethertype = (payload[6] << 8) | payload[7];
  565. #ifdef NOT_YET
  566. /* If IEEE 802.1X is used, check whether the port is authorized to send
  567. * the received frame. */
  568. if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
  569. if (ethertype == ETH_P_PAE) {
  570. printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
  571. dev->name);
  572. if (ieee->hostapd && ieee->apdev) {
  573. /* Send IEEE 802.1X frames to the user
  574. * space daemon for processing */
  575. prism2_rx_80211(ieee->apdev, skb, rx_stats,
  576. PRISM2_RX_MGMT);
  577. ieee->apdevstats.rx_packets++;
  578. ieee->apdevstats.rx_bytes += skb->len;
  579. goto rx_exit;
  580. }
  581. } else if (!frame_authorized) {
  582. printk(KERN_DEBUG "%s: dropped frame from "
  583. "unauthorized port (IEEE 802.1X): "
  584. "ethertype=0x%04x\n", dev->name, ethertype);
  585. goto rx_dropped;
  586. }
  587. }
  588. #endif
  589. /* convert hdr + possible LLC headers into Ethernet header */
  590. if (skb->len - hdrlen >= 8 &&
  591. ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
  592. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  593. memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
  594. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  595. * replace EtherType */
  596. skb_pull(skb, hdrlen + SNAP_SIZE);
  597. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  598. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  599. } else {
  600. u16 len;
  601. /* Leave Ethernet header part of hdr and full payload */
  602. skb_pull(skb, hdrlen);
  603. len = htons(skb->len);
  604. memcpy(skb_push(skb, 2), &len, 2);
  605. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  606. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  607. }
  608. #ifdef NOT_YET
  609. if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  610. IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
  611. /* Non-standard frame: get addr4 from its bogus location after
  612. * the payload */
  613. memcpy(skb->data + ETH_ALEN,
  614. skb->data + skb->len - ETH_ALEN, ETH_ALEN);
  615. skb_trim(skb, skb->len - ETH_ALEN);
  616. }
  617. #endif
  618. stats->rx_packets++;
  619. stats->rx_bytes += skb->len;
  620. #ifdef NOT_YET
  621. if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
  622. if (dst[0] & 0x01) {
  623. /* copy multicast frame both to the higher layers and
  624. * to the wireless media */
  625. ieee->ap->bridged_multicast++;
  626. skb2 = skb_clone(skb, GFP_ATOMIC);
  627. if (skb2 == NULL)
  628. printk(KERN_DEBUG "%s: skb_clone failed for "
  629. "multicast frame\n", dev->name);
  630. } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
  631. /* send frame directly to the associated STA using
  632. * wireless media and not passing to higher layers */
  633. ieee->ap->bridged_unicast++;
  634. skb2 = skb;
  635. skb = NULL;
  636. }
  637. }
  638. if (skb2 != NULL) {
  639. /* send to wireless media */
  640. skb2->protocol = __constant_htons(ETH_P_802_3);
  641. skb2->mac.raw = skb2->nh.raw = skb2->data;
  642. /* skb2->nh.raw = skb2->data + ETH_HLEN; */
  643. skb2->dev = dev;
  644. dev_queue_xmit(skb2);
  645. }
  646. #endif
  647. if (skb) {
  648. skb->protocol = eth_type_trans(skb, dev);
  649. memset(skb->cb, 0, sizeof(skb->cb));
  650. skb->dev = dev;
  651. skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
  652. netif_rx(skb);
  653. }
  654. rx_exit:
  655. #ifdef NOT_YET
  656. if (sta)
  657. hostap_handle_sta_release(sta);
  658. #endif
  659. return 1;
  660. rx_dropped:
  661. stats->rx_dropped++;
  662. /* Returning 0 indicates to caller that we have not handled the SKB--
  663. * so it is still allocated and can be used again by underlying
  664. * hardware as a DMA target */
  665. return 0;
  666. }
  667. #define MGMT_FRAME_FIXED_PART_LENGTH 0x24
  668. static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
  669. /*
  670. * Make ther structure we read from the beacon packet has
  671. * the right values
  672. */
  673. static int ieee80211_verify_qos_info(struct ieee80211_qos_information_element
  674. *info_element, int sub_type)
  675. {
  676. if (info_element->qui_subtype != sub_type)
  677. return -1;
  678. if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
  679. return -1;
  680. if (info_element->qui_type != QOS_OUI_TYPE)
  681. return -1;
  682. if (info_element->version != QOS_VERSION_1)
  683. return -1;
  684. return 0;
  685. }
  686. /*
  687. * Parse a QoS parameter element
  688. */
  689. static int ieee80211_read_qos_param_element(struct ieee80211_qos_parameter_info
  690. *element_param, struct ieee80211_info_element
  691. *info_element)
  692. {
  693. int ret = 0;
  694. u16 size = sizeof(struct ieee80211_qos_parameter_info) - 2;
  695. if ((info_element == NULL) || (element_param == NULL))
  696. return -1;
  697. if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
  698. memcpy(element_param->info_element.qui, info_element->data,
  699. info_element->len);
  700. element_param->info_element.elementID = info_element->id;
  701. element_param->info_element.length = info_element->len;
  702. } else
  703. ret = -1;
  704. if (ret == 0)
  705. ret = ieee80211_verify_qos_info(&element_param->info_element,
  706. QOS_OUI_PARAM_SUB_TYPE);
  707. return ret;
  708. }
  709. /*
  710. * Parse a QoS information element
  711. */
  712. static int ieee80211_read_qos_info_element(struct
  713. ieee80211_qos_information_element
  714. *element_info, struct ieee80211_info_element
  715. *info_element)
  716. {
  717. int ret = 0;
  718. u16 size = sizeof(struct ieee80211_qos_information_element) - 2;
  719. if (element_info == NULL)
  720. return -1;
  721. if (info_element == NULL)
  722. return -1;
  723. if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
  724. memcpy(element_info->qui, info_element->data,
  725. info_element->len);
  726. element_info->elementID = info_element->id;
  727. element_info->length = info_element->len;
  728. } else
  729. ret = -1;
  730. if (ret == 0)
  731. ret = ieee80211_verify_qos_info(element_info,
  732. QOS_OUI_INFO_SUB_TYPE);
  733. return ret;
  734. }
  735. /*
  736. * Write QoS parameters from the ac parameters.
  737. */
  738. static int ieee80211_qos_convert_ac_to_parameters(struct
  739. ieee80211_qos_parameter_info
  740. *param_elm, struct
  741. ieee80211_qos_parameters
  742. *qos_param)
  743. {
  744. int rc = 0;
  745. int i;
  746. struct ieee80211_qos_ac_parameter *ac_params;
  747. u32 txop;
  748. u8 cw_min;
  749. u8 cw_max;
  750. for (i = 0; i < QOS_QUEUE_NUM; i++) {
  751. ac_params = &(param_elm->ac_params_record[i]);
  752. qos_param->aifs[i] = (ac_params->aci_aifsn) & 0x0F;
  753. qos_param->aifs[i] -= (qos_param->aifs[i] < 2) ? 0 : 2;
  754. cw_min = ac_params->ecw_min_max & 0x0F;
  755. qos_param->cw_min[i] = (u16) ((1 << cw_min) - 1);
  756. cw_max = (ac_params->ecw_min_max & 0xF0) >> 4;
  757. qos_param->cw_max[i] = (u16) ((1 << cw_max) - 1);
  758. qos_param->flag[i] =
  759. (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
  760. txop = le16_to_cpu(ac_params->tx_op_limit) * 32;
  761. qos_param->tx_op_limit[i] = (u16) txop;
  762. }
  763. return rc;
  764. }
  765. /*
  766. * we have a generic data element which it may contain QoS information or
  767. * parameters element. check the information element length to decide
  768. * which type to read
  769. */
  770. static int ieee80211_parse_qos_info_param_IE(struct ieee80211_info_element
  771. *info_element,
  772. struct ieee80211_network *network)
  773. {
  774. int rc = 0;
  775. struct ieee80211_qos_parameters *qos_param = NULL;
  776. struct ieee80211_qos_information_element qos_info_element;
  777. rc = ieee80211_read_qos_info_element(&qos_info_element, info_element);
  778. if (rc == 0) {
  779. network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
  780. network->flags |= NETWORK_HAS_QOS_INFORMATION;
  781. } else {
  782. struct ieee80211_qos_parameter_info param_element;
  783. rc = ieee80211_read_qos_param_element(&param_element,
  784. info_element);
  785. if (rc == 0) {
  786. qos_param = &(network->qos_data.parameters);
  787. ieee80211_qos_convert_ac_to_parameters(&param_element,
  788. qos_param);
  789. network->flags |= NETWORK_HAS_QOS_PARAMETERS;
  790. network->qos_data.param_count =
  791. param_element.info_element.ac_info & 0x0F;
  792. }
  793. }
  794. if (rc == 0) {
  795. IEEE80211_DEBUG_QOS("QoS is supported\n");
  796. network->qos_data.supported = 1;
  797. }
  798. return rc;
  799. }
  800. static int ieee80211_parse_info_param(struct ieee80211_info_element
  801. *info_element, u16 length,
  802. struct ieee80211_network *network)
  803. {
  804. u8 i;
  805. #ifdef CONFIG_IEEE80211_DEBUG
  806. char rates_str[64];
  807. char *p;
  808. #endif
  809. while (length >= sizeof(*info_element)) {
  810. if (sizeof(*info_element) + info_element->len > length) {
  811. IEEE80211_DEBUG_MGMT("Info elem: parse failed: "
  812. "info_element->len + 2 > left : "
  813. "info_element->len+2=%zd left=%d, id=%d.\n",
  814. info_element->len +
  815. sizeof(*info_element),
  816. length, info_element->id);
  817. return 1;
  818. }
  819. switch (info_element->id) {
  820. case MFIE_TYPE_SSID:
  821. if (ieee80211_is_empty_essid(info_element->data,
  822. info_element->len)) {
  823. network->flags |= NETWORK_EMPTY_ESSID;
  824. break;
  825. }
  826. network->ssid_len = min(info_element->len,
  827. (u8) IW_ESSID_MAX_SIZE);
  828. memcpy(network->ssid, info_element->data,
  829. network->ssid_len);
  830. if (network->ssid_len < IW_ESSID_MAX_SIZE)
  831. memset(network->ssid + network->ssid_len, 0,
  832. IW_ESSID_MAX_SIZE - network->ssid_len);
  833. IEEE80211_DEBUG_MGMT("MFIE_TYPE_SSID: '%s' len=%d.\n",
  834. network->ssid, network->ssid_len);
  835. break;
  836. case MFIE_TYPE_RATES:
  837. #ifdef CONFIG_IEEE80211_DEBUG
  838. p = rates_str;
  839. #endif
  840. network->rates_len = min(info_element->len,
  841. MAX_RATES_LENGTH);
  842. for (i = 0; i < network->rates_len; i++) {
  843. network->rates[i] = info_element->data[i];
  844. #ifdef CONFIG_IEEE80211_DEBUG
  845. p += snprintf(p, sizeof(rates_str) -
  846. (p - rates_str), "%02X ",
  847. network->rates[i]);
  848. #endif
  849. if (ieee80211_is_ofdm_rate
  850. (info_element->data[i])) {
  851. network->flags |= NETWORK_HAS_OFDM;
  852. if (info_element->data[i] &
  853. IEEE80211_BASIC_RATE_MASK)
  854. network->flags &=
  855. ~NETWORK_HAS_CCK;
  856. }
  857. }
  858. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES: '%s' (%d)\n",
  859. rates_str, network->rates_len);
  860. break;
  861. case MFIE_TYPE_RATES_EX:
  862. #ifdef CONFIG_IEEE80211_DEBUG
  863. p = rates_str;
  864. #endif
  865. network->rates_ex_len = min(info_element->len,
  866. MAX_RATES_EX_LENGTH);
  867. for (i = 0; i < network->rates_ex_len; i++) {
  868. network->rates_ex[i] = info_element->data[i];
  869. #ifdef CONFIG_IEEE80211_DEBUG
  870. p += snprintf(p, sizeof(rates_str) -
  871. (p - rates_str), "%02X ",
  872. network->rates[i]);
  873. #endif
  874. if (ieee80211_is_ofdm_rate
  875. (info_element->data[i])) {
  876. network->flags |= NETWORK_HAS_OFDM;
  877. if (info_element->data[i] &
  878. IEEE80211_BASIC_RATE_MASK)
  879. network->flags &=
  880. ~NETWORK_HAS_CCK;
  881. }
  882. }
  883. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
  884. rates_str, network->rates_ex_len);
  885. break;
  886. case MFIE_TYPE_DS_SET:
  887. IEEE80211_DEBUG_MGMT("MFIE_TYPE_DS_SET: %d\n",
  888. info_element->data[0]);
  889. network->channel = info_element->data[0];
  890. break;
  891. case MFIE_TYPE_FH_SET:
  892. IEEE80211_DEBUG_MGMT("MFIE_TYPE_FH_SET: ignored\n");
  893. break;
  894. case MFIE_TYPE_CF_SET:
  895. IEEE80211_DEBUG_MGMT("MFIE_TYPE_CF_SET: ignored\n");
  896. break;
  897. case MFIE_TYPE_TIM:
  898. IEEE80211_DEBUG_MGMT("MFIE_TYPE_TIM: ignored\n");
  899. break;
  900. case MFIE_TYPE_ERP_INFO:
  901. network->erp_value = info_element->data[0];
  902. IEEE80211_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
  903. network->erp_value);
  904. break;
  905. case MFIE_TYPE_IBSS_SET:
  906. network->atim_window = info_element->data[0];
  907. IEEE80211_DEBUG_MGMT("MFIE_TYPE_IBSS_SET: %d\n",
  908. network->atim_window);
  909. break;
  910. case MFIE_TYPE_CHALLENGE:
  911. IEEE80211_DEBUG_MGMT("MFIE_TYPE_CHALLENGE: ignored\n");
  912. break;
  913. case MFIE_TYPE_GENERIC:
  914. IEEE80211_DEBUG_MGMT("MFIE_TYPE_GENERIC: %d bytes\n",
  915. info_element->len);
  916. if (!ieee80211_parse_qos_info_param_IE(info_element,
  917. network))
  918. break;
  919. if (info_element->len >= 4 &&
  920. info_element->data[0] == 0x00 &&
  921. info_element->data[1] == 0x50 &&
  922. info_element->data[2] == 0xf2 &&
  923. info_element->data[3] == 0x01) {
  924. network->wpa_ie_len = min(info_element->len + 2,
  925. MAX_WPA_IE_LEN);
  926. memcpy(network->wpa_ie, info_element,
  927. network->wpa_ie_len);
  928. }
  929. break;
  930. case MFIE_TYPE_RSN:
  931. IEEE80211_DEBUG_MGMT("MFIE_TYPE_RSN: %d bytes\n",
  932. info_element->len);
  933. network->rsn_ie_len = min(info_element->len + 2,
  934. MAX_WPA_IE_LEN);
  935. memcpy(network->rsn_ie, info_element,
  936. network->rsn_ie_len);
  937. break;
  938. case MFIE_TYPE_QOS_PARAMETER:
  939. printk(KERN_ERR
  940. "QoS Error need to parse QOS_PARAMETER IE\n");
  941. break;
  942. default:
  943. IEEE80211_DEBUG_MGMT("unsupported IE %d\n",
  944. info_element->id);
  945. break;
  946. }
  947. length -= sizeof(*info_element) + info_element->len;
  948. info_element =
  949. (struct ieee80211_info_element *)&info_element->
  950. data[info_element->len];
  951. }
  952. return 0;
  953. }
  954. static int ieee80211_handle_assoc_resp(struct ieee80211_device *ieee, struct ieee80211_assoc_response
  955. *frame, struct ieee80211_rx_stats *stats)
  956. {
  957. struct ieee80211_network network_resp;
  958. struct ieee80211_network *network = &network_resp;
  959. struct net_device *dev = ieee->dev;
  960. network->flags = 0;
  961. network->qos_data.active = 0;
  962. network->qos_data.supported = 0;
  963. network->qos_data.param_count = 0;
  964. network->qos_data.old_param_count = 0;
  965. //network->atim_window = le16_to_cpu(frame->aid) & (0x3FFF);
  966. network->atim_window = le16_to_cpu(frame->aid);
  967. network->listen_interval = le16_to_cpu(frame->status);
  968. memcpy(network->bssid, frame->header.addr3, ETH_ALEN);
  969. network->capability = le16_to_cpu(frame->capability);
  970. network->last_scanned = jiffies;
  971. network->rates_len = network->rates_ex_len = 0;
  972. network->last_associate = 0;
  973. network->ssid_len = 0;
  974. network->erp_value =
  975. (network->capability & WLAN_CAPABILITY_IBSS) ? 0x3 : 0x0;
  976. if (stats->freq == IEEE80211_52GHZ_BAND) {
  977. /* for A band (No DS info) */
  978. network->channel = stats->received_channel;
  979. } else
  980. network->flags |= NETWORK_HAS_CCK;
  981. network->wpa_ie_len = 0;
  982. network->rsn_ie_len = 0;
  983. if (ieee80211_parse_info_param
  984. (frame->info_element, stats->len - sizeof(*frame), network))
  985. return 1;
  986. network->mode = 0;
  987. if (stats->freq == IEEE80211_52GHZ_BAND)
  988. network->mode = IEEE_A;
  989. else {
  990. if (network->flags & NETWORK_HAS_OFDM)
  991. network->mode |= IEEE_G;
  992. if (network->flags & NETWORK_HAS_CCK)
  993. network->mode |= IEEE_B;
  994. }
  995. if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
  996. network->flags |= NETWORK_EMPTY_ESSID;
  997. memcpy(&network->stats, stats, sizeof(network->stats));
  998. if (ieee->handle_assoc_response != NULL)
  999. ieee->handle_assoc_response(dev, frame, network);
  1000. return 0;
  1001. }
  1002. /***************************************************/
  1003. static int ieee80211_network_init(struct ieee80211_device *ieee, struct ieee80211_probe_response
  1004. *beacon,
  1005. struct ieee80211_network *network,
  1006. struct ieee80211_rx_stats *stats)
  1007. {
  1008. network->qos_data.active = 0;
  1009. network->qos_data.supported = 0;
  1010. network->qos_data.param_count = 0;
  1011. network->qos_data.old_param_count = 0;
  1012. /* Pull out fixed field data */
  1013. memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
  1014. network->capability = le16_to_cpu(beacon->capability);
  1015. network->last_scanned = jiffies;
  1016. network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
  1017. network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
  1018. network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
  1019. /* Where to pull this? beacon->listen_interval; */
  1020. network->listen_interval = 0x0A;
  1021. network->rates_len = network->rates_ex_len = 0;
  1022. network->last_associate = 0;
  1023. network->ssid_len = 0;
  1024. network->flags = 0;
  1025. network->atim_window = 0;
  1026. network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
  1027. 0x3 : 0x0;
  1028. if (stats->freq == IEEE80211_52GHZ_BAND) {
  1029. /* for A band (No DS info) */
  1030. network->channel = stats->received_channel;
  1031. } else
  1032. network->flags |= NETWORK_HAS_CCK;
  1033. network->wpa_ie_len = 0;
  1034. network->rsn_ie_len = 0;
  1035. if (ieee80211_parse_info_param
  1036. (beacon->info_element, stats->len - sizeof(*beacon), network))
  1037. return 1;
  1038. network->mode = 0;
  1039. if (stats->freq == IEEE80211_52GHZ_BAND)
  1040. network->mode = IEEE_A;
  1041. else {
  1042. if (network->flags & NETWORK_HAS_OFDM)
  1043. network->mode |= IEEE_G;
  1044. if (network->flags & NETWORK_HAS_CCK)
  1045. network->mode |= IEEE_B;
  1046. }
  1047. if (network->mode == 0) {
  1048. IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
  1049. "network.\n",
  1050. escape_essid(network->ssid,
  1051. network->ssid_len),
  1052. MAC_ARG(network->bssid));
  1053. return 1;
  1054. }
  1055. if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
  1056. network->flags |= NETWORK_EMPTY_ESSID;
  1057. memcpy(&network->stats, stats, sizeof(network->stats));
  1058. return 0;
  1059. }
  1060. static inline int is_same_network(struct ieee80211_network *src,
  1061. struct ieee80211_network *dst)
  1062. {
  1063. /* A network is only a duplicate if the channel, BSSID, and ESSID
  1064. * all match. We treat all <hidden> with the same BSSID and channel
  1065. * as one network */
  1066. return ((src->ssid_len == dst->ssid_len) &&
  1067. (src->channel == dst->channel) &&
  1068. !compare_ether_addr(src->bssid, dst->bssid) &&
  1069. !memcmp(src->ssid, dst->ssid, src->ssid_len));
  1070. }
  1071. static void update_network(struct ieee80211_network *dst,
  1072. struct ieee80211_network *src)
  1073. {
  1074. int qos_active;
  1075. u8 old_param;
  1076. memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
  1077. dst->capability = src->capability;
  1078. memcpy(dst->rates, src->rates, src->rates_len);
  1079. dst->rates_len = src->rates_len;
  1080. memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
  1081. dst->rates_ex_len = src->rates_ex_len;
  1082. dst->mode = src->mode;
  1083. dst->flags = src->flags;
  1084. dst->time_stamp[0] = src->time_stamp[0];
  1085. dst->time_stamp[1] = src->time_stamp[1];
  1086. dst->beacon_interval = src->beacon_interval;
  1087. dst->listen_interval = src->listen_interval;
  1088. dst->atim_window = src->atim_window;
  1089. dst->erp_value = src->erp_value;
  1090. memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
  1091. dst->wpa_ie_len = src->wpa_ie_len;
  1092. memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
  1093. dst->rsn_ie_len = src->rsn_ie_len;
  1094. dst->last_scanned = jiffies;
  1095. qos_active = src->qos_data.active;
  1096. old_param = dst->qos_data.old_param_count;
  1097. if (dst->flags & NETWORK_HAS_QOS_MASK)
  1098. memcpy(&dst->qos_data, &src->qos_data,
  1099. sizeof(struct ieee80211_qos_data));
  1100. else {
  1101. dst->qos_data.supported = src->qos_data.supported;
  1102. dst->qos_data.param_count = src->qos_data.param_count;
  1103. }
  1104. if (dst->qos_data.supported == 1) {
  1105. if (dst->ssid_len)
  1106. IEEE80211_DEBUG_QOS
  1107. ("QoS the network %s is QoS supported\n",
  1108. dst->ssid);
  1109. else
  1110. IEEE80211_DEBUG_QOS
  1111. ("QoS the network is QoS supported\n");
  1112. }
  1113. dst->qos_data.active = qos_active;
  1114. dst->qos_data.old_param_count = old_param;
  1115. /* dst->last_associate is not overwritten */
  1116. }
  1117. static inline int is_beacon(int fc)
  1118. {
  1119. return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
  1120. }
  1121. static void ieee80211_process_probe_response(struct ieee80211_device
  1122. *ieee, struct
  1123. ieee80211_probe_response
  1124. *beacon, struct ieee80211_rx_stats
  1125. *stats)
  1126. {
  1127. struct net_device *dev = ieee->dev;
  1128. struct ieee80211_network network;
  1129. struct ieee80211_network *target;
  1130. struct ieee80211_network *oldest = NULL;
  1131. #ifdef CONFIG_IEEE80211_DEBUG
  1132. struct ieee80211_info_element *info_element = beacon->info_element;
  1133. #endif
  1134. unsigned long flags;
  1135. IEEE80211_DEBUG_SCAN("'%s' (" MAC_FMT
  1136. "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
  1137. escape_essid(info_element->data,
  1138. info_element->len),
  1139. MAC_ARG(beacon->header.addr3),
  1140. (beacon->capability & (1 << 0xf)) ? '1' : '0',
  1141. (beacon->capability & (1 << 0xe)) ? '1' : '0',
  1142. (beacon->capability & (1 << 0xd)) ? '1' : '0',
  1143. (beacon->capability & (1 << 0xc)) ? '1' : '0',
  1144. (beacon->capability & (1 << 0xb)) ? '1' : '0',
  1145. (beacon->capability & (1 << 0xa)) ? '1' : '0',
  1146. (beacon->capability & (1 << 0x9)) ? '1' : '0',
  1147. (beacon->capability & (1 << 0x8)) ? '1' : '0',
  1148. (beacon->capability & (1 << 0x7)) ? '1' : '0',
  1149. (beacon->capability & (1 << 0x6)) ? '1' : '0',
  1150. (beacon->capability & (1 << 0x5)) ? '1' : '0',
  1151. (beacon->capability & (1 << 0x4)) ? '1' : '0',
  1152. (beacon->capability & (1 << 0x3)) ? '1' : '0',
  1153. (beacon->capability & (1 << 0x2)) ? '1' : '0',
  1154. (beacon->capability & (1 << 0x1)) ? '1' : '0',
  1155. (beacon->capability & (1 << 0x0)) ? '1' : '0');
  1156. if (ieee80211_network_init(ieee, beacon, &network, stats)) {
  1157. IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
  1158. escape_essid(info_element->data,
  1159. info_element->len),
  1160. MAC_ARG(beacon->header.addr3),
  1161. is_beacon(le16_to_cpu
  1162. (beacon->header.
  1163. frame_ctl)) ?
  1164. "BEACON" : "PROBE RESPONSE");
  1165. return;
  1166. }
  1167. /* The network parsed correctly -- so now we scan our known networks
  1168. * to see if we can find it in our list.
  1169. *
  1170. * NOTE: This search is definitely not optimized. Once its doing
  1171. * the "right thing" we'll optimize it for efficiency if
  1172. * necessary */
  1173. /* Search for this entry in the list and update it if it is
  1174. * already there. */
  1175. spin_lock_irqsave(&ieee->lock, flags);
  1176. list_for_each_entry(target, &ieee->network_list, list) {
  1177. if (is_same_network(target, &network))
  1178. break;
  1179. if ((oldest == NULL) ||
  1180. (target->last_scanned < oldest->last_scanned))
  1181. oldest = target;
  1182. }
  1183. /* If we didn't find a match, then get a new network slot to initialize
  1184. * with this beacon's information */
  1185. if (&target->list == &ieee->network_list) {
  1186. if (list_empty(&ieee->network_free_list)) {
  1187. /* If there are no more slots, expire the oldest */
  1188. list_del(&oldest->list);
  1189. target = oldest;
  1190. IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
  1191. "network list.\n",
  1192. escape_essid(target->ssid,
  1193. target->ssid_len),
  1194. MAC_ARG(target->bssid));
  1195. } else {
  1196. /* Otherwise just pull from the free list */
  1197. target = list_entry(ieee->network_free_list.next,
  1198. struct ieee80211_network, list);
  1199. list_del(ieee->network_free_list.next);
  1200. }
  1201. #ifdef CONFIG_IEEE80211_DEBUG
  1202. IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
  1203. escape_essid(network.ssid,
  1204. network.ssid_len),
  1205. MAC_ARG(network.bssid),
  1206. is_beacon(le16_to_cpu
  1207. (beacon->header.
  1208. frame_ctl)) ?
  1209. "BEACON" : "PROBE RESPONSE");
  1210. #endif
  1211. memcpy(target, &network, sizeof(*target));
  1212. list_add_tail(&target->list, &ieee->network_list);
  1213. } else {
  1214. IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
  1215. escape_essid(target->ssid,
  1216. target->ssid_len),
  1217. MAC_ARG(target->bssid),
  1218. is_beacon(le16_to_cpu
  1219. (beacon->header.
  1220. frame_ctl)) ?
  1221. "BEACON" : "PROBE RESPONSE");
  1222. update_network(target, &network);
  1223. }
  1224. spin_unlock_irqrestore(&ieee->lock, flags);
  1225. if (is_beacon(le16_to_cpu(beacon->header.frame_ctl))) {
  1226. if (ieee->handle_beacon != NULL)
  1227. ieee->handle_beacon(dev, beacon, &network);
  1228. } else {
  1229. if (ieee->handle_probe_response != NULL)
  1230. ieee->handle_probe_response(dev, beacon, &network);
  1231. }
  1232. }
  1233. void ieee80211_rx_mgt(struct ieee80211_device *ieee,
  1234. struct ieee80211_hdr_4addr *header,
  1235. struct ieee80211_rx_stats *stats)
  1236. {
  1237. switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
  1238. case IEEE80211_STYPE_ASSOC_RESP:
  1239. IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
  1240. WLAN_FC_GET_STYPE(le16_to_cpu
  1241. (header->frame_ctl)));
  1242. ieee80211_handle_assoc_resp(ieee,
  1243. (struct ieee80211_assoc_response *)
  1244. header, stats);
  1245. break;
  1246. case IEEE80211_STYPE_REASSOC_RESP:
  1247. IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
  1248. WLAN_FC_GET_STYPE(le16_to_cpu
  1249. (header->frame_ctl)));
  1250. break;
  1251. case IEEE80211_STYPE_PROBE_REQ:
  1252. IEEE80211_DEBUG_MGMT("recieved auth (%d)\n",
  1253. WLAN_FC_GET_STYPE(le16_to_cpu
  1254. (header->frame_ctl)));
  1255. if (ieee->handle_probe_request != NULL)
  1256. ieee->handle_probe_request(ieee->dev,
  1257. (struct
  1258. ieee80211_probe_request *)
  1259. header, stats);
  1260. break;
  1261. case IEEE80211_STYPE_PROBE_RESP:
  1262. IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
  1263. WLAN_FC_GET_STYPE(le16_to_cpu
  1264. (header->frame_ctl)));
  1265. IEEE80211_DEBUG_SCAN("Probe response\n");
  1266. ieee80211_process_probe_response(ieee,
  1267. (struct
  1268. ieee80211_probe_response *)
  1269. header, stats);
  1270. break;
  1271. case IEEE80211_STYPE_BEACON:
  1272. IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
  1273. WLAN_FC_GET_STYPE(le16_to_cpu
  1274. (header->frame_ctl)));
  1275. IEEE80211_DEBUG_SCAN("Beacon\n");
  1276. ieee80211_process_probe_response(ieee,
  1277. (struct
  1278. ieee80211_probe_response *)
  1279. header, stats);
  1280. break;
  1281. case IEEE80211_STYPE_AUTH:
  1282. IEEE80211_DEBUG_MGMT("recieved auth (%d)\n",
  1283. WLAN_FC_GET_STYPE(le16_to_cpu
  1284. (header->frame_ctl)));
  1285. if (ieee->handle_auth != NULL)
  1286. ieee->handle_auth(ieee->dev,
  1287. (struct ieee80211_auth *)header);
  1288. break;
  1289. case IEEE80211_STYPE_DISASSOC:
  1290. if (ieee->handle_disassoc != NULL)
  1291. ieee->handle_disassoc(ieee->dev,
  1292. (struct ieee80211_disassoc *)
  1293. header);
  1294. break;
  1295. case IEEE80211_STYPE_DEAUTH:
  1296. printk("DEAUTH from AP\n");
  1297. if (ieee->handle_deauth != NULL)
  1298. ieee->handle_deauth(ieee->dev, (struct ieee80211_auth *)
  1299. header);
  1300. break;
  1301. default:
  1302. IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
  1303. WLAN_FC_GET_STYPE(le16_to_cpu
  1304. (header->frame_ctl)));
  1305. IEEE80211_WARNING("%s: Unknown management packet: %d\n",
  1306. ieee->dev->name,
  1307. WLAN_FC_GET_STYPE(le16_to_cpu
  1308. (header->frame_ctl)));
  1309. break;
  1310. }
  1311. }
  1312. EXPORT_SYMBOL(ieee80211_rx_mgt);
  1313. EXPORT_SYMBOL(ieee80211_rx);