cfg.c 53 KB

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