scan.c 39 KB

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