util.c 44 KB

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