util.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * Wireless utility functions
  3. *
  4. * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/slab.h>
  9. #include <net/cfg80211.h>
  10. #include <net/ip.h>
  11. #include "core.h"
  12. struct ieee80211_rate *
  13. ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  14. u32 basic_rates, int bitrate)
  15. {
  16. struct ieee80211_rate *result = &sband->bitrates[0];
  17. int i;
  18. for (i = 0; i < sband->n_bitrates; i++) {
  19. if (!(basic_rates & BIT(i)))
  20. continue;
  21. if (sband->bitrates[i].bitrate > bitrate)
  22. continue;
  23. result = &sband->bitrates[i];
  24. }
  25. return result;
  26. }
  27. EXPORT_SYMBOL(ieee80211_get_response_rate);
  28. int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
  29. {
  30. /* see 802.11 17.3.8.3.2 and Annex J
  31. * there are overlapping channel numbers in 5GHz and 2GHz bands */
  32. if (band == IEEE80211_BAND_5GHZ) {
  33. if (chan >= 182 && chan <= 196)
  34. return 4000 + chan * 5;
  35. else
  36. return 5000 + chan * 5;
  37. } else { /* IEEE80211_BAND_2GHZ */
  38. if (chan == 14)
  39. return 2484;
  40. else if (chan < 14)
  41. return 2407 + chan * 5;
  42. else
  43. return 0; /* not supported */
  44. }
  45. }
  46. EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  47. int ieee80211_frequency_to_channel(int freq)
  48. {
  49. /* see 802.11 17.3.8.3.2 and Annex J */
  50. if (freq == 2484)
  51. return 14;
  52. else if (freq < 2484)
  53. return (freq - 2407) / 5;
  54. else if (freq >= 4910 && freq <= 4980)
  55. return (freq - 4000) / 5;
  56. else
  57. return (freq - 5000) / 5;
  58. }
  59. EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  60. struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
  61. int freq)
  62. {
  63. enum ieee80211_band band;
  64. struct ieee80211_supported_band *sband;
  65. int i;
  66. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  67. sband = wiphy->bands[band];
  68. if (!sband)
  69. continue;
  70. for (i = 0; i < sband->n_channels; i++) {
  71. if (sband->channels[i].center_freq == freq)
  72. return &sband->channels[i];
  73. }
  74. }
  75. return NULL;
  76. }
  77. EXPORT_SYMBOL(__ieee80211_get_channel);
  78. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
  79. enum ieee80211_band band)
  80. {
  81. int i, want;
  82. switch (band) {
  83. case IEEE80211_BAND_5GHZ:
  84. want = 3;
  85. for (i = 0; i < sband->n_bitrates; i++) {
  86. if (sband->bitrates[i].bitrate == 60 ||
  87. sband->bitrates[i].bitrate == 120 ||
  88. sband->bitrates[i].bitrate == 240) {
  89. sband->bitrates[i].flags |=
  90. IEEE80211_RATE_MANDATORY_A;
  91. want--;
  92. }
  93. }
  94. WARN_ON(want);
  95. break;
  96. case IEEE80211_BAND_2GHZ:
  97. want = 7;
  98. for (i = 0; i < sband->n_bitrates; i++) {
  99. if (sband->bitrates[i].bitrate == 10) {
  100. sband->bitrates[i].flags |=
  101. IEEE80211_RATE_MANDATORY_B |
  102. IEEE80211_RATE_MANDATORY_G;
  103. want--;
  104. }
  105. if (sband->bitrates[i].bitrate == 20 ||
  106. sband->bitrates[i].bitrate == 55 ||
  107. sband->bitrates[i].bitrate == 110 ||
  108. sband->bitrates[i].bitrate == 60 ||
  109. sband->bitrates[i].bitrate == 120 ||
  110. sband->bitrates[i].bitrate == 240) {
  111. sband->bitrates[i].flags |=
  112. IEEE80211_RATE_MANDATORY_G;
  113. want--;
  114. }
  115. if (sband->bitrates[i].bitrate != 10 &&
  116. sband->bitrates[i].bitrate != 20 &&
  117. sband->bitrates[i].bitrate != 55 &&
  118. sband->bitrates[i].bitrate != 110)
  119. sband->bitrates[i].flags |=
  120. IEEE80211_RATE_ERP_G;
  121. }
  122. WARN_ON(want != 0 && want != 3 && want != 6);
  123. break;
  124. case IEEE80211_NUM_BANDS:
  125. WARN_ON(1);
  126. break;
  127. }
  128. }
  129. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  130. {
  131. enum ieee80211_band band;
  132. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  133. if (wiphy->bands[band])
  134. set_mandatory_flags_band(wiphy->bands[band], band);
  135. }
  136. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  137. struct key_params *params, int key_idx,
  138. bool pairwise, const u8 *mac_addr)
  139. {
  140. int i;
  141. if (key_idx > 5)
  142. return -EINVAL;
  143. if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  144. return -EINVAL;
  145. if (pairwise && !mac_addr)
  146. return -EINVAL;
  147. /*
  148. * Disallow pairwise keys with non-zero index unless it's WEP
  149. * or a vendor specific cipher (because current deployments use
  150. * pairwise WEP keys with non-zero indices and for vendor specific
  151. * ciphers this should be validated in the driver or hardware level
  152. * - but 802.11i clearly specifies to use zero)
  153. */
  154. if (pairwise && key_idx &&
  155. ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
  156. (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
  157. (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
  158. return -EINVAL;
  159. switch (params->cipher) {
  160. case WLAN_CIPHER_SUITE_WEP40:
  161. if (params->key_len != WLAN_KEY_LEN_WEP40)
  162. return -EINVAL;
  163. break;
  164. case WLAN_CIPHER_SUITE_TKIP:
  165. if (params->key_len != WLAN_KEY_LEN_TKIP)
  166. return -EINVAL;
  167. break;
  168. case WLAN_CIPHER_SUITE_CCMP:
  169. if (params->key_len != WLAN_KEY_LEN_CCMP)
  170. return -EINVAL;
  171. break;
  172. case WLAN_CIPHER_SUITE_WEP104:
  173. if (params->key_len != WLAN_KEY_LEN_WEP104)
  174. return -EINVAL;
  175. break;
  176. case WLAN_CIPHER_SUITE_AES_CMAC:
  177. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  178. return -EINVAL;
  179. break;
  180. default:
  181. /*
  182. * We don't know anything about this algorithm,
  183. * allow using it -- but the driver must check
  184. * all parameters! We still check below whether
  185. * or not the driver supports this algorithm,
  186. * of course.
  187. */
  188. break;
  189. }
  190. if (params->seq) {
  191. switch (params->cipher) {
  192. case WLAN_CIPHER_SUITE_WEP40:
  193. case WLAN_CIPHER_SUITE_WEP104:
  194. /* These ciphers do not use key sequence */
  195. return -EINVAL;
  196. case WLAN_CIPHER_SUITE_TKIP:
  197. case WLAN_CIPHER_SUITE_CCMP:
  198. case WLAN_CIPHER_SUITE_AES_CMAC:
  199. if (params->seq_len != 6)
  200. return -EINVAL;
  201. break;
  202. }
  203. }
  204. for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
  205. if (params->cipher == rdev->wiphy.cipher_suites[i])
  206. break;
  207. if (i == rdev->wiphy.n_cipher_suites)
  208. return -EINVAL;
  209. return 0;
  210. }
  211. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  212. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  213. const unsigned char rfc1042_header[] __aligned(2) =
  214. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  215. EXPORT_SYMBOL(rfc1042_header);
  216. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  217. const unsigned char bridge_tunnel_header[] __aligned(2) =
  218. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  219. EXPORT_SYMBOL(bridge_tunnel_header);
  220. unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
  221. {
  222. unsigned int hdrlen = 24;
  223. if (ieee80211_is_data(fc)) {
  224. if (ieee80211_has_a4(fc))
  225. hdrlen = 30;
  226. if (ieee80211_is_data_qos(fc)) {
  227. hdrlen += IEEE80211_QOS_CTL_LEN;
  228. if (ieee80211_has_order(fc))
  229. hdrlen += IEEE80211_HT_CTL_LEN;
  230. }
  231. goto out;
  232. }
  233. if (ieee80211_is_ctl(fc)) {
  234. /*
  235. * ACK and CTS are 10 bytes, all others 16. To see how
  236. * to get this condition consider
  237. * subtype mask: 0b0000000011110000 (0x00F0)
  238. * ACK subtype: 0b0000000011010000 (0x00D0)
  239. * CTS subtype: 0b0000000011000000 (0x00C0)
  240. * bits that matter: ^^^ (0x00E0)
  241. * value of those: 0b0000000011000000 (0x00C0)
  242. */
  243. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  244. hdrlen = 10;
  245. else
  246. hdrlen = 16;
  247. }
  248. out:
  249. return hdrlen;
  250. }
  251. EXPORT_SYMBOL(ieee80211_hdrlen);
  252. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  253. {
  254. const struct ieee80211_hdr *hdr =
  255. (const struct ieee80211_hdr *)skb->data;
  256. unsigned int hdrlen;
  257. if (unlikely(skb->len < 10))
  258. return 0;
  259. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  260. if (unlikely(hdrlen > skb->len))
  261. return 0;
  262. return hdrlen;
  263. }
  264. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  265. static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  266. {
  267. int ae = meshhdr->flags & MESH_FLAGS_AE;
  268. /* 7.1.3.5a.2 */
  269. switch (ae) {
  270. case 0:
  271. return 6;
  272. case MESH_FLAGS_AE_A4:
  273. return 12;
  274. case MESH_FLAGS_AE_A5_A6:
  275. return 18;
  276. case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
  277. return 24;
  278. default:
  279. return 6;
  280. }
  281. }
  282. int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
  283. enum nl80211_iftype iftype)
  284. {
  285. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  286. u16 hdrlen, ethertype;
  287. u8 *payload;
  288. u8 dst[ETH_ALEN];
  289. u8 src[ETH_ALEN] __aligned(2);
  290. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  291. return -1;
  292. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  293. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  294. * header
  295. * IEEE 802.11 address fields:
  296. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  297. * 0 0 DA SA BSSID n/a
  298. * 0 1 DA BSSID SA n/a
  299. * 1 0 BSSID SA DA n/a
  300. * 1 1 RA TA DA SA
  301. */
  302. memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
  303. memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
  304. switch (hdr->frame_control &
  305. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  306. case cpu_to_le16(IEEE80211_FCTL_TODS):
  307. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  308. iftype != NL80211_IFTYPE_AP_VLAN &&
  309. iftype != NL80211_IFTYPE_P2P_GO))
  310. return -1;
  311. break;
  312. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  313. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  314. iftype != NL80211_IFTYPE_MESH_POINT &&
  315. iftype != NL80211_IFTYPE_AP_VLAN &&
  316. iftype != NL80211_IFTYPE_STATION))
  317. return -1;
  318. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  319. struct ieee80211s_hdr *meshdr =
  320. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  321. /* make sure meshdr->flags is on the linear part */
  322. if (!pskb_may_pull(skb, hdrlen + 1))
  323. return -1;
  324. if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
  325. skb_copy_bits(skb, hdrlen +
  326. offsetof(struct ieee80211s_hdr, eaddr1),
  327. dst, ETH_ALEN);
  328. skb_copy_bits(skb, hdrlen +
  329. offsetof(struct ieee80211s_hdr, eaddr2),
  330. src, ETH_ALEN);
  331. }
  332. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  333. }
  334. break;
  335. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  336. if ((iftype != NL80211_IFTYPE_STATION &&
  337. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  338. iftype != NL80211_IFTYPE_MESH_POINT) ||
  339. (is_multicast_ether_addr(dst) &&
  340. !compare_ether_addr(src, addr)))
  341. return -1;
  342. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  343. struct ieee80211s_hdr *meshdr =
  344. (struct ieee80211s_hdr *) (skb->data + hdrlen);
  345. /* make sure meshdr->flags is on the linear part */
  346. if (!pskb_may_pull(skb, hdrlen + 1))
  347. return -1;
  348. if (meshdr->flags & MESH_FLAGS_AE_A4)
  349. skb_copy_bits(skb, hdrlen +
  350. offsetof(struct ieee80211s_hdr, eaddr1),
  351. src, ETH_ALEN);
  352. hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
  353. }
  354. break;
  355. case cpu_to_le16(0):
  356. if (iftype != NL80211_IFTYPE_ADHOC)
  357. return -1;
  358. break;
  359. }
  360. if (!pskb_may_pull(skb, hdrlen + 8))
  361. return -1;
  362. payload = skb->data + hdrlen;
  363. ethertype = (payload[6] << 8) | payload[7];
  364. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  365. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  366. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  367. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  368. * replace EtherType */
  369. skb_pull(skb, hdrlen + 6);
  370. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  371. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  372. } else {
  373. struct ethhdr *ehdr;
  374. __be16 len;
  375. skb_pull(skb, hdrlen);
  376. len = htons(skb->len);
  377. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  378. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  379. memcpy(ehdr->h_source, src, ETH_ALEN);
  380. ehdr->h_proto = len;
  381. }
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(ieee80211_data_to_8023);
  385. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  386. enum nl80211_iftype iftype, u8 *bssid, bool qos)
  387. {
  388. struct ieee80211_hdr hdr;
  389. u16 hdrlen, ethertype;
  390. __le16 fc;
  391. const u8 *encaps_data;
  392. int encaps_len, skip_header_bytes;
  393. int nh_pos, h_pos;
  394. int head_need;
  395. if (unlikely(skb->len < ETH_HLEN))
  396. return -EINVAL;
  397. nh_pos = skb_network_header(skb) - skb->data;
  398. h_pos = skb_transport_header(skb) - skb->data;
  399. /* convert Ethernet header to proper 802.11 header (based on
  400. * operation mode) */
  401. ethertype = (skb->data[12] << 8) | skb->data[13];
  402. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  403. switch (iftype) {
  404. case NL80211_IFTYPE_AP:
  405. case NL80211_IFTYPE_AP_VLAN:
  406. case NL80211_IFTYPE_P2P_GO:
  407. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  408. /* DA BSSID SA */
  409. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  410. memcpy(hdr.addr2, addr, ETH_ALEN);
  411. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  412. hdrlen = 24;
  413. break;
  414. case NL80211_IFTYPE_STATION:
  415. case NL80211_IFTYPE_P2P_CLIENT:
  416. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  417. /* BSSID SA DA */
  418. memcpy(hdr.addr1, bssid, ETH_ALEN);
  419. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  420. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  421. hdrlen = 24;
  422. break;
  423. case NL80211_IFTYPE_ADHOC:
  424. /* DA SA BSSID */
  425. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  426. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  427. memcpy(hdr.addr3, bssid, ETH_ALEN);
  428. hdrlen = 24;
  429. break;
  430. default:
  431. return -EOPNOTSUPP;
  432. }
  433. if (qos) {
  434. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  435. hdrlen += 2;
  436. }
  437. hdr.frame_control = fc;
  438. hdr.duration_id = 0;
  439. hdr.seq_ctrl = 0;
  440. skip_header_bytes = ETH_HLEN;
  441. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  442. encaps_data = bridge_tunnel_header;
  443. encaps_len = sizeof(bridge_tunnel_header);
  444. skip_header_bytes -= 2;
  445. } else if (ethertype > 0x600) {
  446. encaps_data = rfc1042_header;
  447. encaps_len = sizeof(rfc1042_header);
  448. skip_header_bytes -= 2;
  449. } else {
  450. encaps_data = NULL;
  451. encaps_len = 0;
  452. }
  453. skb_pull(skb, skip_header_bytes);
  454. nh_pos -= skip_header_bytes;
  455. h_pos -= skip_header_bytes;
  456. head_need = hdrlen + encaps_len - skb_headroom(skb);
  457. if (head_need > 0 || skb_cloned(skb)) {
  458. head_need = max(head_need, 0);
  459. if (head_need)
  460. skb_orphan(skb);
  461. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
  462. pr_err("failed to reallocate Tx buffer\n");
  463. return -ENOMEM;
  464. }
  465. skb->truesize += head_need;
  466. }
  467. if (encaps_data) {
  468. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  469. nh_pos += encaps_len;
  470. h_pos += encaps_len;
  471. }
  472. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  473. nh_pos += hdrlen;
  474. h_pos += hdrlen;
  475. /* Update skb pointers to various headers since this modified frame
  476. * is going to go through Linux networking code that may potentially
  477. * need things like pointer to IP header. */
  478. skb_set_mac_header(skb, 0);
  479. skb_set_network_header(skb, nh_pos);
  480. skb_set_transport_header(skb, h_pos);
  481. return 0;
  482. }
  483. EXPORT_SYMBOL(ieee80211_data_from_8023);
  484. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  485. const u8 *addr, enum nl80211_iftype iftype,
  486. const unsigned int extra_headroom,
  487. bool has_80211_header)
  488. {
  489. struct sk_buff *frame = NULL;
  490. u16 ethertype;
  491. u8 *payload;
  492. const struct ethhdr *eth;
  493. int remaining, err;
  494. u8 dst[ETH_ALEN], src[ETH_ALEN];
  495. if (has_80211_header) {
  496. err = ieee80211_data_to_8023(skb, addr, iftype);
  497. if (err)
  498. goto out;
  499. /* skip the wrapping header */
  500. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  501. if (!eth)
  502. goto out;
  503. } else {
  504. eth = (struct ethhdr *) skb->data;
  505. }
  506. while (skb != frame) {
  507. u8 padding;
  508. __be16 len = eth->h_proto;
  509. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  510. remaining = skb->len;
  511. memcpy(dst, eth->h_dest, ETH_ALEN);
  512. memcpy(src, eth->h_source, ETH_ALEN);
  513. padding = (4 - subframe_len) & 0x3;
  514. /* the last MSDU has no padding */
  515. if (subframe_len > remaining)
  516. goto purge;
  517. skb_pull(skb, sizeof(struct ethhdr));
  518. /* reuse skb for the last subframe */
  519. if (remaining <= subframe_len + padding)
  520. frame = skb;
  521. else {
  522. unsigned int hlen = ALIGN(extra_headroom, 4);
  523. /*
  524. * Allocate and reserve two bytes more for payload
  525. * alignment since sizeof(struct ethhdr) is 14.
  526. */
  527. frame = dev_alloc_skb(hlen + subframe_len + 2);
  528. if (!frame)
  529. goto purge;
  530. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  531. memcpy(skb_put(frame, ntohs(len)), skb->data,
  532. ntohs(len));
  533. eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
  534. padding);
  535. if (!eth) {
  536. dev_kfree_skb(frame);
  537. goto purge;
  538. }
  539. }
  540. skb_reset_network_header(frame);
  541. frame->dev = skb->dev;
  542. frame->priority = skb->priority;
  543. payload = frame->data;
  544. ethertype = (payload[6] << 8) | payload[7];
  545. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  546. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  547. compare_ether_addr(payload,
  548. bridge_tunnel_header) == 0)) {
  549. /* remove RFC1042 or Bridge-Tunnel
  550. * encapsulation and replace EtherType */
  551. skb_pull(frame, 6);
  552. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  553. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  554. } else {
  555. memcpy(skb_push(frame, sizeof(__be16)), &len,
  556. sizeof(__be16));
  557. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  558. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  559. }
  560. __skb_queue_tail(list, frame);
  561. }
  562. return;
  563. purge:
  564. __skb_queue_purge(list);
  565. out:
  566. dev_kfree_skb(skb);
  567. }
  568. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  569. /* Given a data frame determine the 802.1p/1d tag to use. */
  570. unsigned int cfg80211_classify8021d(struct sk_buff *skb)
  571. {
  572. unsigned int dscp;
  573. /* skb->priority values from 256->263 are magic values to
  574. * directly indicate a specific 802.1d priority. This is used
  575. * to allow 802.1d priority to be passed directly in from VLAN
  576. * tags, etc.
  577. */
  578. if (skb->priority >= 256 && skb->priority <= 263)
  579. return skb->priority - 256;
  580. switch (skb->protocol) {
  581. case htons(ETH_P_IP):
  582. dscp = ip_hdr(skb)->tos & 0xfc;
  583. break;
  584. default:
  585. return 0;
  586. }
  587. return dscp >> 5;
  588. }
  589. EXPORT_SYMBOL(cfg80211_classify8021d);
  590. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  591. {
  592. u8 *end, *pos;
  593. pos = bss->information_elements;
  594. if (pos == NULL)
  595. return NULL;
  596. end = pos + bss->len_information_elements;
  597. while (pos + 1 < end) {
  598. if (pos + 2 + pos[1] > end)
  599. break;
  600. if (pos[0] == ie)
  601. return pos;
  602. pos += 2 + pos[1];
  603. }
  604. return NULL;
  605. }
  606. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  607. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  608. {
  609. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  610. struct net_device *dev = wdev->netdev;
  611. int i;
  612. if (!wdev->connect_keys)
  613. return;
  614. for (i = 0; i < 6; i++) {
  615. if (!wdev->connect_keys->params[i].cipher)
  616. continue;
  617. if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
  618. &wdev->connect_keys->params[i])) {
  619. netdev_err(dev, "failed to set key %d\n", i);
  620. continue;
  621. }
  622. if (wdev->connect_keys->def == i)
  623. if (rdev->ops->set_default_key(wdev->wiphy, dev,
  624. i, true, true)) {
  625. netdev_err(dev, "failed to set defkey %d\n", i);
  626. continue;
  627. }
  628. if (wdev->connect_keys->defmgmt == i)
  629. if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
  630. netdev_err(dev, "failed to set mgtdef %d\n", i);
  631. }
  632. kfree(wdev->connect_keys);
  633. wdev->connect_keys = NULL;
  634. }
  635. static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  636. {
  637. struct cfg80211_event *ev;
  638. unsigned long flags;
  639. const u8 *bssid = NULL;
  640. spin_lock_irqsave(&wdev->event_lock, flags);
  641. while (!list_empty(&wdev->event_list)) {
  642. ev = list_first_entry(&wdev->event_list,
  643. struct cfg80211_event, list);
  644. list_del(&ev->list);
  645. spin_unlock_irqrestore(&wdev->event_lock, flags);
  646. wdev_lock(wdev);
  647. switch (ev->type) {
  648. case EVENT_CONNECT_RESULT:
  649. if (!is_zero_ether_addr(ev->cr.bssid))
  650. bssid = ev->cr.bssid;
  651. __cfg80211_connect_result(
  652. wdev->netdev, bssid,
  653. ev->cr.req_ie, ev->cr.req_ie_len,
  654. ev->cr.resp_ie, ev->cr.resp_ie_len,
  655. ev->cr.status,
  656. ev->cr.status == WLAN_STATUS_SUCCESS,
  657. NULL);
  658. break;
  659. case EVENT_ROAMED:
  660. __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
  661. ev->rm.req_ie, ev->rm.req_ie_len,
  662. ev->rm.resp_ie, ev->rm.resp_ie_len);
  663. break;
  664. case EVENT_DISCONNECTED:
  665. __cfg80211_disconnected(wdev->netdev,
  666. ev->dc.ie, ev->dc.ie_len,
  667. ev->dc.reason, true);
  668. break;
  669. case EVENT_IBSS_JOINED:
  670. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
  671. break;
  672. }
  673. wdev_unlock(wdev);
  674. kfree(ev);
  675. spin_lock_irqsave(&wdev->event_lock, flags);
  676. }
  677. spin_unlock_irqrestore(&wdev->event_lock, flags);
  678. }
  679. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  680. {
  681. struct wireless_dev *wdev;
  682. ASSERT_RTNL();
  683. ASSERT_RDEV_LOCK(rdev);
  684. mutex_lock(&rdev->devlist_mtx);
  685. list_for_each_entry(wdev, &rdev->netdev_list, list)
  686. cfg80211_process_wdev_events(wdev);
  687. mutex_unlock(&rdev->devlist_mtx);
  688. }
  689. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  690. struct net_device *dev, enum nl80211_iftype ntype,
  691. u32 *flags, struct vif_params *params)
  692. {
  693. int err;
  694. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  695. ASSERT_RDEV_LOCK(rdev);
  696. /* don't support changing VLANs, you just re-create them */
  697. if (otype == NL80211_IFTYPE_AP_VLAN)
  698. return -EOPNOTSUPP;
  699. if (!rdev->ops->change_virtual_intf ||
  700. !(rdev->wiphy.interface_modes & (1 << ntype)))
  701. return -EOPNOTSUPP;
  702. /* if it's part of a bridge, reject changing type to station/ibss */
  703. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  704. (ntype == NL80211_IFTYPE_ADHOC ||
  705. ntype == NL80211_IFTYPE_STATION ||
  706. ntype == NL80211_IFTYPE_P2P_CLIENT))
  707. return -EBUSY;
  708. if (ntype != otype) {
  709. err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
  710. ntype);
  711. if (err)
  712. return err;
  713. dev->ieee80211_ptr->use_4addr = false;
  714. dev->ieee80211_ptr->mesh_id_up_len = 0;
  715. switch (otype) {
  716. case NL80211_IFTYPE_ADHOC:
  717. cfg80211_leave_ibss(rdev, dev, false);
  718. break;
  719. case NL80211_IFTYPE_STATION:
  720. case NL80211_IFTYPE_P2P_CLIENT:
  721. cfg80211_disconnect(rdev, dev,
  722. WLAN_REASON_DEAUTH_LEAVING, true);
  723. break;
  724. case NL80211_IFTYPE_MESH_POINT:
  725. /* mesh should be handled? */
  726. break;
  727. default:
  728. break;
  729. }
  730. cfg80211_process_rdev_events(rdev);
  731. }
  732. err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
  733. ntype, flags, params);
  734. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  735. if (!err && params && params->use_4addr != -1)
  736. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  737. if (!err) {
  738. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  739. switch (ntype) {
  740. case NL80211_IFTYPE_STATION:
  741. if (dev->ieee80211_ptr->use_4addr)
  742. break;
  743. /* fall through */
  744. case NL80211_IFTYPE_P2P_CLIENT:
  745. case NL80211_IFTYPE_ADHOC:
  746. dev->priv_flags |= IFF_DONT_BRIDGE;
  747. break;
  748. case NL80211_IFTYPE_P2P_GO:
  749. case NL80211_IFTYPE_AP:
  750. case NL80211_IFTYPE_AP_VLAN:
  751. case NL80211_IFTYPE_WDS:
  752. case NL80211_IFTYPE_MESH_POINT:
  753. /* bridging OK */
  754. break;
  755. case NL80211_IFTYPE_MONITOR:
  756. /* monitor can't bridge anyway */
  757. break;
  758. case NL80211_IFTYPE_UNSPECIFIED:
  759. case NUM_NL80211_IFTYPES:
  760. /* not happening */
  761. break;
  762. }
  763. }
  764. return err;
  765. }
  766. u16 cfg80211_calculate_bitrate(struct rate_info *rate)
  767. {
  768. int modulation, streams, bitrate;
  769. if (!(rate->flags & RATE_INFO_FLAGS_MCS))
  770. return rate->legacy;
  771. /* the formula below does only work for MCS values smaller than 32 */
  772. if (rate->mcs >= 32)
  773. return 0;
  774. modulation = rate->mcs & 7;
  775. streams = (rate->mcs >> 3) + 1;
  776. bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
  777. 13500000 : 6500000;
  778. if (modulation < 4)
  779. bitrate *= (modulation + 1);
  780. else if (modulation == 4)
  781. bitrate *= (modulation + 2);
  782. else
  783. bitrate *= (modulation + 3);
  784. bitrate *= streams;
  785. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  786. bitrate = (bitrate / 9) * 10;
  787. /* do NOT round down here */
  788. return (bitrate + 50000) / 100000;
  789. }
  790. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  791. u32 beacon_int)
  792. {
  793. struct wireless_dev *wdev;
  794. int res = 0;
  795. if (!beacon_int)
  796. return -EINVAL;
  797. mutex_lock(&rdev->devlist_mtx);
  798. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  799. if (!wdev->beacon_interval)
  800. continue;
  801. if (wdev->beacon_interval != beacon_int) {
  802. res = -EINVAL;
  803. break;
  804. }
  805. }
  806. mutex_unlock(&rdev->devlist_mtx);
  807. return res;
  808. }
  809. int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
  810. struct wireless_dev *wdev,
  811. enum nl80211_iftype iftype)
  812. {
  813. struct wireless_dev *wdev_iter;
  814. int num[NUM_NL80211_IFTYPES];
  815. int total = 1;
  816. int i, j;
  817. ASSERT_RTNL();
  818. /* Always allow software iftypes */
  819. if (rdev->wiphy.software_iftypes & BIT(iftype))
  820. return 0;
  821. /*
  822. * Drivers will gradually all set this flag, until all
  823. * have it we only enforce for those that set it.
  824. */
  825. if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
  826. return 0;
  827. memset(num, 0, sizeof(num));
  828. num[iftype] = 1;
  829. mutex_lock(&rdev->devlist_mtx);
  830. list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
  831. if (wdev_iter == wdev)
  832. continue;
  833. if (!netif_running(wdev_iter->netdev))
  834. continue;
  835. if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
  836. continue;
  837. num[wdev_iter->iftype]++;
  838. total++;
  839. }
  840. mutex_unlock(&rdev->devlist_mtx);
  841. for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
  842. const struct ieee80211_iface_combination *c;
  843. struct ieee80211_iface_limit *limits;
  844. c = &rdev->wiphy.iface_combinations[i];
  845. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  846. GFP_KERNEL);
  847. if (!limits)
  848. return -ENOMEM;
  849. if (total > c->max_interfaces)
  850. goto cont;
  851. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  852. if (rdev->wiphy.software_iftypes & BIT(iftype))
  853. continue;
  854. for (j = 0; j < c->n_limits; j++) {
  855. if (!(limits[j].types & iftype))
  856. continue;
  857. if (limits[j].max < num[iftype])
  858. goto cont;
  859. limits[j].max -= num[iftype];
  860. }
  861. }
  862. /* yay, it fits */
  863. kfree(limits);
  864. return 0;
  865. cont:
  866. kfree(limits);
  867. }
  868. return -EBUSY;
  869. }
  870. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  871. const u8 *rates, unsigned int n_rates,
  872. u32 *mask)
  873. {
  874. int i, j;
  875. if (!sband)
  876. return -EINVAL;
  877. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  878. return -EINVAL;
  879. *mask = 0;
  880. for (i = 0; i < n_rates; i++) {
  881. int rate = (rates[i] & 0x7f) * 5;
  882. bool found = false;
  883. for (j = 0; j < sband->n_bitrates; j++) {
  884. if (sband->bitrates[j].bitrate == rate) {
  885. found = true;
  886. *mask |= BIT(j);
  887. break;
  888. }
  889. }
  890. if (!found)
  891. return -EINVAL;
  892. }
  893. /*
  894. * mask must have at least one bit set here since we
  895. * didn't accept a 0-length rates array nor allowed
  896. * entries in the array that didn't exist
  897. */
  898. return 0;
  899. }