ieee80211_output.c revision 1.42 1 /* $NetBSD: ieee80211_output.c,v 1.42 2006/02/19 07:49:28 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.42 2006/02/19 07:49:28 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 cryptolen, /* crypto overhead: header+trailer */
790 firstlen, /* first fragment's payload + overhead length */
791 hdrlen, /* header length w/o driver padding */
792 lastlen, /* last fragment's payload length w/ overhead */
793 lastlen0, /* last fragment's payload length w/o overhead */
794 npkt, /* number of fragments */
795 overlen, /* non-802.11 header overhead per fragment */
796 paylen; /* payload length w/o overhead */
797
798 hdrlen = ieee80211_anyhdrsize((const void *)wh);
799
800 /* Account for padding required by the driver. */
801 if (icflags & IEEE80211_F_DATAPAD)
802 paylen = len - roundup(hdrlen, sizeof(u_int32_t));
803 else
804 paylen = len - hdrlen;
805
806 overlen = IEEE80211_CRC_LEN;
807
808 if (wk != NULL) {
809 cryptolen = wk->wk_cipher->ic_header +
810 wk->wk_cipher->ic_trailer;
811 paylen -= cryptolen;
812 overlen += cryptolen;
813 }
814
815 npkt = paylen / fraglen;
816 lastlen0 = paylen % fraglen;
817
818 if (npkt == 0) /* no fragments */
819 lastlen = paylen + overlen;
820 else if (lastlen0 != 0) { /* a short "tail" fragment */
821 lastlen = lastlen0 + overlen;
822 npkt++;
823 } else /* full-length "tail" fragment */
824 lastlen = fraglen + overlen;
825
826 if (npktp != NULL)
827 *npktp = npkt;
828
829 if (npkt > 1)
830 firstlen = fraglen + overlen;
831 else
832 firstlen = paylen + overlen;
833
834 if (debug) {
835 printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d "
836 "fraglen %d overlen %d len %d rate %d icflags %08x\n",
837 __func__, npkt, firstlen, lastlen0, lastlen, fraglen,
838 overlen, len, rate, icflags);
839 }
840
841 ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
842 (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL;
843
844 rc = ieee80211_compute_duration1(firstlen + hdrlen,
845 ack, icflags, rate, d0);
846 if (rc == -1)
847 return rc;
848
849 if (npkt <= 1) {
850 *dn = *d0;
851 return 0;
852 }
853 return ieee80211_compute_duration1(lastlen + hdrlen, ack, icflags, rate,
854 dn);
855 }
856
857 /*
858 * Add a supported rates element id to a frame.
859 */
860 static u_int8_t *
861 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
862 {
863 int nrates;
864
865 *frm++ = IEEE80211_ELEMID_RATES;
866 nrates = rs->rs_nrates;
867 if (nrates > IEEE80211_RATE_SIZE)
868 nrates = IEEE80211_RATE_SIZE;
869 *frm++ = nrates;
870 memcpy(frm, rs->rs_rates, nrates);
871 return frm + nrates;
872 }
873
874 /*
875 * Add an extended supported rates element id to a frame.
876 */
877 static u_int8_t *
878 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
879 {
880 /*
881 * Add an extended supported rates element if operating in 11g mode.
882 */
883 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
884 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
885 *frm++ = IEEE80211_ELEMID_XRATES;
886 *frm++ = nrates;
887 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
888 frm += nrates;
889 }
890 return frm;
891 }
892
893 /*
894 * Add an ssid elemet to a frame.
895 */
896 static u_int8_t *
897 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
898 {
899 *frm++ = IEEE80211_ELEMID_SSID;
900 *frm++ = len;
901 memcpy(frm, ssid, len);
902 return frm + len;
903 }
904
905 /*
906 * Add an erp element to a frame.
907 */
908 static u_int8_t *
909 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
910 {
911 u_int8_t erp;
912
913 *frm++ = IEEE80211_ELEMID_ERP;
914 *frm++ = 1;
915 erp = 0;
916 if (ic->ic_nonerpsta != 0)
917 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
918 if (ic->ic_flags & IEEE80211_F_USEPROT)
919 erp |= IEEE80211_ERP_USE_PROTECTION;
920 if (ic->ic_flags & IEEE80211_F_USEBARKER)
921 erp |= IEEE80211_ERP_LONG_PREAMBLE;
922 *frm++ = erp;
923 return frm;
924 }
925
926 static u_int8_t *
927 ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
928 {
929 #define WPA_OUI_BYTES 0x00, 0x50, 0xf2
930 #define ADDSHORT(frm, v) do { \
931 frm[0] = (v) & 0xff; \
932 frm[1] = (v) >> 8; \
933 frm += 2; \
934 } while (0)
935 #define ADDSELECTOR(frm, sel) do { \
936 memcpy(frm, sel, 4); \
937 frm += 4; \
938 } while (0)
939 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
940 static const u_int8_t cipher_suite[][4] = {
941 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */
942 { WPA_OUI_BYTES, WPA_CSE_TKIP },
943 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */
944 { WPA_OUI_BYTES, WPA_CSE_CCMP },
945 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
946 { WPA_OUI_BYTES, WPA_CSE_NULL },
947 };
948 static const u_int8_t wep104_suite[4] =
949 { WPA_OUI_BYTES, WPA_CSE_WEP104 };
950 static const u_int8_t key_mgt_unspec[4] =
951 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
952 static const u_int8_t key_mgt_psk[4] =
953 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
954 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
955 u_int8_t *frm = ie;
956 u_int8_t *selcnt;
957
958 *frm++ = IEEE80211_ELEMID_VENDOR;
959 *frm++ = 0; /* length filled in below */
960 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */
961 frm += sizeof(oui);
962 ADDSHORT(frm, WPA_VERSION);
963
964 /* XXX filter out CKIP */
965
966 /* multicast cipher */
967 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
968 rsn->rsn_mcastkeylen >= 13)
969 ADDSELECTOR(frm, wep104_suite);
970 else
971 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
972
973 /* unicast cipher list */
974 selcnt = frm;
975 ADDSHORT(frm, 0); /* selector count */
976 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
977 selcnt[0]++;
978 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
979 }
980 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
981 selcnt[0]++;
982 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
983 }
984
985 /* authenticator selector list */
986 selcnt = frm;
987 ADDSHORT(frm, 0); /* selector count */
988 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
989 selcnt[0]++;
990 ADDSELECTOR(frm, key_mgt_unspec);
991 }
992 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
993 selcnt[0]++;
994 ADDSELECTOR(frm, key_mgt_psk);
995 }
996
997 /* optional capabilities */
998 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
999 ADDSHORT(frm, rsn->rsn_caps);
1000
1001 /* calculate element length */
1002 ie[1] = frm - ie - 2;
1003 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
1004 ("WPA IE too big, %u > %zu",
1005 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
1006 return frm;
1007 #undef ADDSHORT
1008 #undef ADDSELECTOR
1009 #undef WPA_OUI_BYTES
1010 }
1011
1012 static u_int8_t *
1013 ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
1014 {
1015 #define RSN_OUI_BYTES 0x00, 0x0f, 0xac
1016 #define ADDSHORT(frm, v) do { \
1017 frm[0] = (v) & 0xff; \
1018 frm[1] = (v) >> 8; \
1019 frm += 2; \
1020 } while (0)
1021 #define ADDSELECTOR(frm, sel) do { \
1022 memcpy(frm, sel, 4); \
1023 frm += 4; \
1024 } while (0)
1025 static const u_int8_t cipher_suite[][4] = {
1026 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */
1027 { RSN_OUI_BYTES, RSN_CSE_TKIP },
1028 { RSN_OUI_BYTES, RSN_CSE_WRAP },
1029 { RSN_OUI_BYTES, RSN_CSE_CCMP },
1030 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */
1031 { RSN_OUI_BYTES, RSN_CSE_NULL },
1032 };
1033 static const u_int8_t wep104_suite[4] =
1034 { RSN_OUI_BYTES, RSN_CSE_WEP104 };
1035 static const u_int8_t key_mgt_unspec[4] =
1036 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
1037 static const u_int8_t key_mgt_psk[4] =
1038 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
1039 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
1040 u_int8_t *frm = ie;
1041 u_int8_t *selcnt;
1042
1043 *frm++ = IEEE80211_ELEMID_RSN;
1044 *frm++ = 0; /* length filled in below */
1045 ADDSHORT(frm, RSN_VERSION);
1046
1047 /* XXX filter out CKIP */
1048
1049 /* multicast cipher */
1050 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
1051 rsn->rsn_mcastkeylen >= 13)
1052 ADDSELECTOR(frm, wep104_suite);
1053 else
1054 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
1055
1056 /* unicast cipher list */
1057 selcnt = frm;
1058 ADDSHORT(frm, 0); /* selector count */
1059 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
1060 selcnt[0]++;
1061 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
1062 }
1063 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
1064 selcnt[0]++;
1065 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
1066 }
1067
1068 /* authenticator selector list */
1069 selcnt = frm;
1070 ADDSHORT(frm, 0); /* selector count */
1071 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
1072 selcnt[0]++;
1073 ADDSELECTOR(frm, key_mgt_unspec);
1074 }
1075 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
1076 selcnt[0]++;
1077 ADDSELECTOR(frm, key_mgt_psk);
1078 }
1079
1080 /* optional capabilities */
1081 ADDSHORT(frm, rsn->rsn_caps);
1082 /* XXX PMKID */
1083
1084 /* calculate element length */
1085 ie[1] = frm - ie - 2;
1086 IASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
1087 ("RSN IE too big, %u > %zu",
1088 ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
1089 return frm;
1090 #undef ADDSELECTOR
1091 #undef ADDSHORT
1092 #undef RSN_OUI_BYTES
1093 }
1094
1095 /*
1096 * Add a WPA/RSN element to a frame.
1097 */
1098 static u_int8_t *
1099 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
1100 {
1101
1102 IASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
1103 if (ic->ic_flags & IEEE80211_F_WPA2)
1104 frm = ieee80211_setup_rsn_ie(ic, frm);
1105 if (ic->ic_flags & IEEE80211_F_WPA1)
1106 frm = ieee80211_setup_wpa_ie(ic, frm);
1107 return frm;
1108 }
1109
1110 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1111 /*
1112 * Add a WME information element to a frame.
1113 */
1114 static u_int8_t *
1115 ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
1116 {
1117 static const struct ieee80211_wme_info info = {
1118 .wme_id = IEEE80211_ELEMID_VENDOR,
1119 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1120 .wme_oui = { WME_OUI_BYTES },
1121 .wme_type = WME_OUI_TYPE,
1122 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1123 .wme_version = WME_VERSION,
1124 .wme_info = 0,
1125 };
1126 memcpy(frm, &info, sizeof(info));
1127 return frm + sizeof(info);
1128 }
1129
1130 /*
1131 * Add a WME parameters element to a frame.
1132 */
1133 static u_int8_t *
1134 ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
1135 {
1136 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1137 #define ADDSHORT(frm, v) do { \
1138 frm[0] = (v) & 0xff; \
1139 frm[1] = (v) >> 8; \
1140 frm += 2; \
1141 } while (0)
1142 /* NB: this works 'cuz a param has an info at the front */
1143 static const struct ieee80211_wme_info param = {
1144 .wme_id = IEEE80211_ELEMID_VENDOR,
1145 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1146 .wme_oui = { WME_OUI_BYTES },
1147 .wme_type = WME_OUI_TYPE,
1148 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1149 .wme_version = WME_VERSION,
1150 };
1151 int i;
1152
1153 memcpy(frm, ¶m, sizeof(param));
1154 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1155 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1156 *frm++ = 0; /* reserved field */
1157 for (i = 0; i < WME_NUM_AC; i++) {
1158 const struct wmeParams *ac =
1159 &wme->wme_bssChanParams.cap_wmeParams[i];
1160 *frm++ = SM(i, WME_PARAM_ACI)
1161 | SM(ac->wmep_acm, WME_PARAM_ACM)
1162 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1163 ;
1164 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1165 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1166 ;
1167 ADDSHORT(frm, ac->wmep_txopLimit);
1168 }
1169 return frm;
1170 #undef SM
1171 #undef ADDSHORT
1172 }
1173 #undef WME_OUI_BYTES
1174
1175 /*
1176 * Send a probe request frame with the specified ssid
1177 * and any optional information element data.
1178 */
1179 int
1180 ieee80211_send_probereq(struct ieee80211_node *ni,
1181 const u_int8_t sa[IEEE80211_ADDR_LEN],
1182 const u_int8_t da[IEEE80211_ADDR_LEN],
1183 const u_int8_t bssid[IEEE80211_ADDR_LEN],
1184 const u_int8_t *ssid, size_t ssidlen,
1185 const void *optie, size_t optielen)
1186 {
1187 struct ieee80211com *ic = ni->ni_ic;
1188 enum ieee80211_phymode mode;
1189 struct ieee80211_frame *wh;
1190 struct mbuf *m;
1191 u_int8_t *frm;
1192
1193 /*
1194 * Hold a reference on the node so it doesn't go away until after
1195 * the xmit is complete all the way in the driver. On error we
1196 * will remove our reference.
1197 */
1198 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1199 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1200 __func__, __LINE__,
1201 ni, ether_sprintf(ni->ni_macaddr),
1202 ieee80211_node_refcnt(ni)+1);
1203 ieee80211_ref_node(ni);
1204
1205 /*
1206 * prreq frame format
1207 * [tlv] ssid
1208 * [tlv] supported rates
1209 * [tlv] extended supported rates
1210 * [tlv] user-specified ie's
1211 */
1212 m = ieee80211_getmgtframe(&frm,
1213 2 + IEEE80211_NWID_LEN
1214 + 2 + IEEE80211_RATE_SIZE
1215 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1216 + (optie != NULL ? optielen : 0)
1217 );
1218 if (m == NULL) {
1219 ic->ic_stats.is_tx_nobuf++;
1220 ieee80211_free_node(ni);
1221 return ENOMEM;
1222 }
1223
1224 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1225 mode = ieee80211_chan2mode(ic, ic->ic_curchan);
1226 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1227 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1228
1229 if (optie != NULL) {
1230 memcpy(frm, optie, optielen);
1231 frm += optielen;
1232 }
1233 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1234
1235 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1236 if (m == NULL)
1237 return ENOMEM;
1238 IASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1239 m->m_pkthdr.rcvif = (void *)ni;
1240
1241 wh = mtod(m, struct ieee80211_frame *);
1242 ieee80211_send_setup(ic, ni, wh,
1243 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1244 sa, da, bssid);
1245 /* XXX power management? */
1246
1247 IEEE80211_NODE_STAT(ni, tx_probereq);
1248 IEEE80211_NODE_STAT(ni, tx_mgmt);
1249
1250 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1251 "[%s] send probe req on channel %u\n",
1252 ether_sprintf(wh->i_addr1),
1253 ieee80211_chan2ieee(ic, ic->ic_curchan));
1254
1255 IF_ENQUEUE(&ic->ic_mgtq, m);
1256 (*ic->ic_ifp->if_start)(ic->ic_ifp);
1257 return 0;
1258 }
1259
1260 /*
1261 * Send a management frame. The node is for the destination (or ic_bss
1262 * when in station mode). Nodes other than ic_bss have their reference
1263 * count bumped to reflect our use for an indeterminant time.
1264 */
1265 int
1266 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1267 int type, int arg)
1268 {
1269 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1270 struct mbuf *m;
1271 u_int8_t *frm;
1272 u_int16_t capinfo;
1273 int has_challenge, is_shared_key, ret, timer, status;
1274
1275 IASSERT(ni != NULL, ("null node"));
1276
1277 /*
1278 * Hold a reference on the node so it doesn't go away until after
1279 * the xmit is complete all the way in the driver. On error we
1280 * will remove our reference.
1281 */
1282 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1283 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1284 __func__, __LINE__,
1285 ni, ether_sprintf(ni->ni_macaddr),
1286 ieee80211_node_refcnt(ni)+1);
1287 ieee80211_ref_node(ni);
1288
1289 timer = 0;
1290 switch (type) {
1291 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1292 /*
1293 * probe response frame format
1294 * [8] time stamp
1295 * [2] beacon interval
1296 * [2] cabability information
1297 * [tlv] ssid
1298 * [tlv] supported rates
1299 * [tlv] parameter set (FH/DS)
1300 * [tlv] parameter set (IBSS)
1301 * [tlv] extended rate phy (ERP)
1302 * [tlv] extended supported rates
1303 * [tlv] WPA
1304 * [tlv] WME (optional)
1305 */
1306 m = ieee80211_getmgtframe(&frm,
1307 8
1308 + sizeof(u_int16_t)
1309 + sizeof(u_int16_t)
1310 + 2 + IEEE80211_NWID_LEN
1311 + 2 + IEEE80211_RATE_SIZE
1312 + 7 /* max(7,3) */
1313 + 6
1314 + 3
1315 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1316 /* XXX !WPA1+WPA2 fits w/o a cluster */
1317 + (ic->ic_flags & IEEE80211_F_WPA ?
1318 2*sizeof(struct ieee80211_ie_wpa) : 0)
1319 + sizeof(struct ieee80211_wme_param)
1320 );
1321 if (m == NULL)
1322 senderr(ENOMEM, is_tx_nobuf);
1323
1324 memset(frm, 0, 8); /* timestamp should be filled later */
1325 frm += 8;
1326 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1327 frm += 2;
1328 if (ic->ic_opmode == IEEE80211_M_IBSS)
1329 capinfo = IEEE80211_CAPINFO_IBSS;
1330 else
1331 capinfo = IEEE80211_CAPINFO_ESS;
1332 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1333 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1334 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1335 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1336 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1337 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1338 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1339 *(u_int16_t *)frm = htole16(capinfo);
1340 frm += 2;
1341
1342 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1343 ic->ic_bss->ni_esslen);
1344 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1345
1346 if (ic->ic_phytype == IEEE80211_T_FH) {
1347 *frm++ = IEEE80211_ELEMID_FHPARMS;
1348 *frm++ = 5;
1349 *frm++ = ni->ni_fhdwell & 0x00ff;
1350 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1351 *frm++ = IEEE80211_FH_CHANSET(
1352 ieee80211_chan2ieee(ic, ic->ic_curchan));
1353 *frm++ = IEEE80211_FH_CHANPAT(
1354 ieee80211_chan2ieee(ic, ic->ic_curchan));
1355 *frm++ = ni->ni_fhindex;
1356 } else {
1357 *frm++ = IEEE80211_ELEMID_DSPARMS;
1358 *frm++ = 1;
1359 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan);
1360 }
1361
1362 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1363 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1364 *frm++ = 2;
1365 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1366 }
1367 if (ic->ic_flags & IEEE80211_F_WPA)
1368 frm = ieee80211_add_wpa(frm, ic);
1369 if (ic->ic_curmode == IEEE80211_MODE_11G)
1370 frm = ieee80211_add_erp(frm, ic);
1371 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1372 if (ic->ic_flags & IEEE80211_F_WME)
1373 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1374 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1375 break;
1376
1377 case IEEE80211_FC0_SUBTYPE_AUTH:
1378 status = arg >> 16;
1379 arg &= 0xffff;
1380 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1381 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1382 ni->ni_challenge != NULL);
1383
1384 /*
1385 * Deduce whether we're doing open authentication or
1386 * shared key authentication. We do the latter if
1387 * we're in the middle of a shared key authentication
1388 * handshake or if we're initiating an authentication
1389 * request and configured to use shared key.
1390 */
1391 is_shared_key = has_challenge ||
1392 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1393 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1394 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1395
1396 m = ieee80211_getmgtframe(&frm,
1397 3 * sizeof(u_int16_t)
1398 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1399 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1400 );
1401 if (m == NULL)
1402 senderr(ENOMEM, is_tx_nobuf);
1403
1404 ((u_int16_t *)frm)[0] =
1405 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1406 : htole16(IEEE80211_AUTH_ALG_OPEN);
1407 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
1408 ((u_int16_t *)frm)[2] = htole16(status);/* status */
1409
1410 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1411 ((u_int16_t *)frm)[3] =
1412 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1413 IEEE80211_ELEMID_CHALLENGE);
1414 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1415 IEEE80211_CHALLENGE_LEN);
1416 m->m_pkthdr.len = m->m_len =
1417 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1418 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1419 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1420 "[%s] request encrypt frame (%s)\n",
1421 ether_sprintf(ni->ni_macaddr), __func__);
1422 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1423 }
1424 } else
1425 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1426
1427 /* XXX not right for shared key */
1428 if (status == IEEE80211_STATUS_SUCCESS)
1429 IEEE80211_NODE_STAT(ni, tx_auth);
1430 else
1431 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1432
1433 if (ic->ic_opmode == IEEE80211_M_STA)
1434 timer = IEEE80211_TRANS_WAIT;
1435 break;
1436
1437 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1438 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1439 "[%s] send station deauthenticate (reason %d)\n",
1440 ether_sprintf(ni->ni_macaddr), arg);
1441 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1442 if (m == NULL)
1443 senderr(ENOMEM, is_tx_nobuf);
1444 *(u_int16_t *)frm = htole16(arg); /* reason */
1445 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1446
1447 IEEE80211_NODE_STAT(ni, tx_deauth);
1448 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1449
1450 ieee80211_node_unauthorize(ni); /* port closed */
1451 break;
1452
1453 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1454 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1455 /*
1456 * asreq frame format
1457 * [2] capability information
1458 * [2] listen interval
1459 * [6*] current AP address (reassoc only)
1460 * [tlv] ssid
1461 * [tlv] supported rates
1462 * [tlv] extended supported rates
1463 * [tlv] WME
1464 * [tlv] user-specified ie's
1465 */
1466 m = ieee80211_getmgtframe(&frm,
1467 sizeof(u_int16_t)
1468 + sizeof(u_int16_t)
1469 + IEEE80211_ADDR_LEN
1470 + 2 + IEEE80211_NWID_LEN
1471 + 2 + IEEE80211_RATE_SIZE
1472 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1473 + sizeof(struct ieee80211_wme_info)
1474 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1475 );
1476 if (m == NULL)
1477 senderr(ENOMEM, is_tx_nobuf);
1478
1479 capinfo = 0;
1480 if (ic->ic_opmode == IEEE80211_M_IBSS)
1481 capinfo |= IEEE80211_CAPINFO_IBSS;
1482 else /* IEEE80211_M_STA */
1483 capinfo |= IEEE80211_CAPINFO_ESS;
1484 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1485 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1486 /*
1487 * NB: Some 11a AP's reject the request when
1488 * short premable is set.
1489 */
1490 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1491 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1492 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1493 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1494 (ic->ic_caps & IEEE80211_C_SHSLOT))
1495 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1496 *(u_int16_t *)frm = htole16(capinfo);
1497 frm += 2;
1498
1499 *(u_int16_t *)frm = htole16(ic->ic_lintval);
1500 frm += 2;
1501
1502 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1503 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1504 frm += IEEE80211_ADDR_LEN;
1505 }
1506
1507 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1508 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1509 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1510 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1511 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1512 if (ic->ic_opt_ie != NULL) {
1513 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1514 frm += ic->ic_opt_ie_len;
1515 }
1516 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1517
1518 timer = IEEE80211_TRANS_WAIT;
1519 break;
1520
1521 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1522 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1523 /*
1524 * asreq frame format
1525 * [2] capability information
1526 * [2] status
1527 * [2] association ID
1528 * [tlv] supported rates
1529 * [tlv] extended supported rates
1530 * [tlv] WME (if enabled and STA enabled)
1531 */
1532 m = ieee80211_getmgtframe(&frm,
1533 sizeof(u_int16_t)
1534 + sizeof(u_int16_t)
1535 + sizeof(u_int16_t)
1536 + 2 + IEEE80211_RATE_SIZE
1537 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1538 + sizeof(struct ieee80211_wme_param)
1539 );
1540 if (m == NULL)
1541 senderr(ENOMEM, is_tx_nobuf);
1542
1543 capinfo = IEEE80211_CAPINFO_ESS;
1544 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1545 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1546 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1547 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1548 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1549 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1550 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1551 *(u_int16_t *)frm = htole16(capinfo);
1552 frm += 2;
1553
1554 *(u_int16_t *)frm = htole16(arg); /* status */
1555 frm += 2;
1556
1557 if (arg == IEEE80211_STATUS_SUCCESS) {
1558 *(u_int16_t *)frm = htole16(ni->ni_associd);
1559 IEEE80211_NODE_STAT(ni, tx_assoc);
1560 } else
1561 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1562 frm += 2;
1563
1564 frm = ieee80211_add_rates(frm, &ni->ni_rates);
1565 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1566 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1567 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1568 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1569 break;
1570
1571 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1572 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1573 "[%s] send station disassociate (reason %d)\n",
1574 ether_sprintf(ni->ni_macaddr), arg);
1575 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1576 if (m == NULL)
1577 senderr(ENOMEM, is_tx_nobuf);
1578 *(u_int16_t *)frm = htole16(arg); /* reason */
1579 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1580
1581 IEEE80211_NODE_STAT(ni, tx_disassoc);
1582 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1583 break;
1584
1585 default:
1586 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1587 "[%s] invalid mgmt frame type %u\n",
1588 ether_sprintf(ni->ni_macaddr), type);
1589 senderr(EINVAL, is_tx_unknownmgt);
1590 /* NOTREACHED */
1591 }
1592
1593 ret = ieee80211_mgmt_output(ic, ni, m, type);
1594 if (ret == 0) {
1595 if (timer)
1596 ic->ic_mgt_timer = timer;
1597 } else {
1598 bad:
1599 ieee80211_free_node(ni);
1600 }
1601 return ret;
1602 #undef senderr
1603 }
1604
1605 /*
1606 * Allocate a beacon frame and fillin the appropriate bits.
1607 */
1608 struct mbuf *
1609 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1610 struct ieee80211_beacon_offsets *bo)
1611 {
1612 struct ifnet *ifp = ic->ic_ifp;
1613 struct ieee80211_frame *wh;
1614 struct mbuf *m;
1615 int pktlen;
1616 u_int8_t *frm, *efrm;
1617 u_int16_t capinfo;
1618 struct ieee80211_rateset *rs;
1619
1620 /*
1621 * beacon frame format
1622 * [8] time stamp
1623 * [2] beacon interval
1624 * [2] cabability information
1625 * [tlv] ssid
1626 * [tlv] supported rates
1627 * [3] parameter set (DS)
1628 * [tlv] parameter set (IBSS/TIM)
1629 * [tlv] extended rate phy (ERP)
1630 * [tlv] extended supported rates
1631 * [tlv] WME parameters
1632 * [tlv] WPA/RSN parameters
1633 * XXX Vendor-specific OIDs (e.g. Atheros)
1634 * NB: we allocate the max space required for the TIM bitmap.
1635 */
1636 rs = &ni->ni_rates;
1637 pktlen = 8 /* time stamp */
1638 + sizeof(u_int16_t) /* beacon interval */
1639 + sizeof(u_int16_t) /* capabilities */
1640 + 2 + ni->ni_esslen /* ssid */
1641 + 2 + IEEE80211_RATE_SIZE /* supported rates */
1642 + 2 + 1 /* DS parameters */
1643 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */
1644 + 2 + 1 /* ERP */
1645 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1646 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */
1647 sizeof(struct ieee80211_wme_param) : 0)
1648 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
1649 2*sizeof(struct ieee80211_ie_wpa) : 0)
1650 ;
1651 m = ieee80211_getmgtframe(&frm, pktlen);
1652 if (m == NULL) {
1653 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1654 "%s: cannot get buf; size %u\n", __func__, pktlen);
1655 ic->ic_stats.is_tx_nobuf++;
1656 return NULL;
1657 }
1658
1659 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
1660 frm += 8;
1661 *(u_int16_t *)frm = htole16(ni->ni_intval);
1662 frm += 2;
1663 if (ic->ic_opmode == IEEE80211_M_IBSS)
1664 capinfo = IEEE80211_CAPINFO_IBSS;
1665 else
1666 capinfo = IEEE80211_CAPINFO_ESS;
1667 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1668 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1669 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1670 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1671 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1672 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1673 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1674 bo->bo_caps = (u_int16_t *)frm;
1675 *(u_int16_t *)frm = htole16(capinfo);
1676 frm += 2;
1677 *frm++ = IEEE80211_ELEMID_SSID;
1678 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1679 *frm++ = ni->ni_esslen;
1680 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1681 frm += ni->ni_esslen;
1682 } else
1683 *frm++ = 0;
1684 frm = ieee80211_add_rates(frm, rs);
1685 if (ic->ic_curmode != IEEE80211_MODE_FH) {
1686 *frm++ = IEEE80211_ELEMID_DSPARMS;
1687 *frm++ = 1;
1688 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1689 }
1690 bo->bo_tim = frm;
1691 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1692 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1693 *frm++ = 2;
1694 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1695 bo->bo_tim_len = 0;
1696 } else {
1697 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1698
1699 tie->tim_ie = IEEE80211_ELEMID_TIM;
1700 tie->tim_len = 4; /* length */
1701 tie->tim_count = 0; /* DTIM count */
1702 tie->tim_period = ic->ic_dtim_period; /* DTIM period */
1703 tie->tim_bitctl = 0; /* bitmap control */
1704 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
1705 frm += sizeof(struct ieee80211_tim_ie);
1706 bo->bo_tim_len = 1;
1707 }
1708 bo->bo_trailer = frm;
1709 if (ic->ic_flags & IEEE80211_F_WME) {
1710 bo->bo_wme = frm;
1711 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1712 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1713 }
1714 if (ic->ic_flags & IEEE80211_F_WPA)
1715 frm = ieee80211_add_wpa(frm, ic);
1716 if (ic->ic_curmode == IEEE80211_MODE_11G)
1717 frm = ieee80211_add_erp(frm, ic);
1718 efrm = ieee80211_add_xrates(frm, rs);
1719 bo->bo_trailer_len = efrm - bo->bo_trailer;
1720 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1721
1722 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1723 IASSERT(m != NULL, ("no space for 802.11 header?"));
1724 wh = mtod(m, struct ieee80211_frame *);
1725 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1726 IEEE80211_FC0_SUBTYPE_BEACON;
1727 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1728 *(u_int16_t *)wh->i_dur = 0;
1729 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1730 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1731 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1732 *(u_int16_t *)wh->i_seq = 0;
1733
1734 return m;
1735 }
1736
1737 /*
1738 * Update the dynamic parts of a beacon frame based on the current state.
1739 */
1740 int
1741 ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1742 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1743 {
1744 int len_changed = 0;
1745 u_int16_t capinfo;
1746
1747 IEEE80211_BEACON_LOCK(ic);
1748 /* XXX faster to recalculate entirely or just changes? */
1749 if (ic->ic_opmode == IEEE80211_M_IBSS)
1750 capinfo = IEEE80211_CAPINFO_IBSS;
1751 else
1752 capinfo = IEEE80211_CAPINFO_ESS;
1753 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1754 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1755 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1756 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1757 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1758 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1759 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1760 *bo->bo_caps = htole16(capinfo);
1761
1762 if (ic->ic_flags & IEEE80211_F_WME) {
1763 struct ieee80211_wme_state *wme = &ic->ic_wme;
1764
1765 /*
1766 * Check for agressive mode change. When there is
1767 * significant high priority traffic in the BSS
1768 * throttle back BE traffic by using conservative
1769 * parameters. Otherwise BE uses agressive params
1770 * to optimize performance of legacy/non-QoS traffic.
1771 */
1772 if (wme->wme_flags & WME_F_AGGRMODE) {
1773 if (wme->wme_hipri_traffic >
1774 wme->wme_hipri_switch_thresh) {
1775 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1776 "%s: traffic %u, disable aggressive mode\n",
1777 __func__, wme->wme_hipri_traffic);
1778 wme->wme_flags &= ~WME_F_AGGRMODE;
1779 ieee80211_wme_updateparams_locked(ic);
1780 wme->wme_hipri_traffic =
1781 wme->wme_hipri_switch_hysteresis;
1782 } else
1783 wme->wme_hipri_traffic = 0;
1784 } else {
1785 if (wme->wme_hipri_traffic <=
1786 wme->wme_hipri_switch_thresh) {
1787 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1788 "%s: traffic %u, enable aggressive mode\n",
1789 __func__, wme->wme_hipri_traffic);
1790 wme->wme_flags |= WME_F_AGGRMODE;
1791 ieee80211_wme_updateparams_locked(ic);
1792 wme->wme_hipri_traffic = 0;
1793 } else
1794 wme->wme_hipri_traffic =
1795 wme->wme_hipri_switch_hysteresis;
1796 }
1797 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1798 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
1799 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1800 }
1801 }
1802
1803 #ifndef IEEE80211_NO_HOSTAP
1804 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/
1805 struct ieee80211_tim_ie *tie =
1806 (struct ieee80211_tim_ie *) bo->bo_tim;
1807 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1808 u_int timlen, timoff, i;
1809 /*
1810 * ATIM/DTIM needs updating. If it fits in the
1811 * current space allocated then just copy in the
1812 * new bits. Otherwise we need to move any trailing
1813 * data to make room. Note that we know there is
1814 * contiguous space because ieee80211_beacon_allocate
1815 * insures there is space in the mbuf to write a
1816 * maximal-size virtual bitmap (based on ic_max_aid).
1817 */
1818 /*
1819 * Calculate the bitmap size and offset, copy any
1820 * trailer out of the way, and then copy in the
1821 * new bitmap and update the information element.
1822 * Note that the tim bitmap must contain at least
1823 * one byte and any offset must be even.
1824 */
1825 if (ic->ic_ps_pending != 0) {
1826 timoff = 128; /* impossibly large */
1827 for (i = 0; i < ic->ic_tim_len; i++)
1828 if (ic->ic_tim_bitmap[i]) {
1829 timoff = i &~ 1;
1830 break;
1831 }
1832 IASSERT(timoff != 128, ("tim bitmap empty!"));
1833 for (i = ic->ic_tim_len-1; i >= timoff; i--)
1834 if (ic->ic_tim_bitmap[i])
1835 break;
1836 timlen = 1 + (i - timoff);
1837 } else {
1838 timoff = 0;
1839 timlen = 1;
1840 }
1841 if (timlen != bo->bo_tim_len) {
1842 /* copy up/down trailer */
1843 ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
1844 bo->bo_trailer_len);
1845 bo->bo_trailer = tie->tim_bitmap+timlen;
1846 bo->bo_wme = bo->bo_trailer;
1847 bo->bo_tim_len = timlen;
1848
1849 /* update information element */
1850 tie->tim_len = 3 + timlen;
1851 tie->tim_bitctl = timoff;
1852 len_changed = 1;
1853 }
1854 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1855 bo->bo_tim_len);
1856
1857 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1858
1859 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1860 "%s: TIM updated, pending %u, off %u, len %u\n",
1861 __func__, ic->ic_ps_pending, timoff, timlen);
1862 }
1863 /* count down DTIM period */
1864 if (tie->tim_count == 0)
1865 tie->tim_count = tie->tim_period - 1;
1866 else
1867 tie->tim_count--;
1868 /* update state for buffered multicast frames on DTIM */
1869 if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
1870 tie->tim_bitctl |= 1;
1871 else
1872 tie->tim_bitctl &= ~1;
1873 }
1874 #endif /* !IEEE80211_NO_HOSTAP */
1875 IEEE80211_BEACON_UNLOCK(ic);
1876
1877 return len_changed;
1878 }
1879
1880 /*
1881 * Save an outbound packet for a node in power-save sleep state.
1882 * The new packet is placed on the node's saved queue, and the TIM
1883 * is changed, if necessary.
1884 */
1885 void
1886 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1887 struct mbuf *m)
1888 {
1889 int qlen, age;
1890
1891 IEEE80211_NODE_SAVEQ_LOCK(ni);
1892 if (IF_QFULL(&ni->ni_savedq)) {
1893 IF_DROP(&ni->ni_savedq);
1894 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1895 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1896 "[%s] pwr save q overflow, drops %d (size %d)\n",
1897 ether_sprintf(ni->ni_macaddr),
1898 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1899 #ifdef IEEE80211_DEBUG
1900 if (ieee80211_msg_dumppkts(ic))
1901 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1902 #endif
1903 m_freem(m);
1904 return;
1905 }
1906 /*
1907 * Tag the frame with it's expiry time and insert
1908 * it in the queue. The aging interval is 4 times
1909 * the listen interval specified by the station.
1910 * Frames that sit around too long are reclaimed
1911 * using this information.
1912 */
1913 /* XXX handle overflow? */
1914 age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
1915 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1916 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1917
1918 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1919 "[%s] save frame with age %d, %u now queued\n",
1920 ether_sprintf(ni->ni_macaddr), age, qlen);
1921
1922 if (qlen == 1)
1923 ic->ic_set_tim(ni, 1);
1924 }
1925