cfg.c 54 KB

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