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