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