cfg.c 54 KB

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