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