cfg.c 49 KB

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