util.c 29 KB

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