cfg.c 54 KB

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