util.c 39 KB

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