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