ieee80211_output.c revision 172232
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2007 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 172232 2007-09-18 21:09:26Z sam $"); 29 30#include "opt_inet.h" 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/mbuf.h> 35#include <sys/kernel.h> 36#include <sys/endian.h> 37 38#include <sys/socket.h> 39 40#include <net/bpf.h> 41#include <net/ethernet.h> 42#include <net/if.h> 43#include <net/if_llc.h> 44#include <net/if_media.h> 45#include <net/if_vlan_var.h> 46 47#include <net80211/ieee80211_var.h> 48#include <net80211/ieee80211_regdomain.h> 49 50#ifdef INET 51#include <netinet/in.h> 52#include <netinet/if_ether.h> 53#include <netinet/in_systm.h> 54#include <netinet/ip.h> 55#endif 56 57#define ETHER_HEADER_COPY(dst, src) \ 58 memcpy(dst, src, sizeof(struct ether_header)) 59 60static struct mbuf *ieee80211_encap_fastframe(struct ieee80211com *ic, 61 struct mbuf *m1, const struct ether_header *eh1, 62 struct mbuf *m2, const struct ether_header *eh2); 63static int ieee80211_fragment(struct ieee80211com *, struct mbuf *, 64 u_int hdrsize, u_int ciphdrsize, u_int mtu); 65static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int); 66 67#ifdef IEEE80211_DEBUG 68/* 69 * Decide if an outbound management frame should be 70 * printed when debugging is enabled. This filters some 71 * of the less interesting frames that come frequently 72 * (e.g. beacons). 73 */ 74static __inline int 75doprint(struct ieee80211com *ic, int subtype) 76{ 77 switch (subtype) { 78 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 79 return (ic->ic_opmode == IEEE80211_M_IBSS); 80 } 81 return 1; 82} 83#endif 84 85/* 86 * Set the direction field and address fields of an outgoing 87 * non-QoS frame. Note this should be called early on in 88 * constructing a frame as it sets i_fc[1]; other bits can 89 * then be or'd in. 90 */ 91static void 92ieee80211_send_setup(struct ieee80211com *ic, 93 struct ieee80211_node *ni, 94 struct ieee80211_frame *wh, 95 int type, 96 const uint8_t sa[IEEE80211_ADDR_LEN], 97 const uint8_t da[IEEE80211_ADDR_LEN], 98 const uint8_t bssid[IEEE80211_ADDR_LEN]) 99{ 100#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 101 102 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 103 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 104 switch (ic->ic_opmode) { 105 case IEEE80211_M_STA: 106 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 107 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 108 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 109 IEEE80211_ADDR_COPY(wh->i_addr3, da); 110 break; 111 case IEEE80211_M_IBSS: 112 case IEEE80211_M_AHDEMO: 113 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 114 IEEE80211_ADDR_COPY(wh->i_addr1, da); 115 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 116 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 117 break; 118 case IEEE80211_M_HOSTAP: 119 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 120 IEEE80211_ADDR_COPY(wh->i_addr1, da); 121 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 122 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 123 break; 124 case IEEE80211_M_WDS: 125 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 126 /* XXX cheat, bssid holds RA */ 127 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 128 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 129 IEEE80211_ADDR_COPY(wh->i_addr3, da); 130 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 131 break; 132 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 133 break; 134 } 135 } else { 136 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 137 IEEE80211_ADDR_COPY(wh->i_addr1, da); 138 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 139 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 140 } 141 *(uint16_t *)&wh->i_dur[0] = 0; 142 /* NB: use non-QoS tid */ 143 *(uint16_t *)&wh->i_seq[0] = 144 htole16(ni->ni_txseqs[IEEE80211_NONQOS_TID] << IEEE80211_SEQ_SEQ_SHIFT); 145 ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 146#undef WH4 147} 148 149/* 150 * Send a management frame to the specified node. The node pointer 151 * must have a reference as the pointer will be passed to the driver 152 * and potentially held for a long time. If the frame is successfully 153 * dispatched to the driver, then it is responsible for freeing the 154 * reference (and potentially free'ing up any associated storage). 155 */ 156int 157ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni, 158 struct mbuf *m, int type) 159{ 160 struct ifnet *ifp = ic->ic_ifp; 161 struct ieee80211_frame *wh; 162 163 KASSERT(ni != NULL, ("null node")); 164 165 /* 166 * Yech, hack alert! We want to pass the node down to the 167 * driver's start routine. If we don't do so then the start 168 * routine must immediately look it up again and that can 169 * cause a lock order reversal if, for example, this frame 170 * is being sent because the station is being timedout and 171 * the frame being sent is a DEAUTH message. We could stick 172 * this in an m_tag and tack that on to the mbuf. However 173 * that's rather expensive to do for every frame so instead 174 * we stuff it in the rcvif field since outbound frames do 175 * not (presently) use this. 176 */ 177 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 178 if (m == NULL) 179 return ENOMEM; 180 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 181 m->m_pkthdr.rcvif = (void *)ni; 182 183 wh = mtod(m, struct ieee80211_frame *); 184 ieee80211_send_setup(ic, ni, wh, 185 IEEE80211_FC0_TYPE_MGT | type, 186 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 187 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) { 188 m->m_flags &= ~M_LINK0; 189 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 190 "[%s] encrypting frame (%s)\n", 191 ether_sprintf(wh->i_addr1), __func__); 192 wh->i_fc[1] |= IEEE80211_FC1_WEP; 193 } 194 if (ni->ni_flags & IEEE80211_NODE_QOS) { 195 /* NB: force all management frames to the highest queue */ 196 M_WME_SETAC(m, WME_AC_VO); 197 } else 198 M_WME_SETAC(m, WME_AC_BE); 199#ifdef IEEE80211_DEBUG 200 /* avoid printing too many frames */ 201 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) || 202 ieee80211_msg_dumppkts(ic)) { 203 printf("[%s] send %s on channel %u\n", 204 ether_sprintf(wh->i_addr1), 205 ieee80211_mgt_subtype_name[ 206 (type & IEEE80211_FC0_SUBTYPE_MASK) >> 207 IEEE80211_FC0_SUBTYPE_SHIFT], 208 ieee80211_chan2ieee(ic, ic->ic_curchan)); 209 } 210#endif 211 IEEE80211_NODE_STAT(ni, tx_mgmt); 212 IF_ENQUEUE(&ic->ic_mgtq, m); 213 if_start(ifp); 214 ifp->if_opackets++; 215 216 return 0; 217} 218 219/* 220 * Raw packet transmit stub for legacy drivers. 221 * Send the packet through the mgt q so we bypass 222 * the normal encapsulation work. 223 */ 224int 225ieee80211_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 226 const struct ieee80211_bpf_params *params) 227{ 228 struct ieee80211com *ic = ni->ni_ic; 229 struct ifnet *ifp = ic->ic_ifp; 230 231 m->m_pkthdr.rcvif = (void *) ni; 232 IF_ENQUEUE(&ic->ic_mgtq, m); 233 if_start(ifp); 234 ifp->if_opackets++; 235 236 return 0; 237} 238 239/* 240 * 802.11 output routine. This is (currently) used only to 241 * connect bpf write calls to the 802.11 layer for injecting 242 * raw 802.11 frames. Note we locate the ieee80211com from 243 * the ifnet using a spare field setup at attach time. This 244 * will go away when the virtual ap support comes in. 245 */ 246int 247ieee80211_output(struct ifnet *ifp, struct mbuf *m, 248 struct sockaddr *dst, struct rtentry *rt0) 249{ 250#define senderr(e) do { error = (e); goto bad;} while (0) 251 struct ieee80211com *ic = ifp->if_spare2; /* XXX */ 252 struct ieee80211_node *ni = NULL; 253 struct ieee80211_frame *wh; 254 int error; 255 256 /* 257 * Hand to the 802.3 code if not tagged as 258 * a raw 802.11 frame. 259 */ 260 if (dst->sa_family != AF_IEEE80211) 261 return ether_output(ifp, m, dst, rt0); 262#ifdef MAC 263 error = mac_check_ifnet_transmit(ifp, m); 264 if (error) 265 senderr(error); 266#endif 267 if (ifp->if_flags & IFF_MONITOR) 268 senderr(ENETDOWN); 269 if ((ifp->if_flags & IFF_UP) == 0) 270 senderr(ENETDOWN); 271 272 /* XXX bypass bridge, pfil, carp, etc. */ 273 274 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 275 senderr(EIO); /* XXX */ 276 wh = mtod(m, struct ieee80211_frame *); 277 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 278 IEEE80211_FC0_VERSION_0) 279 senderr(EIO); /* XXX */ 280 281 /* locate destination node */ 282 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 283 case IEEE80211_FC1_DIR_NODS: 284 case IEEE80211_FC1_DIR_FROMDS: 285 ni = ieee80211_find_txnode(ic, wh->i_addr1); 286 break; 287 case IEEE80211_FC1_DIR_TODS: 288 case IEEE80211_FC1_DIR_DSTODS: 289 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) 290 senderr(EIO); /* XXX */ 291 ni = ieee80211_find_txnode(ic, wh->i_addr3); 292 break; 293 default: 294 senderr(EIO); /* XXX */ 295 } 296 if (ni == NULL) { 297 /* 298 * Permit packets w/ bpf params through regardless 299 * (see below about sa_len). 300 */ 301 if (dst->sa_len == 0) 302 senderr(EHOSTUNREACH); 303 ni = ieee80211_ref_node(ic->ic_bss); 304 } 305 306 /* XXX ctrl frames should go through */ 307 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 308 (m->m_flags & M_PWR_SAV) == 0) { 309 /* 310 * Station in power save mode; pass the frame 311 * to the 802.11 layer and continue. We'll get 312 * the frame back when the time is right. 313 */ 314 ieee80211_pwrsave(ni, m); 315 error = 0; 316 goto reclaim; 317 } 318 319 /* calculate priority so drivers can find the tx queue */ 320 /* XXX assumes an 802.3 frame */ 321 if (ieee80211_classify(ic, m, ni)) 322 senderr(EIO); /* XXX */ 323 324 BPF_MTAP(ifp, m); 325 /* 326 * NB: DLT_IEEE802_11_RADIO identifies the parameters are 327 * present by setting the sa_len field of the sockaddr (yes, 328 * this is a hack). 329 * NB: we assume sa_data is suitably aligned to cast. 330 */ 331 return ic->ic_raw_xmit(ni, m, (const struct ieee80211_bpf_params *) 332 (dst->sa_len ? dst->sa_data : NULL)); 333bad: 334 if (m != NULL) 335 m_freem(m); 336reclaim: 337 if (ni != NULL) 338 ieee80211_free_node(ni); 339 return error; 340#undef senderr 341} 342 343/* 344 * Send a null data frame to the specified node. 345 * 346 * NB: the caller is assumed to have setup a node reference 347 * for use; this is necessary to deal with a race condition 348 * when probing for inactive stations. 349 */ 350int 351ieee80211_send_nulldata(struct ieee80211_node *ni) 352{ 353 struct ieee80211com *ic = ni->ni_ic; 354 struct ifnet *ifp = ic->ic_ifp; 355 struct mbuf *m; 356 struct ieee80211_frame *wh; 357 358 MGETHDR(m, M_NOWAIT, MT_DATA); 359 if (m == NULL) { 360 /* XXX debug msg */ 361 ieee80211_unref_node(&ni); 362 ic->ic_stats.is_tx_nobuf++; 363 return ENOMEM; 364 } 365 MH_ALIGN(m, sizeof(struct ieee80211_frame)); 366 m->m_pkthdr.rcvif = (void *) ni; 367 368 wh = mtod(m, struct ieee80211_frame *); 369 ieee80211_send_setup(ic, ni, wh, 370 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 371 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 372 /* NB: power management bit is never sent by an AP */ 373 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 374 ic->ic_opmode != IEEE80211_M_HOSTAP && 375 ic->ic_opmode != IEEE80211_M_WDS) 376 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 377 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame); 378 M_WME_SETAC(m, WME_AC_BE); 379 380 IEEE80211_NODE_STAT(ni, tx_data); 381 382 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 383 "[%s] send null data frame on channel %u, pwr mgt %s\n", 384 ether_sprintf(ni->ni_macaddr), 385 ieee80211_chan2ieee(ic, ic->ic_curchan), 386 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 387 388 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */ 389 if_start(ifp); 390 391 return 0; 392} 393 394/* 395 * Assign priority to a frame based on any vlan tag assigned 396 * to the station and/or any Diffserv setting in an IP header. 397 * Finally, if an ACM policy is setup (in station mode) it's 398 * applied. 399 */ 400int 401ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni) 402{ 403 int v_wme_ac, d_wme_ac, ac; 404#ifdef INET 405 struct ether_header *eh; 406#endif 407 408 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 409 ac = WME_AC_BE; 410 goto done; 411 } 412 413 /* 414 * If node has a vlan tag then all traffic 415 * to it must have a matching tag. 416 */ 417 v_wme_ac = 0; 418 if (ni->ni_vlan != 0) { 419 if ((m->m_flags & M_VLANTAG) == 0) { 420 IEEE80211_NODE_STAT(ni, tx_novlantag); 421 return 1; 422 } 423 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 424 EVL_VLANOFTAG(ni->ni_vlan)) { 425 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 426 return 1; 427 } 428 /* map vlan priority to AC */ 429 switch (EVL_PRIOFTAG(ni->ni_vlan)) { 430 case 1: 431 case 2: 432 v_wme_ac = WME_AC_BK; 433 break; 434 case 0: 435 case 3: 436 v_wme_ac = WME_AC_BE; 437 break; 438 case 4: 439 case 5: 440 v_wme_ac = WME_AC_VI; 441 break; 442 case 6: 443 case 7: 444 v_wme_ac = WME_AC_VO; 445 break; 446 } 447 } 448 449#ifdef INET 450 eh = mtod(m, struct ether_header *); 451 if (eh->ether_type == htons(ETHERTYPE_IP)) { 452 const struct ip *ip = (struct ip *) 453 (mtod(m, uint8_t *) + sizeof (*eh)); 454 /* 455 * IP frame, map the TOS field. 456 */ 457 switch (ip->ip_tos) { 458 case 0x08: 459 case 0x20: 460 d_wme_ac = WME_AC_BK; /* background */ 461 break; 462 case 0x28: 463 case 0xa0: 464 d_wme_ac = WME_AC_VI; /* video */ 465 break; 466 case 0x30: /* voice */ 467 case 0xe0: 468 case 0x88: /* XXX UPSD */ 469 case 0xb8: 470 d_wme_ac = WME_AC_VO; 471 break; 472 default: 473 d_wme_ac = WME_AC_BE; 474 break; 475 } 476 } else { 477#endif /* INET */ 478 d_wme_ac = WME_AC_BE; 479#ifdef INET 480 } 481#endif 482 /* 483 * Use highest priority AC. 484 */ 485 if (v_wme_ac > d_wme_ac) 486 ac = v_wme_ac; 487 else 488 ac = d_wme_ac; 489 490 /* 491 * Apply ACM policy. 492 */ 493 if (ic->ic_opmode == IEEE80211_M_STA) { 494 static const int acmap[4] = { 495 WME_AC_BK, /* WME_AC_BE */ 496 WME_AC_BK, /* WME_AC_BK */ 497 WME_AC_BE, /* WME_AC_VI */ 498 WME_AC_VI, /* WME_AC_VO */ 499 }; 500 while (ac != WME_AC_BK && 501 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 502 ac = acmap[ac]; 503 } 504done: 505 M_WME_SETAC(m, ac); 506 return 0; 507} 508 509/* 510 * Insure there is sufficient contiguous space to encapsulate the 511 * 802.11 data frame. If room isn't already there, arrange for it. 512 * Drivers and cipher modules assume we have done the necessary work 513 * and fail rudely if they don't find the space they need. 514 */ 515static struct mbuf * 516ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize, 517 struct ieee80211_key *key, struct mbuf *m) 518{ 519#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 520 int needed_space = ic->ic_headroom + hdrsize; 521 522 if (key != NULL) { 523 /* XXX belongs in crypto code? */ 524 needed_space += key->wk_cipher->ic_header; 525 /* XXX frags */ 526 /* 527 * When crypto is being done in the host we must insure 528 * the data are writable for the cipher routines; clone 529 * a writable mbuf chain. 530 * XXX handle SWMIC specially 531 */ 532 if (key->wk_flags & (IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC)) { 533 m = m_unshare(m, M_NOWAIT); 534 if (m == NULL) { 535 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 536 "%s: cannot get writable mbuf\n", __func__); 537 ic->ic_stats.is_tx_nobuf++; /* XXX new stat */ 538 return NULL; 539 } 540 } 541 } 542 /* 543 * We know we are called just before stripping an Ethernet 544 * header and prepending an LLC header. This means we know 545 * there will be 546 * sizeof(struct ether_header) - sizeof(struct llc) 547 * bytes recovered to which we need additional space for the 548 * 802.11 header and any crypto header. 549 */ 550 /* XXX check trailing space and copy instead? */ 551 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 552 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 553 if (n == NULL) { 554 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 555 "%s: cannot expand storage\n", __func__); 556 ic->ic_stats.is_tx_nobuf++; 557 m_freem(m); 558 return NULL; 559 } 560 KASSERT(needed_space <= MHLEN, 561 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 562 /* 563 * Setup new mbuf to have leading space to prepend the 564 * 802.11 header and any crypto header bits that are 565 * required (the latter are added when the driver calls 566 * back to ieee80211_crypto_encap to do crypto encapsulation). 567 */ 568 /* NB: must be first 'cuz it clobbers m_data */ 569 m_move_pkthdr(n, m); 570 n->m_len = 0; /* NB: m_gethdr does not set */ 571 n->m_data += needed_space; 572 /* 573 * Pull up Ethernet header to create the expected layout. 574 * We could use m_pullup but that's overkill (i.e. we don't 575 * need the actual data) and it cannot fail so do it inline 576 * for speed. 577 */ 578 /* NB: struct ether_header is known to be contiguous */ 579 n->m_len += sizeof(struct ether_header); 580 m->m_len -= sizeof(struct ether_header); 581 m->m_data += sizeof(struct ether_header); 582 /* 583 * Replace the head of the chain. 584 */ 585 n->m_next = m; 586 m = n; 587 } 588 return m; 589#undef TO_BE_RECLAIMED 590} 591 592/* 593 * Return the transmit key to use in sending a unicast frame. 594 * If a unicast key is set we use that. When no unicast key is set 595 * we fall back to the default transmit key. 596 */ 597static __inline struct ieee80211_key * 598ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 599{ 600 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 601 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 602 IEEE80211_KEY_UNDEFINED(&ic->ic_nw_keys[ic->ic_def_txkey])) 603 return NULL; 604 return &ic->ic_nw_keys[ic->ic_def_txkey]; 605 } else { 606 return &ni->ni_ucastkey; 607 } 608} 609 610/* 611 * Return the transmit key to use in sending a multicast frame. 612 * Multicast traffic always uses the group key which is installed as 613 * the default tx key. 614 */ 615static __inline struct ieee80211_key * 616ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 617{ 618 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 619 IEEE80211_KEY_UNDEFINED(&ic->ic_nw_keys[ic->ic_def_txkey])) 620 return NULL; 621 return &ic->ic_nw_keys[ic->ic_def_txkey]; 622} 623 624/* 625 * Encapsulate an outbound data frame. The mbuf chain is updated. 626 * If an error is encountered NULL is returned. The caller is required 627 * to provide a node reference and pullup the ethernet header in the 628 * first mbuf. 629 */ 630struct mbuf * 631ieee80211_encap(struct ieee80211com *ic, struct mbuf *m, 632 struct ieee80211_node *ni) 633{ 634 struct ether_header eh; 635 struct ieee80211_frame *wh; 636 struct ieee80211_key *key; 637 struct llc *llc; 638 int hdrsize, datalen, addqos, txfrag, isff; 639 640 /* 641 * Copy existing Ethernet header to a safe place. The 642 * rest of the code assumes it's ok to strip it when 643 * reorganizing state for the final encapsulation. 644 */ 645 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 646 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header)); 647 648 /* 649 * Insure space for additional headers. First identify 650 * transmit key to use in calculating any buffer adjustments 651 * required. This is also used below to do privacy 652 * encapsulation work. Then calculate the 802.11 header 653 * size and any padding required by the driver. 654 * 655 * Note key may be NULL if we fall back to the default 656 * transmit key and that is not set. In that case the 657 * buffer may not be expanded as needed by the cipher 658 * routines, but they will/should discard it. 659 */ 660 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 661 if (ic->ic_opmode == IEEE80211_M_STA || 662 !IEEE80211_IS_MULTICAST(eh.ether_dhost)) 663 key = ieee80211_crypto_getucastkey(ic, ni); 664 else 665 key = ieee80211_crypto_getmcastkey(ic, ni); 666 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) { 667 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 668 "[%s] no default transmit key (%s) deftxkey %u\n", 669 ether_sprintf(eh.ether_dhost), __func__, 670 ic->ic_def_txkey); 671 ic->ic_stats.is_tx_nodefkey++; 672 goto bad; 673 } 674 } else 675 key = NULL; 676 /* XXX 4-address format */ 677 /* 678 * XXX Some ap's don't handle QoS-encapsulated EAPOL 679 * frames so suppress use. This may be an issue if other 680 * ap's require all data frames to be QoS-encapsulated 681 * once negotiated in which case we'll need to make this 682 * configurable. 683 */ 684 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) && 685 eh.ether_type != htons(ETHERTYPE_PAE); 686 if (addqos) 687 hdrsize = sizeof(struct ieee80211_qosframe); 688 else 689 hdrsize = sizeof(struct ieee80211_frame); 690 if (ic->ic_flags & IEEE80211_F_DATAPAD) 691 hdrsize = roundup(hdrsize, sizeof(uint32_t)); 692 693 if ((isff = m->m_flags & M_FF) != 0) { 694 struct mbuf *m2; 695 struct ether_header eh2; 696 697 /* 698 * Fast frame encapsulation. There must be two packets 699 * chained with m_nextpkt. We do header adjustment for 700 * each, add the tunnel encapsulation, and then concatenate 701 * the mbuf chains to form a single frame for transmission. 702 */ 703 m2 = m->m_nextpkt; 704 if (m2 == NULL) { 705 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG, 706 "%s: only one frame\n", __func__); 707 goto bad; 708 } 709 m->m_nextpkt = NULL; 710 /* 711 * Include fast frame headers in adjusting header 712 * layout; this allocates space according to what 713 * ieee80211_encap_fastframe will do. 714 */ 715 m = ieee80211_mbuf_adjust(ic, 716 hdrsize + sizeof(struct llc) + sizeof(uint32_t) + 2 + 717 sizeof(struct ether_header), 718 key, m); 719 if (m == NULL) { 720 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 721 m_freem(m2); 722 goto bad; 723 } 724 /* 725 * Copy second frame's Ethernet header out of line 726 * and adjust for encapsulation headers. Note that 727 * we make room for padding in case there isn't room 728 * at the end of first frame. 729 */ 730 KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!")); 731 memcpy(&eh2, mtod(m2, caddr_t), sizeof(struct ether_header)); 732 m2 = ieee80211_mbuf_adjust(ic, 733 ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header), 734 NULL, m2); 735 if (m2 == NULL) { 736 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 737 goto bad; 738 } 739 m = ieee80211_encap_fastframe(ic, m, &eh, m2, &eh2); 740 if (m == NULL) 741 goto bad; 742 } else { 743 /* 744 * Normal frame. 745 */ 746 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m); 747 if (m == NULL) { 748 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 749 goto bad; 750 } 751 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 752 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 753 llc = mtod(m, struct llc *); 754 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 755 llc->llc_control = LLC_UI; 756 llc->llc_snap.org_code[0] = 0; 757 llc->llc_snap.org_code[1] = 0; 758 llc->llc_snap.org_code[2] = 0; 759 llc->llc_snap.ether_type = eh.ether_type; 760 } 761 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 762 763 M_PREPEND(m, hdrsize, M_DONTWAIT); 764 if (m == NULL) { 765 ic->ic_stats.is_tx_nobuf++; 766 goto bad; 767 } 768 wh = mtod(m, struct ieee80211_frame *); 769 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 770 *(uint16_t *)wh->i_dur = 0; 771 switch (ic->ic_opmode) { 772 case IEEE80211_M_STA: 773 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 774 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 775 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 776 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 777 break; 778 case IEEE80211_M_IBSS: 779 case IEEE80211_M_AHDEMO: 780 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 781 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 782 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 783 /* 784 * NB: always use the bssid from ic_bss as the 785 * neighbor's may be stale after an ibss merge 786 */ 787 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid); 788 break; 789 case IEEE80211_M_HOSTAP: 790 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 791 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 792 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 793 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 794 break; 795 case IEEE80211_M_MONITOR: 796 case IEEE80211_M_WDS: 797 goto bad; 798 } 799 if (m->m_flags & M_MORE_DATA) 800 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 801 if (addqos) { 802 struct ieee80211_qosframe *qwh = 803 (struct ieee80211_qosframe *) wh; 804 int ac, tid; 805 806 ac = M_WME_GETAC(m); 807 /* map from access class/queue to 11e header priorty value */ 808 tid = WME_AC_TO_TID(ac); 809 qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 810 /* 811 * Check if A-MPDU tx aggregation is setup or if we 812 * should try to enable it. The sta must be associated 813 * with HT and A-MPDU enabled for use. On the first 814 * frame that goes out We issue an ADDBA request and 815 * wait for a reply. The frame being encapsulated 816 * will go out w/o using A-MPDU, or possibly it might 817 * be collected by the driver and held/retransmit. 818 * ieee80211_ampdu_request handles staggering requests 819 * in case the receiver NAK's us or we are otherwise 820 * unable to establish a BA stream. 821 */ 822 if ((ni->ni_flags & IEEE80211_NODE_HT) && 823 (ic->ic_flags_ext & IEEE80211_FEXT_AMPDU_TX)) { 824 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac]; 825 826 if (IEEE80211_AMPDU_RUNNING(tap)) { 827 /* 828 * Operational, mark frame for aggregation. 829 */ 830 qwh->i_qos[0] |= IEEE80211_QOS_ACKPOLICY_BA; 831 } else if (!IEEE80211_AMPDU_REQUESTED(tap)) { 832 /* 833 * Not negotiated yet, request service. 834 */ 835 ieee80211_ampdu_request(ni, tap); 836 } 837 } 838 /* XXX works even when BA marked above */ 839 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 840 qwh->i_qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 841 qwh->i_qos[1] = 0; 842 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 843 844 *(uint16_t *)wh->i_seq = 845 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 846 ni->ni_txseqs[tid]++; 847 } else { 848 *(uint16_t *)wh->i_seq = 849 htole16(ni->ni_txseqs[IEEE80211_NONQOS_TID] << IEEE80211_SEQ_SEQ_SHIFT); 850 ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 851 } 852 /* check if xmit fragmentation is required */ 853 txfrag = (m->m_pkthdr.len > ic->ic_fragthreshold && 854 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 855 (ic->ic_caps & IEEE80211_C_TXFRAG) && 856 !isff); /* NB: don't fragment ff's */ 857 if (key != NULL) { 858 /* 859 * IEEE 802.1X: send EAPOL frames always in the clear. 860 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 861 */ 862 if (eh.ether_type != htons(ETHERTYPE_PAE) || 863 ((ic->ic_flags & IEEE80211_F_WPA) && 864 (ic->ic_opmode == IEEE80211_M_STA ? 865 !IEEE80211_KEY_UNDEFINED(key) : 866 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 867 wh->i_fc[1] |= IEEE80211_FC1_WEP; 868 if (!ieee80211_crypto_enmic(ic, key, m, txfrag)) { 869 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 870 "[%s] enmic failed, discard frame\n", 871 ether_sprintf(eh.ether_dhost)); 872 ic->ic_stats.is_crypto_enmicfail++; 873 goto bad; 874 } 875 } 876 } 877 /* 878 * NB: frag flags may leak from above; they should only 879 * be set on return to the caller if we fragment at 880 * the 802.11 layer. 881 */ 882 m->m_flags &= ~(M_FRAG | M_FIRSTFRAG); 883 if (txfrag && !ieee80211_fragment(ic, m, hdrsize, 884 key != NULL ? key->wk_cipher->ic_header : 0, ic->ic_fragthreshold)) 885 goto bad; 886 887 IEEE80211_NODE_STAT(ni, tx_data); 888 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 889 IEEE80211_NODE_STAT(ni, tx_mcast); 890 else 891 IEEE80211_NODE_STAT(ni, tx_ucast); 892 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 893 894 return m; 895bad: 896 if (m != NULL) 897 m_freem(m); 898 return NULL; 899} 900 901/* 902 * Do Ethernet-LLC encapsulation for each payload in a fast frame 903 * tunnel encapsulation. The frame is assumed to have an Ethernet 904 * header at the front that must be stripped before prepending the 905 * LLC followed by the Ethernet header passed in (with an Ethernet 906 * type that specifies the payload size). 907 */ 908static struct mbuf * 909ieee80211_encap1(struct ieee80211com *ic, struct mbuf *m, 910 const struct ether_header *eh) 911{ 912 struct llc *llc; 913 uint16_t payload; 914 915 /* XXX optimize by combining m_adj+M_PREPEND */ 916 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 917 llc = mtod(m, struct llc *); 918 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 919 llc->llc_control = LLC_UI; 920 llc->llc_snap.org_code[0] = 0; 921 llc->llc_snap.org_code[1] = 0; 922 llc->llc_snap.org_code[2] = 0; 923 llc->llc_snap.ether_type = eh->ether_type; 924 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 925 926 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT); 927 if (m == NULL) { /* XXX cannot happen */ 928 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG, 929 "%s: no space for ether_header\n", __func__); 930 ic->ic_stats.is_tx_nobuf++; 931 return NULL; 932 } 933 ETHER_HEADER_COPY(mtod(m, void *), eh); 934 mtod(m, struct ether_header *)->ether_type = htons(payload); 935 return m; 936} 937 938/* 939 * Do fast frame tunnel encapsulation. The two frames and 940 * Ethernet headers are supplied. The caller is assumed to 941 * have arrange for space in the mbuf chains for encapsulating 942 * headers (to avoid major mbuf fragmentation). 943 * 944 * The encapsulated frame is returned or NULL if there is a 945 * problem (should not happen). 946 */ 947static struct mbuf * 948ieee80211_encap_fastframe(struct ieee80211com *ic, 949 struct mbuf *m1, const struct ether_header *eh1, 950 struct mbuf *m2, const struct ether_header *eh2) 951{ 952 struct llc *llc; 953 struct mbuf *m; 954 int pad; 955 956 /* 957 * First, each frame gets a standard encapsulation. 958 */ 959 m1 = ieee80211_encap1(ic, m1, eh1); 960 if (m1 == NULL) { 961 m_freem(m2); 962 return NULL; 963 } 964 m2 = ieee80211_encap1(ic, m2, eh2); 965 if (m2 == NULL) { 966 m_freem(m1); 967 return NULL; 968 } 969 970 /* 971 * Pad leading frame to a 4-byte boundary. If there 972 * is space at the end of the first frame, put it 973 * there; otherwise prepend to the front of the second 974 * frame. We know doing the second will always work 975 * because we reserve space above. We prefer appending 976 * as this typically has better DMA alignment properties. 977 */ 978 for (m = m1; m->m_next != NULL; m = m->m_next) 979 ; 980 pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len; 981 if (pad) { 982 if (M_TRAILINGSPACE(m) < pad) { /* prepend to second */ 983 m2->m_data -= pad; 984 m2->m_len += pad; 985 m2->m_pkthdr.len += pad; 986 } else { /* append to first */ 987 m->m_len += pad; 988 m1->m_pkthdr.len += pad; 989 } 990 } 991 992 /* 993 * Now, stick 'em together and prepend the tunnel headers; 994 * first the Atheros tunnel header (all zero for now) and 995 * then a special fast frame LLC. 996 * 997 * XXX optimize by prepending together 998 */ 999 m->m_next = m2; /* NB: last mbuf from above */ 1000 m1->m_pkthdr.len += m2->m_pkthdr.len; 1001 M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT); 1002 if (m1 == NULL) { /* XXX cannot happen */ 1003 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG, 1004 "%s: no space for tunnel header\n", __func__); 1005 ic->ic_stats.is_tx_nobuf++; 1006 return NULL; 1007 } 1008 memset(mtod(m1, void *), 0, sizeof(uint32_t)+2); 1009 1010 M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT); 1011 if (m1 == NULL) { /* XXX cannot happen */ 1012 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG, 1013 "%s: no space for llc header\n", __func__); 1014 ic->ic_stats.is_tx_nobuf++; 1015 return NULL; 1016 } 1017 llc = mtod(m1, struct llc *); 1018 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1019 llc->llc_control = LLC_UI; 1020 llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0; 1021 llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1; 1022 llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2; 1023 llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE); 1024 1025 ic->ic_stats.is_ff_encap++; 1026 1027 return m1; 1028} 1029 1030/* 1031 * Fragment the frame according to the specified mtu. 1032 * The size of the 802.11 header (w/o padding) is provided 1033 * so we don't need to recalculate it. We create a new 1034 * mbuf for each fragment and chain it through m_nextpkt; 1035 * we might be able to optimize this by reusing the original 1036 * packet's mbufs but that is significantly more complicated. 1037 */ 1038static int 1039ieee80211_fragment(struct ieee80211com *ic, struct mbuf *m0, 1040 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1041{ 1042 struct ieee80211_frame *wh, *whf; 1043 struct mbuf *m, *prev, *next; 1044 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1045 1046 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1047 KASSERT(m0->m_pkthdr.len > mtu, 1048 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1049 1050 wh = mtod(m0, struct ieee80211_frame *); 1051 /* NB: mark the first frag; it will be propagated below */ 1052 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1053 totalhdrsize = hdrsize + ciphdrsize; 1054 fragno = 1; 1055 off = mtu - ciphdrsize; 1056 remainder = m0->m_pkthdr.len - off; 1057 prev = m0; 1058 do { 1059 fragsize = totalhdrsize + remainder; 1060 if (fragsize > mtu) 1061 fragsize = mtu; 1062 KASSERT(fragsize < MCLBYTES, 1063 ("fragment size %u too big!", fragsize)); 1064 if (fragsize > MHLEN) 1065 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1066 else 1067 m = m_gethdr(M_DONTWAIT, MT_DATA); 1068 if (m == NULL) 1069 goto bad; 1070 /* leave room to prepend any cipher header */ 1071 m_align(m, fragsize - ciphdrsize); 1072 1073 /* 1074 * Form the header in the fragment. Note that since 1075 * we mark the first fragment with the MORE_FRAG bit 1076 * it automatically is propagated to each fragment; we 1077 * need only clear it on the last fragment (done below). 1078 */ 1079 whf = mtod(m, struct ieee80211_frame *); 1080 memcpy(whf, wh, hdrsize); 1081 *(uint16_t *)&whf->i_seq[0] |= htole16( 1082 (fragno & IEEE80211_SEQ_FRAG_MASK) << 1083 IEEE80211_SEQ_FRAG_SHIFT); 1084 fragno++; 1085 1086 payload = fragsize - totalhdrsize; 1087 /* NB: destination is known to be contiguous */ 1088 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize); 1089 m->m_len = hdrsize + payload; 1090 m->m_pkthdr.len = hdrsize + payload; 1091 m->m_flags |= M_FRAG; 1092 1093 /* chain up the fragment */ 1094 prev->m_nextpkt = m; 1095 prev = m; 1096 1097 /* deduct fragment just formed */ 1098 remainder -= payload; 1099 off += payload; 1100 } while (remainder != 0); 1101 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 1102 1103 /* strip first mbuf now that everything has been copied */ 1104 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 1105 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1106 1107 ic->ic_stats.is_tx_fragframes++; 1108 ic->ic_stats.is_tx_frags += fragno-1; 1109 1110 return 1; 1111bad: 1112 /* reclaim fragments but leave original frame for caller to free */ 1113 for (m = m0->m_nextpkt; m != NULL; m = next) { 1114 next = m->m_nextpkt; 1115 m->m_nextpkt = NULL; /* XXX paranoid */ 1116 m_freem(m); 1117 } 1118 m0->m_nextpkt = NULL; 1119 return 0; 1120} 1121 1122/* 1123 * Add a supported rates element id to a frame. 1124 */ 1125static uint8_t * 1126ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 1127{ 1128 int nrates; 1129 1130 *frm++ = IEEE80211_ELEMID_RATES; 1131 nrates = rs->rs_nrates; 1132 if (nrates > IEEE80211_RATE_SIZE) 1133 nrates = IEEE80211_RATE_SIZE; 1134 *frm++ = nrates; 1135 memcpy(frm, rs->rs_rates, nrates); 1136 return frm + nrates; 1137} 1138 1139/* 1140 * Add an extended supported rates element id to a frame. 1141 */ 1142static uint8_t * 1143ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 1144{ 1145 /* 1146 * Add an extended supported rates element if operating in 11g mode. 1147 */ 1148 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 1149 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 1150 *frm++ = IEEE80211_ELEMID_XRATES; 1151 *frm++ = nrates; 1152 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 1153 frm += nrates; 1154 } 1155 return frm; 1156} 1157 1158/* 1159 * Add an ssid elemet to a frame. 1160 */ 1161static uint8_t * 1162ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 1163{ 1164 *frm++ = IEEE80211_ELEMID_SSID; 1165 *frm++ = len; 1166 memcpy(frm, ssid, len); 1167 return frm + len; 1168} 1169 1170/* 1171 * Add an erp element to a frame. 1172 */ 1173static uint8_t * 1174ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic) 1175{ 1176 uint8_t erp; 1177 1178 *frm++ = IEEE80211_ELEMID_ERP; 1179 *frm++ = 1; 1180 erp = 0; 1181 if (ic->ic_nonerpsta != 0) 1182 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 1183 if (ic->ic_flags & IEEE80211_F_USEPROT) 1184 erp |= IEEE80211_ERP_USE_PROTECTION; 1185 if (ic->ic_flags & IEEE80211_F_USEBARKER) 1186 erp |= IEEE80211_ERP_LONG_PREAMBLE; 1187 *frm++ = erp; 1188 return frm; 1189} 1190 1191static uint8_t * 1192ieee80211_setup_wpa_ie(struct ieee80211com *ic, uint8_t *ie) 1193{ 1194#define WPA_OUI_BYTES 0x00, 0x50, 0xf2 1195#define ADDSHORT(frm, v) do { \ 1196 frm[0] = (v) & 0xff; \ 1197 frm[1] = (v) >> 8; \ 1198 frm += 2; \ 1199} while (0) 1200#define ADDSELECTOR(frm, sel) do { \ 1201 memcpy(frm, sel, 4); \ 1202 frm += 4; \ 1203} while (0) 1204 static const uint8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE }; 1205 static const uint8_t cipher_suite[][4] = { 1206 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */ 1207 { WPA_OUI_BYTES, WPA_CSE_TKIP }, 1208 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */ 1209 { WPA_OUI_BYTES, WPA_CSE_CCMP }, 1210 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 1211 { WPA_OUI_BYTES, WPA_CSE_NULL }, 1212 }; 1213 static const uint8_t wep104_suite[4] = 1214 { WPA_OUI_BYTES, WPA_CSE_WEP104 }; 1215 static const uint8_t key_mgt_unspec[4] = 1216 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC }; 1217 static const uint8_t key_mgt_psk[4] = 1218 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK }; 1219 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 1220 uint8_t *frm = ie; 1221 uint8_t *selcnt; 1222 1223 *frm++ = IEEE80211_ELEMID_VENDOR; 1224 *frm++ = 0; /* length filled in below */ 1225 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */ 1226 frm += sizeof(oui); 1227 ADDSHORT(frm, WPA_VERSION); 1228 1229 /* XXX filter out CKIP */ 1230 1231 /* multicast cipher */ 1232 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 1233 rsn->rsn_mcastkeylen >= 13) 1234 ADDSELECTOR(frm, wep104_suite); 1235 else 1236 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 1237 1238 /* unicast cipher list */ 1239 selcnt = frm; 1240 ADDSHORT(frm, 0); /* selector count */ 1241 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 1242 selcnt[0]++; 1243 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 1244 } 1245 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 1246 selcnt[0]++; 1247 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 1248 } 1249 1250 /* authenticator selector list */ 1251 selcnt = frm; 1252 ADDSHORT(frm, 0); /* selector count */ 1253 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 1254 selcnt[0]++; 1255 ADDSELECTOR(frm, key_mgt_unspec); 1256 } 1257 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 1258 selcnt[0]++; 1259 ADDSELECTOR(frm, key_mgt_psk); 1260 } 1261 1262 /* optional capabilities */ 1263 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH) 1264 ADDSHORT(frm, rsn->rsn_caps); 1265 1266 /* calculate element length */ 1267 ie[1] = frm - ie - 2; 1268 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 1269 ("WPA IE too big, %u > %zu", 1270 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 1271 return frm; 1272#undef ADDSHORT 1273#undef ADDSELECTOR 1274#undef WPA_OUI_BYTES 1275} 1276 1277static uint8_t * 1278ieee80211_setup_rsn_ie(struct ieee80211com *ic, uint8_t *ie) 1279{ 1280#define RSN_OUI_BYTES 0x00, 0x0f, 0xac 1281#define ADDSHORT(frm, v) do { \ 1282 frm[0] = (v) & 0xff; \ 1283 frm[1] = (v) >> 8; \ 1284 frm += 2; \ 1285} while (0) 1286#define ADDSELECTOR(frm, sel) do { \ 1287 memcpy(frm, sel, 4); \ 1288 frm += 4; \ 1289} while (0) 1290 static const uint8_t cipher_suite[][4] = { 1291 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */ 1292 { RSN_OUI_BYTES, RSN_CSE_TKIP }, 1293 { RSN_OUI_BYTES, RSN_CSE_WRAP }, 1294 { RSN_OUI_BYTES, RSN_CSE_CCMP }, 1295 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 1296 { RSN_OUI_BYTES, RSN_CSE_NULL }, 1297 }; 1298 static const uint8_t wep104_suite[4] = 1299 { RSN_OUI_BYTES, RSN_CSE_WEP104 }; 1300 static const uint8_t key_mgt_unspec[4] = 1301 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC }; 1302 static const uint8_t key_mgt_psk[4] = 1303 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK }; 1304 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 1305 uint8_t *frm = ie; 1306 uint8_t *selcnt; 1307 1308 *frm++ = IEEE80211_ELEMID_RSN; 1309 *frm++ = 0; /* length filled in below */ 1310 ADDSHORT(frm, RSN_VERSION); 1311 1312 /* XXX filter out CKIP */ 1313 1314 /* multicast cipher */ 1315 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 1316 rsn->rsn_mcastkeylen >= 13) 1317 ADDSELECTOR(frm, wep104_suite); 1318 else 1319 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 1320 1321 /* unicast cipher list */ 1322 selcnt = frm; 1323 ADDSHORT(frm, 0); /* selector count */ 1324 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 1325 selcnt[0]++; 1326 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 1327 } 1328 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 1329 selcnt[0]++; 1330 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 1331 } 1332 1333 /* authenticator selector list */ 1334 selcnt = frm; 1335 ADDSHORT(frm, 0); /* selector count */ 1336 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 1337 selcnt[0]++; 1338 ADDSELECTOR(frm, key_mgt_unspec); 1339 } 1340 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 1341 selcnt[0]++; 1342 ADDSELECTOR(frm, key_mgt_psk); 1343 } 1344 1345 /* optional capabilities */ 1346 ADDSHORT(frm, rsn->rsn_caps); 1347 /* XXX PMKID */ 1348 1349 /* calculate element length */ 1350 ie[1] = frm - ie - 2; 1351 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 1352 ("RSN IE too big, %u > %zu", 1353 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 1354 return frm; 1355#undef ADDSELECTOR 1356#undef ADDSHORT 1357#undef RSN_OUI_BYTES 1358} 1359 1360/* 1361 * Add a WPA/RSN element to a frame. 1362 */ 1363static uint8_t * 1364ieee80211_add_wpa(uint8_t *frm, struct ieee80211com *ic) 1365{ 1366 1367 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!")); 1368 if (ic->ic_flags & IEEE80211_F_WPA2) 1369 frm = ieee80211_setup_rsn_ie(ic, frm); 1370 if (ic->ic_flags & IEEE80211_F_WPA1) 1371 frm = ieee80211_setup_wpa_ie(ic, frm); 1372 return frm; 1373} 1374 1375#define WME_OUI_BYTES 0x00, 0x50, 0xf2 1376/* 1377 * Add a WME information element to a frame. 1378 */ 1379static uint8_t * 1380ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme) 1381{ 1382 static const struct ieee80211_wme_info info = { 1383 .wme_id = IEEE80211_ELEMID_VENDOR, 1384 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1385 .wme_oui = { WME_OUI_BYTES }, 1386 .wme_type = WME_OUI_TYPE, 1387 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1388 .wme_version = WME_VERSION, 1389 .wme_info = 0, 1390 }; 1391 memcpy(frm, &info, sizeof(info)); 1392 return frm + sizeof(info); 1393} 1394 1395/* 1396 * Add a WME parameters element to a frame. 1397 */ 1398static uint8_t * 1399ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme) 1400{ 1401#define SM(_v, _f) (((_v) << _f##_S) & _f) 1402#define ADDSHORT(frm, v) do { \ 1403 frm[0] = (v) & 0xff; \ 1404 frm[1] = (v) >> 8; \ 1405 frm += 2; \ 1406} while (0) 1407 /* NB: this works 'cuz a param has an info at the front */ 1408 static const struct ieee80211_wme_info param = { 1409 .wme_id = IEEE80211_ELEMID_VENDOR, 1410 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1411 .wme_oui = { WME_OUI_BYTES }, 1412 .wme_type = WME_OUI_TYPE, 1413 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1414 .wme_version = WME_VERSION, 1415 }; 1416 int i; 1417 1418 memcpy(frm, ¶m, sizeof(param)); 1419 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1420 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1421 *frm++ = 0; /* reserved field */ 1422 for (i = 0; i < WME_NUM_AC; i++) { 1423 const struct wmeParams *ac = 1424 &wme->wme_bssChanParams.cap_wmeParams[i]; 1425 *frm++ = SM(i, WME_PARAM_ACI) 1426 | SM(ac->wmep_acm, WME_PARAM_ACM) 1427 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1428 ; 1429 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1430 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1431 ; 1432 ADDSHORT(frm, ac->wmep_txopLimit); 1433 } 1434 return frm; 1435#undef SM 1436#undef ADDSHORT 1437} 1438#undef WME_OUI_BYTES 1439 1440#define ATH_OUI_BYTES 0x00, 0x03, 0x7f 1441/* 1442 * Add a WME information element to a frame. 1443 */ 1444static uint8_t * 1445ieee80211_add_ath(uint8_t *frm, uint8_t caps, uint16_t defkeyix) 1446{ 1447 static const struct ieee80211_ath_ie info = { 1448 .ath_id = IEEE80211_ELEMID_VENDOR, 1449 .ath_len = sizeof(struct ieee80211_ath_ie) - 2, 1450 .ath_oui = { ATH_OUI_BYTES }, 1451 .ath_oui_type = ATH_OUI_TYPE, 1452 .ath_oui_subtype= ATH_OUI_SUBTYPE, 1453 .ath_version = ATH_OUI_VERSION, 1454 }; 1455 struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm; 1456 1457 memcpy(frm, &info, sizeof(info)); 1458 ath->ath_capability = caps; 1459 ath->ath_defkeyix[0] = (defkeyix & 0xff); 1460 ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff); 1461 return frm + sizeof(info); 1462} 1463#undef ATH_OUI_BYTES 1464 1465/* 1466 * Send a probe request frame with the specified ssid 1467 * and any optional information element data. 1468 */ 1469int 1470ieee80211_send_probereq(struct ieee80211_node *ni, 1471 const uint8_t sa[IEEE80211_ADDR_LEN], 1472 const uint8_t da[IEEE80211_ADDR_LEN], 1473 const uint8_t bssid[IEEE80211_ADDR_LEN], 1474 const uint8_t *ssid, size_t ssidlen, 1475 const void *optie, size_t optielen) 1476{ 1477 struct ieee80211com *ic = ni->ni_ic; 1478 struct ieee80211_frame *wh; 1479 const struct ieee80211_rateset *rs; 1480 struct mbuf *m; 1481 uint8_t *frm; 1482 1483 /* 1484 * Hold a reference on the node so it doesn't go away until after 1485 * the xmit is complete all the way in the driver. On error we 1486 * will remove our reference. 1487 */ 1488 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1489 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1490 __func__, __LINE__, 1491 ni, ether_sprintf(ni->ni_macaddr), 1492 ieee80211_node_refcnt(ni)+1); 1493 ieee80211_ref_node(ni); 1494 1495 /* 1496 * prreq frame format 1497 * [tlv] ssid 1498 * [tlv] supported rates 1499 * [tlv] extended supported rates 1500 * [tlv] user-specified ie's 1501 */ 1502 m = ieee80211_getmgtframe(&frm, 1503 ic->ic_headroom + sizeof(struct ieee80211_frame), 1504 2 + IEEE80211_NWID_LEN 1505 + 2 + IEEE80211_RATE_SIZE 1506 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1507 + (optie != NULL ? optielen : 0) 1508 ); 1509 if (m == NULL) { 1510 ic->ic_stats.is_tx_nobuf++; 1511 ieee80211_free_node(ni); 1512 return ENOMEM; 1513 } 1514 1515 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1516 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 1517 frm = ieee80211_add_rates(frm, rs); 1518 frm = ieee80211_add_xrates(frm, rs); 1519 1520 if (optie != NULL) { 1521 memcpy(frm, optie, optielen); 1522 frm += optielen; 1523 } 1524 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1525 1526 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1527 if (m == NULL) 1528 return ENOMEM; 1529 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 1530 m->m_pkthdr.rcvif = (void *)ni; 1531 1532 wh = mtod(m, struct ieee80211_frame *); 1533 ieee80211_send_setup(ic, ni, wh, 1534 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1535 sa, da, bssid); 1536 /* XXX power management? */ 1537 1538 IEEE80211_NODE_STAT(ni, tx_probereq); 1539 IEEE80211_NODE_STAT(ni, tx_mgmt); 1540 1541 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1542 "[%s] send probe req on channel %u\n", 1543 ether_sprintf(wh->i_addr1), 1544 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1545 1546 IF_ENQUEUE(&ic->ic_mgtq, m); 1547 if_start(ic->ic_ifp); 1548 return 0; 1549} 1550 1551/* 1552 * Calculate capability information for mgt frames. 1553 */ 1554static uint16_t 1555getcapinfo(struct ieee80211com *ic, struct ieee80211_channel *chan) 1556{ 1557 uint16_t capinfo; 1558 1559 KASSERT(ic->ic_opmode != IEEE80211_M_STA, ("station mode")); 1560 1561 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1562 capinfo = IEEE80211_CAPINFO_ESS; 1563 else if (ic->ic_opmode == IEEE80211_M_IBSS) 1564 capinfo = IEEE80211_CAPINFO_IBSS; 1565 else 1566 capinfo = 0; 1567 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1568 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1569 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1570 IEEE80211_IS_CHAN_2GHZ(chan)) 1571 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1572 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1573 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1574 return capinfo; 1575} 1576 1577/* 1578 * Send a management frame. The node is for the destination (or ic_bss 1579 * when in station mode). Nodes other than ic_bss have their reference 1580 * count bumped to reflect our use for an indeterminant time. 1581 */ 1582int 1583ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 1584 int type, int arg) 1585{ 1586#define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 1587 struct mbuf *m; 1588 uint8_t *frm; 1589 uint16_t capinfo; 1590 int has_challenge, is_shared_key, ret, status; 1591 1592 KASSERT(ni != NULL, ("null node")); 1593 1594 /* 1595 * Hold a reference on the node so it doesn't go away until after 1596 * the xmit is complete all the way in the driver. On error we 1597 * will remove our reference. 1598 */ 1599 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1600 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1601 __func__, __LINE__, 1602 ni, ether_sprintf(ni->ni_macaddr), 1603 ieee80211_node_refcnt(ni)+1); 1604 ieee80211_ref_node(ni); 1605 1606 switch (type) { 1607 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1608 /* 1609 * probe response frame format 1610 * [8] time stamp 1611 * [2] beacon interval 1612 * [2] cabability information 1613 * [tlv] ssid 1614 * [tlv] supported rates 1615 * [tlv] parameter set (FH/DS) 1616 * [tlv] parameter set (IBSS) 1617 * [tlv] extended rate phy (ERP) 1618 * [tlv] extended supported rates 1619 * [tlv] WPA 1620 * [tlv] WME (optional) 1621 * [tlv] HT capabilities 1622 * [tlv] HT information 1623 * [tlv] Vendor OUI HT capabilities (optional) 1624 * [tlv] Vendor OUI HT information (optional) 1625 * [tlv] Atheros capabilities 1626 */ 1627 m = ieee80211_getmgtframe(&frm, 1628 ic->ic_headroom + sizeof(struct ieee80211_frame), 1629 8 1630 + sizeof(uint16_t) 1631 + sizeof(uint16_t) 1632 + 2 + IEEE80211_NWID_LEN 1633 + 2 + IEEE80211_RATE_SIZE 1634 + 7 /* max(7,3) */ 1635 + 6 1636 + 3 1637 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1638 /* XXX !WPA1+WPA2 fits w/o a cluster */ 1639 + (ic->ic_flags & IEEE80211_F_WPA ? 1640 2*sizeof(struct ieee80211_ie_wpa) : 0) 1641 + sizeof(struct ieee80211_wme_param) 1642 /* XXX check for cluster requirement */ 1643 + 2*sizeof(struct ieee80211_ie_htcap) + 4 1644 + 2*sizeof(struct ieee80211_ie_htinfo) + 4 1645 + sizeof(struct ieee80211_ath_ie) 1646 ); 1647 if (m == NULL) 1648 senderr(ENOMEM, is_tx_nobuf); 1649 1650 memset(frm, 0, 8); /* timestamp should be filled later */ 1651 frm += 8; 1652 *(uint16_t *)frm = htole16(ic->ic_bss->ni_intval); 1653 frm += 2; 1654 capinfo = getcapinfo(ic, ic->ic_curchan); 1655 *(uint16_t *)frm = htole16(capinfo); 1656 frm += 2; 1657 1658 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1659 ic->ic_bss->ni_esslen); 1660 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1661 1662 if (IEEE80211_IS_CHAN_FHSS(ic->ic_curchan)) { 1663 *frm++ = IEEE80211_ELEMID_FHPARMS; 1664 *frm++ = 5; 1665 *frm++ = ni->ni_fhdwell & 0x00ff; 1666 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff; 1667 *frm++ = IEEE80211_FH_CHANSET( 1668 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1669 *frm++ = IEEE80211_FH_CHANPAT( 1670 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1671 *frm++ = ni->ni_fhindex; 1672 } else { 1673 *frm++ = IEEE80211_ELEMID_DSPARMS; 1674 *frm++ = 1; 1675 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan); 1676 } 1677 1678 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1679 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1680 *frm++ = 2; 1681 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1682 } 1683 if (ic->ic_flags & IEEE80211_F_WPA) 1684 frm = ieee80211_add_wpa(frm, ic); 1685 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) 1686 frm = ieee80211_add_erp(frm, ic); 1687 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1688 if (ic->ic_flags & IEEE80211_F_WME) 1689 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1690 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) { 1691 frm = ieee80211_add_htcap(frm, ni); 1692 frm = ieee80211_add_htinfo(frm, ni); 1693 if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) { 1694 frm = ieee80211_add_htcap_vendor(frm, ni); 1695 frm = ieee80211_add_htinfo_vendor(frm, ni); 1696 } 1697 } 1698 if (ni->ni_ath_ie != NULL) 1699 frm = ieee80211_add_ath(frm, ni->ni_ath_flags, 1700 ni->ni_ath_defkeyix); 1701 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1702 break; 1703 1704 case IEEE80211_FC0_SUBTYPE_AUTH: 1705 status = arg >> 16; 1706 arg &= 0xffff; 1707 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1708 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1709 ni->ni_challenge != NULL); 1710 1711 /* 1712 * Deduce whether we're doing open authentication or 1713 * shared key authentication. We do the latter if 1714 * we're in the middle of a shared key authentication 1715 * handshake or if we're initiating an authentication 1716 * request and configured to use shared key. 1717 */ 1718 is_shared_key = has_challenge || 1719 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1720 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1721 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED); 1722 1723 m = ieee80211_getmgtframe(&frm, 1724 ic->ic_headroom + sizeof(struct ieee80211_frame), 1725 3 * sizeof(uint16_t) 1726 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1727 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0) 1728 ); 1729 if (m == NULL) 1730 senderr(ENOMEM, is_tx_nobuf); 1731 1732 ((uint16_t *)frm)[0] = 1733 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1734 : htole16(IEEE80211_AUTH_ALG_OPEN); 1735 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 1736 ((uint16_t *)frm)[2] = htole16(status);/* status */ 1737 1738 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1739 ((uint16_t *)frm)[3] = 1740 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1741 IEEE80211_ELEMID_CHALLENGE); 1742 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 1743 IEEE80211_CHALLENGE_LEN); 1744 m->m_pkthdr.len = m->m_len = 1745 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 1746 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1747 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1748 "[%s] request encrypt frame (%s)\n", 1749 ether_sprintf(ni->ni_macaddr), __func__); 1750 m->m_flags |= M_LINK0; /* WEP-encrypt, please */ 1751 } 1752 } else 1753 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 1754 1755 /* XXX not right for shared key */ 1756 if (status == IEEE80211_STATUS_SUCCESS) 1757 IEEE80211_NODE_STAT(ni, tx_auth); 1758 else 1759 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1760 1761 if (ic->ic_opmode == IEEE80211_M_STA) 1762 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 1763 (void *) ic->ic_state); 1764 break; 1765 1766 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1767 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1768 "[%s] send station deauthenticate (reason %d)\n", 1769 ether_sprintf(ni->ni_macaddr), arg); 1770 m = ieee80211_getmgtframe(&frm, 1771 ic->ic_headroom + sizeof(struct ieee80211_frame), 1772 sizeof(uint16_t)); 1773 if (m == NULL) 1774 senderr(ENOMEM, is_tx_nobuf); 1775 *(uint16_t *)frm = htole16(arg); /* reason */ 1776 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 1777 1778 IEEE80211_NODE_STAT(ni, tx_deauth); 1779 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1780 1781 ieee80211_node_unauthorize(ni); /* port closed */ 1782 break; 1783 1784 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1785 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1786 /* 1787 * asreq frame format 1788 * [2] capability information 1789 * [2] listen interval 1790 * [6*] current AP address (reassoc only) 1791 * [tlv] ssid 1792 * [tlv] supported rates 1793 * [tlv] extended supported rates 1794 * [tlv] WME 1795 * [tlv] HT capabilities 1796 * [tlv] Vendor OUI HT capabilities (optional) 1797 * [tlv] Atheros capabilities (if negotiated) 1798 * [tlv] user-specified ie's 1799 */ 1800 m = ieee80211_getmgtframe(&frm, 1801 ic->ic_headroom + sizeof(struct ieee80211_frame), 1802 sizeof(uint16_t) 1803 + sizeof(uint16_t) 1804 + IEEE80211_ADDR_LEN 1805 + 2 + IEEE80211_NWID_LEN 1806 + 2 + IEEE80211_RATE_SIZE 1807 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1808 + sizeof(struct ieee80211_wme_info) 1809 + 2*sizeof(struct ieee80211_ie_htcap) + 4 1810 + sizeof(struct ieee80211_ath_ie) 1811 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 1812 ); 1813 if (m == NULL) 1814 senderr(ENOMEM, is_tx_nobuf); 1815 1816 KASSERT(ic->ic_opmode == IEEE80211_M_STA, 1817 ("wrong mode %u", ic->ic_opmode)); 1818 capinfo = IEEE80211_CAPINFO_ESS; 1819 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1820 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1821 /* 1822 * NB: Some 11a AP's reject the request when 1823 * short premable is set. 1824 */ 1825 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1826 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1827 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1828 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1829 (ic->ic_caps & IEEE80211_C_SHSLOT)) 1830 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1831 *(uint16_t *)frm = htole16(capinfo); 1832 frm += 2; 1833 1834 KASSERT(ic->ic_bss->ni_intval != 0, 1835 ("beacon interval is zero!")); 1836 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 1837 ic->ic_bss->ni_intval)); 1838 frm += 2; 1839 1840 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1841 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1842 frm += IEEE80211_ADDR_LEN; 1843 } 1844 1845 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1846 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1847 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1848 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1849 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1850 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) { 1851 frm = ieee80211_add_htcap(frm, ni); 1852 if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) 1853 frm = ieee80211_add_htcap_vendor(frm, ni); 1854 } 1855 if (IEEE80211_ATH_CAP(ic, ni, IEEE80211_F_ATHEROS)) 1856 frm = ieee80211_add_ath(frm, 1857 IEEE80211_ATH_CAP(ic, ni, IEEE80211_F_ATHEROS), 1858 (ic->ic_flags & IEEE80211_F_WPA) == 0 && 1859 ni->ni_authmode != IEEE80211_AUTH_8021X && 1860 ic->ic_def_txkey != IEEE80211_KEYIX_NONE ? 1861 ic->ic_def_txkey : 0x7fff); 1862 if (ic->ic_opt_ie != NULL) { 1863 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 1864 frm += ic->ic_opt_ie_len; 1865 } 1866 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1867 1868 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 1869 (void *) ic->ic_state); 1870 break; 1871 1872 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1873 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1874 /* 1875 * asresp frame format 1876 * [2] capability information 1877 * [2] status 1878 * [2] association ID 1879 * [tlv] supported rates 1880 * [tlv] extended supported rates 1881 * [tlv] WME (if enabled and STA enabled) 1882 * [tlv] HT capabilities (standard or vendor OUI) 1883 * [tlv] HT information (standard or vendor OUI) 1884 * [tlv] Atheros capabilities (if enabled and STA enabled) 1885 */ 1886 m = ieee80211_getmgtframe(&frm, 1887 ic->ic_headroom + sizeof(struct ieee80211_frame), 1888 sizeof(uint16_t) 1889 + sizeof(uint16_t) 1890 + sizeof(uint16_t) 1891 + 2 + IEEE80211_RATE_SIZE 1892 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1893 + sizeof(struct ieee80211_wme_param) 1894 + sizeof(struct ieee80211_ie_htcap) + 4 1895 + sizeof(struct ieee80211_ie_htinfo) + 4 1896 + sizeof(struct ieee80211_ath_ie) 1897 ); 1898 if (m == NULL) 1899 senderr(ENOMEM, is_tx_nobuf); 1900 1901 capinfo = getcapinfo(ic, ic->ic_curchan); 1902 *(uint16_t *)frm = htole16(capinfo); 1903 frm += 2; 1904 1905 *(uint16_t *)frm = htole16(arg); /* status */ 1906 frm += 2; 1907 1908 if (arg == IEEE80211_STATUS_SUCCESS) { 1909 *(uint16_t *)frm = htole16(ni->ni_associd); 1910 IEEE80211_NODE_STAT(ni, tx_assoc); 1911 } else 1912 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1913 frm += 2; 1914 1915 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1916 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1917 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1918 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1919 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) { 1920 /* NB: respond according to what we received */ 1921 if (ni->ni_flags & IEEE80211_NODE_HTCOMPAT) { 1922 frm = ieee80211_add_htcap_vendor(frm, ni); 1923 frm = ieee80211_add_htinfo_vendor(frm, ni); 1924 } else { 1925 frm = ieee80211_add_htcap(frm, ni); 1926 frm = ieee80211_add_htinfo(frm, ni); 1927 } 1928 } 1929 if (IEEE80211_ATH_CAP(ic, ni, IEEE80211_F_ATHEROS)) 1930 frm = ieee80211_add_ath(frm, 1931 IEEE80211_ATH_CAP(ic, ni, IEEE80211_F_ATHEROS), 1932 ni->ni_ath_defkeyix); 1933 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1934 break; 1935 1936 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1937 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1938 "[%s] send station disassociate (reason %d)\n", 1939 ether_sprintf(ni->ni_macaddr), arg); 1940 m = ieee80211_getmgtframe(&frm, 1941 ic->ic_headroom + sizeof(struct ieee80211_frame), 1942 sizeof(uint16_t)); 1943 if (m == NULL) 1944 senderr(ENOMEM, is_tx_nobuf); 1945 *(uint16_t *)frm = htole16(arg); /* reason */ 1946 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 1947 1948 IEEE80211_NODE_STAT(ni, tx_disassoc); 1949 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 1950 break; 1951 1952 default: 1953 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1954 "[%s] invalid mgmt frame type %u\n", 1955 ether_sprintf(ni->ni_macaddr), type); 1956 senderr(EINVAL, is_tx_unknownmgt); 1957 /* NOTREACHED */ 1958 } 1959 1960 ret = ieee80211_mgmt_output(ic, ni, m, type); 1961 if (ret != 0) 1962 goto bad; 1963 return 0; 1964bad: 1965 ieee80211_free_node(ni); 1966 return ret; 1967#undef senderr 1968} 1969 1970static void 1971ieee80211_tx_mgt_timeout(void *arg) 1972{ 1973 struct ieee80211_node *ni = arg; 1974 struct ieee80211com *ic = ni->ni_ic; 1975 1976 if (ic->ic_state != IEEE80211_S_INIT && 1977 (ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1978 /* 1979 * NB: it's safe to specify a timeout as the reason here; 1980 * it'll only be used in the right state. 1981 */ 1982 ieee80211_new_state(ic, IEEE80211_S_SCAN, 1983 IEEE80211_SCAN_FAIL_TIMEOUT); 1984 } 1985} 1986 1987static void 1988ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 1989{ 1990 struct ieee80211com *ic = ni->ni_ic; 1991 enum ieee80211_state ostate = (enum ieee80211_state) arg; 1992 1993 /* 1994 * Frame transmit completed; arrange timer callback. If 1995 * transmit was successfuly we wait for response. Otherwise 1996 * we arrange an immediate callback instead of doing the 1997 * callback directly since we don't know what state the driver 1998 * is in (e.g. what locks it is holding). This work should 1999 * not be too time-critical and not happen too often so the 2000 * added overhead is acceptable. 2001 * 2002 * XXX what happens if !acked but response shows up before callback? 2003 */ 2004 if (ic->ic_state == ostate) 2005 callout_reset(&ic->ic_mgtsend, 2006 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 2007 ieee80211_tx_mgt_timeout, ni); 2008} 2009 2010/* 2011 * Allocate a beacon frame and fillin the appropriate bits. 2012 */ 2013struct mbuf * 2014ieee80211_beacon_alloc(struct ieee80211_node *ni, 2015 struct ieee80211_beacon_offsets *bo) 2016{ 2017 struct ieee80211com *ic = ni->ni_ic; 2018 struct ifnet *ifp = ic->ic_ifp; 2019 struct ieee80211_frame *wh; 2020 struct mbuf *m; 2021 int pktlen; 2022 uint8_t *frm; 2023 uint16_t capinfo; 2024 struct ieee80211_rateset *rs; 2025 2026 /* 2027 * beacon frame format 2028 * [8] time stamp 2029 * [2] beacon interval 2030 * [2] cabability information 2031 * [tlv] ssid 2032 * [tlv] supported rates 2033 * [3] parameter set (DS) 2034 * [tlv] parameter set (IBSS/TIM) 2035 * [tlv] country code 2036 * [tlv] extended rate phy (ERP) 2037 * [tlv] extended supported rates 2038 * [tlv] WME parameters 2039 * [tlv] WPA/RSN parameters 2040 * [tlv] HT capabilities 2041 * [tlv] HT information 2042 * [tlv] Vendor OUI HT capabilities (optional) 2043 * [tlv] Vendor OUI HT information (optional) 2044 * XXX Vendor-specific OIDs (e.g. Atheros) 2045 * NB: we allocate the max space required for the TIM bitmap. 2046 */ 2047 rs = &ni->ni_rates; 2048 pktlen = 8 /* time stamp */ 2049 + sizeof(uint16_t) /* beacon interval */ 2050 + sizeof(uint16_t) /* capabilities */ 2051 + 2 + ni->ni_esslen /* ssid */ 2052 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 2053 + 2 + 1 /* DS parameters */ 2054 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */ 2055 + sizeof(struct ieee80211_country_ie) /* country code */ 2056 + 2 + 1 /* ERP */ 2057 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2058 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */ 2059 sizeof(struct ieee80211_wme_param) : 0) 2060 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 2061 2*sizeof(struct ieee80211_ie_wpa) : 0) 2062 /* XXX conditional? */ 2063 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 2064 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 2065 ; 2066 m = ieee80211_getmgtframe(&frm, 2067 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 2068 if (m == NULL) { 2069 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 2070 "%s: cannot get buf; size %u\n", __func__, pktlen); 2071 ic->ic_stats.is_tx_nobuf++; 2072 return NULL; 2073 } 2074 2075 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 2076 frm += 8; 2077 *(uint16_t *)frm = htole16(ni->ni_intval); 2078 frm += 2; 2079 capinfo = getcapinfo(ic, ni->ni_chan); 2080 bo->bo_caps = (uint16_t *)frm; 2081 *(uint16_t *)frm = htole16(capinfo); 2082 frm += 2; 2083 *frm++ = IEEE80211_ELEMID_SSID; 2084 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) { 2085 *frm++ = ni->ni_esslen; 2086 memcpy(frm, ni->ni_essid, ni->ni_esslen); 2087 frm += ni->ni_esslen; 2088 } else 2089 *frm++ = 0; 2090 frm = ieee80211_add_rates(frm, rs); 2091 if (!IEEE80211_IS_CHAN_FHSS(ic->ic_bsschan)) { 2092 *frm++ = IEEE80211_ELEMID_DSPARMS; 2093 *frm++ = 1; 2094 *frm++ = ieee80211_chan2ieee(ic, ic->ic_bsschan); 2095 } 2096 bo->bo_tim = frm; 2097 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2098 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2099 *frm++ = 2; 2100 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2101 bo->bo_tim_len = 0; 2102 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2103 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 2104 2105 tie->tim_ie = IEEE80211_ELEMID_TIM; 2106 tie->tim_len = 4; /* length */ 2107 tie->tim_count = 0; /* DTIM count */ 2108 tie->tim_period = ic->ic_dtim_period; /* DTIM period */ 2109 tie->tim_bitctl = 0; /* bitmap control */ 2110 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 2111 frm += sizeof(struct ieee80211_tim_ie); 2112 bo->bo_tim_len = 1; 2113 } 2114 bo->bo_tim_trailer = frm; 2115 if (ic->ic_flags & IEEE80211_F_DOTH) 2116 frm = ieee80211_add_countryie(frm, ic, 2117 ic->ic_countrycode, ic->ic_location); 2118 if (ic->ic_flags & IEEE80211_F_WME) { 2119 bo->bo_wme = frm; 2120 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2121 } else 2122 bo->bo_wme = NULL; 2123 if (ic->ic_flags & IEEE80211_F_WPA) 2124 frm = ieee80211_add_wpa(frm, ic); 2125 if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan)) { 2126 bo->bo_erp = frm; 2127 frm = ieee80211_add_erp(frm, ic); 2128 } else 2129 bo->bo_erp = NULL; 2130 frm = ieee80211_add_xrates(frm, rs); 2131 if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) { 2132 frm = ieee80211_add_htcap(frm, ni); 2133 bo->bo_htinfo = frm; 2134 frm = ieee80211_add_htinfo(frm, ni); 2135 if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) { 2136 frm = ieee80211_add_htcap_vendor(frm, ni); 2137 frm = ieee80211_add_htinfo_vendor(frm, ni); 2138 } 2139 } else 2140 bo->bo_htinfo = NULL; 2141 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 2142 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2143 2144 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 2145 KASSERT(m != NULL, ("no space for 802.11 header?")); 2146 wh = mtod(m, struct ieee80211_frame *); 2147 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2148 IEEE80211_FC0_SUBTYPE_BEACON; 2149 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2150 *(uint16_t *)wh->i_dur = 0; 2151 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 2152 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 2153 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 2154 *(uint16_t *)wh->i_seq = 0; 2155 2156 return m; 2157} 2158 2159/* 2160 * Update the dynamic parts of a beacon frame based on the current state. 2161 */ 2162int 2163ieee80211_beacon_update(struct ieee80211_node *ni, 2164 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 2165{ 2166 struct ieee80211com *ic = ni->ni_ic; 2167 int len_changed = 0; 2168 uint16_t capinfo; 2169 2170 IEEE80211_BEACON_LOCK(ic); 2171 /* XXX faster to recalculate entirely or just changes? */ 2172 capinfo = getcapinfo(ic, ni->ni_chan); 2173 *bo->bo_caps = htole16(capinfo); 2174 2175 if (ic->ic_flags & IEEE80211_F_WME) { 2176 struct ieee80211_wme_state *wme = &ic->ic_wme; 2177 2178 /* 2179 * Check for agressive mode change. When there is 2180 * significant high priority traffic in the BSS 2181 * throttle back BE traffic by using conservative 2182 * parameters. Otherwise BE uses agressive params 2183 * to optimize performance of legacy/non-QoS traffic. 2184 */ 2185 if (wme->wme_flags & WME_F_AGGRMODE) { 2186 if (wme->wme_hipri_traffic > 2187 wme->wme_hipri_switch_thresh) { 2188 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 2189 "%s: traffic %u, disable aggressive mode\n", 2190 __func__, wme->wme_hipri_traffic); 2191 wme->wme_flags &= ~WME_F_AGGRMODE; 2192 ieee80211_wme_updateparams_locked(ic); 2193 wme->wme_hipri_traffic = 2194 wme->wme_hipri_switch_hysteresis; 2195 } else 2196 wme->wme_hipri_traffic = 0; 2197 } else { 2198 if (wme->wme_hipri_traffic <= 2199 wme->wme_hipri_switch_thresh) { 2200 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 2201 "%s: traffic %u, enable aggressive mode\n", 2202 __func__, wme->wme_hipri_traffic); 2203 wme->wme_flags |= WME_F_AGGRMODE; 2204 ieee80211_wme_updateparams_locked(ic); 2205 wme->wme_hipri_traffic = 0; 2206 } else 2207 wme->wme_hipri_traffic = 2208 wme->wme_hipri_switch_hysteresis; 2209 } 2210 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 2211 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 2212 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 2213 } 2214 } 2215 2216 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 2217 ieee80211_ht_update_beacon(ic, bo); 2218 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 2219 } 2220 2221 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 2222 struct ieee80211_tim_ie *tie = 2223 (struct ieee80211_tim_ie *) bo->bo_tim; 2224 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 2225 u_int timlen, timoff, i; 2226 /* 2227 * ATIM/DTIM needs updating. If it fits in the 2228 * current space allocated then just copy in the 2229 * new bits. Otherwise we need to move any trailing 2230 * data to make room. Note that we know there is 2231 * contiguous space because ieee80211_beacon_allocate 2232 * insures there is space in the mbuf to write a 2233 * maximal-size virtual bitmap (based on ic_max_aid). 2234 */ 2235 /* 2236 * Calculate the bitmap size and offset, copy any 2237 * trailer out of the way, and then copy in the 2238 * new bitmap and update the information element. 2239 * Note that the tim bitmap must contain at least 2240 * one byte and any offset must be even. 2241 */ 2242 if (ic->ic_ps_pending != 0) { 2243 timoff = 128; /* impossibly large */ 2244 for (i = 0; i < ic->ic_tim_len; i++) 2245 if (ic->ic_tim_bitmap[i]) { 2246 timoff = i &~ 1; 2247 break; 2248 } 2249 KASSERT(timoff != 128, ("tim bitmap empty!")); 2250 for (i = ic->ic_tim_len-1; i >= timoff; i--) 2251 if (ic->ic_tim_bitmap[i]) 2252 break; 2253 timlen = 1 + (i - timoff); 2254 } else { 2255 timoff = 0; 2256 timlen = 1; 2257 } 2258 if (timlen != bo->bo_tim_len) { 2259 /* copy up/down trailer */ 2260 int adjust = tie->tim_bitmap+timlen 2261 - bo->bo_tim_trailer; 2262 ovbcopy(bo->bo_tim_trailer, 2263 bo->bo_tim_trailer+adjust, 2264 bo->bo_tim_trailer_len); 2265 bo->bo_tim_trailer += adjust; 2266 bo->bo_wme += adjust; 2267 bo->bo_erp += adjust; 2268 bo->bo_htinfo += adjust; 2269 bo->bo_tim_len = timlen; 2270 2271 /* update information element */ 2272 tie->tim_len = 3 + timlen; 2273 tie->tim_bitctl = timoff; 2274 len_changed = 1; 2275 } 2276 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff, 2277 bo->bo_tim_len); 2278 2279 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 2280 2281 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2282 "%s: TIM updated, pending %u, off %u, len %u\n", 2283 __func__, ic->ic_ps_pending, timoff, timlen); 2284 } 2285 /* count down DTIM period */ 2286 if (tie->tim_count == 0) 2287 tie->tim_count = tie->tim_period - 1; 2288 else 2289 tie->tim_count--; 2290 /* update state for buffered multicast frames on DTIM */ 2291 if (mcast && tie->tim_count == 0) 2292 tie->tim_bitctl |= 1; 2293 else 2294 tie->tim_bitctl &= ~1; 2295 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 2296 /* 2297 * ERP element needs updating. 2298 */ 2299 (void) ieee80211_add_erp(bo->bo_erp, ic); 2300 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 2301 } 2302 } 2303 IEEE80211_BEACON_UNLOCK(ic); 2304 2305 return len_changed; 2306} 2307