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