scan.c 39 KB

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