cfg.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  1. /*
  2. * Implement cfg80211 ("iw") support.
  3. *
  4. * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
  5. * Holger Schurig <hs4233@mail.mn-solutions.de>
  6. *
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/wait.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/ieee80211.h>
  14. #include <net/cfg80211.h>
  15. #include <asm/unaligned.h>
  16. #include "decl.h"
  17. #include "cfg.h"
  18. #include "cmd.h"
  19. #define CHAN2G(_channel, _freq, _flags) { \
  20. .band = IEEE80211_BAND_2GHZ, \
  21. .center_freq = (_freq), \
  22. .hw_value = (_channel), \
  23. .flags = (_flags), \
  24. .max_antenna_gain = 0, \
  25. .max_power = 30, \
  26. }
  27. static struct ieee80211_channel lbs_2ghz_channels[] = {
  28. CHAN2G(1, 2412, 0),
  29. CHAN2G(2, 2417, 0),
  30. CHAN2G(3, 2422, 0),
  31. CHAN2G(4, 2427, 0),
  32. CHAN2G(5, 2432, 0),
  33. CHAN2G(6, 2437, 0),
  34. CHAN2G(7, 2442, 0),
  35. CHAN2G(8, 2447, 0),
  36. CHAN2G(9, 2452, 0),
  37. CHAN2G(10, 2457, 0),
  38. CHAN2G(11, 2462, 0),
  39. CHAN2G(12, 2467, 0),
  40. CHAN2G(13, 2472, 0),
  41. CHAN2G(14, 2484, 0),
  42. };
  43. #define RATETAB_ENT(_rate, _hw_value, _flags) { \
  44. .bitrate = (_rate), \
  45. .hw_value = (_hw_value), \
  46. .flags = (_flags), \
  47. }
  48. /* Table 6 in section 3.2.1.1 */
  49. static struct ieee80211_rate lbs_rates[] = {
  50. RATETAB_ENT(10, 0, 0),
  51. RATETAB_ENT(20, 1, 0),
  52. RATETAB_ENT(55, 2, 0),
  53. RATETAB_ENT(110, 3, 0),
  54. RATETAB_ENT(60, 9, 0),
  55. RATETAB_ENT(90, 6, 0),
  56. RATETAB_ENT(120, 7, 0),
  57. RATETAB_ENT(180, 8, 0),
  58. RATETAB_ENT(240, 9, 0),
  59. RATETAB_ENT(360, 10, 0),
  60. RATETAB_ENT(480, 11, 0),
  61. RATETAB_ENT(540, 12, 0),
  62. };
  63. static struct ieee80211_supported_band lbs_band_2ghz = {
  64. .channels = lbs_2ghz_channels,
  65. .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  66. .bitrates = lbs_rates,
  67. .n_bitrates = ARRAY_SIZE(lbs_rates),
  68. };
  69. static const u32 cipher_suites[] = {
  70. WLAN_CIPHER_SUITE_WEP40,
  71. WLAN_CIPHER_SUITE_WEP104,
  72. WLAN_CIPHER_SUITE_TKIP,
  73. WLAN_CIPHER_SUITE_CCMP,
  74. };
  75. /* Time to stay on the channel */
  76. #define LBS_DWELL_PASSIVE 100
  77. #define LBS_DWELL_ACTIVE 40
  78. /***************************************************************************
  79. * Misc utility functions
  80. *
  81. * TLVs are Marvell specific. They are very similar to IEs, they have the
  82. * same structure: type, length, data*. The only difference: for IEs, the
  83. * type and length are u8, but for TLVs they're __le16.
  84. */
  85. /*
  86. * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
  87. * in the firmware spec
  88. */
  89. static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
  90. {
  91. int ret = -ENOTSUPP;
  92. switch (auth_type) {
  93. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  94. case NL80211_AUTHTYPE_SHARED_KEY:
  95. ret = auth_type;
  96. break;
  97. case NL80211_AUTHTYPE_AUTOMATIC:
  98. ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
  99. break;
  100. case NL80211_AUTHTYPE_NETWORK_EAP:
  101. ret = 0x80;
  102. break;
  103. default:
  104. /* silence compiler */
  105. break;
  106. }
  107. return ret;
  108. }
  109. /* Various firmware commands need the list of supported rates, but with
  110. the hight-bit set for basic rates */
  111. static int lbs_add_rates(u8 *rates)
  112. {
  113. size_t i;
  114. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  115. u8 rate = lbs_rates[i].bitrate / 5;
  116. if (rate == 0x02 || rate == 0x04 ||
  117. rate == 0x0b || rate == 0x16)
  118. rate |= 0x80;
  119. rates[i] = rate;
  120. }
  121. return ARRAY_SIZE(lbs_rates);
  122. }
  123. /***************************************************************************
  124. * TLV utility functions
  125. *
  126. * TLVs are Marvell specific. They are very similar to IEs, they have the
  127. * same structure: type, length, data*. The only difference: for IEs, the
  128. * type and length are u8, but for TLVs they're __le16.
  129. */
  130. /*
  131. * Add ssid TLV
  132. */
  133. #define LBS_MAX_SSID_TLV_SIZE \
  134. (sizeof(struct mrvl_ie_header) \
  135. + IEEE80211_MAX_SSID_LEN)
  136. static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
  137. {
  138. struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
  139. /*
  140. * TLV-ID SSID 00 00
  141. * length 06 00
  142. * ssid 4d 4e 54 45 53 54
  143. */
  144. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  145. ssid_tlv->header.len = cpu_to_le16(ssid_len);
  146. memcpy(ssid_tlv->ssid, ssid, ssid_len);
  147. return sizeof(ssid_tlv->header) + ssid_len;
  148. }
  149. /*
  150. * Add channel list TLV (section 8.4.2)
  151. *
  152. * Actual channel data comes from priv->wdev->wiphy->channels.
  153. */
  154. #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  155. (sizeof(struct mrvl_ie_header) \
  156. + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
  157. static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
  158. int last_channel, int active_scan)
  159. {
  160. int chanscanparamsize = sizeof(struct chanscanparamset) *
  161. (last_channel - priv->scan_channel);
  162. struct mrvl_ie_header *header = (void *) tlv;
  163. /*
  164. * TLV-ID CHANLIST 01 01
  165. * length 0e 00
  166. * channel 00 01 00 00 00 64 00
  167. * radio type 00
  168. * channel 01
  169. * scan type 00
  170. * min scan time 00 00
  171. * max scan time 64 00
  172. * channel 2 00 02 00 00 00 64 00
  173. *
  174. */
  175. header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
  176. header->len = cpu_to_le16(chanscanparamsize);
  177. tlv += sizeof(struct mrvl_ie_header);
  178. /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
  179. last_channel); */
  180. memset(tlv, 0, chanscanparamsize);
  181. while (priv->scan_channel < last_channel) {
  182. struct chanscanparamset *param = (void *) tlv;
  183. param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
  184. param->channumber =
  185. priv->scan_req->channels[priv->scan_channel]->hw_value;
  186. if (active_scan) {
  187. param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
  188. } else {
  189. param->chanscanmode.passivescan = 1;
  190. param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
  191. }
  192. tlv += sizeof(struct chanscanparamset);
  193. priv->scan_channel++;
  194. }
  195. return sizeof(struct mrvl_ie_header) + chanscanparamsize;
  196. }
  197. /*
  198. * Add rates TLV
  199. *
  200. * The rates are in lbs_bg_rates[], but for the 802.11b
  201. * rates the high bit is set. We add this TLV only because
  202. * there's a firmware which otherwise doesn't report all
  203. * APs in range.
  204. */
  205. #define LBS_MAX_RATES_TLV_SIZE \
  206. (sizeof(struct mrvl_ie_header) \
  207. + (ARRAY_SIZE(lbs_rates)))
  208. /* Adds a TLV with all rates the hardware supports */
  209. static int lbs_add_supported_rates_tlv(u8 *tlv)
  210. {
  211. size_t i;
  212. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  213. /*
  214. * TLV-ID RATES 01 00
  215. * length 0e 00
  216. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  217. */
  218. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  219. tlv += sizeof(rate_tlv->header);
  220. i = lbs_add_rates(tlv);
  221. tlv += i;
  222. rate_tlv->header.len = cpu_to_le16(i);
  223. return sizeof(rate_tlv->header) + i;
  224. }
  225. /* Add common rates from a TLV and return the new end of the TLV */
  226. static u8 *
  227. add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
  228. {
  229. int hw, ap, ap_max = ie[1];
  230. u8 hw_rate;
  231. /* Advance past IE header */
  232. ie += 2;
  233. lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
  234. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  235. hw_rate = lbs_rates[hw].bitrate / 5;
  236. for (ap = 0; ap < ap_max; ap++) {
  237. if (hw_rate == (ie[ap] & 0x7f)) {
  238. *tlv++ = ie[ap];
  239. *nrates = *nrates + 1;
  240. }
  241. }
  242. }
  243. return tlv;
  244. }
  245. /*
  246. * Adds a TLV with all rates the hardware *and* BSS supports.
  247. */
  248. static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
  249. {
  250. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  251. const u8 *rates_eid, *ext_rates_eid;
  252. int n = 0;
  253. rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  254. ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  255. /*
  256. * 01 00 TLV_TYPE_RATES
  257. * 04 00 len
  258. * 82 84 8b 96 rates
  259. */
  260. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  261. tlv += sizeof(rate_tlv->header);
  262. /* Add basic rates */
  263. if (rates_eid) {
  264. tlv = add_ie_rates(tlv, rates_eid, &n);
  265. /* Add extended rates, if any */
  266. if (ext_rates_eid)
  267. tlv = add_ie_rates(tlv, ext_rates_eid, &n);
  268. } else {
  269. lbs_deb_assoc("assoc: bss had no basic rate IE\n");
  270. /* Fallback: add basic 802.11b rates */
  271. *tlv++ = 0x82;
  272. *tlv++ = 0x84;
  273. *tlv++ = 0x8b;
  274. *tlv++ = 0x96;
  275. n = 4;
  276. }
  277. rate_tlv->header.len = cpu_to_le16(n);
  278. return sizeof(rate_tlv->header) + n;
  279. }
  280. /*
  281. * Add auth type TLV.
  282. *
  283. * This is only needed for newer firmware (V9 and up).
  284. */
  285. #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
  286. sizeof(struct mrvl_ie_auth_type)
  287. static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
  288. {
  289. struct mrvl_ie_auth_type *auth = (void *) tlv;
  290. /*
  291. * 1f 01 TLV_TYPE_AUTH_TYPE
  292. * 01 00 len
  293. * 01 auth type
  294. */
  295. auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
  296. auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
  297. auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
  298. return sizeof(*auth);
  299. }
  300. /*
  301. * Add channel (phy ds) TLV
  302. */
  303. #define LBS_MAX_CHANNEL_TLV_SIZE \
  304. sizeof(struct mrvl_ie_header)
  305. static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
  306. {
  307. struct mrvl_ie_ds_param_set *ds = (void *) tlv;
  308. /*
  309. * 03 00 TLV_TYPE_PHY_DS
  310. * 01 00 len
  311. * 06 channel
  312. */
  313. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  314. ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
  315. ds->channel = channel;
  316. return sizeof(*ds);
  317. }
  318. /*
  319. * Add (empty) CF param TLV of the form:
  320. */
  321. #define LBS_MAX_CF_PARAM_TLV_SIZE \
  322. sizeof(struct mrvl_ie_header)
  323. static int lbs_add_cf_param_tlv(u8 *tlv)
  324. {
  325. struct mrvl_ie_cf_param_set *cf = (void *)tlv;
  326. /*
  327. * 04 00 TLV_TYPE_CF
  328. * 06 00 len
  329. * 00 cfpcnt
  330. * 00 cfpperiod
  331. * 00 00 cfpmaxduration
  332. * 00 00 cfpdurationremaining
  333. */
  334. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  335. cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
  336. return sizeof(*cf);
  337. }
  338. /*
  339. * Add WPA TLV
  340. */
  341. #define LBS_MAX_WPA_TLV_SIZE \
  342. (sizeof(struct mrvl_ie_header) \
  343. + 128 /* TODO: I guessed the size */)
  344. static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
  345. {
  346. size_t tlv_len;
  347. /*
  348. * We need just convert an IE to an TLV. IEs use u8 for the header,
  349. * u8 type
  350. * u8 len
  351. * u8[] data
  352. * but TLVs use __le16 instead:
  353. * __le16 type
  354. * __le16 len
  355. * u8[] data
  356. */
  357. *tlv++ = *ie++;
  358. *tlv++ = 0;
  359. tlv_len = *tlv++ = *ie++;
  360. *tlv++ = 0;
  361. while (tlv_len--)
  362. *tlv++ = *ie++;
  363. /* the TLV is two bytes larger than the IE */
  364. return ie_len + 2;
  365. }
  366. /***************************************************************************
  367. * Set Channel
  368. */
  369. static int lbs_cfg_set_channel(struct wiphy *wiphy,
  370. struct net_device *netdev,
  371. struct ieee80211_channel *channel,
  372. enum nl80211_channel_type channel_type)
  373. {
  374. struct lbs_private *priv = wiphy_priv(wiphy);
  375. int ret = -ENOTSUPP;
  376. lbs_deb_enter_args(LBS_DEB_CFG80211, "freq %d, type %d",
  377. channel->center_freq, channel_type);
  378. if (channel_type != NL80211_CHAN_NO_HT)
  379. goto out;
  380. ret = lbs_set_channel(priv, channel->hw_value);
  381. out:
  382. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  383. return ret;
  384. }
  385. /***************************************************************************
  386. * Scanning
  387. */
  388. /*
  389. * When scanning, the firmware doesn't send a nul packet with the power-safe
  390. * bit to the AP. So we cannot stay away from our current channel too long,
  391. * otherwise we loose data. So take a "nap" while scanning every other
  392. * while.
  393. */
  394. #define LBS_SCAN_BEFORE_NAP 4
  395. /*
  396. * When the firmware reports back a scan-result, it gives us an "u8 rssi",
  397. * which isn't really an RSSI, as it becomes larger when moving away from
  398. * the AP. Anyway, we need to convert that into mBm.
  399. */
  400. #define LBS_SCAN_RSSI_TO_MBM(rssi) \
  401. ((-(int)rssi + 3)*100)
  402. static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
  403. struct cmd_header *resp)
  404. {
  405. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  406. int bsssize;
  407. const u8 *pos;
  408. const u8 *tsfdesc;
  409. int tsfsize;
  410. int i;
  411. int ret = -EILSEQ;
  412. lbs_deb_enter(LBS_DEB_CFG80211);
  413. bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
  414. lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
  415. scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
  416. if (scanresp->nr_sets == 0) {
  417. ret = 0;
  418. goto done;
  419. }
  420. /*
  421. * The general layout of the scan response is described in chapter
  422. * 5.7.1. Basically we have a common part, then any number of BSS
  423. * descriptor sections. Finally we have section with the same number
  424. * of TSFs.
  425. *
  426. * cmd_ds_802_11_scan_rsp
  427. * cmd_header
  428. * pos_size
  429. * nr_sets
  430. * bssdesc 1
  431. * bssid
  432. * rssi
  433. * timestamp
  434. * intvl
  435. * capa
  436. * IEs
  437. * bssdesc 2
  438. * bssdesc n
  439. * MrvlIEtypes_TsfFimestamp_t
  440. * TSF for BSS 1
  441. * TSF for BSS 2
  442. * TSF for BSS n
  443. */
  444. pos = scanresp->bssdesc_and_tlvbuffer;
  445. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
  446. scanresp->bssdescriptsize);
  447. tsfdesc = pos + bsssize;
  448. tsfsize = 4 + 8 * scanresp->nr_sets;
  449. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
  450. /* Validity check: we expect a Marvell-Local TLV */
  451. i = get_unaligned_le16(tsfdesc);
  452. tsfdesc += 2;
  453. if (i != TLV_TYPE_TSFTIMESTAMP) {
  454. lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
  455. goto done;
  456. }
  457. /* Validity check: the TLV holds TSF values with 8 bytes each, so
  458. * the size in the TLV must match the nr_sets value */
  459. i = get_unaligned_le16(tsfdesc);
  460. tsfdesc += 2;
  461. if (i / 8 != scanresp->nr_sets) {
  462. lbs_deb_scan("scan response: invalid number of TSF timestamp "
  463. "sets (expected %d got %d)\n", scanresp->nr_sets,
  464. i / 8);
  465. goto done;
  466. }
  467. for (i = 0; i < scanresp->nr_sets; i++) {
  468. const u8 *bssid;
  469. const u8 *ie;
  470. int left;
  471. int ielen;
  472. int rssi;
  473. u16 intvl;
  474. u16 capa;
  475. int chan_no = -1;
  476. const u8 *ssid = NULL;
  477. u8 ssid_len = 0;
  478. DECLARE_SSID_BUF(ssid_buf);
  479. int len = get_unaligned_le16(pos);
  480. pos += 2;
  481. /* BSSID */
  482. bssid = pos;
  483. pos += ETH_ALEN;
  484. /* RSSI */
  485. rssi = *pos++;
  486. /* Packet time stamp */
  487. pos += 8;
  488. /* Beacon interval */
  489. intvl = get_unaligned_le16(pos);
  490. pos += 2;
  491. /* Capabilities */
  492. capa = get_unaligned_le16(pos);
  493. pos += 2;
  494. /* To find out the channel, we must parse the IEs */
  495. ie = pos;
  496. /* 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
  497. interval, capabilities */
  498. ielen = left = len - (6 + 1 + 8 + 2 + 2);
  499. while (left >= 2) {
  500. u8 id, elen;
  501. id = *pos++;
  502. elen = *pos++;
  503. left -= 2;
  504. if (elen > left || elen == 0) {
  505. lbs_deb_scan("scan response: invalid IE fmt\n");
  506. goto done;
  507. }
  508. if (id == WLAN_EID_DS_PARAMS)
  509. chan_no = *pos;
  510. if (id == WLAN_EID_SSID) {
  511. ssid = pos;
  512. ssid_len = elen;
  513. }
  514. left -= elen;
  515. pos += elen;
  516. }
  517. /* No channel, no luck */
  518. if (chan_no != -1) {
  519. struct wiphy *wiphy = priv->wdev->wiphy;
  520. int freq = ieee80211_channel_to_frequency(chan_no);
  521. struct ieee80211_channel *channel =
  522. ieee80211_get_channel(wiphy, freq);
  523. lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
  524. "%d dBm\n",
  525. bssid, capa, chan_no,
  526. print_ssid(ssid_buf, ssid, ssid_len),
  527. LBS_SCAN_RSSI_TO_MBM(rssi)/100);
  528. if (channel ||
  529. !(channel->flags & IEEE80211_CHAN_DISABLED))
  530. cfg80211_inform_bss(wiphy, channel,
  531. bssid, le64_to_cpu(*(__le64 *)tsfdesc),
  532. capa, intvl, ie, ielen,
  533. LBS_SCAN_RSSI_TO_MBM(rssi),
  534. GFP_KERNEL);
  535. } else
  536. lbs_deb_scan("scan response: missing BSS channel IE\n");
  537. tsfdesc += 8;
  538. }
  539. ret = 0;
  540. done:
  541. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  542. return ret;
  543. }
  544. /*
  545. * Our scan command contains a TLV, consting of a SSID TLV, a channel list
  546. * TLV and a rates TLV. Determine the maximum size of them:
  547. */
  548. #define LBS_SCAN_MAX_CMD_SIZE \
  549. (sizeof(struct cmd_ds_802_11_scan) \
  550. + LBS_MAX_SSID_TLV_SIZE \
  551. + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  552. + LBS_MAX_RATES_TLV_SIZE)
  553. /*
  554. * Assumes priv->scan_req is initialized and valid
  555. * Assumes priv->scan_channel is initialized
  556. */
  557. static void lbs_scan_worker(struct work_struct *work)
  558. {
  559. struct lbs_private *priv =
  560. container_of(work, struct lbs_private, scan_work.work);
  561. struct cmd_ds_802_11_scan *scan_cmd;
  562. u8 *tlv; /* pointer into our current, growing TLV storage area */
  563. int last_channel;
  564. int running, carrier;
  565. lbs_deb_enter(LBS_DEB_SCAN);
  566. scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
  567. if (scan_cmd == NULL)
  568. goto out_no_scan_cmd;
  569. /* prepare fixed part of scan command */
  570. scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
  571. /* stop network while we're away from our main channel */
  572. running = !netif_queue_stopped(priv->dev);
  573. carrier = netif_carrier_ok(priv->dev);
  574. if (running)
  575. netif_stop_queue(priv->dev);
  576. if (carrier)
  577. netif_carrier_off(priv->dev);
  578. /* prepare fixed part of scan command */
  579. tlv = scan_cmd->tlvbuffer;
  580. /* add SSID TLV */
  581. if (priv->scan_req->n_ssids)
  582. tlv += lbs_add_ssid_tlv(tlv,
  583. priv->scan_req->ssids[0].ssid,
  584. priv->scan_req->ssids[0].ssid_len);
  585. /* add channel TLVs */
  586. last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
  587. if (last_channel > priv->scan_req->n_channels)
  588. last_channel = priv->scan_req->n_channels;
  589. tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
  590. priv->scan_req->n_ssids);
  591. /* add rates TLV */
  592. tlv += lbs_add_supported_rates_tlv(tlv);
  593. if (priv->scan_channel < priv->scan_req->n_channels) {
  594. cancel_delayed_work(&priv->scan_work);
  595. if (!priv->stopping)
  596. queue_delayed_work(priv->work_thread, &priv->scan_work,
  597. msecs_to_jiffies(300));
  598. }
  599. /* This is the final data we are about to send */
  600. scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
  601. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  602. sizeof(*scan_cmd));
  603. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  604. tlv - scan_cmd->tlvbuffer);
  605. __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  606. le16_to_cpu(scan_cmd->hdr.size),
  607. lbs_ret_scan, 0);
  608. if (priv->scan_channel >= priv->scan_req->n_channels) {
  609. /* Mark scan done */
  610. if (priv->internal_scan)
  611. kfree(priv->scan_req);
  612. else
  613. cfg80211_scan_done(priv->scan_req, false);
  614. priv->scan_req = NULL;
  615. priv->last_scan = jiffies;
  616. }
  617. /* Restart network */
  618. if (carrier)
  619. netif_carrier_on(priv->dev);
  620. if (running && !priv->tx_pending_len)
  621. netif_wake_queue(priv->dev);
  622. kfree(scan_cmd);
  623. /* Wake up anything waiting on scan completion */
  624. if (priv->scan_req == NULL) {
  625. lbs_deb_scan("scan: waking up waiters\n");
  626. wake_up_all(&priv->scan_q);
  627. }
  628. out_no_scan_cmd:
  629. lbs_deb_leave(LBS_DEB_SCAN);
  630. }
  631. static void _internal_start_scan(struct lbs_private *priv, bool internal,
  632. struct cfg80211_scan_request *request)
  633. {
  634. lbs_deb_enter(LBS_DEB_CFG80211);
  635. lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
  636. request->n_ssids, request->n_channels, request->ie_len);
  637. priv->scan_channel = 0;
  638. queue_delayed_work(priv->work_thread, &priv->scan_work,
  639. msecs_to_jiffies(50));
  640. priv->scan_req = request;
  641. priv->internal_scan = internal;
  642. lbs_deb_leave(LBS_DEB_CFG80211);
  643. }
  644. static int lbs_cfg_scan(struct wiphy *wiphy,
  645. struct net_device *dev,
  646. struct cfg80211_scan_request *request)
  647. {
  648. struct lbs_private *priv = wiphy_priv(wiphy);
  649. int ret = 0;
  650. lbs_deb_enter(LBS_DEB_CFG80211);
  651. if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
  652. /* old scan request not yet processed */
  653. ret = -EAGAIN;
  654. goto out;
  655. }
  656. _internal_start_scan(priv, false, request);
  657. if (priv->surpriseremoved)
  658. ret = -EIO;
  659. out:
  660. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  661. return ret;
  662. }
  663. /***************************************************************************
  664. * Events
  665. */
  666. void lbs_send_disconnect_notification(struct lbs_private *priv)
  667. {
  668. lbs_deb_enter(LBS_DEB_CFG80211);
  669. cfg80211_disconnected(priv->dev,
  670. 0,
  671. NULL, 0,
  672. GFP_KERNEL);
  673. lbs_deb_leave(LBS_DEB_CFG80211);
  674. }
  675. void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
  676. {
  677. lbs_deb_enter(LBS_DEB_CFG80211);
  678. cfg80211_michael_mic_failure(priv->dev,
  679. priv->assoc_bss,
  680. event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
  681. NL80211_KEYTYPE_GROUP :
  682. NL80211_KEYTYPE_PAIRWISE,
  683. -1,
  684. NULL,
  685. GFP_KERNEL);
  686. lbs_deb_leave(LBS_DEB_CFG80211);
  687. }
  688. /***************************************************************************
  689. * Connect/disconnect
  690. */
  691. /*
  692. * This removes all WEP keys
  693. */
  694. static int lbs_remove_wep_keys(struct lbs_private *priv)
  695. {
  696. struct cmd_ds_802_11_set_wep cmd;
  697. int ret;
  698. lbs_deb_enter(LBS_DEB_CFG80211);
  699. memset(&cmd, 0, sizeof(cmd));
  700. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  701. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  702. cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
  703. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  704. lbs_deb_leave(LBS_DEB_CFG80211);
  705. return ret;
  706. }
  707. /*
  708. * Set WEP keys
  709. */
  710. static int lbs_set_wep_keys(struct lbs_private *priv)
  711. {
  712. struct cmd_ds_802_11_set_wep cmd;
  713. int i;
  714. int ret;
  715. lbs_deb_enter(LBS_DEB_CFG80211);
  716. /*
  717. * command 13 00
  718. * size 50 00
  719. * sequence xx xx
  720. * result 00 00
  721. * action 02 00 ACT_ADD
  722. * transmit key 00 00
  723. * type for key 1 01 WEP40
  724. * type for key 2 00
  725. * type for key 3 00
  726. * type for key 4 00
  727. * key 1 39 39 39 39 39 00 00 00
  728. * 00 00 00 00 00 00 00 00
  729. * key 2 00 00 00 00 00 00 00 00
  730. * 00 00 00 00 00 00 00 00
  731. * key 3 00 00 00 00 00 00 00 00
  732. * 00 00 00 00 00 00 00 00
  733. * key 4 00 00 00 00 00 00 00 00
  734. */
  735. if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
  736. priv->wep_key_len[2] || priv->wep_key_len[3]) {
  737. /* Only set wep keys if we have at least one of them */
  738. memset(&cmd, 0, sizeof(cmd));
  739. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  740. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  741. cmd.action = cpu_to_le16(CMD_ACT_ADD);
  742. for (i = 0; i < 4; i++) {
  743. switch (priv->wep_key_len[i]) {
  744. case WLAN_KEY_LEN_WEP40:
  745. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  746. break;
  747. case WLAN_KEY_LEN_WEP104:
  748. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  749. break;
  750. default:
  751. cmd.keytype[i] = 0;
  752. break;
  753. }
  754. memcpy(cmd.keymaterial[i], priv->wep_key[i],
  755. priv->wep_key_len[i]);
  756. }
  757. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  758. } else {
  759. /* Otherwise remove all wep keys */
  760. ret = lbs_remove_wep_keys(priv);
  761. }
  762. lbs_deb_leave(LBS_DEB_CFG80211);
  763. return ret;
  764. }
  765. /*
  766. * Enable/Disable RSN status
  767. */
  768. static int lbs_enable_rsn(struct lbs_private *priv, int enable)
  769. {
  770. struct cmd_ds_802_11_enable_rsn cmd;
  771. int ret;
  772. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
  773. /*
  774. * cmd 2f 00
  775. * size 0c 00
  776. * sequence xx xx
  777. * result 00 00
  778. * action 01 00 ACT_SET
  779. * enable 01 00
  780. */
  781. memset(&cmd, 0, sizeof(cmd));
  782. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  783. cmd.action = cpu_to_le16(CMD_ACT_SET);
  784. cmd.enable = cpu_to_le16(enable);
  785. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  786. lbs_deb_leave(LBS_DEB_CFG80211);
  787. return ret;
  788. }
  789. /*
  790. * Set WPA/WPA key material
  791. */
  792. /* like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
  793. * get rid of WEXT, this should go into host.h */
  794. struct cmd_key_material {
  795. struct cmd_header hdr;
  796. __le16 action;
  797. struct MrvlIEtype_keyParamSet param;
  798. } __packed;
  799. static int lbs_set_key_material(struct lbs_private *priv,
  800. int key_type,
  801. int key_info,
  802. u8 *key, u16 key_len)
  803. {
  804. struct cmd_key_material cmd;
  805. int ret;
  806. lbs_deb_enter(LBS_DEB_CFG80211);
  807. /*
  808. * Example for WPA (TKIP):
  809. *
  810. * cmd 5e 00
  811. * size 34 00
  812. * sequence xx xx
  813. * result 00 00
  814. * action 01 00
  815. * TLV type 00 01 key param
  816. * length 00 26
  817. * key type 01 00 TKIP
  818. * key info 06 00 UNICAST | ENABLED
  819. * key len 20 00
  820. * key 32 bytes
  821. */
  822. memset(&cmd, 0, sizeof(cmd));
  823. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  824. cmd.action = cpu_to_le16(CMD_ACT_SET);
  825. cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  826. cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
  827. cmd.param.keytypeid = cpu_to_le16(key_type);
  828. cmd.param.keyinfo = cpu_to_le16(key_info);
  829. cmd.param.keylen = cpu_to_le16(key_len);
  830. if (key && key_len)
  831. memcpy(cmd.param.key, key, key_len);
  832. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  833. lbs_deb_leave(LBS_DEB_CFG80211);
  834. return ret;
  835. }
  836. /*
  837. * Sets the auth type (open, shared, etc) in the firmware. That
  838. * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
  839. * command doesn't send an authentication frame at all, it just
  840. * stores the auth_type.
  841. */
  842. static int lbs_set_authtype(struct lbs_private *priv,
  843. struct cfg80211_connect_params *sme)
  844. {
  845. struct cmd_ds_802_11_authenticate cmd;
  846. int ret;
  847. lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
  848. /*
  849. * cmd 11 00
  850. * size 19 00
  851. * sequence xx xx
  852. * result 00 00
  853. * BSS id 00 13 19 80 da 30
  854. * auth type 00
  855. * reserved 00 00 00 00 00 00 00 00 00 00
  856. */
  857. memset(&cmd, 0, sizeof(cmd));
  858. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  859. if (sme->bssid)
  860. memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
  861. /* convert auth_type */
  862. ret = lbs_auth_to_authtype(sme->auth_type);
  863. if (ret < 0)
  864. goto done;
  865. cmd.authtype = ret;
  866. ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
  867. done:
  868. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  869. return ret;
  870. }
  871. /*
  872. * Create association request
  873. */
  874. #define LBS_ASSOC_MAX_CMD_SIZE \
  875. (sizeof(struct cmd_ds_802_11_associate) \
  876. - 512 /* cmd_ds_802_11_associate.iebuf */ \
  877. + LBS_MAX_SSID_TLV_SIZE \
  878. + LBS_MAX_CHANNEL_TLV_SIZE \
  879. + LBS_MAX_CF_PARAM_TLV_SIZE \
  880. + LBS_MAX_AUTH_TYPE_TLV_SIZE \
  881. + LBS_MAX_WPA_TLV_SIZE)
  882. static int lbs_associate(struct lbs_private *priv,
  883. struct cfg80211_bss *bss,
  884. struct cfg80211_connect_params *sme)
  885. {
  886. struct cmd_ds_802_11_associate_response *resp;
  887. struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
  888. GFP_KERNEL);
  889. const u8 *ssid_eid;
  890. size_t len, resp_ie_len;
  891. int status;
  892. int ret;
  893. u8 *pos = &(cmd->iebuf[0]);
  894. u8 *tmp;
  895. lbs_deb_enter(LBS_DEB_CFG80211);
  896. if (!cmd) {
  897. ret = -ENOMEM;
  898. goto done;
  899. }
  900. /*
  901. * cmd 50 00
  902. * length 34 00
  903. * sequence xx xx
  904. * result 00 00
  905. * BSS id 00 13 19 80 da 30
  906. * capabilities 11 00
  907. * listen interval 0a 00
  908. * beacon interval 00 00
  909. * DTIM period 00
  910. * TLVs xx (up to 512 bytes)
  911. */
  912. cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  913. /* Fill in static fields */
  914. memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
  915. cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  916. cmd->capability = cpu_to_le16(bss->capability);
  917. /* add SSID TLV */
  918. ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  919. if (ssid_eid)
  920. pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
  921. else
  922. lbs_deb_assoc("no SSID\n");
  923. /* add DS param TLV */
  924. if (bss->channel)
  925. pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
  926. else
  927. lbs_deb_assoc("no channel\n");
  928. /* add (empty) CF param TLV */
  929. pos += lbs_add_cf_param_tlv(pos);
  930. /* add rates TLV */
  931. tmp = pos + 4; /* skip Marvell IE header */
  932. pos += lbs_add_common_rates_tlv(pos, bss);
  933. lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
  934. /* add auth type TLV */
  935. if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
  936. pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
  937. /* add WPA/WPA2 TLV */
  938. if (sme->ie && sme->ie_len)
  939. pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
  940. len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
  941. (u16)(pos - (u8 *) &cmd->iebuf);
  942. cmd->hdr.size = cpu_to_le16(len);
  943. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
  944. le16_to_cpu(cmd->hdr.size));
  945. /* store for later use */
  946. memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
  947. ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
  948. if (ret)
  949. goto done;
  950. /* generate connect message to cfg80211 */
  951. resp = (void *) cmd; /* recast for easier field access */
  952. status = le16_to_cpu(resp->statuscode);
  953. /* Older FW versions map the IEEE 802.11 Status Code in the association
  954. * response to the following values returned in resp->statuscode:
  955. *
  956. * IEEE Status Code Marvell Status Code
  957. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  958. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  959. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  960. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  961. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  962. * others -> 0x0003 ASSOC_RESULT_REFUSED
  963. *
  964. * Other response codes:
  965. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  966. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  967. * association response from the AP)
  968. */
  969. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  970. switch (status) {
  971. case 0:
  972. break;
  973. case 1:
  974. lbs_deb_assoc("invalid association parameters\n");
  975. status = WLAN_STATUS_CAPS_UNSUPPORTED;
  976. break;
  977. case 2:
  978. lbs_deb_assoc("timer expired while waiting for AP\n");
  979. status = WLAN_STATUS_AUTH_TIMEOUT;
  980. break;
  981. case 3:
  982. lbs_deb_assoc("association refused by AP\n");
  983. status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  984. break;
  985. case 4:
  986. lbs_deb_assoc("authentication refused by AP\n");
  987. status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  988. break;
  989. default:
  990. lbs_deb_assoc("association failure %d\n", status);
  991. /* v5 OLPC firmware does return the AP status code if
  992. * it's not one of the values above. Let that through.
  993. */
  994. break;
  995. }
  996. }
  997. lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
  998. "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
  999. le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
  1000. resp_ie_len = le16_to_cpu(resp->hdr.size)
  1001. - sizeof(resp->hdr)
  1002. - 6;
  1003. cfg80211_connect_result(priv->dev,
  1004. priv->assoc_bss,
  1005. sme->ie, sme->ie_len,
  1006. resp->iebuf, resp_ie_len,
  1007. status,
  1008. GFP_KERNEL);
  1009. if (status == 0) {
  1010. /* TODO: get rid of priv->connect_status */
  1011. priv->connect_status = LBS_CONNECTED;
  1012. netif_carrier_on(priv->dev);
  1013. if (!priv->tx_pending_len)
  1014. netif_tx_wake_all_queues(priv->dev);
  1015. }
  1016. done:
  1017. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1018. return ret;
  1019. }
  1020. static struct cfg80211_scan_request *
  1021. _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
  1022. {
  1023. struct cfg80211_scan_request *creq = NULL;
  1024. int i, n_channels = 0;
  1025. enum ieee80211_band band;
  1026. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1027. if (wiphy->bands[band])
  1028. n_channels += wiphy->bands[band]->n_channels;
  1029. }
  1030. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1031. n_channels * sizeof(void *),
  1032. GFP_ATOMIC);
  1033. if (!creq)
  1034. return NULL;
  1035. /* SSIDs come after channels */
  1036. creq->ssids = (void *)&creq->channels[n_channels];
  1037. creq->n_channels = n_channels;
  1038. creq->n_ssids = 1;
  1039. /* Scan all available channels */
  1040. i = 0;
  1041. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1042. int j;
  1043. if (!wiphy->bands[band])
  1044. continue;
  1045. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1046. /* ignore disabled channels */
  1047. if (wiphy->bands[band]->channels[j].flags &
  1048. IEEE80211_CHAN_DISABLED)
  1049. continue;
  1050. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1051. i++;
  1052. }
  1053. }
  1054. if (i) {
  1055. /* Set real number of channels specified in creq->channels[] */
  1056. creq->n_channels = i;
  1057. /* Scan for the SSID we're going to connect to */
  1058. memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
  1059. creq->ssids[0].ssid_len = sme->ssid_len;
  1060. } else {
  1061. /* No channels found... */
  1062. kfree(creq);
  1063. creq = NULL;
  1064. }
  1065. return creq;
  1066. }
  1067. static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
  1068. struct cfg80211_connect_params *sme)
  1069. {
  1070. struct lbs_private *priv = wiphy_priv(wiphy);
  1071. struct cfg80211_bss *bss = NULL;
  1072. int ret = 0;
  1073. u8 preamble = RADIO_PREAMBLE_SHORT;
  1074. lbs_deb_enter(LBS_DEB_CFG80211);
  1075. if (!sme->bssid) {
  1076. /* Run a scan if one isn't in-progress already and if the last
  1077. * scan was done more than 2 seconds ago.
  1078. */
  1079. if (priv->scan_req == NULL &&
  1080. time_after(jiffies, priv->last_scan + (2 * HZ))) {
  1081. struct cfg80211_scan_request *creq;
  1082. creq = _new_connect_scan_req(wiphy, sme);
  1083. if (!creq) {
  1084. ret = -EINVAL;
  1085. goto done;
  1086. }
  1087. lbs_deb_assoc("assoc: scanning for compatible AP\n");
  1088. _internal_start_scan(priv, true, creq);
  1089. }
  1090. /* Wait for any in-progress scan to complete */
  1091. lbs_deb_assoc("assoc: waiting for scan to complete\n");
  1092. wait_event_interruptible_timeout(priv->scan_q,
  1093. (priv->scan_req == NULL),
  1094. (15 * HZ));
  1095. lbs_deb_assoc("assoc: scanning competed\n");
  1096. }
  1097. /* Find the BSS we want using available scan results */
  1098. bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
  1099. sme->ssid, sme->ssid_len,
  1100. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  1101. if (!bss) {
  1102. lbs_pr_err("assoc: bss %pM not in scan results\n",
  1103. sme->bssid);
  1104. ret = -ENOENT;
  1105. goto done;
  1106. }
  1107. lbs_deb_assoc("trying %pM\n", bss->bssid);
  1108. lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
  1109. sme->crypto.cipher_group,
  1110. sme->key_idx, sme->key_len);
  1111. /* As this is a new connection, clear locally stored WEP keys */
  1112. priv->wep_tx_key = 0;
  1113. memset(priv->wep_key, 0, sizeof(priv->wep_key));
  1114. memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
  1115. /* set/remove WEP keys */
  1116. switch (sme->crypto.cipher_group) {
  1117. case WLAN_CIPHER_SUITE_WEP40:
  1118. case WLAN_CIPHER_SUITE_WEP104:
  1119. /* Store provided WEP keys in priv-> */
  1120. priv->wep_tx_key = sme->key_idx;
  1121. priv->wep_key_len[sme->key_idx] = sme->key_len;
  1122. memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
  1123. /* Set WEP keys and WEP mode */
  1124. lbs_set_wep_keys(priv);
  1125. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  1126. lbs_set_mac_control(priv);
  1127. /* No RSN mode for WEP */
  1128. lbs_enable_rsn(priv, 0);
  1129. break;
  1130. case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
  1131. /*
  1132. * If we don't have no WEP, no WPA and no WPA2,
  1133. * we remove all keys like in the WPA/WPA2 setup,
  1134. * we just don't set RSN.
  1135. *
  1136. * Therefore: fall-throught
  1137. */
  1138. case WLAN_CIPHER_SUITE_TKIP:
  1139. case WLAN_CIPHER_SUITE_CCMP:
  1140. /* Remove WEP keys and WEP mode */
  1141. lbs_remove_wep_keys(priv);
  1142. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1143. lbs_set_mac_control(priv);
  1144. /* clear the WPA/WPA2 keys */
  1145. lbs_set_key_material(priv,
  1146. KEY_TYPE_ID_WEP, /* doesn't matter */
  1147. KEY_INFO_WPA_UNICAST,
  1148. NULL, 0);
  1149. lbs_set_key_material(priv,
  1150. KEY_TYPE_ID_WEP, /* doesn't matter */
  1151. KEY_INFO_WPA_MCAST,
  1152. NULL, 0);
  1153. /* RSN mode for WPA/WPA2 */
  1154. lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
  1155. break;
  1156. default:
  1157. lbs_pr_err("unsupported cipher group 0x%x\n",
  1158. sme->crypto.cipher_group);
  1159. ret = -ENOTSUPP;
  1160. goto done;
  1161. }
  1162. lbs_set_authtype(priv, sme);
  1163. lbs_set_radio(priv, preamble, 1);
  1164. /* Do the actual association */
  1165. ret = lbs_associate(priv, bss, sme);
  1166. done:
  1167. if (bss)
  1168. cfg80211_put_bss(bss);
  1169. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1170. return ret;
  1171. }
  1172. static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1173. u16 reason_code)
  1174. {
  1175. struct lbs_private *priv = wiphy_priv(wiphy);
  1176. struct cmd_ds_802_11_deauthenticate cmd;
  1177. lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
  1178. /* store for lbs_cfg_ret_disconnect() */
  1179. priv->disassoc_reason = reason_code;
  1180. memset(&cmd, 0, sizeof(cmd));
  1181. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1182. /* Mildly ugly to use a locally store my own BSSID ... */
  1183. memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
  1184. cmd.reasoncode = cpu_to_le16(reason_code);
  1185. if (lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd))
  1186. return -EFAULT;
  1187. cfg80211_disconnected(priv->dev,
  1188. priv->disassoc_reason,
  1189. NULL, 0,
  1190. GFP_KERNEL);
  1191. priv->connect_status = LBS_DISCONNECTED;
  1192. return 0;
  1193. }
  1194. static int lbs_cfg_set_default_key(struct wiphy *wiphy,
  1195. struct net_device *netdev,
  1196. u8 key_index)
  1197. {
  1198. struct lbs_private *priv = wiphy_priv(wiphy);
  1199. lbs_deb_enter(LBS_DEB_CFG80211);
  1200. if (key_index != priv->wep_tx_key) {
  1201. lbs_deb_assoc("set_default_key: to %d\n", key_index);
  1202. priv->wep_tx_key = key_index;
  1203. lbs_set_wep_keys(priv);
  1204. }
  1205. return 0;
  1206. }
  1207. static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1208. u8 idx, bool pairwise, const u8 *mac_addr,
  1209. struct key_params *params)
  1210. {
  1211. struct lbs_private *priv = wiphy_priv(wiphy);
  1212. u16 key_info;
  1213. u16 key_type;
  1214. int ret = 0;
  1215. lbs_deb_enter(LBS_DEB_CFG80211);
  1216. lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
  1217. params->cipher, mac_addr);
  1218. lbs_deb_assoc("add_key: key index %d, key len %d\n",
  1219. idx, params->key_len);
  1220. if (params->key_len)
  1221. lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
  1222. params->key, params->key_len);
  1223. lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
  1224. if (params->seq_len)
  1225. lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
  1226. params->seq, params->seq_len);
  1227. switch (params->cipher) {
  1228. case WLAN_CIPHER_SUITE_WEP40:
  1229. case WLAN_CIPHER_SUITE_WEP104:
  1230. /* actually compare if something has changed ... */
  1231. if ((priv->wep_key_len[idx] != params->key_len) ||
  1232. memcmp(priv->wep_key[idx],
  1233. params->key, params->key_len) != 0) {
  1234. priv->wep_key_len[idx] = params->key_len;
  1235. memcpy(priv->wep_key[idx],
  1236. params->key, params->key_len);
  1237. lbs_set_wep_keys(priv);
  1238. }
  1239. break;
  1240. case WLAN_CIPHER_SUITE_TKIP:
  1241. case WLAN_CIPHER_SUITE_CCMP:
  1242. key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
  1243. ? KEY_INFO_WPA_UNICAST
  1244. : KEY_INFO_WPA_MCAST);
  1245. key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
  1246. ? KEY_TYPE_ID_TKIP
  1247. : KEY_TYPE_ID_AES;
  1248. lbs_set_key_material(priv,
  1249. key_type,
  1250. key_info,
  1251. params->key, params->key_len);
  1252. break;
  1253. default:
  1254. lbs_pr_err("unhandled cipher 0x%x\n", params->cipher);
  1255. ret = -ENOTSUPP;
  1256. break;
  1257. }
  1258. return ret;
  1259. }
  1260. static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1261. u8 key_index, bool pairwise, const u8 *mac_addr)
  1262. {
  1263. lbs_deb_enter(LBS_DEB_CFG80211);
  1264. lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
  1265. key_index, mac_addr);
  1266. #ifdef TODO
  1267. struct lbs_private *priv = wiphy_priv(wiphy);
  1268. /*
  1269. * I think can keep this a NO-OP, because:
  1270. * - we clear all keys whenever we do lbs_cfg_connect() anyway
  1271. * - neither "iw" nor "wpa_supplicant" won't call this during
  1272. * an ongoing connection
  1273. * - TODO: but I have to check if this is still true when
  1274. * I set the AP to periodic re-keying
  1275. * - we've not kzallec() something when we've added a key at
  1276. * lbs_cfg_connect() or lbs_cfg_add_key().
  1277. *
  1278. * This causes lbs_cfg_del_key() only called at disconnect time,
  1279. * where we'd just waste time deleting a key that is not going
  1280. * to be used anyway.
  1281. */
  1282. if (key_index < 3 && priv->wep_key_len[key_index]) {
  1283. priv->wep_key_len[key_index] = 0;
  1284. lbs_set_wep_keys(priv);
  1285. }
  1286. #endif
  1287. return 0;
  1288. }
  1289. /***************************************************************************
  1290. * Get station
  1291. */
  1292. static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
  1293. u8 *mac, struct station_info *sinfo)
  1294. {
  1295. struct lbs_private *priv = wiphy_priv(wiphy);
  1296. s8 signal, noise;
  1297. int ret;
  1298. size_t i;
  1299. lbs_deb_enter(LBS_DEB_CFG80211);
  1300. sinfo->filled |= STATION_INFO_TX_BYTES |
  1301. STATION_INFO_TX_PACKETS |
  1302. STATION_INFO_RX_BYTES |
  1303. STATION_INFO_RX_PACKETS;
  1304. sinfo->tx_bytes = priv->dev->stats.tx_bytes;
  1305. sinfo->tx_packets = priv->dev->stats.tx_packets;
  1306. sinfo->rx_bytes = priv->dev->stats.rx_bytes;
  1307. sinfo->rx_packets = priv->dev->stats.rx_packets;
  1308. /* Get current RSSI */
  1309. ret = lbs_get_rssi(priv, &signal, &noise);
  1310. if (ret == 0) {
  1311. sinfo->signal = signal;
  1312. sinfo->filled |= STATION_INFO_SIGNAL;
  1313. }
  1314. /* Convert priv->cur_rate from hw_value to NL80211 value */
  1315. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  1316. if (priv->cur_rate == lbs_rates[i].hw_value) {
  1317. sinfo->txrate.legacy = lbs_rates[i].bitrate;
  1318. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1319. break;
  1320. }
  1321. }
  1322. return 0;
  1323. }
  1324. /***************************************************************************
  1325. * "Site survey", here just current channel and noise level
  1326. */
  1327. static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
  1328. int idx, struct survey_info *survey)
  1329. {
  1330. struct lbs_private *priv = wiphy_priv(wiphy);
  1331. s8 signal, noise;
  1332. int ret;
  1333. if (idx != 0)
  1334. ret = -ENOENT;
  1335. lbs_deb_enter(LBS_DEB_CFG80211);
  1336. survey->channel = ieee80211_get_channel(wiphy,
  1337. ieee80211_channel_to_frequency(priv->channel));
  1338. ret = lbs_get_rssi(priv, &signal, &noise);
  1339. if (ret == 0) {
  1340. survey->filled = SURVEY_INFO_NOISE_DBM;
  1341. survey->noise = noise;
  1342. }
  1343. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1344. return ret;
  1345. }
  1346. /***************************************************************************
  1347. * Change interface
  1348. */
  1349. static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
  1350. enum nl80211_iftype type, u32 *flags,
  1351. struct vif_params *params)
  1352. {
  1353. struct lbs_private *priv = wiphy_priv(wiphy);
  1354. int ret = 0;
  1355. lbs_deb_enter(LBS_DEB_CFG80211);
  1356. switch (type) {
  1357. case NL80211_IFTYPE_MONITOR:
  1358. ret = lbs_set_monitor_mode(priv, 1);
  1359. break;
  1360. case NL80211_IFTYPE_STATION:
  1361. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  1362. ret = lbs_set_monitor_mode(priv, 0);
  1363. if (!ret)
  1364. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
  1365. break;
  1366. case NL80211_IFTYPE_ADHOC:
  1367. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  1368. ret = lbs_set_monitor_mode(priv, 0);
  1369. if (!ret)
  1370. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
  1371. break;
  1372. default:
  1373. ret = -ENOTSUPP;
  1374. }
  1375. if (!ret)
  1376. priv->wdev->iftype = type;
  1377. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1378. return ret;
  1379. }
  1380. /***************************************************************************
  1381. * IBSS (Ad-Hoc)
  1382. */
  1383. /* The firmware needs the following bits masked out of the beacon-derived
  1384. * capability field when associating/joining to a BSS:
  1385. * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
  1386. */
  1387. #define CAPINFO_MASK (~(0xda00))
  1388. static void lbs_join_post(struct lbs_private *priv,
  1389. struct cfg80211_ibss_params *params,
  1390. u8 *bssid, u16 capability)
  1391. {
  1392. u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
  1393. 2 + 4 + /* basic rates */
  1394. 2 + 1 + /* DS parameter */
  1395. 2 + 2 + /* atim */
  1396. 2 + 8]; /* extended rates */
  1397. u8 *fake = fake_ie;
  1398. lbs_deb_enter(LBS_DEB_CFG80211);
  1399. /*
  1400. * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
  1401. * the real IE from the firmware. So we fabricate a fake IE based on
  1402. * what the firmware actually sends (sniffed with wireshark).
  1403. */
  1404. /* Fake SSID IE */
  1405. *fake++ = WLAN_EID_SSID;
  1406. *fake++ = params->ssid_len;
  1407. memcpy(fake, params->ssid, params->ssid_len);
  1408. fake += params->ssid_len;
  1409. /* Fake supported basic rates IE */
  1410. *fake++ = WLAN_EID_SUPP_RATES;
  1411. *fake++ = 4;
  1412. *fake++ = 0x82;
  1413. *fake++ = 0x84;
  1414. *fake++ = 0x8b;
  1415. *fake++ = 0x96;
  1416. /* Fake DS channel IE */
  1417. *fake++ = WLAN_EID_DS_PARAMS;
  1418. *fake++ = 1;
  1419. *fake++ = params->channel->hw_value;
  1420. /* Fake IBSS params IE */
  1421. *fake++ = WLAN_EID_IBSS_PARAMS;
  1422. *fake++ = 2;
  1423. *fake++ = 0; /* ATIM=0 */
  1424. *fake++ = 0;
  1425. /* Fake extended rates IE, TODO: don't add this for 802.11b only,
  1426. * but I don't know how this could be checked */
  1427. *fake++ = WLAN_EID_EXT_SUPP_RATES;
  1428. *fake++ = 8;
  1429. *fake++ = 0x0c;
  1430. *fake++ = 0x12;
  1431. *fake++ = 0x18;
  1432. *fake++ = 0x24;
  1433. *fake++ = 0x30;
  1434. *fake++ = 0x48;
  1435. *fake++ = 0x60;
  1436. *fake++ = 0x6c;
  1437. lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
  1438. cfg80211_inform_bss(priv->wdev->wiphy,
  1439. params->channel,
  1440. bssid,
  1441. 0,
  1442. capability,
  1443. params->beacon_interval,
  1444. fake_ie, fake - fake_ie,
  1445. 0, GFP_KERNEL);
  1446. memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
  1447. priv->wdev->ssid_len = params->ssid_len;
  1448. cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
  1449. /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
  1450. priv->connect_status = LBS_CONNECTED;
  1451. netif_carrier_on(priv->dev);
  1452. if (!priv->tx_pending_len)
  1453. netif_wake_queue(priv->dev);
  1454. lbs_deb_leave(LBS_DEB_CFG80211);
  1455. }
  1456. static int lbs_ibss_join_existing(struct lbs_private *priv,
  1457. struct cfg80211_ibss_params *params,
  1458. struct cfg80211_bss *bss)
  1459. {
  1460. const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1461. struct cmd_ds_802_11_ad_hoc_join cmd;
  1462. u8 preamble = RADIO_PREAMBLE_SHORT;
  1463. int ret = 0;
  1464. lbs_deb_enter(LBS_DEB_CFG80211);
  1465. /* TODO: set preamble based on scan result */
  1466. ret = lbs_set_radio(priv, preamble, 1);
  1467. if (ret)
  1468. goto out;
  1469. /*
  1470. * Example CMD_802_11_AD_HOC_JOIN command:
  1471. *
  1472. * command 2c 00 CMD_802_11_AD_HOC_JOIN
  1473. * size 65 00
  1474. * sequence xx xx
  1475. * result 00 00
  1476. * bssid 02 27 27 97 2f 96
  1477. * ssid 49 42 53 53 00 00 00 00
  1478. * 00 00 00 00 00 00 00 00
  1479. * 00 00 00 00 00 00 00 00
  1480. * 00 00 00 00 00 00 00 00
  1481. * type 02 CMD_BSS_TYPE_IBSS
  1482. * beacon period 64 00
  1483. * dtim period 00
  1484. * timestamp 00 00 00 00 00 00 00 00
  1485. * localtime 00 00 00 00 00 00 00 00
  1486. * IE DS 03
  1487. * IE DS len 01
  1488. * IE DS channel 01
  1489. * reserveed 00 00 00 00
  1490. * IE IBSS 06
  1491. * IE IBSS len 02
  1492. * IE IBSS atim 00 00
  1493. * reserved 00 00 00 00
  1494. * capability 02 00
  1495. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
  1496. * fail timeout ff 00
  1497. * probe delay 00 00
  1498. */
  1499. memset(&cmd, 0, sizeof(cmd));
  1500. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1501. memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
  1502. memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
  1503. cmd.bss.type = CMD_BSS_TYPE_IBSS;
  1504. cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
  1505. cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
  1506. cmd.bss.ds.header.len = 1;
  1507. cmd.bss.ds.channel = params->channel->hw_value;
  1508. cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1509. cmd.bss.ibss.header.len = 2;
  1510. cmd.bss.ibss.atimwindow = 0;
  1511. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1512. /* set rates to the intersection of our rates and the rates in the
  1513. bss */
  1514. if (!rates_eid) {
  1515. lbs_add_rates(cmd.bss.rates);
  1516. } else {
  1517. int hw, i;
  1518. u8 rates_max = rates_eid[1];
  1519. u8 *rates = cmd.bss.rates;
  1520. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  1521. u8 hw_rate = lbs_rates[hw].bitrate / 5;
  1522. for (i = 0; i < rates_max; i++) {
  1523. if (hw_rate == (rates_eid[i+2] & 0x7f)) {
  1524. u8 rate = rates_eid[i+2];
  1525. if (rate == 0x02 || rate == 0x04 ||
  1526. rate == 0x0b || rate == 0x16)
  1527. rate |= 0x80;
  1528. *rates++ = rate;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. /* Only v8 and below support setting this */
  1534. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  1535. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1536. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1537. }
  1538. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  1539. if (ret)
  1540. goto out;
  1541. /*
  1542. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1543. *
  1544. * response 2c 80
  1545. * size 09 00
  1546. * sequence xx xx
  1547. * result 00 00
  1548. * reserved 00
  1549. */
  1550. lbs_join_post(priv, params, bss->bssid, bss->capability);
  1551. out:
  1552. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1553. return ret;
  1554. }
  1555. static int lbs_ibss_start_new(struct lbs_private *priv,
  1556. struct cfg80211_ibss_params *params)
  1557. {
  1558. struct cmd_ds_802_11_ad_hoc_start cmd;
  1559. struct cmd_ds_802_11_ad_hoc_result *resp =
  1560. (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
  1561. u8 preamble = RADIO_PREAMBLE_SHORT;
  1562. int ret = 0;
  1563. u16 capability;
  1564. lbs_deb_enter(LBS_DEB_CFG80211);
  1565. ret = lbs_set_radio(priv, preamble, 1);
  1566. if (ret)
  1567. goto out;
  1568. /*
  1569. * Example CMD_802_11_AD_HOC_START command:
  1570. *
  1571. * command 2b 00 CMD_802_11_AD_HOC_START
  1572. * size b1 00
  1573. * sequence xx xx
  1574. * result 00 00
  1575. * ssid 54 45 53 54 00 00 00 00
  1576. * 00 00 00 00 00 00 00 00
  1577. * 00 00 00 00 00 00 00 00
  1578. * 00 00 00 00 00 00 00 00
  1579. * bss type 02
  1580. * beacon period 64 00
  1581. * dtim period 00
  1582. * IE IBSS 06
  1583. * IE IBSS len 02
  1584. * IE IBSS atim 00 00
  1585. * reserved 00 00 00 00
  1586. * IE DS 03
  1587. * IE DS len 01
  1588. * IE DS channel 01
  1589. * reserved 00 00 00 00
  1590. * probe delay 00 00
  1591. * capability 02 00
  1592. * rates 82 84 8b 96 (basic rates with have bit 7 set)
  1593. * 0c 12 18 24 30 48 60 6c
  1594. * padding 100 bytes
  1595. */
  1596. memset(&cmd, 0, sizeof(cmd));
  1597. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1598. memcpy(cmd.ssid, params->ssid, params->ssid_len);
  1599. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  1600. cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
  1601. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1602. cmd.ibss.header.len = 2;
  1603. cmd.ibss.atimwindow = 0;
  1604. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  1605. cmd.ds.header.len = 1;
  1606. cmd.ds.channel = params->channel->hw_value;
  1607. /* Only v8 and below support setting probe delay */
  1608. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
  1609. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1610. /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
  1611. capability = WLAN_CAPABILITY_IBSS;
  1612. cmd.capability = cpu_to_le16(capability);
  1613. lbs_add_rates(cmd.rates);
  1614. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  1615. if (ret)
  1616. goto out;
  1617. /*
  1618. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1619. *
  1620. * response 2b 80
  1621. * size 14 00
  1622. * sequence xx xx
  1623. * result 00 00
  1624. * reserved 00
  1625. * bssid 02 2b 7b 0f 86 0e
  1626. */
  1627. lbs_join_post(priv, params, resp->bssid, capability);
  1628. out:
  1629. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1630. return ret;
  1631. }
  1632. static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1633. struct cfg80211_ibss_params *params)
  1634. {
  1635. struct lbs_private *priv = wiphy_priv(wiphy);
  1636. int ret = 0;
  1637. struct cfg80211_bss *bss;
  1638. DECLARE_SSID_BUF(ssid_buf);
  1639. lbs_deb_enter(LBS_DEB_CFG80211);
  1640. if (!params->channel) {
  1641. ret = -ENOTSUPP;
  1642. goto out;
  1643. }
  1644. ret = lbs_set_channel(priv, params->channel->hw_value);
  1645. if (ret)
  1646. goto out;
  1647. /* Search if someone is beaconing. This assumes that the
  1648. * bss list is populated already */
  1649. bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
  1650. params->ssid, params->ssid_len,
  1651. WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
  1652. if (bss) {
  1653. ret = lbs_ibss_join_existing(priv, params, bss);
  1654. cfg80211_put_bss(bss);
  1655. } else
  1656. ret = lbs_ibss_start_new(priv, params);
  1657. out:
  1658. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1659. return ret;
  1660. }
  1661. static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1662. {
  1663. struct lbs_private *priv = wiphy_priv(wiphy);
  1664. struct cmd_ds_802_11_ad_hoc_stop cmd;
  1665. int ret = 0;
  1666. lbs_deb_enter(LBS_DEB_CFG80211);
  1667. memset(&cmd, 0, sizeof(cmd));
  1668. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1669. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  1670. /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
  1671. lbs_mac_event_disconnected(priv);
  1672. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1673. return ret;
  1674. }
  1675. /***************************************************************************
  1676. * Initialization
  1677. */
  1678. static struct cfg80211_ops lbs_cfg80211_ops = {
  1679. .set_channel = lbs_cfg_set_channel,
  1680. .scan = lbs_cfg_scan,
  1681. .connect = lbs_cfg_connect,
  1682. .disconnect = lbs_cfg_disconnect,
  1683. .add_key = lbs_cfg_add_key,
  1684. .del_key = lbs_cfg_del_key,
  1685. .set_default_key = lbs_cfg_set_default_key,
  1686. .get_station = lbs_cfg_get_station,
  1687. .dump_survey = lbs_get_survey,
  1688. .change_virtual_intf = lbs_change_intf,
  1689. .join_ibss = lbs_join_ibss,
  1690. .leave_ibss = lbs_leave_ibss,
  1691. };
  1692. /*
  1693. * At this time lbs_private *priv doesn't even exist, so we just allocate
  1694. * memory and don't initialize the wiphy further. This is postponed until we
  1695. * can talk to the firmware and happens at registration time in
  1696. * lbs_cfg_wiphy_register().
  1697. */
  1698. struct wireless_dev *lbs_cfg_alloc(struct device *dev)
  1699. {
  1700. int ret = 0;
  1701. struct wireless_dev *wdev;
  1702. lbs_deb_enter(LBS_DEB_CFG80211);
  1703. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1704. if (!wdev) {
  1705. dev_err(dev, "cannot allocate wireless device\n");
  1706. return ERR_PTR(-ENOMEM);
  1707. }
  1708. wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
  1709. if (!wdev->wiphy) {
  1710. dev_err(dev, "cannot allocate wiphy\n");
  1711. ret = -ENOMEM;
  1712. goto err_wiphy_new;
  1713. }
  1714. lbs_deb_leave(LBS_DEB_CFG80211);
  1715. return wdev;
  1716. err_wiphy_new:
  1717. kfree(wdev);
  1718. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1719. return ERR_PTR(ret);
  1720. }
  1721. static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
  1722. {
  1723. struct region_code_mapping {
  1724. const char *cn;
  1725. int code;
  1726. };
  1727. /* Section 5.17.2 */
  1728. static struct region_code_mapping regmap[] = {
  1729. {"US ", 0x10}, /* US FCC */
  1730. {"CA ", 0x20}, /* Canada */
  1731. {"EU ", 0x30}, /* ETSI */
  1732. {"ES ", 0x31}, /* Spain */
  1733. {"FR ", 0x32}, /* France */
  1734. {"JP ", 0x40}, /* Japan */
  1735. };
  1736. size_t i;
  1737. lbs_deb_enter(LBS_DEB_CFG80211);
  1738. for (i = 0; i < ARRAY_SIZE(regmap); i++)
  1739. if (regmap[i].code == priv->regioncode) {
  1740. regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
  1741. break;
  1742. }
  1743. lbs_deb_leave(LBS_DEB_CFG80211);
  1744. }
  1745. /*
  1746. * This function get's called after lbs_setup_firmware() determined the
  1747. * firmware capabities. So we can setup the wiphy according to our
  1748. * hardware/firmware.
  1749. */
  1750. int lbs_cfg_register(struct lbs_private *priv)
  1751. {
  1752. struct wireless_dev *wdev = priv->wdev;
  1753. int ret;
  1754. lbs_deb_enter(LBS_DEB_CFG80211);
  1755. wdev->wiphy->max_scan_ssids = 1;
  1756. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1757. wdev->wiphy->interface_modes =
  1758. BIT(NL80211_IFTYPE_STATION) |
  1759. BIT(NL80211_IFTYPE_ADHOC);
  1760. if (lbs_rtap_supported(priv))
  1761. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1762. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
  1763. /*
  1764. * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
  1765. * never seen a firmware without WPA
  1766. */
  1767. wdev->wiphy->cipher_suites = cipher_suites;
  1768. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1769. wdev->wiphy->reg_notifier = lbs_reg_notifier;
  1770. ret = wiphy_register(wdev->wiphy);
  1771. if (ret < 0)
  1772. lbs_pr_err("cannot register wiphy device\n");
  1773. priv->wiphy_registered = true;
  1774. ret = register_netdev(priv->dev);
  1775. if (ret)
  1776. lbs_pr_err("cannot register network device\n");
  1777. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1778. lbs_cfg_set_regulatory_hint(priv);
  1779. lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
  1780. return ret;
  1781. }
  1782. int lbs_reg_notifier(struct wiphy *wiphy,
  1783. struct regulatory_request *request)
  1784. {
  1785. struct lbs_private *priv = wiphy_priv(wiphy);
  1786. int ret;
  1787. lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
  1788. "callback for domain %c%c\n", request->alpha2[0],
  1789. request->alpha2[1]);
  1790. ret = lbs_set_11d_domain_info(priv, request, wiphy->bands);
  1791. lbs_deb_leave(LBS_DEB_CFG80211);
  1792. return ret;
  1793. }
  1794. void lbs_scan_deinit(struct lbs_private *priv)
  1795. {
  1796. lbs_deb_enter(LBS_DEB_CFG80211);
  1797. cancel_delayed_work_sync(&priv->scan_work);
  1798. }
  1799. void lbs_cfg_free(struct lbs_private *priv)
  1800. {
  1801. struct wireless_dev *wdev = priv->wdev;
  1802. lbs_deb_enter(LBS_DEB_CFG80211);
  1803. if (!wdev)
  1804. return;
  1805. if (priv->wiphy_registered)
  1806. wiphy_unregister(wdev->wiphy);
  1807. if (wdev->wiphy)
  1808. wiphy_free(wdev->wiphy);
  1809. kfree(wdev);
  1810. }