util.c 29 KB

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