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