1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2001 Atsushi Onoe 5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_inet.h" 33 #include "opt_inet6.h" 34 #include "opt_wlan.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/endian.h> 42 43 #include <sys/socket.h> 44 45 #include <net/bpf.h> 46 #include <net/ethernet.h> 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_llc.h> 50 #include <net/if_media.h> 51 #include <net/if_vlan_var.h> 52 53 #include <net80211/ieee80211_var.h> 54 #include <net80211/ieee80211_regdomain.h> 55 #ifdef IEEE80211_SUPPORT_SUPERG 56 #include <net80211/ieee80211_superg.h> 57 #endif 58 #ifdef IEEE80211_SUPPORT_TDMA 59 #include <net80211/ieee80211_tdma.h> 60 #endif 61 #include <net80211/ieee80211_wds.h> 62 #include <net80211/ieee80211_mesh.h> 63 #include <net80211/ieee80211_vht.h> 64 65 #if defined(INET) || defined(INET6) 66 #include <netinet/in.h> 67 #endif 68 69 #ifdef INET 70 #include <netinet/if_ether.h> 71 #include <netinet/in_systm.h> 72 #include <netinet/ip.h> 73 #endif 74 #ifdef INET6 75 #include <netinet/ip6.h> 76 #endif 77 78 #include <security/mac/mac_framework.h> 79 80 #define ETHER_HEADER_COPY(dst, src) \ 81 memcpy(dst, src, sizeof(struct ether_header)) 82 83 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *, 84 u_int hdrsize, u_int ciphdrsize, u_int mtu); 85 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int); 86 87 #ifdef IEEE80211_DEBUG 88 /* 89 * Decide if an outbound management frame should be 90 * printed when debugging is enabled. This filters some 91 * of the less interesting frames that come frequently 92 * (e.g. beacons). 93 */ 94 static __inline int 95 doprint(struct ieee80211vap *vap, int subtype) 96 { 97 switch (subtype) { 98 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 99 return (vap->iv_opmode == IEEE80211_M_IBSS); 100 } 101 return 1; 102 } 103 #endif 104 105 /* 106 * Transmit a frame to the given destination on the given VAP. 107 * 108 * It's up to the caller to figure out the details of who this 109 * is going to and resolving the node. 110 * 111 * This routine takes care of queuing it for power save, 112 * A-MPDU state stuff, fast-frames state stuff, encapsulation 113 * if required, then passing it up to the driver layer. 114 * 115 * This routine (for now) consumes the mbuf and frees the node 116 * reference; it ideally will return a TX status which reflects 117 * whether the mbuf was consumed or not, so the caller can 118 * free the mbuf (if appropriate) and the node reference (again, 119 * if appropriate.) 120 */ 121 int 122 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m, 123 struct ieee80211_node *ni) 124 { 125 struct ieee80211com *ic = vap->iv_ic; 126 struct ifnet *ifp = vap->iv_ifp; 127 int mcast; 128 129 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 130 (m->m_flags & M_PWR_SAV) == 0) { 131 /* 132 * Station in power save mode; pass the frame 133 * to the 802.11 layer and continue. We'll get 134 * the frame back when the time is right. 135 * XXX lose WDS vap linkage? 136 */ 137 if (ieee80211_pwrsave(ni, m) != 0) 138 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 139 ieee80211_free_node(ni); 140 141 /* 142 * We queued it fine, so tell the upper layer 143 * that we consumed it. 144 */ 145 return (0); 146 } 147 /* calculate priority so drivers can find the tx queue */ 148 if (ieee80211_classify(ni, m)) { 149 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 150 ni->ni_macaddr, NULL, 151 "%s", "classification failure"); 152 vap->iv_stats.is_tx_classify++; 153 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 154 m_freem(m); 155 ieee80211_free_node(ni); 156 157 /* XXX better status? */ 158 return (0); 159 } 160 /* 161 * Stash the node pointer. Note that we do this after 162 * any call to ieee80211_dwds_mcast because that code 163 * uses any existing value for rcvif to identify the 164 * interface it (might have been) received on. 165 */ 166 m->m_pkthdr.rcvif = (void *)ni; 167 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0; 168 169 BPF_MTAP(ifp, m); /* 802.3 tx */ 170 171 /* 172 * Check if A-MPDU tx aggregation is setup or if we 173 * should try to enable it. The sta must be associated 174 * with HT and A-MPDU enabled for use. When the policy 175 * routine decides we should enable A-MPDU we issue an 176 * ADDBA request and wait for a reply. The frame being 177 * encapsulated will go out w/o using A-MPDU, or possibly 178 * it might be collected by the driver and held/retransmit. 179 * The default ic_ampdu_enable routine handles staggering 180 * ADDBA requests in case the receiver NAK's us or we are 181 * otherwise unable to establish a BA stream. 182 * 183 * Don't treat group-addressed frames as candidates for aggregation; 184 * net80211 doesn't support 802.11aa-2012 and so group addressed 185 * frames will always have sequence numbers allocated from the NON_QOS 186 * TID. 187 */ 188 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) && 189 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) { 190 if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) { 191 int tid = WME_AC_TO_TID(M_WME_GETAC(m)); 192 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid]; 193 194 ieee80211_txampdu_count_packet(tap); 195 if (IEEE80211_AMPDU_RUNNING(tap)) { 196 /* 197 * Operational, mark frame for aggregation. 198 * 199 * XXX do tx aggregation here 200 */ 201 m->m_flags |= M_AMPDU_MPDU; 202 } else if (!IEEE80211_AMPDU_REQUESTED(tap) && 203 ic->ic_ampdu_enable(ni, tap)) { 204 /* 205 * Not negotiated yet, request service. 206 */ 207 ieee80211_ampdu_request(ni, tap); 208 /* XXX hold frame for reply? */ 209 } 210 } 211 } 212 213 #ifdef IEEE80211_SUPPORT_SUPERG 214 /* 215 * Check for AMSDU/FF; queue for aggregation 216 * 217 * Note: we don't bother trying to do fast frames or 218 * A-MSDU encapsulation for 802.3 drivers. Now, we 219 * likely could do it for FF (because it's a magic 220 * atheros tunnel LLC type) but I don't think we're going 221 * to really need to. For A-MSDU we'd have to set the 222 * A-MSDU QoS bit in the wifi header, so we just plain 223 * can't do it. 224 * 225 * Strictly speaking, we could actually /do/ A-MSDU / FF 226 * with A-MPDU together which for certain circumstances 227 * is beneficial (eg A-MSDU of TCK ACKs.) However, 228 * I'll ignore that for now so existing behaviour is maintained. 229 * Later on it would be good to make "amsdu + ampdu" configurable. 230 */ 231 else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) { 232 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) { 233 m = ieee80211_amsdu_check(ni, m); 234 if (m == NULL) { 235 /* NB: any ni ref held on stageq */ 236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 237 "%s: amsdu_check queued frame\n", 238 __func__); 239 return (0); 240 } 241 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni, 242 IEEE80211_NODE_FF)) { 243 m = ieee80211_ff_check(ni, m); 244 if (m == NULL) { 245 /* NB: any ni ref held on stageq */ 246 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 247 "%s: ff_check queued frame\n", 248 __func__); 249 return (0); 250 } 251 } 252 } 253 #endif /* IEEE80211_SUPPORT_SUPERG */ 254 255 /* 256 * Grab the TX lock - serialise the TX process from this 257 * point (where TX state is being checked/modified) 258 * through to driver queue. 259 */ 260 IEEE80211_TX_LOCK(ic); 261 262 /* 263 * XXX make the encap and transmit code a separate function 264 * so things like the FF (and later A-MSDU) path can just call 265 * it for flushed frames. 266 */ 267 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) { 268 /* 269 * Encapsulate the packet in prep for transmission. 270 */ 271 m = ieee80211_encap(vap, ni, m); 272 if (m == NULL) { 273 /* NB: stat+msg handled in ieee80211_encap */ 274 IEEE80211_TX_UNLOCK(ic); 275 ieee80211_free_node(ni); 276 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 277 return (ENOBUFS); 278 } 279 } 280 (void) ieee80211_parent_xmitpkt(ic, m); 281 282 /* 283 * Unlock at this point - no need to hold it across 284 * ieee80211_free_node() (ie, the comlock) 285 */ 286 IEEE80211_TX_UNLOCK(ic); 287 ic->ic_lastdata = ticks; 288 289 return (0); 290 } 291 292 293 294 /* 295 * Send the given mbuf through the given vap. 296 * 297 * This consumes the mbuf regardless of whether the transmit 298 * was successful or not. 299 * 300 * This does none of the initial checks that ieee80211_start() 301 * does (eg CAC timeout, interface wakeup) - the caller must 302 * do this first. 303 */ 304 static int 305 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m) 306 { 307 #define IS_DWDS(vap) \ 308 (vap->iv_opmode == IEEE80211_M_WDS && \ 309 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) 310 struct ieee80211com *ic = vap->iv_ic; 311 struct ifnet *ifp = vap->iv_ifp; 312 struct ieee80211_node *ni; 313 struct ether_header *eh; 314 315 /* 316 * Cancel any background scan. 317 */ 318 if (ic->ic_flags & IEEE80211_F_SCAN) 319 ieee80211_cancel_anyscan(vap); 320 /* 321 * Find the node for the destination so we can do 322 * things like power save and fast frames aggregation. 323 * 324 * NB: past this point various code assumes the first 325 * mbuf has the 802.3 header present (and contiguous). 326 */ 327 ni = NULL; 328 if (m->m_len < sizeof(struct ether_header) && 329 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 330 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 331 "discard frame, %s\n", "m_pullup failed"); 332 vap->iv_stats.is_tx_nobuf++; /* XXX */ 333 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 334 return (ENOBUFS); 335 } 336 eh = mtod(m, struct ether_header *); 337 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 338 if (IS_DWDS(vap)) { 339 /* 340 * Only unicast frames from the above go out 341 * DWDS vaps; multicast frames are handled by 342 * dispatching the frame as it comes through 343 * the AP vap (see below). 344 */ 345 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS, 346 eh->ether_dhost, "mcast", "%s", "on DWDS"); 347 vap->iv_stats.is_dwds_mcast++; 348 m_freem(m); 349 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 350 /* XXX better status? */ 351 return (ENOBUFS); 352 } 353 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 354 /* 355 * Spam DWDS vap's w/ multicast traffic. 356 */ 357 /* XXX only if dwds in use? */ 358 ieee80211_dwds_mcast(vap, m); 359 } 360 } 361 #ifdef IEEE80211_SUPPORT_MESH 362 if (vap->iv_opmode != IEEE80211_M_MBSS) { 363 #endif 364 ni = ieee80211_find_txnode(vap, eh->ether_dhost); 365 if (ni == NULL) { 366 /* NB: ieee80211_find_txnode does stat+msg */ 367 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 368 m_freem(m); 369 /* XXX better status? */ 370 return (ENOBUFS); 371 } 372 if (ni->ni_associd == 0 && 373 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) { 374 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 375 eh->ether_dhost, NULL, 376 "sta not associated (type 0x%04x)", 377 htons(eh->ether_type)); 378 vap->iv_stats.is_tx_notassoc++; 379 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 380 m_freem(m); 381 ieee80211_free_node(ni); 382 /* XXX better status? */ 383 return (ENOBUFS); 384 } 385 #ifdef IEEE80211_SUPPORT_MESH 386 } else { 387 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) { 388 /* 389 * Proxy station only if configured. 390 */ 391 if (!ieee80211_mesh_isproxyena(vap)) { 392 IEEE80211_DISCARD_MAC(vap, 393 IEEE80211_MSG_OUTPUT | 394 IEEE80211_MSG_MESH, 395 eh->ether_dhost, NULL, 396 "%s", "proxy not enabled"); 397 vap->iv_stats.is_mesh_notproxy++; 398 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 399 m_freem(m); 400 /* XXX better status? */ 401 return (ENOBUFS); 402 } 403 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 404 "forward frame from DS SA(%6D), DA(%6D)\n", 405 eh->ether_shost, ":", 406 eh->ether_dhost, ":"); 407 ieee80211_mesh_proxy_check(vap, eh->ether_shost); 408 } 409 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m); 410 if (ni == NULL) { 411 /* 412 * NB: ieee80211_mesh_discover holds/disposes 413 * frame (e.g. queueing on path discovery). 414 */ 415 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 416 /* XXX better status? */ 417 return (ENOBUFS); 418 } 419 } 420 #endif 421 422 /* 423 * We've resolved the sender, so attempt to transmit it. 424 */ 425 426 if (vap->iv_state == IEEE80211_S_SLEEP) { 427 /* 428 * In power save; queue frame and then wakeup device 429 * for transmit. 430 */ 431 ic->ic_lastdata = ticks; 432 if (ieee80211_pwrsave(ni, m) != 0) 433 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 434 ieee80211_free_node(ni); 435 ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 436 return (0); 437 } 438 439 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0) 440 return (ENOBUFS); 441 return (0); 442 #undef IS_DWDS 443 } 444 445 /* 446 * Start method for vap's. All packets from the stack come 447 * through here. We handle common processing of the packets 448 * before dispatching them to the underlying device. 449 * 450 * if_transmit() requires that the mbuf be consumed by this call 451 * regardless of the return condition. 452 */ 453 int 454 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m) 455 { 456 struct ieee80211vap *vap = ifp->if_softc; 457 struct ieee80211com *ic = vap->iv_ic; 458 459 /* 460 * No data frames go out unless we're running. 461 * Note in particular this covers CAC and CSA 462 * states (though maybe we should check muting 463 * for CSA). 464 */ 465 if (vap->iv_state != IEEE80211_S_RUN && 466 vap->iv_state != IEEE80211_S_SLEEP) { 467 IEEE80211_LOCK(ic); 468 /* re-check under the com lock to avoid races */ 469 if (vap->iv_state != IEEE80211_S_RUN && 470 vap->iv_state != IEEE80211_S_SLEEP) { 471 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 472 "%s: ignore queue, in %s state\n", 473 __func__, ieee80211_state_name[vap->iv_state]); 474 vap->iv_stats.is_tx_badstate++; 475 IEEE80211_UNLOCK(ic); 476 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 477 m_freem(m); 478 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 479 return (ENETDOWN); 480 } 481 IEEE80211_UNLOCK(ic); 482 } 483 484 /* 485 * Sanitize mbuf flags for net80211 use. We cannot 486 * clear M_PWR_SAV or M_MORE_DATA because these may 487 * be set for frames that are re-submitted from the 488 * power save queue. 489 * 490 * NB: This must be done before ieee80211_classify as 491 * it marks EAPOL in frames with M_EAPOL. 492 */ 493 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA); 494 495 /* 496 * Bump to the packet transmission path. 497 * The mbuf will be consumed here. 498 */ 499 return (ieee80211_start_pkt(vap, m)); 500 } 501 502 void 503 ieee80211_vap_qflush(struct ifnet *ifp) 504 { 505 506 /* Empty for now */ 507 } 508 509 /* 510 * 802.11 raw output routine. 511 * 512 * XXX TODO: this (and other send routines) should correctly 513 * XXX keep the pwr mgmt bit set if it decides to call into the 514 * XXX driver to send a frame whilst the state is SLEEP. 515 * 516 * Otherwise the peer may decide that we're awake and flood us 517 * with traffic we are still too asleep to receive! 518 */ 519 int 520 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni, 521 struct mbuf *m, const struct ieee80211_bpf_params *params) 522 { 523 struct ieee80211com *ic = vap->iv_ic; 524 int error; 525 526 /* 527 * Set node - the caller has taken a reference, so ensure 528 * that the mbuf has the same node value that 529 * it would if it were going via the normal path. 530 */ 531 m->m_pkthdr.rcvif = (void *)ni; 532 533 /* 534 * Attempt to add bpf transmit parameters. 535 * 536 * For now it's ok to fail; the raw_xmit api still takes 537 * them as an option. 538 * 539 * Later on when ic_raw_xmit() has params removed, 540 * they'll have to be added - so fail the transmit if 541 * they can't be. 542 */ 543 if (params) 544 (void) ieee80211_add_xmit_params(m, params); 545 546 error = ic->ic_raw_xmit(ni, m, params); 547 if (error) { 548 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1); 549 ieee80211_free_node(ni); 550 } 551 return (error); 552 } 553 554 /* 555 * 802.11 output routine. This is (currently) used only to 556 * connect bpf write calls to the 802.11 layer for injecting 557 * raw 802.11 frames. 558 */ 559 int 560 ieee80211_output(struct ifnet *ifp, struct mbuf *m, 561 const struct sockaddr *dst, struct route *ro) 562 { 563 #define senderr(e) do { error = (e); goto bad;} while (0) 564 struct ieee80211_node *ni = NULL; 565 struct ieee80211vap *vap; 566 struct ieee80211_frame *wh; 567 struct ieee80211com *ic = NULL; 568 int error; 569 int ret; 570 571 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) { 572 /* 573 * Short-circuit requests if the vap is marked OACTIVE 574 * as this can happen because a packet came down through 575 * ieee80211_start before the vap entered RUN state in 576 * which case it's ok to just drop the frame. This 577 * should not be necessary but callers of if_output don't 578 * check OACTIVE. 579 */ 580 senderr(ENETDOWN); 581 } 582 vap = ifp->if_softc; 583 ic = vap->iv_ic; 584 /* 585 * Hand to the 802.3 code if not tagged as 586 * a raw 802.11 frame. 587 */ 588 if (dst->sa_family != AF_IEEE80211) 589 return vap->iv_output(ifp, m, dst, ro); 590 #ifdef MAC 591 error = mac_ifnet_check_transmit(ifp, m); 592 if (error) 593 senderr(error); 594 #endif 595 if (ifp->if_flags & IFF_MONITOR) 596 senderr(ENETDOWN); 597 if (!IFNET_IS_UP_RUNNING(ifp)) 598 senderr(ENETDOWN); 599 if (vap->iv_state == IEEE80211_S_CAC) { 600 IEEE80211_DPRINTF(vap, 601 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 602 "block %s frame in CAC state\n", "raw data"); 603 vap->iv_stats.is_tx_badstate++; 604 senderr(EIO); /* XXX */ 605 } else if (vap->iv_state == IEEE80211_S_SCAN) 606 senderr(EIO); 607 /* XXX bypass bridge, pfil, carp, etc. */ 608 609 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 610 senderr(EIO); /* XXX */ 611 wh = mtod(m, struct ieee80211_frame *); 612 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 613 IEEE80211_FC0_VERSION_0) 614 senderr(EIO); /* XXX */ 615 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh)) 616 senderr(EIO); /* XXX */ 617 618 /* locate destination node */ 619 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 620 case IEEE80211_FC1_DIR_NODS: 621 case IEEE80211_FC1_DIR_FROMDS: 622 ni = ieee80211_find_txnode(vap, wh->i_addr1); 623 break; 624 case IEEE80211_FC1_DIR_TODS: 625 case IEEE80211_FC1_DIR_DSTODS: 626 ni = ieee80211_find_txnode(vap, wh->i_addr3); 627 break; 628 default: 629 senderr(EIO); /* XXX */ 630 } 631 if (ni == NULL) { 632 /* 633 * Permit packets w/ bpf params through regardless 634 * (see below about sa_len). 635 */ 636 if (dst->sa_len == 0) 637 senderr(EHOSTUNREACH); 638 ni = ieee80211_ref_node(vap->iv_bss); 639 } 640 641 /* 642 * Sanitize mbuf for net80211 flags leaked from above. 643 * 644 * NB: This must be done before ieee80211_classify as 645 * it marks EAPOL in frames with M_EAPOL. 646 */ 647 m->m_flags &= ~M_80211_TX; 648 649 /* calculate priority so drivers can find the tx queue */ 650 /* XXX assumes an 802.3 frame */ 651 if (ieee80211_classify(ni, m)) 652 senderr(EIO); /* XXX */ 653 654 IEEE80211_NODE_STAT(ni, tx_data); 655 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 656 IEEE80211_NODE_STAT(ni, tx_mcast); 657 m->m_flags |= M_MCAST; 658 } else 659 IEEE80211_NODE_STAT(ni, tx_ucast); 660 /* NB: ieee80211_encap does not include 802.11 header */ 661 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len); 662 663 IEEE80211_TX_LOCK(ic); 664 665 /* 666 * NB: DLT_IEEE802_11_RADIO identifies the parameters are 667 * present by setting the sa_len field of the sockaddr (yes, 668 * this is a hack). 669 * NB: we assume sa_data is suitably aligned to cast. 670 */ 671 ret = ieee80211_raw_output(vap, ni, m, 672 (const struct ieee80211_bpf_params *)(dst->sa_len ? 673 dst->sa_data : NULL)); 674 IEEE80211_TX_UNLOCK(ic); 675 return (ret); 676 bad: 677 if (m != NULL) 678 m_freem(m); 679 if (ni != NULL) 680 ieee80211_free_node(ni); 681 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 682 return error; 683 #undef senderr 684 } 685 686 /* 687 * Set the direction field and address fields of an outgoing 688 * frame. Note this should be called early on in constructing 689 * a frame as it sets i_fc[1]; other bits can then be or'd in. 690 */ 691 void 692 ieee80211_send_setup( 693 struct ieee80211_node *ni, 694 struct mbuf *m, 695 int type, int tid, 696 const uint8_t sa[IEEE80211_ADDR_LEN], 697 const uint8_t da[IEEE80211_ADDR_LEN], 698 const uint8_t bssid[IEEE80211_ADDR_LEN]) 699 { 700 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 701 struct ieee80211vap *vap = ni->ni_vap; 702 struct ieee80211_tx_ampdu *tap; 703 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 704 ieee80211_seq seqno; 705 706 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic); 707 708 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 709 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 710 switch (vap->iv_opmode) { 711 case IEEE80211_M_STA: 712 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 713 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 714 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 715 IEEE80211_ADDR_COPY(wh->i_addr3, da); 716 break; 717 case IEEE80211_M_IBSS: 718 case IEEE80211_M_AHDEMO: 719 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 720 IEEE80211_ADDR_COPY(wh->i_addr1, da); 721 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 722 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 723 break; 724 case IEEE80211_M_HOSTAP: 725 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 726 IEEE80211_ADDR_COPY(wh->i_addr1, da); 727 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 728 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 729 break; 730 case IEEE80211_M_WDS: 731 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 732 IEEE80211_ADDR_COPY(wh->i_addr1, da); 733 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 734 IEEE80211_ADDR_COPY(wh->i_addr3, da); 735 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 736 break; 737 case IEEE80211_M_MBSS: 738 #ifdef IEEE80211_SUPPORT_MESH 739 if (IEEE80211_IS_MULTICAST(da)) { 740 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 741 /* XXX next hop */ 742 IEEE80211_ADDR_COPY(wh->i_addr1, da); 743 IEEE80211_ADDR_COPY(wh->i_addr2, 744 vap->iv_myaddr); 745 } else { 746 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 747 IEEE80211_ADDR_COPY(wh->i_addr1, da); 748 IEEE80211_ADDR_COPY(wh->i_addr2, 749 vap->iv_myaddr); 750 IEEE80211_ADDR_COPY(wh->i_addr3, da); 751 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 752 } 753 #endif 754 break; 755 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 756 break; 757 } 758 } else { 759 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 760 IEEE80211_ADDR_COPY(wh->i_addr1, da); 761 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 762 #ifdef IEEE80211_SUPPORT_MESH 763 if (vap->iv_opmode == IEEE80211_M_MBSS) 764 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 765 else 766 #endif 767 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 768 } 769 *(uint16_t *)&wh->i_dur[0] = 0; 770 771 /* 772 * XXX TODO: this is what the TX lock is for. 773 * Here we're incrementing sequence numbers, and they 774 * need to be in lock-step with what the driver is doing 775 * both in TX ordering and crypto encap (IV increment.) 776 * 777 * If the driver does seqno itself, then we can skip 778 * assigning sequence numbers here, and we can avoid 779 * requiring the TX lock. 780 */ 781 tap = &ni->ni_tx_ampdu[tid]; 782 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) { 783 m->m_flags |= M_AMPDU_MPDU; 784 785 /* NB: zero out i_seq field (for s/w encryption etc) */ 786 *(uint16_t *)&wh->i_seq[0] = 0; 787 } else { 788 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK, 789 type & IEEE80211_FC0_SUBTYPE_MASK)) 790 /* 791 * 802.11-2012 9.3.2.10 - QoS multicast frames 792 * come out of a different seqno space. 793 */ 794 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 795 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 796 } else { 797 seqno = ni->ni_txseqs[tid]++; 798 } 799 else 800 seqno = 0; 801 802 *(uint16_t *)&wh->i_seq[0] = 803 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 804 M_SEQNO_SET(m, seqno); 805 } 806 807 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 808 m->m_flags |= M_MCAST; 809 #undef WH4 810 } 811 812 /* 813 * Send a management frame to the specified node. The node pointer 814 * must have a reference as the pointer will be passed to the driver 815 * and potentially held for a long time. If the frame is successfully 816 * dispatched to the driver, then it is responsible for freeing the 817 * reference (and potentially free'ing up any associated storage); 818 * otherwise deal with reclaiming any reference (on error). 819 */ 820 int 821 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type, 822 struct ieee80211_bpf_params *params) 823 { 824 struct ieee80211vap *vap = ni->ni_vap; 825 struct ieee80211com *ic = ni->ni_ic; 826 struct ieee80211_frame *wh; 827 int ret; 828 829 KASSERT(ni != NULL, ("null node")); 830 831 if (vap->iv_state == IEEE80211_S_CAC) { 832 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 833 ni, "block %s frame in CAC state", 834 ieee80211_mgt_subtype_name(type)); 835 vap->iv_stats.is_tx_badstate++; 836 ieee80211_free_node(ni); 837 m_freem(m); 838 return EIO; /* XXX */ 839 } 840 841 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 842 if (m == NULL) { 843 ieee80211_free_node(ni); 844 return ENOMEM; 845 } 846 847 IEEE80211_TX_LOCK(ic); 848 849 wh = mtod(m, struct ieee80211_frame *); 850 ieee80211_send_setup(ni, m, 851 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID, 852 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 853 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 854 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1, 855 "encrypting frame (%s)", __func__); 856 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 857 } 858 m->m_flags |= M_ENCAP; /* mark encapsulated */ 859 860 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?")); 861 M_WME_SETAC(m, params->ibp_pri); 862 863 #ifdef IEEE80211_DEBUG 864 /* avoid printing too many frames */ 865 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) || 866 ieee80211_msg_dumppkts(vap)) { 867 printf("[%s] send %s on channel %u\n", 868 ether_sprintf(wh->i_addr1), 869 ieee80211_mgt_subtype_name(type), 870 ieee80211_chan2ieee(ic, ic->ic_curchan)); 871 } 872 #endif 873 IEEE80211_NODE_STAT(ni, tx_mgmt); 874 875 ret = ieee80211_raw_output(vap, ni, m, params); 876 IEEE80211_TX_UNLOCK(ic); 877 return (ret); 878 } 879 880 static void 881 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg, 882 int status) 883 { 884 struct ieee80211vap *vap = ni->ni_vap; 885 886 wakeup(vap); 887 } 888 889 /* 890 * Send a null data frame to the specified node. If the station 891 * is setup for QoS then a QoS Null Data frame is constructed. 892 * If this is a WDS station then a 4-address frame is constructed. 893 * 894 * NB: the caller is assumed to have setup a node reference 895 * for use; this is necessary to deal with a race condition 896 * when probing for inactive stations. Like ieee80211_mgmt_output 897 * we must cleanup any node reference on error; however we 898 * can safely just unref it as we know it will never be the 899 * last reference to the node. 900 */ 901 int 902 ieee80211_send_nulldata(struct ieee80211_node *ni) 903 { 904 struct ieee80211vap *vap = ni->ni_vap; 905 struct ieee80211com *ic = ni->ni_ic; 906 struct mbuf *m; 907 struct ieee80211_frame *wh; 908 int hdrlen; 909 uint8_t *frm; 910 int ret; 911 912 if (vap->iv_state == IEEE80211_S_CAC) { 913 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 914 ni, "block %s frame in CAC state", "null data"); 915 ieee80211_unref_node(&ni); 916 vap->iv_stats.is_tx_badstate++; 917 return EIO; /* XXX */ 918 } 919 920 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) 921 hdrlen = sizeof(struct ieee80211_qosframe); 922 else 923 hdrlen = sizeof(struct ieee80211_frame); 924 /* NB: only WDS vap's get 4-address frames */ 925 if (vap->iv_opmode == IEEE80211_M_WDS) 926 hdrlen += IEEE80211_ADDR_LEN; 927 if (ic->ic_flags & IEEE80211_F_DATAPAD) 928 hdrlen = roundup(hdrlen, sizeof(uint32_t)); 929 930 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0); 931 if (m == NULL) { 932 /* XXX debug msg */ 933 ieee80211_unref_node(&ni); 934 vap->iv_stats.is_tx_nobuf++; 935 return ENOMEM; 936 } 937 KASSERT(M_LEADINGSPACE(m) >= hdrlen, 938 ("leading space %zd", M_LEADINGSPACE(m))); 939 M_PREPEND(m, hdrlen, M_NOWAIT); 940 if (m == NULL) { 941 /* NB: cannot happen */ 942 ieee80211_free_node(ni); 943 return ENOMEM; 944 } 945 946 IEEE80211_TX_LOCK(ic); 947 948 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */ 949 if (ni->ni_flags & IEEE80211_NODE_QOS) { 950 const int tid = WME_AC_TO_TID(WME_AC_BE); 951 uint8_t *qos; 952 953 ieee80211_send_setup(ni, m, 954 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL, 955 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 956 957 if (vap->iv_opmode == IEEE80211_M_WDS) 958 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 959 else 960 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 961 qos[0] = tid & IEEE80211_QOS_TID; 962 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy) 963 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 964 qos[1] = 0; 965 } else { 966 ieee80211_send_setup(ni, m, 967 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 968 IEEE80211_NONQOS_TID, 969 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 970 } 971 if (vap->iv_opmode != IEEE80211_M_WDS) { 972 /* NB: power management bit is never sent by an AP */ 973 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 974 vap->iv_opmode != IEEE80211_M_HOSTAP) 975 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 976 } 977 if ((ic->ic_flags & IEEE80211_F_SCAN) && 978 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) { 979 ieee80211_add_callback(m, ieee80211_nulldata_transmitted, 980 NULL); 981 } 982 m->m_len = m->m_pkthdr.len = hdrlen; 983 m->m_flags |= M_ENCAP; /* mark encapsulated */ 984 985 M_WME_SETAC(m, WME_AC_BE); 986 987 IEEE80211_NODE_STAT(ni, tx_data); 988 989 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni, 990 "send %snull data frame on channel %u, pwr mgt %s", 991 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "", 992 ieee80211_chan2ieee(ic, ic->ic_curchan), 993 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 994 995 ret = ieee80211_raw_output(vap, ni, m, NULL); 996 IEEE80211_TX_UNLOCK(ic); 997 return (ret); 998 } 999 1000 /* 1001 * Assign priority to a frame based on any vlan tag assigned 1002 * to the station and/or any Diffserv setting in an IP header. 1003 * Finally, if an ACM policy is setup (in station mode) it's 1004 * applied. 1005 */ 1006 int 1007 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m) 1008 { 1009 const struct ether_header *eh = mtod(m, struct ether_header *); 1010 int v_wme_ac, d_wme_ac, ac; 1011 1012 /* 1013 * Always promote PAE/EAPOL frames to high priority. 1014 */ 1015 if (eh->ether_type == htons(ETHERTYPE_PAE)) { 1016 /* NB: mark so others don't need to check header */ 1017 m->m_flags |= M_EAPOL; 1018 ac = WME_AC_VO; 1019 goto done; 1020 } 1021 /* 1022 * Non-qos traffic goes to BE. 1023 */ 1024 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 1025 ac = WME_AC_BE; 1026 goto done; 1027 } 1028 1029 /* 1030 * If node has a vlan tag then all traffic 1031 * to it must have a matching tag. 1032 */ 1033 v_wme_ac = 0; 1034 if (ni->ni_vlan != 0) { 1035 if ((m->m_flags & M_VLANTAG) == 0) { 1036 IEEE80211_NODE_STAT(ni, tx_novlantag); 1037 return 1; 1038 } 1039 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 1040 EVL_VLANOFTAG(ni->ni_vlan)) { 1041 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 1042 return 1; 1043 } 1044 /* map vlan priority to AC */ 1045 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan)); 1046 } 1047 1048 /* XXX m_copydata may be too slow for fast path */ 1049 #ifdef INET 1050 if (eh->ether_type == htons(ETHERTYPE_IP)) { 1051 uint8_t tos; 1052 /* 1053 * IP frame, map the DSCP bits from the TOS field. 1054 */ 1055 /* NB: ip header may not be in first mbuf */ 1056 m_copydata(m, sizeof(struct ether_header) + 1057 offsetof(struct ip, ip_tos), sizeof(tos), &tos); 1058 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1059 d_wme_ac = TID_TO_WME_AC(tos); 1060 } else { 1061 #endif /* INET */ 1062 #ifdef INET6 1063 if (eh->ether_type == htons(ETHERTYPE_IPV6)) { 1064 uint32_t flow; 1065 uint8_t tos; 1066 /* 1067 * IPv6 frame, map the DSCP bits from the traffic class field. 1068 */ 1069 m_copydata(m, sizeof(struct ether_header) + 1070 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), 1071 (caddr_t) &flow); 1072 tos = (uint8_t)(ntohl(flow) >> 20); 1073 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1074 d_wme_ac = TID_TO_WME_AC(tos); 1075 } else { 1076 #endif /* INET6 */ 1077 d_wme_ac = WME_AC_BE; 1078 #ifdef INET6 1079 } 1080 #endif 1081 #ifdef INET 1082 } 1083 #endif 1084 /* 1085 * Use highest priority AC. 1086 */ 1087 if (v_wme_ac > d_wme_ac) 1088 ac = v_wme_ac; 1089 else 1090 ac = d_wme_ac; 1091 1092 /* 1093 * Apply ACM policy. 1094 */ 1095 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) { 1096 static const int acmap[4] = { 1097 WME_AC_BK, /* WME_AC_BE */ 1098 WME_AC_BK, /* WME_AC_BK */ 1099 WME_AC_BE, /* WME_AC_VI */ 1100 WME_AC_VI, /* WME_AC_VO */ 1101 }; 1102 struct ieee80211com *ic = ni->ni_ic; 1103 1104 while (ac != WME_AC_BK && 1105 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 1106 ac = acmap[ac]; 1107 } 1108 done: 1109 M_WME_SETAC(m, ac); 1110 return 0; 1111 } 1112 1113 /* 1114 * Insure there is sufficient contiguous space to encapsulate the 1115 * 802.11 data frame. If room isn't already there, arrange for it. 1116 * Drivers and cipher modules assume we have done the necessary work 1117 * and fail rudely if they don't find the space they need. 1118 */ 1119 struct mbuf * 1120 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize, 1121 struct ieee80211_key *key, struct mbuf *m) 1122 { 1123 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 1124 int needed_space = vap->iv_ic->ic_headroom + hdrsize; 1125 1126 if (key != NULL) { 1127 /* XXX belongs in crypto code? */ 1128 needed_space += key->wk_cipher->ic_header; 1129 /* XXX frags */ 1130 /* 1131 * When crypto is being done in the host we must insure 1132 * the data are writable for the cipher routines; clone 1133 * a writable mbuf chain. 1134 * XXX handle SWMIC specially 1135 */ 1136 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) { 1137 m = m_unshare(m, M_NOWAIT); 1138 if (m == NULL) { 1139 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1140 "%s: cannot get writable mbuf\n", __func__); 1141 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */ 1142 return NULL; 1143 } 1144 } 1145 } 1146 /* 1147 * We know we are called just before stripping an Ethernet 1148 * header and prepending an LLC header. This means we know 1149 * there will be 1150 * sizeof(struct ether_header) - sizeof(struct llc) 1151 * bytes recovered to which we need additional space for the 1152 * 802.11 header and any crypto header. 1153 */ 1154 /* XXX check trailing space and copy instead? */ 1155 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 1156 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 1157 if (n == NULL) { 1158 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1159 "%s: cannot expand storage\n", __func__); 1160 vap->iv_stats.is_tx_nobuf++; 1161 m_freem(m); 1162 return NULL; 1163 } 1164 KASSERT(needed_space <= MHLEN, 1165 ("not enough room, need %u got %d\n", needed_space, MHLEN)); 1166 /* 1167 * Setup new mbuf to have leading space to prepend the 1168 * 802.11 header and any crypto header bits that are 1169 * required (the latter are added when the driver calls 1170 * back to ieee80211_crypto_encap to do crypto encapsulation). 1171 */ 1172 /* NB: must be first 'cuz it clobbers m_data */ 1173 m_move_pkthdr(n, m); 1174 n->m_len = 0; /* NB: m_gethdr does not set */ 1175 n->m_data += needed_space; 1176 /* 1177 * Pull up Ethernet header to create the expected layout. 1178 * We could use m_pullup but that's overkill (i.e. we don't 1179 * need the actual data) and it cannot fail so do it inline 1180 * for speed. 1181 */ 1182 /* NB: struct ether_header is known to be contiguous */ 1183 n->m_len += sizeof(struct ether_header); 1184 m->m_len -= sizeof(struct ether_header); 1185 m->m_data += sizeof(struct ether_header); 1186 /* 1187 * Replace the head of the chain. 1188 */ 1189 n->m_next = m; 1190 m = n; 1191 } 1192 return m; 1193 #undef TO_BE_RECLAIMED 1194 } 1195 1196 /* 1197 * Return the transmit key to use in sending a unicast frame. 1198 * If a unicast key is set we use that. When no unicast key is set 1199 * we fall back to the default transmit key. 1200 */ 1201 static __inline struct ieee80211_key * 1202 ieee80211_crypto_getucastkey(struct ieee80211vap *vap, 1203 struct ieee80211_node *ni) 1204 { 1205 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 1206 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 1207 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 1208 return NULL; 1209 return &vap->iv_nw_keys[vap->iv_def_txkey]; 1210 } else { 1211 return &ni->ni_ucastkey; 1212 } 1213 } 1214 1215 /* 1216 * Return the transmit key to use in sending a multicast frame. 1217 * Multicast traffic always uses the group key which is installed as 1218 * the default tx key. 1219 */ 1220 static __inline struct ieee80211_key * 1221 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap, 1222 struct ieee80211_node *ni) 1223 { 1224 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 1225 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 1226 return NULL; 1227 return &vap->iv_nw_keys[vap->iv_def_txkey]; 1228 } 1229 1230 /* 1231 * Encapsulate an outbound data frame. The mbuf chain is updated. 1232 * If an error is encountered NULL is returned. The caller is required 1233 * to provide a node reference and pullup the ethernet header in the 1234 * first mbuf. 1235 * 1236 * NB: Packet is assumed to be processed by ieee80211_classify which 1237 * marked EAPOL frames w/ M_EAPOL. 1238 */ 1239 struct mbuf * 1240 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni, 1241 struct mbuf *m) 1242 { 1243 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh)) 1244 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc) 1245 struct ieee80211com *ic = ni->ni_ic; 1246 #ifdef IEEE80211_SUPPORT_MESH 1247 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1248 struct ieee80211_meshcntl_ae10 *mc; 1249 struct ieee80211_mesh_route *rt = NULL; 1250 int dir = -1; 1251 #endif 1252 struct ether_header eh; 1253 struct ieee80211_frame *wh; 1254 struct ieee80211_key *key; 1255 struct llc *llc; 1256 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast; 1257 ieee80211_seq seqno; 1258 int meshhdrsize, meshae; 1259 uint8_t *qos; 1260 int is_amsdu = 0; 1261 1262 IEEE80211_TX_LOCK_ASSERT(ic); 1263 1264 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST)); 1265 1266 /* 1267 * Copy existing Ethernet header to a safe place. The 1268 * rest of the code assumes it's ok to strip it when 1269 * reorganizing state for the final encapsulation. 1270 */ 1271 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 1272 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t)); 1273 1274 /* 1275 * Insure space for additional headers. First identify 1276 * transmit key to use in calculating any buffer adjustments 1277 * required. This is also used below to do privacy 1278 * encapsulation work. Then calculate the 802.11 header 1279 * size and any padding required by the driver. 1280 * 1281 * Note key may be NULL if we fall back to the default 1282 * transmit key and that is not set. In that case the 1283 * buffer may not be expanded as needed by the cipher 1284 * routines, but they will/should discard it. 1285 */ 1286 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1287 if (vap->iv_opmode == IEEE80211_M_STA || 1288 !IEEE80211_IS_MULTICAST(eh.ether_dhost) || 1289 (vap->iv_opmode == IEEE80211_M_WDS && 1290 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) 1291 key = ieee80211_crypto_getucastkey(vap, ni); 1292 else 1293 key = ieee80211_crypto_getmcastkey(vap, ni); 1294 if (key == NULL && (m->m_flags & M_EAPOL) == 0) { 1295 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 1296 eh.ether_dhost, 1297 "no default transmit key (%s) deftxkey %u", 1298 __func__, vap->iv_def_txkey); 1299 vap->iv_stats.is_tx_nodefkey++; 1300 goto bad; 1301 } 1302 } else 1303 key = NULL; 1304 /* 1305 * XXX Some ap's don't handle QoS-encapsulated EAPOL 1306 * frames so suppress use. This may be an issue if other 1307 * ap's require all data frames to be QoS-encapsulated 1308 * once negotiated in which case we'll need to make this 1309 * configurable. 1310 * 1311 * Don't send multicast QoS frames. 1312 * Technically multicast frames can be QoS if all stations in the 1313 * BSS are also QoS. 1314 * 1315 * NB: mesh data frames are QoS, including multicast frames. 1316 */ 1317 addqos = 1318 (((is_mcast == 0) && (ni->ni_flags & 1319 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) || 1320 (vap->iv_opmode == IEEE80211_M_MBSS)) && 1321 (m->m_flags & M_EAPOL) == 0; 1322 1323 if (addqos) 1324 hdrsize = sizeof(struct ieee80211_qosframe); 1325 else 1326 hdrsize = sizeof(struct ieee80211_frame); 1327 #ifdef IEEE80211_SUPPORT_MESH 1328 if (vap->iv_opmode == IEEE80211_M_MBSS) { 1329 /* 1330 * Mesh data frames are encapsulated according to the 1331 * rules of Section 11B.8.5 (p.139 of D3.0 spec). 1332 * o Group Addressed data (aka multicast) originating 1333 * at the local sta are sent w/ 3-address format and 1334 * address extension mode 00 1335 * o Individually Addressed data (aka unicast) originating 1336 * at the local sta are sent w/ 4-address format and 1337 * address extension mode 00 1338 * o Group Addressed data forwarded from a non-mesh sta are 1339 * sent w/ 3-address format and address extension mode 01 1340 * o Individually Address data from another sta are sent 1341 * w/ 4-address format and address extension mode 10 1342 */ 1343 is4addr = 0; /* NB: don't use, disable */ 1344 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) { 1345 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost); 1346 KASSERT(rt != NULL, ("route is NULL")); 1347 dir = IEEE80211_FC1_DIR_DSTODS; 1348 hdrsize += IEEE80211_ADDR_LEN; 1349 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 1350 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate, 1351 vap->iv_myaddr)) { 1352 IEEE80211_NOTE_MAC(vap, 1353 IEEE80211_MSG_MESH, 1354 eh.ether_dhost, 1355 "%s", "trying to send to ourself"); 1356 goto bad; 1357 } 1358 meshae = IEEE80211_MESH_AE_10; 1359 meshhdrsize = 1360 sizeof(struct ieee80211_meshcntl_ae10); 1361 } else { 1362 meshae = IEEE80211_MESH_AE_00; 1363 meshhdrsize = 1364 sizeof(struct ieee80211_meshcntl); 1365 } 1366 } else { 1367 dir = IEEE80211_FC1_DIR_FROMDS; 1368 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) { 1369 /* proxy group */ 1370 meshae = IEEE80211_MESH_AE_01; 1371 meshhdrsize = 1372 sizeof(struct ieee80211_meshcntl_ae01); 1373 } else { 1374 /* group */ 1375 meshae = IEEE80211_MESH_AE_00; 1376 meshhdrsize = sizeof(struct ieee80211_meshcntl); 1377 } 1378 } 1379 } else { 1380 #endif 1381 /* 1382 * 4-address frames need to be generated for: 1383 * o packets sent through a WDS vap (IEEE80211_M_WDS) 1384 * o packets sent through a vap marked for relaying 1385 * (e.g. a station operating with dynamic WDS) 1386 */ 1387 is4addr = vap->iv_opmode == IEEE80211_M_WDS || 1388 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) && 1389 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)); 1390 if (is4addr) 1391 hdrsize += IEEE80211_ADDR_LEN; 1392 meshhdrsize = meshae = 0; 1393 #ifdef IEEE80211_SUPPORT_MESH 1394 } 1395 #endif 1396 /* 1397 * Honor driver DATAPAD requirement. 1398 */ 1399 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1400 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1401 else 1402 hdrspace = hdrsize; 1403 1404 if (__predict_true((m->m_flags & M_FF) == 0)) { 1405 /* 1406 * Normal frame. 1407 */ 1408 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m); 1409 if (m == NULL) { 1410 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 1411 goto bad; 1412 } 1413 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 1414 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 1415 llc = mtod(m, struct llc *); 1416 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1417 llc->llc_control = LLC_UI; 1418 llc->llc_snap.org_code[0] = 0; 1419 llc->llc_snap.org_code[1] = 0; 1420 llc->llc_snap.org_code[2] = 0; 1421 llc->llc_snap.ether_type = eh.ether_type; 1422 } else { 1423 #ifdef IEEE80211_SUPPORT_SUPERG 1424 /* 1425 * Aggregated frame. Check if it's for AMSDU or FF. 1426 * 1427 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented 1428 * anywhere for some reason. But, since 11n requires 1429 * AMSDU RX, we can just assume "11n" == "AMSDU". 1430 */ 1431 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__); 1432 if (ieee80211_amsdu_tx_ok(ni)) { 1433 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key); 1434 is_amsdu = 1; 1435 } else { 1436 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key); 1437 } 1438 if (m == NULL) 1439 #endif 1440 goto bad; 1441 } 1442 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 1443 1444 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT); 1445 if (m == NULL) { 1446 vap->iv_stats.is_tx_nobuf++; 1447 goto bad; 1448 } 1449 wh = mtod(m, struct ieee80211_frame *); 1450 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1451 *(uint16_t *)wh->i_dur = 0; 1452 qos = NULL; /* NB: quiet compiler */ 1453 if (is4addr) { 1454 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 1455 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 1456 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1457 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1458 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost); 1459 } else switch (vap->iv_opmode) { 1460 case IEEE80211_M_STA: 1461 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1462 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 1463 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1464 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1465 break; 1466 case IEEE80211_M_IBSS: 1467 case IEEE80211_M_AHDEMO: 1468 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1469 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1470 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1471 /* 1472 * NB: always use the bssid from iv_bss as the 1473 * neighbor's may be stale after an ibss merge 1474 */ 1475 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid); 1476 break; 1477 case IEEE80211_M_HOSTAP: 1478 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1479 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1480 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 1481 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 1482 break; 1483 #ifdef IEEE80211_SUPPORT_MESH 1484 case IEEE80211_M_MBSS: 1485 /* NB: offset by hdrspace to deal with DATAPAD */ 1486 mc = (struct ieee80211_meshcntl_ae10 *) 1487 (mtod(m, uint8_t *) + hdrspace); 1488 wh->i_fc[1] = dir; 1489 switch (meshae) { 1490 case IEEE80211_MESH_AE_00: /* no proxy */ 1491 mc->mc_flags = 0; 1492 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */ 1493 IEEE80211_ADDR_COPY(wh->i_addr1, 1494 ni->ni_macaddr); 1495 IEEE80211_ADDR_COPY(wh->i_addr2, 1496 vap->iv_myaddr); 1497 IEEE80211_ADDR_COPY(wh->i_addr3, 1498 eh.ether_dhost); 1499 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, 1500 eh.ether_shost); 1501 qos =((struct ieee80211_qosframe_addr4 *) 1502 wh)->i_qos; 1503 } else if (dir == IEEE80211_FC1_DIR_FROMDS) { 1504 /* mcast */ 1505 IEEE80211_ADDR_COPY(wh->i_addr1, 1506 eh.ether_dhost); 1507 IEEE80211_ADDR_COPY(wh->i_addr2, 1508 vap->iv_myaddr); 1509 IEEE80211_ADDR_COPY(wh->i_addr3, 1510 eh.ether_shost); 1511 qos = ((struct ieee80211_qosframe *) 1512 wh)->i_qos; 1513 } 1514 break; 1515 case IEEE80211_MESH_AE_01: /* mcast, proxy */ 1516 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1517 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1518 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1519 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr); 1520 mc->mc_flags = 1; 1521 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4, 1522 eh.ether_shost); 1523 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1524 break; 1525 case IEEE80211_MESH_AE_10: /* ucast, proxy */ 1526 KASSERT(rt != NULL, ("route is NULL")); 1527 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop); 1528 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1529 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate); 1530 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr); 1531 mc->mc_flags = IEEE80211_MESH_AE_10; 1532 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost); 1533 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost); 1534 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1535 break; 1536 default: 1537 KASSERT(0, ("meshae %d", meshae)); 1538 break; 1539 } 1540 mc->mc_ttl = ms->ms_ttl; 1541 ms->ms_seq++; 1542 le32enc(mc->mc_seq, ms->ms_seq); 1543 break; 1544 #endif 1545 case IEEE80211_M_WDS: /* NB: is4addr should always be true */ 1546 default: 1547 goto bad; 1548 } 1549 if (m->m_flags & M_MORE_DATA) 1550 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1551 if (addqos) { 1552 int ac, tid; 1553 1554 if (is4addr) { 1555 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1556 /* NB: mesh case handled earlier */ 1557 } else if (vap->iv_opmode != IEEE80211_M_MBSS) 1558 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1559 ac = M_WME_GETAC(m); 1560 /* map from access class/queue to 11e header priorty value */ 1561 tid = WME_AC_TO_TID(ac); 1562 qos[0] = tid & IEEE80211_QOS_TID; 1563 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 1564 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1565 #ifdef IEEE80211_SUPPORT_MESH 1566 if (vap->iv_opmode == IEEE80211_M_MBSS) 1567 qos[1] = IEEE80211_QOS_MC; 1568 else 1569 #endif 1570 qos[1] = 0; 1571 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 1572 1573 /* 1574 * If this is an A-MSDU then ensure we set the 1575 * relevant field. 1576 */ 1577 if (is_amsdu) 1578 qos[0] |= IEEE80211_QOS_AMSDU; 1579 1580 /* 1581 * XXX TODO TX lock is needed for atomic updates of sequence 1582 * numbers. If the driver does it, then don't do it here; 1583 * and we don't need the TX lock held. 1584 */ 1585 if ((m->m_flags & M_AMPDU_MPDU) == 0) { 1586 /* 1587 * 802.11-2012 9.3.2.10 - 1588 * 1589 * If this is a multicast frame then we need 1590 * to ensure that the sequence number comes from 1591 * a separate seqno space and not the TID space. 1592 * 1593 * Otherwise multicast frames may actually cause 1594 * holes in the TX blockack window space and 1595 * upset various things. 1596 */ 1597 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1598 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1599 else 1600 seqno = ni->ni_txseqs[tid]++; 1601 1602 /* 1603 * NB: don't assign a sequence # to potential 1604 * aggregates; we expect this happens at the 1605 * point the frame comes off any aggregation q 1606 * as otherwise we may introduce holes in the 1607 * BA sequence space and/or make window accouting 1608 * more difficult. 1609 * 1610 * XXX may want to control this with a driver 1611 * capability; this may also change when we pull 1612 * aggregation up into net80211 1613 */ 1614 seqno = ni->ni_txseqs[tid]++; 1615 *(uint16_t *)wh->i_seq = 1616 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1617 M_SEQNO_SET(m, seqno); 1618 } else { 1619 /* NB: zero out i_seq field (for s/w encryption etc) */ 1620 *(uint16_t *)wh->i_seq = 0; 1621 } 1622 } else { 1623 /* 1624 * XXX TODO TX lock is needed for atomic updates of sequence 1625 * numbers. If the driver does it, then don't do it here; 1626 * and we don't need the TX lock held. 1627 */ 1628 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1629 *(uint16_t *)wh->i_seq = 1630 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1631 M_SEQNO_SET(m, seqno); 1632 1633 /* 1634 * XXX TODO: we shouldn't allow EAPOL, etc that would 1635 * be forced to be non-QoS traffic to be A-MSDU encapsulated. 1636 */ 1637 if (is_amsdu) 1638 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n", 1639 __func__); 1640 } 1641 1642 /* 1643 * Check if xmit fragmentation is required. 1644 * 1645 * If the hardware does fragmentation offload, then don't bother 1646 * doing it here. 1647 */ 1648 if (IEEE80211_CONF_FRAG_OFFLOAD(ic)) 1649 txfrag = 0; 1650 else 1651 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold && 1652 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1653 (vap->iv_caps & IEEE80211_C_TXFRAG) && 1654 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0); 1655 1656 if (key != NULL) { 1657 /* 1658 * IEEE 802.1X: send EAPOL frames always in the clear. 1659 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 1660 */ 1661 if ((m->m_flags & M_EAPOL) == 0 || 1662 ((vap->iv_flags & IEEE80211_F_WPA) && 1663 (vap->iv_opmode == IEEE80211_M_STA ? 1664 !IEEE80211_KEY_UNDEFINED(key) : 1665 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 1666 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 1667 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) { 1668 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, 1669 eh.ether_dhost, 1670 "%s", "enmic failed, discard frame"); 1671 vap->iv_stats.is_crypto_enmicfail++; 1672 goto bad; 1673 } 1674 } 1675 } 1676 if (txfrag && !ieee80211_fragment(vap, m, hdrsize, 1677 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold)) 1678 goto bad; 1679 1680 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1681 1682 IEEE80211_NODE_STAT(ni, tx_data); 1683 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1684 IEEE80211_NODE_STAT(ni, tx_mcast); 1685 m->m_flags |= M_MCAST; 1686 } else 1687 IEEE80211_NODE_STAT(ni, tx_ucast); 1688 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 1689 1690 return m; 1691 bad: 1692 if (m != NULL) 1693 m_freem(m); 1694 return NULL; 1695 #undef WH4 1696 #undef MC01 1697 } 1698 1699 void 1700 ieee80211_free_mbuf(struct mbuf *m) 1701 { 1702 struct mbuf *next; 1703 1704 if (m == NULL) 1705 return; 1706 1707 do { 1708 next = m->m_nextpkt; 1709 m->m_nextpkt = NULL; 1710 m_freem(m); 1711 } while ((m = next) != NULL); 1712 } 1713 1714 /* 1715 * Fragment the frame according to the specified mtu. 1716 * The size of the 802.11 header (w/o padding) is provided 1717 * so we don't need to recalculate it. We create a new 1718 * mbuf for each fragment and chain it through m_nextpkt; 1719 * we might be able to optimize this by reusing the original 1720 * packet's mbufs but that is significantly more complicated. 1721 */ 1722 static int 1723 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0, 1724 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1725 { 1726 struct ieee80211com *ic = vap->iv_ic; 1727 struct ieee80211_frame *wh, *whf; 1728 struct mbuf *m, *prev; 1729 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1730 u_int hdrspace; 1731 1732 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1733 KASSERT(m0->m_pkthdr.len > mtu, 1734 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1735 1736 /* 1737 * Honor driver DATAPAD requirement. 1738 */ 1739 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1740 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1741 else 1742 hdrspace = hdrsize; 1743 1744 wh = mtod(m0, struct ieee80211_frame *); 1745 /* NB: mark the first frag; it will be propagated below */ 1746 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1747 totalhdrsize = hdrspace + ciphdrsize; 1748 fragno = 1; 1749 off = mtu - ciphdrsize; 1750 remainder = m0->m_pkthdr.len - off; 1751 prev = m0; 1752 do { 1753 fragsize = MIN(totalhdrsize + remainder, mtu); 1754 m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR); 1755 if (m == NULL) 1756 goto bad; 1757 /* leave room to prepend any cipher header */ 1758 m_align(m, fragsize - ciphdrsize); 1759 1760 /* 1761 * Form the header in the fragment. Note that since 1762 * we mark the first fragment with the MORE_FRAG bit 1763 * it automatically is propagated to each fragment; we 1764 * need only clear it on the last fragment (done below). 1765 * NB: frag 1+ dont have Mesh Control field present. 1766 */ 1767 whf = mtod(m, struct ieee80211_frame *); 1768 memcpy(whf, wh, hdrsize); 1769 #ifdef IEEE80211_SUPPORT_MESH 1770 if (vap->iv_opmode == IEEE80211_M_MBSS) { 1771 if (IEEE80211_IS_DSTODS(wh)) 1772 ((struct ieee80211_qosframe_addr4 *) 1773 whf)->i_qos[1] &= ~IEEE80211_QOS_MC; 1774 else 1775 ((struct ieee80211_qosframe *) 1776 whf)->i_qos[1] &= ~IEEE80211_QOS_MC; 1777 } 1778 #endif 1779 *(uint16_t *)&whf->i_seq[0] |= htole16( 1780 (fragno & IEEE80211_SEQ_FRAG_MASK) << 1781 IEEE80211_SEQ_FRAG_SHIFT); 1782 fragno++; 1783 1784 payload = fragsize - totalhdrsize; 1785 /* NB: destination is known to be contiguous */ 1786 1787 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace); 1788 m->m_len = hdrspace + payload; 1789 m->m_pkthdr.len = hdrspace + payload; 1790 m->m_flags |= M_FRAG; 1791 1792 /* chain up the fragment */ 1793 prev->m_nextpkt = m; 1794 prev = m; 1795 1796 /* deduct fragment just formed */ 1797 remainder -= payload; 1798 off += payload; 1799 } while (remainder != 0); 1800 1801 /* set the last fragment */ 1802 m->m_flags |= M_LASTFRAG; 1803 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 1804 1805 /* strip first mbuf now that everything has been copied */ 1806 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 1807 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1808 1809 vap->iv_stats.is_tx_fragframes++; 1810 vap->iv_stats.is_tx_frags += fragno-1; 1811 1812 return 1; 1813 bad: 1814 /* reclaim fragments but leave original frame for caller to free */ 1815 ieee80211_free_mbuf(m0->m_nextpkt); 1816 m0->m_nextpkt = NULL; 1817 return 0; 1818 } 1819 1820 /* 1821 * Add a supported rates element id to a frame. 1822 */ 1823 uint8_t * 1824 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 1825 { 1826 int nrates; 1827 1828 *frm++ = IEEE80211_ELEMID_RATES; 1829 nrates = rs->rs_nrates; 1830 if (nrates > IEEE80211_RATE_SIZE) 1831 nrates = IEEE80211_RATE_SIZE; 1832 *frm++ = nrates; 1833 memcpy(frm, rs->rs_rates, nrates); 1834 return frm + nrates; 1835 } 1836 1837 /* 1838 * Add an extended supported rates element id to a frame. 1839 */ 1840 uint8_t * 1841 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 1842 { 1843 /* 1844 * Add an extended supported rates element if operating in 11g mode. 1845 */ 1846 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 1847 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 1848 *frm++ = IEEE80211_ELEMID_XRATES; 1849 *frm++ = nrates; 1850 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 1851 frm += nrates; 1852 } 1853 return frm; 1854 } 1855 1856 /* 1857 * Add an ssid element to a frame. 1858 */ 1859 uint8_t * 1860 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 1861 { 1862 *frm++ = IEEE80211_ELEMID_SSID; 1863 *frm++ = len; 1864 memcpy(frm, ssid, len); 1865 return frm + len; 1866 } 1867 1868 /* 1869 * Add an erp element to a frame. 1870 */ 1871 static uint8_t * 1872 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic) 1873 { 1874 uint8_t erp; 1875 1876 *frm++ = IEEE80211_ELEMID_ERP; 1877 *frm++ = 1; 1878 erp = 0; 1879 if (ic->ic_nonerpsta != 0) 1880 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 1881 if (ic->ic_flags & IEEE80211_F_USEPROT) 1882 erp |= IEEE80211_ERP_USE_PROTECTION; 1883 if (ic->ic_flags & IEEE80211_F_USEBARKER) 1884 erp |= IEEE80211_ERP_LONG_PREAMBLE; 1885 *frm++ = erp; 1886 return frm; 1887 } 1888 1889 /* 1890 * Add a CFParams element to a frame. 1891 */ 1892 static uint8_t * 1893 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic) 1894 { 1895 #define ADDSHORT(frm, v) do { \ 1896 le16enc(frm, v); \ 1897 frm += 2; \ 1898 } while (0) 1899 *frm++ = IEEE80211_ELEMID_CFPARMS; 1900 *frm++ = 6; 1901 *frm++ = 0; /* CFP count */ 1902 *frm++ = 2; /* CFP period */ 1903 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */ 1904 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */ 1905 return frm; 1906 #undef ADDSHORT 1907 } 1908 1909 static __inline uint8_t * 1910 add_appie(uint8_t *frm, const struct ieee80211_appie *ie) 1911 { 1912 memcpy(frm, ie->ie_data, ie->ie_len); 1913 return frm + ie->ie_len; 1914 } 1915 1916 static __inline uint8_t * 1917 add_ie(uint8_t *frm, const uint8_t *ie) 1918 { 1919 memcpy(frm, ie, 2 + ie[1]); 1920 return frm + 2 + ie[1]; 1921 } 1922 1923 #define WME_OUI_BYTES 0x00, 0x50, 0xf2 1924 /* 1925 * Add a WME information element to a frame. 1926 */ 1927 uint8_t * 1928 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme) 1929 { 1930 static const struct ieee80211_wme_info info = { 1931 .wme_id = IEEE80211_ELEMID_VENDOR, 1932 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1933 .wme_oui = { WME_OUI_BYTES }, 1934 .wme_type = WME_OUI_TYPE, 1935 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1936 .wme_version = WME_VERSION, 1937 .wme_info = 0, 1938 }; 1939 memcpy(frm, &info, sizeof(info)); 1940 return frm + sizeof(info); 1941 } 1942 1943 /* 1944 * Add a WME parameters element to a frame. 1945 */ 1946 static uint8_t * 1947 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme) 1948 { 1949 #define SM(_v, _f) (((_v) << _f##_S) & _f) 1950 #define ADDSHORT(frm, v) do { \ 1951 le16enc(frm, v); \ 1952 frm += 2; \ 1953 } while (0) 1954 /* NB: this works 'cuz a param has an info at the front */ 1955 static const struct ieee80211_wme_info param = { 1956 .wme_id = IEEE80211_ELEMID_VENDOR, 1957 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1958 .wme_oui = { WME_OUI_BYTES }, 1959 .wme_type = WME_OUI_TYPE, 1960 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1961 .wme_version = WME_VERSION, 1962 }; 1963 int i; 1964 1965 memcpy(frm, ¶m, sizeof(param)); 1966 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1967 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1968 *frm++ = 0; /* reserved field */ 1969 for (i = 0; i < WME_NUM_AC; i++) { 1970 const struct wmeParams *ac = 1971 &wme->wme_bssChanParams.cap_wmeParams[i]; 1972 *frm++ = SM(i, WME_PARAM_ACI) 1973 | SM(ac->wmep_acm, WME_PARAM_ACM) 1974 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1975 ; 1976 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1977 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1978 ; 1979 ADDSHORT(frm, ac->wmep_txopLimit); 1980 } 1981 return frm; 1982 #undef SM 1983 #undef ADDSHORT 1984 } 1985 #undef WME_OUI_BYTES 1986 1987 /* 1988 * Add an 11h Power Constraint element to a frame. 1989 */ 1990 static uint8_t * 1991 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap) 1992 { 1993 const struct ieee80211_channel *c = vap->iv_bss->ni_chan; 1994 /* XXX per-vap tx power limit? */ 1995 int8_t limit = vap->iv_ic->ic_txpowlimit / 2; 1996 1997 frm[0] = IEEE80211_ELEMID_PWRCNSTR; 1998 frm[1] = 1; 1999 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0; 2000 return frm + 3; 2001 } 2002 2003 /* 2004 * Add an 11h Power Capability element to a frame. 2005 */ 2006 static uint8_t * 2007 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c) 2008 { 2009 frm[0] = IEEE80211_ELEMID_PWRCAP; 2010 frm[1] = 2; 2011 frm[2] = c->ic_minpower; 2012 frm[3] = c->ic_maxpower; 2013 return frm + 4; 2014 } 2015 2016 /* 2017 * Add an 11h Supported Channels element to a frame. 2018 */ 2019 static uint8_t * 2020 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic) 2021 { 2022 static const int ielen = 26; 2023 2024 frm[0] = IEEE80211_ELEMID_SUPPCHAN; 2025 frm[1] = ielen; 2026 /* XXX not correct */ 2027 memcpy(frm+2, ic->ic_chan_avail, ielen); 2028 return frm + 2 + ielen; 2029 } 2030 2031 /* 2032 * Add an 11h Quiet time element to a frame. 2033 */ 2034 static uint8_t * 2035 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update) 2036 { 2037 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm; 2038 2039 quiet->quiet_ie = IEEE80211_ELEMID_QUIET; 2040 quiet->len = 6; 2041 2042 /* 2043 * Only update every beacon interval - otherwise probe responses 2044 * would update the quiet count value. 2045 */ 2046 if (update) { 2047 if (vap->iv_quiet_count_value == 1) 2048 vap->iv_quiet_count_value = vap->iv_quiet_count; 2049 else if (vap->iv_quiet_count_value > 1) 2050 vap->iv_quiet_count_value--; 2051 } 2052 2053 if (vap->iv_quiet_count_value == 0) { 2054 /* value 0 is reserved as per 802.11h standerd */ 2055 vap->iv_quiet_count_value = 1; 2056 } 2057 2058 quiet->tbttcount = vap->iv_quiet_count_value; 2059 quiet->period = vap->iv_quiet_period; 2060 quiet->duration = htole16(vap->iv_quiet_duration); 2061 quiet->offset = htole16(vap->iv_quiet_offset); 2062 return frm + sizeof(*quiet); 2063 } 2064 2065 /* 2066 * Add an 11h Channel Switch Announcement element to a frame. 2067 * Note that we use the per-vap CSA count to adjust the global 2068 * counter so we can use this routine to form probe response 2069 * frames and get the current count. 2070 */ 2071 static uint8_t * 2072 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap) 2073 { 2074 struct ieee80211com *ic = vap->iv_ic; 2075 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm; 2076 2077 csa->csa_ie = IEEE80211_ELEMID_CSA; 2078 csa->csa_len = 3; 2079 csa->csa_mode = 1; /* XXX force quiet on channel */ 2080 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan); 2081 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count; 2082 return frm + sizeof(*csa); 2083 } 2084 2085 /* 2086 * Add an 11h country information element to a frame. 2087 */ 2088 static uint8_t * 2089 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic) 2090 { 2091 2092 if (ic->ic_countryie == NULL || 2093 ic->ic_countryie_chan != ic->ic_bsschan) { 2094 /* 2095 * Handle lazy construction of ie. This is done on 2096 * first use and after a channel change that requires 2097 * re-calculation. 2098 */ 2099 if (ic->ic_countryie != NULL) 2100 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE); 2101 ic->ic_countryie = ieee80211_alloc_countryie(ic); 2102 if (ic->ic_countryie == NULL) 2103 return frm; 2104 ic->ic_countryie_chan = ic->ic_bsschan; 2105 } 2106 return add_appie(frm, ic->ic_countryie); 2107 } 2108 2109 uint8_t * 2110 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap) 2111 { 2112 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) 2113 return (add_ie(frm, vap->iv_wpa_ie)); 2114 else { 2115 /* XXX else complain? */ 2116 return (frm); 2117 } 2118 } 2119 2120 uint8_t * 2121 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap) 2122 { 2123 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL) 2124 return (add_ie(frm, vap->iv_rsn_ie)); 2125 else { 2126 /* XXX else complain? */ 2127 return (frm); 2128 } 2129 } 2130 2131 uint8_t * 2132 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni) 2133 { 2134 if (ni->ni_flags & IEEE80211_NODE_QOS) { 2135 *frm++ = IEEE80211_ELEMID_QOS; 2136 *frm++ = 1; 2137 *frm++ = 0; 2138 } 2139 2140 return (frm); 2141 } 2142 2143 /* 2144 * Send a probe request frame with the specified ssid 2145 * and any optional information element data. 2146 */ 2147 int 2148 ieee80211_send_probereq(struct ieee80211_node *ni, 2149 const uint8_t sa[IEEE80211_ADDR_LEN], 2150 const uint8_t da[IEEE80211_ADDR_LEN], 2151 const uint8_t bssid[IEEE80211_ADDR_LEN], 2152 const uint8_t *ssid, size_t ssidlen) 2153 { 2154 struct ieee80211vap *vap = ni->ni_vap; 2155 struct ieee80211com *ic = ni->ni_ic; 2156 struct ieee80211_node *bss; 2157 const struct ieee80211_txparam *tp; 2158 struct ieee80211_bpf_params params; 2159 const struct ieee80211_rateset *rs; 2160 struct mbuf *m; 2161 uint8_t *frm; 2162 int ret; 2163 2164 bss = ieee80211_ref_node(vap->iv_bss); 2165 2166 if (vap->iv_state == IEEE80211_S_CAC) { 2167 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 2168 "block %s frame in CAC state", "probe request"); 2169 vap->iv_stats.is_tx_badstate++; 2170 ieee80211_free_node(bss); 2171 return EIO; /* XXX */ 2172 } 2173 2174 /* 2175 * Hold a reference on the node so it doesn't go away until after 2176 * the xmit is complete all the way in the driver. On error we 2177 * will remove our reference. 2178 */ 2179 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2180 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2181 __func__, __LINE__, 2182 ni, ether_sprintf(ni->ni_macaddr), 2183 ieee80211_node_refcnt(ni)+1); 2184 ieee80211_ref_node(ni); 2185 2186 /* 2187 * prreq frame format 2188 * [tlv] ssid 2189 * [tlv] supported rates 2190 * [tlv] RSN (optional) 2191 * [tlv] extended supported rates 2192 * [tlv] HT cap (optional) 2193 * [tlv] VHT cap (optional) 2194 * [tlv] WPA (optional) 2195 * [tlv] user-specified ie's 2196 */ 2197 m = ieee80211_getmgtframe(&frm, 2198 ic->ic_headroom + sizeof(struct ieee80211_frame), 2199 2 + IEEE80211_NWID_LEN 2200 + 2 + IEEE80211_RATE_SIZE 2201 + sizeof(struct ieee80211_ie_htcap) 2202 + sizeof(struct ieee80211_ie_vhtcap) 2203 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */ 2204 + sizeof(struct ieee80211_ie_wpa) 2205 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2206 + sizeof(struct ieee80211_ie_wpa) 2207 + (vap->iv_appie_probereq != NULL ? 2208 vap->iv_appie_probereq->ie_len : 0) 2209 ); 2210 if (m == NULL) { 2211 vap->iv_stats.is_tx_nobuf++; 2212 ieee80211_free_node(ni); 2213 ieee80211_free_node(bss); 2214 return ENOMEM; 2215 } 2216 2217 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 2218 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2219 frm = ieee80211_add_rates(frm, rs); 2220 frm = ieee80211_add_rsn(frm, vap); 2221 frm = ieee80211_add_xrates(frm, rs); 2222 2223 /* 2224 * Note: we can't use bss; we don't have one yet. 2225 * 2226 * So, we should announce our capabilities 2227 * in this channel mode (2g/5g), not the 2228 * channel details itself. 2229 */ 2230 if ((vap->iv_opmode == IEEE80211_M_IBSS) && 2231 (vap->iv_flags_ht & IEEE80211_FHT_HT)) { 2232 struct ieee80211_channel *c; 2233 2234 /* 2235 * Get the HT channel that we should try upgrading to. 2236 * If we can do 40MHz then this'll upgrade it appropriately. 2237 */ 2238 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2239 vap->iv_flags_ht); 2240 frm = ieee80211_add_htcap_ch(frm, vap, c); 2241 } 2242 2243 /* 2244 * XXX TODO: need to figure out what/how to update the 2245 * VHT channel. 2246 */ 2247 #if 0 2248 (vap->iv_flags_vht & IEEE80211_FVHT_VHT) { 2249 struct ieee80211_channel *c; 2250 2251 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2252 vap->iv_flags_ht); 2253 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht); 2254 frm = ieee80211_add_vhtcap_ch(frm, vap, c); 2255 } 2256 #endif 2257 2258 frm = ieee80211_add_wpa(frm, vap); 2259 if (vap->iv_appie_probereq != NULL) 2260 frm = add_appie(frm, vap->iv_appie_probereq); 2261 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2262 2263 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame), 2264 ("leading space %zd", M_LEADINGSPACE(m))); 2265 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 2266 if (m == NULL) { 2267 /* NB: cannot happen */ 2268 ieee80211_free_node(ni); 2269 ieee80211_free_node(bss); 2270 return ENOMEM; 2271 } 2272 2273 IEEE80211_TX_LOCK(ic); 2274 ieee80211_send_setup(ni, m, 2275 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 2276 IEEE80211_NONQOS_TID, sa, da, bssid); 2277 /* XXX power management? */ 2278 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2279 2280 M_WME_SETAC(m, WME_AC_BE); 2281 2282 IEEE80211_NODE_STAT(ni, tx_probereq); 2283 IEEE80211_NODE_STAT(ni, tx_mgmt); 2284 2285 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2286 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n", 2287 ieee80211_chan2ieee(ic, ic->ic_curchan), 2288 ether_sprintf(bssid), 2289 sa, ":", 2290 da, ":", 2291 ssidlen, ssid); 2292 2293 memset(¶ms, 0, sizeof(params)); 2294 params.ibp_pri = M_WME_GETAC(m); 2295 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 2296 params.ibp_rate0 = tp->mgmtrate; 2297 if (IEEE80211_IS_MULTICAST(da)) { 2298 params.ibp_flags |= IEEE80211_BPF_NOACK; 2299 params.ibp_try0 = 1; 2300 } else 2301 params.ibp_try0 = tp->maxretry; 2302 params.ibp_power = ni->ni_txpower; 2303 ret = ieee80211_raw_output(vap, ni, m, ¶ms); 2304 IEEE80211_TX_UNLOCK(ic); 2305 ieee80211_free_node(bss); 2306 return (ret); 2307 } 2308 2309 /* 2310 * Calculate capability information for mgt frames. 2311 */ 2312 uint16_t 2313 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan) 2314 { 2315 struct ieee80211com *ic = vap->iv_ic; 2316 uint16_t capinfo; 2317 2318 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode")); 2319 2320 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2321 capinfo = IEEE80211_CAPINFO_ESS; 2322 else if (vap->iv_opmode == IEEE80211_M_IBSS) 2323 capinfo = IEEE80211_CAPINFO_IBSS; 2324 else 2325 capinfo = 0; 2326 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2327 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2328 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2329 IEEE80211_IS_CHAN_2GHZ(chan)) 2330 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2331 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2332 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2333 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH)) 2334 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2335 return capinfo; 2336 } 2337 2338 /* 2339 * Send a management frame. The node is for the destination (or ic_bss 2340 * when in station mode). Nodes other than ic_bss have their reference 2341 * count bumped to reflect our use for an indeterminant time. 2342 */ 2343 int 2344 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg) 2345 { 2346 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT) 2347 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 2348 struct ieee80211vap *vap = ni->ni_vap; 2349 struct ieee80211com *ic = ni->ni_ic; 2350 struct ieee80211_node *bss = vap->iv_bss; 2351 struct ieee80211_bpf_params params; 2352 struct mbuf *m; 2353 uint8_t *frm; 2354 uint16_t capinfo; 2355 int has_challenge, is_shared_key, ret, status; 2356 2357 KASSERT(ni != NULL, ("null node")); 2358 2359 /* 2360 * Hold a reference on the node so it doesn't go away until after 2361 * the xmit is complete all the way in the driver. On error we 2362 * will remove our reference. 2363 */ 2364 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2365 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2366 __func__, __LINE__, 2367 ni, ether_sprintf(ni->ni_macaddr), 2368 ieee80211_node_refcnt(ni)+1); 2369 ieee80211_ref_node(ni); 2370 2371 memset(¶ms, 0, sizeof(params)); 2372 switch (type) { 2373 2374 case IEEE80211_FC0_SUBTYPE_AUTH: 2375 status = arg >> 16; 2376 arg &= 0xffff; 2377 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 2378 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 2379 ni->ni_challenge != NULL); 2380 2381 /* 2382 * Deduce whether we're doing open authentication or 2383 * shared key authentication. We do the latter if 2384 * we're in the middle of a shared key authentication 2385 * handshake or if we're initiating an authentication 2386 * request and configured to use shared key. 2387 */ 2388 is_shared_key = has_challenge || 2389 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 2390 (arg == IEEE80211_AUTH_SHARED_REQUEST && 2391 bss->ni_authmode == IEEE80211_AUTH_SHARED); 2392 2393 m = ieee80211_getmgtframe(&frm, 2394 ic->ic_headroom + sizeof(struct ieee80211_frame), 2395 3 * sizeof(uint16_t) 2396 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 2397 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0) 2398 ); 2399 if (m == NULL) 2400 senderr(ENOMEM, is_tx_nobuf); 2401 2402 ((uint16_t *)frm)[0] = 2403 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 2404 : htole16(IEEE80211_AUTH_ALG_OPEN); 2405 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 2406 ((uint16_t *)frm)[2] = htole16(status);/* status */ 2407 2408 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 2409 ((uint16_t *)frm)[3] = 2410 htole16((IEEE80211_CHALLENGE_LEN << 8) | 2411 IEEE80211_ELEMID_CHALLENGE); 2412 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 2413 IEEE80211_CHALLENGE_LEN); 2414 m->m_pkthdr.len = m->m_len = 2415 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 2416 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 2417 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2418 "request encrypt frame (%s)", __func__); 2419 /* mark frame for encryption */ 2420 params.ibp_flags |= IEEE80211_BPF_CRYPTO; 2421 } 2422 } else 2423 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 2424 2425 /* XXX not right for shared key */ 2426 if (status == IEEE80211_STATUS_SUCCESS) 2427 IEEE80211_NODE_STAT(ni, tx_auth); 2428 else 2429 IEEE80211_NODE_STAT(ni, tx_auth_fail); 2430 2431 if (vap->iv_opmode == IEEE80211_M_STA) 2432 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2433 (void *) vap->iv_state); 2434 break; 2435 2436 case IEEE80211_FC0_SUBTYPE_DEAUTH: 2437 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2438 "send station deauthenticate (reason: %d (%s))", arg, 2439 ieee80211_reason_to_string(arg)); 2440 m = ieee80211_getmgtframe(&frm, 2441 ic->ic_headroom + sizeof(struct ieee80211_frame), 2442 sizeof(uint16_t)); 2443 if (m == NULL) 2444 senderr(ENOMEM, is_tx_nobuf); 2445 *(uint16_t *)frm = htole16(arg); /* reason */ 2446 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 2447 2448 IEEE80211_NODE_STAT(ni, tx_deauth); 2449 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 2450 2451 ieee80211_node_unauthorize(ni); /* port closed */ 2452 break; 2453 2454 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2455 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 2456 /* 2457 * asreq frame format 2458 * [2] capability information 2459 * [2] listen interval 2460 * [6*] current AP address (reassoc only) 2461 * [tlv] ssid 2462 * [tlv] supported rates 2463 * [tlv] extended supported rates 2464 * [4] power capability (optional) 2465 * [28] supported channels (optional) 2466 * [tlv] HT capabilities 2467 * [tlv] VHT capabilities 2468 * [tlv] WME (optional) 2469 * [tlv] Vendor OUI HT capabilities (optional) 2470 * [tlv] Atheros capabilities (if negotiated) 2471 * [tlv] AppIE's (optional) 2472 */ 2473 m = ieee80211_getmgtframe(&frm, 2474 ic->ic_headroom + sizeof(struct ieee80211_frame), 2475 sizeof(uint16_t) 2476 + sizeof(uint16_t) 2477 + IEEE80211_ADDR_LEN 2478 + 2 + IEEE80211_NWID_LEN 2479 + 2 + IEEE80211_RATE_SIZE 2480 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2481 + 4 2482 + 2 + 26 2483 + sizeof(struct ieee80211_wme_info) 2484 + sizeof(struct ieee80211_ie_htcap) 2485 + sizeof(struct ieee80211_ie_vhtcap) 2486 + 4 + sizeof(struct ieee80211_ie_htcap) 2487 #ifdef IEEE80211_SUPPORT_SUPERG 2488 + sizeof(struct ieee80211_ath_ie) 2489 #endif 2490 + (vap->iv_appie_wpa != NULL ? 2491 vap->iv_appie_wpa->ie_len : 0) 2492 + (vap->iv_appie_assocreq != NULL ? 2493 vap->iv_appie_assocreq->ie_len : 0) 2494 ); 2495 if (m == NULL) 2496 senderr(ENOMEM, is_tx_nobuf); 2497 2498 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 2499 ("wrong mode %u", vap->iv_opmode)); 2500 capinfo = IEEE80211_CAPINFO_ESS; 2501 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2502 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2503 /* 2504 * NB: Some 11a AP's reject the request when 2505 * short preamble is set. 2506 */ 2507 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2508 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 2509 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2510 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 2511 (ic->ic_caps & IEEE80211_C_SHSLOT)) 2512 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2513 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) && 2514 (vap->iv_flags & IEEE80211_F_DOTH)) 2515 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2516 *(uint16_t *)frm = htole16(capinfo); 2517 frm += 2; 2518 2519 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!")); 2520 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 2521 bss->ni_intval)); 2522 frm += 2; 2523 2524 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 2525 IEEE80211_ADDR_COPY(frm, bss->ni_bssid); 2526 frm += IEEE80211_ADDR_LEN; 2527 } 2528 2529 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 2530 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2531 frm = ieee80211_add_rsn(frm, vap); 2532 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2533 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) { 2534 frm = ieee80211_add_powercapability(frm, 2535 ic->ic_curchan); 2536 frm = ieee80211_add_supportedchannels(frm, ic); 2537 } 2538 2539 /* 2540 * Check the channel - we may be using an 11n NIC with an 2541 * 11n capable station, but we're configured to be an 11b 2542 * channel. 2543 */ 2544 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2545 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2546 ni->ni_ies.htcap_ie != NULL && 2547 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) { 2548 frm = ieee80211_add_htcap(frm, ni); 2549 } 2550 2551 if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) && 2552 IEEE80211_IS_CHAN_VHT(ni->ni_chan) && 2553 ni->ni_ies.vhtcap_ie != NULL && 2554 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) { 2555 frm = ieee80211_add_vhtcap(frm, ni); 2556 } 2557 2558 frm = ieee80211_add_wpa(frm, vap); 2559 if ((ic->ic_flags & IEEE80211_F_WME) && 2560 ni->ni_ies.wme_ie != NULL) 2561 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 2562 2563 /* 2564 * Same deal - only send HT info if we're on an 11n 2565 * capable channel. 2566 */ 2567 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2568 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2569 ni->ni_ies.htcap_ie != NULL && 2570 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) { 2571 frm = ieee80211_add_htcap_vendor(frm, ni); 2572 } 2573 #ifdef IEEE80211_SUPPORT_SUPERG 2574 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) { 2575 frm = ieee80211_add_ath(frm, 2576 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2577 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 2578 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 2579 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 2580 } 2581 #endif /* IEEE80211_SUPPORT_SUPERG */ 2582 if (vap->iv_appie_assocreq != NULL) 2583 frm = add_appie(frm, vap->iv_appie_assocreq); 2584 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2585 2586 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2587 (void *) vap->iv_state); 2588 break; 2589 2590 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2591 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 2592 /* 2593 * asresp frame format 2594 * [2] capability information 2595 * [2] status 2596 * [2] association ID 2597 * [tlv] supported rates 2598 * [tlv] extended supported rates 2599 * [tlv] HT capabilities (standard, if STA enabled) 2600 * [tlv] HT information (standard, if STA enabled) 2601 * [tlv] VHT capabilities (standard, if STA enabled) 2602 * [tlv] VHT information (standard, if STA enabled) 2603 * [tlv] WME (if configured and STA enabled) 2604 * [tlv] HT capabilities (vendor OUI, if STA enabled) 2605 * [tlv] HT information (vendor OUI, if STA enabled) 2606 * [tlv] Atheros capabilities (if STA enabled) 2607 * [tlv] AppIE's (optional) 2608 */ 2609 m = ieee80211_getmgtframe(&frm, 2610 ic->ic_headroom + sizeof(struct ieee80211_frame), 2611 sizeof(uint16_t) 2612 + sizeof(uint16_t) 2613 + sizeof(uint16_t) 2614 + 2 + IEEE80211_RATE_SIZE 2615 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2616 + sizeof(struct ieee80211_ie_htcap) + 4 2617 + sizeof(struct ieee80211_ie_htinfo) + 4 2618 + sizeof(struct ieee80211_ie_vhtcap) 2619 + sizeof(struct ieee80211_ie_vht_operation) 2620 + sizeof(struct ieee80211_wme_param) 2621 #ifdef IEEE80211_SUPPORT_SUPERG 2622 + sizeof(struct ieee80211_ath_ie) 2623 #endif 2624 + (vap->iv_appie_assocresp != NULL ? 2625 vap->iv_appie_assocresp->ie_len : 0) 2626 ); 2627 if (m == NULL) 2628 senderr(ENOMEM, is_tx_nobuf); 2629 2630 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 2631 *(uint16_t *)frm = htole16(capinfo); 2632 frm += 2; 2633 2634 *(uint16_t *)frm = htole16(arg); /* status */ 2635 frm += 2; 2636 2637 if (arg == IEEE80211_STATUS_SUCCESS) { 2638 *(uint16_t *)frm = htole16(ni->ni_associd); 2639 IEEE80211_NODE_STAT(ni, tx_assoc); 2640 } else 2641 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 2642 frm += 2; 2643 2644 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2645 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2646 /* NB: respond according to what we received */ 2647 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) { 2648 frm = ieee80211_add_htcap(frm, ni); 2649 frm = ieee80211_add_htinfo(frm, ni); 2650 } 2651 if ((vap->iv_flags & IEEE80211_F_WME) && 2652 ni->ni_ies.wme_ie != NULL) 2653 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2654 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) { 2655 frm = ieee80211_add_htcap_vendor(frm, ni); 2656 frm = ieee80211_add_htinfo_vendor(frm, ni); 2657 } 2658 if (ni->ni_flags & IEEE80211_NODE_VHT) { 2659 frm = ieee80211_add_vhtcap(frm, ni); 2660 frm = ieee80211_add_vhtinfo(frm, ni); 2661 } 2662 #ifdef IEEE80211_SUPPORT_SUPERG 2663 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 2664 frm = ieee80211_add_ath(frm, 2665 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2666 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 2667 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 2668 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 2669 #endif /* IEEE80211_SUPPORT_SUPERG */ 2670 if (vap->iv_appie_assocresp != NULL) 2671 frm = add_appie(frm, vap->iv_appie_assocresp); 2672 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2673 break; 2674 2675 case IEEE80211_FC0_SUBTYPE_DISASSOC: 2676 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 2677 "send station disassociate (reason: %d (%s))", arg, 2678 ieee80211_reason_to_string(arg)); 2679 m = ieee80211_getmgtframe(&frm, 2680 ic->ic_headroom + sizeof(struct ieee80211_frame), 2681 sizeof(uint16_t)); 2682 if (m == NULL) 2683 senderr(ENOMEM, is_tx_nobuf); 2684 *(uint16_t *)frm = htole16(arg); /* reason */ 2685 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 2686 2687 IEEE80211_NODE_STAT(ni, tx_disassoc); 2688 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 2689 break; 2690 2691 default: 2692 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 2693 "invalid mgmt frame type %u", type); 2694 senderr(EINVAL, is_tx_unknownmgt); 2695 /* NOTREACHED */ 2696 } 2697 2698 /* NB: force non-ProbeResp frames to the highest queue */ 2699 params.ibp_pri = WME_AC_VO; 2700 params.ibp_rate0 = bss->ni_txparms->mgmtrate; 2701 /* NB: we know all frames are unicast */ 2702 params.ibp_try0 = bss->ni_txparms->maxretry; 2703 params.ibp_power = bss->ni_txpower; 2704 return ieee80211_mgmt_output(ni, m, type, ¶ms); 2705 bad: 2706 ieee80211_free_node(ni); 2707 return ret; 2708 #undef senderr 2709 #undef HTFLAGS 2710 } 2711 2712 /* 2713 * Return an mbuf with a probe response frame in it. 2714 * Space is left to prepend and 802.11 header at the 2715 * front but it's left to the caller to fill in. 2716 */ 2717 struct mbuf * 2718 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy) 2719 { 2720 struct ieee80211vap *vap = bss->ni_vap; 2721 struct ieee80211com *ic = bss->ni_ic; 2722 const struct ieee80211_rateset *rs; 2723 struct mbuf *m; 2724 uint16_t capinfo; 2725 uint8_t *frm; 2726 2727 /* 2728 * probe response frame format 2729 * [8] time stamp 2730 * [2] beacon interval 2731 * [2] cabability information 2732 * [tlv] ssid 2733 * [tlv] supported rates 2734 * [tlv] parameter set (FH/DS) 2735 * [tlv] parameter set (IBSS) 2736 * [tlv] country (optional) 2737 * [3] power control (optional) 2738 * [5] channel switch announcement (CSA) (optional) 2739 * [tlv] extended rate phy (ERP) 2740 * [tlv] extended supported rates 2741 * [tlv] RSN (optional) 2742 * [tlv] HT capabilities 2743 * [tlv] HT information 2744 * [tlv] VHT capabilities 2745 * [tlv] VHT information 2746 * [tlv] WPA (optional) 2747 * [tlv] WME (optional) 2748 * [tlv] Vendor OUI HT capabilities (optional) 2749 * [tlv] Vendor OUI HT information (optional) 2750 * [tlv] Atheros capabilities 2751 * [tlv] AppIE's (optional) 2752 * [tlv] Mesh ID (MBSS) 2753 * [tlv] Mesh Conf (MBSS) 2754 */ 2755 m = ieee80211_getmgtframe(&frm, 2756 ic->ic_headroom + sizeof(struct ieee80211_frame), 2757 8 2758 + sizeof(uint16_t) 2759 + sizeof(uint16_t) 2760 + 2 + IEEE80211_NWID_LEN 2761 + 2 + IEEE80211_RATE_SIZE 2762 + 7 /* max(7,3) */ 2763 + IEEE80211_COUNTRY_MAX_SIZE 2764 + 3 2765 + sizeof(struct ieee80211_csa_ie) 2766 + sizeof(struct ieee80211_quiet_ie) 2767 + 3 2768 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2769 + sizeof(struct ieee80211_ie_wpa) 2770 + sizeof(struct ieee80211_ie_htcap) 2771 + sizeof(struct ieee80211_ie_htinfo) 2772 + sizeof(struct ieee80211_ie_wpa) 2773 + sizeof(struct ieee80211_wme_param) 2774 + 4 + sizeof(struct ieee80211_ie_htcap) 2775 + 4 + sizeof(struct ieee80211_ie_htinfo) 2776 + sizeof(struct ieee80211_ie_vhtcap) 2777 + sizeof(struct ieee80211_ie_vht_operation) 2778 #ifdef IEEE80211_SUPPORT_SUPERG 2779 + sizeof(struct ieee80211_ath_ie) 2780 #endif 2781 #ifdef IEEE80211_SUPPORT_MESH 2782 + 2 + IEEE80211_MESHID_LEN 2783 + sizeof(struct ieee80211_meshconf_ie) 2784 #endif 2785 + (vap->iv_appie_proberesp != NULL ? 2786 vap->iv_appie_proberesp->ie_len : 0) 2787 ); 2788 if (m == NULL) { 2789 vap->iv_stats.is_tx_nobuf++; 2790 return NULL; 2791 } 2792 2793 memset(frm, 0, 8); /* timestamp should be filled later */ 2794 frm += 8; 2795 *(uint16_t *)frm = htole16(bss->ni_intval); 2796 frm += 2; 2797 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 2798 *(uint16_t *)frm = htole16(capinfo); 2799 frm += 2; 2800 2801 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen); 2802 rs = ieee80211_get_suprates(ic, bss->ni_chan); 2803 frm = ieee80211_add_rates(frm, rs); 2804 2805 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) { 2806 *frm++ = IEEE80211_ELEMID_FHPARMS; 2807 *frm++ = 5; 2808 *frm++ = bss->ni_fhdwell & 0x00ff; 2809 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff; 2810 *frm++ = IEEE80211_FH_CHANSET( 2811 ieee80211_chan2ieee(ic, bss->ni_chan)); 2812 *frm++ = IEEE80211_FH_CHANPAT( 2813 ieee80211_chan2ieee(ic, bss->ni_chan)); 2814 *frm++ = bss->ni_fhindex; 2815 } else { 2816 *frm++ = IEEE80211_ELEMID_DSPARMS; 2817 *frm++ = 1; 2818 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan); 2819 } 2820 2821 if (vap->iv_opmode == IEEE80211_M_IBSS) { 2822 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2823 *frm++ = 2; 2824 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2825 } 2826 if ((vap->iv_flags & IEEE80211_F_DOTH) || 2827 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 2828 frm = ieee80211_add_countryie(frm, ic); 2829 if (vap->iv_flags & IEEE80211_F_DOTH) { 2830 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan)) 2831 frm = ieee80211_add_powerconstraint(frm, vap); 2832 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 2833 frm = ieee80211_add_csa(frm, vap); 2834 } 2835 if (vap->iv_flags & IEEE80211_F_DOTH) { 2836 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 2837 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 2838 if (vap->iv_quiet) 2839 frm = ieee80211_add_quiet(frm, vap, 0); 2840 } 2841 } 2842 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan)) 2843 frm = ieee80211_add_erp(frm, ic); 2844 frm = ieee80211_add_xrates(frm, rs); 2845 frm = ieee80211_add_rsn(frm, vap); 2846 /* 2847 * NB: legacy 11b clients do not get certain ie's. 2848 * The caller identifies such clients by passing 2849 * a token in legacy to us. Could expand this to be 2850 * any legacy client for stuff like HT ie's. 2851 */ 2852 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2853 legacy != IEEE80211_SEND_LEGACY_11B) { 2854 frm = ieee80211_add_htcap(frm, bss); 2855 frm = ieee80211_add_htinfo(frm, bss); 2856 } 2857 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) && 2858 legacy != IEEE80211_SEND_LEGACY_11B) { 2859 frm = ieee80211_add_vhtcap(frm, bss); 2860 frm = ieee80211_add_vhtinfo(frm, bss); 2861 } 2862 frm = ieee80211_add_wpa(frm, vap); 2863 if (vap->iv_flags & IEEE80211_F_WME) 2864 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2865 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2866 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) && 2867 legacy != IEEE80211_SEND_LEGACY_11B) { 2868 frm = ieee80211_add_htcap_vendor(frm, bss); 2869 frm = ieee80211_add_htinfo_vendor(frm, bss); 2870 } 2871 #ifdef IEEE80211_SUPPORT_SUPERG 2872 if ((vap->iv_flags & IEEE80211_F_ATHEROS) && 2873 legacy != IEEE80211_SEND_LEGACY_11B) 2874 frm = ieee80211_add_athcaps(frm, bss); 2875 #endif 2876 if (vap->iv_appie_proberesp != NULL) 2877 frm = add_appie(frm, vap->iv_appie_proberesp); 2878 #ifdef IEEE80211_SUPPORT_MESH 2879 if (vap->iv_opmode == IEEE80211_M_MBSS) { 2880 frm = ieee80211_add_meshid(frm, vap); 2881 frm = ieee80211_add_meshconf(frm, vap); 2882 } 2883 #endif 2884 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2885 2886 return m; 2887 } 2888 2889 /* 2890 * Send a probe response frame to the specified mac address. 2891 * This does not go through the normal mgt frame api so we 2892 * can specify the destination address and re-use the bss node 2893 * for the sta reference. 2894 */ 2895 int 2896 ieee80211_send_proberesp(struct ieee80211vap *vap, 2897 const uint8_t da[IEEE80211_ADDR_LEN], int legacy) 2898 { 2899 struct ieee80211_node *bss = vap->iv_bss; 2900 struct ieee80211com *ic = vap->iv_ic; 2901 struct mbuf *m; 2902 int ret; 2903 2904 if (vap->iv_state == IEEE80211_S_CAC) { 2905 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss, 2906 "block %s frame in CAC state", "probe response"); 2907 vap->iv_stats.is_tx_badstate++; 2908 return EIO; /* XXX */ 2909 } 2910 2911 /* 2912 * Hold a reference on the node so it doesn't go away until after 2913 * the xmit is complete all the way in the driver. On error we 2914 * will remove our reference. 2915 */ 2916 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2917 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2918 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr), 2919 ieee80211_node_refcnt(bss)+1); 2920 ieee80211_ref_node(bss); 2921 2922 m = ieee80211_alloc_proberesp(bss, legacy); 2923 if (m == NULL) { 2924 ieee80211_free_node(bss); 2925 return ENOMEM; 2926 } 2927 2928 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 2929 KASSERT(m != NULL, ("no room for header")); 2930 2931 IEEE80211_TX_LOCK(ic); 2932 ieee80211_send_setup(bss, m, 2933 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP, 2934 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid); 2935 /* XXX power management? */ 2936 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2937 2938 M_WME_SETAC(m, WME_AC_BE); 2939 2940 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2941 "send probe resp on channel %u to %s%s\n", 2942 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da), 2943 legacy ? " <legacy>" : ""); 2944 IEEE80211_NODE_STAT(bss, tx_mgmt); 2945 2946 ret = ieee80211_raw_output(vap, bss, m, NULL); 2947 IEEE80211_TX_UNLOCK(ic); 2948 return (ret); 2949 } 2950 2951 /* 2952 * Allocate and build a RTS (Request To Send) control frame. 2953 */ 2954 struct mbuf * 2955 ieee80211_alloc_rts(struct ieee80211com *ic, 2956 const uint8_t ra[IEEE80211_ADDR_LEN], 2957 const uint8_t ta[IEEE80211_ADDR_LEN], 2958 uint16_t dur) 2959 { 2960 struct ieee80211_frame_rts *rts; 2961 struct mbuf *m; 2962 2963 /* XXX honor ic_headroom */ 2964 m = m_gethdr(M_NOWAIT, MT_DATA); 2965 if (m != NULL) { 2966 rts = mtod(m, struct ieee80211_frame_rts *); 2967 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2968 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS; 2969 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2970 *(u_int16_t *)rts->i_dur = htole16(dur); 2971 IEEE80211_ADDR_COPY(rts->i_ra, ra); 2972 IEEE80211_ADDR_COPY(rts->i_ta, ta); 2973 2974 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 2975 } 2976 return m; 2977 } 2978 2979 /* 2980 * Allocate and build a CTS (Clear To Send) control frame. 2981 */ 2982 struct mbuf * 2983 ieee80211_alloc_cts(struct ieee80211com *ic, 2984 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur) 2985 { 2986 struct ieee80211_frame_cts *cts; 2987 struct mbuf *m; 2988 2989 /* XXX honor ic_headroom */ 2990 m = m_gethdr(M_NOWAIT, MT_DATA); 2991 if (m != NULL) { 2992 cts = mtod(m, struct ieee80211_frame_cts *); 2993 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2994 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS; 2995 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2996 *(u_int16_t *)cts->i_dur = htole16(dur); 2997 IEEE80211_ADDR_COPY(cts->i_ra, ra); 2998 2999 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 3000 } 3001 return m; 3002 } 3003 3004 static void 3005 ieee80211_tx_mgt_timeout(void *arg) 3006 { 3007 struct ieee80211vap *vap = arg; 3008 3009 IEEE80211_LOCK(vap->iv_ic); 3010 if (vap->iv_state != IEEE80211_S_INIT && 3011 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) { 3012 /* 3013 * NB: it's safe to specify a timeout as the reason here; 3014 * it'll only be used in the right state. 3015 */ 3016 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN, 3017 IEEE80211_SCAN_FAIL_TIMEOUT); 3018 } 3019 IEEE80211_UNLOCK(vap->iv_ic); 3020 } 3021 3022 /* 3023 * This is the callback set on net80211-sourced transmitted 3024 * authentication request frames. 3025 * 3026 * This does a couple of things: 3027 * 3028 * + If the frame transmitted was a success, it schedules a future 3029 * event which will transition the interface to scan. 3030 * If a state transition _then_ occurs before that event occurs, 3031 * said state transition will cancel this callout. 3032 * 3033 * + If the frame transmit was a failure, it immediately schedules 3034 * the transition back to scan. 3035 */ 3036 static void 3037 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 3038 { 3039 struct ieee80211vap *vap = ni->ni_vap; 3040 enum ieee80211_state ostate = (enum ieee80211_state) arg; 3041 3042 /* 3043 * Frame transmit completed; arrange timer callback. If 3044 * transmit was successfully we wait for response. Otherwise 3045 * we arrange an immediate callback instead of doing the 3046 * callback directly since we don't know what state the driver 3047 * is in (e.g. what locks it is holding). This work should 3048 * not be too time-critical and not happen too often so the 3049 * added overhead is acceptable. 3050 * 3051 * XXX what happens if !acked but response shows up before callback? 3052 */ 3053 if (vap->iv_state == ostate) { 3054 callout_reset(&vap->iv_mgtsend, 3055 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 3056 ieee80211_tx_mgt_timeout, vap); 3057 } 3058 } 3059 3060 static void 3061 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm, 3062 struct ieee80211_node *ni) 3063 { 3064 struct ieee80211vap *vap = ni->ni_vap; 3065 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3066 struct ieee80211com *ic = ni->ni_ic; 3067 struct ieee80211_rateset *rs = &ni->ni_rates; 3068 uint16_t capinfo; 3069 3070 /* 3071 * beacon frame format 3072 * 3073 * TODO: update to 802.11-2012; a lot of stuff has changed; 3074 * vendor extensions should be at the end, etc. 3075 * 3076 * [8] time stamp 3077 * [2] beacon interval 3078 * [2] cabability information 3079 * [tlv] ssid 3080 * [tlv] supported rates 3081 * [3] parameter set (DS) 3082 * [8] CF parameter set (optional) 3083 * [tlv] parameter set (IBSS/TIM) 3084 * [tlv] country (optional) 3085 * [3] power control (optional) 3086 * [5] channel switch announcement (CSA) (optional) 3087 * XXX TODO: Quiet 3088 * XXX TODO: IBSS DFS 3089 * XXX TODO: TPC report 3090 * [tlv] extended rate phy (ERP) 3091 * [tlv] extended supported rates 3092 * [tlv] RSN parameters 3093 * XXX TODO: BSSLOAD 3094 * (XXX EDCA parameter set, QoS capability?) 3095 * XXX TODO: AP channel report 3096 * 3097 * [tlv] HT capabilities 3098 * [tlv] HT information 3099 * XXX TODO: 20/40 BSS coexistence 3100 * Mesh: 3101 * XXX TODO: Meshid 3102 * XXX TODO: mesh config 3103 * XXX TODO: mesh awake window 3104 * XXX TODO: beacon timing (mesh, etc) 3105 * XXX TODO: MCCAOP Advertisement Overview 3106 * XXX TODO: MCCAOP Advertisement 3107 * XXX TODO: Mesh channel switch parameters 3108 * VHT: 3109 * XXX TODO: VHT capabilities 3110 * XXX TODO: VHT operation 3111 * XXX TODO: VHT transmit power envelope 3112 * XXX TODO: channel switch wrapper element 3113 * XXX TODO: extended BSS load element 3114 * 3115 * XXX Vendor-specific OIDs (e.g. Atheros) 3116 * [tlv] WPA parameters 3117 * [tlv] WME parameters 3118 * [tlv] Vendor OUI HT capabilities (optional) 3119 * [tlv] Vendor OUI HT information (optional) 3120 * [tlv] Atheros capabilities (optional) 3121 * [tlv] TDMA parameters (optional) 3122 * [tlv] Mesh ID (MBSS) 3123 * [tlv] Mesh Conf (MBSS) 3124 * [tlv] application data (optional) 3125 */ 3126 3127 memset(bo, 0, sizeof(*bo)); 3128 3129 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 3130 frm += 8; 3131 *(uint16_t *)frm = htole16(ni->ni_intval); 3132 frm += 2; 3133 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3134 bo->bo_caps = (uint16_t *)frm; 3135 *(uint16_t *)frm = htole16(capinfo); 3136 frm += 2; 3137 *frm++ = IEEE80211_ELEMID_SSID; 3138 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) { 3139 *frm++ = ni->ni_esslen; 3140 memcpy(frm, ni->ni_essid, ni->ni_esslen); 3141 frm += ni->ni_esslen; 3142 } else 3143 *frm++ = 0; 3144 frm = ieee80211_add_rates(frm, rs); 3145 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) { 3146 *frm++ = IEEE80211_ELEMID_DSPARMS; 3147 *frm++ = 1; 3148 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 3149 } 3150 if (ic->ic_flags & IEEE80211_F_PCF) { 3151 bo->bo_cfp = frm; 3152 frm = ieee80211_add_cfparms(frm, ic); 3153 } 3154 bo->bo_tim = frm; 3155 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3156 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 3157 *frm++ = 2; 3158 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 3159 bo->bo_tim_len = 0; 3160 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3161 vap->iv_opmode == IEEE80211_M_MBSS) { 3162 /* TIM IE is the same for Mesh and Hostap */ 3163 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 3164 3165 tie->tim_ie = IEEE80211_ELEMID_TIM; 3166 tie->tim_len = 4; /* length */ 3167 tie->tim_count = 0; /* DTIM count */ 3168 tie->tim_period = vap->iv_dtim_period; /* DTIM period */ 3169 tie->tim_bitctl = 0; /* bitmap control */ 3170 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 3171 frm += sizeof(struct ieee80211_tim_ie); 3172 bo->bo_tim_len = 1; 3173 } 3174 bo->bo_tim_trailer = frm; 3175 if ((vap->iv_flags & IEEE80211_F_DOTH) || 3176 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 3177 frm = ieee80211_add_countryie(frm, ic); 3178 if (vap->iv_flags & IEEE80211_F_DOTH) { 3179 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 3180 frm = ieee80211_add_powerconstraint(frm, vap); 3181 bo->bo_csa = frm; 3182 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 3183 frm = ieee80211_add_csa(frm, vap); 3184 } else 3185 bo->bo_csa = frm; 3186 3187 bo->bo_quiet = NULL; 3188 if (vap->iv_flags & IEEE80211_F_DOTH) { 3189 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3190 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) && 3191 (vap->iv_quiet == 1)) { 3192 /* 3193 * We only insert the quiet IE offset if 3194 * the quiet IE is enabled. Otherwise don't 3195 * put it here or we'll just overwrite 3196 * some other beacon contents. 3197 */ 3198 if (vap->iv_quiet) { 3199 bo->bo_quiet = frm; 3200 frm = ieee80211_add_quiet(frm,vap, 0); 3201 } 3202 } 3203 } 3204 3205 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) { 3206 bo->bo_erp = frm; 3207 frm = ieee80211_add_erp(frm, ic); 3208 } 3209 frm = ieee80211_add_xrates(frm, rs); 3210 frm = ieee80211_add_rsn(frm, vap); 3211 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 3212 frm = ieee80211_add_htcap(frm, ni); 3213 bo->bo_htinfo = frm; 3214 frm = ieee80211_add_htinfo(frm, ni); 3215 } 3216 3217 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) { 3218 frm = ieee80211_add_vhtcap(frm, ni); 3219 bo->bo_vhtinfo = frm; 3220 frm = ieee80211_add_vhtinfo(frm, ni); 3221 /* Transmit power envelope */ 3222 /* Channel switch wrapper element */ 3223 /* Extended bss load element */ 3224 } 3225 3226 frm = ieee80211_add_wpa(frm, vap); 3227 if (vap->iv_flags & IEEE80211_F_WME) { 3228 bo->bo_wme = frm; 3229 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 3230 } 3231 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 3232 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) { 3233 frm = ieee80211_add_htcap_vendor(frm, ni); 3234 frm = ieee80211_add_htinfo_vendor(frm, ni); 3235 } 3236 3237 #ifdef IEEE80211_SUPPORT_SUPERG 3238 if (vap->iv_flags & IEEE80211_F_ATHEROS) { 3239 bo->bo_ath = frm; 3240 frm = ieee80211_add_athcaps(frm, ni); 3241 } 3242 #endif 3243 #ifdef IEEE80211_SUPPORT_TDMA 3244 if (vap->iv_caps & IEEE80211_C_TDMA) { 3245 bo->bo_tdma = frm; 3246 frm = ieee80211_add_tdma(frm, vap); 3247 } 3248 #endif 3249 if (vap->iv_appie_beacon != NULL) { 3250 bo->bo_appie = frm; 3251 bo->bo_appie_len = vap->iv_appie_beacon->ie_len; 3252 frm = add_appie(frm, vap->iv_appie_beacon); 3253 } 3254 3255 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */ 3256 #ifdef IEEE80211_SUPPORT_MESH 3257 if (vap->iv_opmode == IEEE80211_M_MBSS) { 3258 frm = ieee80211_add_meshid(frm, vap); 3259 bo->bo_meshconf = frm; 3260 frm = ieee80211_add_meshconf(frm, vap); 3261 } 3262 #endif 3263 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 3264 bo->bo_csa_trailer_len = frm - bo->bo_csa; 3265 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3266 } 3267 3268 /* 3269 * Allocate a beacon frame and fillin the appropriate bits. 3270 */ 3271 struct mbuf * 3272 ieee80211_beacon_alloc(struct ieee80211_node *ni) 3273 { 3274 struct ieee80211vap *vap = ni->ni_vap; 3275 struct ieee80211com *ic = ni->ni_ic; 3276 struct ifnet *ifp = vap->iv_ifp; 3277 struct ieee80211_frame *wh; 3278 struct mbuf *m; 3279 int pktlen; 3280 uint8_t *frm; 3281 3282 /* 3283 * Update the "We're putting the quiet IE in the beacon" state. 3284 */ 3285 if (vap->iv_quiet == 1) 3286 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3287 else if (vap->iv_quiet == 0) 3288 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3289 3290 /* 3291 * beacon frame format 3292 * 3293 * Note: This needs updating for 802.11-2012. 3294 * 3295 * [8] time stamp 3296 * [2] beacon interval 3297 * [2] cabability information 3298 * [tlv] ssid 3299 * [tlv] supported rates 3300 * [3] parameter set (DS) 3301 * [8] CF parameter set (optional) 3302 * [tlv] parameter set (IBSS/TIM) 3303 * [tlv] country (optional) 3304 * [3] power control (optional) 3305 * [5] channel switch announcement (CSA) (optional) 3306 * [tlv] extended rate phy (ERP) 3307 * [tlv] extended supported rates 3308 * [tlv] RSN parameters 3309 * [tlv] HT capabilities 3310 * [tlv] HT information 3311 * [tlv] VHT capabilities 3312 * [tlv] VHT operation 3313 * [tlv] Vendor OUI HT capabilities (optional) 3314 * [tlv] Vendor OUI HT information (optional) 3315 * XXX Vendor-specific OIDs (e.g. Atheros) 3316 * [tlv] WPA parameters 3317 * [tlv] WME parameters 3318 * [tlv] TDMA parameters (optional) 3319 * [tlv] Mesh ID (MBSS) 3320 * [tlv] Mesh Conf (MBSS) 3321 * [tlv] application data (optional) 3322 * NB: we allocate the max space required for the TIM bitmap. 3323 * XXX how big is this? 3324 */ 3325 pktlen = 8 /* time stamp */ 3326 + sizeof(uint16_t) /* beacon interval */ 3327 + sizeof(uint16_t) /* capabilities */ 3328 + 2 + ni->ni_esslen /* ssid */ 3329 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 3330 + 2 + 1 /* DS parameters */ 3331 + 2 + 6 /* CF parameters */ 3332 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */ 3333 + IEEE80211_COUNTRY_MAX_SIZE /* country */ 3334 + 2 + 1 /* power control */ 3335 + sizeof(struct ieee80211_csa_ie) /* CSA */ 3336 + sizeof(struct ieee80211_quiet_ie) /* Quiet */ 3337 + 2 + 1 /* ERP */ 3338 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 3339 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 3340 2*sizeof(struct ieee80211_ie_wpa) : 0) 3341 /* XXX conditional? */ 3342 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 3343 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 3344 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */ 3345 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */ 3346 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */ 3347 sizeof(struct ieee80211_wme_param) : 0) 3348 #ifdef IEEE80211_SUPPORT_SUPERG 3349 + sizeof(struct ieee80211_ath_ie) /* ATH */ 3350 #endif 3351 #ifdef IEEE80211_SUPPORT_TDMA 3352 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */ 3353 sizeof(struct ieee80211_tdma_param) : 0) 3354 #endif 3355 #ifdef IEEE80211_SUPPORT_MESH 3356 + 2 + ni->ni_meshidlen 3357 + sizeof(struct ieee80211_meshconf_ie) 3358 #endif 3359 + IEEE80211_MAX_APPIE 3360 ; 3361 m = ieee80211_getmgtframe(&frm, 3362 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 3363 if (m == NULL) { 3364 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 3365 "%s: cannot get buf; size %u\n", __func__, pktlen); 3366 vap->iv_stats.is_tx_nobuf++; 3367 return NULL; 3368 } 3369 ieee80211_beacon_construct(m, frm, ni); 3370 3371 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 3372 KASSERT(m != NULL, ("no space for 802.11 header?")); 3373 wh = mtod(m, struct ieee80211_frame *); 3374 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3375 IEEE80211_FC0_SUBTYPE_BEACON; 3376 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3377 *(uint16_t *)wh->i_dur = 0; 3378 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 3379 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 3380 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 3381 *(uint16_t *)wh->i_seq = 0; 3382 3383 return m; 3384 } 3385 3386 /* 3387 * Update the dynamic parts of a beacon frame based on the current state. 3388 */ 3389 int 3390 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast) 3391 { 3392 struct ieee80211vap *vap = ni->ni_vap; 3393 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3394 struct ieee80211com *ic = ni->ni_ic; 3395 int len_changed = 0; 3396 uint16_t capinfo; 3397 struct ieee80211_frame *wh; 3398 ieee80211_seq seqno; 3399 3400 IEEE80211_LOCK(ic); 3401 /* 3402 * Handle 11h channel change when we've reached the count. 3403 * We must recalculate the beacon frame contents to account 3404 * for the new channel. Note we do this only for the first 3405 * vap that reaches this point; subsequent vaps just update 3406 * their beacon state to reflect the recalculated channel. 3407 */ 3408 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) && 3409 vap->iv_csa_count == ic->ic_csa_count) { 3410 vap->iv_csa_count = 0; 3411 /* 3412 * Effect channel change before reconstructing the beacon 3413 * frame contents as many places reference ni_chan. 3414 */ 3415 if (ic->ic_csa_newchan != NULL) 3416 ieee80211_csa_completeswitch(ic); 3417 /* 3418 * NB: ieee80211_beacon_construct clears all pending 3419 * updates in bo_flags so we don't need to explicitly 3420 * clear IEEE80211_BEACON_CSA. 3421 */ 3422 ieee80211_beacon_construct(m, 3423 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3424 3425 /* XXX do WME aggressive mode processing? */ 3426 IEEE80211_UNLOCK(ic); 3427 return 1; /* just assume length changed */ 3428 } 3429 3430 /* 3431 * Handle the quiet time element being added and removed. 3432 * Again, for now we just cheat and reconstruct the whole 3433 * beacon - that way the gap is provided as appropriate. 3434 * 3435 * So, track whether we have already added the IE versus 3436 * whether we want to be adding the IE. 3437 */ 3438 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) && 3439 (vap->iv_quiet == 0)) { 3440 /* 3441 * Quiet time beacon IE enabled, but it's disabled; 3442 * recalc 3443 */ 3444 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3445 ieee80211_beacon_construct(m, 3446 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3447 /* XXX do WME aggressive mode processing? */ 3448 IEEE80211_UNLOCK(ic); 3449 return 1; /* just assume length changed */ 3450 } 3451 3452 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) && 3453 (vap->iv_quiet == 1)) { 3454 /* 3455 * Quiet time beacon IE disabled, but it's now enabled; 3456 * recalc 3457 */ 3458 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3459 ieee80211_beacon_construct(m, 3460 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3461 /* XXX do WME aggressive mode processing? */ 3462 IEEE80211_UNLOCK(ic); 3463 return 1; /* just assume length changed */ 3464 } 3465 3466 wh = mtod(m, struct ieee80211_frame *); 3467 3468 /* 3469 * XXX TODO Strictly speaking this should be incremented with the TX 3470 * lock held so as to serialise access to the non-qos TID sequence 3471 * number space. 3472 * 3473 * If the driver identifies it does its own TX seqno management then 3474 * we can skip this (and still not do the TX seqno.) 3475 */ 3476 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 3477 *(uint16_t *)&wh->i_seq[0] = 3478 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 3479 M_SEQNO_SET(m, seqno); 3480 3481 /* XXX faster to recalculate entirely or just changes? */ 3482 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3483 *bo->bo_caps = htole16(capinfo); 3484 3485 if (vap->iv_flags & IEEE80211_F_WME) { 3486 struct ieee80211_wme_state *wme = &ic->ic_wme; 3487 3488 /* 3489 * Check for aggressive mode change. When there is 3490 * significant high priority traffic in the BSS 3491 * throttle back BE traffic by using conservative 3492 * parameters. Otherwise BE uses aggressive params 3493 * to optimize performance of legacy/non-QoS traffic. 3494 */ 3495 if (wme->wme_flags & WME_F_AGGRMODE) { 3496 if (wme->wme_hipri_traffic > 3497 wme->wme_hipri_switch_thresh) { 3498 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3499 "%s: traffic %u, disable aggressive mode\n", 3500 __func__, wme->wme_hipri_traffic); 3501 wme->wme_flags &= ~WME_F_AGGRMODE; 3502 ieee80211_wme_updateparams_locked(vap); 3503 wme->wme_hipri_traffic = 3504 wme->wme_hipri_switch_hysteresis; 3505 } else 3506 wme->wme_hipri_traffic = 0; 3507 } else { 3508 if (wme->wme_hipri_traffic <= 3509 wme->wme_hipri_switch_thresh) { 3510 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3511 "%s: traffic %u, enable aggressive mode\n", 3512 __func__, wme->wme_hipri_traffic); 3513 wme->wme_flags |= WME_F_AGGRMODE; 3514 ieee80211_wme_updateparams_locked(vap); 3515 wme->wme_hipri_traffic = 0; 3516 } else 3517 wme->wme_hipri_traffic = 3518 wme->wme_hipri_switch_hysteresis; 3519 } 3520 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 3521 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 3522 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 3523 } 3524 } 3525 3526 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 3527 ieee80211_ht_update_beacon(vap, bo); 3528 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 3529 } 3530 #ifdef IEEE80211_SUPPORT_TDMA 3531 if (vap->iv_caps & IEEE80211_C_TDMA) { 3532 /* 3533 * NB: the beacon is potentially updated every TBTT. 3534 */ 3535 ieee80211_tdma_update_beacon(vap, bo); 3536 } 3537 #endif 3538 #ifdef IEEE80211_SUPPORT_MESH 3539 if (vap->iv_opmode == IEEE80211_M_MBSS) 3540 ieee80211_mesh_update_beacon(vap, bo); 3541 #endif 3542 3543 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3544 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/ 3545 struct ieee80211_tim_ie *tie = 3546 (struct ieee80211_tim_ie *) bo->bo_tim; 3547 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 3548 u_int timlen, timoff, i; 3549 /* 3550 * ATIM/DTIM needs updating. If it fits in the 3551 * current space allocated then just copy in the 3552 * new bits. Otherwise we need to move any trailing 3553 * data to make room. Note that we know there is 3554 * contiguous space because ieee80211_beacon_allocate 3555 * insures there is space in the mbuf to write a 3556 * maximal-size virtual bitmap (based on iv_max_aid). 3557 */ 3558 /* 3559 * Calculate the bitmap size and offset, copy any 3560 * trailer out of the way, and then copy in the 3561 * new bitmap and update the information element. 3562 * Note that the tim bitmap must contain at least 3563 * one byte and any offset must be even. 3564 */ 3565 if (vap->iv_ps_pending != 0) { 3566 timoff = 128; /* impossibly large */ 3567 for (i = 0; i < vap->iv_tim_len; i++) 3568 if (vap->iv_tim_bitmap[i]) { 3569 timoff = i &~ 1; 3570 break; 3571 } 3572 KASSERT(timoff != 128, ("tim bitmap empty!")); 3573 for (i = vap->iv_tim_len-1; i >= timoff; i--) 3574 if (vap->iv_tim_bitmap[i]) 3575 break; 3576 timlen = 1 + (i - timoff); 3577 } else { 3578 timoff = 0; 3579 timlen = 1; 3580 } 3581 3582 /* 3583 * TODO: validate this! 3584 */ 3585 if (timlen != bo->bo_tim_len) { 3586 /* copy up/down trailer */ 3587 int adjust = tie->tim_bitmap+timlen 3588 - bo->bo_tim_trailer; 3589 ovbcopy(bo->bo_tim_trailer, 3590 bo->bo_tim_trailer+adjust, 3591 bo->bo_tim_trailer_len); 3592 bo->bo_tim_trailer += adjust; 3593 bo->bo_erp += adjust; 3594 bo->bo_htinfo += adjust; 3595 bo->bo_vhtinfo += adjust; 3596 #ifdef IEEE80211_SUPPORT_SUPERG 3597 bo->bo_ath += adjust; 3598 #endif 3599 #ifdef IEEE80211_SUPPORT_TDMA 3600 bo->bo_tdma += adjust; 3601 #endif 3602 #ifdef IEEE80211_SUPPORT_MESH 3603 bo->bo_meshconf += adjust; 3604 #endif 3605 bo->bo_appie += adjust; 3606 bo->bo_wme += adjust; 3607 bo->bo_csa += adjust; 3608 bo->bo_quiet += adjust; 3609 bo->bo_tim_len = timlen; 3610 3611 /* update information element */ 3612 tie->tim_len = 3 + timlen; 3613 tie->tim_bitctl = timoff; 3614 len_changed = 1; 3615 } 3616 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff, 3617 bo->bo_tim_len); 3618 3619 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 3620 3621 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 3622 "%s: TIM updated, pending %u, off %u, len %u\n", 3623 __func__, vap->iv_ps_pending, timoff, timlen); 3624 } 3625 /* count down DTIM period */ 3626 if (tie->tim_count == 0) 3627 tie->tim_count = tie->tim_period - 1; 3628 else 3629 tie->tim_count--; 3630 /* update state for buffered multicast frames on DTIM */ 3631 if (mcast && tie->tim_count == 0) 3632 tie->tim_bitctl |= 1; 3633 else 3634 tie->tim_bitctl &= ~1; 3635 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) { 3636 struct ieee80211_csa_ie *csa = 3637 (struct ieee80211_csa_ie *) bo->bo_csa; 3638 3639 /* 3640 * Insert or update CSA ie. If we're just starting 3641 * to count down to the channel switch then we need 3642 * to insert the CSA ie. Otherwise we just need to 3643 * drop the count. The actual change happens above 3644 * when the vap's count reaches the target count. 3645 */ 3646 if (vap->iv_csa_count == 0) { 3647 memmove(&csa[1], csa, bo->bo_csa_trailer_len); 3648 bo->bo_erp += sizeof(*csa); 3649 bo->bo_htinfo += sizeof(*csa); 3650 bo->bo_vhtinfo += sizeof(*csa); 3651 bo->bo_wme += sizeof(*csa); 3652 #ifdef IEEE80211_SUPPORT_SUPERG 3653 bo->bo_ath += sizeof(*csa); 3654 #endif 3655 #ifdef IEEE80211_SUPPORT_TDMA 3656 bo->bo_tdma += sizeof(*csa); 3657 #endif 3658 #ifdef IEEE80211_SUPPORT_MESH 3659 bo->bo_meshconf += sizeof(*csa); 3660 #endif 3661 bo->bo_appie += sizeof(*csa); 3662 bo->bo_csa_trailer_len += sizeof(*csa); 3663 bo->bo_quiet += sizeof(*csa); 3664 bo->bo_tim_trailer_len += sizeof(*csa); 3665 m->m_len += sizeof(*csa); 3666 m->m_pkthdr.len += sizeof(*csa); 3667 3668 ieee80211_add_csa(bo->bo_csa, vap); 3669 } else 3670 csa->csa_count--; 3671 vap->iv_csa_count++; 3672 /* NB: don't clear IEEE80211_BEACON_CSA */ 3673 } 3674 3675 /* 3676 * Only add the quiet time IE if we've enabled it 3677 * as appropriate. 3678 */ 3679 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3680 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 3681 if (vap->iv_quiet && 3682 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) { 3683 ieee80211_add_quiet(bo->bo_quiet, vap, 1); 3684 } 3685 } 3686 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 3687 /* 3688 * ERP element needs updating. 3689 */ 3690 (void) ieee80211_add_erp(bo->bo_erp, ic); 3691 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 3692 } 3693 #ifdef IEEE80211_SUPPORT_SUPERG 3694 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) { 3695 ieee80211_add_athcaps(bo->bo_ath, ni); 3696 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH); 3697 } 3698 #endif 3699 } 3700 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) { 3701 const struct ieee80211_appie *aie = vap->iv_appie_beacon; 3702 int aielen; 3703 uint8_t *frm; 3704 3705 aielen = 0; 3706 if (aie != NULL) 3707 aielen += aie->ie_len; 3708 if (aielen != bo->bo_appie_len) { 3709 /* copy up/down trailer */ 3710 int adjust = aielen - bo->bo_appie_len; 3711 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust, 3712 bo->bo_tim_trailer_len); 3713 bo->bo_tim_trailer += adjust; 3714 bo->bo_appie += adjust; 3715 bo->bo_appie_len = aielen; 3716 3717 len_changed = 1; 3718 } 3719 frm = bo->bo_appie; 3720 if (aie != NULL) 3721 frm = add_appie(frm, aie); 3722 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE); 3723 } 3724 IEEE80211_UNLOCK(ic); 3725 3726 return len_changed; 3727 } 3728 3729 /* 3730 * Do Ethernet-LLC encapsulation for each payload in a fast frame 3731 * tunnel encapsulation. The frame is assumed to have an Ethernet 3732 * header at the front that must be stripped before prepending the 3733 * LLC followed by the Ethernet header passed in (with an Ethernet 3734 * type that specifies the payload size). 3735 */ 3736 struct mbuf * 3737 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m, 3738 const struct ether_header *eh) 3739 { 3740 struct llc *llc; 3741 uint16_t payload; 3742 3743 /* XXX optimize by combining m_adj+M_PREPEND */ 3744 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 3745 llc = mtod(m, struct llc *); 3746 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 3747 llc->llc_control = LLC_UI; 3748 llc->llc_snap.org_code[0] = 0; 3749 llc->llc_snap.org_code[1] = 0; 3750 llc->llc_snap.org_code[2] = 0; 3751 llc->llc_snap.ether_type = eh->ether_type; 3752 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 3753 3754 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT); 3755 if (m == NULL) { /* XXX cannot happen */ 3756 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 3757 "%s: no space for ether_header\n", __func__); 3758 vap->iv_stats.is_tx_nobuf++; 3759 return NULL; 3760 } 3761 ETHER_HEADER_COPY(mtod(m, void *), eh); 3762 mtod(m, struct ether_header *)->ether_type = htons(payload); 3763 return m; 3764 } 3765 3766 /* 3767 * Complete an mbuf transmission. 3768 * 3769 * For now, this simply processes a completed frame after the 3770 * driver has completed it's transmission and/or retransmission. 3771 * It assumes the frame is an 802.11 encapsulated frame. 3772 * 3773 * Later on it will grow to become the exit path for a given frame 3774 * from the driver and, depending upon how it's been encapsulated 3775 * and already transmitted, it may end up doing A-MPDU retransmission, 3776 * power save requeuing, etc. 3777 * 3778 * In order for the above to work, the driver entry point to this 3779 * must not hold any driver locks. Thus, the driver needs to delay 3780 * any actual mbuf completion until it can release said locks. 3781 * 3782 * This frees the mbuf and if the mbuf has a node reference, 3783 * the node reference will be freed. 3784 */ 3785 void 3786 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status) 3787 { 3788 3789 if (ni != NULL) { 3790 struct ifnet *ifp = ni->ni_vap->iv_ifp; 3791 3792 if (status == 0) { 3793 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 3794 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3795 if (m->m_flags & M_MCAST) 3796 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 3797 } else 3798 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 3799 if (m->m_flags & M_TXCB) 3800 ieee80211_process_callback(ni, m, status); 3801 ieee80211_free_node(ni); 3802 } 3803 m_freem(m); 3804 } 3805