scan.c 35 KB

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