util.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*
  2. * Wireless utility functions
  3. *
  4. * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. */
  7. #include <linux/export.h>
  8. #include <linux/bitops.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/slab.h>
  11. #include <net/cfg80211.h>
  12. #include <net/ip.h>
  13. #include <net/dsfield.h>
  14. #include <linux/if_vlan.h>
  15. #include <linux/mpls.h>
  16. #include "core.h"
  17. #include "rdev-ops.h"
  18. struct ieee80211_rate *
  19. ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  20. u32 basic_rates, int bitrate)
  21. {
  22. struct ieee80211_rate *result = &sband->bitrates[0];
  23. int i;
  24. for (i = 0; i < sband->n_bitrates; i++) {
  25. if (!(basic_rates & BIT(i)))
  26. continue;
  27. if (sband->bitrates[i].bitrate > bitrate)
  28. continue;
  29. result = &sband->bitrates[i];
  30. }
  31. return result;
  32. }
  33. EXPORT_SYMBOL(ieee80211_get_response_rate);
  34. u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
  35. enum nl80211_bss_scan_width scan_width)
  36. {
  37. struct ieee80211_rate *bitrates;
  38. u32 mandatory_rates = 0;
  39. enum ieee80211_rate_flags mandatory_flag;
  40. int i;
  41. if (WARN_ON(!sband))
  42. return 1;
  43. if (sband->band == IEEE80211_BAND_2GHZ) {
  44. if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
  45. scan_width == NL80211_BSS_CHAN_WIDTH_10)
  46. mandatory_flag = IEEE80211_RATE_MANDATORY_G;
  47. else
  48. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  49. } else {
  50. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  51. }
  52. bitrates = sband->bitrates;
  53. for (i = 0; i < sband->n_bitrates; i++)
  54. if (bitrates[i].flags & mandatory_flag)
  55. mandatory_rates |= BIT(i);
  56. return mandatory_rates;
  57. }
  58. EXPORT_SYMBOL(ieee80211_mandatory_rates);
  59. int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
  60. {
  61. /* see 802.11 17.3.8.3.2 and Annex J
  62. * there are overlapping channel numbers in 5GHz and 2GHz bands */
  63. if (chan <= 0)
  64. return 0; /* not supported */
  65. switch (band) {
  66. case IEEE80211_BAND_2GHZ:
  67. if (chan == 14)
  68. return 2484;
  69. else if (chan < 14)
  70. return 2407 + chan * 5;
  71. break;
  72. case IEEE80211_BAND_5GHZ:
  73. if (chan >= 182 && chan <= 196)
  74. return 4000 + chan * 5;
  75. else
  76. return 5000 + chan * 5;
  77. break;
  78. case IEEE80211_BAND_60GHZ:
  79. if (chan < 5)
  80. return 56160 + chan * 2160;
  81. break;
  82. default:
  83. ;
  84. }
  85. return 0; /* not supported */
  86. }
  87. EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  88. int ieee80211_frequency_to_channel(int freq)
  89. {
  90. /* see 802.11 17.3.8.3.2 and Annex J */
  91. if (freq == 2484)
  92. return 14;
  93. else if (freq < 2484)
  94. return (freq - 2407) / 5;
  95. else if (freq >= 4910 && freq <= 4980)
  96. return (freq - 4000) / 5;
  97. else if (freq <= 45000) /* DMG band lower limit */
  98. return (freq - 5000) / 5;
  99. else if (freq >= 58320 && freq <= 64800)
  100. return (freq - 56160) / 2160;
  101. else
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  105. struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  106. int freq)
  107. {
  108. enum ieee80211_band band;
  109. struct ieee80211_supported_band *sband;
  110. int i;
  111. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  112. sband = wiphy->bands[band];
  113. if (!sband)
  114. continue;
  115. for (i = 0; i < sband->n_channels; i++) {
  116. if (sband->channels[i].center_freq == freq)
  117. return &sband->channels[i];
  118. }
  119. }
  120. return NULL;
  121. }
  122. EXPORT_SYMBOL(__ieee80211_get_channel);
  123. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
  124. enum ieee80211_band band)
  125. {
  126. int i, want;
  127. switch (band) {
  128. case IEEE80211_BAND_5GHZ:
  129. want = 3;
  130. for (i = 0; i < sband->n_bitrates; i++) {
  131. if (sband->bitrates[i].bitrate == 60 ||
  132. sband->bitrates[i].bitrate == 120 ||
  133. sband->bitrates[i].bitrate == 240) {
  134. sband->bitrates[i].flags |=
  135. IEEE80211_RATE_MANDATORY_A;
  136. want--;
  137. }
  138. }
  139. WARN_ON(want);
  140. break;
  141. case IEEE80211_BAND_2GHZ:
  142. want = 7;
  143. for (i = 0; i < sband->n_bitrates; i++) {
  144. if (sband->bitrates[i].bitrate == 10) {
  145. sband->bitrates[i].flags |=
  146. IEEE80211_RATE_MANDATORY_B |
  147. IEEE80211_RATE_MANDATORY_G;
  148. want--;
  149. }
  150. if (sband->bitrates[i].bitrate == 20 ||
  151. sband->bitrates[i].bitrate == 55 ||
  152. sband->bitrates[i].bitrate == 110 ||
  153. sband->bitrates[i].bitrate == 60 ||
  154. sband->bitrates[i].bitrate == 120 ||
  155. sband->bitrates[i].bitrate == 240) {
  156. sband->bitrates[i].flags |=
  157. IEEE80211_RATE_MANDATORY_G;
  158. want--;
  159. }
  160. if (sband->bitrates[i].bitrate != 10 &&
  161. sband->bitrates[i].bitrate != 20 &&
  162. sband->bitrates[i].bitrate != 55 &&
  163. sband->bitrates[i].bitrate != 110)
  164. sband->bitrates[i].flags |=
  165. IEEE80211_RATE_ERP_G;
  166. }
  167. WARN_ON(want != 0 && want != 3 && want != 6);
  168. break;
  169. case IEEE80211_BAND_60GHZ:
  170. /* check for mandatory HT MCS 1..4 */
  171. WARN_ON(!sband->ht_cap.ht_supported);
  172. WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
  173. break;
  174. case IEEE80211_NUM_BANDS:
  175. WARN_ON(1);
  176. break;
  177. }
  178. }
  179. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  180. {
  181. enum ieee80211_band band;
  182. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  183. if (wiphy->bands[band])
  184. set_mandatory_flags_band(wiphy->bands[band], band);
  185. }
  186. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
  187. {
  188. int i;
  189. for (i = 0; i < wiphy->n_cipher_suites; i++)
  190. if (cipher == wiphy->cipher_suites[i])
  191. return true;
  192. return false;
  193. }
  194. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  195. struct key_params *params, int key_idx,
  196. bool pairwise, const u8 *mac_addr)
  197. {
  198. if (key_idx > 5)
  199. return -EINVAL;
  200. if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  201. return -EINVAL;
  202. if (pairwise && !mac_addr)
  203. return -EINVAL;
  204. switch (params->cipher) {
  205. case WLAN_CIPHER_SUITE_TKIP:
  206. case WLAN_CIPHER_SUITE_CCMP:
  207. case WLAN_CIPHER_SUITE_CCMP_256:
  208. case WLAN_CIPHER_SUITE_GCMP:
  209. case WLAN_CIPHER_SUITE_GCMP_256:
  210. /* Disallow pairwise keys with non-zero index unless it's WEP
  211. * or a vendor specific cipher (because current deployments use
  212. * pairwise WEP keys with non-zero indices and for vendor
  213. * specific ciphers this should be validated in the driver or
  214. * hardware level - but 802.11i clearly specifies to use zero)
  215. */
  216. if (pairwise && key_idx)
  217. return -EINVAL;
  218. break;
  219. case WLAN_CIPHER_SUITE_AES_CMAC:
  220. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  221. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  222. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  223. /* Disallow BIP (group-only) cipher as pairwise cipher */
  224. if (pairwise)
  225. return -EINVAL;
  226. break;
  227. default:
  228. break;
  229. }
  230. switch (params->cipher) {
  231. case WLAN_CIPHER_SUITE_WEP40:
  232. if (params->key_len != WLAN_KEY_LEN_WEP40)
  233. return -EINVAL;
  234. break;
  235. case WLAN_CIPHER_SUITE_TKIP:
  236. if (params->key_len != WLAN_KEY_LEN_TKIP)
  237. return -EINVAL;
  238. break;
  239. case WLAN_CIPHER_SUITE_CCMP:
  240. if (params->key_len != WLAN_KEY_LEN_CCMP)
  241. return -EINVAL;
  242. break;
  243. case WLAN_CIPHER_SUITE_CCMP_256:
  244. if (params->key_len != WLAN_KEY_LEN_CCMP_256)
  245. return -EINVAL;
  246. break;
  247. case WLAN_CIPHER_SUITE_GCMP:
  248. if (params->key_len != WLAN_KEY_LEN_GCMP)
  249. return -EINVAL;
  250. break;
  251. case WLAN_CIPHER_SUITE_GCMP_256:
  252. if (params->key_len != WLAN_KEY_LEN_GCMP_256)
  253. return -EINVAL;
  254. break;
  255. case WLAN_CIPHER_SUITE_WEP104:
  256. if (params->key_len != WLAN_KEY_LEN_WEP104)
  257. return -EINVAL;
  258. break;
  259. case WLAN_CIPHER_SUITE_AES_CMAC:
  260. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  261. return -EINVAL;
  262. break;
  263. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  264. if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
  265. return -EINVAL;
  266. break;
  267. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  268. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
  269. return -EINVAL;
  270. break;
  271. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  272. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
  273. return -EINVAL;
  274. break;
  275. default:
  276. /*
  277. * We don't know anything about this algorithm,
  278. * allow using it -- but the driver must check
  279. * all parameters! We still check below whether
  280. * or not the driver supports this algorithm,
  281. * of course.
  282. */
  283. break;
  284. }
  285. if (params->seq) {
  286. switch (params->cipher) {
  287. case WLAN_CIPHER_SUITE_WEP40:
  288. case WLAN_CIPHER_SUITE_WEP104:
  289. /* These ciphers do not use key sequence */
  290. return -EINVAL;
  291. case WLAN_CIPHER_SUITE_TKIP:
  292. case WLAN_CIPHER_SUITE_CCMP:
  293. case WLAN_CIPHER_SUITE_CCMP_256:
  294. case WLAN_CIPHER_SUITE_GCMP:
  295. case WLAN_CIPHER_SUITE_GCMP_256:
  296. case WLAN_CIPHER_SUITE_AES_CMAC:
  297. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  298. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  299. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  300. if (params->seq_len != 6)
  301. return -EINVAL;
  302. break;
  303. }
  304. }
  305. if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
  306. return -EINVAL;
  307. return 0;
  308. }
  309. unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
  310. {
  311. unsigned int hdrlen = 24;
  312. if (ieee80211_is_data(fc)) {
  313. if (ieee80211_has_a4(fc))
  314. hdrlen = 30;
  315. if (ieee80211_is_data_qos(fc)) {
  316. hdrlen += IEEE80211_QOS_CTL_LEN;
  317. if (ieee80211_has_order(fc))
  318. hdrlen += IEEE80211_HT_CTL_LEN;
  319. }
  320. goto out;
  321. }
  322. if (ieee80211_is_mgmt(fc)) {
  323. if (ieee80211_has_order(fc))
  324. hdrlen += IEEE80211_HT_CTL_LEN;
  325. goto out;
  326. }
  327. if (ieee80211_is_ctl(fc)) {
  328. /*
  329. * ACK and CTS are 10 bytes, all others 16. To see how
  330. * to get this condition consider
  331. * subtype mask: 0b0000000011110000 (0x00F0)
  332. * ACK subtype: 0b0000000011010000 (0x00D0)
  333. * CTS subtype: 0b0000000011000000 (0x00C0)
  334. * bits that matter: ^^^ (0x00E0)
  335. * value of those: 0b0000000011000000 (0x00C0)
  336. */
  337. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  338. hdrlen = 10;
  339. else
  340. hdrlen = 16;
  341. }
  342. out:
  343. return hdrlen;
  344. }
  345. EXPORT_SYMBOL(ieee80211_hdrlen);
  346. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  347. {
  348. const struct ieee80211_hdr *hdr =
  349. (const struct ieee80211_hdr *)skb->data;
  350. unsigned int hdrlen;
  351. if (unlikely(skb->len < 10))
  352. return 0;
  353. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  354. if (unlikely(hdrlen > skb->len))
  355. return 0;
  356. return hdrlen;
  357. }
  358. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  359. unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  360. {
  361. int ae = meshhdr->flags & MESH_FLAGS_AE;
  362. /* 802.11-2012, 8.2.4.7.3 */
  363. switch (ae) {
  364. default:
  365. case 0:
  366. return 6;
  367. case MESH_FLAGS_AE_A4:
  368. return 12;
  369. case MESH_FLAGS_AE_A5_A6:
  370. return 18;
  371. }
  372. }
  373. EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
  374. int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
  375. enum nl80211_iftype iftype)
  376. {
  377. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  378. u16 hdrlen, ethertype;
  379. u8 *payload;
  380. u8 dst[ETH_ALEN];
  381. u8 src[ETH_ALEN] __aligned(2);
  382. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  383. return -1;
  384. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  385. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  386. * header
  387. * IEEE 802.11 address fields:
  388. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  389. * 0 0 DA SA BSSID n/a
  390. * 0 1 DA BSSID SA n/a
  391. * 1 0 BSSID SA DA n/a
  392. * 1 1 RA TA DA SA
  393. */
  394. memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
  395. memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
  396. switch (hdr->frame_control &
  397. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  398. case cpu_to_le16(IEEE80211_FCTL_TODS):
  399. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  400. iftype != NL80211_IFTYPE_AP_VLAN &&
  401. iftype != NL80211_IFTYPE_P2P_GO))
  402. return -1;
  403. break;
  404. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  405. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  406. iftype != NL80211_IFTYPE_MESH_POINT &&
  407. iftype != NL80211_IFTYPE_AP_VLAN &&
  408. iftype != NL80211_IFTYPE_STATION))
  409. return -1;
  410. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  411. struct ieee80211s_hdr *meshdr =
  412. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  413. /* make sure meshdr->flags is on the linear part */
  414. if (!pskb_may_pull(skb, hdrlen + 1))
  415. return -1;
  416. if (meshdr->flags & MESH_FLAGS_AE_A4)
  417. return -1;
  418. if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
  419. skb_copy_bits(skb, hdrlen +
  420. offsetof(struct ieee80211s_hdr, eaddr1),
  421. dst, ETH_ALEN);
  422. skb_copy_bits(skb, hdrlen +
  423. offsetof(struct ieee80211s_hdr, eaddr2),
  424. src, ETH_ALEN);
  425. }
  426. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  427. }
  428. break;
  429. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  430. if ((iftype != NL80211_IFTYPE_STATION &&
  431. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  432. iftype != NL80211_IFTYPE_MESH_POINT) ||
  433. (is_multicast_ether_addr(dst) &&
  434. ether_addr_equal(src, addr)))
  435. return -1;
  436. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  437. struct ieee80211s_hdr *meshdr =
  438. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  439. /* make sure meshdr->flags is on the linear part */
  440. if (!pskb_may_pull(skb, hdrlen + 1))
  441. return -1;
  442. if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
  443. return -1;
  444. if (meshdr->flags & MESH_FLAGS_AE_A4)
  445. skb_copy_bits(skb, hdrlen +
  446. offsetof(struct ieee80211s_hdr, eaddr1),
  447. src, ETH_ALEN);
  448. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  449. }
  450. break;
  451. case cpu_to_le16(0):
  452. if (iftype != NL80211_IFTYPE_ADHOC &&
  453. iftype != NL80211_IFTYPE_STATION &&
  454. iftype != NL80211_IFTYPE_OCB)
  455. return -1;
  456. break;
  457. }
  458. if (!pskb_may_pull(skb, hdrlen + 8))
  459. return -1;
  460. payload = skb->data + hdrlen;
  461. ethertype = (payload[6] << 8) | payload[7];
  462. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  463. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  464. ether_addr_equal(payload, bridge_tunnel_header))) {
  465. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  466. * replace EtherType */
  467. skb_pull(skb, hdrlen + 6);
  468. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  469. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  470. } else {
  471. struct ethhdr *ehdr;
  472. __be16 len;
  473. skb_pull(skb, hdrlen);
  474. len = htons(skb->len);
  475. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  476. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  477. memcpy(ehdr->h_source, src, ETH_ALEN);
  478. ehdr->h_proto = len;
  479. }
  480. return 0;
  481. }
  482. EXPORT_SYMBOL(ieee80211_data_to_8023);
  483. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  484. enum nl80211_iftype iftype,
  485. const u8 *bssid, bool qos)
  486. {
  487. struct ieee80211_hdr hdr;
  488. u16 hdrlen, ethertype;
  489. __le16 fc;
  490. const u8 *encaps_data;
  491. int encaps_len, skip_header_bytes;
  492. int nh_pos, h_pos;
  493. int head_need;
  494. if (unlikely(skb->len < ETH_HLEN))
  495. return -EINVAL;
  496. nh_pos = skb_network_header(skb) - skb->data;
  497. h_pos = skb_transport_header(skb) - skb->data;
  498. /* convert Ethernet header to proper 802.11 header (based on
  499. * operation mode) */
  500. ethertype = (skb->data[12] << 8) | skb->data[13];
  501. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  502. switch (iftype) {
  503. case NL80211_IFTYPE_AP:
  504. case NL80211_IFTYPE_AP_VLAN:
  505. case NL80211_IFTYPE_P2P_GO:
  506. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  507. /* DA BSSID SA */
  508. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  509. memcpy(hdr.addr2, addr, ETH_ALEN);
  510. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  511. hdrlen = 24;
  512. break;
  513. case NL80211_IFTYPE_STATION:
  514. case NL80211_IFTYPE_P2P_CLIENT:
  515. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  516. /* BSSID SA DA */
  517. memcpy(hdr.addr1, bssid, ETH_ALEN);
  518. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  519. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  520. hdrlen = 24;
  521. break;
  522. case NL80211_IFTYPE_OCB:
  523. case NL80211_IFTYPE_ADHOC:
  524. /* DA SA BSSID */
  525. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  526. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  527. memcpy(hdr.addr3, bssid, ETH_ALEN);
  528. hdrlen = 24;
  529. break;
  530. default:
  531. return -EOPNOTSUPP;
  532. }
  533. if (qos) {
  534. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  535. hdrlen += 2;
  536. }
  537. hdr.frame_control = fc;
  538. hdr.duration_id = 0;
  539. hdr.seq_ctrl = 0;
  540. skip_header_bytes = ETH_HLEN;
  541. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  542. encaps_data = bridge_tunnel_header;
  543. encaps_len = sizeof(bridge_tunnel_header);
  544. skip_header_bytes -= 2;
  545. } else if (ethertype >= ETH_P_802_3_MIN) {
  546. encaps_data = rfc1042_header;
  547. encaps_len = sizeof(rfc1042_header);
  548. skip_header_bytes -= 2;
  549. } else {
  550. encaps_data = NULL;
  551. encaps_len = 0;
  552. }
  553. skb_pull(skb, skip_header_bytes);
  554. nh_pos -= skip_header_bytes;
  555. h_pos -= skip_header_bytes;
  556. head_need = hdrlen + encaps_len - skb_headroom(skb);
  557. if (head_need > 0 || skb_cloned(skb)) {
  558. head_need = max(head_need, 0);
  559. if (head_need)
  560. skb_orphan(skb);
  561. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
  562. return -ENOMEM;
  563. skb->truesize += head_need;
  564. }
  565. if (encaps_data) {
  566. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  567. nh_pos += encaps_len;
  568. h_pos += encaps_len;
  569. }
  570. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  571. nh_pos += hdrlen;
  572. h_pos += hdrlen;
  573. /* Update skb pointers to various headers since this modified frame
  574. * is going to go through Linux networking code that may potentially
  575. * need things like pointer to IP header. */
  576. skb_set_mac_header(skb, 0);
  577. skb_set_network_header(skb, nh_pos);
  578. skb_set_transport_header(skb, h_pos);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(ieee80211_data_from_8023);
  582. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  583. const u8 *addr, enum nl80211_iftype iftype,
  584. const unsigned int extra_headroom,
  585. bool has_80211_header)
  586. {
  587. struct sk_buff *frame = NULL;
  588. u16 ethertype;
  589. u8 *payload;
  590. const struct ethhdr *eth;
  591. int remaining, err;
  592. u8 dst[ETH_ALEN], src[ETH_ALEN];
  593. if (has_80211_header) {
  594. err = ieee80211_data_to_8023(skb, addr, iftype);
  595. if (err)
  596. goto out;
  597. /* skip the wrapping header */
  598. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  599. if (!eth)
  600. goto out;
  601. } else {
  602. eth = (struct ethhdr *) skb->data;
  603. }
  604. while (skb != frame) {
  605. u8 padding;
  606. __be16 len = eth->h_proto;
  607. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  608. remaining = skb->len;
  609. memcpy(dst, eth->h_dest, ETH_ALEN);
  610. memcpy(src, eth->h_source, ETH_ALEN);
  611. padding = (4 - subframe_len) & 0x3;
  612. /* the last MSDU has no padding */
  613. if (subframe_len > remaining)
  614. goto purge;
  615. skb_pull(skb, sizeof(struct ethhdr));
  616. /* reuse skb for the last subframe */
  617. if (remaining <= subframe_len + padding)
  618. frame = skb;
  619. else {
  620. unsigned int hlen = ALIGN(extra_headroom, 4);
  621. /*
  622. * Allocate and reserve two bytes more for payload
  623. * alignment since sizeof(struct ethhdr) is 14.
  624. */
  625. frame = dev_alloc_skb(hlen + subframe_len + 2);
  626. if (!frame)
  627. goto purge;
  628. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  629. memcpy(skb_put(frame, ntohs(len)), skb->data,
  630. ntohs(len));
  631. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  632. padding);
  633. if (!eth) {
  634. dev_kfree_skb(frame);
  635. goto purge;
  636. }
  637. }
  638. skb_reset_network_header(frame);
  639. frame->dev = skb->dev;
  640. frame->priority = skb->priority;
  641. payload = frame->data;
  642. ethertype = (payload[6] << 8) | payload[7];
  643. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  644. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  645. ether_addr_equal(payload, bridge_tunnel_header))) {
  646. /* remove RFC1042 or Bridge-Tunnel
  647. * encapsulation and replace EtherType */
  648. skb_pull(frame, 6);
  649. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  650. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  651. } else {
  652. memcpy(skb_push(frame, sizeof(__be16)), &len,
  653. sizeof(__be16));
  654. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  655. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  656. }
  657. __skb_queue_tail(list, frame);
  658. }
  659. return;
  660. purge:
  661. __skb_queue_purge(list);
  662. out:
  663. dev_kfree_skb(skb);
  664. }
  665. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  666. /* Given a data frame determine the 802.1p/1d tag to use. */
  667. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  668. struct cfg80211_qos_map *qos_map)
  669. {
  670. unsigned int dscp;
  671. unsigned char vlan_priority;
  672. /* skb->priority values from 256->263 are magic values to
  673. * directly indicate a specific 802.1d priority. This is used
  674. * to allow 802.1d priority to be passed directly in from VLAN
  675. * tags, etc.
  676. */
  677. if (skb->priority >= 256 && skb->priority <= 263)
  678. return skb->priority - 256;
  679. if (skb_vlan_tag_present(skb)) {
  680. vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
  681. >> VLAN_PRIO_SHIFT;
  682. if (vlan_priority > 0)
  683. return vlan_priority;
  684. }
  685. switch (skb->protocol) {
  686. case htons(ETH_P_IP):
  687. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  688. break;
  689. case htons(ETH_P_IPV6):
  690. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  691. break;
  692. case htons(ETH_P_MPLS_UC):
  693. case htons(ETH_P_MPLS_MC): {
  694. struct mpls_label mpls_tmp, *mpls;
  695. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  696. sizeof(*mpls), &mpls_tmp);
  697. if (!mpls)
  698. return 0;
  699. return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  700. >> MPLS_LS_TC_SHIFT;
  701. }
  702. case htons(ETH_P_80221):
  703. /* 802.21 is always network control traffic */
  704. return 7;
  705. default:
  706. return 0;
  707. }
  708. if (qos_map) {
  709. unsigned int i, tmp_dscp = dscp >> 2;
  710. for (i = 0; i < qos_map->num_des; i++) {
  711. if (tmp_dscp == qos_map->dscp_exception[i].dscp)
  712. return qos_map->dscp_exception[i].up;
  713. }
  714. for (i = 0; i < 8; i++) {
  715. if (tmp_dscp >= qos_map->up[i].low &&
  716. tmp_dscp <= qos_map->up[i].high)
  717. return i;
  718. }
  719. }
  720. return dscp >> 5;
  721. }
  722. EXPORT_SYMBOL(cfg80211_classify8021d);
  723. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  724. {
  725. const struct cfg80211_bss_ies *ies;
  726. ies = rcu_dereference(bss->ies);
  727. if (!ies)
  728. return NULL;
  729. return cfg80211_find_ie(ie, ies->data, ies->len);
  730. }
  731. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  732. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  733. {
  734. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  735. struct net_device *dev = wdev->netdev;
  736. int i;
  737. if (!wdev->connect_keys)
  738. return;
  739. for (i = 0; i < 6; i++) {
  740. if (!wdev->connect_keys->params[i].cipher)
  741. continue;
  742. if (rdev_add_key(rdev, dev, i, false, NULL,
  743. &wdev->connect_keys->params[i])) {
  744. netdev_err(dev, "failed to set key %d\n", i);
  745. continue;
  746. }
  747. if (wdev->connect_keys->def == i)
  748. if (rdev_set_default_key(rdev, dev, i, true, true)) {
  749. netdev_err(dev, "failed to set defkey %d\n", i);
  750. continue;
  751. }
  752. if (wdev->connect_keys->defmgmt == i)
  753. if (rdev_set_default_mgmt_key(rdev, dev, i))
  754. netdev_err(dev, "failed to set mgtdef %d\n", i);
  755. }
  756. kzfree(wdev->connect_keys);
  757. wdev->connect_keys = NULL;
  758. }
  759. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  760. {
  761. struct cfg80211_event *ev;
  762. unsigned long flags;
  763. const u8 *bssid = NULL;
  764. spin_lock_irqsave(&wdev->event_lock, flags);
  765. while (!list_empty(&wdev->event_list)) {
  766. ev = list_first_entry(&wdev->event_list,
  767. struct cfg80211_event, list);
  768. list_del(&ev->list);
  769. spin_unlock_irqrestore(&wdev->event_lock, flags);
  770. wdev_lock(wdev);
  771. switch (ev->type) {
  772. case EVENT_CONNECT_RESULT:
  773. if (!is_zero_ether_addr(ev->cr.bssid))
  774. bssid = ev->cr.bssid;
  775. __cfg80211_connect_result(
  776. wdev->netdev, bssid,
  777. ev->cr.req_ie, ev->cr.req_ie_len,
  778. ev->cr.resp_ie, ev->cr.resp_ie_len,
  779. ev->cr.status,
  780. ev->cr.status == WLAN_STATUS_SUCCESS,
  781. NULL);
  782. break;
  783. case EVENT_ROAMED:
  784. __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
  785. ev->rm.req_ie_len, ev->rm.resp_ie,
  786. ev->rm.resp_ie_len);
  787. break;
  788. case EVENT_DISCONNECTED:
  789. __cfg80211_disconnected(wdev->netdev,
  790. ev->dc.ie, ev->dc.ie_len,
  791. ev->dc.reason, true);
  792. break;
  793. case EVENT_IBSS_JOINED:
  794. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  795. ev->ij.channel);
  796. break;
  797. case EVENT_STOPPED:
  798. __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
  799. break;
  800. }
  801. wdev_unlock(wdev);
  802. kfree(ev);
  803. spin_lock_irqsave(&wdev->event_lock, flags);
  804. }
  805. spin_unlock_irqrestore(&wdev->event_lock, flags);
  806. }
  807. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  808. {
  809. struct wireless_dev *wdev;
  810. ASSERT_RTNL();
  811. list_for_each_entry(wdev, &rdev->wdev_list, list)
  812. cfg80211_process_wdev_events(wdev);
  813. }
  814. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  815. struct net_device *dev, enum nl80211_iftype ntype,
  816. u32 *flags, struct vif_params *params)
  817. {
  818. int err;
  819. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  820. ASSERT_RTNL();
  821. /* don't support changing VLANs, you just re-create them */
  822. if (otype == NL80211_IFTYPE_AP_VLAN)
  823. return -EOPNOTSUPP;
  824. /* cannot change into P2P device type */
  825. if (ntype == NL80211_IFTYPE_P2P_DEVICE)
  826. return -EOPNOTSUPP;
  827. if (!rdev->ops->change_virtual_intf ||
  828. !(rdev->wiphy.interface_modes & (1 << ntype)))
  829. return -EOPNOTSUPP;
  830. /* if it's part of a bridge, reject changing type to station/ibss */
  831. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  832. (ntype == NL80211_IFTYPE_ADHOC ||
  833. ntype == NL80211_IFTYPE_STATION ||
  834. ntype == NL80211_IFTYPE_P2P_CLIENT))
  835. return -EBUSY;
  836. if (ntype != otype && netif_running(dev)) {
  837. dev->ieee80211_ptr->use_4addr = false;
  838. dev->ieee80211_ptr->mesh_id_up_len = 0;
  839. wdev_lock(dev->ieee80211_ptr);
  840. rdev_set_qos_map(rdev, dev, NULL);
  841. wdev_unlock(dev->ieee80211_ptr);
  842. switch (otype) {
  843. case NL80211_IFTYPE_AP:
  844. cfg80211_stop_ap(rdev, dev, true);
  845. break;
  846. case NL80211_IFTYPE_ADHOC:
  847. cfg80211_leave_ibss(rdev, dev, false);
  848. break;
  849. case NL80211_IFTYPE_STATION:
  850. case NL80211_IFTYPE_P2P_CLIENT:
  851. wdev_lock(dev->ieee80211_ptr);
  852. cfg80211_disconnect(rdev, dev,
  853. WLAN_REASON_DEAUTH_LEAVING, true);
  854. wdev_unlock(dev->ieee80211_ptr);
  855. break;
  856. case NL80211_IFTYPE_MESH_POINT:
  857. /* mesh should be handled? */
  858. break;
  859. default:
  860. break;
  861. }
  862. cfg80211_process_rdev_events(rdev);
  863. }
  864. err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
  865. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  866. if (!err && params && params->use_4addr != -1)
  867. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  868. if (!err) {
  869. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  870. switch (ntype) {
  871. case NL80211_IFTYPE_STATION:
  872. if (dev->ieee80211_ptr->use_4addr)
  873. break;
  874. /* fall through */
  875. case NL80211_IFTYPE_OCB:
  876. case NL80211_IFTYPE_P2P_CLIENT:
  877. case NL80211_IFTYPE_ADHOC:
  878. dev->priv_flags |= IFF_DONT_BRIDGE;
  879. break;
  880. case NL80211_IFTYPE_P2P_GO:
  881. case NL80211_IFTYPE_AP:
  882. case NL80211_IFTYPE_AP_VLAN:
  883. case NL80211_IFTYPE_WDS:
  884. case NL80211_IFTYPE_MESH_POINT:
  885. /* bridging OK */
  886. break;
  887. case NL80211_IFTYPE_MONITOR:
  888. /* monitor can't bridge anyway */
  889. break;
  890. case NL80211_IFTYPE_UNSPECIFIED:
  891. case NUM_NL80211_IFTYPES:
  892. /* not happening */
  893. break;
  894. case NL80211_IFTYPE_P2P_DEVICE:
  895. WARN_ON(1);
  896. break;
  897. }
  898. }
  899. if (!err && ntype != otype && netif_running(dev)) {
  900. cfg80211_update_iface_num(rdev, ntype, 1);
  901. cfg80211_update_iface_num(rdev, otype, -1);
  902. }
  903. return err;
  904. }
  905. static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
  906. {
  907. static const u32 __mcs2bitrate[] = {
  908. /* control PHY */
  909. [0] = 275,
  910. /* SC PHY */
  911. [1] = 3850,
  912. [2] = 7700,
  913. [3] = 9625,
  914. [4] = 11550,
  915. [5] = 12512, /* 1251.25 mbps */
  916. [6] = 15400,
  917. [7] = 19250,
  918. [8] = 23100,
  919. [9] = 25025,
  920. [10] = 30800,
  921. [11] = 38500,
  922. [12] = 46200,
  923. /* OFDM PHY */
  924. [13] = 6930,
  925. [14] = 8662, /* 866.25 mbps */
  926. [15] = 13860,
  927. [16] = 17325,
  928. [17] = 20790,
  929. [18] = 27720,
  930. [19] = 34650,
  931. [20] = 41580,
  932. [21] = 45045,
  933. [22] = 51975,
  934. [23] = 62370,
  935. [24] = 67568, /* 6756.75 mbps */
  936. /* LP-SC PHY */
  937. [25] = 6260,
  938. [26] = 8340,
  939. [27] = 11120,
  940. [28] = 12510,
  941. [29] = 16680,
  942. [30] = 22240,
  943. [31] = 25030,
  944. };
  945. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  946. return 0;
  947. return __mcs2bitrate[rate->mcs];
  948. }
  949. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  950. {
  951. static const u32 base[4][10] = {
  952. { 6500000,
  953. 13000000,
  954. 19500000,
  955. 26000000,
  956. 39000000,
  957. 52000000,
  958. 58500000,
  959. 65000000,
  960. 78000000,
  961. 0,
  962. },
  963. { 13500000,
  964. 27000000,
  965. 40500000,
  966. 54000000,
  967. 81000000,
  968. 108000000,
  969. 121500000,
  970. 135000000,
  971. 162000000,
  972. 180000000,
  973. },
  974. { 29300000,
  975. 58500000,
  976. 87800000,
  977. 117000000,
  978. 175500000,
  979. 234000000,
  980. 263300000,
  981. 292500000,
  982. 351000000,
  983. 390000000,
  984. },
  985. { 58500000,
  986. 117000000,
  987. 175500000,
  988. 234000000,
  989. 351000000,
  990. 468000000,
  991. 526500000,
  992. 585000000,
  993. 702000000,
  994. 780000000,
  995. },
  996. };
  997. u32 bitrate;
  998. int idx;
  999. if (WARN_ON_ONCE(rate->mcs > 9))
  1000. return 0;
  1001. switch (rate->bw) {
  1002. case RATE_INFO_BW_160:
  1003. idx = 3;
  1004. break;
  1005. case RATE_INFO_BW_80:
  1006. idx = 2;
  1007. break;
  1008. case RATE_INFO_BW_40:
  1009. idx = 1;
  1010. break;
  1011. case RATE_INFO_BW_5:
  1012. case RATE_INFO_BW_10:
  1013. default:
  1014. WARN_ON(1);
  1015. /* fall through */
  1016. case RATE_INFO_BW_20:
  1017. idx = 0;
  1018. }
  1019. bitrate = base[idx][rate->mcs];
  1020. bitrate *= rate->nss;
  1021. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1022. bitrate = (bitrate / 9) * 10;
  1023. /* do NOT round down here */
  1024. return (bitrate + 50000) / 100000;
  1025. }
  1026. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  1027. {
  1028. int modulation, streams, bitrate;
  1029. if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
  1030. !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
  1031. return rate->legacy;
  1032. if (rate->flags & RATE_INFO_FLAGS_60G)
  1033. return cfg80211_calculate_bitrate_60g(rate);
  1034. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  1035. return cfg80211_calculate_bitrate_vht(rate);
  1036. /* the formula below does only work for MCS values smaller than 32 */
  1037. if (WARN_ON_ONCE(rate->mcs >= 32))
  1038. return 0;
  1039. modulation = rate->mcs & 7;
  1040. streams = (rate->mcs >> 3) + 1;
  1041. bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
  1042. if (modulation < 4)
  1043. bitrate *= (modulation + 1);
  1044. else if (modulation == 4)
  1045. bitrate *= (modulation + 2);
  1046. else
  1047. bitrate *= (modulation + 3);
  1048. bitrate *= streams;
  1049. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1050. bitrate = (bitrate / 9) * 10;
  1051. /* do NOT round down here */
  1052. return (bitrate + 50000) / 100000;
  1053. }
  1054. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  1055. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  1056. enum ieee80211_p2p_attr_id attr,
  1057. u8 *buf, unsigned int bufsize)
  1058. {
  1059. u8 *out = buf;
  1060. u16 attr_remaining = 0;
  1061. bool desired_attr = false;
  1062. u16 desired_len = 0;
  1063. while (len > 0) {
  1064. unsigned int iedatalen;
  1065. unsigned int copy;
  1066. const u8 *iedata;
  1067. if (len < 2)
  1068. return -EILSEQ;
  1069. iedatalen = ies[1];
  1070. if (iedatalen + 2 > len)
  1071. return -EILSEQ;
  1072. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1073. goto cont;
  1074. if (iedatalen < 4)
  1075. goto cont;
  1076. iedata = ies + 2;
  1077. /* check WFA OUI, P2P subtype */
  1078. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1079. iedata[2] != 0x9a || iedata[3] != 0x09)
  1080. goto cont;
  1081. iedatalen -= 4;
  1082. iedata += 4;
  1083. /* check attribute continuation into this IE */
  1084. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1085. if (copy && desired_attr) {
  1086. desired_len += copy;
  1087. if (out) {
  1088. memcpy(out, iedata, min(bufsize, copy));
  1089. out += min(bufsize, copy);
  1090. bufsize -= min(bufsize, copy);
  1091. }
  1092. if (copy == attr_remaining)
  1093. return desired_len;
  1094. }
  1095. attr_remaining -= copy;
  1096. if (attr_remaining)
  1097. goto cont;
  1098. iedatalen -= copy;
  1099. iedata += copy;
  1100. while (iedatalen > 0) {
  1101. u16 attr_len;
  1102. /* P2P attribute ID & size must fit */
  1103. if (iedatalen < 3)
  1104. return -EILSEQ;
  1105. desired_attr = iedata[0] == attr;
  1106. attr_len = get_unaligned_le16(iedata + 1);
  1107. iedatalen -= 3;
  1108. iedata += 3;
  1109. copy = min_t(unsigned int, attr_len, iedatalen);
  1110. if (desired_attr) {
  1111. desired_len += copy;
  1112. if (out) {
  1113. memcpy(out, iedata, min(bufsize, copy));
  1114. out += min(bufsize, copy);
  1115. bufsize -= min(bufsize, copy);
  1116. }
  1117. if (copy == attr_len)
  1118. return desired_len;
  1119. }
  1120. iedata += copy;
  1121. iedatalen -= copy;
  1122. attr_remaining = attr_len - copy;
  1123. }
  1124. cont:
  1125. len -= ies[1] + 2;
  1126. ies += ies[1] + 2;
  1127. }
  1128. if (attr_remaining && desired_attr)
  1129. return -EILSEQ;
  1130. return -ENOENT;
  1131. }
  1132. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1133. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1134. {
  1135. int i;
  1136. for (i = 0; i < n_ids; i++)
  1137. if (ids[i] == id)
  1138. return true;
  1139. return false;
  1140. }
  1141. size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
  1142. const u8 *ids, int n_ids,
  1143. const u8 *after_ric, int n_after_ric,
  1144. size_t offset)
  1145. {
  1146. size_t pos = offset;
  1147. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
  1148. if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
  1149. pos += 2 + ies[pos + 1];
  1150. while (pos < ielen &&
  1151. !ieee80211_id_in_list(after_ric, n_after_ric,
  1152. ies[pos]))
  1153. pos += 2 + ies[pos + 1];
  1154. } else {
  1155. pos += 2 + ies[pos + 1];
  1156. }
  1157. }
  1158. return pos;
  1159. }
  1160. EXPORT_SYMBOL(ieee80211_ie_split_ric);
  1161. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1162. const u8 *ids, int n_ids, size_t offset)
  1163. {
  1164. return ieee80211_ie_split_ric(ies, ielen, ids, n_ids, NULL, 0, offset);
  1165. }
  1166. EXPORT_SYMBOL(ieee80211_ie_split);
  1167. bool ieee80211_operating_class_to_band(u8 operating_class,
  1168. enum ieee80211_band *band)
  1169. {
  1170. switch (operating_class) {
  1171. case 112:
  1172. case 115 ... 127:
  1173. case 128 ... 130:
  1174. *band = IEEE80211_BAND_5GHZ;
  1175. return true;
  1176. case 81:
  1177. case 82:
  1178. case 83:
  1179. case 84:
  1180. *band = IEEE80211_BAND_2GHZ;
  1181. return true;
  1182. case 180:
  1183. *band = IEEE80211_BAND_60GHZ;
  1184. return true;
  1185. }
  1186. return false;
  1187. }
  1188. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1189. bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
  1190. u8 *op_class)
  1191. {
  1192. u8 vht_opclass;
  1193. u16 freq = chandef->center_freq1;
  1194. if (freq >= 2412 && freq <= 2472) {
  1195. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1196. return false;
  1197. /* 2.407 GHz, channels 1..13 */
  1198. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1199. if (freq > chandef->chan->center_freq)
  1200. *op_class = 83; /* HT40+ */
  1201. else
  1202. *op_class = 84; /* HT40- */
  1203. } else {
  1204. *op_class = 81;
  1205. }
  1206. return true;
  1207. }
  1208. if (freq == 2484) {
  1209. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1210. return false;
  1211. *op_class = 82; /* channel 14 */
  1212. return true;
  1213. }
  1214. switch (chandef->width) {
  1215. case NL80211_CHAN_WIDTH_80:
  1216. vht_opclass = 128;
  1217. break;
  1218. case NL80211_CHAN_WIDTH_160:
  1219. vht_opclass = 129;
  1220. break;
  1221. case NL80211_CHAN_WIDTH_80P80:
  1222. vht_opclass = 130;
  1223. break;
  1224. case NL80211_CHAN_WIDTH_10:
  1225. case NL80211_CHAN_WIDTH_5:
  1226. return false; /* unsupported for now */
  1227. default:
  1228. vht_opclass = 0;
  1229. break;
  1230. }
  1231. /* 5 GHz, channels 36..48 */
  1232. if (freq >= 5180 && freq <= 5240) {
  1233. if (vht_opclass) {
  1234. *op_class = vht_opclass;
  1235. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1236. if (freq > chandef->chan->center_freq)
  1237. *op_class = 116;
  1238. else
  1239. *op_class = 117;
  1240. } else {
  1241. *op_class = 115;
  1242. }
  1243. return true;
  1244. }
  1245. /* 5 GHz, channels 52..64 */
  1246. if (freq >= 5260 && freq <= 5320) {
  1247. if (vht_opclass) {
  1248. *op_class = vht_opclass;
  1249. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1250. if (freq > chandef->chan->center_freq)
  1251. *op_class = 119;
  1252. else
  1253. *op_class = 120;
  1254. } else {
  1255. *op_class = 118;
  1256. }
  1257. return true;
  1258. }
  1259. /* 5 GHz, channels 100..144 */
  1260. if (freq >= 5500 && freq <= 5720) {
  1261. if (vht_opclass) {
  1262. *op_class = vht_opclass;
  1263. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1264. if (freq > chandef->chan->center_freq)
  1265. *op_class = 122;
  1266. else
  1267. *op_class = 123;
  1268. } else {
  1269. *op_class = 121;
  1270. }
  1271. return true;
  1272. }
  1273. /* 5 GHz, channels 149..169 */
  1274. if (freq >= 5745 && freq <= 5845) {
  1275. if (vht_opclass) {
  1276. *op_class = vht_opclass;
  1277. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1278. if (freq > chandef->chan->center_freq)
  1279. *op_class = 126;
  1280. else
  1281. *op_class = 127;
  1282. } else if (freq <= 5805) {
  1283. *op_class = 124;
  1284. } else {
  1285. *op_class = 125;
  1286. }
  1287. return true;
  1288. }
  1289. /* 56.16 GHz, channel 1..4 */
  1290. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  1291. if (chandef->width >= NL80211_CHAN_WIDTH_40)
  1292. return false;
  1293. *op_class = 180;
  1294. return true;
  1295. }
  1296. /* not supported yet */
  1297. return false;
  1298. }
  1299. EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
  1300. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  1301. u32 beacon_int)
  1302. {
  1303. struct wireless_dev *wdev;
  1304. int res = 0;
  1305. if (!beacon_int)
  1306. return -EINVAL;
  1307. list_for_each_entry(wdev, &rdev->wdev_list, list) {
  1308. if (!wdev->beacon_interval)
  1309. continue;
  1310. if (wdev->beacon_interval != beacon_int) {
  1311. res = -EINVAL;
  1312. break;
  1313. }
  1314. }
  1315. return res;
  1316. }
  1317. int cfg80211_iter_combinations(struct wiphy *wiphy,
  1318. const int num_different_channels,
  1319. const u8 radar_detect,
  1320. const int iftype_num[NUM_NL80211_IFTYPES],
  1321. void (*iter)(const struct ieee80211_iface_combination *c,
  1322. void *data),
  1323. void *data)
  1324. {
  1325. const struct ieee80211_regdomain *regdom;
  1326. enum nl80211_dfs_regions region = 0;
  1327. int i, j, iftype;
  1328. int num_interfaces = 0;
  1329. u32 used_iftypes = 0;
  1330. if (radar_detect) {
  1331. rcu_read_lock();
  1332. regdom = rcu_dereference(cfg80211_regdomain);
  1333. if (regdom)
  1334. region = regdom->dfs_region;
  1335. rcu_read_unlock();
  1336. }
  1337. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1338. num_interfaces += iftype_num[iftype];
  1339. if (iftype_num[iftype] > 0 &&
  1340. !(wiphy->software_iftypes & BIT(iftype)))
  1341. used_iftypes |= BIT(iftype);
  1342. }
  1343. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  1344. const struct ieee80211_iface_combination *c;
  1345. struct ieee80211_iface_limit *limits;
  1346. u32 all_iftypes = 0;
  1347. c = &wiphy->iface_combinations[i];
  1348. if (num_interfaces > c->max_interfaces)
  1349. continue;
  1350. if (num_different_channels > c->num_different_channels)
  1351. continue;
  1352. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  1353. GFP_KERNEL);
  1354. if (!limits)
  1355. return -ENOMEM;
  1356. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1357. if (wiphy->software_iftypes & BIT(iftype))
  1358. continue;
  1359. for (j = 0; j < c->n_limits; j++) {
  1360. all_iftypes |= limits[j].types;
  1361. if (!(limits[j].types & BIT(iftype)))
  1362. continue;
  1363. if (limits[j].max < iftype_num[iftype])
  1364. goto cont;
  1365. limits[j].max -= iftype_num[iftype];
  1366. }
  1367. }
  1368. if (radar_detect != (c->radar_detect_widths & radar_detect))
  1369. goto cont;
  1370. if (radar_detect && c->radar_detect_regions &&
  1371. !(c->radar_detect_regions & BIT(region)))
  1372. goto cont;
  1373. /* Finally check that all iftypes that we're currently
  1374. * using are actually part of this combination. If they
  1375. * aren't then we can't use this combination and have
  1376. * to continue to the next.
  1377. */
  1378. if ((all_iftypes & used_iftypes) != used_iftypes)
  1379. goto cont;
  1380. /* This combination covered all interface types and
  1381. * supported the requested numbers, so we're good.
  1382. */
  1383. (*iter)(c, data);
  1384. cont:
  1385. kfree(limits);
  1386. }
  1387. return 0;
  1388. }
  1389. EXPORT_SYMBOL(cfg80211_iter_combinations);
  1390. static void
  1391. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  1392. void *data)
  1393. {
  1394. int *num = data;
  1395. (*num)++;
  1396. }
  1397. int cfg80211_check_combinations(struct wiphy *wiphy,
  1398. const int num_different_channels,
  1399. const u8 radar_detect,
  1400. const int iftype_num[NUM_NL80211_IFTYPES])
  1401. {
  1402. int err, num = 0;
  1403. err = cfg80211_iter_combinations(wiphy, num_different_channels,
  1404. radar_detect, iftype_num,
  1405. cfg80211_iter_sum_ifcombs, &num);
  1406. if (err)
  1407. return err;
  1408. if (num == 0)
  1409. return -EBUSY;
  1410. return 0;
  1411. }
  1412. EXPORT_SYMBOL(cfg80211_check_combinations);
  1413. int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
  1414. struct wireless_dev *wdev,
  1415. enum nl80211_iftype iftype,
  1416. struct ieee80211_channel *chan,
  1417. enum cfg80211_chan_mode chanmode,
  1418. u8 radar_detect)
  1419. {
  1420. struct wireless_dev *wdev_iter;
  1421. int num[NUM_NL80211_IFTYPES];
  1422. struct ieee80211_channel
  1423. *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
  1424. struct ieee80211_channel *ch;
  1425. enum cfg80211_chan_mode chmode;
  1426. int num_different_channels = 0;
  1427. int total = 1;
  1428. int i;
  1429. ASSERT_RTNL();
  1430. if (WARN_ON(hweight32(radar_detect) > 1))
  1431. return -EINVAL;
  1432. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  1433. return -EINVAL;
  1434. /* Always allow software iftypes */
  1435. if (rdev->wiphy.software_iftypes & BIT(iftype)) {
  1436. if (radar_detect)
  1437. return -EINVAL;
  1438. return 0;
  1439. }
  1440. memset(num, 0, sizeof(num));
  1441. memset(used_channels, 0, sizeof(used_channels));
  1442. num[iftype] = 1;
  1443. /* TODO: We'll probably not need this anymore, since this
  1444. * should only be called with CHAN_MODE_UNDEFINED. There are
  1445. * still a couple of pending calls where other chanmodes are
  1446. * used, but we should get rid of them.
  1447. */
  1448. switch (chanmode) {
  1449. case CHAN_MODE_UNDEFINED:
  1450. break;
  1451. case CHAN_MODE_SHARED:
  1452. WARN_ON(!chan);
  1453. used_channels[0] = chan;
  1454. num_different_channels++;
  1455. break;
  1456. case CHAN_MODE_EXCLUSIVE:
  1457. num_different_channels++;
  1458. break;
  1459. }
  1460. list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
  1461. if (wdev_iter == wdev)
  1462. continue;
  1463. if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
  1464. if (!wdev_iter->p2p_started)
  1465. continue;
  1466. } else if (wdev_iter->netdev) {
  1467. if (!netif_running(wdev_iter->netdev))
  1468. continue;
  1469. } else {
  1470. WARN_ON(1);
  1471. }
  1472. if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
  1473. continue;
  1474. /*
  1475. * We may be holding the "wdev" mutex, but now need to lock
  1476. * wdev_iter. This is OK because once we get here wdev_iter
  1477. * is not wdev (tested above), but we need to use the nested
  1478. * locking for lockdep.
  1479. */
  1480. mutex_lock_nested(&wdev_iter->mtx, 1);
  1481. __acquire(wdev_iter->mtx);
  1482. cfg80211_get_chan_state(wdev_iter, &ch, &chmode, &radar_detect);
  1483. wdev_unlock(wdev_iter);
  1484. switch (chmode) {
  1485. case CHAN_MODE_UNDEFINED:
  1486. break;
  1487. case CHAN_MODE_SHARED:
  1488. for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
  1489. if (!used_channels[i] || used_channels[i] == ch)
  1490. break;
  1491. if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
  1492. return -EBUSY;
  1493. if (used_channels[i] == NULL) {
  1494. used_channels[i] = ch;
  1495. num_different_channels++;
  1496. }
  1497. break;
  1498. case CHAN_MODE_EXCLUSIVE:
  1499. num_different_channels++;
  1500. break;
  1501. }
  1502. num[wdev_iter->iftype]++;
  1503. total++;
  1504. }
  1505. if (total == 1 && !radar_detect)
  1506. return 0;
  1507. return cfg80211_check_combinations(&rdev->wiphy, num_different_channels,
  1508. radar_detect, num);
  1509. }
  1510. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  1511. const u8 *rates, unsigned int n_rates,
  1512. u32 *mask)
  1513. {
  1514. int i, j;
  1515. if (!sband)
  1516. return -EINVAL;
  1517. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  1518. return -EINVAL;
  1519. *mask = 0;
  1520. for (i = 0; i < n_rates; i++) {
  1521. int rate = (rates[i] & 0x7f) * 5;
  1522. bool found = false;
  1523. for (j = 0; j < sband->n_bitrates; j++) {
  1524. if (sband->bitrates[j].bitrate == rate) {
  1525. found = true;
  1526. *mask |= BIT(j);
  1527. break;
  1528. }
  1529. }
  1530. if (!found)
  1531. return -EINVAL;
  1532. }
  1533. /*
  1534. * mask must have at least one bit set here since we
  1535. * didn't accept a 0-length rates array nor allowed
  1536. * entries in the array that didn't exist
  1537. */
  1538. return 0;
  1539. }
  1540. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  1541. {
  1542. enum ieee80211_band band;
  1543. unsigned int n_channels = 0;
  1544. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  1545. if (wiphy->bands[band])
  1546. n_channels += wiphy->bands[band]->n_channels;
  1547. return n_channels;
  1548. }
  1549. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  1550. int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
  1551. struct station_info *sinfo)
  1552. {
  1553. struct cfg80211_registered_device *rdev;
  1554. struct wireless_dev *wdev;
  1555. wdev = dev->ieee80211_ptr;
  1556. if (!wdev)
  1557. return -EOPNOTSUPP;
  1558. rdev = wiphy_to_rdev(wdev->wiphy);
  1559. if (!rdev->ops->get_station)
  1560. return -EOPNOTSUPP;
  1561. return rdev_get_station(rdev, dev, mac_addr, sinfo);
  1562. }
  1563. EXPORT_SYMBOL(cfg80211_get_station);
  1564. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  1565. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  1566. const unsigned char rfc1042_header[] __aligned(2) =
  1567. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1568. EXPORT_SYMBOL(rfc1042_header);
  1569. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  1570. const unsigned char bridge_tunnel_header[] __aligned(2) =
  1571. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  1572. EXPORT_SYMBOL(bridge_tunnel_header);