scan.c 35 KB

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