1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11 IBSS mode support.
33  */
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_media.h>
53 #include <net/if_llc.h>
54 #include <net/ethernet.h>
55 
56 #include <net/bpf.h>
57 
58 #include <netproto/802_11/ieee80211_var.h>
59 #include <netproto/802_11/ieee80211_adhoc.h>
60 #include <netproto/802_11/ieee80211_input.h>
61 #ifdef IEEE80211_SUPPORT_SUPERG
62 #include <netproto/802_11/ieee80211_superg.h>
63 #endif
64 #ifdef IEEE80211_SUPPORT_TDMA
65 #include <netproto/802_11/ieee80211_tdma.h>
66 #endif
67 #include <netproto/802_11/ieee80211_sta.h>
68 
69 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
70 
71 static	void adhoc_vattach(struct ieee80211vap *);
72 static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73 static int adhoc_input(struct ieee80211_node *, struct mbuf *,
74 	    const struct ieee80211_rx_stats *, int, int);
75 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 	int subtype, const struct ieee80211_rx_stats *, int, int);
77 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
78 	int subtype, const struct ieee80211_rx_stats *rxs, int, int);
79 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
80 
81 void
82 ieee80211_adhoc_attach(struct ieee80211com *ic)
83 {
84 	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
85 	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
86 }
87 
88 void
89 ieee80211_adhoc_detach(struct ieee80211com *ic)
90 {
91 }
92 
93 static void
94 adhoc_vdetach(struct ieee80211vap *vap)
95 {
96 }
97 
98 static void
99 adhoc_vattach(struct ieee80211vap *vap)
100 {
101 	vap->iv_newstate = adhoc_newstate;
102 	vap->iv_input = adhoc_input;
103 	if (vap->iv_opmode == IEEE80211_M_IBSS)
104 		vap->iv_recv_mgmt = adhoc_recv_mgmt;
105 	else
106 		vap->iv_recv_mgmt = ahdemo_recv_mgmt;
107 	vap->iv_recv_ctl = adhoc_recv_ctl;
108 	vap->iv_opdetach = adhoc_vdetach;
109 #ifdef IEEE80211_SUPPORT_TDMA
110 	/*
111 	 * Throw control to tdma support.  Note we do this
112 	 * after setting up our callbacks so it can piggyback
113 	 * on top of us.
114 	 */
115 	if (vap->iv_caps & IEEE80211_C_TDMA)
116 		ieee80211_tdma_vattach(vap);
117 #endif
118 }
119 
120 static void
121 sta_leave(void *arg, struct ieee80211_node *ni)
122 {
123 	struct ieee80211vap *vap = arg;
124 
125 	if (ni->ni_vap == vap && ni != vap->iv_bss)
126 		ieee80211_node_leave(ni);
127 }
128 
129 /*
130  * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
131  */
132 static int
133 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
134 {
135 	struct ieee80211com *ic = vap->iv_ic;
136 	struct ieee80211_node *ni;
137 	enum ieee80211_state ostate;
138 
139 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
140 
141 	ostate = vap->iv_state;
142 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
143 	    __func__, ieee80211_state_name[ostate],
144 	    ieee80211_state_name[nstate], arg);
145 	vap->iv_state = nstate;			/* state transition */
146 	if (ostate != IEEE80211_S_SCAN)
147 		ieee80211_cancel_scan(vap);	/* background scan */
148 	ni = vap->iv_bss;			/* NB: no reference held */
149 	switch (nstate) {
150 	case IEEE80211_S_INIT:
151 		switch (ostate) {
152 		case IEEE80211_S_SCAN:
153 			ieee80211_cancel_scan(vap);
154 			break;
155 		default:
156 			break;
157 		}
158 		if (ostate != IEEE80211_S_INIT) {
159 			/* NB: optimize INIT -> INIT case */
160 			ieee80211_reset_bss(vap);
161 		}
162 		break;
163 	case IEEE80211_S_SCAN:
164 		switch (ostate) {
165 		case IEEE80211_S_RUN:		/* beacon miss */
166 			/* purge station table; entries are stale */
167 			ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
168 			/* fall thru... */
169 		case IEEE80211_S_INIT:
170 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
171 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
172 				/*
173 				 * Already have a channel; bypass the
174 				 * scan and startup immediately.
175 				 */
176 				ieee80211_create_ibss(vap,
177 				    ieee80211_ht_adjust_channel(ic,
178 				    vap->iv_des_chan, vap->iv_flags_ht));
179 				break;
180 			}
181 			/*
182 			 * Initiate a scan.  We can come here as a result
183 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
184 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
185 			 * and the scan request parameters will be present
186 			 * in iv_scanreq.  Otherwise we do the default.
187 			 */
188 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
189 				ieee80211_check_scan(vap,
190 				    vap->iv_scanreq_flags,
191 				    vap->iv_scanreq_duration,
192 				    vap->iv_scanreq_mindwell,
193 				    vap->iv_scanreq_maxdwell,
194 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
195 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
196 			} else
197 				ieee80211_check_scan_current(vap);
198 			break;
199 		case IEEE80211_S_SCAN:
200 			/*
201 			 * This can happen because of a change in state
202 			 * that requires a reset.  Trigger a new scan
203 			 * unless we're in manual roaming mode in which
204 			 * case an application must issue an explicit request.
205 			 */
206 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
207 				ieee80211_check_scan_current(vap);
208 			break;
209 		default:
210 			goto invalid;
211 		}
212 		break;
213 	case IEEE80211_S_RUN:
214 		if (vap->iv_flags & IEEE80211_F_WPA) {
215 			/* XXX validate prerequisites */
216 		}
217 		switch (ostate) {
218 		case IEEE80211_S_SCAN:
219 #ifdef IEEE80211_DEBUG
220 			if (ieee80211_msg_debug(vap)) {
221 				ieee80211_note(vap,
222 				    "synchronized with %s ssid ",
223 				    ether_sprintf(ni->ni_bssid));
224 				ieee80211_print_essid(vap->iv_bss->ni_essid,
225 				    ni->ni_esslen);
226 				/* XXX MCS/HT */
227 				kprintf(" channel %d start %uMb\n",
228 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
229 				    IEEE80211_RATE2MBS(ni->ni_txrate));
230 			}
231 #endif
232 			break;
233 		case IEEE80211_S_RUN:	/* IBSS merge */
234 			break;
235 		default:
236 			goto invalid;
237 		}
238 		/*
239 		 * When 802.1x is not in use mark the port authorized
240 		 * at this point so traffic can flow.
241 		 */
242 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
243 			ieee80211_node_authorize(ni);
244 		/*
245 		 * Fake association when joining an existing bss.
246 		 */
247 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
248 		    ic->ic_newassoc != NULL)
249 			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
250 		break;
251 	case IEEE80211_S_SLEEP:
252 		vap->iv_sta_ps(vap, 0);
253 		break;
254 	default:
255 	invalid:
256 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
257 		    "%s: unexpected state transition %s -> %s\n", __func__,
258 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
259 		break;
260 	}
261 	return 0;
262 }
263 
264 /*
265  * Decide if a received management frame should be
266  * printed when debugging is enabled.  This filters some
267  * of the less interesting frames that come frequently
268  * (e.g. beacons).
269  */
270 static __inline int
271 doprint(struct ieee80211vap *vap, int subtype)
272 {
273 	switch (subtype) {
274 	case IEEE80211_FC0_SUBTYPE_BEACON:
275 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
276 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
277 		return 1;
278 	}
279 	return 1;
280 }
281 
282 /*
283  * Process a received frame.  The node associated with the sender
284  * should be supplied.  If nothing was found in the node table then
285  * the caller is assumed to supply a reference to iv_bss instead.
286  * The RSSI and a timestamp are also supplied.  The RSSI data is used
287  * during AP scanning to select a AP to associate with; it can have
288  * any units so long as values have consistent units and higher values
289  * mean ``better signal''.  The receive timestamp is currently not used
290  * by the 802.11 layer.
291  */
292 static int
293 adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
294     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
295 {
296 	struct ieee80211vap *vap = ni->ni_vap;
297 	struct ieee80211com *ic = ni->ni_ic;
298 	struct ifnet *ifp = vap->iv_ifp;
299 	struct ieee80211_frame *wh;
300 	struct ieee80211_key *key;
301 	struct ether_header *eh;
302 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
303 	uint8_t dir, type, subtype, qos;
304 	uint8_t *bssid;
305 	uint16_t rxseq;
306 
307 	if (m->m_flags & M_AMPDU_MPDU) {
308 		/*
309 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
310 		 * w/ M_AMPDU_MPDU marked have already passed through
311 		 * here but were received out of order and been held on
312 		 * the reorder queue.  When resubmitted they are marked
313 		 * with the M_AMPDU_MPDU flag and we can bypass most of
314 		 * the normal processing.
315 		 */
316 		wh = mtod(m, struct ieee80211_frame *);
317 		type = IEEE80211_FC0_TYPE_DATA;
318 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
319 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
320 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
321 		goto resubmit_ampdu;
322 	}
323 
324 	KASSERT(ni != NULL, ("null node"));
325 	ni->ni_inact = ni->ni_inact_reload;
326 
327 	type = -1;			/* undefined */
328 
329 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
330 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
331 		    ni->ni_macaddr, NULL,
332 		    "too short (1): len %u", m->m_pkthdr.len);
333 		vap->iv_stats.is_rx_tooshort++;
334 		goto out;
335 	}
336 	/*
337 	 * Bit of a cheat here, we use a pointer for a 3-address
338 	 * frame format but don't reference fields past outside
339 	 * ieee80211_frame_min w/o first validating the data is
340 	 * present.
341 	 */
342 	wh = mtod(m, struct ieee80211_frame *);
343 
344 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
345 	    IEEE80211_FC0_VERSION_0) {
346 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
347 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
348 		    wh->i_fc[0], wh->i_fc[1]);
349 		vap->iv_stats.is_rx_badversion++;
350 		goto err;
351 	}
352 
353 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
354 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
355 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
356 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
357 		if (dir != IEEE80211_FC1_DIR_NODS)
358 			bssid = wh->i_addr1;
359 		else if (type == IEEE80211_FC0_TYPE_CTL)
360 			bssid = wh->i_addr1;
361 		else {
362 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
363 				IEEE80211_DISCARD_MAC(vap,
364 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
365 				    NULL, "too short (2): len %u",
366 				    m->m_pkthdr.len);
367 				vap->iv_stats.is_rx_tooshort++;
368 				goto out;
369 			}
370 			bssid = wh->i_addr3;
371 		}
372 		/*
373 		 * Validate the bssid.
374 		 */
375 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
376 		     (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
377 		      subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
378 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
379 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
380 			/* not interested in */
381 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
382 			    bssid, NULL, "%s", "not to bss");
383 			vap->iv_stats.is_rx_wrongbss++;
384 			goto out;
385 		}
386 		/*
387 		 * Data frame, cons up a node when it doesn't
388 		 * exist. This should probably done after an ACL check.
389 		 */
390 		if (type == IEEE80211_FC0_TYPE_DATA &&
391 		    ni == vap->iv_bss &&
392 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
393 			/*
394 			 * Beware of frames that come in too early; we
395 			 * can receive broadcast frames and creating sta
396 			 * entries will blow up because there is no bss
397 			 * channel yet.
398 			 */
399 			if (vap->iv_state != IEEE80211_S_RUN) {
400 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
401 				    wh, "data", "not in RUN state (%s)",
402 				    ieee80211_state_name[vap->iv_state]);
403 				vap->iv_stats.is_rx_badstate++;
404 				goto err;
405 			}
406 			/*
407 			 * Fake up a node for this newly
408 			 * discovered member of the IBSS.
409 			 */
410 			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
411 			if (ni == NULL) {
412 				/* NB: stat kept for alloc failure */
413 				goto err;
414 			}
415 		}
416 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
417 		ni->ni_noise = nf;
418 		if (IEEE80211_HAS_SEQ(type, subtype) &&
419 		    IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
420 			uint8_t tid = ieee80211_gettid(wh);
421 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
422 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
423 				ic->ic_wme.wme_hipri_traffic++;
424 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
425 			if (! ieee80211_check_rxseq(ni, wh)) {
426 				/* duplicate, discard */
427 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
428 				    bssid, "duplicate",
429 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
430 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
431 				    ni->ni_rxseqs[tid] >>
432 					IEEE80211_SEQ_SEQ_SHIFT,
433 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
434 				    ni->ni_rxseqs[tid] &
435 					IEEE80211_SEQ_FRAG_MASK,
436 				    tid);
437 				vap->iv_stats.is_rx_dup++;
438 				IEEE80211_NODE_STAT(ni, rx_dup);
439 				goto out;
440 			}
441 			ni->ni_rxseqs[tid] = rxseq;
442 		}
443 	}
444 
445 	switch (type) {
446 	case IEEE80211_FC0_TYPE_DATA:
447 		hdrspace = ieee80211_hdrspace(ic, wh);
448 		if (m->m_len < hdrspace &&
449 		    (m = m_pullup(m, hdrspace)) == NULL) {
450 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
451 			    ni->ni_macaddr, NULL,
452 			    "data too short: expecting %u", hdrspace);
453 			vap->iv_stats.is_rx_tooshort++;
454 			goto out;		/* XXX */
455 		}
456 		if (dir != IEEE80211_FC1_DIR_NODS) {
457 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
458 			    wh, "data", "incorrect dir 0x%x", dir);
459 			vap->iv_stats.is_rx_wrongdir++;
460 			goto out;
461 		}
462 		/* XXX no power-save support */
463 
464 		/*
465 		 * Handle A-MPDU re-ordering.  If the frame is to be
466 		 * processed directly then ieee80211_ampdu_reorder
467 		 * will return 0; otherwise it has consumed the mbuf
468 		 * and we should do nothing more with it.
469 		 */
470 		if ((m->m_flags & M_AMPDU) &&
471 		    ieee80211_ampdu_reorder(ni, m) != 0) {
472 			m = NULL;
473 			goto out;
474 		}
475 	resubmit_ampdu:
476 
477 		/*
478 		 * Handle privacy requirements.  Note that we
479 		 * must not be preempted from here until after
480 		 * we (potentially) call ieee80211_crypto_demic;
481 		 * otherwise we may violate assumptions in the
482 		 * crypto cipher modules used to do delayed update
483 		 * of replay sequence numbers.
484 		 */
485 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
486 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
487 				/*
488 				 * Discard encrypted frames when privacy is off.
489 				 */
490 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
491 				    wh, "WEP", "%s", "PRIVACY off");
492 				vap->iv_stats.is_rx_noprivacy++;
493 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
494 				goto out;
495 			}
496 			key = ieee80211_crypto_decap(ni, m, hdrspace);
497 			if (key == NULL) {
498 				/* NB: stats+msgs handled in crypto_decap */
499 				IEEE80211_NODE_STAT(ni, rx_wepfail);
500 				goto out;
501 			}
502 			wh = mtod(m, struct ieee80211_frame *);
503 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
504 		} else {
505 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
506 			key = NULL;
507 		}
508 
509 		/*
510 		 * Save QoS bits for use below--before we strip the header.
511 		 */
512 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
513 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
514 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
515 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
516 		} else
517 			qos = 0;
518 
519 		/*
520 		 * Next up, any fragmentation.
521 		 */
522 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
523 			m = ieee80211_defrag(ni, m, hdrspace);
524 			if (m == NULL) {
525 				/* Fragment dropped or frame not complete yet */
526 				goto out;
527 			}
528 		}
529 		wh = NULL;		/* no longer valid, catch any uses */
530 
531 		/*
532 		 * Next strip any MSDU crypto bits.
533 		 */
534 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
535 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
536 			    ni->ni_macaddr, "data", "%s", "demic error");
537 			vap->iv_stats.is_rx_demicfail++;
538 			IEEE80211_NODE_STAT(ni, rx_demicfail);
539 			goto out;
540 		}
541 
542 		/* copy to listener after decrypt */
543 		if (ieee80211_radiotap_active_vap(vap))
544 			ieee80211_radiotap_rx(vap, m);
545 		need_tap = 0;
546 
547 		/*
548 		 * Finally, strip the 802.11 header.
549 		 */
550 		m = ieee80211_decap(vap, m, hdrspace);
551 		if (m == NULL) {
552 			/* XXX mask bit to check for both */
553 			/* don't count Null data frames as errors */
554 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
555 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
556 				goto out;
557 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
558 			    ni->ni_macaddr, "data", "%s", "decap error");
559 			vap->iv_stats.is_rx_decap++;
560 			IEEE80211_NODE_STAT(ni, rx_decap);
561 			goto err;
562 		}
563 		eh = mtod(m, struct ether_header *);
564 		if (!ieee80211_node_is_authorized(ni)) {
565 			/*
566 			 * Deny any non-PAE frames received prior to
567 			 * authorization.  For open/shared-key
568 			 * authentication the port is mark authorized
569 			 * after authentication completes.  For 802.1x
570 			 * the port is not marked authorized by the
571 			 * authenticator until the handshake has completed.
572 			 */
573 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
574 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
575 				    eh->ether_shost, "data",
576 				    "unauthorized port: ether type 0x%x len %u",
577 				    eh->ether_type, m->m_pkthdr.len);
578 				vap->iv_stats.is_rx_unauth++;
579 				IEEE80211_NODE_STAT(ni, rx_unauth);
580 				goto err;
581 			}
582 		} else {
583 			/*
584 			 * When denying unencrypted frames, discard
585 			 * any non-PAE frames received without encryption.
586 			 */
587 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
588 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
589 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
590 				/*
591 				 * Drop unencrypted frames.
592 				 */
593 				vap->iv_stats.is_rx_unencrypted++;
594 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
595 				goto out;
596 			}
597 		}
598 		/* XXX require HT? */
599 		if (qos & IEEE80211_QOS_AMSDU) {
600 			m = ieee80211_decap_amsdu(ni, m);
601 			if (m == NULL)
602 				return IEEE80211_FC0_TYPE_DATA;
603 		} else {
604 #ifdef IEEE80211_SUPPORT_SUPERG
605 			m = ieee80211_decap_fastframe(vap, ni, m);
606 			if (m == NULL)
607 				return IEEE80211_FC0_TYPE_DATA;
608 #endif
609 		}
610 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
611 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
612 		else
613 			ieee80211_deliver_data(vap, ni, m);
614 		return IEEE80211_FC0_TYPE_DATA;
615 
616 	case IEEE80211_FC0_TYPE_MGT:
617 		vap->iv_stats.is_rx_mgmt++;
618 		IEEE80211_NODE_STAT(ni, rx_mgmt);
619 		if (dir != IEEE80211_FC1_DIR_NODS) {
620 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
621 			    wh, "data", "incorrect dir 0x%x", dir);
622 			vap->iv_stats.is_rx_wrongdir++;
623 			goto err;
624 		}
625 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
626 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
627 			    ni->ni_macaddr, "mgt", "too short: len %u",
628 			    m->m_pkthdr.len);
629 			vap->iv_stats.is_rx_tooshort++;
630 			goto out;
631 		}
632 #ifdef IEEE80211_DEBUG
633 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
634 		    ieee80211_msg_dumppkts(vap)) {
635 			if_printf(ifp, "received %s from %s rssi %d\n",
636 			    ieee80211_mgt_subtype_name[subtype >>
637 				IEEE80211_FC0_SUBTYPE_SHIFT],
638 			    ether_sprintf(wh->i_addr2), rssi);
639 		}
640 #endif
641 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
642 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
643 			    wh, NULL, "%s", "WEP set but not permitted");
644 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
645 			goto out;
646 		}
647 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
648 		goto out;
649 
650 	case IEEE80211_FC0_TYPE_CTL:
651 		vap->iv_stats.is_rx_ctl++;
652 		IEEE80211_NODE_STAT(ni, rx_ctrl);
653 		vap->iv_recv_ctl(ni, m, subtype);
654 		goto out;
655 
656 	default:
657 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
658 		    wh, "bad", "frame type 0x%x", type);
659 		/* should not come here */
660 		break;
661 	}
662 err:
663 	IFNET_STAT_INC(ifp, ierrors, 1);
664 out:
665 	if (m != NULL) {
666 		if (need_tap && ieee80211_radiotap_active_vap(vap))
667 			ieee80211_radiotap_rx(vap, m);
668 		m_freem(m);
669 	}
670 	return type;
671 }
672 
673 static int
674 is11bclient(const uint8_t *rates, const uint8_t *xrates)
675 {
676 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
677 	int i;
678 
679 	/* NB: the 11b clients we care about will not have xrates */
680 	if (xrates != NULL || rates == NULL)
681 		return 0;
682 	for (i = 0; i < rates[1]; i++) {
683 		int r = rates[2+i] & IEEE80211_RATE_VAL;
684 		if (r > 2*11 || ((1<<r) & brates) == 0)
685 			return 0;
686 	}
687 	return 1;
688 }
689 
690 static void
691 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
692 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
693 {
694 	struct ieee80211vap *vap = ni->ni_vap;
695 	struct ieee80211com *ic = ni->ni_ic;
696 	struct ieee80211_channel *rxchan = ic->ic_curchan;
697 	struct ieee80211_frame *wh;
698 	uint8_t *frm, *efrm, *sfrm;
699 	uint8_t *ssid, *rates, *xrates;
700 #if 0
701 	int ht_state_change = 0;
702 #endif
703 
704 	wh = mtod(m0, struct ieee80211_frame *);
705 	frm = (uint8_t *)&wh[1];
706 	efrm = mtod(m0, uint8_t *) + m0->m_len;
707 	switch (subtype) {
708 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
709 	case IEEE80211_FC0_SUBTYPE_BEACON: {
710 		struct ieee80211_scanparams scan;
711 		struct ieee80211_channel *c;
712 		/*
713 		 * We process beacon/probe response
714 		 * frames to discover neighbors.
715 		 */
716 		if (rxs != NULL) {
717 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
718 			if (c != NULL)
719 				rxchan = c;
720 		}
721 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
722 			return;
723 		/*
724 		 * Count frame now that we know it's to be processed.
725 		 */
726 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
727 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
728 			IEEE80211_NODE_STAT(ni, rx_beacons);
729 		} else
730 			IEEE80211_NODE_STAT(ni, rx_proberesp);
731 		/*
732 		 * If scanning, just pass information to the scan module.
733 		 */
734 		if (ic->ic_flags & IEEE80211_F_SCAN) {
735 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
736 				/*
737 				 * Actively scanning a channel marked passive;
738 				 * send a probe request now that we know there
739 				 * is 802.11 traffic present.
740 				 *
741 				 * XXX check if the beacon we recv'd gives
742 				 * us what we need and suppress the probe req
743 				 */
744 				ieee80211_probe_curchan(vap, 1);
745 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
746 			}
747 			ieee80211_add_scan(vap, rxchan, &scan, wh,
748 			    subtype, rssi, nf);
749 			return;
750 		}
751 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
752 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
753 				/*
754 				 * Create a new entry in the neighbor table.
755 				 */
756 				ni = ieee80211_add_neighbor(vap, wh, &scan);
757 			} else if (ni->ni_capinfo == 0) {
758 				/*
759 				 * Update faked node created on transmit.
760 				 * Note this also updates the tsf.
761 				 */
762 				ieee80211_init_neighbor(ni, wh, &scan);
763 			} else {
764 				/*
765 				 * Record tsf for potential resync.
766 				 */
767 				memcpy(ni->ni_tstamp.data, scan.tstamp,
768 					sizeof(ni->ni_tstamp));
769 			}
770 			/*
771 			 * This isn't enabled yet - otherwise it would
772 			 * update the HT parameters and channel width
773 			 * from any node, which could lead to lots of
774 			 * strange behaviour if the 11n nodes aren't
775 			 * exactly configured to match.
776 			 */
777 #if 0
778 			if (scan.htcap != NULL && scan.htinfo != NULL &&
779 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
780 				if (ieee80211_ht_updateparams(ni,
781 				    scan.htcap, scan.htinfo))
782 					ht_state_change = 1;
783 			}
784 #endif
785 			if (ni != NULL) {
786 				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
787 				ni->ni_noise = nf;
788 			}
789 			/*
790 			 * Same here - the channel width change should
791 			 * be applied to the specific peer node, not
792 			 * to the ic.  Ie, the interface configuration
793 			 * should stay in its current channel width;
794 			 * but it should change the rate control and
795 			 * any queued frames for the given node only.
796 			 *
797 			 * Since there's no (current) way to inform
798 			 * the driver that a channel width change has
799 			 * occured for a single node, just stub this
800 			 * out.
801 			 */
802 #if 0
803 			if (ht_state_change)
804 				ieee80211_update_chw(ic);
805 #endif
806 		}
807 		break;
808 	}
809 
810 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
811 		if (vap->iv_state != IEEE80211_S_RUN) {
812 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
813 			    wh, NULL, "wrong state %s",
814 			    ieee80211_state_name[vap->iv_state]);
815 			vap->iv_stats.is_rx_mgtdiscard++;
816 			return;
817 		}
818 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
819 			/* frame must be directed */
820 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
821 			    wh, NULL, "%s", "not unicast");
822 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
823 			return;
824 		}
825 
826 		/*
827 		 * prreq frame format
828 		 *	[tlv] ssid
829 		 *	[tlv] supported rates
830 		 *	[tlv] extended supported rates
831 		 */
832 		ssid = rates = xrates = NULL;
833 		sfrm = frm;
834 		while (efrm - frm > 1) {
835 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
836 			switch (*frm) {
837 			case IEEE80211_ELEMID_SSID:
838 				ssid = frm;
839 				break;
840 			case IEEE80211_ELEMID_RATES:
841 				rates = frm;
842 				break;
843 			case IEEE80211_ELEMID_XRATES:
844 				xrates = frm;
845 				break;
846 			}
847 			frm += frm[1] + 2;
848 		}
849 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
850 		if (xrates != NULL)
851 			IEEE80211_VERIFY_ELEMENT(xrates,
852 				IEEE80211_RATE_MAXSIZE - rates[1], return);
853 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
854 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
855 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
856 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
857 			    wh, NULL,
858 			    "%s", "no ssid with ssid suppression enabled");
859 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
860 			return;
861 		}
862 
863 		/* XXX find a better class or define it's own */
864 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
865 		    "%s", "recv probe req");
866 		/*
867 		 * Some legacy 11b clients cannot hack a complete
868 		 * probe response frame.  When the request includes
869 		 * only a bare-bones rate set, communicate this to
870 		 * the transmit side.
871 		 */
872 		ieee80211_send_proberesp(vap, wh->i_addr2,
873 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
874 		break;
875 
876 	case IEEE80211_FC0_SUBTYPE_ACTION:
877 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
878 		if (ni == vap->iv_bss) {
879 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
880 			    wh, NULL, "%s", "unknown node");
881 			vap->iv_stats.is_rx_mgtdiscard++;
882 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
883 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
884 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
885 			    wh, NULL, "%s", "not for us");
886 			vap->iv_stats.is_rx_mgtdiscard++;
887 		} else if (vap->iv_state != IEEE80211_S_RUN) {
888 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
889 			    wh, NULL, "wrong state %s",
890 			    ieee80211_state_name[vap->iv_state]);
891 			vap->iv_stats.is_rx_mgtdiscard++;
892 		} else {
893 			if (ieee80211_parse_action(ni, m0) == 0)
894 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
895 		}
896 		break;
897 
898 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
899 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
900 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
901 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
902 	case IEEE80211_FC0_SUBTYPE_ATIM:
903 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
904 	case IEEE80211_FC0_SUBTYPE_AUTH:
905 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
906 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
907 		    wh, NULL, "%s", "not handled");
908 		vap->iv_stats.is_rx_mgtdiscard++;
909 		break;
910 
911 	default:
912 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
913 		    wh, "mgt", "subtype 0x%x not handled", subtype);
914 		vap->iv_stats.is_rx_badsubtype++;
915 		break;
916 	}
917 }
918 #undef IEEE80211_VERIFY_LENGTH
919 #undef IEEE80211_VERIFY_ELEMENT
920 
921 static void
922 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
923 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
924 {
925 	struct ieee80211vap *vap = ni->ni_vap;
926 	struct ieee80211com *ic = ni->ni_ic;
927 	struct ieee80211_frame *wh;
928 
929 	/*
930 	 * Process management frames when scanning; useful for doing
931 	 * a site-survey.
932 	 */
933 	if (ic->ic_flags & IEEE80211_F_SCAN)
934 		adhoc_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
935 	else {
936 		wh = mtod(m0, struct ieee80211_frame *);
937 		switch (subtype) {
938 		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
939 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
940 		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
941 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
942 		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
943 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
944 		case IEEE80211_FC0_SUBTYPE_BEACON:
945 		case IEEE80211_FC0_SUBTYPE_ATIM:
946 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
947 		case IEEE80211_FC0_SUBTYPE_AUTH:
948 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
949 		case IEEE80211_FC0_SUBTYPE_ACTION:
950 		case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
951 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
952 			     wh, NULL, "%s", "not handled");
953 			vap->iv_stats.is_rx_mgtdiscard++;
954 			break;
955 		default:
956 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
957 			     wh, "mgt", "subtype 0x%x not handled", subtype);
958 			vap->iv_stats.is_rx_badsubtype++;
959 			break;
960 		}
961 	}
962 }
963 
964 static void
965 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
966 {
967 
968 	switch (subtype) {
969 	case IEEE80211_FC0_SUBTYPE_BAR:
970 		ieee80211_recv_bar(ni, m);
971 		break;
972 	}
973 }
974