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