util.c 35 KB

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