scan.c 36 KB

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