util.c 35 KB

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