util.c 33 KB

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