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