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