util.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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 == NL80211_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 nl80211_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 NL80211_BAND_2GHZ:
  67. if (chan == 14)
  68. return 2484;
  69. else if (chan < 14)
  70. return 2407 + chan * 5;
  71. break;
  72. case NL80211_BAND_5GHZ:
  73. if (chan >= 182 && chan <= 196)
  74. return 4000 + chan * 5;
  75. else
  76. return 5000 + chan * 5;
  77. break;
  78. case NL80211_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 nl80211_band band;
  109. struct ieee80211_supported_band *sband;
  110. int i;
  111. for (band = 0; band < NUM_NL80211_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 nl80211_band band)
  125. {
  126. int i, want;
  127. switch (band) {
  128. case NL80211_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 NL80211_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 NL80211_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 NUM_NL80211_BANDS:
  175. WARN_ON(1);
  176. break;
  177. }
  178. }
  179. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  180. {
  181. enum nl80211_band band;
  182. for (band = 0; band < NUM_NL80211_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. static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
  360. {
  361. int ae = 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. unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  374. {
  375. return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
  376. }
  377. EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
  378. static int __ieee80211_data_to_8023(struct sk_buff *skb, struct ethhdr *ehdr,
  379. const u8 *addr, enum nl80211_iftype iftype)
  380. {
  381. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  382. struct {
  383. u8 hdr[ETH_ALEN] __aligned(2);
  384. __be16 proto;
  385. } payload;
  386. struct ethhdr tmp;
  387. u16 hdrlen;
  388. u8 mesh_flags = 0;
  389. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  390. return -1;
  391. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  392. if (skb->len < hdrlen + 8)
  393. return -1;
  394. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  395. * header
  396. * IEEE 802.11 address fields:
  397. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  398. * 0 0 DA SA BSSID n/a
  399. * 0 1 DA BSSID SA n/a
  400. * 1 0 BSSID SA DA n/a
  401. * 1 1 RA TA DA SA
  402. */
  403. memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
  404. memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
  405. if (iftype == NL80211_IFTYPE_MESH_POINT)
  406. skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
  407. switch (hdr->frame_control &
  408. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  409. case cpu_to_le16(IEEE80211_FCTL_TODS):
  410. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  411. iftype != NL80211_IFTYPE_AP_VLAN &&
  412. iftype != NL80211_IFTYPE_P2P_GO))
  413. return -1;
  414. break;
  415. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  416. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  417. iftype != NL80211_IFTYPE_MESH_POINT &&
  418. iftype != NL80211_IFTYPE_AP_VLAN &&
  419. iftype != NL80211_IFTYPE_STATION))
  420. return -1;
  421. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  422. if (mesh_flags & MESH_FLAGS_AE_A4)
  423. return -1;
  424. if (mesh_flags & MESH_FLAGS_AE_A5_A6) {
  425. skb_copy_bits(skb, hdrlen +
  426. offsetof(struct ieee80211s_hdr, eaddr1),
  427. tmp.h_dest, 2 * ETH_ALEN);
  428. }
  429. hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
  430. }
  431. break;
  432. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  433. if ((iftype != NL80211_IFTYPE_STATION &&
  434. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  435. iftype != NL80211_IFTYPE_MESH_POINT) ||
  436. (is_multicast_ether_addr(tmp.h_dest) &&
  437. ether_addr_equal(tmp.h_source, addr)))
  438. return -1;
  439. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  440. if (mesh_flags & MESH_FLAGS_AE_A5_A6)
  441. return -1;
  442. if (mesh_flags & MESH_FLAGS_AE_A4)
  443. skb_copy_bits(skb, hdrlen +
  444. offsetof(struct ieee80211s_hdr, eaddr1),
  445. tmp.h_source, ETH_ALEN);
  446. hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
  447. }
  448. break;
  449. case cpu_to_le16(0):
  450. if (iftype != NL80211_IFTYPE_ADHOC &&
  451. iftype != NL80211_IFTYPE_STATION &&
  452. iftype != NL80211_IFTYPE_OCB)
  453. return -1;
  454. break;
  455. }
  456. skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
  457. tmp.h_proto = payload.proto;
  458. if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
  459. tmp.h_proto != htons(ETH_P_AARP) &&
  460. tmp.h_proto != htons(ETH_P_IPX)) ||
  461. ether_addr_equal(payload.hdr, bridge_tunnel_header)))
  462. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  463. * replace EtherType */
  464. hdrlen += ETH_ALEN + 2;
  465. else
  466. tmp.h_proto = htons(skb->len - hdrlen);
  467. pskb_pull(skb, hdrlen);
  468. if (!ehdr)
  469. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  470. memcpy(ehdr, &tmp, sizeof(tmp));
  471. return 0;
  472. }
  473. int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
  474. enum nl80211_iftype iftype)
  475. {
  476. return __ieee80211_data_to_8023(skb, NULL, addr, iftype);
  477. }
  478. EXPORT_SYMBOL(ieee80211_data_to_8023);
  479. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  480. enum nl80211_iftype iftype,
  481. const u8 *bssid, bool qos)
  482. {
  483. struct ieee80211_hdr hdr;
  484. u16 hdrlen, ethertype;
  485. __le16 fc;
  486. const u8 *encaps_data;
  487. int encaps_len, skip_header_bytes;
  488. int nh_pos, h_pos;
  489. int head_need;
  490. if (unlikely(skb->len < ETH_HLEN))
  491. return -EINVAL;
  492. nh_pos = skb_network_header(skb) - skb->data;
  493. h_pos = skb_transport_header(skb) - skb->data;
  494. /* convert Ethernet header to proper 802.11 header (based on
  495. * operation mode) */
  496. ethertype = (skb->data[12] << 8) | skb->data[13];
  497. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  498. switch (iftype) {
  499. case NL80211_IFTYPE_AP:
  500. case NL80211_IFTYPE_AP_VLAN:
  501. case NL80211_IFTYPE_P2P_GO:
  502. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  503. /* DA BSSID SA */
  504. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  505. memcpy(hdr.addr2, addr, ETH_ALEN);
  506. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  507. hdrlen = 24;
  508. break;
  509. case NL80211_IFTYPE_STATION:
  510. case NL80211_IFTYPE_P2P_CLIENT:
  511. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  512. /* BSSID SA DA */
  513. memcpy(hdr.addr1, bssid, ETH_ALEN);
  514. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  515. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  516. hdrlen = 24;
  517. break;
  518. case NL80211_IFTYPE_OCB:
  519. case NL80211_IFTYPE_ADHOC:
  520. /* DA SA BSSID */
  521. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  522. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  523. memcpy(hdr.addr3, bssid, ETH_ALEN);
  524. hdrlen = 24;
  525. break;
  526. default:
  527. return -EOPNOTSUPP;
  528. }
  529. if (qos) {
  530. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  531. hdrlen += 2;
  532. }
  533. hdr.frame_control = fc;
  534. hdr.duration_id = 0;
  535. hdr.seq_ctrl = 0;
  536. skip_header_bytes = ETH_HLEN;
  537. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  538. encaps_data = bridge_tunnel_header;
  539. encaps_len = sizeof(bridge_tunnel_header);
  540. skip_header_bytes -= 2;
  541. } else if (ethertype >= ETH_P_802_3_MIN) {
  542. encaps_data = rfc1042_header;
  543. encaps_len = sizeof(rfc1042_header);
  544. skip_header_bytes -= 2;
  545. } else {
  546. encaps_data = NULL;
  547. encaps_len = 0;
  548. }
  549. skb_pull(skb, skip_header_bytes);
  550. nh_pos -= skip_header_bytes;
  551. h_pos -= skip_header_bytes;
  552. head_need = hdrlen + encaps_len - skb_headroom(skb);
  553. if (head_need > 0 || skb_cloned(skb)) {
  554. head_need = max(head_need, 0);
  555. if (head_need)
  556. skb_orphan(skb);
  557. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
  558. return -ENOMEM;
  559. skb->truesize += head_need;
  560. }
  561. if (encaps_data) {
  562. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  563. nh_pos += encaps_len;
  564. h_pos += encaps_len;
  565. }
  566. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  567. nh_pos += hdrlen;
  568. h_pos += hdrlen;
  569. /* Update skb pointers to various headers since this modified frame
  570. * is going to go through Linux networking code that may potentially
  571. * need things like pointer to IP header. */
  572. skb_reset_mac_header(skb);
  573. skb_set_network_header(skb, nh_pos);
  574. skb_set_transport_header(skb, h_pos);
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(ieee80211_data_from_8023);
  578. static void
  579. __frame_add_frag(struct sk_buff *skb, struct page *page,
  580. void *ptr, int len, int size)
  581. {
  582. struct skb_shared_info *sh = skb_shinfo(skb);
  583. int page_offset;
  584. page_ref_inc(page);
  585. page_offset = ptr - page_address(page);
  586. skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
  587. }
  588. static void
  589. __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
  590. int offset, int len)
  591. {
  592. struct skb_shared_info *sh = skb_shinfo(skb);
  593. const skb_frag_t *frag = &sh->frags[-1];
  594. struct page *frag_page;
  595. void *frag_ptr;
  596. int frag_len, frag_size;
  597. int head_size = skb->len - skb->data_len;
  598. int cur_len;
  599. frag_page = virt_to_head_page(skb->head);
  600. frag_ptr = skb->data;
  601. frag_size = head_size;
  602. while (offset >= frag_size) {
  603. offset -= frag_size;
  604. frag++;
  605. frag_page = skb_frag_page(frag);
  606. frag_ptr = skb_frag_address(frag);
  607. frag_size = skb_frag_size(frag);
  608. }
  609. frag_ptr += offset;
  610. frag_len = frag_size - offset;
  611. cur_len = min(len, frag_len);
  612. __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
  613. len -= cur_len;
  614. while (len > 0) {
  615. frag++;
  616. frag_len = skb_frag_size(frag);
  617. cur_len = min(len, frag_len);
  618. __frame_add_frag(frame, skb_frag_page(frag),
  619. skb_frag_address(frag), cur_len, frag_len);
  620. len -= cur_len;
  621. }
  622. }
  623. static struct sk_buff *
  624. __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
  625. int offset, int len, bool reuse_frag)
  626. {
  627. struct sk_buff *frame;
  628. int cur_len = len;
  629. if (skb->len - offset < len)
  630. return NULL;
  631. /*
  632. * When reusing framents, copy some data to the head to simplify
  633. * ethernet header handling and speed up protocol header processing
  634. * in the stack later.
  635. */
  636. if (reuse_frag)
  637. cur_len = min_t(int, len, 32);
  638. /*
  639. * Allocate and reserve two bytes more for payload
  640. * alignment since sizeof(struct ethhdr) is 14.
  641. */
  642. frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
  643. if (!frame)
  644. return NULL;
  645. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  646. skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
  647. len -= cur_len;
  648. if (!len)
  649. return frame;
  650. offset += cur_len;
  651. __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
  652. return frame;
  653. }
  654. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  655. const u8 *addr, enum nl80211_iftype iftype,
  656. const unsigned int extra_headroom,
  657. bool has_80211_header)
  658. {
  659. unsigned int hlen = ALIGN(extra_headroom, 4);
  660. struct sk_buff *frame = NULL;
  661. u16 ethertype;
  662. u8 *payload;
  663. int offset = 0, remaining, err;
  664. struct ethhdr eth;
  665. bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
  666. bool reuse_skb = false;
  667. bool last = false;
  668. if (has_80211_header) {
  669. err = __ieee80211_data_to_8023(skb, &eth, addr, iftype);
  670. if (err)
  671. goto out;
  672. }
  673. while (!last) {
  674. unsigned int subframe_len;
  675. int len;
  676. u8 padding;
  677. skb_copy_bits(skb, offset, &eth, sizeof(eth));
  678. len = ntohs(eth.h_proto);
  679. subframe_len = sizeof(struct ethhdr) + len;
  680. padding = (4 - subframe_len) & 0x3;
  681. /* the last MSDU has no padding */
  682. remaining = skb->len - offset;
  683. if (subframe_len > remaining)
  684. goto purge;
  685. offset += sizeof(struct ethhdr);
  686. /* reuse skb for the last subframe */
  687. last = remaining <= subframe_len + padding;
  688. if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
  689. skb_pull(skb, offset);
  690. frame = skb;
  691. reuse_skb = true;
  692. } else {
  693. frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
  694. reuse_frag);
  695. if (!frame)
  696. goto purge;
  697. offset += len + padding;
  698. }
  699. skb_reset_network_header(frame);
  700. frame->dev = skb->dev;
  701. frame->priority = skb->priority;
  702. payload = frame->data;
  703. ethertype = (payload[6] << 8) | payload[7];
  704. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  705. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  706. ether_addr_equal(payload, bridge_tunnel_header))) {
  707. eth.h_proto = htons(ethertype);
  708. skb_pull(frame, ETH_ALEN + 2);
  709. }
  710. memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
  711. __skb_queue_tail(list, frame);
  712. }
  713. if (!reuse_skb)
  714. dev_kfree_skb(skb);
  715. return;
  716. purge:
  717. __skb_queue_purge(list);
  718. out:
  719. dev_kfree_skb(skb);
  720. }
  721. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  722. /* Given a data frame determine the 802.1p/1d tag to use. */
  723. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  724. struct cfg80211_qos_map *qos_map)
  725. {
  726. unsigned int dscp;
  727. unsigned char vlan_priority;
  728. /* skb->priority values from 256->263 are magic values to
  729. * directly indicate a specific 802.1d priority. This is used
  730. * to allow 802.1d priority to be passed directly in from VLAN
  731. * tags, etc.
  732. */
  733. if (skb->priority >= 256 && skb->priority <= 263)
  734. return skb->priority - 256;
  735. if (skb_vlan_tag_present(skb)) {
  736. vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
  737. >> VLAN_PRIO_SHIFT;
  738. if (vlan_priority > 0)
  739. return vlan_priority;
  740. }
  741. switch (skb->protocol) {
  742. case htons(ETH_P_IP):
  743. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  744. break;
  745. case htons(ETH_P_IPV6):
  746. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  747. break;
  748. case htons(ETH_P_MPLS_UC):
  749. case htons(ETH_P_MPLS_MC): {
  750. struct mpls_label mpls_tmp, *mpls;
  751. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  752. sizeof(*mpls), &mpls_tmp);
  753. if (!mpls)
  754. return 0;
  755. return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  756. >> MPLS_LS_TC_SHIFT;
  757. }
  758. case htons(ETH_P_80221):
  759. /* 802.21 is always network control traffic */
  760. return 7;
  761. default:
  762. return 0;
  763. }
  764. if (qos_map) {
  765. unsigned int i, tmp_dscp = dscp >> 2;
  766. for (i = 0; i < qos_map->num_des; i++) {
  767. if (tmp_dscp == qos_map->dscp_exception[i].dscp)
  768. return qos_map->dscp_exception[i].up;
  769. }
  770. for (i = 0; i < 8; i++) {
  771. if (tmp_dscp >= qos_map->up[i].low &&
  772. tmp_dscp <= qos_map->up[i].high)
  773. return i;
  774. }
  775. }
  776. return dscp >> 5;
  777. }
  778. EXPORT_SYMBOL(cfg80211_classify8021d);
  779. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  780. {
  781. const struct cfg80211_bss_ies *ies;
  782. ies = rcu_dereference(bss->ies);
  783. if (!ies)
  784. return NULL;
  785. return cfg80211_find_ie(ie, ies->data, ies->len);
  786. }
  787. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  788. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  789. {
  790. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  791. struct net_device *dev = wdev->netdev;
  792. int i;
  793. if (!wdev->connect_keys)
  794. return;
  795. for (i = 0; i < 6; i++) {
  796. if (!wdev->connect_keys->params[i].cipher)
  797. continue;
  798. if (rdev_add_key(rdev, dev, i, false, NULL,
  799. &wdev->connect_keys->params[i])) {
  800. netdev_err(dev, "failed to set key %d\n", i);
  801. continue;
  802. }
  803. if (wdev->connect_keys->def == i)
  804. if (rdev_set_default_key(rdev, dev, i, true, true)) {
  805. netdev_err(dev, "failed to set defkey %d\n", i);
  806. continue;
  807. }
  808. if (wdev->connect_keys->defmgmt == i)
  809. if (rdev_set_default_mgmt_key(rdev, dev, i))
  810. netdev_err(dev, "failed to set mgtdef %d\n", i);
  811. }
  812. kzfree(wdev->connect_keys);
  813. wdev->connect_keys = NULL;
  814. }
  815. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  816. {
  817. struct cfg80211_event *ev;
  818. unsigned long flags;
  819. const u8 *bssid = NULL;
  820. spin_lock_irqsave(&wdev->event_lock, flags);
  821. while (!list_empty(&wdev->event_list)) {
  822. ev = list_first_entry(&wdev->event_list,
  823. struct cfg80211_event, list);
  824. list_del(&ev->list);
  825. spin_unlock_irqrestore(&wdev->event_lock, flags);
  826. wdev_lock(wdev);
  827. switch (ev->type) {
  828. case EVENT_CONNECT_RESULT:
  829. if (!is_zero_ether_addr(ev->cr.bssid))
  830. bssid = ev->cr.bssid;
  831. __cfg80211_connect_result(
  832. wdev->netdev, bssid,
  833. ev->cr.req_ie, ev->cr.req_ie_len,
  834. ev->cr.resp_ie, ev->cr.resp_ie_len,
  835. ev->cr.status,
  836. ev->cr.status == WLAN_STATUS_SUCCESS,
  837. ev->cr.bss);
  838. break;
  839. case EVENT_ROAMED:
  840. __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
  841. ev->rm.req_ie_len, ev->rm.resp_ie,
  842. ev->rm.resp_ie_len);
  843. break;
  844. case EVENT_DISCONNECTED:
  845. __cfg80211_disconnected(wdev->netdev,
  846. ev->dc.ie, ev->dc.ie_len,
  847. ev->dc.reason,
  848. !ev->dc.locally_generated);
  849. break;
  850. case EVENT_IBSS_JOINED:
  851. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  852. ev->ij.channel);
  853. break;
  854. case EVENT_STOPPED:
  855. __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
  856. break;
  857. }
  858. wdev_unlock(wdev);
  859. kfree(ev);
  860. spin_lock_irqsave(&wdev->event_lock, flags);
  861. }
  862. spin_unlock_irqrestore(&wdev->event_lock, flags);
  863. }
  864. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  865. {
  866. struct wireless_dev *wdev;
  867. ASSERT_RTNL();
  868. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
  869. cfg80211_process_wdev_events(wdev);
  870. }
  871. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  872. struct net_device *dev, enum nl80211_iftype ntype,
  873. u32 *flags, struct vif_params *params)
  874. {
  875. int err;
  876. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  877. ASSERT_RTNL();
  878. /* don't support changing VLANs, you just re-create them */
  879. if (otype == NL80211_IFTYPE_AP_VLAN)
  880. return -EOPNOTSUPP;
  881. /* cannot change into P2P device type */
  882. if (ntype == NL80211_IFTYPE_P2P_DEVICE)
  883. return -EOPNOTSUPP;
  884. if (!rdev->ops->change_virtual_intf ||
  885. !(rdev->wiphy.interface_modes & (1 << ntype)))
  886. return -EOPNOTSUPP;
  887. /* if it's part of a bridge, reject changing type to station/ibss */
  888. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  889. (ntype == NL80211_IFTYPE_ADHOC ||
  890. ntype == NL80211_IFTYPE_STATION ||
  891. ntype == NL80211_IFTYPE_P2P_CLIENT))
  892. return -EBUSY;
  893. if (ntype != otype) {
  894. dev->ieee80211_ptr->use_4addr = false;
  895. dev->ieee80211_ptr->mesh_id_up_len = 0;
  896. wdev_lock(dev->ieee80211_ptr);
  897. rdev_set_qos_map(rdev, dev, NULL);
  898. wdev_unlock(dev->ieee80211_ptr);
  899. switch (otype) {
  900. case NL80211_IFTYPE_AP:
  901. cfg80211_stop_ap(rdev, dev, true);
  902. break;
  903. case NL80211_IFTYPE_ADHOC:
  904. cfg80211_leave_ibss(rdev, dev, false);
  905. break;
  906. case NL80211_IFTYPE_STATION:
  907. case NL80211_IFTYPE_P2P_CLIENT:
  908. wdev_lock(dev->ieee80211_ptr);
  909. cfg80211_disconnect(rdev, dev,
  910. WLAN_REASON_DEAUTH_LEAVING, true);
  911. wdev_unlock(dev->ieee80211_ptr);
  912. break;
  913. case NL80211_IFTYPE_MESH_POINT:
  914. /* mesh should be handled? */
  915. break;
  916. default:
  917. break;
  918. }
  919. cfg80211_process_rdev_events(rdev);
  920. }
  921. err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
  922. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  923. if (!err && params && params->use_4addr != -1)
  924. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  925. if (!err) {
  926. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  927. switch (ntype) {
  928. case NL80211_IFTYPE_STATION:
  929. if (dev->ieee80211_ptr->use_4addr)
  930. break;
  931. /* fall through */
  932. case NL80211_IFTYPE_OCB:
  933. case NL80211_IFTYPE_P2P_CLIENT:
  934. case NL80211_IFTYPE_ADHOC:
  935. dev->priv_flags |= IFF_DONT_BRIDGE;
  936. break;
  937. case NL80211_IFTYPE_P2P_GO:
  938. case NL80211_IFTYPE_AP:
  939. case NL80211_IFTYPE_AP_VLAN:
  940. case NL80211_IFTYPE_WDS:
  941. case NL80211_IFTYPE_MESH_POINT:
  942. /* bridging OK */
  943. break;
  944. case NL80211_IFTYPE_MONITOR:
  945. /* monitor can't bridge anyway */
  946. break;
  947. case NL80211_IFTYPE_UNSPECIFIED:
  948. case NUM_NL80211_IFTYPES:
  949. /* not happening */
  950. break;
  951. case NL80211_IFTYPE_P2P_DEVICE:
  952. WARN_ON(1);
  953. break;
  954. }
  955. }
  956. if (!err && ntype != otype && netif_running(dev)) {
  957. cfg80211_update_iface_num(rdev, ntype, 1);
  958. cfg80211_update_iface_num(rdev, otype, -1);
  959. }
  960. return err;
  961. }
  962. static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
  963. {
  964. static const u32 __mcs2bitrate[] = {
  965. /* control PHY */
  966. [0] = 275,
  967. /* SC PHY */
  968. [1] = 3850,
  969. [2] = 7700,
  970. [3] = 9625,
  971. [4] = 11550,
  972. [5] = 12512, /* 1251.25 mbps */
  973. [6] = 15400,
  974. [7] = 19250,
  975. [8] = 23100,
  976. [9] = 25025,
  977. [10] = 30800,
  978. [11] = 38500,
  979. [12] = 46200,
  980. /* OFDM PHY */
  981. [13] = 6930,
  982. [14] = 8662, /* 866.25 mbps */
  983. [15] = 13860,
  984. [16] = 17325,
  985. [17] = 20790,
  986. [18] = 27720,
  987. [19] = 34650,
  988. [20] = 41580,
  989. [21] = 45045,
  990. [22] = 51975,
  991. [23] = 62370,
  992. [24] = 67568, /* 6756.75 mbps */
  993. /* LP-SC PHY */
  994. [25] = 6260,
  995. [26] = 8340,
  996. [27] = 11120,
  997. [28] = 12510,
  998. [29] = 16680,
  999. [30] = 22240,
  1000. [31] = 25030,
  1001. };
  1002. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  1003. return 0;
  1004. return __mcs2bitrate[rate->mcs];
  1005. }
  1006. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  1007. {
  1008. static const u32 base[4][10] = {
  1009. { 6500000,
  1010. 13000000,
  1011. 19500000,
  1012. 26000000,
  1013. 39000000,
  1014. 52000000,
  1015. 58500000,
  1016. 65000000,
  1017. 78000000,
  1018. 0,
  1019. },
  1020. { 13500000,
  1021. 27000000,
  1022. 40500000,
  1023. 54000000,
  1024. 81000000,
  1025. 108000000,
  1026. 121500000,
  1027. 135000000,
  1028. 162000000,
  1029. 180000000,
  1030. },
  1031. { 29300000,
  1032. 58500000,
  1033. 87800000,
  1034. 117000000,
  1035. 175500000,
  1036. 234000000,
  1037. 263300000,
  1038. 292500000,
  1039. 351000000,
  1040. 390000000,
  1041. },
  1042. { 58500000,
  1043. 117000000,
  1044. 175500000,
  1045. 234000000,
  1046. 351000000,
  1047. 468000000,
  1048. 526500000,
  1049. 585000000,
  1050. 702000000,
  1051. 780000000,
  1052. },
  1053. };
  1054. u32 bitrate;
  1055. int idx;
  1056. if (WARN_ON_ONCE(rate->mcs > 9))
  1057. return 0;
  1058. switch (rate->bw) {
  1059. case RATE_INFO_BW_160:
  1060. idx = 3;
  1061. break;
  1062. case RATE_INFO_BW_80:
  1063. idx = 2;
  1064. break;
  1065. case RATE_INFO_BW_40:
  1066. idx = 1;
  1067. break;
  1068. case RATE_INFO_BW_5:
  1069. case RATE_INFO_BW_10:
  1070. default:
  1071. WARN_ON(1);
  1072. /* fall through */
  1073. case RATE_INFO_BW_20:
  1074. idx = 0;
  1075. }
  1076. bitrate = base[idx][rate->mcs];
  1077. bitrate *= rate->nss;
  1078. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1079. bitrate = (bitrate / 9) * 10;
  1080. /* do NOT round down here */
  1081. return (bitrate + 50000) / 100000;
  1082. }
  1083. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  1084. {
  1085. int modulation, streams, bitrate;
  1086. if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
  1087. !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
  1088. return rate->legacy;
  1089. if (rate->flags & RATE_INFO_FLAGS_60G)
  1090. return cfg80211_calculate_bitrate_60g(rate);
  1091. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  1092. return cfg80211_calculate_bitrate_vht(rate);
  1093. /* the formula below does only work for MCS values smaller than 32 */
  1094. if (WARN_ON_ONCE(rate->mcs >= 32))
  1095. return 0;
  1096. modulation = rate->mcs & 7;
  1097. streams = (rate->mcs >> 3) + 1;
  1098. bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
  1099. if (modulation < 4)
  1100. bitrate *= (modulation + 1);
  1101. else if (modulation == 4)
  1102. bitrate *= (modulation + 2);
  1103. else
  1104. bitrate *= (modulation + 3);
  1105. bitrate *= streams;
  1106. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1107. bitrate = (bitrate / 9) * 10;
  1108. /* do NOT round down here */
  1109. return (bitrate + 50000) / 100000;
  1110. }
  1111. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  1112. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  1113. enum ieee80211_p2p_attr_id attr,
  1114. u8 *buf, unsigned int bufsize)
  1115. {
  1116. u8 *out = buf;
  1117. u16 attr_remaining = 0;
  1118. bool desired_attr = false;
  1119. u16 desired_len = 0;
  1120. while (len > 0) {
  1121. unsigned int iedatalen;
  1122. unsigned int copy;
  1123. const u8 *iedata;
  1124. if (len < 2)
  1125. return -EILSEQ;
  1126. iedatalen = ies[1];
  1127. if (iedatalen + 2 > len)
  1128. return -EILSEQ;
  1129. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1130. goto cont;
  1131. if (iedatalen < 4)
  1132. goto cont;
  1133. iedata = ies + 2;
  1134. /* check WFA OUI, P2P subtype */
  1135. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1136. iedata[2] != 0x9a || iedata[3] != 0x09)
  1137. goto cont;
  1138. iedatalen -= 4;
  1139. iedata += 4;
  1140. /* check attribute continuation into this IE */
  1141. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1142. if (copy && desired_attr) {
  1143. desired_len += copy;
  1144. if (out) {
  1145. memcpy(out, iedata, min(bufsize, copy));
  1146. out += min(bufsize, copy);
  1147. bufsize -= min(bufsize, copy);
  1148. }
  1149. if (copy == attr_remaining)
  1150. return desired_len;
  1151. }
  1152. attr_remaining -= copy;
  1153. if (attr_remaining)
  1154. goto cont;
  1155. iedatalen -= copy;
  1156. iedata += copy;
  1157. while (iedatalen > 0) {
  1158. u16 attr_len;
  1159. /* P2P attribute ID & size must fit */
  1160. if (iedatalen < 3)
  1161. return -EILSEQ;
  1162. desired_attr = iedata[0] == attr;
  1163. attr_len = get_unaligned_le16(iedata + 1);
  1164. iedatalen -= 3;
  1165. iedata += 3;
  1166. copy = min_t(unsigned int, attr_len, iedatalen);
  1167. if (desired_attr) {
  1168. desired_len += copy;
  1169. if (out) {
  1170. memcpy(out, iedata, min(bufsize, copy));
  1171. out += min(bufsize, copy);
  1172. bufsize -= min(bufsize, copy);
  1173. }
  1174. if (copy == attr_len)
  1175. return desired_len;
  1176. }
  1177. iedata += copy;
  1178. iedatalen -= copy;
  1179. attr_remaining = attr_len - copy;
  1180. }
  1181. cont:
  1182. len -= ies[1] + 2;
  1183. ies += ies[1] + 2;
  1184. }
  1185. if (attr_remaining && desired_attr)
  1186. return -EILSEQ;
  1187. return -ENOENT;
  1188. }
  1189. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1190. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1191. {
  1192. int i;
  1193. for (i = 0; i < n_ids; i++)
  1194. if (ids[i] == id)
  1195. return true;
  1196. return false;
  1197. }
  1198. size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
  1199. const u8 *ids, int n_ids,
  1200. const u8 *after_ric, int n_after_ric,
  1201. size_t offset)
  1202. {
  1203. size_t pos = offset;
  1204. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
  1205. if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
  1206. pos += 2 + ies[pos + 1];
  1207. while (pos < ielen &&
  1208. !ieee80211_id_in_list(after_ric, n_after_ric,
  1209. ies[pos]))
  1210. pos += 2 + ies[pos + 1];
  1211. } else {
  1212. pos += 2 + ies[pos + 1];
  1213. }
  1214. }
  1215. return pos;
  1216. }
  1217. EXPORT_SYMBOL(ieee80211_ie_split_ric);
  1218. bool ieee80211_operating_class_to_band(u8 operating_class,
  1219. enum nl80211_band *band)
  1220. {
  1221. switch (operating_class) {
  1222. case 112:
  1223. case 115 ... 127:
  1224. case 128 ... 130:
  1225. *band = NL80211_BAND_5GHZ;
  1226. return true;
  1227. case 81:
  1228. case 82:
  1229. case 83:
  1230. case 84:
  1231. *band = NL80211_BAND_2GHZ;
  1232. return true;
  1233. case 180:
  1234. *band = NL80211_BAND_60GHZ;
  1235. return true;
  1236. }
  1237. return false;
  1238. }
  1239. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1240. bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
  1241. u8 *op_class)
  1242. {
  1243. u8 vht_opclass;
  1244. u16 freq = chandef->center_freq1;
  1245. if (freq >= 2412 && freq <= 2472) {
  1246. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1247. return false;
  1248. /* 2.407 GHz, channels 1..13 */
  1249. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1250. if (freq > chandef->chan->center_freq)
  1251. *op_class = 83; /* HT40+ */
  1252. else
  1253. *op_class = 84; /* HT40- */
  1254. } else {
  1255. *op_class = 81;
  1256. }
  1257. return true;
  1258. }
  1259. if (freq == 2484) {
  1260. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1261. return false;
  1262. *op_class = 82; /* channel 14 */
  1263. return true;
  1264. }
  1265. switch (chandef->width) {
  1266. case NL80211_CHAN_WIDTH_80:
  1267. vht_opclass = 128;
  1268. break;
  1269. case NL80211_CHAN_WIDTH_160:
  1270. vht_opclass = 129;
  1271. break;
  1272. case NL80211_CHAN_WIDTH_80P80:
  1273. vht_opclass = 130;
  1274. break;
  1275. case NL80211_CHAN_WIDTH_10:
  1276. case NL80211_CHAN_WIDTH_5:
  1277. return false; /* unsupported for now */
  1278. default:
  1279. vht_opclass = 0;
  1280. break;
  1281. }
  1282. /* 5 GHz, channels 36..48 */
  1283. if (freq >= 5180 && freq <= 5240) {
  1284. if (vht_opclass) {
  1285. *op_class = vht_opclass;
  1286. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1287. if (freq > chandef->chan->center_freq)
  1288. *op_class = 116;
  1289. else
  1290. *op_class = 117;
  1291. } else {
  1292. *op_class = 115;
  1293. }
  1294. return true;
  1295. }
  1296. /* 5 GHz, channels 52..64 */
  1297. if (freq >= 5260 && freq <= 5320) {
  1298. if (vht_opclass) {
  1299. *op_class = vht_opclass;
  1300. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1301. if (freq > chandef->chan->center_freq)
  1302. *op_class = 119;
  1303. else
  1304. *op_class = 120;
  1305. } else {
  1306. *op_class = 118;
  1307. }
  1308. return true;
  1309. }
  1310. /* 5 GHz, channels 100..144 */
  1311. if (freq >= 5500 && freq <= 5720) {
  1312. if (vht_opclass) {
  1313. *op_class = vht_opclass;
  1314. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1315. if (freq > chandef->chan->center_freq)
  1316. *op_class = 122;
  1317. else
  1318. *op_class = 123;
  1319. } else {
  1320. *op_class = 121;
  1321. }
  1322. return true;
  1323. }
  1324. /* 5 GHz, channels 149..169 */
  1325. if (freq >= 5745 && freq <= 5845) {
  1326. if (vht_opclass) {
  1327. *op_class = vht_opclass;
  1328. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1329. if (freq > chandef->chan->center_freq)
  1330. *op_class = 126;
  1331. else
  1332. *op_class = 127;
  1333. } else if (freq <= 5805) {
  1334. *op_class = 124;
  1335. } else {
  1336. *op_class = 125;
  1337. }
  1338. return true;
  1339. }
  1340. /* 56.16 GHz, channel 1..4 */
  1341. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  1342. if (chandef->width >= NL80211_CHAN_WIDTH_40)
  1343. return false;
  1344. *op_class = 180;
  1345. return true;
  1346. }
  1347. /* not supported yet */
  1348. return false;
  1349. }
  1350. EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
  1351. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  1352. u32 beacon_int)
  1353. {
  1354. struct wireless_dev *wdev;
  1355. int res = 0;
  1356. if (!beacon_int)
  1357. return -EINVAL;
  1358. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  1359. if (!wdev->beacon_interval)
  1360. continue;
  1361. if (wdev->beacon_interval != beacon_int) {
  1362. res = -EINVAL;
  1363. break;
  1364. }
  1365. }
  1366. return res;
  1367. }
  1368. int cfg80211_iter_combinations(struct wiphy *wiphy,
  1369. const int num_different_channels,
  1370. const u8 radar_detect,
  1371. const int iftype_num[NUM_NL80211_IFTYPES],
  1372. void (*iter)(const struct ieee80211_iface_combination *c,
  1373. void *data),
  1374. void *data)
  1375. {
  1376. const struct ieee80211_regdomain *regdom;
  1377. enum nl80211_dfs_regions region = 0;
  1378. int i, j, iftype;
  1379. int num_interfaces = 0;
  1380. u32 used_iftypes = 0;
  1381. if (radar_detect) {
  1382. rcu_read_lock();
  1383. regdom = rcu_dereference(cfg80211_regdomain);
  1384. if (regdom)
  1385. region = regdom->dfs_region;
  1386. rcu_read_unlock();
  1387. }
  1388. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1389. num_interfaces += iftype_num[iftype];
  1390. if (iftype_num[iftype] > 0 &&
  1391. !(wiphy->software_iftypes & BIT(iftype)))
  1392. used_iftypes |= BIT(iftype);
  1393. }
  1394. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  1395. const struct ieee80211_iface_combination *c;
  1396. struct ieee80211_iface_limit *limits;
  1397. u32 all_iftypes = 0;
  1398. c = &wiphy->iface_combinations[i];
  1399. if (num_interfaces > c->max_interfaces)
  1400. continue;
  1401. if (num_different_channels > c->num_different_channels)
  1402. continue;
  1403. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  1404. GFP_KERNEL);
  1405. if (!limits)
  1406. return -ENOMEM;
  1407. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1408. if (wiphy->software_iftypes & BIT(iftype))
  1409. continue;
  1410. for (j = 0; j < c->n_limits; j++) {
  1411. all_iftypes |= limits[j].types;
  1412. if (!(limits[j].types & BIT(iftype)))
  1413. continue;
  1414. if (limits[j].max < iftype_num[iftype])
  1415. goto cont;
  1416. limits[j].max -= iftype_num[iftype];
  1417. }
  1418. }
  1419. if (radar_detect != (c->radar_detect_widths & radar_detect))
  1420. goto cont;
  1421. if (radar_detect && c->radar_detect_regions &&
  1422. !(c->radar_detect_regions & BIT(region)))
  1423. goto cont;
  1424. /* Finally check that all iftypes that we're currently
  1425. * using are actually part of this combination. If they
  1426. * aren't then we can't use this combination and have
  1427. * to continue to the next.
  1428. */
  1429. if ((all_iftypes & used_iftypes) != used_iftypes)
  1430. goto cont;
  1431. /* This combination covered all interface types and
  1432. * supported the requested numbers, so we're good.
  1433. */
  1434. (*iter)(c, data);
  1435. cont:
  1436. kfree(limits);
  1437. }
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL(cfg80211_iter_combinations);
  1441. static void
  1442. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  1443. void *data)
  1444. {
  1445. int *num = data;
  1446. (*num)++;
  1447. }
  1448. int cfg80211_check_combinations(struct wiphy *wiphy,
  1449. const int num_different_channels,
  1450. const u8 radar_detect,
  1451. const int iftype_num[NUM_NL80211_IFTYPES])
  1452. {
  1453. int err, num = 0;
  1454. err = cfg80211_iter_combinations(wiphy, num_different_channels,
  1455. radar_detect, iftype_num,
  1456. cfg80211_iter_sum_ifcombs, &num);
  1457. if (err)
  1458. return err;
  1459. if (num == 0)
  1460. return -EBUSY;
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL(cfg80211_check_combinations);
  1464. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  1465. const u8 *rates, unsigned int n_rates,
  1466. u32 *mask)
  1467. {
  1468. int i, j;
  1469. if (!sband)
  1470. return -EINVAL;
  1471. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  1472. return -EINVAL;
  1473. *mask = 0;
  1474. for (i = 0; i < n_rates; i++) {
  1475. int rate = (rates[i] & 0x7f) * 5;
  1476. bool found = false;
  1477. for (j = 0; j < sband->n_bitrates; j++) {
  1478. if (sband->bitrates[j].bitrate == rate) {
  1479. found = true;
  1480. *mask |= BIT(j);
  1481. break;
  1482. }
  1483. }
  1484. if (!found)
  1485. return -EINVAL;
  1486. }
  1487. /*
  1488. * mask must have at least one bit set here since we
  1489. * didn't accept a 0-length rates array nor allowed
  1490. * entries in the array that didn't exist
  1491. */
  1492. return 0;
  1493. }
  1494. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  1495. {
  1496. enum nl80211_band band;
  1497. unsigned int n_channels = 0;
  1498. for (band = 0; band < NUM_NL80211_BANDS; band++)
  1499. if (wiphy->bands[band])
  1500. n_channels += wiphy->bands[band]->n_channels;
  1501. return n_channels;
  1502. }
  1503. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  1504. int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
  1505. struct station_info *sinfo)
  1506. {
  1507. struct cfg80211_registered_device *rdev;
  1508. struct wireless_dev *wdev;
  1509. wdev = dev->ieee80211_ptr;
  1510. if (!wdev)
  1511. return -EOPNOTSUPP;
  1512. rdev = wiphy_to_rdev(wdev->wiphy);
  1513. if (!rdev->ops->get_station)
  1514. return -EOPNOTSUPP;
  1515. return rdev_get_station(rdev, dev, mac_addr, sinfo);
  1516. }
  1517. EXPORT_SYMBOL(cfg80211_get_station);
  1518. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  1519. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  1520. const unsigned char rfc1042_header[] __aligned(2) =
  1521. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1522. EXPORT_SYMBOL(rfc1042_header);
  1523. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  1524. const unsigned char bridge_tunnel_header[] __aligned(2) =
  1525. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  1526. EXPORT_SYMBOL(bridge_tunnel_header);