util.c 30 KB

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