2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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.
32 * $FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.26.2.8 2006/09/02 15:06:04 sam Exp $
33 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_output.c,v 1.9 2006/12/09 07:22:49 sephe Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/endian.h>
44 #include <sys/socket.h>
47 #include <net/ethernet.h>
49 #include <net/if_arp.h>
50 #include <net/if_llc.h>
51 #include <net/if_media.h>
52 #include <net/vlan/if_vlan_var.h>
54 #include <netproto/802_11/ieee80211_var.h>
57 #include <netinet/in.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
63 #ifdef IEEE80211_DEBUG
65 * Decide if an outbound management frame should be
66 * printed when debugging is enabled. This filters some
67 * of the less interesting frames that come frequently
71 doprint(struct ieee80211com *ic, int subtype)
74 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
75 return (ic->ic_opmode == IEEE80211_M_IBSS);
82 * Set the direction field and address fields of an outgoing
83 * non-QoS frame. Note this should be called early on in
84 * constructing a frame as it sets i_fc[1]; other bits can
88 ieee80211_send_setup(struct ieee80211com *ic,
89 struct ieee80211_node *ni,
90 struct ieee80211_frame *wh,
92 const uint8_t sa[IEEE80211_ADDR_LEN],
93 const uint8_t da[IEEE80211_ADDR_LEN],
94 const uint8_t bssid[IEEE80211_ADDR_LEN])
96 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
98 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
99 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
100 switch (ic->ic_opmode) {
101 case IEEE80211_M_STA:
102 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
103 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
104 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
105 IEEE80211_ADDR_COPY(wh->i_addr3, da);
107 case IEEE80211_M_IBSS:
108 case IEEE80211_M_AHDEMO:
109 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
110 IEEE80211_ADDR_COPY(wh->i_addr1, da);
111 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
112 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
114 case IEEE80211_M_HOSTAP:
115 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
116 IEEE80211_ADDR_COPY(wh->i_addr1, da);
117 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
118 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
120 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
124 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
125 IEEE80211_ADDR_COPY(wh->i_addr1, da);
126 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
127 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
129 *(uint16_t *)&wh->i_dur[0] = 0;
130 /* NB: use non-QoS tid */
131 *(uint16_t *)&wh->i_seq[0] =
132 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
138 * Send a management frame to the specified node. The node pointer
139 * must have a reference as the pointer will be passed to the driver
140 * and potentially held for a long time. If the frame is successfully
141 * dispatched to the driver, then it is responsible for freeing the
142 * reference (and potentially free'ing up any associated storage).
145 ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
146 struct mbuf *m, int type, int timer)
148 struct ifnet *ifp = ic->ic_ifp;
149 struct ieee80211_frame *wh;
151 KASSERT(ni != NULL, ("null node"));
154 * Yech, hack alert! We want to pass the node down to the
155 * driver's start routine. If we don't do so then the start
156 * routine must immediately look it up again and that can
157 * cause a lock order reversal if, for example, this frame
158 * is being sent because the station is being timedout and
159 * the frame being sent is a DEAUTH message. We could stick
160 * this in an m_tag and tack that on to the mbuf. However
161 * that's rather expensive to do for every frame so instead
162 * we stuff it in the rcvif field since outbound frames do
163 * not (presently) use this.
165 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
168 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
169 m->m_pkthdr.rcvif = (void *)ni;
171 wh = mtod(m, struct ieee80211_frame *);
172 ieee80211_send_setup(ic, ni, wh,
173 IEEE80211_FC0_TYPE_MGT | type,
174 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
175 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
176 m->m_flags &= ~M_LINK0;
177 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
178 "[%6D] encrypting frame (%s)\n",
179 wh->i_addr1, ":", __func__);
180 wh->i_fc[1] |= IEEE80211_FC1_WEP;
182 #ifdef IEEE80211_DEBUG
183 /* avoid printing too many frames */
184 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
185 ieee80211_msg_dumppkts(ic)) {
186 printf("[%6D] send %s on channel %u\n",
188 ieee80211_mgt_subtype_name[
189 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
190 IEEE80211_FC0_SUBTYPE_SHIFT],
191 ieee80211_chan2ieee(ic, ic->ic_curchan));
194 IEEE80211_NODE_STAT(ni, tx_mgmt);
195 IF_ENQUEUE(&ic->ic_mgtq, m);
198 * Set the mgt frame timeout.
200 ic->ic_mgt_timer = timer;
208 * Send a null data frame to the specified node.
210 * NB: the caller is assumed to have setup a node reference
211 * for use; this is necessary to deal with a race condition
212 * when probing for inactive stations.
215 ieee80211_send_nulldata(struct ieee80211_node *ni)
217 struct ieee80211com *ic = ni->ni_ic;
218 struct ifnet *ifp = ic->ic_ifp;
220 struct ieee80211_frame *wh;
222 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
225 ic->ic_stats.is_tx_nobuf++;
226 ieee80211_unref_node(&ni);
229 m->m_pkthdr.rcvif = (void *) ni;
231 wh = mtod(m, struct ieee80211_frame *);
232 ieee80211_send_setup(ic, ni, wh,
233 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
234 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
235 /* NB: power management bit is never sent by an AP */
236 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
237 ic->ic_opmode != IEEE80211_M_HOSTAP)
238 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
239 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
241 IEEE80211_NODE_STAT(ni, tx_data);
243 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
244 "[%s] send null data frame on channel %u, pwr mgt %s\n",
246 ieee80211_chan2ieee(ic, ic->ic_curchan),
247 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
249 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */
255 * Assign priority to a frame based on any vlan tag assigned
256 * to the station and/or any Diffserv setting in an IP header.
257 * Finally, if an ACM policy is setup (in station mode) it's
261 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
263 int v_wme_ac = 0, d_wme_ac, ac;
265 struct ether_header *eh;
268 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
275 * If node has a vlan tag then all traffic
276 * to it must have a matching tag.
279 if (ni->ni_vlan != 0) {
280 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
282 IEEE80211_NODE_STAT(ni, tx_novlantag);
285 if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
286 EVL_VLANOFTAG(ni->ni_vlan)) {
287 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
290 /* map vlan priority to AC */
291 switch (EVL_PRIOFTAG(ni->ni_vlan)) {
294 v_wme_ac = WME_AC_BK;
298 v_wme_ac = WME_AC_BE;
302 v_wme_ac = WME_AC_VI;
306 v_wme_ac = WME_AC_VO;
310 #endif /* FREEBSD_VLAN */
313 eh = mtod(m, struct ether_header *);
314 if (eh->ether_type == htons(ETHERTYPE_IP)) {
315 const struct ip *ip = (struct ip *)
316 (mtod(m, uint8_t *) + sizeof (*eh));
318 * IP frame, map the TOS field.
320 switch (ip->ip_tos) {
323 d_wme_ac = WME_AC_BK; /* background */
327 d_wme_ac = WME_AC_VI; /* video */
329 case 0x30: /* voice */
331 case 0x88: /* XXX UPSD */
333 d_wme_ac = WME_AC_VO;
336 d_wme_ac = WME_AC_BE;
341 d_wme_ac = WME_AC_BE;
346 * Use highest priority AC.
348 if (v_wme_ac > d_wme_ac)
356 if (ic->ic_opmode == IEEE80211_M_STA) {
357 static const int acmap[4] = {
358 WME_AC_BK, /* WME_AC_BE */
359 WME_AC_BK, /* WME_AC_BK */
360 WME_AC_BE, /* WME_AC_VI */
361 WME_AC_VI, /* WME_AC_VO */
363 while (ac != WME_AC_BK &&
364 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
373 * Insure there is sufficient contiguous space to encapsulate the
374 * 802.11 data frame. If room isn't already there, arrange for it.
375 * Drivers and cipher modules assume we have done the necessary work
376 * and fail rudely if they don't find the space they need.
379 ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
380 struct ieee80211_key *key, struct mbuf *m)
382 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
383 int needed_space = hdrsize;
386 /* XXX belongs in crypto code? */
387 needed_space += key->wk_cipher->ic_header;
390 * When crypto is being done in the host we must insure
391 * the data are writable for the cipher routines; clone
392 * a writable mbuf chain.
393 * XXX handle SWMIC specially
395 if (key->wk_flags & (IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC)) {
396 m = ieee80211_mbuf_clone(m, MB_DONTWAIT);
398 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
399 "%s: cannot get writable mbuf\n", __func__);
400 ic->ic_stats.is_tx_nobuf++; /* XXX new stat */
406 * We know we are called just before stripping an Ethernet
407 * header and prepending an LLC header. This means we know
409 * sizeof(struct ether_header) - sizeof(struct llc)
410 * bytes recovered to which we need additional space for the
411 * 802.11 header and any crypto header.
413 /* XXX check trailing space and copy instead? */
414 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
415 struct mbuf *n = m_gethdr(MB_DONTWAIT, m->m_type);
417 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
418 "%s: cannot expand storage\n", __func__);
419 ic->ic_stats.is_tx_nobuf++;
423 KASSERT(needed_space <= MHLEN,
424 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
426 * Setup new mbuf to have leading space to prepend the
427 * 802.11 header and any crypto header bits that are
428 * required (the latter are added when the driver calls
429 * back to ieee80211_crypto_encap to do crypto encapsulation).
431 /* NB: must be first 'cuz it clobbers m_data */
433 n->m_len = 0; /* NB: m_gethdr does not set */
434 n->m_data += needed_space;
436 * Pull up Ethernet header to create the expected layout.
437 * We could use m_pullup but that's overkill (i.e. we don't
438 * need the actual data) and it cannot fail so do it inline
441 /* NB: struct ether_header is known to be contiguous */
442 n->m_len += sizeof(struct ether_header);
443 m->m_len -= sizeof(struct ether_header);
444 m->m_data += sizeof(struct ether_header);
446 * Replace the head of the chain.
452 #undef TO_BE_RECLAIMED
455 #define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none)
457 * Return the transmit key to use in sending a unicast frame.
458 * If a unicast key is set we use that. When no unicast key is set
459 * we fall back to the default transmit key.
461 static __inline struct ieee80211_key *
462 ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
464 if (KEY_UNDEFINED(ni->ni_ucastkey)) {
465 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
466 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
468 return &ic->ic_nw_keys[ic->ic_def_txkey];
470 return &ni->ni_ucastkey;
475 * Return the transmit key to use in sending a multicast frame.
476 * Multicast traffic always uses the group key which is installed as
477 * the default tx key.
479 static __inline struct ieee80211_key *
480 ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
482 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
483 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
485 return &ic->ic_nw_keys[ic->ic_def_txkey];
489 * Encapsulate an outbound data frame. The mbuf chain is updated.
490 * If an error is encountered NULL is returned. The caller is required
491 * to provide a node reference and pullup the ethernet header in the
495 ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
496 struct ieee80211_node *ni)
498 struct ether_header eh;
499 struct ieee80211_frame *wh;
500 struct ieee80211_key *key;
502 int hdrsize, datalen, addqos;
504 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
505 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
508 * Insure space for additional headers. First identify
509 * transmit key to use in calculating any buffer adjustments
510 * required. This is also used below to do privacy
511 * encapsulation work. Then calculate the 802.11 header
512 * size and any padding required by the driver.
514 * Note key may be NULL if we fall back to the default
515 * transmit key and that is not set. In that case the
516 * buffer may not be expanded as needed by the cipher
517 * routines, but they will/should discard it.
519 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
520 if (ic->ic_opmode == IEEE80211_M_STA ||
521 !IEEE80211_IS_MULTICAST(eh.ether_dhost))
522 key = ieee80211_crypto_getucastkey(ic, ni);
524 key = ieee80211_crypto_getmcastkey(ic, ni);
525 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
526 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
527 "[%6D] no default transmit key (%s) deftxkey %u\n",
528 eh.ether_dhost, ":", __func__,
530 ic->ic_stats.is_tx_nodefkey++;
534 /* XXX 4-address format */
536 * XXX Some ap's don't handle QoS-encapsulated EAPOL
537 * frames so suppress use. This may be an issue if other
538 * ap's require all data frames to be QoS-encapsulated
539 * once negotiated in which case we'll need to make this
542 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
543 eh.ether_type != htons(ETHERTYPE_PAE);
545 hdrsize = sizeof(struct ieee80211_qosframe);
547 hdrsize = sizeof(struct ieee80211_frame);
548 if (ic->ic_flags & IEEE80211_F_DATAPAD)
549 hdrsize = roundup(hdrsize, sizeof(uint32_t));
550 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
552 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
556 /* NB: this could be optimized because of ieee80211_mbuf_adjust */
557 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
558 llc = mtod(m, struct llc *);
559 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
560 llc->llc_control = LLC_UI;
561 llc->llc_snap.org_code[0] = 0;
562 llc->llc_snap.org_code[1] = 0;
563 llc->llc_snap.org_code[2] = 0;
564 llc->llc_snap.ether_type = eh.ether_type;
565 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
567 M_PREPEND(m, hdrsize, MB_DONTWAIT);
569 ic->ic_stats.is_tx_nobuf++;
572 wh = mtod(m, struct ieee80211_frame *);
573 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
574 *(uint16_t *)wh->i_dur = 0;
575 switch (ic->ic_opmode) {
576 case IEEE80211_M_STA:
577 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
578 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
579 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
580 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
582 case IEEE80211_M_IBSS:
583 case IEEE80211_M_AHDEMO:
584 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
585 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
586 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
588 * NB: always use the bssid from ic_bss as the
589 * neighbor's may be stale after an ibss merge
591 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
593 case IEEE80211_M_HOSTAP:
594 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
595 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
596 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
597 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
599 case IEEE80211_M_MONITOR:
602 if (m->m_flags & M_MORE_DATA)
603 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
605 struct ieee80211_qosframe *qwh =
606 (struct ieee80211_qosframe *) wh;
610 /* map from access class/queue to 11e header priorty value */
611 tid = WME_AC_TO_TID(ac);
612 qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
613 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
614 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
616 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
618 *(uint16_t *)wh->i_seq =
619 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
620 ni->ni_txseqs[tid]++;
622 *(uint16_t *)wh->i_seq =
623 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
628 * IEEE 802.1X: send EAPOL frames always in the clear.
629 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
631 if (eh.ether_type != htons(ETHERTYPE_PAE) ||
632 ((ic->ic_flags & IEEE80211_F_WPA) &&
633 (ic->ic_opmode == IEEE80211_M_STA ?
634 !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
635 wh->i_fc[1] |= IEEE80211_FC1_WEP;
636 /* XXX do fragmentation */
637 if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
638 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
639 "[%6D] enmic failed, discard frame\n",
640 eh.ether_dhost, ":");
641 ic->ic_stats.is_crypto_enmicfail++;
647 IEEE80211_NODE_STAT(ni, tx_data);
648 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
649 IEEE80211_NODE_STAT(ni, tx_mcast);
651 IEEE80211_NODE_STAT(ni, tx_ucast);
652 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
662 * Add a supported rates element id to a frame.
665 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
669 *frm++ = IEEE80211_ELEMID_RATES;
670 nrates = rs->rs_nrates;
671 if (nrates > IEEE80211_RATE_SIZE)
672 nrates = IEEE80211_RATE_SIZE;
674 memcpy(frm, rs->rs_rates, nrates);
679 * Add an extended supported rates element id to a frame.
682 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
685 * Add an extended supported rates element if operating in 11g mode.
687 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
688 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
689 *frm++ = IEEE80211_ELEMID_XRATES;
691 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
698 * Add an ssid elemet to a frame.
701 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
703 *frm++ = IEEE80211_ELEMID_SSID;
705 memcpy(frm, ssid, len);
710 * Add an erp element to a frame.
713 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
717 *frm++ = IEEE80211_ELEMID_ERP;
720 if (ic->ic_nonerpsta != 0)
721 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
722 if (ic->ic_flags & IEEE80211_F_USEPROT)
723 erp |= IEEE80211_ERP_USE_PROTECTION;
724 if (ic->ic_flags & IEEE80211_F_USEBARKER)
725 erp |= IEEE80211_ERP_LONG_PREAMBLE;
731 ieee80211_setup_wpa_ie(struct ieee80211com *ic, uint8_t *ie)
733 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2
734 #define ADDSHORT(frm, v) do { \
735 frm[0] = (v) & 0xff; \
739 #define ADDSELECTOR(frm, sel) do { \
740 memcpy(frm, sel, 4); \
743 static const uint8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
744 static const uint8_t cipher_suite[][4] = {
745 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */
746 { WPA_OUI_BYTES, WPA_CSE_TKIP },
747 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */
748 { WPA_OUI_BYTES, WPA_CSE_CCMP },
749 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
750 { WPA_OUI_BYTES, WPA_CSE_NULL },
752 static const uint8_t wep104_suite[4] =
753 { WPA_OUI_BYTES, WPA_CSE_WEP104 };
754 static const uint8_t key_mgt_unspec[4] =
755 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
756 static const uint8_t key_mgt_psk[4] =
757 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
758 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
762 *frm++ = IEEE80211_ELEMID_VENDOR;
763 *frm++ = 0; /* length filled in below */
764 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */
766 ADDSHORT(frm, WPA_VERSION);
768 /* XXX filter out CKIP */
770 /* multicast cipher */
771 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
772 rsn->rsn_mcastkeylen >= 13)
773 ADDSELECTOR(frm, wep104_suite);
775 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
777 /* unicast cipher list */
779 ADDSHORT(frm, 0); /* selector count */
780 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
782 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
784 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
786 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
789 /* authenticator selector list */
791 ADDSHORT(frm, 0); /* selector count */
792 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
794 ADDSELECTOR(frm, key_mgt_unspec);
796 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
798 ADDSELECTOR(frm, key_mgt_psk);
801 /* optional capabilities */
802 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
803 ADDSHORT(frm, rsn->rsn_caps);
805 /* calculate element length */
806 ie[1] = frm - ie - 2;
807 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
808 ("WPA IE too big, %u > %zu",
809 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
817 ieee80211_setup_rsn_ie(struct ieee80211com *ic, uint8_t *ie)
819 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac
820 #define ADDSHORT(frm, v) do { \
821 frm[0] = (v) & 0xff; \
825 #define ADDSELECTOR(frm, sel) do { \
826 memcpy(frm, sel, 4); \
829 static const uint8_t cipher_suite[][4] = {
830 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */
831 { RSN_OUI_BYTES, RSN_CSE_TKIP },
832 { RSN_OUI_BYTES, RSN_CSE_WRAP },
833 { RSN_OUI_BYTES, RSN_CSE_CCMP },
834 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
835 { RSN_OUI_BYTES, RSN_CSE_NULL },
837 static const uint8_t wep104_suite[4] =
838 { RSN_OUI_BYTES, RSN_CSE_WEP104 };
839 static const uint8_t key_mgt_unspec[4] =
840 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
841 static const uint8_t key_mgt_psk[4] =
842 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
843 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
847 *frm++ = IEEE80211_ELEMID_RSN;
848 *frm++ = 0; /* length filled in below */
849 ADDSHORT(frm, RSN_VERSION);
851 /* XXX filter out CKIP */
853 /* multicast cipher */
854 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
855 rsn->rsn_mcastkeylen >= 13)
856 ADDSELECTOR(frm, wep104_suite);
858 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
860 /* unicast cipher list */
862 ADDSHORT(frm, 0); /* selector count */
863 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
865 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
867 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
869 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
872 /* authenticator selector list */
874 ADDSHORT(frm, 0); /* selector count */
875 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
877 ADDSELECTOR(frm, key_mgt_unspec);
879 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
881 ADDSELECTOR(frm, key_mgt_psk);
884 /* optional capabilities */
885 ADDSHORT(frm, rsn->rsn_caps);
888 /* calculate element length */
889 ie[1] = frm - ie - 2;
890 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
891 ("RSN IE too big, %u > %zu",
892 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
900 * Add a WPA/RSN element to a frame.
903 ieee80211_add_wpa(uint8_t *frm, struct ieee80211com *ic)
906 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
907 if (ic->ic_flags & IEEE80211_F_WPA2)
908 frm = ieee80211_setup_rsn_ie(ic, frm);
909 if (ic->ic_flags & IEEE80211_F_WPA1)
910 frm = ieee80211_setup_wpa_ie(ic, frm);
914 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
916 * Add a WME information element to a frame.
919 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
921 static const struct ieee80211_wme_info info = {
922 .wme_id = IEEE80211_ELEMID_VENDOR,
923 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
924 .wme_oui = { WME_OUI_BYTES },
925 .wme_type = WME_OUI_TYPE,
926 .wme_subtype = WME_INFO_OUI_SUBTYPE,
927 .wme_version = WME_VERSION,
930 memcpy(frm, &info, sizeof(info));
931 return frm + sizeof(info);
935 * Add a WME parameters element to a frame.
938 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
940 #define SM(_v, _f) (((_v) << _f##_S) & _f)
941 #define ADDSHORT(frm, v) do { \
942 frm[0] = (v) & 0xff; \
946 /* NB: this works 'cuz a param has an info at the front */
947 static const struct ieee80211_wme_info param = {
948 .wme_id = IEEE80211_ELEMID_VENDOR,
949 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
950 .wme_oui = { WME_OUI_BYTES },
951 .wme_type = WME_OUI_TYPE,
952 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
953 .wme_version = WME_VERSION,
957 memcpy(frm, ¶m, sizeof(param));
958 frm += __offsetof(struct ieee80211_wme_info, wme_info);
959 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
960 *frm++ = 0; /* reserved field */
961 for (i = 0; i < WME_NUM_AC; i++) {
962 const struct wmeParams *ac =
963 &wme->wme_bssChanParams.cap_wmeParams[i];
964 *frm++ = SM(i, WME_PARAM_ACI)
965 | SM(ac->wmep_acm, WME_PARAM_ACM)
966 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
968 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
969 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
971 ADDSHORT(frm, ac->wmep_txopLimit);
980 * Send a probe request frame with the specified ssid
981 * and any optional information element data.
984 ieee80211_send_probereq(struct ieee80211_node *ni,
985 const uint8_t sa[IEEE80211_ADDR_LEN],
986 const uint8_t da[IEEE80211_ADDR_LEN],
987 const uint8_t bssid[IEEE80211_ADDR_LEN],
988 const uint8_t *ssid, size_t ssidlen,
989 const void *optie, size_t optielen)
991 struct ieee80211com *ic = ni->ni_ic;
992 struct ifnet *ifp = ic->ic_ifp;
993 enum ieee80211_phymode mode;
994 struct ieee80211_frame *wh;
999 * Hold a reference on the node so it doesn't go away until after
1000 * the xmit is complete all the way in the driver. On error we
1001 * will remove our reference.
1003 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1004 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1006 ni, ni->ni_macaddr, ":",
1007 ieee80211_node_refcnt(ni) + 1);
1008 ieee80211_ref_node(ni);
1011 * prreq frame format
1013 * [tlv] supported rates
1014 * [tlv] extended supported rates
1015 * [tlv] user-specified ie's
1017 m = ieee80211_getmgtframe(&frm,
1018 2 + IEEE80211_NWID_LEN
1019 + 2 + IEEE80211_RATE_SIZE
1020 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1021 + (optie != NULL ? optielen : 0)
1024 ic->ic_stats.is_tx_nobuf++;
1025 ieee80211_free_node(ni);
1029 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1030 mode = ieee80211_chan2mode(ic, ic->ic_curchan);
1031 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1032 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1034 if (optie != NULL) {
1035 memcpy(frm, optie, optielen);
1038 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1040 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1043 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1044 m->m_pkthdr.rcvif = (void *)ni;
1046 wh = mtod(m, struct ieee80211_frame *);
1047 ieee80211_send_setup(ic, ni, wh,
1048 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1050 /* XXX power management? */
1052 IEEE80211_NODE_STAT(ni, tx_probereq);
1053 IEEE80211_NODE_STAT(ni, tx_mgmt);
1055 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1056 "[%6D] send probe req on channel %u\n",
1058 ieee80211_chan2ieee(ic, ic->ic_curchan));
1060 IF_ENQUEUE(&ic->ic_mgtq, m);
1066 * Calculate capability information for mgt frames.
1069 getcapinfo(struct ieee80211com *ic, struct ieee80211_channel *chan)
1073 KASSERT(ic->ic_opmode != IEEE80211_M_STA, ("station mode"));
1075 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1076 capinfo = IEEE80211_CAPINFO_ESS;
1077 else if (ic->ic_opmode == IEEE80211_M_IBSS)
1078 capinfo = IEEE80211_CAPINFO_IBSS;
1081 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1082 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1083 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
1084 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1085 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1086 if (ic->ic_caps_ext & IEEE80211_CEXT_PBCC)
1087 capinfo |= IEEE80211_CAPINFO_PBCC;
1089 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1090 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1094 static struct mbuf *
1095 _ieee80211_probe_resp_alloc(struct ieee80211com *ic, struct ieee80211_node *ni)
1097 struct ieee80211_rateset *rs;
1104 * probe response frame format
1106 * [2] beacon interval
1107 * [2] cabability information
1109 * [tlv] supported rates
1110 * [tlv] parameter set (FH/DS)
1111 * [4] parameter set (IBSS)
1112 * [tlv] extended rate phy (ERP)
1113 * [tlv] extended supported rates
1115 * [tlv] WME (optional)
1118 pktlen = 8 /* time stamp */
1119 + sizeof(uint16_t) /* beacon interval */
1120 + sizeof(uint16_t) /* capabilities */
1121 + 2 + ni->ni_esslen /* ssid */
1122 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1123 + 2 + 5 /* max(5,1) */ /* DS/FH parameters */
1124 + 2 + 2 /* IBSS parameters */
1126 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1127 /* XXX !WPA1+WPA2 fits w/o a cluster */
1128 + (ic->ic_flags & IEEE80211_F_WPA ? /* WPA 1+2 */
1129 2*sizeof(struct ieee80211_ie_wpa) : 0)
1130 + sizeof(struct ieee80211_wme_param); /* WME */
1132 m = ieee80211_getmgtframe(&frm, pktlen);
1134 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1135 "%s: cannot get buf; size %u\n", __func__, pktlen);
1136 ic->ic_stats.is_tx_nobuf++;
1140 memset(frm, 0, 8); /* timestamp should be filled later */
1142 *(uint16_t *)frm = htole16(ni->ni_intval);
1144 capinfo = getcapinfo(ic, ni->ni_chan);
1145 *(uint16_t *)frm = htole16(capinfo);
1148 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1149 frm = ieee80211_add_rates(frm, rs);
1151 if (ic->ic_phytype == IEEE80211_T_FH) {
1152 *frm++ = IEEE80211_ELEMID_FHPARMS;
1154 *frm++ = ni->ni_fhdwell & 0x00ff;
1155 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1156 *frm++ = IEEE80211_FH_CHANSET(
1157 ieee80211_chan2ieee(ic, ni->ni_chan));
1158 *frm++ = IEEE80211_FH_CHANPAT(
1159 ieee80211_chan2ieee(ic, ni->ni_chan));
1160 *frm++ = ni->ni_fhindex;
1162 *frm++ = IEEE80211_ELEMID_DSPARMS;
1164 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1167 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1168 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1170 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1172 if (ic->ic_flags & IEEE80211_F_WPA)
1173 frm = ieee80211_add_wpa(frm, ic);
1174 if (ic->ic_curmode == IEEE80211_MODE_11G)
1175 frm = ieee80211_add_erp(frm, ic);
1176 frm = ieee80211_add_xrates(frm, rs);
1177 if (ic->ic_flags & IEEE80211_F_WME)
1178 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1179 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1180 KKASSERT(m->m_len <= pktlen);
1186 * Send a management frame. The node is for the destination (or ic_bss
1187 * when in station mode). Nodes other than ic_bss have their reference
1188 * count bumped to reflect our use for an indeterminant time.
1191 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1194 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1198 int has_challenge, is_shared_key, ret, timer, status;
1200 KASSERT(ni != NULL, ("null node"));
1203 * Hold a reference on the node so it doesn't go away until after
1204 * the xmit is complete all the way in the driver. On error we
1205 * will remove our reference.
1207 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1208 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1210 ni, ni->ni_macaddr, ":",
1211 ieee80211_node_refcnt(ni) + 1);
1212 ieee80211_ref_node(ni);
1216 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1218 * probe response frame format
1220 * [2] beacon interval
1221 * [2] cabability information
1223 * [tlv] supported rates
1224 * [tlv] parameter set (FH/DS)
1225 * [tlv] parameter set (IBSS)
1226 * [tlv] extended rate phy (ERP)
1227 * [tlv] extended supported rates
1229 * [tlv] WME (optional)
1231 m = ieee80211_getmgtframe(&frm,
1235 + 2 + IEEE80211_NWID_LEN
1236 + 2 + IEEE80211_RATE_SIZE
1240 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1241 /* XXX !WPA1+WPA2 fits w/o a cluster */
1242 + (ic->ic_flags & IEEE80211_F_WPA ?
1243 2*sizeof(struct ieee80211_ie_wpa) : 0)
1244 + sizeof(struct ieee80211_wme_param)
1247 senderr(ENOMEM, is_tx_nobuf);
1249 memset(frm, 0, 8); /* timestamp should be filled later */
1251 *(uint16_t *)frm = htole16(ic->ic_bss->ni_intval);
1253 capinfo = getcapinfo(ic, ic->ic_curchan);
1254 *(uint16_t *)frm = htole16(capinfo);
1257 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1258 ic->ic_bss->ni_esslen);
1259 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1261 if (ic->ic_phytype == IEEE80211_T_FH) {
1262 *frm++ = IEEE80211_ELEMID_FHPARMS;
1264 *frm++ = ni->ni_fhdwell & 0x00ff;
1265 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1266 *frm++ = IEEE80211_FH_CHANSET(
1267 ieee80211_chan2ieee(ic, ic->ic_curchan));
1268 *frm++ = IEEE80211_FH_CHANPAT(
1269 ieee80211_chan2ieee(ic, ic->ic_curchan));
1270 *frm++ = ni->ni_fhindex;
1272 *frm++ = IEEE80211_ELEMID_DSPARMS;
1274 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan);
1277 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1278 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1280 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1282 if (ic->ic_flags & IEEE80211_F_WPA)
1283 frm = ieee80211_add_wpa(frm, ic);
1284 if (ic->ic_curmode == IEEE80211_MODE_11G)
1285 frm = ieee80211_add_erp(frm, ic);
1286 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1287 if (ic->ic_flags & IEEE80211_F_WME)
1288 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1289 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1292 case IEEE80211_FC0_SUBTYPE_AUTH:
1295 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1296 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1297 ni->ni_challenge != NULL);
1300 * Deduce whether we're doing open authentication or
1301 * shared key authentication. We do the latter if
1302 * we're in the middle of a shared key authentication
1303 * handshake or if we're initiating an authentication
1304 * request and configured to use shared key.
1306 is_shared_key = has_challenge ||
1307 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1308 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1309 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1311 m = ieee80211_getmgtframe(&frm,
1312 3 * sizeof(uint16_t)
1313 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1314 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1317 senderr(ENOMEM, is_tx_nobuf);
1319 ((uint16_t *)frm)[0] =
1320 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1321 : htole16(IEEE80211_AUTH_ALG_OPEN);
1322 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1323 ((uint16_t *)frm)[2] = htole16(status);/* status */
1325 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1326 ((uint16_t *)frm)[3] =
1327 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1328 IEEE80211_ELEMID_CHALLENGE);
1329 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1330 IEEE80211_CHALLENGE_LEN);
1331 m->m_pkthdr.len = m->m_len =
1332 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1333 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1334 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1335 "[%6D] request encrypt frame (%s)\n",
1336 ni->ni_macaddr, ":", __func__);
1337 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1340 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1342 /* XXX not right for shared key */
1343 if (status == IEEE80211_STATUS_SUCCESS)
1344 IEEE80211_NODE_STAT(ni, tx_auth);
1346 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1348 if (ic->ic_opmode == IEEE80211_M_STA)
1349 timer = IEEE80211_TRANS_WAIT;
1352 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1353 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1354 "[%6D] send station deauthenticate (reason %d)\n",
1355 ni->ni_macaddr, ":", arg);
1356 m = ieee80211_getmgtframe(&frm, sizeof(uint16_t));
1358 senderr(ENOMEM, is_tx_nobuf);
1359 *(uint16_t *)frm = htole16(arg); /* reason */
1360 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1362 IEEE80211_NODE_STAT(ni, tx_deauth);
1363 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1365 ieee80211_node_unauthorize(ni); /* port closed */
1368 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1369 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1370 const struct ieee80211_rateset *rs;
1373 * asreq frame format
1374 * [2] capability information
1375 * [2] listen interval
1376 * [6*] current AP address (reassoc only)
1378 * [tlv] supported rates
1379 * [tlv] extended supported rates
1381 * [tlv] user-specified ie's
1383 m = ieee80211_getmgtframe(&frm,
1386 + IEEE80211_ADDR_LEN
1387 + 2 + IEEE80211_NWID_LEN
1388 + 2 + IEEE80211_RATE_SIZE
1389 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1390 + sizeof(struct ieee80211_wme_info)
1391 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1394 senderr(ENOMEM, is_tx_nobuf);
1396 KASSERT(ic->ic_opmode == IEEE80211_M_STA,
1397 ("wrong mode %u", ic->ic_opmode));
1398 capinfo = IEEE80211_CAPINFO_ESS;
1399 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1400 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1402 * NB: Some 11a AP's reject the request when
1403 * short premable or PBCC modulation is set.
1405 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
1406 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
1407 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1408 if (ic->ic_caps_ext & IEEE80211_CEXT_PBCC)
1409 capinfo |= IEEE80211_CAPINFO_PBCC;
1411 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1412 (ic->ic_caps & IEEE80211_C_SHSLOT))
1413 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1414 *(uint16_t *)frm = htole16(capinfo);
1417 *(uint16_t *)frm = htole16(ic->ic_lintval);
1420 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1421 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1422 frm += IEEE80211_ADDR_LEN;
1425 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1427 rs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
1428 frm = ieee80211_add_rates(frm, rs);
1429 frm = ieee80211_add_xrates(frm, rs);
1431 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1432 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1433 if (ic->ic_opt_ie != NULL) {
1434 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1435 frm += ic->ic_opt_ie_len;
1437 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1439 timer = IEEE80211_TRANS_WAIT;
1443 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1444 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1446 * asreq frame format
1447 * [2] capability information
1449 * [2] association ID
1450 * [tlv] supported rates
1451 * [tlv] extended supported rates
1452 * [tlv] WME (if enabled and STA enabled)
1454 m = ieee80211_getmgtframe(&frm,
1458 + 2 + IEEE80211_RATE_SIZE
1459 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1460 + sizeof(struct ieee80211_wme_param)
1463 senderr(ENOMEM, is_tx_nobuf);
1465 capinfo = getcapinfo(ic, ic->ic_curchan);
1466 *(uint16_t *)frm = htole16(capinfo);
1469 *(uint16_t *)frm = htole16(arg); /* status */
1472 if (arg == IEEE80211_STATUS_SUCCESS) {
1473 *(uint16_t *)frm = htole16(ni->ni_associd);
1474 IEEE80211_NODE_STAT(ni, tx_assoc);
1476 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1479 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1480 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1481 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1482 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1483 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1486 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1487 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1488 "[%6D] send station disassociate (reason %d)\n",
1489 ni->ni_macaddr, ":", arg);
1490 m = ieee80211_getmgtframe(&frm, sizeof(uint16_t));
1492 senderr(ENOMEM, is_tx_nobuf);
1493 *(uint16_t *)frm = htole16(arg); /* reason */
1494 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1496 IEEE80211_NODE_STAT(ni, tx_disassoc);
1497 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1501 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1502 "[%6D] invalid mgmt frame type %u\n",
1503 ni->ni_macaddr, ":", type);
1504 senderr(EINVAL, is_tx_unknownmgt);
1507 ret = ieee80211_mgmt_output(ic, ni, m, type, timer);
1510 ieee80211_free_node(ni);
1517 * Allocate a probe response frame and fillin the appropriate bits.
1520 ieee80211_probe_resp_alloc(struct ieee80211com *ic, struct ieee80211_node *ni)
1522 struct ieee80211_frame *wh;
1525 m = _ieee80211_probe_resp_alloc(ic, ni);
1529 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1530 KASSERT(m != NULL, ("no space for 802.11 header?"));
1532 wh = mtod(m, struct ieee80211_frame *);
1533 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1534 IEEE80211_FC0_SUBTYPE_PROBE_RESP;
1535 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1536 *(uint16_t *)wh->i_dur = 0;
1537 bzero(wh->i_addr1, sizeof(wh->i_addr1));
1538 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1539 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1540 *(uint16_t *)wh->i_seq = 0;
1546 * Allocate a beacon frame and fillin the appropriate bits.
1549 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1550 struct ieee80211_beacon_offsets *bo)
1552 struct ifnet *ifp = ic->ic_ifp;
1553 struct ieee80211_frame *wh;
1556 uint8_t *frm, *efrm;
1558 struct ieee80211_rateset *rs;
1561 * beacon frame format
1563 * [2] beacon interval
1564 * [2] cabability information
1566 * [tlv] supported rates
1567 * [3] parameter set (DS)
1568 * [tlv] parameter set (IBSS/TIM)
1569 * [tlv] extended rate phy (ERP)
1570 * [tlv] extended supported rates
1571 * [tlv] WME parameters
1572 * [tlv] WPA/RSN parameters
1573 * XXX Vendor-specific OIDs (e.g. Atheros)
1574 * NB: we allocate the max space required for the TIM bitmap.
1577 pktlen = 8 /* time stamp */
1578 + sizeof(uint16_t) /* beacon interval */
1579 + sizeof(uint16_t) /* capabilities */
1580 + 2 + ni->ni_esslen /* ssid */
1581 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1582 + 2 + 1 /* DS parameters */
1583 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */
1585 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1586 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */
1587 sizeof(struct ieee80211_wme_param) : 0)
1588 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
1589 2*sizeof(struct ieee80211_ie_wpa) : 0)
1591 m = ieee80211_getmgtframe(&frm, pktlen);
1593 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1594 "%s: cannot get buf; size %u\n", __func__, pktlen);
1595 ic->ic_stats.is_tx_nobuf++;
1599 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
1601 *(uint16_t *)frm = htole16(ni->ni_intval);
1603 capinfo = getcapinfo(ic, ni->ni_chan);
1604 bo->bo_caps = (uint16_t *)frm;
1605 *(uint16_t *)frm = htole16(capinfo);
1607 *frm++ = IEEE80211_ELEMID_SSID;
1608 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1609 *frm++ = ni->ni_esslen;
1610 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1611 frm += ni->ni_esslen;
1614 frm = ieee80211_add_rates(frm, rs);
1615 if (ic->ic_curmode != IEEE80211_MODE_FH) {
1616 *frm++ = IEEE80211_ELEMID_DSPARMS;
1618 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1621 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1622 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1624 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1626 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1627 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1629 tie->tim_ie = IEEE80211_ELEMID_TIM;
1630 tie->tim_len = 4; /* length */
1631 tie->tim_count = 0; /* DTIM count */
1632 tie->tim_period = ic->ic_dtim_period; /* DTIM period */
1633 tie->tim_bitctl = 0; /* bitmap control */
1634 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
1635 frm += sizeof(struct ieee80211_tim_ie);
1638 bo->bo_trailer = frm;
1639 if (ic->ic_flags & IEEE80211_F_WME) {
1641 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1642 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1644 if (ic->ic_flags & IEEE80211_F_WPA)
1645 frm = ieee80211_add_wpa(frm, ic);
1646 if (ic->ic_curmode == IEEE80211_MODE_11G) {
1648 frm = ieee80211_add_erp(frm, ic);
1650 efrm = ieee80211_add_xrates(frm, rs);
1651 bo->bo_trailer_len = efrm - bo->bo_trailer;
1652 m->m_pkthdr.len = m->m_len = efrm - mtod(m, uint8_t *);
1653 KKASSERT(m->m_len <= pktlen);
1655 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1656 KASSERT(m != NULL, ("no space for 802.11 header?"));
1657 wh = mtod(m, struct ieee80211_frame *);
1658 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1659 IEEE80211_FC0_SUBTYPE_BEACON;
1660 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1661 *(uint16_t *)wh->i_dur = 0;
1662 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1663 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1664 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1665 *(uint16_t *)wh->i_seq = 0;
1671 * Update the dynamic parts of a beacon frame based on the current state.
1674 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1675 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1677 int len_changed = 0;
1680 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1682 /* XXX faster to recalculate entirely or just changes? */
1683 capinfo = getcapinfo(ic, ni->ni_chan);
1684 *bo->bo_caps = htole16(capinfo);
1686 if (ic->ic_flags & IEEE80211_F_WME) {
1687 struct ieee80211_wme_state *wme = &ic->ic_wme;
1690 * Check for agressive mode change. When there is
1691 * significant high priority traffic in the BSS
1692 * throttle back BE traffic by using conservative
1693 * parameters. Otherwise BE uses agressive params
1694 * to optimize performance of legacy/non-QoS traffic.
1696 if (wme->wme_flags & WME_F_AGGRMODE) {
1697 if (wme->wme_hipri_traffic >
1698 wme->wme_hipri_switch_thresh) {
1699 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1700 "%s: traffic %u, disable aggressive mode\n",
1701 __func__, wme->wme_hipri_traffic);
1702 wme->wme_flags &= ~WME_F_AGGRMODE;
1703 ieee80211_wme_updateparams(ic);
1704 wme->wme_hipri_traffic =
1705 wme->wme_hipri_switch_hysteresis;
1707 wme->wme_hipri_traffic = 0;
1709 if (wme->wme_hipri_traffic <=
1710 wme->wme_hipri_switch_thresh) {
1711 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1712 "%s: traffic %u, enable aggressive mode\n",
1713 __func__, wme->wme_hipri_traffic);
1714 wme->wme_flags |= WME_F_AGGRMODE;
1715 ieee80211_wme_updateparams(ic);
1716 wme->wme_hipri_traffic = 0;
1718 wme->wme_hipri_traffic =
1719 wme->wme_hipri_switch_hysteresis;
1721 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1722 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
1723 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1727 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/
1728 struct ieee80211_tim_ie *tie =
1729 (struct ieee80211_tim_ie *) bo->bo_tim;
1730 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1731 u_int timlen, timoff, i;
1733 * ATIM/DTIM needs updating. If it fits in the
1734 * current space allocated then just copy in the
1735 * new bits. Otherwise we need to move any trailing
1736 * data to make room. Note that we know there is
1737 * contiguous space because ieee80211_beacon_allocate
1738 * insures there is space in the mbuf to write a
1739 * maximal-size virtual bitmap (based on ic_max_aid).
1742 * Calculate the bitmap size and offset, copy any
1743 * trailer out of the way, and then copy in the
1744 * new bitmap and update the information element.
1745 * Note that the tim bitmap must contain at least
1746 * one byte and any offset must be even.
1748 if (ic->ic_ps_pending != 0) {
1749 timoff = 128; /* impossibly large */
1750 for (i = 0; i < ic->ic_tim_len; i++)
1751 if (ic->ic_tim_bitmap[i]) {
1755 KASSERT(timoff != 128, ("tim bitmap empty!"));
1756 for (i = ic->ic_tim_len-1; i >= timoff; i--)
1757 if (ic->ic_tim_bitmap[i])
1759 timlen = 1 + (i - timoff);
1764 if (timlen != bo->bo_tim_len) {
1765 /* copy up/down trailer */
1766 int adjust = tie->tim_bitmap+timlen
1768 ovbcopy(bo->bo_trailer, bo->bo_trailer+adjust,
1769 bo->bo_trailer_len);
1770 bo->bo_trailer += adjust;
1771 bo->bo_wme += adjust;
1772 bo->bo_erp += adjust;
1773 bo->bo_tim_len = timlen;
1775 /* update information element */
1776 tie->tim_len = 3 + timlen;
1777 tie->tim_bitctl = timoff;
1780 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1783 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1785 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1786 "%s: TIM updated, pending %u, off %u, len %u\n",
1787 __func__, ic->ic_ps_pending, timoff, timlen);
1789 /* count down DTIM period */
1790 if (tie->tim_count == 0)
1791 tie->tim_count = tie->tim_period - 1;
1794 /* update state for buffered multicast frames on DTIM */
1795 if (mcast && tie->tim_count == 0)
1796 tie->tim_bitctl |= 1;
1798 tie->tim_bitctl &= ~1;
1799 if (ic->ic_flags_ext & IEEE80211_FEXT_ERPUPDATE) {
1801 * ERP element needs updating.
1803 (void) ieee80211_add_erp(bo->bo_erp, ic);
1804 ic->ic_flags_ext &= ~IEEE80211_FEXT_ERPUPDATE;
1812 * Save an outbound packet for a node in power-save sleep state.
1813 * The new packet is placed on the node's saved queue, and the TIM
1814 * is changed, if necessary.
1817 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1822 ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
1824 if (IF_QFULL(&ni->ni_savedq)) {
1825 IF_DROP(&ni->ni_savedq);
1826 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1827 "[%6D] pwr save q overflow, drops %d (size %d)\n",
1828 ni->ni_macaddr, ":",
1829 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1830 #ifdef IEEE80211_DEBUG
1831 if (ieee80211_msg_dumppkts(ic))
1832 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1838 * Tag the frame with it's expiry time and insert
1839 * it in the queue. The aging interval is 4 times
1840 * the listen interval specified by the station.
1841 * Frames that sit around too long are reclaimed
1842 * using this information.
1844 /* XXX handle overflow? */
1845 age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
1846 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1848 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1849 "[%6D] save frame with age %d, %u now queued\n",
1850 ni->ni_macaddr, ":", age, qlen);
1853 ic->ic_set_tim(ni, 1);