ieee80211_output.c revision 1.18 1 /* $NetBSD: ieee80211_output.c,v 1.18 2004/12/19 08:08:06 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.18 2004/12/19 08:08:06 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 #ifdef IEEE80211_DEBUG
94 /*
95 * Decide if an outbound management frame should be
96 * printed when debugging is enabled. This filters some
97 * of the less interesting frames that come frequently
98 * (e.g. beacons).
99 */
100 static __inline int
101 doprint(struct ieee80211com *ic, int subtype)
102 {
103 switch (subtype) {
104 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
105 return (ic->ic_opmode == IEEE80211_M_IBSS);
106 }
107 return 1;
108 }
109 #endif
110
111 /*
112 * Send a management frame to the specified node. The node pointer
113 * must have a reference as the pointer will be passed to the driver
114 * and potentially held for a long time. If the frame is successfully
115 * dispatched to the driver, then it is responsible for freeing the
116 * reference (and potentially free'ing up any associated storage).
117 */
118 static int
119 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
120 struct mbuf *m, int type)
121 {
122 struct ieee80211com *ic = (void *)ifp;
123 struct ieee80211_frame *wh;
124
125 IASSERT(ni != NULL, ("null node"));
126 ni->ni_inact = 0;
127
128 /*
129 * Yech, hack alert! We want to pass the node down to the
130 * driver's start routine. If we don't do so then the start
131 * routine must immediately look it up again and that can
132 * cause a lock order reversal if, for example, this frame
133 * is being sent because the station is being timedout and
134 * the frame being sent is a DEAUTH message. We could stick
135 * this in an m_tag and tack that on to the mbuf. However
136 * that's rather expensive to do for every frame so instead
137 * we stuff it in the rcvif field since outbound frames do
138 * not (presently) use this.
139 */
140 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
141 if (m == NULL)
142 return ENOMEM;
143 #ifdef __FreeBSD__
144 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
145 #endif
146 m->m_pkthdr.rcvif = (void *)ni;
147
148 wh = mtod(m, struct ieee80211_frame *);
149 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
150 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
151 *(u_int16_t *)&wh->i_dur[0] = 0;
152 *(u_int16_t *)&wh->i_seq[0] =
153 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
154 ni->ni_txseq++;
155 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
156 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
157 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
158
159 if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
160 m->m_flags &= ~M_LINK0;
161 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
162 ("%s: encrypting frame for %s\n",
163 __func__, ether_sprintf(wh->i_addr1)));
164 wh->i_fc[1] |= IEEE80211_FC1_WEP;
165 }
166 #ifdef IEEE80211_DEBUG
167 /* avoid printing too many frames */
168 if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
169 ieee80211_msg_dumppkts(ic)) {
170 if_printf(ifp, "sending %s to %s on channel %u\n",
171 ieee80211_mgt_subtype_name[
172 (type & IEEE80211_FC0_SUBTYPE_MASK)
173 >> IEEE80211_FC0_SUBTYPE_SHIFT],
174 ether_sprintf(ni->ni_macaddr),
175 ieee80211_chan2ieee(ic, ni->ni_chan));
176 }
177 #endif
178 IF_ENQUEUE(&ic->ic_mgtq, m);
179 ifp->if_timer = 1;
180 (*ifp->if_start)(ifp);
181 return 0;
182 }
183
184 /*
185 * Encapsulate an outbound data frame. The mbuf chain is updated and
186 * a reference to the destination node is returned. If an error is
187 * encountered NULL is returned and the node reference will also be NULL.
188 *
189 * NB: The caller is responsible for free'ing a returned node reference.
190 * The convention is ic_bss is not reference counted; the caller must
191 * maintain that.
192 */
193 struct mbuf *
194 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
195 {
196 struct ieee80211com *ic = (void *)ifp;
197 struct ether_header eh;
198 struct ieee80211_frame *wh;
199 struct ieee80211_node *ni = NULL;
200 struct llc *llc;
201
202 if (m->m_len < sizeof(struct ether_header)) {
203 m = m_pullup(m, sizeof(struct ether_header));
204 if (m == NULL) {
205 ic->ic_stats.is_tx_nombuf++;
206 goto bad;
207 }
208 }
209 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
210
211 ni = ieee80211_find_txnode(ic, eh.ether_dhost);
212 if (ni == NULL) {
213 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
214 ("%s: no node for dst %s, discard frame\n",
215 __func__, ether_sprintf(eh.ether_dhost)));
216 ic->ic_stats.is_tx_nonode++;
217 goto bad;
218 }
219 ni->ni_inact = 0;
220
221 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
222 llc = mtod(m, struct llc *);
223 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
224 llc->llc_control = LLC_UI;
225 llc->llc_snap.org_code[0] = 0;
226 llc->llc_snap.org_code[1] = 0;
227 llc->llc_snap.org_code[2] = 0;
228 llc->llc_snap.ether_type = eh.ether_type;
229 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
230 if (m == NULL) {
231 ic->ic_stats.is_tx_nombuf++;
232 goto bad;
233 }
234 wh = mtod(m, struct ieee80211_frame *);
235 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
236 *(u_int16_t *)&wh->i_dur[0] = 0;
237 *(u_int16_t *)&wh->i_seq[0] =
238 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
239 ni->ni_txseq++;
240 switch (ic->ic_opmode) {
241 case IEEE80211_M_STA:
242 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
243 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
244 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
245 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
246 break;
247 case IEEE80211_M_IBSS:
248 case IEEE80211_M_AHDEMO:
249 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
250 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
251 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
252 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
253 break;
254 case IEEE80211_M_HOSTAP:
255 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
256 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
257 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
258 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
259 break;
260 case IEEE80211_M_MONITOR:
261 goto bad;
262 }
263 if (ic->ic_flags & IEEE80211_F_PRIVACY)
264 wh->i_fc[1] |= IEEE80211_FC1_WEP;
265 *pni = ni;
266 return m;
267 bad:
268 if (m != NULL)
269 m_freem(m);
270 if (ni != NULL)
271 ieee80211_release_node(ic, ni);
272 *pni = NULL;
273 return NULL;
274 }
275
276 /*
277 * Arguments in:
278 *
279 * paylen: payload length (no FCS, no WEP header)
280 *
281 * hdrlen: header length
282 *
283 * rate: MSDU speed, units 500kb/s
284 *
285 * flags: IEEE80211_F_SHPREAMBLE (use short preamble),
286 * IEEE80211_F_SHSLOT (use short slot length)
287 *
288 * Arguments out:
289 *
290 * d: 802.11 Duration field for data frame,
291 * PLCP Length for data frame,
292 * PLCP Service field for data frame,
293 * 802.11 Duration field for RTS
294 */
295 static int
296 ieee80211_compute_duration1(int len, uint32_t flags, int rate,
297 struct ieee80211_duration *d)
298 {
299 int ack, bitlen, cts, data_dur, remainder;
300
301 /* RTS reserves medium for SIFS | CTS | SIFS | (DATA) | SIFS | ACK
302 * DATA reserves medium for SIFS | ACK
303 */
304
305 bitlen = len * 8;
306
307 #if 0
308 /* RTS is always sent at 1 Mb/s. (XXX Really?) */
309 d->d_rts_plcp_len = sizeof(struct ieee80211_frame_rts) * 8;
310 #endif
311 d->d_plcp_svc = 0;
312
313 switch (rate) {
314 case 2: /* 1 Mb/s */
315 case 4: /* 2 Mb/s */
316 data_dur = (bitlen * 2) / rate;
317
318 /* 1 - 2 Mb/s WLAN: send ACK/CTS at 1 Mb/s */
319 cts = IEEE80211_DUR_DS_SLOW_CTS;
320 ack = IEEE80211_DUR_DS_SLOW_ACK;
321 break;
322 case 44: /* 22 Mb/s */
323 d->d_plcp_svc = IEEE80211_PLCP_SERVICE_PBCC;
324 /*FALLTHROUGH*/
325 case 11: /* 5.5 Mb/s */
326 case 22: /* 11 Mb/s */
327 remainder = (bitlen * 2) % rate;
328
329 if (remainder != 0)
330 data_dur = (bitlen * 2) / rate + 1;
331 else
332 data_dur = (bitlen * 2) / rate;
333
334 if (rate == 22 && remainder <= 6)
335 d->d_plcp_svc |= IEEE80211_PLCP_SERVICE_LENEXT;
336
337 /* 5.5 - 11 Mb/s WLAN: send ACK/CTS at 2 Mb/s */
338 cts = IEEE80211_DUR_DS_FAST_CTS;
339 ack = IEEE80211_DUR_DS_FAST_ACK;
340 break;
341 default:
342 /* TBD */
343 return -1;
344 }
345
346 d->d_rts_dur = data_dur + 3 * (IEEE80211_DUR_DS_SIFS +
347 IEEE80211_DUR_DS_SHORT_PREAMBLE +
348 IEEE80211_DUR_DS_FAST_PLCPHDR) + cts + ack;
349 d->d_data_dur = data_dur + IEEE80211_DUR_DS_SIFS +
350 2 * (IEEE80211_DUR_DS_SHORT_PREAMBLE +
351 IEEE80211_DUR_DS_FAST_PLCPHDR) + ack;
352
353 d->d_plcp_len = data_dur;
354
355 if ((flags & IEEE80211_F_SHPREAMBLE) != 0)
356 return 0;
357
358 d->d_rts_dur += 3 * (IEEE80211_DUR_DS_LONG_PREAMBLE -
359 IEEE80211_DUR_DS_SHORT_PREAMBLE) +
360 3 * (IEEE80211_DUR_DS_SLOW_PLCPHDR -
361 IEEE80211_DUR_DS_FAST_PLCPHDR);
362 d->d_data_dur += 2 * (IEEE80211_DUR_DS_LONG_PREAMBLE -
363 IEEE80211_DUR_DS_SHORT_PREAMBLE) +
364 2 * (IEEE80211_DUR_DS_SLOW_PLCPHDR -
365 IEEE80211_DUR_DS_FAST_PLCPHDR);
366 return 0;
367 }
368
369 /*
370 * Arguments in:
371 *
372 * wh: 802.11 header
373 *
374 * paylen: payload length (no FCS, no WEP header)
375 *
376 * rate: MSDU speed, units 500kb/s
377 *
378 * fraglen: fragment length, set to maximum (or higher) for no
379 * fragmentation
380 *
381 * flags: IEEE80211_F_PRIVACY (hardware adds WEP),
382 * IEEE80211_F_SHPREAMBLE (use short preamble),
383 * IEEE80211_F_SHSLOT (use short slot length)
384 *
385 * Arguments out:
386 *
387 * d0: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
388 * of first/only fragment
389 *
390 * dn: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
391 * of first/only fragment
392 */
393 int
394 ieee80211_compute_duration(struct ieee80211_frame *wh, int paylen,
395 uint32_t flags, int fraglen, int rate, struct ieee80211_duration *d0,
396 struct ieee80211_duration *dn, int *npktp)
397 {
398 int rc;
399 int firstlen, hdrlen, lastlen, lastlen0, npkt, overlen;
400
401 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
402 hdrlen = sizeof(struct ieee80211_frame_addr4);
403 else
404 hdrlen = sizeof(struct ieee80211_frame);
405
406 if ((flags & IEEE80211_F_PRIVACY) != 0) {
407 overlen = IEEE80211_WEP_TOTLEN + IEEE80211_CRC_LEN;
408 #if 0 /* 802.11 lets us extend a fragment's length by the length of
409 * a WEP header, AFTER fragmentation. Might want to disable
410 * this while fancy link adaptations are running.
411 */
412 fraglen -= IEEE80211_WEP_TOTLEN;
413 #endif
414 #if 0 /* Ditto CRC? */
415 fraglen -= IEEE80211_CRC_LEN;
416 #endif
417 } else
418 overlen = IEEE80211_CRC_LEN;
419
420 npkt = paylen / fraglen;
421 lastlen0 = paylen % fraglen;
422
423 if (npkt == 0) {
424 /* no fragments */
425 lastlen = paylen + overlen;
426 } else if (lastlen0 != 0) {
427 /* a short fragment */
428 lastlen = lastlen0 + overlen;
429 npkt++;
430 } else
431 lastlen = fraglen + overlen;
432
433 if (npktp != NULL)
434 *npktp = npkt;
435
436 if (npkt > 1)
437 firstlen = fraglen + overlen;
438 else
439 firstlen = paylen + overlen;
440
441 rc = ieee80211_compute_duration1(firstlen + hdrlen, flags, rate, d0);
442 if (rc == -1)
443 return rc;
444 if (npkt > 1) {
445 *dn = *d0;
446 return 0;
447 }
448 return ieee80211_compute_duration1(lastlen + hdrlen, flags, rate, dn);
449 }
450
451 /*
452 * Add a supported rates element id to a frame.
453 */
454 u_int8_t *
455 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
456 {
457 int nrates;
458
459 *frm++ = IEEE80211_ELEMID_RATES;
460 nrates = rs->rs_nrates;
461 if (nrates > IEEE80211_RATE_SIZE)
462 nrates = IEEE80211_RATE_SIZE;
463 *frm++ = nrates;
464 memcpy(frm, rs->rs_rates, nrates);
465 return frm + nrates;
466 }
467
468 /*
469 * Add an extended supported rates element id to a frame.
470 */
471 u_int8_t *
472 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
473 {
474 /*
475 * Add an extended supported rates element if operating in 11g mode.
476 */
477 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
478 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
479 *frm++ = IEEE80211_ELEMID_XRATES;
480 *frm++ = nrates;
481 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
482 frm += nrates;
483 }
484 return frm;
485 }
486
487 /*
488 * Add an ssid elemet to a frame.
489 */
490 static u_int8_t *
491 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
492 {
493 *frm++ = IEEE80211_ELEMID_SSID;
494 *frm++ = len;
495 memcpy(frm, ssid, len);
496 return frm + len;
497 }
498
499 static struct mbuf *
500 ieee80211_getmbuf(int flags, int type, u_int pktlen)
501 {
502 struct mbuf *m;
503
504 IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
505 #ifdef __FreeBSD__
506 if (pktlen <= MHLEN)
507 MGETHDR(m, flags, type);
508 else
509 m = m_getcl(flags, type, M_PKTHDR);
510 #else
511 MGETHDR(m, flags, type);
512 if (m != NULL && pktlen > MHLEN)
513 MCLGET(m, flags);
514 #endif
515 return m;
516 }
517
518 /*
519 * Send a management frame. The node is for the destination (or ic_bss
520 * when in station mode). Nodes other than ic_bss have their reference
521 * count bumped to reflect our use for an indeterminant time.
522 */
523 int
524 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
525 int type, int arg)
526 {
527 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
528 struct ifnet *ifp = &ic->ic_if;
529 struct mbuf *m;
530 u_int8_t *frm;
531 enum ieee80211_phymode mode;
532 u_int16_t capinfo;
533 int has_challenge, is_shared_key, ret, timer;
534
535 IASSERT(ni != NULL, ("null node"));
536
537 /*
538 * Hold a reference on the node so it doesn't go away until after
539 * the xmit is complete all the way in the driver. On error we
540 * will remove our reference.
541 */
542 ieee80211_ref_node(ni);
543 timer = 0;
544 switch (type) {
545 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
546 /*
547 * prreq frame format
548 * [tlv] ssid
549 * [tlv] supported rates
550 * [tlv] extended supported rates
551 */
552 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
553 2 + ic->ic_des_esslen
554 + 2 + IEEE80211_RATE_SIZE
555 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
556 if (m == NULL)
557 senderr(ENOMEM, is_tx_nombuf);
558 m->m_data += sizeof(struct ieee80211_frame);
559 frm = mtod(m, u_int8_t *);
560 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
561 mode = ieee80211_chan2mode(ic, ni->ni_chan);
562 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
563 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
564 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
565
566 timer = IEEE80211_TRANS_WAIT;
567 break;
568
569 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
570 /*
571 * probe response frame format
572 * [8] time stamp
573 * [2] beacon interval
574 * [2] cabability information
575 * [tlv] ssid
576 * [tlv] supported rates
577 * [tlv] parameter set (FH/DS)
578 * [tlv] parameter set (IBSS)
579 * [tlv] extended supported rates
580 */
581 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
582 8 + 2 + 2 + 2
583 + 2 + ni->ni_esslen
584 + 2 + IEEE80211_RATE_SIZE
585 + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
586 + 6
587 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
588 if (m == NULL)
589 senderr(ENOMEM, is_tx_nombuf);
590 m->m_data += sizeof(struct ieee80211_frame);
591 frm = mtod(m, u_int8_t *);
592
593 memset(frm, 0, 8); /* timestamp should be filled later */
594 frm += 8;
595 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
596 frm += 2;
597 if (ic->ic_opmode == IEEE80211_M_IBSS)
598 capinfo = IEEE80211_CAPINFO_IBSS;
599 else
600 capinfo = IEEE80211_CAPINFO_ESS;
601 if (ic->ic_flags & IEEE80211_F_PRIVACY)
602 capinfo |= IEEE80211_CAPINFO_PRIVACY;
603 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
604 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
605 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
606 *(u_int16_t *)frm = htole16(capinfo);
607 frm += 2;
608
609 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
610 ic->ic_bss->ni_esslen);
611 frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
612
613 if (ic->ic_phytype == IEEE80211_T_FH) {
614 *frm++ = IEEE80211_ELEMID_FHPARMS;
615 *frm++ = 5;
616 *frm++ = ni->ni_fhdwell & 0x00ff;
617 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
618 *frm++ = IEEE80211_FH_CHANSET(
619 ieee80211_chan2ieee(ic, ni->ni_chan));
620 *frm++ = IEEE80211_FH_CHANPAT(
621 ieee80211_chan2ieee(ic, ni->ni_chan));
622 *frm++ = ni->ni_fhindex;
623 } else {
624 *frm++ = IEEE80211_ELEMID_DSPARMS;
625 *frm++ = 1;
626 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
627 }
628
629 if (ic->ic_opmode == IEEE80211_M_IBSS) {
630 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
631 *frm++ = 2;
632 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
633 } else { /* IEEE80211_M_HOSTAP */
634 /* TODO: TIM */
635 *frm++ = IEEE80211_ELEMID_TIM;
636 *frm++ = 4; /* length */
637 *frm++ = 0; /* DTIM count */
638 *frm++ = 1; /* DTIM period */
639 *frm++ = 0; /* bitmap control */
640 *frm++ = 0; /* Partial Virtual Bitmap (variable length) */
641 }
642 frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
643 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
644 break;
645
646 case IEEE80211_FC0_SUBTYPE_AUTH:
647 MGETHDR(m, M_DONTWAIT, MT_DATA);
648 if (m == NULL)
649 senderr(ENOMEM, is_tx_nombuf);
650
651 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
652 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
653 ni->ni_challenge != NULL);
654
655 is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
656 arg == IEEE80211_AUTH_SHARED_PASS);
657
658 if (has_challenge) {
659 MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
660 m->m_pkthdr.len = m->m_len =
661 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
662 } else {
663 MH_ALIGN(m, 2 * 3);
664 m->m_pkthdr.len = m->m_len = 2 * 3;
665 }
666 frm = mtod(m, u_int8_t *);
667 ((u_int16_t *)frm)[0] =
668 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
669 : htole16(IEEE80211_AUTH_ALG_OPEN);
670 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */
671 ((u_int16_t *)frm)[2] = 0; /* status */
672
673 if (has_challenge) {
674 ((u_int16_t *)frm)[3] =
675 htole16((IEEE80211_CHALLENGE_LEN << 8) |
676 IEEE80211_ELEMID_CHALLENGE);
677 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
678 IEEE80211_CHALLENGE_LEN);
679 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
680 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
681 ("%s: request encrypt frame\n", __func__));
682 m->m_flags |= M_LINK0; /* WEP-encrypt, please */
683 }
684 }
685 if (ic->ic_opmode == IEEE80211_M_STA)
686 timer = IEEE80211_TRANS_WAIT;
687 break;
688
689 case IEEE80211_FC0_SUBTYPE_DEAUTH:
690 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
691 ("send station %s deauthenticate (reason %d)\n",
692 ether_sprintf(ni->ni_macaddr), arg));
693 MGETHDR(m, M_DONTWAIT, MT_DATA);
694 if (m == NULL)
695 senderr(ENOMEM, is_tx_nombuf);
696 MH_ALIGN(m, 2);
697 m->m_pkthdr.len = m->m_len = 2;
698 *mtod(m, u_int16_t *) = htole16(arg); /* reason */
699 break;
700
701 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
702 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
703 /*
704 * asreq frame format
705 * [2] capability information
706 * [2] listen interval
707 * [6*] current AP address (reassoc only)
708 * [tlv] ssid
709 * [tlv] supported rates
710 * [tlv] extended supported rates
711 */
712 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
713 sizeof(capinfo)
714 + sizeof(u_int16_t)
715 + IEEE80211_ADDR_LEN
716 + 2 + ni->ni_esslen
717 + 2 + IEEE80211_RATE_SIZE
718 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
719 if (m == NULL)
720 senderr(ENOMEM, is_tx_nombuf);
721 m->m_data += sizeof(struct ieee80211_frame);
722 frm = mtod(m, u_int8_t *);
723
724 capinfo = 0;
725 if (ic->ic_opmode == IEEE80211_M_IBSS)
726 capinfo |= IEEE80211_CAPINFO_IBSS;
727 else /* IEEE80211_M_STA */
728 capinfo |= IEEE80211_CAPINFO_ESS;
729 if (ic->ic_flags & IEEE80211_F_PRIVACY)
730 capinfo |= IEEE80211_CAPINFO_PRIVACY;
731 /*
732 * NB: Some 11a AP's reject the request when
733 * short premable is set.
734 */
735 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
736 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
737 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
738 if (ic->ic_flags & IEEE80211_F_SHSLOT)
739 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
740 *(u_int16_t *)frm = htole16(capinfo);
741 frm += 2;
742
743 *(u_int16_t *)frm = htole16(ic->ic_lintval);
744 frm += 2;
745
746 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
747 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
748 frm += IEEE80211_ADDR_LEN;
749 }
750
751 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
752 frm = ieee80211_add_rates(frm, &ni->ni_rates);
753 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
754 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
755
756 timer = IEEE80211_TRANS_WAIT;
757 break;
758
759 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
760 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
761 /*
762 * asreq frame format
763 * [2] capability information
764 * [2] status
765 * [2] association ID
766 * [tlv] supported rates
767 * [tlv] extended supported rates
768 */
769 m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
770 sizeof(capinfo)
771 + sizeof(u_int16_t)
772 + sizeof(u_int16_t)
773 + 2 + IEEE80211_RATE_SIZE
774 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
775 if (m == NULL)
776 senderr(ENOMEM, is_tx_nombuf);
777 m->m_data += sizeof(struct ieee80211_frame);
778 frm = mtod(m, u_int8_t *);
779
780 capinfo = IEEE80211_CAPINFO_ESS;
781 if (ic->ic_flags & IEEE80211_F_PRIVACY)
782 capinfo |= IEEE80211_CAPINFO_PRIVACY;
783 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
784 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
785 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
786 *(u_int16_t *)frm = htole16(capinfo);
787 frm += 2;
788
789 *(u_int16_t *)frm = htole16(arg); /* status */
790 frm += 2;
791
792 if (arg == IEEE80211_STATUS_SUCCESS)
793 *(u_int16_t *)frm = htole16(ni->ni_associd);
794 frm += 2;
795
796 frm = ieee80211_add_rates(frm, &ni->ni_rates);
797 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
798 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
799 break;
800
801 case IEEE80211_FC0_SUBTYPE_DISASSOC:
802 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
803 ("send station %s disassociate (reason %d)\n",
804 ether_sprintf(ni->ni_macaddr), arg));
805 MGETHDR(m, M_DONTWAIT, MT_DATA);
806 if (m == NULL)
807 senderr(ENOMEM, is_tx_nombuf);
808 MH_ALIGN(m, 2);
809 m->m_pkthdr.len = m->m_len = 2;
810 *mtod(m, u_int16_t *) = htole16(arg); /* reason */
811 break;
812
813 default:
814 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
815 ("%s: invalid mgmt frame type %u\n", __func__, type));
816 senderr(EINVAL, is_tx_unknownmgt);
817 /* NOTREACHED */
818 }
819
820 ret = ieee80211_mgmt_output(ifp, ni, m, type);
821 if (ret == 0) {
822 if (timer)
823 ic->ic_mgt_timer = timer;
824 } else {
825 bad:
826 ieee80211_release_node(ic, ni);
827 }
828 return ret;
829 #undef senderr
830 }
831
832 void
833 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
834 struct mbuf *m)
835 {
836 /* Store the new packet on our queue, changing the TIM if necessary */
837
838 if (IF_IS_EMPTY(&ni->ni_savedq)) {
839 ic->ic_set_tim(ic, ni->ni_associd, 1);
840 }
841 if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
842 IF_DROP(&ni->ni_savedq);
843 m_freem(m);
844 if (ic->ic_if.if_flags & IFF_DEBUG)
845 printf("%s: station %s power save queue overflow"
846 " of size %d drops %d\n",
847 ic->ic_if.if_xname,
848 ether_sprintf(ni->ni_macaddr),
849 IEEE80211_PS_MAX_QUEUE,
850 ni->ni_savedq.ifq_drops);
851 } else {
852 /* Similar to ieee80211_mgmt_output, store the node in
853 * the rcvif field.
854 */
855 IF_ENQUEUE(&ni->ni_savedq, m);
856 m->m_pkthdr.rcvif = (void *)ni;
857 }
858 }
859
860