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