util.c 41 KB

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