cfg.c 52 KB

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