util.c 30 KB

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