util.c 34 KB

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