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