ieee80211_output.c revision 139527
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2004 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 139527 2004-12-31 21:54:53Z sam $"); 35 36#include "opt_inet.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/mbuf.h> 41#include <sys/kernel.h> 42#include <sys/endian.h> 43 44#include <sys/socket.h> 45 46#include <net/bpf.h> 47#include <net/ethernet.h> 48#include <net/if.h> 49#include <net/if_llc.h> 50#include <net/if_media.h> 51#include <net/if_vlan_var.h> 52 53#include <net80211/ieee80211_var.h> 54 55#ifdef INET 56#include <netinet/in.h> 57#include <netinet/if_ether.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#endif 61 62#ifdef IEEE80211_DEBUG 63/* 64 * Decide if an outbound management frame should be 65 * printed when debugging is enabled. This filters some 66 * of the less interesting frames that come frequently 67 * (e.g. beacons). 68 */ 69static __inline int 70doprint(struct ieee80211com *ic, int subtype) 71{ 72 switch (subtype) { 73 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 74 return (ic->ic_opmode == IEEE80211_M_IBSS); 75 } 76 return 1; 77} 78#endif 79 80/* 81 * Send a management frame to the specified node. The node pointer 82 * must have a reference as the pointer will be passed to the driver 83 * and potentially held for a long time. If the frame is successfully 84 * dispatched to the driver, then it is responsible for freeing the 85 * reference (and potentially free'ing up any associated storage). 86 */ 87static int 88ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni, 89 struct mbuf *m, int type) 90{ 91 struct ifnet *ifp = ic->ic_ifp; 92 struct ieee80211_frame *wh; 93 94 KASSERT(ni != NULL, ("null node")); 95 96 /* 97 * Yech, hack alert! We want to pass the node down to the 98 * driver's start routine. If we don't do so then the start 99 * routine must immediately look it up again and that can 100 * cause a lock order reversal if, for example, this frame 101 * is being sent because the station is being timedout and 102 * the frame being sent is a DEAUTH message. We could stick 103 * this in an m_tag and tack that on to the mbuf. However 104 * that's rather expensive to do for every frame so instead 105 * we stuff it in the rcvif field since outbound frames do 106 * not (presently) use this. 107 */ 108 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 109 if (m == NULL) 110 return ENOMEM; 111 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 112 m->m_pkthdr.rcvif = (void *)ni; 113 114 wh = mtod(m, struct ieee80211_frame *); 115 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type; 116 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 117 *(u_int16_t *)wh->i_dur = 0; 118 *(u_int16_t *)wh->i_seq = 119 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 120 ni->ni_txseqs[0]++; 121 /* 122 * Hack. When sending PROBE_REQ frames while scanning we 123 * explicitly force a broadcast rather than (as before) clobber 124 * ni_macaddr and ni_bssid. This is stopgap, we need a way 125 * to communicate this directly rather than do something 126 * implicit based on surrounding state. 127 */ 128 if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ && 129 (ic->ic_flags & IEEE80211_F_SCAN)) { 130 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 131 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 132 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 133 } else { 134 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 135 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 136 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 137 } 138 139 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) { 140 m->m_flags &= ~M_LINK0; 141 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 142 "[%s] encrypting frame (%s)\n", 143 ether_sprintf(wh->i_addr1), __func__); 144 wh->i_fc[1] |= IEEE80211_FC1_WEP; 145 } 146#ifdef IEEE80211_DEBUG 147 /* avoid printing too many frames */ 148 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) || 149 ieee80211_msg_dumppkts(ic)) { 150 printf("[%s] send %s on channel %u\n", 151 ether_sprintf(wh->i_addr1), 152 ieee80211_mgt_subtype_name[ 153 (type & IEEE80211_FC0_SUBTYPE_MASK) >> 154 IEEE80211_FC0_SUBTYPE_SHIFT], 155 ieee80211_chan2ieee(ic, ni->ni_chan)); 156 } 157#endif 158 IEEE80211_NODE_STAT(ni, tx_mgmt); 159 IF_ENQUEUE(&ic->ic_mgtq, m); 160 ifp->if_timer = 1; 161 if_start(ifp); 162 return 0; 163} 164 165/* 166 * Send a null data frame to the specified node. 167 */ 168int 169ieee80211_send_nulldata(struct ieee80211com *ic, struct ieee80211_node *ni) 170{ 171 struct ifnet *ifp = ic->ic_ifp; 172 struct mbuf *m; 173 struct ieee80211_frame *wh; 174 175 MGETHDR(m, M_NOWAIT, MT_HEADER); 176 if (m == NULL) { 177 /* XXX debug msg */ 178 ic->ic_stats.is_tx_nobuf++; 179 return ENOMEM; 180 } 181 m->m_pkthdr.rcvif = (void *) ieee80211_ref_node(ni); 182 183 wh = mtod(m, struct ieee80211_frame *); 184 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA | 185 IEEE80211_FC0_SUBTYPE_NODATA; 186 *(u_int16_t *)wh->i_dur = 0; 187 *(u_int16_t *)wh->i_seq = 188 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 189 ni->ni_txseqs[0]++; 190 191 /* XXX WDS */ 192 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 193 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 194 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 195 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr); 196 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame); 197 198 IEEE80211_NODE_STAT(ni, tx_data); 199 200 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */ 201 if_start(ifp); 202 203 return 0; 204} 205 206/* 207 * Assign priority to a frame based on any vlan tag assigned 208 * to the station and/or any Diffserv setting in an IP header. 209 * Finally, if an ACM policy is setup (in station mode) it's 210 * applied. 211 */ 212int 213ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni) 214{ 215 int v_wme_ac, d_wme_ac, ac; 216#ifdef INET 217 struct ether_header *eh; 218#endif 219 220 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 221 ac = WME_AC_BE; 222 goto done; 223 } 224 225 /* 226 * If node has a vlan tag then all traffic 227 * to it must have a matching tag. 228 */ 229 v_wme_ac = 0; 230 if (ni->ni_vlan != 0) { 231 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m); 232 if (mtag != NULL) { 233 IEEE80211_NODE_STAT(ni, tx_novlantag); 234 return 1; 235 } 236 if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) != 237 EVL_VLANOFTAG(ni->ni_vlan)) { 238 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 239 return 1; 240 } 241 /* map vlan priority to AC */ 242 switch (EVL_PRIOFTAG(ni->ni_vlan)) { 243 case 1: 244 case 2: 245 v_wme_ac = WME_AC_BK; 246 break; 247 case 0: 248 case 3: 249 v_wme_ac = WME_AC_BE; 250 break; 251 case 4: 252 case 5: 253 v_wme_ac = WME_AC_VI; 254 break; 255 case 6: 256 case 7: 257 v_wme_ac = WME_AC_VO; 258 break; 259 } 260 } 261 262#ifdef INET 263 eh = mtod(m, struct ether_header *); 264 if (eh->ether_type == htons(ETHERTYPE_IP)) { 265 const struct ip *ip = (struct ip *) 266 (mtod(m, u_int8_t *) + sizeof (*eh)); 267 /* 268 * IP frame, map the TOS field. 269 */ 270 switch (ip->ip_tos) { 271 case 0x08: 272 case 0x20: 273 d_wme_ac = WME_AC_BK; /* background */ 274 break; 275 case 0x28: 276 case 0xa0: 277 d_wme_ac = WME_AC_VI; /* video */ 278 break; 279 case 0x30: /* voice */ 280 case 0xe0: 281 case 0x88: /* XXX UPSD */ 282 case 0xb8: 283 d_wme_ac = WME_AC_VO; 284 break; 285 default: 286 d_wme_ac = WME_AC_BE; 287 break; 288 } 289 } else { 290#endif /* INET */ 291 d_wme_ac = WME_AC_BE; 292#ifdef INET 293 } 294#endif 295 /* 296 * Use highest priority AC. 297 */ 298 if (v_wme_ac > d_wme_ac) 299 ac = v_wme_ac; 300 else 301 ac = d_wme_ac; 302 303 /* 304 * Apply ACM policy. 305 */ 306 if (ic->ic_opmode == IEEE80211_M_STA) { 307 static const int acmap[4] = { 308 WME_AC_BK, /* WME_AC_BE */ 309 WME_AC_BK, /* WME_AC_BK */ 310 WME_AC_BE, /* WME_AC_VI */ 311 WME_AC_VI, /* WME_AC_VO */ 312 }; 313 while (ac != WME_AC_BK && 314 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 315 ac = acmap[ac]; 316 } 317done: 318 M_WME_SETAC(m, ac); 319 return 0; 320} 321 322/* 323 * Insure there is sufficient contiguous space to encapsulate the 324 * 802.11 data frame. If room isn't already there, arrange for it. 325 * Drivers and cipher modules assume we have done the necessary work 326 * and fail rudely if they don't find the space they need. 327 */ 328static struct mbuf * 329ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize, 330 struct ieee80211_key *key, struct mbuf *m) 331{ 332#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 333 int needed_space = hdrsize; 334 335 if (key != NULL) { 336 /* XXX belongs in crypto code? */ 337 needed_space += key->wk_cipher->ic_header; 338 /* XXX frags */ 339 } 340 /* 341 * We know we are called just before stripping an Ethernet 342 * header and prepending an LLC header. This means we know 343 * there will be 344 * sizeof(struct ether_header) - sizeof(struct llc) 345 * bytes recovered to which we need additional space for the 346 * 802.11 header and any crypto header. 347 */ 348 /* XXX check trailing space and copy instead? */ 349 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 350 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 351 if (n == NULL) { 352 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 353 "%s: cannot expand storage\n", __func__); 354 ic->ic_stats.is_tx_nobuf++; 355 m_freem(m); 356 return NULL; 357 } 358 KASSERT(needed_space <= MHLEN, 359 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 360 /* 361 * Setup new mbuf to have leading space to prepend the 362 * 802.11 header and any crypto header bits that are 363 * required (the latter are added when the driver calls 364 * back to ieee80211_crypto_encap to do crypto encapsulation). 365 */ 366 /* NB: must be first 'cuz it clobbers m_data */ 367 m_move_pkthdr(n, m); 368 n->m_len = 0; /* NB: m_gethdr does not set */ 369 n->m_data += needed_space; 370 /* 371 * Pull up Ethernet header to create the expected layout. 372 * We could use m_pullup but that's overkill (i.e. we don't 373 * need the actual data) and it cannot fail so do it inline 374 * for speed. 375 */ 376 /* NB: struct ether_header is known to be contiguous */ 377 n->m_len += sizeof(struct ether_header); 378 m->m_len -= sizeof(struct ether_header); 379 m->m_data += sizeof(struct ether_header); 380 /* 381 * Replace the head of the chain. 382 */ 383 n->m_next = m; 384 m = n; 385 } 386 return m; 387#undef TO_BE_RECLAIMED 388} 389 390#define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none) 391/* 392 * Return the transmit key to use in sending a unicast frame. 393 * If a unicast key is set we use that. When no unicast key is set 394 * we fall back to the default transmit key. 395 */ 396static __inline struct ieee80211_key * 397ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 398{ 399 if (KEY_UNDEFINED(ni->ni_ucastkey)) { 400 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 401 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 402 return NULL; 403 return &ic->ic_nw_keys[ic->ic_def_txkey]; 404 } else { 405 return &ni->ni_ucastkey; 406 } 407} 408 409/* 410 * Return the transmit key to use in sending a multicast frame. 411 * Multicast traffic always uses the group key which is installed as 412 * the default tx key. 413 */ 414static __inline struct ieee80211_key * 415ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 416{ 417 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 418 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 419 return NULL; 420 return &ic->ic_nw_keys[ic->ic_def_txkey]; 421} 422 423/* 424 * Encapsulate an outbound data frame. The mbuf chain is updated. 425 * If an error is encountered NULL is returned. The caller is required 426 * to provide a node reference and pullup the ethernet header in the 427 * first mbuf. 428 */ 429struct mbuf * 430ieee80211_encap(struct ieee80211com *ic, struct mbuf *m, 431 struct ieee80211_node *ni) 432{ 433 struct ether_header eh; 434 struct ieee80211_frame *wh; 435 struct ieee80211_key *key; 436 struct llc *llc; 437 int hdrsize, datalen, addqos; 438 439 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 440 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header)); 441 442 /* 443 * Insure space for additional headers. First identify 444 * transmit key to use in calculating any buffer adjustments 445 * required. This is also used below to do privacy 446 * encapsulation work. Then calculate the 802.11 header 447 * size and any padding required by the driver. 448 * 449 * Note key may be NULL if we fall back to the default 450 * transmit key and that is not set. In that case the 451 * buffer may not be expanded as needed by the cipher 452 * routines, but they will/should discard it. 453 */ 454 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 455 if (ic->ic_opmode == IEEE80211_M_STA || 456 !IEEE80211_IS_MULTICAST(eh.ether_dhost)) 457 key = ieee80211_crypto_getucastkey(ic, ni); 458 else 459 key = ieee80211_crypto_getmcastkey(ic, ni); 460 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) { 461 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 462 "[%s] no default transmit key (%s) deftxkey %u\n", 463 ether_sprintf(eh.ether_dhost), __func__, 464 ic->ic_def_txkey); 465 ic->ic_stats.is_tx_nodefkey++; 466 } 467 } else 468 key = NULL; 469 /* XXX 4-address format */ 470 /* 471 * XXX Some ap's don't handle QoS-encapsulated EAPOL 472 * frames so suppress use. This may be an issue if other 473 * ap's require all data frames to be QoS-encapsulated 474 * once negotiated in which case we'll need to make this 475 * configurable. 476 */ 477 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) && 478 eh.ether_type != htons(ETHERTYPE_PAE); 479 if (addqos) 480 hdrsize = sizeof(struct ieee80211_qosframe); 481 else 482 hdrsize = sizeof(struct ieee80211_frame); 483 if (ic->ic_flags & IEEE80211_F_DATAPAD) 484 hdrsize = roundup(hdrsize, sizeof(u_int32_t)); 485 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m); 486 if (m == NULL) { 487 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 488 goto bad; 489 } 490 491 /* NB: this could be optimized because of ieee80211_mbuf_adjust */ 492 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 493 llc = mtod(m, struct llc *); 494 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 495 llc->llc_control = LLC_UI; 496 llc->llc_snap.org_code[0] = 0; 497 llc->llc_snap.org_code[1] = 0; 498 llc->llc_snap.org_code[2] = 0; 499 llc->llc_snap.ether_type = eh.ether_type; 500 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 501 502 M_PREPEND(m, hdrsize, M_DONTWAIT); 503 if (m == NULL) { 504 ic->ic_stats.is_tx_nobuf++; 505 goto bad; 506 } 507 wh = mtod(m, struct ieee80211_frame *); 508 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 509 *(u_int16_t *)wh->i_dur = 0; 510 switch (ic->ic_opmode) { 511 case IEEE80211_M_STA: 512 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 513 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 514 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 515 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 516 break; 517 case IEEE80211_M_IBSS: 518 case IEEE80211_M_AHDEMO: 519 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 520 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 521 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 522 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 523 break; 524 case IEEE80211_M_HOSTAP: 525 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 526 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 527 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 528 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 529 break; 530 case IEEE80211_M_MONITOR: 531 goto bad; 532 } 533 if (addqos) { 534 struct ieee80211_qosframe *qwh = 535 (struct ieee80211_qosframe *) wh; 536 int ac, tid; 537 538 ac = M_WME_GETAC(m); 539 /* map from access class/queue to 11e header priorty value */ 540 tid = WME_AC_TO_TID(ac); 541 qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 542 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 543 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S; 544 qwh->i_qos[1] = 0; 545 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 546 547 *(u_int16_t *)wh->i_seq = 548 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 549 ni->ni_txseqs[tid]++; 550 } else { 551 *(u_int16_t *)wh->i_seq = 552 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 553 ni->ni_txseqs[0]++; 554 } 555 if (key != NULL) { 556 /* 557 * IEEE 802.1X: send EAPOL frames always in the clear. 558 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 559 */ 560 if (eh.ether_type != htons(ETHERTYPE_PAE) || 561 ((ic->ic_flags & IEEE80211_F_WPA) && 562 !KEY_UNDEFINED(ni->ni_ucastkey))) { 563 wh->i_fc[1] |= IEEE80211_FC1_WEP; 564 /* XXX do fragmentation */ 565 if (!ieee80211_crypto_enmic(ic, key, m)) { 566 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 567 "[%s] enmic failed, discard frame\n", 568 ether_sprintf(eh.ether_dhost)); 569 ic->ic_stats.is_crypto_enmicfail++; 570 goto bad; 571 } 572 } 573 } 574 575 IEEE80211_NODE_STAT(ni, tx_data); 576 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 577 578 return m; 579bad: 580 if (m != NULL) 581 m_freem(m); 582 return NULL; 583} 584 585/* 586 * Add a supported rates element id to a frame. 587 */ 588static u_int8_t * 589ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs) 590{ 591 int nrates; 592 593 *frm++ = IEEE80211_ELEMID_RATES; 594 nrates = rs->rs_nrates; 595 if (nrates > IEEE80211_RATE_SIZE) 596 nrates = IEEE80211_RATE_SIZE; 597 *frm++ = nrates; 598 memcpy(frm, rs->rs_rates, nrates); 599 return frm + nrates; 600} 601 602/* 603 * Add an extended supported rates element id to a frame. 604 */ 605static u_int8_t * 606ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs) 607{ 608 /* 609 * Add an extended supported rates element if operating in 11g mode. 610 */ 611 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 612 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 613 *frm++ = IEEE80211_ELEMID_XRATES; 614 *frm++ = nrates; 615 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 616 frm += nrates; 617 } 618 return frm; 619} 620 621/* 622 * Add an ssid elemet to a frame. 623 */ 624static u_int8_t * 625ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len) 626{ 627 *frm++ = IEEE80211_ELEMID_SSID; 628 *frm++ = len; 629 memcpy(frm, ssid, len); 630 return frm + len; 631} 632 633/* 634 * Add an erp element to a frame. 635 */ 636static u_int8_t * 637ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic) 638{ 639 u_int8_t erp; 640 641 *frm++ = IEEE80211_ELEMID_ERP; 642 *frm++ = 1; 643 erp = 0; 644 if (ic->ic_nonerpsta != 0) 645 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 646 if (ic->ic_flags & IEEE80211_F_USEPROT) 647 erp |= IEEE80211_ERP_USE_PROTECTION; 648 if (ic->ic_flags & IEEE80211_F_USEBARKER) 649 erp |= IEEE80211_ERP_LONG_PREAMBLE; 650 *frm++ = erp; 651 return frm; 652} 653 654static u_int8_t * 655ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie) 656{ 657#define WPA_OUI_BYTES 0x00, 0x50, 0xf2 658#define ADDSHORT(frm, v) do { \ 659 frm[0] = (v) & 0xff; \ 660 frm[1] = (v) >> 8; \ 661 frm += 2; \ 662} while (0) 663#define ADDSELECTOR(frm, sel) do { \ 664 memcpy(frm, sel, 4); \ 665 frm += 4; \ 666} while (0) 667 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE }; 668 static const u_int8_t cipher_suite[][4] = { 669 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */ 670 { WPA_OUI_BYTES, WPA_CSE_TKIP }, 671 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */ 672 { WPA_OUI_BYTES, WPA_CSE_CCMP }, 673 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 674 { WPA_OUI_BYTES, WPA_CSE_NULL }, 675 }; 676 static const u_int8_t wep104_suite[4] = 677 { WPA_OUI_BYTES, WPA_CSE_WEP104 }; 678 static const u_int8_t key_mgt_unspec[4] = 679 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC }; 680 static const u_int8_t key_mgt_psk[4] = 681 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK }; 682 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 683 u_int8_t *frm = ie; 684 u_int8_t *selcnt; 685 686 *frm++ = IEEE80211_ELEMID_VENDOR; 687 *frm++ = 0; /* length filled in below */ 688 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */ 689 frm += sizeof(oui); 690 ADDSHORT(frm, WPA_VERSION); 691 692 /* XXX filter out CKIP */ 693 694 /* multicast cipher */ 695 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 696 rsn->rsn_mcastkeylen >= 13) 697 ADDSELECTOR(frm, wep104_suite); 698 else 699 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 700 701 /* unicast cipher list */ 702 selcnt = frm; 703 ADDSHORT(frm, 0); /* selector count */ 704 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 705 selcnt[0]++; 706 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 707 } 708 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 709 selcnt[0]++; 710 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 711 } 712 713 /* authenticator selector list */ 714 selcnt = frm; 715 ADDSHORT(frm, 0); /* selector count */ 716 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 717 selcnt[0]++; 718 ADDSELECTOR(frm, key_mgt_unspec); 719 } 720 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 721 selcnt[0]++; 722 ADDSELECTOR(frm, key_mgt_psk); 723 } 724 725 /* optional capabilities */ 726 if (rsn->rsn_caps != 0) 727 ADDSHORT(frm, rsn->rsn_caps); 728 729 /* calculate element length */ 730 ie[1] = frm - ie - 2; 731 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 732 ("WPA IE too big, %u > %zu", 733 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 734 return frm; 735#undef ADDSHORT 736#undef ADDSELECTOR 737#undef WPA_OUI_BYTES 738} 739 740static u_int8_t * 741ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie) 742{ 743#define RSN_OUI_BYTES 0x00, 0x0f, 0xac 744#define ADDSHORT(frm, v) do { \ 745 frm[0] = (v) & 0xff; \ 746 frm[1] = (v) >> 8; \ 747 frm += 2; \ 748} while (0) 749#define ADDSELECTOR(frm, sel) do { \ 750 memcpy(frm, sel, 4); \ 751 frm += 4; \ 752} while (0) 753 static const u_int8_t cipher_suite[][4] = { 754 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */ 755 { RSN_OUI_BYTES, RSN_CSE_TKIP }, 756 { RSN_OUI_BYTES, RSN_CSE_WRAP }, 757 { RSN_OUI_BYTES, RSN_CSE_CCMP }, 758 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 759 { RSN_OUI_BYTES, RSN_CSE_NULL }, 760 }; 761 static const u_int8_t wep104_suite[4] = 762 { RSN_OUI_BYTES, RSN_CSE_WEP104 }; 763 static const u_int8_t key_mgt_unspec[4] = 764 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC }; 765 static const u_int8_t key_mgt_psk[4] = 766 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK }; 767 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 768 u_int8_t *frm = ie; 769 u_int8_t *selcnt; 770 771 *frm++ = IEEE80211_ELEMID_RSN; 772 *frm++ = 0; /* length filled in below */ 773 ADDSHORT(frm, RSN_VERSION); 774 775 /* XXX filter out CKIP */ 776 777 /* multicast cipher */ 778 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 779 rsn->rsn_mcastkeylen >= 13) 780 ADDSELECTOR(frm, wep104_suite); 781 else 782 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 783 784 /* unicast cipher list */ 785 selcnt = frm; 786 ADDSHORT(frm, 0); /* selector count */ 787 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 788 selcnt[0]++; 789 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 790 } 791 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 792 selcnt[0]++; 793 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 794 } 795 796 /* authenticator selector list */ 797 selcnt = frm; 798 ADDSHORT(frm, 0); /* selector count */ 799 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 800 selcnt[0]++; 801 ADDSELECTOR(frm, key_mgt_unspec); 802 } 803 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 804 selcnt[0]++; 805 ADDSELECTOR(frm, key_mgt_psk); 806 } 807 808 /* optional capabilities */ 809 if (rsn->rsn_caps != 0) 810 ADDSHORT(frm, rsn->rsn_caps); 811 /* XXX PMKID */ 812 813 /* calculate element length */ 814 ie[1] = frm - ie - 2; 815 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 816 ("RSN IE too big, %u > %zu", 817 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 818 return frm; 819#undef ADDSELECTOR 820#undef ADDSHORT 821#undef RSN_OUI_BYTES 822} 823 824/* 825 * Add a WPA/RSN element to a frame. 826 */ 827static u_int8_t * 828ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic) 829{ 830 831 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!")); 832 if (ic->ic_flags & IEEE80211_F_WPA2) 833 frm = ieee80211_setup_rsn_ie(ic, frm); 834 if (ic->ic_flags & IEEE80211_F_WPA1) 835 frm = ieee80211_setup_wpa_ie(ic, frm); 836 return frm; 837} 838 839#define WME_OUI_BYTES 0x00, 0x50, 0xf2 840/* 841 * Add a WME information element to a frame. 842 */ 843static u_int8_t * 844ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme) 845{ 846 static const struct ieee80211_wme_info info = { 847 .wme_id = IEEE80211_ELEMID_VENDOR, 848 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 849 .wme_oui = { WME_OUI_BYTES }, 850 .wme_type = WME_OUI_TYPE, 851 .wme_subtype = WME_INFO_OUI_SUBTYPE, 852 .wme_version = WME_VERSION, 853 .wme_info = 0, 854 }; 855 memcpy(frm, &info, sizeof(info)); 856 return frm + sizeof(info); 857} 858 859/* 860 * Add a WME parameters element to a frame. 861 */ 862static u_int8_t * 863ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme) 864{ 865#define SM(_v, _f) (((_v) << _f##_S) & _f) 866#define ADDSHORT(frm, v) do { \ 867 frm[0] = (v) & 0xff; \ 868 frm[1] = (v) >> 8; \ 869 frm += 2; \ 870} while (0) 871 /* NB: this works 'cuz a param has an info at the front */ 872 static const struct ieee80211_wme_info param = { 873 .wme_id = IEEE80211_ELEMID_VENDOR, 874 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 875 .wme_oui = { WME_OUI_BYTES }, 876 .wme_type = WME_OUI_TYPE, 877 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 878 .wme_version = WME_VERSION, 879 }; 880 int i; 881 882 memcpy(frm, ¶m, sizeof(param)); 883 frm += __offsetof(struct ieee80211_wme_info, wme_info); 884 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 885 *frm++ = 0; /* reserved field */ 886 for (i = 0; i < WME_NUM_AC; i++) { 887 const struct wmeParams *ac = 888 &wme->wme_bssChanParams.cap_wmeParams[i]; 889 *frm++ = SM(i, WME_PARAM_ACI) 890 | SM(ac->wmep_acm, WME_PARAM_ACM) 891 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 892 ; 893 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 894 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 895 ; 896 ADDSHORT(frm, ac->wmep_txopLimit); 897 } 898 return frm; 899#undef SM 900#undef ADDSHORT 901} 902#undef WME_OUI_BYTES 903 904/* 905 * Send a management frame. The node is for the destination (or ic_bss 906 * when in station mode). Nodes other than ic_bss have their reference 907 * count bumped to reflect our use for an indeterminant time. 908 */ 909int 910ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 911 int type, int arg) 912{ 913#define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 914 struct mbuf *m; 915 u_int8_t *frm; 916 enum ieee80211_phymode mode; 917 u_int16_t capinfo; 918 int has_challenge, is_shared_key, ret, timer, status; 919 920 KASSERT(ni != NULL, ("null node")); 921 922 /* 923 * Hold a reference on the node so it doesn't go away until after 924 * the xmit is complete all the way in the driver. On error we 925 * will remove our reference. 926 */ 927 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 928 "ieee80211_ref_node (%s:%u) %s refcnt %d\n", 929 __func__, __LINE__, 930 ether_sprintf(ni->ni_macaddr), 931 ieee80211_node_refcnt(ni)+1); 932 ieee80211_ref_node(ni); 933 934 timer = 0; 935 switch (type) { 936 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 937 /* 938 * prreq frame format 939 * [tlv] ssid 940 * [tlv] supported rates 941 * [tlv] extended supported rates 942 * [tlv] WME (optional) 943 * [tlv] user-specified ie's 944 */ 945 m = ieee80211_getmgtframe(&frm, 946 2 + IEEE80211_NWID_LEN 947 + 2 + IEEE80211_RATE_SIZE 948 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 949 + sizeof(struct ieee80211_wme_param) 950 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 951 ); 952 if (m == NULL) 953 senderr(ENOMEM, is_tx_nobuf); 954 955 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen); 956 mode = ieee80211_chan2mode(ic, ni->ni_chan); 957 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]); 958 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]); 959 if (ic->ic_flags & IEEE80211_F_WME) 960 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 961 if (ic->ic_opt_ie != NULL) { 962 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 963 frm += ic->ic_opt_ie_len; 964 } 965 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 966 967 IEEE80211_NODE_STAT(ni, tx_probereq); 968 if (ic->ic_opmode == IEEE80211_M_STA) 969 timer = IEEE80211_TRANS_WAIT; 970 break; 971 972 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 973 /* 974 * probe response frame format 975 * [8] time stamp 976 * [2] beacon interval 977 * [2] cabability information 978 * [tlv] ssid 979 * [tlv] supported rates 980 * [tlv] parameter set (FH/DS) 981 * [tlv] parameter set (IBSS) 982 * [tlv] extended rate phy (ERP) 983 * [tlv] extended supported rates 984 * [tlv] WPA 985 */ 986 m = ieee80211_getmgtframe(&frm, 987 8 988 + sizeof(u_int16_t) 989 + sizeof(u_int16_t) 990 + 2 + IEEE80211_NWID_LEN 991 + 2 + IEEE80211_RATE_SIZE 992 + 7 /* max(7,3) */ 993 + 6 994 + 3 995 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 996 /* XXX !WPA1+WPA2 fits w/o a cluster */ 997 + (ic->ic_flags & IEEE80211_F_WPA ? 998 2*sizeof(struct ieee80211_ie_wpa) : 0) 999 ); 1000 if (m == NULL) 1001 senderr(ENOMEM, is_tx_nobuf); 1002 1003 memset(frm, 0, 8); /* timestamp should be filled later */ 1004 frm += 8; 1005 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval); 1006 frm += 2; 1007 if (ic->ic_opmode == IEEE80211_M_IBSS) 1008 capinfo = IEEE80211_CAPINFO_IBSS; 1009 else 1010 capinfo = IEEE80211_CAPINFO_ESS; 1011 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1012 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1013 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1014 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1015 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1016 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1017 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1018 *(u_int16_t *)frm = htole16(capinfo); 1019 frm += 2; 1020 1021 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1022 ic->ic_bss->ni_esslen); 1023 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1024 1025 if (ic->ic_phytype == IEEE80211_T_FH) { 1026 *frm++ = IEEE80211_ELEMID_FHPARMS; 1027 *frm++ = 5; 1028 *frm++ = ni->ni_fhdwell & 0x00ff; 1029 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff; 1030 *frm++ = IEEE80211_FH_CHANSET( 1031 ieee80211_chan2ieee(ic, ni->ni_chan)); 1032 *frm++ = IEEE80211_FH_CHANPAT( 1033 ieee80211_chan2ieee(ic, ni->ni_chan)); 1034 *frm++ = ni->ni_fhindex; 1035 } else { 1036 *frm++ = IEEE80211_ELEMID_DSPARMS; 1037 *frm++ = 1; 1038 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 1039 } 1040 1041 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1042 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1043 *frm++ = 2; 1044 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1045 } 1046 if (ic->ic_flags & IEEE80211_F_WPA) 1047 frm = ieee80211_add_wpa(frm, ic); 1048 if (ic->ic_curmode == IEEE80211_MODE_11G) 1049 frm = ieee80211_add_erp(frm, ic); 1050 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1051 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1052 break; 1053 1054 case IEEE80211_FC0_SUBTYPE_AUTH: 1055 status = arg >> 16; 1056 arg &= 0xffff; 1057 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1058 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1059 ni->ni_challenge != NULL); 1060 1061 /* 1062 * Deduce whether we're doing open authentication or 1063 * shared key authentication. We do the latter if 1064 * we're in the middle of a shared key authentication 1065 * handshake or if we're initiating an authentication 1066 * request and configured to use shared key. 1067 */ 1068 is_shared_key = has_challenge || 1069 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1070 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1071 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED); 1072 1073 m = ieee80211_getmgtframe(&frm, 1074 3 * sizeof(u_int16_t) 1075 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1076 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0) 1077 ); 1078 if (m == NULL) 1079 senderr(ENOMEM, is_tx_nobuf); 1080 1081 ((u_int16_t *)frm)[0] = 1082 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1083 : htole16(IEEE80211_AUTH_ALG_OPEN); 1084 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */ 1085 ((u_int16_t *)frm)[2] = htole16(status);/* status */ 1086 1087 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1088 ((u_int16_t *)frm)[3] = 1089 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1090 IEEE80211_ELEMID_CHALLENGE); 1091 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge, 1092 IEEE80211_CHALLENGE_LEN); 1093 m->m_pkthdr.len = m->m_len = 1094 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN; 1095 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1096 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1097 "[%s] request encrypt frame (%s)\n", 1098 ether_sprintf(ni->ni_macaddr), __func__); 1099 m->m_flags |= M_LINK0; /* WEP-encrypt, please */ 1100 } 1101 } else 1102 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t); 1103 1104 /* XXX not right for shared key */ 1105 if (status == IEEE80211_STATUS_SUCCESS) 1106 IEEE80211_NODE_STAT(ni, tx_auth); 1107 else 1108 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1109 1110 /* 1111 * When 802.1x is not in use mark the port 1112 * authorized at this point so traffic can flow. 1113 */ 1114 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1115 status == IEEE80211_STATUS_SUCCESS && 1116 ni->ni_authmode != IEEE80211_AUTH_8021X) 1117 ieee80211_node_authorize(ic, ni); 1118 if (ic->ic_opmode == IEEE80211_M_STA) 1119 timer = IEEE80211_TRANS_WAIT; 1120 break; 1121 1122 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1123 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1124 "[%s] send station deauthenticate (reason %d)\n", 1125 ether_sprintf(ni->ni_macaddr), arg); 1126 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1127 if (m == NULL) 1128 senderr(ENOMEM, is_tx_nobuf); 1129 *(u_int16_t *)frm = htole16(arg); /* reason */ 1130 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1131 1132 IEEE80211_NODE_STAT(ni, tx_deauth); 1133 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1134 1135 ieee80211_node_unauthorize(ic, ni); /* port closed */ 1136 break; 1137 1138 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1139 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1140 /* 1141 * asreq frame format 1142 * [2] capability information 1143 * [2] listen interval 1144 * [6*] current AP address (reassoc only) 1145 * [tlv] ssid 1146 * [tlv] supported rates 1147 * [tlv] extended supported rates 1148 * [tlv] WME 1149 * [tlv] user-specified ie's 1150 */ 1151 m = ieee80211_getmgtframe(&frm, 1152 sizeof(u_int16_t) 1153 + sizeof(u_int16_t) 1154 + IEEE80211_ADDR_LEN 1155 + 2 + IEEE80211_NWID_LEN 1156 + 2 + IEEE80211_RATE_SIZE 1157 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1158 + sizeof(struct ieee80211_wme_info) 1159 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 1160 ); 1161 if (m == NULL) 1162 senderr(ENOMEM, is_tx_nobuf); 1163 1164 capinfo = 0; 1165 if (ic->ic_opmode == IEEE80211_M_IBSS) 1166 capinfo |= IEEE80211_CAPINFO_IBSS; 1167 else /* IEEE80211_M_STA */ 1168 capinfo |= IEEE80211_CAPINFO_ESS; 1169 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1170 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1171 /* 1172 * NB: Some 11a AP's reject the request when 1173 * short premable is set. 1174 */ 1175 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1176 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1177 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1178 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) && 1179 (ic->ic_caps & IEEE80211_C_SHSLOT)) 1180 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1181 *(u_int16_t *)frm = htole16(capinfo); 1182 frm += 2; 1183 1184 *(u_int16_t *)frm = htole16(ic->ic_lintval); 1185 frm += 2; 1186 1187 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1188 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1189 frm += IEEE80211_ADDR_LEN; 1190 } 1191 1192 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1193 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1194 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1195 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1196 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1197 if (ic->ic_opt_ie != NULL) { 1198 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 1199 frm += ic->ic_opt_ie_len; 1200 } 1201 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1202 1203 timer = IEEE80211_TRANS_WAIT; 1204 break; 1205 1206 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1207 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1208 /* 1209 * asreq frame format 1210 * [2] capability information 1211 * [2] status 1212 * [2] association ID 1213 * [tlv] supported rates 1214 * [tlv] extended supported rates 1215 * [tlv] WME (if enabled and STA enabled) 1216 */ 1217 m = ieee80211_getmgtframe(&frm, 1218 sizeof(u_int16_t) 1219 + sizeof(u_int16_t) 1220 + sizeof(u_int16_t) 1221 + 2 + IEEE80211_RATE_SIZE 1222 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1223 + sizeof(struct ieee80211_wme_param) 1224 ); 1225 if (m == NULL) 1226 senderr(ENOMEM, is_tx_nobuf); 1227 1228 capinfo = IEEE80211_CAPINFO_ESS; 1229 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1230 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1231 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1232 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1233 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1234 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1235 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1236 *(u_int16_t *)frm = htole16(capinfo); 1237 frm += 2; 1238 1239 *(u_int16_t *)frm = htole16(arg); /* status */ 1240 frm += 2; 1241 1242 if (arg == IEEE80211_STATUS_SUCCESS) { 1243 *(u_int16_t *)frm = htole16(ni->ni_associd); 1244 IEEE80211_NODE_STAT(ni, tx_assoc); 1245 } else 1246 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1247 frm += 2; 1248 1249 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1250 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1251 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1252 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1253 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1254 break; 1255 1256 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1257 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1258 "[%s] send station disassociate (reason %d)\n", 1259 ether_sprintf(ni->ni_macaddr), arg); 1260 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1261 if (m == NULL) 1262 senderr(ENOMEM, is_tx_nobuf); 1263 *(u_int16_t *)frm = htole16(arg); /* reason */ 1264 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1265 1266 IEEE80211_NODE_STAT(ni, tx_disassoc); 1267 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 1268 break; 1269 1270 default: 1271 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1272 "[%s] invalid mgmt frame type %u\n", 1273 ether_sprintf(ni->ni_macaddr), type); 1274 senderr(EINVAL, is_tx_unknownmgt); 1275 /* NOTREACHED */ 1276 } 1277 1278 ret = ieee80211_mgmt_output(ic, ni, m, type); 1279 if (ret == 0) { 1280 if (timer) 1281 ic->ic_mgt_timer = timer; 1282 } else { 1283bad: 1284 ieee80211_free_node(ni); 1285 } 1286 return ret; 1287#undef senderr 1288} 1289 1290/* 1291 * Allocate a beacon frame and fillin the appropriate bits. 1292 */ 1293struct mbuf * 1294ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni, 1295 struct ieee80211_beacon_offsets *bo) 1296{ 1297 struct ifnet *ifp = ic->ic_ifp; 1298 struct ieee80211_frame *wh; 1299 struct mbuf *m; 1300 int pktlen; 1301 u_int8_t *frm, *efrm; 1302 u_int16_t capinfo; 1303 struct ieee80211_rateset *rs; 1304 1305 /* 1306 * beacon frame format 1307 * [8] time stamp 1308 * [2] beacon interval 1309 * [2] cabability information 1310 * [tlv] ssid 1311 * [tlv] supported rates 1312 * [3] parameter set (DS) 1313 * [tlv] parameter set (IBSS/TIM) 1314 * [tlv] extended rate phy (ERP) 1315 * [tlv] extended supported rates 1316 * [tlv] WME parameters 1317 * [tlv] WPA/RSN parameters 1318 * XXX Vendor-specific OIDs (e.g. Atheros) 1319 * NB: we allocate the max space required for the TIM bitmap. 1320 */ 1321 rs = &ni->ni_rates; 1322 pktlen = 8 /* time stamp */ 1323 + sizeof(u_int16_t) /* beacon interval */ 1324 + sizeof(u_int16_t) /* capabilities */ 1325 + 2 + ni->ni_esslen /* ssid */ 1326 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 1327 + 2 + 1 /* DS parameters */ 1328 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */ 1329 + 2 + 1 /* ERP */ 1330 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1331 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */ 1332 sizeof(struct ieee80211_wme_param) : 0) 1333 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 1334 2*sizeof(struct ieee80211_ie_wpa) : 0) 1335 ; 1336 m = ieee80211_getmgtframe(&frm, pktlen); 1337 if (m == NULL) { 1338 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1339 "%s: cannot get buf; size %u\n", __func__, pktlen); 1340 ic->ic_stats.is_tx_nobuf++; 1341 return NULL; 1342 } 1343 1344 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 1345 frm += 8; 1346 *(u_int16_t *)frm = htole16(ni->ni_intval); 1347 frm += 2; 1348 if (ic->ic_opmode == IEEE80211_M_IBSS) 1349 capinfo = IEEE80211_CAPINFO_IBSS; 1350 else 1351 capinfo = IEEE80211_CAPINFO_ESS; 1352 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1353 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1354 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1355 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1356 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1357 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1358 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1359 bo->bo_caps = (u_int16_t *)frm; 1360 *(u_int16_t *)frm = htole16(capinfo); 1361 frm += 2; 1362 *frm++ = IEEE80211_ELEMID_SSID; 1363 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) { 1364 *frm++ = ni->ni_esslen; 1365 memcpy(frm, ni->ni_essid, ni->ni_esslen); 1366 frm += ni->ni_esslen; 1367 } else 1368 *frm++ = 0; 1369 frm = ieee80211_add_rates(frm, rs); 1370 if (ic->ic_curmode != IEEE80211_MODE_FH) { 1371 *frm++ = IEEE80211_ELEMID_DSPARMS; 1372 *frm++ = 1; 1373 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 1374 } 1375 bo->bo_tim = frm; 1376 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1377 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1378 *frm++ = 2; 1379 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1380 bo->bo_tim_len = 0; 1381 } else { 1382 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 1383 1384 tie->tim_ie = IEEE80211_ELEMID_TIM; 1385 tie->tim_len = 4; /* length */ 1386 tie->tim_count = 0; /* DTIM count */ 1387 tie->tim_period = ic->ic_dtim_period; /* DTIM period */ 1388 tie->tim_bitctl = 0; /* bitmap control */ 1389 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 1390 frm += sizeof(struct ieee80211_tim_ie); 1391 bo->bo_tim_len = 1; 1392 } 1393 bo->bo_trailer = frm; 1394 if (ic->ic_flags & IEEE80211_F_WME) { 1395 bo->bo_wme = frm; 1396 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1397 } 1398 if (ic->ic_flags & IEEE80211_F_WPA) 1399 frm = ieee80211_add_wpa(frm, ic); 1400 if (ic->ic_curmode == IEEE80211_MODE_11G) 1401 frm = ieee80211_add_erp(frm, ic); 1402 efrm = ieee80211_add_xrates(frm, rs); 1403 bo->bo_trailer_len = efrm - bo->bo_trailer; 1404 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *); 1405 1406 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1407 KASSERT(m != NULL, ("no space for 802.11 header?")); 1408 wh = mtod(m, struct ieee80211_frame *); 1409 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1410 IEEE80211_FC0_SUBTYPE_BEACON; 1411 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1412 *(u_int16_t *)wh->i_dur = 0; 1413 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 1414 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 1415 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 1416 *(u_int16_t *)wh->i_seq = 0; 1417 1418 return m; 1419} 1420 1421/* 1422 * Update the dynamic parts of a beacon frame based on the current state. 1423 */ 1424int 1425ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni, 1426 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 1427{ 1428 int len_changed = 0; 1429 u_int16_t capinfo; 1430 1431 IEEE80211_BEACON_LOCK(ic); 1432 /* XXX faster to recalculate entirely or just changes? */ 1433 if (ic->ic_opmode == IEEE80211_M_IBSS) 1434 capinfo = IEEE80211_CAPINFO_IBSS; 1435 else 1436 capinfo = IEEE80211_CAPINFO_ESS; 1437 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1438 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1439 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1440 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1441 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1442 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1443 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1444 *bo->bo_caps = htole16(capinfo); 1445 1446 if (ic->ic_flags & IEEE80211_F_WME) { 1447 struct ieee80211_wme_state *wme = &ic->ic_wme; 1448 1449 /* 1450 * Check for agressive mode change. When there is 1451 * significant high priority traffic in the BSS 1452 * throttle back BE traffic by using conservative 1453 * parameters. Otherwise BE uses agressive params 1454 * to optimize performance of legacy/non-QoS traffic. 1455 */ 1456 if (wme->wme_flags & WME_F_AGGRMODE) { 1457 if (wme->wme_hipri_traffic > 1458 wme->wme_hipri_switch_thresh) { 1459 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1460 "%s: traffic %u, disable aggressive mode\n", 1461 __func__, wme->wme_hipri_traffic); 1462 wme->wme_flags &= ~WME_F_AGGRMODE; 1463 ieee80211_wme_updateparams_locked(ic); 1464 wme->wme_hipri_traffic = 1465 wme->wme_hipri_switch_hysteresis; 1466 } else 1467 wme->wme_hipri_traffic = 0; 1468 } else { 1469 if (wme->wme_hipri_traffic <= 1470 wme->wme_hipri_switch_thresh) { 1471 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1472 "%s: traffic %u, enable aggressive mode\n", 1473 __func__, wme->wme_hipri_traffic); 1474 wme->wme_flags |= WME_F_AGGRMODE; 1475 ieee80211_wme_updateparams_locked(ic); 1476 wme->wme_hipri_traffic = 0; 1477 } else 1478 wme->wme_hipri_traffic = 1479 wme->wme_hipri_switch_hysteresis; 1480 } 1481 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) { 1482 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 1483 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1484 } 1485 } 1486 1487 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 1488 struct ieee80211_tim_ie *tie = 1489 (struct ieee80211_tim_ie *) bo->bo_tim; 1490 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) { 1491 u_int timlen, timoff, i; 1492 /* 1493 * ATIM/DTIM needs updating. If it fits in the 1494 * current space allocated then just copy in the 1495 * new bits. Otherwise we need to move any trailing 1496 * data to make room. Note that we know there is 1497 * contiguous space because ieee80211_beacon_allocate 1498 * insures there is space in the mbuf to write a 1499 * maximal-size virtual bitmap (based on ic_max_aid). 1500 */ 1501 /* 1502 * Calculate the bitmap size and offset, copy any 1503 * trailer out of the way, and then copy in the 1504 * new bitmap and update the information element. 1505 * Note that the tim bitmap must contain at least 1506 * one byte and any offset must be even. 1507 */ 1508 if (ic->ic_ps_pending != 0) { 1509 timoff = 128; /* impossibly large */ 1510 for (i = 0; i < ic->ic_tim_len; i++) 1511 if (ic->ic_tim_bitmap[i]) { 1512 timoff = i &~ 1; 1513 break; 1514 } 1515 KASSERT(timoff != 128, ("tim bitmap empty!")); 1516 for (i = ic->ic_tim_len-1; i >= timoff; i--) 1517 if (ic->ic_tim_bitmap[i]) 1518 break; 1519 timlen = 1 + (i - timoff); 1520 } else { 1521 timoff = 0; 1522 timlen = 1; 1523 } 1524 if (timlen != bo->bo_tim_len) { 1525 /* copy up/down trailer */ 1526 ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen, 1527 bo->bo_trailer_len); 1528 bo->bo_trailer = tie->tim_bitmap+timlen; 1529 bo->bo_wme = bo->bo_trailer; 1530 bo->bo_tim_len = timlen; 1531 1532 /* update information element */ 1533 tie->tim_len = 3 + timlen; 1534 tie->tim_bitctl = timoff; 1535 len_changed = 1; 1536 } 1537 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff, 1538 bo->bo_tim_len); 1539 1540 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE; 1541 1542 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1543 "%s: TIM updated, pending %u, off %u, len %u\n", 1544 __func__, ic->ic_ps_pending, timoff, timlen); 1545 } 1546 /* count down DTIM period */ 1547 if (tie->tim_count == 0) 1548 tie->tim_count = tie->tim_period - 1; 1549 else 1550 tie->tim_count--; 1551 /* update state for buffered multicast frames on DTIM */ 1552 if (mcast && (tie->tim_count == 1 || tie->tim_period == 1)) 1553 tie->tim_bitctl |= 1; 1554 else 1555 tie->tim_bitctl &= ~1; 1556 } 1557 IEEE80211_BEACON_UNLOCK(ic); 1558 1559 return len_changed; 1560} 1561 1562/* 1563 * Save an outbound packet for a node in power-save sleep state. 1564 * The new packet is placed on the node's saved queue, and the TIM 1565 * is changed, if necessary. 1566 */ 1567void 1568ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni, 1569 struct mbuf *m) 1570{ 1571 int qlen, age; 1572 1573 IEEE80211_NODE_SAVEQ_LOCK(ni); 1574 if (_IF_QFULL(&ni->ni_savedq)) { 1575 _IF_DROP(&ni->ni_savedq); 1576 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1577 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1578 "[%s] pwr save q overflow, drops %d (size %d)\n", 1579 ether_sprintf(ni->ni_macaddr), 1580 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE); 1581#ifdef IEEE80211_DEBUG 1582 if (ieee80211_msg_dumppkts(ic)) 1583 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1); 1584#endif 1585 m_freem(m); 1586 return; 1587 } 1588 /* 1589 * Tag the frame with it's expiry time and insert 1590 * it in the queue. The aging interval is 4 times 1591 * the listen interval specified by the station. 1592 * Frames that sit around too long are reclaimed 1593 * using this information. 1594 */ 1595 /* XXX handle overflow? */ 1596 age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */ 1597 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age); 1598 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1599 1600 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1601 "[%s] save frame, %u now queued\n", 1602 ether_sprintf(ni->ni_macaddr), qlen); 1603 1604 if (qlen == 1) 1605 ic->ic_set_tim(ic, ni, 1); 1606} 1607