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