1 /* $NetBSD: ieee80211_output.c,v 1.47 2007/03/04 06:03:19 christos Exp $ */ 2 /*- 3 * Copyright (c) 2001 Atsushi Onoe 4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * Alternatively, this software may be distributed under the terms of the 19 * GNU General Public License ("GPL") version 2 as published by the Free 20 * Software Foundation. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifdef __FreeBSD__ 36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.34 2005/08/10 16:22:29 sam Exp $"); 37 #endif 38 #ifdef __NetBSD__ 39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.47 2007/03/04 06:03:19 christos Exp $"); 40 #endif 41 42 #include "opt_inet.h" 43 44 #ifdef __NetBSD__ 45 #include "bpfilter.h" 46 #endif /* __NetBSD__ */ 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/mbuf.h> 51 #include <sys/kernel.h> 52 #include <sys/endian.h> 53 #include <sys/errno.h> 54 #include <sys/proc.h> 55 #include <sys/sysctl.h> 56 57 #include <net/if.h> 58 #include <net/if_llc.h> 59 #include <net/if_media.h> 60 #include <net/if_arp.h> 61 #include <net/if_ether.h> 62 #include <net/if_llc.h> 63 #include <net/if_vlanvar.h> 64 65 #include <net80211/ieee80211_netbsd.h> 66 #include <net80211/ieee80211_var.h> 67 68 #if NBPFILTER > 0 69 #include <net/bpf.h> 70 #endif 71 72 #ifdef INET 73 #include <netinet/in.h> 74 #include <netinet/in_systm.h> 75 #include <netinet/in_var.h> 76 #include <netinet/ip.h> 77 #include <net/if_ether.h> 78 #endif 79 80 static int ieee80211_fragment(struct ieee80211com *, struct mbuf *, 81 u_int hdrsize, u_int ciphdrsize, u_int mtu); 82 83 #ifdef IEEE80211_DEBUG 84 /* 85 * Decide if an outbound management frame should be 86 * printed when debugging is enabled. This filters some 87 * of the less interesting frames that come frequently 88 * (e.g. beacons). 89 */ 90 static __inline int 91 doprint(struct ieee80211com *ic, int subtype) 92 { 93 switch (subtype) { 94 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 95 return (ic->ic_opmode == IEEE80211_M_IBSS); 96 } 97 return 1; 98 } 99 #endif 100 101 /* 102 * Set the direction field and address fields of an outgoing 103 * non-QoS frame. Note this should be called early on in 104 * constructing a frame as it sets i_fc[1]; other bits can 105 * then be or'd in. 106 */ 107 static void 108 ieee80211_send_setup(struct ieee80211com *ic, 109 struct ieee80211_node *ni, 110 struct ieee80211_frame *wh, 111 int type, 112 const u_int8_t sa[IEEE80211_ADDR_LEN], 113 const u_int8_t da[IEEE80211_ADDR_LEN], 114 const u_int8_t bssid[IEEE80211_ADDR_LEN]) 115 { 116 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 117 118 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 119 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 120 switch (ic->ic_opmode) { 121 case IEEE80211_M_STA: 122 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 123 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 124 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 125 IEEE80211_ADDR_COPY(wh->i_addr3, da); 126 break; 127 case IEEE80211_M_IBSS: 128 case IEEE80211_M_AHDEMO: 129 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 130 IEEE80211_ADDR_COPY(wh->i_addr1, da); 131 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 132 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 133 break; 134 case IEEE80211_M_HOSTAP: 135 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 136 IEEE80211_ADDR_COPY(wh->i_addr1, da); 137 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 138 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 139 break; 140 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 141 break; 142 } 143 } else { 144 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 145 IEEE80211_ADDR_COPY(wh->i_addr1, da); 146 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 147 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 148 } 149 *(u_int16_t *)&wh->i_dur[0] = 0; 150 /* NB: use non-QoS tid */ 151 *(u_int16_t *)&wh->i_seq[0] = 152 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 153 ni->ni_txseqs[0]++; 154 #undef WH4 155 } 156 157 /* 158 * Send a management frame to the specified node. The node pointer 159 * must have a reference as the pointer will be passed to the driver 160 * and potentially held for a long time. If the frame is successfully 161 * dispatched to the driver, then it is responsible for freeing the 162 * reference (and potentially free'ing up any associated storage). 163 */ 164 static int 165 ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni, 166 struct mbuf *m, int type, int timer) 167 { 168 struct ifnet *ifp = ic->ic_ifp; 169 struct ieee80211_frame *wh; 170 171 IASSERT(ni != NULL, ("null node")); 172 173 /* 174 * Yech, hack alert! We want to pass the node down to the 175 * driver's start routine. If we don't do so then the start 176 * routine must immediately look it up again and that can 177 * cause a lock order reversal if, for example, this frame 178 * is being sent because the station is being timedout and 179 * the frame being sent is a DEAUTH message. We could stick 180 * this in an m_tag and tack that on to the mbuf. However 181 * that's rather expensive to do for every frame so instead 182 * we stuff it in the rcvif field since outbound frames do 183 * not (presently) use this. 184 */ 185 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 186 if (m == NULL) 187 return ENOMEM; 188 #ifdef __FreeBSD__ 189 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 190 #endif 191 m->m_pkthdr.rcvif = (void *)ni; 192 193 wh = mtod(m, struct ieee80211_frame *); 194 ieee80211_send_setup(ic, ni, wh, 195 IEEE80211_FC0_TYPE_MGT | type, 196 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 197 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) { 198 m->m_flags &= ~M_LINK0; 199 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 200 "[%s] encrypting frame (%s)\n", 201 ether_sprintf(wh->i_addr1), __func__); 202 wh->i_fc[1] |= IEEE80211_FC1_WEP; 203 } 204 #ifdef IEEE80211_DEBUG 205 /* avoid printing too many frames */ 206 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) || 207 ieee80211_msg_dumppkts(ic)) { 208 printf("[%s] send %s on channel %u\n", 209 ether_sprintf(wh->i_addr1), 210 ieee80211_mgt_subtype_name[ 211 (type & IEEE80211_FC0_SUBTYPE_MASK) >> 212 IEEE80211_FC0_SUBTYPE_SHIFT], 213 ieee80211_chan2ieee(ic, ic->ic_curchan)); 214 } 215 #endif 216 IEEE80211_NODE_STAT(ni, tx_mgmt); 217 IF_ENQUEUE(&ic->ic_mgtq, m); 218 if (timer) { 219 /* 220 * Set the mgt frame timeout. 221 */ 222 ic->ic_mgt_timer = timer; 223 ifp->if_timer = 1; 224 } 225 (*ifp->if_start)(ifp); 226 return 0; 227 } 228 229 /* 230 * Send a null data frame to the specified node. 231 * 232 * NB: the caller is assumed to have setup a node reference 233 * for use; this is necessary to deal with a race condition 234 * when probing for inactive stations. 235 */ 236 int 237 ieee80211_send_nulldata(struct ieee80211_node *ni) 238 { 239 struct ieee80211com *ic = ni->ni_ic; 240 struct ifnet *ifp = ic->ic_ifp; 241 struct mbuf *m; 242 struct ieee80211_frame *wh; 243 244 MGETHDR(m, M_NOWAIT, MT_HEADER); 245 if (m == NULL) { 246 /* XXX debug msg */ 247 ic->ic_stats.is_tx_nobuf++; 248 ieee80211_unref_node(&ni); 249 return ENOMEM; 250 } 251 m->m_pkthdr.rcvif = (void *) ni; 252 253 wh = mtod(m, struct ieee80211_frame *); 254 ieee80211_send_setup(ic, ni, wh, 255 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 256 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 257 /* NB: power management bit is never sent by an AP */ 258 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 259 ic->ic_opmode != IEEE80211_M_HOSTAP) 260 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 261 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame); 262 263 IEEE80211_NODE_STAT(ni, tx_data); 264 265 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 266 "[%s] send null data frame on channel %u, pwr mgt %s\n", 267 ether_sprintf(ni->ni_macaddr), 268 ieee80211_chan2ieee(ic, ic->ic_curchan), 269 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 270 271 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */ 272 (*ifp->if_start)(ifp); 273 274 return 0; 275 } 276 277 /* 278 * Assign priority to a frame based on any vlan tag assigned 279 * to the station and/or any Diffserv setting in an IP header. 280 * Finally, if an ACM policy is setup (in station mode) it's 281 * applied. 282 */ 283 int 284 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni) 285 { 286 int v_wme_ac, d_wme_ac, ac; 287 #ifdef INET 288 struct ether_header *eh; 289 #endif 290 291 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 292 ac = WME_AC_BE; 293 goto done; 294 } 295 296 /* 297 * If node has a vlan tag then all traffic 298 * to it must have a matching tag. 299 */ 300 v_wme_ac = 0; 301 if (ni->ni_vlan != 0) { 302 /* XXX used to check ec_nvlans. */ 303 struct m_tag *mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL); 304 if (mtag == NULL) { 305 IEEE80211_NODE_STAT(ni, tx_novlantag); 306 return 1; 307 } 308 if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) != 309 EVL_VLANOFTAG(ni->ni_vlan)) { 310 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 311 return 1; 312 } 313 /* map vlan priority to AC */ 314 switch (EVL_PRIOFTAG(ni->ni_vlan)) { 315 case 1: 316 case 2: 317 v_wme_ac = WME_AC_BK; 318 break; 319 case 0: 320 case 3: 321 v_wme_ac = WME_AC_BE; 322 break; 323 case 4: 324 case 5: 325 v_wme_ac = WME_AC_VI; 326 break; 327 case 6: 328 case 7: 329 v_wme_ac = WME_AC_VO; 330 break; 331 } 332 } 333 334 #ifdef INET 335 eh = mtod(m, struct ether_header *); 336 if (eh->ether_type == htons(ETHERTYPE_IP)) { 337 const struct ip *ip = (struct ip *) 338 (mtod(m, u_int8_t *) + sizeof (*eh)); 339 /* 340 * IP frame, map the TOS field. 341 */ 342 switch (ip->ip_tos) { 343 case 0x08: 344 case 0x20: 345 d_wme_ac = WME_AC_BK; /* background */ 346 break; 347 case 0x28: 348 case 0xa0: 349 d_wme_ac = WME_AC_VI; /* video */ 350 break; 351 case 0x30: /* voice */ 352 case 0xe0: 353 case 0x88: /* XXX UPSD */ 354 case 0xb8: 355 d_wme_ac = WME_AC_VO; 356 break; 357 default: 358 d_wme_ac = WME_AC_BE; 359 break; 360 } 361 } else { 362 #endif /* INET */ 363 d_wme_ac = WME_AC_BE; 364 #ifdef INET 365 } 366 #endif 367 /* 368 * Use highest priority AC. 369 */ 370 if (v_wme_ac > d_wme_ac) 371 ac = v_wme_ac; 372 else 373 ac = d_wme_ac; 374 375 /* 376 * Apply ACM policy. 377 */ 378 if (ic->ic_opmode == IEEE80211_M_STA) { 379 static const int acmap[4] = { 380 WME_AC_BK, /* WME_AC_BE */ 381 WME_AC_BK, /* WME_AC_BK */ 382 WME_AC_BE, /* WME_AC_VI */ 383 WME_AC_VI, /* WME_AC_VO */ 384 }; 385 while (ac != WME_AC_BK && 386 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 387 ac = acmap[ac]; 388 } 389 done: 390 M_WME_SETAC(m, ac); 391 return 0; 392 } 393 394 /* 395 * Insure there is sufficient contiguous space to encapsulate the 396 * 802.11 data frame. If room isn't already there, arrange for it. 397 * Drivers and cipher modules assume we have done the necessary work 398 * and fail rudely if they don't find the space they need. 399 */ 400 static struct mbuf * 401 ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize, 402 struct ieee80211_key *key, struct mbuf *m) 403 { 404 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 405 int needed_space = hdrsize; 406 int wlen = 0; 407 408 if (key != NULL) { 409 /* XXX belongs in crypto code? */ 410 needed_space += key->wk_cipher->ic_header; 411 /* XXX frags */ 412 } 413 /* 414 * We know we are called just before stripping an Ethernet 415 * header and prepending an LLC header. This means we know 416 * there will be 417 * sizeof(struct ether_header) - sizeof(struct llc) 418 * bytes recovered to which we need additional space for the 419 * 802.11 header and any crypto header. 420 */ 421 /* XXX check trailing space and copy instead? */ 422 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 423 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 424 if (n == NULL) { 425 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 426 "%s: cannot expand storage\n", __func__); 427 ic->ic_stats.is_tx_nobuf++; 428 m_freem(m); 429 return NULL; 430 } 431 IASSERT(needed_space <= MHLEN, 432 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 433 /* 434 * Setup new mbuf to have leading space to prepend the 435 * 802.11 header and any crypto header bits that are 436 * required (the latter are added when the driver calls 437 * back to ieee80211_crypto_encap to do crypto encapsulation). 438 */ 439 /* NB: must be first 'cuz it clobbers m_data */ 440 M_MOVE_PKTHDR(n, m); 441 n->m_len = 0; /* NB: m_gethdr does not set */ 442 n->m_data += needed_space; 443 /* 444 * Pull up Ethernet header to create the expected layout. 445 * We could use m_pullup but that's overkill (i.e. we don't 446 * need the actual data) and it cannot fail so do it inline 447 * for speed. 448 */ 449 /* NB: struct ether_header is known to be contiguous */ 450 n->m_len += sizeof(struct ether_header); 451 m->m_len -= sizeof(struct ether_header); 452 m->m_data += sizeof(struct ether_header); 453 /* 454 * Replace the head of the chain. 455 */ 456 n->m_next = m; 457 m = n; 458 } else { 459 /* We will overwrite the ethernet header in the 460 * 802.11 encapsulation stage. Make sure that it 461 * is writable. 462 */ 463 wlen = sizeof(struct ether_header); 464 } 465 466 /* 467 * If we're going to s/w encrypt the mbuf chain make sure it is 468 * writable. 469 */ 470 if (key != NULL && (key->wk_flags & IEEE80211_KEY_SWCRYPT) != 0) 471 wlen = M_COPYALL; 472 473 if (wlen != 0 && m_makewritable(&m, 0, wlen, M_DONTWAIT) != 0) { 474 m_freem(m); 475 return NULL; 476 } 477 return m; 478 #undef TO_BE_RECLAIMED 479 } 480 481 /* 482 * Return the transmit key to use in sending a unicast frame. 483 * If a unicast key is set we use that. When no unicast key is set 484 * we fall back to the default transmit key. 485 */ 486 static __inline struct ieee80211_key * 487 ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 488 { 489 if (IEEE80211_KEY_UNDEFINED(ni->ni_ucastkey)) { 490 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 491 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 492 return NULL; 493 return &ic->ic_nw_keys[ic->ic_def_txkey]; 494 } else { 495 return &ni->ni_ucastkey; 496 } 497 } 498 499 /* 500 * Return the transmit key to use in sending a multicast frame. 501 * Multicast traffic always uses the group key which is installed as 502 * the default tx key. 503 */ 504 static __inline struct ieee80211_key * 505 ieee80211_crypto_getmcastkey(struct ieee80211com *ic, 506 struct ieee80211_node *ni) 507 { 508 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 509 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 510 return NULL; 511 return &ic->ic_nw_keys[ic->ic_def_txkey]; 512 } 513 514 /* 515 * Encapsulate an outbound data frame. The mbuf chain is updated. 516 * If an error is encountered NULL is returned. The caller is required 517 * to provide a node reference and pullup the ethernet header in the 518 * first mbuf. 519 */ 520 struct mbuf * 521 ieee80211_encap(struct ieee80211com *ic, struct mbuf *m, 522 struct ieee80211_node *ni) 523 { 524 struct ether_header eh; 525 struct ieee80211_frame *wh; 526 struct ieee80211_key *key; 527 struct llc *llc; 528 int hdrsize, datalen, addqos, txfrag; 529 530 IASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 531 memcpy(&eh, mtod(m, void *), sizeof(struct ether_header)); 532 533 /* 534 * Insure space for additional headers. First identify 535 * transmit key to use in calculating any buffer adjustments 536 * required. This is also used below to do privacy 537 * encapsulation work. Then calculate the 802.11 header 538 * size and any padding required by the driver. 539 * 540 * Note key may be NULL if we fall back to the default 541 * transmit key and that is not set. In that case the 542 * buffer may not be expanded as needed by the cipher 543 * routines, but they will/should discard it. 544 */ 545 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 546 if (ic->ic_opmode == IEEE80211_M_STA || 547 !IEEE80211_IS_MULTICAST(eh.ether_dhost)) 548 key = ieee80211_crypto_getucastkey(ic, ni); 549 else 550 key = ieee80211_crypto_getmcastkey(ic, ni); 551 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) { 552 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 553 "[%s] no default transmit key (%s) deftxkey %u\n", 554 ether_sprintf(eh.ether_dhost), __func__, 555 ic->ic_def_txkey); 556 ic->ic_stats.is_tx_nodefkey++; 557 } 558 } else 559 key = NULL; 560 /* XXX 4-address format */ 561 /* 562 * XXX Some ap's don't handle QoS-encapsulated EAPOL 563 * frames so suppress use. This may be an issue if other 564 * ap's require all data frames to be QoS-encapsulated 565 * once negotiated in which case we'll need to make this 566 * configurable. 567 */ 568 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) && 569 eh.ether_type != htons(ETHERTYPE_PAE); 570 if (addqos) 571 hdrsize = sizeof(struct ieee80211_qosframe); 572 else 573 hdrsize = sizeof(struct ieee80211_frame); 574 if (ic->ic_flags & IEEE80211_F_DATAPAD) 575 hdrsize = roundup(hdrsize, sizeof(u_int32_t)); 576 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m); 577 if (m == NULL) { 578 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 579 goto bad; 580 } 581 582 /* NB: this could be optimized because of ieee80211_mbuf_adjust */ 583 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 584 llc = mtod(m, struct llc *); 585 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 586 llc->llc_control = LLC_UI; 587 llc->llc_snap.org_code[0] = 0; 588 llc->llc_snap.org_code[1] = 0; 589 llc->llc_snap.org_code[2] = 0; 590 llc->llc_snap.ether_type = eh.ether_type; 591 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 592 593 M_PREPEND(m, hdrsize, M_DONTWAIT); 594 if (m == NULL) { 595 ic->ic_stats.is_tx_nobuf++; 596 goto bad; 597 } 598 wh = mtod(m, struct ieee80211_frame *); 599 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 600 *(u_int16_t *)wh->i_dur = 0; 601 switch (ic->ic_opmode) { 602 case IEEE80211_M_STA: 603 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 604 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 605 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 606 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 607 break; 608 case IEEE80211_M_IBSS: 609 case IEEE80211_M_AHDEMO: 610 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 611 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 612 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 613 /* 614 * NB: always use the bssid from ic_bss as the 615 * neighbor's may be stale after an ibss merge 616 */ 617 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid); 618 break; 619 case IEEE80211_M_HOSTAP: 620 #ifndef IEEE80211_NO_HOSTAP 621 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 622 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 623 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 624 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 625 #endif /* !IEEE80211_NO_HOSTAP */ 626 break; 627 case IEEE80211_M_MONITOR: 628 goto bad; 629 } 630 if (m->m_flags & M_MORE_DATA) 631 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 632 if (addqos) { 633 struct ieee80211_qosframe *qwh = 634 (struct ieee80211_qosframe *) wh; 635 int ac, tid; 636 637 ac = M_WME_GETAC(m); 638 /* map from access class/queue to 11e header priorty value */ 639 tid = WME_AC_TO_TID(ac); 640 qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 641 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 642 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S; 643 qwh->i_qos[1] = 0; 644 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 645 646 *(u_int16_t *)wh->i_seq = 647 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 648 ni->ni_txseqs[tid]++; 649 } else { 650 *(u_int16_t *)wh->i_seq = 651 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 652 ni->ni_txseqs[0]++; 653 } 654 /* check if xmit fragmentation is required */ 655 txfrag = (m->m_pkthdr.len > ic->ic_fragthreshold && 656 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 657 (m->m_flags & M_FF) == 0); /* NB: don't fragment ff's */ 658 if (key != NULL) { 659 /* 660 * IEEE 802.1X: send EAPOL frames always in the clear. 661 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 662 */ 663 if (eh.ether_type != htons(ETHERTYPE_PAE) || 664 ((ic->ic_flags & IEEE80211_F_WPA) && 665 (ic->ic_opmode == IEEE80211_M_STA ? 666 !IEEE80211_KEY_UNDEFINED(*key) : 667 !IEEE80211_KEY_UNDEFINED(ni->ni_ucastkey)))) { 668 wh->i_fc[1] |= IEEE80211_FC1_WEP; 669 if (!ieee80211_crypto_enmic(ic, key, m, txfrag)) { 670 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 671 "[%s] enmic failed, discard frame\n", 672 ether_sprintf(eh.ether_dhost)); 673 ic->ic_stats.is_crypto_enmicfail++; 674 goto bad; 675 } 676 } 677 } 678 if (txfrag && !ieee80211_fragment(ic, m, hdrsize, 679 key != NULL ? key->wk_cipher->ic_header : 0, ic->ic_fragthreshold)) 680 goto bad; 681 682 IEEE80211_NODE_STAT(ni, tx_data); 683 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 684 685 return m; 686 bad: 687 if (m != NULL) 688 m_freem(m); 689 return NULL; 690 } 691 692 /* 693 * Arguments in: 694 * 695 * paylen: payload length (no FCS, no WEP header) 696 * 697 * hdrlen: header length 698 * 699 * rate: MSDU speed, units 500kb/s 700 * 701 * flags: IEEE80211_F_SHPREAMBLE (use short preamble), 702 * IEEE80211_F_SHSLOT (use short slot length) 703 * 704 * Arguments out: 705 * 706 * d: 802.11 Duration field for RTS, 707 * 802.11 Duration field for data frame, 708 * PLCP Length for data frame, 709 * residual octets at end of data slot 710 */ 711 static int 712 ieee80211_compute_duration1(int len, int use_ack, uint32_t icflags, int rate, 713 struct ieee80211_duration *d) 714 { 715 int pre, ctsrate; 716 int ack, bitlen, data_dur, remainder; 717 718 /* RTS reserves medium for SIFS | CTS | SIFS | (DATA) | SIFS | ACK 719 * DATA reserves medium for SIFS | ACK 720 * 721 * XXXMYC: no ACK on multicast/broadcast or control packets 722 */ 723 724 bitlen = len * 8; 725 726 pre = IEEE80211_DUR_DS_SIFS; 727 if ((icflags & IEEE80211_F_SHPREAMBLE) != 0) 728 pre += IEEE80211_DUR_DS_SHORT_PREAMBLE + IEEE80211_DUR_DS_FAST_PLCPHDR; 729 else 730 pre += IEEE80211_DUR_DS_LONG_PREAMBLE + IEEE80211_DUR_DS_SLOW_PLCPHDR; 731 732 d->d_residue = 0; 733 data_dur = (bitlen * 2) / rate; 734 remainder = (bitlen * 2) % rate; 735 if (remainder != 0) { 736 d->d_residue = (rate - remainder) / 16; 737 data_dur++; 738 } 739 740 switch (rate) { 741 case 2: /* 1 Mb/s */ 742 case 4: /* 2 Mb/s */ 743 /* 1 - 2 Mb/s WLAN: send ACK/CTS at 1 Mb/s */ 744 ctsrate = 2; 745 break; 746 case 11: /* 5.5 Mb/s */ 747 case 22: /* 11 Mb/s */ 748 case 44: /* 22 Mb/s */ 749 /* 5.5 - 11 Mb/s WLAN: send ACK/CTS at 2 Mb/s */ 750 ctsrate = 4; 751 break; 752 default: 753 /* TBD */ 754 return -1; 755 } 756 757 d->d_plcp_len = data_dur; 758 759 ack = (use_ack) ? pre + (IEEE80211_DUR_DS_SLOW_ACK * 2) / ctsrate : 0; 760 761 d->d_rts_dur = 762 pre + (IEEE80211_DUR_DS_SLOW_CTS * 2) / ctsrate + 763 pre + data_dur + 764 ack; 765 766 d->d_data_dur = ack; 767 768 return 0; 769 } 770 771 /* 772 * Arguments in: 773 * 774 * wh: 802.11 header 775 * 776 * paylen: payload length (no FCS, no WEP header) 777 * 778 * rate: MSDU speed, units 500kb/s 779 * 780 * fraglen: fragment length, set to maximum (or higher) for no 781 * fragmentation 782 * 783 * flags: IEEE80211_F_PRIVACY (hardware adds WEP), 784 * IEEE80211_F_SHPREAMBLE (use short preamble), 785 * IEEE80211_F_SHSLOT (use short slot length) 786 * 787 * Arguments out: 788 * 789 * d0: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields 790 * of first/only fragment 791 * 792 * dn: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields 793 * of last fragment 794 * 795 * ieee80211_compute_duration assumes crypto-encapsulation, if any, 796 * has already taken place. 797 */ 798 int 799 ieee80211_compute_duration(const struct ieee80211_frame_min *wh, 800 const struct ieee80211_key *wk, int len, 801 uint32_t icflags, int fraglen, int rate, struct ieee80211_duration *d0, 802 struct ieee80211_duration *dn, int *npktp, int debug) 803 { 804 int ack, rc; 805 int cryptolen, /* crypto overhead: header+trailer */ 806 firstlen, /* first fragment's payload + overhead length */ 807 hdrlen, /* header length w/o driver padding */ 808 lastlen, /* last fragment's payload length w/ overhead */ 809 lastlen0, /* last fragment's payload length w/o overhead */ 810 npkt, /* number of fragments */ 811 overlen, /* non-802.11 header overhead per fragment */ 812 paylen; /* payload length w/o overhead */ 813 814 hdrlen = ieee80211_anyhdrsize((const void *)wh); 815 816 /* Account for padding required by the driver. */ 817 if (icflags & IEEE80211_F_DATAPAD) 818 paylen = len - roundup(hdrlen, sizeof(u_int32_t)); 819 else 820 paylen = len - hdrlen; 821 822 overlen = IEEE80211_CRC_LEN; 823 824 if (wk != NULL) { 825 cryptolen = wk->wk_cipher->ic_header + 826 wk->wk_cipher->ic_trailer; 827 paylen -= cryptolen; 828 overlen += cryptolen; 829 } 830 831 npkt = paylen / fraglen; 832 lastlen0 = paylen % fraglen; 833 834 if (npkt == 0) /* no fragments */ 835 lastlen = paylen + overlen; 836 else if (lastlen0 != 0) { /* a short "tail" fragment */ 837 lastlen = lastlen0 + overlen; 838 npkt++; 839 } else /* full-length "tail" fragment */ 840 lastlen = fraglen + overlen; 841 842 if (npktp != NULL) 843 *npktp = npkt; 844 845 if (npkt > 1) 846 firstlen = fraglen + overlen; 847 else 848 firstlen = paylen + overlen; 849 850 if (debug) { 851 printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d " 852 "fraglen %d overlen %d len %d rate %d icflags %08x\n", 853 __func__, npkt, firstlen, lastlen0, lastlen, fraglen, 854 overlen, len, rate, icflags); 855 } 856 857 ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) && 858 (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL; 859 860 rc = ieee80211_compute_duration1(firstlen + hdrlen, 861 ack, icflags, rate, d0); 862 if (rc == -1) 863 return rc; 864 865 if (npkt <= 1) { 866 *dn = *d0; 867 return 0; 868 } 869 return ieee80211_compute_duration1(lastlen + hdrlen, ack, icflags, rate, 870 dn); 871 } 872 873 /* 874 * Fragment the frame according to the specified mtu. 875 * The size of the 802.11 header (w/o padding) is provided 876 * so we don't need to recalculate it. We create a new 877 * mbuf for each fragment and chain it through m_nextpkt; 878 * we might be able to optimize this by reusing the original 879 * packet's mbufs but that is significantly more complicated. 880 */ 881 static int 882 ieee80211_fragment(struct ieee80211com *ic, struct mbuf *m0, 883 u_int hdrsize, u_int ciphdrsize, u_int mtu) 884 { 885 struct ieee80211_frame *wh, *whf; 886 struct mbuf *m, *prev, *next; 887 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 888 889 IASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 890 IASSERT(m0->m_pkthdr.len > mtu, 891 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 892 893 wh = mtod(m0, struct ieee80211_frame *); 894 /* NB: mark the first frag; it will be propagated below */ 895 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 896 totalhdrsize = hdrsize + ciphdrsize; 897 fragno = 1; 898 off = mtu - ciphdrsize; 899 remainder = m0->m_pkthdr.len - off; 900 prev = m0; 901 do { 902 fragsize = totalhdrsize + remainder; 903 if (fragsize > mtu) 904 fragsize = mtu; 905 IASSERT(fragsize < MCLBYTES, 906 ("fragment size %u too big!", fragsize)); 907 if (fragsize > MHLEN) 908 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 909 else 910 m = m_gethdr(M_DONTWAIT, MT_DATA); 911 if (m == NULL) 912 goto bad; 913 /* leave room to prepend any cipher header */ 914 m_align(m, fragsize - ciphdrsize); 915 916 /* 917 * Form the header in the fragment. Note that since 918 * we mark the first fragment with the MORE_FRAG bit 919 * it automatically is propagated to each fragment; we 920 * need only clear it on the last fragment (done below). 921 */ 922 whf = mtod(m, struct ieee80211_frame *); 923 memcpy(whf, wh, hdrsize); 924 *(u_int16_t *)&whf->i_seq[0] |= htole16( 925 (fragno & IEEE80211_SEQ_FRAG_MASK) << 926 IEEE80211_SEQ_FRAG_SHIFT); 927 fragno++; 928 929 payload = fragsize - totalhdrsize; 930 /* NB: destination is known to be contiguous */ 931 m_copydata(m0, off, payload, mtod(m, u_int8_t *) + hdrsize); 932 m->m_len = hdrsize + payload; 933 m->m_pkthdr.len = hdrsize + payload; 934 m->m_flags |= M_FRAG; 935 936 /* chain up the fragment */ 937 prev->m_nextpkt = m; 938 prev = m; 939 940 /* deduct fragment just formed */ 941 remainder -= payload; 942 off += payload; 943 } while (remainder != 0); 944 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 945 946 /* strip first mbuf now that everything has been copied */ 947 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 948 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 949 950 ic->ic_stats.is_tx_fragframes++; 951 ic->ic_stats.is_tx_frags += fragno-1; 952 953 return 1; 954 bad: 955 /* reclaim fragments but leave original frame for caller to free */ 956 for (m = m0->m_nextpkt; m != NULL; m = next) { 957 next = m->m_nextpkt; 958 m->m_nextpkt = NULL; /* XXX paranoid */ 959 m_freem(m); 960 } 961 m0->m_nextpkt = NULL; 962 return 0; 963 } 964 965 /* 966 * Add a supported rates element id to a frame. 967 */ 968 static u_int8_t * 969 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs) 970 { 971 int nrates; 972 973 *frm++ = IEEE80211_ELEMID_RATES; 974 nrates = rs->rs_nrates; 975 if (nrates > IEEE80211_RATE_SIZE) 976 nrates = IEEE80211_RATE_SIZE; 977 *frm++ = nrates; 978 memcpy(frm, rs->rs_rates, nrates); 979 return frm + nrates; 980 } 981 982 /* 983 * Add an extended supported rates element id to a frame. 984 */ 985 static u_int8_t * 986 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs) 987 { 988 /* 989 * Add an extended supported rates element if operating in 11g mode. 990 */ 991 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 992 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 993 *frm++ = IEEE80211_ELEMID_XRATES; 994 *frm++ = nrates; 995 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 996 frm += nrates; 997 } 998 return frm; 999 } 1000 1001 /* 1002 * Add an ssid elemet to a frame. 1003 */ 1004 static u_int8_t * 1005 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len) 1006 { 1007 *frm++ = IEEE80211_ELEMID_SSID; 1008 *frm++ = len; 1009 memcpy(frm, ssid, len); 1010 return frm + len; 1011 } 1012 1013 /* 1014 * Add an erp element to a frame. 1015 */ 1016 static u_int8_t * 1017 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic) 1018 { 1019 u_int8_t erp; 1020 1021 *frm++ = IEEE80211_ELEMID_ERP; 1022 *frm++ = 1; 1023 erp = 0; 1024 if (ic->ic_nonerpsta != 0) 1025 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 1026 if (ic->ic_flags & IEEE80211_F_USEPROT) 1027 erp |= IEEE80211_ERP_USE_PROTECTION; 1028 if (ic->ic_flags & IEEE80211_F_USEBARKER) 1029 erp |= IEEE80211_ERP_LONG_PREAMBLE; 1030 *frm++ = erp; 1031 return frm; 1032 } 1033 1034 static u_int8_t * 1035 ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie) 1036 { 1037 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2 1038 #define ADDSHORT(frm, v) do { \ 1039 frm[0] = (v) & 0xff; \ 1040 frm[1] = (v) >> 8; \ 1041 frm += 2; \ 1042 } while (0) 1043 #define ADDSELECTOR(frm, sel) do { \ 1044 memcpy(frm, sel, 4); \ 1045 frm += 4; \ 1046 } while (0) 1047 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE }; 1048 static const u_int8_t cipher_suite[][4] = { 1049 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */ 1050 { WPA_OUI_BYTES, WPA_CSE_TKIP }, 1051 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */ 1052 { WPA_OUI_BYTES, WPA_CSE_CCMP }, 1053 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 1054 { WPA_OUI_BYTES, WPA_CSE_NULL }, 1055 }; 1056 static const u_int8_t wep104_suite[4] = 1057 { WPA_OUI_BYTES, WPA_CSE_WEP104 }; 1058 static const u_int8_t key_mgt_unspec[4] = 1059 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC }; 1060 static const u_int8_t key_mgt_psk[4] = 1061 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK }; 1062 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 1063 u_int8_t *frm = ie; 1064 u_int8_t *selcnt; 1065 1066 *frm++ = IEEE80211_ELEMID_VENDOR; 1067 *frm++ = 0; /* length filled in below */ 1068 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */ 1069 frm += sizeof(oui); 1070 ADDSHORT(frm, WPA_VERSION); 1071 1072 /* XXX filter out CKIP */ 1073 1074 /* multicast cipher */ 1075 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 1076 rsn->rsn_mcastkeylen >= 13) 1077 ADDSELECTOR(frm, wep104_suite); 1078 else 1079 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 1080 1081 /* unicast cipher list */ 1082 selcnt = frm; 1083 ADDSHORT(frm, 0); /* selector count */ 1084 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 1085 selcnt[0]++; 1086 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 1087 } 1088 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 1089 selcnt[0]++; 1090 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 1091 } 1092 1093 /* authenticator selector list */ 1094 selcnt = frm; 1095 ADDSHORT(frm, 0); /* selector count */ 1096 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 1097 selcnt[0]++; 1098 ADDSELECTOR(frm, key_mgt_unspec); 1099 } 1100 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 1101 selcnt[0]++; 1102 ADDSELECTOR(frm, key_mgt_psk); 1103 } 1104 1105 /* optional capabilities */ 1106 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH) 1107 ADDSHORT(frm, rsn->rsn_caps); 1108 1109 /* calculate element length */ 1110 ie[1] = frm - ie - 2; 1111 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 1112 ("WPA IE too big, %u > %zu", 1113 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 1114 return frm; 1115 #undef ADDSHORT 1116 #undef ADDSELECTOR 1117 #undef WPA_OUI_BYTES 1118 } 1119 1120 static u_int8_t * 1121 ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie) 1122 { 1123 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac 1124 #define ADDSHORT(frm, v) do { \ 1125 frm[0] = (v) & 0xff; \ 1126 frm[1] = (v) >> 8; \ 1127 frm += 2; \ 1128 } while (0) 1129 #define ADDSELECTOR(frm, sel) do { \ 1130 memcpy(frm, sel, 4); \ 1131 frm += 4; \ 1132 } while (0) 1133 static const u_int8_t cipher_suite[][4] = { 1134 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */ 1135 { RSN_OUI_BYTES, RSN_CSE_TKIP }, 1136 { RSN_OUI_BYTES, RSN_CSE_WRAP }, 1137 { RSN_OUI_BYTES, RSN_CSE_CCMP }, 1138 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 1139 { RSN_OUI_BYTES, RSN_CSE_NULL }, 1140 }; 1141 static const u_int8_t wep104_suite[4] = 1142 { RSN_OUI_BYTES, RSN_CSE_WEP104 }; 1143 static const u_int8_t key_mgt_unspec[4] = 1144 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC }; 1145 static const u_int8_t key_mgt_psk[4] = 1146 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK }; 1147 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 1148 u_int8_t *frm = ie; 1149 u_int8_t *selcnt; 1150 1151 *frm++ = IEEE80211_ELEMID_RSN; 1152 *frm++ = 0; /* length filled in below */ 1153 ADDSHORT(frm, RSN_VERSION); 1154 1155 /* XXX filter out CKIP */ 1156 1157 /* multicast cipher */ 1158 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 1159 rsn->rsn_mcastkeylen >= 13) 1160 ADDSELECTOR(frm, wep104_suite); 1161 else 1162 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 1163 1164 /* unicast cipher list */ 1165 selcnt = frm; 1166 ADDSHORT(frm, 0); /* selector count */ 1167 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 1168 selcnt[0]++; 1169 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 1170 } 1171 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 1172 selcnt[0]++; 1173 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 1174 } 1175 1176 /* authenticator selector list */ 1177 selcnt = frm; 1178 ADDSHORT(frm, 0); /* selector count */ 1179 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 1180 selcnt[0]++; 1181 ADDSELECTOR(frm, key_mgt_unspec); 1182 } 1183 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 1184 selcnt[0]++; 1185 ADDSELECTOR(frm, key_mgt_psk); 1186 } 1187 1188 /* optional capabilities */ 1189 ADDSHORT(frm, rsn->rsn_caps); 1190 /* XXX PMKID */ 1191 1192 /* calculate element length */ 1193 ie[1] = frm - ie - 2; 1194 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 1195 ("RSN IE too big, %u > %zu", 1196 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 1197 return frm; 1198 #undef ADDSELECTOR 1199 #undef ADDSHORT 1200 #undef RSN_OUI_BYTES 1201 } 1202 1203 /* 1204 * Add a WPA/RSN element to a frame. 1205 */ 1206 static u_int8_t * 1207 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic) 1208 { 1209 1210 IASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!")); 1211 if (ic->ic_flags & IEEE80211_F_WPA2) 1212 frm = ieee80211_setup_rsn_ie(ic, frm); 1213 if (ic->ic_flags & IEEE80211_F_WPA1) 1214 frm = ieee80211_setup_wpa_ie(ic, frm); 1215 return frm; 1216 } 1217 1218 #define WME_OUI_BYTES 0x00, 0x50, 0xf2 1219 /* 1220 * Add a WME information element to a frame. 1221 */ 1222 static u_int8_t * 1223 ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme) 1224 { 1225 static const struct ieee80211_wme_info info = { 1226 .wme_id = IEEE80211_ELEMID_VENDOR, 1227 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1228 .wme_oui = { WME_OUI_BYTES }, 1229 .wme_type = WME_OUI_TYPE, 1230 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1231 .wme_version = WME_VERSION, 1232 .wme_info = 0, 1233 }; 1234 memcpy(frm, &info, sizeof(info)); 1235 return frm + sizeof(info); 1236 } 1237 1238 /* 1239 * Add a WME parameters element to a frame. 1240 */ 1241 static u_int8_t * 1242 ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme) 1243 { 1244 #define SM(_v, _f) (((_v) << _f##_S) & _f) 1245 #define ADDSHORT(frm, v) do { \ 1246 frm[0] = (v) & 0xff; \ 1247 frm[1] = (v) >> 8; \ 1248 frm += 2; \ 1249 } while (0) 1250 /* NB: this works 'cuz a param has an info at the front */ 1251 static const struct ieee80211_wme_info param = { 1252 .wme_id = IEEE80211_ELEMID_VENDOR, 1253 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1254 .wme_oui = { WME_OUI_BYTES }, 1255 .wme_type = WME_OUI_TYPE, 1256 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1257 .wme_version = WME_VERSION, 1258 }; 1259 int i; 1260 1261 memcpy(frm, ¶m, sizeof(param)); 1262 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1263 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1264 *frm++ = 0; /* reserved field */ 1265 for (i = 0; i < WME_NUM_AC; i++) { 1266 const struct wmeParams *ac = 1267 &wme->wme_bssChanParams.cap_wmeParams[i]; 1268 *frm++ = SM(i, WME_PARAM_ACI) 1269 | SM(ac->wmep_acm, WME_PARAM_ACM) 1270 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1271 ; 1272 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1273 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1274 ; 1275 ADDSHORT(frm, ac->wmep_txopLimit); 1276 } 1277 return frm; 1278 #undef SM 1279 #undef ADDSHORT 1280 } 1281 #undef WME_OUI_BYTES 1282 1283 /* 1284 * Send a probe request frame with the specified ssid 1285 * and any optional information element data. 1286 */ 1287 int 1288 ieee80211_send_probereq(struct ieee80211_node *ni, 1289 const u_int8_t sa[IEEE80211_ADDR_LEN], 1290 const u_int8_t da[IEEE80211_ADDR_LEN], 1291 const u_int8_t bssid[IEEE80211_ADDR_LEN], 1292 const u_int8_t *ssid, size_t ssidlen, 1293 const void *optie, size_t optielen) 1294 { 1295 struct ieee80211com *ic = ni->ni_ic; 1296 enum ieee80211_phymode mode; 1297 struct ieee80211_frame *wh; 1298 struct mbuf *m; 1299 u_int8_t *frm; 1300 1301 /* 1302 * Hold a reference on the node so it doesn't go away until after 1303 * the xmit is complete all the way in the driver. On error we 1304 * will remove our reference. 1305 */ 1306 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1307 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1308 __func__, __LINE__, 1309 ni, ether_sprintf(ni->ni_macaddr), 1310 ieee80211_node_refcnt(ni)+1); 1311 ieee80211_ref_node(ni); 1312 1313 /* 1314 * prreq frame format 1315 * [tlv] ssid 1316 * [tlv] supported rates 1317 * [tlv] extended supported rates 1318 * [tlv] user-specified ie's 1319 */ 1320 m = ieee80211_getmgtframe(&frm, 1321 2 + IEEE80211_NWID_LEN 1322 + 2 + IEEE80211_RATE_SIZE 1323 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1324 + (optie != NULL ? optielen : 0) 1325 ); 1326 if (m == NULL) { 1327 ic->ic_stats.is_tx_nobuf++; 1328 ieee80211_free_node(ni); 1329 return ENOMEM; 1330 } 1331 1332 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1333 mode = ieee80211_chan2mode(ic, ic->ic_curchan); 1334 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]); 1335 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]); 1336 1337 if (optie != NULL) { 1338 memcpy(frm, optie, optielen); 1339 frm += optielen; 1340 } 1341 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1342 1343 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1344 if (m == NULL) 1345 return ENOMEM; 1346 IASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 1347 m->m_pkthdr.rcvif = (void *)ni; 1348 1349 wh = mtod(m, struct ieee80211_frame *); 1350 ieee80211_send_setup(ic, ni, wh, 1351 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1352 sa, da, bssid); 1353 /* XXX power management? */ 1354 1355 IEEE80211_NODE_STAT(ni, tx_probereq); 1356 IEEE80211_NODE_STAT(ni, tx_mgmt); 1357 1358 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1359 "[%s] send probe req on channel %u\n", 1360 ether_sprintf(wh->i_addr1), 1361 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1362 1363 IF_ENQUEUE(&ic->ic_mgtq, m); 1364 (*ic->ic_ifp->if_start)(ic->ic_ifp); 1365 return 0; 1366 } 1367 1368 /* 1369 * Send a management frame. The node is for the destination (or ic_bss 1370 * when in station mode). Nodes other than ic_bss have their reference 1371 * count bumped to reflect our use for an indeterminant time. 1372 */ 1373 int 1374 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 1375 int type, int arg) 1376 { 1377 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 1378 struct mbuf *m; 1379 u_int8_t *frm; 1380 u_int16_t capinfo; 1381 int has_challenge, is_shared_key, ret, timer, status; 1382 1383 IASSERT(ni != NULL, ("null node")); 1384 1385 /* 1386 * Hold a reference on the node so it doesn't go away until after 1387 * the xmit is complete all the way in the driver. On error we 1388 * will remove our reference. 1389 */ 1390 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1391 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1392 __func__, __LINE__, 1393 ni, ether_sprintf(ni->ni_macaddr), 1394 ieee80211_node_refcnt(ni)+1); 1395 ieee80211_ref_node(ni); 1396 1397 timer = 0; 1398 switch (type) { 1399 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1400 /* 1401 * probe response frame format 1402 * [8] time stamp 1403 * [2] beacon interval 1404 * [2] cabability information 1405 * [tlv] ssid 1406 * [tlv] supported rates 1407 * [tlv] parameter set (FH/DS) 1408 * [tlv] parameter set (IBSS) 1409 * [tlv] extended rate phy (ERP) 1410 * [tlv] extended supported rates 1411 * [tlv] WPA 1412 * [tlv] WME (optional) 1413 */ 1414 m = ieee80211_getmgtframe(&frm, 1415 8 1416 + sizeof(u_int16_t) 1417 + sizeof(u_int16_t) 1418 + 2 + IEEE80211_NWID_LEN 1419 + 2 + IEEE80211_RATE_SIZE 1420 + 7 /* max(7,3) */ 1421 + 6 1422 + 3 1423 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1424 /* XXX !WPA1+WPA2 fits w/o a cluster */ 1425 + (ic->ic_flags & IEEE80211_F_WPA ? 1426 2*sizeof(struct ieee80211_ie_wpa) : 0) 1427 + sizeof(struct ieee80211_wme_param) 1428 ); 1429 if (m == NULL) 1430 senderr(ENOMEM, is_tx_nobuf); 1431 1432 memset(frm, 0, 8); /* timestamp should be filled later */ 1433 frm += 8; 1434 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval); 1435 frm += 2; 1436 if (ic->ic_opmode == IEEE80211_M_IBSS) 1437 capinfo = IEEE80211_CAPINFO_IBSS; 1438 else 1439 capinfo = IEEE80211_CAPINFO_ESS; 1440 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1441 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1442 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1443 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1444 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1445 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1446 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1447 *(u_int16_t *)frm = htole16(capinfo); 1448 frm += 2; 1449 1450 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1451 ic->ic_bss->ni_esslen); 1452 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1453 1454 if (ic->ic_phytype == IEEE80211_T_FH) { 1455 *frm++ = IEEE80211_ELEMID_FHPARMS; 1456 *frm++ = 5; 1457 *frm++ = ni->ni_fhdwell & 0x00ff; 1458 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff; 1459 *frm++ = IEEE80211_FH_CHANSET( 1460 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1461 *frm++ = IEEE80211_FH_CHANPAT( 1462 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1463 *frm++ = ni->ni_fhindex; 1464 } else { 1465 *frm++ = IEEE80211_ELEMID_DSPARMS; 1466 *frm++ = 1; 1467 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan); 1468 } 1469 1470 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1471 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1472 *frm++ = 2; 1473 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1474 } 1475 if (ic->ic_flags & IEEE80211_F_WPA) 1476 frm = ieee80211_add_wpa(frm, ic); 1477 if (ic->ic_curmode == IEEE80211_MODE_11G) 1478 frm = ieee80211_add_erp(frm, ic); 1479 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1480 if (ic->ic_flags & IEEE80211_F_WME) 1481 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1482 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1483 break; 1484 1485 case IEEE80211_FC0_SUBTYPE_AUTH: 1486 status = arg >> 16; 1487 arg &= 0xffff; 1488 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1489 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1490 ni->ni_challenge != NULL); 1491 1492 /* 1493 * Deduce whether we're doing open authentication or 1494 * shared key authentication. We do the latter if 1495 * we're in the middle of a shared key authentication 1496 * handshake or if we're initiating an authentication 1497 * request and configured to use shared key. 1498 */ 1499 is_shared_key = has_challenge || 1500 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1501 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1502 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED); 1503 1504 m = ieee80211_getmgtframe(&frm, 1505 3 * sizeof(u_int16_t) 1506 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1507 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0) 1508 ); 1509 if (m == NULL) 1510 senderr(ENOMEM, is_tx_nobuf); 1511 1512 ((u_int16_t *)frm)[0] = 1513 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1514 : htole16(IEEE80211_AUTH_ALG_OPEN); 1515 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */ 1516 ((u_int16_t *)frm)[2] = htole16(status);/* status */ 1517 1518 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1519 ((u_int16_t *)frm)[3] = 1520 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1521 IEEE80211_ELEMID_CHALLENGE); 1522 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge, 1523 IEEE80211_CHALLENGE_LEN); 1524 m->m_pkthdr.len = m->m_len = 1525 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN; 1526 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1527 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1528 "[%s] request encrypt frame (%s)\n", 1529 ether_sprintf(ni->ni_macaddr), __func__); 1530 m->m_flags |= M_LINK0; /* WEP-encrypt, please */ 1531 } 1532 } else 1533 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t); 1534 1535 /* XXX not right for shared key */ 1536 if (status == IEEE80211_STATUS_SUCCESS) 1537 IEEE80211_NODE_STAT(ni, tx_auth); 1538 else 1539 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1540 1541 if (ic->ic_opmode == IEEE80211_M_STA) 1542 timer = IEEE80211_TRANS_WAIT; 1543 break; 1544 1545 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1546 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1547 "[%s] send station deauthenticate (reason %d)\n", 1548 ether_sprintf(ni->ni_macaddr), arg); 1549 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1550 if (m == NULL) 1551 senderr(ENOMEM, is_tx_nobuf); 1552 *(u_int16_t *)frm = htole16(arg); /* reason */ 1553 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1554 1555 IEEE80211_NODE_STAT(ni, tx_deauth); 1556 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1557 1558 ieee80211_node_unauthorize(ni); /* port closed */ 1559 break; 1560 1561 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1562 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1563 /* 1564 * asreq frame format 1565 * [2] capability information 1566 * [2] listen interval 1567 * [6*] current AP address (reassoc only) 1568 * [tlv] ssid 1569 * [tlv] supported rates 1570 * [tlv] extended supported rates 1571 * [tlv] WME 1572 * [tlv] user-specified ie's 1573 */ 1574 m = ieee80211_getmgtframe(&frm, 1575 sizeof(u_int16_t) 1576 + sizeof(u_int16_t) 1577 + IEEE80211_ADDR_LEN 1578 + 2 + IEEE80211_NWID_LEN 1579 + 2 + IEEE80211_RATE_SIZE 1580 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1581 + sizeof(struct ieee80211_wme_info) 1582 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 1583 ); 1584 if (m == NULL) 1585 senderr(ENOMEM, is_tx_nobuf); 1586 1587 capinfo = 0; 1588 if (ic->ic_opmode == IEEE80211_M_IBSS) 1589 capinfo |= IEEE80211_CAPINFO_IBSS; 1590 else /* IEEE80211_M_STA */ 1591 capinfo |= IEEE80211_CAPINFO_ESS; 1592 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1593 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1594 /* 1595 * NB: Some 11a AP's reject the request when 1596 * short premable is set. 1597 */ 1598 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1599 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1600 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1601 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) && 1602 (ic->ic_caps & IEEE80211_C_SHSLOT)) 1603 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1604 *(u_int16_t *)frm = htole16(capinfo); 1605 frm += 2; 1606 1607 *(u_int16_t *)frm = htole16(ic->ic_lintval); 1608 frm += 2; 1609 1610 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1611 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1612 frm += IEEE80211_ADDR_LEN; 1613 } 1614 1615 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1616 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1617 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1618 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1619 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1620 if (ic->ic_opt_ie != NULL) { 1621 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 1622 frm += ic->ic_opt_ie_len; 1623 } 1624 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1625 1626 timer = IEEE80211_TRANS_WAIT; 1627 break; 1628 1629 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1630 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1631 /* 1632 * asreq frame format 1633 * [2] capability information 1634 * [2] status 1635 * [2] association ID 1636 * [tlv] supported rates 1637 * [tlv] extended supported rates 1638 * [tlv] WME (if enabled and STA enabled) 1639 */ 1640 m = ieee80211_getmgtframe(&frm, 1641 sizeof(u_int16_t) 1642 + sizeof(u_int16_t) 1643 + sizeof(u_int16_t) 1644 + 2 + IEEE80211_RATE_SIZE 1645 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1646 + sizeof(struct ieee80211_wme_param) 1647 ); 1648 if (m == NULL) 1649 senderr(ENOMEM, is_tx_nobuf); 1650 1651 capinfo = IEEE80211_CAPINFO_ESS; 1652 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1653 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1654 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1655 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1656 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1657 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1658 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1659 *(u_int16_t *)frm = htole16(capinfo); 1660 frm += 2; 1661 1662 *(u_int16_t *)frm = htole16(arg); /* status */ 1663 frm += 2; 1664 1665 if (arg == IEEE80211_STATUS_SUCCESS) { 1666 *(u_int16_t *)frm = htole16(ni->ni_associd); 1667 IEEE80211_NODE_STAT(ni, tx_assoc); 1668 } else 1669 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1670 frm += 2; 1671 1672 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1673 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1674 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1675 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1676 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1677 break; 1678 1679 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1680 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1681 "[%s] send station disassociate (reason %d)\n", 1682 ether_sprintf(ni->ni_macaddr), arg); 1683 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1684 if (m == NULL) 1685 senderr(ENOMEM, is_tx_nobuf); 1686 *(u_int16_t *)frm = htole16(arg); /* reason */ 1687 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1688 1689 IEEE80211_NODE_STAT(ni, tx_disassoc); 1690 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 1691 break; 1692 1693 default: 1694 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1695 "[%s] invalid mgmt frame type %u\n", 1696 ether_sprintf(ni->ni_macaddr), type); 1697 senderr(EINVAL, is_tx_unknownmgt); 1698 /* NOTREACHED */ 1699 } 1700 ret = ieee80211_mgmt_output(ic, ni, m, type, timer); 1701 if (ret != 0) { 1702 bad: 1703 ieee80211_free_node(ni); 1704 } 1705 return ret; 1706 #undef senderr 1707 } 1708 1709 /* 1710 * Allocate a beacon frame and fillin the appropriate bits. 1711 */ 1712 struct mbuf * 1713 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni, 1714 struct ieee80211_beacon_offsets *bo) 1715 { 1716 struct ifnet *ifp = ic->ic_ifp; 1717 struct ieee80211_frame *wh; 1718 struct mbuf *m; 1719 int pktlen; 1720 u_int8_t *frm, *efrm; 1721 u_int16_t capinfo; 1722 struct ieee80211_rateset *rs; 1723 1724 /* 1725 * beacon frame format 1726 * [8] time stamp 1727 * [2] beacon interval 1728 * [2] cabability information 1729 * [tlv] ssid 1730 * [tlv] supported rates 1731 * [3] parameter set (DS) 1732 * [tlv] parameter set (IBSS/TIM) 1733 * [tlv] extended rate phy (ERP) 1734 * [tlv] extended supported rates 1735 * [tlv] WME parameters 1736 * [tlv] WPA/RSN parameters 1737 * XXX Vendor-specific OIDs (e.g. Atheros) 1738 * NB: we allocate the max space required for the TIM bitmap. 1739 */ 1740 rs = &ni->ni_rates; 1741 pktlen = 8 /* time stamp */ 1742 + sizeof(u_int16_t) /* beacon interval */ 1743 + sizeof(u_int16_t) /* capabilities */ 1744 + 2 + ni->ni_esslen /* ssid */ 1745 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 1746 + 2 + 1 /* DS parameters */ 1747 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */ 1748 + 2 + 1 /* ERP */ 1749 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1750 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */ 1751 sizeof(struct ieee80211_wme_param) : 0) 1752 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 1753 2*sizeof(struct ieee80211_ie_wpa) : 0) 1754 ; 1755 m = ieee80211_getmgtframe(&frm, pktlen); 1756 if (m == NULL) { 1757 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1758 "%s: cannot get buf; size %u\n", __func__, pktlen); 1759 ic->ic_stats.is_tx_nobuf++; 1760 return NULL; 1761 } 1762 1763 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 1764 frm += 8; 1765 *(u_int16_t *)frm = htole16(ni->ni_intval); 1766 frm += 2; 1767 if (ic->ic_opmode == IEEE80211_M_IBSS) 1768 capinfo = IEEE80211_CAPINFO_IBSS; 1769 else 1770 capinfo = IEEE80211_CAPINFO_ESS; 1771 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1772 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1773 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1774 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1775 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1776 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1777 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1778 bo->bo_caps = (u_int16_t *)frm; 1779 *(u_int16_t *)frm = htole16(capinfo); 1780 frm += 2; 1781 *frm++ = IEEE80211_ELEMID_SSID; 1782 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) { 1783 *frm++ = ni->ni_esslen; 1784 memcpy(frm, ni->ni_essid, ni->ni_esslen); 1785 frm += ni->ni_esslen; 1786 } else 1787 *frm++ = 0; 1788 frm = ieee80211_add_rates(frm, rs); 1789 if (ic->ic_curmode != IEEE80211_MODE_FH) { 1790 *frm++ = IEEE80211_ELEMID_DSPARMS; 1791 *frm++ = 1; 1792 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 1793 } 1794 bo->bo_tim = frm; 1795 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1796 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1797 *frm++ = 2; 1798 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1799 bo->bo_tim_len = 0; 1800 } else { 1801 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 1802 1803 tie->tim_ie = IEEE80211_ELEMID_TIM; 1804 tie->tim_len = 4; /* length */ 1805 tie->tim_count = 0; /* DTIM count */ 1806 tie->tim_period = ic->ic_dtim_period; /* DTIM period */ 1807 tie->tim_bitctl = 0; /* bitmap control */ 1808 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 1809 frm += sizeof(struct ieee80211_tim_ie); 1810 bo->bo_tim_len = 1; 1811 } 1812 bo->bo_trailer = frm; 1813 if (ic->ic_flags & IEEE80211_F_WME) { 1814 bo->bo_wme = frm; 1815 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1816 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1817 } 1818 if (ic->ic_flags & IEEE80211_F_WPA) 1819 frm = ieee80211_add_wpa(frm, ic); 1820 if (ic->ic_curmode == IEEE80211_MODE_11G) 1821 frm = ieee80211_add_erp(frm, ic); 1822 efrm = ieee80211_add_xrates(frm, rs); 1823 bo->bo_trailer_len = efrm - bo->bo_trailer; 1824 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *); 1825 1826 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1827 IASSERT(m != NULL, ("no space for 802.11 header?")); 1828 wh = mtod(m, struct ieee80211_frame *); 1829 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1830 IEEE80211_FC0_SUBTYPE_BEACON; 1831 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1832 *(u_int16_t *)wh->i_dur = 0; 1833 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 1834 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 1835 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 1836 *(u_int16_t *)wh->i_seq = 0; 1837 1838 return m; 1839 } 1840 1841 /* 1842 * Update the dynamic parts of a beacon frame based on the current state. 1843 */ 1844 int 1845 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni, 1846 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 1847 { 1848 int len_changed = 0; 1849 u_int16_t capinfo; 1850 1851 IEEE80211_BEACON_LOCK(ic); 1852 /* XXX faster to recalculate entirely or just changes? */ 1853 if (ic->ic_opmode == IEEE80211_M_IBSS) 1854 capinfo = IEEE80211_CAPINFO_IBSS; 1855 else 1856 capinfo = IEEE80211_CAPINFO_ESS; 1857 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1858 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1859 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1860 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1861 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1862 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1863 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1864 *bo->bo_caps = htole16(capinfo); 1865 1866 if (ic->ic_flags & IEEE80211_F_WME) { 1867 struct ieee80211_wme_state *wme = &ic->ic_wme; 1868 1869 /* 1870 * Check for agressive mode change. When there is 1871 * significant high priority traffic in the BSS 1872 * throttle back BE traffic by using conservative 1873 * parameters. Otherwise BE uses agressive params 1874 * to optimize performance of legacy/non-QoS traffic. 1875 */ 1876 if (wme->wme_flags & WME_F_AGGRMODE) { 1877 if (wme->wme_hipri_traffic > 1878 wme->wme_hipri_switch_thresh) { 1879 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1880 "%s: traffic %u, disable aggressive mode\n", 1881 __func__, wme->wme_hipri_traffic); 1882 wme->wme_flags &= ~WME_F_AGGRMODE; 1883 ieee80211_wme_updateparams_locked(ic); 1884 wme->wme_hipri_traffic = 1885 wme->wme_hipri_switch_hysteresis; 1886 } else 1887 wme->wme_hipri_traffic = 0; 1888 } else { 1889 if (wme->wme_hipri_traffic <= 1890 wme->wme_hipri_switch_thresh) { 1891 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1892 "%s: traffic %u, enable aggressive mode\n", 1893 __func__, wme->wme_hipri_traffic); 1894 wme->wme_flags |= WME_F_AGGRMODE; 1895 ieee80211_wme_updateparams_locked(ic); 1896 wme->wme_hipri_traffic = 0; 1897 } else 1898 wme->wme_hipri_traffic = 1899 wme->wme_hipri_switch_hysteresis; 1900 } 1901 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) { 1902 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 1903 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1904 } 1905 } 1906 1907 #ifndef IEEE80211_NO_HOSTAP 1908 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 1909 struct ieee80211_tim_ie *tie = 1910 (struct ieee80211_tim_ie *) bo->bo_tim; 1911 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) { 1912 u_int timlen, timoff, i; 1913 /* 1914 * ATIM/DTIM needs updating. If it fits in the 1915 * current space allocated then just copy in the 1916 * new bits. Otherwise we need to move any trailing 1917 * data to make room. Note that we know there is 1918 * contiguous space because ieee80211_beacon_allocate 1919 * insures there is space in the mbuf to write a 1920 * maximal-size virtual bitmap (based on ic_max_aid). 1921 */ 1922 /* 1923 * Calculate the bitmap size and offset, copy any 1924 * trailer out of the way, and then copy in the 1925 * new bitmap and update the information element. 1926 * Note that the tim bitmap must contain at least 1927 * one byte and any offset must be even. 1928 */ 1929 if (ic->ic_ps_pending != 0) { 1930 timoff = 128; /* impossibly large */ 1931 for (i = 0; i < ic->ic_tim_len; i++) 1932 if (ic->ic_tim_bitmap[i]) { 1933 timoff = i &~ 1; 1934 break; 1935 } 1936 IASSERT(timoff != 128, ("tim bitmap empty!")); 1937 for (i = ic->ic_tim_len-1; i >= timoff; i--) 1938 if (ic->ic_tim_bitmap[i]) 1939 break; 1940 timlen = 1 + (i - timoff); 1941 } else { 1942 timoff = 0; 1943 timlen = 1; 1944 } 1945 if (timlen != bo->bo_tim_len) { 1946 /* copy up/down trailer */ 1947 ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen, 1948 bo->bo_trailer_len); 1949 bo->bo_trailer = tie->tim_bitmap+timlen; 1950 bo->bo_wme = bo->bo_trailer; 1951 bo->bo_tim_len = timlen; 1952 1953 /* update information element */ 1954 tie->tim_len = 3 + timlen; 1955 tie->tim_bitctl = timoff; 1956 len_changed = 1; 1957 } 1958 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff, 1959 bo->bo_tim_len); 1960 1961 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE; 1962 1963 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1964 "%s: TIM updated, pending %u, off %u, len %u\n", 1965 __func__, ic->ic_ps_pending, timoff, timlen); 1966 } 1967 /* count down DTIM period */ 1968 if (tie->tim_count == 0) 1969 tie->tim_count = tie->tim_period - 1; 1970 else 1971 tie->tim_count--; 1972 /* update state for buffered multicast frames on DTIM */ 1973 if (mcast && (tie->tim_count == 1 || tie->tim_period == 1)) 1974 tie->tim_bitctl |= 1; 1975 else 1976 tie->tim_bitctl &= ~1; 1977 } 1978 #endif /* !IEEE80211_NO_HOSTAP */ 1979 IEEE80211_BEACON_UNLOCK(ic); 1980 1981 return len_changed; 1982 } 1983 1984 /* 1985 * Save an outbound packet for a node in power-save sleep state. 1986 * The new packet is placed on the node's saved queue, and the TIM 1987 * is changed, if necessary. 1988 */ 1989 void 1990 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni, 1991 struct mbuf *m) 1992 { 1993 int qlen, age; 1994 1995 IEEE80211_NODE_SAVEQ_LOCK(ni); 1996 if (IF_QFULL(&ni->ni_savedq)) { 1997 IF_DROP(&ni->ni_savedq); 1998 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1999 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 2000 "[%s] pwr save q overflow, drops %d (size %d)\n", 2001 ether_sprintf(ni->ni_macaddr), 2002 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE); 2003 #ifdef IEEE80211_DEBUG 2004 if (ieee80211_msg_dumppkts(ic)) 2005 ieee80211_dump_pkt(mtod(m, void *), m->m_len, -1, -1); 2006 #endif 2007 m_freem(m); 2008 return; 2009 } 2010 /* 2011 * Tag the frame with it's expiry time and insert 2012 * it in the queue. The aging interval is 4 times 2013 * the listen interval specified by the station. 2014 * Frames that sit around too long are reclaimed 2015 * using this information. 2016 */ 2017 /* XXX handle overflow? */ 2018 age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */ 2019 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age); 2020 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 2021 2022 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2023 "[%s] save frame with age %d, %u now queued\n", 2024 ether_sprintf(ni->ni_macaddr), age, qlen); 2025 2026 if (qlen == 1) 2027 ic->ic_set_tim(ni, 1); 2028 } 2029