cfg.c 53 KB

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