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