cfg.c 52 KB

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