scan.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 = le16_to_cpup((void *) pos);
  491. pos += 2;
  492. /* capability information is 2 bytes long */
  493. bss->capability = le16_to_cpup((void *) 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. char *start, char *stop,
  664. struct bss_descriptor *bss)
  665. {
  666. struct chan_freq_power *cfp;
  667. char *current_val; /* For rates */
  668. struct iw_event iwe; /* Temporary buffer */
  669. int j;
  670. #define PERFECT_RSSI ((uint8_t)50)
  671. #define WORST_RSSI ((uint8_t)0)
  672. #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
  673. uint8_t rssi;
  674. lbs_deb_enter(LBS_DEB_SCAN);
  675. cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
  676. if (!cfp) {
  677. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  678. start = NULL;
  679. goto out;
  680. }
  681. /* First entry *MUST* be the BSSID */
  682. iwe.cmd = SIOCGIWAP;
  683. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  684. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  685. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  686. /* SSID */
  687. iwe.cmd = SIOCGIWESSID;
  688. iwe.u.data.flags = 1;
  689. iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
  690. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  691. /* Mode */
  692. iwe.cmd = SIOCGIWMODE;
  693. iwe.u.mode = bss->mode;
  694. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  695. /* Frequency */
  696. iwe.cmd = SIOCGIWFREQ;
  697. iwe.u.freq.m = (long)cfp->freq * 100000;
  698. iwe.u.freq.e = 1;
  699. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  700. /* Add quality statistics */
  701. iwe.cmd = IWEVQUAL;
  702. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  703. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  704. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  705. iwe.u.qual.qual =
  706. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  707. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  708. (RSSI_DIFF * RSSI_DIFF);
  709. if (iwe.u.qual.qual > 100)
  710. iwe.u.qual.qual = 100;
  711. if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  712. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  713. } else {
  714. iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
  715. }
  716. /* Locally created ad-hoc BSSs won't have beacons if this is the
  717. * only station in the adhoc network; so get signal strength
  718. * from receive statistics.
  719. */
  720. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
  721. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  722. priv->curbssparams.ssid_len,
  723. bss->ssid, bss->ssid_len)) {
  724. int snr, nf;
  725. snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  726. nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  727. iwe.u.qual.level = CAL_RSSI(snr, nf);
  728. }
  729. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  730. /* Add encryption capability */
  731. iwe.cmd = SIOCGIWENCODE;
  732. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  733. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  734. } else {
  735. iwe.u.data.flags = IW_ENCODE_DISABLED;
  736. }
  737. iwe.u.data.length = 0;
  738. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  739. current_val = start + IW_EV_LCP_LEN;
  740. iwe.cmd = SIOCGIWRATE;
  741. iwe.u.bitrate.fixed = 0;
  742. iwe.u.bitrate.disabled = 0;
  743. iwe.u.bitrate.value = 0;
  744. for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
  745. /* Bit rate given in 500 kb/s units */
  746. iwe.u.bitrate.value = bss->rates[j] * 500000;
  747. current_val = iwe_stream_add_value(start, current_val,
  748. stop, &iwe, IW_EV_PARAM_LEN);
  749. }
  750. if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
  751. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  752. priv->curbssparams.ssid_len,
  753. bss->ssid, bss->ssid_len)) {
  754. iwe.u.bitrate.value = 22 * 500000;
  755. current_val = iwe_stream_add_value(start, current_val,
  756. stop, &iwe, IW_EV_PARAM_LEN);
  757. }
  758. /* Check if we added any event */
  759. if((current_val - start) > IW_EV_LCP_LEN)
  760. start = current_val;
  761. memset(&iwe, 0, sizeof(iwe));
  762. if (bss->wpa_ie_len) {
  763. char buf[MAX_WPA_IE_LEN];
  764. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  765. iwe.cmd = IWEVGENIE;
  766. iwe.u.data.length = bss->wpa_ie_len;
  767. start = iwe_stream_add_point(start, stop, &iwe, buf);
  768. }
  769. memset(&iwe, 0, sizeof(iwe));
  770. if (bss->rsn_ie_len) {
  771. char buf[MAX_WPA_IE_LEN];
  772. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  773. iwe.cmd = IWEVGENIE;
  774. iwe.u.data.length = bss->rsn_ie_len;
  775. start = iwe_stream_add_point(start, stop, &iwe, buf);
  776. }
  777. if (bss->mesh) {
  778. char custom[MAX_CUSTOM_LEN];
  779. char *p = custom;
  780. iwe.cmd = IWEVCUSTOM;
  781. p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
  782. iwe.u.data.length = p - custom;
  783. if (iwe.u.data.length)
  784. start = iwe_stream_add_point(start, stop, &iwe, custom);
  785. }
  786. out:
  787. lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
  788. return start;
  789. }
  790. /**
  791. * @brief Handle Scan Network ioctl
  792. *
  793. * @param dev A pointer to net_device structure
  794. * @param info A pointer to iw_request_info structure
  795. * @param vwrq A pointer to iw_param structure
  796. * @param extra A pointer to extra data buf
  797. *
  798. * @return 0 --success, otherwise fail
  799. */
  800. int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
  801. union iwreq_data *wrqu, char *extra)
  802. {
  803. struct lbs_private *priv = dev->priv;
  804. int ret = 0;
  805. lbs_deb_enter(LBS_DEB_WEXT);
  806. if (!netif_running(dev)) {
  807. ret = -ENETDOWN;
  808. goto out;
  809. }
  810. /* mac80211 does this:
  811. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  812. if (sdata->type != IEEE80211_IF_TYPE_xxx) {
  813. ret = -EOPNOTSUPP;
  814. goto out;
  815. }
  816. */
  817. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  818. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  819. struct iw_scan_req *req = (struct iw_scan_req *)extra;
  820. priv->scan_ssid_len = req->essid_len;
  821. memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
  822. lbs_deb_wext("set_scan, essid '%s'\n",
  823. escape_essid(priv->scan_ssid, priv->scan_ssid_len));
  824. } else {
  825. priv->scan_ssid_len = 0;
  826. }
  827. if (!delayed_work_pending(&priv->scan_work))
  828. queue_delayed_work(priv->work_thread, &priv->scan_work,
  829. msecs_to_jiffies(50));
  830. /* set marker that currently a scan is taking place */
  831. priv->scan_channel = -1;
  832. if (priv->surpriseremoved)
  833. ret = -EIO;
  834. out:
  835. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  836. return ret;
  837. }
  838. /**
  839. * @brief Handle Retrieve scan table ioctl
  840. *
  841. * @param dev A pointer to net_device structure
  842. * @param info A pointer to iw_request_info structure
  843. * @param dwrq A pointer to iw_point structure
  844. * @param extra A pointer to extra data buf
  845. *
  846. * @return 0 --success, otherwise fail
  847. */
  848. int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
  849. struct iw_point *dwrq, char *extra)
  850. {
  851. #define SCAN_ITEM_SIZE 128
  852. struct lbs_private *priv = dev->priv;
  853. int err = 0;
  854. char *ev = extra;
  855. char *stop = ev + dwrq->length;
  856. struct bss_descriptor *iter_bss;
  857. struct bss_descriptor *safe;
  858. lbs_deb_enter(LBS_DEB_WEXT);
  859. /* iwlist should wait until the current scan is finished */
  860. if (priv->scan_channel)
  861. return -EAGAIN;
  862. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  863. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
  864. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  865. CMD_OPTION_WAITFORRSP, 0, NULL);
  866. mutex_lock(&priv->lock);
  867. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  868. char *next_ev;
  869. unsigned long stale_time;
  870. if (stop - ev < SCAN_ITEM_SIZE) {
  871. err = -E2BIG;
  872. break;
  873. }
  874. /* For mesh device, list only mesh networks */
  875. if (dev == priv->mesh_dev && !iter_bss->mesh)
  876. continue;
  877. /* Prune old an old scan result */
  878. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  879. if (time_after(jiffies, stale_time)) {
  880. list_move_tail(&iter_bss->list, &priv->network_free_list);
  881. clear_bss_descriptor(iter_bss);
  882. continue;
  883. }
  884. /* Translate to WE format this entry */
  885. next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
  886. if (next_ev == NULL)
  887. continue;
  888. ev = next_ev;
  889. }
  890. mutex_unlock(&priv->lock);
  891. dwrq->length = (ev - extra);
  892. dwrq->flags = 0;
  893. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
  894. return err;
  895. }
  896. /*********************************************************************/
  897. /* */
  898. /* Command execution */
  899. /* */
  900. /*********************************************************************/
  901. /**
  902. * @brief This function handles the command response of scan
  903. *
  904. * Called from handle_cmd_response() in cmdrespc.
  905. *
  906. * The response buffer for the scan command has the following
  907. * memory layout:
  908. *
  909. * .-----------------------------------------------------------.
  910. * | header (4 * sizeof(u16)): Standard command response hdr |
  911. * .-----------------------------------------------------------.
  912. * | bufsize (u16) : sizeof the BSS Description data |
  913. * .-----------------------------------------------------------.
  914. * | NumOfSet (u8) : Number of BSS Descs returned |
  915. * .-----------------------------------------------------------.
  916. * | BSSDescription data (variable, size given in bufsize) |
  917. * .-----------------------------------------------------------.
  918. * | TLV data (variable, size calculated using header->size, |
  919. * | bufsize and sizeof the fixed fields above) |
  920. * .-----------------------------------------------------------.
  921. *
  922. * @param priv A pointer to struct lbs_private structure
  923. * @param resp A pointer to cmd_ds_command
  924. *
  925. * @return 0 or -1
  926. */
  927. static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
  928. struct cmd_header *resp)
  929. {
  930. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  931. struct bss_descriptor *iter_bss;
  932. struct bss_descriptor *safe;
  933. uint8_t *bssinfo;
  934. uint16_t scanrespsize;
  935. int bytesleft;
  936. int idx;
  937. int tlvbufsize;
  938. int ret;
  939. lbs_deb_enter(LBS_DEB_SCAN);
  940. /* Prune old entries from scan table */
  941. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  942. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  943. if (time_before(jiffies, stale_time))
  944. continue;
  945. list_move_tail (&iter_bss->list, &priv->network_free_list);
  946. clear_bss_descriptor(iter_bss);
  947. }
  948. if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
  949. lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
  950. scanresp->nr_sets, MAX_NETWORK_COUNT);
  951. ret = -1;
  952. goto done;
  953. }
  954. bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
  955. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  956. scanrespsize = le16_to_cpu(resp->size);
  957. lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
  958. bssinfo = scanresp->bssdesc_and_tlvbuffer;
  959. /* The size of the TLV buffer is equal to the entire command response
  960. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  961. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  962. * response header (S_DS_GEN)
  963. */
  964. tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
  965. + sizeof(scanresp->nr_sets)
  966. + S_DS_GEN);
  967. /*
  968. * Process each scan response returned (scanresp->nr_sets). Save
  969. * the information in the newbssentry and then insert into the
  970. * driver scan table either as an update to an existing entry
  971. * or as an addition at the end of the table
  972. */
  973. for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
  974. struct bss_descriptor new;
  975. struct bss_descriptor *found = NULL;
  976. struct bss_descriptor *oldest = NULL;
  977. DECLARE_MAC_BUF(mac);
  978. /* Process the data fields and IEs returned for this BSS */
  979. memset(&new, 0, sizeof (struct bss_descriptor));
  980. if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
  981. /* error parsing the scan response, skipped */
  982. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  983. continue;
  984. }
  985. /* Try to find this bss in the scan table */
  986. list_for_each_entry (iter_bss, &priv->network_list, list) {
  987. if (is_same_network(iter_bss, &new)) {
  988. found = iter_bss;
  989. break;
  990. }
  991. if ((oldest == NULL) ||
  992. (iter_bss->last_scanned < oldest->last_scanned))
  993. oldest = iter_bss;
  994. }
  995. if (found) {
  996. /* found, clear it */
  997. clear_bss_descriptor(found);
  998. } else if (!list_empty(&priv->network_free_list)) {
  999. /* Pull one from the free list */
  1000. found = list_entry(priv->network_free_list.next,
  1001. struct bss_descriptor, list);
  1002. list_move_tail(&found->list, &priv->network_list);
  1003. } else if (oldest) {
  1004. /* If there are no more slots, expire the oldest */
  1005. found = oldest;
  1006. clear_bss_descriptor(found);
  1007. list_move_tail(&found->list, &priv->network_list);
  1008. } else {
  1009. continue;
  1010. }
  1011. lbs_deb_scan("SCAN_RESP: BSSID %s\n", print_mac(mac, new.bssid));
  1012. /* Copy the locally created newbssentry to the scan table */
  1013. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1014. }
  1015. ret = 0;
  1016. done:
  1017. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1018. return ret;
  1019. }