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