xref: /freebsd/sys/net80211/ieee80211_adhoc.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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 <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_adhoc.h>
60 #include <net80211/ieee80211_input.h>
61 #ifdef IEEE80211_SUPPORT_SUPERG
62 #include <net80211/ieee80211_superg.h>
63 #endif
64 #ifdef IEEE80211_SUPPORT_TDMA
65 #include <net80211/ieee80211_tdma.h>
66 #endif
67 #include <net80211/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_INIT:
219 			/*
220 			 * Already have a channel; bypass the
221 			 * scan and startup immediately.
222 			 * Note that ieee80211_create_ibss will call
223 			 * back to do a RUN->RUN state change.
224 			 */
225 			ieee80211_create_ibss(vap,
226 			    ieee80211_ht_adjust_channel(ic,
227 				ic->ic_curchan, vap->iv_flags_ht));
228 			/* NB: iv_bss is changed on return */
229 			ni = vap->iv_bss;
230 			break;
231 		case IEEE80211_S_SCAN:
232 #ifdef IEEE80211_DEBUG
233 			if (ieee80211_msg_debug(vap)) {
234 				ieee80211_note(vap,
235 				    "synchronized with %s ssid ",
236 				    ether_sprintf(ni->ni_bssid));
237 				ieee80211_print_essid(vap->iv_bss->ni_essid,
238 				    ni->ni_esslen);
239 				/* XXX MCS/HT */
240 				printf(" channel %d start %uMb\n",
241 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
242 				    IEEE80211_RATE2MBS(ni->ni_txrate));
243 			}
244 #endif
245 			break;
246 		case IEEE80211_S_RUN:	/* IBSS merge */
247 			break;
248 		default:
249 			goto invalid;
250 		}
251 		/*
252 		 * When 802.1x is not in use mark the port authorized
253 		 * at this point so traffic can flow.
254 		 */
255 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
256 			ieee80211_node_authorize(ni);
257 		/*
258 		 * Fake association when joining an existing bss.
259 		 */
260 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
261 		    ic->ic_newassoc != NULL)
262 			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
263 		break;
264 	case IEEE80211_S_SLEEP:
265 		vap->iv_sta_ps(vap, 0);
266 		break;
267 	default:
268 	invalid:
269 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
270 		    "%s: unexpected state transition %s -> %s\n", __func__,
271 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
272 		break;
273 	}
274 	return 0;
275 }
276 
277 /*
278  * Decide if a received management frame should be
279  * printed when debugging is enabled.  This filters some
280  * of the less interesting frames that come frequently
281  * (e.g. beacons).
282  */
283 static __inline int
284 doprint(struct ieee80211vap *vap, int subtype)
285 {
286 	switch (subtype) {
287 	case IEEE80211_FC0_SUBTYPE_BEACON:
288 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
289 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
290 		return 1;
291 	}
292 	return 1;
293 }
294 
295 /*
296  * Process a received frame.  The node associated with the sender
297  * should be supplied.  If nothing was found in the node table then
298  * the caller is assumed to supply a reference to iv_bss instead.
299  * The RSSI and a timestamp are also supplied.  The RSSI data is used
300  * during AP scanning to select a AP to associate with; it can have
301  * any units so long as values have consistent units and higher values
302  * mean ``better signal''.  The receive timestamp is currently not used
303  * by the 802.11 layer.
304  */
305 static int
306 adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
307     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
308 {
309 	struct ieee80211vap *vap = ni->ni_vap;
310 	struct ieee80211com *ic = ni->ni_ic;
311 	struct ifnet *ifp = vap->iv_ifp;
312 	struct ieee80211_frame *wh;
313 	struct ieee80211_key *key;
314 	struct ether_header *eh;
315 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
316 	uint8_t dir, type, subtype, qos;
317 	uint8_t *bssid;
318 
319 	if (m->m_flags & M_AMPDU_MPDU) {
320 		/*
321 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
322 		 * w/ M_AMPDU_MPDU marked have already passed through
323 		 * here but were received out of order and been held on
324 		 * the reorder queue.  When resubmitted they are marked
325 		 * with the M_AMPDU_MPDU flag and we can bypass most of
326 		 * the normal processing.
327 		 */
328 		wh = mtod(m, struct ieee80211_frame *);
329 		type = IEEE80211_FC0_TYPE_DATA;
330 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
331 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
332 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
333 		goto resubmit_ampdu;
334 	}
335 
336 	KASSERT(ni != NULL, ("null node"));
337 	ni->ni_inact = ni->ni_inact_reload;
338 
339 	type = -1;			/* undefined */
340 
341 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
342 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
343 		    ni->ni_macaddr, NULL,
344 		    "too short (1): len %u", m->m_pkthdr.len);
345 		vap->iv_stats.is_rx_tooshort++;
346 		goto out;
347 	}
348 	/*
349 	 * Bit of a cheat here, we use a pointer for a 3-address
350 	 * frame format but don't reference fields past outside
351 	 * ieee80211_frame_min w/o first validating the data is
352 	 * present.
353 	 */
354 	wh = mtod(m, struct ieee80211_frame *);
355 
356 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
357 	    IEEE80211_FC0_VERSION_0) {
358 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
359 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
360 		    wh->i_fc[0], wh->i_fc[1]);
361 		vap->iv_stats.is_rx_badversion++;
362 		goto err;
363 	}
364 
365 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
366 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
367 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
368 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
369 		if (dir != IEEE80211_FC1_DIR_NODS)
370 			bssid = wh->i_addr1;
371 		else if (type == IEEE80211_FC0_TYPE_CTL)
372 			bssid = wh->i_addr1;
373 		else {
374 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
375 				IEEE80211_DISCARD_MAC(vap,
376 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
377 				    NULL, "too short (2): len %u",
378 				    m->m_pkthdr.len);
379 				vap->iv_stats.is_rx_tooshort++;
380 				goto out;
381 			}
382 			bssid = wh->i_addr3;
383 		}
384 		/*
385 		 * Validate the bssid.
386 		 */
387 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
388 		     (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
389 		      subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
390 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
391 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
392 			/* not interested in */
393 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
394 			    bssid, NULL, "%s", "not to bss");
395 			vap->iv_stats.is_rx_wrongbss++;
396 			goto out;
397 		}
398 		/*
399 		 * Data frame, cons up a node when it doesn't
400 		 * exist. This should probably done after an ACL check.
401 		 */
402 		if (type == IEEE80211_FC0_TYPE_DATA &&
403 		    ni == vap->iv_bss &&
404 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
405 			/*
406 			 * Beware of frames that come in too early; we
407 			 * can receive broadcast frames and creating sta
408 			 * entries will blow up because there is no bss
409 			 * channel yet.
410 			 */
411 			if (vap->iv_state != IEEE80211_S_RUN) {
412 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
413 				    wh, "data", "not in RUN state (%s)",
414 				    ieee80211_state_name[vap->iv_state]);
415 				vap->iv_stats.is_rx_badstate++;
416 				goto err;
417 			}
418 			/*
419 			 * Fake up a node for this newly
420 			 * discovered member of the IBSS.
421 			 */
422 			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
423 			if (ni == NULL) {
424 				/* NB: stat kept for alloc failure */
425 				goto err;
426 			}
427 		}
428 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
429 		ni->ni_noise = nf;
430 		if (IEEE80211_HAS_SEQ(type, subtype) &&
431 		    IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
432 			uint8_t tid = ieee80211_gettid(wh);
433 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
434 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
435 				ic->ic_wme.wme_hipri_traffic++;
436 			if (! ieee80211_check_rxseq(ni, wh, bssid))
437 				goto out;
438 		}
439 	}
440 
441 	switch (type) {
442 	case IEEE80211_FC0_TYPE_DATA:
443 		hdrspace = ieee80211_hdrspace(ic, wh);
444 		if (m->m_len < hdrspace &&
445 		    (m = m_pullup(m, hdrspace)) == NULL) {
446 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
447 			    ni->ni_macaddr, NULL,
448 			    "data too short: expecting %u", hdrspace);
449 			vap->iv_stats.is_rx_tooshort++;
450 			goto out;		/* XXX */
451 		}
452 		if (dir != IEEE80211_FC1_DIR_NODS) {
453 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
454 			    wh, "data", "incorrect dir 0x%x", dir);
455 			vap->iv_stats.is_rx_wrongdir++;
456 			goto out;
457 		}
458 		/* XXX no power-save support */
459 
460 		/*
461 		 * Handle A-MPDU re-ordering.  If the frame is to be
462 		 * processed directly then ieee80211_ampdu_reorder
463 		 * will return 0; otherwise it has consumed the mbuf
464 		 * and we should do nothing more with it.
465 		 */
466 		if ((m->m_flags & M_AMPDU) &&
467 		    ieee80211_ampdu_reorder(ni, m) != 0) {
468 			m = NULL;
469 			goto out;
470 		}
471 	resubmit_ampdu:
472 
473 		/*
474 		 * Handle privacy requirements.  Note that we
475 		 * must not be preempted from here until after
476 		 * we (potentially) call ieee80211_crypto_demic;
477 		 * otherwise we may violate assumptions in the
478 		 * crypto cipher modules used to do delayed update
479 		 * of replay sequence numbers.
480 		 */
481 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
482 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
483 				/*
484 				 * Discard encrypted frames when privacy is off.
485 				 */
486 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
487 				    wh, "WEP", "%s", "PRIVACY off");
488 				vap->iv_stats.is_rx_noprivacy++;
489 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
490 				goto out;
491 			}
492 			key = ieee80211_crypto_decap(ni, m, hdrspace);
493 			if (key == NULL) {
494 				/* NB: stats+msgs handled in crypto_decap */
495 				IEEE80211_NODE_STAT(ni, rx_wepfail);
496 				goto out;
497 			}
498 			wh = mtod(m, struct ieee80211_frame *);
499 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
500 		} else {
501 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
502 			key = NULL;
503 		}
504 
505 		/*
506 		 * Save QoS bits for use below--before we strip the header.
507 		 */
508 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
509 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
510 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
511 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
512 		} else
513 			qos = 0;
514 
515 		/*
516 		 * Next up, any fragmentation.
517 		 */
518 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
519 			m = ieee80211_defrag(ni, m, hdrspace);
520 			if (m == NULL) {
521 				/* Fragment dropped or frame not complete yet */
522 				goto out;
523 			}
524 		}
525 		wh = NULL;		/* no longer valid, catch any uses */
526 
527 		/*
528 		 * Next strip any MSDU crypto bits.
529 		 */
530 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
531 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
532 			    ni->ni_macaddr, "data", "%s", "demic error");
533 			vap->iv_stats.is_rx_demicfail++;
534 			IEEE80211_NODE_STAT(ni, rx_demicfail);
535 			goto out;
536 		}
537 
538 		/* copy to listener after decrypt */
539 		if (ieee80211_radiotap_active_vap(vap))
540 			ieee80211_radiotap_rx(vap, m);
541 		need_tap = 0;
542 
543 		/*
544 		 * Finally, strip the 802.11 header.
545 		 */
546 		m = ieee80211_decap(vap, m, hdrspace);
547 		if (m == NULL) {
548 			/* XXX mask bit to check for both */
549 			/* don't count Null data frames as errors */
550 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
551 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
552 				goto out;
553 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
554 			    ni->ni_macaddr, "data", "%s", "decap error");
555 			vap->iv_stats.is_rx_decap++;
556 			IEEE80211_NODE_STAT(ni, rx_decap);
557 			goto err;
558 		}
559 		eh = mtod(m, struct ether_header *);
560 		if (!ieee80211_node_is_authorized(ni)) {
561 			/*
562 			 * Deny any non-PAE frames received prior to
563 			 * authorization.  For open/shared-key
564 			 * authentication the port is mark authorized
565 			 * after authentication completes.  For 802.1x
566 			 * the port is not marked authorized by the
567 			 * authenticator until the handshake has completed.
568 			 */
569 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
570 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
571 				    eh->ether_shost, "data",
572 				    "unauthorized port: ether type 0x%x len %u",
573 				    eh->ether_type, m->m_pkthdr.len);
574 				vap->iv_stats.is_rx_unauth++;
575 				IEEE80211_NODE_STAT(ni, rx_unauth);
576 				goto err;
577 			}
578 		} else {
579 			/*
580 			 * When denying unencrypted frames, discard
581 			 * any non-PAE frames received without encryption.
582 			 */
583 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
584 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
585 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
586 				/*
587 				 * Drop unencrypted frames.
588 				 */
589 				vap->iv_stats.is_rx_unencrypted++;
590 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
591 				goto out;
592 			}
593 		}
594 		/* XXX require HT? */
595 		if (qos & IEEE80211_QOS_AMSDU) {
596 			m = ieee80211_decap_amsdu(ni, m);
597 			if (m == NULL)
598 				return IEEE80211_FC0_TYPE_DATA;
599 		} else {
600 #ifdef IEEE80211_SUPPORT_SUPERG
601 			m = ieee80211_decap_fastframe(vap, ni, m);
602 			if (m == NULL)
603 				return IEEE80211_FC0_TYPE_DATA;
604 #endif
605 		}
606 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
607 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
608 		else
609 			ieee80211_deliver_data(vap, ni, m);
610 		return IEEE80211_FC0_TYPE_DATA;
611 
612 	case IEEE80211_FC0_TYPE_MGT:
613 		vap->iv_stats.is_rx_mgmt++;
614 		IEEE80211_NODE_STAT(ni, rx_mgmt);
615 		if (dir != IEEE80211_FC1_DIR_NODS) {
616 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
617 			    wh, "data", "incorrect dir 0x%x", dir);
618 			vap->iv_stats.is_rx_wrongdir++;
619 			goto err;
620 		}
621 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
622 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
623 			    ni->ni_macaddr, "mgt", "too short: len %u",
624 			    m->m_pkthdr.len);
625 			vap->iv_stats.is_rx_tooshort++;
626 			goto out;
627 		}
628 #ifdef IEEE80211_DEBUG
629 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
630 		    ieee80211_msg_dumppkts(vap)) {
631 			if_printf(ifp, "received %s from %s rssi %d\n",
632 			    ieee80211_mgt_subtype_name(subtype),
633 			    ether_sprintf(wh->i_addr2), rssi);
634 		}
635 #endif
636 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
637 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
638 			    wh, NULL, "%s", "WEP set but not permitted");
639 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
640 			goto out;
641 		}
642 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
643 		goto out;
644 
645 	case IEEE80211_FC0_TYPE_CTL:
646 		vap->iv_stats.is_rx_ctl++;
647 		IEEE80211_NODE_STAT(ni, rx_ctrl);
648 		vap->iv_recv_ctl(ni, m, subtype);
649 		goto out;
650 
651 	default:
652 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
653 		    wh, "bad", "frame type 0x%x", type);
654 		/* should not come here */
655 		break;
656 	}
657 err:
658 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
659 out:
660 	if (m != NULL) {
661 		if (need_tap && ieee80211_radiotap_active_vap(vap))
662 			ieee80211_radiotap_rx(vap, m);
663 		m_freem(m);
664 	}
665 	return type;
666 }
667 
668 static int
669 is11bclient(const uint8_t *rates, const uint8_t *xrates)
670 {
671 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
672 	int i;
673 
674 	/* NB: the 11b clients we care about will not have xrates */
675 	if (xrates != NULL || rates == NULL)
676 		return 0;
677 	for (i = 0; i < rates[1]; i++) {
678 		int r = rates[2+i] & IEEE80211_RATE_VAL;
679 		if (r > 2*11 || ((1<<r) & brates) == 0)
680 			return 0;
681 	}
682 	return 1;
683 }
684 
685 static void
686 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
687 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
688 {
689 	struct ieee80211vap *vap = ni->ni_vap;
690 	struct ieee80211com *ic = ni->ni_ic;
691 	struct ieee80211_channel *rxchan = ic->ic_curchan;
692 	struct ieee80211_frame *wh;
693 	uint8_t *frm, *efrm;
694 	uint8_t *ssid, *rates, *xrates;
695 #if 0
696 	int ht_state_change = 0;
697 #endif
698 
699 	wh = mtod(m0, struct ieee80211_frame *);
700 	frm = (uint8_t *)&wh[1];
701 	efrm = mtod(m0, uint8_t *) + m0->m_len;
702 	switch (subtype) {
703 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
704 	case IEEE80211_FC0_SUBTYPE_BEACON: {
705 		struct ieee80211_scanparams scan;
706 		struct ieee80211_channel *c;
707 		/*
708 		 * We process beacon/probe response
709 		 * frames to discover neighbors.
710 		 */
711 		if (rxs != NULL) {
712 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
713 			if (c != NULL)
714 				rxchan = c;
715 		}
716 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
717 			return;
718 		/*
719 		 * Count frame now that we know it's to be processed.
720 		 */
721 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
722 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
723 			IEEE80211_NODE_STAT(ni, rx_beacons);
724 		} else
725 			IEEE80211_NODE_STAT(ni, rx_proberesp);
726 		/*
727 		 * If scanning, just pass information to the scan module.
728 		 */
729 		if (ic->ic_flags & IEEE80211_F_SCAN) {
730 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
731 				/*
732 				 * Actively scanning a channel marked passive;
733 				 * send a probe request now that we know there
734 				 * is 802.11 traffic present.
735 				 *
736 				 * XXX check if the beacon we recv'd gives
737 				 * us what we need and suppress the probe req
738 				 */
739 				ieee80211_probe_curchan(vap, 1);
740 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
741 			}
742 			ieee80211_add_scan(vap, rxchan, &scan, wh,
743 			    subtype, rssi, nf);
744 			return;
745 		}
746 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
747 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
748 				/*
749 				 * Create a new entry in the neighbor table.
750 				 *
751 				 * XXX TODO:
752 				 *
753 				 * Here we're not scanning; so if we have an
754 				 * SSID then make sure it matches our SSID.
755 				 * Otherwise this code will match on all IBSS
756 				 * beacons/probe requests for all SSIDs,
757 				 * filling the node table with nodes that
758 				 * aren't ours.
759 				 */
760 				if (ieee80211_ibss_node_check_new(ni, &scan))
761 					ni = ieee80211_add_neighbor(vap, wh, &scan);
762 				else
763 					ni = NULL;
764 			} else if (ni->ni_capinfo == 0) {
765 				/*
766 				 * Update faked node created on transmit.
767 				 * Note this also updates the tsf.
768 				 */
769 				ieee80211_init_neighbor(ni, wh, &scan);
770 			} else {
771 				/*
772 				 * Record tsf for potential resync.
773 				 */
774 				memcpy(ni->ni_tstamp.data, scan.tstamp,
775 					sizeof(ni->ni_tstamp));
776 			}
777 			/*
778 			 * This isn't enabled yet - otherwise it would
779 			 * update the HT parameters and channel width
780 			 * from any node, which could lead to lots of
781 			 * strange behaviour if the 11n nodes aren't
782 			 * exactly configured to match.
783 			 */
784 #if 0
785 			if (scan.htcap != NULL && scan.htinfo != NULL &&
786 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
787 				if (ieee80211_ht_updateparams(ni,
788 				    scan.htcap, scan.htinfo))
789 					ht_state_change = 1;
790 			}
791 #endif
792 			if (ni != NULL) {
793 				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
794 				ni->ni_noise = nf;
795 			}
796 			/*
797 			 * Same here - the channel width change should
798 			 * be applied to the specific peer node, not
799 			 * to the ic.  Ie, the interface configuration
800 			 * should stay in its current channel width;
801 			 * but it should change the rate control and
802 			 * any queued frames for the given node only.
803 			 *
804 			 * Since there's no (current) way to inform
805 			 * the driver that a channel width change has
806 			 * occurred for a single node, just stub this
807 			 * out.
808 			 */
809 #if 0
810 			if (ht_state_change)
811 				ieee80211_update_chw(ic);
812 #endif
813 		}
814 		break;
815 	}
816 
817 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
818 		if (vap->iv_state != IEEE80211_S_RUN) {
819 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
820 			    wh, NULL, "wrong state %s",
821 			    ieee80211_state_name[vap->iv_state]);
822 			vap->iv_stats.is_rx_mgtdiscard++;
823 			return;
824 		}
825 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
826 			/* frame must be directed */
827 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
828 			    wh, NULL, "%s", "not unicast");
829 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
830 			return;
831 		}
832 
833 		/*
834 		 * prreq frame format
835 		 *	[tlv] ssid
836 		 *	[tlv] supported rates
837 		 *	[tlv] extended supported rates
838 		 */
839 		ssid = rates = xrates = NULL;
840 		while (efrm - frm > 1) {
841 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
842 			switch (*frm) {
843 			case IEEE80211_ELEMID_SSID:
844 				ssid = frm;
845 				break;
846 			case IEEE80211_ELEMID_RATES:
847 				rates = frm;
848 				break;
849 			case IEEE80211_ELEMID_XRATES:
850 				xrates = frm;
851 				break;
852 			}
853 			frm += frm[1] + 2;
854 		}
855 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
856 		if (xrates != NULL)
857 			IEEE80211_VERIFY_ELEMENT(xrates,
858 				IEEE80211_RATE_MAXSIZE - rates[1], return);
859 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
860 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
861 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
862 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
863 			    wh, NULL,
864 			    "%s", "no ssid with ssid suppression enabled");
865 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
866 			return;
867 		}
868 
869 		/* XXX find a better class or define it's own */
870 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
871 		    "%s", "recv probe req");
872 		/*
873 		 * Some legacy 11b clients cannot hack a complete
874 		 * probe response frame.  When the request includes
875 		 * only a bare-bones rate set, communicate this to
876 		 * the transmit side.
877 		 */
878 		ieee80211_send_proberesp(vap, wh->i_addr2,
879 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
880 		break;
881 
882 	case IEEE80211_FC0_SUBTYPE_ACTION:
883 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
884 		if ((ni == vap->iv_bss) &&
885 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
886 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
887 			    wh, NULL, "%s", "unknown node");
888 			vap->iv_stats.is_rx_mgtdiscard++;
889 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
890 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
891 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
892 			    wh, NULL, "%s", "not for us");
893 			vap->iv_stats.is_rx_mgtdiscard++;
894 		} else if (vap->iv_state != IEEE80211_S_RUN) {
895 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
896 			    wh, NULL, "wrong state %s",
897 			    ieee80211_state_name[vap->iv_state]);
898 			vap->iv_stats.is_rx_mgtdiscard++;
899 		} else {
900 			if (ieee80211_parse_action(ni, m0) == 0)
901 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
902 		}
903 		break;
904 
905 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
906 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
907 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
908 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
909 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
910 	case IEEE80211_FC0_SUBTYPE_ATIM:
911 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
912 	case IEEE80211_FC0_SUBTYPE_AUTH:
913 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
914 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
915 		    wh, NULL, "%s", "not handled");
916 		vap->iv_stats.is_rx_mgtdiscard++;
917 		break;
918 
919 	default:
920 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
921 		    wh, "mgt", "subtype 0x%x not handled", subtype);
922 		vap->iv_stats.is_rx_badsubtype++;
923 		break;
924 	}
925 }
926 #undef IEEE80211_VERIFY_LENGTH
927 #undef IEEE80211_VERIFY_ELEMENT
928 
929 static void
930 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
931 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
932 {
933 	struct ieee80211vap *vap = ni->ni_vap;
934 	struct ieee80211com *ic = ni->ni_ic;
935 	struct ieee80211_frame *wh;
936 
937 	/*
938 	 * Process management frames when scanning; useful for doing
939 	 * a site-survey.
940 	 */
941 	if (ic->ic_flags & IEEE80211_F_SCAN)
942 		adhoc_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
943 	else {
944 		wh = mtod(m0, struct ieee80211_frame *);
945 		switch (subtype) {
946 		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
947 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
948 		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
949 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
950 		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
951 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
952 		case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
953 		case IEEE80211_FC0_SUBTYPE_BEACON:
954 		case IEEE80211_FC0_SUBTYPE_ATIM:
955 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
956 		case IEEE80211_FC0_SUBTYPE_AUTH:
957 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
958 		case IEEE80211_FC0_SUBTYPE_ACTION:
959 		case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
960 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
961 			     wh, NULL, "%s", "not handled");
962 			vap->iv_stats.is_rx_mgtdiscard++;
963 			break;
964 		default:
965 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
966 			     wh, "mgt", "subtype 0x%x not handled", subtype);
967 			vap->iv_stats.is_rx_badsubtype++;
968 			break;
969 		}
970 	}
971 }
972 
973 static void
974 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
975 {
976 
977 	switch (subtype) {
978 	case IEEE80211_FC0_SUBTYPE_BAR:
979 		ieee80211_recv_bar(ni, m);
980 		break;
981 	}
982 }
983