ieee80211_output.c revision 1.32 1 /* $NetBSD: ieee80211_output.c,v 1.32 2005/06/27 05:49:13 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.32 2005/06/27 05:49:13 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 /*
412 * Return the transmit key to use in sending a unicast frame.
413 * If a unicast key is set we use that. When no unicast key is set
414 * we fall back to the default transmit key.
415 */
416 static __inline struct ieee80211_key *
417 ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
418 {
419 if (IEEE80211_KEY_UNDEFINED(ni->ni_ucastkey)) {
420 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
421 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
422 return NULL;
423 return &ic->ic_nw_keys[ic->ic_def_txkey];
424 } else {
425 return &ni->ni_ucastkey;
426 }
427 }
428
429 /*
430 * Return the transmit key to use in sending a multicast frame.
431 * Multicast traffic always uses the group key which is installed as
432 * the default tx key.
433 */
434 static __inline struct ieee80211_key *
435 ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
436 {
437 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
438 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
439 return NULL;
440 return &ic->ic_nw_keys[ic->ic_def_txkey];
441 }
442
443 /*
444 * Encapsulate an outbound data frame. The mbuf chain is updated.
445 * If an error is encountered NULL is returned. The caller is required
446 * to provide a node reference and pullup the ethernet header in the
447 * first mbuf.
448 */
449 struct mbuf *
450 ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
451 struct ieee80211_node *ni)
452 {
453 struct ether_header eh;
454 struct ieee80211_frame *wh;
455 struct ieee80211_key *key;
456 struct llc *llc;
457 int hdrsize, datalen, addqos;
458
459 IASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
460 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
461
462 /*
463 * Insure space for additional headers. First identify
464 * transmit key to use in calculating any buffer adjustments
465 * required. This is also used below to do privacy
466 * encapsulation work. Then calculate the 802.11 header
467 * size and any padding required by the driver.
468 *
469 * Note key may be NULL if we fall back to the default
470 * transmit key and that is not set. In that case the
471 * buffer may not be expanded as needed by the cipher
472 * routines, but they will/should discard it.
473 */
474 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
475 if (ic->ic_opmode == IEEE80211_M_STA ||
476 !IEEE80211_IS_MULTICAST(eh.ether_dhost))
477 key = ieee80211_crypto_getucastkey(ic, ni);
478 else
479 key = ieee80211_crypto_getmcastkey(ic, ni);
480 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
481 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
482 "[%s] no default transmit key (%s) deftxkey %u\n",
483 ether_sprintf(eh.ether_dhost), __func__,
484 ic->ic_def_txkey);
485 ic->ic_stats.is_tx_nodefkey++;
486 }
487 } else
488 key = NULL;
489 /* XXX 4-address format */
490 /*
491 * XXX Some ap's don't handle QoS-encapsulated EAPOL
492 * frames so suppress use. This may be an issue if other
493 * ap's require all data frames to be QoS-encapsulated
494 * once negotiated in which case we'll need to make this
495 * configurable.
496 */
497 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
498 eh.ether_type != htons(ETHERTYPE_PAE);
499 if (addqos)
500 hdrsize = sizeof(struct ieee80211_qosframe);
501 else
502 hdrsize = sizeof(struct ieee80211_frame);
503 if (ic->ic_flags & IEEE80211_F_DATAPAD)
504 hdrsize = roundup(hdrsize, sizeof(u_int32_t));
505 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
506 if (m == NULL) {
507 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
508 goto bad;
509 }
510
511 /* NB: this could be optimized because of ieee80211_mbuf_adjust */
512 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
513 llc = mtod(m, struct llc *);
514 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
515 llc->llc_control = LLC_UI;
516 llc->llc_snap.org_code[0] = 0;
517 llc->llc_snap.org_code[1] = 0;
518 llc->llc_snap.org_code[2] = 0;
519 llc->llc_snap.ether_type = eh.ether_type;
520 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
521
522 M_PREPEND(m, hdrsize, M_DONTWAIT);
523 if (m == NULL) {
524 ic->ic_stats.is_tx_nobuf++;
525 goto bad;
526 }
527 wh = mtod(m, struct ieee80211_frame *);
528 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
529 *(u_int16_t *)wh->i_dur = 0;
530 switch (ic->ic_opmode) {
531 case IEEE80211_M_STA:
532 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
533 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
534 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
535 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
536 break;
537 case IEEE80211_M_IBSS:
538 case IEEE80211_M_AHDEMO:
539 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
540 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
541 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
542 /*
543 * NB: always use the bssid from ic_bss as the
544 * neighbor's may be stale after an ibss merge
545 */
546 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
547 break;
548 case IEEE80211_M_HOSTAP:
549 #ifndef IEEE80211_NO_HOSTAP
550 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
551 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
552 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
553 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
554 #endif /* !IEEE80211_NO_HOSTAP */
555 break;
556 case IEEE80211_M_MONITOR:
557 goto bad;
558 }
559 if (addqos) {
560 struct ieee80211_qosframe *qwh =
561 (struct ieee80211_qosframe *) wh;
562 int ac, tid;
563
564 ac = M_WME_GETAC(m);
565 /* map from access class/queue to 11e header priorty value */
566 tid = WME_AC_TO_TID(ac);
567 qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
568 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
569 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
570 qwh->i_qos[1] = 0;
571 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
572
573 *(u_int16_t *)wh->i_seq =
574 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
575 ni->ni_txseqs[tid]++;
576 } else {
577 *(u_int16_t *)wh->i_seq =
578 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
579 ni->ni_txseqs[0]++;
580 }
581 if (key != NULL) {
582 /*
583 * IEEE 802.1X: send EAPOL frames always in the clear.
584 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
585 */
586 if (eh.ether_type != htons(ETHERTYPE_PAE) ||
587 ((ic->ic_flags & IEEE80211_F_WPA) &&
588 (ic->ic_opmode == IEEE80211_M_STA ?
589 !IEEE80211_KEY_UNDEFINED(*key) :
590 !IEEE80211_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_min *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 ((wh->i_fc[1] & IEEE80211_FC1_WEP) != 0) {
732 /* XXX assumes the packet is already WEP encapsulated */
733 paylen -= IEEE80211_WEP_TOTLEN;
734 overlen = IEEE80211_WEP_TOTLEN + IEEE80211_CRC_LEN;
735 } else
736 overlen = IEEE80211_CRC_LEN;
737
738 npkt = paylen / fraglen;
739 lastlen0 = paylen % fraglen;
740
741 if (npkt == 0) /* no fragments */
742 lastlen = paylen + overlen;
743 else if (lastlen0 != 0) { /* a short "tail" fragment */
744 lastlen = lastlen0 + overlen;
745 npkt++;
746 } else /* full-length "tail" fragment */
747 lastlen = fraglen + overlen;
748
749 if (npktp != NULL)
750 *npktp = npkt;
751
752 if (npkt > 1)
753 firstlen = fraglen + overlen;
754 else
755 firstlen = paylen + overlen;
756
757 if (debug) {
758 printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d "
759 "fraglen %d overlen %d len %d rate %d flags %08x\n",
760 __func__, npkt, firstlen, lastlen0, lastlen, fraglen,
761 overlen, len, rate, flags);
762 }
763
764 ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
765 (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL;
766
767 rc = ieee80211_compute_duration1(firstlen + hdrlen,
768 ack, flags, rate, d0);
769 if (rc == -1)
770 return rc;
771
772 if (npkt <= 1) {
773 *dn = *d0;
774 return 0;
775 }
776 return ieee80211_compute_duration1(lastlen + hdrlen, ack, flags, rate,
777 dn);
778 }
779
780 /*
781 * Add a supported rates element id to a frame.
782 */
783 static u_int8_t *
784 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
785 {
786 int nrates;
787
788 *frm++ = IEEE80211_ELEMID_RATES;
789 nrates = rs->rs_nrates;
790 if (nrates > IEEE80211_RATE_SIZE)
791 nrates = IEEE80211_RATE_SIZE;
792 *frm++ = nrates;
793 memcpy(frm, rs->rs_rates, nrates);
794 return frm + nrates;
795 }
796
797 /*
798 * Add an extended supported rates element id to a frame.
799 */
800 static u_int8_t *
801 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
802 {
803 /*
804 * Add an extended supported rates element if operating in 11g mode.
805 */
806 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
807 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
808 *frm++ = IEEE80211_ELEMID_XRATES;
809 *frm++ = nrates;
810 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
811 frm += nrates;
812 }
813 return frm;
814 }
815
816 /*
817 * Add an ssid elemet to a frame.
818 */
819 static u_int8_t *
820 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
821 {
822 *frm++ = IEEE80211_ELEMID_SSID;
823 *frm++ = len;
824 memcpy(frm, ssid, len);
825 return frm + len;
826 }
827
828 /*
829 * Add an erp element to a frame.
830 */
831 static u_int8_t *
832 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
833 {
834 u_int8_t erp;
835
836 *frm++ = IEEE80211_ELEMID_ERP;
837 *frm++ = 1;
838 erp = 0;
839 if (ic->ic_nonerpsta != 0)
840 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
841 if (ic->ic_flags & IEEE80211_F_USEPROT)
842 erp |= IEEE80211_ERP_USE_PROTECTION;
843 if (ic->ic_flags & IEEE80211_F_USEBARKER)
844 erp |= IEEE80211_ERP_LONG_PREAMBLE;
845 *frm++ = erp;
846 return frm;
847 }
848
849 static u_int8_t *
850 ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
851 {
852 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2
853 #define ADDSHORT(frm, v) do { \
854 frm[0] = (v) & 0xff; \
855 frm[1] = (v) >> 8; \
856 frm += 2; \
857 } while (0)
858 #define ADDSELECTOR(frm, sel) do { \
859 memcpy(frm, sel, 4); \
860 frm += 4; \
861 } while (0)
862 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
863 static const u_int8_t cipher_suite[][4] = {
864 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */
865 { WPA_OUI_BYTES, WPA_CSE_TKIP },
866 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */
867 { WPA_OUI_BYTES, WPA_CSE_CCMP },
868 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
869 { WPA_OUI_BYTES, WPA_CSE_NULL },
870 };
871 static const u_int8_t wep104_suite[4] =
872 { WPA_OUI_BYTES, WPA_CSE_WEP104 };
873 static const u_int8_t key_mgt_unspec[4] =
874 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
875 static const u_int8_t key_mgt_psk[4] =
876 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
877 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
878 u_int8_t *frm = ie;
879 u_int8_t *selcnt;
880
881 *frm++ = IEEE80211_ELEMID_VENDOR;
882 *frm++ = 0; /* length filled in below */
883 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */
884 frm += sizeof(oui);
885 ADDSHORT(frm, WPA_VERSION);
886
887 /* XXX filter out CKIP */
888
889 /* multicast cipher */
890 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
891 rsn->rsn_mcastkeylen >= 13)
892 ADDSELECTOR(frm, wep104_suite);
893 else
894 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
895
896 /* unicast cipher list */
897 selcnt = frm;
898 ADDSHORT(frm, 0); /* selector count */
899 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
900 selcnt[0]++;
901 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
902 }
903 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
904 selcnt[0]++;
905 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
906 }
907
908 /* authenticator selector list */
909 selcnt = frm;
910 ADDSHORT(frm, 0); /* selector count */
911 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
912 selcnt[0]++;
913 ADDSELECTOR(frm, key_mgt_unspec);
914 }
915 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
916 selcnt[0]++;
917 ADDSELECTOR(frm, key_mgt_psk);
918 }
919
920 /* optional capabilities */
921 if (rsn->rsn_caps != 0)
922 ADDSHORT(frm, rsn->rsn_caps);
923
924 /* calculate element length */
925 ie[1] = frm - ie - 2;
926 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
927 ("WPA IE too big, %u > %zu",
928 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
929 return frm;
930 #undef ADDSHORT
931 #undef ADDSELECTOR
932 #undef WPA_OUI_BYTES
933 }
934
935 static u_int8_t *
936 ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
937 {
938 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac
939 #define ADDSHORT(frm, v) do { \
940 frm[0] = (v) & 0xff; \
941 frm[1] = (v) >> 8; \
942 frm += 2; \
943 } while (0)
944 #define ADDSELECTOR(frm, sel) do { \
945 memcpy(frm, sel, 4); \
946 frm += 4; \
947 } while (0)
948 static const u_int8_t cipher_suite[][4] = {
949 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */
950 { RSN_OUI_BYTES, RSN_CSE_TKIP },
951 { RSN_OUI_BYTES, RSN_CSE_WRAP },
952 { RSN_OUI_BYTES, RSN_CSE_CCMP },
953 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
954 { RSN_OUI_BYTES, RSN_CSE_NULL },
955 };
956 static const u_int8_t wep104_suite[4] =
957 { RSN_OUI_BYTES, RSN_CSE_WEP104 };
958 static const u_int8_t key_mgt_unspec[4] =
959 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
960 static const u_int8_t key_mgt_psk[4] =
961 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
962 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
963 u_int8_t *frm = ie;
964 u_int8_t *selcnt;
965
966 *frm++ = IEEE80211_ELEMID_RSN;
967 *frm++ = 0; /* length filled in below */
968 ADDSHORT(frm, RSN_VERSION);
969
970 /* XXX filter out CKIP */
971
972 /* multicast cipher */
973 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
974 rsn->rsn_mcastkeylen >= 13)
975 ADDSELECTOR(frm, wep104_suite);
976 else
977 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
978
979 /* unicast cipher list */
980 selcnt = frm;
981 ADDSHORT(frm, 0); /* selector count */
982 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
983 selcnt[0]++;
984 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
985 }
986 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
987 selcnt[0]++;
988 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
989 }
990
991 /* authenticator selector list */
992 selcnt = frm;
993 ADDSHORT(frm, 0); /* selector count */
994 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
995 selcnt[0]++;
996 ADDSELECTOR(frm, key_mgt_unspec);
997 }
998 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
999 selcnt[0]++;
1000 ADDSELECTOR(frm, key_mgt_psk);
1001 }
1002
1003 /* optional capabilities */
1004 if (rsn->rsn_caps != 0)
1005 ADDSHORT(frm, rsn->rsn_caps);
1006 /* XXX PMKID */
1007
1008 /* calculate element length */
1009 ie[1] = frm - ie - 2;
1010 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
1011 ("RSN IE too big, %u > %zu",
1012 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
1013 return frm;
1014 #undef ADDSELECTOR
1015 #undef ADDSHORT
1016 #undef RSN_OUI_BYTES
1017 }
1018
1019 /*
1020 * Add a WPA/RSN element to a frame.
1021 */
1022 static u_int8_t *
1023 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
1024 {
1025
1026 IASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
1027 if (ic->ic_flags & IEEE80211_F_WPA2)
1028 frm = ieee80211_setup_rsn_ie(ic, frm);
1029 if (ic->ic_flags & IEEE80211_F_WPA1)
1030 frm = ieee80211_setup_wpa_ie(ic, frm);
1031 return frm;
1032 }
1033
1034 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1035 /*
1036 * Add a WME information element to a frame.
1037 */
1038 static u_int8_t *
1039 ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
1040 {
1041 static const struct ieee80211_wme_info info = {
1042 .wme_id = IEEE80211_ELEMID_VENDOR,
1043 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1044 .wme_oui = { WME_OUI_BYTES },
1045 .wme_type = WME_OUI_TYPE,
1046 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1047 .wme_version = WME_VERSION,
1048 .wme_info = 0,
1049 };
1050 memcpy(frm, &info, sizeof(info));
1051 return frm + sizeof(info);
1052 }
1053
1054 /*
1055 * Add a WME parameters element to a frame.
1056 */
1057 static u_int8_t *
1058 ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
1059 {
1060 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1061 #define ADDSHORT(frm, v) do { \
1062 frm[0] = (v) & 0xff; \
1063 frm[1] = (v) >> 8; \
1064 frm += 2; \
1065 } while (0)
1066 /* NB: this works 'cuz a param has an info at the front */
1067 static const struct ieee80211_wme_info param = {
1068 .wme_id = IEEE80211_ELEMID_VENDOR,
1069 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1070 .wme_oui = { WME_OUI_BYTES },
1071 .wme_type = WME_OUI_TYPE,
1072 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1073 .wme_version = WME_VERSION,
1074 };
1075 int i;
1076
1077 memcpy(frm, ¶m, sizeof(param));
1078 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1079 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1080 *frm++ = 0; /* reserved field */
1081 for (i = 0; i < WME_NUM_AC; i++) {
1082 const struct wmeParams *ac =
1083 &wme->wme_bssChanParams.cap_wmeParams[i];
1084 *frm++ = SM(i, WME_PARAM_ACI)
1085 | SM(ac->wmep_acm, WME_PARAM_ACM)
1086 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1087 ;
1088 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1089 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1090 ;
1091 ADDSHORT(frm, ac->wmep_txopLimit);
1092 }
1093 return frm;
1094 #undef SM
1095 #undef ADDSHORT
1096 }
1097 #undef WME_OUI_BYTES
1098
1099 /*
1100 * Send a management frame. The node is for the destination (or ic_bss
1101 * when in station mode). Nodes other than ic_bss have their reference
1102 * count bumped to reflect our use for an indeterminant time.
1103 */
1104 int
1105 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1106 int type, int arg)
1107 {
1108 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1109 struct mbuf *m;
1110 u_int8_t *frm;
1111 enum ieee80211_phymode mode;
1112 u_int16_t capinfo;
1113 int has_challenge, is_shared_key, ret, timer, status;
1114
1115 IASSERT(ni != NULL, ("null node"));
1116
1117 /*
1118 * Hold a reference on the node so it doesn't go away until after
1119 * the xmit is complete all the way in the driver. On error we
1120 * will remove our reference.
1121 */
1122 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1123 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1124 __func__, __LINE__,
1125 ni, ether_sprintf(ni->ni_macaddr),
1126 ieee80211_node_refcnt(ni)+1);
1127 ieee80211_ref_node(ni);
1128
1129 timer = 0;
1130 switch (type) {
1131 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1132 /*
1133 * prreq frame format
1134 * [tlv] ssid
1135 * [tlv] supported rates
1136 * [tlv] extended supported rates
1137 * [tlv] user-specified ie's
1138 */
1139 m = ieee80211_getmgtframe(&frm,
1140 2 + IEEE80211_NWID_LEN
1141 + 2 + IEEE80211_RATE_SIZE
1142 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1143 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1144 );
1145 if (m == NULL)
1146 senderr(ENOMEM, is_tx_nobuf);
1147
1148 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
1149 mode = ieee80211_chan2mode(ic, ni->ni_chan);
1150 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1151 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1152 if (ic->ic_opt_ie != NULL) {
1153 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1154 frm += ic->ic_opt_ie_len;
1155 }
1156 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1157
1158 IEEE80211_NODE_STAT(ni, tx_probereq);
1159 if (ic->ic_opmode == IEEE80211_M_STA)
1160 timer = IEEE80211_TRANS_WAIT;
1161 break;
1162
1163 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1164 /*
1165 * probe response frame format
1166 * [8] time stamp
1167 * [2] beacon interval
1168 * [2] cabability information
1169 * [tlv] ssid
1170 * [tlv] supported rates
1171 * [tlv] parameter set (FH/DS)
1172 * [tlv] parameter set (IBSS)
1173 * [tlv] extended rate phy (ERP)
1174 * [tlv] extended supported rates
1175 * [tlv] WPA
1176 * [tlv] WME (optional)
1177 */
1178 m = ieee80211_getmgtframe(&frm,
1179 8
1180 + sizeof(u_int16_t)
1181 + sizeof(u_int16_t)
1182 + 2 + IEEE80211_NWID_LEN
1183 + 2 + IEEE80211_RATE_SIZE
1184 + 7 /* max(7,3) */
1185 + 6
1186 + 3
1187 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1188 /* XXX !WPA1+WPA2 fits w/o a cluster */
1189 + (ic->ic_flags & IEEE80211_F_WPA ?
1190 2*sizeof(struct ieee80211_ie_wpa) : 0)
1191 + sizeof(struct ieee80211_wme_param)
1192 );
1193 if (m == NULL)
1194 senderr(ENOMEM, is_tx_nobuf);
1195
1196 memset(frm, 0, 8); /* timestamp should be filled later */
1197 frm += 8;
1198 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1199 frm += 2;
1200 if (ic->ic_opmode == IEEE80211_M_IBSS)
1201 capinfo = IEEE80211_CAPINFO_IBSS;
1202 else
1203 capinfo = IEEE80211_CAPINFO_ESS;
1204 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1205 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1206 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1207 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1208 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1209 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1210 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1211 *(u_int16_t *)frm = htole16(capinfo);
1212 frm += 2;
1213
1214 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1215 ic->ic_bss->ni_esslen);
1216 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1217
1218 if (ic->ic_phytype == IEEE80211_T_FH) {
1219 *frm++ = IEEE80211_ELEMID_FHPARMS;
1220 *frm++ = 5;
1221 *frm++ = ni->ni_fhdwell & 0x00ff;
1222 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1223 *frm++ = IEEE80211_FH_CHANSET(
1224 ieee80211_chan2ieee(ic, ni->ni_chan));
1225 *frm++ = IEEE80211_FH_CHANPAT(
1226 ieee80211_chan2ieee(ic, ni->ni_chan));
1227 *frm++ = ni->ni_fhindex;
1228 } else {
1229 *frm++ = IEEE80211_ELEMID_DSPARMS;
1230 *frm++ = 1;
1231 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1232 }
1233
1234 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1235 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1236 *frm++ = 2;
1237 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1238 }
1239 if (ic->ic_flags & IEEE80211_F_WPA)
1240 frm = ieee80211_add_wpa(frm, ic);
1241 if (ic->ic_curmode == IEEE80211_MODE_11G)
1242 frm = ieee80211_add_erp(frm, ic);
1243 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1244 if (ic->ic_flags & IEEE80211_F_WME)
1245 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1246 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1247 break;
1248
1249 case IEEE80211_FC0_SUBTYPE_AUTH:
1250 status = arg >> 16;
1251 arg &= 0xffff;
1252 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1253 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1254 ni->ni_challenge != NULL);
1255
1256 /*
1257 * Deduce whether we're doing open authentication or
1258 * shared key authentication. We do the latter if
1259 * we're in the middle of a shared key authentication
1260 * handshake or if we're initiating an authentication
1261 * request and configured to use shared key.
1262 */
1263 is_shared_key = has_challenge ||
1264 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1265 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1266 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1267
1268 m = ieee80211_getmgtframe(&frm,
1269 3 * sizeof(u_int16_t)
1270 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1271 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1272 );
1273 if (m == NULL)
1274 senderr(ENOMEM, is_tx_nobuf);
1275
1276 ((u_int16_t *)frm)[0] =
1277 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1278 : htole16(IEEE80211_AUTH_ALG_OPEN);
1279 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
1280 ((u_int16_t *)frm)[2] = htole16(status);/* status */
1281
1282 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1283 ((u_int16_t *)frm)[3] =
1284 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1285 IEEE80211_ELEMID_CHALLENGE);
1286 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1287 IEEE80211_CHALLENGE_LEN);
1288 m->m_pkthdr.len = m->m_len =
1289 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1290 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1291 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1292 "[%s] request encrypt frame (%s)\n",
1293 ether_sprintf(ni->ni_macaddr), __func__);
1294 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1295 }
1296 } else
1297 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1298
1299 /* XXX not right for shared key */
1300 if (status == IEEE80211_STATUS_SUCCESS)
1301 IEEE80211_NODE_STAT(ni, tx_auth);
1302 else
1303 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1304
1305 /*
1306 * When 802.1x is not in use mark the port
1307 * authorized at this point so traffic can flow.
1308 */
1309 #ifndef IEEE80211_NO_HOSTAP
1310 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1311 status == IEEE80211_STATUS_SUCCESS &&
1312 ni->ni_authmode != IEEE80211_AUTH_8021X)
1313 ieee80211_node_authorize(ic, ni);
1314 #endif /* !IEEE80211_NO_HOSTAP */
1315 if (ic->ic_opmode == IEEE80211_M_STA)
1316 timer = IEEE80211_TRANS_WAIT;
1317 break;
1318
1319 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1320 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1321 "[%s] send station deauthenticate (reason %d)\n",
1322 ether_sprintf(ni->ni_macaddr), arg);
1323 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1324 if (m == NULL)
1325 senderr(ENOMEM, is_tx_nobuf);
1326 *(u_int16_t *)frm = htole16(arg); /* reason */
1327 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1328
1329 IEEE80211_NODE_STAT(ni, tx_deauth);
1330 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1331
1332 ieee80211_node_unauthorize(ic, ni); /* port closed */
1333 break;
1334
1335 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1336 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1337 /*
1338 * asreq frame format
1339 * [2] capability information
1340 * [2] listen interval
1341 * [6*] current AP address (reassoc only)
1342 * [tlv] ssid
1343 * [tlv] supported rates
1344 * [tlv] extended supported rates
1345 * [tlv] WME
1346 * [tlv] user-specified ie's
1347 */
1348 m = ieee80211_getmgtframe(&frm,
1349 sizeof(u_int16_t)
1350 + sizeof(u_int16_t)
1351 + IEEE80211_ADDR_LEN
1352 + 2 + IEEE80211_NWID_LEN
1353 + 2 + IEEE80211_RATE_SIZE
1354 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1355 + sizeof(struct ieee80211_wme_info)
1356 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1357 );
1358 if (m == NULL)
1359 senderr(ENOMEM, is_tx_nobuf);
1360
1361 capinfo = 0;
1362 if (ic->ic_opmode == IEEE80211_M_IBSS)
1363 capinfo |= IEEE80211_CAPINFO_IBSS;
1364 else /* IEEE80211_M_STA */
1365 capinfo |= IEEE80211_CAPINFO_ESS;
1366 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1367 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1368 /*
1369 * NB: Some 11a AP's reject the request when
1370 * short premable is set.
1371 */
1372 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1373 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1374 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1375 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1376 (ic->ic_caps & IEEE80211_C_SHSLOT))
1377 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1378 *(u_int16_t *)frm = htole16(capinfo);
1379 frm += 2;
1380
1381 *(u_int16_t *)frm = htole16(ic->ic_lintval);
1382 frm += 2;
1383
1384 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1385 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1386 frm += IEEE80211_ADDR_LEN;
1387 }
1388
1389 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1390 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1391 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1392 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1393 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1394 if (ic->ic_opt_ie != NULL) {
1395 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1396 frm += ic->ic_opt_ie_len;
1397 }
1398 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1399
1400 timer = IEEE80211_TRANS_WAIT;
1401 break;
1402
1403 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1404 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1405 /*
1406 * asreq frame format
1407 * [2] capability information
1408 * [2] status
1409 * [2] association ID
1410 * [tlv] supported rates
1411 * [tlv] extended supported rates
1412 * [tlv] WME (if enabled and STA enabled)
1413 */
1414 m = ieee80211_getmgtframe(&frm,
1415 sizeof(u_int16_t)
1416 + sizeof(u_int16_t)
1417 + sizeof(u_int16_t)
1418 + 2 + IEEE80211_RATE_SIZE
1419 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1420 + sizeof(struct ieee80211_wme_param)
1421 );
1422 if (m == NULL)
1423 senderr(ENOMEM, is_tx_nobuf);
1424
1425 capinfo = IEEE80211_CAPINFO_ESS;
1426 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1427 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1428 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1429 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1430 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1431 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1432 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1433 *(u_int16_t *)frm = htole16(capinfo);
1434 frm += 2;
1435
1436 *(u_int16_t *)frm = htole16(arg); /* status */
1437 frm += 2;
1438
1439 if (arg == IEEE80211_STATUS_SUCCESS) {
1440 *(u_int16_t *)frm = htole16(ni->ni_associd);
1441 IEEE80211_NODE_STAT(ni, tx_assoc);
1442 } else
1443 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1444 frm += 2;
1445
1446 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1447 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1448 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1449 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1450 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1451 break;
1452
1453 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1454 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1455 "[%s] send station disassociate (reason %d)\n",
1456 ether_sprintf(ni->ni_macaddr), arg);
1457 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1458 if (m == NULL)
1459 senderr(ENOMEM, is_tx_nobuf);
1460 *(u_int16_t *)frm = htole16(arg); /* reason */
1461 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1462
1463 IEEE80211_NODE_STAT(ni, tx_disassoc);
1464 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1465 break;
1466
1467 default:
1468 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1469 "[%s] invalid mgmt frame type %u\n",
1470 ether_sprintf(ni->ni_macaddr), type);
1471 senderr(EINVAL, is_tx_unknownmgt);
1472 /* NOTREACHED */
1473 }
1474
1475 ret = ieee80211_mgmt_output(ic, ni, m, type);
1476 if (ret == 0) {
1477 if (timer)
1478 ic->ic_mgt_timer = timer;
1479 } else {
1480 bad:
1481 ieee80211_free_node(ni);
1482 }
1483 return ret;
1484 #undef senderr
1485 }
1486
1487 /*
1488 * Allocate a beacon frame and fillin the appropriate bits.
1489 */
1490 struct mbuf *
1491 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1492 struct ieee80211_beacon_offsets *bo)
1493 {
1494 struct ifnet *ifp = ic->ic_ifp;
1495 struct ieee80211_frame *wh;
1496 struct mbuf *m;
1497 int pktlen;
1498 u_int8_t *frm, *efrm;
1499 u_int16_t capinfo;
1500 struct ieee80211_rateset *rs;
1501
1502 /*
1503 * beacon frame format
1504 * [8] time stamp
1505 * [2] beacon interval
1506 * [2] cabability information
1507 * [tlv] ssid
1508 * [tlv] supported rates
1509 * [3] parameter set (DS)
1510 * [tlv] parameter set (IBSS/TIM)
1511 * [tlv] extended rate phy (ERP)
1512 * [tlv] extended supported rates
1513 * [tlv] WME parameters
1514 * [tlv] WPA/RSN parameters
1515 * XXX Vendor-specific OIDs (e.g. Atheros)
1516 * NB: we allocate the max space required for the TIM bitmap.
1517 */
1518 rs = &ni->ni_rates;
1519 pktlen = 8 /* time stamp */
1520 + sizeof(u_int16_t) /* beacon interval */
1521 + sizeof(u_int16_t) /* capabilities */
1522 + 2 + ni->ni_esslen /* ssid */
1523 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1524 + 2 + 1 /* DS parameters */
1525 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */
1526 + 2 + 1 /* ERP */
1527 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1528 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */
1529 sizeof(struct ieee80211_wme_param) : 0)
1530 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
1531 2*sizeof(struct ieee80211_ie_wpa) : 0)
1532 ;
1533 m = ieee80211_getmgtframe(&frm, pktlen);
1534 if (m == NULL) {
1535 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1536 "%s: cannot get buf; size %u\n", __func__, pktlen);
1537 ic->ic_stats.is_tx_nobuf++;
1538 return NULL;
1539 }
1540
1541 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
1542 frm += 8;
1543 *(u_int16_t *)frm = htole16(ni->ni_intval);
1544 frm += 2;
1545 if (ic->ic_opmode == IEEE80211_M_IBSS)
1546 capinfo = IEEE80211_CAPINFO_IBSS;
1547 else
1548 capinfo = IEEE80211_CAPINFO_ESS;
1549 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1550 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1551 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1552 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1553 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1554 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1555 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1556 bo->bo_caps = (u_int16_t *)frm;
1557 *(u_int16_t *)frm = htole16(capinfo);
1558 frm += 2;
1559 *frm++ = IEEE80211_ELEMID_SSID;
1560 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1561 *frm++ = ni->ni_esslen;
1562 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1563 frm += ni->ni_esslen;
1564 } else
1565 *frm++ = 0;
1566 frm = ieee80211_add_rates(frm, rs);
1567 if (ic->ic_curmode != IEEE80211_MODE_FH) {
1568 *frm++ = IEEE80211_ELEMID_DSPARMS;
1569 *frm++ = 1;
1570 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1571 }
1572 bo->bo_tim = frm;
1573 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1574 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1575 *frm++ = 2;
1576 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1577 bo->bo_tim_len = 0;
1578 } else {
1579 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1580
1581 tie->tim_ie = IEEE80211_ELEMID_TIM;
1582 tie->tim_len = 4; /* length */
1583 tie->tim_count = 0; /* DTIM count */
1584 tie->tim_period = ic->ic_dtim_period; /* DTIM period */
1585 tie->tim_bitctl = 0; /* bitmap control */
1586 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
1587 frm += sizeof(struct ieee80211_tim_ie);
1588 bo->bo_tim_len = 1;
1589 }
1590 bo->bo_trailer = frm;
1591 if (ic->ic_flags & IEEE80211_F_WME) {
1592 bo->bo_wme = frm;
1593 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1594 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1595 }
1596 if (ic->ic_flags & IEEE80211_F_WPA)
1597 frm = ieee80211_add_wpa(frm, ic);
1598 if (ic->ic_curmode == IEEE80211_MODE_11G)
1599 frm = ieee80211_add_erp(frm, ic);
1600 efrm = ieee80211_add_xrates(frm, rs);
1601 bo->bo_trailer_len = efrm - bo->bo_trailer;
1602 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1603
1604 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1605 IASSERT(m != NULL, ("no space for 802.11 header?"));
1606 wh = mtod(m, struct ieee80211_frame *);
1607 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1608 IEEE80211_FC0_SUBTYPE_BEACON;
1609 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1610 *(u_int16_t *)wh->i_dur = 0;
1611 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1612 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1613 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1614 *(u_int16_t *)wh->i_seq = 0;
1615
1616 return m;
1617 }
1618
1619 /*
1620 * Update the dynamic parts of a beacon frame based on the current state.
1621 */
1622 int
1623 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1624 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1625 {
1626 int len_changed = 0;
1627 u_int16_t capinfo;
1628
1629 IEEE80211_BEACON_LOCK(ic);
1630 /* XXX faster to recalculate entirely or just changes? */
1631 if (ic->ic_opmode == IEEE80211_M_IBSS)
1632 capinfo = IEEE80211_CAPINFO_IBSS;
1633 else
1634 capinfo = IEEE80211_CAPINFO_ESS;
1635 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1636 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1637 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1638 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1639 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1640 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1641 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1642 *bo->bo_caps = htole16(capinfo);
1643
1644 if (ic->ic_flags & IEEE80211_F_WME) {
1645 struct ieee80211_wme_state *wme = &ic->ic_wme;
1646
1647 /*
1648 * Check for agressive mode change. When there is
1649 * significant high priority traffic in the BSS
1650 * throttle back BE traffic by using conservative
1651 * parameters. Otherwise BE uses agressive params
1652 * to optimize performance of legacy/non-QoS traffic.
1653 */
1654 if (wme->wme_flags & WME_F_AGGRMODE) {
1655 if (wme->wme_hipri_traffic >
1656 wme->wme_hipri_switch_thresh) {
1657 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1658 "%s: traffic %u, disable aggressive mode\n",
1659 __func__, wme->wme_hipri_traffic);
1660 wme->wme_flags &= ~WME_F_AGGRMODE;
1661 ieee80211_wme_updateparams_locked(ic);
1662 wme->wme_hipri_traffic =
1663 wme->wme_hipri_switch_hysteresis;
1664 } else
1665 wme->wme_hipri_traffic = 0;
1666 } else {
1667 if (wme->wme_hipri_traffic <=
1668 wme->wme_hipri_switch_thresh) {
1669 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1670 "%s: traffic %u, enable aggressive mode\n",
1671 __func__, wme->wme_hipri_traffic);
1672 wme->wme_flags |= WME_F_AGGRMODE;
1673 ieee80211_wme_updateparams_locked(ic);
1674 wme->wme_hipri_traffic = 0;
1675 } else
1676 wme->wme_hipri_traffic =
1677 wme->wme_hipri_switch_hysteresis;
1678 }
1679 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1680 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
1681 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1682 }
1683 }
1684
1685 #ifndef IEEE80211_NO_HOSTAP
1686 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/
1687 struct ieee80211_tim_ie *tie =
1688 (struct ieee80211_tim_ie *) bo->bo_tim;
1689 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1690 u_int timlen, timoff, i;
1691 /*
1692 * ATIM/DTIM needs updating. If it fits in the
1693 * current space allocated then just copy in the
1694 * new bits. Otherwise we need to move any trailing
1695 * data to make room. Note that we know there is
1696 * contiguous space because ieee80211_beacon_allocate
1697 * insures there is space in the mbuf to write a
1698 * maximal-size virtual bitmap (based on ic_max_aid).
1699 */
1700 /*
1701 * Calculate the bitmap size and offset, copy any
1702 * trailer out of the way, and then copy in the
1703 * new bitmap and update the information element.
1704 * Note that the tim bitmap must contain at least
1705 * one byte and any offset must be even.
1706 */
1707 if (ic->ic_ps_pending != 0) {
1708 timoff = 128; /* impossibly large */
1709 for (i = 0; i < ic->ic_tim_len; i++)
1710 if (ic->ic_tim_bitmap[i]) {
1711 timoff = i &~ 1;
1712 break;
1713 }
1714 IASSERT(timoff != 128, ("tim bitmap empty!"));
1715 for (i = ic->ic_tim_len-1; i >= timoff; i--)
1716 if (ic->ic_tim_bitmap[i])
1717 break;
1718 timlen = 1 + (i - timoff);
1719 } else {
1720 timoff = 0;
1721 timlen = 1;
1722 }
1723 if (timlen != bo->bo_tim_len) {
1724 /* copy up/down trailer */
1725 ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
1726 bo->bo_trailer_len);
1727 bo->bo_trailer = tie->tim_bitmap+timlen;
1728 bo->bo_wme = bo->bo_trailer;
1729 bo->bo_tim_len = timlen;
1730
1731 /* update information element */
1732 tie->tim_len = 3 + timlen;
1733 tie->tim_bitctl = timoff;
1734 len_changed = 1;
1735 }
1736 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1737 bo->bo_tim_len);
1738
1739 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1740
1741 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1742 "%s: TIM updated, pending %u, off %u, len %u\n",
1743 __func__, ic->ic_ps_pending, timoff, timlen);
1744 }
1745 /* count down DTIM period */
1746 if (tie->tim_count == 0)
1747 tie->tim_count = tie->tim_period - 1;
1748 else
1749 tie->tim_count--;
1750 /* update state for buffered multicast frames on DTIM */
1751 if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
1752 tie->tim_bitctl |= 1;
1753 else
1754 tie->tim_bitctl &= ~1;
1755 }
1756 #endif /* !IEEE80211_NO_HOSTAP */
1757 IEEE80211_BEACON_UNLOCK(ic);
1758
1759 return len_changed;
1760 }
1761
1762 /*
1763 * Save an outbound packet for a node in power-save sleep state.
1764 * The new packet is placed on the node's saved queue, and the TIM
1765 * is changed, if necessary.
1766 */
1767 void
1768 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1769 struct mbuf *m)
1770 {
1771 int qlen, age;
1772
1773 IEEE80211_NODE_SAVEQ_LOCK(ni);
1774 if (IF_QFULL(&ni->ni_savedq)) {
1775 IF_DROP(&ni->ni_savedq);
1776 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1777 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1778 "[%s] pwr save q overflow, drops %d (size %d)\n",
1779 ether_sprintf(ni->ni_macaddr),
1780 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1781 #ifdef IEEE80211_DEBUG
1782 if (ieee80211_msg_dumppkts(ic))
1783 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1784 #endif
1785 m_freem(m);
1786 return;
1787 }
1788 /*
1789 * Tag the frame with it's expiry time and insert
1790 * it in the queue. The aging interval is 4 times
1791 * the listen interval specified by the station.
1792 * Frames that sit around too long are reclaimed
1793 * using this information.
1794 */
1795 /* XXX handle overflow? */
1796 age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
1797 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1798 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1799
1800 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1801 "[%s] save frame, %u now queued\n",
1802 ether_sprintf(ni->ni_macaddr), qlen);
1803
1804 if (qlen == 1)
1805 ic->ic_set_tim(ic, ni, 1);
1806 }
1807