scan.c 35 KB

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