cfg.c 55 KB

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