util.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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_reset_mac_header(skb);
  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 void
  579. __frame_add_frag(struct sk_buff *skb, struct page *page,
  580. void *ptr, int len, int size)
  581. {
  582. struct skb_shared_info *sh = skb_shinfo(skb);
  583. int page_offset;
  584. atomic_inc(&page->_count);
  585. page_offset = ptr - page_address(page);
  586. skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
  587. }
  588. static void
  589. __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
  590. int offset, int len)
  591. {
  592. struct skb_shared_info *sh = skb_shinfo(skb);
  593. const skb_frag_t *frag = &sh->frags[-1];
  594. struct page *frag_page;
  595. void *frag_ptr;
  596. int frag_len, frag_size;
  597. int head_size = skb->len - skb->data_len;
  598. int cur_len;
  599. frag_page = virt_to_head_page(skb->head);
  600. frag_ptr = skb->data;
  601. frag_size = head_size;
  602. while (offset >= frag_size) {
  603. offset -= frag_size;
  604. frag++;
  605. frag_page = skb_frag_page(frag);
  606. frag_ptr = skb_frag_address(frag);
  607. frag_size = skb_frag_size(frag);
  608. }
  609. frag_ptr += offset;
  610. frag_len = frag_size - offset;
  611. cur_len = min(len, frag_len);
  612. __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
  613. len -= cur_len;
  614. while (len > 0) {
  615. frag++;
  616. frag_len = skb_frag_size(frag);
  617. cur_len = min(len, frag_len);
  618. __frame_add_frag(frame, skb_frag_page(frag),
  619. skb_frag_address(frag), cur_len, frag_len);
  620. len -= cur_len;
  621. }
  622. }
  623. static struct sk_buff *
  624. __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
  625. int offset, int len, bool reuse_frag)
  626. {
  627. struct sk_buff *frame;
  628. int cur_len = len;
  629. if (skb->len - offset < len)
  630. return NULL;
  631. /*
  632. * When reusing framents, copy some data to the head to simplify
  633. * ethernet header handling and speed up protocol header processing
  634. * in the stack later.
  635. */
  636. if (reuse_frag)
  637. cur_len = min_t(int, len, 32);
  638. /*
  639. * Allocate and reserve two bytes more for payload
  640. * alignment since sizeof(struct ethhdr) is 14.
  641. */
  642. frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
  643. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  644. skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
  645. len -= cur_len;
  646. if (!len)
  647. return frame;
  648. offset += cur_len;
  649. __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
  650. return frame;
  651. }
  652. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  653. const u8 *addr, enum nl80211_iftype iftype,
  654. const unsigned int extra_headroom,
  655. bool has_80211_header)
  656. {
  657. unsigned int hlen = ALIGN(extra_headroom, 4);
  658. struct sk_buff *frame = NULL;
  659. u16 ethertype;
  660. u8 *payload;
  661. int offset = 0, remaining, err;
  662. struct ethhdr eth;
  663. bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
  664. bool reuse_skb = false;
  665. bool last = false;
  666. if (has_80211_header) {
  667. err = __ieee80211_data_to_8023(skb, &eth, addr, iftype);
  668. if (err)
  669. goto out;
  670. }
  671. while (!last) {
  672. unsigned int subframe_len;
  673. int len;
  674. u8 padding;
  675. skb_copy_bits(skb, offset, &eth, sizeof(eth));
  676. len = ntohs(eth.h_proto);
  677. subframe_len = sizeof(struct ethhdr) + len;
  678. padding = (4 - subframe_len) & 0x3;
  679. /* the last MSDU has no padding */
  680. remaining = skb->len - offset;
  681. if (subframe_len > remaining)
  682. goto purge;
  683. offset += sizeof(struct ethhdr);
  684. /* reuse skb for the last subframe */
  685. last = remaining <= subframe_len + padding;
  686. if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
  687. skb_pull(skb, offset);
  688. frame = skb;
  689. reuse_skb = true;
  690. } else {
  691. frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
  692. reuse_frag);
  693. if (!frame)
  694. goto purge;
  695. offset += len + padding;
  696. }
  697. skb_reset_network_header(frame);
  698. frame->dev = skb->dev;
  699. frame->priority = skb->priority;
  700. payload = frame->data;
  701. ethertype = (payload[6] << 8) | payload[7];
  702. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  703. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  704. ether_addr_equal(payload, bridge_tunnel_header))) {
  705. eth.h_proto = htons(ethertype);
  706. skb_pull(frame, ETH_ALEN + 2);
  707. }
  708. memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
  709. __skb_queue_tail(list, frame);
  710. }
  711. if (!reuse_skb)
  712. dev_kfree_skb(skb);
  713. return;
  714. purge:
  715. __skb_queue_purge(list);
  716. out:
  717. dev_kfree_skb(skb);
  718. }
  719. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  720. /* Given a data frame determine the 802.1p/1d tag to use. */
  721. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  722. struct cfg80211_qos_map *qos_map)
  723. {
  724. unsigned int dscp;
  725. unsigned char vlan_priority;
  726. /* skb->priority values from 256->263 are magic values to
  727. * directly indicate a specific 802.1d priority. This is used
  728. * to allow 802.1d priority to be passed directly in from VLAN
  729. * tags, etc.
  730. */
  731. if (skb->priority >= 256 && skb->priority <= 263)
  732. return skb->priority - 256;
  733. if (skb_vlan_tag_present(skb)) {
  734. vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
  735. >> VLAN_PRIO_SHIFT;
  736. if (vlan_priority > 0)
  737. return vlan_priority;
  738. }
  739. switch (skb->protocol) {
  740. case htons(ETH_P_IP):
  741. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  742. break;
  743. case htons(ETH_P_IPV6):
  744. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  745. break;
  746. case htons(ETH_P_MPLS_UC):
  747. case htons(ETH_P_MPLS_MC): {
  748. struct mpls_label mpls_tmp, *mpls;
  749. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  750. sizeof(*mpls), &mpls_tmp);
  751. if (!mpls)
  752. return 0;
  753. return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  754. >> MPLS_LS_TC_SHIFT;
  755. }
  756. case htons(ETH_P_80221):
  757. /* 802.21 is always network control traffic */
  758. return 7;
  759. default:
  760. return 0;
  761. }
  762. if (qos_map) {
  763. unsigned int i, tmp_dscp = dscp >> 2;
  764. for (i = 0; i < qos_map->num_des; i++) {
  765. if (tmp_dscp == qos_map->dscp_exception[i].dscp)
  766. return qos_map->dscp_exception[i].up;
  767. }
  768. for (i = 0; i < 8; i++) {
  769. if (tmp_dscp >= qos_map->up[i].low &&
  770. tmp_dscp <= qos_map->up[i].high)
  771. return i;
  772. }
  773. }
  774. return dscp >> 5;
  775. }
  776. EXPORT_SYMBOL(cfg80211_classify8021d);
  777. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  778. {
  779. const struct cfg80211_bss_ies *ies;
  780. ies = rcu_dereference(bss->ies);
  781. if (!ies)
  782. return NULL;
  783. return cfg80211_find_ie(ie, ies->data, ies->len);
  784. }
  785. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  786. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  787. {
  788. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  789. struct net_device *dev = wdev->netdev;
  790. int i;
  791. if (!wdev->connect_keys)
  792. return;
  793. for (i = 0; i < 6; i++) {
  794. if (!wdev->connect_keys->params[i].cipher)
  795. continue;
  796. if (rdev_add_key(rdev, dev, i, false, NULL,
  797. &wdev->connect_keys->params[i])) {
  798. netdev_err(dev, "failed to set key %d\n", i);
  799. continue;
  800. }
  801. if (wdev->connect_keys->def == i)
  802. if (rdev_set_default_key(rdev, dev, i, true, true)) {
  803. netdev_err(dev, "failed to set defkey %d\n", i);
  804. continue;
  805. }
  806. if (wdev->connect_keys->defmgmt == i)
  807. if (rdev_set_default_mgmt_key(rdev, dev, i))
  808. netdev_err(dev, "failed to set mgtdef %d\n", i);
  809. }
  810. kzfree(wdev->connect_keys);
  811. wdev->connect_keys = NULL;
  812. }
  813. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  814. {
  815. struct cfg80211_event *ev;
  816. unsigned long flags;
  817. const u8 *bssid = NULL;
  818. spin_lock_irqsave(&wdev->event_lock, flags);
  819. while (!list_empty(&wdev->event_list)) {
  820. ev = list_first_entry(&wdev->event_list,
  821. struct cfg80211_event, list);
  822. list_del(&ev->list);
  823. spin_unlock_irqrestore(&wdev->event_lock, flags);
  824. wdev_lock(wdev);
  825. switch (ev->type) {
  826. case EVENT_CONNECT_RESULT:
  827. if (!is_zero_ether_addr(ev->cr.bssid))
  828. bssid = ev->cr.bssid;
  829. __cfg80211_connect_result(
  830. wdev->netdev, bssid,
  831. ev->cr.req_ie, ev->cr.req_ie_len,
  832. ev->cr.resp_ie, ev->cr.resp_ie_len,
  833. ev->cr.status,
  834. ev->cr.status == WLAN_STATUS_SUCCESS,
  835. NULL);
  836. break;
  837. case EVENT_ROAMED:
  838. __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
  839. ev->rm.req_ie_len, ev->rm.resp_ie,
  840. ev->rm.resp_ie_len);
  841. break;
  842. case EVENT_DISCONNECTED:
  843. __cfg80211_disconnected(wdev->netdev,
  844. ev->dc.ie, ev->dc.ie_len,
  845. ev->dc.reason,
  846. !ev->dc.locally_generated);
  847. break;
  848. case EVENT_IBSS_JOINED:
  849. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  850. ev->ij.channel);
  851. break;
  852. case EVENT_STOPPED:
  853. __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
  854. break;
  855. }
  856. wdev_unlock(wdev);
  857. kfree(ev);
  858. spin_lock_irqsave(&wdev->event_lock, flags);
  859. }
  860. spin_unlock_irqrestore(&wdev->event_lock, flags);
  861. }
  862. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  863. {
  864. struct wireless_dev *wdev;
  865. ASSERT_RTNL();
  866. list_for_each_entry(wdev, &rdev->wdev_list, list)
  867. cfg80211_process_wdev_events(wdev);
  868. }
  869. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  870. struct net_device *dev, enum nl80211_iftype ntype,
  871. u32 *flags, struct vif_params *params)
  872. {
  873. int err;
  874. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  875. ASSERT_RTNL();
  876. /* don't support changing VLANs, you just re-create them */
  877. if (otype == NL80211_IFTYPE_AP_VLAN)
  878. return -EOPNOTSUPP;
  879. /* cannot change into P2P device type */
  880. if (ntype == NL80211_IFTYPE_P2P_DEVICE)
  881. return -EOPNOTSUPP;
  882. if (!rdev->ops->change_virtual_intf ||
  883. !(rdev->wiphy.interface_modes & (1 << ntype)))
  884. return -EOPNOTSUPP;
  885. /* if it's part of a bridge, reject changing type to station/ibss */
  886. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  887. (ntype == NL80211_IFTYPE_ADHOC ||
  888. ntype == NL80211_IFTYPE_STATION ||
  889. ntype == NL80211_IFTYPE_P2P_CLIENT))
  890. return -EBUSY;
  891. if (ntype != otype) {
  892. dev->ieee80211_ptr->use_4addr = false;
  893. dev->ieee80211_ptr->mesh_id_up_len = 0;
  894. wdev_lock(dev->ieee80211_ptr);
  895. rdev_set_qos_map(rdev, dev, NULL);
  896. wdev_unlock(dev->ieee80211_ptr);
  897. switch (otype) {
  898. case NL80211_IFTYPE_AP:
  899. cfg80211_stop_ap(rdev, dev, true);
  900. break;
  901. case NL80211_IFTYPE_ADHOC:
  902. cfg80211_leave_ibss(rdev, dev, false);
  903. break;
  904. case NL80211_IFTYPE_STATION:
  905. case NL80211_IFTYPE_P2P_CLIENT:
  906. wdev_lock(dev->ieee80211_ptr);
  907. cfg80211_disconnect(rdev, dev,
  908. WLAN_REASON_DEAUTH_LEAVING, true);
  909. wdev_unlock(dev->ieee80211_ptr);
  910. break;
  911. case NL80211_IFTYPE_MESH_POINT:
  912. /* mesh should be handled? */
  913. break;
  914. default:
  915. break;
  916. }
  917. cfg80211_process_rdev_events(rdev);
  918. }
  919. err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
  920. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  921. if (!err && params && params->use_4addr != -1)
  922. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  923. if (!err) {
  924. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  925. switch (ntype) {
  926. case NL80211_IFTYPE_STATION:
  927. if (dev->ieee80211_ptr->use_4addr)
  928. break;
  929. /* fall through */
  930. case NL80211_IFTYPE_OCB:
  931. case NL80211_IFTYPE_P2P_CLIENT:
  932. case NL80211_IFTYPE_ADHOC:
  933. dev->priv_flags |= IFF_DONT_BRIDGE;
  934. break;
  935. case NL80211_IFTYPE_P2P_GO:
  936. case NL80211_IFTYPE_AP:
  937. case NL80211_IFTYPE_AP_VLAN:
  938. case NL80211_IFTYPE_WDS:
  939. case NL80211_IFTYPE_MESH_POINT:
  940. /* bridging OK */
  941. break;
  942. case NL80211_IFTYPE_MONITOR:
  943. /* monitor can't bridge anyway */
  944. break;
  945. case NL80211_IFTYPE_UNSPECIFIED:
  946. case NUM_NL80211_IFTYPES:
  947. /* not happening */
  948. break;
  949. case NL80211_IFTYPE_P2P_DEVICE:
  950. WARN_ON(1);
  951. break;
  952. }
  953. }
  954. if (!err && ntype != otype && netif_running(dev)) {
  955. cfg80211_update_iface_num(rdev, ntype, 1);
  956. cfg80211_update_iface_num(rdev, otype, -1);
  957. }
  958. return err;
  959. }
  960. static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
  961. {
  962. static const u32 __mcs2bitrate[] = {
  963. /* control PHY */
  964. [0] = 275,
  965. /* SC PHY */
  966. [1] = 3850,
  967. [2] = 7700,
  968. [3] = 9625,
  969. [4] = 11550,
  970. [5] = 12512, /* 1251.25 mbps */
  971. [6] = 15400,
  972. [7] = 19250,
  973. [8] = 23100,
  974. [9] = 25025,
  975. [10] = 30800,
  976. [11] = 38500,
  977. [12] = 46200,
  978. /* OFDM PHY */
  979. [13] = 6930,
  980. [14] = 8662, /* 866.25 mbps */
  981. [15] = 13860,
  982. [16] = 17325,
  983. [17] = 20790,
  984. [18] = 27720,
  985. [19] = 34650,
  986. [20] = 41580,
  987. [21] = 45045,
  988. [22] = 51975,
  989. [23] = 62370,
  990. [24] = 67568, /* 6756.75 mbps */
  991. /* LP-SC PHY */
  992. [25] = 6260,
  993. [26] = 8340,
  994. [27] = 11120,
  995. [28] = 12510,
  996. [29] = 16680,
  997. [30] = 22240,
  998. [31] = 25030,
  999. };
  1000. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  1001. return 0;
  1002. return __mcs2bitrate[rate->mcs];
  1003. }
  1004. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  1005. {
  1006. static const u32 base[4][10] = {
  1007. { 6500000,
  1008. 13000000,
  1009. 19500000,
  1010. 26000000,
  1011. 39000000,
  1012. 52000000,
  1013. 58500000,
  1014. 65000000,
  1015. 78000000,
  1016. 0,
  1017. },
  1018. { 13500000,
  1019. 27000000,
  1020. 40500000,
  1021. 54000000,
  1022. 81000000,
  1023. 108000000,
  1024. 121500000,
  1025. 135000000,
  1026. 162000000,
  1027. 180000000,
  1028. },
  1029. { 29300000,
  1030. 58500000,
  1031. 87800000,
  1032. 117000000,
  1033. 175500000,
  1034. 234000000,
  1035. 263300000,
  1036. 292500000,
  1037. 351000000,
  1038. 390000000,
  1039. },
  1040. { 58500000,
  1041. 117000000,
  1042. 175500000,
  1043. 234000000,
  1044. 351000000,
  1045. 468000000,
  1046. 526500000,
  1047. 585000000,
  1048. 702000000,
  1049. 780000000,
  1050. },
  1051. };
  1052. u32 bitrate;
  1053. int idx;
  1054. if (WARN_ON_ONCE(rate->mcs > 9))
  1055. return 0;
  1056. switch (rate->bw) {
  1057. case RATE_INFO_BW_160:
  1058. idx = 3;
  1059. break;
  1060. case RATE_INFO_BW_80:
  1061. idx = 2;
  1062. break;
  1063. case RATE_INFO_BW_40:
  1064. idx = 1;
  1065. break;
  1066. case RATE_INFO_BW_5:
  1067. case RATE_INFO_BW_10:
  1068. default:
  1069. WARN_ON(1);
  1070. /* fall through */
  1071. case RATE_INFO_BW_20:
  1072. idx = 0;
  1073. }
  1074. bitrate = base[idx][rate->mcs];
  1075. bitrate *= rate->nss;
  1076. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1077. bitrate = (bitrate / 9) * 10;
  1078. /* do NOT round down here */
  1079. return (bitrate + 50000) / 100000;
  1080. }
  1081. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  1082. {
  1083. int modulation, streams, bitrate;
  1084. if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
  1085. !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
  1086. return rate->legacy;
  1087. if (rate->flags & RATE_INFO_FLAGS_60G)
  1088. return cfg80211_calculate_bitrate_60g(rate);
  1089. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  1090. return cfg80211_calculate_bitrate_vht(rate);
  1091. /* the formula below does only work for MCS values smaller than 32 */
  1092. if (WARN_ON_ONCE(rate->mcs >= 32))
  1093. return 0;
  1094. modulation = rate->mcs & 7;
  1095. streams = (rate->mcs >> 3) + 1;
  1096. bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
  1097. if (modulation < 4)
  1098. bitrate *= (modulation + 1);
  1099. else if (modulation == 4)
  1100. bitrate *= (modulation + 2);
  1101. else
  1102. bitrate *= (modulation + 3);
  1103. bitrate *= streams;
  1104. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1105. bitrate = (bitrate / 9) * 10;
  1106. /* do NOT round down here */
  1107. return (bitrate + 50000) / 100000;
  1108. }
  1109. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  1110. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  1111. enum ieee80211_p2p_attr_id attr,
  1112. u8 *buf, unsigned int bufsize)
  1113. {
  1114. u8 *out = buf;
  1115. u16 attr_remaining = 0;
  1116. bool desired_attr = false;
  1117. u16 desired_len = 0;
  1118. while (len > 0) {
  1119. unsigned int iedatalen;
  1120. unsigned int copy;
  1121. const u8 *iedata;
  1122. if (len < 2)
  1123. return -EILSEQ;
  1124. iedatalen = ies[1];
  1125. if (iedatalen + 2 > len)
  1126. return -EILSEQ;
  1127. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1128. goto cont;
  1129. if (iedatalen < 4)
  1130. goto cont;
  1131. iedata = ies + 2;
  1132. /* check WFA OUI, P2P subtype */
  1133. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1134. iedata[2] != 0x9a || iedata[3] != 0x09)
  1135. goto cont;
  1136. iedatalen -= 4;
  1137. iedata += 4;
  1138. /* check attribute continuation into this IE */
  1139. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1140. if (copy && desired_attr) {
  1141. desired_len += copy;
  1142. if (out) {
  1143. memcpy(out, iedata, min(bufsize, copy));
  1144. out += min(bufsize, copy);
  1145. bufsize -= min(bufsize, copy);
  1146. }
  1147. if (copy == attr_remaining)
  1148. return desired_len;
  1149. }
  1150. attr_remaining -= copy;
  1151. if (attr_remaining)
  1152. goto cont;
  1153. iedatalen -= copy;
  1154. iedata += copy;
  1155. while (iedatalen > 0) {
  1156. u16 attr_len;
  1157. /* P2P attribute ID & size must fit */
  1158. if (iedatalen < 3)
  1159. return -EILSEQ;
  1160. desired_attr = iedata[0] == attr;
  1161. attr_len = get_unaligned_le16(iedata + 1);
  1162. iedatalen -= 3;
  1163. iedata += 3;
  1164. copy = min_t(unsigned int, attr_len, iedatalen);
  1165. if (desired_attr) {
  1166. desired_len += copy;
  1167. if (out) {
  1168. memcpy(out, iedata, min(bufsize, copy));
  1169. out += min(bufsize, copy);
  1170. bufsize -= min(bufsize, copy);
  1171. }
  1172. if (copy == attr_len)
  1173. return desired_len;
  1174. }
  1175. iedata += copy;
  1176. iedatalen -= copy;
  1177. attr_remaining = attr_len - copy;
  1178. }
  1179. cont:
  1180. len -= ies[1] + 2;
  1181. ies += ies[1] + 2;
  1182. }
  1183. if (attr_remaining && desired_attr)
  1184. return -EILSEQ;
  1185. return -ENOENT;
  1186. }
  1187. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1188. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1189. {
  1190. int i;
  1191. for (i = 0; i < n_ids; i++)
  1192. if (ids[i] == id)
  1193. return true;
  1194. return false;
  1195. }
  1196. size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
  1197. const u8 *ids, int n_ids,
  1198. const u8 *after_ric, int n_after_ric,
  1199. size_t offset)
  1200. {
  1201. size_t pos = offset;
  1202. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
  1203. if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
  1204. pos += 2 + ies[pos + 1];
  1205. while (pos < ielen &&
  1206. !ieee80211_id_in_list(after_ric, n_after_ric,
  1207. ies[pos]))
  1208. pos += 2 + ies[pos + 1];
  1209. } else {
  1210. pos += 2 + ies[pos + 1];
  1211. }
  1212. }
  1213. return pos;
  1214. }
  1215. EXPORT_SYMBOL(ieee80211_ie_split_ric);
  1216. bool ieee80211_operating_class_to_band(u8 operating_class,
  1217. enum ieee80211_band *band)
  1218. {
  1219. switch (operating_class) {
  1220. case 112:
  1221. case 115 ... 127:
  1222. case 128 ... 130:
  1223. *band = IEEE80211_BAND_5GHZ;
  1224. return true;
  1225. case 81:
  1226. case 82:
  1227. case 83:
  1228. case 84:
  1229. *band = IEEE80211_BAND_2GHZ;
  1230. return true;
  1231. case 180:
  1232. *band = IEEE80211_BAND_60GHZ;
  1233. return true;
  1234. }
  1235. return false;
  1236. }
  1237. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1238. bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
  1239. u8 *op_class)
  1240. {
  1241. u8 vht_opclass;
  1242. u16 freq = chandef->center_freq1;
  1243. if (freq >= 2412 && freq <= 2472) {
  1244. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1245. return false;
  1246. /* 2.407 GHz, channels 1..13 */
  1247. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1248. if (freq > chandef->chan->center_freq)
  1249. *op_class = 83; /* HT40+ */
  1250. else
  1251. *op_class = 84; /* HT40- */
  1252. } else {
  1253. *op_class = 81;
  1254. }
  1255. return true;
  1256. }
  1257. if (freq == 2484) {
  1258. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1259. return false;
  1260. *op_class = 82; /* channel 14 */
  1261. return true;
  1262. }
  1263. switch (chandef->width) {
  1264. case NL80211_CHAN_WIDTH_80:
  1265. vht_opclass = 128;
  1266. break;
  1267. case NL80211_CHAN_WIDTH_160:
  1268. vht_opclass = 129;
  1269. break;
  1270. case NL80211_CHAN_WIDTH_80P80:
  1271. vht_opclass = 130;
  1272. break;
  1273. case NL80211_CHAN_WIDTH_10:
  1274. case NL80211_CHAN_WIDTH_5:
  1275. return false; /* unsupported for now */
  1276. default:
  1277. vht_opclass = 0;
  1278. break;
  1279. }
  1280. /* 5 GHz, channels 36..48 */
  1281. if (freq >= 5180 && freq <= 5240) {
  1282. if (vht_opclass) {
  1283. *op_class = vht_opclass;
  1284. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1285. if (freq > chandef->chan->center_freq)
  1286. *op_class = 116;
  1287. else
  1288. *op_class = 117;
  1289. } else {
  1290. *op_class = 115;
  1291. }
  1292. return true;
  1293. }
  1294. /* 5 GHz, channels 52..64 */
  1295. if (freq >= 5260 && freq <= 5320) {
  1296. if (vht_opclass) {
  1297. *op_class = vht_opclass;
  1298. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1299. if (freq > chandef->chan->center_freq)
  1300. *op_class = 119;
  1301. else
  1302. *op_class = 120;
  1303. } else {
  1304. *op_class = 118;
  1305. }
  1306. return true;
  1307. }
  1308. /* 5 GHz, channels 100..144 */
  1309. if (freq >= 5500 && freq <= 5720) {
  1310. if (vht_opclass) {
  1311. *op_class = vht_opclass;
  1312. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1313. if (freq > chandef->chan->center_freq)
  1314. *op_class = 122;
  1315. else
  1316. *op_class = 123;
  1317. } else {
  1318. *op_class = 121;
  1319. }
  1320. return true;
  1321. }
  1322. /* 5 GHz, channels 149..169 */
  1323. if (freq >= 5745 && freq <= 5845) {
  1324. if (vht_opclass) {
  1325. *op_class = vht_opclass;
  1326. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1327. if (freq > chandef->chan->center_freq)
  1328. *op_class = 126;
  1329. else
  1330. *op_class = 127;
  1331. } else if (freq <= 5805) {
  1332. *op_class = 124;
  1333. } else {
  1334. *op_class = 125;
  1335. }
  1336. return true;
  1337. }
  1338. /* 56.16 GHz, channel 1..4 */
  1339. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  1340. if (chandef->width >= NL80211_CHAN_WIDTH_40)
  1341. return false;
  1342. *op_class = 180;
  1343. return true;
  1344. }
  1345. /* not supported yet */
  1346. return false;
  1347. }
  1348. EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
  1349. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  1350. u32 beacon_int)
  1351. {
  1352. struct wireless_dev *wdev;
  1353. int res = 0;
  1354. if (!beacon_int)
  1355. return -EINVAL;
  1356. list_for_each_entry(wdev, &rdev->wdev_list, list) {
  1357. if (!wdev->beacon_interval)
  1358. continue;
  1359. if (wdev->beacon_interval != beacon_int) {
  1360. res = -EINVAL;
  1361. break;
  1362. }
  1363. }
  1364. return res;
  1365. }
  1366. int cfg80211_iter_combinations(struct wiphy *wiphy,
  1367. const int num_different_channels,
  1368. const u8 radar_detect,
  1369. const int iftype_num[NUM_NL80211_IFTYPES],
  1370. void (*iter)(const struct ieee80211_iface_combination *c,
  1371. void *data),
  1372. void *data)
  1373. {
  1374. const struct ieee80211_regdomain *regdom;
  1375. enum nl80211_dfs_regions region = 0;
  1376. int i, j, iftype;
  1377. int num_interfaces = 0;
  1378. u32 used_iftypes = 0;
  1379. if (radar_detect) {
  1380. rcu_read_lock();
  1381. regdom = rcu_dereference(cfg80211_regdomain);
  1382. if (regdom)
  1383. region = regdom->dfs_region;
  1384. rcu_read_unlock();
  1385. }
  1386. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1387. num_interfaces += iftype_num[iftype];
  1388. if (iftype_num[iftype] > 0 &&
  1389. !(wiphy->software_iftypes & BIT(iftype)))
  1390. used_iftypes |= BIT(iftype);
  1391. }
  1392. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  1393. const struct ieee80211_iface_combination *c;
  1394. struct ieee80211_iface_limit *limits;
  1395. u32 all_iftypes = 0;
  1396. c = &wiphy->iface_combinations[i];
  1397. if (num_interfaces > c->max_interfaces)
  1398. continue;
  1399. if (num_different_channels > c->num_different_channels)
  1400. continue;
  1401. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  1402. GFP_KERNEL);
  1403. if (!limits)
  1404. return -ENOMEM;
  1405. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1406. if (wiphy->software_iftypes & BIT(iftype))
  1407. continue;
  1408. for (j = 0; j < c->n_limits; j++) {
  1409. all_iftypes |= limits[j].types;
  1410. if (!(limits[j].types & BIT(iftype)))
  1411. continue;
  1412. if (limits[j].max < iftype_num[iftype])
  1413. goto cont;
  1414. limits[j].max -= iftype_num[iftype];
  1415. }
  1416. }
  1417. if (radar_detect != (c->radar_detect_widths & radar_detect))
  1418. goto cont;
  1419. if (radar_detect && c->radar_detect_regions &&
  1420. !(c->radar_detect_regions & BIT(region)))
  1421. goto cont;
  1422. /* Finally check that all iftypes that we're currently
  1423. * using are actually part of this combination. If they
  1424. * aren't then we can't use this combination and have
  1425. * to continue to the next.
  1426. */
  1427. if ((all_iftypes & used_iftypes) != used_iftypes)
  1428. goto cont;
  1429. /* This combination covered all interface types and
  1430. * supported the requested numbers, so we're good.
  1431. */
  1432. (*iter)(c, data);
  1433. cont:
  1434. kfree(limits);
  1435. }
  1436. return 0;
  1437. }
  1438. EXPORT_SYMBOL(cfg80211_iter_combinations);
  1439. static void
  1440. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  1441. void *data)
  1442. {
  1443. int *num = data;
  1444. (*num)++;
  1445. }
  1446. int cfg80211_check_combinations(struct wiphy *wiphy,
  1447. const int num_different_channels,
  1448. const u8 radar_detect,
  1449. const int iftype_num[NUM_NL80211_IFTYPES])
  1450. {
  1451. int err, num = 0;
  1452. err = cfg80211_iter_combinations(wiphy, num_different_channels,
  1453. radar_detect, iftype_num,
  1454. cfg80211_iter_sum_ifcombs, &num);
  1455. if (err)
  1456. return err;
  1457. if (num == 0)
  1458. return -EBUSY;
  1459. return 0;
  1460. }
  1461. EXPORT_SYMBOL(cfg80211_check_combinations);
  1462. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  1463. const u8 *rates, unsigned int n_rates,
  1464. u32 *mask)
  1465. {
  1466. int i, j;
  1467. if (!sband)
  1468. return -EINVAL;
  1469. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  1470. return -EINVAL;
  1471. *mask = 0;
  1472. for (i = 0; i < n_rates; i++) {
  1473. int rate = (rates[i] & 0x7f) * 5;
  1474. bool found = false;
  1475. for (j = 0; j < sband->n_bitrates; j++) {
  1476. if (sband->bitrates[j].bitrate == rate) {
  1477. found = true;
  1478. *mask |= BIT(j);
  1479. break;
  1480. }
  1481. }
  1482. if (!found)
  1483. return -EINVAL;
  1484. }
  1485. /*
  1486. * mask must have at least one bit set here since we
  1487. * didn't accept a 0-length rates array nor allowed
  1488. * entries in the array that didn't exist
  1489. */
  1490. return 0;
  1491. }
  1492. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  1493. {
  1494. enum ieee80211_band band;
  1495. unsigned int n_channels = 0;
  1496. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  1497. if (wiphy->bands[band])
  1498. n_channels += wiphy->bands[band]->n_channels;
  1499. return n_channels;
  1500. }
  1501. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  1502. int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
  1503. struct station_info *sinfo)
  1504. {
  1505. struct cfg80211_registered_device *rdev;
  1506. struct wireless_dev *wdev;
  1507. wdev = dev->ieee80211_ptr;
  1508. if (!wdev)
  1509. return -EOPNOTSUPP;
  1510. rdev = wiphy_to_rdev(wdev->wiphy);
  1511. if (!rdev->ops->get_station)
  1512. return -EOPNOTSUPP;
  1513. return rdev_get_station(rdev, dev, mac_addr, sinfo);
  1514. }
  1515. EXPORT_SYMBOL(cfg80211_get_station);
  1516. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  1517. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  1518. const unsigned char rfc1042_header[] __aligned(2) =
  1519. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1520. EXPORT_SYMBOL(rfc1042_header);
  1521. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  1522. const unsigned char bridge_tunnel_header[] __aligned(2) =
  1523. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  1524. EXPORT_SYMBOL(bridge_tunnel_header);