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