util.c 43 KB

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