nrelease - fix/improve livecd
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_output.c
CommitLineData
32176cfd 1/*-
f186073c 2 * Copyright (c) 2001 Atsushi Onoe
32176cfd 3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
f186073c
JS
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
f186073c
JS
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f186073c
JS
25 */
26
085ff963
MD
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
f186073c 30#include "opt_inet.h"
32176cfd
RP
31#include "opt_inet6.h"
32#include "opt_wlan.h"
f186073c
JS
33
34#include <sys/param.h>
35#include <sys/systm.h>
f186073c 36#include <sys/kernel.h>
4f655ef5
MD
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
f186073c 39#include <sys/endian.h>
f186073c 40
841ab66c 41#include <sys/socket.h>
f186073c 42
841ab66c
SZ
43#include <net/bpf.h>
44#include <net/ethernet.h>
f186073c 45#include <net/if.h>
085ff963 46#include <net/if_var.h>
f186073c 47#include <net/if_llc.h>
841ab66c 48#include <net/if_media.h>
085ff963
MD
49#include <net/vlan/if_vlan_var.h>
50
51#if defined(__DragonFly__)
34a60cf6 52#include <net/ifq_var.h>
085ff963 53#endif
f186073c
JS
54
55#include <netproto/802_11/ieee80211_var.h>
32176cfd
RP
56#include <netproto/802_11/ieee80211_regdomain.h>
57#ifdef IEEE80211_SUPPORT_SUPERG
58#include <netproto/802_11/ieee80211_superg.h>
59#endif
60#ifdef IEEE80211_SUPPORT_TDMA
61#include <netproto/802_11/ieee80211_tdma.h>
62#endif
63#include <netproto/802_11/ieee80211_wds.h>
64#include <netproto/802_11/ieee80211_mesh.h>
f186073c 65
085ff963 66#if defined(INET) || defined(INET6)
f186073c 67#include <netinet/in.h>
085ff963
MD
68#endif
69
70#ifdef INET
f186073c 71#include <netinet/if_ether.h>
841ab66c
SZ
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#endif
32176cfd
RP
75#ifdef INET6
76#include <netinet/ip6.h>
77#endif
78
4f655ef5
MD
79#if defined(__DragonFly__)
80#else
81#include <security/mac/mac_framework.h>
82#endif
085ff963 83
32176cfd
RP
84#define ETHER_HEADER_COPY(dst, src) \
85 memcpy(dst, src, sizeof(struct ether_header))
86
32176cfd
RP
87static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
88 u_int hdrsize, u_int ciphdrsize, u_int mtu);
89static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
841ab66c
SZ
90
91#ifdef IEEE80211_DEBUG
92/*
93 * Decide if an outbound management frame should be
94 * printed when debugging is enabled. This filters some
95 * of the less interesting frames that come frequently
96 * (e.g. beacons).
97 */
98static __inline int
32176cfd 99doprint(struct ieee80211vap *vap, int subtype)
841ab66c
SZ
100{
101 switch (subtype) {
102 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
32176cfd 103 return (vap->iv_opmode == IEEE80211_M_IBSS);
841ab66c
SZ
104 }
105 return 1;
106}
f186073c
JS
107#endif
108
085ff963
MD
109/*
110 * Transmit a frame to the given destination on the given VAP.
111 *
112 * It's up to the caller to figure out the details of who this
113 * is going to and resolving the node.
114 *
115 * This routine takes care of queuing it for power save,
116 * A-MPDU state stuff, fast-frames state stuff, encapsulation
117 * if required, then passing it up to the driver layer.
118 *
119 * This routine (for now) consumes the mbuf and frees the node
120 * reference; it ideally will return a TX status which reflects
121 * whether the mbuf was consumed or not, so the caller can
122 * free the mbuf (if appropriate) and the node reference (again,
123 * if appropriate.)
124 */
125int
126ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
127 struct ieee80211_node *ni)
128{
129 struct ieee80211com *ic = vap->iv_ic;
130 struct ifnet *ifp = vap->iv_ifp;
4f655ef5 131 int len, mcast;
085ff963
MD
132
133 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
134 (m->m_flags & M_PWR_SAV) == 0) {
135 /*
136 * Station in power save mode; pass the frame
137 * to the 802.11 layer and continue. We'll get
138 * the frame back when the time is right.
139 * XXX lose WDS vap linkage?
140 */
4f898719 141 if (ieee80211_pwrsave(ni, m) != 0)
4f655ef5 142 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
143 ieee80211_free_node(ni);
144
145 /*
146 * We queued it fine, so tell the upper layer
147 * that we consumed it.
148 */
149 return (0);
150 }
151 /* calculate priority so drivers can find the tx queue */
152 if (ieee80211_classify(ni, m)) {
153 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
154 ni->ni_macaddr, NULL,
155 "%s", "classification failure");
156 vap->iv_stats.is_tx_classify++;
4f655ef5 157 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
158 m_freem(m);
159 ieee80211_free_node(ni);
160
161 /* XXX better status? */
162 return (0);
163 }
164 /*
165 * Stash the node pointer. Note that we do this after
166 * any call to ieee80211_dwds_mcast because that code
167 * uses any existing value for rcvif to identify the
168 * interface it (might have been) received on.
169 */
170 m->m_pkthdr.rcvif = (void *)ni;
4f898719
IV
171 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
172 len = m->m_pkthdr.len;
085ff963
MD
173
174 BPF_MTAP(ifp, m); /* 802.3 tx */
175
176 /*
177 * Check if A-MPDU tx aggregation is setup or if we
178 * should try to enable it. The sta must be associated
179 * with HT and A-MPDU enabled for use. When the policy
180 * routine decides we should enable A-MPDU we issue an
181 * ADDBA request and wait for a reply. The frame being
182 * encapsulated will go out w/o using A-MPDU, or possibly
183 * it might be collected by the driver and held/retransmit.
184 * The default ic_ampdu_enable routine handles staggering
185 * ADDBA requests in case the receiver NAK's us or we are
186 * otherwise unable to establish a BA stream.
187 */
188 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
4f655ef5
MD
189 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
190 if ((m->m_flags & M_EAPOL) == 0) {
191 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
192 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
193
194 ieee80211_txampdu_count_packet(tap);
195 if (IEEE80211_AMPDU_RUNNING(tap)) {
196 /*
197 * Operational, mark frame for aggregation.
198 *
199 * XXX do tx aggregation here
200 */
201 m->m_flags |= M_AMPDU_MPDU;
202 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
203 ic->ic_ampdu_enable(ni, tap)) {
204 /*
205 * Not negotiated yet, request service.
206 */
207 ieee80211_ampdu_request(ni, tap);
208 /* XXX hold frame for reply? */
209 }
085ff963
MD
210 }
211 }
212
213#ifdef IEEE80211_SUPPORT_SUPERG
4f655ef5
MD
214 /*
215 * Check for AMSDU/FF; queue for aggregation
216 *
217 * Note: we don't bother trying to do fast frames or
218 * A-MSDU encapsulation for 802.3 drivers. Now, we
219 * likely could do it for FF (because it's a magic
220 * atheros tunnel LLC type) but I don't think we're going
221 * to really need to. For A-MSDU we'd have to set the
222 * A-MSDU QoS bit in the wifi header, so we just plain
223 * can't do it.
224 *
225 * Strictly speaking, we could actually /do/ A-MSDU / FF
226 * with A-MPDU together which for certain circumstances
227 * is beneficial (eg A-MSDU of TCK ACKs.) However,
228 * I'll ignore that for now so existing behaviour is maintained.
229 * Later on it would be good to make "amsdu + ampdu" configurable.
230 */
231 else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
232 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
233 m = ieee80211_amsdu_check(ni, m);
234 if (m == NULL) {
235 /* NB: any ni ref held on stageq */
236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
237 "%s: amsdu_check queued frame\n",
238 __func__);
239 return (0);
240 }
241 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
242 IEEE80211_NODE_FF)) {
243 m = ieee80211_ff_check(ni, m);
244 if (m == NULL) {
245 /* NB: any ni ref held on stageq */
246 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
247 "%s: ff_check queued frame\n",
248 __func__);
249 return (0);
250 }
085ff963
MD
251 }
252 }
253#endif /* IEEE80211_SUPPORT_SUPERG */
254
255 /*
256 * Grab the TX lock - serialise the TX process from this
257 * point (where TX state is being checked/modified)
258 * through to driver queue.
259 */
260 IEEE80211_TX_LOCK(ic);
261
4f655ef5
MD
262 /*
263 * XXX make the encap and transmit code a separate function
264 * so things like the FF (and later A-MSDU) path can just call
265 * it for flushed frames.
266 */
085ff963
MD
267 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
268 /*
269 * Encapsulate the packet in prep for transmission.
270 */
271 m = ieee80211_encap(vap, ni, m);
272 if (m == NULL) {
273 /* NB: stat+msg handled in ieee80211_encap */
274 IEEE80211_TX_UNLOCK(ic);
275 ieee80211_free_node(ni);
4f655ef5 276 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
277 return (ENOBUFS);
278 }
279 }
c75fa8b8
MD
280 /* HACK - added by DragonFly, mbuf could lose rcvif assignment above */
281 m->m_pkthdr.rcvif = (void *)ni;
4f655ef5 282 (void) ieee80211_parent_xmitpkt(ic, m);
085ff963
MD
283
284 /*
285 * Unlock at this point - no need to hold it across
286 * ieee80211_free_node() (ie, the comlock)
287 */
288 IEEE80211_TX_UNLOCK(ic);
085ff963
MD
289 ic->ic_lastdata = ticks;
290
291 return (0);
292}
293
294
295
296/*
297 * Send the given mbuf through the given vap.
298 *
299 * This consumes the mbuf regardless of whether the transmit
300 * was successful or not.
301 *
302 * This does none of the initial checks that ieee80211_start()
303 * does (eg CAC timeout, interface wakeup) - the caller must
304 * do this first.
305 */
306static int
307ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
308{
309#define IS_DWDS(vap) \
310 (vap->iv_opmode == IEEE80211_M_WDS && \
311 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
312 struct ieee80211com *ic = vap->iv_ic;
313 struct ifnet *ifp = vap->iv_ifp;
314 struct ieee80211_node *ni;
315 struct ether_header *eh;
316
317 /*
318 * Cancel any background scan.
319 */
320 if (ic->ic_flags & IEEE80211_F_SCAN)
321 ieee80211_cancel_anyscan(vap);
322 /*
323 * Find the node for the destination so we can do
324 * things like power save and fast frames aggregation.
325 *
326 * NB: past this point various code assumes the first
327 * mbuf has the 802.3 header present (and contiguous).
328 */
329 ni = NULL;
330 if (m->m_len < sizeof(struct ether_header) &&
331 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
332 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
333 "discard frame, %s\n", "m_pullup failed");
334 vap->iv_stats.is_tx_nobuf++; /* XXX */
4f655ef5 335 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
336 return (ENOBUFS);
337 }
338 eh = mtod(m, struct ether_header *);
339 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
340 if (IS_DWDS(vap)) {
341 /*
342 * Only unicast frames from the above go out
343 * DWDS vaps; multicast frames are handled by
344 * dispatching the frame as it comes through
345 * the AP vap (see below).
346 */
347 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
348 eh->ether_dhost, "mcast", "%s", "on DWDS");
349 vap->iv_stats.is_dwds_mcast++;
350 m_freem(m);
4f655ef5 351 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
352 /* XXX better status? */
353 return (ENOBUFS);
354 }
355 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
356 /*
357 * Spam DWDS vap's w/ multicast traffic.
358 */
359 /* XXX only if dwds in use? */
085ff963
MD
360 ieee80211_dwds_mcast(vap, m);
361 }
362 }
363#ifdef IEEE80211_SUPPORT_MESH
364 if (vap->iv_opmode != IEEE80211_M_MBSS) {
365#endif
366 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
367 if (ni == NULL) {
368 /* NB: ieee80211_find_txnode does stat+msg */
4f655ef5 369 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
370 m_freem(m);
371 /* XXX better status? */
372 return (ENOBUFS);
373 }
374 if (ni->ni_associd == 0 &&
375 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
376 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
377 eh->ether_dhost, NULL,
378 "sta not associated (type 0x%04x)",
379 htons(eh->ether_type));
380 vap->iv_stats.is_tx_notassoc++;
4f655ef5 381 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
382 m_freem(m);
383 ieee80211_free_node(ni);
384 /* XXX better status? */
385 return (ENOBUFS);
386 }
387#ifdef IEEE80211_SUPPORT_MESH
388 } else {
389 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
390 /*
391 * Proxy station only if configured.
392 */
393 if (!ieee80211_mesh_isproxyena(vap)) {
394 IEEE80211_DISCARD_MAC(vap,
395 IEEE80211_MSG_OUTPUT |
396 IEEE80211_MSG_MESH,
397 eh->ether_dhost, NULL,
398 "%s", "proxy not enabled");
399 vap->iv_stats.is_mesh_notproxy++;
4f655ef5 400 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
401 m_freem(m);
402 /* XXX better status? */
403 return (ENOBUFS);
404 }
4f655ef5 405#if defined(__DragonFly__)
085ff963 406 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
f92fae3f
SW
407 "forward frame from DS SA(%s), DA(%s)\n",
408 ether_sprintf(eh->ether_shost),
409 ether_sprintf(eh->ether_dhost));
4f655ef5
MD
410#else
411 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
412 "forward frame from DS SA(%6D), DA(%6D)\n",
413 eh->ether_shost, ":",
414 eh->ether_dhost, ":");
415#endif
085ff963
MD
416 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
417 }
418 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
419 if (ni == NULL) {
420 /*
421 * NB: ieee80211_mesh_discover holds/disposes
422 * frame (e.g. queueing on path discovery).
423 */
4f655ef5 424 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
425 /* XXX better status? */
426 return (ENOBUFS);
427 }
428 }
429#endif
430
431 /*
432 * We've resolved the sender, so attempt to transmit it.
433 */
434
435 if (vap->iv_state == IEEE80211_S_SLEEP) {
436 /*
437 * In power save; queue frame and then wakeup device
438 * for transmit.
439 */
440 ic->ic_lastdata = ticks;
4f898719 441 if (ieee80211_pwrsave(ni, m) != 0)
4f655ef5 442 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
443 ieee80211_free_node(ni);
444 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
445 return (0);
446 }
447
448 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
449 return (ENOBUFS);
450 return (0);
451#undef IS_DWDS
452}
453
32176cfd
RP
454/*
455 * Start method for vap's. All packets from the stack come
456 * through here. We handle common processing of the packets
457 * before dispatching them to the underlying device.
085ff963
MD
458 *
459 * if_transmit() requires that the mbuf be consumed by this call
460 * regardless of the return condition.
32176cfd 461 */
085ff963
MD
462
463#if defined(__DragonFly__)
464
32176cfd 465void
085ff963 466ieee80211_vap_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
32176cfd 467{
32176cfd
RP
468 struct ieee80211vap *vap = ifp->if_softc;
469 struct ieee80211com *ic = vap->iv_ic;
4f655ef5 470 struct ifnet *parent = vap->iv_ifp;
ea86af0d 471 struct mbuf *m = NULL;
93d6499b 472
32176cfd
RP
473 /* NB: parent must be up and running */
474 if (!IFNET_IS_UP_RUNNING(parent)) {
475 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
476 "%s: ignore queue, parent %s not up+running\n",
477 __func__, parent->if_xname);
478 /* XXX stat */
085ff963
MD
479 /*m_freem(m);*/
480 /*return (EINVAL);*/
32176cfd
RP
481 return;
482 }
085ff963
MD
483
484 wlan_assert_serialized();
485 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
486
32176cfd
RP
487 /*
488 * No data frames go out unless we're running.
489 * Note in particular this covers CAC and CSA
490 * states (though maybe we should check muting
491 * for CSA).
492 */
d98a0bcf
MD
493 if (vap->iv_state != IEEE80211_S_RUN &&
494 vap->iv_state != IEEE80211_S_SLEEP) {
085ff963 495 IEEE80211_LOCK(ic);
32176cfd 496 /* re-check under the com lock to avoid races */
d98a0bcf
MD
497 if (vap->iv_state != IEEE80211_S_RUN &&
498 vap->iv_state != IEEE80211_S_SLEEP) {
32176cfd
RP
499 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
500 "%s: ignore queue, in %s state\n",
501 __func__, ieee80211_state_name[vap->iv_state]);
502 vap->iv_stats.is_tx_badstate++;
085ff963 503 IEEE80211_UNLOCK(ic);
f0a26983 504 ifsq_set_oactive(ifsq);
085ff963
MD
505 /*m_freem(m);*/
506 /* return (EINVAL); */
32176cfd
RP
507 return;
508 }
085ff963 509 IEEE80211_UNLOCK(ic);
32176cfd 510 }
085ff963
MD
511
512 wlan_serialize_exit();
32176cfd 513 for (;;) {
ac9843a1 514 m = ifsq_dequeue(ifsq);
32176cfd
RP
515 if (m == NULL)
516 break;
085ff963 517
32176cfd
RP
518 /*
519 * Sanitize mbuf flags for net80211 use. We cannot
520 * clear M_PWR_SAV or M_MORE_DATA because these may
521 * be set for frames that are re-submitted from the
522 * power save queue.
523 *
524 * NB: This must be done before ieee80211_classify as
525 * it marks EAPOL in frames with M_EAPOL.
526 */
527 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
d98a0bcf 528
32176cfd 529 /*
085ff963
MD
530 * Bump to the packet transmission path.
531 * The mbuf will be consumed here.
32176cfd 532 */
085ff963
MD
533 ieee80211_start_pkt(vap, m);
534 }
535 wlan_serialize_enter();
536}
32176cfd 537
085ff963 538#else
32176cfd 539
085ff963
MD
540int
541ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
542{
543 struct ieee80211vap *vap = ifp->if_softc;
544 struct ieee80211com *ic = vap->iv_ic;
085ff963
MD
545
546 /*
547 * No data frames go out unless we're running.
548 * Note in particular this covers CAC and CSA
549 * states (though maybe we should check muting
550 * for CSA).
551 */
552 if (vap->iv_state != IEEE80211_S_RUN &&
553 vap->iv_state != IEEE80211_S_SLEEP) {
554 IEEE80211_LOCK(ic);
555 /* re-check under the com lock to avoid races */
556 if (vap->iv_state != IEEE80211_S_RUN &&
557 vap->iv_state != IEEE80211_S_SLEEP) {
558 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
559 "%s: ignore queue, in %s state\n",
560 __func__, ieee80211_state_name[vap->iv_state]);
561 vap->iv_stats.is_tx_badstate++;
562 IEEE80211_UNLOCK(ic);
563 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
564 m_freem(m);
4f655ef5 565 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4f898719 566 return (ENETDOWN);
32176cfd 567 }
085ff963 568 IEEE80211_UNLOCK(ic);
32176cfd 569 }
085ff963
MD
570
571 /*
572 * Sanitize mbuf flags for net80211 use. We cannot
573 * clear M_PWR_SAV or M_MORE_DATA because these may
574 * be set for frames that are re-submitted from the
575 * power save queue.
576 *
577 * NB: This must be done before ieee80211_classify as
578 * it marks EAPOL in frames with M_EAPOL.
579 */
580 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
581
582 /*
583 * Bump to the packet transmission path.
584 * The mbuf will be consumed here.
585 */
586 return (ieee80211_start_pkt(vap, m));
32176cfd
RP
587}
588
085ff963
MD
589void
590ieee80211_vap_qflush(struct ifnet *ifp)
591{
592
593 /* Empty for now */
594}
595
596#endif
597
598/*
599 * 802.11 raw output routine.
600 *
601 * XXX TODO: this (and other send routines) should correctly
602 * XXX keep the pwr mgmt bit set if it decides to call into the
603 * XXX driver to send a frame whilst the state is SLEEP.
604 *
605 * Otherwise the peer may decide that we're awake and flood us
606 * with traffic we are still too asleep to receive!
607 */
608int
609ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
610 struct mbuf *m, const struct ieee80211_bpf_params *params)
611{
612 struct ieee80211com *ic = vap->iv_ic;
4f655ef5 613 int error;
085ff963 614
4f898719
IV
615 /*
616 * Set node - the caller has taken a reference, so ensure
617 * that the mbuf has the same node value that
618 * it would if it were going via the normal path.
619 */
620 m->m_pkthdr.rcvif = (void *)ni;
621
622 /*
623 * Attempt to add bpf transmit parameters.
624 *
625 * For now it's ok to fail; the raw_xmit api still takes
626 * them as an option.
627 *
628 * Later on when ic_raw_xmit() has params removed,
629 * they'll have to be added - so fail the transmit if
630 * they can't be.
631 */
632 if (params)
633 (void) ieee80211_add_xmit_params(m, params);
634
4f655ef5
MD
635 error = ic->ic_raw_xmit(ni, m, params);
636 if (error) {
637 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
638 ieee80211_free_node(ni);
639 }
640 return (error);
085ff963 641}
cc0d8938 642
32176cfd
RP
643/*
644 * 802.11 output routine. This is (currently) used only to
645 * connect bpf write calls to the 802.11 layer for injecting
646 * raw 802.11 frames.
647 */
085ff963 648#if defined(__DragonFly__)
32176cfd
RP
649int
650ieee80211_output(struct ifnet *ifp, struct mbuf *m,
34a60cf6 651 struct sockaddr *dst, struct rtentry *rt)
085ff963
MD
652#else
653int
654ieee80211_output(struct ifnet *ifp, struct mbuf *m,
294727bf 655 const struct sockaddr *dst, struct route *ro)
085ff963 656#endif
32176cfd
RP
657{
658#define senderr(e) do { error = (e); goto bad;} while (0)
659 struct ieee80211_node *ni = NULL;
660 struct ieee80211vap *vap;
661 struct ieee80211_frame *wh;
085ff963 662 struct ieee80211com *ic = NULL;
32176cfd 663 int error;
085ff963 664 int ret;
32176cfd 665
085ff963
MD
666#if defined(__DragonFly__)
667 struct ifaltq_subque *ifsq;
f0a26983 668 ifsq = ifq_get_subq_default(&ifp->if_snd);
4f655ef5 669 if (ifsq_is_oactive(ifsq)) {
085ff963 670#else
4f655ef5 671 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
085ff963 672#endif
32176cfd
RP
673 /*
674 * Short-circuit requests if the vap is marked OACTIVE
675 * as this can happen because a packet came down through
676 * ieee80211_start before the vap entered RUN state in
677 * which case it's ok to just drop the frame. This
678 * should not be necessary but callers of if_output don't
679 * check OACTIVE.
680 */
681 senderr(ENETDOWN);
682 }
683 vap = ifp->if_softc;
085ff963 684 ic = vap->iv_ic;
32176cfd
RP
685 /*
686 * Hand to the 802.3 code if not tagged as
687 * a raw 802.11 frame.
688 */
085ff963 689#if defined(__DragonFly__)
32176cfd 690 if (dst->sa_family != AF_IEEE80211)
34a60cf6 691 return vap->iv_output(ifp, m, dst, rt);
085ff963
MD
692#else
693 if (dst->sa_family != AF_IEEE80211)
694 return vap->iv_output(ifp, m, dst, ro);
695#endif
32176cfd
RP
696#ifdef MAC
697 error = mac_ifnet_check_transmit(ifp, m);
698 if (error)
699 senderr(error);
700#endif
701 if (ifp->if_flags & IFF_MONITOR)
702 senderr(ENETDOWN);
703 if (!IFNET_IS_UP_RUNNING(ifp))
704 senderr(ENETDOWN);
705 if (vap->iv_state == IEEE80211_S_CAC) {
706 IEEE80211_DPRINTF(vap,
707 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
708 "block %s frame in CAC state\n", "raw data");
709 vap->iv_stats.is_tx_badstate++;
710 senderr(EIO); /* XXX */
085ff963
MD
711 } else if (vap->iv_state == IEEE80211_S_SCAN)
712 senderr(EIO);
32176cfd
RP
713 /* XXX bypass bridge, pfil, carp, etc. */
714
715 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
716 senderr(EIO); /* XXX */
717 wh = mtod(m, struct ieee80211_frame *);
718 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
719 IEEE80211_FC0_VERSION_0)
720 senderr(EIO); /* XXX */
721
722 /* locate destination node */
723 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
724 case IEEE80211_FC1_DIR_NODS:
725 case IEEE80211_FC1_DIR_FROMDS:
726 ni = ieee80211_find_txnode(vap, wh->i_addr1);
727 break;
728 case IEEE80211_FC1_DIR_TODS:
729 case IEEE80211_FC1_DIR_DSTODS:
730 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
731 senderr(EIO); /* XXX */
732 ni = ieee80211_find_txnode(vap, wh->i_addr3);
733 break;
734 default:
735 senderr(EIO); /* XXX */
736 }
737 if (ni == NULL) {
738 /*
739 * Permit packets w/ bpf params through regardless
740 * (see below about sa_len).
741 */
742 if (dst->sa_len == 0)
743 senderr(EHOSTUNREACH);
744 ni = ieee80211_ref_node(vap->iv_bss);
745 }
746
747 /*
748 * Sanitize mbuf for net80211 flags leaked from above.
749 *
750 * NB: This must be done before ieee80211_classify as
751 * it marks EAPOL in frames with M_EAPOL.
752 */
753 m->m_flags &= ~M_80211_TX;
754
755 /* calculate priority so drivers can find the tx queue */
756 /* XXX assumes an 802.3 frame */
757 if (ieee80211_classify(ni, m))
758 senderr(EIO); /* XXX */
759
4f655ef5 760 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
32176cfd
RP
761 IEEE80211_NODE_STAT(ni, tx_data);
762 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
763 IEEE80211_NODE_STAT(ni, tx_mcast);
764 m->m_flags |= M_MCAST;
765 } else
766 IEEE80211_NODE_STAT(ni, tx_ucast);
767 /* NB: ieee80211_encap does not include 802.11 header */
768 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
769
085ff963
MD
770 IEEE80211_TX_LOCK(ic);
771
32176cfd
RP
772 /*
773 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
774 * present by setting the sa_len field of the sockaddr (yes,
775 * this is a hack).
776 * NB: we assume sa_data is suitably aligned to cast.
777 */
085ff963 778 ret = ieee80211_raw_output(vap, ni, m,
32176cfd
RP
779 (const struct ieee80211_bpf_params *)(dst->sa_len ?
780 dst->sa_data : NULL));
085ff963
MD
781 IEEE80211_TX_UNLOCK(ic);
782 return (ret);
32176cfd
RP
783bad:
784 if (m != NULL)
785 m_freem(m);
786 if (ni != NULL)
787 ieee80211_free_node(ni);
4f655ef5 788 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
32176cfd
RP
789 return error;
790#undef senderr
791}
792
841ab66c
SZ
793/*
794 * Set the direction field and address fields of an outgoing
32176cfd
RP
795 * frame. Note this should be called early on in constructing
796 * a frame as it sets i_fc[1]; other bits can then be or'd in.
841ab66c 797 */
32176cfd
RP
798void
799ieee80211_send_setup(
841ab66c 800 struct ieee80211_node *ni,
32176cfd
RP
801 struct mbuf *m,
802 int type, int tid,
841ab66c
SZ
803 const uint8_t sa[IEEE80211_ADDR_LEN],
804 const uint8_t da[IEEE80211_ADDR_LEN],
805 const uint8_t bssid[IEEE80211_ADDR_LEN])
806{
807#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
32176cfd 808 struct ieee80211vap *vap = ni->ni_vap;
085ff963 809 struct ieee80211_tx_ampdu *tap;
32176cfd
RP
810 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
811 ieee80211_seq seqno;
841ab66c 812
085ff963
MD
813 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
814
841ab66c
SZ
815 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
816 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
32176cfd 817 switch (vap->iv_opmode) {
841ab66c
SZ
818 case IEEE80211_M_STA:
819 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
820 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
821 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
822 IEEE80211_ADDR_COPY(wh->i_addr3, da);
823 break;
824 case IEEE80211_M_IBSS:
825 case IEEE80211_M_AHDEMO:
826 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
827 IEEE80211_ADDR_COPY(wh->i_addr1, da);
828 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
829 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
830 break;
831 case IEEE80211_M_HOSTAP:
832 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
833 IEEE80211_ADDR_COPY(wh->i_addr1, da);
834 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
835 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
836 break;
32176cfd
RP
837 case IEEE80211_M_WDS:
838 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
839 IEEE80211_ADDR_COPY(wh->i_addr1, da);
840 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
841 IEEE80211_ADDR_COPY(wh->i_addr3, da);
842 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
843 break;
844 case IEEE80211_M_MBSS:
845#ifdef IEEE80211_SUPPORT_MESH
32176cfd
RP
846 if (IEEE80211_IS_MULTICAST(da)) {
847 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
848 /* XXX next hop */
849 IEEE80211_ADDR_COPY(wh->i_addr1, da);
850 IEEE80211_ADDR_COPY(wh->i_addr2,
851 vap->iv_myaddr);
852 } else {
853 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
854 IEEE80211_ADDR_COPY(wh->i_addr1, da);
855 IEEE80211_ADDR_COPY(wh->i_addr2,
856 vap->iv_myaddr);
857 IEEE80211_ADDR_COPY(wh->i_addr3, da);
858 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
859 }
860#endif
861 break;
841ab66c
SZ
862 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
863 break;
864 }
865 } else {
866 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
867 IEEE80211_ADDR_COPY(wh->i_addr1, da);
868 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
32176cfd
RP
869#ifdef IEEE80211_SUPPORT_MESH
870 if (vap->iv_opmode == IEEE80211_M_MBSS)
871 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
872 else
873#endif
874 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
841ab66c
SZ
875 }
876 *(uint16_t *)&wh->i_dur[0] = 0;
32176cfd 877
085ff963
MD
878 tap = &ni->ni_tx_ampdu[tid];
879 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
880 m->m_flags |= M_AMPDU_MPDU;
881 else {
4f898719
IV
882 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
883 type & IEEE80211_FC0_SUBTYPE_MASK))
884 seqno = ni->ni_txseqs[tid]++;
885 else
886 seqno = 0;
4f655ef5 887
085ff963
MD
888 *(uint16_t *)&wh->i_seq[0] =
889 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
890 M_SEQNO_SET(m, seqno);
891 }
32176cfd
RP
892
893 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
894 m->m_flags |= M_MCAST;
841ab66c
SZ
895#undef WH4
896}
897
f186073c
JS
898/*
899 * Send a management frame to the specified node. The node pointer
900 * must have a reference as the pointer will be passed to the driver
901 * and potentially held for a long time. If the frame is successfully
4f655ef5
MD
902 * dispatched to the driver, then it is responsible forkfreeing the
903 * reference (and potentiallykfree'ing up any associated storage);
32176cfd 904 * otherwise deal with reclaiming any reference (on error).
f186073c 905 */
32176cfd
RP
906int
907ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
908 struct ieee80211_bpf_params *params)
f186073c 909{
32176cfd
RP
910 struct ieee80211vap *vap = ni->ni_vap;
911 struct ieee80211com *ic = ni->ni_ic;
f186073c 912 struct ieee80211_frame *wh;
085ff963
MD
913 int ret;
914
f186073c 915 KASSERT(ni != NULL, ("null node"));
f186073c 916
32176cfd
RP
917 if (vap->iv_state == IEEE80211_S_CAC) {
918 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
919 ni, "block %s frame in CAC state",
4f655ef5 920 ieee80211_mgt_subtype_name(type));
32176cfd
RP
921 vap->iv_stats.is_tx_badstate++;
922 ieee80211_free_node(ni);
923 m_freem(m);
924 return EIO; /* XXX */
925 }
926
b5523eac 927 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
32176cfd
RP
928 if (m == NULL) {
929 ieee80211_free_node(ni);
f186073c 930 return ENOMEM;
32176cfd 931 }
f186073c 932
085ff963
MD
933 IEEE80211_TX_LOCK(ic);
934
f186073c 935 wh = mtod(m, struct ieee80211_frame *);
32176cfd
RP
936 ieee80211_send_setup(ni, m,
937 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
938 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
939 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
940 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
941 "encrypting frame (%s)", __func__);
085ff963 942 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
841ab66c 943 }
32176cfd
RP
944 m->m_flags |= M_ENCAP; /* mark encapsulated */
945
946 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
947 M_WME_SETAC(m, params->ibp_pri);
948
f186073c 949#ifdef IEEE80211_DEBUG
841ab66c 950 /* avoid printing too many frames */
32176cfd
RP
951 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
952 ieee80211_msg_dumppkts(vap)) {
1e290df3 953 kprintf("[%s] send %s on channel %u\n",
085ff963 954 ether_sprintf(wh->i_addr1),
4f655ef5 955 ieee80211_mgt_subtype_name(type),
841ab66c
SZ
956 ieee80211_chan2ieee(ic, ic->ic_curchan));
957 }
f186073c 958#endif
841ab66c 959 IEEE80211_NODE_STAT(ni, tx_mgmt);
32176cfd 960
085ff963
MD
961 ret = ieee80211_raw_output(vap, ni, m, params);
962 IEEE80211_TX_UNLOCK(ic);
963 return (ret);
841ab66c 964}
f186073c 965
4f655ef5
MD
966static void
967ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
968 int status)
969{
970 struct ieee80211vap *vap = ni->ni_vap;
971
972 wakeup(vap);
973}
974
841ab66c 975/*
32176cfd
RP
976 * Send a null data frame to the specified node. If the station
977 * is setup for QoS then a QoS Null Data frame is constructed.
978 * If this is a WDS station then a 4-address frame is constructed.
841ab66c
SZ
979 *
980 * NB: the caller is assumed to have setup a node reference
981 * for use; this is necessary to deal with a race condition
32176cfd
RP
982 * when probing for inactive stations. Like ieee80211_mgmt_output
983 * we must cleanup any node reference on error; however we
984 * can safely just unref it as we know it will never be the
985 * last reference to the node.
841ab66c
SZ
986 */
987int
988ieee80211_send_nulldata(struct ieee80211_node *ni)
989{
32176cfd 990 struct ieee80211vap *vap = ni->ni_vap;
841ab66c 991 struct ieee80211com *ic = ni->ni_ic;
841ab66c
SZ
992 struct mbuf *m;
993 struct ieee80211_frame *wh;
32176cfd
RP
994 int hdrlen;
995 uint8_t *frm;
085ff963 996 int ret;
32176cfd
RP
997
998 if (vap->iv_state == IEEE80211_S_CAC) {
999 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1000 ni, "block %s frame in CAC state", "null data");
1001 ieee80211_unref_node(&ni);
1002 vap->iv_stats.is_tx_badstate++;
1003 return EIO; /* XXX */
1004 }
1005
1006 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1007 hdrlen = sizeof(struct ieee80211_qosframe);
1008 else
1009 hdrlen = sizeof(struct ieee80211_frame);
1010 /* NB: only WDS vap's get 4-address frames */
1011 if (vap->iv_opmode == IEEE80211_M_WDS)
1012 hdrlen += IEEE80211_ADDR_LEN;
1013 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1014 hdrlen = roundup(hdrlen, sizeof(uint32_t));
841ab66c 1015
32176cfd 1016 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
841ab66c
SZ
1017 if (m == NULL) {
1018 /* XXX debug msg */
841ab66c 1019 ieee80211_unref_node(&ni);
32176cfd
RP
1020 vap->iv_stats.is_tx_nobuf++;
1021 return ENOMEM;
1022 }
1023 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1024 ("leading space %zd", M_LEADINGSPACE(m)));
b5523eac 1025 M_PREPEND(m, hdrlen, M_NOWAIT);
32176cfd
RP
1026 if (m == NULL) {
1027 /* NB: cannot happen */
1028 ieee80211_free_node(ni);
841ab66c
SZ
1029 return ENOMEM;
1030 }
841ab66c 1031
085ff963
MD
1032 IEEE80211_TX_LOCK(ic);
1033
32176cfd
RP
1034 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
1035 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1036 const int tid = WME_AC_TO_TID(WME_AC_BE);
1037 uint8_t *qos;
1038
1039 ieee80211_send_setup(ni, m,
1040 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1041 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1042
1043 if (vap->iv_opmode == IEEE80211_M_WDS)
1044 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1045 else
1046 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1047 qos[0] = tid & IEEE80211_QOS_TID;
1048 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1049 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1050 qos[1] = 0;
1051 } else {
1052 ieee80211_send_setup(ni, m,
1053 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1054 IEEE80211_NONQOS_TID,
1055 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1056 }
1057 if (vap->iv_opmode != IEEE80211_M_WDS) {
1058 /* NB: power management bit is never sent by an AP */
1059 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1060 vap->iv_opmode != IEEE80211_M_HOSTAP)
1061 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1062 }
4f655ef5
MD
1063 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1064 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1065 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1066 NULL);
1067 }
32176cfd
RP
1068 m->m_len = m->m_pkthdr.len = hdrlen;
1069 m->m_flags |= M_ENCAP; /* mark encapsulated */
1070
1071 M_WME_SETAC(m, WME_AC_BE);
841ab66c
SZ
1072
1073 IEEE80211_NODE_STAT(ni, tx_data);
1074
32176cfd
RP
1075 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1076 "send %snull data frame on channel %u, pwr mgt %s",
1077 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
841ab66c
SZ
1078 ieee80211_chan2ieee(ic, ic->ic_curchan),
1079 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1080
085ff963
MD
1081 ret = ieee80211_raw_output(vap, ni, m, NULL);
1082 IEEE80211_TX_UNLOCK(ic);
1083 return (ret);
841ab66c
SZ
1084}
1085
1086/*
1087 * Assign priority to a frame based on any vlan tag assigned
1088 * to the station and/or any Diffserv setting in an IP header.
1089 * Finally, if an ACM policy is setup (in station mode) it's
1090 * applied.
1091 */
1092int
32176cfd 1093ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
841ab66c 1094{
32176cfd
RP
1095 const struct ether_header *eh = mtod(m, struct ether_header *);
1096 int v_wme_ac, d_wme_ac, ac;
841ab66c 1097
32176cfd
RP
1098 /*
1099 * Always promote PAE/EAPOL frames to high priority.
1100 */
1101 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
1102 /* NB: mark so others don't need to check header */
1103 m->m_flags |= M_EAPOL;
1104 ac = WME_AC_VO;
1105 goto done;
1106 }
1107 /*
1108 * Non-qos traffic goes to BE.
1109 */
841ab66c
SZ
1110 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1111 ac = WME_AC_BE;
1112 goto done;
1113 }
1114
841ab66c
SZ
1115 /*
1116 * If node has a vlan tag then all traffic
1117 * to it must have a matching tag.
1118 */
1119 v_wme_ac = 0;
1120 if (ni->ni_vlan != 0) {
32176cfd 1121 if ((m->m_flags & M_VLANTAG) == 0) {
841ab66c
SZ
1122 IEEE80211_NODE_STAT(ni, tx_novlantag);
1123 return 1;
1124 }
085ff963 1125#if defined(__DragonFly__)
32176cfd 1126 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
841ab66c
SZ
1127 EVL_VLANOFTAG(ni->ni_vlan)) {
1128 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1129 return 1;
1130 }
085ff963
MD
1131#else
1132 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1133 EVL_VLANOFTAG(ni->ni_vlan)) {
1134 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1135 return 1;
1136 }
1137#endif
841ab66c 1138 /* map vlan priority to AC */
32176cfd 1139 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
841ab66c 1140 }
841ab66c 1141
32176cfd 1142 /* XXX m_copydata may be too slow for fast path */
841ab66c 1143#ifdef INET
841ab66c 1144 if (eh->ether_type == htons(ETHERTYPE_IP)) {
32176cfd 1145 uint8_t tos;
841ab66c 1146 /*
32176cfd 1147 * IP frame, map the DSCP bits from the TOS field.
841ab66c 1148 */
32176cfd
RP
1149 /* NB: ip header may not be in first mbuf */
1150 m_copydata(m, sizeof(struct ether_header) +
1151 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1152 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1153 d_wme_ac = TID_TO_WME_AC(tos);
841ab66c
SZ
1154 } else {
1155#endif /* INET */
32176cfd
RP
1156#ifdef INET6
1157 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
1158 uint32_t flow;
1159 uint8_t tos;
1160 /*
085ff963 1161 * IPv6 frame, map the DSCP bits from the traffic class field.
32176cfd
RP
1162 */
1163 m_copydata(m, sizeof(struct ether_header) +
05d02a38 1164 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), &flow);
32176cfd
RP
1165 tos = (uint8_t)(ntohl(flow) >> 20);
1166 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1167 d_wme_ac = TID_TO_WME_AC(tos);
1168 } else {
1169#endif /* INET6 */
841ab66c 1170 d_wme_ac = WME_AC_BE;
32176cfd
RP
1171#ifdef INET6
1172 }
1173#endif
841ab66c
SZ
1174#ifdef INET
1175 }
1176#endif
1177 /*
1178 * Use highest priority AC.
1179 */
1180 if (v_wme_ac > d_wme_ac)
1181 ac = v_wme_ac;
1182 else
1183 ac = d_wme_ac;
1184
1185 /*
1186 * Apply ACM policy.
1187 */
32176cfd 1188 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
841ab66c
SZ
1189 static const int acmap[4] = {
1190 WME_AC_BK, /* WME_AC_BE */
1191 WME_AC_BK, /* WME_AC_BK */
1192 WME_AC_BE, /* WME_AC_VI */
1193 WME_AC_VI, /* WME_AC_VO */
1194 };
32176cfd
RP
1195 struct ieee80211com *ic = ni->ni_ic;
1196
841ab66c
SZ
1197 while (ac != WME_AC_BK &&
1198 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1199 ac = acmap[ac];
1200 }
1201done:
1202 M_WME_SETAC(m, ac);
f186073c
JS
1203 return 0;
1204}
1205
1206/*
841ab66c
SZ
1207 * Insure there is sufficient contiguous space to encapsulate the
1208 * 802.11 data frame. If room isn't already there, arrange for it.
1209 * Drivers and cipher modules assume we have done the necessary work
1210 * and fail rudely if they don't find the space they need.
1211 */
32176cfd
RP
1212struct mbuf *
1213ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
841ab66c
SZ
1214 struct ieee80211_key *key, struct mbuf *m)
1215{
1216#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
32176cfd 1217 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
841ab66c
SZ
1218
1219 if (key != NULL) {
1220 /* XXX belongs in crypto code? */
32176cfd 1221 needed_space += key->wk_cipher->ic_header;
841ab66c
SZ
1222 /* XXX frags */
1223 /*
1224 * When crypto is being done in the host we must insure
1225 * the data are writable for the cipher routines; clone
1226 * a writable mbuf chain.
1227 * XXX handle SWMIC specially
1228 */
32176cfd 1229 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
b5523eac 1230 m = m_unshare(m, M_NOWAIT);
841ab66c 1231 if (m == NULL) {
32176cfd 1232 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
841ab66c 1233 "%s: cannot get writable mbuf\n", __func__);
32176cfd 1234 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
841ab66c
SZ
1235 return NULL;
1236 }
1237 }
1238 }
1239 /*
1240 * We know we are called just before stripping an Ethernet
1241 * header and prepending an LLC header. This means we know
1242 * there will be
1243 * sizeof(struct ether_header) - sizeof(struct llc)
1244 * bytes recovered to which we need additional space for the
1245 * 802.11 header and any crypto header.
1246 */
1247 /* XXX check trailing space and copy instead? */
1248 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
b5523eac 1249 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
841ab66c 1250 if (n == NULL) {
32176cfd 1251 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
841ab66c 1252 "%s: cannot expand storage\n", __func__);
32176cfd 1253 vap->iv_stats.is_tx_nobuf++;
841ab66c
SZ
1254 m_freem(m);
1255 return NULL;
1256 }
085ff963
MD
1257#if defined(__DragonFly__)
1258 KASSERT(needed_space <= MHLEN,
1259 ("not enough room, need %u got %zd\n", needed_space, MHLEN));
1260#else
841ab66c 1261 KASSERT(needed_space <= MHLEN,
085ff963
MD
1262 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1263#endif
841ab66c
SZ
1264 /*
1265 * Setup new mbuf to have leading space to prepend the
1266 * 802.11 header and any crypto header bits that are
1267 * required (the latter are added when the driver calls
1268 * back to ieee80211_crypto_encap to do crypto encapsulation).
1269 */
1270 /* NB: must be first 'cuz it clobbers m_data */
1271 m_move_pkthdr(n, m);
1272 n->m_len = 0; /* NB: m_gethdr does not set */
1273 n->m_data += needed_space;
1274 /*
1275 * Pull up Ethernet header to create the expected layout.
1276 * We could use m_pullup but that's overkill (i.e. we don't
1277 * need the actual data) and it cannot fail so do it inline
1278 * for speed.
1279 */
1280 /* NB: struct ether_header is known to be contiguous */
1281 n->m_len += sizeof(struct ether_header);
1282 m->m_len -= sizeof(struct ether_header);
1283 m->m_data += sizeof(struct ether_header);
1284 /*
1285 * Replace the head of the chain.
1286 */
1287 n->m_next = m;
1288 m = n;
1289 }
1290 return m;
1291#undef TO_BE_RECLAIMED
1292}
1293
841ab66c
SZ
1294/*
1295 * Return the transmit key to use in sending a unicast frame.
1296 * If a unicast key is set we use that. When no unicast key is set
1297 * we fall back to the default transmit key.
1298 */
1299static __inline struct ieee80211_key *
32176cfd
RP
1300ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1301 struct ieee80211_node *ni)
841ab66c 1302{
32176cfd
RP
1303 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1304 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1305 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
841ab66c 1306 return NULL;
32176cfd 1307 return &vap->iv_nw_keys[vap->iv_def_txkey];
841ab66c
SZ
1308 } else {
1309 return &ni->ni_ucastkey;
1310 }
1311}
1312
1313/*
1314 * Return the transmit key to use in sending a multicast frame.
1315 * Multicast traffic always uses the group key which is installed as
1316 * the default tx key.
1317 */
1318static __inline struct ieee80211_key *
32176cfd
RP
1319ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1320 struct ieee80211_node *ni)
841ab66c 1321{
32176cfd
RP
1322 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1323 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
841ab66c 1324 return NULL;
32176cfd 1325 return &vap->iv_nw_keys[vap->iv_def_txkey];
841ab66c
SZ
1326}
1327
1328/*
1329 * Encapsulate an outbound data frame. The mbuf chain is updated.
1330 * If an error is encountered NULL is returned. The caller is required
1331 * to provide a node reference and pullup the ethernet header in the
1332 * first mbuf.
32176cfd
RP
1333 *
1334 * NB: Packet is assumed to be processed by ieee80211_classify which
1335 * marked EAPOL frames w/ M_EAPOL.
f186073c
JS
1336 */
1337struct mbuf *
32176cfd
RP
1338ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1339 struct mbuf *m)
f186073c 1340{
32176cfd 1341#define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
085ff963 1342#define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
32176cfd
RP
1343 struct ieee80211com *ic = ni->ni_ic;
1344#ifdef IEEE80211_SUPPORT_MESH
1345 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1346 struct ieee80211_meshcntl_ae10 *mc;
085ff963
MD
1347 struct ieee80211_mesh_route *rt = NULL;
1348 int dir = -1;
32176cfd 1349#endif
f186073c
JS
1350 struct ether_header eh;
1351 struct ieee80211_frame *wh;
841ab66c 1352 struct ieee80211_key *key;
f186073c 1353 struct llc *llc;
32176cfd
RP
1354 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1355 ieee80211_seq seqno;
1356 int meshhdrsize, meshae;
1357 uint8_t *qos;
4f655ef5 1358 int is_amsdu = 0;
f186073c 1359
085ff963
MD
1360 IEEE80211_TX_LOCK_ASSERT(ic);
1361
32176cfd
RP
1362 /*
1363 * Copy existing Ethernet header to a safe place. The
1364 * rest of the code assumes it's ok to strip it when
1365 * reorganizing state for the final encapsulation.
1366 */
841ab66c 1367 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
32176cfd 1368 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
f186073c 1369
841ab66c
SZ
1370 /*
1371 * Insure space for additional headers. First identify
1372 * transmit key to use in calculating any buffer adjustments
1373 * required. This is also used below to do privacy
1374 * encapsulation work. Then calculate the 802.11 header
1375 * size and any padding required by the driver.
1376 *
1377 * Note key may be NULL if we fall back to the default
1378 * transmit key and that is not set. In that case the
1379 * buffer may not be expanded as needed by the cipher
1380 * routines, but they will/should discard it.
1381 */
32176cfd
RP
1382 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1383 if (vap->iv_opmode == IEEE80211_M_STA ||
1384 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1385 (vap->iv_opmode == IEEE80211_M_WDS &&
1386 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1387 key = ieee80211_crypto_getucastkey(vap, ni);
841ab66c 1388 else
32176cfd
RP
1389 key = ieee80211_crypto_getmcastkey(vap, ni);
1390 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1391 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1392 eh.ether_dhost,
1393 "no default transmit key (%s) deftxkey %u",
1394 __func__, vap->iv_def_txkey);
1395 vap->iv_stats.is_tx_nodefkey++;
a92bce5e 1396 goto bad;
841ab66c
SZ
1397 }
1398 } else
1399 key = NULL;
841ab66c
SZ
1400 /*
1401 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1402 * frames so suppress use. This may be an issue if other
1403 * ap's require all data frames to be QoS-encapsulated
1404 * once negotiated in which case we'll need to make this
1405 * configurable.
085ff963 1406 * NB: mesh data frames are QoS.
841ab66c 1407 */
085ff963
MD
1408 addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1409 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1410 (m->m_flags & M_EAPOL) == 0;
841ab66c
SZ
1411 if (addqos)
1412 hdrsize = sizeof(struct ieee80211_qosframe);
1413 else
1414 hdrsize = sizeof(struct ieee80211_frame);
32176cfd
RP
1415#ifdef IEEE80211_SUPPORT_MESH
1416 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1417 /*
1418 * Mesh data frames are encapsulated according to the
1419 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1420 * o Group Addressed data (aka multicast) originating
1421 * at the local sta are sent w/ 3-address format and
1422 * address extension mode 00
1423 * o Individually Addressed data (aka unicast) originating
1424 * at the local sta are sent w/ 4-address format and
1425 * address extension mode 00
1426 * o Group Addressed data forwarded from a non-mesh sta are
1427 * sent w/ 3-address format and address extension mode 01
1428 * o Individually Address data from another sta are sent
1429 * w/ 4-address format and address extension mode 10
1430 */
1431 is4addr = 0; /* NB: don't use, disable */
085ff963
MD
1432 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1433 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1434 KASSERT(rt != NULL, ("route is NULL"));
1435 dir = IEEE80211_FC1_DIR_DSTODS;
1436 hdrsize += IEEE80211_ADDR_LEN;
1437 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1438 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1439 vap->iv_myaddr)) {
1440 IEEE80211_NOTE_MAC(vap,
1441 IEEE80211_MSG_MESH,
1442 eh.ether_dhost,
1443 "%s", "trying to send to ourself");
1444 goto bad;
1445 }
1446 meshae = IEEE80211_MESH_AE_10;
1447 meshhdrsize =
1448 sizeof(struct ieee80211_meshcntl_ae10);
1449 } else {
1450 meshae = IEEE80211_MESH_AE_00;
1451 meshhdrsize =
1452 sizeof(struct ieee80211_meshcntl);
1453 }
32176cfd 1454 } else {
085ff963
MD
1455 dir = IEEE80211_FC1_DIR_FROMDS;
1456 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1457 /* proxy group */
1458 meshae = IEEE80211_MESH_AE_01;
1459 meshhdrsize =
1460 sizeof(struct ieee80211_meshcntl_ae01);
1461 } else {
1462 /* group */
1463 meshae = IEEE80211_MESH_AE_00;
1464 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1465 }
32176cfd
RP
1466 }
1467 } else {
1468#endif
1469 /*
1470 * 4-address frames need to be generated for:
1471 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1472 * o packets sent through a vap marked for relaying
1473 * (e.g. a station operating with dynamic WDS)
1474 */
1475 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1476 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1477 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1478 if (is4addr)
1479 hdrsize += IEEE80211_ADDR_LEN;
1480 meshhdrsize = meshae = 0;
1481#ifdef IEEE80211_SUPPORT_MESH
f186073c 1482 }
32176cfd
RP
1483#endif
1484 /*
1485 * Honor driver DATAPAD requirement.
1486 */
1487 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1488 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1489 else
1490 hdrspace = hdrsize;
f186073c 1491
32176cfd
RP
1492 if (__predict_true((m->m_flags & M_FF) == 0)) {
1493 /*
1494 * Normal frame.
1495 */
1496 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1497 if (m == NULL) {
1498 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1499 goto bad;
1500 }
1501 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1502 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1503 llc = mtod(m, struct llc *);
1504 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1505 llc->llc_control = LLC_UI;
1506 llc->llc_snap.org_code[0] = 0;
1507 llc->llc_snap.org_code[1] = 0;
1508 llc->llc_snap.org_code[2] = 0;
1509 llc->llc_snap.ether_type = eh.ether_type;
1510 } else {
1511#ifdef IEEE80211_SUPPORT_SUPERG
1512 /*
4f655ef5
MD
1513 * Aggregated frame. Check if it's for AMSDU or FF.
1514 *
1515 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1516 * anywhere for some reason. But, since 11n requires
1517 * AMSDU RX, we can just assume "11n" == "AMSDU".
32176cfd 1518 */
4f655ef5
MD
1519 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1520 if (ieee80211_amsdu_tx_ok(ni)) {
1521 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1522 is_amsdu = 1;
1523 } else {
1524 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1525 }
32176cfd
RP
1526 if (m == NULL)
1527#endif
1528 goto bad;
1529 }
841ab66c
SZ
1530 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1531
b5523eac 1532 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
f186073c 1533 if (m == NULL) {
32176cfd 1534 vap->iv_stats.is_tx_nobuf++;
f186073c
JS
1535 goto bad;
1536 }
1537 wh = mtod(m, struct ieee80211_frame *);
1538 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1539 *(uint16_t *)wh->i_dur = 0;
32176cfd
RP
1540 qos = NULL; /* NB: quiet compiler */
1541 if (is4addr) {
1542 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1543 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1544 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1545 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1546 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1547 } else switch (vap->iv_opmode) {
f186073c
JS
1548 case IEEE80211_M_STA:
1549 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1550 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1551 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1552 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1553 break;
1554 case IEEE80211_M_IBSS:
1555 case IEEE80211_M_AHDEMO:
1556 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1557 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1558 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
841ab66c 1559 /*
32176cfd 1560 * NB: always use the bssid from iv_bss as the
841ab66c
SZ
1561 * neighbor's may be stale after an ibss merge
1562 */
32176cfd 1563 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
f186073c
JS
1564 break;
1565 case IEEE80211_M_HOSTAP:
1566 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1567 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1568 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1569 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1570 break;
32176cfd
RP
1571#ifdef IEEE80211_SUPPORT_MESH
1572 case IEEE80211_M_MBSS:
1573 /* NB: offset by hdrspace to deal with DATAPAD */
1574 mc = (struct ieee80211_meshcntl_ae10 *)
1575 (mtod(m, uint8_t *) + hdrspace);
085ff963 1576 wh->i_fc[1] = dir;
32176cfd 1577 switch (meshae) {
085ff963 1578 case IEEE80211_MESH_AE_00: /* no proxy */
32176cfd 1579 mc->mc_flags = 0;
085ff963
MD
1580 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1581 IEEE80211_ADDR_COPY(wh->i_addr1,
1582 ni->ni_macaddr);
1583 IEEE80211_ADDR_COPY(wh->i_addr2,
1584 vap->iv_myaddr);
1585 IEEE80211_ADDR_COPY(wh->i_addr3,
1586 eh.ether_dhost);
1587 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1588 eh.ether_shost);
1589 qos =((struct ieee80211_qosframe_addr4 *)
1590 wh)->i_qos;
1591 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1592 /* mcast */
1593 IEEE80211_ADDR_COPY(wh->i_addr1,
1594 eh.ether_dhost);
1595 IEEE80211_ADDR_COPY(wh->i_addr2,
1596 vap->iv_myaddr);
1597 IEEE80211_ADDR_COPY(wh->i_addr3,
1598 eh.ether_shost);
1599 qos = ((struct ieee80211_qosframe *)
1600 wh)->i_qos;
1601 }
32176cfd 1602 break;
085ff963 1603 case IEEE80211_MESH_AE_01: /* mcast, proxy */
32176cfd
RP
1604 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1605 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1606 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1607 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1608 mc->mc_flags = 1;
085ff963
MD
1609 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1610 eh.ether_shost);
32176cfd
RP
1611 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1612 break;
085ff963
MD
1613 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1614 KASSERT(rt != NULL, ("route is NULL"));
1615 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
32176cfd 1616 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
085ff963 1617 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
32176cfd 1618 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
085ff963
MD
1619 mc->mc_flags = IEEE80211_MESH_AE_10;
1620 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1621 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
32176cfd
RP
1622 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1623 break;
1624 default:
1625 KASSERT(0, ("meshae %d", meshae));
1626 break;
1627 }
1628 mc->mc_ttl = ms->ms_ttl;
1629 ms->ms_seq++;
4f655ef5 1630 le32enc(mc->mc_seq, ms->ms_seq);
32176cfd
RP
1631 break;
1632#endif
1633 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1634 default:
f186073c
JS
1635 goto bad;
1636 }
841ab66c
SZ
1637 if (m->m_flags & M_MORE_DATA)
1638 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1639 if (addqos) {
841ab66c
SZ
1640 int ac, tid;
1641
32176cfd
RP
1642 if (is4addr) {
1643 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1644 /* NB: mesh case handled earlier */
1645 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1646 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
841ab66c
SZ
1647 ac = M_WME_GETAC(m);
1648 /* map from access class/queue to 11e header priorty value */
1649 tid = WME_AC_TO_TID(ac);
32176cfd 1650 qos[0] = tid & IEEE80211_QOS_TID;
841ab66c 1651 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
32176cfd 1652 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
085ff963
MD
1653#ifdef IEEE80211_SUPPORT_MESH
1654 if (vap->iv_opmode == IEEE80211_M_MBSS)
1655 qos[1] = IEEE80211_QOS_MC;
1656 else
1657#endif
1658 qos[1] = 0;
32176cfd 1659 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
841ab66c 1660
4f655ef5
MD
1661 /*
1662 * If this is an A-MSDU then ensure we set the
1663 * relevant field.
1664 */
1665 if (is_amsdu)
1666 qos[0] |= IEEE80211_QOS_AMSDU;
1667
32176cfd
RP
1668 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1669 /*
1670 * NB: don't assign a sequence # to potential
1671 * aggregates; we expect this happens at the
1672 * point the frame comes off any aggregation q
1673 * as otherwise we may introduce holes in the
1674 * BA sequence space and/or make window accouting
1675 * more difficult.
1676 *
1677 * XXX may want to control this with a driver
1678 * capability; this may also change when we pull
1679 * aggregation up into net80211
1680 */
1681 seqno = ni->ni_txseqs[tid]++;
1682 *(uint16_t *)wh->i_seq =
1683 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1684 M_SEQNO_SET(m, seqno);
1685 }
841ab66c 1686 } else {
32176cfd 1687 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
841ab66c 1688 *(uint16_t *)wh->i_seq =
32176cfd
RP
1689 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1690 M_SEQNO_SET(m, seqno);
4f655ef5
MD
1691
1692 /*
1693 * XXX TODO: we shouldn't allow EAPOL, etc that would
1694 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1695 */
1696 if (is_amsdu)
1697 kprintf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1698 __func__);
841ab66c 1699 }
32176cfd
RP
1700
1701
1702 /* check if xmit fragmentation is required */
1703 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1704 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1705 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1706 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
841ab66c
SZ
1707 if (key != NULL) {
1708 /*
1709 * IEEE 802.1X: send EAPOL frames always in the clear.
1710 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1711 */
32176cfd
RP
1712 if ((m->m_flags & M_EAPOL) == 0 ||
1713 ((vap->iv_flags & IEEE80211_F_WPA) &&
1714 (vap->iv_opmode == IEEE80211_M_STA ?
1715 !IEEE80211_KEY_UNDEFINED(key) :
1716 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
085ff963 1717 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
32176cfd
RP
1718 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1719 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1720 eh.ether_dhost,
1721 "%s", "enmic failed, discard frame");
1722 vap->iv_stats.is_crypto_enmicfail++;
841ab66c
SZ
1723 goto bad;
1724 }
1725 }
1726 }
32176cfd
RP
1727 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1728 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1729 goto bad;
1730
1731 m->m_flags |= M_ENCAP; /* mark encapsulated */
841ab66c
SZ
1732
1733 IEEE80211_NODE_STAT(ni, tx_data);
32176cfd 1734 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
fb134238 1735 IEEE80211_NODE_STAT(ni, tx_mcast);
32176cfd
RP
1736 m->m_flags |= M_MCAST;
1737 } else
fb134238 1738 IEEE80211_NODE_STAT(ni, tx_ucast);
841ab66c
SZ
1739 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1740
f186073c
JS
1741 return m;
1742bad:
1743 if (m != NULL)
1744 m_freem(m);
f186073c 1745 return NULL;
32176cfd 1746#undef WH4
085ff963 1747#undef MC01
32176cfd
RP
1748}
1749
4f655ef5
MD
1750void
1751ieee80211_free_mbuf(struct mbuf *m)
1752{
1753 struct mbuf *next;
1754
1755 if (m == NULL)
1756 return;
1757
1758 do {
1759 next = m->m_nextpkt;
1760 m->m_nextpkt = NULL;
1761 m_freem(m);
1762 } while ((m = next) != NULL);
1763}
1764
32176cfd
RP
1765/*
1766 * Fragment the frame according to the specified mtu.
1767 * The size of the 802.11 header (w/o padding) is provided
1768 * so we don't need to recalculate it. We create a new
1769 * mbuf for each fragment and chain it through m_nextpkt;
1770 * we might be able to optimize this by reusing the original
1771 * packet's mbufs but that is significantly more complicated.
1772 */
1773static int
1774ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1775 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1776{
085ff963 1777 struct ieee80211com *ic = vap->iv_ic;
32176cfd 1778 struct ieee80211_frame *wh, *whf;
4f655ef5 1779 struct mbuf *m, *prev;
32176cfd 1780 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
085ff963 1781 u_int hdrspace;
32176cfd
RP
1782
1783 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1784 KASSERT(m0->m_pkthdr.len > mtu,
1785 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1786
085ff963
MD
1787 /*
1788 * Honor driver DATAPAD requirement.
1789 */
1790 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1791 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1792 else
1793 hdrspace = hdrsize;
1794
32176cfd
RP
1795 wh = mtod(m0, struct ieee80211_frame *);
1796 /* NB: mark the first frag; it will be propagated below */
1797 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
085ff963 1798 totalhdrsize = hdrspace + ciphdrsize;
32176cfd
RP
1799 fragno = 1;
1800 off = mtu - ciphdrsize;
1801 remainder = m0->m_pkthdr.len - off;
1802 prev = m0;
1803 do {
1804 fragsize = totalhdrsize + remainder;
1805 if (fragsize > mtu)
1806 fragsize = mtu;
1807 /* XXX fragsize can be >2048! */
1808 KASSERT(fragsize < MCLBYTES,
1809 ("fragment size %u too big!", fragsize));
1810 if (fragsize > MHLEN)
b5523eac 1811 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
32176cfd 1812 else
b5523eac 1813 m = m_gethdr(M_NOWAIT, MT_DATA);
32176cfd
RP
1814 if (m == NULL)
1815 goto bad;
1816 /* leave room to prepend any cipher header */
1817 m_align(m, fragsize - ciphdrsize);
1818
1819 /*
1820 * Form the header in the fragment. Note that since
1821 * we mark the first fragment with the MORE_FRAG bit
1822 * it automatically is propagated to each fragment; we
1823 * need only clear it on the last fragment (done below).
085ff963 1824 * NB: frag 1+ dont have Mesh Control field present.
32176cfd
RP
1825 */
1826 whf = mtod(m, struct ieee80211_frame *);
1827 memcpy(whf, wh, hdrsize);
085ff963
MD
1828#ifdef IEEE80211_SUPPORT_MESH
1829 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1830 if (IEEE80211_IS_DSTODS(wh))
1831 ((struct ieee80211_qosframe_addr4 *)
1832 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1833 else
1834 ((struct ieee80211_qosframe *)
1835 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1836 }
1837#endif
32176cfd
RP
1838 *(uint16_t *)&whf->i_seq[0] |= htole16(
1839 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1840 IEEE80211_SEQ_FRAG_SHIFT);
1841 fragno++;
1842
1843 payload = fragsize - totalhdrsize;
1844 /* NB: destination is known to be contiguous */
085ff963
MD
1845
1846 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1847 m->m_len = hdrspace + payload;
1848 m->m_pkthdr.len = hdrspace + payload;
32176cfd
RP
1849 m->m_flags |= M_FRAG;
1850
1851 /* chain up the fragment */
1852 prev->m_nextpkt = m;
1853 prev = m;
1854
1855 /* deduct fragment just formed */
1856 remainder -= payload;
1857 off += payload;
1858 } while (remainder != 0);
1859
1860 /* set the last fragment */
1861 m->m_flags |= M_LASTFRAG;
1862 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1863
1864 /* strip first mbuf now that everything has been copied */
1865 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1866 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1867
1868 vap->iv_stats.is_tx_fragframes++;
1869 vap->iv_stats.is_tx_frags += fragno-1;
1870
1871 return 1;
1872bad:
1873 /* reclaim fragments but leave original frame for caller to free */
4f655ef5 1874 ieee80211_free_mbuf(m0->m_nextpkt);
32176cfd
RP
1875 m0->m_nextpkt = NULL;
1876 return 0;
f186073c
JS
1877}
1878
1879/*
1880 * Add a supported rates element id to a frame.
1881 */
1882uint8_t *
1883ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1884{
1885 int nrates;
1886
1887 *frm++ = IEEE80211_ELEMID_RATES;
1888 nrates = rs->rs_nrates;
1889 if (nrates > IEEE80211_RATE_SIZE)
1890 nrates = IEEE80211_RATE_SIZE;
1891 *frm++ = nrates;
1892 memcpy(frm, rs->rs_rates, nrates);
1893 return frm + nrates;
1894}
1895
1896/*
1897 * Add an extended supported rates element id to a frame.
1898 */
1899uint8_t *
1900ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1901{
1902 /*
1903 * Add an extended supported rates element if operating in 11g mode.
1904 */
1905 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1906 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1907 *frm++ = IEEE80211_ELEMID_XRATES;
1908 *frm++ = nrates;
1909 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1910 frm += nrates;
1911 }
1912 return frm;
1913}
1914
1915/*
32176cfd 1916 * Add an ssid element to a frame.
f186073c 1917 */
e218318c 1918uint8_t *
f186073c
JS
1919ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1920{
1921 *frm++ = IEEE80211_ELEMID_SSID;
1922 *frm++ = len;
1923 memcpy(frm, ssid, len);
1924 return frm + len;
1925}
1926
841ab66c
SZ
1927/*
1928 * Add an erp element to a frame.
1929 */
1930static uint8_t *
1931ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1932{
1933 uint8_t erp;
1934
1935 *frm++ = IEEE80211_ELEMID_ERP;
1936 *frm++ = 1;
1937 erp = 0;
1938 if (ic->ic_nonerpsta != 0)
1939 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1940 if (ic->ic_flags & IEEE80211_F_USEPROT)
1941 erp |= IEEE80211_ERP_USE_PROTECTION;
1942 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1943 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1944 *frm++ = erp;
1945 return frm;
1946}
1947
32176cfd
RP
1948/*
1949 * Add a CFParams element to a frame.
1950 */
841ab66c 1951static uint8_t *
32176cfd 1952ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
841ab66c 1953{
32176cfd 1954#define ADDSHORT(frm, v) do { \
4f655ef5 1955 le16enc(frm, v); \
32176cfd 1956 frm += 2; \
841ab66c 1957} while (0)
32176cfd
RP
1958 *frm++ = IEEE80211_ELEMID_CFPARMS;
1959 *frm++ = 6;
1960 *frm++ = 0; /* CFP count */
1961 *frm++ = 2; /* CFP period */
1962 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1963 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
841ab66c
SZ
1964 return frm;
1965#undef ADDSHORT
841ab66c
SZ
1966}
1967
32176cfd
RP
1968static __inline uint8_t *
1969add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
f186073c 1970{
32176cfd
RP
1971 memcpy(frm, ie->ie_data, ie->ie_len);
1972 return frm + ie->ie_len;
841ab66c
SZ
1973}
1974
32176cfd
RP
1975static __inline uint8_t *
1976add_ie(uint8_t *frm, const uint8_t *ie)
841ab66c 1977{
32176cfd
RP
1978 memcpy(frm, ie, 2 + ie[1]);
1979 return frm + 2 + ie[1];
841ab66c
SZ
1980}
1981
1982#define WME_OUI_BYTES 0x00, 0x50, 0xf2
1983/*
1984 * Add a WME information element to a frame.
1985 */
4f655ef5 1986uint8_t *
841ab66c
SZ
1987ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1988{
1989 static const struct ieee80211_wme_info info = {
1990 .wme_id = IEEE80211_ELEMID_VENDOR,
1991 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1992 .wme_oui = { WME_OUI_BYTES },
1993 .wme_type = WME_OUI_TYPE,
1994 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1995 .wme_version = WME_VERSION,
1996 .wme_info = 0,
1997 };
1998 memcpy(frm, &info, sizeof(info));
1999 return frm + sizeof(info);
2000}
2001
2002/*
2003 * Add a WME parameters element to a frame.
2004 */
2005static uint8_t *
2006ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
2007{
2008#define SM(_v, _f) (((_v) << _f##_S) & _f)
32176cfd 2009#define ADDSHORT(frm, v) do { \
4f655ef5 2010 le16enc(frm, v); \
32176cfd 2011 frm += 2; \
841ab66c
SZ
2012} while (0)
2013 /* NB: this works 'cuz a param has an info at the front */
2014 static const struct ieee80211_wme_info param = {
2015 .wme_id = IEEE80211_ELEMID_VENDOR,
2016 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2017 .wme_oui = { WME_OUI_BYTES },
2018 .wme_type = WME_OUI_TYPE,
2019 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2020 .wme_version = WME_VERSION,
2021 };
2022 int i;
2023
2024 memcpy(frm, &param, sizeof(param));
2025 frm += __offsetof(struct ieee80211_wme_info, wme_info);
2026 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
2027 *frm++ = 0; /* reserved field */
2028 for (i = 0; i < WME_NUM_AC; i++) {
2029 const struct wmeParams *ac =
2030 &wme->wme_bssChanParams.cap_wmeParams[i];
2031 *frm++ = SM(i, WME_PARAM_ACI)
2032 | SM(ac->wmep_acm, WME_PARAM_ACM)
2033 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
2034 ;
2035 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
2036 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
2037 ;
2038 ADDSHORT(frm, ac->wmep_txopLimit);
2039 }
2040 return frm;
2041#undef SM
2042#undef ADDSHORT
2043}
2044#undef WME_OUI_BYTES
2045
32176cfd
RP
2046/*
2047 * Add an 11h Power Constraint element to a frame.
2048 */
2049static uint8_t *
2050ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2051{
2052 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2053 /* XXX per-vap tx power limit? */
2054 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2055
2056 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2057 frm[1] = 1;
2058 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
2059 return frm + 3;
2060}
2061
2062/*
2063 * Add an 11h Power Capability element to a frame.
2064 */
2065static uint8_t *
2066ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2067{
2068 frm[0] = IEEE80211_ELEMID_PWRCAP;
2069 frm[1] = 2;
2070 frm[2] = c->ic_minpower;
2071 frm[3] = c->ic_maxpower;
2072 return frm + 4;
2073}
2074
2075/*
2076 * Add an 11h Supported Channels element to a frame.
2077 */
2078static uint8_t *
2079ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2080{
2081 static const int ielen = 26;
2082
2083 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2084 frm[1] = ielen;
2085 /* XXX not correct */
2086 memcpy(frm+2, ic->ic_chan_avail, ielen);
2087 return frm + 2 + ielen;
2088}
2089
085ff963
MD
2090/*
2091 * Add an 11h Quiet time element to a frame.
2092 */
2093static uint8_t *
2094ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
2095{
2096 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2097
2098 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2099 quiet->len = 6;
2100 if (vap->iv_quiet_count_value == 1)
2101 vap->iv_quiet_count_value = vap->iv_quiet_count;
2102 else if (vap->iv_quiet_count_value > 1)
2103 vap->iv_quiet_count_value--;
2104
2105 if (vap->iv_quiet_count_value == 0) {
2106 /* value 0 is reserved as per 802.11h standerd */
2107 vap->iv_quiet_count_value = 1;
2108 }
2109
2110 quiet->tbttcount = vap->iv_quiet_count_value;
2111 quiet->period = vap->iv_quiet_period;
2112 quiet->duration = htole16(vap->iv_quiet_duration);
2113 quiet->offset = htole16(vap->iv_quiet_offset);
2114 return frm + sizeof(*quiet);
2115}
2116
32176cfd
RP
2117/*
2118 * Add an 11h Channel Switch Announcement element to a frame.
2119 * Note that we use the per-vap CSA count to adjust the global
2120 * counter so we can use this routine to form probe response
2121 * frames and get the current count.
2122 */
2123static uint8_t *
2124ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2125{
2126 struct ieee80211com *ic = vap->iv_ic;
2127 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2128
2129 csa->csa_ie = IEEE80211_ELEMID_CSA;
2130 csa->csa_len = 3;
2131 csa->csa_mode = 1; /* XXX force quiet on channel */
2132 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2133 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2134 return frm + sizeof(*csa);
2135}
2136
2137/*
2138 * Add an 11h country information element to a frame.
2139 */
2140static uint8_t *
2141ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2142{
2143
2144 if (ic->ic_countryie == NULL ||
2145 ic->ic_countryie_chan != ic->ic_bsschan) {
2146 /*
2147 * Handle lazy construction of ie. This is done on
2148 * first use and after a channel change that requires
2149 * re-calculation.
2150 */
2151 if (ic->ic_countryie != NULL)
4f655ef5 2152 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
32176cfd
RP
2153 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2154 if (ic->ic_countryie == NULL)
2155 return frm;
2156 ic->ic_countryie_chan = ic->ic_bsschan;
2157 }
2158 return add_appie(frm, ic->ic_countryie);
2159}
2160
085ff963
MD
2161uint8_t *
2162ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2163{
2164 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2165 return (add_ie(frm, vap->iv_wpa_ie));
2166 else {
2167 /* XXX else complain? */
2168 return (frm);
2169 }
2170}
2171
2172uint8_t *
2173ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2174{
2175 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2176 return (add_ie(frm, vap->iv_rsn_ie));
2177 else {
2178 /* XXX else complain? */
2179 return (frm);
2180 }
2181}
2182
2183uint8_t *
2184ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2185{
2186 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2187 *frm++ = IEEE80211_ELEMID_QOS;
2188 *frm++ = 1;
2189 *frm++ = 0;
2190 }
2191
2192 return (frm);
2193}
2194
841ab66c
SZ
2195/*
2196 * Send a probe request frame with the specified ssid
2197 * and any optional information element data.
2198 */
2199int
2200ieee80211_send_probereq(struct ieee80211_node *ni,
2201 const uint8_t sa[IEEE80211_ADDR_LEN],
2202 const uint8_t da[IEEE80211_ADDR_LEN],
2203 const uint8_t bssid[IEEE80211_ADDR_LEN],
32176cfd 2204 const uint8_t *ssid, size_t ssidlen)
841ab66c 2205{
32176cfd 2206 struct ieee80211vap *vap = ni->ni_vap;
841ab66c 2207 struct ieee80211com *ic = ni->ni_ic;
32176cfd
RP
2208 const struct ieee80211_txparam *tp;
2209 struct ieee80211_bpf_params params;
085ff963 2210 struct ieee80211_frame *wh;
32176cfd 2211 const struct ieee80211_rateset *rs;
f186073c 2212 struct mbuf *m;
841ab66c 2213 uint8_t *frm;
085ff963 2214 int ret;
f186073c 2215
32176cfd
RP
2216 if (vap->iv_state == IEEE80211_S_CAC) {
2217 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2218 "block %s frame in CAC state", "probe request");
2219 vap->iv_stats.is_tx_badstate++;
2220 return EIO; /* XXX */
2221 }
2222
841ab66c
SZ
2223 /*
2224 * Hold a reference on the node so it doesn't go away until after
2225 * the xmit is complete all the way in the driver. On error we
2226 * will remove our reference.
2227 */
32176cfd 2228 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1e290df3 2229 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
841ab66c 2230 __func__, __LINE__,
085ff963 2231 ni, ether_sprintf(ni->ni_macaddr),
32176cfd 2232 ieee80211_node_refcnt(ni)+1);
841ab66c
SZ
2233 ieee80211_ref_node(ni);
2234
2235 /*
2236 * prreq frame format
2237 * [tlv] ssid
2238 * [tlv] supported rates
32176cfd 2239 * [tlv] RSN (optional)
841ab66c 2240 * [tlv] extended supported rates
32176cfd 2241 * [tlv] WPA (optional)
841ab66c
SZ
2242 * [tlv] user-specified ie's
2243 */
2244 m = ieee80211_getmgtframe(&frm,
4ac84526 2245 ic->ic_headroom + sizeof(struct ieee80211_frame),
32176cfd 2246 2 + IEEE80211_NWID_LEN
841ab66c 2247 + 2 + IEEE80211_RATE_SIZE
32176cfd 2248 + sizeof(struct ieee80211_ie_wpa)
841ab66c 2249 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
32176cfd
RP
2250 + sizeof(struct ieee80211_ie_wpa)
2251 + (vap->iv_appie_probereq != NULL ?
2252 vap->iv_appie_probereq->ie_len : 0)
841ab66c
SZ
2253 );
2254 if (m == NULL) {
32176cfd 2255 vap->iv_stats.is_tx_nobuf++;
841ab66c
SZ
2256 ieee80211_free_node(ni);
2257 return ENOMEM;
2258 }
2259
2260 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
32176cfd
RP
2261 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2262 frm = ieee80211_add_rates(frm, rs);
085ff963 2263 frm = ieee80211_add_rsn(frm, vap);
32176cfd 2264 frm = ieee80211_add_xrates(frm, rs);
085ff963 2265 frm = ieee80211_add_wpa(frm, vap);
32176cfd
RP
2266 if (vap->iv_appie_probereq != NULL)
2267 frm = add_appie(frm, vap->iv_appie_probereq);
841ab66c
SZ
2268 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2269
32176cfd
RP
2270 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2271 ("leading space %zd", M_LEADINGSPACE(m)));
b5523eac 2272 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
32176cfd
RP
2273 if (m == NULL) {
2274 /* NB: cannot happen */
2275 ieee80211_free_node(ni);
841ab66c 2276 return ENOMEM;
32176cfd 2277 }
841ab66c 2278
085ff963
MD
2279 IEEE80211_TX_LOCK(ic);
2280 wh = mtod(m, struct ieee80211_frame *);
32176cfd
RP
2281 ieee80211_send_setup(ni, m,
2282 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2283 IEEE80211_NONQOS_TID, sa, da, bssid);
841ab66c 2284 /* XXX power management? */
32176cfd
RP
2285 m->m_flags |= M_ENCAP; /* mark encapsulated */
2286
2287 M_WME_SETAC(m, WME_AC_BE);
841ab66c
SZ
2288
2289 IEEE80211_NODE_STAT(ni, tx_probereq);
2290 IEEE80211_NODE_STAT(ni, tx_mgmt);
2291
32176cfd 2292 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1e290df3 2293 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
085ff963 2294 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
f92fae3f 2295 (int)ssidlen, ssid);
32176cfd
RP
2296
2297 memset(&params, 0, sizeof(params));
2298 params.ibp_pri = M_WME_GETAC(m);
2299 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2300 params.ibp_rate0 = tp->mgmtrate;
2301 if (IEEE80211_IS_MULTICAST(da)) {
2302 params.ibp_flags |= IEEE80211_BPF_NOACK;
2303 params.ibp_try0 = 1;
2304 } else
2305 params.ibp_try0 = tp->maxretry;
2306 params.ibp_power = ni->ni_txpower;
085ff963
MD
2307 ret = ieee80211_raw_output(vap, ni, m, &params);
2308 IEEE80211_TX_UNLOCK(ic);
2309 return (ret);
841ab66c
SZ
2310}
2311
2312/*
2313 * Calculate capability information for mgt frames.
2314 */
32176cfd
RP
2315uint16_t
2316ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
841ab66c 2317{
32176cfd 2318 struct ieee80211com *ic = vap->iv_ic;
841ab66c
SZ
2319 uint16_t capinfo;
2320
32176cfd 2321 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
841ab66c 2322
32176cfd 2323 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
841ab66c 2324 capinfo = IEEE80211_CAPINFO_ESS;
32176cfd 2325 else if (vap->iv_opmode == IEEE80211_M_IBSS)
841ab66c 2326 capinfo = IEEE80211_CAPINFO_IBSS;
f186073c 2327 else
841ab66c 2328 capinfo = 0;
32176cfd 2329 if (vap->iv_flags & IEEE80211_F_PRIVACY)
841ab66c 2330 capinfo |= IEEE80211_CAPINFO_PRIVACY;
32176cfd
RP
2331 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2332 IEEE80211_IS_CHAN_2GHZ(chan))
2333 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
841ab66c
SZ
2334 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2335 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
32176cfd
RP
2336 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2337 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
841ab66c 2338 return capinfo;
f186073c
JS
2339}
2340
2341/*
2342 * Send a management frame. The node is for the destination (or ic_bss
2343 * when in station mode). Nodes other than ic_bss have their reference
2344 * count bumped to reflect our use for an indeterminant time.
2345 */
2346int
32176cfd 2347ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
f186073c 2348{
32176cfd
RP
2349#define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2350#define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2351 struct ieee80211vap *vap = ni->ni_vap;
2352 struct ieee80211com *ic = ni->ni_ic;
2353 struct ieee80211_node *bss = vap->iv_bss;
2354 struct ieee80211_bpf_params params;
f186073c
JS
2355 struct mbuf *m;
2356 uint8_t *frm;
f186073c 2357 uint16_t capinfo;
32176cfd 2358 int has_challenge, is_shared_key, ret, status;
f186073c
JS
2359
2360 KASSERT(ni != NULL, ("null node"));
2361
2362 /*
2363 * Hold a reference on the node so it doesn't go away until after
2364 * the xmit is complete all the way in the driver. On error we
2365 * will remove our reference.
2366 */
32176cfd 2367 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1e290df3 2368 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
841ab66c 2369 __func__, __LINE__,
085ff963 2370 ni, ether_sprintf(ni->ni_macaddr),
32176cfd 2371 ieee80211_node_refcnt(ni)+1);
841ab66c
SZ
2372 ieee80211_ref_node(ni);
2373
32176cfd 2374 memset(&params, 0, sizeof(params));
f186073c 2375 switch (type) {
f186073c
JS
2376
2377 case IEEE80211_FC0_SUBTYPE_AUTH:
841ab66c
SZ
2378 status = arg >> 16;
2379 arg &= 0xffff;
2380 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2381 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2382 ni->ni_challenge != NULL);
2383
2384 /*
2385 * Deduce whether we're doing open authentication or
2386 * shared key authentication. We do the latter if
2387 * we're in the middle of a shared key authentication
2388 * handshake or if we're initiating an authentication
2389 * request and configured to use shared key.
2390 */
2391 is_shared_key = has_challenge ||
2392 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2393 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
32176cfd 2394 bss->ni_authmode == IEEE80211_AUTH_SHARED);
841ab66c
SZ
2395
2396 m = ieee80211_getmgtframe(&frm,
4ac84526 2397 ic->ic_headroom + sizeof(struct ieee80211_frame),
841ab66c
SZ
2398 3 * sizeof(uint16_t)
2399 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2400 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2401 );
f186073c 2402 if (m == NULL)
841ab66c
SZ
2403 senderr(ENOMEM, is_tx_nobuf);
2404
2405 ((uint16_t *)frm)[0] =
2406 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2407 : htole16(IEEE80211_AUTH_ALG_OPEN);
f186073c 2408 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
841ab66c
SZ
2409 ((uint16_t *)frm)[2] = htole16(status);/* status */
2410
2411 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2412 ((uint16_t *)frm)[3] =
2413 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2414 IEEE80211_ELEMID_CHALLENGE);
2415 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2416 IEEE80211_CHALLENGE_LEN);
2417 m->m_pkthdr.len = m->m_len =
2418 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2419 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
32176cfd
RP
2420 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2421 "request encrypt frame (%s)", __func__);
2422 /* mark frame for encryption */
2423 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
841ab66c
SZ
2424 }
2425 } else
2426 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2427
2428 /* XXX not right for shared key */
2429 if (status == IEEE80211_STATUS_SUCCESS)
2430 IEEE80211_NODE_STAT(ni, tx_auth);
2431 else
2432 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2433
32176cfd
RP
2434 if (vap->iv_opmode == IEEE80211_M_STA)
2435 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2436 (void *) vap->iv_state);
f186073c
JS
2437 break;
2438
2439 case IEEE80211_FC0_SUBTYPE_DEAUTH:
32176cfd 2440 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
4f655ef5
MD
2441 "send station deauthenticate (reason: %d (%s))", arg,
2442 ieee80211_reason_to_string(arg));
4ac84526
SZ
2443 m = ieee80211_getmgtframe(&frm,
2444 ic->ic_headroom + sizeof(struct ieee80211_frame),
2445 sizeof(uint16_t));
f186073c 2446 if (m == NULL)
841ab66c
SZ
2447 senderr(ENOMEM, is_tx_nobuf);
2448 *(uint16_t *)frm = htole16(arg); /* reason */
2449 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2450
2451 IEEE80211_NODE_STAT(ni, tx_deauth);
2452 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2453
2454 ieee80211_node_unauthorize(ni); /* port closed */
f186073c
JS
2455 break;
2456
2457 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
208a1285 2458 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
f186073c
JS
2459 /*
2460 * asreq frame format
2461 * [2] capability information
2462 * [2] listen interval
2463 * [6*] current AP address (reassoc only)
2464 * [tlv] ssid
2465 * [tlv] supported rates
2466 * [tlv] extended supported rates
32176cfd
RP
2467 * [4] power capability (optional)
2468 * [28] supported channels (optional)
2469 * [tlv] HT capabilities
2470 * [tlv] WME (optional)
2471 * [tlv] Vendor OUI HT capabilities (optional)
2472 * [tlv] Atheros capabilities (if negotiated)
2473 * [tlv] AppIE's (optional)
f186073c 2474 */
841ab66c 2475 m = ieee80211_getmgtframe(&frm,
4ac84526 2476 ic->ic_headroom + sizeof(struct ieee80211_frame),
841ab66c 2477 sizeof(uint16_t)
f186073c
JS
2478 + sizeof(uint16_t)
2479 + IEEE80211_ADDR_LEN
841ab66c 2480 + 2 + IEEE80211_NWID_LEN
f186073c 2481 + 2 + IEEE80211_RATE_SIZE
841ab66c 2482 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
32176cfd
RP
2483 + 4
2484 + 2 + 26
841ab66c 2485 + sizeof(struct ieee80211_wme_info)
32176cfd
RP
2486 + sizeof(struct ieee80211_ie_htcap)
2487 + 4 + sizeof(struct ieee80211_ie_htcap)
2488#ifdef IEEE80211_SUPPORT_SUPERG
2489 + sizeof(struct ieee80211_ath_ie)
2490#endif
2491 + (vap->iv_appie_wpa != NULL ?
2492 vap->iv_appie_wpa->ie_len : 0)
2493 + (vap->iv_appie_assocreq != NULL ?
2494 vap->iv_appie_assocreq->ie_len : 0)
841ab66c 2495 );
f186073c 2496 if (m == NULL)
841ab66c 2497 senderr(ENOMEM, is_tx_nobuf);
f186073c 2498
32176cfd
RP
2499 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2500 ("wrong mode %u", vap->iv_opmode));
841ab66c 2501 capinfo = IEEE80211_CAPINFO_ESS;
32176cfd 2502 if (vap->iv_flags & IEEE80211_F_PRIVACY)
f186073c
JS
2503 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2504 /*
2505 * NB: Some 11a AP's reject the request when
32176cfd 2506 * short premable is set.
f186073c 2507 */
32176cfd
RP
2508 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2509 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2510 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2511 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
841ab66c 2512 (ic->ic_caps & IEEE80211_C_SHSLOT))
f186073c 2513 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
32176cfd
RP
2514 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2515 (vap->iv_flags & IEEE80211_F_DOTH))
2516 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
f186073c
JS
2517 *(uint16_t *)frm = htole16(capinfo);
2518 frm += 2;
2519
32176cfd 2520 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
22b21df8 2521 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
32176cfd 2522 bss->ni_intval));
f186073c
JS
2523 frm += 2;
2524
2525 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
32176cfd 2526 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
f186073c
JS
2527 frm += IEEE80211_ADDR_LEN;
2528 }
2529
2530 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
32176cfd 2531 frm = ieee80211_add_rates(frm, &ni->ni_rates);
085ff963 2532 frm = ieee80211_add_rsn(frm, vap);
32176cfd
RP
2533 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2534 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2535 frm = ieee80211_add_powercapability(frm,
2536 ic->ic_curchan);
2537 frm = ieee80211_add_supportedchannels(frm, ic);
2538 }
4f898719
IV
2539
2540 /*
2541 * Check the channel - we may be using an 11n NIC with an
2542 * 11n capable station, but we're configured to be an 11b
2543 * channel.
2544 */
32176cfd 2545 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
4f898719 2546 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
32176cfd 2547 ni->ni_ies.htcap_ie != NULL &&
4f898719 2548 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
32176cfd 2549 frm = ieee80211_add_htcap(frm, ni);
4f898719 2550 }
085ff963 2551 frm = ieee80211_add_wpa(frm, vap);
32176cfd
RP
2552 if ((ic->ic_flags & IEEE80211_F_WME) &&
2553 ni->ni_ies.wme_ie != NULL)
841ab66c 2554 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
4f898719
IV
2555
2556 /*
2557 * Same deal - only send HT info if we're on an 11n
2558 * capable channel.
2559 */
32176cfd 2560 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
4f898719 2561 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
32176cfd 2562 ni->ni_ies.htcap_ie != NULL &&
4f898719 2563 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
32176cfd 2564 frm = ieee80211_add_htcap_vendor(frm, ni);
4f898719 2565 }
32176cfd
RP
2566#ifdef IEEE80211_SUPPORT_SUPERG
2567 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2568 frm = ieee80211_add_ath(frm,
2569 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2570 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2571 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2572 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
841ab66c 2573 }
32176cfd
RP
2574#endif /* IEEE80211_SUPPORT_SUPERG */
2575 if (vap->iv_appie_assocreq != NULL)
2576 frm = add_appie(frm, vap->iv_appie_assocreq);
f186073c
JS
2577 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2578
32176cfd
RP
2579 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2580 (void *) vap->iv_state);
f186073c
JS
2581 break;
2582
2583 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2584 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2585 /*
32176cfd 2586 * asresp frame format
f186073c
JS
2587 * [2] capability information
2588 * [2] status
2589 * [2] association ID
2590 * [tlv] supported rates
2591 * [tlv] extended supported rates
32176cfd
RP
2592 * [tlv] HT capabilities (standard, if STA enabled)
2593 * [tlv] HT information (standard, if STA enabled)
2594 * [tlv] WME (if configured and STA enabled)
2595 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2596 * [tlv] HT information (vendor OUI, if STA enabled)
2597 * [tlv] Atheros capabilities (if STA enabled)
2598 * [tlv] AppIE's (optional)
f186073c 2599 */
841ab66c 2600 m = ieee80211_getmgtframe(&frm,
4ac84526 2601 ic->ic_headroom + sizeof(struct ieee80211_frame),
841ab66c 2602 sizeof(uint16_t)
f186073c
JS
2603 + sizeof(uint16_t)
2604 + sizeof(uint16_t)
2605 + 2 + IEEE80211_RATE_SIZE
841ab66c 2606 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
32176cfd
RP
2607 + sizeof(struct ieee80211_ie_htcap) + 4
2608 + sizeof(struct ieee80211_ie_htinfo) + 4
841ab66c 2609 + sizeof(struct ieee80211_wme_param)
32176cfd
RP
2610#ifdef IEEE80211_SUPPORT_SUPERG
2611 + sizeof(struct ieee80211_ath_ie)
2612#endif
2613 + (vap->iv_appie_assocresp != NULL ?
2614 vap->iv_appie_assocresp->ie_len : 0)
841ab66c 2615 );
f186073c 2616 if (m == NULL)
841ab66c 2617 senderr(ENOMEM, is_tx_nobuf);
f186073c 2618
32176cfd 2619 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
f186073c
JS
2620 *(uint16_t *)frm = htole16(capinfo);
2621 frm += 2;
2622
2623 *(uint16_t *)frm = htole16(arg); /* status */
2624 frm += 2;
2625
841ab66c 2626 if (arg == IEEE80211_STATUS_SUCCESS) {
f186073c 2627 *(uint16_t *)frm = htole16(ni->ni_associd);
841ab66c
SZ
2628 IEEE80211_NODE_STAT(ni, tx_assoc);
2629 } else
2630 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
f186073c
JS
2631 frm += 2;
2632
32176cfd
RP
2633 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2634 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2635 /* NB: respond according to what we received */
2636 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2637 frm = ieee80211_add_htcap(frm, ni);
2638 frm = ieee80211_add_htinfo(frm, ni);
2639 }
2640 if ((vap->iv_flags & IEEE80211_F_WME) &&
2641 ni->ni_ies.wme_ie != NULL)
841ab66c 2642 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
32176cfd
RP
2643 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2644 frm = ieee80211_add_htcap_vendor(frm, ni);
2645 frm = ieee80211_add_htinfo_vendor(frm, ni);
2646 }
2647#ifdef IEEE80211_SUPPORT_SUPERG
2648 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2649 frm = ieee80211_add_ath(frm,
2650 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2651 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2652 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2653 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2654#endif /* IEEE80211_SUPPORT_SUPERG */
2655 if (vap->iv_appie_assocresp != NULL)
2656 frm = add_appie(frm, vap->iv_appie_assocresp);
f186073c
JS
2657 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2658 break;
2659
2660 case IEEE80211_FC0_SUBTYPE_DISASSOC:
32176cfd 2661 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
4f655ef5
MD
2662 "send station disassociate (reason: %d (%s))", arg,
2663 ieee80211_reason_to_string(arg));
4ac84526
SZ
2664 m = ieee80211_getmgtframe(&frm,
2665 ic->ic_headroom + sizeof(struct ieee80211_frame),
2666 sizeof(uint16_t));
f186073c 2667 if (m == NULL)
841ab66c
SZ
2668 senderr(ENOMEM, is_tx_nobuf);
2669 *(uint16_t *)frm = htole16(arg); /* reason */
2670 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2671
2672 IEEE80211_NODE_STAT(ni, tx_disassoc);
2673 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
f186073c
JS
2674 break;
2675
2676 default:
32176cfd
RP
2677 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2678 "invalid mgmt frame type %u", type);
f186073c
JS
2679 senderr(EINVAL, is_tx_unknownmgt);
2680 /* NOTREACHED */
2681 }
32176cfd
RP
2682
2683 /* NB: force non-ProbeResp frames to the highest queue */
2684 params.ibp_pri = WME_AC_VO;
2685 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2686 /* NB: we know all frames are unicast */
2687 params.ibp_try0 = bss->ni_txparms->maxretry;
2688 params.ibp_power = bss->ni_txpower;
2689 return ieee80211_mgmt_output(ni, m, type, &params);
f186073c 2690bad:
32176cfd 2691 ieee80211_free_node(ni);
f186073c
JS
2692 return ret;
2693#undef senderr
32176cfd 2694#undef HTFLAGS
f186073c 2695}
841ab66c 2696
3da93495 2697/*
32176cfd
RP
2698 * Return an mbuf with a probe response frame in it.
2699 * Space is left to prepend and 802.11 header at the
2700 * front but it's left to the caller to fill in.
3da93495
SZ
2701 */
2702struct mbuf *
32176cfd 2703ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3da93495 2704{
32176cfd
RP
2705 struct ieee80211vap *vap = bss->ni_vap;
2706 struct ieee80211com *ic = bss->ni_ic;
2707 const struct ieee80211_rateset *rs;
3da93495 2708 struct mbuf *m;
32176cfd
RP
2709 uint16_t capinfo;
2710 uint8_t *frm;
3da93495 2711
32176cfd
RP
2712 /*
2713 * probe response frame format
2714 * [8] time stamp
2715 * [2] beacon interval
2716 * [2] cabability information
2717 * [tlv] ssid
2718 * [tlv] supported rates
2719 * [tlv] parameter set (FH/DS)
2720 * [tlv] parameter set (IBSS)
2721 * [tlv] country (optional)
2722 * [3] power control (optional)
2723 * [5] channel switch announcement (CSA) (optional)
2724 * [tlv] extended rate phy (ERP)
2725 * [tlv] extended supported rates
2726 * [tlv] RSN (optional)
2727 * [tlv] HT capabilities
2728 * [tlv] HT information
2729 * [tlv] WPA (optional)
2730 * [tlv] WME (optional)
2731 * [tlv] Vendor OUI HT capabilities (optional)
2732 * [tlv] Vendor OUI HT information (optional)
2733 * [tlv] Atheros capabilities
2734 * [tlv] AppIE's (optional)
2735 * [tlv] Mesh ID (MBSS)
2736 * [tlv] Mesh Conf (MBSS)
2737 */
2738 m = ieee80211_getmgtframe(&frm,
2739 ic->ic_headroom + sizeof(struct ieee80211_frame),
2740 8
2741 + sizeof(uint16_t)
2742 + sizeof(uint16_t)
2743 + 2 + IEEE80211_NWID_LEN
2744 + 2 + IEEE80211_RATE_SIZE
2745 + 7 /* max(7,3) */
2746 + IEEE80211_COUNTRY_MAX_SIZE
2747 + 3
2748 + sizeof(struct ieee80211_csa_ie)
085ff963 2749 + sizeof(struct ieee80211_quiet_ie)
32176cfd
RP
2750 + 3
2751 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2752 + sizeof(struct ieee80211_ie_wpa)
2753 + sizeof(struct ieee80211_ie_htcap)
2754 + sizeof(struct ieee80211_ie_htinfo)
2755 + sizeof(struct ieee80211_ie_wpa)
2756 + sizeof(struct ieee80211_wme_param)
2757 + 4 + sizeof(struct ieee80211_ie_htcap)
2758 + 4 + sizeof(struct ieee80211_ie_htinfo)
2759#ifdef IEEE80211_SUPPORT_SUPERG
2760 + sizeof(struct ieee80211_ath_ie)
2761#endif
2762#ifdef IEEE80211_SUPPORT_MESH
2763 + 2 + IEEE80211_MESHID_LEN
2764 + sizeof(struct ieee80211_meshconf_ie)
2765#endif
2766 + (vap->iv_appie_proberesp != NULL ?
2767 vap->iv_appie_proberesp->ie_len : 0)
2768 );
2769 if (m == NULL) {
2770 vap->iv_stats.is_tx_nobuf++;
3da93495 2771 return NULL;
32176cfd
RP
2772 }
2773
2774 memset(frm, 0, 8); /* timestamp should be filled later */
2775 frm += 8;
2776 *(uint16_t *)frm = htole16(bss->ni_intval);
2777 frm += 2;
2778 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2779 *(uint16_t *)frm = htole16(capinfo);
2780 frm += 2;
2781
2782 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2783 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2784 frm = ieee80211_add_rates(frm, rs);
2785
2786 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2787 *frm++ = IEEE80211_ELEMID_FHPARMS;
2788 *frm++ = 5;
2789 *frm++ = bss->ni_fhdwell & 0x00ff;
2790 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2791 *frm++ = IEEE80211_FH_CHANSET(
2792 ieee80211_chan2ieee(ic, bss->ni_chan));
2793 *frm++ = IEEE80211_FH_CHANPAT(
2794 ieee80211_chan2ieee(ic, bss->ni_chan));
2795 *frm++ = bss->ni_fhindex;
2796 } else {
2797 *frm++ = IEEE80211_ELEMID_DSPARMS;
2798 *frm++ = 1;
2799 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2800 }
2801
2802 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2803 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2804 *frm++ = 2;
2805 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2806 }
2807 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2808 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2809 frm = ieee80211_add_countryie(frm, ic);
2810 if (vap->iv_flags & IEEE80211_F_DOTH) {
2811 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2812 frm = ieee80211_add_powerconstraint(frm, vap);
2813 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2814 frm = ieee80211_add_csa(frm, vap);
2815 }
085ff963
MD
2816 if (vap->iv_flags & IEEE80211_F_DOTH) {
2817 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2818 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2819 if (vap->iv_quiet)
2820 frm = ieee80211_add_quiet(frm, vap);
2821 }
2822 }
32176cfd
RP
2823 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2824 frm = ieee80211_add_erp(frm, ic);
2825 frm = ieee80211_add_xrates(frm, rs);
085ff963 2826 frm = ieee80211_add_rsn(frm, vap);
32176cfd
RP
2827 /*
2828 * NB: legacy 11b clients do not get certain ie's.
2829 * The caller identifies such clients by passing
2830 * a token in legacy to us. Could expand this to be
2831 * any legacy client for stuff like HT ie's.
2832 */
2833 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2834 legacy != IEEE80211_SEND_LEGACY_11B) {
2835 frm = ieee80211_add_htcap(frm, bss);
2836 frm = ieee80211_add_htinfo(frm, bss);
2837 }
085ff963 2838 frm = ieee80211_add_wpa(frm, vap);
32176cfd
RP
2839 if (vap->iv_flags & IEEE80211_F_WME)
2840 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2841 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2842 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2843 legacy != IEEE80211_SEND_LEGACY_11B) {
2844 frm = ieee80211_add_htcap_vendor(frm, bss);
2845 frm = ieee80211_add_htinfo_vendor(frm, bss);
2846 }
2847#ifdef IEEE80211_SUPPORT_SUPERG
2848 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2849 legacy != IEEE80211_SEND_LEGACY_11B)
2850 frm = ieee80211_add_athcaps(frm, bss);
2851#endif
2852 if (vap->iv_appie_proberesp != NULL)
2853 frm = add_appie(frm, vap->iv_appie_proberesp);
2854#ifdef IEEE80211_SUPPORT_MESH
2855 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2856 frm = ieee80211_add_meshid(frm, vap);
2857 frm = ieee80211_add_meshconf(frm, vap);
2858 }
2859#endif
2860 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2861
2862 return m;
2863}
2864
2865/*
2866 * Send a probe response frame to the specified mac address.
2867 * This does not go through the normal mgt frame api so we
2868 * can specify the destination address and re-use the bss node
2869 * for the sta reference.
2870 */
2871int
2872ieee80211_send_proberesp(struct ieee80211vap *vap,
2873 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2874{
2875 struct ieee80211_node *bss = vap->iv_bss;
2876 struct ieee80211com *ic = vap->iv_ic;
085ff963 2877 struct ieee80211_frame *wh;
32176cfd 2878 struct mbuf *m;
085ff963 2879 int ret;
32176cfd
RP
2880
2881 if (vap->iv_state == IEEE80211_S_CAC) {
2882 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2883 "block %s frame in CAC state", "probe response");
2884 vap->iv_stats.is_tx_badstate++;
2885 return EIO; /* XXX */
2886 }
2887
2888 /*
2889 * Hold a reference on the node so it doesn't go away until after
2890 * the xmit is complete all the way in the driver. On error we
2891 * will remove our reference.
2892 */
2893 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1e290df3 2894 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
085ff963 2895 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
32176cfd
RP
2896 ieee80211_node_refcnt(bss)+1);
2897 ieee80211_ref_node(bss);
2898
2899 m = ieee80211_alloc_proberesp(bss, legacy);
2900 if (m == NULL) {
2901 ieee80211_free_node(bss);
2902 return ENOMEM;
2903 }
3da93495 2904
b5523eac 2905 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
32176cfd 2906 KASSERT(m != NULL, ("no room for header"));
3da93495 2907
085ff963
MD
2908 IEEE80211_TX_LOCK(ic);
2909 wh = mtod(m, struct ieee80211_frame *);
32176cfd
RP
2910 ieee80211_send_setup(bss, m,
2911 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2912 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2913 /* XXX power management? */
2914 m->m_flags |= M_ENCAP; /* mark encapsulated */
2915
2916 M_WME_SETAC(m, WME_AC_BE);
2917
2918 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1e290df3 2919 "send probe resp on channel %u to %s%s\n",
085ff963 2920 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
32176cfd
RP
2921 legacy ? " <legacy>" : "");
2922 IEEE80211_NODE_STAT(bss, tx_mgmt);
2923
085ff963
MD
2924 ret = ieee80211_raw_output(vap, bss, m, NULL);
2925 IEEE80211_TX_UNLOCK(ic);
2926 return (ret);
32176cfd 2927}
3da93495 2928
32176cfd
RP
2929/*
2930 * Allocate and build a RTS (Request To Send) control frame.
2931 */
2932struct mbuf *
2933ieee80211_alloc_rts(struct ieee80211com *ic,
2934 const uint8_t ra[IEEE80211_ADDR_LEN],
2935 const uint8_t ta[IEEE80211_ADDR_LEN],
2936 uint16_t dur)
2937{
2938 struct ieee80211_frame_rts *rts;
2939 struct mbuf *m;
2940
2941 /* XXX honor ic_headroom */
b5523eac 2942 m = m_gethdr(M_NOWAIT, MT_DATA);
32176cfd
RP
2943 if (m != NULL) {
2944 rts = mtod(m, struct ieee80211_frame_rts *);
2945 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2946 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2947 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2948 *(u_int16_t *)rts->i_dur = htole16(dur);
2949 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2950 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2951
2952 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2953 }
3da93495
SZ
2954 return m;
2955}
2956
841ab66c 2957/*
32176cfd 2958 * Allocate and build a CTS (Clear To Send) control frame.
841ab66c
SZ
2959 */
2960struct mbuf *
32176cfd
RP
2961ieee80211_alloc_cts(struct ieee80211com *ic,
2962 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
841ab66c 2963{
32176cfd 2964 struct ieee80211_frame_cts *cts;
841ab66c 2965 struct mbuf *m;
32176cfd
RP
2966
2967 /* XXX honor ic_headroom */
b5523eac 2968 m = m_gethdr(M_NOWAIT, MT_DATA);
32176cfd
RP
2969 if (m != NULL) {
2970 cts = mtod(m, struct ieee80211_frame_cts *);
2971 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2972 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2973 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2974 *(u_int16_t *)cts->i_dur = htole16(dur);
2975 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2976
2977 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2978 }
2979 return m;
2980}
2981
2982static void
085ff963 2983ieee80211_tx_mgt_timeout(void *arg)
32176cfd 2984{
085ff963 2985 struct ieee80211vap *vap = arg;
32176cfd 2986
085ff963 2987 IEEE80211_LOCK(vap->iv_ic);
32176cfd
RP
2988 if (vap->iv_state != IEEE80211_S_INIT &&
2989 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2990 /*
2991 * NB: it's safe to specify a timeout as the reason here;
2992 * it'll only be used in the right state.
2993 */
085ff963 2994 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
32176cfd
RP
2995 IEEE80211_SCAN_FAIL_TIMEOUT);
2996 }
085ff963 2997 IEEE80211_UNLOCK(vap->iv_ic);
32176cfd
RP
2998}
2999
085ff963
MD
3000/*
3001 * This is the callback set on net80211-sourced transmitted
3002 * authentication request frames.
3003 *
3004 * This does a couple of things:
3005 *
3006 * + If the frame transmitted was a success, it schedules a future
3007 * event which will transition the interface to scan.
3008 * If a state transition _then_ occurs before that event occurs,
3009 * said state transition will cancel this callout.
3010 *
3011 * + If the frame transmit was a failure, it immediately schedules
3012 * the transition back to scan.
3013 */
32176cfd
RP
3014static void
3015ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3016{
3017 struct ieee80211vap *vap = ni->ni_vap;
3018 enum ieee80211_state ostate = (enum ieee80211_state) arg;
3019
3020 /*
3021 * Frame transmit completed; arrange timer callback. If
4f655ef5 3022 * transmit was successfully we wait for response. Otherwise
32176cfd
RP
3023 * we arrange an immediate callback instead of doing the
3024 * callback directly since we don't know what state the driver
3025 * is in (e.g. what locks it is holding). This work should
3026 * not be too time-critical and not happen too often so the
3027 * added overhead is acceptable.
3028 *
3029 * XXX what happens if !acked but response shows up before callback?
3030 */
085ff963 3031 if (vap->iv_state == ostate) {
32176cfd
RP
3032 callout_reset(&vap->iv_mgtsend,
3033 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
085ff963
MD
3034 ieee80211_tx_mgt_timeout, vap);
3035 }
32176cfd
RP
3036}
3037
3038static void
3039ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
4f655ef5 3040 struct ieee80211_node *ni)
32176cfd
RP
3041{
3042 struct ieee80211vap *vap = ni->ni_vap;
4f655ef5 3043 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
32176cfd
RP
3044 struct ieee80211com *ic = ni->ni_ic;
3045 struct ieee80211_rateset *rs = &ni->ni_rates;
841ab66c 3046 uint16_t capinfo;
841ab66c
SZ
3047
3048 /*
3049 * beacon frame format
3050 * [8] time stamp
3051 * [2] beacon interval
3052 * [2] cabability information
3053 * [tlv] ssid
3054 * [tlv] supported rates
3055 * [3] parameter set (DS)
32176cfd 3056 * [8] CF parameter set (optional)
841ab66c 3057 * [tlv] parameter set (IBSS/TIM)
32176cfd
RP
3058 * [tlv] country (optional)
3059 * [3] power control (optional)
3060 * [5] channel switch announcement (CSA) (optional)
841ab66c
SZ
3061 * [tlv] extended rate phy (ERP)
3062 * [tlv] extended supported rates
32176cfd
RP
3063 * [tlv] RSN parameters
3064 * [tlv] HT capabilities
3065 * [tlv] HT information
841ab66c 3066 * XXX Vendor-specific OIDs (e.g. Atheros)
32176cfd
RP
3067 * [tlv] WPA parameters
3068 * [tlv] WME parameters
3069 * [tlv] Vendor OUI HT capabilities (optional)
3070 * [tlv] Vendor OUI HT information (optional)
3071 * [tlv] Atheros capabilities (optional)
3072 * [tlv] TDMA parameters (optional)
3073 * [tlv] Mesh ID (MBSS)
3074 * [tlv] Mesh Conf (MBSS)
3075 * [tlv] application data (optional)
841ab66c 3076 */
32176cfd
RP
3077
3078 memset(bo, 0, sizeof(*bo));
841ab66c
SZ
3079
3080 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3081 frm += 8;
3082 *(uint16_t *)frm = htole16(ni->ni_intval);
3083 frm += 2;
32176cfd 3084 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
841ab66c
SZ
3085 bo->bo_caps = (uint16_t *)frm;
3086 *(uint16_t *)frm = htole16(capinfo);
3087 frm += 2;
3088 *frm++ = IEEE80211_ELEMID_SSID;
32176cfd 3089 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
841ab66c
SZ
3090 *frm++ = ni->ni_esslen;
3091 memcpy(frm, ni->ni_essid, ni->ni_esslen);
3092 frm += ni->ni_esslen;
3093 } else
3094 *frm++ = 0;
3095 frm = ieee80211_add_rates(frm, rs);
32176cfd 3096 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
841ab66c
SZ
3097 *frm++ = IEEE80211_ELEMID_DSPARMS;
3098 *frm++ = 1;
3099 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3100 }
32176cfd
RP
3101 if (ic->ic_flags & IEEE80211_F_PCF) {
3102 bo->bo_cfp = frm;
3103 frm = ieee80211_add_cfparms(frm, ic);
3104 }
841ab66c 3105 bo->bo_tim = frm;
32176cfd 3106 if (vap->iv_opmode == IEEE80211_M_IBSS) {
841ab66c
SZ
3107 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3108 *frm++ = 2;
3109 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3110 bo->bo_tim_len = 0;
32176cfd
RP
3111 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3112 vap->iv_opmode == IEEE80211_M_MBSS) {
3113 /* TIM IE is the same for Mesh and Hostap */
841ab66c
SZ
3114 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3115
3116 tie->tim_ie = IEEE80211_ELEMID_TIM;
3117 tie->tim_len = 4; /* length */
3118 tie->tim_count = 0; /* DTIM count */
32176cfd 3119 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
841ab66c
SZ
3120 tie->tim_bitctl = 0; /* bitmap control */
3121 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3122 frm += sizeof(struct ieee80211_tim_ie);
3123 bo->bo_tim_len = 1;
3124 }
32176cfd
RP
3125 bo->bo_tim_trailer = frm;
3126 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3127 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3128 frm = ieee80211_add_countryie(frm, ic);
3129 if (vap->iv_flags & IEEE80211_F_DOTH) {
3130 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3131 frm = ieee80211_add_powerconstraint(frm, vap);
3132 bo->bo_csa = frm;
3133 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3134 frm = ieee80211_add_csa(frm, vap);
3135 } else
3136 bo->bo_csa = frm;
085ff963
MD
3137
3138 if (vap->iv_flags & IEEE80211_F_DOTH) {
3139 bo->bo_quiet = frm;
3140 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3141 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3142 if (vap->iv_quiet)
3143 frm = ieee80211_add_quiet(frm,vap);
3144 }
3145 } else
3146 bo->bo_quiet = frm;
3147
32176cfd
RP
3148 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3149 bo->bo_erp = frm;
3150 frm = ieee80211_add_erp(frm, ic);
3151 }
3152 frm = ieee80211_add_xrates(frm, rs);
085ff963 3153 frm = ieee80211_add_rsn(frm, vap);
32176cfd
RP
3154 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3155 frm = ieee80211_add_htcap(frm, ni);
3156 bo->bo_htinfo = frm;
3157 frm = ieee80211_add_htinfo(frm, ni);
3158 }
085ff963 3159 frm = ieee80211_add_wpa(frm, vap);
32176cfd 3160 if (vap->iv_flags & IEEE80211_F_WME) {
841ab66c
SZ
3161 bo->bo_wme = frm;
3162 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
841ab66c 3163 }
32176cfd
RP
3164 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3165 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3166 frm = ieee80211_add_htcap_vendor(frm, ni);
3167 frm = ieee80211_add_htinfo_vendor(frm, ni);
3168 }
3169#ifdef IEEE80211_SUPPORT_SUPERG
3170 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3171 bo->bo_ath = frm;
3172 frm = ieee80211_add_athcaps(frm, ni);
3173 }
3174#endif
3175#ifdef IEEE80211_SUPPORT_TDMA
3176 if (vap->iv_caps & IEEE80211_C_TDMA) {
3177 bo->bo_tdma = frm;
3178 frm = ieee80211_add_tdma(frm, vap);
3179 }
3180#endif
3181 if (vap->iv_appie_beacon != NULL) {
3182 bo->bo_appie = frm;
3183 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3184 frm = add_appie(frm, vap->iv_appie_beacon);
3185 }
3186#ifdef IEEE80211_SUPPORT_MESH
3187 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3188 frm = ieee80211_add_meshid(frm, vap);
3189 bo->bo_meshconf = frm;
3190 frm = ieee80211_add_meshconf(frm, vap);
3191 }
3192#endif
3193 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3194 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3195 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3196}
3197
3198/*
3199 * Allocate a beacon frame and fillin the appropriate bits.
3200 */
3201struct mbuf *
4f655ef5 3202ieee80211_beacon_alloc(struct ieee80211_node *ni)
32176cfd
RP
3203{
3204 struct ieee80211vap *vap = ni->ni_vap;
3205 struct ieee80211com *ic = ni->ni_ic;
3206 struct ifnet *ifp = vap->iv_ifp;
3207 struct ieee80211_frame *wh;
3208 struct mbuf *m;
3209 int pktlen;
3210 uint8_t *frm;
3211
3212 /*
3213 * beacon frame format
3214 * [8] time stamp
3215 * [2] beacon interval
3216 * [2] cabability information
3217 * [tlv] ssid
3218 * [tlv] supported rates
3219 * [3] parameter set (DS)
3220 * [8] CF parameter set (optional)
3221 * [tlv] parameter set (IBSS/TIM)
3222 * [tlv] country (optional)
3223 * [3] power control (optional)
3224 * [5] channel switch announcement (CSA) (optional)
3225 * [tlv] extended rate phy (ERP)
3226 * [tlv] extended supported rates
3227 * [tlv] RSN parameters
3228 * [tlv] HT capabilities
3229 * [tlv] HT information
3230 * [tlv] Vendor OUI HT capabilities (optional)
3231 * [tlv] Vendor OUI HT information (optional)
3232 * XXX Vendor-specific OIDs (e.g. Atheros)
3233 * [tlv] WPA parameters
3234 * [tlv] WME parameters
3235 * [tlv] TDMA parameters (optional)
3236 * [tlv] Mesh ID (MBSS)
3237 * [tlv] Mesh Conf (MBSS)
3238 * [tlv] application data (optional)
3239 * NB: we allocate the max space required for the TIM bitmap.
3240 * XXX how big is this?
3241 */
3242 pktlen = 8 /* time stamp */
3243 + sizeof(uint16_t) /* beacon interval */
3244 + sizeof(uint16_t) /* capabilities */
3245 + 2 + ni->ni_esslen /* ssid */
3246 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3247 + 2 + 1 /* DS parameters */
3248 + 2 + 6 /* CF parameters */
3249 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3250 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3251 + 2 + 1 /* power control */
085ff963
MD
3252 + sizeof(struct ieee80211_csa_ie) /* CSA */
3253 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
32176cfd
RP
3254 + 2 + 1 /* ERP */
3255 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3256 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3257 2*sizeof(struct ieee80211_ie_wpa) : 0)
3258 /* XXX conditional? */
3259 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3260 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3261 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3262 sizeof(struct ieee80211_wme_param) : 0)
3263#ifdef IEEE80211_SUPPORT_SUPERG
3264 + sizeof(struct ieee80211_ath_ie) /* ATH */
3265#endif
3266#ifdef IEEE80211_SUPPORT_TDMA
3267 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3268 sizeof(struct ieee80211_tdma_param) : 0)
3269#endif
3270#ifdef IEEE80211_SUPPORT_MESH
3271 + 2 + ni->ni_meshidlen
3272 + sizeof(struct ieee80211_meshconf_ie)
3273#endif
3274 + IEEE80211_MAX_APPIE
3275 ;
3276 m = ieee80211_getmgtframe(&frm,
3277 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3278 if (m == NULL) {
3279 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3280 "%s: cannot get buf; size %u\n", __func__, pktlen);
3281 vap->iv_stats.is_tx_nobuf++;
3282 return NULL;
841ab66c 3283 }
4f655ef5 3284 ieee80211_beacon_construct(m, frm, ni);
841ab66c 3285
b5523eac 3286 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
841ab66c
SZ
3287 KASSERT(m != NULL, ("no space for 802.11 header?"));
3288 wh = mtod(m, struct ieee80211_frame *);
3289 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3290 IEEE80211_FC0_SUBTYPE_BEACON;
3291 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3292 *(uint16_t *)wh->i_dur = 0;
3293 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
32176cfd 3294 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
841ab66c
SZ
3295 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3296 *(uint16_t *)wh->i_seq = 0;
3297
3298 return m;
3299}
3300
3301/*
3302 * Update the dynamic parts of a beacon frame based on the current state.
3303 */
3304int
4f655ef5 3305ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
841ab66c 3306{
32176cfd 3307 struct ieee80211vap *vap = ni->ni_vap;
4f655ef5 3308 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
32176cfd 3309 struct ieee80211com *ic = ni->ni_ic;
841ab66c
SZ
3310 int len_changed = 0;
3311 uint16_t capinfo;
085ff963
MD
3312 struct ieee80211_frame *wh;
3313 ieee80211_seq seqno;
841ab66c 3314
085ff963 3315 IEEE80211_LOCK(ic);
32176cfd
RP
3316 /*
3317 * Handle 11h channel change when we've reached the count.
3318 * We must recalculate the beacon frame contents to account
3319 * for the new channel. Note we do this only for the first
3320 * vap that reaches this point; subsequent vaps just update
3321 * their beacon state to reflect the recalculated channel.
3322 */
3323 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3324 vap->iv_csa_count == ic->ic_csa_count) {
3325 vap->iv_csa_count = 0;
3326 /*
3327 * Effect channel change before reconstructing the beacon
3328 * frame contents as many places reference ni_chan.
3329 */
3330 if (ic->ic_csa_newchan != NULL)
3331 ieee80211_csa_completeswitch(ic);
3332 /*
3333 * NB: ieee80211_beacon_construct clears all pending
3334 * updates in bo_flags so we don't need to explicitly
3335 * clear IEEE80211_BEACON_CSA.
3336 */
3337 ieee80211_beacon_construct(m,
4f655ef5 3338 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
32176cfd
RP
3339
3340 /* XXX do WME aggressive mode processing? */
085ff963 3341 IEEE80211_UNLOCK(ic);
32176cfd
RP
3342 return 1; /* just assume length changed */
3343 }
841ab66c 3344
085ff963
MD
3345 wh = mtod(m, struct ieee80211_frame *);
3346 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3347 *(uint16_t *)&wh->i_seq[0] =
3348 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3349 M_SEQNO_SET(m, seqno);
3350
841ab66c 3351 /* XXX faster to recalculate entirely or just changes? */
32176cfd 3352 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
841ab66c
SZ
3353 *bo->bo_caps = htole16(capinfo);
3354
32176cfd 3355 if (vap->iv_flags & IEEE80211_F_WME) {
841ab66c
SZ
3356 struct ieee80211_wme_state *wme = &ic->ic_wme;
3357
3358 /*
4f655ef5 3359 * Check for aggressive mode change. When there is
841ab66c
SZ
3360 * significant high priority traffic in the BSS
3361 * throttle back BE traffic by using conservative
4f655ef5 3362 * parameters. Otherwise BE uses aggressive params
841ab66c
SZ
3363 * to optimize performance of legacy/non-QoS traffic.
3364 */
3365 if (wme->wme_flags & WME_F_AGGRMODE) {
3366 if (wme->wme_hipri_traffic >
3367 wme->wme_hipri_switch_thresh) {
32176cfd 3368 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
841ab66c
SZ
3369 "%s: traffic %u, disable aggressive mode\n",
3370 __func__, wme->wme_hipri_traffic);
3371 wme->wme_flags &= ~WME_F_AGGRMODE;
32176cfd 3372 ieee80211_wme_updateparams_locked(vap);
841ab66c
SZ
3373 wme->wme_hipri_traffic =
3374 wme->wme_hipri_switch_hysteresis;
3375 } else
3376 wme->wme_hipri_traffic = 0;
3377 } else {
3378 if (wme->wme_hipri_traffic <=
3379 wme->wme_hipri_switch_thresh) {
32176cfd 3380 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
841ab66c
SZ
3381 "%s: traffic %u, enable aggressive mode\n",
3382 __func__, wme->wme_hipri_traffic);
3383 wme->wme_flags |= WME_F_AGGRMODE;
32176cfd 3384 ieee80211_wme_updateparams_locked(vap);
841ab66c
SZ
3385 wme->wme_hipri_traffic = 0;
3386 } else
3387 wme->wme_hipri_traffic =
3388 wme->wme_hipri_switch_hysteresis;
3389 }
32176cfd 3390 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
841ab66c 3391 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
32176cfd 3392 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
841ab66c
SZ
3393 }
3394 }
3395
32176cfd
RP
3396 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3397 ieee80211_ht_update_beacon(vap, bo);
3398 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3399 }
3400#ifdef IEEE80211_SUPPORT_TDMA
3401 if (vap->iv_caps & IEEE80211_C_TDMA) {
3402 /*
3403 * NB: the beacon is potentially updated every TBTT.
3404 */
3405 ieee80211_tdma_update_beacon(vap, bo);
3406 }
3407#endif
3408#ifdef IEEE80211_SUPPORT_MESH
3409 if (vap->iv_opmode == IEEE80211_M_MBSS)
3410 ieee80211_mesh_update_beacon(vap, bo);
3411#endif
3412
3413 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3414 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
841ab66c
SZ
3415 struct ieee80211_tim_ie *tie =
3416 (struct ieee80211_tim_ie *) bo->bo_tim;
32176cfd 3417 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
841ab66c
SZ
3418 u_int timlen, timoff, i;
3419 /*
3420 * ATIM/DTIM needs updating. If it fits in the
3421 * current space allocated then just copy in the
3422 * new bits. Otherwise we need to move any trailing
3423 * data to make room. Note that we know there is
3424 * contiguous space because ieee80211_beacon_allocate
3425 * insures there is space in the mbuf to write a
32176cfd 3426 * maximal-size virtual bitmap (based on iv_max_aid).
841ab66c
SZ
3427 */
3428 /*
3429 * Calculate the bitmap size and offset, copy any
3430 * trailer out of the way, and then copy in the
3431 * new bitmap and update the information element.
3432 * Note that the tim bitmap must contain at least
3433 * one byte and any offset must be even.
3434 */
32176cfd 3435 if (vap->iv_ps_pending != 0) {
841ab66c 3436 timoff = 128; /* impossibly large */
32176cfd
RP
3437 for (i = 0; i < vap->iv_tim_len; i++)
3438 if (vap->iv_tim_bitmap[i]) {
841ab66c
SZ
3439 timoff = i &~ 1;
3440 break;
3441 }
3442 KASSERT(timoff != 128, ("tim bitmap empty!"));
32176cfd
RP
3443 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3444 if (vap->iv_tim_bitmap[i])
841ab66c
SZ
3445 break;
3446 timlen = 1 + (i - timoff);
3447 } else {
3448 timoff = 0;
3449 timlen = 1;
3450 }
3451 if (timlen != bo->bo_tim_len) {
3452 /* copy up/down trailer */
3453 int adjust = tie->tim_bitmap+timlen
32176cfd 3454 - bo->bo_tim_trailer;
afd2da4d
MD
3455 bcopy(bo->bo_tim_trailer,
3456 bo->bo_tim_trailer+adjust,
3457 bo->bo_tim_trailer_len);
32176cfd 3458 bo->bo_tim_trailer += adjust;
841ab66c 3459 bo->bo_erp += adjust;
32176cfd 3460 bo->bo_htinfo += adjust;
085ff963 3461#ifdef IEEE80211_SUPPORT_SUPERG
32176cfd
RP
3462 bo->bo_ath += adjust;
3463#endif
085ff963 3464#ifdef IEEE80211_SUPPORT_TDMA
32176cfd
RP
3465 bo->bo_tdma += adjust;
3466#endif
085ff963 3467#ifdef IEEE80211_SUPPORT_MESH
32176cfd
RP
3468 bo->bo_meshconf += adjust;
3469#endif
3470 bo->bo_appie += adjust;
3471 bo->bo_wme += adjust;
3472 bo->bo_csa += adjust;
085ff963 3473 bo->bo_quiet += adjust;
841ab66c
SZ
3474 bo->bo_tim_len = timlen;
3475
3476 /* update information element */
3477 tie->tim_len = 3 + timlen;
3478 tie->tim_bitctl = timoff;
3479 len_changed = 1;
3480 }
32176cfd 3481 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
841ab66c
SZ
3482 bo->bo_tim_len);
3483
32176cfd 3484 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
841ab66c 3485
32176cfd 3486 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
841ab66c 3487 "%s: TIM updated, pending %u, off %u, len %u\n",
32176cfd 3488 __func__, vap->iv_ps_pending, timoff, timlen);
841ab66c
SZ
3489 }
3490 /* count down DTIM period */
3491 if (tie->tim_count == 0)
3492 tie->tim_count = tie->tim_period - 1;
3493 else
3494 tie->tim_count--;
3495 /* update state for buffered multicast frames on DTIM */
3496 if (mcast && tie->tim_count == 0)
3497 tie->tim_bitctl |= 1;
3498 else
3499 tie->tim_bitctl &= ~1;
32176cfd
RP
3500 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3501 struct ieee80211_csa_ie *csa =
3502 (struct ieee80211_csa_ie *) bo->bo_csa;
3503
3504 /*
3505 * Insert or update CSA ie. If we're just starting
3506 * to count down to the channel switch then we need
3507 * to insert the CSA ie. Otherwise we just need to
3508 * drop the count. The actual change happens above
3509 * when the vap's count reaches the target count.
3510 */
3511 if (vap->iv_csa_count == 0) {
3512 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3513 bo->bo_erp += sizeof(*csa);
3514 bo->bo_htinfo += sizeof(*csa);
3515 bo->bo_wme += sizeof(*csa);
085ff963 3516#ifdef IEEE80211_SUPPORT_SUPERG
32176cfd
RP
3517 bo->bo_ath += sizeof(*csa);
3518#endif
085ff963 3519#ifdef IEEE80211_SUPPORT_TDMA
32176cfd
RP
3520 bo->bo_tdma += sizeof(*csa);
3521#endif
085ff963 3522#ifdef IEEE80211_SUPPORT_MESH
32176cfd
RP
3523 bo->bo_meshconf += sizeof(*csa);
3524#endif
3525 bo->bo_appie += sizeof(*csa);
3526 bo->bo_csa_trailer_len += sizeof(*csa);
085ff963 3527 bo->bo_quiet += sizeof(*csa);
32176cfd
RP
3528 bo->bo_tim_trailer_len += sizeof(*csa);
3529 m->m_len += sizeof(*csa);
3530 m->m_pkthdr.len += sizeof(*csa);
3531
3532 ieee80211_add_csa(bo->bo_csa, vap);
3533 } else
3534 csa->csa_count--;
3535 vap->iv_csa_count++;
3536 /* NB: don't clear IEEE80211_BEACON_CSA */
3537 }
085ff963
MD
3538 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3539 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3540 if (vap->iv_quiet)
3541 ieee80211_add_quiet(bo->bo_quiet, vap);
3542 }
32176cfd 3543 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
841ab66c
SZ
3544 /*
3545 * ERP element needs updating.
3546 */
3547 (void) ieee80211_add_erp(bo->bo_erp, ic);
32176cfd 3548 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
841ab66c 3549 }
32176cfd
RP
3550#ifdef IEEE80211_SUPPORT_SUPERG
3551 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3552 ieee80211_add_athcaps(bo->bo_ath, ni);
3553 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
6dd1c373 3554 }
841ab66c 3555#endif
841ab66c 3556 }
32176cfd
RP
3557 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3558 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3559 int aielen;
3560 uint8_t *frm;
3561
3562 aielen = 0;
3563 if (aie != NULL)
3564 aielen += aie->ie_len;
3565 if (aielen != bo->bo_appie_len) {
3566 /* copy up/down trailer */
3567 int adjust = aielen - bo->bo_appie_len;
afd2da4d
MD
3568 bcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3569 bo->bo_tim_trailer_len);
32176cfd
RP
3570 bo->bo_tim_trailer += adjust;
3571 bo->bo_appie += adjust;
3572 bo->bo_appie_len = aielen;
3573
3574 len_changed = 1;
2d7dda79 3575 }
32176cfd
RP
3576 frm = bo->bo_appie;
3577 if (aie != NULL)
3578 frm = add_appie(frm, aie);
3579 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
2d7dda79 3580 }
085ff963 3581 IEEE80211_UNLOCK(ic);
2d7dda79 3582
32176cfd 3583 return len_changed;
322b19a8 3584}
085ff963
MD
3585
3586/*
3587 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3588 * tunnel encapsulation. The frame is assumed to have an Ethernet
3589 * header at the front that must be stripped before prepending the
3590 * LLC followed by the Ethernet header passed in (with an Ethernet
3591 * type that specifies the payload size).
3592 */
3593struct mbuf *
3594ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3595 const struct ether_header *eh)
3596{
3597 struct llc *llc;
3598 uint16_t payload;
3599
3600 /* XXX optimize by combining m_adj+M_PREPEND */
3601 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3602 llc = mtod(m, struct llc *);
3603 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3604 llc->llc_control = LLC_UI;
3605 llc->llc_snap.org_code[0] = 0;
3606 llc->llc_snap.org_code[1] = 0;
3607 llc->llc_snap.org_code[2] = 0;
3608 llc->llc_snap.ether_type = eh->ether_type;
3609 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3610
b5523eac 3611 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
085ff963
MD
3612 if (m == NULL) { /* XXX cannot happen */
3613 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3614 "%s: no space for ether_header\n", __func__);
3615 vap->iv_stats.is_tx_nobuf++;
3616 return NULL;
3617 }
3618 ETHER_HEADER_COPY(mtod(m, void *), eh);
3619 mtod(m, struct ether_header *)->ether_type = htons(payload);
3620 return m;
3621}
3622
3623/*
3624 * Complete an mbuf transmission.
3625 *
3626 * For now, this simply processes a completed frame after the
3627 * driver has completed it's transmission and/or retransmission.
3628 * It assumes the frame is an 802.11 encapsulated frame.
3629 *
3630 * Later on it will grow to become the exit path for a given frame
3631 * from the driver and, depending upon how it's been encapsulated
3632 * and already transmitted, it may end up doing A-MPDU retransmission,
3633 * power save requeuing, etc.
3634 *
3635 * In order for the above to work, the driver entry point to this
3636 * must not hold any driver locks. Thus, the driver needs to delay
3637 * any actual mbuf completion until it can release said locks.
3638 *
3639 * This frees the mbuf and if the mbuf has a node reference,
3640 * the node reference will be freed.
3641 */
3642void
3643ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3644{
3645
3646 if (ni != NULL) {
4f655ef5
MD
3647 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3648
3649 if (status == 0) {
4f655ef5 3650 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
664776f3
IV
3651#if defined(__DragonFly__)
3652 /*
3653 * On DragonFly, IFCOUNTER_OBYTES and
3654 * IFCOUNTER_OMCASTS increments are currently done
3655 * by ifq_dispatch() already.
3656 */
3657#else
3658 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
4f655ef5
MD
3659 if (m->m_flags & M_MCAST)
3660 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
664776f3 3661#endif
4f655ef5
MD
3662 } else
3663 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
085ff963
MD
3664 if (m->m_flags & M_TXCB)
3665 ieee80211_process_callback(ni, m, status);
3666 ieee80211_free_node(ni);
3667 }
3668 m_freem(m);
3669}