xref: /freebsd/sys/net80211/ieee80211_adhoc.c (revision f4f8f02054f3abb6ceb84aefcdecc78d5c8b462f)
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_media.h>
52 #include <net/if_llc.h>
53 #include <net/ethernet.h>
54 
55 #include <net/bpf.h>
56 
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_adhoc.h>
59 #include <net80211/ieee80211_input.h>
60 #ifdef IEEE80211_SUPPORT_SUPERG
61 #include <net80211/ieee80211_superg.h>
62 #endif
63 #ifdef IEEE80211_SUPPORT_TDMA
64 #include <net80211/ieee80211_tdma.h>
65 #endif
66 
67 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
68 
69 static	void adhoc_vattach(struct ieee80211vap *);
70 static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
71 static int adhoc_input(struct ieee80211_node *, struct mbuf *,
72 	int rssi, int noise, uint32_t rstamp);
73 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74 	int subtype, int rssi, int noise, uint32_t rstamp);
75 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 	int subtype, int rssi, int noise, uint32_t rstamp);
77 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78 
79 void
80 ieee80211_adhoc_attach(struct ieee80211com *ic)
81 {
82 	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
83 	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
84 }
85 
86 void
87 ieee80211_adhoc_detach(struct ieee80211com *ic)
88 {
89 }
90 
91 static void
92 adhoc_vdetach(struct ieee80211vap *vap)
93 {
94 }
95 
96 static void
97 adhoc_vattach(struct ieee80211vap *vap)
98 {
99 	vap->iv_newstate = adhoc_newstate;
100 	vap->iv_input = adhoc_input;
101 	if (vap->iv_opmode == IEEE80211_M_IBSS)
102 		vap->iv_recv_mgmt = adhoc_recv_mgmt;
103 	else
104 		vap->iv_recv_mgmt = ahdemo_recv_mgmt;
105 	vap->iv_recv_ctl = adhoc_recv_ctl;
106 	vap->iv_opdetach = adhoc_vdetach;
107 #ifdef IEEE80211_SUPPORT_TDMA
108 	/*
109 	 * Throw control to tdma support.  Note we do this
110 	 * after setting up our callbacks so it can piggyback
111 	 * on top of us.
112 	 */
113 	if (vap->iv_caps & IEEE80211_C_TDMA)
114 		ieee80211_tdma_vattach(vap);
115 #endif
116 }
117 
118 static void
119 sta_leave(void *arg, struct ieee80211_node *ni)
120 {
121 	struct ieee80211vap *vap = arg;
122 
123 	if (ni->ni_vap == vap && ni != vap->iv_bss)
124 		ieee80211_node_leave(ni);
125 }
126 
127 /*
128  * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
129  */
130 static int
131 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
132 {
133 	struct ieee80211com *ic = vap->iv_ic;
134 	struct ieee80211_node *ni;
135 	enum ieee80211_state ostate;
136 
137 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
138 
139 	ostate = vap->iv_state;
140 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
141 	    __func__, ieee80211_state_name[ostate],
142 	    ieee80211_state_name[nstate], arg);
143 	vap->iv_state = nstate;			/* state transition */
144 	if (ostate != IEEE80211_S_SCAN)
145 		ieee80211_cancel_scan(vap);	/* background scan */
146 	ni = vap->iv_bss;			/* NB: no reference held */
147 	switch (nstate) {
148 	case IEEE80211_S_INIT:
149 		switch (ostate) {
150 		case IEEE80211_S_SCAN:
151 			ieee80211_cancel_scan(vap);
152 			break;
153 		default:
154 			break;
155 		}
156 		if (ostate != IEEE80211_S_INIT) {
157 			/* NB: optimize INIT -> INIT case */
158 			ieee80211_reset_bss(vap);
159 		}
160 		break;
161 	case IEEE80211_S_SCAN:
162 		switch (ostate) {
163 		case IEEE80211_S_RUN:		/* beacon miss */
164 			/* purge station table; entries are stale */
165 			ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
166 			/* fall thru... */
167 		case IEEE80211_S_INIT:
168 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
169 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
170 				/*
171 				 * Already have a channel; bypass the
172 				 * scan and startup immediately.
173 				 */
174 				ieee80211_create_ibss(vap, vap->iv_des_chan);
175 				break;
176 			}
177 			/*
178 			 * Initiate a scan.  We can come here as a result
179 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
180 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
181 			 * and the scan request parameters will be present
182 			 * in iv_scanreq.  Otherwise we do the default.
183 			 */
184 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
185 				ieee80211_check_scan(vap,
186 				    vap->iv_scanreq_flags,
187 				    vap->iv_scanreq_duration,
188 				    vap->iv_scanreq_mindwell,
189 				    vap->iv_scanreq_maxdwell,
190 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
191 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
192 			} else
193 				ieee80211_check_scan_current(vap);
194 			break;
195 		case IEEE80211_S_SCAN:
196 			/*
197 			 * This can happen because of a change in state
198 			 * that requires a reset.  Trigger a new scan
199 			 * unless we're in manual roaming mode in which
200 			 * case an application must issue an explicit request.
201 			 */
202 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203 				ieee80211_check_scan_current(vap);
204 			break;
205 		default:
206 			goto invalid;
207 		}
208 		break;
209 	case IEEE80211_S_RUN:
210 		if (vap->iv_flags & IEEE80211_F_WPA) {
211 			/* XXX validate prerequisites */
212 		}
213 		switch (ostate) {
214 		case IEEE80211_S_SCAN:
215 #ifdef IEEE80211_DEBUG
216 			if (ieee80211_msg_debug(vap)) {
217 				ieee80211_note(vap,
218 				    "synchronized with %s ssid ",
219 				    ether_sprintf(ni->ni_bssid));
220 				ieee80211_print_essid(vap->iv_bss->ni_essid,
221 				    ni->ni_esslen);
222 				/* XXX MCS/HT */
223 				printf(" channel %d start %uMb\n",
224 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
225 				    IEEE80211_RATE2MBS(ni->ni_txrate));
226 			}
227 #endif
228 			break;
229 		default:
230 			goto invalid;
231 		}
232 		/*
233 		 * When 802.1x is not in use mark the port authorized
234 		 * at this point so traffic can flow.
235 		 */
236 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
237 			ieee80211_node_authorize(ni);
238 		/*
239 		 * Fake association when joining an existing bss.
240 		 */
241 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
242 		    ic->ic_newassoc != NULL)
243 			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
244 		break;
245 	case IEEE80211_S_SLEEP:
246 		ieee80211_sta_pwrsave(vap, 0);
247 		break;
248 	default:
249 	invalid:
250 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
251 		    "%s: unexpected state transition %s -> %s\n", __func__,
252 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
253 		break;
254 	}
255 	return 0;
256 }
257 
258 /*
259  * Decide if a received management frame should be
260  * printed when debugging is enabled.  This filters some
261  * of the less interesting frames that come frequently
262  * (e.g. beacons).
263  */
264 static __inline int
265 doprint(struct ieee80211vap *vap, int subtype)
266 {
267 	switch (subtype) {
268 	case IEEE80211_FC0_SUBTYPE_BEACON:
269 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
270 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
271 		return 1;
272 	}
273 	return 1;
274 }
275 
276 /*
277  * Process a received frame.  The node associated with the sender
278  * should be supplied.  If nothing was found in the node table then
279  * the caller is assumed to supply a reference to iv_bss instead.
280  * The RSSI and a timestamp are also supplied.  The RSSI data is used
281  * during AP scanning to select a AP to associate with; it can have
282  * any units so long as values have consistent units and higher values
283  * mean ``better signal''.  The receive timestamp is currently not used
284  * by the 802.11 layer.
285  */
286 static int
287 adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
288 	int rssi, int noise, uint32_t rstamp)
289 {
290 #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
291 #define	HAS_SEQ(type)	((type & 0x4) == 0)
292 	struct ieee80211vap *vap = ni->ni_vap;
293 	struct ieee80211com *ic = ni->ni_ic;
294 	struct ifnet *ifp = vap->iv_ifp;
295 	struct ieee80211_frame *wh;
296 	struct ieee80211_key *key;
297 	struct ether_header *eh;
298 	int hdrspace, need_tap;
299 	uint8_t dir, type, subtype, qos;
300 	uint8_t *bssid;
301 	uint16_t rxseq;
302 
303 	if (m->m_flags & M_AMPDU_MPDU) {
304 		/*
305 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
306 		 * w/ M_AMPDU_MPDU marked have already passed through
307 		 * here but were received out of order and been held on
308 		 * the reorder queue.  When resubmitted they are marked
309 		 * with the M_AMPDU_MPDU flag and we can bypass most of
310 		 * the normal processing.
311 		 */
312 		wh = mtod(m, struct ieee80211_frame *);
313 		type = IEEE80211_FC0_TYPE_DATA;
314 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
315 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
316 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
317 		goto resubmit_ampdu;
318 	}
319 
320 	KASSERT(ni != NULL, ("null node"));
321 	ni->ni_inact = ni->ni_inact_reload;
322 
323 	need_tap = 1;			/* mbuf need to be tapped. */
324 	type = -1;			/* undefined */
325 
326 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
327 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
328 		    ni->ni_macaddr, NULL,
329 		    "too short (1): len %u", m->m_pkthdr.len);
330 		vap->iv_stats.is_rx_tooshort++;
331 		goto out;
332 	}
333 	/*
334 	 * Bit of a cheat here, we use a pointer for a 3-address
335 	 * frame format but don't reference fields past outside
336 	 * ieee80211_frame_min w/o first validating the data is
337 	 * present.
338 	 */
339 	wh = mtod(m, struct ieee80211_frame *);
340 
341 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
342 	    IEEE80211_FC0_VERSION_0) {
343 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
344 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
345 		    wh->i_fc[0], wh->i_fc[1]);
346 		vap->iv_stats.is_rx_badversion++;
347 		goto err;
348 	}
349 
350 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
351 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
352 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
353 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
354 		if (dir != IEEE80211_FC1_DIR_NODS)
355 			bssid = wh->i_addr1;
356 		else if (type == IEEE80211_FC0_TYPE_CTL)
357 			bssid = wh->i_addr1;
358 		else {
359 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
360 				IEEE80211_DISCARD_MAC(vap,
361 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
362 				    NULL, "too short (2): len %u",
363 				    m->m_pkthdr.len);
364 				vap->iv_stats.is_rx_tooshort++;
365 				goto out;
366 			}
367 			bssid = wh->i_addr3;
368 		}
369 		/*
370 		 * Validate the bssid.
371 		 */
372 		if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
373 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
374 			/* not interested in */
375 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
376 			    bssid, NULL, "%s", "not to bss");
377 			vap->iv_stats.is_rx_wrongbss++;
378 			goto out;
379 		}
380 		/*
381 		 * Data frame, cons up a node when it doesn't
382 		 * exist. This should probably done after an ACL check.
383 		 */
384 		if (type == IEEE80211_FC0_TYPE_DATA &&
385 		    ni == vap->iv_bss &&
386 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
387 			/*
388 			 * Beware of frames that come in too early; we
389 			 * can receive broadcast frames and creating sta
390 			 * entries will blow up because there is no bss
391 			 * channel yet.
392 			 */
393 			if (vap->iv_state != IEEE80211_S_RUN) {
394 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
395 				    wh, "data", "not in RUN state (%s)",
396 				    ieee80211_state_name[vap->iv_state]);
397 				vap->iv_stats.is_rx_badstate++;
398 				goto err;
399 			}
400 			/*
401 			 * Fake up a node for this newly
402 			 * discovered member of the IBSS.
403 			 */
404 			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
405 			if (ni == NULL) {
406 				/* NB: stat kept for alloc failure */
407 				goto err;
408 			}
409 		}
410 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
411 		ni->ni_noise = noise;
412 		ni->ni_rstamp = rstamp;
413 		if (HAS_SEQ(type)) {
414 			uint8_t tid = ieee80211_gettid(wh);
415 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
416 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
417 				ic->ic_wme.wme_hipri_traffic++;
418 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
419 			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
420 			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
421 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
422 				/* duplicate, discard */
423 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
424 				    bssid, "duplicate",
425 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
426 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
427 				    ni->ni_rxseqs[tid] >>
428 					IEEE80211_SEQ_SEQ_SHIFT,
429 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
430 				    ni->ni_rxseqs[tid] &
431 					IEEE80211_SEQ_FRAG_MASK,
432 				    tid);
433 				vap->iv_stats.is_rx_dup++;
434 				IEEE80211_NODE_STAT(ni, rx_dup);
435 				goto out;
436 			}
437 			ni->ni_rxseqs[tid] = rxseq;
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_WEP) {
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_WEP;
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 (bpf_peers_present(vap->iv_rawbpf))
540 			bpf_mtap(vap->iv_rawbpf, 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 				IEEE80211_FC0_SUBTYPE_SHIFT],
634 			    ether_sprintf(wh->i_addr2), rssi);
635 		}
636 #endif
637 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
638 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
639 			    wh, NULL, "%s", "WEP set but not permitted");
640 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
641 			goto out;
642 		}
643 		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
644 		goto out;
645 
646 	case IEEE80211_FC0_TYPE_CTL:
647 		vap->iv_stats.is_rx_ctl++;
648 		IEEE80211_NODE_STAT(ni, rx_ctrl);
649 		vap->iv_recv_ctl(ni, m, subtype);
650 		goto out;
651 
652 	default:
653 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
654 		    wh, "bad", "frame type 0x%x", type);
655 		/* should not come here */
656 		break;
657 	}
658 err:
659 	ifp->if_ierrors++;
660 out:
661 	if (m != NULL) {
662 		if (need_tap && bpf_peers_present(vap->iv_rawbpf))
663 			bpf_mtap(vap->iv_rawbpf, m);
664 		m_freem(m);
665 	}
666 	return type;
667 #undef SEQ_LEQ
668 }
669 
670 static int
671 is11bclient(const uint8_t *rates, const uint8_t *xrates)
672 {
673 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
674 	int i;
675 
676 	/* NB: the 11b clients we care about will not have xrates */
677 	if (xrates != NULL || rates == NULL)
678 		return 0;
679 	for (i = 0; i < rates[1]; i++) {
680 		int r = rates[2+i] & IEEE80211_RATE_VAL;
681 		if (r > 2*11 || ((1<<r) & brates) == 0)
682 			return 0;
683 	}
684 	return 1;
685 }
686 
687 static void
688 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
689 	int subtype, int rssi, int noise, uint32_t rstamp)
690 {
691 	struct ieee80211vap *vap = ni->ni_vap;
692 	struct ieee80211com *ic = ni->ni_ic;
693 	struct ieee80211_frame *wh;
694 	uint8_t *frm, *efrm, *sfrm;
695 	uint8_t *ssid, *rates, *xrates;
696 
697 	wh = mtod(m0, struct ieee80211_frame *);
698 	frm = (uint8_t *)&wh[1];
699 	efrm = mtod(m0, uint8_t *) + m0->m_len;
700 	switch (subtype) {
701 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
702 	case IEEE80211_FC0_SUBTYPE_BEACON: {
703 		struct ieee80211_scanparams scan;
704 		/*
705 		 * We process beacon/probe response
706 		 * frames to discover neighbors.
707 		 */
708 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
709 			return;
710 		/*
711 		 * Count frame now that we know it's to be processed.
712 		 */
713 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
714 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
715 			IEEE80211_NODE_STAT(ni, rx_beacons);
716 		} else
717 			IEEE80211_NODE_STAT(ni, rx_proberesp);
718 		/*
719 		 * If scanning, just pass information to the scan module.
720 		 */
721 		if (ic->ic_flags & IEEE80211_F_SCAN) {
722 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
723 				/*
724 				 * Actively scanning a channel marked passive;
725 				 * send a probe request now that we know there
726 				 * is 802.11 traffic present.
727 				 *
728 				 * XXX check if the beacon we recv'd gives
729 				 * us what we need and suppress the probe req
730 				 */
731 				ieee80211_probe_curchan(vap, 1);
732 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
733 			}
734 			ieee80211_add_scan(vap, &scan, wh,
735 				subtype, rssi, noise, rstamp);
736 			return;
737 		}
738 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
739 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
740 				/*
741 				 * Create a new entry in the neighbor table.
742 				 */
743 				ni = ieee80211_add_neighbor(vap, wh, &scan);
744 			} else if (ni->ni_capinfo == 0) {
745 				/*
746 				 * Update faked node created on transmit.
747 				 * Note this also updates the tsf.
748 				 */
749 				ieee80211_init_neighbor(ni, wh, &scan);
750 			} else {
751 				/*
752 				 * Record tsf for potential resync.
753 				 */
754 				memcpy(ni->ni_tstamp.data, scan.tstamp,
755 					sizeof(ni->ni_tstamp));
756 			}
757 			if (ni != NULL) {
758 				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
759 				ni->ni_noise = noise;
760 				ni->ni_rstamp = rstamp;
761 			}
762 		}
763 		break;
764 	}
765 
766 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
767 		if (vap->iv_state != IEEE80211_S_RUN) {
768 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
769 			    wh, NULL, "wrong state %s",
770 			    ieee80211_state_name[vap->iv_state]);
771 			vap->iv_stats.is_rx_mgtdiscard++;
772 			return;
773 		}
774 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
775 			/* frame must be directed */
776 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
777 			    wh, NULL, "%s", "not unicast");
778 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
779 			return;
780 		}
781 
782 		/*
783 		 * prreq frame format
784 		 *	[tlv] ssid
785 		 *	[tlv] supported rates
786 		 *	[tlv] extended supported rates
787 		 */
788 		ssid = rates = xrates = NULL;
789 		sfrm = frm;
790 		while (efrm - frm > 1) {
791 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
792 			switch (*frm) {
793 			case IEEE80211_ELEMID_SSID:
794 				ssid = frm;
795 				break;
796 			case IEEE80211_ELEMID_RATES:
797 				rates = frm;
798 				break;
799 			case IEEE80211_ELEMID_XRATES:
800 				xrates = frm;
801 				break;
802 			}
803 			frm += frm[1] + 2;
804 		}
805 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
806 		if (xrates != NULL)
807 			IEEE80211_VERIFY_ELEMENT(xrates,
808 				IEEE80211_RATE_MAXSIZE - rates[1], return);
809 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
810 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
811 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
812 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
813 			    wh, NULL,
814 			    "%s", "no ssid with ssid suppression enabled");
815 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
816 			return;
817 		}
818 
819 		/* XXX find a better class or define it's own */
820 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
821 		    "%s", "recv probe req");
822 		/*
823 		 * Some legacy 11b clients cannot hack a complete
824 		 * probe response frame.  When the request includes
825 		 * only a bare-bones rate set, communicate this to
826 		 * the transmit side.
827 		 */
828 		ieee80211_send_proberesp(vap, wh->i_addr2,
829 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
830 		break;
831 
832 	case IEEE80211_FC0_SUBTYPE_ACTION: {
833 		const struct ieee80211_action *ia;
834 
835 		if (vap->iv_state != IEEE80211_S_RUN) {
836 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837 			    wh, NULL, "wrong state %s",
838 			    ieee80211_state_name[vap->iv_state]);
839 			vap->iv_stats.is_rx_mgtdiscard++;
840 			return;
841 		}
842 		/*
843 		 * action frame format:
844 		 *	[1] category
845 		 *	[1] action
846 		 *	[tlv] parameters
847 		 */
848 		IEEE80211_VERIFY_LENGTH(efrm - frm,
849 			sizeof(struct ieee80211_action), return);
850 		ia = (const struct ieee80211_action *) frm;
851 
852 		vap->iv_stats.is_rx_action++;
853 		IEEE80211_NODE_STAT(ni, rx_action);
854 
855 		/* verify frame payloads but defer processing */
856 		/* XXX maybe push this to method */
857 		switch (ia->ia_category) {
858 		case IEEE80211_ACTION_CAT_BA:
859 			switch (ia->ia_action) {
860 			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
861 				IEEE80211_VERIFY_LENGTH(efrm - frm,
862 				    sizeof(struct ieee80211_action_ba_addbarequest),
863 				    return);
864 				break;
865 			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
866 				IEEE80211_VERIFY_LENGTH(efrm - frm,
867 				    sizeof(struct ieee80211_action_ba_addbaresponse),
868 				    return);
869 				break;
870 			case IEEE80211_ACTION_BA_DELBA:
871 				IEEE80211_VERIFY_LENGTH(efrm - frm,
872 				    sizeof(struct ieee80211_action_ba_delba),
873 				    return);
874 				break;
875 			}
876 			break;
877 		case IEEE80211_ACTION_CAT_HT:
878 			switch (ia->ia_action) {
879 			case IEEE80211_ACTION_HT_TXCHWIDTH:
880 				IEEE80211_VERIFY_LENGTH(efrm - frm,
881 				    sizeof(struct ieee80211_action_ht_txchwidth),
882 				    return);
883 				break;
884 			}
885 			break;
886 		}
887 		ic->ic_recv_action(ni, frm, efrm);
888 		break;
889 	}
890 
891 	case IEEE80211_FC0_SUBTYPE_AUTH:
892 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
893 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
894 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
895 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
896 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
897 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
898 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
899 		     wh, NULL, "%s", "not handled");
900 		vap->iv_stats.is_rx_mgtdiscard++;
901 		return;
902 
903 	default:
904 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
905 		     wh, "mgt", "subtype 0x%x not handled", subtype);
906 		vap->iv_stats.is_rx_badsubtype++;
907 		break;
908 	}
909 }
910 #undef IEEE80211_VERIFY_LENGTH
911 #undef IEEE80211_VERIFY_ELEMENT
912 
913 static void
914 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
915 	int subtype, int rssi, int noise, uint32_t rstamp)
916 {
917 	struct ieee80211vap *vap = ni->ni_vap;
918 	struct ieee80211com *ic = ni->ni_ic;
919 
920 	/*
921 	 * Process management frames when scanning; useful for doing
922 	 * a site-survey.
923 	 */
924 	if (ic->ic_flags & IEEE80211_F_SCAN)
925 		adhoc_recv_mgmt(ni, m0, subtype, rssi, noise, rstamp);
926 	else
927 		vap->iv_stats.is_rx_mgtdiscard++;
928 }
929 
930 static void
931 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
932 {
933 }
934