util.c 35 KB

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