libertas: remove unused 11d code
[safe/jmp/linux-2.6] / drivers / net / wireless / libertas / scan.c
1 /**
2   * Functions implementing wlan scan IOCTL and firmware command APIs
3   *
4   * IOCTL handlers as well as command preperation and response routines
5   *  for sending scan commands to the firmware.
6   */
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_arp.h>
11 #include <asm/unaligned.h>
12 #include <net/lib80211.h>
13
14 #include "host.h"
15 #include "decl.h"
16 #include "dev.h"
17 #include "scan.h"
18 #include "cmd.h"
19
20 //! Approximate amount of data needed to pass a scan result back to iwlist
21 #define MAX_SCAN_CELL_SIZE  (IW_EV_ADDR_LEN             \
22                              + IW_ESSID_MAX_SIZE        \
23                              + IW_EV_UINT_LEN           \
24                              + IW_EV_FREQ_LEN           \
25                              + IW_EV_QUAL_LEN           \
26                              + IW_ESSID_MAX_SIZE        \
27                              + IW_EV_PARAM_LEN          \
28                              + 40)      /* 40 for WPAIE */
29
30 //! Memory needed to store a max sized channel List TLV for a firmware scan
31 #define CHAN_TLV_MAX_SIZE  (sizeof(struct mrvl_ie_header)    \
32                             + (MRVDRV_MAX_CHANNELS_PER_SCAN     \
33                                * sizeof(struct chanscanparamset)))
34
35 //! Memory needed to store a max number/size SSID TLV for a firmware scan
36 #define SSID_TLV_MAX_SIZE  (1 * sizeof(struct mrvl_ie_ssid_param_set))
37
38 //! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
39 #define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan)   \
40                             + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
41
42 //! The maximum number of channels the firmware can scan per command
43 #define MRVDRV_MAX_CHANNELS_PER_SCAN   14
44
45 /**
46  * @brief Number of channels to scan per firmware scan command issuance.
47  *
48  *  Number restricted to prevent hitting the limit on the amount of scan data
49  *  returned in a single firmware scan command.
50  */
51 #define MRVDRV_CHANNELS_PER_SCAN_CMD   4
52
53 //! Scan time specified in the channel TLV for each channel for passive scans
54 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME  100
55
56 //! Scan time specified in the channel TLV for each channel for active scans
57 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME   100
58
59 #define DEFAULT_MAX_SCAN_AGE (15 * HZ)
60
61 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
62                               struct cmd_header *resp);
63
64 /*********************************************************************/
65 /*                                                                   */
66 /*  Misc helper functions                                            */
67 /*                                                                   */
68 /*********************************************************************/
69
70 /**
71  *  @brief Unsets the MSB on basic rates
72  *
73  * Scan through an array and unset the MSB for basic data rates.
74  *
75  *  @param rates     buffer of data rates
76  *  @param len       size of buffer
77  */
78 static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
79 {
80         int i;
81
82         for (i = 0; i < len; i++)
83                 rates[i] &= 0x7f;
84 }
85
86
87 static inline void clear_bss_descriptor(struct bss_descriptor *bss)
88 {
89         /* Don't blow away ->list, just BSS data */
90         memset(bss, 0, offsetof(struct bss_descriptor, list));
91 }
92
93 /**
94  *  @brief Compare two SSIDs
95  *
96  *  @param ssid1    A pointer to ssid to compare
97  *  @param ssid2    A pointer to ssid to compare
98  *
99  *  @return         0: ssid is same, otherwise is different
100  */
101 int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
102                  uint8_t ssid2_len)
103 {
104         if (ssid1_len != ssid2_len)
105                 return -1;
106
107         return memcmp(ssid1, ssid2, ssid1_len);
108 }
109
110 static inline int is_same_network(struct bss_descriptor *src,
111                                   struct bss_descriptor *dst)
112 {
113         /* A network is only a duplicate if the channel, BSSID, and ESSID
114          * all match.  We treat all <hidden> with the same BSSID and channel
115          * as one network */
116         return ((src->ssid_len == dst->ssid_len) &&
117                 (src->channel == dst->channel) &&
118                 !compare_ether_addr(src->bssid, dst->bssid) &&
119                 !memcmp(src->ssid, dst->ssid, src->ssid_len));
120 }
121
122
123
124
125 /*********************************************************************/
126 /*                                                                   */
127 /*  Main scanning support                                            */
128 /*                                                                   */
129 /*********************************************************************/
130
131 /**
132  *  @brief Create a channel list for the driver to scan based on region info
133  *
134  *  Only used from lbs_scan_setup_scan_config()
135  *
136  *  Use the driver region/band information to construct a comprehensive list
137  *    of channels to scan.  This routine is used for any scan that is not
138  *    provided a specific channel list to scan.
139  *
140  *  @param priv          A pointer to struct lbs_private structure
141  *  @param scanchanlist  Output parameter: resulting channel list to scan
142  *
143  *  @return              void
144  */
145 static int lbs_scan_create_channel_list(struct lbs_private *priv,
146                                         struct chanscanparamset *scanchanlist)
147 {
148         struct region_channel *scanregion;
149         struct chan_freq_power *cfp;
150         int rgnidx;
151         int chanidx;
152         int nextchan;
153         uint8_t scantype;
154
155         chanidx = 0;
156
157         /* Set the default scan type to the user specified type, will later
158          *   be changed to passive on a per channel basis if restricted by
159          *   regulatory requirements (11d or 11h)
160          */
161         scantype = CMD_SCAN_TYPE_ACTIVE;
162
163         for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
164                 if (!priv->region_channel[rgnidx].valid)
165                         continue;
166                 scanregion = &priv->region_channel[rgnidx];
167
168                 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
169                         struct chanscanparamset *chan = &scanchanlist[chanidx];
170
171                         cfp = scanregion->CFP + nextchan;
172
173                         if (scanregion->band == BAND_B || scanregion->band == BAND_G)
174                                 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
175
176                         if (scantype == CMD_SCAN_TYPE_PASSIVE) {
177                                 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
178                                 chan->chanscanmode.passivescan = 1;
179                         } else {
180                                 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
181                                 chan->chanscanmode.passivescan = 0;
182                         }
183
184                         chan->channumber = cfp->channel;
185                 }
186         }
187         return chanidx;
188 }
189
190 /*
191  * Add SSID TLV of the form:
192  *
193  * TLV-ID SSID     00 00
194  * length          06 00
195  * ssid            4d 4e 54 45 53 54
196  */
197 static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
198 {
199         struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
200
201         ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
202         ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
203         memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
204         return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
205 }
206
207 /*
208  * Add CHANLIST TLV of the form
209  *
210  * TLV-ID CHANLIST 01 01
211  * length          5b 00
212  * channel 1       00 01 00 00 00 64 00
213  *   radio type    00
214  *   channel          01
215  *   scan type           00
216  *   min scan time          00 00
217  *   max scan time                64 00
218  * channel 2       00 02 00 00 00 64 00
219  * channel 3       00 03 00 00 00 64 00
220  * channel 4       00 04 00 00 00 64 00
221  * channel 5       00 05 00 00 00 64 00
222  * channel 6       00 06 00 00 00 64 00
223  * channel 7       00 07 00 00 00 64 00
224  * channel 8       00 08 00 00 00 64 00
225  * channel 9       00 09 00 00 00 64 00
226  * channel 10      00 0a 00 00 00 64 00
227  * channel 11      00 0b 00 00 00 64 00
228  * channel 12      00 0c 00 00 00 64 00
229  * channel 13      00 0d 00 00 00 64 00
230  *
231  */
232 static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
233                                      struct chanscanparamset *chan_list,
234                                      int chan_count)
235 {
236         size_t size = sizeof(struct chanscanparamset) *chan_count;
237         struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
238
239         chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
240         memcpy(chan_tlv->chanscanparam, chan_list, size);
241         chan_tlv->header.len = cpu_to_le16(size);
242         return sizeof(chan_tlv->header) + size;
243 }
244
245 /*
246  * Add RATES TLV of the form
247  *
248  * TLV-ID RATES    01 00
249  * length          0e 00
250  * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c
251  *
252  * The rates are in lbs_bg_rates[], but for the 802.11b
253  * rates the high bit isn't set.
254  */
255 static int lbs_scan_add_rates_tlv(uint8_t *tlv)
256 {
257         int i;
258         struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
259
260         rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261         tlv += sizeof(rate_tlv->header);
262         for (i = 0; i < MAX_RATES; i++) {
263                 *tlv = lbs_bg_rates[i];
264                 if (*tlv == 0)
265                         break;
266                 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
267                    MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
268                    Note that the values are MBit/s * 2, to mark them as
269                    basic rates so that the firmware likes it better */
270                 if (*tlv == 0x02 || *tlv == 0x04 ||
271                     *tlv == 0x0b || *tlv == 0x16)
272                         *tlv |= 0x80;
273                 tlv++;
274         }
275         rate_tlv->header.len = cpu_to_le16(i);
276         return sizeof(rate_tlv->header) + i;
277 }
278
279 /*
280  * Generate the CMD_802_11_SCAN command with the proper tlv
281  * for a bunch of channels.
282  */
283 static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
284                        struct chanscanparamset *chan_list, int chan_count)
285 {
286         int ret = -ENOMEM;
287         struct cmd_ds_802_11_scan *scan_cmd;
288         uint8_t *tlv;   /* pointer into our current, growing TLV storage area */
289
290         lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
291                 bsstype, chan_list ? chan_list[0].channumber : -1,
292                 chan_count);
293
294         /* create the fixed part for scan command */
295         scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
296         if (scan_cmd == NULL)
297                 goto out;
298
299         tlv = scan_cmd->tlvbuffer;
300         /* TODO: do we need to scan for a specific BSSID?
301         memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
302         scan_cmd->bsstype = bsstype;
303
304         /* add TLVs */
305         if (priv->scan_ssid_len)
306                 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
307         if (chan_list && chan_count)
308                 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
309         tlv += lbs_scan_add_rates_tlv(tlv);
310
311         /* This is the final data we are about to send */
312         scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
313         lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
314                     sizeof(*scan_cmd));
315         lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
316                     tlv - scan_cmd->tlvbuffer);
317
318         ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
319                         le16_to_cpu(scan_cmd->hdr.size),
320                         lbs_ret_80211_scan, 0);
321
322 out:
323         kfree(scan_cmd);
324         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
325         return ret;
326 }
327
328 /**
329  *  @brief Internal function used to start a scan based on an input config
330  *
331  *  Use the input user scan configuration information when provided in
332  *    order to send the appropriate scan commands to firmware to populate or
333  *    update the internal driver scan table
334  *
335  *  @param priv          A pointer to struct lbs_private structure
336  *  @param full_scan     Do a full-scan (blocking)
337  *
338  *  @return              0 or < 0 if error
339  */
340 int lbs_scan_networks(struct lbs_private *priv, int full_scan)
341 {
342         int ret = -ENOMEM;
343         struct chanscanparamset *chan_list;
344         struct chanscanparamset *curr_chans;
345         int chan_count;
346         uint8_t bsstype = CMD_BSS_TYPE_ANY;
347         int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
348         union iwreq_data wrqu;
349 #ifdef CONFIG_LIBERTAS_DEBUG
350         struct bss_descriptor *iter;
351         int i = 0;
352         DECLARE_SSID_BUF(ssid);
353 #endif
354
355         lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
356
357         /* Cancel any partial outstanding partial scans if this scan
358          * is a full scan.
359          */
360         if (full_scan && delayed_work_pending(&priv->scan_work))
361                 cancel_delayed_work(&priv->scan_work);
362
363         /* User-specified bsstype or channel list
364         TODO: this can be implemented if some user-space application
365         need the feature. Formerly, it was accessible from debugfs,
366         but then nowhere used.
367         if (user_cfg) {
368                 if (user_cfg->bsstype)
369                 bsstype = user_cfg->bsstype;
370         } */
371
372         lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
373
374         /* Create list of channels to scan */
375         chan_list = kzalloc(sizeof(struct chanscanparamset) *
376                             LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
377         if (!chan_list) {
378                 lbs_pr_alert("SCAN: chan_list empty\n");
379                 goto out;
380         }
381
382         /* We want to scan all channels */
383         chan_count = lbs_scan_create_channel_list(priv, chan_list);
384
385         netif_stop_queue(priv->dev);
386         netif_carrier_off(priv->dev);
387         if (priv->mesh_dev) {
388                 netif_stop_queue(priv->mesh_dev);
389                 netif_carrier_off(priv->mesh_dev);
390         }
391
392         /* Prepare to continue an interrupted scan */
393         lbs_deb_scan("chan_count %d, scan_channel %d\n",
394                      chan_count, priv->scan_channel);
395         curr_chans = chan_list;
396         /* advance channel list by already-scanned-channels */
397         if (priv->scan_channel > 0) {
398                 curr_chans += priv->scan_channel;
399                 chan_count -= priv->scan_channel;
400         }
401
402         /* Send scan command(s)
403          * numchannels contains the number of channels we should maximally scan
404          * chan_count is the total number of channels to scan
405          */
406
407         while (chan_count) {
408                 int to_scan = min(numchannels, chan_count);
409                 lbs_deb_scan("scanning %d of %d channels\n",
410                              to_scan, chan_count);
411                 ret = lbs_do_scan(priv, bsstype, curr_chans,
412                                   to_scan);
413                 if (ret) {
414                         lbs_pr_err("SCAN_CMD failed\n");
415                         goto out2;
416                 }
417                 curr_chans += to_scan;
418                 chan_count -= to_scan;
419
420                 /* somehow schedule the next part of the scan */
421                 if (chan_count && !full_scan &&
422                     !priv->surpriseremoved) {
423                         /* -1 marks just that we're currently scanning */
424                         if (priv->scan_channel < 0)
425                                 priv->scan_channel = to_scan;
426                         else
427                                 priv->scan_channel += to_scan;
428                         cancel_delayed_work(&priv->scan_work);
429                         queue_delayed_work(priv->work_thread, &priv->scan_work,
430                                            msecs_to_jiffies(300));
431                         /* skip over GIWSCAN event */
432                         goto out;
433                 }
434
435         }
436         memset(&wrqu, 0, sizeof(union iwreq_data));
437         wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
438
439 #ifdef CONFIG_LIBERTAS_DEBUG
440         /* Dump the scan table */
441         mutex_lock(&priv->lock);
442         lbs_deb_scan("scan table:\n");
443         list_for_each_entry(iter, &priv->network_list, list)
444                 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
445                              i++, iter->bssid, iter->rssi,
446                              print_ssid(ssid, iter->ssid, iter->ssid_len));
447         mutex_unlock(&priv->lock);
448 #endif
449
450 out2:
451         priv->scan_channel = 0;
452
453 out:
454         if (priv->connect_status == LBS_CONNECTED) {
455                 netif_carrier_on(priv->dev);
456                 if (!priv->tx_pending_len)
457                         netif_wake_queue(priv->dev);
458         }
459         if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
460                 netif_carrier_on(priv->mesh_dev);
461                 if (!priv->tx_pending_len)
462                         netif_wake_queue(priv->mesh_dev);
463         }
464         kfree(chan_list);
465
466         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
467         return ret;
468 }
469
470 void lbs_scan_worker(struct work_struct *work)
471 {
472         struct lbs_private *priv =
473                 container_of(work, struct lbs_private, scan_work.work);
474
475         lbs_deb_enter(LBS_DEB_SCAN);
476         lbs_scan_networks(priv, 0);
477         lbs_deb_leave(LBS_DEB_SCAN);
478 }
479
480
481 /*********************************************************************/
482 /*                                                                   */
483 /*  Result interpretation                                            */
484 /*                                                                   */
485 /*********************************************************************/
486
487 /**
488  *  @brief Interpret a BSS scan response returned from the firmware
489  *
490  *  Parse the various fixed fields and IEs passed back for a a BSS probe
491  *  response or beacon from the scan command.  Record information as needed
492  *  in the scan table struct bss_descriptor for that entry.
493  *
494  *  @param bss  Output parameter: Pointer to the BSS Entry
495  *
496  *  @return             0 or -1
497  */
498 static int lbs_process_bss(struct bss_descriptor *bss,
499                            uint8_t **pbeaconinfo, int *bytesleft)
500 {
501         struct ieee_ie_fh_param_set *fh;
502         struct ieee_ie_ds_param_set *ds;
503         struct ieee_ie_cf_param_set *cf;
504         struct ieee_ie_ibss_param_set *ibss;
505         DECLARE_SSID_BUF(ssid);
506         struct ieee_ie_country_info_set *pcountryinfo;
507         uint8_t *pos, *end, *p;
508         uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
509         uint16_t beaconsize = 0;
510         int ret;
511
512         lbs_deb_enter(LBS_DEB_SCAN);
513
514         if (*bytesleft >= sizeof(beaconsize)) {
515                 /* Extract & convert beacon size from the command buffer */
516                 beaconsize = get_unaligned_le16(*pbeaconinfo);
517                 *bytesleft -= sizeof(beaconsize);
518                 *pbeaconinfo += sizeof(beaconsize);
519         }
520
521         if (beaconsize == 0 || beaconsize > *bytesleft) {
522                 *pbeaconinfo += *bytesleft;
523                 *bytesleft = 0;
524                 ret = -1;
525                 goto done;
526         }
527
528         /* Initialize the current working beacon pointer for this BSS iteration */
529         pos = *pbeaconinfo;
530         end = pos + beaconsize;
531
532         /* Advance the return beacon pointer past the current beacon */
533         *pbeaconinfo += beaconsize;
534         *bytesleft -= beaconsize;
535
536         memcpy(bss->bssid, pos, ETH_ALEN);
537         lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
538         pos += ETH_ALEN;
539
540         if ((end - pos) < 12) {
541                 lbs_deb_scan("process_bss: Not enough bytes left\n");
542                 ret = -1;
543                 goto done;
544         }
545
546         /*
547          * next 4 fields are RSSI, time stamp, beacon interval,
548          *   and capability information
549          */
550
551         /* RSSI is 1 byte long */
552         bss->rssi = *pos;
553         lbs_deb_scan("process_bss: RSSI %d\n", *pos);
554         pos++;
555
556         /* time stamp is 8 bytes long */
557         pos += 8;
558
559         /* beacon interval is 2 bytes long */
560         bss->beaconperiod = get_unaligned_le16(pos);
561         pos += 2;
562
563         /* capability information is 2 bytes long */
564         bss->capability = get_unaligned_le16(pos);
565         lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
566         pos += 2;
567
568         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
569                 lbs_deb_scan("process_bss: WEP enabled\n");
570         if (bss->capability & WLAN_CAPABILITY_IBSS)
571                 bss->mode = IW_MODE_ADHOC;
572         else
573                 bss->mode = IW_MODE_INFRA;
574
575         /* rest of the current buffer are IE's */
576         lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
577         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
578
579         /* process variable IE */
580         while (pos <= end - 2) {
581                 if (pos + pos[1] > end) {
582                         lbs_deb_scan("process_bss: error in processing IE, "
583                                      "bytes left < IE length\n");
584                         break;
585                 }
586
587                 switch (pos[0]) {
588                 case WLAN_EID_SSID:
589                         bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
590                         memcpy(bss->ssid, pos + 2, bss->ssid_len);
591                         lbs_deb_scan("got SSID IE: '%s', len %u\n",
592                                      print_ssid(ssid, bss->ssid, bss->ssid_len),
593                                      bss->ssid_len);
594                         break;
595
596                 case WLAN_EID_SUPP_RATES:
597                         n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
598                         memcpy(bss->rates, pos + 2, n_basic_rates);
599                         got_basic_rates = 1;
600                         lbs_deb_scan("got RATES IE\n");
601                         break;
602
603                 case WLAN_EID_FH_PARAMS:
604                         fh = (struct ieee_ie_fh_param_set *) pos;
605                         memcpy(&bss->phy.fh, fh, sizeof(*fh));
606                         lbs_deb_scan("got FH IE\n");
607                         break;
608
609                 case WLAN_EID_DS_PARAMS:
610                         ds = (struct ieee_ie_ds_param_set *) pos;
611                         bss->channel = ds->channel;
612                         memcpy(&bss->phy.ds, ds, sizeof(*ds));
613                         lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
614                         break;
615
616                 case WLAN_EID_CF_PARAMS:
617                         cf = (struct ieee_ie_cf_param_set *) pos;
618                         memcpy(&bss->ss.cf, cf, sizeof(*cf));
619                         lbs_deb_scan("got CF IE\n");
620                         break;
621
622                 case WLAN_EID_IBSS_PARAMS:
623                         ibss = (struct ieee_ie_ibss_param_set *) pos;
624                         bss->atimwindow = ibss->atimwindow;
625                         memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
626                         lbs_deb_scan("got IBSS IE\n");
627                         break;
628
629                 case WLAN_EID_COUNTRY:
630                         pcountryinfo = (struct ieee_ie_country_info_set *) pos;
631                         lbs_deb_scan("got COUNTRY IE\n");
632                         if (pcountryinfo->header.len < sizeof(pcountryinfo->countrycode)
633                             || pcountryinfo->header.len > 254) {
634                                 lbs_deb_scan("%s: 11D- Err CountryInfo len %d, min %zd, max 254\n",
635                                              __func__,
636                                              pcountryinfo->header.len,
637                                              sizeof(pcountryinfo->countrycode));
638                                 ret = -1;
639                                 goto done;
640                         }
641
642                         memcpy(&bss->countryinfo, pcountryinfo,
643                                 pcountryinfo->header.len + 2);
644                         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
645                                     (uint8_t *) pcountryinfo,
646                                     (int) (pcountryinfo->header.len + 2));
647                         break;
648
649                 case WLAN_EID_EXT_SUPP_RATES:
650                         /* only process extended supported rate if data rate is
651                          * already found. Data rate IE should come before
652                          * extended supported rate IE
653                          */
654                         lbs_deb_scan("got RATESEX IE\n");
655                         if (!got_basic_rates) {
656                                 lbs_deb_scan("... but ignoring it\n");
657                                 break;
658                         }
659
660                         n_ex_rates = pos[1];
661                         if (n_basic_rates + n_ex_rates > MAX_RATES)
662                                 n_ex_rates = MAX_RATES - n_basic_rates;
663
664                         p = bss->rates + n_basic_rates;
665                         memcpy(p, pos + 2, n_ex_rates);
666                         break;
667
668                 case WLAN_EID_GENERIC:
669                         if (pos[1] >= 4 &&
670                             pos[2] == 0x00 && pos[3] == 0x50 &&
671                             pos[4] == 0xf2 && pos[5] == 0x01) {
672                                 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
673                                 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
674                                 lbs_deb_scan("got WPA IE\n");
675                                 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
676                                             bss->wpa_ie_len);
677                         } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
678                                    pos[2] == 0x00 && pos[3] == 0x50 &&
679                                    pos[4] == 0x43 && pos[5] == 0x04) {
680                                 lbs_deb_scan("got mesh IE\n");
681                                 bss->mesh = 1;
682                         } else {
683                                 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
684                                         pos[2], pos[3],
685                                         pos[4], pos[5],
686                                         pos[1]);
687                         }
688                         break;
689
690                 case WLAN_EID_RSN:
691                         lbs_deb_scan("got RSN IE\n");
692                         bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
693                         memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
694                         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
695                                     bss->rsn_ie, bss->rsn_ie_len);
696                         break;
697
698                 default:
699                         lbs_deb_scan("got IE 0x%04x, len %d\n",
700                                      pos[0], pos[1]);
701                         break;
702                 }
703
704                 pos += pos[1] + 2;
705         }
706
707         /* Timestamp */
708         bss->last_scanned = jiffies;
709         lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
710
711         ret = 0;
712
713 done:
714         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
715         return ret;
716 }
717
718 /**
719  *  @brief Send a scan command for all available channels filtered on a spec
720  *
721  *  Used in association code and from debugfs
722  *
723  *  @param priv             A pointer to struct lbs_private structure
724  *  @param ssid             A pointer to the SSID to scan for
725  *  @param ssid_len         Length of the SSID
726  *
727  *  @return                0-success, otherwise fail
728  */
729 int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
730                                 uint8_t ssid_len)
731 {
732         DECLARE_SSID_BUF(ssid_buf);
733         int ret = 0;
734
735         lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
736                            print_ssid(ssid_buf, ssid, ssid_len));
737
738         if (!ssid_len)
739                 goto out;
740
741         memcpy(priv->scan_ssid, ssid, ssid_len);
742         priv->scan_ssid_len = ssid_len;
743
744         lbs_scan_networks(priv, 1);
745         if (priv->surpriseremoved) {
746                 ret = -1;
747                 goto out;
748         }
749
750 out:
751         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
752         return ret;
753 }
754
755
756
757
758 /*********************************************************************/
759 /*                                                                   */
760 /*  Support for Wireless Extensions                                  */
761 /*                                                                   */
762 /*********************************************************************/
763
764
765 #define MAX_CUSTOM_LEN 64
766
767 static inline char *lbs_translate_scan(struct lbs_private *priv,
768                                             struct iw_request_info *info,
769                                             char *start, char *stop,
770                                             struct bss_descriptor *bss)
771 {
772         struct chan_freq_power *cfp;
773         char *current_val;      /* For rates */
774         struct iw_event iwe;    /* Temporary buffer */
775         int j;
776 #define PERFECT_RSSI ((uint8_t)50)
777 #define WORST_RSSI   ((uint8_t)0)
778 #define RSSI_DIFF    ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
779         uint8_t rssi;
780
781         lbs_deb_enter(LBS_DEB_SCAN);
782
783         cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
784         if (!cfp) {
785                 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
786                 start = NULL;
787                 goto out;
788         }
789
790         /* First entry *MUST* be the BSSID */
791         iwe.cmd = SIOCGIWAP;
792         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
793         memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
794         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
795
796         /* SSID */
797         iwe.cmd = SIOCGIWESSID;
798         iwe.u.data.flags = 1;
799         iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
800         start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
801
802         /* Mode */
803         iwe.cmd = SIOCGIWMODE;
804         iwe.u.mode = bss->mode;
805         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
806
807         /* Frequency */
808         iwe.cmd = SIOCGIWFREQ;
809         iwe.u.freq.m = (long)cfp->freq * 100000;
810         iwe.u.freq.e = 1;
811         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
812
813         /* Add quality statistics */
814         iwe.cmd = IWEVQUAL;
815         iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
816         iwe.u.qual.level = SCAN_RSSI(bss->rssi);
817
818         rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
819         iwe.u.qual.qual =
820                 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
821                  (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
822                 (RSSI_DIFF * RSSI_DIFF);
823         if (iwe.u.qual.qual > 100)
824                 iwe.u.qual.qual = 100;
825
826         if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
827                 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
828         } else {
829                 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
830         }
831
832         /* Locally created ad-hoc BSSs won't have beacons if this is the
833          * only station in the adhoc network; so get signal strength
834          * from receive statistics.
835          */
836         if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
837             && !lbs_ssid_cmp(priv->curbssparams.ssid,
838                              priv->curbssparams.ssid_len,
839                              bss->ssid, bss->ssid_len)) {
840                 int snr, nf;
841                 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
842                 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
843                 iwe.u.qual.level = CAL_RSSI(snr, nf);
844         }
845         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
846
847         /* Add encryption capability */
848         iwe.cmd = SIOCGIWENCODE;
849         if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
850                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
851         } else {
852                 iwe.u.data.flags = IW_ENCODE_DISABLED;
853         }
854         iwe.u.data.length = 0;
855         start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
856
857         current_val = start + iwe_stream_lcp_len(info);
858
859         iwe.cmd = SIOCGIWRATE;
860         iwe.u.bitrate.fixed = 0;
861         iwe.u.bitrate.disabled = 0;
862         iwe.u.bitrate.value = 0;
863
864         for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
865                 /* Bit rate given in 500 kb/s units */
866                 iwe.u.bitrate.value = bss->rates[j] * 500000;
867                 current_val = iwe_stream_add_value(info, start, current_val,
868                                                    stop, &iwe, IW_EV_PARAM_LEN);
869         }
870         if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
871             && !lbs_ssid_cmp(priv->curbssparams.ssid,
872                              priv->curbssparams.ssid_len,
873                              bss->ssid, bss->ssid_len)) {
874                 iwe.u.bitrate.value = 22 * 500000;
875                 current_val = iwe_stream_add_value(info, start, current_val,
876                                                    stop, &iwe, IW_EV_PARAM_LEN);
877         }
878         /* Check if we added any event */
879         if ((current_val - start) > iwe_stream_lcp_len(info))
880                 start = current_val;
881
882         memset(&iwe, 0, sizeof(iwe));
883         if (bss->wpa_ie_len) {
884                 char buf[MAX_WPA_IE_LEN];
885                 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
886                 iwe.cmd = IWEVGENIE;
887                 iwe.u.data.length = bss->wpa_ie_len;
888                 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
889         }
890
891         memset(&iwe, 0, sizeof(iwe));
892         if (bss->rsn_ie_len) {
893                 char buf[MAX_WPA_IE_LEN];
894                 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
895                 iwe.cmd = IWEVGENIE;
896                 iwe.u.data.length = bss->rsn_ie_len;
897                 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
898         }
899
900         if (bss->mesh) {
901                 char custom[MAX_CUSTOM_LEN];
902                 char *p = custom;
903
904                 iwe.cmd = IWEVCUSTOM;
905                 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
906                 iwe.u.data.length = p - custom;
907                 if (iwe.u.data.length)
908                         start = iwe_stream_add_point(info, start, stop,
909                                                      &iwe, custom);
910         }
911
912 out:
913         lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
914         return start;
915 }
916
917
918 /**
919  *  @brief Handle Scan Network ioctl
920  *
921  *  @param dev          A pointer to net_device structure
922  *  @param info         A pointer to iw_request_info structure
923  *  @param vwrq         A pointer to iw_param structure
924  *  @param extra        A pointer to extra data buf
925  *
926  *  @return             0 --success, otherwise fail
927  */
928 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
929                  union iwreq_data *wrqu, char *extra)
930 {
931         DECLARE_SSID_BUF(ssid);
932         struct lbs_private *priv = dev->ml_priv;
933         int ret = 0;
934
935         lbs_deb_enter(LBS_DEB_WEXT);
936
937         if (!priv->radio_on) {
938                 ret = -EINVAL;
939                 goto out;
940         }
941
942         if (!netif_running(dev)) {
943                 ret = -ENETDOWN;
944                 goto out;
945         }
946
947         /* mac80211 does this:
948         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
949         if (sdata->type != IEEE80211_IF_TYPE_xxx) {
950                 ret = -EOPNOTSUPP;
951                 goto out;
952         }
953         */
954
955         if (wrqu->data.length == sizeof(struct iw_scan_req) &&
956             wrqu->data.flags & IW_SCAN_THIS_ESSID) {
957                 struct iw_scan_req *req = (struct iw_scan_req *)extra;
958                 priv->scan_ssid_len = req->essid_len;
959                 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
960                 lbs_deb_wext("set_scan, essid '%s'\n",
961                         print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
962         } else {
963                 priv->scan_ssid_len = 0;
964         }
965
966         if (!delayed_work_pending(&priv->scan_work))
967                 queue_delayed_work(priv->work_thread, &priv->scan_work,
968                                    msecs_to_jiffies(50));
969         /* set marker that currently a scan is taking place */
970         priv->scan_channel = -1;
971
972         if (priv->surpriseremoved)
973                 ret = -EIO;
974
975 out:
976         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
977         return ret;
978 }
979
980
981 /**
982  *  @brief  Handle Retrieve scan table ioctl
983  *
984  *  @param dev          A pointer to net_device structure
985  *  @param info         A pointer to iw_request_info structure
986  *  @param dwrq         A pointer to iw_point structure
987  *  @param extra        A pointer to extra data buf
988  *
989  *  @return             0 --success, otherwise fail
990  */
991 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
992                  struct iw_point *dwrq, char *extra)
993 {
994 #define SCAN_ITEM_SIZE 128
995         struct lbs_private *priv = dev->ml_priv;
996         int err = 0;
997         char *ev = extra;
998         char *stop = ev + dwrq->length;
999         struct bss_descriptor *iter_bss;
1000         struct bss_descriptor *safe;
1001
1002         lbs_deb_enter(LBS_DEB_WEXT);
1003
1004         /* iwlist should wait until the current scan is finished */
1005         if (priv->scan_channel)
1006                 return -EAGAIN;
1007
1008         /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1009         if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1010                 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1011                                 CMD_OPTION_WAITFORRSP, 0, NULL);
1012                 if (err)
1013                         goto out;
1014         }
1015
1016         mutex_lock(&priv->lock);
1017         list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1018                 char *next_ev;
1019                 unsigned long stale_time;
1020
1021                 if (stop - ev < SCAN_ITEM_SIZE) {
1022                         err = -E2BIG;
1023                         break;
1024                 }
1025
1026                 /* For mesh device, list only mesh networks */
1027                 if (dev == priv->mesh_dev && !iter_bss->mesh)
1028                         continue;
1029
1030                 /* Prune old an old scan result */
1031                 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1032                 if (time_after(jiffies, stale_time)) {
1033                         list_move_tail(&iter_bss->list, &priv->network_free_list);
1034                         clear_bss_descriptor(iter_bss);
1035                         continue;
1036                 }
1037
1038                 /* Translate to WE format this entry */
1039                 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
1040                 if (next_ev == NULL)
1041                         continue;
1042                 ev = next_ev;
1043         }
1044         mutex_unlock(&priv->lock);
1045
1046         dwrq->length = (ev - extra);
1047         dwrq->flags = 0;
1048 out:
1049         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
1050         return err;
1051 }
1052
1053
1054
1055
1056 /*********************************************************************/
1057 /*                                                                   */
1058 /*  Command execution                                                */
1059 /*                                                                   */
1060 /*********************************************************************/
1061
1062
1063 /**
1064  *  @brief This function handles the command response of scan
1065  *
1066  *  Called from handle_cmd_response() in cmdrespc.
1067  *
1068  *   The response buffer for the scan command has the following
1069  *      memory layout:
1070  *
1071  *     .-----------------------------------------------------------.
1072  *     |  header (4 * sizeof(u16)):  Standard command response hdr |
1073  *     .-----------------------------------------------------------.
1074  *     |  bufsize (u16) : sizeof the BSS Description data          |
1075  *     .-----------------------------------------------------------.
1076  *     |  NumOfSet (u8) : Number of BSS Descs returned             |
1077  *     .-----------------------------------------------------------.
1078  *     |  BSSDescription data (variable, size given in bufsize)    |
1079  *     .-----------------------------------------------------------.
1080  *     |  TLV data (variable, size calculated using header->size,  |
1081  *     |            bufsize and sizeof the fixed fields above)     |
1082  *     .-----------------------------------------------------------.
1083  *
1084  *  @param priv    A pointer to struct lbs_private structure
1085  *  @param resp    A pointer to cmd_ds_command
1086  *
1087  *  @return        0 or -1
1088  */
1089 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1090                               struct cmd_header *resp)
1091 {
1092         struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
1093         struct bss_descriptor *iter_bss;
1094         struct bss_descriptor *safe;
1095         uint8_t *bssinfo;
1096         uint16_t scanrespsize;
1097         int bytesleft;
1098         int idx;
1099         int tlvbufsize;
1100         int ret;
1101
1102         lbs_deb_enter(LBS_DEB_SCAN);
1103
1104         /* Prune old entries from scan table */
1105         list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1106                 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1107                 if (time_before(jiffies, stale_time))
1108                         continue;
1109                 list_move_tail (&iter_bss->list, &priv->network_free_list);
1110                 clear_bss_descriptor(iter_bss);
1111         }
1112
1113         if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1114                 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1115                              scanresp->nr_sets, MAX_NETWORK_COUNT);
1116                 ret = -1;
1117                 goto done;
1118         }
1119
1120         bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
1121         lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1122
1123         scanrespsize = le16_to_cpu(resp->size);
1124         lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
1125
1126         bssinfo = scanresp->bssdesc_and_tlvbuffer;
1127
1128         /* The size of the TLV buffer is equal to the entire command response
1129          *   size (scanrespsize) minus the fixed fields (sizeof()'s), the
1130          *   BSS Descriptions (bssdescriptsize as bytesLef) and the command
1131          *   response header (S_DS_GEN)
1132          */
1133         tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1134                                      + sizeof(scanresp->nr_sets)
1135                                      + S_DS_GEN);
1136
1137         /*
1138          *  Process each scan response returned (scanresp->nr_sets). Save
1139          *    the information in the newbssentry and then insert into the
1140          *    driver scan table either as an update to an existing entry
1141          *    or as an addition at the end of the table
1142          */
1143         for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
1144                 struct bss_descriptor new;
1145                 struct bss_descriptor *found = NULL;
1146                 struct bss_descriptor *oldest = NULL;
1147
1148                 /* Process the data fields and IEs returned for this BSS */
1149                 memset(&new, 0, sizeof (struct bss_descriptor));
1150                 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
1151                         /* error parsing the scan response, skipped */
1152                         lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1153                         continue;
1154                 }
1155
1156                 /* Try to find this bss in the scan table */
1157                 list_for_each_entry (iter_bss, &priv->network_list, list) {
1158                         if (is_same_network(iter_bss, &new)) {
1159                                 found = iter_bss;
1160                                 break;
1161                         }
1162
1163                         if ((oldest == NULL) ||
1164                             (iter_bss->last_scanned < oldest->last_scanned))
1165                                 oldest = iter_bss;
1166                 }
1167
1168                 if (found) {
1169                         /* found, clear it */
1170                         clear_bss_descriptor(found);
1171                 } else if (!list_empty(&priv->network_free_list)) {
1172                         /* Pull one from the free list */
1173                         found = list_entry(priv->network_free_list.next,
1174                                            struct bss_descriptor, list);
1175                         list_move_tail(&found->list, &priv->network_list);
1176                 } else if (oldest) {
1177                         /* If there are no more slots, expire the oldest */
1178                         found = oldest;
1179                         clear_bss_descriptor(found);
1180                         list_move_tail(&found->list, &priv->network_list);
1181                 } else {
1182                         continue;
1183                 }
1184
1185                 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
1186
1187                 /* Copy the locally created newbssentry to the scan table */
1188                 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1189         }
1190
1191         ret = 0;
1192
1193 done:
1194         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1195         return ret;
1196 }