cfg.c 54 KB

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