cfg.c 54 KB

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