util.c 46 KB

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