util.c 42 KB

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