util.c 35 KB

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