util.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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, u8 *bssid, bool qos)
  434. {
  435. struct ieee80211_hdr hdr;
  436. u16 hdrlen, ethertype;
  437. __le16 fc;
  438. const u8 *encaps_data;
  439. int encaps_len, skip_header_bytes;
  440. int nh_pos, h_pos;
  441. int head_need;
  442. if (unlikely(skb->len < ETH_HLEN))
  443. return -EINVAL;
  444. nh_pos = skb_network_header(skb) - skb->data;
  445. h_pos = skb_transport_header(skb) - skb->data;
  446. /* convert Ethernet header to proper 802.11 header (based on
  447. * operation mode) */
  448. ethertype = (skb->data[12] << 8) | skb->data[13];
  449. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  450. switch (iftype) {
  451. case NL80211_IFTYPE_AP:
  452. case NL80211_IFTYPE_AP_VLAN:
  453. case NL80211_IFTYPE_P2P_GO:
  454. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  455. /* DA BSSID SA */
  456. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  457. memcpy(hdr.addr2, addr, ETH_ALEN);
  458. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  459. hdrlen = 24;
  460. break;
  461. case NL80211_IFTYPE_STATION:
  462. case NL80211_IFTYPE_P2P_CLIENT:
  463. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  464. /* BSSID SA DA */
  465. memcpy(hdr.addr1, bssid, ETH_ALEN);
  466. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  467. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  468. hdrlen = 24;
  469. break;
  470. case NL80211_IFTYPE_ADHOC:
  471. /* DA SA BSSID */
  472. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  473. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  474. memcpy(hdr.addr3, bssid, ETH_ALEN);
  475. hdrlen = 24;
  476. break;
  477. default:
  478. return -EOPNOTSUPP;
  479. }
  480. if (qos) {
  481. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  482. hdrlen += 2;
  483. }
  484. hdr.frame_control = fc;
  485. hdr.duration_id = 0;
  486. hdr.seq_ctrl = 0;
  487. skip_header_bytes = ETH_HLEN;
  488. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  489. encaps_data = bridge_tunnel_header;
  490. encaps_len = sizeof(bridge_tunnel_header);
  491. skip_header_bytes -= 2;
  492. } else if (ethertype >= ETH_P_802_3_MIN) {
  493. encaps_data = rfc1042_header;
  494. encaps_len = sizeof(rfc1042_header);
  495. skip_header_bytes -= 2;
  496. } else {
  497. encaps_data = NULL;
  498. encaps_len = 0;
  499. }
  500. skb_pull(skb, skip_header_bytes);
  501. nh_pos -= skip_header_bytes;
  502. h_pos -= skip_header_bytes;
  503. head_need = hdrlen + encaps_len - skb_headroom(skb);
  504. if (head_need > 0 || skb_cloned(skb)) {
  505. head_need = max(head_need, 0);
  506. if (head_need)
  507. skb_orphan(skb);
  508. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
  509. return -ENOMEM;
  510. skb->truesize += head_need;
  511. }
  512. if (encaps_data) {
  513. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  514. nh_pos += encaps_len;
  515. h_pos += encaps_len;
  516. }
  517. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  518. nh_pos += hdrlen;
  519. h_pos += hdrlen;
  520. /* Update skb pointers to various headers since this modified frame
  521. * is going to go through Linux networking code that may potentially
  522. * need things like pointer to IP header. */
  523. skb_set_mac_header(skb, 0);
  524. skb_set_network_header(skb, nh_pos);
  525. skb_set_transport_header(skb, h_pos);
  526. return 0;
  527. }
  528. EXPORT_SYMBOL(ieee80211_data_from_8023);
  529. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  530. const u8 *addr, enum nl80211_iftype iftype,
  531. const unsigned int extra_headroom,
  532. bool has_80211_header)
  533. {
  534. struct sk_buff *frame = NULL;
  535. u16 ethertype;
  536. u8 *payload;
  537. const struct ethhdr *eth;
  538. int remaining, err;
  539. u8 dst[ETH_ALEN], src[ETH_ALEN];
  540. if (has_80211_header) {
  541. err = ieee80211_data_to_8023(skb, addr, iftype);
  542. if (err)
  543. goto out;
  544. /* skip the wrapping header */
  545. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  546. if (!eth)
  547. goto out;
  548. } else {
  549. eth = (struct ethhdr *) skb->data;
  550. }
  551. while (skb != frame) {
  552. u8 padding;
  553. __be16 len = eth->h_proto;
  554. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  555. remaining = skb->len;
  556. memcpy(dst, eth->h_dest, ETH_ALEN);
  557. memcpy(src, eth->h_source, ETH_ALEN);
  558. padding = (4 - subframe_len) & 0x3;
  559. /* the last MSDU has no padding */
  560. if (subframe_len > remaining)
  561. goto purge;
  562. skb_pull(skb, sizeof(struct ethhdr));
  563. /* reuse skb for the last subframe */
  564. if (remaining <= subframe_len + padding)
  565. frame = skb;
  566. else {
  567. unsigned int hlen = ALIGN(extra_headroom, 4);
  568. /*
  569. * Allocate and reserve two bytes more for payload
  570. * alignment since sizeof(struct ethhdr) is 14.
  571. */
  572. frame = dev_alloc_skb(hlen + subframe_len + 2);
  573. if (!frame)
  574. goto purge;
  575. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  576. memcpy(skb_put(frame, ntohs(len)), skb->data,
  577. ntohs(len));
  578. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  579. padding);
  580. if (!eth) {
  581. dev_kfree_skb(frame);
  582. goto purge;
  583. }
  584. }
  585. skb_reset_network_header(frame);
  586. frame->dev = skb->dev;
  587. frame->priority = skb->priority;
  588. payload = frame->data;
  589. ethertype = (payload[6] << 8) | payload[7];
  590. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  591. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  592. ether_addr_equal(payload, bridge_tunnel_header))) {
  593. /* remove RFC1042 or Bridge-Tunnel
  594. * encapsulation and replace EtherType */
  595. skb_pull(frame, 6);
  596. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  597. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  598. } else {
  599. memcpy(skb_push(frame, sizeof(__be16)), &len,
  600. sizeof(__be16));
  601. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  602. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  603. }
  604. __skb_queue_tail(list, frame);
  605. }
  606. return;
  607. purge:
  608. __skb_queue_purge(list);
  609. out:
  610. dev_kfree_skb(skb);
  611. }
  612. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  613. /* Given a data frame determine the 802.1p/1d tag to use. */
  614. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  615. struct cfg80211_qos_map *qos_map)
  616. {
  617. unsigned int dscp;
  618. unsigned char vlan_priority;
  619. /* skb->priority values from 256->263 are magic values to
  620. * directly indicate a specific 802.1d priority. This is used
  621. * to allow 802.1d priority to be passed directly in from VLAN
  622. * tags, etc.
  623. */
  624. if (skb->priority >= 256 && skb->priority <= 263)
  625. return skb->priority - 256;
  626. if (vlan_tx_tag_present(skb)) {
  627. vlan_priority = (vlan_tx_tag_get(skb) & VLAN_PRIO_MASK)
  628. >> VLAN_PRIO_SHIFT;
  629. if (vlan_priority > 0)
  630. return vlan_priority;
  631. }
  632. switch (skb->protocol) {
  633. case htons(ETH_P_IP):
  634. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  635. break;
  636. case htons(ETH_P_IPV6):
  637. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  638. break;
  639. case htons(ETH_P_MPLS_UC):
  640. case htons(ETH_P_MPLS_MC): {
  641. struct mpls_label mpls_tmp, *mpls;
  642. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  643. sizeof(*mpls), &mpls_tmp);
  644. if (!mpls)
  645. return 0;
  646. return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  647. >> MPLS_LS_TC_SHIFT;
  648. }
  649. case htons(ETH_P_80221):
  650. /* 802.21 is always network control traffic */
  651. return 7;
  652. default:
  653. return 0;
  654. }
  655. if (qos_map) {
  656. unsigned int i, tmp_dscp = dscp >> 2;
  657. for (i = 0; i < qos_map->num_des; i++) {
  658. if (tmp_dscp == qos_map->dscp_exception[i].dscp)
  659. return qos_map->dscp_exception[i].up;
  660. }
  661. for (i = 0; i < 8; i++) {
  662. if (tmp_dscp >= qos_map->up[i].low &&
  663. tmp_dscp <= qos_map->up[i].high)
  664. return i;
  665. }
  666. }
  667. return dscp >> 5;
  668. }
  669. EXPORT_SYMBOL(cfg80211_classify8021d);
  670. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  671. {
  672. const struct cfg80211_bss_ies *ies;
  673. ies = rcu_dereference(bss->ies);
  674. if (!ies)
  675. return NULL;
  676. return cfg80211_find_ie(ie, ies->data, ies->len);
  677. }
  678. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  679. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  680. {
  681. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  682. struct net_device *dev = wdev->netdev;
  683. int i;
  684. if (!wdev->connect_keys)
  685. return;
  686. for (i = 0; i < 6; i++) {
  687. if (!wdev->connect_keys->params[i].cipher)
  688. continue;
  689. if (rdev_add_key(rdev, dev, i, false, NULL,
  690. &wdev->connect_keys->params[i])) {
  691. netdev_err(dev, "failed to set key %d\n", i);
  692. continue;
  693. }
  694. if (wdev->connect_keys->def == i)
  695. if (rdev_set_default_key(rdev, dev, i, true, true)) {
  696. netdev_err(dev, "failed to set defkey %d\n", i);
  697. continue;
  698. }
  699. if (wdev->connect_keys->defmgmt == i)
  700. if (rdev_set_default_mgmt_key(rdev, dev, i))
  701. netdev_err(dev, "failed to set mgtdef %d\n", i);
  702. }
  703. kfree(wdev->connect_keys);
  704. wdev->connect_keys = NULL;
  705. }
  706. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  707. {
  708. struct cfg80211_event *ev;
  709. unsigned long flags;
  710. const u8 *bssid = NULL;
  711. spin_lock_irqsave(&wdev->event_lock, flags);
  712. while (!list_empty(&wdev->event_list)) {
  713. ev = list_first_entry(&wdev->event_list,
  714. struct cfg80211_event, list);
  715. list_del(&ev->list);
  716. spin_unlock_irqrestore(&wdev->event_lock, flags);
  717. wdev_lock(wdev);
  718. switch (ev->type) {
  719. case EVENT_CONNECT_RESULT:
  720. if (!is_zero_ether_addr(ev->cr.bssid))
  721. bssid = ev->cr.bssid;
  722. __cfg80211_connect_result(
  723. wdev->netdev, bssid,
  724. ev->cr.req_ie, ev->cr.req_ie_len,
  725. ev->cr.resp_ie, ev->cr.resp_ie_len,
  726. ev->cr.status,
  727. ev->cr.status == WLAN_STATUS_SUCCESS,
  728. NULL);
  729. break;
  730. case EVENT_ROAMED:
  731. __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
  732. ev->rm.req_ie_len, ev->rm.resp_ie,
  733. ev->rm.resp_ie_len);
  734. break;
  735. case EVENT_DISCONNECTED:
  736. __cfg80211_disconnected(wdev->netdev,
  737. ev->dc.ie, ev->dc.ie_len,
  738. ev->dc.reason, true);
  739. break;
  740. case EVENT_IBSS_JOINED:
  741. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  742. ev->ij.channel);
  743. break;
  744. }
  745. wdev_unlock(wdev);
  746. kfree(ev);
  747. spin_lock_irqsave(&wdev->event_lock, flags);
  748. }
  749. spin_unlock_irqrestore(&wdev->event_lock, flags);
  750. }
  751. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  752. {
  753. struct wireless_dev *wdev;
  754. ASSERT_RTNL();
  755. list_for_each_entry(wdev, &rdev->wdev_list, list)
  756. cfg80211_process_wdev_events(wdev);
  757. }
  758. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  759. struct net_device *dev, enum nl80211_iftype ntype,
  760. u32 *flags, struct vif_params *params)
  761. {
  762. int err;
  763. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  764. ASSERT_RTNL();
  765. /* don't support changing VLANs, you just re-create them */
  766. if (otype == NL80211_IFTYPE_AP_VLAN)
  767. return -EOPNOTSUPP;
  768. /* cannot change into P2P device type */
  769. if (ntype == NL80211_IFTYPE_P2P_DEVICE)
  770. return -EOPNOTSUPP;
  771. if (!rdev->ops->change_virtual_intf ||
  772. !(rdev->wiphy.interface_modes & (1 << ntype)))
  773. return -EOPNOTSUPP;
  774. /* if it's part of a bridge, reject changing type to station/ibss */
  775. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  776. (ntype == NL80211_IFTYPE_ADHOC ||
  777. ntype == NL80211_IFTYPE_STATION ||
  778. ntype == NL80211_IFTYPE_P2P_CLIENT))
  779. return -EBUSY;
  780. if (ntype != otype && netif_running(dev)) {
  781. dev->ieee80211_ptr->use_4addr = false;
  782. dev->ieee80211_ptr->mesh_id_up_len = 0;
  783. wdev_lock(dev->ieee80211_ptr);
  784. rdev_set_qos_map(rdev, dev, NULL);
  785. wdev_unlock(dev->ieee80211_ptr);
  786. switch (otype) {
  787. case NL80211_IFTYPE_AP:
  788. cfg80211_stop_ap(rdev, dev, true);
  789. break;
  790. case NL80211_IFTYPE_ADHOC:
  791. cfg80211_leave_ibss(rdev, dev, false);
  792. break;
  793. case NL80211_IFTYPE_STATION:
  794. case NL80211_IFTYPE_P2P_CLIENT:
  795. wdev_lock(dev->ieee80211_ptr);
  796. cfg80211_disconnect(rdev, dev,
  797. WLAN_REASON_DEAUTH_LEAVING, true);
  798. wdev_unlock(dev->ieee80211_ptr);
  799. break;
  800. case NL80211_IFTYPE_MESH_POINT:
  801. /* mesh should be handled? */
  802. break;
  803. default:
  804. break;
  805. }
  806. cfg80211_process_rdev_events(rdev);
  807. }
  808. err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
  809. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  810. if (!err && params && params->use_4addr != -1)
  811. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  812. if (!err) {
  813. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  814. switch (ntype) {
  815. case NL80211_IFTYPE_STATION:
  816. if (dev->ieee80211_ptr->use_4addr)
  817. break;
  818. /* fall through */
  819. case NL80211_IFTYPE_P2P_CLIENT:
  820. case NL80211_IFTYPE_ADHOC:
  821. dev->priv_flags |= IFF_DONT_BRIDGE;
  822. break;
  823. case NL80211_IFTYPE_P2P_GO:
  824. case NL80211_IFTYPE_AP:
  825. case NL80211_IFTYPE_AP_VLAN:
  826. case NL80211_IFTYPE_WDS:
  827. case NL80211_IFTYPE_MESH_POINT:
  828. /* bridging OK */
  829. break;
  830. case NL80211_IFTYPE_MONITOR:
  831. /* monitor can't bridge anyway */
  832. break;
  833. case NL80211_IFTYPE_UNSPECIFIED:
  834. case NUM_NL80211_IFTYPES:
  835. /* not happening */
  836. break;
  837. case NL80211_IFTYPE_P2P_DEVICE:
  838. WARN_ON(1);
  839. break;
  840. }
  841. }
  842. if (!err && ntype != otype && netif_running(dev)) {
  843. cfg80211_update_iface_num(rdev, ntype, 1);
  844. cfg80211_update_iface_num(rdev, otype, -1);
  845. }
  846. return err;
  847. }
  848. static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
  849. {
  850. static const u32 __mcs2bitrate[] = {
  851. /* control PHY */
  852. [0] = 275,
  853. /* SC PHY */
  854. [1] = 3850,
  855. [2] = 7700,
  856. [3] = 9625,
  857. [4] = 11550,
  858. [5] = 12512, /* 1251.25 mbps */
  859. [6] = 15400,
  860. [7] = 19250,
  861. [8] = 23100,
  862. [9] = 25025,
  863. [10] = 30800,
  864. [11] = 38500,
  865. [12] = 46200,
  866. /* OFDM PHY */
  867. [13] = 6930,
  868. [14] = 8662, /* 866.25 mbps */
  869. [15] = 13860,
  870. [16] = 17325,
  871. [17] = 20790,
  872. [18] = 27720,
  873. [19] = 34650,
  874. [20] = 41580,
  875. [21] = 45045,
  876. [22] = 51975,
  877. [23] = 62370,
  878. [24] = 67568, /* 6756.75 mbps */
  879. /* LP-SC PHY */
  880. [25] = 6260,
  881. [26] = 8340,
  882. [27] = 11120,
  883. [28] = 12510,
  884. [29] = 16680,
  885. [30] = 22240,
  886. [31] = 25030,
  887. };
  888. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  889. return 0;
  890. return __mcs2bitrate[rate->mcs];
  891. }
  892. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  893. {
  894. static const u32 base[4][10] = {
  895. { 6500000,
  896. 13000000,
  897. 19500000,
  898. 26000000,
  899. 39000000,
  900. 52000000,
  901. 58500000,
  902. 65000000,
  903. 78000000,
  904. 0,
  905. },
  906. { 13500000,
  907. 27000000,
  908. 40500000,
  909. 54000000,
  910. 81000000,
  911. 108000000,
  912. 121500000,
  913. 135000000,
  914. 162000000,
  915. 180000000,
  916. },
  917. { 29300000,
  918. 58500000,
  919. 87800000,
  920. 117000000,
  921. 175500000,
  922. 234000000,
  923. 263300000,
  924. 292500000,
  925. 351000000,
  926. 390000000,
  927. },
  928. { 58500000,
  929. 117000000,
  930. 175500000,
  931. 234000000,
  932. 351000000,
  933. 468000000,
  934. 526500000,
  935. 585000000,
  936. 702000000,
  937. 780000000,
  938. },
  939. };
  940. u32 bitrate;
  941. int idx;
  942. if (WARN_ON_ONCE(rate->mcs > 9))
  943. return 0;
  944. idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
  945. RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
  946. rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
  947. rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
  948. bitrate = base[idx][rate->mcs];
  949. bitrate *= rate->nss;
  950. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  951. bitrate = (bitrate / 9) * 10;
  952. /* do NOT round down here */
  953. return (bitrate + 50000) / 100000;
  954. }
  955. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  956. {
  957. int modulation, streams, bitrate;
  958. if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
  959. !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
  960. return rate->legacy;
  961. if (rate->flags & RATE_INFO_FLAGS_60G)
  962. return cfg80211_calculate_bitrate_60g(rate);
  963. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  964. return cfg80211_calculate_bitrate_vht(rate);
  965. /* the formula below does only work for MCS values smaller than 32 */
  966. if (WARN_ON_ONCE(rate->mcs >= 32))
  967. return 0;
  968. modulation = rate->mcs & 7;
  969. streams = (rate->mcs >> 3) + 1;
  970. bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
  971. 13500000 : 6500000;
  972. if (modulation < 4)
  973. bitrate *= (modulation + 1);
  974. else if (modulation == 4)
  975. bitrate *= (modulation + 2);
  976. else
  977. bitrate *= (modulation + 3);
  978. bitrate *= streams;
  979. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  980. bitrate = (bitrate / 9) * 10;
  981. /* do NOT round down here */
  982. return (bitrate + 50000) / 100000;
  983. }
  984. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  985. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  986. enum ieee80211_p2p_attr_id attr,
  987. u8 *buf, unsigned int bufsize)
  988. {
  989. u8 *out = buf;
  990. u16 attr_remaining = 0;
  991. bool desired_attr = false;
  992. u16 desired_len = 0;
  993. while (len > 0) {
  994. unsigned int iedatalen;
  995. unsigned int copy;
  996. const u8 *iedata;
  997. if (len < 2)
  998. return -EILSEQ;
  999. iedatalen = ies[1];
  1000. if (iedatalen + 2 > len)
  1001. return -EILSEQ;
  1002. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1003. goto cont;
  1004. if (iedatalen < 4)
  1005. goto cont;
  1006. iedata = ies + 2;
  1007. /* check WFA OUI, P2P subtype */
  1008. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1009. iedata[2] != 0x9a || iedata[3] != 0x09)
  1010. goto cont;
  1011. iedatalen -= 4;
  1012. iedata += 4;
  1013. /* check attribute continuation into this IE */
  1014. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1015. if (copy && desired_attr) {
  1016. desired_len += copy;
  1017. if (out) {
  1018. memcpy(out, iedata, min(bufsize, copy));
  1019. out += min(bufsize, copy);
  1020. bufsize -= min(bufsize, copy);
  1021. }
  1022. if (copy == attr_remaining)
  1023. return desired_len;
  1024. }
  1025. attr_remaining -= copy;
  1026. if (attr_remaining)
  1027. goto cont;
  1028. iedatalen -= copy;
  1029. iedata += copy;
  1030. while (iedatalen > 0) {
  1031. u16 attr_len;
  1032. /* P2P attribute ID & size must fit */
  1033. if (iedatalen < 3)
  1034. return -EILSEQ;
  1035. desired_attr = iedata[0] == attr;
  1036. attr_len = get_unaligned_le16(iedata + 1);
  1037. iedatalen -= 3;
  1038. iedata += 3;
  1039. copy = min_t(unsigned int, attr_len, iedatalen);
  1040. if (desired_attr) {
  1041. desired_len += copy;
  1042. if (out) {
  1043. memcpy(out, iedata, min(bufsize, copy));
  1044. out += min(bufsize, copy);
  1045. bufsize -= min(bufsize, copy);
  1046. }
  1047. if (copy == attr_len)
  1048. return desired_len;
  1049. }
  1050. iedata += copy;
  1051. iedatalen -= copy;
  1052. attr_remaining = attr_len - copy;
  1053. }
  1054. cont:
  1055. len -= ies[1] + 2;
  1056. ies += ies[1] + 2;
  1057. }
  1058. if (attr_remaining && desired_attr)
  1059. return -EILSEQ;
  1060. return -ENOENT;
  1061. }
  1062. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1063. bool ieee80211_operating_class_to_band(u8 operating_class,
  1064. enum ieee80211_band *band)
  1065. {
  1066. switch (operating_class) {
  1067. case 112:
  1068. case 115 ... 127:
  1069. *band = IEEE80211_BAND_5GHZ;
  1070. return true;
  1071. case 81:
  1072. case 82:
  1073. case 83:
  1074. case 84:
  1075. *band = IEEE80211_BAND_2GHZ;
  1076. return true;
  1077. case 180:
  1078. *band = IEEE80211_BAND_60GHZ;
  1079. return true;
  1080. }
  1081. return false;
  1082. }
  1083. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1084. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  1085. u32 beacon_int)
  1086. {
  1087. struct wireless_dev *wdev;
  1088. int res = 0;
  1089. if (!beacon_int)
  1090. return -EINVAL;
  1091. list_for_each_entry(wdev, &rdev->wdev_list, list) {
  1092. if (!wdev->beacon_interval)
  1093. continue;
  1094. if (wdev->beacon_interval != beacon_int) {
  1095. res = -EINVAL;
  1096. break;
  1097. }
  1098. }
  1099. return res;
  1100. }
  1101. int cfg80211_iter_combinations(struct wiphy *wiphy,
  1102. const int num_different_channels,
  1103. const u8 radar_detect,
  1104. const int iftype_num[NUM_NL80211_IFTYPES],
  1105. void (*iter)(const struct ieee80211_iface_combination *c,
  1106. void *data),
  1107. void *data)
  1108. {
  1109. int i, j, iftype;
  1110. int num_interfaces = 0;
  1111. u32 used_iftypes = 0;
  1112. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1113. num_interfaces += iftype_num[iftype];
  1114. if (iftype_num[iftype] > 0 &&
  1115. !(wiphy->software_iftypes & BIT(iftype)))
  1116. used_iftypes |= BIT(iftype);
  1117. }
  1118. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  1119. const struct ieee80211_iface_combination *c;
  1120. struct ieee80211_iface_limit *limits;
  1121. u32 all_iftypes = 0;
  1122. c = &wiphy->iface_combinations[i];
  1123. if (num_interfaces > c->max_interfaces)
  1124. continue;
  1125. if (num_different_channels > c->num_different_channels)
  1126. continue;
  1127. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  1128. GFP_KERNEL);
  1129. if (!limits)
  1130. return -ENOMEM;
  1131. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1132. if (wiphy->software_iftypes & BIT(iftype))
  1133. continue;
  1134. for (j = 0; j < c->n_limits; j++) {
  1135. all_iftypes |= limits[j].types;
  1136. if (!(limits[j].types & BIT(iftype)))
  1137. continue;
  1138. if (limits[j].max < iftype_num[iftype])
  1139. goto cont;
  1140. limits[j].max -= iftype_num[iftype];
  1141. }
  1142. }
  1143. if (radar_detect != (c->radar_detect_widths & radar_detect))
  1144. goto cont;
  1145. /* Finally check that all iftypes that we're currently
  1146. * using are actually part of this combination. If they
  1147. * aren't then we can't use this combination and have
  1148. * to continue to the next.
  1149. */
  1150. if ((all_iftypes & used_iftypes) != used_iftypes)
  1151. goto cont;
  1152. /* This combination covered all interface types and
  1153. * supported the requested numbers, so we're good.
  1154. */
  1155. (*iter)(c, data);
  1156. cont:
  1157. kfree(limits);
  1158. }
  1159. return 0;
  1160. }
  1161. EXPORT_SYMBOL(cfg80211_iter_combinations);
  1162. static void
  1163. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  1164. void *data)
  1165. {
  1166. int *num = data;
  1167. (*num)++;
  1168. }
  1169. int cfg80211_check_combinations(struct wiphy *wiphy,
  1170. const int num_different_channels,
  1171. const u8 radar_detect,
  1172. const int iftype_num[NUM_NL80211_IFTYPES])
  1173. {
  1174. int err, num = 0;
  1175. err = cfg80211_iter_combinations(wiphy, num_different_channels,
  1176. radar_detect, iftype_num,
  1177. cfg80211_iter_sum_ifcombs, &num);
  1178. if (err)
  1179. return err;
  1180. if (num == 0)
  1181. return -EBUSY;
  1182. return 0;
  1183. }
  1184. EXPORT_SYMBOL(cfg80211_check_combinations);
  1185. int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
  1186. struct wireless_dev *wdev,
  1187. enum nl80211_iftype iftype,
  1188. struct ieee80211_channel *chan,
  1189. enum cfg80211_chan_mode chanmode,
  1190. u8 radar_detect)
  1191. {
  1192. struct wireless_dev *wdev_iter;
  1193. int num[NUM_NL80211_IFTYPES];
  1194. struct ieee80211_channel
  1195. *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
  1196. struct ieee80211_channel *ch;
  1197. enum cfg80211_chan_mode chmode;
  1198. int num_different_channels = 0;
  1199. int total = 1;
  1200. int i;
  1201. ASSERT_RTNL();
  1202. if (WARN_ON(hweight32(radar_detect) > 1))
  1203. return -EINVAL;
  1204. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  1205. return -EINVAL;
  1206. /* Always allow software iftypes */
  1207. if (rdev->wiphy.software_iftypes & BIT(iftype)) {
  1208. if (radar_detect)
  1209. return -EINVAL;
  1210. return 0;
  1211. }
  1212. memset(num, 0, sizeof(num));
  1213. memset(used_channels, 0, sizeof(used_channels));
  1214. num[iftype] = 1;
  1215. /* TODO: We'll probably not need this anymore, since this
  1216. * should only be called with CHAN_MODE_UNDEFINED. There are
  1217. * still a couple of pending calls where other chanmodes are
  1218. * used, but we should get rid of them.
  1219. */
  1220. switch (chanmode) {
  1221. case CHAN_MODE_UNDEFINED:
  1222. break;
  1223. case CHAN_MODE_SHARED:
  1224. WARN_ON(!chan);
  1225. used_channels[0] = chan;
  1226. num_different_channels++;
  1227. break;
  1228. case CHAN_MODE_EXCLUSIVE:
  1229. num_different_channels++;
  1230. break;
  1231. }
  1232. list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
  1233. if (wdev_iter == wdev)
  1234. continue;
  1235. if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
  1236. if (!wdev_iter->p2p_started)
  1237. continue;
  1238. } else if (wdev_iter->netdev) {
  1239. if (!netif_running(wdev_iter->netdev))
  1240. continue;
  1241. } else {
  1242. WARN_ON(1);
  1243. }
  1244. if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
  1245. continue;
  1246. /*
  1247. * We may be holding the "wdev" mutex, but now need to lock
  1248. * wdev_iter. This is OK because once we get here wdev_iter
  1249. * is not wdev (tested above), but we need to use the nested
  1250. * locking for lockdep.
  1251. */
  1252. mutex_lock_nested(&wdev_iter->mtx, 1);
  1253. __acquire(wdev_iter->mtx);
  1254. cfg80211_get_chan_state(wdev_iter, &ch, &chmode, &radar_detect);
  1255. wdev_unlock(wdev_iter);
  1256. switch (chmode) {
  1257. case CHAN_MODE_UNDEFINED:
  1258. break;
  1259. case CHAN_MODE_SHARED:
  1260. for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
  1261. if (!used_channels[i] || used_channels[i] == ch)
  1262. break;
  1263. if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
  1264. return -EBUSY;
  1265. if (used_channels[i] == NULL) {
  1266. used_channels[i] = ch;
  1267. num_different_channels++;
  1268. }
  1269. break;
  1270. case CHAN_MODE_EXCLUSIVE:
  1271. num_different_channels++;
  1272. break;
  1273. }
  1274. num[wdev_iter->iftype]++;
  1275. total++;
  1276. }
  1277. if (total == 1 && !radar_detect)
  1278. return 0;
  1279. return cfg80211_check_combinations(&rdev->wiphy, num_different_channels,
  1280. radar_detect, num);
  1281. }
  1282. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  1283. const u8 *rates, unsigned int n_rates,
  1284. u32 *mask)
  1285. {
  1286. int i, j;
  1287. if (!sband)
  1288. return -EINVAL;
  1289. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  1290. return -EINVAL;
  1291. *mask = 0;
  1292. for (i = 0; i < n_rates; i++) {
  1293. int rate = (rates[i] & 0x7f) * 5;
  1294. bool found = false;
  1295. for (j = 0; j < sband->n_bitrates; j++) {
  1296. if (sband->bitrates[j].bitrate == rate) {
  1297. found = true;
  1298. *mask |= BIT(j);
  1299. break;
  1300. }
  1301. }
  1302. if (!found)
  1303. return -EINVAL;
  1304. }
  1305. /*
  1306. * mask must have at least one bit set here since we
  1307. * didn't accept a 0-length rates array nor allowed
  1308. * entries in the array that didn't exist
  1309. */
  1310. return 0;
  1311. }
  1312. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  1313. {
  1314. enum ieee80211_band band;
  1315. unsigned int n_channels = 0;
  1316. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  1317. if (wiphy->bands[band])
  1318. n_channels += wiphy->bands[band]->n_channels;
  1319. return n_channels;
  1320. }
  1321. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  1322. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  1323. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  1324. const unsigned char rfc1042_header[] __aligned(2) =
  1325. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1326. EXPORT_SYMBOL(rfc1042_header);
  1327. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  1328. const unsigned char bridge_tunnel_header[] __aligned(2) =
  1329. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  1330. EXPORT_SYMBOL(bridge_tunnel_header);