ieee80211_adhoc.c revision 191547
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: head/sys/net80211/ieee80211_adhoc.c 191547 2009-04-26 21:50:21Z sam $");
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
69static	void adhoc_vattach(struct ieee80211vap *);
70static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
71static int adhoc_input(struct ieee80211_node *, struct mbuf *,
72	int rssi, int noise, uint32_t rstamp);
73static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74	int subtype, int rssi, int noise, uint32_t rstamp);
75static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76	int subtype, int rssi, int noise, uint32_t rstamp);
77static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78
79void
80ieee80211_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
86void
87ieee80211_adhoc_detach(struct ieee80211com *ic)
88{
89}
90
91static void
92adhoc_vdetach(struct ieee80211vap *vap)
93{
94}
95
96static void
97adhoc_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
118static void
119sta_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 */
130static int
131adhoc_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 */
264static __inline int
265doprint(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 */
286static int
287adhoc_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	default:
652		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
653		    wh, "bad", "frame type 0x%x", type);
654		/* should not come here */
655		break;
656	}
657err:
658	ifp->if_ierrors++;
659out:
660	if (m != NULL) {
661		if (need_tap && bpf_peers_present(vap->iv_rawbpf))
662			bpf_mtap(vap->iv_rawbpf, m);
663		m_freem(m);
664	}
665	return type;
666#undef SEQ_LEQ
667}
668
669static int
670is11bclient(const uint8_t *rates, const uint8_t *xrates)
671{
672	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
673	int i;
674
675	/* NB: the 11b clients we care about will not have xrates */
676	if (xrates != NULL || rates == NULL)
677		return 0;
678	for (i = 0; i < rates[1]; i++) {
679		int r = rates[2+i] & IEEE80211_RATE_VAL;
680		if (r > 2*11 || ((1<<r) & brates) == 0)
681			return 0;
682	}
683	return 1;
684}
685
686static void
687adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
688	int subtype, int rssi, int noise, uint32_t rstamp)
689{
690	struct ieee80211vap *vap = ni->ni_vap;
691	struct ieee80211com *ic = ni->ni_ic;
692	struct ieee80211_frame *wh;
693	uint8_t *frm, *efrm, *sfrm;
694	uint8_t *ssid, *rates, *xrates;
695
696	wh = mtod(m0, struct ieee80211_frame *);
697	frm = (uint8_t *)&wh[1];
698	efrm = mtod(m0, uint8_t *) + m0->m_len;
699	switch (subtype) {
700	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
701	case IEEE80211_FC0_SUBTYPE_BEACON: {
702		struct ieee80211_scanparams scan;
703		/*
704		 * We process beacon/probe response
705		 * frames to discover neighbors.
706		 */
707		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
708			return;
709		/*
710		 * Count frame now that we know it's to be processed.
711		 */
712		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
713			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
714			IEEE80211_NODE_STAT(ni, rx_beacons);
715		} else
716			IEEE80211_NODE_STAT(ni, rx_proberesp);
717		/*
718		 * If scanning, just pass information to the scan module.
719		 */
720		if (ic->ic_flags & IEEE80211_F_SCAN) {
721			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
722				/*
723				 * Actively scanning a channel marked passive;
724				 * send a probe request now that we know there
725				 * is 802.11 traffic present.
726				 *
727				 * XXX check if the beacon we recv'd gives
728				 * us what we need and suppress the probe req
729				 */
730				ieee80211_probe_curchan(vap, 1);
731				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
732			}
733			ieee80211_add_scan(vap, &scan, wh,
734				subtype, rssi, noise, rstamp);
735			return;
736		}
737		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
738			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
739				/*
740				 * Create a new entry in the neighbor table.
741				 */
742				ni = ieee80211_add_neighbor(vap, wh, &scan);
743			} else if (ni->ni_capinfo == 0) {
744				/*
745				 * Update faked node created on transmit.
746				 * Note this also updates the tsf.
747				 */
748				ieee80211_init_neighbor(ni, wh, &scan);
749			} else {
750				/*
751				 * Record tsf for potential resync.
752				 */
753				memcpy(ni->ni_tstamp.data, scan.tstamp,
754					sizeof(ni->ni_tstamp));
755			}
756			if (ni != NULL) {
757				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
758				ni->ni_noise = noise;
759				ni->ni_rstamp = rstamp;
760			}
761		}
762		break;
763	}
764
765	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
766		if (vap->iv_state != IEEE80211_S_RUN) {
767			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
768			    wh, NULL, "wrong state %s",
769			    ieee80211_state_name[vap->iv_state]);
770			vap->iv_stats.is_rx_mgtdiscard++;
771			return;
772		}
773		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
774			/* frame must be directed */
775			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
776			    wh, NULL, "%s", "not unicast");
777			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
778			return;
779		}
780
781		/*
782		 * prreq frame format
783		 *	[tlv] ssid
784		 *	[tlv] supported rates
785		 *	[tlv] extended supported rates
786		 */
787		ssid = rates = xrates = NULL;
788		sfrm = frm;
789		while (efrm - frm > 1) {
790			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
791			switch (*frm) {
792			case IEEE80211_ELEMID_SSID:
793				ssid = frm;
794				break;
795			case IEEE80211_ELEMID_RATES:
796				rates = frm;
797				break;
798			case IEEE80211_ELEMID_XRATES:
799				xrates = frm;
800				break;
801			}
802			frm += frm[1] + 2;
803		}
804		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
805		if (xrates != NULL)
806			IEEE80211_VERIFY_ELEMENT(xrates,
807				IEEE80211_RATE_MAXSIZE - rates[1], return);
808		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
809		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
810		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
811			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
812			    wh, NULL,
813			    "%s", "no ssid with ssid suppression enabled");
814			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
815			return;
816		}
817
818		/* XXX find a better class or define it's own */
819		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
820		    "%s", "recv probe req");
821		/*
822		 * Some legacy 11b clients cannot hack a complete
823		 * probe response frame.  When the request includes
824		 * only a bare-bones rate set, communicate this to
825		 * the transmit side.
826		 */
827		ieee80211_send_proberesp(vap, wh->i_addr2,
828		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
829		break;
830
831	case IEEE80211_FC0_SUBTYPE_ACTION: {
832		const struct ieee80211_action *ia;
833
834		if (vap->iv_state != IEEE80211_S_RUN) {
835			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
836			    wh, NULL, "wrong state %s",
837			    ieee80211_state_name[vap->iv_state]);
838			vap->iv_stats.is_rx_mgtdiscard++;
839			return;
840		}
841		/*
842		 * action frame format:
843		 *	[1] category
844		 *	[1] action
845		 *	[tlv] parameters
846		 */
847		IEEE80211_VERIFY_LENGTH(efrm - frm,
848			sizeof(struct ieee80211_action), return);
849		ia = (const struct ieee80211_action *) frm;
850
851		vap->iv_stats.is_rx_action++;
852		IEEE80211_NODE_STAT(ni, rx_action);
853
854		/* verify frame payloads but defer processing */
855		/* XXX maybe push this to method */
856		switch (ia->ia_category) {
857		case IEEE80211_ACTION_CAT_BA:
858			switch (ia->ia_action) {
859			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
860				IEEE80211_VERIFY_LENGTH(efrm - frm,
861				    sizeof(struct ieee80211_action_ba_addbarequest),
862				    return);
863				break;
864			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
865				IEEE80211_VERIFY_LENGTH(efrm - frm,
866				    sizeof(struct ieee80211_action_ba_addbaresponse),
867				    return);
868				break;
869			case IEEE80211_ACTION_BA_DELBA:
870				IEEE80211_VERIFY_LENGTH(efrm - frm,
871				    sizeof(struct ieee80211_action_ba_delba),
872				    return);
873				break;
874			}
875			break;
876		case IEEE80211_ACTION_CAT_HT:
877			switch (ia->ia_action) {
878			case IEEE80211_ACTION_HT_TXCHWIDTH:
879				IEEE80211_VERIFY_LENGTH(efrm - frm,
880				    sizeof(struct ieee80211_action_ht_txchwidth),
881				    return);
882				break;
883			}
884			break;
885		}
886		ic->ic_recv_action(ni, frm, efrm);
887		break;
888	}
889
890	case IEEE80211_FC0_SUBTYPE_AUTH:
891	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
892	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
893	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
894	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
895	case IEEE80211_FC0_SUBTYPE_DEAUTH:
896	case IEEE80211_FC0_SUBTYPE_DISASSOC:
897		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
898		     wh, NULL, "%s", "not handled");
899		vap->iv_stats.is_rx_mgtdiscard++;
900		return;
901
902	default:
903		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
904		     wh, "mgt", "subtype 0x%x not handled", subtype);
905		vap->iv_stats.is_rx_badsubtype++;
906		break;
907	}
908}
909#undef IEEE80211_VERIFY_LENGTH
910#undef IEEE80211_VERIFY_ELEMENT
911
912static void
913ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
914	int subtype, int rssi, int noise, uint32_t rstamp)
915{
916	struct ieee80211vap *vap = ni->ni_vap;
917	struct ieee80211com *ic = ni->ni_ic;
918
919	/*
920	 * Process management frames when scanning; useful for doing
921	 * a site-survey.
922	 */
923	if (ic->ic_flags & IEEE80211_F_SCAN)
924		adhoc_recv_mgmt(ni, m0, subtype, rssi, noise, rstamp);
925	else
926		vap->iv_stats.is_rx_mgtdiscard++;
927}
928
929static void
930adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
931{
932}
933