util.c 42 KB

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