scan.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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[0].channumber, chan_count);
  261. /* create the fixed part for scan command */
  262. scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
  263. if (scan_cmd == NULL)
  264. goto out;
  265. tlv = scan_cmd->tlvbuffer;
  266. /* TODO: do we need to scan for a specific BSSID?
  267. memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
  268. scan_cmd->bsstype = bsstype;
  269. /* add TLVs */
  270. if (priv->scan_ssid_len)
  271. tlv += lbs_scan_add_ssid_tlv(priv, tlv);
  272. if (chan_list && chan_count)
  273. tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
  274. tlv += lbs_scan_add_rates_tlv(tlv);
  275. /* This is the final data we are about to send */
  276. scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
  277. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  278. sizeof(*scan_cmd));
  279. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  280. tlv - scan_cmd->tlvbuffer);
  281. ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  282. le16_to_cpu(scan_cmd->hdr.size),
  283. lbs_ret_80211_scan, 0);
  284. out:
  285. kfree(scan_cmd);
  286. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  287. return ret;
  288. }
  289. /**
  290. * @brief Internal function used to start a scan based on an input config
  291. *
  292. * Use the input user scan configuration information when provided in
  293. * order to send the appropriate scan commands to firmware to populate or
  294. * update the internal driver scan table
  295. *
  296. * @param priv A pointer to struct lbs_private structure
  297. * @param full_scan Do a full-scan (blocking)
  298. *
  299. * @return 0 or < 0 if error
  300. */
  301. int lbs_scan_networks(struct lbs_private *priv, int full_scan)
  302. {
  303. int ret = -ENOMEM;
  304. struct chanscanparamset *chan_list;
  305. struct chanscanparamset *curr_chans;
  306. int chan_count;
  307. uint8_t bsstype = CMD_BSS_TYPE_ANY;
  308. int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
  309. union iwreq_data wrqu;
  310. #ifdef CONFIG_LIBERTAS_DEBUG
  311. struct bss_descriptor *iter;
  312. int i = 0;
  313. DECLARE_MAC_BUF(mac);
  314. #endif
  315. lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
  316. /* Cancel any partial outstanding partial scans if this scan
  317. * is a full scan.
  318. */
  319. if (full_scan && delayed_work_pending(&priv->scan_work))
  320. cancel_delayed_work(&priv->scan_work);
  321. /* User-specified bsstype or channel list
  322. TODO: this can be implemented if some user-space application
  323. need the feature. Formerly, it was accessible from debugfs,
  324. but then nowhere used.
  325. if (user_cfg) {
  326. if (user_cfg->bsstype)
  327. bsstype = user_cfg->bsstype;
  328. } */
  329. lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
  330. /* Create list of channels to scan */
  331. chan_list = kzalloc(sizeof(struct chanscanparamset) *
  332. LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
  333. if (!chan_list) {
  334. lbs_pr_alert("SCAN: chan_list empty\n");
  335. goto out;
  336. }
  337. /* We want to scan all channels */
  338. chan_count = lbs_scan_create_channel_list(priv, chan_list);
  339. netif_stop_queue(priv->dev);
  340. netif_carrier_off(priv->dev);
  341. if (priv->mesh_dev) {
  342. netif_stop_queue(priv->mesh_dev);
  343. netif_carrier_off(priv->mesh_dev);
  344. }
  345. /* Prepare to continue an interrupted scan */
  346. lbs_deb_scan("chan_count %d, scan_channel %d\n",
  347. chan_count, priv->scan_channel);
  348. curr_chans = chan_list;
  349. /* advance channel list by already-scanned-channels */
  350. if (priv->scan_channel > 0) {
  351. curr_chans += priv->scan_channel;
  352. chan_count -= priv->scan_channel;
  353. }
  354. /* Send scan command(s)
  355. * numchannels contains the number of channels we should maximally scan
  356. * chan_count is the total number of channels to scan
  357. */
  358. while (chan_count) {
  359. int to_scan = min(numchannels, chan_count);
  360. lbs_deb_scan("scanning %d of %d channels\n",
  361. to_scan, chan_count);
  362. ret = lbs_do_scan(priv, bsstype, curr_chans,
  363. to_scan);
  364. if (ret) {
  365. lbs_pr_err("SCAN_CMD failed\n");
  366. goto out2;
  367. }
  368. curr_chans += to_scan;
  369. chan_count -= to_scan;
  370. /* somehow schedule the next part of the scan */
  371. if (chan_count && !full_scan &&
  372. !priv->surpriseremoved) {
  373. /* -1 marks just that we're currently scanning */
  374. if (priv->scan_channel < 0)
  375. priv->scan_channel = to_scan;
  376. else
  377. priv->scan_channel += to_scan;
  378. cancel_delayed_work(&priv->scan_work);
  379. queue_delayed_work(priv->work_thread, &priv->scan_work,
  380. msecs_to_jiffies(300));
  381. /* skip over GIWSCAN event */
  382. goto out;
  383. }
  384. }
  385. memset(&wrqu, 0, sizeof(union iwreq_data));
  386. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  387. #ifdef CONFIG_LIBERTAS_DEBUG
  388. /* Dump the scan table */
  389. mutex_lock(&priv->lock);
  390. lbs_deb_scan("scan table:\n");
  391. list_for_each_entry(iter, &priv->network_list, list)
  392. lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
  393. i++, print_mac(mac, iter->bssid), iter->rssi,
  394. escape_essid(iter->ssid, iter->ssid_len));
  395. mutex_unlock(&priv->lock);
  396. #endif
  397. out2:
  398. priv->scan_channel = 0;
  399. out:
  400. if (priv->connect_status == LBS_CONNECTED) {
  401. netif_carrier_on(priv->dev);
  402. if (!priv->tx_pending_len)
  403. netif_wake_queue(priv->dev);
  404. }
  405. if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
  406. netif_carrier_on(priv->mesh_dev);
  407. if (!priv->tx_pending_len)
  408. netif_wake_queue(priv->mesh_dev);
  409. }
  410. kfree(chan_list);
  411. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  412. return ret;
  413. }
  414. void lbs_scan_worker(struct work_struct *work)
  415. {
  416. struct lbs_private *priv =
  417. container_of(work, struct lbs_private, scan_work.work);
  418. lbs_deb_enter(LBS_DEB_SCAN);
  419. lbs_scan_networks(priv, 0);
  420. lbs_deb_leave(LBS_DEB_SCAN);
  421. }
  422. /*********************************************************************/
  423. /* */
  424. /* Result interpretation */
  425. /* */
  426. /*********************************************************************/
  427. /**
  428. * @brief Interpret a BSS scan response returned from the firmware
  429. *
  430. * Parse the various fixed fields and IEs passed back for a a BSS probe
  431. * response or beacon from the scan command. Record information as needed
  432. * in the scan table struct bss_descriptor for that entry.
  433. *
  434. * @param bss Output parameter: Pointer to the BSS Entry
  435. *
  436. * @return 0 or -1
  437. */
  438. static int lbs_process_bss(struct bss_descriptor *bss,
  439. uint8_t **pbeaconinfo, int *bytesleft)
  440. {
  441. struct ieeetypes_fhparamset *pFH;
  442. struct ieeetypes_dsparamset *pDS;
  443. struct ieeetypes_cfparamset *pCF;
  444. struct ieeetypes_ibssparamset *pibss;
  445. DECLARE_MAC_BUF(mac);
  446. struct ieeetypes_countryinfoset *pcountryinfo;
  447. uint8_t *pos, *end, *p;
  448. uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
  449. uint16_t beaconsize = 0;
  450. int ret;
  451. lbs_deb_enter(LBS_DEB_SCAN);
  452. if (*bytesleft >= sizeof(beaconsize)) {
  453. /* Extract & convert beacon size from the command buffer */
  454. beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
  455. *bytesleft -= sizeof(beaconsize);
  456. *pbeaconinfo += sizeof(beaconsize);
  457. }
  458. if (beaconsize == 0 || beaconsize > *bytesleft) {
  459. *pbeaconinfo += *bytesleft;
  460. *bytesleft = 0;
  461. ret = -1;
  462. goto done;
  463. }
  464. /* Initialize the current working beacon pointer for this BSS iteration */
  465. pos = *pbeaconinfo;
  466. end = pos + beaconsize;
  467. /* Advance the return beacon pointer past the current beacon */
  468. *pbeaconinfo += beaconsize;
  469. *bytesleft -= beaconsize;
  470. memcpy(bss->bssid, pos, ETH_ALEN);
  471. lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
  472. pos += ETH_ALEN;
  473. if ((end - pos) < 12) {
  474. lbs_deb_scan("process_bss: Not enough bytes left\n");
  475. ret = -1;
  476. goto done;
  477. }
  478. /*
  479. * next 4 fields are RSSI, time stamp, beacon interval,
  480. * and capability information
  481. */
  482. /* RSSI is 1 byte long */
  483. bss->rssi = *pos;
  484. lbs_deb_scan("process_bss: RSSI %d\n", *pos);
  485. pos++;
  486. /* time stamp is 8 bytes long */
  487. pos += 8;
  488. /* beacon interval is 2 bytes long */
  489. bss->beaconperiod = le16_to_cpup((void *) pos);
  490. pos += 2;
  491. /* capability information is 2 bytes long */
  492. bss->capability = le16_to_cpup((void *) pos);
  493. lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
  494. pos += 2;
  495. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  496. lbs_deb_scan("process_bss: WEP enabled\n");
  497. if (bss->capability & WLAN_CAPABILITY_IBSS)
  498. bss->mode = IW_MODE_ADHOC;
  499. else
  500. bss->mode = IW_MODE_INFRA;
  501. /* rest of the current buffer are IE's */
  502. lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
  503. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
  504. /* process variable IE */
  505. while (pos <= end - 2) {
  506. struct ieee80211_info_element * elem = (void *)pos;
  507. if (pos + elem->len > end) {
  508. lbs_deb_scan("process_bss: error in processing IE, "
  509. "bytes left < IE length\n");
  510. break;
  511. }
  512. switch (elem->id) {
  513. case MFIE_TYPE_SSID:
  514. bss->ssid_len = elem->len;
  515. memcpy(bss->ssid, elem->data, elem->len);
  516. lbs_deb_scan("got SSID IE: '%s', len %u\n",
  517. escape_essid(bss->ssid, bss->ssid_len),
  518. bss->ssid_len);
  519. break;
  520. case MFIE_TYPE_RATES:
  521. n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
  522. memcpy(bss->rates, elem->data, n_basic_rates);
  523. got_basic_rates = 1;
  524. lbs_deb_scan("got RATES IE\n");
  525. break;
  526. case MFIE_TYPE_FH_SET:
  527. pFH = (struct ieeetypes_fhparamset *) pos;
  528. memmove(&bss->phyparamset.fhparamset, pFH,
  529. sizeof(struct ieeetypes_fhparamset));
  530. lbs_deb_scan("got FH IE\n");
  531. break;
  532. case MFIE_TYPE_DS_SET:
  533. pDS = (struct ieeetypes_dsparamset *) pos;
  534. bss->channel = pDS->currentchan;
  535. memcpy(&bss->phyparamset.dsparamset, pDS,
  536. sizeof(struct ieeetypes_dsparamset));
  537. lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
  538. break;
  539. case MFIE_TYPE_CF_SET:
  540. pCF = (struct ieeetypes_cfparamset *) pos;
  541. memcpy(&bss->ssparamset.cfparamset, pCF,
  542. sizeof(struct ieeetypes_cfparamset));
  543. lbs_deb_scan("got CF IE\n");
  544. break;
  545. case MFIE_TYPE_IBSS_SET:
  546. pibss = (struct ieeetypes_ibssparamset *) pos;
  547. bss->atimwindow = le16_to_cpu(pibss->atimwindow);
  548. memmove(&bss->ssparamset.ibssparamset, pibss,
  549. sizeof(struct ieeetypes_ibssparamset));
  550. lbs_deb_scan("got IBSS IE\n");
  551. break;
  552. case MFIE_TYPE_COUNTRY:
  553. pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
  554. lbs_deb_scan("got COUNTRY IE\n");
  555. if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
  556. || pcountryinfo->len > 254) {
  557. lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
  558. pcountryinfo->len, sizeof(pcountryinfo->countrycode));
  559. ret = -1;
  560. goto done;
  561. }
  562. memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
  563. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
  564. (uint8_t *) pcountryinfo,
  565. (int) (pcountryinfo->len + 2));
  566. break;
  567. case MFIE_TYPE_RATES_EX:
  568. /* only process extended supported rate if data rate is
  569. * already found. Data rate IE should come before
  570. * extended supported rate IE
  571. */
  572. lbs_deb_scan("got RATESEX IE\n");
  573. if (!got_basic_rates) {
  574. lbs_deb_scan("... but ignoring it\n");
  575. break;
  576. }
  577. n_ex_rates = elem->len;
  578. if (n_basic_rates + n_ex_rates > MAX_RATES)
  579. n_ex_rates = MAX_RATES - n_basic_rates;
  580. p = bss->rates + n_basic_rates;
  581. memcpy(p, elem->data, n_ex_rates);
  582. break;
  583. case MFIE_TYPE_GENERIC:
  584. if (elem->len >= 4 &&
  585. elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
  586. elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
  587. bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
  588. memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
  589. lbs_deb_scan("got WPA IE\n");
  590. lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
  591. } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
  592. elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
  593. elem->data[2] == 0x43 && elem->data[3] == 0x04) {
  594. lbs_deb_scan("got mesh IE\n");
  595. bss->mesh = 1;
  596. } else {
  597. lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
  598. elem->data[0], elem->data[1],
  599. elem->data[2], elem->data[3],
  600. elem->len);
  601. }
  602. break;
  603. case MFIE_TYPE_RSN:
  604. lbs_deb_scan("got RSN IE\n");
  605. bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
  606. memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
  607. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
  608. bss->rsn_ie, elem->len);
  609. break;
  610. default:
  611. lbs_deb_scan("got IE 0x%04x, len %d\n",
  612. elem->id, elem->len);
  613. break;
  614. }
  615. pos += elem->len + 2;
  616. }
  617. /* Timestamp */
  618. bss->last_scanned = jiffies;
  619. lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
  620. ret = 0;
  621. done:
  622. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  623. return ret;
  624. }
  625. /**
  626. * @brief Send a scan command for all available channels filtered on a spec
  627. *
  628. * Used in association code and from debugfs
  629. *
  630. * @param priv A pointer to struct lbs_private structure
  631. * @param ssid A pointer to the SSID to scan for
  632. * @param ssid_len Length of the SSID
  633. *
  634. * @return 0-success, otherwise fail
  635. */
  636. int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
  637. uint8_t ssid_len)
  638. {
  639. int ret = 0;
  640. lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
  641. escape_essid(ssid, ssid_len));
  642. if (!ssid_len)
  643. goto out;
  644. memcpy(priv->scan_ssid, ssid, ssid_len);
  645. priv->scan_ssid_len = ssid_len;
  646. lbs_scan_networks(priv, 1);
  647. if (priv->surpriseremoved) {
  648. ret = -1;
  649. goto out;
  650. }
  651. out:
  652. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  653. return ret;
  654. }
  655. /*********************************************************************/
  656. /* */
  657. /* Support for Wireless Extensions */
  658. /* */
  659. /*********************************************************************/
  660. #define MAX_CUSTOM_LEN 64
  661. static inline char *lbs_translate_scan(struct lbs_private *priv,
  662. char *start, char *stop,
  663. struct bss_descriptor *bss)
  664. {
  665. struct chan_freq_power *cfp;
  666. char *current_val; /* For rates */
  667. struct iw_event iwe; /* Temporary buffer */
  668. int j;
  669. #define PERFECT_RSSI ((uint8_t)50)
  670. #define WORST_RSSI ((uint8_t)0)
  671. #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
  672. uint8_t rssi;
  673. lbs_deb_enter(LBS_DEB_SCAN);
  674. cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
  675. if (!cfp) {
  676. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  677. start = NULL;
  678. goto out;
  679. }
  680. /* First entry *MUST* be the BSSID */
  681. iwe.cmd = SIOCGIWAP;
  682. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  683. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  684. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  685. /* SSID */
  686. iwe.cmd = SIOCGIWESSID;
  687. iwe.u.data.flags = 1;
  688. iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
  689. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  690. /* Mode */
  691. iwe.cmd = SIOCGIWMODE;
  692. iwe.u.mode = bss->mode;
  693. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  694. /* Frequency */
  695. iwe.cmd = SIOCGIWFREQ;
  696. iwe.u.freq.m = (long)cfp->freq * 100000;
  697. iwe.u.freq.e = 1;
  698. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  699. /* Add quality statistics */
  700. iwe.cmd = IWEVQUAL;
  701. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  702. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  703. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  704. iwe.u.qual.qual =
  705. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  706. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  707. (RSSI_DIFF * RSSI_DIFF);
  708. if (iwe.u.qual.qual > 100)
  709. iwe.u.qual.qual = 100;
  710. if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  711. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  712. } else {
  713. iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
  714. }
  715. /* Locally created ad-hoc BSSs won't have beacons if this is the
  716. * only station in the adhoc network; so get signal strength
  717. * from receive statistics.
  718. */
  719. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
  720. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  721. priv->curbssparams.ssid_len,
  722. bss->ssid, bss->ssid_len)) {
  723. int snr, nf;
  724. snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  725. nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  726. iwe.u.qual.level = CAL_RSSI(snr, nf);
  727. }
  728. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  729. /* Add encryption capability */
  730. iwe.cmd = SIOCGIWENCODE;
  731. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  732. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  733. } else {
  734. iwe.u.data.flags = IW_ENCODE_DISABLED;
  735. }
  736. iwe.u.data.length = 0;
  737. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  738. current_val = start + IW_EV_LCP_LEN;
  739. iwe.cmd = SIOCGIWRATE;
  740. iwe.u.bitrate.fixed = 0;
  741. iwe.u.bitrate.disabled = 0;
  742. iwe.u.bitrate.value = 0;
  743. for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
  744. /* Bit rate given in 500 kb/s units */
  745. iwe.u.bitrate.value = bss->rates[j] * 500000;
  746. current_val = iwe_stream_add_value(start, current_val,
  747. stop, &iwe, IW_EV_PARAM_LEN);
  748. }
  749. if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
  750. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  751. priv->curbssparams.ssid_len,
  752. bss->ssid, bss->ssid_len)) {
  753. iwe.u.bitrate.value = 22 * 500000;
  754. current_val = iwe_stream_add_value(start, current_val,
  755. stop, &iwe, IW_EV_PARAM_LEN);
  756. }
  757. /* Check if we added any event */
  758. if((current_val - start) > IW_EV_LCP_LEN)
  759. start = current_val;
  760. memset(&iwe, 0, sizeof(iwe));
  761. if (bss->wpa_ie_len) {
  762. char buf[MAX_WPA_IE_LEN];
  763. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  764. iwe.cmd = IWEVGENIE;
  765. iwe.u.data.length = bss->wpa_ie_len;
  766. start = iwe_stream_add_point(start, stop, &iwe, buf);
  767. }
  768. memset(&iwe, 0, sizeof(iwe));
  769. if (bss->rsn_ie_len) {
  770. char buf[MAX_WPA_IE_LEN];
  771. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  772. iwe.cmd = IWEVGENIE;
  773. iwe.u.data.length = bss->rsn_ie_len;
  774. start = iwe_stream_add_point(start, stop, &iwe, buf);
  775. }
  776. if (bss->mesh) {
  777. char custom[MAX_CUSTOM_LEN];
  778. char *p = custom;
  779. iwe.cmd = IWEVCUSTOM;
  780. p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
  781. iwe.u.data.length = p - custom;
  782. if (iwe.u.data.length)
  783. start = iwe_stream_add_point(start, stop, &iwe, custom);
  784. }
  785. out:
  786. lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
  787. return start;
  788. }
  789. /**
  790. * @brief Handle Scan Network ioctl
  791. *
  792. * @param dev A pointer to net_device structure
  793. * @param info A pointer to iw_request_info structure
  794. * @param vwrq A pointer to iw_param structure
  795. * @param extra A pointer to extra data buf
  796. *
  797. * @return 0 --success, otherwise fail
  798. */
  799. int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
  800. union iwreq_data *wrqu, char *extra)
  801. {
  802. struct lbs_private *priv = dev->priv;
  803. int ret = 0;
  804. lbs_deb_enter(LBS_DEB_WEXT);
  805. if (!netif_running(dev)) {
  806. ret = -ENETDOWN;
  807. goto out;
  808. }
  809. /* mac80211 does this:
  810. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  811. if (sdata->type != IEEE80211_IF_TYPE_xxx) {
  812. ret = -EOPNOTSUPP;
  813. goto out;
  814. }
  815. */
  816. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  817. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  818. struct iw_scan_req *req = (struct iw_scan_req *)extra;
  819. priv->scan_ssid_len = req->essid_len;
  820. memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
  821. lbs_deb_wext("set_scan, essid '%s'\n",
  822. escape_essid(priv->scan_ssid, priv->scan_ssid_len));
  823. } else {
  824. priv->scan_ssid_len = 0;
  825. }
  826. if (!delayed_work_pending(&priv->scan_work))
  827. queue_delayed_work(priv->work_thread, &priv->scan_work,
  828. msecs_to_jiffies(50));
  829. /* set marker that currently a scan is taking place */
  830. priv->scan_channel = -1;
  831. if (priv->surpriseremoved)
  832. ret = -EIO;
  833. out:
  834. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  835. return ret;
  836. }
  837. /**
  838. * @brief Handle Retrieve scan table ioctl
  839. *
  840. * @param dev A pointer to net_device structure
  841. * @param info A pointer to iw_request_info structure
  842. * @param dwrq A pointer to iw_point structure
  843. * @param extra A pointer to extra data buf
  844. *
  845. * @return 0 --success, otherwise fail
  846. */
  847. int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
  848. struct iw_point *dwrq, char *extra)
  849. {
  850. #define SCAN_ITEM_SIZE 128
  851. struct lbs_private *priv = dev->priv;
  852. int err = 0;
  853. char *ev = extra;
  854. char *stop = ev + dwrq->length;
  855. struct bss_descriptor *iter_bss;
  856. struct bss_descriptor *safe;
  857. lbs_deb_enter(LBS_DEB_WEXT);
  858. /* iwlist should wait until the current scan is finished */
  859. if (priv->scan_channel)
  860. return -EAGAIN;
  861. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  862. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
  863. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  864. CMD_OPTION_WAITFORRSP, 0, NULL);
  865. mutex_lock(&priv->lock);
  866. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  867. char *next_ev;
  868. unsigned long stale_time;
  869. if (stop - ev < SCAN_ITEM_SIZE) {
  870. err = -E2BIG;
  871. break;
  872. }
  873. /* For mesh device, list only mesh networks */
  874. if (dev == priv->mesh_dev && !iter_bss->mesh)
  875. continue;
  876. /* Prune old an old scan result */
  877. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  878. if (time_after(jiffies, stale_time)) {
  879. list_move_tail(&iter_bss->list, &priv->network_free_list);
  880. clear_bss_descriptor(iter_bss);
  881. continue;
  882. }
  883. /* Translate to WE format this entry */
  884. next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
  885. if (next_ev == NULL)
  886. continue;
  887. ev = next_ev;
  888. }
  889. mutex_unlock(&priv->lock);
  890. dwrq->length = (ev - extra);
  891. dwrq->flags = 0;
  892. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
  893. return err;
  894. }
  895. /*********************************************************************/
  896. /* */
  897. /* Command execution */
  898. /* */
  899. /*********************************************************************/
  900. /**
  901. * @brief This function handles the command response of scan
  902. *
  903. * Called from handle_cmd_response() in cmdrespc.
  904. *
  905. * The response buffer for the scan command has the following
  906. * memory layout:
  907. *
  908. * .-----------------------------------------------------------.
  909. * | header (4 * sizeof(u16)): Standard command response hdr |
  910. * .-----------------------------------------------------------.
  911. * | bufsize (u16) : sizeof the BSS Description data |
  912. * .-----------------------------------------------------------.
  913. * | NumOfSet (u8) : Number of BSS Descs returned |
  914. * .-----------------------------------------------------------.
  915. * | BSSDescription data (variable, size given in bufsize) |
  916. * .-----------------------------------------------------------.
  917. * | TLV data (variable, size calculated using header->size, |
  918. * | bufsize and sizeof the fixed fields above) |
  919. * .-----------------------------------------------------------.
  920. *
  921. * @param priv A pointer to struct lbs_private structure
  922. * @param resp A pointer to cmd_ds_command
  923. *
  924. * @return 0 or -1
  925. */
  926. static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
  927. struct cmd_header *resp)
  928. {
  929. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  930. struct bss_descriptor *iter_bss;
  931. struct bss_descriptor *safe;
  932. uint8_t *bssinfo;
  933. uint16_t scanrespsize;
  934. int bytesleft;
  935. int idx;
  936. int tlvbufsize;
  937. int ret;
  938. lbs_deb_enter(LBS_DEB_SCAN);
  939. /* Prune old entries from scan table */
  940. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  941. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  942. if (time_before(jiffies, stale_time))
  943. continue;
  944. list_move_tail (&iter_bss->list, &priv->network_free_list);
  945. clear_bss_descriptor(iter_bss);
  946. }
  947. if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
  948. lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
  949. scanresp->nr_sets, MAX_NETWORK_COUNT);
  950. ret = -1;
  951. goto done;
  952. }
  953. bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
  954. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  955. scanrespsize = le16_to_cpu(resp->size);
  956. lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
  957. bssinfo = scanresp->bssdesc_and_tlvbuffer;
  958. /* The size of the TLV buffer is equal to the entire command response
  959. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  960. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  961. * response header (S_DS_GEN)
  962. */
  963. tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
  964. + sizeof(scanresp->nr_sets)
  965. + S_DS_GEN);
  966. /*
  967. * Process each scan response returned (scanresp->nr_sets). Save
  968. * the information in the newbssentry and then insert into the
  969. * driver scan table either as an update to an existing entry
  970. * or as an addition at the end of the table
  971. */
  972. for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
  973. struct bss_descriptor new;
  974. struct bss_descriptor *found = NULL;
  975. struct bss_descriptor *oldest = NULL;
  976. DECLARE_MAC_BUF(mac);
  977. /* Process the data fields and IEs returned for this BSS */
  978. memset(&new, 0, sizeof (struct bss_descriptor));
  979. if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
  980. /* error parsing the scan response, skipped */
  981. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  982. continue;
  983. }
  984. /* Try to find this bss in the scan table */
  985. list_for_each_entry (iter_bss, &priv->network_list, list) {
  986. if (is_same_network(iter_bss, &new)) {
  987. found = iter_bss;
  988. break;
  989. }
  990. if ((oldest == NULL) ||
  991. (iter_bss->last_scanned < oldest->last_scanned))
  992. oldest = iter_bss;
  993. }
  994. if (found) {
  995. /* found, clear it */
  996. clear_bss_descriptor(found);
  997. } else if (!list_empty(&priv->network_free_list)) {
  998. /* Pull one from the free list */
  999. found = list_entry(priv->network_free_list.next,
  1000. struct bss_descriptor, list);
  1001. list_move_tail(&found->list, &priv->network_list);
  1002. } else if (oldest) {
  1003. /* If there are no more slots, expire the oldest */
  1004. found = oldest;
  1005. clear_bss_descriptor(found);
  1006. list_move_tail(&found->list, &priv->network_list);
  1007. } else {
  1008. continue;
  1009. }
  1010. lbs_deb_scan("SCAN_RESP: BSSID %s\n", print_mac(mac, new.bssid));
  1011. /* Copy the locally created newbssentry to the scan table */
  1012. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1013. }
  1014. ret = 0;
  1015. done:
  1016. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1017. return ret;
  1018. }