util.c 38 KB

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