util.c 38 KB

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