cfg.c 52 KB

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