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