ieee80211_rx.c 44 KB

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