Home | History | Annotate | Line # | Download | only in net80211
ieee80211_input.c revision 1.16
      1 /*	$NetBSD: ieee80211_input.c,v 1.16 2003/12/14 09:56:53 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_input.c,v 1.12 2003/10/17 23:59:11 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_input.c,v 1.16 2003/12/14 09:56:53 dyoung Exp $");
     39 #endif
     40 
     41 #include "opt_inet.h"
     42 
     43 #ifdef __NetBSD__
     44 #include "bpfilter.h"
     45 #endif /* __NetBSD__ */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/socket.h>
     53 #include <sys/sockio.h>
     54 #include <sys/endian.h>
     55 #include <sys/errno.h>
     56 #ifdef __FreeBSD__
     57 #include <sys/bus.h>
     58 #endif
     59 #include <sys/proc.h>
     60 #include <sys/sysctl.h>
     61 
     62 #ifdef __FreeBSD__
     63 #include <machine/atomic.h>
     64 #endif
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_media.h>
     69 #include <net/if_arp.h>
     70 #ifdef __FreeBSD__
     71 #include <net/ethernet.h>
     72 #else
     73 #include <net/if_ether.h>
     74 #endif
     75 #include <net/if_llc.h>
     76 
     77 #include <net80211/ieee80211_var.h>
     78 #include <net80211/ieee80211_compat.h>
     79 
     80 #if NBPFILTER > 0
     81 #include <net/bpf.h>
     82 #endif
     83 
     84 #ifdef INET
     85 #include <netinet/in.h>
     86 #ifdef __FreeBSD__
     87 #include <netinet/if_ether.h>
     88 #else
     89 #include <net/if_ether.h>
     90 #endif
     91 #endif
     92 
     93 static void ieee80211_recv_pspoll(struct ieee80211com *,
     94     struct mbuf *, int, u_int32_t);
     95 
     96 /*
     97  * Process a received frame.  The node associated with the sender
     98  * should be supplied.  If nothing was found in the node table then
     99  * the caller is assumed to supply a reference to ic_bss instead.
    100  * The RSSI and a timestamp are also supplied.  The RSSI data is used
    101  * during AP scanning to select a AP to associate with; it can have
    102  * any units so long as values have consistent units and higher values
    103  * mean ``better signal''.  The receive timestamp is currently not used
    104  * by the 802.11 layer.
    105  */
    106 void
    107 ieee80211_input(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni,
    108 	int rssi, u_int32_t rstamp)
    109 {
    110 	struct ieee80211com *ic = (void *)ifp;
    111 	struct ieee80211_frame *wh;
    112 	struct ether_header *eh;
    113 	struct mbuf *m1;
    114 	int len;
    115 	u_int8_t dir, type, subtype;
    116 	u_int8_t *bssid;
    117 	u_int16_t rxseq;
    118 	ALTQ_DECL(struct altq_pktattr pktattr;)
    119 
    120 	KASSERT(ni != NULL, ("null node"));
    121 
    122 	/* trim CRC here so WEP can find its own CRC at the end of packet. */
    123 	if (m->m_flags & M_HASFCS) {
    124 		m_adj(m, -IEEE80211_CRC_LEN);
    125 		m->m_flags &= ~M_HASFCS;
    126 	}
    127 	KASSERT(m->m_pkthdr.len >= sizeof(struct ieee80211_frame_min),
    128 		("frame length too short: %u", m->m_pkthdr.len));
    129 
    130 	/*
    131 	 * In monitor mode, send everything directly to bpf.
    132 	 * XXX may want to include the CRC
    133 	 */
    134 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
    135 		goto out;
    136 
    137 	wh = mtod(m, struct ieee80211_frame *);
    138 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
    139 	    IEEE80211_FC0_VERSION_0) {
    140 		if (ifp->if_flags & IFF_DEBUG)
    141 			if_printf(ifp, "receive packet with wrong version: %x\n",
    142 			    wh->i_fc[0]);
    143 		ieee80211_unref_node(&ni);
    144 		ic->ic_stats.is_rx_badversion++;
    145 		goto err;
    146 	}
    147 
    148 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
    149 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
    150 	/*
    151 	 * NB: We are not yet prepared to handle control frames,
    152 	 *     but permitting drivers to send them to us allows
    153 	 *     them to go through bpf tapping at the 802.11 layer.
    154 	 */
    155 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
    156 		/* XXX statistic */
    157 		IEEE80211_DPRINTF2(("%s: frame too short, len %u\n",
    158 			__func__, m->m_pkthdr.len));
    159 		ic->ic_stats.is_rx_tooshort++;
    160 		goto out;		/* XXX */
    161 	}
    162 	if (ic->ic_state != IEEE80211_S_SCAN) {
    163 		switch (ic->ic_opmode) {
    164 		case IEEE80211_M_STA:
    165 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) {
    166 				/* not interested in */
    167 				IEEE80211_DPRINTF2(("%s: discard frame from "
    168 					"bss %s\n", __func__,
    169 					ether_sprintf(wh->i_addr2)));
    170 				ic->ic_stats.is_rx_wrongbss++;
    171 				goto out;
    172 			}
    173 			break;
    174 		case IEEE80211_M_IBSS:
    175 		case IEEE80211_M_AHDEMO:
    176 		case IEEE80211_M_HOSTAP:
    177 			if (dir == IEEE80211_FC1_DIR_NODS)
    178 				bssid = wh->i_addr3;
    179 			else
    180 				bssid = wh->i_addr1;
    181 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
    182 			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr) &&
    183 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    184 			    IEEE80211_FC0_TYPE_DATA) {
    185 				/* not interested in */
    186 				IEEE80211_DPRINTF2(("%s: discard frame from "
    187 					"bss %s\n", __func__,
    188 					ether_sprintf(bssid)));
    189 				ic->ic_stats.is_rx_wrongbss++;
    190 				goto out;
    191 			}
    192 			break;
    193 		case IEEE80211_M_MONITOR:
    194 			goto out;
    195 		default:
    196 			/* XXX catch bad values */
    197 			break;
    198 		}
    199 		ni->ni_rssi = rssi;
    200 		ni->ni_rstamp = rstamp;
    201 		rxseq = ni->ni_rxseq;
    202 		ni->ni_rxseq =
    203 		    le16toh(*(u_int16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
    204 		/* TODO: fragment */
    205 		if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
    206 		    rxseq == ni->ni_rxseq) {
    207 			/* duplicate, silently discarded */
    208 			ic->ic_stats.is_rx_dup++; /* XXX per-station stat */
    209 			goto out;
    210 		}
    211 		ni->ni_inact = 0;
    212 	}
    213 
    214 	if (ic->ic_set_tim != NULL &&
    215 	    (wh->i_fc[1] & IEEE80211_FC1_PWR_MGT)
    216 	    && ni->ni_pwrsave == 0) {
    217 		/* turn on power save mode */
    218 
    219 		if (ifp->if_flags & IFF_DEBUG)
    220 			printf("%s: power save mode on for %s\n",
    221 			    ifp->if_xname, ether_sprintf(wh->i_addr2));
    222 
    223 		ni->ni_pwrsave = IEEE80211_PS_SLEEP;
    224 	}
    225 	if (ic->ic_set_tim != NULL &&
    226 	    (wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) == 0 &&
    227 	    ni->ni_pwrsave != 0) {
    228 		/* turn off power save mode, dequeue stored packets */
    229 
    230 		ni->ni_pwrsave = 0;
    231 		if (ic->ic_set_tim)
    232 			ic->ic_set_tim(ic, ni->ni_associd, 0);
    233 
    234 		if (ifp->if_flags & IFF_DEBUG)
    235 			printf("%s: power save mode off for %s\n",
    236 			    ifp->if_xname, ether_sprintf(wh->i_addr2));
    237 
    238 		while (!IF_IS_EMPTY(&ni->ni_savedq)) {
    239 			struct mbuf *m;
    240 			IF_DEQUEUE(&ni->ni_savedq, m);
    241 			IF_ENQUEUE(&ic->ic_pwrsaveq, m);
    242 			(*ifp->if_start)(ifp);
    243 		}
    244 	}
    245 
    246 	switch (type) {
    247 	case IEEE80211_FC0_TYPE_DATA:
    248 		switch (ic->ic_opmode) {
    249 		case IEEE80211_M_STA:
    250 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
    251 				ic->ic_stats.is_rx_wrongdir++;
    252 				goto out;
    253 			}
    254 			if ((ifp->if_flags & IFF_SIMPLEX) &&
    255 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    256 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
    257 				/*
    258 				 * In IEEE802.11 network, multicast packet
    259 				 * sent from me is broadcasted from AP.
    260 				 * It should be silently discarded for
    261 				 * SIMPLEX interface.
    262 				 */
    263 				ic->ic_stats.is_rx_mcastecho++;
    264 				goto out;
    265 			}
    266 			break;
    267 		case IEEE80211_M_IBSS:
    268 		case IEEE80211_M_AHDEMO:
    269 			if (dir != IEEE80211_FC1_DIR_NODS) {
    270 				ic->ic_stats.is_rx_wrongdir++;
    271 				goto out;
    272 			}
    273 			break;
    274 		case IEEE80211_M_HOSTAP:
    275 			if (dir != IEEE80211_FC1_DIR_TODS) {
    276 				ic->ic_stats.is_rx_wrongdir++;
    277 				goto out;
    278 			}
    279 			/* check if source STA is associated */
    280 			if (ni == ic->ic_bss) {
    281 				IEEE80211_DPRINTF(("%s: data from unknown src "
    282 					"%s\n", __func__,
    283 					ether_sprintf(wh->i_addr2)));
    284 				/* NB: caller deals with reference */
    285 				ni = ieee80211_dup_bss(ic, wh->i_addr2);
    286 				if (ni != NULL) {
    287 					IEEE80211_SEND_MGMT(ic, ni,
    288 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
    289 					    IEEE80211_REASON_NOT_AUTHED);
    290 					ieee80211_free_node(ic, ni);
    291 				}
    292 				ic->ic_stats.is_rx_notassoc++;
    293 				goto err;
    294 			}
    295 			if (ni->ni_associd == 0) {
    296 				IEEE80211_DPRINTF(("ieee80211_input: "
    297 				    "data from unassoc src %s\n",
    298 				    ether_sprintf(wh->i_addr2)));
    299 				IEEE80211_SEND_MGMT(ic, ni,
    300 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
    301 				    IEEE80211_REASON_NOT_ASSOCED);
    302 				ieee80211_unref_node(&ni);
    303 				ic->ic_stats.is_rx_notassoc++;
    304 				goto err;
    305 			}
    306 			break;
    307 		case IEEE80211_M_MONITOR:
    308 			break;
    309 		}
    310 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
    311 			if (ic->ic_flags & IEEE80211_F_WEPON) {
    312 				m = ieee80211_wep_crypt(ifp, m, 0);
    313 				if (m == NULL) {
    314 					ic->ic_stats.is_rx_wepfail++;
    315 					goto err;
    316 				}
    317 				wh = mtod(m, struct ieee80211_frame *);
    318 			} else {
    319 				ic->ic_stats.is_rx_nowep++;
    320 				goto out;
    321 			}
    322 		}
    323 #if NBPFILTER > 0
    324 		/* copy to listener after decrypt */
    325 		if (ic->ic_rawbpf)
    326 			bpf_mtap(ic->ic_rawbpf, m);
    327 #endif
    328 		m = ieee80211_decap(ifp, m);
    329 		if (m == NULL) {
    330 			IEEE80211_DPRINTF(("ieee80211_input: "
    331 			    "decapsulation error for src %s\n",
    332 			    ether_sprintf(wh->i_addr2)));
    333 			ic->ic_stats.is_rx_decap++;
    334 			goto err;
    335 		}
    336 		ifp->if_ipackets++;
    337 
    338 		/* perform as a bridge within the AP */
    339 		m1 = NULL;
    340 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    341 			eh = mtod(m, struct ether_header *);
    342 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    343 				m1 = m_copypacket(m, M_DONTWAIT);
    344 				if (m1 == NULL)
    345 					ifp->if_oerrors++;
    346 				else
    347 					m1->m_flags |= M_MCAST;
    348 			} else {
    349 				ni = ieee80211_find_node(ic, eh->ether_dhost);
    350 				if (ni != NULL) {
    351 					if (ni->ni_associd != 0) {
    352 						m1 = m;
    353 						m = NULL;
    354 					}
    355 					ieee80211_unref_node(&ni);
    356 				}
    357 			}
    358 			if (m1 != NULL) {
    359 #ifdef ALTQ
    360 				if (ALTQ_IS_ENABLED(&ifp->if_snd))
    361 					altq_etherclassify(&ifp->if_snd, m1,
    362 					    &pktattr);
    363 #endif
    364 				len = m1->m_pkthdr.len;
    365 				IF_ENQUEUE(&ifp->if_snd, m1);
    366 				if (m != NULL)
    367 					ifp->if_omcasts++;
    368 				ifp->if_obytes += len;
    369 			}
    370 		}
    371 		if (m != NULL) {
    372 #if NBPFILTER > 0
    373 			/*
    374 			 * If we forward packet into transmitter of the AP,
    375 			 * we don't need to duplicate for DLT_EN10MB.
    376 			 */
    377 			if (ifp->if_bpf && m1 == NULL)
    378 				bpf_mtap(ifp->if_bpf, m);
    379 #endif
    380 			(*ifp->if_input)(ifp, m);
    381 		}
    382 		return;
    383 
    384 	case IEEE80211_FC0_TYPE_MGT:
    385 		if (dir != IEEE80211_FC1_DIR_NODS) {
    386 			ic->ic_stats.is_rx_wrongdir++;
    387 			goto err;
    388 		}
    389 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
    390 			ic->ic_stats.is_rx_ahdemo_mgt++;
    391 			goto out;
    392 		}
    393 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    394 
    395 		/* drop frames without interest */
    396 		if (ic->ic_state == IEEE80211_S_SCAN) {
    397 			if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
    398 			    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
    399 				ic->ic_stats.is_rx_mgtdiscard++;
    400 				goto out;
    401 			}
    402 		} else {
    403 			if (ic->ic_opmode != IEEE80211_M_IBSS &&
    404 			    subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
    405 				ic->ic_stats.is_rx_mgtdiscard++;
    406 				goto out;
    407 			}
    408 		}
    409 
    410 		if (ifp->if_flags & IFF_DEBUG) {
    411 			/* avoid to print too many frames */
    412 			int doprint = 0;
    413 
    414 			switch (subtype) {
    415 			case IEEE80211_FC0_SUBTYPE_BEACON:
    416 				if (ic->ic_state == IEEE80211_S_SCAN)
    417 					doprint = 1;
    418 				break;
    419 			case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    420 				if (ic->ic_opmode == IEEE80211_M_IBSS)
    421 					doprint = 1;
    422 				break;
    423 			default:
    424 				doprint = 1;
    425 				break;
    426 			}
    427 #ifdef IEEE80211_DEBUG
    428 			doprint += ieee80211_debug;
    429 #endif
    430 			if (doprint)
    431 				if_printf(ifp, "received %s from %s rssi %d\n",
    432 				    ieee80211_mgt_subtype_name[subtype
    433 				    >> IEEE80211_FC0_SUBTYPE_SHIFT],
    434 				    ether_sprintf(wh->i_addr2), rssi);
    435 		}
    436 #if NBPFILTER > 0
    437 		if (ic->ic_rawbpf)
    438 			bpf_mtap(ic->ic_rawbpf, m);
    439 #endif
    440 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
    441 		m_freem(m);
    442 		return;
    443 
    444 	case IEEE80211_FC0_TYPE_CTL:
    445 		ic->ic_stats.is_rx_ctl++;
    446 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
    447 			goto out;
    448 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    449 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL) {
    450 			/* XXX statistic */
    451 			/* Dump out a single packet from the host */
    452 			if (ifp->if_flags & IFF_DEBUG)
    453 				printf("%s: got power save probe from %s\n",
    454 				    ifp->if_xname,
    455 				    ether_sprintf(wh->i_addr2));
    456 			ieee80211_recv_pspoll(ic, m, rssi, rstamp);
    457 		}
    458 		goto out;
    459 
    460 	default:
    461 		IEEE80211_DPRINTF(("%s: bad type %x\n", __func__, type));
    462 		/* should not come here */
    463 		break;
    464 	}
    465   err:
    466 	ifp->if_ierrors++;
    467   out:
    468 	if (m != NULL) {
    469 #if NBPFILTER > 0
    470 		if (ic->ic_rawbpf)
    471 			bpf_mtap(ic->ic_rawbpf, m);
    472 #endif
    473 		m_freem(m);
    474 	}
    475 }
    476 
    477 struct mbuf *
    478 ieee80211_decap(struct ifnet *ifp, struct mbuf *m)
    479 {
    480 	struct ether_header *eh;
    481 	struct ieee80211_frame wh;
    482 	struct llc *llc;
    483 
    484 	if (m->m_len < sizeof(wh) + sizeof(*llc)) {
    485 		m = m_pullup(m, sizeof(wh) + sizeof(*llc));
    486 		if (m == NULL)
    487 			return NULL;
    488 	}
    489 	memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
    490 	llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
    491 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
    492 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
    493 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
    494 		m_adj(m, sizeof(wh) + sizeof(struct llc) - sizeof(*eh));
    495 		llc = NULL;
    496 	} else {
    497 		m_adj(m, sizeof(wh) - sizeof(*eh));
    498 	}
    499 	eh = mtod(m, struct ether_header *);
    500 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    501 	case IEEE80211_FC1_DIR_NODS:
    502 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
    503 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
    504 		break;
    505 	case IEEE80211_FC1_DIR_TODS:
    506 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
    507 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
    508 		break;
    509 	case IEEE80211_FC1_DIR_FROMDS:
    510 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
    511 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
    512 		break;
    513 	case IEEE80211_FC1_DIR_DSTODS:
    514 		/* not yet supported */
    515 		IEEE80211_DPRINTF(("%s: DS to DS\n", __func__));
    516 		m_freem(m);
    517 		return NULL;
    518 	}
    519 #ifdef ALIGNED_POINTER
    520 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
    521 		struct mbuf *n, *n0, **np;
    522 		caddr_t newdata;
    523 		int off, pktlen;
    524 
    525 		n0 = NULL;
    526 		np = &n0;
    527 		off = 0;
    528 		pktlen = m->m_pkthdr.len;
    529 		while (pktlen > off) {
    530 			if (n0 == NULL) {
    531 				MGETHDR(n, M_DONTWAIT, MT_DATA);
    532 				if (n == NULL) {
    533 					m_freem(m);
    534 					return NULL;
    535 				}
    536 #ifdef __FreeBSD__
    537 				M_MOVE_PKTHDR(n, m);
    538 #else
    539 				M_COPY_PKTHDR(n, m);
    540 #endif
    541 				n->m_len = MHLEN;
    542 			} else {
    543 				MGET(n, M_DONTWAIT, MT_DATA);
    544 				if (n == NULL) {
    545 					m_freem(m);
    546 					m_freem(n0);
    547 					return NULL;
    548 				}
    549 				n->m_len = MLEN;
    550 			}
    551 			if (pktlen - off >= MINCLSIZE) {
    552 				MCLGET(n, M_DONTWAIT);
    553 				if (n->m_flags & M_EXT)
    554 					n->m_len = n->m_ext.ext_size;
    555 			}
    556 			if (n0 == NULL) {
    557 				newdata =
    558 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
    559 				    sizeof(*eh);
    560 				n->m_len -= newdata - n->m_data;
    561 				n->m_data = newdata;
    562 			}
    563 			if (n->m_len > pktlen - off)
    564 				n->m_len = pktlen - off;
    565 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
    566 			off += n->m_len;
    567 			*np = n;
    568 			np = &n->m_next;
    569 		}
    570 		m_freem(m);
    571 		m = n0;
    572 	}
    573 #endif /* ALIGNED_POINTER */
    574 	if (llc != NULL) {
    575 		eh = mtod(m, struct ether_header *);
    576 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
    577 	}
    578 	return m;
    579 }
    580 
    581 /*
    582  * Install received rate set information in the node's state block.
    583  */
    584 static int
    585 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
    586 	u_int8_t *rates, u_int8_t *xrates, int flags)
    587 {
    588 	struct ieee80211_rateset *rs = &ni->ni_rates;
    589 
    590 	memset(rs, 0, sizeof(*rs));
    591 	rs->rs_nrates = rates[1];
    592 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
    593 	if (xrates != NULL) {
    594 		u_int8_t nxrates;
    595 		/*
    596 		 * Tack on 11g extended supported rate element.
    597 		 */
    598 		nxrates = xrates[1];
    599 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
    600 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
    601 			IEEE80211_DPRINTF(("%s: extended rate set too large;"
    602 				" only using %u of %u rates\n",
    603 				__func__, nxrates, xrates[1]));
    604 			ic->ic_stats.is_rx_rstoobig++;
    605 		}
    606 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
    607 		rs->rs_nrates += nxrates;
    608 	}
    609 	return ieee80211_fix_rate(ic, ni, flags);
    610 }
    611 
    612 /* Verify the existence and length of __elem or get out. */
    613 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
    614 	if ((__elem) == NULL) {						\
    615 		IEEE80211_DPRINTF(("%s: no " #__elem "in %s frame\n",	\
    616 			__func__, ieee80211_mgt_subtype_name[subtype >>	\
    617 				IEEE80211_FC0_SUBTYPE_SHIFT]));		\
    618 		ic->ic_stats.is_rx_elem_missing++;			\
    619 		return;							\
    620 	}								\
    621 	if ((__elem)[1] > (__maxlen)) {					\
    622 		IEEE80211_DPRINTF(("%s: bad " #__elem " len %d in %s "	\
    623 			"frame from %s\n", __func__, (__elem)[1],	\
    624 			ieee80211_mgt_subtype_name[subtype >>		\
    625 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
    626 			ether_sprintf(wh->i_addr2)));			\
    627 		ic->ic_stats.is_rx_elem_toobig++;			\
    628 		return;							\
    629 	}								\
    630 } while (0)
    631 
    632 #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
    633 	if ((_len) < (_minlen)) {					\
    634 		IEEE80211_DPRINTF(("%s: %s frame too short from %s\n",	\
    635 			__func__,					\
    636 			ieee80211_mgt_subtype_name[subtype >>		\
    637 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
    638 			ether_sprintf(wh->i_addr2)));			\
    639 		ic->ic_stats.is_rx_elem_toosmall++;			\
    640 		return;							\
    641 	}								\
    642 } while (0)
    643 
    644 static void
    645 ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
    646     struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq,
    647     u_int16_t status)
    648 {
    649 	struct ifnet *ifp = &ic->ic_if;
    650 	int allocbs;
    651 	switch (ic->ic_opmode) {
    652 	case IEEE80211_M_IBSS:
    653 		if (ic->ic_state != IEEE80211_S_RUN ||
    654 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
    655 			ic->ic_stats.is_rx_bad_auth++;
    656 			return;
    657 		}
    658 		ieee80211_new_state(ic, IEEE80211_S_AUTH,
    659 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
    660 		break;
    661 
    662 	case IEEE80211_M_AHDEMO:
    663 		/* should not come here */
    664 		break;
    665 
    666 	case IEEE80211_M_HOSTAP:
    667 		if (ic->ic_state != IEEE80211_S_RUN ||
    668 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
    669 			ic->ic_stats.is_rx_bad_auth++;
    670 			return;
    671 		}
    672 		if (ni == ic->ic_bss) {
    673 			ni = ieee80211_alloc_node(ic, wh->i_addr2);
    674 			if (ni == NULL) {
    675 				ic->ic_stats.is_rx_nodealloc++;
    676 				return;
    677 			}
    678 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
    679 			ni->ni_rssi = rssi;
    680 			ni->ni_rstamp = rstamp;
    681 			ni->ni_chan = ic->ic_bss->ni_chan;
    682 			allocbs = 1;
    683 		} else
    684 			allocbs = 0;
    685 		IEEE80211_SEND_MGMT(ic, ni,
    686 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
    687 		if (ifp->if_flags & IFF_DEBUG)
    688 			if_printf(ifp, "station %s %s authenticated\n",
    689 			    ether_sprintf(ni->ni_macaddr),
    690 			    (allocbs ? "newly" : "already"));
    691 		break;
    692 
    693 	case IEEE80211_M_STA:
    694 		if (ic->ic_state != IEEE80211_S_AUTH ||
    695 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
    696 			ic->ic_stats.is_rx_bad_auth++;
    697 			return;
    698 		}
    699 		if (status != 0) {
    700 			if_printf(&ic->ic_if,
    701 			    "authentication failed (reason %d) for %s\n",
    702 			    status,
    703 			    ether_sprintf(wh->i_addr3));
    704 			if (ni != ic->ic_bss)
    705 				ni->ni_fails++;
    706 			ic->ic_stats.is_rx_auth_fail++;
    707 			return;
    708 		}
    709 		ieee80211_new_state(ic, IEEE80211_S_ASSOC,
    710 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
    711 		break;
    712 	case IEEE80211_M_MONITOR:
    713 		break;
    714 	}
    715 }
    716 
    717 /* TBD send appropriate responses on error? */
    718 static void
    719 ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
    720     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
    721     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
    722 {
    723 	struct ifnet *ifp = &ic->ic_if;
    724 	u_int8_t *challenge = NULL;
    725 	int allocbs, i;
    726 
    727 	if ((ic->ic_flags & IEEE80211_F_WEPON) == 0) {
    728 		IEEE80211_DPRINTF(("%s: WEP is off\n", __func__));
    729 		return;
    730 	}
    731 
    732 	if (frm + 1 < efrm) {
    733 		if ((frm[1] + 2) > (efrm - frm)) {
    734 			IEEE80211_DPRINTF(("elt %d %d bytes too long\n",
    735 			    frm[0], (frm[1] + 2) - (efrm - frm)));
    736 			ic->ic_stats.is_rx_bad_auth++;
    737 			return;
    738 		}
    739 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
    740 			challenge = frm;
    741 		frm += frm[1] + 2;
    742 	}
    743 	switch (seq) {
    744 	case IEEE80211_AUTH_SHARED_CHALLENGE:
    745 	case IEEE80211_AUTH_SHARED_RESPONSE:
    746 		if (challenge == NULL) {
    747 			IEEE80211_DPRINTF(("%s: no challenge sent\n",
    748 			    __func__));
    749 			ic->ic_stats.is_rx_bad_auth++;
    750 			return;
    751 		}
    752 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
    753 			IEEE80211_DPRINTF(("%s: bad challenge len %d\n",
    754 			    __func__, challenge[1]));
    755 			ic->ic_stats.is_rx_bad_auth++;
    756 			return;
    757 		}
    758 	default:
    759 		break;
    760 	}
    761 	switch (ic->ic_opmode) {
    762 	case IEEE80211_M_MONITOR:
    763 	case IEEE80211_M_AHDEMO:
    764 	case IEEE80211_M_IBSS:
    765 		IEEE80211_DPRINTF(("%s: unexpected operating mode\n",
    766 		    __func__));
    767 		return;
    768 	case IEEE80211_M_HOSTAP:
    769 		if (ic->ic_state != IEEE80211_S_RUN) {
    770 			IEEE80211_DPRINTF(("%s: not running\n", __func__));
    771 			return;
    772 		}
    773 		switch (seq) {
    774 		case IEEE80211_AUTH_SHARED_REQUEST:
    775 			if (ni == ic->ic_bss) {
    776 				ni = ieee80211_alloc_node(ic, wh->i_addr2);
    777 				if (ni == NULL) {
    778 					ic->ic_stats.is_rx_nodealloc++;
    779 					return;
    780 				}
    781 				IEEE80211_ADDR_COPY(ni->ni_bssid,
    782 				    ic->ic_bss->ni_bssid);
    783 				ni->ni_rssi = rssi;
    784 				ni->ni_rstamp = rstamp;
    785 				ni->ni_chan = ic->ic_bss->ni_chan;
    786 				allocbs = 1;
    787 			} else
    788 				allocbs = 0;
    789 			if (ni->ni_challenge == NULL)
    790 				ni->ni_challenge = (u_int32_t*)malloc(
    791 				    IEEE80211_CHALLENGE_LEN, M_DEVBUF,
    792 				    M_NOWAIT);
    793 			if (ni->ni_challenge == NULL) {
    794 				IEEE80211_DPRINTF(("challenge alloc failed\n"));
    795 				/* XXX statistic */
    796 				return;
    797 			}
    798 			for (i = IEEE80211_CHALLENGE_LEN / sizeof(u_int32_t);
    799 			     --i >= 0; )
    800 				ni->ni_challenge[i] = arc4random();
    801 			break;
    802 		case IEEE80211_AUTH_SHARED_RESPONSE:
    803 			if (ni == ic->ic_bss) {
    804 				IEEE80211_DPRINTF(("%s: unknown STA\n",
    805 				    __func__));
    806 				return;
    807 			}
    808 			allocbs = 1;
    809 			if (ni->ni_challenge == NULL) {
    810 				IEEE80211_DPRINTF((
    811 				    "%s: no challenge recorded\n", __func__));
    812 				ic->ic_stats.is_rx_bad_auth++;
    813 				return;
    814 			}
    815 			if (memcmp(ni->ni_challenge, &challenge[2],
    816 			           challenge[1]) != 0) {
    817 				IEEE80211_DPRINTF(("%s: challenge mismatch\n",
    818 				    __func__));
    819 				ic->ic_stats.is_rx_auth_fail++;
    820 				return;
    821 			}
    822 			break;
    823 		default:
    824 			IEEE80211_DPRINTF(("%s: bad seq %d from %s\n",
    825 			    __func__, seq, ether_sprintf(wh->i_addr2)));
    826 			ic->ic_stats.is_rx_bad_auth++;
    827 			return;
    828 		}
    829 		IEEE80211_SEND_MGMT(ic, ni,
    830 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
    831 		if (ifp->if_flags & IFF_DEBUG)
    832 			if_printf(ifp, "station %s %s authenticated\n",
    833 			    ether_sprintf(ni->ni_macaddr),
    834 			    (allocbs ? "newly" : "already"));
    835 		break;
    836 
    837 	case IEEE80211_M_STA:
    838 		if (ic->ic_state != IEEE80211_S_AUTH)
    839 			return;
    840 		switch (seq) {
    841 		case IEEE80211_AUTH_SHARED_PASS:
    842 			if (ni->ni_challenge != NULL) {
    843 				FREE(ni->ni_challenge, M_DEVBUF);
    844 				ni->ni_challenge = NULL;
    845 			}
    846 			if (status != 0) {
    847 				if_printf(&ic->ic_if,
    848 				    "%s: auth failed (reason %d) for %s\n",
    849 				    __func__, status,
    850 				    ether_sprintf(wh->i_addr3));
    851 				if (ni != ic->ic_bss)
    852 					ni->ni_fails++;
    853 				ic->ic_stats.is_rx_auth_fail++;
    854 				return;
    855 			}
    856 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
    857 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
    858 			break;
    859 		case IEEE80211_AUTH_SHARED_CHALLENGE:
    860 			if (ni->ni_challenge == NULL)
    861 				ni->ni_challenge = (u_int32_t*)malloc(
    862 				    challenge[1], M_DEVBUF, M_NOWAIT);
    863 			if (ni->ni_challenge == NULL) {
    864 				IEEE80211_DPRINTF((
    865 				    "%s: challenge alloc failed\n", __func__));
    866 				/* XXX statistic */
    867 				return;
    868 			}
    869 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
    870 			break;
    871 		default:
    872 			IEEE80211_DPRINTF(("%s: bad seq %d from %s\n",
    873 			    __func__, seq, ether_sprintf(wh->i_addr2)));
    874 			ic->ic_stats.is_rx_bad_auth++;
    875 			return;
    876 		}
    877 		break;
    878 	}
    879 }
    880 
    881 void
    882 ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
    883 	struct ieee80211_node *ni,
    884 	int subtype, int rssi, u_int32_t rstamp)
    885 {
    886 #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    887 	struct ifnet *ifp = &ic->ic_if;
    888 	struct ieee80211_frame *wh;
    889 	u_int8_t *frm, *efrm;
    890 	u_int8_t *ssid, *rates, *xrates;
    891 	int reassoc, resp, newassoc, allocbs;
    892 
    893 	wh = mtod(m0, struct ieee80211_frame *);
    894 	frm = (u_int8_t *)&wh[1];
    895 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
    896 	switch (subtype) {
    897 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    898 	case IEEE80211_FC0_SUBTYPE_BEACON: {
    899 		u_int8_t *tstamp, *bintval, *capinfo, *country;
    900 		u_int8_t chan, bchan, fhindex, erp;
    901 		u_int16_t fhdwell;
    902 
    903 		if (ic->ic_opmode != IEEE80211_M_IBSS &&
    904 		    ic->ic_state != IEEE80211_S_SCAN) {
    905 			/* XXX: may be useful for background scan */
    906 			return;
    907 		}
    908 
    909 		/*
    910 		 * beacon/probe response frame format
    911 		 *	[8] time stamp
    912 		 *	[2] beacon interval
    913 		 *	[2] capability information
    914 		 *	[tlv] ssid
    915 		 *	[tlv] supported rates
    916 		 *	[tlv] country information
    917 		 *	[tlv] parameter set (FH/DS)
    918 		 *	[tlv] erp information
    919 		 *	[tlv] extended supported rates
    920 		 */
    921 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
    922 		tstamp  = frm;	frm += 8;
    923 		bintval = frm;	frm += 2;
    924 		capinfo = frm;	frm += 2;
    925 		ssid = rates = xrates = country = NULL;
    926 		bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
    927 		chan = bchan;
    928 		fhdwell = 0;
    929 		fhindex = 0;
    930 		erp = 0;
    931 		while (frm < efrm) {
    932 			switch (*frm) {
    933 			case IEEE80211_ELEMID_SSID:
    934 				ssid = frm;
    935 				break;
    936 			case IEEE80211_ELEMID_RATES:
    937 				rates = frm;
    938 				break;
    939 			case IEEE80211_ELEMID_COUNTRY:
    940 				country = frm;
    941 				break;
    942 			case IEEE80211_ELEMID_FHPARMS:
    943 				if (ic->ic_phytype == IEEE80211_T_FH) {
    944 					fhdwell = (frm[3] << 8) | frm[2];
    945 					chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
    946 					fhindex = frm[6];
    947 				}
    948 				break;
    949 			case IEEE80211_ELEMID_DSPARMS:
    950 				/*
    951 				 * XXX hack this since depending on phytype
    952 				 * is problematic for multi-mode devices.
    953 				 */
    954 				if (ic->ic_phytype != IEEE80211_T_FH)
    955 					chan = frm[2];
    956 				break;
    957 			case IEEE80211_ELEMID_TIM:
    958 				break;
    959 			case IEEE80211_ELEMID_XRATES:
    960 				xrates = frm;
    961 				break;
    962 			case IEEE80211_ELEMID_ERP:
    963 				if (frm[1] != 1) {
    964 					IEEE80211_DPRINTF(("%s: invalid ERP "
    965 						"element; length %u, expecting "
    966 						"1\n", __func__, frm[1]));
    967 					ic->ic_stats.is_rx_elem_toobig++;
    968 					break;
    969 				}
    970 				erp = frm[2];
    971 				break;
    972 			default:
    973 				IEEE80211_DPRINTF2(("%s: element id %u/len %u "
    974 					"ignored\n", __func__, *frm, frm[1]));
    975 				ic->ic_stats.is_rx_elem_unknown++;
    976 				break;
    977 			}
    978 			frm += frm[1] + 2;
    979 		}
    980 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
    981 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
    982 		if (
    983 #if IEEE80211_CHAN_MAX < 255
    984 		    chan > IEEE80211_CHAN_MAX ||
    985 #endif
    986 		    isclr(ic->ic_chan_active, chan)) {
    987 			IEEE80211_DPRINTF(("%s: ignore %s with invalid channel "
    988 				"%u\n", __func__,
    989 				ISPROBE(subtype) ? "probe response" : "beacon",
    990 				chan));
    991 			ic->ic_stats.is_rx_badchan++;
    992 			return;
    993 		}
    994 		if (chan != bchan) {
    995 			/*
    996 			 * Frame was received on a channel different from the
    997 			 * one indicated in the DS/FH params element id;
    998 			 * silently discard it.
    999 			 *
   1000 			 * NB: this can happen due to signal leakage.
   1001 			 */
   1002 			IEEE80211_DPRINTF(("%s: ignore %s on channel %u marked "
   1003 				"for channel %u\n", __func__,
   1004 				ISPROBE(subtype) ? "probe response" : "beacon",
   1005 				bchan, chan));
   1006 			ic->ic_stats.is_rx_chanmismatch++;
   1007 			return;
   1008 		}
   1009 
   1010 		/*
   1011 		 * Use mac and channel for lookup so we collect all
   1012 		 * potential AP's when scanning.  Otherwise we may
   1013 		 * see the same AP on multiple channels and will only
   1014 		 * record the last one.  We could filter APs here based
   1015 		 * on rssi, etc. but leave that to the end of the scan
   1016 		 * so we can keep the selection criteria in one spot.
   1017 		 * This may result in a bloat of the scanned AP list but
   1018 		 * it shouldn't be too much.
   1019 		 */
   1020 		ni = ieee80211_lookup_node(ic, wh->i_addr2,
   1021 				&ic->ic_channels[chan]);
   1022 #ifdef IEEE80211_DEBUG
   1023 		if (ieee80211_debug &&
   1024 		    (ni == NULL || ic->ic_state == IEEE80211_S_SCAN)) {
   1025 			printf("%s: %s%s on chan %u (bss chan %u) ",
   1026 			    __func__, (ni == NULL ? "new " : ""),
   1027 			    ISPROBE(subtype) ? "probe response" : "beacon",
   1028 			    chan, bchan);
   1029 			ieee80211_print_essid(ssid + 2, ssid[1]);
   1030 			printf(" from %s\n", ether_sprintf(wh->i_addr2));
   1031 			printf("%s: caps 0x%x bintval %u erp 0x%x\n",
   1032 				__func__, le16toh(*(u_int16_t *)capinfo),
   1033 				le16toh(*(u_int16_t *)bintval), erp);
   1034 			if (country) {
   1035 				int i;
   1036 				printf("%s: country info", __func__);
   1037 				for (i = 0; i < country[1]; i++)
   1038 					printf(" %02x", country[i+2]);
   1039 				printf("\n");
   1040 			}
   1041 		}
   1042 #endif
   1043 		if (ni == NULL) {
   1044 			ni = ieee80211_alloc_node(ic, wh->i_addr2);
   1045 			if (ni == NULL) {
   1046 				ic->ic_stats.is_rx_nodealloc++;
   1047 				return;
   1048 			}
   1049 			ni->ni_esslen = ssid[1];
   1050 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
   1051 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
   1052 		} else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
   1053 			/*
   1054 			 * Update ESSID at probe response to adopt hidden AP by
   1055 			 * Lucent/Cisco, which announces null ESSID in beacon.
   1056 			 */
   1057 			ni->ni_esslen = ssid[1];
   1058 			memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
   1059 			memcpy(ni->ni_essid, ssid + 2, ssid[1]);
   1060 		}
   1061 		IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
   1062 		ni->ni_rssi = rssi;
   1063 		ni->ni_rstamp = rstamp;
   1064 		memcpy(ni->ni_tstamp, tstamp, sizeof(ni->ni_tstamp));
   1065 		ni->ni_intval = le16toh(*(u_int16_t *)bintval);
   1066 		ni->ni_capinfo = le16toh(*(u_int16_t *)capinfo);
   1067 		/* XXX validate channel # */
   1068 		ni->ni_chan = &ic->ic_channels[chan];
   1069 		ni->ni_fhdwell = fhdwell;
   1070 		ni->ni_fhindex = fhindex;
   1071 		ni->ni_erp = erp;
   1072 		/* NB: must be after ni_chan is setup */
   1073 		ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
   1074 		ieee80211_unref_node(&ni);
   1075 		break;
   1076 	}
   1077 
   1078 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ: {
   1079 		u_int8_t rate;
   1080 
   1081 		if (ic->ic_opmode == IEEE80211_M_STA)
   1082 			return;
   1083 		if (ic->ic_state != IEEE80211_S_RUN)
   1084 			return;
   1085 
   1086 		/*
   1087 		 * prreq frame format
   1088 		 *	[tlv] ssid
   1089 		 *	[tlv] supported rates
   1090 		 *	[tlv] extended supported rates
   1091 		 */
   1092 		ssid = rates = xrates = NULL;
   1093 		while (frm < efrm) {
   1094 			switch (*frm) {
   1095 			case IEEE80211_ELEMID_SSID:
   1096 				ssid = frm;
   1097 				break;
   1098 			case IEEE80211_ELEMID_RATES:
   1099 				rates = frm;
   1100 				break;
   1101 			case IEEE80211_ELEMID_XRATES:
   1102 				xrates = frm;
   1103 				break;
   1104 			}
   1105 			frm += frm[1] + 2;
   1106 		}
   1107 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
   1108 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
   1109 		if (ssid[1] != 0 &&
   1110 		    (ssid[1] != ic->ic_bss->ni_esslen ||
   1111 		    memcmp(ssid + 2, ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen) != 0)) {
   1112 #ifdef IEEE80211_DEBUG
   1113 			if (ieee80211_debug) {
   1114 				printf("%s: ssid mismatch ", __func__);
   1115 				ieee80211_print_essid(ssid + 2, ssid[1]);
   1116 				printf(" from %s\n", ether_sprintf(wh->i_addr2));
   1117 			}
   1118 #endif
   1119 			ic->ic_stats.is_rx_ssidmismatch++;
   1120 			return;
   1121 		}
   1122 
   1123 		if (ni == ic->ic_bss) {
   1124 			ni = ieee80211_dup_bss(ic, wh->i_addr2);
   1125 			if (ni == NULL) {
   1126 				ic->ic_stats.is_rx_nodealloc++;
   1127 				return;
   1128 			}
   1129 			IEEE80211_DPRINTF(("%s: new req from %s\n",
   1130 				__func__, ether_sprintf(wh->i_addr2)));
   1131 			allocbs = 1;
   1132 		} else
   1133 			allocbs = 0;
   1134 		ni->ni_rssi = rssi;
   1135 		ni->ni_rstamp = rstamp;
   1136 		rate = ieee80211_setup_rates(ic, ni, rates, xrates,
   1137 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
   1138 				| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
   1139 		if (rate & IEEE80211_RATE_BASIC) {
   1140 			IEEE80211_DPRINTF(("%s: rate negotiation failed: %s\n",
   1141 				__func__,ether_sprintf(wh->i_addr2)));
   1142 		} else {
   1143 			IEEE80211_SEND_MGMT(ic, ni,
   1144 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
   1145 		}
   1146 		if (allocbs) {
   1147 			/* XXX just use free? */
   1148 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1149 				ieee80211_free_node(ic, ni);
   1150 			else
   1151 				ieee80211_unref_node(&ni);
   1152 		}
   1153 		break;
   1154 	}
   1155 
   1156 	case IEEE80211_FC0_SUBTYPE_AUTH: {
   1157 		u_int16_t algo, seq, status;
   1158 		/*
   1159 		 * auth frame format
   1160 		 *	[2] algorithm
   1161 		 *	[2] sequence
   1162 		 *	[2] status
   1163 		 *	[tlv*] challenge
   1164 		 */
   1165 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
   1166 		algo   = le16toh(*(u_int16_t *)frm);
   1167 		seq    = le16toh(*(u_int16_t *)(frm + 2));
   1168 		status = le16toh(*(u_int16_t *)(frm + 4));
   1169 		IEEE80211_DPRINTF(("%s: auth %d seq %d from %s\n",
   1170 		    __func__, algo, seq, ether_sprintf(wh->i_addr2)));
   1171 
   1172 		if (algo == IEEE80211_AUTH_ALG_SHARED)
   1173 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
   1174 			    rstamp, seq, status);
   1175 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
   1176 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
   1177 			    status);
   1178 		else {
   1179 			IEEE80211_DPRINTF(("%s: unsupported auth %d from %s\n",
   1180 				__func__, algo, ether_sprintf(wh->i_addr2)));
   1181 			ic->ic_stats.is_rx_auth_unsupported++;
   1182 			return;
   1183 		}
   1184 		break;
   1185 	}
   1186 
   1187 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
   1188 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
   1189 		u_int16_t capinfo, bintval;
   1190 
   1191 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
   1192 		    (ic->ic_state != IEEE80211_S_RUN))
   1193 			return;
   1194 
   1195 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
   1196 			reassoc = 1;
   1197 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
   1198 		} else {
   1199 			reassoc = 0;
   1200 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
   1201 		}
   1202 		/*
   1203 		 * asreq frame format
   1204 		 *	[2] capability information
   1205 		 *	[2] listen interval
   1206 		 *	[6*] current AP address (reassoc only)
   1207 		 *	[tlv] ssid
   1208 		 *	[tlv] supported rates
   1209 		 *	[tlv] extended supported rates
   1210 		 */
   1211 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
   1212 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
   1213 			IEEE80211_DPRINTF(("%s: ignore other bss from %s\n",
   1214 				__func__, ether_sprintf(wh->i_addr2)));
   1215 			ic->ic_stats.is_rx_assoc_bss++;
   1216 			return;
   1217 		}
   1218 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
   1219 		bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
   1220 		if (reassoc)
   1221 			frm += 6;	/* ignore current AP info */
   1222 		ssid = rates = xrates = NULL;
   1223 		while (frm < efrm) {
   1224 			switch (*frm) {
   1225 			case IEEE80211_ELEMID_SSID:
   1226 				ssid = frm;
   1227 				break;
   1228 			case IEEE80211_ELEMID_RATES:
   1229 				rates = frm;
   1230 				break;
   1231 			case IEEE80211_ELEMID_XRATES:
   1232 				xrates = frm;
   1233 				break;
   1234 			}
   1235 			frm += frm[1] + 2;
   1236 		}
   1237 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
   1238 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
   1239 		if (ssid[1] != ic->ic_bss->ni_esslen ||
   1240 		    memcmp(ssid + 2, ic->ic_bss->ni_essid, ssid[1]) != 0) {
   1241 #ifdef IEEE80211_DEBUG
   1242 			if (ieee80211_debug) {
   1243 				printf("%s: ssid mismatch ", __func__);
   1244 				ieee80211_print_essid(ssid + 2, ssid[1]);
   1245 				printf(" from %s\n", ether_sprintf(wh->i_addr2));
   1246 			}
   1247 #endif
   1248 			ic->ic_stats.is_rx_ssidmismatch++;
   1249 			return;
   1250 		}
   1251 		if (ni == ic->ic_bss) {
   1252 			IEEE80211_DPRINTF(("%s: not authenticated for %s\n",
   1253 				__func__, ether_sprintf(wh->i_addr2)));
   1254 			ni = ieee80211_dup_bss(ic, wh->i_addr2);
   1255 			if (ni != NULL) {
   1256 				IEEE80211_SEND_MGMT(ic, ni,
   1257 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
   1258 				    IEEE80211_REASON_ASSOC_NOT_AUTHED);
   1259 				ieee80211_free_node(ic, ni);
   1260 			}
   1261 			ic->ic_stats.is_rx_assoc_notauth++;
   1262 			return;
   1263 		}
   1264 		/* discard challenge after association */
   1265 		if (ni->ni_challenge != NULL) {
   1266 			FREE(ni->ni_challenge, M_DEVBUF);
   1267 			ni->ni_challenge = NULL;
   1268 		}
   1269 		/* XXX per-node cipher suite */
   1270 		/* XXX some stations use the privacy bit for handling APs
   1271 		       that suport both encrypted and unencrypted traffic */
   1272 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0 ||
   1273 		    (capinfo & IEEE80211_CAPINFO_PRIVACY) !=
   1274 		    ((ic->ic_flags & IEEE80211_F_WEPON) ?
   1275 		     IEEE80211_CAPINFO_PRIVACY : 0)) {
   1276 			IEEE80211_DPRINTF(("%s: capability mismatch %x for %s\n",
   1277 				__func__, capinfo, ether_sprintf(wh->i_addr2)));
   1278 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1279 			ni->ni_associd = 0;
   1280 			IEEE80211_SEND_MGMT(ic, ni, resp,
   1281 				IEEE80211_STATUS_CAPINFO);
   1282 			ic->ic_stats.is_rx_assoc_capmismatch++;
   1283 			return;
   1284 		}
   1285 		ieee80211_setup_rates(ic, ni, rates, xrates,
   1286 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
   1287 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
   1288 		if (ni->ni_rates.rs_nrates == 0) {
   1289 			IEEE80211_DPRINTF(("%s: rate mismatch for %s\n",
   1290 				__func__, ether_sprintf(wh->i_addr2)));
   1291 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1292 			ni->ni_associd = 0;
   1293 			IEEE80211_SEND_MGMT(ic, ni, resp,
   1294 				IEEE80211_STATUS_BASIC_RATE);
   1295 			ic->ic_stats.is_rx_assoc_norate++;
   1296 			return;
   1297 		}
   1298 		ni->ni_rssi = rssi;
   1299 		ni->ni_rstamp = rstamp;
   1300 		ni->ni_intval = bintval;
   1301 		ni->ni_capinfo = capinfo;
   1302 		ni->ni_chan = ic->ic_bss->ni_chan;
   1303 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
   1304 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
   1305 		if (ni->ni_associd == 0) {
   1306 			u_int16_t aid;
   1307 
   1308 			/*
   1309 			 * It would be clever to search the bitmap
   1310 			 * more efficiently, but this will do for now.
   1311 			 */
   1312 			for (aid = 1; aid < ic->ic_max_aid; aid++) {
   1313 				if (!IEEE80211_AID_ISSET(aid,
   1314 				    ic->ic_aid_bitmap))
   1315 					break;
   1316 			}
   1317 
   1318 			if (ic->ic_bss->ni_associd >= ic->ic_max_aid) {
   1319 				IEEE80211_SEND_MGMT(ic, ni, resp,
   1320 				    IEEE80211_REASON_ASSOC_TOOMANY);
   1321 				return;
   1322 			} else {
   1323 				ni->ni_associd = aid | 0xc000;
   1324 				IEEE80211_AID_SET(ni->ni_associd,
   1325 				    ic->ic_aid_bitmap);
   1326 				newassoc = 1;
   1327 			}
   1328 		} else
   1329 			newassoc = 0;
   1330 		/* XXX for 11g must turn off short slot time if long
   1331 	           slot time sta associates */
   1332 		IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
   1333 		if (ifp->if_flags & IFF_DEBUG)
   1334 			if_printf(ifp, "station %s %s associated at aid %d\n",
   1335 			    (newassoc ? "newly" : "already"),
   1336 			    ether_sprintf(ni->ni_macaddr),
   1337 			    ni->ni_associd & ~0xc000);
   1338 		/* give driver a chance to setup state like ni_txrate */
   1339 		if (ic->ic_newassoc)
   1340 			(*ic->ic_newassoc)(ic, ni, newassoc);
   1341 		break;
   1342 	}
   1343 
   1344 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
   1345 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
   1346 		u_int16_t status;
   1347 
   1348 		if (ic->ic_opmode != IEEE80211_M_STA ||
   1349 		    ic->ic_state != IEEE80211_S_ASSOC)
   1350 			return;
   1351 
   1352 		/*
   1353 		 * asresp frame format
   1354 		 *	[2] capability information
   1355 		 *	[2] status
   1356 		 *	[2] association ID
   1357 		 *	[tlv] supported rates
   1358 		 *	[tlv] extended supported rates
   1359 		 */
   1360 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
   1361 		ni = ic->ic_bss;
   1362 		ni->ni_capinfo = le16toh(*(u_int16_t *)frm);
   1363 		frm += 2;
   1364 
   1365 		status = le16toh(*(u_int16_t *)frm);
   1366 		frm += 2;
   1367 		if (status != 0) {
   1368 			if (ifp->if_flags & IFF_DEBUG)
   1369 				if_printf(ifp,
   1370 				    "association failed (reason %d) for %s\n",
   1371 				    status, ether_sprintf(wh->i_addr3));
   1372 			if (ni != ic->ic_bss)
   1373 				ni->ni_fails++;
   1374 			ic->ic_stats.is_rx_auth_fail++;
   1375 			return;
   1376 		}
   1377 		ni->ni_associd = le16toh(*(u_int16_t *)frm);
   1378 		frm += 2;
   1379 
   1380 		rates = xrates = NULL;
   1381 		while (frm < efrm) {
   1382 			switch (*frm) {
   1383 			case IEEE80211_ELEMID_RATES:
   1384 				rates = frm;
   1385 				break;
   1386 			case IEEE80211_ELEMID_XRATES:
   1387 				xrates = frm;
   1388 				break;
   1389 			}
   1390 			frm += frm[1] + 2;
   1391 		}
   1392 
   1393 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
   1394 		ieee80211_setup_rates(ic, ni, rates, xrates,
   1395 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
   1396 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
   1397 		if (ni->ni_rates.rs_nrates != 0)
   1398 			ieee80211_new_state(ic, IEEE80211_S_RUN,
   1399 				wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
   1400 		break;
   1401 	}
   1402 
   1403 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
   1404 		u_int16_t reason;
   1405 		/*
   1406 		 * deauth frame format
   1407 		 *	[2] reason
   1408 		 */
   1409 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
   1410 		reason = le16toh(*(u_int16_t *)frm);
   1411 		ic->ic_stats.is_rx_deauth++;
   1412 		switch (ic->ic_opmode) {
   1413 		case IEEE80211_M_STA:
   1414 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
   1415 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
   1416 			break;
   1417 		case IEEE80211_M_HOSTAP:
   1418 			if (ni != ic->ic_bss) {
   1419 				if (ifp->if_flags & IFF_DEBUG)
   1420 					if_printf(ifp, "station %s deauthenticated"
   1421 					    " by peer (reason %d)\n",
   1422 					    ether_sprintf(ni->ni_macaddr), reason);
   1423 				/* node will be free'd on return */
   1424 				ieee80211_unref_node(&ni);
   1425 			}
   1426 			break;
   1427 		default:
   1428 			break;
   1429 		}
   1430 		break;
   1431 	}
   1432 
   1433 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
   1434 		u_int16_t reason;
   1435 		/*
   1436 		 * disassoc frame format
   1437 		 *	[2] reason
   1438 		 */
   1439 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
   1440 		reason = le16toh(*(u_int16_t *)frm);
   1441 		ic->ic_stats.is_rx_disassoc++;
   1442 		switch (ic->ic_opmode) {
   1443 		case IEEE80211_M_STA:
   1444 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
   1445 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
   1446 			break;
   1447 		case IEEE80211_M_HOSTAP:
   1448 			if (ni != ic->ic_bss) {
   1449 				if (ifp->if_flags & IFF_DEBUG)
   1450 					if_printf(ifp, "station %s disassociated"
   1451 					    " by peer (reason %d)\n",
   1452 					    ether_sprintf(ni->ni_macaddr), reason);
   1453 				IEEE80211_AID_CLR(ni->ni_associd,
   1454 				    ic->ic_aid_bitmap);
   1455 				ni->ni_associd = 0;
   1456 				/* XXX node reclaimed how? */
   1457 			}
   1458 			break;
   1459 		default:
   1460 			break;
   1461 		}
   1462 		break;
   1463 	}
   1464 	default:
   1465 		IEEE80211_DPRINTF(("%s: mgmt frame with subtype 0x%x not "
   1466 			"handled\n", __func__, subtype));
   1467 		ic->ic_stats.is_rx_badsubtype++;
   1468 		break;
   1469 	}
   1470 #undef ISPROBE
   1471 }
   1472 
   1473 static void
   1474 ieee80211_recv_pspoll(struct ieee80211com *ic, struct mbuf *m0, int rssi,
   1475 		      u_int32_t rstamp)
   1476 {
   1477 	struct ifnet *ifp = &ic->ic_if;
   1478 	struct ieee80211_frame *wh;
   1479 	struct ieee80211_node *ni;
   1480 	struct mbuf *m;
   1481 	u_int16_t aid;
   1482 
   1483 	if (ic->ic_set_tim == NULL)  /* No powersaving functionality */
   1484 		return;
   1485 
   1486 	wh = mtod(m0, struct ieee80211_frame *);
   1487 
   1488 	if ((ni = ieee80211_find_node(ic, wh->i_addr2)) == NULL) {
   1489 		if (ifp->if_flags & IFF_DEBUG)
   1490 			printf("%s: station %s sent bogus power save poll\n",
   1491 			       ifp->if_xname, ether_sprintf(wh->i_addr2));
   1492 		return;
   1493 	}
   1494 
   1495 	memcpy(&aid, wh->i_dur, sizeof(wh->i_dur));
   1496 	if ((aid & 0xc000) != 0xc000) {
   1497 		if (ifp->if_flags & IFF_DEBUG)
   1498 			printf("%s: station %s sent bogus aid %x\n",
   1499 			       ifp->if_xname, ether_sprintf(wh->i_addr2), aid);
   1500 		return;
   1501 	}
   1502 
   1503 	if (aid != ni->ni_associd) {
   1504 		if (ifp->if_flags & IFF_DEBUG)
   1505 			printf("%s: station %s aid %x doesn't match pspoll "
   1506 			       "aid %x\n",
   1507 			       ifp->if_xname, ether_sprintf(wh->i_addr2),
   1508 			       ni->ni_associd, aid);
   1509 		return;
   1510 	}
   1511 
   1512 	/* Okay, take the first queued packet and put it out... */
   1513 
   1514 	IF_DEQUEUE(&ni->ni_savedq, m);
   1515 	if (m == NULL) {
   1516 		if (ifp->if_flags & IFF_DEBUG)
   1517 			printf("%s: station %s sent pspoll, "
   1518 			       "but no packets are saved\n",
   1519 			       ifp->if_xname, ether_sprintf(wh->i_addr2));
   1520 		return;
   1521 	}
   1522 	wh = mtod(m, struct ieee80211_frame *);
   1523 
   1524 	/*
   1525 	 * If this is the last packet, turn off the TIM fields.
   1526 	 * If there are more packets, set the more packets bit.
   1527 	 */
   1528 
   1529 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
   1530 		if (ic->ic_set_tim)
   1531 			ic->ic_set_tim(ic, ni->ni_associd, 0);
   1532 	} else {
   1533 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
   1534 	}
   1535 
   1536 	if (ifp->if_flags & IFF_DEBUG)
   1537 		printf("%s: enqueued power saving packet for station %s\n",
   1538 		       ifp->if_xname, ether_sprintf(ni->ni_macaddr));
   1539 
   1540 	IF_ENQUEUE(&ic->ic_pwrsaveq, m);
   1541 	(*ifp->if_start)(ifp);
   1542 }
   1543 #undef IEEE80211_VERIFY_LENGTH
   1544 #undef IEEE80211_VERIFY_ELEMENT
   1545