libertas: change IW_ESSID_MAX_SIZE -> IEEE80211_MAX_SSID_LEN
[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                              + IEEE80211_MAX_SSID_LEN   \
23                              + IW_EV_UINT_LEN           \
24                              + IW_EV_FREQ_LEN           \
25                              + IW_EV_QUAL_LEN           \
26                              + IEEE80211_MAX_SSID_LEN   \
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         uint8_t *pos, *end, *p;
507         uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
508         uint16_t beaconsize = 0;
509         int ret;
510
511         lbs_deb_enter(LBS_DEB_SCAN);
512
513         if (*bytesleft >= sizeof(beaconsize)) {
514                 /* Extract & convert beacon size from the command buffer */
515                 beaconsize = get_unaligned_le16(*pbeaconinfo);
516                 *bytesleft -= sizeof(beaconsize);
517                 *pbeaconinfo += sizeof(beaconsize);
518         }
519
520         if (beaconsize == 0 || beaconsize > *bytesleft) {
521                 *pbeaconinfo += *bytesleft;
522                 *bytesleft = 0;
523                 ret = -1;
524                 goto done;
525         }
526
527         /* Initialize the current working beacon pointer for this BSS iteration */
528         pos = *pbeaconinfo;
529         end = pos + beaconsize;
530
531         /* Advance the return beacon pointer past the current beacon */
532         *pbeaconinfo += beaconsize;
533         *bytesleft -= beaconsize;
534
535         memcpy(bss->bssid, pos, ETH_ALEN);
536         lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
537         pos += ETH_ALEN;
538
539         if ((end - pos) < 12) {
540                 lbs_deb_scan("process_bss: Not enough bytes left\n");
541                 ret = -1;
542                 goto done;
543         }
544
545         /*
546          * next 4 fields are RSSI, time stamp, beacon interval,
547          *   and capability information
548          */
549
550         /* RSSI is 1 byte long */
551         bss->rssi = *pos;
552         lbs_deb_scan("process_bss: RSSI %d\n", *pos);
553         pos++;
554
555         /* time stamp is 8 bytes long */
556         pos += 8;
557
558         /* beacon interval is 2 bytes long */
559         bss->beaconperiod = get_unaligned_le16(pos);
560         pos += 2;
561
562         /* capability information is 2 bytes long */
563         bss->capability = get_unaligned_le16(pos);
564         lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
565         pos += 2;
566
567         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
568                 lbs_deb_scan("process_bss: WEP enabled\n");
569         if (bss->capability & WLAN_CAPABILITY_IBSS)
570                 bss->mode = IW_MODE_ADHOC;
571         else
572                 bss->mode = IW_MODE_INFRA;
573
574         /* rest of the current buffer are IE's */
575         lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
576         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
577
578         /* process variable IE */
579         while (pos <= end - 2) {
580                 if (pos + pos[1] > end) {
581                         lbs_deb_scan("process_bss: error in processing IE, "
582                                      "bytes left < IE length\n");
583                         break;
584                 }
585
586                 switch (pos[0]) {
587                 case WLAN_EID_SSID:
588                         bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
589                         memcpy(bss->ssid, pos + 2, bss->ssid_len);
590                         lbs_deb_scan("got SSID IE: '%s', len %u\n",
591                                      print_ssid(ssid, bss->ssid, bss->ssid_len),
592                                      bss->ssid_len);
593                         break;
594
595                 case WLAN_EID_SUPP_RATES:
596                         n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
597                         memcpy(bss->rates, pos + 2, n_basic_rates);
598                         got_basic_rates = 1;
599                         lbs_deb_scan("got RATES IE\n");
600                         break;
601
602                 case WLAN_EID_FH_PARAMS:
603                         fh = (struct ieee_ie_fh_param_set *) pos;
604                         memcpy(&bss->phy.fh, fh, sizeof(*fh));
605                         lbs_deb_scan("got FH IE\n");
606                         break;
607
608                 case WLAN_EID_DS_PARAMS:
609                         ds = (struct ieee_ie_ds_param_set *) pos;
610                         bss->channel = ds->channel;
611                         memcpy(&bss->phy.ds, ds, sizeof(*ds));
612                         lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
613                         break;
614
615                 case WLAN_EID_CF_PARAMS:
616                         cf = (struct ieee_ie_cf_param_set *) pos;
617                         memcpy(&bss->ss.cf, cf, sizeof(*cf));
618                         lbs_deb_scan("got CF IE\n");
619                         break;
620
621                 case WLAN_EID_IBSS_PARAMS:
622                         ibss = (struct ieee_ie_ibss_param_set *) pos;
623                         bss->atimwindow = ibss->atimwindow;
624                         memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
625                         lbs_deb_scan("got IBSS IE\n");
626                         break;
627
628                 case WLAN_EID_EXT_SUPP_RATES:
629                         /* only process extended supported rate if data rate is
630                          * already found. Data rate IE should come before
631                          * extended supported rate IE
632                          */
633                         lbs_deb_scan("got RATESEX IE\n");
634                         if (!got_basic_rates) {
635                                 lbs_deb_scan("... but ignoring it\n");
636                                 break;
637                         }
638
639                         n_ex_rates = pos[1];
640                         if (n_basic_rates + n_ex_rates > MAX_RATES)
641                                 n_ex_rates = MAX_RATES - n_basic_rates;
642
643                         p = bss->rates + n_basic_rates;
644                         memcpy(p, pos + 2, n_ex_rates);
645                         break;
646
647                 case WLAN_EID_GENERIC:
648                         if (pos[1] >= 4 &&
649                             pos[2] == 0x00 && pos[3] == 0x50 &&
650                             pos[4] == 0xf2 && pos[5] == 0x01) {
651                                 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
652                                 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
653                                 lbs_deb_scan("got WPA IE\n");
654                                 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
655                                             bss->wpa_ie_len);
656                         } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
657                                    pos[2] == 0x00 && pos[3] == 0x50 &&
658                                    pos[4] == 0x43 && pos[5] == 0x04) {
659                                 lbs_deb_scan("got mesh IE\n");
660                                 bss->mesh = 1;
661                         } else {
662                                 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
663                                         pos[2], pos[3],
664                                         pos[4], pos[5],
665                                         pos[1]);
666                         }
667                         break;
668
669                 case WLAN_EID_RSN:
670                         lbs_deb_scan("got RSN IE\n");
671                         bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
672                         memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
673                         lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
674                                     bss->rsn_ie, bss->rsn_ie_len);
675                         break;
676
677                 default:
678                         lbs_deb_scan("got IE 0x%04x, len %d\n",
679                                      pos[0], pos[1]);
680                         break;
681                 }
682
683                 pos += pos[1] + 2;
684         }
685
686         /* Timestamp */
687         bss->last_scanned = jiffies;
688         lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
689
690         ret = 0;
691
692 done:
693         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
694         return ret;
695 }
696
697 /**
698  *  @brief Send a scan command for all available channels filtered on a spec
699  *
700  *  Used in association code and from debugfs
701  *
702  *  @param priv             A pointer to struct lbs_private structure
703  *  @param ssid             A pointer to the SSID to scan for
704  *  @param ssid_len         Length of the SSID
705  *
706  *  @return                0-success, otherwise fail
707  */
708 int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
709                                 uint8_t ssid_len)
710 {
711         DECLARE_SSID_BUF(ssid_buf);
712         int ret = 0;
713
714         lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
715                            print_ssid(ssid_buf, ssid, ssid_len));
716
717         if (!ssid_len)
718                 goto out;
719
720         memcpy(priv->scan_ssid, ssid, ssid_len);
721         priv->scan_ssid_len = ssid_len;
722
723         lbs_scan_networks(priv, 1);
724         if (priv->surpriseremoved) {
725                 ret = -1;
726                 goto out;
727         }
728
729 out:
730         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
731         return ret;
732 }
733
734
735
736
737 /*********************************************************************/
738 /*                                                                   */
739 /*  Support for Wireless Extensions                                  */
740 /*                                                                   */
741 /*********************************************************************/
742
743
744 #define MAX_CUSTOM_LEN 64
745
746 static inline char *lbs_translate_scan(struct lbs_private *priv,
747                                             struct iw_request_info *info,
748                                             char *start, char *stop,
749                                             struct bss_descriptor *bss)
750 {
751         struct chan_freq_power *cfp;
752         char *current_val;      /* For rates */
753         struct iw_event iwe;    /* Temporary buffer */
754         int j;
755 #define PERFECT_RSSI ((uint8_t)50)
756 #define WORST_RSSI   ((uint8_t)0)
757 #define RSSI_DIFF    ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
758         uint8_t rssi;
759
760         lbs_deb_enter(LBS_DEB_SCAN);
761
762         cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
763         if (!cfp) {
764                 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
765                 start = NULL;
766                 goto out;
767         }
768
769         /* First entry *MUST* be the BSSID */
770         iwe.cmd = SIOCGIWAP;
771         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
772         memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
773         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
774
775         /* SSID */
776         iwe.cmd = SIOCGIWESSID;
777         iwe.u.data.flags = 1;
778         iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
779         start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
780
781         /* Mode */
782         iwe.cmd = SIOCGIWMODE;
783         iwe.u.mode = bss->mode;
784         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
785
786         /* Frequency */
787         iwe.cmd = SIOCGIWFREQ;
788         iwe.u.freq.m = (long)cfp->freq * 100000;
789         iwe.u.freq.e = 1;
790         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
791
792         /* Add quality statistics */
793         iwe.cmd = IWEVQUAL;
794         iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
795         iwe.u.qual.level = SCAN_RSSI(bss->rssi);
796
797         rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
798         iwe.u.qual.qual =
799                 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
800                  (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
801                 (RSSI_DIFF * RSSI_DIFF);
802         if (iwe.u.qual.qual > 100)
803                 iwe.u.qual.qual = 100;
804
805         if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
806                 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
807         } else {
808                 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
809         }
810
811         /* Locally created ad-hoc BSSs won't have beacons if this is the
812          * only station in the adhoc network; so get signal strength
813          * from receive statistics.
814          */
815         if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
816             && !lbs_ssid_cmp(priv->curbssparams.ssid,
817                              priv->curbssparams.ssid_len,
818                              bss->ssid, bss->ssid_len)) {
819                 int snr, nf;
820                 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
821                 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
822                 iwe.u.qual.level = CAL_RSSI(snr, nf);
823         }
824         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
825
826         /* Add encryption capability */
827         iwe.cmd = SIOCGIWENCODE;
828         if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
829                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
830         } else {
831                 iwe.u.data.flags = IW_ENCODE_DISABLED;
832         }
833         iwe.u.data.length = 0;
834         start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
835
836         current_val = start + iwe_stream_lcp_len(info);
837
838         iwe.cmd = SIOCGIWRATE;
839         iwe.u.bitrate.fixed = 0;
840         iwe.u.bitrate.disabled = 0;
841         iwe.u.bitrate.value = 0;
842
843         for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
844                 /* Bit rate given in 500 kb/s units */
845                 iwe.u.bitrate.value = bss->rates[j] * 500000;
846                 current_val = iwe_stream_add_value(info, start, current_val,
847                                                    stop, &iwe, IW_EV_PARAM_LEN);
848         }
849         if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
850             && !lbs_ssid_cmp(priv->curbssparams.ssid,
851                              priv->curbssparams.ssid_len,
852                              bss->ssid, bss->ssid_len)) {
853                 iwe.u.bitrate.value = 22 * 500000;
854                 current_val = iwe_stream_add_value(info, start, current_val,
855                                                    stop, &iwe, IW_EV_PARAM_LEN);
856         }
857         /* Check if we added any event */
858         if ((current_val - start) > iwe_stream_lcp_len(info))
859                 start = current_val;
860
861         memset(&iwe, 0, sizeof(iwe));
862         if (bss->wpa_ie_len) {
863                 char buf[MAX_WPA_IE_LEN];
864                 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
865                 iwe.cmd = IWEVGENIE;
866                 iwe.u.data.length = bss->wpa_ie_len;
867                 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
868         }
869
870         memset(&iwe, 0, sizeof(iwe));
871         if (bss->rsn_ie_len) {
872                 char buf[MAX_WPA_IE_LEN];
873                 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
874                 iwe.cmd = IWEVGENIE;
875                 iwe.u.data.length = bss->rsn_ie_len;
876                 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
877         }
878
879         if (bss->mesh) {
880                 char custom[MAX_CUSTOM_LEN];
881                 char *p = custom;
882
883                 iwe.cmd = IWEVCUSTOM;
884                 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
885                 iwe.u.data.length = p - custom;
886                 if (iwe.u.data.length)
887                         start = iwe_stream_add_point(info, start, stop,
888                                                      &iwe, custom);
889         }
890
891 out:
892         lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
893         return start;
894 }
895
896
897 /**
898  *  @brief Handle Scan Network ioctl
899  *
900  *  @param dev          A pointer to net_device structure
901  *  @param info         A pointer to iw_request_info structure
902  *  @param vwrq         A pointer to iw_param structure
903  *  @param extra        A pointer to extra data buf
904  *
905  *  @return             0 --success, otherwise fail
906  */
907 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
908                  union iwreq_data *wrqu, char *extra)
909 {
910         DECLARE_SSID_BUF(ssid);
911         struct lbs_private *priv = dev->ml_priv;
912         int ret = 0;
913
914         lbs_deb_enter(LBS_DEB_WEXT);
915
916         if (!priv->radio_on) {
917                 ret = -EINVAL;
918                 goto out;
919         }
920
921         if (!netif_running(dev)) {
922                 ret = -ENETDOWN;
923                 goto out;
924         }
925
926         /* mac80211 does this:
927         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
928         if (sdata->type != IEEE80211_IF_TYPE_xxx) {
929                 ret = -EOPNOTSUPP;
930                 goto out;
931         }
932         */
933
934         if (wrqu->data.length == sizeof(struct iw_scan_req) &&
935             wrqu->data.flags & IW_SCAN_THIS_ESSID) {
936                 struct iw_scan_req *req = (struct iw_scan_req *)extra;
937                 priv->scan_ssid_len = req->essid_len;
938                 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
939                 lbs_deb_wext("set_scan, essid '%s'\n",
940                         print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
941         } else {
942                 priv->scan_ssid_len = 0;
943         }
944
945         if (!delayed_work_pending(&priv->scan_work))
946                 queue_delayed_work(priv->work_thread, &priv->scan_work,
947                                    msecs_to_jiffies(50));
948         /* set marker that currently a scan is taking place */
949         priv->scan_channel = -1;
950
951         if (priv->surpriseremoved)
952                 ret = -EIO;
953
954 out:
955         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
956         return ret;
957 }
958
959
960 /**
961  *  @brief  Handle Retrieve scan table ioctl
962  *
963  *  @param dev          A pointer to net_device structure
964  *  @param info         A pointer to iw_request_info structure
965  *  @param dwrq         A pointer to iw_point structure
966  *  @param extra        A pointer to extra data buf
967  *
968  *  @return             0 --success, otherwise fail
969  */
970 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
971                  struct iw_point *dwrq, char *extra)
972 {
973 #define SCAN_ITEM_SIZE 128
974         struct lbs_private *priv = dev->ml_priv;
975         int err = 0;
976         char *ev = extra;
977         char *stop = ev + dwrq->length;
978         struct bss_descriptor *iter_bss;
979         struct bss_descriptor *safe;
980
981         lbs_deb_enter(LBS_DEB_WEXT);
982
983         /* iwlist should wait until the current scan is finished */
984         if (priv->scan_channel)
985                 return -EAGAIN;
986
987         /* Update RSSI if current BSS is a locally created ad-hoc BSS */
988         if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
989                 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
990                                 CMD_OPTION_WAITFORRSP, 0, NULL);
991                 if (err)
992                         goto out;
993         }
994
995         mutex_lock(&priv->lock);
996         list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
997                 char *next_ev;
998                 unsigned long stale_time;
999
1000                 if (stop - ev < SCAN_ITEM_SIZE) {
1001                         err = -E2BIG;
1002                         break;
1003                 }
1004
1005                 /* For mesh device, list only mesh networks */
1006                 if (dev == priv->mesh_dev && !iter_bss->mesh)
1007                         continue;
1008
1009                 /* Prune old an old scan result */
1010                 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1011                 if (time_after(jiffies, stale_time)) {
1012                         list_move_tail(&iter_bss->list, &priv->network_free_list);
1013                         clear_bss_descriptor(iter_bss);
1014                         continue;
1015                 }
1016
1017                 /* Translate to WE format this entry */
1018                 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
1019                 if (next_ev == NULL)
1020                         continue;
1021                 ev = next_ev;
1022         }
1023         mutex_unlock(&priv->lock);
1024
1025         dwrq->length = (ev - extra);
1026         dwrq->flags = 0;
1027 out:
1028         lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
1029         return err;
1030 }
1031
1032
1033
1034
1035 /*********************************************************************/
1036 /*                                                                   */
1037 /*  Command execution                                                */
1038 /*                                                                   */
1039 /*********************************************************************/
1040
1041
1042 /**
1043  *  @brief This function handles the command response of scan
1044  *
1045  *  Called from handle_cmd_response() in cmdrespc.
1046  *
1047  *   The response buffer for the scan command has the following
1048  *      memory layout:
1049  *
1050  *     .-----------------------------------------------------------.
1051  *     |  header (4 * sizeof(u16)):  Standard command response hdr |
1052  *     .-----------------------------------------------------------.
1053  *     |  bufsize (u16) : sizeof the BSS Description data          |
1054  *     .-----------------------------------------------------------.
1055  *     |  NumOfSet (u8) : Number of BSS Descs returned             |
1056  *     .-----------------------------------------------------------.
1057  *     |  BSSDescription data (variable, size given in bufsize)    |
1058  *     .-----------------------------------------------------------.
1059  *     |  TLV data (variable, size calculated using header->size,  |
1060  *     |            bufsize and sizeof the fixed fields above)     |
1061  *     .-----------------------------------------------------------.
1062  *
1063  *  @param priv    A pointer to struct lbs_private structure
1064  *  @param resp    A pointer to cmd_ds_command
1065  *
1066  *  @return        0 or -1
1067  */
1068 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1069                               struct cmd_header *resp)
1070 {
1071         struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
1072         struct bss_descriptor *iter_bss;
1073         struct bss_descriptor *safe;
1074         uint8_t *bssinfo;
1075         uint16_t scanrespsize;
1076         int bytesleft;
1077         int idx;
1078         int tlvbufsize;
1079         int ret;
1080
1081         lbs_deb_enter(LBS_DEB_SCAN);
1082
1083         /* Prune old entries from scan table */
1084         list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1085                 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1086                 if (time_before(jiffies, stale_time))
1087                         continue;
1088                 list_move_tail (&iter_bss->list, &priv->network_free_list);
1089                 clear_bss_descriptor(iter_bss);
1090         }
1091
1092         if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1093                 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1094                              scanresp->nr_sets, MAX_NETWORK_COUNT);
1095                 ret = -1;
1096                 goto done;
1097         }
1098
1099         bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
1100         lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1101
1102         scanrespsize = le16_to_cpu(resp->size);
1103         lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
1104
1105         bssinfo = scanresp->bssdesc_and_tlvbuffer;
1106
1107         /* The size of the TLV buffer is equal to the entire command response
1108          *   size (scanrespsize) minus the fixed fields (sizeof()'s), the
1109          *   BSS Descriptions (bssdescriptsize as bytesLef) and the command
1110          *   response header (S_DS_GEN)
1111          */
1112         tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1113                                      + sizeof(scanresp->nr_sets)
1114                                      + S_DS_GEN);
1115
1116         /*
1117          *  Process each scan response returned (scanresp->nr_sets). Save
1118          *    the information in the newbssentry and then insert into the
1119          *    driver scan table either as an update to an existing entry
1120          *    or as an addition at the end of the table
1121          */
1122         for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
1123                 struct bss_descriptor new;
1124                 struct bss_descriptor *found = NULL;
1125                 struct bss_descriptor *oldest = NULL;
1126
1127                 /* Process the data fields and IEs returned for this BSS */
1128                 memset(&new, 0, sizeof (struct bss_descriptor));
1129                 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
1130                         /* error parsing the scan response, skipped */
1131                         lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1132                         continue;
1133                 }
1134
1135                 /* Try to find this bss in the scan table */
1136                 list_for_each_entry (iter_bss, &priv->network_list, list) {
1137                         if (is_same_network(iter_bss, &new)) {
1138                                 found = iter_bss;
1139                                 break;
1140                         }
1141
1142                         if ((oldest == NULL) ||
1143                             (iter_bss->last_scanned < oldest->last_scanned))
1144                                 oldest = iter_bss;
1145                 }
1146
1147                 if (found) {
1148                         /* found, clear it */
1149                         clear_bss_descriptor(found);
1150                 } else if (!list_empty(&priv->network_free_list)) {
1151                         /* Pull one from the free list */
1152                         found = list_entry(priv->network_free_list.next,
1153                                            struct bss_descriptor, list);
1154                         list_move_tail(&found->list, &priv->network_list);
1155                 } else if (oldest) {
1156                         /* If there are no more slots, expire the oldest */
1157                         found = oldest;
1158                         clear_bss_descriptor(found);
1159                         list_move_tail(&found->list, &priv->network_list);
1160                 } else {
1161                         continue;
1162                 }
1163
1164                 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
1165
1166                 /* Copy the locally created newbssentry to the scan table */
1167                 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1168         }
1169
1170         ret = 0;
1171
1172 done:
1173         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1174         return ret;
1175 }