util.c 34 KB

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