util.c 29 KB

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