scan.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. netif_carrier_off(priv->dev);
  496. if (priv->mesh_dev) {
  497. netif_stop_queue(priv->mesh_dev);
  498. netif_carrier_off(priv->mesh_dev);
  499. }
  500. /* Prepare to continue an interrupted scan */
  501. lbs_deb_scan("chan_count %d, scan_channel %d\n",
  502. chan_count, priv->scan_channel);
  503. curr_chans = chan_list;
  504. /* advance channel list by already-scanned-channels */
  505. if (priv->scan_channel > 0) {
  506. curr_chans += priv->scan_channel;
  507. chan_count -= priv->scan_channel;
  508. }
  509. /* Send scan command(s)
  510. * numchannels contains the number of channels we should maximally scan
  511. * chan_count is the total number of channels to scan
  512. */
  513. while (chan_count) {
  514. int to_scan = min(numchannels, chan_count);
  515. lbs_deb_scan("scanning %d of %d channels\n",
  516. to_scan, chan_count);
  517. ret = lbs_do_scan(priv, bsstype, curr_chans,
  518. to_scan);
  519. if (ret) {
  520. lbs_pr_err("SCAN_CMD failed\n");
  521. goto out2;
  522. }
  523. curr_chans += to_scan;
  524. chan_count -= to_scan;
  525. /* somehow schedule the next part of the scan */
  526. if (chan_count && !full_scan &&
  527. !priv->surpriseremoved) {
  528. /* -1 marks just that we're currently scanning */
  529. if (priv->scan_channel < 0)
  530. priv->scan_channel = to_scan;
  531. else
  532. priv->scan_channel += to_scan;
  533. cancel_delayed_work(&priv->scan_work);
  534. queue_delayed_work(priv->work_thread, &priv->scan_work,
  535. msecs_to_jiffies(300));
  536. /* skip over GIWSCAN event */
  537. goto out;
  538. }
  539. }
  540. memset(&wrqu, 0, sizeof(union iwreq_data));
  541. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  542. #ifdef CONFIG_LIBERTAS_DEBUG
  543. /* Dump the scan table */
  544. mutex_lock(&priv->lock);
  545. lbs_deb_scan("scan table:\n");
  546. list_for_each_entry(iter, &priv->network_list, list)
  547. lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
  548. i++, iter->bssid, iter->rssi,
  549. print_ssid(ssid, iter->ssid, iter->ssid_len));
  550. mutex_unlock(&priv->lock);
  551. #endif
  552. out2:
  553. priv->scan_channel = 0;
  554. out:
  555. if (priv->connect_status == LBS_CONNECTED) {
  556. netif_carrier_on(priv->dev);
  557. if (!priv->tx_pending_len)
  558. netif_wake_queue(priv->dev);
  559. }
  560. if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
  561. netif_carrier_on(priv->mesh_dev);
  562. if (!priv->tx_pending_len)
  563. netif_wake_queue(priv->mesh_dev);
  564. }
  565. kfree(chan_list);
  566. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  567. return ret;
  568. }
  569. void lbs_scan_worker(struct work_struct *work)
  570. {
  571. struct lbs_private *priv =
  572. container_of(work, struct lbs_private, scan_work.work);
  573. lbs_deb_enter(LBS_DEB_SCAN);
  574. lbs_scan_networks(priv, 0);
  575. lbs_deb_leave(LBS_DEB_SCAN);
  576. }
  577. /*********************************************************************/
  578. /* */
  579. /* Result interpretation */
  580. /* */
  581. /*********************************************************************/
  582. /**
  583. * @brief Interpret a BSS scan response returned from the firmware
  584. *
  585. * Parse the various fixed fields and IEs passed back for a a BSS probe
  586. * response or beacon from the scan command. Record information as needed
  587. * in the scan table struct bss_descriptor for that entry.
  588. *
  589. * @param bss Output parameter: Pointer to the BSS Entry
  590. *
  591. * @return 0 or -1
  592. */
  593. static int lbs_process_bss(struct bss_descriptor *bss,
  594. uint8_t **pbeaconinfo, int *bytesleft)
  595. {
  596. struct ieee_ie_fh_param_set *fh;
  597. struct ieee_ie_ds_param_set *ds;
  598. struct ieee_ie_cf_param_set *cf;
  599. struct ieee_ie_ibss_param_set *ibss;
  600. DECLARE_SSID_BUF(ssid);
  601. uint8_t *pos, *end, *p;
  602. uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
  603. uint16_t beaconsize = 0;
  604. int ret;
  605. lbs_deb_enter(LBS_DEB_SCAN);
  606. if (*bytesleft >= sizeof(beaconsize)) {
  607. /* Extract & convert beacon size from the command buffer */
  608. beaconsize = get_unaligned_le16(*pbeaconinfo);
  609. *bytesleft -= sizeof(beaconsize);
  610. *pbeaconinfo += sizeof(beaconsize);
  611. }
  612. if (beaconsize == 0 || beaconsize > *bytesleft) {
  613. *pbeaconinfo += *bytesleft;
  614. *bytesleft = 0;
  615. ret = -1;
  616. goto done;
  617. }
  618. /* Initialize the current working beacon pointer for this BSS iteration */
  619. pos = *pbeaconinfo;
  620. end = pos + beaconsize;
  621. /* Advance the return beacon pointer past the current beacon */
  622. *pbeaconinfo += beaconsize;
  623. *bytesleft -= beaconsize;
  624. memcpy(bss->bssid, pos, ETH_ALEN);
  625. lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
  626. pos += ETH_ALEN;
  627. if ((end - pos) < 12) {
  628. lbs_deb_scan("process_bss: Not enough bytes left\n");
  629. ret = -1;
  630. goto done;
  631. }
  632. /*
  633. * next 4 fields are RSSI, time stamp, beacon interval,
  634. * and capability information
  635. */
  636. /* RSSI is 1 byte long */
  637. bss->rssi = *pos;
  638. lbs_deb_scan("process_bss: RSSI %d\n", *pos);
  639. pos++;
  640. /* time stamp is 8 bytes long */
  641. pos += 8;
  642. /* beacon interval is 2 bytes long */
  643. bss->beaconperiod = get_unaligned_le16(pos);
  644. pos += 2;
  645. /* capability information is 2 bytes long */
  646. bss->capability = get_unaligned_le16(pos);
  647. lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
  648. pos += 2;
  649. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  650. lbs_deb_scan("process_bss: WEP enabled\n");
  651. if (bss->capability & WLAN_CAPABILITY_IBSS)
  652. bss->mode = IW_MODE_ADHOC;
  653. else
  654. bss->mode = IW_MODE_INFRA;
  655. /* rest of the current buffer are IE's */
  656. lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
  657. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
  658. /* process variable IE */
  659. while (pos <= end - 2) {
  660. if (pos + pos[1] > end) {
  661. lbs_deb_scan("process_bss: error in processing IE, "
  662. "bytes left < IE length\n");
  663. break;
  664. }
  665. switch (pos[0]) {
  666. case WLAN_EID_SSID:
  667. bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
  668. memcpy(bss->ssid, pos + 2, bss->ssid_len);
  669. lbs_deb_scan("got SSID IE: '%s', len %u\n",
  670. print_ssid(ssid, bss->ssid, bss->ssid_len),
  671. bss->ssid_len);
  672. break;
  673. case WLAN_EID_SUPP_RATES:
  674. n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
  675. memcpy(bss->rates, pos + 2, n_basic_rates);
  676. got_basic_rates = 1;
  677. lbs_deb_scan("got RATES IE\n");
  678. break;
  679. case WLAN_EID_FH_PARAMS:
  680. fh = (struct ieee_ie_fh_param_set *) pos;
  681. memcpy(&bss->phy.fh, fh, sizeof(*fh));
  682. lbs_deb_scan("got FH IE\n");
  683. break;
  684. case WLAN_EID_DS_PARAMS:
  685. ds = (struct ieee_ie_ds_param_set *) pos;
  686. bss->channel = ds->channel;
  687. memcpy(&bss->phy.ds, ds, sizeof(*ds));
  688. lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
  689. break;
  690. case WLAN_EID_CF_PARAMS:
  691. cf = (struct ieee_ie_cf_param_set *) pos;
  692. memcpy(&bss->ss.cf, cf, sizeof(*cf));
  693. lbs_deb_scan("got CF IE\n");
  694. break;
  695. case WLAN_EID_IBSS_PARAMS:
  696. ibss = (struct ieee_ie_ibss_param_set *) pos;
  697. bss->atimwindow = ibss->atimwindow;
  698. memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
  699. lbs_deb_scan("got IBSS IE\n");
  700. break;
  701. case WLAN_EID_EXT_SUPP_RATES:
  702. /* only process extended supported rate if data rate is
  703. * already found. Data rate IE should come before
  704. * extended supported rate IE
  705. */
  706. lbs_deb_scan("got RATESEX IE\n");
  707. if (!got_basic_rates) {
  708. lbs_deb_scan("... but ignoring it\n");
  709. break;
  710. }
  711. n_ex_rates = pos[1];
  712. if (n_basic_rates + n_ex_rates > MAX_RATES)
  713. n_ex_rates = MAX_RATES - n_basic_rates;
  714. p = bss->rates + n_basic_rates;
  715. memcpy(p, pos + 2, n_ex_rates);
  716. break;
  717. case WLAN_EID_GENERIC:
  718. if (pos[1] >= 4 &&
  719. pos[2] == 0x00 && pos[3] == 0x50 &&
  720. pos[4] == 0xf2 && pos[5] == 0x01) {
  721. bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
  722. memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
  723. lbs_deb_scan("got WPA IE\n");
  724. lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
  725. bss->wpa_ie_len);
  726. } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
  727. pos[2] == 0x00 && pos[3] == 0x50 &&
  728. pos[4] == 0x43 && pos[5] == 0x04) {
  729. lbs_deb_scan("got mesh IE\n");
  730. bss->mesh = 1;
  731. } else {
  732. lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
  733. pos[2], pos[3],
  734. pos[4], pos[5],
  735. pos[1]);
  736. }
  737. break;
  738. case WLAN_EID_RSN:
  739. lbs_deb_scan("got RSN IE\n");
  740. bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
  741. memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
  742. lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
  743. bss->rsn_ie, bss->rsn_ie_len);
  744. break;
  745. default:
  746. lbs_deb_scan("got IE 0x%04x, len %d\n",
  747. pos[0], pos[1]);
  748. break;
  749. }
  750. pos += pos[1] + 2;
  751. }
  752. /* Timestamp */
  753. bss->last_scanned = jiffies;
  754. lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
  755. ret = 0;
  756. done:
  757. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  758. return ret;
  759. }
  760. /**
  761. * @brief Send a scan command for all available channels filtered on a spec
  762. *
  763. * Used in association code and from debugfs
  764. *
  765. * @param priv A pointer to struct lbs_private structure
  766. * @param ssid A pointer to the SSID to scan for
  767. * @param ssid_len Length of the SSID
  768. *
  769. * @return 0-success, otherwise fail
  770. */
  771. int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
  772. uint8_t ssid_len)
  773. {
  774. DECLARE_SSID_BUF(ssid_buf);
  775. int ret = 0;
  776. lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
  777. print_ssid(ssid_buf, ssid, ssid_len));
  778. if (!ssid_len)
  779. goto out;
  780. memcpy(priv->scan_ssid, ssid, ssid_len);
  781. priv->scan_ssid_len = ssid_len;
  782. lbs_scan_networks(priv, 1);
  783. if (priv->surpriseremoved) {
  784. ret = -1;
  785. goto out;
  786. }
  787. out:
  788. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  789. return ret;
  790. }
  791. /*********************************************************************/
  792. /* */
  793. /* Support for Wireless Extensions */
  794. /* */
  795. /*********************************************************************/
  796. #define MAX_CUSTOM_LEN 64
  797. static inline char *lbs_translate_scan(struct lbs_private *priv,
  798. struct iw_request_info *info,
  799. char *start, char *stop,
  800. struct bss_descriptor *bss)
  801. {
  802. struct chan_freq_power *cfp;
  803. char *current_val; /* For rates */
  804. struct iw_event iwe; /* Temporary buffer */
  805. int j;
  806. #define PERFECT_RSSI ((uint8_t)50)
  807. #define WORST_RSSI ((uint8_t)0)
  808. #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
  809. uint8_t rssi;
  810. lbs_deb_enter(LBS_DEB_SCAN);
  811. cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
  812. if (!cfp) {
  813. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  814. start = NULL;
  815. goto out;
  816. }
  817. /* First entry *MUST* be the BSSID */
  818. iwe.cmd = SIOCGIWAP;
  819. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  820. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  821. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
  822. /* SSID */
  823. iwe.cmd = SIOCGIWESSID;
  824. iwe.u.data.flags = 1;
  825. iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
  826. start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
  827. /* Mode */
  828. iwe.cmd = SIOCGIWMODE;
  829. iwe.u.mode = bss->mode;
  830. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
  831. /* Frequency */
  832. iwe.cmd = SIOCGIWFREQ;
  833. iwe.u.freq.m = (long)cfp->freq * 100000;
  834. iwe.u.freq.e = 1;
  835. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
  836. /* Add quality statistics */
  837. iwe.cmd = IWEVQUAL;
  838. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  839. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  840. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  841. iwe.u.qual.qual =
  842. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  843. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  844. (RSSI_DIFF * RSSI_DIFF);
  845. if (iwe.u.qual.qual > 100)
  846. iwe.u.qual.qual = 100;
  847. if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  848. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  849. } else {
  850. iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
  851. }
  852. /* Locally created ad-hoc BSSs won't have beacons if this is the
  853. * only station in the adhoc network; so get signal strength
  854. * from receive statistics.
  855. */
  856. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
  857. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  858. priv->curbssparams.ssid_len,
  859. bss->ssid, bss->ssid_len)) {
  860. int snr, nf;
  861. snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  862. nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  863. iwe.u.qual.level = CAL_RSSI(snr, nf);
  864. }
  865. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
  866. /* Add encryption capability */
  867. iwe.cmd = SIOCGIWENCODE;
  868. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  869. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  870. } else {
  871. iwe.u.data.flags = IW_ENCODE_DISABLED;
  872. }
  873. iwe.u.data.length = 0;
  874. start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
  875. current_val = start + iwe_stream_lcp_len(info);
  876. iwe.cmd = SIOCGIWRATE;
  877. iwe.u.bitrate.fixed = 0;
  878. iwe.u.bitrate.disabled = 0;
  879. iwe.u.bitrate.value = 0;
  880. for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
  881. /* Bit rate given in 500 kb/s units */
  882. iwe.u.bitrate.value = bss->rates[j] * 500000;
  883. current_val = iwe_stream_add_value(info, start, current_val,
  884. stop, &iwe, IW_EV_PARAM_LEN);
  885. }
  886. if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
  887. && !lbs_ssid_cmp(priv->curbssparams.ssid,
  888. priv->curbssparams.ssid_len,
  889. bss->ssid, bss->ssid_len)) {
  890. iwe.u.bitrate.value = 22 * 500000;
  891. current_val = iwe_stream_add_value(info, start, current_val,
  892. stop, &iwe, IW_EV_PARAM_LEN);
  893. }
  894. /* Check if we added any event */
  895. if ((current_val - start) > iwe_stream_lcp_len(info))
  896. start = current_val;
  897. memset(&iwe, 0, sizeof(iwe));
  898. if (bss->wpa_ie_len) {
  899. char buf[MAX_WPA_IE_LEN];
  900. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  901. iwe.cmd = IWEVGENIE;
  902. iwe.u.data.length = bss->wpa_ie_len;
  903. start = iwe_stream_add_point(info, start, stop, &iwe, buf);
  904. }
  905. memset(&iwe, 0, sizeof(iwe));
  906. if (bss->rsn_ie_len) {
  907. char buf[MAX_WPA_IE_LEN];
  908. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  909. iwe.cmd = IWEVGENIE;
  910. iwe.u.data.length = bss->rsn_ie_len;
  911. start = iwe_stream_add_point(info, start, stop, &iwe, buf);
  912. }
  913. if (bss->mesh) {
  914. char custom[MAX_CUSTOM_LEN];
  915. char *p = custom;
  916. iwe.cmd = IWEVCUSTOM;
  917. p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
  918. iwe.u.data.length = p - custom;
  919. if (iwe.u.data.length)
  920. start = iwe_stream_add_point(info, start, stop,
  921. &iwe, custom);
  922. }
  923. out:
  924. lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
  925. return start;
  926. }
  927. /**
  928. * @brief Handle Scan Network ioctl
  929. *
  930. * @param dev A pointer to net_device structure
  931. * @param info A pointer to iw_request_info structure
  932. * @param vwrq A pointer to iw_param structure
  933. * @param extra A pointer to extra data buf
  934. *
  935. * @return 0 --success, otherwise fail
  936. */
  937. int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
  938. union iwreq_data *wrqu, char *extra)
  939. {
  940. DECLARE_SSID_BUF(ssid);
  941. struct lbs_private *priv = dev->ml_priv;
  942. int ret = 0;
  943. lbs_deb_enter(LBS_DEB_WEXT);
  944. if (!priv->radio_on) {
  945. ret = -EINVAL;
  946. goto out;
  947. }
  948. if (!netif_running(dev)) {
  949. ret = -ENETDOWN;
  950. goto out;
  951. }
  952. /* mac80211 does this:
  953. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  954. if (sdata->type != IEEE80211_IF_TYPE_xxx) {
  955. ret = -EOPNOTSUPP;
  956. goto out;
  957. }
  958. */
  959. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  960. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  961. struct iw_scan_req *req = (struct iw_scan_req *)extra;
  962. priv->scan_ssid_len = req->essid_len;
  963. memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
  964. lbs_deb_wext("set_scan, essid '%s'\n",
  965. print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
  966. } else {
  967. priv->scan_ssid_len = 0;
  968. }
  969. if (!delayed_work_pending(&priv->scan_work))
  970. queue_delayed_work(priv->work_thread, &priv->scan_work,
  971. msecs_to_jiffies(50));
  972. /* set marker that currently a scan is taking place */
  973. priv->scan_channel = -1;
  974. if (priv->surpriseremoved)
  975. ret = -EIO;
  976. out:
  977. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  978. return ret;
  979. }
  980. /**
  981. * @brief Handle Retrieve scan table ioctl
  982. *
  983. * @param dev A pointer to net_device structure
  984. * @param info A pointer to iw_request_info structure
  985. * @param dwrq A pointer to iw_point structure
  986. * @param extra A pointer to extra data buf
  987. *
  988. * @return 0 --success, otherwise fail
  989. */
  990. int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
  991. struct iw_point *dwrq, char *extra)
  992. {
  993. #define SCAN_ITEM_SIZE 128
  994. struct lbs_private *priv = dev->ml_priv;
  995. int err = 0;
  996. char *ev = extra;
  997. char *stop = ev + dwrq->length;
  998. struct bss_descriptor *iter_bss;
  999. struct bss_descriptor *safe;
  1000. lbs_deb_enter(LBS_DEB_WEXT);
  1001. /* iwlist should wait until the current scan is finished */
  1002. if (priv->scan_channel)
  1003. return -EAGAIN;
  1004. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  1005. if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
  1006. err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  1007. CMD_OPTION_WAITFORRSP, 0, NULL);
  1008. if (err)
  1009. goto out;
  1010. }
  1011. mutex_lock(&priv->lock);
  1012. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  1013. char *next_ev;
  1014. unsigned long stale_time;
  1015. if (stop - ev < SCAN_ITEM_SIZE) {
  1016. err = -E2BIG;
  1017. break;
  1018. }
  1019. /* For mesh device, list only mesh networks */
  1020. if (dev == priv->mesh_dev && !iter_bss->mesh)
  1021. continue;
  1022. /* Prune old an old scan result */
  1023. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1024. if (time_after(jiffies, stale_time)) {
  1025. list_move_tail(&iter_bss->list, &priv->network_free_list);
  1026. clear_bss_descriptor(iter_bss);
  1027. continue;
  1028. }
  1029. /* Translate to WE format this entry */
  1030. next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
  1031. if (next_ev == NULL)
  1032. continue;
  1033. ev = next_ev;
  1034. }
  1035. mutex_unlock(&priv->lock);
  1036. dwrq->length = (ev - extra);
  1037. dwrq->flags = 0;
  1038. out:
  1039. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
  1040. return err;
  1041. }
  1042. /*********************************************************************/
  1043. /* */
  1044. /* Command execution */
  1045. /* */
  1046. /*********************************************************************/
  1047. /**
  1048. * @brief This function handles the command response of scan
  1049. *
  1050. * Called from handle_cmd_response() in cmdrespc.
  1051. *
  1052. * The response buffer for the scan command has the following
  1053. * memory layout:
  1054. *
  1055. * .-----------------------------------------------------------.
  1056. * | header (4 * sizeof(u16)): Standard command response hdr |
  1057. * .-----------------------------------------------------------.
  1058. * | bufsize (u16) : sizeof the BSS Description data |
  1059. * .-----------------------------------------------------------.
  1060. * | NumOfSet (u8) : Number of BSS Descs returned |
  1061. * .-----------------------------------------------------------.
  1062. * | BSSDescription data (variable, size given in bufsize) |
  1063. * .-----------------------------------------------------------.
  1064. * | TLV data (variable, size calculated using header->size, |
  1065. * | bufsize and sizeof the fixed fields above) |
  1066. * .-----------------------------------------------------------.
  1067. *
  1068. * @param priv A pointer to struct lbs_private structure
  1069. * @param resp A pointer to cmd_ds_command
  1070. *
  1071. * @return 0 or -1
  1072. */
  1073. static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
  1074. struct cmd_header *resp)
  1075. {
  1076. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  1077. struct bss_descriptor *iter_bss;
  1078. struct bss_descriptor *safe;
  1079. uint8_t *bssinfo;
  1080. uint16_t scanrespsize;
  1081. int bytesleft;
  1082. int idx;
  1083. int tlvbufsize;
  1084. int ret;
  1085. lbs_deb_enter(LBS_DEB_SCAN);
  1086. /* Prune old entries from scan table */
  1087. list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
  1088. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1089. if (time_before(jiffies, stale_time))
  1090. continue;
  1091. list_move_tail (&iter_bss->list, &priv->network_free_list);
  1092. clear_bss_descriptor(iter_bss);
  1093. }
  1094. if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
  1095. lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
  1096. scanresp->nr_sets, MAX_NETWORK_COUNT);
  1097. ret = -1;
  1098. goto done;
  1099. }
  1100. bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
  1101. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1102. scanrespsize = le16_to_cpu(resp->size);
  1103. lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
  1104. bssinfo = scanresp->bssdesc_and_tlvbuffer;
  1105. /* The size of the TLV buffer is equal to the entire command response
  1106. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1107. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1108. * response header (sizeof(struct cmd_header))
  1109. */
  1110. tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
  1111. + sizeof(scanresp->nr_sets)
  1112. + sizeof(struct cmd_header));
  1113. /*
  1114. * Process each scan response returned (scanresp->nr_sets). Save
  1115. * the information in the newbssentry and then insert into the
  1116. * driver scan table either as an update to an existing entry
  1117. * or as an addition at the end of the table
  1118. */
  1119. for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
  1120. struct bss_descriptor new;
  1121. struct bss_descriptor *found = NULL;
  1122. struct bss_descriptor *oldest = NULL;
  1123. /* Process the data fields and IEs returned for this BSS */
  1124. memset(&new, 0, sizeof (struct bss_descriptor));
  1125. if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
  1126. /* error parsing the scan response, skipped */
  1127. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1128. continue;
  1129. }
  1130. /* Try to find this bss in the scan table */
  1131. list_for_each_entry (iter_bss, &priv->network_list, list) {
  1132. if (is_same_network(iter_bss, &new)) {
  1133. found = iter_bss;
  1134. break;
  1135. }
  1136. if ((oldest == NULL) ||
  1137. (iter_bss->last_scanned < oldest->last_scanned))
  1138. oldest = iter_bss;
  1139. }
  1140. if (found) {
  1141. /* found, clear it */
  1142. clear_bss_descriptor(found);
  1143. } else if (!list_empty(&priv->network_free_list)) {
  1144. /* Pull one from the free list */
  1145. found = list_entry(priv->network_free_list.next,
  1146. struct bss_descriptor, list);
  1147. list_move_tail(&found->list, &priv->network_list);
  1148. } else if (oldest) {
  1149. /* If there are no more slots, expire the oldest */
  1150. found = oldest;
  1151. clear_bss_descriptor(found);
  1152. list_move_tail(&found->list, &priv->network_list);
  1153. } else {
  1154. continue;
  1155. }
  1156. lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
  1157. /* Copy the locally created newbssentry to the scan table */
  1158. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1159. }
  1160. ret = 0;
  1161. done:
  1162. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1163. return ret;
  1164. }