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