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