util.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 (band == IEEE80211_BAND_5GHZ) {
  35. if (chan >= 182 && chan <= 196)
  36. return 4000 + chan * 5;
  37. else
  38. return 5000 + chan * 5;
  39. } else { /* IEEE80211_BAND_2GHZ */
  40. if (chan == 14)
  41. return 2484;
  42. else if (chan < 14)
  43. return 2407 + chan * 5;
  44. else
  45. return 0; /* not supported */
  46. }
  47. }
  48. EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  49. int ieee80211_frequency_to_channel(int freq)
  50. {
  51. /* see 802.11 17.3.8.3.2 and Annex J */
  52. if (freq == 2484)
  53. return 14;
  54. else if (freq < 2484)
  55. return (freq - 2407) / 5;
  56. else if (freq >= 4910 && freq <= 4980)
  57. return (freq - 4000) / 5;
  58. else
  59. return (freq - 5000) / 5;
  60. }
  61. EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  62. struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  63. int freq)
  64. {
  65. enum ieee80211_band band;
  66. struct ieee80211_supported_band *sband;
  67. int i;
  68. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  69. sband = wiphy->bands[band];
  70. if (!sband)
  71. continue;
  72. for (i = 0; i < sband->n_channels; i++) {
  73. if (sband->channels[i].center_freq == freq)
  74. return &sband->channels[i];
  75. }
  76. }
  77. return NULL;
  78. }
  79. EXPORT_SYMBOL(__ieee80211_get_channel);
  80. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
  81. enum ieee80211_band band)
  82. {
  83. int i, want;
  84. switch (band) {
  85. case IEEE80211_BAND_5GHZ:
  86. want = 3;
  87. for (i = 0; i < sband->n_bitrates; i++) {
  88. if (sband->bitrates[i].bitrate == 60 ||
  89. sband->bitrates[i].bitrate == 120 ||
  90. sband->bitrates[i].bitrate == 240) {
  91. sband->bitrates[i].flags |=
  92. IEEE80211_RATE_MANDATORY_A;
  93. want--;
  94. }
  95. }
  96. WARN_ON(want);
  97. break;
  98. case IEEE80211_BAND_2GHZ:
  99. want = 7;
  100. for (i = 0; i < sband->n_bitrates; i++) {
  101. if (sband->bitrates[i].bitrate == 10) {
  102. sband->bitrates[i].flags |=
  103. IEEE80211_RATE_MANDATORY_B |
  104. IEEE80211_RATE_MANDATORY_G;
  105. want--;
  106. }
  107. if (sband->bitrates[i].bitrate == 20 ||
  108. sband->bitrates[i].bitrate == 55 ||
  109. sband->bitrates[i].bitrate == 110 ||
  110. sband->bitrates[i].bitrate == 60 ||
  111. sband->bitrates[i].bitrate == 120 ||
  112. sband->bitrates[i].bitrate == 240) {
  113. sband->bitrates[i].flags |=
  114. IEEE80211_RATE_MANDATORY_G;
  115. want--;
  116. }
  117. if (sband->bitrates[i].bitrate != 10 &&
  118. sband->bitrates[i].bitrate != 20 &&
  119. sband->bitrates[i].bitrate != 55 &&
  120. sband->bitrates[i].bitrate != 110)
  121. sband->bitrates[i].flags |=
  122. IEEE80211_RATE_ERP_G;
  123. }
  124. WARN_ON(want != 0 && want != 3 && want != 6);
  125. break;
  126. case IEEE80211_NUM_BANDS:
  127. WARN_ON(1);
  128. break;
  129. }
  130. }
  131. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  132. {
  133. enum ieee80211_band band;
  134. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  135. if (wiphy->bands[band])
  136. set_mandatory_flags_band(wiphy->bands[band], band);
  137. }
  138. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
  139. {
  140. int i;
  141. for (i = 0; i < wiphy->n_cipher_suites; i++)
  142. if (cipher == wiphy->cipher_suites[i])
  143. return true;
  144. return false;
  145. }
  146. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  147. struct key_params *params, int key_idx,
  148. bool pairwise, const u8 *mac_addr)
  149. {
  150. if (key_idx > 5)
  151. return -EINVAL;
  152. if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  153. return -EINVAL;
  154. if (pairwise && !mac_addr)
  155. return -EINVAL;
  156. /*
  157. * Disallow pairwise keys with non-zero index unless it's WEP
  158. * or a vendor specific cipher (because current deployments use
  159. * pairwise WEP keys with non-zero indices and for vendor specific
  160. * ciphers this should be validated in the driver or hardware level
  161. * - but 802.11i clearly specifies to use zero)
  162. */
  163. if (pairwise && key_idx &&
  164. ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
  165. (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
  166. (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
  167. return -EINVAL;
  168. switch (params->cipher) {
  169. case WLAN_CIPHER_SUITE_WEP40:
  170. if (params->key_len != WLAN_KEY_LEN_WEP40)
  171. return -EINVAL;
  172. break;
  173. case WLAN_CIPHER_SUITE_TKIP:
  174. if (params->key_len != WLAN_KEY_LEN_TKIP)
  175. return -EINVAL;
  176. break;
  177. case WLAN_CIPHER_SUITE_CCMP:
  178. if (params->key_len != WLAN_KEY_LEN_CCMP)
  179. return -EINVAL;
  180. break;
  181. case WLAN_CIPHER_SUITE_WEP104:
  182. if (params->key_len != WLAN_KEY_LEN_WEP104)
  183. return -EINVAL;
  184. break;
  185. case WLAN_CIPHER_SUITE_AES_CMAC:
  186. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  187. return -EINVAL;
  188. break;
  189. default:
  190. /*
  191. * We don't know anything about this algorithm,
  192. * allow using it -- but the driver must check
  193. * all parameters! We still check below whether
  194. * or not the driver supports this algorithm,
  195. * of course.
  196. */
  197. break;
  198. }
  199. if (params->seq) {
  200. switch (params->cipher) {
  201. case WLAN_CIPHER_SUITE_WEP40:
  202. case WLAN_CIPHER_SUITE_WEP104:
  203. /* These ciphers do not use key sequence */
  204. return -EINVAL;
  205. case WLAN_CIPHER_SUITE_TKIP:
  206. case WLAN_CIPHER_SUITE_CCMP:
  207. case WLAN_CIPHER_SUITE_AES_CMAC:
  208. if (params->seq_len != 6)
  209. return -EINVAL;
  210. break;
  211. }
  212. }
  213. if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
  214. return -EINVAL;
  215. return 0;
  216. }
  217. unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
  218. {
  219. unsigned int hdrlen = 24;
  220. if (ieee80211_is_data(fc)) {
  221. if (ieee80211_has_a4(fc))
  222. hdrlen = 30;
  223. if (ieee80211_is_data_qos(fc)) {
  224. hdrlen += IEEE80211_QOS_CTL_LEN;
  225. if (ieee80211_has_order(fc))
  226. hdrlen += IEEE80211_HT_CTL_LEN;
  227. }
  228. goto out;
  229. }
  230. if (ieee80211_is_ctl(fc)) {
  231. /*
  232. * ACK and CTS are 10 bytes, all others 16. To see how
  233. * to get this condition consider
  234. * subtype mask: 0b0000000011110000 (0x00F0)
  235. * ACK subtype: 0b0000000011010000 (0x00D0)
  236. * CTS subtype: 0b0000000011000000 (0x00C0)
  237. * bits that matter: ^^^ (0x00E0)
  238. * value of those: 0b0000000011000000 (0x00C0)
  239. */
  240. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  241. hdrlen = 10;
  242. else
  243. hdrlen = 16;
  244. }
  245. out:
  246. return hdrlen;
  247. }
  248. EXPORT_SYMBOL(ieee80211_hdrlen);
  249. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  250. {
  251. const struct ieee80211_hdr *hdr =
  252. (const struct ieee80211_hdr *)skb->data;
  253. unsigned int hdrlen;
  254. if (unlikely(skb->len < 10))
  255. return 0;
  256. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  257. if (unlikely(hdrlen > skb->len))
  258. return 0;
  259. return hdrlen;
  260. }
  261. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  262. static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  263. {
  264. int ae = meshhdr->flags & MESH_FLAGS_AE;
  265. /* 7.1.3.5a.2 */
  266. switch (ae) {
  267. case 0:
  268. return 6;
  269. case MESH_FLAGS_AE_A4:
  270. return 12;
  271. case MESH_FLAGS_AE_A5_A6:
  272. return 18;
  273. case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
  274. return 24;
  275. default:
  276. return 6;
  277. }
  278. }
  279. int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
  280. enum nl80211_iftype iftype)
  281. {
  282. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  283. u16 hdrlen, ethertype;
  284. u8 *payload;
  285. u8 dst[ETH_ALEN];
  286. u8 src[ETH_ALEN] __aligned(2);
  287. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  288. return -1;
  289. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  290. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  291. * header
  292. * IEEE 802.11 address fields:
  293. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  294. * 0 0 DA SA BSSID n/a
  295. * 0 1 DA BSSID SA n/a
  296. * 1 0 BSSID SA DA n/a
  297. * 1 1 RA TA DA SA
  298. */
  299. memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
  300. memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
  301. switch (hdr->frame_control &
  302. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  303. case cpu_to_le16(IEEE80211_FCTL_TODS):
  304. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  305. iftype != NL80211_IFTYPE_AP_VLAN &&
  306. iftype != NL80211_IFTYPE_P2P_GO))
  307. return -1;
  308. break;
  309. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  310. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  311. iftype != NL80211_IFTYPE_MESH_POINT &&
  312. iftype != NL80211_IFTYPE_AP_VLAN &&
  313. iftype != NL80211_IFTYPE_STATION))
  314. return -1;
  315. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  316. struct ieee80211s_hdr *meshdr =
  317. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  318. /* make sure meshdr->flags is on the linear part */
  319. if (!pskb_may_pull(skb, hdrlen + 1))
  320. return -1;
  321. if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
  322. skb_copy_bits(skb, hdrlen +
  323. offsetof(struct ieee80211s_hdr, eaddr1),
  324. dst, ETH_ALEN);
  325. skb_copy_bits(skb, hdrlen +
  326. offsetof(struct ieee80211s_hdr, eaddr2),
  327. src, ETH_ALEN);
  328. }
  329. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  330. }
  331. break;
  332. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  333. if ((iftype != NL80211_IFTYPE_STATION &&
  334. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  335. iftype != NL80211_IFTYPE_MESH_POINT) ||
  336. (is_multicast_ether_addr(dst) &&
  337. !compare_ether_addr(src, addr)))
  338. return -1;
  339. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  340. struct ieee80211s_hdr *meshdr =
  341. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  342. /* make sure meshdr->flags is on the linear part */
  343. if (!pskb_may_pull(skb, hdrlen + 1))
  344. return -1;
  345. if (meshdr->flags & MESH_FLAGS_AE_A4)
  346. skb_copy_bits(skb, hdrlen +
  347. offsetof(struct ieee80211s_hdr, eaddr1),
  348. src, ETH_ALEN);
  349. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  350. }
  351. break;
  352. case cpu_to_le16(0):
  353. if (iftype != NL80211_IFTYPE_ADHOC &&
  354. iftype != NL80211_IFTYPE_STATION)
  355. return -1;
  356. break;
  357. }
  358. if (!pskb_may_pull(skb, hdrlen + 8))
  359. return -1;
  360. payload = skb->data + hdrlen;
  361. ethertype = (payload[6] << 8) | payload[7];
  362. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  363. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  364. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  365. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  366. * replace EtherType */
  367. skb_pull(skb, hdrlen + 6);
  368. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  369. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  370. } else {
  371. struct ethhdr *ehdr;
  372. __be16 len;
  373. skb_pull(skb, hdrlen);
  374. len = htons(skb->len);
  375. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  376. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  377. memcpy(ehdr->h_source, src, ETH_ALEN);
  378. ehdr->h_proto = len;
  379. }
  380. return 0;
  381. }
  382. EXPORT_SYMBOL(ieee80211_data_to_8023);
  383. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  384. enum nl80211_iftype iftype, u8 *bssid, bool qos)
  385. {
  386. struct ieee80211_hdr hdr;
  387. u16 hdrlen, ethertype;
  388. __le16 fc;
  389. const u8 *encaps_data;
  390. int encaps_len, skip_header_bytes;
  391. int nh_pos, h_pos;
  392. int head_need;
  393. if (unlikely(skb->len < ETH_HLEN))
  394. return -EINVAL;
  395. nh_pos = skb_network_header(skb) - skb->data;
  396. h_pos = skb_transport_header(skb) - skb->data;
  397. /* convert Ethernet header to proper 802.11 header (based on
  398. * operation mode) */
  399. ethertype = (skb->data[12] << 8) | skb->data[13];
  400. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  401. switch (iftype) {
  402. case NL80211_IFTYPE_AP:
  403. case NL80211_IFTYPE_AP_VLAN:
  404. case NL80211_IFTYPE_P2P_GO:
  405. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  406. /* DA BSSID SA */
  407. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  408. memcpy(hdr.addr2, addr, ETH_ALEN);
  409. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  410. hdrlen = 24;
  411. break;
  412. case NL80211_IFTYPE_STATION:
  413. case NL80211_IFTYPE_P2P_CLIENT:
  414. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  415. /* BSSID SA DA */
  416. memcpy(hdr.addr1, bssid, ETH_ALEN);
  417. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  418. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  419. hdrlen = 24;
  420. break;
  421. case NL80211_IFTYPE_ADHOC:
  422. /* DA SA BSSID */
  423. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  424. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  425. memcpy(hdr.addr3, bssid, ETH_ALEN);
  426. hdrlen = 24;
  427. break;
  428. default:
  429. return -EOPNOTSUPP;
  430. }
  431. if (qos) {
  432. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  433. hdrlen += 2;
  434. }
  435. hdr.frame_control = fc;
  436. hdr.duration_id = 0;
  437. hdr.seq_ctrl = 0;
  438. skip_header_bytes = ETH_HLEN;
  439. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  440. encaps_data = bridge_tunnel_header;
  441. encaps_len = sizeof(bridge_tunnel_header);
  442. skip_header_bytes -= 2;
  443. } else if (ethertype > 0x600) {
  444. encaps_data = rfc1042_header;
  445. encaps_len = sizeof(rfc1042_header);
  446. skip_header_bytes -= 2;
  447. } else {
  448. encaps_data = NULL;
  449. encaps_len = 0;
  450. }
  451. skb_pull(skb, skip_header_bytes);
  452. nh_pos -= skip_header_bytes;
  453. h_pos -= skip_header_bytes;
  454. head_need = hdrlen + encaps_len - skb_headroom(skb);
  455. if (head_need > 0 || skb_cloned(skb)) {
  456. head_need = max(head_need, 0);
  457. if (head_need)
  458. skb_orphan(skb);
  459. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
  460. return -ENOMEM;
  461. skb->truesize += head_need;
  462. }
  463. if (encaps_data) {
  464. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  465. nh_pos += encaps_len;
  466. h_pos += encaps_len;
  467. }
  468. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  469. nh_pos += hdrlen;
  470. h_pos += hdrlen;
  471. /* Update skb pointers to various headers since this modified frame
  472. * is going to go through Linux networking code that may potentially
  473. * need things like pointer to IP header. */
  474. skb_set_mac_header(skb, 0);
  475. skb_set_network_header(skb, nh_pos);
  476. skb_set_transport_header(skb, h_pos);
  477. return 0;
  478. }
  479. EXPORT_SYMBOL(ieee80211_data_from_8023);
  480. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  481. const u8 *addr, enum nl80211_iftype iftype,
  482. const unsigned int extra_headroom,
  483. bool has_80211_header)
  484. {
  485. struct sk_buff *frame = NULL;
  486. u16 ethertype;
  487. u8 *payload;
  488. const struct ethhdr *eth;
  489. int remaining, err;
  490. u8 dst[ETH_ALEN], src[ETH_ALEN];
  491. if (has_80211_header) {
  492. err = ieee80211_data_to_8023(skb, addr, iftype);
  493. if (err)
  494. goto out;
  495. /* skip the wrapping header */
  496. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  497. if (!eth)
  498. goto out;
  499. } else {
  500. eth = (struct ethhdr *) skb->data;
  501. }
  502. while (skb != frame) {
  503. u8 padding;
  504. __be16 len = eth->h_proto;
  505. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  506. remaining = skb->len;
  507. memcpy(dst, eth->h_dest, ETH_ALEN);
  508. memcpy(src, eth->h_source, ETH_ALEN);
  509. padding = (4 - subframe_len) & 0x3;
  510. /* the last MSDU has no padding */
  511. if (subframe_len > remaining)
  512. goto purge;
  513. skb_pull(skb, sizeof(struct ethhdr));
  514. /* reuse skb for the last subframe */
  515. if (remaining <= subframe_len + padding)
  516. frame = skb;
  517. else {
  518. unsigned int hlen = ALIGN(extra_headroom, 4);
  519. /*
  520. * Allocate and reserve two bytes more for payload
  521. * alignment since sizeof(struct ethhdr) is 14.
  522. */
  523. frame = dev_alloc_skb(hlen + subframe_len + 2);
  524. if (!frame)
  525. goto purge;
  526. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  527. memcpy(skb_put(frame, ntohs(len)), skb->data,
  528. ntohs(len));
  529. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  530. padding);
  531. if (!eth) {
  532. dev_kfree_skb(frame);
  533. goto purge;
  534. }
  535. }
  536. skb_reset_network_header(frame);
  537. frame->dev = skb->dev;
  538. frame->priority = skb->priority;
  539. payload = frame->data;
  540. ethertype = (payload[6] << 8) | payload[7];
  541. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  542. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  543. compare_ether_addr(payload,
  544. bridge_tunnel_header) == 0)) {
  545. /* remove RFC1042 or Bridge-Tunnel
  546. * encapsulation and replace EtherType */
  547. skb_pull(frame, 6);
  548. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  549. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  550. } else {
  551. memcpy(skb_push(frame, sizeof(__be16)), &len,
  552. sizeof(__be16));
  553. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  554. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  555. }
  556. __skb_queue_tail(list, frame);
  557. }
  558. return;
  559. purge:
  560. __skb_queue_purge(list);
  561. out:
  562. dev_kfree_skb(skb);
  563. }
  564. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  565. /* Given a data frame determine the 802.1p/1d tag to use. */
  566. unsigned int cfg80211_classify8021d(struct sk_buff *skb)
  567. {
  568. unsigned int dscp;
  569. /* skb->priority values from 256->263 are magic values to
  570. * directly indicate a specific 802.1d priority. This is used
  571. * to allow 802.1d priority to be passed directly in from VLAN
  572. * tags, etc.
  573. */
  574. if (skb->priority >= 256 && skb->priority <= 263)
  575. return skb->priority - 256;
  576. switch (skb->protocol) {
  577. case htons(ETH_P_IP):
  578. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  579. break;
  580. case htons(ETH_P_IPV6):
  581. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 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.bss, ev->rm.req_ie,
  660. ev->rm.req_ie_len, ev->rm.resp_ie,
  661. 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. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  790. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  791. u32 beacon_int)
  792. {
  793. struct wireless_dev *wdev;
  794. int res = 0;
  795. if (!beacon_int)
  796. return -EINVAL;
  797. mutex_lock(&rdev->devlist_mtx);
  798. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  799. if (!wdev->beacon_interval)
  800. continue;
  801. if (wdev->beacon_interval != beacon_int) {
  802. res = -EINVAL;
  803. break;
  804. }
  805. }
  806. mutex_unlock(&rdev->devlist_mtx);
  807. return res;
  808. }
  809. int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
  810. struct wireless_dev *wdev,
  811. enum nl80211_iftype iftype)
  812. {
  813. struct wireless_dev *wdev_iter;
  814. int num[NUM_NL80211_IFTYPES];
  815. int total = 1;
  816. int i, j;
  817. ASSERT_RTNL();
  818. /* Always allow software iftypes */
  819. if (rdev->wiphy.software_iftypes & BIT(iftype))
  820. return 0;
  821. /*
  822. * Drivers will gradually all set this flag, until all
  823. * have it we only enforce for those that set it.
  824. */
  825. if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
  826. return 0;
  827. memset(num, 0, sizeof(num));
  828. num[iftype] = 1;
  829. mutex_lock(&rdev->devlist_mtx);
  830. list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
  831. if (wdev_iter == wdev)
  832. continue;
  833. if (!netif_running(wdev_iter->netdev))
  834. continue;
  835. if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
  836. continue;
  837. num[wdev_iter->iftype]++;
  838. total++;
  839. }
  840. mutex_unlock(&rdev->devlist_mtx);
  841. for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
  842. const struct ieee80211_iface_combination *c;
  843. struct ieee80211_iface_limit *limits;
  844. c = &rdev->wiphy.iface_combinations[i];
  845. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  846. GFP_KERNEL);
  847. if (!limits)
  848. return -ENOMEM;
  849. if (total > c->max_interfaces)
  850. goto cont;
  851. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  852. if (rdev->wiphy.software_iftypes & BIT(iftype))
  853. continue;
  854. for (j = 0; j < c->n_limits; j++) {
  855. if (!(limits[j].types & iftype))
  856. continue;
  857. if (limits[j].max < num[iftype])
  858. goto cont;
  859. limits[j].max -= num[iftype];
  860. }
  861. }
  862. /* yay, it fits */
  863. kfree(limits);
  864. return 0;
  865. cont:
  866. kfree(limits);
  867. }
  868. return -EBUSY;
  869. }
  870. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  871. const u8 *rates, unsigned int n_rates,
  872. u32 *mask)
  873. {
  874. int i, j;
  875. if (!sband)
  876. return -EINVAL;
  877. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  878. return -EINVAL;
  879. *mask = 0;
  880. for (i = 0; i < n_rates; i++) {
  881. int rate = (rates[i] & 0x7f) * 5;
  882. bool found = false;
  883. for (j = 0; j < sband->n_bitrates; j++) {
  884. if (sband->bitrates[j].bitrate == rate) {
  885. found = true;
  886. *mask |= BIT(j);
  887. break;
  888. }
  889. }
  890. if (!found)
  891. return -EINVAL;
  892. }
  893. /*
  894. * mask must have at least one bit set here since we
  895. * didn't accept a 0-length rates array nor allowed
  896. * entries in the array that didn't exist
  897. */
  898. return 0;
  899. }
  900. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  901. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  902. const unsigned char rfc1042_header[] __aligned(2) =
  903. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  904. EXPORT_SYMBOL(rfc1042_header);
  905. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  906. const unsigned char bridge_tunnel_header[] __aligned(2) =
  907. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  908. EXPORT_SYMBOL(bridge_tunnel_header);