util.c 31 KB

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