ieee80211_adhoc.c revision 179216
1/*-
2 * Copyright (c) 2007-2008 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 179216 2008-05-22 22:14:58Z 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
61#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
62
63static	void adhoc_vattach(struct ieee80211vap *);
64static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
65static int adhoc_input(struct ieee80211_node *, struct mbuf *,
66	int rssi, int noise, uint32_t rstamp);
67static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
68	int subtype, int rssi, int noise, uint32_t rstamp);
69
70void
71ieee80211_adhoc_attach(struct ieee80211com *ic)
72{
73	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
74	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
75}
76
77void
78ieee80211_adhoc_detach(struct ieee80211com *ic)
79{
80}
81
82static void
83adhoc_vdetach(struct ieee80211vap *vap)
84{
85}
86
87static void
88adhoc_vattach(struct ieee80211vap *vap)
89{
90	vap->iv_newstate = adhoc_newstate;
91	vap->iv_input = adhoc_input;
92	vap->iv_recv_mgmt = adhoc_recv_mgmt;
93	vap->iv_opdetach = adhoc_vdetach;
94}
95
96/*
97 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
98 */
99static int
100adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
101{
102#ifdef IEEE80211_DEBUG
103	struct ieee80211com *ic = vap->iv_ic;
104#endif
105	struct ieee80211_node *ni;
106	enum ieee80211_state ostate;
107
108	IEEE80211_LOCK_ASSERT(vap->iv_ic);
109
110	ostate = vap->iv_state;
111	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
112	    __func__, ieee80211_state_name[ostate],
113	    ieee80211_state_name[nstate], arg);
114	vap->iv_state = nstate;			/* state transition */
115	if (ostate != IEEE80211_S_SCAN)
116		ieee80211_cancel_scan(vap);	/* background scan */
117	ni = vap->iv_bss;			/* NB: no reference held */
118	switch (nstate) {
119	case IEEE80211_S_INIT:
120		switch (ostate) {
121		case IEEE80211_S_SCAN:
122			ieee80211_cancel_scan(vap);
123			break;
124		default:
125			break;
126		}
127		if (ostate != IEEE80211_S_INIT) {
128			/* NB: optimize INIT -> INIT case */
129			ieee80211_reset_bss(vap);
130		}
131		break;
132	case IEEE80211_S_SCAN:
133		switch (ostate) {
134		case IEEE80211_S_INIT:
135		case IEEE80211_S_RUN:		/* beacon miss */
136			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
137			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
138				/*
139				 * Already have a channel; bypass the
140				 * scan and startup immediately.
141				 */
142				ieee80211_create_ibss(vap, vap->iv_des_chan);
143				break;
144			}
145			/*
146			 * Initiate a scan.  We can come here as a result
147			 * of an IEEE80211_IOC_SCAN_REQ too in which case
148			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
149			 * and the scan request parameters will be present
150			 * in iv_scanreq.  Otherwise we do the default.
151			 */
152			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
153				ieee80211_check_scan(vap,
154				    vap->iv_scanreq_flags,
155				    vap->iv_scanreq_duration,
156				    vap->iv_scanreq_mindwell,
157				    vap->iv_scanreq_maxdwell,
158				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
159				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
160			} else
161				ieee80211_check_scan_current(vap);
162			break;
163		case IEEE80211_S_SCAN:
164			/*
165			 * This can happen because of a change in state
166			 * that requires a reset.  Trigger a new scan
167			 * unless we're in manual roaming mode in which
168			 * case an application must issue an explicit request.
169			 */
170			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
171				ieee80211_check_scan_current(vap);
172			break;
173		default:
174			goto invalid;
175		}
176		break;
177	case IEEE80211_S_RUN:
178		if (vap->iv_flags & IEEE80211_F_WPA) {
179			/* XXX validate prerequisites */
180		}
181		switch (ostate) {
182		case IEEE80211_S_SCAN:
183#ifdef IEEE80211_DEBUG
184			if (ieee80211_msg_debug(vap)) {
185				ieee80211_note(vap,
186				    "synchronized with %s ssid ",
187				    ether_sprintf(ni->ni_bssid));
188				ieee80211_print_essid(vap->iv_bss->ni_essid,
189				    ni->ni_esslen);
190				/* XXX MCS/HT */
191				printf(" channel %d start %uMb\n",
192				    ieee80211_chan2ieee(ic, ic->ic_curchan),
193				    IEEE80211_RATE2MBS(ni->ni_txrate));
194			}
195#endif
196			break;
197		default:
198			goto invalid;
199		}
200		/*
201		 * When 802.1x is not in use mark the port authorized
202		 * at this point so traffic can flow.
203		 */
204		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
205			ieee80211_node_authorize(ni);
206		break;
207	case IEEE80211_S_SLEEP:
208		ieee80211_sta_pwrsave(vap, 0);
209		break;
210	default:
211	invalid:
212		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
213		    "%s: invalid state transition %s -> %s\n", __func__,
214		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
215		break;
216	}
217	return 0;
218}
219
220/*
221 * Decide if a received management frame should be
222 * printed when debugging is enabled.  This filters some
223 * of the less interesting frames that come frequently
224 * (e.g. beacons).
225 */
226static __inline int
227doprint(struct ieee80211vap *vap, int subtype)
228{
229	switch (subtype) {
230	case IEEE80211_FC0_SUBTYPE_BEACON:
231		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
232	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
233		return 1;
234	}
235	return 1;
236}
237
238/*
239 * Process a received frame.  The node associated with the sender
240 * should be supplied.  If nothing was found in the node table then
241 * the caller is assumed to supply a reference to iv_bss instead.
242 * The RSSI and a timestamp are also supplied.  The RSSI data is used
243 * during AP scanning to select a AP to associate with; it can have
244 * any units so long as values have consistent units and higher values
245 * mean ``better signal''.  The receive timestamp is currently not used
246 * by the 802.11 layer.
247 */
248static int
249adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
250	int rssi, int noise, uint32_t rstamp)
251{
252#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
253#define	HAS_SEQ(type)	((type & 0x4) == 0)
254	struct ieee80211vap *vap = ni->ni_vap;
255	struct ieee80211com *ic = ni->ni_ic;
256	struct ifnet *ifp = vap->iv_ifp;
257	struct ieee80211_frame *wh;
258	struct ieee80211_key *key;
259	struct ether_header *eh;
260	int hdrspace, need_tap;
261	uint8_t dir, type, subtype, qos;
262	uint8_t *bssid;
263	uint16_t rxseq;
264
265	if (m->m_flags & M_AMPDU) {
266		/*
267		 * Fastpath for A-MPDU reorder q resubmission.  Frames
268		 * w/ M_AMPDU marked have already passed through here
269		 * but were received out of order and been held on the
270		 * reorder queue.  When resubmitted they are marked
271		 * with the M_AMPDU flag and we can bypass most of the
272		 * normal processing.
273		 */
274		wh = mtod(m, struct ieee80211_frame *);
275		type = IEEE80211_FC0_TYPE_DATA;
276		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
277		subtype = IEEE80211_FC0_SUBTYPE_QOS;
278		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
279		goto resubmit_ampdu;
280	}
281
282	KASSERT(ni != NULL, ("null node"));
283	ni->ni_inact = ni->ni_inact_reload;
284
285	need_tap = 1;			/* mbuf need to be tapped. */
286	type = -1;			/* undefined */
287
288	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
289		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
290		    ni->ni_macaddr, NULL,
291		    "too short (1): len %u", m->m_pkthdr.len);
292		vap->iv_stats.is_rx_tooshort++;
293		goto out;
294	}
295	/*
296	 * Bit of a cheat here, we use a pointer for a 3-address
297	 * frame format but don't reference fields past outside
298	 * ieee80211_frame_min w/o first validating the data is
299	 * present.
300	 */
301	wh = mtod(m, struct ieee80211_frame *);
302
303	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
304	    IEEE80211_FC0_VERSION_0) {
305		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
306		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
307		vap->iv_stats.is_rx_badversion++;
308		goto err;
309	}
310
311	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
312	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
313	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
314	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
315		if (dir != IEEE80211_FC1_DIR_NODS)
316			bssid = wh->i_addr1;
317		else if (type == IEEE80211_FC0_TYPE_CTL)
318			bssid = wh->i_addr1;
319		else {
320			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
321				IEEE80211_DISCARD_MAC(vap,
322				    IEEE80211_MSG_ANY, ni->ni_macaddr,
323				    NULL, "too short (2): len %u",
324				    m->m_pkthdr.len);
325				vap->iv_stats.is_rx_tooshort++;
326				goto out;
327			}
328			bssid = wh->i_addr3;
329		}
330		/*
331		 * Validate the bssid.
332		 */
333		if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
334		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
335			/* not interested in */
336			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
337			    bssid, NULL, "%s", "not to bss");
338			vap->iv_stats.is_rx_wrongbss++;
339			goto out;
340		}
341		/*
342		 * Data frame, cons up a node when it doesn't
343		 * exist. This should probably done after an ACL check.
344		 */
345		if (type == IEEE80211_FC0_TYPE_DATA &&
346		    ni == vap->iv_bss &&
347		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
348			/*
349			 * Fake up a node for this newly
350			 * discovered member of the IBSS.
351			 */
352			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
353			if (ni == NULL) {
354				/* NB: stat kept for alloc failure */
355				goto err;
356			}
357		}
358		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
359		ni->ni_noise = noise;
360		ni->ni_rstamp = rstamp;
361		if (HAS_SEQ(type)) {
362			uint8_t tid = ieee80211_gettid(wh);
363			if (IEEE80211_QOS_HAS_SEQ(wh) &&
364			    TID_TO_WME_AC(tid) >= WME_AC_VI)
365				ic->ic_wme.wme_hipri_traffic++;
366			rxseq = le16toh(*(uint16_t *)wh->i_seq);
367			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
368			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
369			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
370				/* duplicate, discard */
371				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
372				    bssid, "duplicate",
373				    "seqno <%u,%u> fragno <%u,%u> tid %u",
374				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
375				    ni->ni_rxseqs[tid] >>
376					IEEE80211_SEQ_SEQ_SHIFT,
377				    rxseq & IEEE80211_SEQ_FRAG_MASK,
378				    ni->ni_rxseqs[tid] &
379					IEEE80211_SEQ_FRAG_MASK,
380				    tid);
381				vap->iv_stats.is_rx_dup++;
382				IEEE80211_NODE_STAT(ni, rx_dup);
383				goto out;
384			}
385			ni->ni_rxseqs[tid] = rxseq;
386		}
387	}
388
389	switch (type) {
390	case IEEE80211_FC0_TYPE_DATA:
391		hdrspace = ieee80211_hdrspace(ic, wh);
392		if (m->m_len < hdrspace &&
393		    (m = m_pullup(m, hdrspace)) == NULL) {
394			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
395			    ni->ni_macaddr, NULL,
396			    "data too short: expecting %u", hdrspace);
397			vap->iv_stats.is_rx_tooshort++;
398			goto out;		/* XXX */
399		}
400		if (dir != IEEE80211_FC1_DIR_NODS) {
401			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
402			    wh, "data", "incorrect dir 0x%x", dir);
403			vap->iv_stats.is_rx_wrongdir++;
404			goto out;
405		}
406		/* XXX no power-save support */
407
408		/*
409		 * Handle A-MPDU re-ordering.  The station must be
410		 * associated and negotiated HT.  The frame must be
411		 * a QoS frame (not QoS null data) and not previously
412		 * processed for A-MPDU re-ordering.  If the frame is
413		 * to be processed directly then ieee80211_ampdu_reorder
414		 * will return 0; otherwise it has consumed the mbuf
415		 * and we should do nothing more with it.
416		 */
417		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
418		    subtype == IEEE80211_FC0_SUBTYPE_QOS &&
419		    ieee80211_ampdu_reorder(ni, m) != 0) {
420			m = NULL;
421			goto out;
422		}
423	resubmit_ampdu:
424
425		/*
426		 * Handle privacy requirements.  Note that we
427		 * must not be preempted from here until after
428		 * we (potentially) call ieee80211_crypto_demic;
429		 * otherwise we may violate assumptions in the
430		 * crypto cipher modules used to do delayed update
431		 * of replay sequence numbers.
432		 */
433		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
434			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
435				/*
436				 * Discard encrypted frames when privacy is off.
437				 */
438				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
439				    wh, "WEP", "%s", "PRIVACY off");
440				vap->iv_stats.is_rx_noprivacy++;
441				IEEE80211_NODE_STAT(ni, rx_noprivacy);
442				goto out;
443			}
444			key = ieee80211_crypto_decap(ni, m, hdrspace);
445			if (key == NULL) {
446				/* NB: stats+msgs handled in crypto_decap */
447				IEEE80211_NODE_STAT(ni, rx_wepfail);
448				goto out;
449			}
450			wh = mtod(m, struct ieee80211_frame *);
451			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
452		} else {
453			/* XXX M_WEP and IEEE80211_F_PRIVACY */
454			key = NULL;
455		}
456
457		/*
458		 * Save QoS bits for use below--before we strip the header.
459		 */
460		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
461			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
462			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
463			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
464		} else
465			qos = 0;
466
467		/*
468		 * Next up, any fragmentation.
469		 */
470		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
471			m = ieee80211_defrag(ni, m, hdrspace);
472			if (m == NULL) {
473				/* Fragment dropped or frame not complete yet */
474				goto out;
475			}
476		}
477		wh = NULL;		/* no longer valid, catch any uses */
478
479		/*
480		 * Next strip any MSDU crypto bits.
481		 */
482		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
483			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
484			    ni->ni_macaddr, "data", "%s", "demic error");
485			vap->iv_stats.is_rx_demicfail++;
486			IEEE80211_NODE_STAT(ni, rx_demicfail);
487			goto out;
488		}
489
490		/* copy to listener after decrypt */
491		if (bpf_peers_present(vap->iv_rawbpf))
492			bpf_mtap(vap->iv_rawbpf, m);
493		need_tap = 0;
494
495		/*
496		 * Finally, strip the 802.11 header.
497		 */
498		m = ieee80211_decap(vap, m, hdrspace);
499		if (m == NULL) {
500			/* XXX mask bit to check for both */
501			/* don't count Null data frames as errors */
502			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
503			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
504				goto out;
505			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
506			    ni->ni_macaddr, "data", "%s", "decap error");
507			vap->iv_stats.is_rx_decap++;
508			IEEE80211_NODE_STAT(ni, rx_decap);
509			goto err;
510		}
511		eh = mtod(m, struct ether_header *);
512		if (!ieee80211_node_is_authorized(ni)) {
513			/*
514			 * Deny any non-PAE frames received prior to
515			 * authorization.  For open/shared-key
516			 * authentication the port is mark authorized
517			 * after authentication completes.  For 802.1x
518			 * the port is not marked authorized by the
519			 * authenticator until the handshake has completed.
520			 */
521			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
522				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
523				    eh->ether_shost, "data",
524				    "unauthorized port: ether type 0x%x len %u",
525				    eh->ether_type, m->m_pkthdr.len);
526				vap->iv_stats.is_rx_unauth++;
527				IEEE80211_NODE_STAT(ni, rx_unauth);
528				goto err;
529			}
530		} else {
531			/*
532			 * When denying unencrypted frames, discard
533			 * any non-PAE frames received without encryption.
534			 */
535			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
536			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
537			    eh->ether_type != htons(ETHERTYPE_PAE)) {
538				/*
539				 * Drop unencrypted frames.
540				 */
541				vap->iv_stats.is_rx_unencrypted++;
542				IEEE80211_NODE_STAT(ni, rx_unencrypted);
543				goto out;
544			}
545		}
546		/* XXX require HT? */
547		if (qos & IEEE80211_QOS_AMSDU) {
548			m = ieee80211_decap_amsdu(ni, m);
549			if (m == NULL)
550				return IEEE80211_FC0_TYPE_DATA;
551		} else if ((ni->ni_ath_flags & IEEE80211_NODE_FF) &&
552#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
553		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
554			struct llc *llc;
555
556			/*
557			 * Check for fast-frame tunnel encapsulation.
558			 */
559			if (m->m_len < FF_LLC_SIZE &&
560			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
561				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
562				    ni->ni_macaddr, "fast-frame",
563				    "%s", "m_pullup(llc) failed");
564				vap->iv_stats.is_rx_tooshort++;
565				return IEEE80211_FC0_TYPE_DATA;
566			}
567			llc = (struct llc *)(mtod(m, uint8_t *) +
568				sizeof(struct ether_header));
569			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
570				m_adj(m, FF_LLC_SIZE);
571				m = ieee80211_decap_fastframe(ni, m);
572				if (m == NULL)
573					return IEEE80211_FC0_TYPE_DATA;
574			}
575		}
576#undef FF_LLC_SIZE
577		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
578			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
579		else
580			ieee80211_deliver_data(vap, ni, m);
581		return IEEE80211_FC0_TYPE_DATA;
582
583	case IEEE80211_FC0_TYPE_MGT:
584		vap->iv_stats.is_rx_mgmt++;
585		IEEE80211_NODE_STAT(ni, rx_mgmt);
586		if (dir != IEEE80211_FC1_DIR_NODS) {
587			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
588			    wh, "data", "incorrect dir 0x%x", dir);
589			vap->iv_stats.is_rx_wrongdir++;
590			goto err;
591		}
592		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
593			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
594			    ni->ni_macaddr, "mgt", "too short: len %u",
595			    m->m_pkthdr.len);
596			vap->iv_stats.is_rx_tooshort++;
597			goto out;
598		}
599#ifdef IEEE80211_DEBUG
600		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
601		    ieee80211_msg_dumppkts(vap)) {
602			if_printf(ifp, "received %s from %s rssi %d\n",
603			    ieee80211_mgt_subtype_name[subtype >>
604				IEEE80211_FC0_SUBTYPE_SHIFT],
605			    ether_sprintf(wh->i_addr2), rssi);
606		}
607#endif
608		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
609			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
610			    wh, NULL, "%s", "WEP set but not permitted");
611			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
612			goto out;
613		}
614		if (bpf_peers_present(vap->iv_rawbpf))
615			bpf_mtap(vap->iv_rawbpf, m);
616		/* NB: only IBSS mode gets mgt frames */
617		if (vap->iv_opmode == IEEE80211_M_IBSS)
618			vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
619		m_freem(m);
620		return IEEE80211_FC0_TYPE_MGT;
621
622	case IEEE80211_FC0_TYPE_CTL:
623		vap->iv_stats.is_rx_ctl++;
624		IEEE80211_NODE_STAT(ni, rx_ctrl);
625		goto out;
626	default:
627		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
628		    wh, "bad", "frame type 0x%x", type);
629		/* should not come here */
630		break;
631	}
632err:
633	ifp->if_ierrors++;
634out:
635	if (m != NULL) {
636		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
637			bpf_mtap(vap->iv_rawbpf, m);
638		m_freem(m);
639	}
640	return type;
641#undef SEQ_LEQ
642}
643
644static int
645is11bclient(const uint8_t *rates, const uint8_t *xrates)
646{
647	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
648	int i;
649
650	/* NB: the 11b clients we care about will not have xrates */
651	if (xrates != NULL || rates == NULL)
652		return 0;
653	for (i = 0; i < rates[1]; i++) {
654		int r = rates[2+i] & IEEE80211_RATE_VAL;
655		if (r > 2*11 || ((1<<r) & brates) == 0)
656			return 0;
657	}
658	return 1;
659}
660
661static void
662adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
663	int subtype, int rssi, int noise, uint32_t rstamp)
664{
665	struct ieee80211vap *vap = ni->ni_vap;
666	struct ieee80211com *ic = ni->ni_ic;
667	struct ieee80211_frame *wh;
668	uint8_t *frm, *efrm, *sfrm;
669	uint8_t *ssid, *rates, *xrates;
670
671	wh = mtod(m0, struct ieee80211_frame *);
672	frm = (uint8_t *)&wh[1];
673	efrm = mtod(m0, uint8_t *) + m0->m_len;
674	switch (subtype) {
675	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
676	case IEEE80211_FC0_SUBTYPE_BEACON: {
677		struct ieee80211_scanparams scan;
678		/*
679		 * We process beacon/probe response
680		 * frames to discover neighbors.
681		 */
682		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
683			return;
684		/*
685		 * Count frame now that we know it's to be processed.
686		 */
687		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
688			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
689			IEEE80211_NODE_STAT(ni, rx_beacons);
690		} else
691			IEEE80211_NODE_STAT(ni, rx_proberesp);
692		/*
693		 * If scanning, just pass information to the scan module.
694		 */
695		if (ic->ic_flags & IEEE80211_F_SCAN) {
696			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
697				/*
698				 * Actively scanning a channel marked passive;
699				 * send a probe request now that we know there
700				 * is 802.11 traffic present.
701				 *
702				 * XXX check if the beacon we recv'd gives
703				 * us what we need and suppress the probe req
704				 */
705				ieee80211_probe_curchan(vap, 1);
706				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
707			}
708			ieee80211_add_scan(vap, &scan, wh,
709				subtype, rssi, noise, rstamp);
710			return;
711		}
712		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
713			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
714				/*
715				 * Create a new entry in the neighbor table.
716				 */
717				ni = ieee80211_add_neighbor(vap, wh, &scan);
718			} else if (ni->ni_capinfo == 0) {
719				/*
720				 * Update faked node created on transmit.
721				 * Note this also updates the tsf.
722				 */
723				ieee80211_init_neighbor(ni, wh, &scan);
724			} else {
725				/*
726				 * Record tsf for potential resync.
727				 */
728				memcpy(ni->ni_tstamp.data, scan.tstamp,
729					sizeof(ni->ni_tstamp));
730			}
731			if (ni != NULL) {
732				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
733				ni->ni_noise = noise;
734				ni->ni_rstamp = rstamp;
735			}
736		}
737		break;
738	}
739
740	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
741		if (vap->iv_state != IEEE80211_S_RUN) {
742			vap->iv_stats.is_rx_mgtdiscard++;
743			return;
744		}
745		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
746			/* frame must be directed */
747			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
748			return;
749		}
750
751		/*
752		 * prreq frame format
753		 *	[tlv] ssid
754		 *	[tlv] supported rates
755		 *	[tlv] extended supported rates
756		 */
757		ssid = rates = xrates = NULL;
758		sfrm = frm;
759		while (efrm - frm > 1) {
760			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
761			switch (*frm) {
762			case IEEE80211_ELEMID_SSID:
763				ssid = frm;
764				break;
765			case IEEE80211_ELEMID_RATES:
766				rates = frm;
767				break;
768			case IEEE80211_ELEMID_XRATES:
769				xrates = frm;
770				break;
771			}
772			frm += frm[1] + 2;
773		}
774		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
775		if (xrates != NULL)
776			IEEE80211_VERIFY_ELEMENT(xrates,
777				IEEE80211_RATE_MAXSIZE - rates[1], return);
778		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
779		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
780		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
781			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
782			    wh, NULL,
783			    "%s", "no ssid with ssid suppression enabled");
784			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
785			return;
786		}
787
788		/* XXX find a better class or define it's own */
789		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
790		    "%s", "recv probe req");
791		/*
792		 * Some legacy 11b clients cannot hack a complete
793		 * probe response frame.  When the request includes
794		 * only a bare-bones rate set, communicate this to
795		 * the transmit side.
796		 */
797		ieee80211_send_proberesp(vap, wh->i_addr2,
798		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
799		break;
800
801	case IEEE80211_FC0_SUBTYPE_ACTION: {
802		const struct ieee80211_action *ia;
803
804		if (vap->iv_state != IEEE80211_S_RUN) {
805			vap->iv_stats.is_rx_mgtdiscard++;
806			return;
807		}
808		/*
809		 * action frame format:
810		 *	[1] category
811		 *	[1] action
812		 *	[tlv] parameters
813		 */
814		IEEE80211_VERIFY_LENGTH(efrm - frm,
815			sizeof(struct ieee80211_action), return);
816		ia = (const struct ieee80211_action *) frm;
817
818		vap->iv_stats.is_rx_action++;
819		IEEE80211_NODE_STAT(ni, rx_action);
820
821		/* verify frame payloads but defer processing */
822		/* XXX maybe push this to method */
823		switch (ia->ia_category) {
824		case IEEE80211_ACTION_CAT_BA:
825			switch (ia->ia_action) {
826			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
827				IEEE80211_VERIFY_LENGTH(efrm - frm,
828				    sizeof(struct ieee80211_action_ba_addbarequest),
829				    return);
830				break;
831			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
832				IEEE80211_VERIFY_LENGTH(efrm - frm,
833				    sizeof(struct ieee80211_action_ba_addbaresponse),
834				    return);
835				break;
836			case IEEE80211_ACTION_BA_DELBA:
837				IEEE80211_VERIFY_LENGTH(efrm - frm,
838				    sizeof(struct ieee80211_action_ba_delba),
839				    return);
840				break;
841			}
842			break;
843		case IEEE80211_ACTION_CAT_HT:
844			switch (ia->ia_action) {
845			case IEEE80211_ACTION_HT_TXCHWIDTH:
846				IEEE80211_VERIFY_LENGTH(efrm - frm,
847				    sizeof(struct ieee80211_action_ht_txchwidth),
848				    return);
849				break;
850			}
851			break;
852		}
853		ic->ic_recv_action(ni, frm, efrm);
854		break;
855	}
856
857	case IEEE80211_FC0_SUBTYPE_AUTH:
858	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
859	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
860	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
861	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
862	case IEEE80211_FC0_SUBTYPE_DEAUTH:
863	case IEEE80211_FC0_SUBTYPE_DISASSOC:
864		vap->iv_stats.is_rx_mgtdiscard++;
865		return;
866
867	default:
868		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
869		     wh, "mgt", "subtype 0x%x not handled", subtype);
870		vap->iv_stats.is_rx_badsubtype++;
871		break;
872	}
873}
874#undef IEEE80211_VERIFY_LENGTH
875#undef IEEE80211_VERIFY_ELEMENT
876