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