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