xref: /freebsd-14-stable/sys/net80211/ieee80211_hostap.c (revision e9624aa3f2f8b370c42141b83c4b71bf06832142)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifdef __FreeBSD__
30 #endif
31 
32 /*
33  * IEEE 802.11 HOSTAP mode support.
34  */
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/endian.h>
47 #include <sys/errno.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_media.h>
54 #include <net/if_llc.h>
55 #include <net/if_private.h>
56 #include <net/ethernet.h>
57 
58 #include <net/bpf.h>
59 
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_hostap.h>
62 #include <net80211/ieee80211_input.h>
63 #ifdef IEEE80211_SUPPORT_SUPERG
64 #include <net80211/ieee80211_superg.h>
65 #endif
66 #include <net80211/ieee80211_wds.h>
67 #include <net80211/ieee80211_vht.h>
68 #include <net80211/ieee80211_sta.h> /* for parse_wmeie */
69 
70 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
71 
72 static	void hostap_vattach(struct ieee80211vap *);
73 static	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
74 static	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
75 	    const struct ieee80211_rx_stats *,
76 	    int rssi, int nf);
77 static void hostap_deliver_data(struct ieee80211vap *,
78 	    struct ieee80211_node *, struct mbuf *);
79 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
80 	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
81 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
82 
83 void
ieee80211_hostap_attach(struct ieee80211com * ic)84 ieee80211_hostap_attach(struct ieee80211com *ic)
85 {
86 	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
87 }
88 
89 void
ieee80211_hostap_detach(struct ieee80211com * ic)90 ieee80211_hostap_detach(struct ieee80211com *ic)
91 {
92 }
93 
94 static void
hostap_vdetach(struct ieee80211vap * vap)95 hostap_vdetach(struct ieee80211vap *vap)
96 {
97 }
98 
99 static void
hostap_vattach(struct ieee80211vap * vap)100 hostap_vattach(struct ieee80211vap *vap)
101 {
102 	vap->iv_newstate = hostap_newstate;
103 	vap->iv_input = hostap_input;
104 	vap->iv_recv_mgmt = hostap_recv_mgmt;
105 	vap->iv_recv_ctl = hostap_recv_ctl;
106 	vap->iv_opdetach = hostap_vdetach;
107 	vap->iv_deliver_data = hostap_deliver_data;
108 	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
109 }
110 
111 static void
sta_disassoc(void * arg,struct ieee80211_node * ni)112 sta_disassoc(void *arg, struct ieee80211_node *ni)
113 {
114 
115 	if (ni->ni_associd != 0) {
116 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
117 			IEEE80211_REASON_ASSOC_LEAVE);
118 		ieee80211_node_leave(ni);
119 	}
120 }
121 
122 static void
sta_csa(void * arg,struct ieee80211_node * ni)123 sta_csa(void *arg, struct ieee80211_node *ni)
124 {
125 	struct ieee80211vap *vap = ni->ni_vap;
126 
127 	if (ni->ni_associd != 0)
128 		if (ni->ni_inact > vap->iv_inact_init) {
129 			ni->ni_inact = vap->iv_inact_init;
130 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
131 			    "%s: inact %u", __func__, ni->ni_inact);
132 		}
133 }
134 
135 static void
sta_drop(void * arg,struct ieee80211_node * ni)136 sta_drop(void *arg, struct ieee80211_node *ni)
137 {
138 
139 	if (ni->ni_associd != 0)
140 		ieee80211_node_leave(ni);
141 }
142 
143 /*
144  * Does a channel change require associated stations to re-associate
145  * so protocol state is correct.  This is used when doing CSA across
146  * bands or similar (e.g. HT -> legacy).
147  */
148 static int
isbandchange(struct ieee80211com * ic)149 isbandchange(struct ieee80211com *ic)
150 {
151 	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
152 	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
153 	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
154 }
155 
156 /*
157  * IEEE80211_M_HOSTAP vap state machine handler.
158  */
159 static int
hostap_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)160 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
161 {
162 	struct ieee80211com *ic = vap->iv_ic;
163 	enum ieee80211_state ostate;
164 
165 	IEEE80211_LOCK_ASSERT(ic);
166 
167 	ostate = vap->iv_state;
168 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
169 	    __func__, ieee80211_state_name[ostate],
170 	    ieee80211_state_name[nstate], arg);
171 	vap->iv_state = nstate;			/* state transition */
172 	if (ostate != IEEE80211_S_SCAN)
173 		ieee80211_cancel_scan(vap);	/* background scan */
174 	switch (nstate) {
175 	case IEEE80211_S_INIT:
176 		switch (ostate) {
177 		case IEEE80211_S_SCAN:
178 			ieee80211_cancel_scan(vap);
179 			break;
180 		case IEEE80211_S_CAC:
181 			ieee80211_dfs_cac_stop(vap);
182 			break;
183 		case IEEE80211_S_RUN:
184 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
185 			    sta_disassoc, NULL);
186 			break;
187 		default:
188 			break;
189 		}
190 		if (ostate != IEEE80211_S_INIT) {
191 			/* NB: optimize INIT -> INIT case */
192 			ieee80211_reset_bss(vap);
193 		}
194 		if (vap->iv_auth->ia_detach != NULL)
195 			vap->iv_auth->ia_detach(vap);
196 		break;
197 	case IEEE80211_S_SCAN:
198 		switch (ostate) {
199 		case IEEE80211_S_CSA:
200 		case IEEE80211_S_RUN:
201 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
202 			    sta_disassoc, NULL);
203 			/*
204 			 * Clear overlapping BSS state; the beacon frame
205 			 * will be reconstructed on transition to the RUN
206 			 * state and the timeout routines check if the flag
207 			 * is set before doing anything so this is sufficient.
208 			 */
209 			vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
210 			vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
211 			/* XXX TODO: schedule deferred update? */
212 			/* fall thru... */
213 		case IEEE80211_S_CAC:
214 			/*
215 			 * NB: We may get here because of a manual channel
216 			 *     change in which case we need to stop CAC
217 			 * XXX no need to stop if ostate RUN but it's ok
218 			 */
219 			ieee80211_dfs_cac_stop(vap);
220 			/* fall thru... */
221 		case IEEE80211_S_INIT:
222 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
223 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
224 				/*
225 				 * Already have a channel; bypass the
226 				 * scan and startup immediately.
227 				 * ieee80211_create_ibss will call back to
228 				 * move us to RUN state.
229 				 */
230 				ieee80211_create_ibss(vap, vap->iv_des_chan);
231 				break;
232 			}
233 			/*
234 			 * Initiate a scan.  We can come here as a result
235 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
236 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
237 			 * and the scan request parameters will be present
238 			 * in iv_scanreq.  Otherwise we do the default.
239 			 */
240 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
241 				ieee80211_check_scan(vap,
242 				    vap->iv_scanreq_flags,
243 				    vap->iv_scanreq_duration,
244 				    vap->iv_scanreq_mindwell,
245 				    vap->iv_scanreq_maxdwell,
246 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
247 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
248 			} else
249 				ieee80211_check_scan_current(vap);
250 			break;
251 		case IEEE80211_S_SCAN:
252 			/*
253 			 * A state change requires a reset; scan.
254 			 */
255 			ieee80211_check_scan_current(vap);
256 			break;
257 		default:
258 			break;
259 		}
260 		break;
261 	case IEEE80211_S_CAC:
262 		/*
263 		 * Start CAC on a DFS channel.  We come here when starting
264 		 * a bss on a DFS channel (see ieee80211_create_ibss).
265 		 */
266 		ieee80211_dfs_cac_start(vap);
267 		break;
268 	case IEEE80211_S_RUN:
269 		if (vap->iv_flags & IEEE80211_F_WPA) {
270 			/* XXX validate prerequisites */
271 		}
272 		switch (ostate) {
273 		case IEEE80211_S_INIT:
274 			/*
275 			 * Already have a channel; bypass the
276 			 * scan and startup immediately.
277 			 * Note that ieee80211_create_ibss will call
278 			 * back to do a RUN->RUN state change.
279 			 */
280 			ieee80211_create_ibss(vap,
281 			    ieee80211_ht_adjust_channel(ic,
282 				ic->ic_curchan, vap->iv_flags_ht));
283 			/* NB: iv_bss is changed on return */
284 			break;
285 		case IEEE80211_S_CAC:
286 			/*
287 			 * NB: This is the normal state change when CAC
288 			 * expires and no radar was detected; no need to
289 			 * clear the CAC timer as it's already expired.
290 			 */
291 			/* fall thru... */
292 		case IEEE80211_S_CSA:
293 			/*
294 			 * Shorten inactivity timer of associated stations
295 			 * to weed out sta's that don't follow a CSA.
296 			 */
297 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
298 			    sta_csa, NULL);
299 			/*
300 			 * Update bss node channel to reflect where
301 			 * we landed after CSA.
302 			 */
303 			ieee80211_node_set_chan(vap->iv_bss,
304 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
305 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
306 			/* XXX bypass debug msgs */
307 			break;
308 		case IEEE80211_S_SCAN:
309 		case IEEE80211_S_RUN:
310 #ifdef IEEE80211_DEBUG
311 			if (ieee80211_msg_debug(vap)) {
312 				struct ieee80211_node *ni = vap->iv_bss;
313 				ieee80211_note(vap,
314 				    "synchronized with %s ssid ",
315 				    ether_sprintf(ni->ni_bssid));
316 				ieee80211_print_essid(ni->ni_essid,
317 				    ni->ni_esslen);
318 				/* XXX MCS/HT */
319 				printf(" channel %d start %uMb\n",
320 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
321 				    IEEE80211_RATE2MBS(ni->ni_txrate));
322 			}
323 #endif
324 			break;
325 		default:
326 			break;
327 		}
328 		/*
329 		 * Start/stop the authenticator.  We delay until here
330 		 * to allow configuration to happen out of order.
331 		 */
332 		if (vap->iv_auth->ia_attach != NULL) {
333 			/* XXX check failure */
334 			vap->iv_auth->ia_attach(vap);
335 		} else if (vap->iv_auth->ia_detach != NULL) {
336 			vap->iv_auth->ia_detach(vap);
337 		}
338 		ieee80211_node_authorize(vap->iv_bss);
339 		break;
340 	case IEEE80211_S_CSA:
341 		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
342 			/*
343 			 * On a ``band change'' silently drop associated
344 			 * stations as they must re-associate before they
345 			 * can pass traffic (as otherwise protocol state
346 			 * such as capabilities and the negotiated rate
347 			 * set may/will be wrong).
348 			 */
349 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
350 			    sta_drop, NULL);
351 		}
352 		break;
353 	default:
354 		break;
355 	}
356 	return 0;
357 }
358 
359 static void
hostap_deliver_data(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m)360 hostap_deliver_data(struct ieee80211vap *vap,
361 	struct ieee80211_node *ni, struct mbuf *m)
362 {
363 	struct ether_header *eh = mtod(m, struct ether_header *);
364 	struct ifnet *ifp = vap->iv_ifp;
365 
366 	/* clear driver/net80211 flags before passing up */
367 	m->m_flags &= ~(M_MCAST | M_BCAST);
368 	m_clrprotoflags(m);
369 
370 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
371 	    ("gack, opmode %d", vap->iv_opmode));
372 	/*
373 	 * Do accounting.
374 	 */
375 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
376 	IEEE80211_NODE_STAT(ni, rx_data);
377 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
378 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
379 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
380 		IEEE80211_NODE_STAT(ni, rx_mcast);
381 	} else
382 		IEEE80211_NODE_STAT(ni, rx_ucast);
383 
384 	/* perform as a bridge within the AP */
385 	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
386 		struct mbuf *mcopy = NULL;
387 
388 		if (m->m_flags & M_MCAST) {
389 			mcopy = m_dup(m, IEEE80211_M_NOWAIT);
390 			if (mcopy == NULL)
391 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
392 			else
393 				mcopy->m_flags |= M_MCAST;
394 		} else {
395 			/*
396 			 * Check if the destination is associated with the
397 			 * same vap and authorized to receive traffic.
398 			 * Beware of traffic destined for the vap itself;
399 			 * sending it will not work; just let it be delivered
400 			 * normally.
401 			 */
402 			struct ieee80211_node *sta = ieee80211_find_vap_node(
403 			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
404 			if (sta != NULL) {
405 				if (ieee80211_node_is_authorized(sta)) {
406 					/*
407 					 * Beware of sending to ourself; this
408 					 * needs to happen via the normal
409 					 * input path.
410 					 */
411 					if (sta != vap->iv_bss) {
412 						mcopy = m;
413 						m = NULL;
414 					}
415 				} else {
416 					vap->iv_stats.is_rx_unauth++;
417 					IEEE80211_NODE_STAT(sta, rx_unauth);
418 				}
419 				ieee80211_free_node(sta);
420 			}
421 		}
422 		if (mcopy != NULL)
423 			(void) ieee80211_vap_xmitpkt(vap, mcopy);
424 	}
425 	if (m != NULL) {
426 		struct epoch_tracker et;
427 
428 		/*
429 		 * Mark frame as coming from vap's interface.
430 		 */
431 		m->m_pkthdr.rcvif = ifp;
432 		if (m->m_flags & M_MCAST) {
433 			/*
434 			 * Spam DWDS vap's w/ multicast traffic.
435 			 */
436 			/* XXX only if dwds in use? */
437 			ieee80211_dwds_mcast(vap, m);
438 		}
439 		if (ni->ni_vlan != 0) {
440 			/* attach vlan tag */
441 			m->m_pkthdr.ether_vtag = ni->ni_vlan;
442 			m->m_flags |= M_VLANTAG;
443 		}
444 		NET_EPOCH_ENTER(et);
445 		ifp->if_input(ifp, m);
446 		NET_EPOCH_EXIT(et);
447 	}
448 }
449 
450 /*
451  * Decide if a received management frame should be
452  * printed when debugging is enabled.  This filters some
453  * of the less interesting frames that come frequently
454  * (e.g. beacons).
455  */
456 static __inline int
doprint(struct ieee80211vap * vap,int subtype)457 doprint(struct ieee80211vap *vap, int subtype)
458 {
459 	switch (subtype) {
460 	case IEEE80211_FC0_SUBTYPE_BEACON:
461 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
462 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
463 		return 0;
464 	}
465 	return 1;
466 }
467 
468 /*
469  * Process a received frame.  The node associated with the sender
470  * should be supplied.  If nothing was found in the node table then
471  * the caller is assumed to supply a reference to iv_bss instead.
472  * The RSSI and a timestamp are also supplied.  The RSSI data is used
473  * during AP scanning to select a AP to associate with; it can have
474  * any units so long as values have consistent units and higher values
475  * mean ``better signal''.  The receive timestamp is currently not used
476  * by the 802.11 layer.
477  */
478 static int
hostap_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)479 hostap_input(struct ieee80211_node *ni, struct mbuf *m,
480     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
481 {
482 	struct ieee80211vap *vap = ni->ni_vap;
483 	struct ieee80211com *ic = ni->ni_ic;
484 	struct ifnet *ifp = vap->iv_ifp;
485 	struct ieee80211_frame *wh;
486 	struct ieee80211_key *key;
487 	struct ether_header *eh;
488 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
489 	uint8_t dir, type, subtype, qos;
490 	uint8_t *bssid;
491 	int is_hw_decrypted = 0;
492 	int has_decrypted = 0;
493 
494 	/*
495 	 * Some devices do hardware decryption all the way through
496 	 * to pretending the frame wasn't encrypted in the first place.
497 	 * So, tag it appropriately so it isn't discarded inappropriately.
498 	 */
499 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
500 		is_hw_decrypted = 1;
501 
502 	if (m->m_flags & M_AMPDU_MPDU) {
503 		/*
504 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
505 		 * w/ M_AMPDU_MPDU marked have already passed through
506 		 * here but were received out of order and been held on
507 		 * the reorder queue.  When resubmitted they are marked
508 		 * with the M_AMPDU_MPDU flag and we can bypass most of
509 		 * the normal processing.
510 		 */
511 		wh = mtod(m, struct ieee80211_frame *);
512 		type = IEEE80211_FC0_TYPE_DATA;
513 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
514 		subtype = IEEE80211_FC0_SUBTYPE_QOS_DATA;
515 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
516 		goto resubmit_ampdu;
517 	}
518 
519 	KASSERT(ni != NULL, ("null node"));
520 	ni->ni_inact = ni->ni_inact_reload;
521 
522 	type = -1;			/* undefined */
523 
524 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
525 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
526 		    ni->ni_macaddr, NULL,
527 		    "too short (1): len %u", m->m_pkthdr.len);
528 		vap->iv_stats.is_rx_tooshort++;
529 		goto out;
530 	}
531 	/*
532 	 * Bit of a cheat here, we use a pointer for a 3-address
533 	 * frame format but don't reference fields past outside
534 	 * ieee80211_frame_min w/o first validating the data is
535 	 * present.
536 	 */
537 	wh = mtod(m, struct ieee80211_frame *);
538 
539 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
540 	    IEEE80211_FC0_VERSION_0) {
541 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
542 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
543 		    wh->i_fc[0], wh->i_fc[1]);
544 		vap->iv_stats.is_rx_badversion++;
545 		goto err;
546 	}
547 
548 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
549 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
550 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
551 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
552 		if (dir != IEEE80211_FC1_DIR_NODS)
553 			bssid = wh->i_addr1;
554 		else if (type == IEEE80211_FC0_TYPE_CTL)
555 			bssid = wh->i_addr1;
556 		else {
557 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
558 				IEEE80211_DISCARD_MAC(vap,
559 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
560 				    NULL, "too short (2): len %u",
561 				    m->m_pkthdr.len);
562 				vap->iv_stats.is_rx_tooshort++;
563 				goto out;
564 			}
565 			bssid = wh->i_addr3;
566 		}
567 		/*
568 		 * Validate the bssid.
569 		 */
570 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
571 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
572 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
573 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
574 			/* not interested in */
575 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
576 			    bssid, NULL, "%s", "not to bss");
577 			vap->iv_stats.is_rx_wrongbss++;
578 			goto out;
579 		}
580 
581 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
582 		ni->ni_noise = nf;
583 		if (IEEE80211_HAS_SEQ(type, subtype)) {
584 			uint8_t tid = ieee80211_gettid(wh);
585 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
586 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
587 				ic->ic_wme.wme_hipri_traffic++;
588 			if (! ieee80211_check_rxseq(ni, wh, bssid, rxs))
589 				goto out;
590 		}
591 	}
592 
593 	switch (type) {
594 	case IEEE80211_FC0_TYPE_DATA:
595 		hdrspace = ieee80211_hdrspace(ic, wh);
596 		if (m->m_len < hdrspace &&
597 		    (m = m_pullup(m, hdrspace)) == NULL) {
598 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
599 			    ni->ni_macaddr, NULL,
600 			    "data too short: expecting %u", hdrspace);
601 			vap->iv_stats.is_rx_tooshort++;
602 			goto out;		/* XXX */
603 		}
604 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
605 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
606 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
607 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
608 				IEEE80211_DISCARD(vap,
609 				    IEEE80211_MSG_INPUT, wh, "data",
610 				    "incorrect dir 0x%x", dir);
611 			} else {
612 				IEEE80211_DISCARD(vap,
613 				    IEEE80211_MSG_INPUT |
614 				    IEEE80211_MSG_WDS, wh,
615 				    "4-address data",
616 				    "%s", "DWDS not enabled");
617 			}
618 			vap->iv_stats.is_rx_wrongdir++;
619 			goto out;
620 		}
621 		/* check if source STA is associated */
622 		if (ni == vap->iv_bss) {
623 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
624 			    wh, "data", "%s", "unknown src");
625 			ieee80211_send_error(ni, wh->i_addr2,
626 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
627 			    IEEE80211_REASON_NOT_AUTHED);
628 			vap->iv_stats.is_rx_notassoc++;
629 			goto err;
630 		}
631 		if (ni->ni_associd == 0) {
632 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
633 			    wh, "data", "%s", "unassoc src");
634 			IEEE80211_SEND_MGMT(ni,
635 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
636 			    IEEE80211_REASON_NOT_ASSOCED);
637 			vap->iv_stats.is_rx_notassoc++;
638 			goto err;
639 		}
640 
641 		/*
642 		 * Check for power save state change.
643 		 * XXX out-of-order A-MPDU frames?
644 		 */
645 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
646 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
647 			vap->iv_node_ps(ni,
648 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
649 		/*
650 		 * For 4-address packets handle WDS discovery
651 		 * notifications.  Once a WDS link is setup frames
652 		 * are just delivered to the WDS vap (see below).
653 		 */
654 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
655 			if (!ieee80211_node_is_authorized(ni)) {
656 				IEEE80211_DISCARD(vap,
657 				    IEEE80211_MSG_INPUT |
658 				    IEEE80211_MSG_WDS, wh,
659 				    "4-address data",
660 				    "%s", "unauthorized port");
661 				vap->iv_stats.is_rx_unauth++;
662 				IEEE80211_NODE_STAT(ni, rx_unauth);
663 				goto err;
664 			}
665 			ieee80211_dwds_discover(ni, m);
666 			return type;
667 		}
668 
669 		/*
670 		 * Handle A-MPDU re-ordering.  If the frame is to be
671 		 * processed directly then ieee80211_ampdu_reorder
672 		 * will return 0; otherwise it has consumed the mbuf
673 		 * and we should do nothing more with it.
674 		 */
675 		if ((m->m_flags & M_AMPDU) &&
676 		    ieee80211_ampdu_reorder(ni, m, rxs) != 0) {
677 			m = NULL;
678 			goto out;
679 		}
680 	resubmit_ampdu:
681 
682 		/*
683 		 * Handle privacy requirements.  Note that we
684 		 * must not be preempted from here until after
685 		 * we (potentially) call ieee80211_crypto_demic;
686 		 * otherwise we may violate assumptions in the
687 		 * crypto cipher modules used to do delayed update
688 		 * of replay sequence numbers.
689 		 */
690 		if (is_hw_decrypted || IEEE80211_IS_PROTECTED(wh)) {
691 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
692 				/*
693 				 * Discard encrypted frames when privacy is off.
694 				 */
695 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
696 				    wh, "WEP", "%s", "PRIVACY off");
697 				vap->iv_stats.is_rx_noprivacy++;
698 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
699 				goto out;
700 			}
701 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
702 				/* NB: stats+msgs handled in crypto_decap */
703 				IEEE80211_NODE_STAT(ni, rx_wepfail);
704 				goto out;
705 			}
706 			wh = mtod(m, struct ieee80211_frame *);
707 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
708 			has_decrypted = 1;
709 		} else {
710 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
711 			key = NULL;
712 		}
713 
714 		/*
715 		 * Save QoS bits for use below--before we strip the header.
716 		 */
717 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS_DATA)
718 			qos = ieee80211_getqos(wh)[0];
719 		else
720 			qos = 0;
721 
722 		/*
723 		 * Next up, any fragmentation.
724 		 */
725 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
726 			m = ieee80211_defrag(ni, m, hdrspace, has_decrypted);
727 			if (m == NULL) {
728 				/* Fragment dropped or frame not complete yet */
729 				goto out;
730 			}
731 		}
732 		wh = NULL;		/* no longer valid, catch any uses */
733 
734 		/*
735 		 * Next strip any MSDU crypto bits.
736 		 */
737 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
738 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
739 			    ni->ni_macaddr, "data", "%s", "demic error");
740 			vap->iv_stats.is_rx_demicfail++;
741 			IEEE80211_NODE_STAT(ni, rx_demicfail);
742 			goto out;
743 		}
744 		/* copy to listener after decrypt */
745 		if (ieee80211_radiotap_active_vap(vap))
746 			ieee80211_radiotap_rx(vap, m);
747 		need_tap = 0;
748 		/*
749 		 * Finally, strip the 802.11 header.
750 		 */
751 		m = ieee80211_decap(vap, m, hdrspace, qos);
752 		if (m == NULL) {
753 			/* XXX mask bit to check for both */
754 			/* don't count Null data frames as errors */
755 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
756 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
757 				goto out;
758 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
759 			    ni->ni_macaddr, "data", "%s", "decap error");
760 			vap->iv_stats.is_rx_decap++;
761 			IEEE80211_NODE_STAT(ni, rx_decap);
762 			goto err;
763 		}
764 		if (!(qos & IEEE80211_QOS_AMSDU))
765 			eh = mtod(m, struct ether_header *);
766 		else
767 			eh = NULL;
768 		if (!ieee80211_node_is_authorized(ni)) {
769 			/*
770 			 * Deny any non-PAE frames received prior to
771 			 * authorization.  For open/shared-key
772 			 * authentication the port is mark authorized
773 			 * after authentication completes.  For 802.1x
774 			 * the port is not marked authorized by the
775 			 * authenticator until the handshake has completed.
776 			 */
777 			if (eh == NULL ||
778 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
779 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
780 				    ni->ni_macaddr, "data", "unauthorized or "
781 				    "unknown port: ether type 0x%x len %u",
782 				    eh == NULL ? -1 : eh->ether_type,
783 				    m->m_pkthdr.len);
784 				vap->iv_stats.is_rx_unauth++;
785 				IEEE80211_NODE_STAT(ni, rx_unauth);
786 				goto err;
787 			}
788 		} else {
789 			/*
790 			 * When denying unencrypted frames, discard
791 			 * any non-PAE frames received without encryption.
792 			 */
793 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
794 			    ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
795 			    (is_hw_decrypted == 0) &&
796 			    (eh == NULL ||
797 			     eh->ether_type != htons(ETHERTYPE_PAE))) {
798 				/*
799 				 * Drop unencrypted frames.
800 				 */
801 				vap->iv_stats.is_rx_unencrypted++;
802 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
803 				goto out;
804 			}
805 		}
806 		/* XXX require HT? */
807 		if (qos & IEEE80211_QOS_AMSDU) {
808 			m = ieee80211_decap_amsdu(ni, m);
809 			if (m == NULL)
810 				return IEEE80211_FC0_TYPE_DATA;
811 		} else {
812 #ifdef IEEE80211_SUPPORT_SUPERG
813 			m = ieee80211_decap_fastframe(vap, ni, m);
814 			if (m == NULL)
815 				return IEEE80211_FC0_TYPE_DATA;
816 #endif
817 		}
818 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
819 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
820 		else
821 			hostap_deliver_data(vap, ni, m);
822 		return IEEE80211_FC0_TYPE_DATA;
823 
824 	case IEEE80211_FC0_TYPE_MGT:
825 		vap->iv_stats.is_rx_mgmt++;
826 		IEEE80211_NODE_STAT(ni, rx_mgmt);
827 		if (dir != IEEE80211_FC1_DIR_NODS) {
828 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
829 			    wh, "mgt", "incorrect dir 0x%x", dir);
830 			vap->iv_stats.is_rx_wrongdir++;
831 			goto err;
832 		}
833 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
834 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
835 			    ni->ni_macaddr, "mgt", "too short: len %u",
836 			    m->m_pkthdr.len);
837 			vap->iv_stats.is_rx_tooshort++;
838 			goto out;
839 		}
840 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
841 			/* ensure return frames are unicast */
842 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
843 			    wh, NULL, "source is multicast: %s",
844 			    ether_sprintf(wh->i_addr2));
845 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
846 			goto out;
847 		}
848 #ifdef IEEE80211_DEBUG
849 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
850 		    ieee80211_msg_dumppkts(vap)) {
851 			if_printf(ifp, "received %s from %s rssi %d\n",
852 			    ieee80211_mgt_subtype_name(subtype),
853 			    ether_sprintf(wh->i_addr2), rssi);
854 		}
855 #endif
856 		if (IEEE80211_IS_PROTECTED(wh)) {
857 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
858 				/*
859 				 * Only shared key auth frames with a challenge
860 				 * should be encrypted, discard all others.
861 				 */
862 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
863 				    wh, NULL,
864 				    "%s", "WEP set but not permitted");
865 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
866 				goto out;
867 			}
868 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
869 				/*
870 				 * Discard encrypted frames when privacy is off.
871 				 */
872 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
873 				    wh, NULL, "%s", "WEP set but PRIVACY off");
874 				vap->iv_stats.is_rx_noprivacy++;
875 				goto out;
876 			}
877 			hdrspace = ieee80211_hdrspace(ic, wh);
878 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
879 				/* NB: stats+msgs handled in crypto_decap */
880 				goto out;
881 			}
882 			wh = mtod(m, struct ieee80211_frame *);
883 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
884 			has_decrypted = 1;
885 		}
886 		/*
887 		 * Pass the packet to radiotap before calling iv_recv_mgmt().
888 		 * Otherwise iv_recv_mgmt() might pass another packet to
889 		 * radiotap, resulting in out of order packet captures.
890 		 */
891 		if (ieee80211_radiotap_active_vap(vap))
892 			ieee80211_radiotap_rx(vap, m);
893 		need_tap = 0;
894 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
895 		goto out;
896 
897 	case IEEE80211_FC0_TYPE_CTL:
898 		vap->iv_stats.is_rx_ctl++;
899 		IEEE80211_NODE_STAT(ni, rx_ctrl);
900 		vap->iv_recv_ctl(ni, m, subtype);
901 		goto out;
902 	default:
903 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
904 		    wh, "bad", "frame type 0x%x", type);
905 		/* should not come here */
906 		break;
907 	}
908 err:
909 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
910 out:
911 	if (m != NULL) {
912 		if (need_tap && ieee80211_radiotap_active_vap(vap))
913 			ieee80211_radiotap_rx(vap, m);
914 		m_freem(m);
915 	}
916 	return type;
917 }
918 
919 static void
hostap_auth_open(struct ieee80211_node * ni,struct ieee80211_frame * wh,int rssi,int nf,uint16_t seq,uint16_t status)920 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
921     int rssi, int nf, uint16_t seq, uint16_t status)
922 {
923 	struct ieee80211vap *vap = ni->ni_vap;
924 
925 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
926 
927 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
928 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
929 		    ni->ni_macaddr, "open auth",
930 		    "bad sta auth mode %u", ni->ni_authmode);
931 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
932 		/*
933 		 * Clear any challenge text that may be there if
934 		 * a previous shared key auth failed and then an
935 		 * open auth is attempted.
936 		 */
937 		if (ni->ni_challenge != NULL) {
938 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
939 			ni->ni_challenge = NULL;
940 		}
941 		/* XXX hack to workaround calling convention */
942 		ieee80211_send_error(ni, wh->i_addr2,
943 		    IEEE80211_FC0_SUBTYPE_AUTH,
944 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
945 		return;
946 	}
947 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
948 		vap->iv_stats.is_rx_bad_auth++;
949 		return;
950 	}
951 	/* always accept open authentication requests */
952 	if (ni == vap->iv_bss) {
953 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
954 		if (ni == NULL)
955 			return;
956 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
957 		(void) ieee80211_ref_node(ni);
958 	/*
959 	 * Mark the node as referenced to reflect that it's
960 	 * reference count has been bumped to insure it remains
961 	 * after the transaction completes.
962 	 */
963 	ni->ni_flags |= IEEE80211_NODE_AREF;
964 	/*
965 	 * Mark the node as requiring a valid association id
966 	 * before outbound traffic is permitted.
967 	 */
968 	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
969 
970 	if (vap->iv_acl != NULL &&
971 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
972 		/*
973 		 * When the ACL policy is set to RADIUS we defer the
974 		 * authorization to a user agent.  Dispatch an event,
975 		 * a subsequent MLME call will decide the fate of the
976 		 * station.  If the user agent is not present then the
977 		 * node will be reclaimed due to inactivity.
978 		 */
979 		IEEE80211_NOTE_MAC(vap,
980 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
981 		    "%s", "station authentication deferred (radius acl)");
982 		ieee80211_notify_node_auth(ni);
983 	} else {
984 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
985 		IEEE80211_NOTE_MAC(vap,
986 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
987 		    "%s", "station authenticated (open)");
988 		/*
989 		 * When 802.1x is not in use mark the port
990 		 * authorized at this point so traffic can flow.
991 		 */
992 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
993 			ieee80211_node_authorize(ni);
994 	}
995 }
996 
997 static void
hostap_auth_shared(struct ieee80211_node * ni,struct ieee80211_frame * wh,uint8_t * frm,uint8_t * efrm,int rssi,int nf,uint16_t seq,uint16_t status)998 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
999     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1000     uint16_t seq, uint16_t status)
1001 {
1002 	struct ieee80211vap *vap = ni->ni_vap;
1003 	uint8_t *challenge;
1004 	int estatus;
1005 
1006 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1007 
1008 	/*
1009 	 * NB: this can happen as we allow pre-shared key
1010 	 * authentication to be enabled w/o wep being turned
1011 	 * on so that configuration of these can be done
1012 	 * in any order.  It may be better to enforce the
1013 	 * ordering in which case this check would just be
1014 	 * for sanity/consistency.
1015 	 */
1016 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1017 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1018 		    ni->ni_macaddr, "shared key auth",
1019 		    "%s", " PRIVACY is disabled");
1020 		estatus = IEEE80211_STATUS_ALG;
1021 		goto bad;
1022 	}
1023 	/*
1024 	 * Pre-shared key authentication is evil; accept
1025 	 * it only if explicitly configured (it is supported
1026 	 * mainly for compatibility with clients like Mac OS X).
1027 	 */
1028 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1029 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1030 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1031 		    ni->ni_macaddr, "shared key auth",
1032 		    "bad sta auth mode %u", ni->ni_authmode);
1033 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1034 		estatus = IEEE80211_STATUS_ALG;
1035 		goto bad;
1036 	}
1037 
1038 	challenge = NULL;
1039 	if (frm + 1 < efrm) {
1040 		if ((frm[1] + 2) > (efrm - frm)) {
1041 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1042 			    ni->ni_macaddr, "shared key auth",
1043 			    "ie %d/%d too long",
1044 			    frm[0], (frm[1] + 2) - (efrm - frm));
1045 			vap->iv_stats.is_rx_bad_auth++;
1046 			estatus = IEEE80211_STATUS_CHALLENGE;
1047 			goto bad;
1048 		}
1049 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1050 			challenge = frm;
1051 		frm += frm[1] + 2;
1052 	}
1053 	switch (seq) {
1054 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1055 	case IEEE80211_AUTH_SHARED_RESPONSE:
1056 		if (challenge == NULL) {
1057 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058 			    ni->ni_macaddr, "shared key auth",
1059 			    "%s", "no challenge");
1060 			vap->iv_stats.is_rx_bad_auth++;
1061 			estatus = IEEE80211_STATUS_CHALLENGE;
1062 			goto bad;
1063 		}
1064 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1065 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1066 			    ni->ni_macaddr, "shared key auth",
1067 			    "bad challenge len %d", challenge[1]);
1068 			vap->iv_stats.is_rx_bad_auth++;
1069 			estatus = IEEE80211_STATUS_CHALLENGE;
1070 			goto bad;
1071 		}
1072 	default:
1073 		break;
1074 	}
1075 	switch (seq) {
1076 	case IEEE80211_AUTH_SHARED_REQUEST:
1077 	{
1078 #ifdef IEEE80211_DEBUG
1079 		bool allocbs;
1080 #endif
1081 
1082 		if (ni == vap->iv_bss) {
1083 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1084 			if (ni == NULL) {
1085 				/* NB: no way to return an error */
1086 				return;
1087 			}
1088 #ifdef IEEE80211_DEBUG
1089 			allocbs = 1;
1090 #endif
1091 		} else {
1092 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1093 				(void) ieee80211_ref_node(ni);
1094 #ifdef IEEE80211_DEBUG
1095 			allocbs = 0;
1096 #endif
1097 		}
1098 		/*
1099 		 * Mark the node as referenced to reflect that it's
1100 		 * reference count has been bumped to insure it remains
1101 		 * after the transaction completes.
1102 		 */
1103 		ni->ni_flags |= IEEE80211_NODE_AREF;
1104 		/*
1105 		 * Mark the node as requiring a valid association id
1106 		 * before outbound traffic is permitted.
1107 		 */
1108 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1109 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1110 		ni->ni_noise = nf;
1111 		if (!ieee80211_alloc_challenge(ni)) {
1112 			/* NB: don't return error so they rexmit */
1113 			return;
1114 		}
1115 		net80211_get_random_bytes(ni->ni_challenge,
1116 			IEEE80211_CHALLENGE_LEN);
1117 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1118 		    ni, "shared key %sauth request", allocbs ? "" : "re");
1119 		/*
1120 		 * When the ACL policy is set to RADIUS we defer the
1121 		 * authorization to a user agent.  Dispatch an event,
1122 		 * a subsequent MLME call will decide the fate of the
1123 		 * station.  If the user agent is not present then the
1124 		 * node will be reclaimed due to inactivity.
1125 		 */
1126 		if (vap->iv_acl != NULL &&
1127 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1128 			IEEE80211_NOTE_MAC(vap,
1129 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1130 			    ni->ni_macaddr,
1131 			    "%s", "station authentication deferred (radius acl)");
1132 			ieee80211_notify_node_auth(ni);
1133 			return;
1134 		}
1135 		break;
1136 	}
1137 	case IEEE80211_AUTH_SHARED_RESPONSE:
1138 		if (ni == vap->iv_bss) {
1139 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1140 			    ni->ni_macaddr, "shared key response",
1141 			    "%s", "unknown station");
1142 			/* NB: don't send a response */
1143 			return;
1144 		}
1145 		if (ni->ni_challenge == NULL) {
1146 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1147 			    ni->ni_macaddr, "shared key response",
1148 			    "%s", "no challenge recorded");
1149 			vap->iv_stats.is_rx_bad_auth++;
1150 			estatus = IEEE80211_STATUS_CHALLENGE;
1151 			goto bad;
1152 		}
1153 		if (memcmp(ni->ni_challenge, &challenge[2],
1154 			   challenge[1]) != 0) {
1155 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1156 			    ni->ni_macaddr, "shared key response",
1157 			    "%s", "challenge mismatch");
1158 			vap->iv_stats.is_rx_auth_fail++;
1159 			estatus = IEEE80211_STATUS_CHALLENGE;
1160 			goto bad;
1161 		}
1162 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1163 		    ni, "%s", "station authenticated (shared key)");
1164 		ieee80211_node_authorize(ni);
1165 		break;
1166 	default:
1167 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1168 		    ni->ni_macaddr, "shared key auth",
1169 		    "bad seq %d", seq);
1170 		vap->iv_stats.is_rx_bad_auth++;
1171 		estatus = IEEE80211_STATUS_SEQUENCE;
1172 		goto bad;
1173 	}
1174 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1175 	return;
1176 bad:
1177 	/*
1178 	 * Send an error response; but only when operating as an AP.
1179 	 */
1180 	/* XXX hack to workaround calling convention */
1181 	ieee80211_send_error(ni, wh->i_addr2,
1182 	    IEEE80211_FC0_SUBTYPE_AUTH,
1183 	    (seq + 1) | (estatus<<16));
1184 }
1185 
1186 /*
1187  * Convert a WPA cipher selector OUI to an internal
1188  * cipher algorithm.  Where appropriate we also
1189  * record any key length.
1190  */
1191 static int
wpa_cipher(const uint8_t * sel,uint8_t * keylen,uint8_t * cipher)1192 wpa_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1193 {
1194 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1195 	uint32_t w = le32dec(sel);
1196 
1197 	switch (w) {
1198 	case WPA_SEL(WPA_CSE_NULL):
1199 		*cipher = IEEE80211_CIPHER_NONE;
1200 		break;
1201 	case WPA_SEL(WPA_CSE_WEP40):
1202 		if (keylen)
1203 			*keylen = 40 / NBBY;
1204 		*cipher = IEEE80211_CIPHER_WEP;
1205 		break;
1206 	case WPA_SEL(WPA_CSE_WEP104):
1207 		if (keylen)
1208 			*keylen = 104 / NBBY;
1209 		*cipher = IEEE80211_CIPHER_WEP;
1210 		break;
1211 	case WPA_SEL(WPA_CSE_TKIP):
1212 		*cipher = IEEE80211_CIPHER_TKIP;
1213 		break;
1214 	case WPA_SEL(WPA_CSE_CCMP):
1215 		*cipher = IEEE80211_CIPHER_AES_CCM;
1216 		break;
1217 	default:
1218 		return (EINVAL);
1219 	}
1220 
1221 	return (0);
1222 #undef WPA_SEL
1223 }
1224 
1225 /*
1226  * Convert a WPA key management/authentication algorithm
1227  * to an internal code.
1228  */
1229 static int
wpa_keymgmt(const uint8_t * sel)1230 wpa_keymgmt(const uint8_t *sel)
1231 {
1232 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1233 	uint32_t w = le32dec(sel);
1234 
1235 	switch (w) {
1236 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1237 		return WPA_ASE_8021X_UNSPEC;
1238 	case WPA_SEL(WPA_ASE_8021X_PSK):
1239 		return WPA_ASE_8021X_PSK;
1240 	case WPA_SEL(WPA_ASE_NONE):
1241 		return WPA_ASE_NONE;
1242 	}
1243 	return 0;		/* NB: so is discarded */
1244 #undef WPA_SEL
1245 }
1246 
1247 /*
1248  * Parse a WPA information element to collect parameters.
1249  * Note that we do not validate security parameters; that
1250  * is handled by the authenticator; the parsing done here
1251  * is just for internal use in making operational decisions.
1252  */
1253 static int
ieee80211_parse_wpa(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)1254 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1255 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1256 {
1257 	uint8_t len = frm[1];
1258 	uint32_t w;
1259 	int error, n;
1260 
1261 	/*
1262 	 * Check the length once for fixed parts: OUI, type,
1263 	 * version, mcast cipher, and 2 selector counts.
1264 	 * Other, variable-length data, must be checked separately.
1265 	 */
1266 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1267 		IEEE80211_DISCARD_IE(vap,
1268 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1269 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1270 		return IEEE80211_REASON_IE_INVALID;
1271 	}
1272 	if (len < 14) {
1273 		IEEE80211_DISCARD_IE(vap,
1274 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1275 		    wh, "WPA", "too short, len %u", len);
1276 		return IEEE80211_REASON_IE_INVALID;
1277 	}
1278 	frm += 6, len -= 4;		/* NB: len is payload only */
1279 	/* NB: iswpaoui already validated the OUI and type */
1280 	w = le16dec(frm);
1281 	if (w != WPA_VERSION) {
1282 		IEEE80211_DISCARD_IE(vap,
1283 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1284 		    wh, "WPA", "bad version %u", w);
1285 		return IEEE80211_REASON_IE_INVALID;
1286 	}
1287 	frm += 2, len -= 2;
1288 
1289 	memset(rsn, 0, sizeof(*rsn));
1290 
1291 	/* multicast/group cipher */
1292 	error = wpa_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
1293 	if (error != 0) {
1294 		IEEE80211_DISCARD_IE(vap,
1295 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1296 		    wh, "WPA", "unknown mcast cipher suite %08X",
1297 		    le32dec(frm));
1298 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1299 	}
1300 	frm += 4, len -= 4;
1301 
1302 	/* unicast ciphers */
1303 	n = le16dec(frm);
1304 	frm += 2, len -= 2;
1305 	if (len < n*4+2) {
1306 		IEEE80211_DISCARD_IE(vap,
1307 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1308 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1309 		    len, n);
1310 		return IEEE80211_REASON_IE_INVALID;
1311 	}
1312 	w = 0;
1313 	for (; n > 0; n--) {
1314 		uint8_t cipher;
1315 
1316 		error = wpa_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
1317 		if (error == 0)
1318 			w |= 1 << cipher;
1319 
1320 		frm += 4, len -= 4;
1321 	}
1322 	if (w == 0) {
1323 		IEEE80211_DISCARD_IE(vap,
1324 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1325 		    wh, "WPA", "no usable pairwise cipher suite found (w=%d)",
1326 		    w);
1327 		return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
1328 	}
1329 	/* XXX other? */
1330 	if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1331 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1332 	else
1333 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1334 
1335 	/* key management algorithms */
1336 	n = le16dec(frm);
1337 	frm += 2, len -= 2;
1338 	if (len < n*4) {
1339 		IEEE80211_DISCARD_IE(vap,
1340 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1341 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1342 		    len, n);
1343 		return IEEE80211_REASON_IE_INVALID;
1344 	}
1345 	w = 0;
1346 	for (; n > 0; n--) {
1347 		w |= wpa_keymgmt(frm);
1348 		frm += 4, len -= 4;
1349 	}
1350 	if (w & WPA_ASE_8021X_UNSPEC)
1351 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1352 	else
1353 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1354 
1355 	if (len > 2)		/* optional capabilities */
1356 		rsn->rsn_caps = le16dec(frm);
1357 
1358 	return 0;
1359 }
1360 
1361 /*
1362  * Convert an RSN cipher selector OUI to an internal
1363  * cipher algorithm.  Where appropriate we also
1364  * record any key length.
1365  */
1366 static int
rsn_cipher(const uint8_t * sel,uint8_t * keylen,uint8_t * cipher)1367 rsn_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1368 {
1369 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1370 	uint32_t w = le32dec(sel);
1371 
1372 	switch (w) {
1373 	case RSN_SEL(RSN_CSE_NULL):
1374 		*cipher = IEEE80211_CIPHER_NONE;
1375 		break;
1376 	case RSN_SEL(RSN_CSE_WEP40):
1377 		if (keylen)
1378 			*keylen = 40 / NBBY;
1379 		*cipher = IEEE80211_CIPHER_WEP;
1380 		break;
1381 	case RSN_SEL(RSN_CSE_WEP104):
1382 		if (keylen)
1383 			*keylen = 104 / NBBY;
1384 		*cipher = IEEE80211_CIPHER_WEP;
1385 		break;
1386 	case RSN_SEL(RSN_CSE_TKIP):
1387 		*cipher = IEEE80211_CIPHER_TKIP;
1388 		break;
1389 	case RSN_SEL(RSN_CSE_CCMP):
1390 		*cipher = IEEE80211_CIPHER_AES_CCM;
1391 		break;
1392 	case RSN_SEL(RSN_CSE_WRAP):
1393 		*cipher = IEEE80211_CIPHER_AES_OCB;
1394 		break;
1395 	default:
1396 		return (EINVAL);
1397 	}
1398 
1399 	return (0);
1400 #undef WPA_SEL
1401 }
1402 
1403 /*
1404  * Convert an RSN key management/authentication algorithm
1405  * to an internal code.
1406  */
1407 static int
rsn_keymgmt(const uint8_t * sel)1408 rsn_keymgmt(const uint8_t *sel)
1409 {
1410 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1411 	uint32_t w = le32dec(sel);
1412 
1413 	switch (w) {
1414 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1415 		return RSN_ASE_8021X_UNSPEC;
1416 	case RSN_SEL(RSN_ASE_8021X_PSK):
1417 		return RSN_ASE_8021X_PSK;
1418 	case RSN_SEL(RSN_ASE_NONE):
1419 		return RSN_ASE_NONE;
1420 	}
1421 	return 0;		/* NB: so is discarded */
1422 #undef RSN_SEL
1423 }
1424 
1425 /*
1426  * Parse a WPA/RSN information element to collect parameters
1427  * and validate the parameters against what has been
1428  * configured for the system.
1429  */
1430 static int
ieee80211_parse_rsn(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)1431 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1432 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1433 {
1434 	uint8_t len = frm[1];
1435 	uint32_t w;
1436 	int error, n;
1437 
1438 	/*
1439 	 * Check the length once for fixed parts:
1440 	 * version, mcast cipher, and 2 selector counts.
1441 	 * Other, variable-length data, must be checked separately.
1442 	 */
1443 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1444 		IEEE80211_DISCARD_IE(vap,
1445 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1446 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1447 		return IEEE80211_REASON_IE_INVALID;
1448 	}
1449 	/* XXX may be shorter */
1450 	if (len < 10) {
1451 		IEEE80211_DISCARD_IE(vap,
1452 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1453 		    wh, "RSN", "too short, len %u", len);
1454 		return IEEE80211_REASON_IE_INVALID;
1455 	}
1456 	frm += 2;
1457 	w = le16dec(frm);
1458 	if (w != RSN_VERSION) {
1459 		IEEE80211_DISCARD_IE(vap,
1460 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1461 		    wh, "RSN", "bad version %u", w);
1462 		return IEEE80211_REASON_UNSUPP_RSN_IE_VERSION;
1463 	}
1464 	frm += 2, len -= 2;
1465 
1466 	memset(rsn, 0, sizeof(*rsn));
1467 
1468 	/* multicast/group cipher */
1469 	error = rsn_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
1470 	if (error != 0) {
1471 		IEEE80211_DISCARD_IE(vap,
1472 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1473 		    wh, "RSN", "unknown mcast cipher suite %08X",
1474 		    le32dec(frm));
1475 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1476 	}
1477 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_NONE) {
1478 		IEEE80211_DISCARD_IE(vap,
1479 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1480 		    wh, "RSN", "invalid mcast cipher suite %d",
1481 		    rsn->rsn_mcastcipher);
1482 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1483 	}
1484 	frm += 4, len -= 4;
1485 
1486 	/* unicast ciphers */
1487 	n = le16dec(frm);
1488 	frm += 2, len -= 2;
1489 	if (len < n*4+2) {
1490 		IEEE80211_DISCARD_IE(vap,
1491 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1492 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1493 		    len, n);
1494 		return IEEE80211_REASON_IE_INVALID;
1495 	}
1496 	w = 0;
1497 
1498 	for (; n > 0; n--) {
1499 		uint8_t cipher;
1500 
1501 		error = rsn_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
1502 		if (error == 0)
1503 			w |= 1 << cipher;
1504 
1505 		frm += 4, len -= 4;
1506 	}
1507         if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1508                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1509 	else if (w & (1 << IEEE80211_CIPHER_AES_OCB))
1510 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_OCB;
1511 	else if (w & (1 << IEEE80211_CIPHER_TKIP))
1512 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1513 	else if ((w & (1 << IEEE80211_CIPHER_NONE)) &&
1514 	    (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP ||
1515 	     rsn->rsn_mcastcipher == IEEE80211_CIPHER_TKIP))
1516 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_NONE;
1517 	else {
1518 		IEEE80211_DISCARD_IE(vap,
1519 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1520 		    wh, "RSN", "no usable pairwise cipher suite found (w=%d)",
1521 		    w);
1522 		return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
1523 	}
1524 
1525 	/* key management algorithms */
1526 	n = le16dec(frm);
1527 	frm += 2, len -= 2;
1528 	if (len < n*4) {
1529 		IEEE80211_DISCARD_IE(vap,
1530 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1531 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1532 		    len, n);
1533 		return IEEE80211_REASON_IE_INVALID;
1534 	}
1535 	w = 0;
1536 	for (; n > 0; n--) {
1537 		w |= rsn_keymgmt(frm);
1538 		frm += 4, len -= 4;
1539 	}
1540 	if (w & RSN_ASE_8021X_UNSPEC)
1541 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1542 	else
1543 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1544 
1545 	/* optional RSN capabilities */
1546 	if (len > 2)
1547 		rsn->rsn_caps = le16dec(frm);
1548 	/* XXXPMKID */
1549 
1550 	return 0;
1551 }
1552 
1553 /*
1554  * WPA/802.11i association request processing.
1555  */
1556 static int
wpa_assocreq(struct ieee80211_node * ni,struct ieee80211_rsnparms * rsnparms,const struct ieee80211_frame * wh,const uint8_t * wpa,const uint8_t * rsn,uint16_t capinfo)1557 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1558 	const struct ieee80211_frame *wh, const uint8_t *wpa,
1559 	const uint8_t *rsn, uint16_t capinfo)
1560 {
1561 	struct ieee80211vap *vap = ni->ni_vap;
1562 	uint8_t reason;
1563 	int badwparsn;
1564 
1565 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1566 	if (wpa == NULL && rsn == NULL) {
1567 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1568 			/*
1569 			 * W-Fi Protected Setup (WPS) permits
1570 			 * clients to associate and pass EAPOL frames
1571 			 * to establish initial credentials.
1572 			 */
1573 			ni->ni_flags |= IEEE80211_NODE_WPS;
1574 			return 1;
1575 		}
1576 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1577 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1578 			/*
1579 			 * Transitional Security Network.  Permits clients
1580 			 * to associate and use WEP while WPA is configured.
1581 			 */
1582 			ni->ni_flags |= IEEE80211_NODE_TSN;
1583 			return 1;
1584 		}
1585 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1586 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1587 		vap->iv_stats.is_rx_assoc_badwpaie++;
1588 		reason = IEEE80211_REASON_IE_INVALID;
1589 		goto bad;
1590 	}
1591 	/* assert right association security credentials */
1592 	badwparsn = 0;			/* NB: to silence compiler */
1593 	switch (vap->iv_flags & IEEE80211_F_WPA) {
1594 	case IEEE80211_F_WPA1:
1595 		badwparsn = (wpa == NULL);
1596 		break;
1597 	case IEEE80211_F_WPA2:
1598 		badwparsn = (rsn == NULL);
1599 		break;
1600 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1601 		badwparsn = (wpa == NULL && rsn == NULL);
1602 		break;
1603 	}
1604 	if (badwparsn) {
1605 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1606 		    wh, NULL,
1607 		    "%s", "missing WPA/RSN IE in association request");
1608 		vap->iv_stats.is_rx_assoc_badwpaie++;
1609 		reason = IEEE80211_REASON_IE_INVALID;
1610 		goto bad;
1611 	}
1612 	/*
1613 	 * Parse WPA/RSN information element.
1614 	 */
1615 	if (wpa != NULL)
1616 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1617 	else
1618 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1619 	if (reason != 0) {
1620 		/* XXX wpa->rsn fallback? */
1621 		/* XXX distinguish WPA/RSN? */
1622 		vap->iv_stats.is_rx_assoc_badwpaie++;
1623 		goto bad;
1624 	}
1625 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1626 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1627 	    wpa != NULL ? "WPA" : "RSN",
1628 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1629 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1630 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1631 
1632 	return 1;
1633 bad:
1634 	ieee80211_node_deauth(ni, reason);
1635 	return 0;
1636 }
1637 
1638 /* XXX find a better place for definition */
1639 struct l2_update_frame {
1640 	struct ether_header eh;
1641 	uint8_t dsap;
1642 	uint8_t ssap;
1643 	uint8_t control;
1644 	uint8_t xid[3];
1645 }  __packed;
1646 
1647 /*
1648  * Deliver a TGf L2UF frame on behalf of a station.
1649  * This primes any bridge when the station is roaming
1650  * between ap's on the same wired network.
1651  */
1652 static void
ieee80211_deliver_l2uf(struct ieee80211_node * ni)1653 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1654 {
1655 	struct ieee80211vap *vap = ni->ni_vap;
1656 	struct ifnet *ifp = vap->iv_ifp;
1657 	struct mbuf *m;
1658 	struct l2_update_frame *l2uf;
1659 	struct ether_header *eh;
1660 
1661 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
1662 	if (m == NULL) {
1663 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1664 		    "%s", "no mbuf for l2uf frame");
1665 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1666 		return;
1667 	}
1668 	l2uf = mtod(m, struct l2_update_frame *);
1669 	eh = &l2uf->eh;
1670 	/* dst: Broadcast address */
1671 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1672 	/* src: associated STA */
1673 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1674 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1675 
1676 	l2uf->dsap = 0;
1677 	l2uf->ssap = 0;
1678 	l2uf->control = 0xf5;
1679 	l2uf->xid[0] = 0x81;
1680 	l2uf->xid[1] = 0x80;
1681 	l2uf->xid[2] = 0x00;
1682 
1683 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1684 	hostap_deliver_data(vap, ni, m);
1685 }
1686 
1687 static void
ratesetmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int rate)1688 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1689 	int reassoc, int resp, const char *tag, int rate)
1690 {
1691 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1692 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1693 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1694 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1695 	ieee80211_node_leave(ni);
1696 }
1697 
1698 static void
capinfomismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int capinfo)1699 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1700 	int reassoc, int resp, const char *tag, int capinfo)
1701 {
1702 	struct ieee80211vap *vap = ni->ni_vap;
1703 
1704 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1705 	    "deny %s request, %s mismatch 0x%x",
1706 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1707 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1708 	ieee80211_node_leave(ni);
1709 	vap->iv_stats.is_rx_assoc_capmismatch++;
1710 }
1711 
1712 static void
htcapmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp)1713 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1714 	int reassoc, int resp)
1715 {
1716 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1717 	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1718 	/* XXX no better code */
1719 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1720 	ieee80211_node_leave(ni);
1721 }
1722 
1723 static void
authalgreject(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int algo,int seq,int status)1724 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1725 	int algo, int seq, int status)
1726 {
1727 	struct ieee80211vap *vap = ni->ni_vap;
1728 
1729 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1730 	    wh, NULL, "unsupported alg %d", algo);
1731 	vap->iv_stats.is_rx_auth_unsupported++;
1732 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1733 	    seq | (status << 16));
1734 }
1735 
1736 static __inline int
ishtmixed(const uint8_t * ie)1737 ishtmixed(const uint8_t *ie)
1738 {
1739 	const struct ieee80211_ie_htinfo *ht =
1740 	    (const struct ieee80211_ie_htinfo *) ie;
1741 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1742 	    IEEE80211_HTINFO_OPMODE_MIXED;
1743 }
1744 
1745 static int
is11bclient(const uint8_t * rates,const uint8_t * xrates)1746 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1747 {
1748 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1749 	int i;
1750 
1751 	/* NB: the 11b clients we care about will not have xrates */
1752 	if (xrates != NULL || rates == NULL)
1753 		return 0;
1754 	for (i = 0; i < rates[1]; i++) {
1755 		int r = rates[2+i] & IEEE80211_RATE_VAL;
1756 		if (r > 2*11 || ((1<<r) & brates) == 0)
1757 			return 0;
1758 	}
1759 	return 1;
1760 }
1761 
1762 static void
hostap_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)1763 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1764 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1765 {
1766 	struct ieee80211vap *vap = ni->ni_vap;
1767 	struct ieee80211com *ic = ni->ni_ic;
1768 	struct ieee80211_frame *wh;
1769 	uint8_t *frm, *efrm, *sfrm;
1770 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1771 	uint8_t *vhtcap, *vhtinfo;
1772 	int reassoc, resp;
1773 	uint8_t rate;
1774 
1775 	wh = mtod(m0, struct ieee80211_frame *);
1776 	frm = (uint8_t *)&wh[1];
1777 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1778 	switch (subtype) {
1779 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1780 		/*
1781 		 * We process beacon/probe response frames when scanning;
1782 		 * otherwise we check beacon frames for overlapping non-ERP
1783 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1784 		 */
1785 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1786 			vap->iv_stats.is_rx_mgtdiscard++;
1787 			return;
1788 		}
1789 		/* FALLTHROUGH */
1790 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1791 		struct ieee80211_scanparams scan;
1792 
1793 		/* NB: accept off-channel frames */
1794 		/* XXX TODO: use rxstatus to determine off-channel details */
1795 		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1796 			return;
1797 		/*
1798 		 * Count frame now that we know it's to be processed.
1799 		 */
1800 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1801 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1802 			IEEE80211_NODE_STAT(ni, rx_beacons);
1803 		} else
1804 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1805 		/*
1806 		 * If scanning, just pass information to the scan module.
1807 		 */
1808 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1809 			if (scan.status == 0 &&		/* NB: on channel */
1810 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1811 				/*
1812 				 * Actively scanning a channel marked passive;
1813 				 * send a probe request now that we know there
1814 				 * is 802.11 traffic present.
1815 				 *
1816 				 * XXX check if the beacon we recv'd gives
1817 				 * us what we need and suppress the probe req
1818 				 */
1819 				ieee80211_probe_curchan(vap, true);
1820 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1821 			}
1822 			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1823 			    subtype, rssi, nf);
1824 			return;
1825 		}
1826 		/*
1827 		 * Check beacon for overlapping bss w/ non ERP stations.
1828 		 * If we detect one and protection is configured but not
1829 		 * enabled, enable it and start a timer that'll bring us
1830 		 * out if we stop seeing the bss.
1831 		 */
1832 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1833 		    scan.status == 0 &&			/* NB: on-channel */
1834 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1835 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1836 			vap->iv_lastnonerp = ticks;
1837 			vap->iv_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1838 			/*
1839 			 * XXX TODO: this may need to check all VAPs?
1840 			 */
1841 			if (vap->iv_protmode != IEEE80211_PROT_NONE &&
1842 			    (vap->iv_flags & IEEE80211_F_USEPROT) == 0) {
1843 				IEEE80211_NOTE_FRAME(vap,
1844 				    IEEE80211_MSG_ASSOC, wh,
1845 				    "non-ERP present on channel %d "
1846 				    "(saw erp 0x%x from channel %d), "
1847 				    "enable use of protection",
1848 				    ic->ic_curchan->ic_ieee,
1849 				    scan.erp, scan.chan);
1850 				vap->iv_flags |= IEEE80211_F_USEPROT;
1851 				ieee80211_vap_update_erp_protmode(vap);
1852 			}
1853 		}
1854 		/*
1855 		 * Check beacon for non-HT station on HT channel
1856 		 * and update HT BSS occupancy as appropriate.
1857 		 */
1858 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1859 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1860 				/*
1861 				 * Off control channel; only check frames
1862 				 * that come in the extension channel when
1863 				 * operating w/ HT40.
1864 				 */
1865 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1866 					break;
1867 				if (scan.chan != ic->ic_curchan->ic_extieee)
1868 					break;
1869 			}
1870 			if (scan.htinfo == NULL) {
1871 				ieee80211_htprot_update(vap,
1872 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1873 				    IEEE80211_HTINFO_NONHT_PRESENT);
1874 			} else if (ishtmixed(scan.htinfo)) {
1875 				/* XXX? take NONHT_PRESENT from beacon? */
1876 				ieee80211_htprot_update(vap,
1877 				    IEEE80211_HTINFO_OPMODE_MIXED |
1878 				    IEEE80211_HTINFO_NONHT_PRESENT);
1879 			}
1880 		}
1881 		break;
1882 	}
1883 
1884 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1885 		if (vap->iv_state != IEEE80211_S_RUN) {
1886 			vap->iv_stats.is_rx_mgtdiscard++;
1887 			return;
1888 		}
1889 		/*
1890 		 * Consult the ACL policy module if setup.
1891 		 */
1892 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1893 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1894 			    wh, NULL, "%s", "disallowed by ACL");
1895 			vap->iv_stats.is_rx_acl++;
1896 			return;
1897 		}
1898 		/*
1899 		 * prreq frame format
1900 		 *	[tlv] ssid
1901 		 *	[tlv] supported rates
1902 		 *	[tlv] extended supported rates
1903 		 */
1904 		ssid = rates = xrates = NULL;
1905 		while (efrm - frm > 1) {
1906 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1907 			switch (*frm) {
1908 			case IEEE80211_ELEMID_SSID:
1909 				ssid = frm;
1910 				break;
1911 			case IEEE80211_ELEMID_RATES:
1912 				rates = frm;
1913 				break;
1914 			case IEEE80211_ELEMID_XRATES:
1915 				xrates = frm;
1916 				break;
1917 			}
1918 			frm += frm[1] + 2;
1919 		}
1920 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1921 		if (xrates != NULL)
1922 			IEEE80211_VERIFY_ELEMENT(xrates,
1923 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1924 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1925 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1926 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1927 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1928 			    wh, NULL,
1929 			    "%s", "no ssid with ssid suppression enabled");
1930 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1931 			return;
1932 		}
1933 
1934 		/* XXX find a better class or define it's own */
1935 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1936 		    "%s", "recv probe req");
1937 		/*
1938 		 * Some legacy 11b clients cannot hack a complete
1939 		 * probe response frame.  When the request includes
1940 		 * only a bare-bones rate set, communicate this to
1941 		 * the transmit side.
1942 		 */
1943 		ieee80211_send_proberesp(vap, wh->i_addr2,
1944 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1945 		break;
1946 
1947 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1948 		uint16_t algo, seq, status;
1949 
1950 		if (vap->iv_state != IEEE80211_S_RUN) {
1951 			vap->iv_stats.is_rx_mgtdiscard++;
1952 			return;
1953 		}
1954 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1955 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1956 			    wh, NULL, "%s", "wrong bssid");
1957 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1958 			return;
1959 		}
1960 		/*
1961 		 * auth frame format
1962 		 *	[2] algorithm
1963 		 *	[2] sequence
1964 		 *	[2] status
1965 		 *	[tlv*] challenge
1966 		 */
1967 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1968 		algo   = le16toh(*(uint16_t *)frm);
1969 		seq    = le16toh(*(uint16_t *)(frm + 2));
1970 		status = le16toh(*(uint16_t *)(frm + 4));
1971 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1972 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1973 		/*
1974 		 * Consult the ACL policy module if setup.
1975 		 */
1976 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1977 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1978 			    wh, NULL, "%s", "disallowed by ACL");
1979 			vap->iv_stats.is_rx_acl++;
1980 			ieee80211_send_error(ni, wh->i_addr2,
1981 			    IEEE80211_FC0_SUBTYPE_AUTH,
1982 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1983 			return;
1984 		}
1985 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1986 			IEEE80211_DISCARD(vap,
1987 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1988 			    wh, NULL, "%s", "TKIP countermeasures enabled");
1989 			vap->iv_stats.is_rx_auth_countermeasures++;
1990 			ieee80211_send_error(ni, wh->i_addr2,
1991 				IEEE80211_FC0_SUBTYPE_AUTH,
1992 				IEEE80211_REASON_MIC_FAILURE);
1993 			return;
1994 		}
1995 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1996 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1997 			    seq, status);
1998 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1999 			hostap_auth_open(ni, wh, rssi, nf, seq, status);
2000 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
2001 			authalgreject(ni, wh, algo,
2002 			    seq+1, IEEE80211_STATUS_ALG);
2003 			return;
2004 		} else {
2005 			/*
2006 			 * We assume that an unknown algorithm is the result
2007 			 * of a decryption failure on a shared key auth frame;
2008 			 * return a status code appropriate for that instead
2009 			 * of IEEE80211_STATUS_ALG.
2010 			 *
2011 			 * NB: a seq# of 4 is intentional; the decrypted
2012 			 *     frame likely has a bogus seq value.
2013 			 */
2014 			authalgreject(ni, wh, algo,
2015 			    4, IEEE80211_STATUS_CHALLENGE);
2016 			return;
2017 		}
2018 		break;
2019 	}
2020 
2021 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2022 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
2023 		uint16_t capinfo, lintval;
2024 		struct ieee80211_rsnparms rsnparms;
2025 
2026 		if (vap->iv_state != IEEE80211_S_RUN) {
2027 			vap->iv_stats.is_rx_mgtdiscard++;
2028 			return;
2029 		}
2030 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
2031 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2032 			    wh, NULL, "%s", "wrong bssid");
2033 			vap->iv_stats.is_rx_assoc_bss++;
2034 			return;
2035 		}
2036 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2037 			reassoc = 1;
2038 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
2039 		} else {
2040 			reassoc = 0;
2041 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
2042 		}
2043 		if (ni == vap->iv_bss) {
2044 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
2045 			    "deny %s request, sta not authenticated",
2046 			    reassoc ? "reassoc" : "assoc");
2047 			ieee80211_send_error(ni, wh->i_addr2,
2048 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2049 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
2050 			vap->iv_stats.is_rx_assoc_notauth++;
2051 			return;
2052 		}
2053 
2054 		/*
2055 		 * asreq frame format
2056 		 *	[2] capability information
2057 		 *	[2] listen interval
2058 		 *	[6*] current AP address (reassoc only)
2059 		 *	[tlv] ssid
2060 		 *	[tlv] supported rates
2061 		 *	[tlv] extended supported rates
2062 		 *	[tlv] WPA or RSN
2063 		 *	[tlv] HT capabilities
2064 		 *	[tlv] Atheros capabilities
2065 		 */
2066 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
2067 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
2068 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
2069 		if (reassoc)
2070 			frm += 6;	/* ignore current AP info */
2071 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
2072 		vhtcap = vhtinfo = NULL;
2073 		sfrm = frm;
2074 		while (efrm - frm > 1) {
2075 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2076 			switch (*frm) {
2077 			case IEEE80211_ELEMID_SSID:
2078 				ssid = frm;
2079 				break;
2080 			case IEEE80211_ELEMID_RATES:
2081 				rates = frm;
2082 				break;
2083 			case IEEE80211_ELEMID_XRATES:
2084 				xrates = frm;
2085 				break;
2086 			case IEEE80211_ELEMID_RSN:
2087 				rsn = frm;
2088 				break;
2089 			case IEEE80211_ELEMID_HTCAP:
2090 				htcap = frm;
2091 				break;
2092 			case IEEE80211_ELEMID_VHT_CAP:
2093 				vhtcap = frm;
2094 				break;
2095 			case IEEE80211_ELEMID_VHT_OPMODE:
2096 				vhtinfo = frm;
2097 				break;
2098 			case IEEE80211_ELEMID_VENDOR:
2099 				if (iswpaoui(frm))
2100 					wpa = frm;
2101 				else if (iswmeinfo(frm))
2102 					wme = frm;
2103 #ifdef IEEE80211_SUPPORT_SUPERG
2104 				else if (isatherosoui(frm))
2105 					ath = frm;
2106 #endif
2107 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2108 					if (ishtcapoui(frm) && htcap == NULL)
2109 						htcap = frm;
2110 				}
2111 				break;
2112 			}
2113 			frm += frm[1] + 2;
2114 		}
2115 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2116 		if (xrates != NULL)
2117 			IEEE80211_VERIFY_ELEMENT(xrates,
2118 				IEEE80211_RATE_MAXSIZE - rates[1], return);
2119 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2120 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2121 		if (htcap != NULL) {
2122 			IEEE80211_VERIFY_LENGTH(htcap[1],
2123 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2124 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2125 			         sizeof(struct ieee80211_ie_htcap)-2,
2126 			     return);		/* XXX just NULL out? */
2127 		}
2128 
2129 		/* Validate VHT IEs */
2130 		if (vhtcap != NULL) {
2131 			IEEE80211_VERIFY_LENGTH(vhtcap[1],
2132 			    sizeof(struct ieee80211_vht_cap),
2133 			    return);
2134 		}
2135 		if (vhtinfo != NULL) {
2136 			IEEE80211_VERIFY_LENGTH(vhtinfo[1],
2137 			    sizeof(struct ieee80211_vht_operation),
2138 			    return);
2139 		}
2140 
2141 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2142 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2143 			return;
2144 		/* discard challenge after association */
2145 		if (ni->ni_challenge != NULL) {
2146 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2147 			ni->ni_challenge = NULL;
2148 		}
2149 		/* NB: 802.11 spec says to ignore station's privacy bit */
2150 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2151 			capinfomismatch(ni, wh, reassoc, resp,
2152 			    "capability", capinfo);
2153 			return;
2154 		}
2155 		/*
2156 		 * Disallow re-associate w/ invalid slot time setting.
2157 		 */
2158 		if (ni->ni_associd != 0 &&
2159 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2160 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2161 			capinfomismatch(ni, wh, reassoc, resp,
2162 			    "slot time", capinfo);
2163 			return;
2164 		}
2165 		rate = ieee80211_setup_rates(ni, rates, xrates,
2166 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2167 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2168 		if (rate & IEEE80211_RATE_BASIC) {
2169 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2170 			vap->iv_stats.is_rx_assoc_norate++;
2171 			return;
2172 		}
2173 		/*
2174 		 * If constrained to 11g-only stations reject an
2175 		 * 11b-only station.  We cheat a bit here by looking
2176 		 * at the max negotiated xmit rate and assuming anyone
2177 		 * with a best rate <24Mb/s is an 11b station.
2178 		 */
2179 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2180 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2181 			vap->iv_stats.is_rx_assoc_norate++;
2182 			return;
2183 		}
2184 
2185 		/*
2186 		 * Do HT rate set handling and setup HT node state.
2187 		 */
2188 		ni->ni_chan = vap->iv_bss->ni_chan;
2189 
2190 		/* VHT */
2191 		if (IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
2192 		    vhtcap != NULL &&
2193 		    vhtinfo != NULL) {
2194 			/* XXX TODO; see below */
2195 			printf("%s: VHT TODO!\n", __func__);
2196 			ieee80211_vht_node_init(ni);
2197 			ieee80211_vht_update_cap(ni, vhtcap, vhtinfo);
2198 		} else if (ni->ni_flags & IEEE80211_NODE_VHT)
2199 			ieee80211_vht_node_cleanup(ni);
2200 
2201 		/* HT */
2202 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2203 			rate = ieee80211_setup_htrates(ni, htcap,
2204 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2205 				IEEE80211_F_DOBRS);
2206 			if (rate & IEEE80211_RATE_BASIC) {
2207 				ratesetmismatch(ni, wh, reassoc, resp,
2208 				    "HT", rate);
2209 				vap->iv_stats.is_ht_assoc_norate++;
2210 				return;
2211 			}
2212 			ieee80211_ht_node_init(ni);
2213 			ieee80211_ht_updatehtcap(ni, htcap);
2214 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2215 			ieee80211_ht_node_cleanup(ni);
2216 
2217 		/* Finally - this will use HT/VHT info to change node channel */
2218 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2219 			ieee80211_ht_updatehtcap_final(ni);
2220 		}
2221 
2222 #ifdef IEEE80211_SUPPORT_SUPERG
2223 		/* Always do ff node cleanup; for A-MSDU */
2224 		ieee80211_ff_node_cleanup(ni);
2225 #endif
2226 		/*
2227 		 * Allow AMPDU operation only with unencrypted traffic
2228 		 * or AES-CCM; the 11n spec only specifies these ciphers
2229 		 * so permitting any others is undefined and can lead
2230 		 * to interoperability problems.
2231 		 */
2232 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2233 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2234 		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2235 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2236 			IEEE80211_NOTE(vap,
2237 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2238 			    "disallow HT use because WEP or TKIP requested, "
2239 			    "capinfo 0x%x ucastcipher %d", capinfo,
2240 			    rsnparms.rsn_ucastcipher);
2241 			ieee80211_ht_node_cleanup(ni);
2242 #ifdef IEEE80211_SUPPORT_SUPERG
2243 			/* Always do ff node cleanup; for A-MSDU */
2244 			ieee80211_ff_node_cleanup(ni);
2245 #endif
2246 			vap->iv_stats.is_ht_assoc_downgrade++;
2247 		}
2248 		/*
2249 		 * If constrained to 11n-only stations reject legacy stations.
2250 		 */
2251 		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2252 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2253 			htcapmismatch(ni, wh, reassoc, resp);
2254 			vap->iv_stats.is_ht_assoc_nohtcap++;
2255 			return;
2256 		}
2257 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2258 		ni->ni_noise = nf;
2259 		ni->ni_intval = lintval;
2260 		ni->ni_capinfo = capinfo;
2261 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2262 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2263 		/*
2264 		 * Store the IEs.
2265 		 * XXX maybe better to just expand
2266 		 */
2267 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2268 #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2269 			if (wpa != NULL)
2270 				setie(wpa_ie, wpa - sfrm);
2271 			if (rsn != NULL)
2272 				setie(rsn_ie, rsn - sfrm);
2273 			if (htcap != NULL)
2274 				setie(htcap_ie, htcap - sfrm);
2275 			if (wme != NULL) {
2276 				setie(wme_ie, wme - sfrm);
2277 				/*
2278 				 * Mark node as capable of QoS.
2279 				 */
2280 				ni->ni_flags |= IEEE80211_NODE_QOS;
2281 				if (ieee80211_parse_wmeie(wme, wh, ni) > 0) {
2282 					if (ni->ni_uapsd != 0)
2283 						ni->ni_flags |=
2284 						    IEEE80211_NODE_UAPSD;
2285 					else
2286 						ni->ni_flags &=
2287 						    ~IEEE80211_NODE_UAPSD;
2288 				}
2289 			} else
2290 				ni->ni_flags &=
2291 				    ~(IEEE80211_NODE_QOS |
2292 				      IEEE80211_NODE_UAPSD);
2293 #ifdef IEEE80211_SUPPORT_SUPERG
2294 			if (ath != NULL) {
2295 				setie(ath_ie, ath - sfrm);
2296 				/*
2297 				 * Parse ATH station parameters.
2298 				 */
2299 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2300 			} else
2301 #endif
2302 				ni->ni_ath_flags = 0;
2303 #undef setie
2304 		} else {
2305 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2306 			ni->ni_flags &= ~IEEE80211_NODE_UAPSD;
2307 			ni->ni_ath_flags = 0;
2308 		}
2309 		ieee80211_node_join(ni, resp);
2310 		ieee80211_deliver_l2uf(ni);
2311 		break;
2312 	}
2313 
2314 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2315 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2316 #ifdef IEEE80211_DEBUG
2317 		uint16_t reason;
2318 #endif
2319 
2320 		if (vap->iv_state != IEEE80211_S_RUN ||
2321 		    /* NB: can happen when in promiscuous mode */
2322 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2323 			vap->iv_stats.is_rx_mgtdiscard++;
2324 			break;
2325 		}
2326 		/*
2327 		 * deauth/disassoc frame format
2328 		 *	[2] reason
2329 		 */
2330 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2331 #ifdef IEEE80211_DEBUG
2332 		reason = le16toh(*(uint16_t *)frm);
2333 #endif
2334 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2335 			vap->iv_stats.is_rx_deauth++;
2336 			IEEE80211_NODE_STAT(ni, rx_deauth);
2337 		} else {
2338 			vap->iv_stats.is_rx_disassoc++;
2339 			IEEE80211_NODE_STAT(ni, rx_disassoc);
2340 		}
2341 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2342 		    "recv %s (reason: %d (%s))",
2343 		    ieee80211_mgt_subtype_name(subtype),
2344 		    reason, ieee80211_reason_to_string(reason));
2345 		if (ni != vap->iv_bss)
2346 			ieee80211_node_leave(ni);
2347 		break;
2348 	}
2349 
2350 	case IEEE80211_FC0_SUBTYPE_ACTION:
2351 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2352 		if (ni == vap->iv_bss) {
2353 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2354 			    wh, NULL, "%s", "unknown node");
2355 			vap->iv_stats.is_rx_mgtdiscard++;
2356 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2357 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2358 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2359 			    wh, NULL, "%s", "not for us");
2360 			vap->iv_stats.is_rx_mgtdiscard++;
2361 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2362 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2363 			    wh, NULL, "wrong state %s",
2364 			    ieee80211_state_name[vap->iv_state]);
2365 			vap->iv_stats.is_rx_mgtdiscard++;
2366 		} else {
2367 			if (ieee80211_parse_action(ni, m0) == 0)
2368 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2369 		}
2370 		break;
2371 
2372 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2373 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2374 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2375 	case IEEE80211_FC0_SUBTYPE_ATIM:
2376 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2377 		    wh, NULL, "%s", "not handled");
2378 		vap->iv_stats.is_rx_mgtdiscard++;
2379 		break;
2380 
2381 	default:
2382 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2383 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2384 		vap->iv_stats.is_rx_badsubtype++;
2385 		break;
2386 	}
2387 }
2388 
2389 static void
hostap_recv_ctl(struct ieee80211_node * ni,struct mbuf * m,int subtype)2390 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2391 {
2392 	switch (subtype) {
2393 	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2394 		ni->ni_vap->iv_recv_pspoll(ni, m);
2395 		break;
2396 	case IEEE80211_FC0_SUBTYPE_BAR:
2397 		ieee80211_recv_bar(ni, m);
2398 		break;
2399 	}
2400 }
2401 
2402 /*
2403  * Process a received ps-poll frame.
2404  */
2405 void
ieee80211_recv_pspoll(struct ieee80211_node * ni,struct mbuf * m0)2406 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2407 {
2408 	struct ieee80211vap *vap = ni->ni_vap;
2409 	struct ieee80211com *ic = vap->iv_ic;
2410 	struct ieee80211_frame_min *wh;
2411 	struct mbuf *m;
2412 	uint16_t aid;
2413 	int qlen;
2414 
2415 	wh = mtod(m0, struct ieee80211_frame_min *);
2416 	if (ni->ni_associd == 0) {
2417 		IEEE80211_DISCARD(vap,
2418 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2419 		    (struct ieee80211_frame *) wh, NULL,
2420 		    "%s", "unassociated station");
2421 		vap->iv_stats.is_ps_unassoc++;
2422 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2423 			IEEE80211_REASON_NOT_ASSOCED);
2424 		return;
2425 	}
2426 
2427 	aid = le16toh(*(uint16_t *)wh->i_dur);
2428 	if (aid != ni->ni_associd) {
2429 		IEEE80211_DISCARD(vap,
2430 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2431 		    (struct ieee80211_frame *) wh, NULL,
2432 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2433 		    ni->ni_associd, aid);
2434 		vap->iv_stats.is_ps_badaid++;
2435 		/*
2436 		 * NB: We used to deauth the station but it turns out
2437 		 * the Blackberry Curve 8230 (and perhaps other devices)
2438 		 * sometimes send the wrong AID when WME is negotiated.
2439 		 * Being more lenient here seems ok as we already check
2440 		 * the station is associated and we only return frames
2441 		 * queued for the station (i.e. we don't use the AID).
2442 		 */
2443 		return;
2444 	}
2445 
2446 	/* Okay, take the first queued packet and put it out... */
2447 	m = ieee80211_node_psq_dequeue(ni, &qlen);
2448 	if (m == NULL) {
2449 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2450 		    "%s", "recv ps-poll, but queue empty");
2451 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2452 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2453 		if (vap->iv_set_tim != NULL)
2454 			vap->iv_set_tim(ni, 0);	/* just in case */
2455 		return;
2456 	}
2457 	/*
2458 	 * If there are more packets, set the more packets bit
2459 	 * in the packet dispatched to the station; otherwise
2460 	 * turn off the TIM bit.
2461 	 */
2462 	if (qlen != 0) {
2463 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2464 		    "recv ps-poll, send packet, %u still queued", qlen);
2465 		m->m_flags |= M_MORE_DATA;
2466 	} else {
2467 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2468 		    "%s", "recv ps-poll, send packet, queue empty");
2469 		if (vap->iv_set_tim != NULL)
2470 			vap->iv_set_tim(ni, 0);
2471 	}
2472 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2473 
2474 	/*
2475 	 * Do the right thing; if it's an encap'ed frame then
2476 	 * call ieee80211_parent_xmitpkt() else
2477 	 * call ieee80211_vap_xmitpkt().
2478 	 */
2479 	if (m->m_flags & M_ENCAP) {
2480 		(void) ieee80211_parent_xmitpkt(ic, m);
2481 	} else {
2482 		(void) ieee80211_vap_xmitpkt(vap, m);
2483 	}
2484 }
2485