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