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