scan.c 36 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/etherdevice.h>
  9. #include <linux/if_arp.h>
  10. #include <asm/unaligned.h>
  11. #include <net/lib80211.h>
  12. #include "host.h"
  13. #include "decl.h"
  14. #include "dev.h"
  15. #include "scan.h"
  16. #include "cmd.h"
  17. //! Approximate amount of data needed to pass a scan result back to iwlist
  18. #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
  19. + IW_ESSID_MAX_SIZE \
  20. + IW_EV_UINT_LEN \
  21. + IW_EV_FREQ_LEN \
  22. + IW_EV_QUAL_LEN \
  23. + IW_ESSID_MAX_SIZE \
  24. + IW_EV_PARAM_LEN \
  25. + 40) /* 40 for WPAIE */
  26. //! Memory needed to store a max sized channel List TLV for a firmware scan
  27. #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
  28. + (MRVDRV_MAX_CHANNELS_PER_SCAN \
  29. * sizeof(struct chanscanparamset)))
  30. //! Memory needed to store a max number/size SSID TLV for a firmware scan
  31. #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
  32. //! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
  33. #define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
  34. + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
  35. //! The maximum number of channels the firmware can scan per command
  36. #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
  37. /**
  38. * @brief Number of channels to scan per firmware scan command issuance.
  39. *
  40. * Number restricted to prevent hitting the limit on the amount of scan data
  41. * returned in a single firmware scan command.
  42. */
  43. #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
  44. //! Scan time specified in the channel TLV for each channel for passive scans
  45. #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
  46. //! Scan time specified in the channel TLV for each channel for active scans
  47. #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
  48. #define DEFAULT_MAX_SCAN_AGE (15 * HZ)
  49. static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
  50. struct cmd_header *resp);
  51. /*********************************************************************/
  52. /* */
  53. /* Misc helper functions */
  54. /* */
  55. /*********************************************************************/
  56. /**
  57. * @brief Unsets the MSB on basic rates
  58. *
  59. * Scan through an array and unset the MSB for basic data rates.
  60. *
  61. * @param rates buffer of data rates
  62. * @param len size of buffer
  63. */
  64. static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
  65. {
  66. int i;
  67. for (i = 0; i < len; i++)
  68. rates[i] &= 0x7f;
  69. }
  70. static inline void clear_bss_descriptor(struct bss_descriptor *bss)
  71. {
  72. /* Don't blow away ->list, just BSS data */
  73. memset(bss, 0, offsetof(struct bss_descriptor, list));
  74. }
  75. /**
  76. * @brief Compare two SSIDs
  77. *
  78. * @param ssid1 A pointer to ssid to compare
  79. * @param ssid2 A pointer to ssid to compare
  80. *
  81. * @return 0: ssid is same, otherwise is different
  82. */
  83. int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
  84. uint8_t ssid2_len)
  85. {
  86. if (ssid1_len != ssid2_len)
  87. return -1;
  88. return memcmp(ssid1, ssid2, ssid1_len);
  89. }
  90. static inline int is_same_network(struct bss_descriptor *src,
  91. struct bss_descriptor *dst)
  92. {
  93. /* A network is only a duplicate if the channel, BSSID, and ESSID
  94. * all match. We treat all <hidden> with the same BSSID and channel
  95. * as one network */
  96. return ((src->ssid_len == dst->ssid_len) &&
  97. (src->channel == dst->channel) &&
  98. !compare_ether_addr(src->bssid, dst->bssid) &&
  99. !memcmp(src->ssid, dst->ssid, src->ssid_len));
  100. }
  101. /*********************************************************************/
  102. /* */
  103. /* Main scanning support */
  104. /* */
  105. /*********************************************************************/
  106. /**
  107. * @brief Create a channel list for the driver to scan based on region info
  108. *
  109. * Only used from lbs_scan_setup_scan_config()
  110. *
  111. * Use the driver region/band information to construct a comprehensive list
  112. * of channels to scan. This routine is used for any scan that is not
  113. * provided a specific channel list to scan.
  114. *
  115. * @param priv A pointer to struct lbs_private structure
  116. * @param scanchanlist Output parameter: resulting channel list to scan
  117. *
  118. * @return void
  119. */
  120. static int lbs_scan_create_channel_list(struct lbs_private *priv,
  121. struct chanscanparamset *scanchanlist)
  122. {
  123. struct region_channel *scanregion;
  124. struct chan_freq_power *cfp;
  125. int rgnidx;
  126. int chanidx;
  127. int nextchan;
  128. uint8_t scantype;
  129. chanidx = 0;
  130. /* Set the default scan type to the user specified type, will later
  131. * be changed to passive on a per channel basis if restricted by
  132. * regulatory requirements (11d or 11h)
  133. */
  134. scantype = CMD_SCAN_TYPE_ACTIVE;
  135. for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
  136. if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
  137. && (priv->mesh_connect_status != LBS_CONNECTED)) {
  138. /* Scan all the supported chan for the first scan */
  139. if (!priv->universal_channel[rgnidx].valid)
  140. continue;
  141. scanregion = &priv->universal_channel[rgnidx];
  142. /* clear the parsed_region_chan for the first scan */
  143. memset(&priv->parsed_region_chan, 0x00,
  144. sizeof(priv->parsed_region_chan));
  145. } else {
  146. if (!priv->region_channel[rgnidx].valid)
  147. continue;
  148. scanregion = &priv->region_channel[rgnidx];
  149. }
  150. for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
  151. struct chanscanparamset *chan = &scanchanlist[chanidx];
  152. cfp = scanregion->CFP + nextchan;
  153. if (priv->enable11d)
  154. scantype = lbs_get_scan_type_11d(cfp->channel,
  155. &priv->parsed_region_chan);
  156. if (scanregion->band == BAND_B || scanregion->band == BAND_G)
  157. chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
  158. if (scantype == CMD_SCAN_TYPE_PASSIVE) {
  159. chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
  160. chan->chanscanmode.passivescan = 1;
  161. } else {
  162. chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
  163. chan->chanscanmode.passivescan = 0;
  164. }
  165. chan->channumber = cfp->channel;
  166. }
  167. }
  168. return chanidx;
  169. }
  170. /*
  171. * Add SSID TLV of the form:
  172. *
  173. * TLV-ID SSID 00 00
  174. * length 06 00
  175. * ssid 4d 4e 54 45 53 54
  176. */
  177. static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
  178. {
  179. struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
  180. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  181. ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
  182. memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
  183. return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
  184. }
  185. /*
  186. * Add CHANLIST TLV of the form
  187. *
  188. * TLV-ID CHANLIST 01 01
  189. * length 5b 00
  190. * channel 1 00 01 00 00 00 64 00
  191. * radio type 00
  192. * channel 01
  193. * scan type 00
  194. * min scan time 00 00
  195. * max scan time 64 00
  196. * channel 2 00 02 00 00 00 64 00
  197. * channel 3 00 03 00 00 00 64 00
  198. * channel 4 00 04 00 00 00 64 00
  199. * channel 5 00 05 00 00 00 64 00
  200. * channel 6 00 06 00 00 00 64 00
  201. * channel 7 00 07 00 00 00 64 00
  202. * channel 8 00 08 00 00 00 64 00
  203. * channel 9 00 09 00 00 00 64 00
  204. * channel 10 00 0a 00 00 00 64 00
  205. * channel 11 00 0b 00 00 00 64 00
  206. * channel 12 00 0c 00 00 00 64 00
  207. * channel 13 00 0d 00 00 00 64 00
  208. *
  209. */
  210. static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
  211. struct chanscanparamset *chan_list,
  212. int chan_count)
  213. {
  214. size_t size = sizeof(struct chanscanparamset) *chan_count;
  215. struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
  216. chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  217. memcpy(chan_tlv->chanscanparam, chan_list, size);
  218. chan_tlv->header.len = cpu_to_le16(size);
  219. return sizeof(chan_tlv->header) + size;
  220. }
  221. /*
  222. * Add RATES TLV of the form
  223. *
  224. * TLV-ID RATES 01 00
  225. * length 0e 00
  226. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  227. *
  228. * The rates are in lbs_bg_rates[], but for the 802.11b
  229. * rates the high bit isn't set.
  230. */
  231. static int lbs_scan_add_rates_tlv(uint8_t *tlv)
  232. {
  233. int i;
  234. struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
  235. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  236. tlv += sizeof(rate_tlv->header);
  237. for (i = 0; i < MAX_RATES; i++) {
  238. *tlv = lbs_bg_rates[i];
  239. if (*tlv == 0)
  240. break;
  241. /* This code makes sure that the 802.11b rates (1 MBit/s, 2
  242. MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
  243. Note that the values are MBit/s * 2, to mark them as
  244. basic rates so that the firmware likes it better */
  245. if (*tlv == 0x02 || *tlv == 0x04 ||
  246. *tlv == 0x0b || *tlv == 0x16)
  247. *tlv |= 0x80;
  248. tlv++;
  249. }
  250. rate_tlv->header.len = cpu_to_le16(i);
  251. return sizeof(rate_tlv->header) + i;
  252. }
  253. /*
  254. * Generate the CMD_802_11_SCAN command with the proper tlv
  255. * for a bunch of channels.
  256. */
  257. static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
  258. struct chanscanparamset *chan_list, int chan_count)
  259. {
  260. int ret = -ENOMEM;
  261. struct cmd_ds_802_11_scan *scan_cmd;
  262. uint8_t *tlv; /* pointer into our current, growing TLV storage area */
  263. lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
  264. bsstype, chan_list ? chan_list[0].channumber : -1,
  265. chan_count);
  266. /* create the fixed part for scan command */
  267. scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
  268. if (scan_cmd == NULL)
  269. goto out;
  270. tlv = scan_cmd->tlvbuffer;
  271. /* TODO: do we need to scan for a specific BSSID?
  272. memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
  273. scan_cmd->bsstype = bsstype;
  274. /* add TLVs */
  275. if (priv->scan_ssid_len)
  276. tlv += lbs_scan_add_ssid_tlv(priv, tlv);
  277. if (chan_list && chan_count)
  278. tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
  279. tlv += lbs_scan_add_rates_tlv(tlv);
  280. /* This is the final data we are about to send */
  281. scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
  282. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  283. sizeof(*scan_cmd));
  284. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  285. tlv - scan_cmd->tlvbuffer);
  286. ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  287. le16_to_cpu(scan_cmd->hdr.size),
  288. lbs_ret_80211_scan, 0);
  289. out:
  290. kfree(scan_cmd);
  291. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  292. return ret;
  293. }
  294. /**
  295. * @brief Internal function used to start a scan based on an input config
  296. *
  297. * Use the input user scan configuration information when provided in
  298. * order to send the appropriate scan commands to firmware to populate or
  299. * update the internal driver scan table
  300. *
  301. * @param priv A pointer to struct lbs_private structure
  302. * @param full_scan Do a full-scan (blocking)
  303. *
  304. * @return 0 or < 0 if error
  305. */
  306. int lbs_scan_networks(struct lbs_private *priv, int full_scan)
  307. {
  308. int ret = -ENOMEM;
  309. struct chanscanparamset *chan_list;
  310. struct chanscanparamset *curr_chans;
  311. int chan_count;
  312. uint8_t bsstype = CMD_BSS_TYPE_ANY;
  313. int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
  314. union iwreq_data wrqu;
  315. #ifdef CONFIG_LIBERTAS_DEBUG
  316. struct bss_descriptor *iter;
  317. int i = 0;
  318. DECLARE_SSID_BUF(ssid);
  319. #endif
  320. lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
  321. /* Cancel any partial outstanding partial scans if this scan
  322. * is a full scan.
  323. */
  324. if (full_scan && delayed_work_pending(&priv->scan_work))
  325. cancel_delayed_work(&priv->scan_work);
  326. /* User-specified bsstype or channel list
  327. TODO: this can be implemented if some user-space application
  328. need the feature. Formerly, it was accessible from debugfs,
  329. but then nowhere used.
  330. if (user_cfg) {
  331. if (user_cfg->bsstype)
  332. bsstype = user_cfg->bsstype;
  333. } */
  334. lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
  335. /* Create list of channels to scan */
  336. chan_list = kzalloc(sizeof(struct chanscanparamset) *
  337. LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
  338. if (!chan_list) {
  339. lbs_pr_alert("SCAN: chan_list empty\n");
  340. goto out;
  341. }
  342. /* We want to scan all channels */
  343. chan_count = lbs_scan_create_channel_list(priv, chan_list);
  344. netif_stop_queue(priv->dev);
  345. netif_carrier_off(priv->dev);
  346. if (priv->mesh_dev) {
  347. netif_stop_queue(priv->mesh_dev);
  348. netif_carrier_off(priv->mesh_dev);
  349. }
  350. /* Prepare to continue an interrupted scan */
  351. lbs_deb_scan("chan_count %d, scan_channel %d\n",
  352. chan_count, priv->scan_channel);
  353. curr_chans = chan_list;
  354. /* advance channel list by already-scanned-channels */
  355. if (priv->scan_channel > 0) {
  356. curr_chans += priv->scan_channel;
  357. chan_count -= priv->scan_channel;
  358. }
  359. /* Send scan command(s)
  360. * numchannels contains the number of channels we should maximally scan
  361. * chan_count is the total number of channels to scan
  362. */
  363. while (chan_count) {
  364. int to_scan = min(numchannels, chan_count);
  365. lbs_deb_scan("scanning %d of %d channels\n",
  366. to_scan, chan_count);
  367. ret = lbs_do_scan(priv, bsstype, curr_chans,
  368. to_scan);
  369. if (ret) {
  370. lbs_pr_err("SCAN_CMD failed\n");
  371. goto out2;
  372. }
  373. curr_chans += to_scan;
  374. chan_count -= to_scan;
  375. /* somehow schedule the next part of the scan */
  376. if (chan_count && !full_scan &&
  377. !priv->surpriseremoved) {
  378. /* -1 marks just that we're currently scanning */
  379. if (priv->scan_channel < 0)
  380. priv->scan_channel = to_scan;
  381. else
  382. priv->scan_channel += to_scan;
  383. cancel_delayed_work(&priv->scan_work);
  384. queue_delayed_work(priv->work_thread, &priv->scan_work,
  385. msecs_to_jiffies(300));
  386. /* skip over GIWSCAN event */
  387. goto out;
  388. }
  389. }
  390. memset(&wrqu, 0, sizeof(union iwreq_data));
  391. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  392. #ifdef CONFIG_LIBERTAS_DEBUG
  393. /* Dump the scan table */
  394. mutex_lock(&priv->lock);
  395. lbs_deb_scan("scan table:\n");
  396. list_for_each_entry(iter, &priv->network_list, list)
  397. lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
  398. i++, iter->bssid, iter->rssi,
  399. print_ssid(ssid, iter->ssid, iter->ssid_len));
  400. mutex_unlock(&priv->lock);
  401. #endif
  402. out2:
  403. priv->scan_channel = 0;
  404. out:
  405. if (priv->connect_status == LBS_CONNECTED) {
  406. netif_carrier_on(priv->dev);
  407. if (!priv->tx_pending_len)
  408. netif_wake_queue(priv->dev);
  409. }
  410. if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
  411. netif_carrier_on(priv->mesh_dev);
  412. if (!priv->tx_pending_len)
  413. netif_wake_queue(priv->mesh_dev);
  414. }
  415. kfree(chan_list);
  416. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  417. return ret;
  418. }
  419. void lbs_scan_worker(struct work_struct *work)
  420. {
  421. struct lbs_private *priv =
  422. container_of(work, struct lbs_private, scan_work.work);
  423. lbs_deb_enter(LBS_DEB_SCAN);
  424. lbs_scan_networks(priv, 0);
  425. lbs_deb_leave(LBS_DEB_SCAN);
  426. }
  427. /*********************************************************************/
  428. /* */
  429. /* Result interpretation */
  430. /* */
  431. /*********************************************************************/
  432. /**
  433. * @brief Interpret a BSS scan response returned from the firmware
  434. *
  435. * Parse the various fixed fields and IEs passed back for a a BSS probe
  436. * response or beacon from the scan command. Record information as needed
  437. * in the scan table struct bss_descriptor for that entry.
  438. *
  439. * @param bss Output parameter: Pointer to the BSS Entry
  440. *
  441. * @return 0 or -1
  442. */
  443. static int lbs_process_bss(struct bss_descriptor *bss,
  444. uint8_t **pbeaconinfo, int *bytesleft)
  445. {
  446. struct ieeetypes_fhparamset *pFH;
  447. struct ieeetypes_dsparamset *pDS;
  448. struct ieeetypes_cfparamset *pCF;
  449. struct ieeetypes_ibssparamset *pibss;
  450. DECLARE_SSID_BUF(ssid);
  451. struct ieeetypes_countryinfoset *pcountryinfo;
  452. uint8_t *pos, *end, *p;
  453. uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
  454. uint16_t beaconsize = 0;
  455. int ret;
  456. lbs_deb_enter(LBS_DEB_SCAN);
  457. if (*bytesleft >= sizeof(beaconsize)) {
  458. /* Extract & convert beacon size from the command buffer */
  459. beaconsize = get_unaligned_le16(*pbeaconinfo);
  460. *bytesleft -= sizeof(beaconsize);
  461. *pbeaconinfo += sizeof(beaconsize);
  462. }
  463. if (beaconsize == 0 || beaconsize > *bytesleft) {
  464. *pbeaconinfo += *bytesleft;
  465. *bytesleft = 0;
  466. ret = -1;
  467. goto done;
  468. }
  469. /* Initialize the current working beacon pointer for this BSS iteration */
  470. pos = *pbeaconinfo;
  471. end = pos + beaconsize;
  472. /* Advance the return beacon pointer past the current beacon */
  473. *pbeaconinfo += beaconsize;
  474. *bytesleft -= beaconsize;
  475. memcpy(bss->bssid, pos, ETH_ALEN);
  476. lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
  477. pos += ETH_ALEN;
  478. if ((end - pos) < 12) {
  479. lbs_deb_scan("process_bss: Not enough bytes left\n");
  480. ret = -1;
  481. goto done;
  482. }
  483. /*
  484. * next 4 fields are RSSI, time stamp, beacon interval,
  485. * and capability information
  486. */
  487. /* RSSI is 1 byte long */
  488. bss->rssi = *pos;
  489. lbs_deb_scan("process_bss: RSSI %d\n", *pos);
  490. pos++;
  491. /* time stamp is 8 bytes long */
  492. pos += 8;
  493. /* beacon interval is 2 bytes long */
  494. bss->beaconperiod = get_unaligned_le16(pos);
  495. pos += 2;
  496. /* capability information is 2 bytes long */
  497. bss->capability = get_unaligned_le16(pos);
  498. lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
  499. pos += 2;
  500. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  501. lbs_deb_scan("process_bss: WEP enabled\n");
  502. if (bss->capability & WLAN_CAPABILITY_IBSS)
  503. bss->mode = IW_MODE_ADHOC;
  504. else
  505. bss->mode = IW_MODE_INFRA;
  506. /* rest of the current buffer are IE's */
  507. lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
  508. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
  509. /* process variable IE */
  510. while (pos <= end - 2) {
  511. if (pos + pos[1] > end) {
  512. lbs_deb_scan("process_bss: error in processing IE, "
  513. "bytes left < IE length\n");
  514. break;
  515. }
  516. switch (pos[0]) {
  517. case WLAN_EID_SSID:
  518. bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
  519. memcpy(bss->ssid, pos + 2, bss->ssid_len);
  520. lbs_deb_scan("got SSID IE: '%s', len %u\n",
  521. print_ssid(ssid, bss->ssid, bss->ssid_len),
  522. bss->ssid_len);
  523. break;
  524. case WLAN_EID_SUPP_RATES:
  525. n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
  526. memcpy(bss->rates, pos + 2, n_basic_rates);
  527. got_basic_rates = 1;
  528. lbs_deb_scan("got RATES IE\n");
  529. break;
  530. case WLAN_EID_FH_PARAMS:
  531. pFH = (struct ieeetypes_fhparamset *) pos;
  532. memmove(&bss->phyparamset.fhparamset, pFH,
  533. sizeof(struct ieeetypes_fhparamset));
  534. lbs_deb_scan("got FH IE\n");
  535. break;
  536. case WLAN_EID_DS_PARAMS:
  537. pDS = (struct ieeetypes_dsparamset *) pos;
  538. bss->channel = pDS->currentchan;
  539. memcpy(&bss->phyparamset.dsparamset, pDS,
  540. sizeof(struct ieeetypes_dsparamset));
  541. lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
  542. break;
  543. case WLAN_EID_CF_PARAMS:
  544. pCF = (struct ieeetypes_cfparamset *) pos;
  545. memcpy(&bss->ssparamset.cfparamset, pCF,
  546. sizeof(struct ieeetypes_cfparamset));
  547. lbs_deb_scan("got CF IE\n");
  548. break;
  549. case WLAN_EID_IBSS_PARAMS:
  550. pibss = (struct ieeetypes_ibssparamset *) pos;
  551. bss->atimwindow = le16_to_cpu(pibss->atimwindow);
  552. memmove(&bss->ssparamset.ibssparamset, pibss,
  553. sizeof(struct ieeetypes_ibssparamset));
  554. lbs_deb_scan("got IBSS IE\n");
  555. break;
  556. case WLAN_EID_COUNTRY:
  557. pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
  558. lbs_deb_scan("got COUNTRY IE\n");
  559. if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
  560. || pcountryinfo->len > 254) {
  561. lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
  562. pcountryinfo->len, sizeof(pcountryinfo->countrycode));
  563. ret = -1;
  564. goto done;
  565. }
  566. memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
  567. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
  568. (uint8_t *) pcountryinfo,
  569. (int) (pcountryinfo->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; bss->rates[j] && (j < sizeof(bss->rates)); 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 = le16_to_cpu(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. }