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