scan.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 (!lbs_is_cmd_allowed(priv)) {
  815. ret = -EBUSY;
  816. goto out;
  817. }
  818. if (!priv->radio_on) {
  819. ret = -EINVAL;
  820. goto out;
  821. }
  822. if (!netif_running(dev)) {
  823. ret = -ENETDOWN;
  824. goto out;
  825. }
  826. /* mac80211 does this:
  827. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  828. if (sdata->type != IEEE80211_IF_TYPE_xxx) {
  829. ret = -EOPNOTSUPP;
  830. goto out;
  831. }
  832. */
  833. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  834. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  835. struct iw_scan_req *req = (struct iw_scan_req *)extra;
  836. priv->scan_ssid_len = req->essid_len;
  837. memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
  838. lbs_deb_wext("set_scan, essid '%s'\n",
  839. print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
  840. } else {
  841. priv->scan_ssid_len = 0;
  842. }
  843. if (!delayed_work_pending(&priv->scan_work))
  844. queue_delayed_work(priv->work_thread, &priv->scan_work,
  845. msecs_to_jiffies(50));
  846. /* set marker that currently a scan is taking place */
  847. priv->scan_channel = -1;
  848. if (priv->surpriseremoved)
  849. ret = -EIO;
  850. out:
  851. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  852. return ret;
  853. }
  854. /**
  855. * @brief Handle Retrieve scan table ioctl
  856. *
  857. * @param dev A pointer to net_device structure
  858. * @param info A pointer to iw_request_info structure
  859. * @param dwrq A pointer to iw_point structure
  860. * @param extra A pointer to extra data buf
  861. *
  862. * @return 0 --success, otherwise fail
  863. */
  864. int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
  865. struct iw_point *dwrq, char *extra)
  866. {
  867. #define SCAN_ITEM_SIZE 128
  868. struct lbs_private *priv = dev->ml_priv;
  869. int err = 0;
  870. char *ev = extra;
  871. char *stop = ev + dwrq->length;
  872. struct bss_descriptor *iter_bss;
  873. struct bss_descriptor *safe;
  874. lbs_deb_enter(LBS_DEB_WEXT);
  875. if (!lbs_is_cmd_allowed(priv)) {
  876. err = -EBUSY;
  877. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
  878. return err;
  879. }
  880. /* iwlist should wait until the current scan is finished */
  881. if (priv->scan_channel)
  882. return -EAGAIN;
  883. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  884. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
  885. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  886. CMD_OPTION_WAITFORRSP, 0, NULL);
  887. mutex_lock(&priv->lock);
  888. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  889. char *next_ev;
  890. unsigned long stale_time;
  891. if (stop - ev < SCAN_ITEM_SIZE) {
  892. err = -E2BIG;
  893. break;
  894. }
  895. /* For mesh device, list only mesh networks */
  896. if (dev == priv->mesh_dev && !iter_bss->mesh)
  897. continue;
  898. /* Prune old an old scan result */
  899. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  900. if (time_after(jiffies, stale_time)) {
  901. list_move_tail(&iter_bss->list, &priv->network_free_list);
  902. clear_bss_descriptor(iter_bss);
  903. continue;
  904. }
  905. /* Translate to WE format this entry */
  906. next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
  907. if (next_ev == NULL)
  908. continue;
  909. ev = next_ev;
  910. }
  911. mutex_unlock(&priv->lock);
  912. dwrq->length = (ev - extra);
  913. dwrq->flags = 0;
  914. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
  915. return err;
  916. }
  917. /*********************************************************************/
  918. /* */
  919. /* Command execution */
  920. /* */
  921. /*********************************************************************/
  922. /**
  923. * @brief This function handles the command response of scan
  924. *
  925. * Called from handle_cmd_response() in cmdrespc.
  926. *
  927. * The response buffer for the scan command has the following
  928. * memory layout:
  929. *
  930. * .-----------------------------------------------------------.
  931. * | header (4 * sizeof(u16)): Standard command response hdr |
  932. * .-----------------------------------------------------------.
  933. * | bufsize (u16) : sizeof the BSS Description data |
  934. * .-----------------------------------------------------------.
  935. * | NumOfSet (u8) : Number of BSS Descs returned |
  936. * .-----------------------------------------------------------.
  937. * | BSSDescription data (variable, size given in bufsize) |
  938. * .-----------------------------------------------------------.
  939. * | TLV data (variable, size calculated using header->size, |
  940. * | bufsize and sizeof the fixed fields above) |
  941. * .-----------------------------------------------------------.
  942. *
  943. * @param priv A pointer to struct lbs_private structure
  944. * @param resp A pointer to cmd_ds_command
  945. *
  946. * @return 0 or -1
  947. */
  948. static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
  949. struct cmd_header *resp)
  950. {
  951. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  952. struct bss_descriptor *iter_bss;
  953. struct bss_descriptor *safe;
  954. uint8_t *bssinfo;
  955. uint16_t scanrespsize;
  956. int bytesleft;
  957. int idx;
  958. int tlvbufsize;
  959. int ret;
  960. lbs_deb_enter(LBS_DEB_SCAN);
  961. /* Prune old entries from scan table */
  962. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  963. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  964. if (time_before(jiffies, stale_time))
  965. continue;
  966. list_move_tail (&iter_bss->list, &priv->network_free_list);
  967. clear_bss_descriptor(iter_bss);
  968. }
  969. if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
  970. lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
  971. scanresp->nr_sets, MAX_NETWORK_COUNT);
  972. ret = -1;
  973. goto done;
  974. }
  975. bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
  976. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  977. scanrespsize = le16_to_cpu(resp->size);
  978. lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
  979. bssinfo = scanresp->bssdesc_and_tlvbuffer;
  980. /* The size of the TLV buffer is equal to the entire command response
  981. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  982. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  983. * response header (S_DS_GEN)
  984. */
  985. tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
  986. + sizeof(scanresp->nr_sets)
  987. + S_DS_GEN);
  988. /*
  989. * Process each scan response returned (scanresp->nr_sets). Save
  990. * the information in the newbssentry and then insert into the
  991. * driver scan table either as an update to an existing entry
  992. * or as an addition at the end of the table
  993. */
  994. for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
  995. struct bss_descriptor new;
  996. struct bss_descriptor *found = NULL;
  997. struct bss_descriptor *oldest = NULL;
  998. /* Process the data fields and IEs returned for this BSS */
  999. memset(&new, 0, sizeof (struct bss_descriptor));
  1000. if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
  1001. /* error parsing the scan response, skipped */
  1002. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1003. continue;
  1004. }
  1005. /* Try to find this bss in the scan table */
  1006. list_for_each_entry (iter_bss, &priv->network_list, list) {
  1007. if (is_same_network(iter_bss, &new)) {
  1008. found = iter_bss;
  1009. break;
  1010. }
  1011. if ((oldest == NULL) ||
  1012. (iter_bss->last_scanned < oldest->last_scanned))
  1013. oldest = iter_bss;
  1014. }
  1015. if (found) {
  1016. /* found, clear it */
  1017. clear_bss_descriptor(found);
  1018. } else if (!list_empty(&priv->network_free_list)) {
  1019. /* Pull one from the free list */
  1020. found = list_entry(priv->network_free_list.next,
  1021. struct bss_descriptor, list);
  1022. list_move_tail(&found->list, &priv->network_list);
  1023. } else if (oldest) {
  1024. /* If there are no more slots, expire the oldest */
  1025. found = oldest;
  1026. clear_bss_descriptor(found);
  1027. list_move_tail(&found->list, &priv->network_list);
  1028. } else {
  1029. continue;
  1030. }
  1031. lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
  1032. /* Copy the locally created newbssentry to the scan table */
  1033. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1034. }
  1035. ret = 0;
  1036. done:
  1037. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1038. return ret;
  1039. }