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