cfg.c 52 KB

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