util.c 36 KB

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