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