wext.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  1. /**
  2. * This file contains ioctl functions
  3. */
  4. #include <linux/ctype.h>
  5. #include <linux/delay.h>
  6. #include <linux/if.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/wireless.h>
  9. #include <linux/bitops.h>
  10. #include <net/ieee80211.h>
  11. #include <net/iw_handler.h>
  12. #include "host.h"
  13. #include "radiotap.h"
  14. #include "decl.h"
  15. #include "defs.h"
  16. #include "dev.h"
  17. #include "join.h"
  18. #include "wext.h"
  19. #include "assoc.h"
  20. /**
  21. * the rates supported by the card
  22. */
  23. static u8 libertas_wlan_data_rates[WLAN_SUPPORTED_RATES] =
  24. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  25. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  26. };
  27. /**
  28. * @brief Convert mw value to dbm value
  29. *
  30. * @param mw the value of mw
  31. * @return the value of dbm
  32. */
  33. static int mw_to_dbm(int mw)
  34. {
  35. if (mw < 2)
  36. return 0;
  37. else if (mw < 3)
  38. return 3;
  39. else if (mw < 4)
  40. return 5;
  41. else if (mw < 6)
  42. return 7;
  43. else if (mw < 7)
  44. return 8;
  45. else if (mw < 8)
  46. return 9;
  47. else if (mw < 10)
  48. return 10;
  49. else if (mw < 13)
  50. return 11;
  51. else if (mw < 16)
  52. return 12;
  53. else if (mw < 20)
  54. return 13;
  55. else if (mw < 25)
  56. return 14;
  57. else if (mw < 32)
  58. return 15;
  59. else if (mw < 40)
  60. return 16;
  61. else if (mw < 50)
  62. return 17;
  63. else if (mw < 63)
  64. return 18;
  65. else if (mw < 79)
  66. return 19;
  67. else if (mw < 100)
  68. return 20;
  69. else
  70. return 21;
  71. }
  72. /**
  73. * @brief Find the channel frequency power info with specific channel
  74. *
  75. * @param adapter A pointer to wlan_adapter structure
  76. * @param band it can be BAND_A, BAND_G or BAND_B
  77. * @param channel the channel for looking
  78. * @return A pointer to struct chan_freq_power structure or NULL if not find.
  79. */
  80. struct chan_freq_power *libertas_find_cfp_by_band_and_channel(wlan_adapter * adapter,
  81. u8 band, u16 channel)
  82. {
  83. struct chan_freq_power *cfp = NULL;
  84. struct region_channel *rc;
  85. int count = sizeof(adapter->region_channel) /
  86. sizeof(adapter->region_channel[0]);
  87. int i, j;
  88. for (j = 0; !cfp && (j < count); j++) {
  89. rc = &adapter->region_channel[j];
  90. if (adapter->enable11d)
  91. rc = &adapter->universal_channel[j];
  92. if (!rc->valid || !rc->CFP)
  93. continue;
  94. if (rc->band != band)
  95. continue;
  96. for (i = 0; i < rc->nrcfp; i++) {
  97. if (rc->CFP[i].channel == channel) {
  98. cfp = &rc->CFP[i];
  99. break;
  100. }
  101. }
  102. }
  103. if (!cfp && channel)
  104. lbs_deb_wext("libertas_find_cfp_by_band_and_channel: can't find "
  105. "cfp by band %d / channel %d\n", band, channel);
  106. return cfp;
  107. }
  108. /**
  109. * @brief Find the channel frequency power info with specific frequency
  110. *
  111. * @param adapter A pointer to wlan_adapter structure
  112. * @param band it can be BAND_A, BAND_G or BAND_B
  113. * @param freq the frequency for looking
  114. * @return A pointer to struct chan_freq_power structure or NULL if not find.
  115. */
  116. static struct chan_freq_power *find_cfp_by_band_and_freq(wlan_adapter * adapter,
  117. u8 band, u32 freq)
  118. {
  119. struct chan_freq_power *cfp = NULL;
  120. struct region_channel *rc;
  121. int count = sizeof(adapter->region_channel) /
  122. sizeof(adapter->region_channel[0]);
  123. int i, j;
  124. for (j = 0; !cfp && (j < count); j++) {
  125. rc = &adapter->region_channel[j];
  126. if (adapter->enable11d)
  127. rc = &adapter->universal_channel[j];
  128. if (!rc->valid || !rc->CFP)
  129. continue;
  130. if (rc->band != band)
  131. continue;
  132. for (i = 0; i < rc->nrcfp; i++) {
  133. if (rc->CFP[i].freq == freq) {
  134. cfp = &rc->CFP[i];
  135. break;
  136. }
  137. }
  138. }
  139. if (!cfp && freq)
  140. lbs_deb_wext("find_cfp_by_band_and_freql: can't find cfp by "
  141. "band %d / freq %d\n", band, freq);
  142. return cfp;
  143. }
  144. /**
  145. * @brief Set Radio On/OFF
  146. *
  147. * @param priv A pointer to wlan_private structure
  148. * @option Radio Option
  149. * @return 0 --success, otherwise fail
  150. */
  151. int wlan_radio_ioctl(wlan_private * priv, u8 option)
  152. {
  153. int ret = 0;
  154. wlan_adapter *adapter = priv->adapter;
  155. lbs_deb_enter(LBS_DEB_WEXT);
  156. if (adapter->radioon != option) {
  157. lbs_deb_wext("switching radio %s\n", option ? "on" : "off");
  158. adapter->radioon = option;
  159. ret = libertas_prepare_and_send_command(priv,
  160. cmd_802_11_radio_control,
  161. cmd_act_set,
  162. cmd_option_waitforrsp, 0, NULL);
  163. }
  164. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  165. return ret;
  166. }
  167. /**
  168. * @brief Copy rates
  169. *
  170. * @param dest A pointer to Dest Buf
  171. * @param src A pointer to Src Buf
  172. * @param len The len of Src Buf
  173. * @return Number of rates copyed
  174. */
  175. static inline int copyrates(u8 * dest, int pos, u8 * src, int len)
  176. {
  177. int i;
  178. for (i = 0; i < len && src[i]; i++, pos++) {
  179. if (pos >= sizeof(u8) * WLAN_SUPPORTED_RATES)
  180. break;
  181. dest[pos] = src[i];
  182. }
  183. return pos;
  184. }
  185. /**
  186. * @brief Get active data rates
  187. *
  188. * @param adapter A pointer to wlan_adapter structure
  189. * @param rate The buf to return the active rates
  190. * @return The number of rates
  191. */
  192. static int get_active_data_rates(wlan_adapter * adapter,
  193. u8* rates)
  194. {
  195. int k = 0;
  196. lbs_deb_enter(LBS_DEB_WEXT);
  197. if (adapter->connect_status != libertas_connected) {
  198. if (adapter->mode == IW_MODE_INFRA) {
  199. lbs_deb_wext("infra\n");
  200. k = copyrates(rates, k, libertas_supported_rates,
  201. sizeof(libertas_supported_rates));
  202. } else {
  203. lbs_deb_wext("Adhoc G\n");
  204. k = copyrates(rates, k, libertas_adhoc_rates_g,
  205. sizeof(libertas_adhoc_rates_g));
  206. }
  207. } else {
  208. k = copyrates(rates, 0, adapter->curbssparams.datarates,
  209. adapter->curbssparams.numofrates);
  210. }
  211. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", k);
  212. return k;
  213. }
  214. static int wlan_get_name(struct net_device *dev, struct iw_request_info *info,
  215. char *cwrq, char *extra)
  216. {
  217. const char *cp;
  218. char comm[6] = { "COMM-" };
  219. char mrvl[6] = { "MRVL-" };
  220. int cnt;
  221. lbs_deb_enter(LBS_DEB_WEXT);
  222. strcpy(cwrq, mrvl);
  223. cp = strstr(libertas_driver_version, comm);
  224. if (cp == libertas_driver_version) //skip leading "COMM-"
  225. cp = libertas_driver_version + strlen(comm);
  226. else
  227. cp = libertas_driver_version;
  228. cnt = strlen(mrvl);
  229. cwrq += cnt;
  230. while (cnt < 16 && (*cp != '-')) {
  231. *cwrq++ = toupper(*cp++);
  232. cnt++;
  233. }
  234. *cwrq = '\0';
  235. lbs_deb_leave(LBS_DEB_WEXT);
  236. return 0;
  237. }
  238. static int wlan_get_freq(struct net_device *dev, struct iw_request_info *info,
  239. struct iw_freq *fwrq, char *extra)
  240. {
  241. wlan_private *priv = dev->priv;
  242. wlan_adapter *adapter = priv->adapter;
  243. struct chan_freq_power *cfp;
  244. lbs_deb_enter(LBS_DEB_WEXT);
  245. cfp = libertas_find_cfp_by_band_and_channel(adapter, 0,
  246. adapter->curbssparams.channel);
  247. if (!cfp) {
  248. if (adapter->curbssparams.channel)
  249. lbs_deb_wext("invalid channel %d\n",
  250. adapter->curbssparams.channel);
  251. return -EINVAL;
  252. }
  253. fwrq->m = (long)cfp->freq * 100000;
  254. fwrq->e = 1;
  255. lbs_deb_wext("freq %u\n", fwrq->m);
  256. lbs_deb_leave(LBS_DEB_WEXT);
  257. return 0;
  258. }
  259. static int wlan_get_wap(struct net_device *dev, struct iw_request_info *info,
  260. struct sockaddr *awrq, char *extra)
  261. {
  262. wlan_private *priv = dev->priv;
  263. wlan_adapter *adapter = priv->adapter;
  264. lbs_deb_enter(LBS_DEB_WEXT);
  265. if (adapter->connect_status == libertas_connected) {
  266. memcpy(awrq->sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  267. } else {
  268. memset(awrq->sa_data, 0, ETH_ALEN);
  269. }
  270. awrq->sa_family = ARPHRD_ETHER;
  271. lbs_deb_leave(LBS_DEB_WEXT);
  272. return 0;
  273. }
  274. static int wlan_set_nick(struct net_device *dev, struct iw_request_info *info,
  275. struct iw_point *dwrq, char *extra)
  276. {
  277. wlan_private *priv = dev->priv;
  278. wlan_adapter *adapter = priv->adapter;
  279. lbs_deb_enter(LBS_DEB_WEXT);
  280. /*
  281. * Check the size of the string
  282. */
  283. if (dwrq->length > 16) {
  284. return -E2BIG;
  285. }
  286. mutex_lock(&adapter->lock);
  287. memset(adapter->nodename, 0, sizeof(adapter->nodename));
  288. memcpy(adapter->nodename, extra, dwrq->length);
  289. mutex_unlock(&adapter->lock);
  290. lbs_deb_leave(LBS_DEB_WEXT);
  291. return 0;
  292. }
  293. static int wlan_get_nick(struct net_device *dev, struct iw_request_info *info,
  294. struct iw_point *dwrq, char *extra)
  295. {
  296. wlan_private *priv = dev->priv;
  297. wlan_adapter *adapter = priv->adapter;
  298. lbs_deb_enter(LBS_DEB_WEXT);
  299. /*
  300. * Get the Nick Name saved
  301. */
  302. mutex_lock(&adapter->lock);
  303. strncpy(extra, adapter->nodename, 16);
  304. mutex_unlock(&adapter->lock);
  305. extra[16] = '\0';
  306. /*
  307. * If none, we may want to get the one that was set
  308. */
  309. /*
  310. * Push it out !
  311. */
  312. dwrq->length = strlen(extra) + 1;
  313. lbs_deb_leave(LBS_DEB_WEXT);
  314. return 0;
  315. }
  316. static int mesh_get_nick(struct net_device *dev, struct iw_request_info *info,
  317. struct iw_point *dwrq, char *extra)
  318. {
  319. wlan_private *priv = dev->priv;
  320. wlan_adapter *adapter = priv->adapter;
  321. lbs_deb_enter(LBS_DEB_WEXT);
  322. /* Use nickname to indicate that mesh is on */
  323. if (adapter->connect_status == libertas_connected) {
  324. strncpy(extra, "Mesh", 12);
  325. extra[12] = '\0';
  326. dwrq->length = strlen(extra) + 1;
  327. }
  328. else {
  329. extra[0] = '\0';
  330. dwrq->length = 1 ;
  331. }
  332. lbs_deb_leave(LBS_DEB_WEXT);
  333. return 0;
  334. }
  335. static int wlan_set_rts(struct net_device *dev, struct iw_request_info *info,
  336. struct iw_param *vwrq, char *extra)
  337. {
  338. int ret = 0;
  339. wlan_private *priv = dev->priv;
  340. wlan_adapter *adapter = priv->adapter;
  341. u32 rthr = vwrq->value;
  342. lbs_deb_enter(LBS_DEB_WEXT);
  343. if (vwrq->disabled) {
  344. adapter->rtsthsd = rthr = MRVDRV_RTS_MAX_VALUE;
  345. } else {
  346. if (rthr < MRVDRV_RTS_MIN_VALUE || rthr > MRVDRV_RTS_MAX_VALUE)
  347. return -EINVAL;
  348. adapter->rtsthsd = rthr;
  349. }
  350. ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
  351. cmd_act_set, cmd_option_waitforrsp,
  352. OID_802_11_RTS_THRESHOLD, &rthr);
  353. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  354. return ret;
  355. }
  356. static int wlan_get_rts(struct net_device *dev, struct iw_request_info *info,
  357. struct iw_param *vwrq, char *extra)
  358. {
  359. int ret = 0;
  360. wlan_private *priv = dev->priv;
  361. wlan_adapter *adapter = priv->adapter;
  362. lbs_deb_enter(LBS_DEB_WEXT);
  363. adapter->rtsthsd = 0;
  364. ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
  365. cmd_act_get, cmd_option_waitforrsp,
  366. OID_802_11_RTS_THRESHOLD, NULL);
  367. if (ret)
  368. goto out;
  369. vwrq->value = adapter->rtsthsd;
  370. vwrq->disabled = ((vwrq->value < MRVDRV_RTS_MIN_VALUE)
  371. || (vwrq->value > MRVDRV_RTS_MAX_VALUE));
  372. vwrq->fixed = 1;
  373. out:
  374. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  375. return ret;
  376. }
  377. static int wlan_set_frag(struct net_device *dev, struct iw_request_info *info,
  378. struct iw_param *vwrq, char *extra)
  379. {
  380. int ret = 0;
  381. u32 fthr = vwrq->value;
  382. wlan_private *priv = dev->priv;
  383. wlan_adapter *adapter = priv->adapter;
  384. lbs_deb_enter(LBS_DEB_WEXT);
  385. if (vwrq->disabled) {
  386. adapter->fragthsd = fthr = MRVDRV_FRAG_MAX_VALUE;
  387. } else {
  388. if (fthr < MRVDRV_FRAG_MIN_VALUE
  389. || fthr > MRVDRV_FRAG_MAX_VALUE)
  390. return -EINVAL;
  391. adapter->fragthsd = fthr;
  392. }
  393. ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
  394. cmd_act_set, cmd_option_waitforrsp,
  395. OID_802_11_FRAGMENTATION_THRESHOLD, &fthr);
  396. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  397. return ret;
  398. }
  399. static int wlan_get_frag(struct net_device *dev, struct iw_request_info *info,
  400. struct iw_param *vwrq, char *extra)
  401. {
  402. int ret = 0;
  403. wlan_private *priv = dev->priv;
  404. wlan_adapter *adapter = priv->adapter;
  405. lbs_deb_enter(LBS_DEB_WEXT);
  406. adapter->fragthsd = 0;
  407. ret = libertas_prepare_and_send_command(priv,
  408. cmd_802_11_snmp_mib,
  409. cmd_act_get, cmd_option_waitforrsp,
  410. OID_802_11_FRAGMENTATION_THRESHOLD, NULL);
  411. if (ret)
  412. goto out;
  413. vwrq->value = adapter->fragthsd;
  414. vwrq->disabled = ((vwrq->value < MRVDRV_FRAG_MIN_VALUE)
  415. || (vwrq->value > MRVDRV_FRAG_MAX_VALUE));
  416. vwrq->fixed = 1;
  417. out:
  418. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  419. return ret;
  420. }
  421. static int wlan_get_mode(struct net_device *dev,
  422. struct iw_request_info *info, u32 * uwrq, char *extra)
  423. {
  424. wlan_private *priv = dev->priv;
  425. wlan_adapter *adapter = priv->adapter;
  426. lbs_deb_enter(LBS_DEB_WEXT);
  427. *uwrq = adapter->mode;
  428. lbs_deb_leave(LBS_DEB_WEXT);
  429. return 0;
  430. }
  431. static int mesh_wlan_get_mode(struct net_device *dev,
  432. struct iw_request_info *info, u32 * uwrq,
  433. char *extra)
  434. {
  435. lbs_deb_enter(LBS_DEB_WEXT);
  436. *uwrq = IW_MODE_REPEAT ;
  437. lbs_deb_leave(LBS_DEB_WEXT);
  438. return 0;
  439. }
  440. static int wlan_get_txpow(struct net_device *dev,
  441. struct iw_request_info *info,
  442. struct iw_param *vwrq, char *extra)
  443. {
  444. int ret = 0;
  445. wlan_private *priv = dev->priv;
  446. wlan_adapter *adapter = priv->adapter;
  447. lbs_deb_enter(LBS_DEB_WEXT);
  448. ret = libertas_prepare_and_send_command(priv,
  449. cmd_802_11_rf_tx_power,
  450. cmd_act_tx_power_opt_get,
  451. cmd_option_waitforrsp, 0, NULL);
  452. if (ret)
  453. goto out;
  454. lbs_deb_wext("tx power level %d dbm\n", adapter->txpowerlevel);
  455. vwrq->value = adapter->txpowerlevel;
  456. vwrq->fixed = 1;
  457. if (adapter->radioon) {
  458. vwrq->disabled = 0;
  459. vwrq->flags = IW_TXPOW_DBM;
  460. } else {
  461. vwrq->disabled = 1;
  462. }
  463. out:
  464. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  465. return ret;
  466. }
  467. static int wlan_set_retry(struct net_device *dev, struct iw_request_info *info,
  468. struct iw_param *vwrq, char *extra)
  469. {
  470. int ret = 0;
  471. wlan_private *priv = dev->priv;
  472. wlan_adapter *adapter = priv->adapter;
  473. lbs_deb_enter(LBS_DEB_WEXT);
  474. if (vwrq->flags == IW_RETRY_LIMIT) {
  475. /* The MAC has a 4-bit Total_Tx_Count register
  476. Total_Tx_Count = 1 + Tx_Retry_Count */
  477. #define TX_RETRY_MIN 0
  478. #define TX_RETRY_MAX 14
  479. if (vwrq->value < TX_RETRY_MIN || vwrq->value > TX_RETRY_MAX)
  480. return -EINVAL;
  481. /* Adding 1 to convert retry count to try count */
  482. adapter->txretrycount = vwrq->value + 1;
  483. ret = libertas_prepare_and_send_command(priv, cmd_802_11_snmp_mib,
  484. cmd_act_set,
  485. cmd_option_waitforrsp,
  486. OID_802_11_TX_RETRYCOUNT, NULL);
  487. if (ret)
  488. goto out;
  489. } else {
  490. return -EOPNOTSUPP;
  491. }
  492. out:
  493. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  494. return ret;
  495. }
  496. static int wlan_get_retry(struct net_device *dev, struct iw_request_info *info,
  497. struct iw_param *vwrq, char *extra)
  498. {
  499. wlan_private *priv = dev->priv;
  500. wlan_adapter *adapter = priv->adapter;
  501. int ret = 0;
  502. lbs_deb_enter(LBS_DEB_WEXT);
  503. adapter->txretrycount = 0;
  504. ret = libertas_prepare_and_send_command(priv,
  505. cmd_802_11_snmp_mib,
  506. cmd_act_get, cmd_option_waitforrsp,
  507. OID_802_11_TX_RETRYCOUNT, NULL);
  508. if (ret)
  509. goto out;
  510. vwrq->disabled = 0;
  511. if (!vwrq->flags) {
  512. vwrq->flags = IW_RETRY_LIMIT;
  513. /* Subtract 1 to convert try count to retry count */
  514. vwrq->value = adapter->txretrycount - 1;
  515. }
  516. out:
  517. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  518. return ret;
  519. }
  520. static inline void sort_channels(struct iw_freq *freq, int num)
  521. {
  522. int i, j;
  523. struct iw_freq temp;
  524. for (i = 0; i < num; i++)
  525. for (j = i + 1; j < num; j++)
  526. if (freq[i].i > freq[j].i) {
  527. temp.i = freq[i].i;
  528. temp.m = freq[i].m;
  529. freq[i].i = freq[j].i;
  530. freq[i].m = freq[j].m;
  531. freq[j].i = temp.i;
  532. freq[j].m = temp.m;
  533. }
  534. }
  535. /* data rate listing
  536. MULTI_BANDS:
  537. abg a b b/g
  538. Infra G(12) A(8) B(4) G(12)
  539. Adhoc A+B(12) A(8) B(4) B(4)
  540. non-MULTI_BANDS:
  541. b b/g
  542. Infra B(4) G(12)
  543. Adhoc B(4) B(4)
  544. */
  545. /**
  546. * @brief Get Range Info
  547. *
  548. * @param dev A pointer to net_device structure
  549. * @param info A pointer to iw_request_info structure
  550. * @param vwrq A pointer to iw_param structure
  551. * @param extra A pointer to extra data buf
  552. * @return 0 --success, otherwise fail
  553. */
  554. static int wlan_get_range(struct net_device *dev, struct iw_request_info *info,
  555. struct iw_point *dwrq, char *extra)
  556. {
  557. int i, j;
  558. wlan_private *priv = dev->priv;
  559. wlan_adapter *adapter = priv->adapter;
  560. struct iw_range *range = (struct iw_range *)extra;
  561. struct chan_freq_power *cfp;
  562. u8 rates[WLAN_SUPPORTED_RATES];
  563. u8 flag = 0;
  564. lbs_deb_enter(LBS_DEB_WEXT);
  565. dwrq->length = sizeof(struct iw_range);
  566. memset(range, 0, sizeof(struct iw_range));
  567. range->min_nwid = 0;
  568. range->max_nwid = 0;
  569. memset(rates, 0, sizeof(rates));
  570. range->num_bitrates = get_active_data_rates(adapter, rates);
  571. for (i = 0; i < min_t(__u8, range->num_bitrates, IW_MAX_BITRATES) && rates[i];
  572. i++) {
  573. range->bitrate[i] = (rates[i] & 0x7f) * 500000;
  574. }
  575. range->num_bitrates = i;
  576. lbs_deb_wext("IW_MAX_BITRATES %d, num_bitrates %d\n", IW_MAX_BITRATES,
  577. range->num_bitrates);
  578. range->num_frequency = 0;
  579. if (priv->adapter->enable11d &&
  580. adapter->connect_status == libertas_connected) {
  581. u8 chan_no;
  582. u8 band;
  583. struct parsed_region_chan_11d *parsed_region_chan =
  584. &adapter->parsed_region_chan;
  585. if (parsed_region_chan == NULL) {
  586. lbs_deb_wext("11d: parsed_region_chan is NULL\n");
  587. goto out;
  588. }
  589. band = parsed_region_chan->band;
  590. lbs_deb_wext("band %d, nr_char %d\n", band,
  591. parsed_region_chan->nr_chan);
  592. for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
  593. && (i < parsed_region_chan->nr_chan); i++) {
  594. chan_no = parsed_region_chan->chanpwr[i].chan;
  595. lbs_deb_wext("chan_no %d\n", chan_no);
  596. range->freq[range->num_frequency].i = (long)chan_no;
  597. range->freq[range->num_frequency].m =
  598. (long)libertas_chan_2_freq(chan_no, band) * 100000;
  599. range->freq[range->num_frequency].e = 1;
  600. range->num_frequency++;
  601. }
  602. flag = 1;
  603. }
  604. if (!flag) {
  605. for (j = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
  606. && (j < sizeof(adapter->region_channel)
  607. / sizeof(adapter->region_channel[0])); j++) {
  608. cfp = adapter->region_channel[j].CFP;
  609. for (i = 0; (range->num_frequency < IW_MAX_FREQUENCIES)
  610. && adapter->region_channel[j].valid
  611. && cfp
  612. && (i < adapter->region_channel[j].nrcfp); i++) {
  613. range->freq[range->num_frequency].i =
  614. (long)cfp->channel;
  615. range->freq[range->num_frequency].m =
  616. (long)cfp->freq * 100000;
  617. range->freq[range->num_frequency].e = 1;
  618. cfp++;
  619. range->num_frequency++;
  620. }
  621. }
  622. }
  623. lbs_deb_wext("IW_MAX_FREQUENCIES %d, num_frequency %d\n",
  624. IW_MAX_FREQUENCIES, range->num_frequency);
  625. range->num_channels = range->num_frequency;
  626. sort_channels(&range->freq[0], range->num_frequency);
  627. /*
  628. * Set an indication of the max TCP throughput in bit/s that we can
  629. * expect using this interface
  630. */
  631. if (i > 2)
  632. range->throughput = 5000 * 1000;
  633. else
  634. range->throughput = 1500 * 1000;
  635. range->min_rts = MRVDRV_RTS_MIN_VALUE;
  636. range->max_rts = MRVDRV_RTS_MAX_VALUE;
  637. range->min_frag = MRVDRV_FRAG_MIN_VALUE;
  638. range->max_frag = MRVDRV_FRAG_MAX_VALUE;
  639. range->encoding_size[0] = 5;
  640. range->encoding_size[1] = 13;
  641. range->num_encoding_sizes = 2;
  642. range->max_encoding_tokens = 4;
  643. range->min_pmp = 1000000;
  644. range->max_pmp = 120000000;
  645. range->min_pmt = 1000;
  646. range->max_pmt = 1000000;
  647. range->pmp_flags = IW_POWER_PERIOD;
  648. range->pmt_flags = IW_POWER_TIMEOUT;
  649. range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
  650. /*
  651. * Minimum version we recommend
  652. */
  653. range->we_version_source = 15;
  654. /*
  655. * Version we are compiled with
  656. */
  657. range->we_version_compiled = WIRELESS_EXT;
  658. range->retry_capa = IW_RETRY_LIMIT;
  659. range->retry_flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
  660. range->min_retry = TX_RETRY_MIN;
  661. range->max_retry = TX_RETRY_MAX;
  662. /*
  663. * Set the qual, level and noise range values
  664. */
  665. range->max_qual.qual = 100;
  666. range->max_qual.level = 0;
  667. range->max_qual.noise = 0;
  668. range->max_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  669. range->avg_qual.qual = 70;
  670. /* TODO: Find real 'good' to 'bad' threshold value for RSSI */
  671. range->avg_qual.level = 0;
  672. range->avg_qual.noise = 0;
  673. range->avg_qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  674. range->sensitivity = 0;
  675. /*
  676. * Setup the supported power level ranges
  677. */
  678. memset(range->txpower, 0, sizeof(range->txpower));
  679. range->txpower[0] = 5;
  680. range->txpower[1] = 7;
  681. range->txpower[2] = 9;
  682. range->txpower[3] = 11;
  683. range->txpower[4] = 13;
  684. range->txpower[5] = 15;
  685. range->txpower[6] = 17;
  686. range->txpower[7] = 19;
  687. range->num_txpower = 8;
  688. range->txpower_capa = IW_TXPOW_DBM;
  689. range->txpower_capa |= IW_TXPOW_RANGE;
  690. range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
  691. IW_EVENT_CAPA_MASK(SIOCGIWAP) |
  692. IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
  693. range->event_capa[1] = IW_EVENT_CAPA_K_1;
  694. if (adapter->fwcapinfo & FW_CAPINFO_WPA) {
  695. range->enc_capa = IW_ENC_CAPA_WPA
  696. | IW_ENC_CAPA_WPA2
  697. | IW_ENC_CAPA_CIPHER_TKIP
  698. | IW_ENC_CAPA_CIPHER_CCMP;
  699. }
  700. out:
  701. lbs_deb_leave(LBS_DEB_WEXT);
  702. return 0;
  703. }
  704. static int wlan_set_power(struct net_device *dev, struct iw_request_info *info,
  705. struct iw_param *vwrq, char *extra)
  706. {
  707. wlan_private *priv = dev->priv;
  708. wlan_adapter *adapter = priv->adapter;
  709. lbs_deb_enter(LBS_DEB_WEXT);
  710. /* PS is currently supported only in Infrastructure mode
  711. * Remove this check if it is to be supported in IBSS mode also
  712. */
  713. if (vwrq->disabled) {
  714. adapter->psmode = wlan802_11powermodecam;
  715. if (adapter->psstate != PS_STATE_FULL_POWER) {
  716. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  717. }
  718. return 0;
  719. }
  720. if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
  721. lbs_deb_wext(
  722. "setting power timeout is not supported\n");
  723. return -EINVAL;
  724. } else if ((vwrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
  725. lbs_deb_wext("setting power period not supported\n");
  726. return -EINVAL;
  727. }
  728. if (adapter->psmode != wlan802_11powermodecam) {
  729. return 0;
  730. }
  731. adapter->psmode = wlan802_11powermodemax_psp;
  732. if (adapter->connect_status == libertas_connected) {
  733. libertas_ps_sleep(priv, cmd_option_waitforrsp);
  734. }
  735. lbs_deb_leave(LBS_DEB_WEXT);
  736. return 0;
  737. }
  738. static int wlan_get_power(struct net_device *dev, struct iw_request_info *info,
  739. struct iw_param *vwrq, char *extra)
  740. {
  741. wlan_private *priv = dev->priv;
  742. wlan_adapter *adapter = priv->adapter;
  743. int mode;
  744. lbs_deb_enter(LBS_DEB_WEXT);
  745. mode = adapter->psmode;
  746. if ((vwrq->disabled = (mode == wlan802_11powermodecam))
  747. || adapter->connect_status == libertas_disconnected)
  748. {
  749. goto out;
  750. }
  751. vwrq->value = 0;
  752. out:
  753. lbs_deb_leave(LBS_DEB_WEXT);
  754. return 0;
  755. }
  756. static struct iw_statistics *wlan_get_wireless_stats(struct net_device *dev)
  757. {
  758. enum {
  759. POOR = 30,
  760. FAIR = 60,
  761. GOOD = 80,
  762. VERY_GOOD = 90,
  763. EXCELLENT = 95,
  764. PERFECT = 100
  765. };
  766. wlan_private *priv = dev->priv;
  767. wlan_adapter *adapter = priv->adapter;
  768. u32 rssi_qual;
  769. u32 tx_qual;
  770. u32 quality = 0;
  771. int stats_valid = 0;
  772. u8 rssi;
  773. u32 tx_retries;
  774. lbs_deb_enter(LBS_DEB_WEXT);
  775. priv->wstats.status = adapter->mode;
  776. /* If we're not associated, all quality values are meaningless */
  777. if (adapter->connect_status != libertas_connected)
  778. goto out;
  779. /* Quality by RSSI */
  780. priv->wstats.qual.level =
  781. CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
  782. adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  783. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  784. priv->wstats.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  785. } else {
  786. priv->wstats.qual.noise =
  787. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  788. }
  789. lbs_deb_wext("signal level %#x\n", priv->wstats.qual.level);
  790. lbs_deb_wext("noise %#x\n", priv->wstats.qual.noise);
  791. rssi = priv->wstats.qual.level - priv->wstats.qual.noise;
  792. if (rssi < 15)
  793. rssi_qual = rssi * POOR / 10;
  794. else if (rssi < 20)
  795. rssi_qual = (rssi - 15) * (FAIR - POOR) / 5 + POOR;
  796. else if (rssi < 30)
  797. rssi_qual = (rssi - 20) * (GOOD - FAIR) / 5 + FAIR;
  798. else if (rssi < 40)
  799. rssi_qual = (rssi - 30) * (VERY_GOOD - GOOD) /
  800. 10 + GOOD;
  801. else
  802. rssi_qual = (rssi - 40) * (PERFECT - VERY_GOOD) /
  803. 10 + VERY_GOOD;
  804. quality = rssi_qual;
  805. /* Quality by TX errors */
  806. priv->wstats.discard.retries = priv->stats.tx_errors;
  807. tx_retries = le16_to_cpu(adapter->logmsg.retry);
  808. if (tx_retries > 75)
  809. tx_qual = (90 - tx_retries) * POOR / 15;
  810. else if (tx_retries > 70)
  811. tx_qual = (75 - tx_retries) * (FAIR - POOR) / 5 + POOR;
  812. else if (tx_retries > 65)
  813. tx_qual = (70 - tx_retries) * (GOOD - FAIR) / 5 + FAIR;
  814. else if (tx_retries > 50)
  815. tx_qual = (65 - tx_retries) * (VERY_GOOD - GOOD) /
  816. 15 + GOOD;
  817. else
  818. tx_qual = (50 - tx_retries) *
  819. (PERFECT - VERY_GOOD) / 50 + VERY_GOOD;
  820. quality = min(quality, tx_qual);
  821. priv->wstats.discard.code = le16_to_cpu(adapter->logmsg.wepundecryptable);
  822. priv->wstats.discard.fragment = le16_to_cpu(adapter->logmsg.rxfrag);
  823. priv->wstats.discard.retries = tx_retries;
  824. priv->wstats.discard.misc = le16_to_cpu(adapter->logmsg.ackfailure);
  825. /* Calculate quality */
  826. priv->wstats.qual.qual = max(quality, (u32)100);
  827. priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  828. stats_valid = 1;
  829. /* update stats asynchronously for future calls */
  830. libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
  831. 0, 0, NULL);
  832. libertas_prepare_and_send_command(priv, cmd_802_11_get_log, 0,
  833. 0, 0, NULL);
  834. out:
  835. if (!stats_valid) {
  836. priv->wstats.miss.beacon = 0;
  837. priv->wstats.discard.retries = 0;
  838. priv->wstats.qual.qual = 0;
  839. priv->wstats.qual.level = 0;
  840. priv->wstats.qual.noise = 0;
  841. priv->wstats.qual.updated = IW_QUAL_ALL_UPDATED;
  842. priv->wstats.qual.updated |= IW_QUAL_NOISE_INVALID |
  843. IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
  844. }
  845. lbs_deb_leave(LBS_DEB_WEXT);
  846. return &priv->wstats;
  847. }
  848. static int wlan_set_freq(struct net_device *dev, struct iw_request_info *info,
  849. struct iw_freq *fwrq, char *extra)
  850. {
  851. int ret = -EINVAL;
  852. wlan_private *priv = dev->priv;
  853. wlan_adapter *adapter = priv->adapter;
  854. struct chan_freq_power *cfp;
  855. struct assoc_request * assoc_req;
  856. lbs_deb_enter(LBS_DEB_WEXT);
  857. mutex_lock(&adapter->lock);
  858. assoc_req = wlan_get_association_request(adapter);
  859. if (!assoc_req) {
  860. ret = -ENOMEM;
  861. goto out;
  862. }
  863. /* If setting by frequency, convert to a channel */
  864. if (fwrq->e == 1) {
  865. long f = fwrq->m / 100000;
  866. cfp = find_cfp_by_band_and_freq(adapter, 0, f);
  867. if (!cfp) {
  868. lbs_deb_wext("invalid freq %ld\n", f);
  869. goto out;
  870. }
  871. fwrq->e = 0;
  872. fwrq->m = (int) cfp->channel;
  873. }
  874. /* Setting by channel number */
  875. if (fwrq->m > 1000 || fwrq->e > 0) {
  876. goto out;
  877. }
  878. cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, fwrq->m);
  879. if (!cfp) {
  880. goto out;
  881. }
  882. assoc_req->channel = fwrq->m;
  883. ret = 0;
  884. out:
  885. if (ret == 0) {
  886. set_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags);
  887. wlan_postpone_association_work(priv);
  888. } else {
  889. wlan_cancel_association_work(priv);
  890. }
  891. mutex_unlock(&adapter->lock);
  892. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  893. return ret;
  894. }
  895. /**
  896. * @brief use index to get the data rate
  897. *
  898. * @param index The index of data rate
  899. * @return data rate or 0
  900. */
  901. u32 libertas_index_to_data_rate(u8 index)
  902. {
  903. if (index >= sizeof(libertas_wlan_data_rates))
  904. index = 0;
  905. return libertas_wlan_data_rates[index];
  906. }
  907. /**
  908. * @brief use rate to get the index
  909. *
  910. * @param rate data rate
  911. * @return index or 0
  912. */
  913. u8 libertas_data_rate_to_index(u32 rate)
  914. {
  915. u8 *ptr;
  916. if (rate)
  917. if ((ptr = memchr(libertas_wlan_data_rates, (u8) rate,
  918. sizeof(libertas_wlan_data_rates))))
  919. return (ptr - libertas_wlan_data_rates);
  920. return 0;
  921. }
  922. static int wlan_set_rate(struct net_device *dev, struct iw_request_info *info,
  923. struct iw_param *vwrq, char *extra)
  924. {
  925. wlan_private *priv = dev->priv;
  926. wlan_adapter *adapter = priv->adapter;
  927. u32 data_rate;
  928. u16 action;
  929. int ret = 0;
  930. u8 rates[WLAN_SUPPORTED_RATES];
  931. u8 *rate;
  932. lbs_deb_enter(LBS_DEB_WEXT);
  933. lbs_deb_wext("vwrq->value %d\n", vwrq->value);
  934. if (vwrq->value == -1) {
  935. action = cmd_act_set_tx_auto; // Auto
  936. adapter->is_datarate_auto = 1;
  937. adapter->datarate = 0;
  938. } else {
  939. if (vwrq->value % 100000) {
  940. return -EINVAL;
  941. }
  942. data_rate = vwrq->value / 500000;
  943. memset(rates, 0, sizeof(rates));
  944. get_active_data_rates(adapter, rates);
  945. rate = rates;
  946. while (*rate) {
  947. lbs_deb_wext("rate=0x%X, wanted data_rate 0x%X\n", *rate,
  948. data_rate);
  949. if ((*rate & 0x7f) == (data_rate & 0x7f))
  950. break;
  951. rate++;
  952. }
  953. if (!*rate) {
  954. lbs_pr_alert("fixed data rate 0x%X out "
  955. "of range\n", data_rate);
  956. return -EINVAL;
  957. }
  958. adapter->datarate = data_rate;
  959. action = cmd_act_set_tx_fix_rate;
  960. adapter->is_datarate_auto = 0;
  961. }
  962. ret = libertas_prepare_and_send_command(priv, cmd_802_11_data_rate,
  963. action, cmd_option_waitforrsp, 0, NULL);
  964. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  965. return ret;
  966. }
  967. static int wlan_get_rate(struct net_device *dev, struct iw_request_info *info,
  968. struct iw_param *vwrq, char *extra)
  969. {
  970. wlan_private *priv = dev->priv;
  971. wlan_adapter *adapter = priv->adapter;
  972. lbs_deb_enter(LBS_DEB_WEXT);
  973. if (adapter->is_datarate_auto) {
  974. vwrq->fixed = 0;
  975. } else {
  976. vwrq->fixed = 1;
  977. }
  978. vwrq->value = adapter->datarate * 500000;
  979. lbs_deb_leave(LBS_DEB_WEXT);
  980. return 0;
  981. }
  982. static int wlan_set_mode(struct net_device *dev,
  983. struct iw_request_info *info, u32 * uwrq, char *extra)
  984. {
  985. int ret = 0;
  986. wlan_private *priv = dev->priv;
  987. wlan_adapter *adapter = priv->adapter;
  988. struct assoc_request * assoc_req;
  989. lbs_deb_enter(LBS_DEB_WEXT);
  990. if ( (*uwrq != IW_MODE_ADHOC)
  991. && (*uwrq != IW_MODE_INFRA)
  992. && (*uwrq != IW_MODE_AUTO)) {
  993. lbs_deb_wext("Invalid mode: 0x%x\n", *uwrq);
  994. ret = -EINVAL;
  995. goto out;
  996. }
  997. mutex_lock(&adapter->lock);
  998. assoc_req = wlan_get_association_request(adapter);
  999. if (!assoc_req) {
  1000. ret = -ENOMEM;
  1001. wlan_cancel_association_work(priv);
  1002. } else {
  1003. assoc_req->mode = *uwrq;
  1004. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  1005. wlan_postpone_association_work(priv);
  1006. lbs_deb_wext("Switching to mode: 0x%x\n", *uwrq);
  1007. }
  1008. mutex_unlock(&adapter->lock);
  1009. out:
  1010. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1011. return ret;
  1012. }
  1013. /**
  1014. * @brief Get Encryption key
  1015. *
  1016. * @param dev A pointer to net_device structure
  1017. * @param info A pointer to iw_request_info structure
  1018. * @param vwrq A pointer to iw_param structure
  1019. * @param extra A pointer to extra data buf
  1020. * @return 0 --success, otherwise fail
  1021. */
  1022. static int wlan_get_encode(struct net_device *dev,
  1023. struct iw_request_info *info,
  1024. struct iw_point *dwrq, u8 * extra)
  1025. {
  1026. wlan_private *priv = dev->priv;
  1027. wlan_adapter *adapter = priv->adapter;
  1028. int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
  1029. lbs_deb_enter(LBS_DEB_WEXT);
  1030. lbs_deb_wext("flags 0x%x, index %d, length %d, wep_tx_keyidx %d\n",
  1031. dwrq->flags, index, dwrq->length, adapter->wep_tx_keyidx);
  1032. dwrq->flags = 0;
  1033. /* Authentication method */
  1034. switch (adapter->secinfo.auth_mode) {
  1035. case IW_AUTH_ALG_OPEN_SYSTEM:
  1036. dwrq->flags = IW_ENCODE_OPEN;
  1037. break;
  1038. case IW_AUTH_ALG_SHARED_KEY:
  1039. case IW_AUTH_ALG_LEAP:
  1040. dwrq->flags = IW_ENCODE_RESTRICTED;
  1041. break;
  1042. default:
  1043. dwrq->flags = IW_ENCODE_DISABLED | IW_ENCODE_OPEN;
  1044. break;
  1045. }
  1046. if ( adapter->secinfo.wep_enabled
  1047. || adapter->secinfo.WPAenabled
  1048. || adapter->secinfo.WPA2enabled) {
  1049. dwrq->flags &= ~IW_ENCODE_DISABLED;
  1050. } else {
  1051. dwrq->flags |= IW_ENCODE_DISABLED;
  1052. }
  1053. memset(extra, 0, 16);
  1054. mutex_lock(&adapter->lock);
  1055. /* Default to returning current transmit key */
  1056. if (index < 0)
  1057. index = adapter->wep_tx_keyidx;
  1058. if ((adapter->wep_keys[index].len) && adapter->secinfo.wep_enabled) {
  1059. memcpy(extra, adapter->wep_keys[index].key,
  1060. adapter->wep_keys[index].len);
  1061. dwrq->length = adapter->wep_keys[index].len;
  1062. dwrq->flags |= (index + 1);
  1063. /* Return WEP enabled */
  1064. dwrq->flags &= ~IW_ENCODE_DISABLED;
  1065. } else if ((adapter->secinfo.WPAenabled)
  1066. || (adapter->secinfo.WPA2enabled)) {
  1067. /* return WPA enabled */
  1068. dwrq->flags &= ~IW_ENCODE_DISABLED;
  1069. } else {
  1070. dwrq->flags |= IW_ENCODE_DISABLED;
  1071. }
  1072. mutex_unlock(&adapter->lock);
  1073. dwrq->flags |= IW_ENCODE_NOKEY;
  1074. lbs_deb_wext("key: " MAC_FMT ", keylen %d\n",
  1075. extra[0], extra[1], extra[2],
  1076. extra[3], extra[4], extra[5], dwrq->length);
  1077. lbs_deb_wext("return flags 0x%x\n", dwrq->flags);
  1078. lbs_deb_leave(LBS_DEB_WEXT);
  1079. return 0;
  1080. }
  1081. /**
  1082. * @brief Set Encryption key (internal)
  1083. *
  1084. * @param priv A pointer to private card structure
  1085. * @param key_material A pointer to key material
  1086. * @param key_length length of key material
  1087. * @param index key index to set
  1088. * @param set_tx_key Force set TX key (1 = yes, 0 = no)
  1089. * @return 0 --success, otherwise fail
  1090. */
  1091. static int wlan_set_wep_key(struct assoc_request *assoc_req,
  1092. const char *key_material,
  1093. u16 key_length,
  1094. u16 index,
  1095. int set_tx_key)
  1096. {
  1097. int ret = 0;
  1098. struct WLAN_802_11_KEY *pkey;
  1099. lbs_deb_enter(LBS_DEB_WEXT);
  1100. /* Paranoid validation of key index */
  1101. if (index > 3) {
  1102. ret = -EINVAL;
  1103. goto out;
  1104. }
  1105. /* validate max key length */
  1106. if (key_length > KEY_LEN_WEP_104) {
  1107. ret = -EINVAL;
  1108. goto out;
  1109. }
  1110. pkey = &assoc_req->wep_keys[index];
  1111. if (key_length > 0) {
  1112. memset(pkey, 0, sizeof(struct WLAN_802_11_KEY));
  1113. pkey->type = KEY_TYPE_ID_WEP;
  1114. /* Standardize the key length */
  1115. pkey->len = (key_length > KEY_LEN_WEP_40) ?
  1116. KEY_LEN_WEP_104 : KEY_LEN_WEP_40;
  1117. memcpy(pkey->key, key_material, key_length);
  1118. }
  1119. if (set_tx_key) {
  1120. /* Ensure the chosen key is valid */
  1121. if (!pkey->len) {
  1122. lbs_deb_wext("key not set, so cannot enable it\n");
  1123. ret = -EINVAL;
  1124. goto out;
  1125. }
  1126. assoc_req->wep_tx_keyidx = index;
  1127. }
  1128. assoc_req->secinfo.wep_enabled = 1;
  1129. out:
  1130. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1131. return ret;
  1132. }
  1133. static int validate_key_index(u16 def_index, u16 raw_index,
  1134. u16 *out_index, u16 *is_default)
  1135. {
  1136. if (!out_index || !is_default)
  1137. return -EINVAL;
  1138. /* Verify index if present, otherwise use default TX key index */
  1139. if (raw_index > 0) {
  1140. if (raw_index > 4)
  1141. return -EINVAL;
  1142. *out_index = raw_index - 1;
  1143. } else {
  1144. *out_index = def_index;
  1145. *is_default = 1;
  1146. }
  1147. return 0;
  1148. }
  1149. static void disable_wep(struct assoc_request *assoc_req)
  1150. {
  1151. int i;
  1152. lbs_deb_enter(LBS_DEB_WEXT);
  1153. /* Set Open System auth mode */
  1154. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1155. /* Clear WEP keys and mark WEP as disabled */
  1156. assoc_req->secinfo.wep_enabled = 0;
  1157. for (i = 0; i < 4; i++)
  1158. assoc_req->wep_keys[i].len = 0;
  1159. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1160. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  1161. lbs_deb_leave(LBS_DEB_WEXT);
  1162. }
  1163. static void disable_wpa(struct assoc_request *assoc_req)
  1164. {
  1165. lbs_deb_enter(LBS_DEB_WEXT);
  1166. memset(&assoc_req->wpa_mcast_key, 0, sizeof (struct WLAN_802_11_KEY));
  1167. assoc_req->wpa_mcast_key.flags = KEY_INFO_WPA_MCAST;
  1168. set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  1169. memset(&assoc_req->wpa_unicast_key, 0, sizeof (struct WLAN_802_11_KEY));
  1170. assoc_req->wpa_unicast_key.flags = KEY_INFO_WPA_UNICAST;
  1171. set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  1172. assoc_req->secinfo.WPAenabled = 0;
  1173. assoc_req->secinfo.WPA2enabled = 0;
  1174. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1175. lbs_deb_leave(LBS_DEB_WEXT);
  1176. }
  1177. /**
  1178. * @brief Set Encryption key
  1179. *
  1180. * @param dev A pointer to net_device structure
  1181. * @param info A pointer to iw_request_info structure
  1182. * @param vwrq A pointer to iw_param structure
  1183. * @param extra A pointer to extra data buf
  1184. * @return 0 --success, otherwise fail
  1185. */
  1186. static int wlan_set_encode(struct net_device *dev,
  1187. struct iw_request_info *info,
  1188. struct iw_point *dwrq, char *extra)
  1189. {
  1190. int ret = 0;
  1191. wlan_private *priv = dev->priv;
  1192. wlan_adapter *adapter = priv->adapter;
  1193. struct assoc_request * assoc_req;
  1194. u16 is_default = 0, index = 0, set_tx_key = 0;
  1195. lbs_deb_enter(LBS_DEB_WEXT);
  1196. mutex_lock(&adapter->lock);
  1197. assoc_req = wlan_get_association_request(adapter);
  1198. if (!assoc_req) {
  1199. ret = -ENOMEM;
  1200. goto out;
  1201. }
  1202. if (dwrq->flags & IW_ENCODE_DISABLED) {
  1203. disable_wep (assoc_req);
  1204. disable_wpa (assoc_req);
  1205. goto out;
  1206. }
  1207. ret = validate_key_index(assoc_req->wep_tx_keyidx,
  1208. (dwrq->flags & IW_ENCODE_INDEX),
  1209. &index, &is_default);
  1210. if (ret) {
  1211. ret = -EINVAL;
  1212. goto out;
  1213. }
  1214. /* If WEP isn't enabled, or if there is no key data but a valid
  1215. * index, set the TX key.
  1216. */
  1217. if (!assoc_req->secinfo.wep_enabled || (dwrq->length == 0 && !is_default))
  1218. set_tx_key = 1;
  1219. ret = wlan_set_wep_key(assoc_req, extra, dwrq->length, index, set_tx_key);
  1220. if (ret)
  1221. goto out;
  1222. if (dwrq->length)
  1223. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  1224. if (set_tx_key)
  1225. set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
  1226. if (dwrq->flags & IW_ENCODE_RESTRICTED) {
  1227. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
  1228. } else if (dwrq->flags & IW_ENCODE_OPEN) {
  1229. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1230. }
  1231. out:
  1232. if (ret == 0) {
  1233. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1234. wlan_postpone_association_work(priv);
  1235. } else {
  1236. wlan_cancel_association_work(priv);
  1237. }
  1238. mutex_unlock(&adapter->lock);
  1239. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1240. return ret;
  1241. }
  1242. /**
  1243. * @brief Get Extended Encryption key (WPA/802.1x and WEP)
  1244. *
  1245. * @param dev A pointer to net_device structure
  1246. * @param info A pointer to iw_request_info structure
  1247. * @param vwrq A pointer to iw_param structure
  1248. * @param extra A pointer to extra data buf
  1249. * @return 0 on success, otherwise failure
  1250. */
  1251. static int wlan_get_encodeext(struct net_device *dev,
  1252. struct iw_request_info *info,
  1253. struct iw_point *dwrq,
  1254. char *extra)
  1255. {
  1256. int ret = -EINVAL;
  1257. wlan_private *priv = dev->priv;
  1258. wlan_adapter *adapter = priv->adapter;
  1259. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1260. int index, max_key_len;
  1261. lbs_deb_enter(LBS_DEB_WEXT);
  1262. max_key_len = dwrq->length - sizeof(*ext);
  1263. if (max_key_len < 0)
  1264. goto out;
  1265. index = dwrq->flags & IW_ENCODE_INDEX;
  1266. if (index) {
  1267. if (index < 1 || index > 4)
  1268. goto out;
  1269. index--;
  1270. } else {
  1271. index = adapter->wep_tx_keyidx;
  1272. }
  1273. if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
  1274. ext->alg != IW_ENCODE_ALG_WEP) {
  1275. if (index != 0 || adapter->mode != IW_MODE_INFRA)
  1276. goto out;
  1277. }
  1278. dwrq->flags = index + 1;
  1279. memset(ext, 0, sizeof(*ext));
  1280. if ( !adapter->secinfo.wep_enabled
  1281. && !adapter->secinfo.WPAenabled
  1282. && !adapter->secinfo.WPA2enabled) {
  1283. ext->alg = IW_ENCODE_ALG_NONE;
  1284. ext->key_len = 0;
  1285. dwrq->flags |= IW_ENCODE_DISABLED;
  1286. } else {
  1287. u8 *key = NULL;
  1288. if ( adapter->secinfo.wep_enabled
  1289. && !adapter->secinfo.WPAenabled
  1290. && !adapter->secinfo.WPA2enabled) {
  1291. /* WEP */
  1292. ext->alg = IW_ENCODE_ALG_WEP;
  1293. ext->key_len = adapter->wep_keys[index].len;
  1294. key = &adapter->wep_keys[index].key[0];
  1295. } else if ( !adapter->secinfo.wep_enabled
  1296. && (adapter->secinfo.WPAenabled ||
  1297. adapter->secinfo.WPA2enabled)) {
  1298. /* WPA */
  1299. struct WLAN_802_11_KEY * pkey = NULL;
  1300. if ( adapter->wpa_mcast_key.len
  1301. && (adapter->wpa_mcast_key.flags & KEY_INFO_WPA_ENABLED))
  1302. pkey = &adapter->wpa_mcast_key;
  1303. else if ( adapter->wpa_unicast_key.len
  1304. && (adapter->wpa_unicast_key.flags & KEY_INFO_WPA_ENABLED))
  1305. pkey = &adapter->wpa_unicast_key;
  1306. if (pkey) {
  1307. if (pkey->type == KEY_TYPE_ID_AES) {
  1308. ext->alg = IW_ENCODE_ALG_CCMP;
  1309. } else {
  1310. ext->alg = IW_ENCODE_ALG_TKIP;
  1311. }
  1312. ext->key_len = pkey->len;
  1313. key = &pkey->key[0];
  1314. } else {
  1315. ext->alg = IW_ENCODE_ALG_TKIP;
  1316. ext->key_len = 0;
  1317. }
  1318. } else {
  1319. goto out;
  1320. }
  1321. if (ext->key_len > max_key_len) {
  1322. ret = -E2BIG;
  1323. goto out;
  1324. }
  1325. if (ext->key_len)
  1326. memcpy(ext->key, key, ext->key_len);
  1327. else
  1328. dwrq->flags |= IW_ENCODE_NOKEY;
  1329. dwrq->flags |= IW_ENCODE_ENABLED;
  1330. }
  1331. ret = 0;
  1332. out:
  1333. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1334. return ret;
  1335. }
  1336. /**
  1337. * @brief Set Encryption key Extended (WPA/802.1x and WEP)
  1338. *
  1339. * @param dev A pointer to net_device structure
  1340. * @param info A pointer to iw_request_info structure
  1341. * @param vwrq A pointer to iw_param structure
  1342. * @param extra A pointer to extra data buf
  1343. * @return 0 --success, otherwise fail
  1344. */
  1345. static int wlan_set_encodeext(struct net_device *dev,
  1346. struct iw_request_info *info,
  1347. struct iw_point *dwrq,
  1348. char *extra)
  1349. {
  1350. int ret = 0;
  1351. wlan_private *priv = dev->priv;
  1352. wlan_adapter *adapter = priv->adapter;
  1353. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1354. int alg = ext->alg;
  1355. struct assoc_request * assoc_req;
  1356. lbs_deb_enter(LBS_DEB_WEXT);
  1357. mutex_lock(&adapter->lock);
  1358. assoc_req = wlan_get_association_request(adapter);
  1359. if (!assoc_req) {
  1360. ret = -ENOMEM;
  1361. goto out;
  1362. }
  1363. if ((alg == IW_ENCODE_ALG_NONE) || (dwrq->flags & IW_ENCODE_DISABLED)) {
  1364. disable_wep (assoc_req);
  1365. disable_wpa (assoc_req);
  1366. } else if (alg == IW_ENCODE_ALG_WEP) {
  1367. u16 is_default = 0, index, set_tx_key = 0;
  1368. ret = validate_key_index(assoc_req->wep_tx_keyidx,
  1369. (dwrq->flags & IW_ENCODE_INDEX),
  1370. &index, &is_default);
  1371. if (ret)
  1372. goto out;
  1373. /* If WEP isn't enabled, or if there is no key data but a valid
  1374. * index, or if the set-TX-key flag was passed, set the TX key.
  1375. */
  1376. if ( !assoc_req->secinfo.wep_enabled
  1377. || (dwrq->length == 0 && !is_default)
  1378. || (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY))
  1379. set_tx_key = 1;
  1380. /* Copy key to driver */
  1381. ret = wlan_set_wep_key (assoc_req, ext->key, ext->key_len, index,
  1382. set_tx_key);
  1383. if (ret)
  1384. goto out;
  1385. if (dwrq->flags & IW_ENCODE_RESTRICTED) {
  1386. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
  1387. } else if (dwrq->flags & IW_ENCODE_OPEN) {
  1388. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1389. }
  1390. /* Mark the various WEP bits as modified */
  1391. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1392. if (dwrq->length)
  1393. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  1394. if (set_tx_key)
  1395. set_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags);
  1396. } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
  1397. struct WLAN_802_11_KEY * pkey;
  1398. /* validate key length */
  1399. if (((alg == IW_ENCODE_ALG_TKIP)
  1400. && (ext->key_len != KEY_LEN_WPA_TKIP))
  1401. || ((alg == IW_ENCODE_ALG_CCMP)
  1402. && (ext->key_len != KEY_LEN_WPA_AES))) {
  1403. lbs_deb_wext("invalid size %d for key of alg"
  1404. "type %d\n",
  1405. ext->key_len,
  1406. alg);
  1407. ret = -EINVAL;
  1408. goto out;
  1409. }
  1410. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
  1411. pkey = &assoc_req->wpa_mcast_key;
  1412. set_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  1413. } else {
  1414. pkey = &assoc_req->wpa_unicast_key;
  1415. set_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  1416. }
  1417. memset(pkey, 0, sizeof (struct WLAN_802_11_KEY));
  1418. memcpy(pkey->key, ext->key, ext->key_len);
  1419. pkey->len = ext->key_len;
  1420. if (pkey->len)
  1421. pkey->flags |= KEY_INFO_WPA_ENABLED;
  1422. /* Do this after zeroing key structure */
  1423. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
  1424. pkey->flags |= KEY_INFO_WPA_MCAST;
  1425. } else {
  1426. pkey->flags |= KEY_INFO_WPA_UNICAST;
  1427. }
  1428. if (alg == IW_ENCODE_ALG_TKIP) {
  1429. pkey->type = KEY_TYPE_ID_TKIP;
  1430. } else if (alg == IW_ENCODE_ALG_CCMP) {
  1431. pkey->type = KEY_TYPE_ID_AES;
  1432. }
  1433. /* If WPA isn't enabled yet, do that now */
  1434. if ( assoc_req->secinfo.WPAenabled == 0
  1435. && assoc_req->secinfo.WPA2enabled == 0) {
  1436. assoc_req->secinfo.WPAenabled = 1;
  1437. assoc_req->secinfo.WPA2enabled = 1;
  1438. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1439. }
  1440. disable_wep (assoc_req);
  1441. }
  1442. out:
  1443. if (ret == 0) {
  1444. wlan_postpone_association_work(priv);
  1445. } else {
  1446. wlan_cancel_association_work(priv);
  1447. }
  1448. mutex_unlock(&adapter->lock);
  1449. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1450. return ret;
  1451. }
  1452. static int wlan_set_genie(struct net_device *dev,
  1453. struct iw_request_info *info,
  1454. struct iw_point *dwrq,
  1455. char *extra)
  1456. {
  1457. wlan_private *priv = dev->priv;
  1458. wlan_adapter *adapter = priv->adapter;
  1459. int ret = 0;
  1460. struct assoc_request * assoc_req;
  1461. lbs_deb_enter(LBS_DEB_WEXT);
  1462. mutex_lock(&adapter->lock);
  1463. assoc_req = wlan_get_association_request(adapter);
  1464. if (!assoc_req) {
  1465. ret = -ENOMEM;
  1466. goto out;
  1467. }
  1468. if (dwrq->length > MAX_WPA_IE_LEN ||
  1469. (dwrq->length && extra == NULL)) {
  1470. ret = -EINVAL;
  1471. goto out;
  1472. }
  1473. if (dwrq->length) {
  1474. memcpy(&assoc_req->wpa_ie[0], extra, dwrq->length);
  1475. assoc_req->wpa_ie_len = dwrq->length;
  1476. } else {
  1477. memset(&assoc_req->wpa_ie[0], 0, sizeof(adapter->wpa_ie));
  1478. assoc_req->wpa_ie_len = 0;
  1479. }
  1480. out:
  1481. if (ret == 0) {
  1482. set_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags);
  1483. wlan_postpone_association_work(priv);
  1484. } else {
  1485. wlan_cancel_association_work(priv);
  1486. }
  1487. mutex_unlock(&adapter->lock);
  1488. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1489. return ret;
  1490. }
  1491. static int wlan_get_genie(struct net_device *dev,
  1492. struct iw_request_info *info,
  1493. struct iw_point *dwrq,
  1494. char *extra)
  1495. {
  1496. int ret = 0;
  1497. wlan_private *priv = dev->priv;
  1498. wlan_adapter *adapter = priv->adapter;
  1499. lbs_deb_enter(LBS_DEB_WEXT);
  1500. if (adapter->wpa_ie_len == 0) {
  1501. dwrq->length = 0;
  1502. goto out;
  1503. }
  1504. if (dwrq->length < adapter->wpa_ie_len) {
  1505. ret = -E2BIG;
  1506. goto out;
  1507. }
  1508. dwrq->length = adapter->wpa_ie_len;
  1509. memcpy(extra, &adapter->wpa_ie[0], adapter->wpa_ie_len);
  1510. out:
  1511. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1512. return ret;
  1513. }
  1514. static int wlan_set_auth(struct net_device *dev,
  1515. struct iw_request_info *info,
  1516. struct iw_param *dwrq,
  1517. char *extra)
  1518. {
  1519. wlan_private *priv = dev->priv;
  1520. wlan_adapter *adapter = priv->adapter;
  1521. struct assoc_request * assoc_req;
  1522. int ret = 0;
  1523. int updated = 0;
  1524. lbs_deb_enter(LBS_DEB_WEXT);
  1525. mutex_lock(&adapter->lock);
  1526. assoc_req = wlan_get_association_request(adapter);
  1527. if (!assoc_req) {
  1528. ret = -ENOMEM;
  1529. goto out;
  1530. }
  1531. switch (dwrq->flags & IW_AUTH_INDEX) {
  1532. case IW_AUTH_TKIP_COUNTERMEASURES:
  1533. case IW_AUTH_CIPHER_PAIRWISE:
  1534. case IW_AUTH_CIPHER_GROUP:
  1535. case IW_AUTH_KEY_MGMT:
  1536. case IW_AUTH_DROP_UNENCRYPTED:
  1537. /*
  1538. * libertas does not use these parameters
  1539. */
  1540. break;
  1541. case IW_AUTH_WPA_VERSION:
  1542. if (dwrq->value & IW_AUTH_WPA_VERSION_DISABLED) {
  1543. assoc_req->secinfo.WPAenabled = 0;
  1544. assoc_req->secinfo.WPA2enabled = 0;
  1545. disable_wpa (assoc_req);
  1546. }
  1547. if (dwrq->value & IW_AUTH_WPA_VERSION_WPA) {
  1548. assoc_req->secinfo.WPAenabled = 1;
  1549. assoc_req->secinfo.wep_enabled = 0;
  1550. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1551. }
  1552. if (dwrq->value & IW_AUTH_WPA_VERSION_WPA2) {
  1553. assoc_req->secinfo.WPA2enabled = 1;
  1554. assoc_req->secinfo.wep_enabled = 0;
  1555. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1556. }
  1557. updated = 1;
  1558. break;
  1559. case IW_AUTH_80211_AUTH_ALG:
  1560. if (dwrq->value & IW_AUTH_ALG_SHARED_KEY) {
  1561. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
  1562. } else if (dwrq->value & IW_AUTH_ALG_OPEN_SYSTEM) {
  1563. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1564. } else if (dwrq->value & IW_AUTH_ALG_LEAP) {
  1565. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_LEAP;
  1566. } else {
  1567. ret = -EINVAL;
  1568. }
  1569. updated = 1;
  1570. break;
  1571. case IW_AUTH_WPA_ENABLED:
  1572. if (dwrq->value) {
  1573. if (!assoc_req->secinfo.WPAenabled &&
  1574. !assoc_req->secinfo.WPA2enabled) {
  1575. assoc_req->secinfo.WPAenabled = 1;
  1576. assoc_req->secinfo.WPA2enabled = 1;
  1577. assoc_req->secinfo.wep_enabled = 0;
  1578. assoc_req->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  1579. }
  1580. } else {
  1581. assoc_req->secinfo.WPAenabled = 0;
  1582. assoc_req->secinfo.WPA2enabled = 0;
  1583. disable_wpa (assoc_req);
  1584. }
  1585. updated = 1;
  1586. break;
  1587. default:
  1588. ret = -EOPNOTSUPP;
  1589. break;
  1590. }
  1591. out:
  1592. if (ret == 0) {
  1593. if (updated)
  1594. set_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags);
  1595. wlan_postpone_association_work(priv);
  1596. } else if (ret != -EOPNOTSUPP) {
  1597. wlan_cancel_association_work(priv);
  1598. }
  1599. mutex_unlock(&adapter->lock);
  1600. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1601. return ret;
  1602. }
  1603. static int wlan_get_auth(struct net_device *dev,
  1604. struct iw_request_info *info,
  1605. struct iw_param *dwrq,
  1606. char *extra)
  1607. {
  1608. int ret = 0;
  1609. wlan_private *priv = dev->priv;
  1610. wlan_adapter *adapter = priv->adapter;
  1611. lbs_deb_enter(LBS_DEB_WEXT);
  1612. switch (dwrq->flags & IW_AUTH_INDEX) {
  1613. case IW_AUTH_WPA_VERSION:
  1614. dwrq->value = 0;
  1615. if (adapter->secinfo.WPAenabled)
  1616. dwrq->value |= IW_AUTH_WPA_VERSION_WPA;
  1617. if (adapter->secinfo.WPA2enabled)
  1618. dwrq->value |= IW_AUTH_WPA_VERSION_WPA2;
  1619. if (!dwrq->value)
  1620. dwrq->value |= IW_AUTH_WPA_VERSION_DISABLED;
  1621. break;
  1622. case IW_AUTH_80211_AUTH_ALG:
  1623. dwrq->value = adapter->secinfo.auth_mode;
  1624. break;
  1625. case IW_AUTH_WPA_ENABLED:
  1626. if (adapter->secinfo.WPAenabled && adapter->secinfo.WPA2enabled)
  1627. dwrq->value = 1;
  1628. break;
  1629. default:
  1630. ret = -EOPNOTSUPP;
  1631. }
  1632. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1633. return ret;
  1634. }
  1635. static int wlan_set_txpow(struct net_device *dev, struct iw_request_info *info,
  1636. struct iw_param *vwrq, char *extra)
  1637. {
  1638. int ret = 0;
  1639. wlan_private *priv = dev->priv;
  1640. wlan_adapter *adapter = priv->adapter;
  1641. u16 dbm;
  1642. lbs_deb_enter(LBS_DEB_WEXT);
  1643. if (vwrq->disabled) {
  1644. wlan_radio_ioctl(priv, RADIO_OFF);
  1645. return 0;
  1646. }
  1647. adapter->preamble = cmd_type_auto_preamble;
  1648. wlan_radio_ioctl(priv, RADIO_ON);
  1649. if ((vwrq->flags & IW_TXPOW_TYPE) == IW_TXPOW_MWATT) {
  1650. dbm = (u16) mw_to_dbm(vwrq->value);
  1651. } else
  1652. dbm = (u16) vwrq->value;
  1653. /* auto tx power control */
  1654. if (vwrq->fixed == 0)
  1655. dbm = 0xffff;
  1656. lbs_deb_wext("txpower set %d dbm\n", dbm);
  1657. ret = libertas_prepare_and_send_command(priv,
  1658. cmd_802_11_rf_tx_power,
  1659. cmd_act_tx_power_opt_set_low,
  1660. cmd_option_waitforrsp, 0, (void *)&dbm);
  1661. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1662. return ret;
  1663. }
  1664. static int wlan_get_essid(struct net_device *dev, struct iw_request_info *info,
  1665. struct iw_point *dwrq, char *extra)
  1666. {
  1667. wlan_private *priv = dev->priv;
  1668. wlan_adapter *adapter = priv->adapter;
  1669. lbs_deb_enter(LBS_DEB_WEXT);
  1670. /*
  1671. * Note : if dwrq->flags != 0, we should get the relevant SSID from
  1672. * the SSID list...
  1673. */
  1674. /*
  1675. * Get the current SSID
  1676. */
  1677. if (adapter->connect_status == libertas_connected) {
  1678. memcpy(extra, adapter->curbssparams.ssid,
  1679. adapter->curbssparams.ssid_len);
  1680. extra[adapter->curbssparams.ssid_len] = '\0';
  1681. } else {
  1682. memset(extra, 0, 32);
  1683. extra[adapter->curbssparams.ssid_len] = '\0';
  1684. }
  1685. /*
  1686. * If none, we may want to get the one that was set
  1687. */
  1688. /* To make the driver backward compatible with WPA supplicant v0.2.4 */
  1689. if (dwrq->length == 32) /* check with WPA supplicant buffer size */
  1690. dwrq->length = min_t(size_t, adapter->curbssparams.ssid_len,
  1691. IW_ESSID_MAX_SIZE);
  1692. else
  1693. dwrq->length = adapter->curbssparams.ssid_len + 1;
  1694. dwrq->flags = 1; /* active */
  1695. lbs_deb_leave(LBS_DEB_WEXT);
  1696. return 0;
  1697. }
  1698. static int wlan_set_essid(struct net_device *dev, struct iw_request_info *info,
  1699. struct iw_point *dwrq, char *extra)
  1700. {
  1701. wlan_private *priv = dev->priv;
  1702. wlan_adapter *adapter = priv->adapter;
  1703. int ret = 0;
  1704. u8 ssid[IW_ESSID_MAX_SIZE];
  1705. u8 ssid_len = 0;
  1706. struct assoc_request * assoc_req;
  1707. int in_ssid_len = dwrq->length;
  1708. lbs_deb_enter(LBS_DEB_WEXT);
  1709. /*
  1710. * WE-20 and earlier NULL pad the end of the SSID and increment
  1711. * SSID length so it can be used like a string. WE-21 and later don't,
  1712. * but some userspace tools aren't able to cope with the change.
  1713. */
  1714. if ((in_ssid_len > 0) && (extra[in_ssid_len - 1] == '\0'))
  1715. in_ssid_len--;
  1716. /* Check the size of the string */
  1717. if (in_ssid_len > IW_ESSID_MAX_SIZE) {
  1718. ret = -E2BIG;
  1719. goto out;
  1720. }
  1721. memset(&ssid, 0, sizeof(ssid));
  1722. if (!dwrq->flags || !in_ssid_len) {
  1723. /* "any" SSID requested; leave SSID blank */
  1724. } else {
  1725. /* Specific SSID requested */
  1726. memcpy(&ssid, extra, in_ssid_len);
  1727. ssid_len = in_ssid_len;
  1728. }
  1729. if (!ssid_len) {
  1730. lbs_deb_wext("requested any SSID\n");
  1731. } else {
  1732. lbs_deb_wext("requested SSID '%s'\n",
  1733. escape_essid(ssid, ssid_len));
  1734. }
  1735. out:
  1736. mutex_lock(&adapter->lock);
  1737. if (ret == 0) {
  1738. /* Get or create the current association request */
  1739. assoc_req = wlan_get_association_request(adapter);
  1740. if (!assoc_req) {
  1741. ret = -ENOMEM;
  1742. } else {
  1743. /* Copy the SSID to the association request */
  1744. memcpy(&assoc_req->ssid, &ssid, IW_ESSID_MAX_SIZE);
  1745. assoc_req->ssid_len = ssid_len;
  1746. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  1747. wlan_postpone_association_work(priv);
  1748. }
  1749. }
  1750. /* Cancel the association request if there was an error */
  1751. if (ret != 0) {
  1752. wlan_cancel_association_work(priv);
  1753. }
  1754. mutex_unlock(&adapter->lock);
  1755. lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
  1756. return ret;
  1757. }
  1758. /**
  1759. * @brief Connect to the AP or Ad-hoc Network with specific bssid
  1760. *
  1761. * @param dev A pointer to net_device structure
  1762. * @param info A pointer to iw_request_info structure
  1763. * @param awrq A pointer to iw_param structure
  1764. * @param extra A pointer to extra data buf
  1765. * @return 0 --success, otherwise fail
  1766. */
  1767. static int wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
  1768. struct sockaddr *awrq, char *extra)
  1769. {
  1770. wlan_private *priv = dev->priv;
  1771. wlan_adapter *adapter = priv->adapter;
  1772. struct assoc_request * assoc_req;
  1773. int ret = 0;
  1774. lbs_deb_enter(LBS_DEB_WEXT);
  1775. if (awrq->sa_family != ARPHRD_ETHER)
  1776. return -EINVAL;
  1777. lbs_deb_wext("ASSOC: WAP: sa_data " MAC_FMT "\n", MAC_ARG(awrq->sa_data));
  1778. mutex_lock(&adapter->lock);
  1779. /* Get or create the current association request */
  1780. assoc_req = wlan_get_association_request(adapter);
  1781. if (!assoc_req) {
  1782. wlan_cancel_association_work(priv);
  1783. ret = -ENOMEM;
  1784. } else {
  1785. /* Copy the BSSID to the association request */
  1786. memcpy(&assoc_req->bssid, awrq->sa_data, ETH_ALEN);
  1787. set_bit(ASSOC_FLAG_BSSID, &assoc_req->flags);
  1788. wlan_postpone_association_work(priv);
  1789. }
  1790. mutex_unlock(&adapter->lock);
  1791. return ret;
  1792. }
  1793. void libertas_get_fwversion(wlan_adapter * adapter, char *fwversion, int maxlen)
  1794. {
  1795. char fwver[32];
  1796. mutex_lock(&adapter->lock);
  1797. if (adapter->fwreleasenumber[3] == 0)
  1798. sprintf(fwver, "%u.%u.%u",
  1799. adapter->fwreleasenumber[2],
  1800. adapter->fwreleasenumber[1],
  1801. adapter->fwreleasenumber[0]);
  1802. else
  1803. sprintf(fwver, "%u.%u.%u.p%u",
  1804. adapter->fwreleasenumber[2],
  1805. adapter->fwreleasenumber[1],
  1806. adapter->fwreleasenumber[0],
  1807. adapter->fwreleasenumber[3]);
  1808. mutex_unlock(&adapter->lock);
  1809. snprintf(fwversion, maxlen, fwver);
  1810. }
  1811. /*
  1812. * iwconfig settable callbacks
  1813. */
  1814. static const iw_handler wlan_handler[] = {
  1815. (iw_handler) NULL, /* SIOCSIWCOMMIT */
  1816. (iw_handler) wlan_get_name, /* SIOCGIWNAME */
  1817. (iw_handler) NULL, /* SIOCSIWNWID */
  1818. (iw_handler) NULL, /* SIOCGIWNWID */
  1819. (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
  1820. (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
  1821. (iw_handler) wlan_set_mode, /* SIOCSIWMODE */
  1822. (iw_handler) wlan_get_mode, /* SIOCGIWMODE */
  1823. (iw_handler) NULL, /* SIOCSIWSENS */
  1824. (iw_handler) NULL, /* SIOCGIWSENS */
  1825. (iw_handler) NULL, /* SIOCSIWRANGE */
  1826. (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
  1827. (iw_handler) NULL, /* SIOCSIWPRIV */
  1828. (iw_handler) NULL, /* SIOCGIWPRIV */
  1829. (iw_handler) NULL, /* SIOCSIWSTATS */
  1830. (iw_handler) NULL, /* SIOCGIWSTATS */
  1831. iw_handler_set_spy, /* SIOCSIWSPY */
  1832. iw_handler_get_spy, /* SIOCGIWSPY */
  1833. iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
  1834. iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
  1835. (iw_handler) wlan_set_wap, /* SIOCSIWAP */
  1836. (iw_handler) wlan_get_wap, /* SIOCGIWAP */
  1837. (iw_handler) NULL, /* SIOCSIWMLME */
  1838. (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
  1839. (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
  1840. (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
  1841. (iw_handler) wlan_set_essid, /* SIOCSIWESSID */
  1842. (iw_handler) wlan_get_essid, /* SIOCGIWESSID */
  1843. (iw_handler) wlan_set_nick, /* SIOCSIWNICKN */
  1844. (iw_handler) wlan_get_nick, /* SIOCGIWNICKN */
  1845. (iw_handler) NULL, /* -- hole -- */
  1846. (iw_handler) NULL, /* -- hole -- */
  1847. (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
  1848. (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
  1849. (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
  1850. (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
  1851. (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
  1852. (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
  1853. (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
  1854. (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
  1855. (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
  1856. (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
  1857. (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
  1858. (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
  1859. (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
  1860. (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
  1861. (iw_handler) NULL, /* -- hole -- */
  1862. (iw_handler) NULL, /* -- hole -- */
  1863. (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
  1864. (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
  1865. (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
  1866. (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
  1867. (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
  1868. (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
  1869. (iw_handler) NULL, /* SIOCSIWPMKSA */
  1870. };
  1871. static const iw_handler mesh_wlan_handler[] = {
  1872. (iw_handler) NULL, /* SIOCSIWCOMMIT */
  1873. (iw_handler) wlan_get_name, /* SIOCGIWNAME */
  1874. (iw_handler) NULL, /* SIOCSIWNWID */
  1875. (iw_handler) NULL, /* SIOCGIWNWID */
  1876. (iw_handler) wlan_set_freq, /* SIOCSIWFREQ */
  1877. (iw_handler) wlan_get_freq, /* SIOCGIWFREQ */
  1878. (iw_handler) NULL, /* SIOCSIWMODE */
  1879. (iw_handler) mesh_wlan_get_mode, /* SIOCGIWMODE */
  1880. (iw_handler) NULL, /* SIOCSIWSENS */
  1881. (iw_handler) NULL, /* SIOCGIWSENS */
  1882. (iw_handler) NULL, /* SIOCSIWRANGE */
  1883. (iw_handler) wlan_get_range, /* SIOCGIWRANGE */
  1884. (iw_handler) NULL, /* SIOCSIWPRIV */
  1885. (iw_handler) NULL, /* SIOCGIWPRIV */
  1886. (iw_handler) NULL, /* SIOCSIWSTATS */
  1887. (iw_handler) NULL, /* SIOCGIWSTATS */
  1888. iw_handler_set_spy, /* SIOCSIWSPY */
  1889. iw_handler_get_spy, /* SIOCGIWSPY */
  1890. iw_handler_set_thrspy, /* SIOCSIWTHRSPY */
  1891. iw_handler_get_thrspy, /* SIOCGIWTHRSPY */
  1892. (iw_handler) NULL, /* SIOCSIWAP */
  1893. (iw_handler) NULL, /* SIOCGIWAP */
  1894. (iw_handler) NULL, /* SIOCSIWMLME */
  1895. (iw_handler) NULL, /* SIOCGIWAPLIST - deprecated */
  1896. (iw_handler) libertas_set_scan, /* SIOCSIWSCAN */
  1897. (iw_handler) libertas_get_scan, /* SIOCGIWSCAN */
  1898. (iw_handler) NULL, /* SIOCSIWESSID */
  1899. (iw_handler) NULL, /* SIOCGIWESSID */
  1900. (iw_handler) NULL, /* SIOCSIWNICKN */
  1901. (iw_handler) mesh_get_nick, /* SIOCGIWNICKN */
  1902. (iw_handler) NULL, /* -- hole -- */
  1903. (iw_handler) NULL, /* -- hole -- */
  1904. (iw_handler) wlan_set_rate, /* SIOCSIWRATE */
  1905. (iw_handler) wlan_get_rate, /* SIOCGIWRATE */
  1906. (iw_handler) wlan_set_rts, /* SIOCSIWRTS */
  1907. (iw_handler) wlan_get_rts, /* SIOCGIWRTS */
  1908. (iw_handler) wlan_set_frag, /* SIOCSIWFRAG */
  1909. (iw_handler) wlan_get_frag, /* SIOCGIWFRAG */
  1910. (iw_handler) wlan_set_txpow, /* SIOCSIWTXPOW */
  1911. (iw_handler) wlan_get_txpow, /* SIOCGIWTXPOW */
  1912. (iw_handler) wlan_set_retry, /* SIOCSIWRETRY */
  1913. (iw_handler) wlan_get_retry, /* SIOCGIWRETRY */
  1914. (iw_handler) wlan_set_encode, /* SIOCSIWENCODE */
  1915. (iw_handler) wlan_get_encode, /* SIOCGIWENCODE */
  1916. (iw_handler) wlan_set_power, /* SIOCSIWPOWER */
  1917. (iw_handler) wlan_get_power, /* SIOCGIWPOWER */
  1918. (iw_handler) NULL, /* -- hole -- */
  1919. (iw_handler) NULL, /* -- hole -- */
  1920. (iw_handler) wlan_set_genie, /* SIOCSIWGENIE */
  1921. (iw_handler) wlan_get_genie, /* SIOCGIWGENIE */
  1922. (iw_handler) wlan_set_auth, /* SIOCSIWAUTH */
  1923. (iw_handler) wlan_get_auth, /* SIOCGIWAUTH */
  1924. (iw_handler) wlan_set_encodeext,/* SIOCSIWENCODEEXT */
  1925. (iw_handler) wlan_get_encodeext,/* SIOCGIWENCODEEXT */
  1926. (iw_handler) NULL, /* SIOCSIWPMKSA */
  1927. };
  1928. struct iw_handler_def libertas_handler_def = {
  1929. .num_standard = sizeof(wlan_handler) / sizeof(iw_handler),
  1930. .standard = (iw_handler *) wlan_handler,
  1931. .get_wireless_stats = wlan_get_wireless_stats,
  1932. };
  1933. struct iw_handler_def mesh_handler_def = {
  1934. .num_standard = sizeof(mesh_wlan_handler) / sizeof(iw_handler),
  1935. .standard = (iw_handler *) mesh_wlan_handler,
  1936. .get_wireless_stats = wlan_get_wireless_stats,
  1937. };