util.c 44 KB

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