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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_vlan_var.h>
53 #include <net80211/ieee80211_var.h>
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
62 #ifdef IEEE80211_DEBUG
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
70 doprint(struct ieee80211com *ic, int subtype)
73 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
74 return (ic->ic_opmode == IEEE80211_M_IBSS);
81 * Set the direction field and address fields of an outgoing
82 * non-QoS frame. Note this should be called early on in
83 * constructing a frame as it sets i_fc[1]; other bits can
87 ieee80211_send_setup(struct ieee80211com *ic,
88 struct ieee80211_node *ni,
89 struct ieee80211_frame *wh,
91 const u_int8_t sa[IEEE80211_ADDR_LEN],
92 const u_int8_t da[IEEE80211_ADDR_LEN],
93 const u_int8_t bssid[IEEE80211_ADDR_LEN])
95 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
97 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
98 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
99 switch (ic->ic_opmode) {
100 case IEEE80211_M_STA:
101 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
102 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
103 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
104 IEEE80211_ADDR_COPY(wh->i_addr3, da);
106 case IEEE80211_M_IBSS:
107 case IEEE80211_M_AHDEMO:
108 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
109 IEEE80211_ADDR_COPY(wh->i_addr1, da);
110 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
111 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
113 case IEEE80211_M_HOSTAP:
114 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
115 IEEE80211_ADDR_COPY(wh->i_addr1, da);
116 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
117 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
119 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
123 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
124 IEEE80211_ADDR_COPY(wh->i_addr1, da);
125 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
126 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
128 *(u_int16_t *)&wh->i_dur[0] = 0;
129 /* NB: use non-QoS tid */
130 *(u_int16_t *)&wh->i_seq[0] =
131 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
137 * Send a management frame to the specified node. The node pointer
138 * must have a reference as the pointer will be passed to the driver
139 * and potentially held for a long time. If the frame is successfully
140 * dispatched to the driver, then it is responsible for freeing the
141 * reference (and potentially free'ing up any associated storage).
144 ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
145 struct mbuf *m, int type, int timer)
147 struct ifnet *ifp = ic->ic_ifp;
148 struct ieee80211_frame *wh;
150 KASSERT(ni != NULL, ("null node"));
153 * Yech, hack alert! We want to pass the node down to the
154 * driver's start routine. If we don't do so then the start
155 * routine must immediately look it up again and that can
156 * cause a lock order reversal if, for example, this frame
157 * is being sent because the station is being timedout and
158 * the frame being sent is a DEAUTH message. We could stick
159 * this in an m_tag and tack that on to the mbuf. However
160 * that's rather expensive to do for every frame so instead
161 * we stuff it in the rcvif field since outbound frames do
162 * not (presently) use this.
164 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
167 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
168 m->m_pkthdr.rcvif = (void *)ni;
170 wh = mtod(m, struct ieee80211_frame *);
171 ieee80211_send_setup(ic, ni, wh,
172 IEEE80211_FC0_TYPE_MGT | type,
173 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
174 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
175 m->m_flags &= ~M_LINK0;
176 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
177 "[%s] encrypting frame (%s)\n",
178 ether_sprintf(wh->i_addr1), __func__);
179 wh->i_fc[1] |= IEEE80211_FC1_WEP;
181 #ifdef IEEE80211_DEBUG
182 /* avoid printing too many frames */
183 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
184 ieee80211_msg_dumppkts(ic)) {
185 printf("[%s] send %s on channel %u\n",
186 ether_sprintf(wh->i_addr1),
187 ieee80211_mgt_subtype_name[
188 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
189 IEEE80211_FC0_SUBTYPE_SHIFT],
190 ieee80211_chan2ieee(ic, ic->ic_curchan));
193 IEEE80211_NODE_STAT(ni, tx_mgmt);
194 IF_ENQUEUE(&ic->ic_mgtq, m);
197 * Set the mgt frame timeout.
199 ic->ic_mgt_timer = timer;
207 * Raw packet transmit stub for legacy drivers.
208 * Send the packet through the mgt q so we bypass
209 * the normal encapsulation work.
212 ieee80211_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
213 const struct ieee80211_bpf_params *params)
215 struct ieee80211com *ic = ni->ni_ic;
216 struct ifnet *ifp = ic->ic_ifp;
218 m->m_pkthdr.rcvif = (void *) ni;
219 IF_ENQUEUE(&ic->ic_mgtq, m);
227 * 802.11 output routine. This is (currently) used only to
228 * connect bpf write calls to the 802.11 layer for injecting
229 * raw 802.11 frames. Note we locate the ieee80211com from
230 * the ifnet using a spare field setup at attach time. This
231 * will go away when the virtual ap support comes in.
234 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
235 struct sockaddr *dst, struct rtentry *rt0)
237 #define senderr(e) do { error = (e); goto bad;} while (0)
238 struct ieee80211com *ic = ifp->if_spare2; /* XXX */
239 struct ieee80211_node *ni = NULL;
240 struct ieee80211_frame *wh;
244 * Hand to the 802.3 code if not tagged as
245 * a raw 802.11 frame.
247 if (dst->sa_family != AF_IEEE80211)
248 return ether_output(ifp, m, dst, rt0);
250 error = mac_check_ifnet_transmit(ifp, m);
254 if (ifp->if_flags & IFF_MONITOR)
256 if ((ifp->if_flags & IFF_UP) == 0)
259 /* XXX bypass bridge, pfil, carp, etc. */
261 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
262 senderr(EIO); /* XXX */
263 wh = mtod(m, struct ieee80211_frame *);
264 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
265 IEEE80211_FC0_VERSION_0)
266 senderr(EIO); /* XXX */
268 /* locate destination node */
269 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
270 case IEEE80211_FC1_DIR_NODS:
271 case IEEE80211_FC1_DIR_FROMDS:
272 ni = ieee80211_find_txnode(ic, wh->i_addr1);
274 case IEEE80211_FC1_DIR_TODS:
275 case IEEE80211_FC1_DIR_DSTODS:
276 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
277 senderr(EIO); /* XXX */
278 ni = ieee80211_find_txnode(ic, wh->i_addr3);
281 senderr(EIO); /* XXX */
285 * Permit packets w/ bpf params through regardless
286 * (see below about sa_len).
288 if (dst->sa_len == 0)
289 senderr(EHOSTUNREACH);
290 ni = ieee80211_ref_node(ic->ic_bss);
293 /* XXX ctrl frames should go through */
294 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
295 (m->m_flags & M_PWR_SAV) == 0) {
297 * Station in power save mode; pass the frame
298 * to the 802.11 layer and continue. We'll get
299 * the frame back when the time is right.
301 ieee80211_pwrsave(ic, ni, m);
306 /* calculate priority so drivers can find the tx queue */
307 /* XXX assumes an 802.3 frame */
308 if (ieee80211_classify(ic, m, ni))
309 senderr(EIO); /* XXX */
313 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
314 * present by setting the sa_len field of the sockaddr (yes,
316 * NB: we assume sa_data is suitably aligned to cast.
318 return ic->ic_raw_xmit(ni, m, (const struct ieee80211_bpf_params *)
319 (dst->sa_len ? dst->sa_data : NULL));
325 ieee80211_free_node(ni);
331 * Send a null data frame to the specified node.
333 * NB: the caller is assumed to have setup a node reference
334 * for use; this is necessary to deal with a race condition
335 * when probing for inactive stations.
338 ieee80211_send_nulldata(struct ieee80211_node *ni)
340 struct ieee80211com *ic = ni->ni_ic;
341 struct ifnet *ifp = ic->ic_ifp;
343 struct ieee80211_frame *wh;
345 MGETHDR(m, M_NOWAIT, MT_DATA);
348 ic->ic_stats.is_tx_nobuf++;
349 ieee80211_unref_node(&ni);
352 m->m_pkthdr.rcvif = (void *) ni;
354 wh = mtod(m, struct ieee80211_frame *);
355 ieee80211_send_setup(ic, ni, wh,
356 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
357 ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
358 /* NB: power management bit is never sent by an AP */
359 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
360 ic->ic_opmode != IEEE80211_M_HOSTAP)
361 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
362 m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
364 IEEE80211_NODE_STAT(ni, tx_data);
366 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
367 "[%s] send null data frame on channel %u, pwr mgt %s\n",
368 ether_sprintf(ni->ni_macaddr),
369 ieee80211_chan2ieee(ic, ic->ic_curchan),
370 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
372 IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */
379 * Assign priority to a frame based on any vlan tag assigned
380 * to the station and/or any Diffserv setting in an IP header.
381 * Finally, if an ACM policy is setup (in station mode) it's
385 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
387 int v_wme_ac, d_wme_ac, ac;
389 struct ether_header *eh;
392 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
398 * If node has a vlan tag then all traffic
399 * to it must have a matching tag.
402 if (ni->ni_vlan != 0) {
403 if ((m->m_flags & M_VLANTAG) == 0) {
404 IEEE80211_NODE_STAT(ni, tx_novlantag);
407 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
408 EVL_VLANOFTAG(ni->ni_vlan)) {
409 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
412 /* map vlan priority to AC */
413 switch (EVL_PRIOFTAG(ni->ni_vlan)) {
416 v_wme_ac = WME_AC_BK;
420 v_wme_ac = WME_AC_BE;
424 v_wme_ac = WME_AC_VI;
428 v_wme_ac = WME_AC_VO;
434 eh = mtod(m, struct ether_header *);
435 if (eh->ether_type == htons(ETHERTYPE_IP)) {
436 const struct ip *ip = (struct ip *)
437 (mtod(m, u_int8_t *) + sizeof (*eh));
439 * IP frame, map the TOS field.
441 switch (ip->ip_tos) {
444 d_wme_ac = WME_AC_BK; /* background */
448 d_wme_ac = WME_AC_VI; /* video */
450 case 0x30: /* voice */
452 case 0x88: /* XXX UPSD */
454 d_wme_ac = WME_AC_VO;
457 d_wme_ac = WME_AC_BE;
462 d_wme_ac = WME_AC_BE;
467 * Use highest priority AC.
469 if (v_wme_ac > d_wme_ac)
477 if (ic->ic_opmode == IEEE80211_M_STA) {
478 static const int acmap[4] = {
479 WME_AC_BK, /* WME_AC_BE */
480 WME_AC_BK, /* WME_AC_BK */
481 WME_AC_BE, /* WME_AC_VI */
482 WME_AC_VI, /* WME_AC_VO */
484 while (ac != WME_AC_BK &&
485 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
494 * Insure there is sufficient contiguous space to encapsulate the
495 * 802.11 data frame. If room isn't already there, arrange for it.
496 * Drivers and cipher modules assume we have done the necessary work
497 * and fail rudely if they don't find the space they need.
500 ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
501 struct ieee80211_key *key, struct mbuf *m)
503 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
504 int needed_space = hdrsize;
507 /* XXX belongs in crypto code? */
508 needed_space += key->wk_cipher->ic_header;
511 * When crypto is being done in the host we must insure
512 * the data are writable for the cipher routines; clone
513 * a writable mbuf chain.
514 * XXX handle SWMIC specially
516 if (key->wk_flags & (IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC)) {
517 m = m_unshare(m, M_NOWAIT);
519 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
520 "%s: cannot get writable mbuf\n", __func__);
521 ic->ic_stats.is_tx_nobuf++; /* XXX new stat */
527 * We know we are called just before stripping an Ethernet
528 * header and prepending an LLC header. This means we know
530 * sizeof(struct ether_header) - sizeof(struct llc)
531 * bytes recovered to which we need additional space for the
532 * 802.11 header and any crypto header.
534 /* XXX check trailing space and copy instead? */
535 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
536 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
538 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
539 "%s: cannot expand storage\n", __func__);
540 ic->ic_stats.is_tx_nobuf++;
544 KASSERT(needed_space <= MHLEN,
545 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
547 * Setup new mbuf to have leading space to prepend the
548 * 802.11 header and any crypto header bits that are
549 * required (the latter are added when the driver calls
550 * back to ieee80211_crypto_encap to do crypto encapsulation).
552 /* NB: must be first 'cuz it clobbers m_data */
554 n->m_len = 0; /* NB: m_gethdr does not set */
555 n->m_data += needed_space;
557 * Pull up Ethernet header to create the expected layout.
558 * We could use m_pullup but that's overkill (i.e. we don't
559 * need the actual data) and it cannot fail so do it inline
562 /* NB: struct ether_header is known to be contiguous */
563 n->m_len += sizeof(struct ether_header);
564 m->m_len -= sizeof(struct ether_header);
565 m->m_data += sizeof(struct ether_header);
567 * Replace the head of the chain.
573 #undef TO_BE_RECLAIMED
576 #define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none)
578 * Return the transmit key to use in sending a unicast frame.
579 * If a unicast key is set we use that. When no unicast key is set
580 * we fall back to the default transmit key.
582 static __inline struct ieee80211_key *
583 ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
585 if (KEY_UNDEFINED(ni->ni_ucastkey)) {
586 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
587 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
589 return &ic->ic_nw_keys[ic->ic_def_txkey];
591 return &ni->ni_ucastkey;
596 * Return the transmit key to use in sending a multicast frame.
597 * Multicast traffic always uses the group key which is installed as
598 * the default tx key.
600 static __inline struct ieee80211_key *
601 ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
603 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
604 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
606 return &ic->ic_nw_keys[ic->ic_def_txkey];
610 * Encapsulate an outbound data frame. The mbuf chain is updated.
611 * If an error is encountered NULL is returned. The caller is required
612 * to provide a node reference and pullup the ethernet header in the
616 ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
617 struct ieee80211_node *ni)
619 struct ether_header eh;
620 struct ieee80211_frame *wh;
621 struct ieee80211_key *key;
623 int hdrsize, datalen, addqos;
625 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
626 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
629 * Insure space for additional headers. First identify
630 * transmit key to use in calculating any buffer adjustments
631 * required. This is also used below to do privacy
632 * encapsulation work. Then calculate the 802.11 header
633 * size and any padding required by the driver.
635 * Note key may be NULL if we fall back to the default
636 * transmit key and that is not set. In that case the
637 * buffer may not be expanded as needed by the cipher
638 * routines, but they will/should discard it.
640 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
641 if (ic->ic_opmode == IEEE80211_M_STA ||
642 !IEEE80211_IS_MULTICAST(eh.ether_dhost))
643 key = ieee80211_crypto_getucastkey(ic, ni);
645 key = ieee80211_crypto_getmcastkey(ic, ni);
646 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
647 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
648 "[%s] no default transmit key (%s) deftxkey %u\n",
649 ether_sprintf(eh.ether_dhost), __func__,
651 ic->ic_stats.is_tx_nodefkey++;
655 /* XXX 4-address format */
657 * XXX Some ap's don't handle QoS-encapsulated EAPOL
658 * frames so suppress use. This may be an issue if other
659 * ap's require all data frames to be QoS-encapsulated
660 * once negotiated in which case we'll need to make this
663 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
664 eh.ether_type != htons(ETHERTYPE_PAE);
666 hdrsize = sizeof(struct ieee80211_qosframe);
668 hdrsize = sizeof(struct ieee80211_frame);
669 if (ic->ic_flags & IEEE80211_F_DATAPAD)
670 hdrsize = roundup(hdrsize, sizeof(u_int32_t));
671 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
673 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
677 /* NB: this could be optimized because of ieee80211_mbuf_adjust */
678 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
679 llc = mtod(m, struct llc *);
680 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
681 llc->llc_control = LLC_UI;
682 llc->llc_snap.org_code[0] = 0;
683 llc->llc_snap.org_code[1] = 0;
684 llc->llc_snap.org_code[2] = 0;
685 llc->llc_snap.ether_type = eh.ether_type;
686 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
688 M_PREPEND(m, hdrsize, M_DONTWAIT);
690 ic->ic_stats.is_tx_nobuf++;
693 wh = mtod(m, struct ieee80211_frame *);
694 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
695 *(u_int16_t *)wh->i_dur = 0;
696 switch (ic->ic_opmode) {
697 case IEEE80211_M_STA:
698 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
699 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
700 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
701 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
703 case IEEE80211_M_IBSS:
704 case IEEE80211_M_AHDEMO:
705 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
706 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
707 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
709 * NB: always use the bssid from ic_bss as the
710 * neighbor's may be stale after an ibss merge
712 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
714 case IEEE80211_M_HOSTAP:
715 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
716 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
717 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
718 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
720 case IEEE80211_M_MONITOR:
723 if (m->m_flags & M_MORE_DATA)
724 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
726 struct ieee80211_qosframe *qwh =
727 (struct ieee80211_qosframe *) wh;
731 /* map from access class/queue to 11e header priorty value */
732 tid = WME_AC_TO_TID(ac);
733 qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
734 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
735 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
737 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
739 *(u_int16_t *)wh->i_seq =
740 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
741 ni->ni_txseqs[tid]++;
743 *(u_int16_t *)wh->i_seq =
744 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
749 * IEEE 802.1X: send EAPOL frames always in the clear.
750 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
752 if (eh.ether_type != htons(ETHERTYPE_PAE) ||
753 ((ic->ic_flags & IEEE80211_F_WPA) &&
754 (ic->ic_opmode == IEEE80211_M_STA ?
755 !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
756 wh->i_fc[1] |= IEEE80211_FC1_WEP;
757 /* XXX do fragmentation */
758 if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
759 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
760 "[%s] enmic failed, discard frame\n",
761 ether_sprintf(eh.ether_dhost));
762 ic->ic_stats.is_crypto_enmicfail++;
768 IEEE80211_NODE_STAT(ni, tx_data);
769 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
770 IEEE80211_NODE_STAT(ni, tx_mcast);
772 IEEE80211_NODE_STAT(ni, tx_ucast);
773 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
783 * Add a supported rates element id to a frame.
786 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
790 *frm++ = IEEE80211_ELEMID_RATES;
791 nrates = rs->rs_nrates;
792 if (nrates > IEEE80211_RATE_SIZE)
793 nrates = IEEE80211_RATE_SIZE;
795 memcpy(frm, rs->rs_rates, nrates);
800 * Add an extended supported rates element id to a frame.
803 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
806 * Add an extended supported rates element if operating in 11g mode.
808 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
809 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
810 *frm++ = IEEE80211_ELEMID_XRATES;
812 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
819 * Add an ssid elemet to a frame.
822 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
824 *frm++ = IEEE80211_ELEMID_SSID;
826 memcpy(frm, ssid, len);
831 * Add an erp element to a frame.
834 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
838 *frm++ = IEEE80211_ELEMID_ERP;
841 if (ic->ic_nonerpsta != 0)
842 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
843 if (ic->ic_flags & IEEE80211_F_USEPROT)
844 erp |= IEEE80211_ERP_USE_PROTECTION;
845 if (ic->ic_flags & IEEE80211_F_USEBARKER)
846 erp |= IEEE80211_ERP_LONG_PREAMBLE;
852 ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
854 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2
855 #define ADDSHORT(frm, v) do { \
856 frm[0] = (v) & 0xff; \
860 #define ADDSELECTOR(frm, sel) do { \
861 memcpy(frm, sel, 4); \
864 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
865 static const u_int8_t cipher_suite[][4] = {
866 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */
867 { WPA_OUI_BYTES, WPA_CSE_TKIP },
868 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */
869 { WPA_OUI_BYTES, WPA_CSE_CCMP },
870 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
871 { WPA_OUI_BYTES, WPA_CSE_NULL },
873 static const u_int8_t wep104_suite[4] =
874 { WPA_OUI_BYTES, WPA_CSE_WEP104 };
875 static const u_int8_t key_mgt_unspec[4] =
876 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
877 static const u_int8_t key_mgt_psk[4] =
878 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
879 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
883 *frm++ = IEEE80211_ELEMID_VENDOR;
884 *frm++ = 0; /* length filled in below */
885 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */
887 ADDSHORT(frm, WPA_VERSION);
889 /* XXX filter out CKIP */
891 /* multicast cipher */
892 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
893 rsn->rsn_mcastkeylen >= 13)
894 ADDSELECTOR(frm, wep104_suite);
896 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
898 /* unicast cipher list */
900 ADDSHORT(frm, 0); /* selector count */
901 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
903 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
905 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
907 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
910 /* authenticator selector list */
912 ADDSHORT(frm, 0); /* selector count */
913 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
915 ADDSELECTOR(frm, key_mgt_unspec);
917 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
919 ADDSELECTOR(frm, key_mgt_psk);
922 /* optional capabilities */
923 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
924 ADDSHORT(frm, rsn->rsn_caps);
926 /* calculate element length */
927 ie[1] = frm - ie - 2;
928 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
929 ("WPA IE too big, %u > %zu",
930 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
938 ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
940 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac
941 #define ADDSHORT(frm, v) do { \
942 frm[0] = (v) & 0xff; \
946 #define ADDSELECTOR(frm, sel) do { \
947 memcpy(frm, sel, 4); \
950 static const u_int8_t cipher_suite[][4] = {
951 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */
952 { RSN_OUI_BYTES, RSN_CSE_TKIP },
953 { RSN_OUI_BYTES, RSN_CSE_WRAP },
954 { RSN_OUI_BYTES, RSN_CSE_CCMP },
955 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
956 { RSN_OUI_BYTES, RSN_CSE_NULL },
958 static const u_int8_t wep104_suite[4] =
959 { RSN_OUI_BYTES, RSN_CSE_WEP104 };
960 static const u_int8_t key_mgt_unspec[4] =
961 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
962 static const u_int8_t key_mgt_psk[4] =
963 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
964 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
968 *frm++ = IEEE80211_ELEMID_RSN;
969 *frm++ = 0; /* length filled in below */
970 ADDSHORT(frm, RSN_VERSION);
972 /* XXX filter out CKIP */
974 /* multicast cipher */
975 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
976 rsn->rsn_mcastkeylen >= 13)
977 ADDSELECTOR(frm, wep104_suite);
979 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
981 /* unicast cipher list */
983 ADDSHORT(frm, 0); /* selector count */
984 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
986 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
988 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
990 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
993 /* authenticator selector list */
995 ADDSHORT(frm, 0); /* selector count */
996 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
998 ADDSELECTOR(frm, key_mgt_unspec);
1000 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
1002 ADDSELECTOR(frm, key_mgt_psk);
1005 /* optional capabilities */
1006 ADDSHORT(frm, rsn->rsn_caps);
1009 /* calculate element length */
1010 ie[1] = frm - ie - 2;
1011 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
1012 ("RSN IE too big, %u > %zu",
1013 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
1017 #undef RSN_OUI_BYTES
1021 * Add a WPA/RSN element to a frame.
1024 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
1027 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
1028 if (ic->ic_flags & IEEE80211_F_WPA2)
1029 frm = ieee80211_setup_rsn_ie(ic, frm);
1030 if (ic->ic_flags & IEEE80211_F_WPA1)
1031 frm = ieee80211_setup_wpa_ie(ic, frm);
1035 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1037 * Add a WME information element to a frame.
1040 ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
1042 static const struct ieee80211_wme_info info = {
1043 .wme_id = IEEE80211_ELEMID_VENDOR,
1044 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1045 .wme_oui = { WME_OUI_BYTES },
1046 .wme_type = WME_OUI_TYPE,
1047 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1048 .wme_version = WME_VERSION,
1051 memcpy(frm, &info, sizeof(info));
1052 return frm + sizeof(info);
1056 * Add a WME parameters element to a frame.
1059 ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
1061 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1062 #define ADDSHORT(frm, v) do { \
1063 frm[0] = (v) & 0xff; \
1064 frm[1] = (v) >> 8; \
1067 /* NB: this works 'cuz a param has an info at the front */
1068 static const struct ieee80211_wme_info param = {
1069 .wme_id = IEEE80211_ELEMID_VENDOR,
1070 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1071 .wme_oui = { WME_OUI_BYTES },
1072 .wme_type = WME_OUI_TYPE,
1073 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1074 .wme_version = WME_VERSION,
1078 memcpy(frm, ¶m, sizeof(param));
1079 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1080 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1081 *frm++ = 0; /* reserved field */
1082 for (i = 0; i < WME_NUM_AC; i++) {
1083 const struct wmeParams *ac =
1084 &wme->wme_bssChanParams.cap_wmeParams[i];
1085 *frm++ = SM(i, WME_PARAM_ACI)
1086 | SM(ac->wmep_acm, WME_PARAM_ACM)
1087 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1089 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1090 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1092 ADDSHORT(frm, ac->wmep_txopLimit);
1098 #undef WME_OUI_BYTES
1101 * Send a probe request frame with the specified ssid
1102 * and any optional information element data.
1105 ieee80211_send_probereq(struct ieee80211_node *ni,
1106 const u_int8_t sa[IEEE80211_ADDR_LEN],
1107 const u_int8_t da[IEEE80211_ADDR_LEN],
1108 const u_int8_t bssid[IEEE80211_ADDR_LEN],
1109 const u_int8_t *ssid, size_t ssidlen,
1110 const void *optie, size_t optielen)
1112 struct ieee80211com *ic = ni->ni_ic;
1113 struct ieee80211_frame *wh;
1114 const struct ieee80211_rateset *rs;
1119 * Hold a reference on the node so it doesn't go away until after
1120 * the xmit is complete all the way in the driver. On error we
1121 * will remove our reference.
1123 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1124 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1126 ni, ether_sprintf(ni->ni_macaddr),
1127 ieee80211_node_refcnt(ni)+1);
1128 ieee80211_ref_node(ni);
1131 * prreq frame format
1133 * [tlv] supported rates
1134 * [tlv] extended supported rates
1135 * [tlv] user-specified ie's
1137 m = ieee80211_getmgtframe(&frm,
1138 2 + IEEE80211_NWID_LEN
1139 + 2 + IEEE80211_RATE_SIZE
1140 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1141 + (optie != NULL ? optielen : 0)
1144 ic->ic_stats.is_tx_nobuf++;
1145 ieee80211_free_node(ni);
1149 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1150 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1151 frm = ieee80211_add_rates(frm, rs);
1152 frm = ieee80211_add_xrates(frm, rs);
1154 if (optie != NULL) {
1155 memcpy(frm, optie, optielen);
1158 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1160 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1163 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1164 m->m_pkthdr.rcvif = (void *)ni;
1166 wh = mtod(m, struct ieee80211_frame *);
1167 ieee80211_send_setup(ic, ni, wh,
1168 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1170 /* XXX power management? */
1172 IEEE80211_NODE_STAT(ni, tx_probereq);
1173 IEEE80211_NODE_STAT(ni, tx_mgmt);
1175 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1176 "[%s] send probe req on channel %u\n",
1177 ether_sprintf(wh->i_addr1),
1178 ieee80211_chan2ieee(ic, ic->ic_curchan));
1180 IF_ENQUEUE(&ic->ic_mgtq, m);
1181 if_start(ic->ic_ifp);
1186 * Calculate capability information for mgt frames.
1189 getcapinfo(struct ieee80211com *ic, struct ieee80211_channel *chan)
1193 KASSERT(ic->ic_opmode != IEEE80211_M_STA, ("station mode"));
1195 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1196 capinfo = IEEE80211_CAPINFO_ESS;
1197 else if (ic->ic_opmode == IEEE80211_M_IBSS)
1198 capinfo = IEEE80211_CAPINFO_IBSS;
1201 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1202 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1203 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1204 IEEE80211_IS_CHAN_2GHZ(chan))
1205 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1206 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1207 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1212 * Send a management frame. The node is for the destination (or ic_bss
1213 * when in station mode). Nodes other than ic_bss have their reference
1214 * count bumped to reflect our use for an indeterminant time.
1217 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1220 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1224 int has_challenge, is_shared_key, ret, timer, status;
1226 KASSERT(ni != NULL, ("null node"));
1229 * Hold a reference on the node so it doesn't go away until after
1230 * the xmit is complete all the way in the driver. On error we
1231 * will remove our reference.
1233 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1234 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1236 ni, ether_sprintf(ni->ni_macaddr),
1237 ieee80211_node_refcnt(ni)+1);
1238 ieee80211_ref_node(ni);
1242 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1244 * probe response frame format
1246 * [2] beacon interval
1247 * [2] cabability information
1249 * [tlv] supported rates
1250 * [tlv] parameter set (FH/DS)
1251 * [tlv] parameter set (IBSS)
1252 * [tlv] extended rate phy (ERP)
1253 * [tlv] extended supported rates
1255 * [tlv] WME (optional)
1257 m = ieee80211_getmgtframe(&frm,
1261 + 2 + IEEE80211_NWID_LEN
1262 + 2 + IEEE80211_RATE_SIZE
1266 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1267 /* XXX !WPA1+WPA2 fits w/o a cluster */
1268 + (ic->ic_flags & IEEE80211_F_WPA ?
1269 2*sizeof(struct ieee80211_ie_wpa) : 0)
1270 + sizeof(struct ieee80211_wme_param)
1273 senderr(ENOMEM, is_tx_nobuf);
1275 memset(frm, 0, 8); /* timestamp should be filled later */
1277 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1279 capinfo = getcapinfo(ic, ic->ic_curchan);
1280 *(u_int16_t *)frm = htole16(capinfo);
1283 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1284 ic->ic_bss->ni_esslen);
1285 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1287 if (ic->ic_phytype == IEEE80211_T_FH) {
1288 *frm++ = IEEE80211_ELEMID_FHPARMS;
1290 *frm++ = ni->ni_fhdwell & 0x00ff;
1291 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1292 *frm++ = IEEE80211_FH_CHANSET(
1293 ieee80211_chan2ieee(ic, ic->ic_curchan));
1294 *frm++ = IEEE80211_FH_CHANPAT(
1295 ieee80211_chan2ieee(ic, ic->ic_curchan));
1296 *frm++ = ni->ni_fhindex;
1298 *frm++ = IEEE80211_ELEMID_DSPARMS;
1300 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan);
1303 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1304 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1306 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1308 if (ic->ic_flags & IEEE80211_F_WPA)
1309 frm = ieee80211_add_wpa(frm, ic);
1310 if (ic->ic_curmode == IEEE80211_MODE_11G)
1311 frm = ieee80211_add_erp(frm, ic);
1312 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1313 if (ic->ic_flags & IEEE80211_F_WME)
1314 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1315 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1318 case IEEE80211_FC0_SUBTYPE_AUTH:
1321 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1322 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1323 ni->ni_challenge != NULL);
1326 * Deduce whether we're doing open authentication or
1327 * shared key authentication. We do the latter if
1328 * we're in the middle of a shared key authentication
1329 * handshake or if we're initiating an authentication
1330 * request and configured to use shared key.
1332 is_shared_key = has_challenge ||
1333 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1334 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1335 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1337 m = ieee80211_getmgtframe(&frm,
1338 3 * sizeof(u_int16_t)
1339 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1340 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1343 senderr(ENOMEM, is_tx_nobuf);
1345 ((u_int16_t *)frm)[0] =
1346 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1347 : htole16(IEEE80211_AUTH_ALG_OPEN);
1348 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
1349 ((u_int16_t *)frm)[2] = htole16(status);/* status */
1351 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1352 ((u_int16_t *)frm)[3] =
1353 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1354 IEEE80211_ELEMID_CHALLENGE);
1355 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1356 IEEE80211_CHALLENGE_LEN);
1357 m->m_pkthdr.len = m->m_len =
1358 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1359 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1360 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1361 "[%s] request encrypt frame (%s)\n",
1362 ether_sprintf(ni->ni_macaddr), __func__);
1363 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1366 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1368 /* XXX not right for shared key */
1369 if (status == IEEE80211_STATUS_SUCCESS)
1370 IEEE80211_NODE_STAT(ni, tx_auth);
1372 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1374 if (ic->ic_opmode == IEEE80211_M_STA)
1375 timer = IEEE80211_TRANS_WAIT;
1378 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1379 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1380 "[%s] send station deauthenticate (reason %d)\n",
1381 ether_sprintf(ni->ni_macaddr), arg);
1382 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1384 senderr(ENOMEM, is_tx_nobuf);
1385 *(u_int16_t *)frm = htole16(arg); /* reason */
1386 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1388 IEEE80211_NODE_STAT(ni, tx_deauth);
1389 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1391 ieee80211_node_unauthorize(ni); /* port closed */
1394 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1395 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1397 * asreq frame format
1398 * [2] capability information
1399 * [2] listen interval
1400 * [6*] current AP address (reassoc only)
1402 * [tlv] supported rates
1403 * [tlv] extended supported rates
1405 * [tlv] user-specified ie's
1407 m = ieee80211_getmgtframe(&frm,
1410 + IEEE80211_ADDR_LEN
1411 + 2 + IEEE80211_NWID_LEN
1412 + 2 + IEEE80211_RATE_SIZE
1413 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1414 + sizeof(struct ieee80211_wme_info)
1415 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1418 senderr(ENOMEM, is_tx_nobuf);
1420 KASSERT(ic->ic_opmode == IEEE80211_M_STA,
1421 ("wrong mode %u", ic->ic_opmode));
1422 capinfo = IEEE80211_CAPINFO_ESS;
1423 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1424 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1426 * NB: Some 11a AP's reject the request when
1427 * short premable is set.
1429 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1430 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1431 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1432 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1433 (ic->ic_caps & IEEE80211_C_SHSLOT))
1434 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1435 *(u_int16_t *)frm = htole16(capinfo);
1438 *(u_int16_t *)frm = htole16(ic->ic_lintval);
1441 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1442 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1443 frm += IEEE80211_ADDR_LEN;
1446 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1447 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1448 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1449 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1450 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1451 if (ic->ic_opt_ie != NULL) {
1452 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1453 frm += ic->ic_opt_ie_len;
1455 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1457 timer = IEEE80211_TRANS_WAIT;
1460 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1461 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1463 * asreq frame format
1464 * [2] capability information
1466 * [2] association ID
1467 * [tlv] supported rates
1468 * [tlv] extended supported rates
1469 * [tlv] WME (if enabled and STA enabled)
1471 m = ieee80211_getmgtframe(&frm,
1475 + 2 + IEEE80211_RATE_SIZE
1476 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1477 + sizeof(struct ieee80211_wme_param)
1480 senderr(ENOMEM, is_tx_nobuf);
1482 capinfo = getcapinfo(ic, ic->ic_curchan);
1483 *(u_int16_t *)frm = htole16(capinfo);
1486 *(u_int16_t *)frm = htole16(arg); /* status */
1489 if (arg == IEEE80211_STATUS_SUCCESS) {
1490 *(u_int16_t *)frm = htole16(ni->ni_associd);
1491 IEEE80211_NODE_STAT(ni, tx_assoc);
1493 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1496 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1497 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1498 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1499 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1500 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1503 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1504 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1505 "[%s] send station disassociate (reason %d)\n",
1506 ether_sprintf(ni->ni_macaddr), arg);
1507 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1509 senderr(ENOMEM, is_tx_nobuf);
1510 *(u_int16_t *)frm = htole16(arg); /* reason */
1511 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1513 IEEE80211_NODE_STAT(ni, tx_disassoc);
1514 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1518 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1519 "[%s] invalid mgmt frame type %u\n",
1520 ether_sprintf(ni->ni_macaddr), type);
1521 senderr(EINVAL, is_tx_unknownmgt);
1524 ret = ieee80211_mgmt_output(ic, ni, m, type, timer);
1527 ieee80211_free_node(ni);
1534 * Allocate a beacon frame and fillin the appropriate bits.
1537 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1538 struct ieee80211_beacon_offsets *bo)
1540 struct ifnet *ifp = ic->ic_ifp;
1541 struct ieee80211_frame *wh;
1544 u_int8_t *frm, *efrm;
1546 struct ieee80211_rateset *rs;
1549 * beacon frame format
1551 * [2] beacon interval
1552 * [2] cabability information
1554 * [tlv] supported rates
1555 * [3] parameter set (DS)
1556 * [tlv] parameter set (IBSS/TIM)
1557 * [tlv] extended rate phy (ERP)
1558 * [tlv] extended supported rates
1559 * [tlv] WME parameters
1560 * [tlv] WPA/RSN parameters
1561 * XXX Vendor-specific OIDs (e.g. Atheros)
1562 * NB: we allocate the max space required for the TIM bitmap.
1565 pktlen = 8 /* time stamp */
1566 + sizeof(u_int16_t) /* beacon interval */
1567 + sizeof(u_int16_t) /* capabilities */
1568 + 2 + ni->ni_esslen /* ssid */
1569 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1570 + 2 + 1 /* DS parameters */
1571 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */
1573 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1574 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */
1575 sizeof(struct ieee80211_wme_param) : 0)
1576 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
1577 2*sizeof(struct ieee80211_ie_wpa) : 0)
1579 m = ieee80211_getmgtframe(&frm, pktlen);
1581 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1582 "%s: cannot get buf; size %u\n", __func__, pktlen);
1583 ic->ic_stats.is_tx_nobuf++;
1587 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
1589 *(u_int16_t *)frm = htole16(ni->ni_intval);
1591 capinfo = getcapinfo(ic, ni->ni_chan);
1592 bo->bo_caps = (u_int16_t *)frm;
1593 *(u_int16_t *)frm = htole16(capinfo);
1595 *frm++ = IEEE80211_ELEMID_SSID;
1596 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1597 *frm++ = ni->ni_esslen;
1598 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1599 frm += ni->ni_esslen;
1602 frm = ieee80211_add_rates(frm, rs);
1603 if (ic->ic_curmode != IEEE80211_MODE_FH) {
1604 *frm++ = IEEE80211_ELEMID_DSPARMS;
1606 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1609 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1610 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1612 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1614 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1615 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1617 tie->tim_ie = IEEE80211_ELEMID_TIM;
1618 tie->tim_len = 4; /* length */
1619 tie->tim_count = 0; /* DTIM count */
1620 tie->tim_period = ic->ic_dtim_period; /* DTIM period */
1621 tie->tim_bitctl = 0; /* bitmap control */
1622 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
1623 frm += sizeof(struct ieee80211_tim_ie);
1626 bo->bo_trailer = frm;
1627 if (ic->ic_flags & IEEE80211_F_WME) {
1629 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1630 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1632 if (ic->ic_flags & IEEE80211_F_WPA)
1633 frm = ieee80211_add_wpa(frm, ic);
1634 if (ic->ic_curmode == IEEE80211_MODE_11G) {
1636 frm = ieee80211_add_erp(frm, ic);
1638 efrm = ieee80211_add_xrates(frm, rs);
1639 bo->bo_trailer_len = efrm - bo->bo_trailer;
1640 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1642 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1643 KASSERT(m != NULL, ("no space for 802.11 header?"));
1644 wh = mtod(m, struct ieee80211_frame *);
1645 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1646 IEEE80211_FC0_SUBTYPE_BEACON;
1647 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1648 *(u_int16_t *)wh->i_dur = 0;
1649 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1650 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1651 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1652 *(u_int16_t *)wh->i_seq = 0;
1658 * Update the dynamic parts of a beacon frame based on the current state.
1661 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1662 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1664 int len_changed = 0;
1667 IEEE80211_BEACON_LOCK(ic);
1668 /* XXX faster to recalculate entirely or just changes? */
1669 capinfo = getcapinfo(ic, ni->ni_chan);
1670 *bo->bo_caps = htole16(capinfo);
1672 if (ic->ic_flags & IEEE80211_F_WME) {
1673 struct ieee80211_wme_state *wme = &ic->ic_wme;
1676 * Check for agressive mode change. When there is
1677 * significant high priority traffic in the BSS
1678 * throttle back BE traffic by using conservative
1679 * parameters. Otherwise BE uses agressive params
1680 * to optimize performance of legacy/non-QoS traffic.
1682 if (wme->wme_flags & WME_F_AGGRMODE) {
1683 if (wme->wme_hipri_traffic >
1684 wme->wme_hipri_switch_thresh) {
1685 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1686 "%s: traffic %u, disable aggressive mode\n",
1687 __func__, wme->wme_hipri_traffic);
1688 wme->wme_flags &= ~WME_F_AGGRMODE;
1689 ieee80211_wme_updateparams_locked(ic);
1690 wme->wme_hipri_traffic =
1691 wme->wme_hipri_switch_hysteresis;
1693 wme->wme_hipri_traffic = 0;
1695 if (wme->wme_hipri_traffic <=
1696 wme->wme_hipri_switch_thresh) {
1697 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1698 "%s: traffic %u, enable aggressive mode\n",
1699 __func__, wme->wme_hipri_traffic);
1700 wme->wme_flags |= WME_F_AGGRMODE;
1701 ieee80211_wme_updateparams_locked(ic);
1702 wme->wme_hipri_traffic = 0;
1704 wme->wme_hipri_traffic =
1705 wme->wme_hipri_switch_hysteresis;
1707 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1708 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
1709 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1713 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/
1714 struct ieee80211_tim_ie *tie =
1715 (struct ieee80211_tim_ie *) bo->bo_tim;
1716 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1717 u_int timlen, timoff, i;
1719 * ATIM/DTIM needs updating. If it fits in the
1720 * current space allocated then just copy in the
1721 * new bits. Otherwise we need to move any trailing
1722 * data to make room. Note that we know there is
1723 * contiguous space because ieee80211_beacon_allocate
1724 * insures there is space in the mbuf to write a
1725 * maximal-size virtual bitmap (based on ic_max_aid).
1728 * Calculate the bitmap size and offset, copy any
1729 * trailer out of the way, and then copy in the
1730 * new bitmap and update the information element.
1731 * Note that the tim bitmap must contain at least
1732 * one byte and any offset must be even.
1734 if (ic->ic_ps_pending != 0) {
1735 timoff = 128; /* impossibly large */
1736 for (i = 0; i < ic->ic_tim_len; i++)
1737 if (ic->ic_tim_bitmap[i]) {
1741 KASSERT(timoff != 128, ("tim bitmap empty!"));
1742 for (i = ic->ic_tim_len-1; i >= timoff; i--)
1743 if (ic->ic_tim_bitmap[i])
1745 timlen = 1 + (i - timoff);
1750 if (timlen != bo->bo_tim_len) {
1751 /* copy up/down trailer */
1752 int adjust = tie->tim_bitmap+timlen
1754 ovbcopy(bo->bo_trailer, bo->bo_trailer+adjust,
1755 bo->bo_trailer_len);
1756 bo->bo_trailer += adjust;
1757 bo->bo_wme += adjust;
1758 bo->bo_erp += adjust;
1759 bo->bo_tim_len = timlen;
1761 /* update information element */
1762 tie->tim_len = 3 + timlen;
1763 tie->tim_bitctl = timoff;
1766 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1769 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1771 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1772 "%s: TIM updated, pending %u, off %u, len %u\n",
1773 __func__, ic->ic_ps_pending, timoff, timlen);
1775 /* count down DTIM period */
1776 if (tie->tim_count == 0)
1777 tie->tim_count = tie->tim_period - 1;
1780 /* update state for buffered multicast frames on DTIM */
1781 if (mcast && tie->tim_count == 0)
1782 tie->tim_bitctl |= 1;
1784 tie->tim_bitctl &= ~1;
1785 if (ic->ic_flags_ext & IEEE80211_FEXT_ERPUPDATE) {
1787 * ERP element needs updating.
1789 (void) ieee80211_add_erp(bo->bo_erp, ic);
1790 ic->ic_flags_ext &= ~IEEE80211_FEXT_ERPUPDATE;
1793 IEEE80211_BEACON_UNLOCK(ic);
1799 * Save an outbound packet for a node in power-save sleep state.
1800 * The new packet is placed on the node's saved queue, and the TIM
1801 * is changed, if necessary.
1804 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1809 IEEE80211_NODE_SAVEQ_LOCK(ni);
1810 if (_IF_QFULL(&ni->ni_savedq)) {
1811 _IF_DROP(&ni->ni_savedq);
1812 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1813 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1814 "[%s] pwr save q overflow, drops %d (size %d)\n",
1815 ether_sprintf(ni->ni_macaddr),
1816 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1817 #ifdef IEEE80211_DEBUG
1818 if (ieee80211_msg_dumppkts(ic))
1819 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1825 * Tag the frame with it's expiry time and insert
1826 * it in the queue. The aging interval is 4 times
1827 * the listen interval specified by the station.
1828 * Frames that sit around too long are reclaimed
1829 * using this information.
1831 /* XXX handle overflow? */
1832 age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
1833 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1834 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1836 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1837 "[%s] save frame with age %d, %u now queued\n",
1838 ether_sprintf(ni->ni_macaddr), age, qlen);
1841 ic->ic_set_tim(ni, 1);