ieee80211_output.c revision 1.12 1 /* $NetBSD: ieee80211_output.c,v 1.12 2004/04/30 23:58:17 dyoung Exp $ */
2 /*-
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002, 2003 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.10 2004/04/02 23:25:39 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.12 2004/04/30 23:58:17 dyoung Exp $");
39 #endif
40
41 #include "opt_inet.h"
42
43 #ifdef __NetBSD__
44 #include "bpfilter.h"
45 #endif /* __NetBSD__ */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/endian.h>
55 #include <sys/errno.h>
56 #ifdef __FreeBSD__
57 #include <sys/bus.h>
58 #endif
59 #include <sys/proc.h>
60 #include <sys/sysctl.h>
61
62 #ifdef __FreeBSD__
63 #include <machine/atomic.h>
64 #endif
65
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_arp.h>
70 #ifdef __FreeBSD__
71 #include <net/ethernet.h>
72 #else
73 #include <net/if_ether.h>
74 #endif
75 #include <net/if_llc.h>
76
77 #include <net80211/ieee80211_var.h>
78 #include <net80211/ieee80211_compat.h>
79
80 #if NBPFILTER > 0
81 #include <net/bpf.h>
82 #endif
83
84 #ifdef INET
85 #include <netinet/in.h>
86 #ifdef __FreeBSD__
87 #include <netinet/if_ether.h>
88 #else
89 #include <net/if_ether.h>
90 #endif
91 #endif
92
93 /*
94 * Send a management frame to the specified node. The node pointer
95 * must have a reference as the pointer will be passed to the driver
96 * and potentially held for a long time. If the frame is successfully
97 * dispatched to the driver, then it is responsible for freeing the
98 * reference (and potentially free'ing up any associated storage).
99 */
100 static int
101 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
102 struct mbuf *m, int type)
103 {
104 struct ieee80211com *ic = (void *)ifp;
105 struct ieee80211_frame *wh;
106
107 IASSERT(ni != NULL, ("null node"));
108 ni->ni_inact = 0;
109
110 /*
111 * Yech, hack alert! We want to pass the node down to the
112 * driver's start routine. If we don't do so then the start
113 * routine must immediately look it up again and that can
114 * cause a lock order reversal if, for example, this frame
115 * is being sent because the station is being timedout and
116 * the frame being sent is a DEAUTH message. We could stick
117 * this in an m_tag and tack that on to the mbuf. However
118 * that's rather expensive to do for every frame so instead
119 * we stuff it in the rcvif field since outbound frames do
120 * not (presently) use this.
121 */
122 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
123 if (m == NULL)
124 return ENOMEM;
125 #ifdef __FreeBSD__
126 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
127 #endif
128 m->m_pkthdr.rcvif = (void *)ni;
129
130 wh = mtod(m, struct ieee80211_frame *);
131 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
132 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
133 *(u_int16_t *)&wh->i_dur[0] = 0;
134 *(u_int16_t *)&wh->i_seq[0] =
135 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
136 ni->ni_txseq++;
137 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
138 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
139 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
140
141 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
142 m->m_flags &= ~M_LINK0;
143 IEEE80211_DPRINTF(("%s: encrypting frame for %s\n", __func__,
144 ether_sprintf(wh->i_addr1)));
145 wh->i_fc[1] |= IEEE80211_FC1_WEP;
146 }
147
148 if (ifp->if_flags & IFF_DEBUG) {
149 /* avoid to print too many frames */
150 if (ic->ic_opmode == IEEE80211_M_IBSS ||
151 #ifdef IEEE80211_DEBUG
152 ieee80211_debug > 1 ||
153 #endif
154 (type & IEEE80211_FC0_SUBTYPE_MASK) !=
155 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
156 if_printf(ifp, "sending %s to %s on channel %u\n",
157 ieee80211_mgt_subtype_name[
158 (type & IEEE80211_FC0_SUBTYPE_MASK)
159 >> IEEE80211_FC0_SUBTYPE_SHIFT],
160 ether_sprintf(ni->ni_macaddr),
161 ieee80211_chan2ieee(ic, ni->ni_chan));
162 }
163
164 IF_ENQUEUE(&ic->ic_mgtq, m);
165 ifp->if_timer = 1;
166 (*ifp->if_start)(ifp);
167 return 0;
168 }
169
170 /*
171 * Encapsulate an outbound data frame. The mbuf chain is updated and
172 * a reference to the destination node is returned. If an error is
173 * encountered NULL is returned and the node reference will also be NULL.
174 *
175 * NB: The caller is responsible for free'ing a returned node reference.
176 * The convention is ic_bss is not reference counted; the caller must
177 * maintain that.
178 */
179 struct mbuf *
180 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
181 {
182 struct ieee80211com *ic = (void *)ifp;
183 struct ether_header eh;
184 struct ieee80211_frame *wh;
185 struct ieee80211_node *ni = NULL;
186 struct llc *llc;
187
188 if (m->m_len < sizeof(struct ether_header)) {
189 m = m_pullup(m, sizeof(struct ether_header));
190 if (m == NULL) {
191 ic->ic_stats.is_tx_nombuf++;
192 goto bad;
193 }
194 }
195 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
196
197 ni = ieee80211_find_txnode(ic, eh.ether_dhost);
198 if (ni == NULL) {
199 IEEE80211_DPRINTF(("%s: no node for dst %s, discard frame\n",
200 __func__, ether_sprintf(eh.ether_dhost)));
201 ic->ic_stats.is_tx_nonode++;
202 goto bad;
203 }
204 ni->ni_inact = 0;
205
206 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
207 llc = mtod(m, struct llc *);
208 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
209 llc->llc_control = LLC_UI;
210 llc->llc_snap.org_code[0] = 0;
211 llc->llc_snap.org_code[1] = 0;
212 llc->llc_snap.org_code[2] = 0;
213 llc->llc_snap.ether_type = eh.ether_type;
214 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
215 if (m == NULL) {
216 ic->ic_stats.is_tx_nombuf++;
217 goto bad;
218 }
219 wh = mtod(m, struct ieee80211_frame *);
220 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
221 *(u_int16_t *)&wh->i_dur[0] = 0;
222 *(u_int16_t *)&wh->i_seq[0] =
223 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
224 ni->ni_txseq++;
225 switch (ic->ic_opmode) {
226 case IEEE80211_M_STA:
227 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
228 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
229 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
230 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
231 break;
232 case IEEE80211_M_IBSS:
233 case IEEE80211_M_AHDEMO:
234 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
235 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
236 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
237 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
238 break;
239 case IEEE80211_M_HOSTAP:
240 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
241 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
242 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
243 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
244 break;
245 case IEEE80211_M_MONITOR:
246 goto bad;
247 }
248 *pni = ni;
249 return m;
250 bad:
251 if (m != NULL)
252 m_freem(m);
253 if (ni && ni != ic->ic_bss)
254 ieee80211_free_node(ic, ni);
255 *pni = NULL;
256 return NULL;
257 }
258
259 /*
260 * Add a supported rates element id to a frame.
261 */
262 u_int8_t *
263 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
264 {
265 int nrates;
266
267 *frm++ = IEEE80211_ELEMID_RATES;
268 nrates = rs->rs_nrates;
269 if (nrates > IEEE80211_RATE_SIZE)
270 nrates = IEEE80211_RATE_SIZE;
271 *frm++ = nrates;
272 memcpy(frm, rs->rs_rates, nrates);
273 return frm + nrates;
274 }
275
276 /*
277 * Add an extended supported rates element id to a frame.
278 */
279 u_int8_t *
280 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
281 {
282 /*
283 * Add an extended supported rates element if operating in 11g mode.
284 */
285 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
286 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
287 *frm++ = IEEE80211_ELEMID_XRATES;
288 *frm++ = nrates;
289 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
290 frm += nrates;
291 }
292 return frm;
293 }
294
295 /*
296 * Add an ssid elemet to a frame.
297 */
298 static u_int8_t *
299 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
300 {
301 *frm++ = IEEE80211_ELEMID_SSID;
302 *frm++ = len;
303 memcpy(frm, ssid, len);
304 return frm + len;
305 }
306
307 static struct mbuf *
308 ieee80211_getmbuf(int flags, int type, u_int pktlen)
309 {
310 struct mbuf *m;
311
312 IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
313 #ifdef __FreeBSD__
314 if (pktlen <= MHLEN)
315 MGETHDR(m, flags, type);
316 else
317 m = m_getcl(flags, type, M_PKTHDR);
318 #else
319 MGETHDR(m, flags, type);
320 if (m != NULL && pktlen > MHLEN)
321 MCLGET(m, flags);
322 #endif
323 return m;
324 }
325
326 /*
327 * Send a management frame. The node is for the destination (or ic_bss
328 * when in station mode). Nodes other than ic_bss have their reference
329 * count bumped to reflect our use for an indeterminant time.
330 */
331 int
332 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
333 int type, int arg)
334 {
335 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
336 struct ifnet *ifp = &ic->ic_if;
337 struct mbuf *m;
338 u_int8_t *frm;
339 enum ieee80211_phymode mode;
340 u_int16_t capinfo;
341 int has_challenge, is_shared_key, ret, timer;
342
343 IASSERT(ni != NULL, ("null node"));
344
345 /*
346 * Hold a reference on the node so it doesn't go away until after
347 * the xmit is complete all the way in the driver. On error we
348 * will remove our reference.
349 */
350 if (ni != ic->ic_bss)
351 ieee80211_ref_node(ni);
352 timer = 0;
353 switch (type) {
354 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
355 /*
356 * prreq frame format
357 * [tlv] ssid
358 * [tlv] supported rates
359 * [tlv] extended supported rates
360 */
361 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
362 2 + ic->ic_des_esslen
363 + 2 + IEEE80211_RATE_SIZE
364 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
365 if (m == NULL)
366 senderr(ENOMEM, is_tx_nombuf);
367 m->m_data += sizeof(struct ieee80211_frame);
368 frm = mtod(m, u_int8_t *);
369 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
370 mode = ieee80211_chan2mode(ic, ni->ni_chan);
371 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
372 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
373 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
374
375 timer = IEEE80211_TRANS_WAIT;
376 break;
377
378 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
379 /*
380 * probe response frame format
381 * [8] time stamp
382 * [2] beacon interval
383 * [2] cabability information
384 * [tlv] ssid
385 * [tlv] supported rates
386 * [tlv] parameter set (FH/DS)
387 * [tlv] parameter set (IBSS)
388 * [tlv] extended supported rates
389 */
390 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
391 8 + 2 + 2 + 2
392 + 2 + ni->ni_esslen
393 + 2 + IEEE80211_RATE_SIZE
394 + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
395 + 6
396 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
397 if (m == NULL)
398 senderr(ENOMEM, is_tx_nombuf);
399 m->m_data += sizeof(struct ieee80211_frame);
400 frm = mtod(m, u_int8_t *);
401
402 memset(frm, 0, 8); /* timestamp should be filled later */
403 frm += 8;
404 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
405 frm += 2;
406 if (ic->ic_opmode == IEEE80211_M_IBSS)
407 capinfo = IEEE80211_CAPINFO_IBSS;
408 else
409 capinfo = IEEE80211_CAPINFO_ESS;
410 if (ic->ic_flags & IEEE80211_F_WEPON)
411 capinfo |= IEEE80211_CAPINFO_PRIVACY;
412 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
413 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
414 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
415 *(u_int16_t *)frm = htole16(capinfo);
416 frm += 2;
417
418 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
419 ic->ic_bss->ni_esslen);
420 frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
421
422 if (ic->ic_phytype == IEEE80211_T_FH) {
423 *frm++ = IEEE80211_ELEMID_FHPARMS;
424 *frm++ = 5;
425 *frm++ = ni->ni_fhdwell & 0x00ff;
426 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
427 *frm++ = IEEE80211_FH_CHANSET(
428 ieee80211_chan2ieee(ic, ni->ni_chan));
429 *frm++ = IEEE80211_FH_CHANPAT(
430 ieee80211_chan2ieee(ic, ni->ni_chan));
431 *frm++ = ni->ni_fhindex;
432 } else {
433 *frm++ = IEEE80211_ELEMID_DSPARMS;
434 *frm++ = 1;
435 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
436 }
437
438 if (ic->ic_opmode == IEEE80211_M_IBSS) {
439 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
440 *frm++ = 2;
441 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
442 } else { /* IEEE80211_M_HOSTAP */
443 /* TODO: TIM */
444 *frm++ = IEEE80211_ELEMID_TIM;
445 *frm++ = 4; /* length */
446 *frm++ = 0; /* DTIM count */
447 *frm++ = 1; /* DTIM period */
448 *frm++ = 0; /* bitmap control */
449 *frm++ = 0; /* Partial Virtual Bitmap (variable length) */
450 }
451 frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
452 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
453 break;
454
455 case IEEE80211_FC0_SUBTYPE_AUTH:
456 MGETHDR(m, M_DONTWAIT, MT_DATA);
457 if (m == NULL)
458 senderr(ENOMEM, is_tx_nombuf);
459
460 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
461 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
462 ni->ni_challenge != NULL);
463
464 is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
465 arg == IEEE80211_AUTH_SHARED_PASS);
466
467 if (has_challenge) {
468 MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
469 m->m_pkthdr.len = m->m_len =
470 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
471 } else {
472 MH_ALIGN(m, 2 * 3);
473 m->m_pkthdr.len = m->m_len = 2 * 3;
474 }
475 frm = mtod(m, u_int8_t *);
476 ((u_int16_t *)frm)[0] =
477 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
478 : htole16(IEEE80211_AUTH_ALG_OPEN);
479 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
480 ((u_int16_t *)frm)[2] = 0; /* status */
481
482 if (has_challenge) {
483 ((u_int16_t *)frm)[3] =
484 htole16((IEEE80211_CHALLENGE_LEN << 8) |
485 IEEE80211_ELEMID_CHALLENGE);
486 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
487 IEEE80211_CHALLENGE_LEN);
488 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
489 IEEE80211_DPRINTF((
490 "%s: request encrypt frame\n", __func__));
491 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
492 }
493 }
494 if (ic->ic_opmode == IEEE80211_M_STA)
495 timer = IEEE80211_TRANS_WAIT;
496 break;
497
498 case IEEE80211_FC0_SUBTYPE_DEAUTH:
499 if (ifp->if_flags & IFF_DEBUG)
500 if_printf(ifp, "station %s deauthenticate (reason %d)\n",
501 ether_sprintf(ni->ni_macaddr), arg);
502 MGETHDR(m, M_DONTWAIT, MT_DATA);
503 if (m == NULL)
504 senderr(ENOMEM, is_tx_nombuf);
505 MH_ALIGN(m, 2);
506 m->m_pkthdr.len = m->m_len = 2;
507 *mtod(m, u_int16_t *) = htole16(arg); /* reason */
508 break;
509
510 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
511 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
512 /*
513 * asreq frame format
514 * [2] capability information
515 * [2] listen interval
516 * [6*] current AP address (reassoc only)
517 * [tlv] ssid
518 * [tlv] supported rates
519 * [tlv] extended supported rates
520 */
521 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
522 sizeof(capinfo)
523 + sizeof(u_int16_t)
524 + IEEE80211_ADDR_LEN
525 + 2 + ni->ni_esslen
526 + 2 + IEEE80211_RATE_SIZE
527 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
528 if (m == NULL)
529 senderr(ENOMEM, is_tx_nombuf);
530 m->m_data += sizeof(struct ieee80211_frame);
531 frm = mtod(m, u_int8_t *);
532
533 capinfo = 0;
534 if (ic->ic_opmode == IEEE80211_M_IBSS)
535 capinfo |= IEEE80211_CAPINFO_IBSS;
536 else /* IEEE80211_M_STA */
537 capinfo |= IEEE80211_CAPINFO_ESS;
538 if (ic->ic_flags & IEEE80211_F_WEPON)
539 capinfo |= IEEE80211_CAPINFO_PRIVACY;
540 /*
541 * NB: Some 11a AP's reject the request when
542 * short premable is set.
543 */
544 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
545 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
546 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
547 if (ic->ic_flags & IEEE80211_F_SHSLOT)
548 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
549 *(u_int16_t *)frm = htole16(capinfo);
550 frm += 2;
551
552 *(u_int16_t *)frm = htole16(ic->ic_lintval);
553 frm += 2;
554
555 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
556 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
557 frm += IEEE80211_ADDR_LEN;
558 }
559
560 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
561 frm = ieee80211_add_rates(frm, &ni->ni_rates);
562 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
563 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
564
565 timer = IEEE80211_TRANS_WAIT;
566 break;
567
568 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
569 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
570 /*
571 * asreq frame format
572 * [2] capability information
573 * [2] status
574 * [2] association ID
575 * [tlv] supported rates
576 * [tlv] extended supported rates
577 */
578 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
579 sizeof(capinfo)
580 + sizeof(u_int16_t)
581 + sizeof(u_int16_t)
582 + 2 + IEEE80211_RATE_SIZE
583 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
584 if (m == NULL)
585 senderr(ENOMEM, is_tx_nombuf);
586 m->m_data += sizeof(struct ieee80211_frame);
587 frm = mtod(m, u_int8_t *);
588
589 capinfo = IEEE80211_CAPINFO_ESS;
590 if (ic->ic_flags & IEEE80211_F_WEPON)
591 capinfo |= IEEE80211_CAPINFO_PRIVACY;
592 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
593 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
594 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
595 *(u_int16_t *)frm = htole16(capinfo);
596 frm += 2;
597
598 *(u_int16_t *)frm = htole16(arg); /* status */
599 frm += 2;
600
601 if (arg == IEEE80211_STATUS_SUCCESS)
602 *(u_int16_t *)frm = htole16(ni->ni_associd);
603 frm += 2;
604
605 frm = ieee80211_add_rates(frm, &ni->ni_rates);
606 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
607 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
608 break;
609
610 case IEEE80211_FC0_SUBTYPE_DISASSOC:
611 if (ifp->if_flags & IFF_DEBUG)
612 if_printf(ifp, "station %s disassociate (reason %d)\n",
613 ether_sprintf(ni->ni_macaddr), arg);
614 MGETHDR(m, M_DONTWAIT, MT_DATA);
615 if (m == NULL)
616 senderr(ENOMEM, is_tx_nombuf);
617 MH_ALIGN(m, 2);
618 m->m_pkthdr.len = m->m_len = 2;
619 *mtod(m, u_int16_t *) = htole16(arg); /* reason */
620 break;
621
622 default:
623 IEEE80211_DPRINTF(("%s: invalid mgmt frame type %u\n",
624 __func__, type));
625 senderr(EINVAL, is_tx_unknownmgt);
626 /* NOTREACHED */
627 }
628
629 ret = ieee80211_mgmt_output(ifp, ni, m, type);
630 if (ret == 0) {
631 if (timer)
632 ic->ic_mgt_timer = timer;
633 } else {
634 bad:
635 if (ni != ic->ic_bss) /* remove ref we added */
636 ieee80211_free_node(ic, ni);
637 }
638 return ret;
639 #undef senderr
640 }
641
642 void
643 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
644 struct mbuf *m)
645 {
646 /* Store the new packet on our queue, changing the TIM if necessary */
647
648 if (IF_IS_EMPTY(&ni->ni_savedq)) {
649 ic->ic_set_tim(ic, ni->ni_associd, 1);
650 }
651 if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
652 IF_DROP(&ni->ni_savedq);
653 m_freem(m);
654 if (ic->ic_if.if_flags & IFF_DEBUG)
655 printf("%s: station %s power save queue overflow"
656 " of size %d drops %d\n",
657 ic->ic_if.if_xname,
658 ether_sprintf(ni->ni_macaddr),
659 IEEE80211_PS_MAX_QUEUE,
660 ni->ni_savedq.ifq_drops);
661 } else {
662 /* Similar to ieee80211_mgmt_output, store the node in
663 * the rcvif field.
664 */
665 IF_ENQUEUE(&ni->ni_savedq, m);
666 m->m_pkthdr.rcvif = (void *)ni;
667 }
668 }
669
670