1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/5.2/sys/net80211/ieee80211_input.c 121181 2003-10-17 23:59:11Z sam $");
35
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/bus.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50
51 #include <machine/atomic.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_llc.h>
59
60 #include <net80211/ieee80211_var.h>
61
62 #include <net/bpf.h>
63
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #endif
68
69 /*
70 * Process a received frame. The node associated with the sender
71 * should be supplied. If nothing was found in the node table then
72 * the caller is assumed to supply a reference to ic_bss instead.
73 * The RSSI and a timestamp are also supplied. The RSSI data is used
74 * during AP scanning to select a AP to associate with; it can have
75 * any units so long as values have consistent units and higher values
76 * mean ``better signal''. The receive timestamp is currently not used
77 * by the 802.11 layer.
78 */
79 void
80 ieee80211_input(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni,
81 int rssi, u_int32_t rstamp)
82 {
83 struct ieee80211com *ic = (void *)ifp;
84 struct ieee80211_frame *wh;
85 struct ether_header *eh;
86 struct mbuf *m1;
87 int len;
88 u_int8_t dir, type, subtype;
89 u_int8_t *bssid;
90 u_int16_t rxseq;
91
92 KASSERT(ni != NULL, ("null node"));
93
94 /* trim CRC here so WEP can find its own CRC at the end of packet. */
95 if (m->m_flags & M_HASFCS) {
96 m_adj(m, -IEEE80211_CRC_LEN);
97 m->m_flags &= ~M_HASFCS;
98 }
99 KASSERT(m->m_pkthdr.len >= sizeof(struct ieee80211_frame_min),
100 ("frame length too short: %u", m->m_pkthdr.len));
101
102 /*
103 * In monitor mode, send everything directly to bpf.
104 * XXX may want to include the CRC
105 */
106 if (ic->ic_opmode == IEEE80211_M_MONITOR)
107 goto out;
108
109 wh = mtod(m, struct ieee80211_frame *);
110 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
111 IEEE80211_FC0_VERSION_0) {
112 if (ifp->if_flags & IFF_DEBUG)
113 if_printf(ifp, "receive packet with wrong version: %x\n",
114 wh->i_fc[0]);
115 ieee80211_unref_node(&ni);
116 ic->ic_stats.is_rx_badversion++;
117 goto err;
118 }
119
120 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
121 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
122 /*
123 * NB: We are not yet prepared to handle control frames,
124 * but permitting drivers to send them to us allows
125 * them to go through bpf tapping at the 802.11 layer.
126 */
127 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
128 /* XXX statistic */
129 IEEE80211_DPRINTF2(("%s: frame too short, len %u\n",
130 __func__, m->m_pkthdr.len));
131 ic->ic_stats.is_rx_tooshort++;
132 goto out; /* XXX */
133 }
134 if (ic->ic_state != IEEE80211_S_SCAN) {
135 switch (ic->ic_opmode) {
136 case IEEE80211_M_STA:
137 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) {
138 /* not interested in */
139 IEEE80211_DPRINTF2(("%s: discard frame from "
140 "bss %s\n", __func__,
141 ether_sprintf(wh->i_addr2)));
142 ic->ic_stats.is_rx_wrongbss++;
143 goto out;
144 }
145 break;
146 case IEEE80211_M_IBSS:
147 case IEEE80211_M_AHDEMO:
148 case IEEE80211_M_HOSTAP:
149 if (dir == IEEE80211_FC1_DIR_NODS)
150 bssid = wh->i_addr3;
151 else
152 bssid = wh->i_addr1;
153 if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
154 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
155 /* not interested in */
156 IEEE80211_DPRINTF2(("%s: discard frame from "
157 "bss %s\n", __func__,
158 ether_sprintf(bssid)));
159 ic->ic_stats.is_rx_wrongbss++;
160 goto out;
161 }
162 break;
163 case IEEE80211_M_MONITOR:
164 goto out;
165 default:
166 /* XXX catch bad values */
167 break;
168 }
169 ni->ni_rssi = rssi;
170 ni->ni_rstamp = rstamp;
171 rxseq = ni->ni_rxseq;
172 ni->ni_rxseq =
173 le16toh(*(u_int16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
174 /* TODO: fragment */
175 if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
176 rxseq == ni->ni_rxseq) {
177 /* duplicate, silently discarded */
178 ic->ic_stats.is_rx_dup++; /* XXX per-station stat */
179 goto out;
180 }
181 ni->ni_inact = 0;
182 }
183
184 switch (type) {
185 case IEEE80211_FC0_TYPE_DATA:
186 switch (ic->ic_opmode) {
187 case IEEE80211_M_STA:
188 if (dir != IEEE80211_FC1_DIR_FROMDS) {
189 ic->ic_stats.is_rx_wrongdir++;
190 goto out;
191 }
192 if ((ifp->if_flags & IFF_SIMPLEX) &&
193 IEEE80211_IS_MULTICAST(wh->i_addr1) &&
194 IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
195 /*
196 * In IEEE802.11 network, multicast packet
197 * sent from me is broadcasted from AP.
198 * It should be silently discarded for
199 * SIMPLEX interface.
200 */
201 ic->ic_stats.is_rx_mcastecho++;
202 goto out;
203 }
204 break;
205 case IEEE80211_M_IBSS:
206 case IEEE80211_M_AHDEMO:
207 if (dir != IEEE80211_FC1_DIR_NODS) {
208 ic->ic_stats.is_rx_wrongdir++;
209 goto out;
210 }
211 break;
212 case IEEE80211_M_HOSTAP:
213 if (dir != IEEE80211_FC1_DIR_TODS) {
214 ic->ic_stats.is_rx_wrongdir++;
215 goto out;
216 }
217 /* check if source STA is associated */
218 if (ni == ic->ic_bss) {
219 IEEE80211_DPRINTF(("%s: data from unknown src "
220 "%s\n", __func__,
221 ether_sprintf(wh->i_addr2)));
222 /* NB: caller deals with reference */
223 ni = ieee80211_dup_bss(ic, wh->i_addr2);
224 if (ni != NULL) {
225 IEEE80211_SEND_MGMT(ic, ni,
226 IEEE80211_FC0_SUBTYPE_DEAUTH,
227 IEEE80211_REASON_NOT_AUTHED);
228 ieee80211_free_node(ic, ni);
229 }
230 ic->ic_stats.is_rx_notassoc++;
231 goto err;
232 }
233 if (ni->ni_associd == 0) {
234 IEEE80211_DPRINTF(("ieee80211_input: "
235 "data from unassoc src %s\n",
236 ether_sprintf(wh->i_addr2)));
237 IEEE80211_SEND_MGMT(ic, ni,
238 IEEE80211_FC0_SUBTYPE_DISASSOC,
239 IEEE80211_REASON_NOT_ASSOCED);
240 ieee80211_unref_node(&ni);
241 ic->ic_stats.is_rx_notassoc++;
242 goto err;
243 }
244 break;
245 case IEEE80211_M_MONITOR:
246 break;
247 }
248 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
249 if (ic->ic_flags & IEEE80211_F_WEPON) {
250 m = ieee80211_wep_crypt(ifp, m, 0);
251 if (m == NULL) {
252 ic->ic_stats.is_rx_wepfail++;
253 goto err;
254 }
255 wh = mtod(m, struct ieee80211_frame *);
256 } else {
257 ic->ic_stats.is_rx_nowep++;
258 goto out;
259 }
260 }
261 /* copy to listener after decrypt */
262 if (ic->ic_rawbpf)
263 bpf_mtap(ic->ic_rawbpf, m);
264 m = ieee80211_decap(ifp, m);
265 if (m == NULL) {
266 ic->ic_stats.is_rx_decap++;
267 goto err;
268 }
269 ifp->if_ipackets++;
270
271 /* perform as a bridge within the AP */
272 m1 = NULL;
273 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
274 eh = mtod(m, struct ether_header *);
275 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
276 m1 = m_copypacket(m, M_DONTWAIT);
277 if (m1 == NULL)
278 ifp->if_oerrors++;
279 else
280 m1->m_flags |= M_MCAST;
281 } else {
282 ni = ieee80211_find_node(ic, eh->ether_dhost);
283 if (ni != NULL) {
284 if (ni->ni_associd != 0) {
285 m1 = m;
286 m = NULL;
287 }
288 ieee80211_unref_node(&ni);
289 }
290 }
291 if (m1 != NULL) {
292 #ifdef ALTQ
293 if (ALTQ_IS_ENABLED(&ifp->if_snd))
294 altq_etherclassify(&ifp->if_snd, m1,
295 &pktattr);
296 #endif
297 len = m1->m_pkthdr.len;
298 IF_ENQUEUE(&ifp->if_snd, m1);
299 if (m != NULL)
300 ifp->if_omcasts++;
301 ifp->if_obytes += len;
302 }
303 }
304 if (m != NULL)
305 (*ifp->if_input)(ifp, m);
306 return;
307
308 case IEEE80211_FC0_TYPE_MGT:
309 if (dir != IEEE80211_FC1_DIR_NODS) {
310 ic->ic_stats.is_rx_wrongdir++;
311 goto err;
312 }
313 if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
314 ic->ic_stats.is_rx_ahdemo_mgt++;
315 goto out;
316 }
317 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
318
319 /* drop frames without interest */
320 if (ic->ic_state == IEEE80211_S_SCAN) {
321 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
322 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
323 ic->ic_stats.is_rx_mgtdiscard++;
324 goto out;
325 }
326 } else {
327 if (ic->ic_opmode != IEEE80211_M_IBSS &&
328 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
329 ic->ic_stats.is_rx_mgtdiscard++;
330 goto out;
331 }
332 }
333
334 if (ifp->if_flags & IFF_DEBUG) {
335 /* avoid to print too many frames */
336 int doprint = 0;
337
338 switch (subtype) {
339 case IEEE80211_FC0_SUBTYPE_BEACON:
340 if (ic->ic_state == IEEE80211_S_SCAN)
341 doprint = 1;
342 break;
343 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
344 if (ic->ic_opmode == IEEE80211_M_IBSS)
345 doprint = 1;
346 break;
347 default:
348 doprint = 1;
349 break;
350 }
351 #ifdef IEEE80211_DEBUG
352 doprint += ieee80211_debug;
353 #endif
354 if (doprint)
355 if_printf(ifp, "received %s from %s rssi %d\n",
356 ieee80211_mgt_subtype_name[subtype
357 >> IEEE80211_FC0_SUBTYPE_SHIFT],
358 ether_sprintf(wh->i_addr2), rssi);
359 }
360 if (ic->ic_rawbpf)
361 bpf_mtap(ic->ic_rawbpf, m);
362 (*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
363 m_freem(m);
364 return;
365
366 case IEEE80211_FC0_TYPE_CTL:
367 ic->ic_stats.is_rx_ctl++;
368 goto out;
369 default:
370 IEEE80211_DPRINTF(("%s: bad type %x\n", __func__, type));
371 /* should not come here */
372 break;
373 }
374 err:
375 ifp->if_ierrors++;
376 out:
377 if (m != NULL) {
378 if (ic->ic_rawbpf)
379 bpf_mtap(ic->ic_rawbpf, m);
380 m_freem(m);
381 }
382 }
383
384 struct mbuf *
385 ieee80211_decap(struct ifnet *ifp, struct mbuf *m)
386 {
387 struct ether_header *eh;
388 struct ieee80211_frame wh;
389 struct llc *llc;
390
391 if (m->m_len < sizeof(wh) + sizeof(*llc)) {
392 m = m_pullup(m, sizeof(wh) + sizeof(*llc));
393 if (m == NULL)
394 return NULL;
395 }
396 memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
397 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
398 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
399 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
400 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
401 m_adj(m, sizeof(wh) + sizeof(struct llc) - sizeof(*eh));
402 llc = NULL;
403 } else {
404 m_adj(m, sizeof(wh) - sizeof(*eh));
405 }
406 eh = mtod(m, struct ether_header *);
407 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
408 case IEEE80211_FC1_DIR_NODS:
409 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
410 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
411 break;
412 case IEEE80211_FC1_DIR_TODS:
413 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
414 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
415 break;
416 case IEEE80211_FC1_DIR_FROMDS:
417 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
418 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
419 break;
420 case IEEE80211_FC1_DIR_DSTODS:
421 /* not yet supported */
422 IEEE80211_DPRINTF(("%s: DS to DS\n", __func__));
423 m_freem(m);
424 return NULL;
425 }
426 #ifdef ALIGNED_POINTER
427 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
428 struct mbuf *n, *n0, **np;
429 caddr_t newdata;
430 int off, pktlen;
431
432 n0 = NULL;
433 np = &n0;
434 off = 0;
435 pktlen = m->m_pkthdr.len;
436 while (pktlen > off) {
437 if (n0 == NULL) {
438 MGETHDR(n, M_DONTWAIT, MT_DATA);
439 if (n == NULL) {
440 m_freem(m);
441 return NULL;
442 }
443 M_MOVE_PKTHDR(n, m);
444 n->m_len = MHLEN;
445 } else {
446 MGET(n, M_DONTWAIT, MT_DATA);
447 if (n == NULL) {
448 m_freem(m);
449 m_freem(n0);
450 return NULL;
451 }
452 n->m_len = MLEN;
453 }
454 if (pktlen - off >= MINCLSIZE) {
455 MCLGET(n, M_DONTWAIT);
456 if (n->m_flags & M_EXT)
457 n->m_len = n->m_ext.ext_size;
458 }
459 if (n0 == NULL) {
460 newdata =
461 (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
462 sizeof(*eh);
463 n->m_len -= newdata - n->m_data;
464 n->m_data = newdata;
465 }
466 if (n->m_len > pktlen - off)
467 n->m_len = pktlen - off;
468 m_copydata(m, off, n->m_len, mtod(n, caddr_t));
469 off += n->m_len;
470 *np = n;
471 np = &n->m_next;
472 }
473 m_freem(m);
474 m = n0;
475 }
476 #endif /* ALIGNED_POINTER */
477 if (llc != NULL) {
478 eh = mtod(m, struct ether_header *);
479 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
480 }
481 return m;
482 }
483
484 /*
485 * Install received rate set information in the node's state block.
486 */
487 static int
488 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
489 u_int8_t *rates, u_int8_t *xrates, int flags)
490 {
491 struct ieee80211_rateset *rs = &ni->ni_rates;
492
493 memset(rs, 0, sizeof(*rs));
494 rs->rs_nrates = rates[1];
495 memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
496 if (xrates != NULL) {
497 u_int8_t nxrates;
498 /*
499 * Tack on 11g extended supported rate element.
500 */
501 nxrates = xrates[1];
502 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
503 nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
504 IEEE80211_DPRINTF(("%s: extended rate set too large;"
505 " only using %u of %u rates\n",
506 __func__, nxrates, xrates[1]));
507 ic->ic_stats.is_rx_rstoobig++;
508 }
509 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
510 rs->rs_nrates += nxrates;
511 }
512 return ieee80211_fix_rate(ic, ni, flags);
513 }
514
515 /* Verify the existence and length of __elem or get out. */
516 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do { \
517 if ((__elem) == NULL) { \
518 IEEE80211_DPRINTF(("%s: no " #__elem "in %s frame\n", \
519 __func__, ieee80211_mgt_subtype_name[subtype >> \
520 IEEE80211_FC0_SUBTYPE_SHIFT])); \
521 ic->ic_stats.is_rx_elem_missing++; \
522 return; \
523 } \
524 if ((__elem)[1] > (__maxlen)) { \
525 IEEE80211_DPRINTF(("%s: bad " #__elem " len %d in %s " \
526 "frame from %s\n", __func__, (__elem)[1], \
527 ieee80211_mgt_subtype_name[subtype >> \
528 IEEE80211_FC0_SUBTYPE_SHIFT], \
529 ether_sprintf(wh->i_addr2))); \
530 ic->ic_stats.is_rx_elem_toobig++; \
531 return; \
532 } \
533 } while (0)
534
535 #define IEEE80211_VERIFY_LENGTH(_len, _minlen) do { \
536 if ((_len) < (_minlen)) { \
537 IEEE80211_DPRINTF(("%s: %s frame too short from %s\n", \
538 __func__, \
539 ieee80211_mgt_subtype_name[subtype >> \
540 IEEE80211_FC0_SUBTYPE_SHIFT], \
541 ether_sprintf(wh->i_addr2))); \
542 ic->ic_stats.is_rx_elem_toosmall++; \
543 return; \
544 } \
545 } while (0)
546
547 void
548 ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
549 struct ieee80211_node *ni,
550 int subtype, int rssi, u_int32_t rstamp)
551 {
552 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
553 struct ifnet *ifp = &ic->ic_if;
554 struct ieee80211_frame *wh;
555 u_int8_t *frm, *efrm;
556 u_int8_t *ssid, *rates, *xrates;
557 int reassoc, resp, newassoc, allocbs;
558
559 wh = mtod(m0, struct ieee80211_frame *);
560 frm = (u_int8_t *)&wh[1];
561 efrm = mtod(m0, u_int8_t *) + m0->m_len;
562 switch (subtype) {
563 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
564 case IEEE80211_FC0_SUBTYPE_BEACON: {
565 u_int8_t *tstamp, *bintval, *capinfo, *country;
566 u_int8_t chan, bchan, fhindex, erp;
567 u_int16_t fhdwell;
568
569 if (ic->ic_opmode != IEEE80211_M_IBSS &&
570 ic->ic_state != IEEE80211_S_SCAN) {
571 /* XXX: may be useful for background scan */
572 return;
573 }
574
575 /*
576 * beacon/probe response frame format
577 * [8] time stamp
578 * [2] beacon interval
579 * [2] capability information
580 * [tlv] ssid
581 * [tlv] supported rates
582 * [tlv] country information
583 * [tlv] parameter set (FH/DS)
584 * [tlv] erp information
585 * [tlv] extended supported rates
586 */
587 IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
588 tstamp = frm; frm += 8;
589 bintval = frm; frm += 2;
590 capinfo = frm; frm += 2;
591 ssid = rates = xrates = country = NULL;
592 bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
593 chan = bchan;
594 fhdwell = 0;
595 fhindex = 0;
596 erp = 0;
597 while (frm < efrm) {
598 switch (*frm) {
599 case IEEE80211_ELEMID_SSID:
600 ssid = frm;
601 break;
602 case IEEE80211_ELEMID_RATES:
603 rates = frm;
604 break;
605 case IEEE80211_ELEMID_COUNTRY:
606 country = frm;
607 break;
608 case IEEE80211_ELEMID_FHPARMS:
609 if (ic->ic_phytype == IEEE80211_T_FH) {
610 fhdwell = (frm[3] << 8) | frm[2];
611 chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
612 fhindex = frm[6];
613 }
614 break;
615 case IEEE80211_ELEMID_DSPARMS:
616 /*
617 * XXX hack this since depending on phytype
618 * is problematic for multi-mode devices.
619 */
620 if (ic->ic_phytype != IEEE80211_T_FH)
621 chan = frm[2];
622 break;
623 case IEEE80211_ELEMID_TIM:
624 break;
625 case IEEE80211_ELEMID_XRATES:
626 xrates = frm;
627 break;
628 case IEEE80211_ELEMID_ERP:
629 if (frm[1] != 1) {
630 IEEE80211_DPRINTF(("%s: invalid ERP "
631 "element; length %u, expecting "
632 "1\n", __func__, frm[1]));
633 ic->ic_stats.is_rx_elem_toobig++;
634 break;
635 }
636 erp = frm[2];
637 break;
638 default:
639 IEEE80211_DPRINTF2(("%s: element id %u/len %u "
640 "ignored\n", __func__, *frm, frm[1]));
641 ic->ic_stats.is_rx_elem_unknown++;
642 break;
643 }
644 frm += frm[1] + 2;
645 }
646 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
647 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
648 if (
649 #if IEEE80211_CHAN_MAX < 255
650 chan > IEEE80211_CHAN_MAX ||
651 #endif
652 isclr(ic->ic_chan_active, chan)) {
653 IEEE80211_DPRINTF(("%s: ignore %s with invalid channel "
654 "%u\n", __func__,
655 ISPROBE(subtype) ? "probe response" : "beacon",
656 chan));
657 ic->ic_stats.is_rx_badchan++;
658 return;
659 }
660 if (chan != bchan) {
661 /*
662 * Frame was received on a channel different from the
663 * one indicated in the DS/FH params element id;
664 * silently discard it.
665 *
666 * NB: this can happen due to signal leakage.
667 */
668 IEEE80211_DPRINTF(("%s: ignore %s on channel %u marked "
669 "for channel %u\n", __func__,
670 ISPROBE(subtype) ? "probe response" : "beacon",
671 bchan, chan));
672 ic->ic_stats.is_rx_chanmismatch++;
673 return;
674 }
675
676 /*
677 * Use mac and channel for lookup so we collect all
678 * potential AP's when scanning. Otherwise we may
679 * see the same AP on multiple channels and will only
680 * record the last one. We could filter APs here based
681 * on rssi, etc. but leave that to the end of the scan
682 * so we can keep the selection criteria in one spot.
683 * This may result in a bloat of the scanned AP list but
684 * it shouldn't be too much.
685 */
686 ni = ieee80211_lookup_node(ic, wh->i_addr2,
687 &ic->ic_channels[chan]);
688 #ifdef IEEE80211_DEBUG
689 if (ieee80211_debug &&
690 (ni == NULL || ic->ic_state == IEEE80211_S_SCAN)) {
691 printf("%s: %s%s on chan %u (bss chan %u) ",
692 __func__, (ni == NULL ? "new " : ""),
693 ISPROBE(subtype) ? "probe response" : "beacon",
694 chan, bchan);
695 ieee80211_print_essid(ssid + 2, ssid[1]);
696 printf(" from %s\n", ether_sprintf(wh->i_addr2));
697 printf("%s: caps 0x%x bintval %u erp 0x%x\n",
698 __func__, le16toh(*(u_int16_t *)capinfo),
699 le16toh(*(u_int16_t *)bintval), erp);
700 if (country)
701 printf("%s: country info %*D\n",
702 __func__, country[1], country+2, " ");
703 }
704 #endif
705 if (ni == NULL) {
706 ni = ieee80211_alloc_node(ic, wh->i_addr2);
707 if (ni == NULL) {
708 ic->ic_stats.is_rx_nodealloc++;
709 return;
710 }
711 ni->ni_esslen = ssid[1];
712 memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
713 memcpy(ni->ni_essid, ssid + 2, ssid[1]);
714 } else if (ssid[1] != 0 && ISPROBE(subtype)) {
715 /*
716 * Update ESSID at probe response to adopt hidden AP by
717 * Lucent/Cisco, which announces null ESSID in beacon.
718 */
719 ni->ni_esslen = ssid[1];
720 memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
721 memcpy(ni->ni_essid, ssid + 2, ssid[1]);
722 }
723 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
724 ni->ni_rssi = rssi;
725 ni->ni_rstamp = rstamp;
726 memcpy(ni->ni_tstamp, tstamp, sizeof(ni->ni_tstamp));
727 ni->ni_intval = le16toh(*(u_int16_t *)bintval);
728 ni->ni_capinfo = le16toh(*(u_int16_t *)capinfo);
729 /* XXX validate channel # */
730 ni->ni_chan = &ic->ic_channels[chan];
731 ni->ni_fhdwell = fhdwell;
732 ni->ni_fhindex = fhindex;
733 ni->ni_erp = erp;
734 /* NB: must be after ni_chan is setup */
735 ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
736 ieee80211_unref_node(&ni);
737 break;
738 }
739
740 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: {
741 u_int8_t rate;
742
743 if (ic->ic_opmode == IEEE80211_M_STA)
744 return;
745 if (ic->ic_state != IEEE80211_S_RUN)
746 return;
747
748 /*
749 * prreq frame format
750 * [tlv] ssid
751 * [tlv] supported rates
752 * [tlv] extended supported rates
753 */
754 ssid = rates = xrates = NULL;
755 while (frm < efrm) {
756 switch (*frm) {
757 case IEEE80211_ELEMID_SSID:
758 ssid = frm;
759 break;
760 case IEEE80211_ELEMID_RATES:
761 rates = frm;
762 break;
763 case IEEE80211_ELEMID_XRATES:
764 xrates = frm;
765 break;
766 }
767 frm += frm[1] + 2;
768 }
769 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
770 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
771 if (ssid[1] != 0 &&
772 (ssid[1] != ic->ic_bss->ni_esslen ||
773 memcmp(ssid + 2, ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen) != 0)) {
774 #ifdef IEEE80211_DEBUG
775 if (ieee80211_debug) {
776 printf("%s: ssid unmatch ", __func__);
777 ieee80211_print_essid(ssid + 2, ssid[1]);
778 printf(" from %s\n", ether_sprintf(wh->i_addr2));
779 }
780 #endif
781 ic->ic_stats.is_rx_ssidmismatch++;
782 return;
783 }
784
785 if (ni == ic->ic_bss) {
786 ni = ieee80211_dup_bss(ic, wh->i_addr2);
787 if (ni == NULL) {
788 ic->ic_stats.is_rx_nodealloc++;
789 return;
790 }
791 IEEE80211_DPRINTF(("%s: new req from %s\n",
792 __func__, ether_sprintf(wh->i_addr2)));
793 allocbs = 1;
794 } else
795 allocbs = 0;
796 ni->ni_rssi = rssi;
797 ni->ni_rstamp = rstamp;
798 rate = ieee80211_setup_rates(ic, ni, rates, xrates,
799 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
800 | IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
801 if (rate & IEEE80211_RATE_BASIC) {
802 IEEE80211_DPRINTF(("%s: rate negotiation failed: %s\n",
803 __func__,ether_sprintf(wh->i_addr2)));
804 } else {
805 IEEE80211_SEND_MGMT(ic, ni,
806 IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
807 }
808 if (allocbs) {
809 /* XXX just use free? */
810 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
811 ieee80211_free_node(ic, ni);
812 else
813 ieee80211_unref_node(&ni);
814 }
815 break;
816 }
817
818 case IEEE80211_FC0_SUBTYPE_AUTH: {
819 u_int16_t algo, seq, status;
820 /*
821 * auth frame format
822 * [2] algorithm
823 * [2] sequence
824 * [2] status
825 * [tlv*] challenge
826 */
827 IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
828 algo = le16toh(*(u_int16_t *)frm);
829 seq = le16toh(*(u_int16_t *)(frm + 2));
830 status = le16toh(*(u_int16_t *)(frm + 4));
831 if (algo != IEEE80211_AUTH_ALG_OPEN) {
832 /* TODO: shared key auth */
833 IEEE80211_DPRINTF(("%s: unsupported auth %d from %s\n",
834 __func__, algo, ether_sprintf(wh->i_addr2)));
835 ic->ic_stats.is_rx_auth_unsupported++;
836 return;
837 }
838 switch (ic->ic_opmode) {
839 case IEEE80211_M_IBSS:
840 if (ic->ic_state != IEEE80211_S_RUN || seq != 1) {
841 IEEE80211_DPRINTF(("%s: discard auth from %s; "
842 "state %u, seq %u\n", __func__,
843 ether_sprintf(wh->i_addr2),
844 ic->ic_state, seq));
845 ic->ic_stats.is_rx_bad_auth++;
846 break;
847 }
848 ieee80211_new_state(ic, IEEE80211_S_AUTH,
849 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
850 break;
851
852 case IEEE80211_M_AHDEMO:
853 /* should not come here */
854 break;
855
856 case IEEE80211_M_HOSTAP:
857 if (ic->ic_state != IEEE80211_S_RUN || seq != 1) {
858 IEEE80211_DPRINTF(("%s: discard auth from %s; "
859 "state %u, seq %u\n", __func__,
860 ether_sprintf(wh->i_addr2),
861 ic->ic_state, seq));
862 ic->ic_stats.is_rx_bad_auth++;
863 break;
864 }
865 if (ni == ic->ic_bss) {
866 ni = ieee80211_alloc_node(ic, wh->i_addr2);
867 if (ni == NULL) {
868 ic->ic_stats.is_rx_nodealloc++;
869 return;
870 }
871 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
872 ni->ni_rssi = rssi;
873 ni->ni_rstamp = rstamp;
874 ni->ni_chan = ic->ic_bss->ni_chan;
875 allocbs = 1;
876 } else
877 allocbs = 0;
878 IEEE80211_SEND_MGMT(ic, ni,
879 IEEE80211_FC0_SUBTYPE_AUTH, 2);
880 if (ifp->if_flags & IFF_DEBUG)
881 if_printf(ifp, "station %s %s authenticated\n",
882 (allocbs ? "newly" : "already"),
883 ether_sprintf(ni->ni_macaddr));
884 break;
885
886 case IEEE80211_M_STA:
887 if (ic->ic_state != IEEE80211_S_AUTH || seq != 2) {
888 IEEE80211_DPRINTF(("%s: discard auth from %s; "
889 "state %u, seq %u\n", __func__,
890 ether_sprintf(wh->i_addr2),
891 ic->ic_state, seq));
892 ic->ic_stats.is_rx_bad_auth++;
893 break;
894 }
895 if (status != 0) {
896 if_printf(&ic->ic_if,
897 "authentication failed (reason %d) for %s\n",
898 status,
899 ether_sprintf(wh->i_addr3));
900 if (ni != ic->ic_bss)
901 ni->ni_fails++;
902 ic->ic_stats.is_rx_auth_fail++;
903 return;
904 }
905 ieee80211_new_state(ic, IEEE80211_S_ASSOC,
906 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
907 break;
908 case IEEE80211_M_MONITOR:
909 break;
910 }
911 break;
912 }
913
914 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
915 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
916 u_int16_t capinfo, bintval;
917
918 if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
919 (ic->ic_state != IEEE80211_S_RUN))
920 return;
921
922 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
923 reassoc = 1;
924 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
925 } else {
926 reassoc = 0;
927 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
928 }
929 /*
930 * asreq frame format
931 * [2] capability information
932 * [2] listen interval
933 * [6*] current AP address (reassoc only)
934 * [tlv] ssid
935 * [tlv] supported rates
936 * [tlv] extended supported rates
937 */
938 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
939 if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
940 IEEE80211_DPRINTF(("%s: ignore other bss from %s\n",
941 __func__, ether_sprintf(wh->i_addr2)));
942 ic->ic_stats.is_rx_assoc_bss++;
943 return;
944 }
945 capinfo = le16toh(*(u_int16_t *)frm); frm += 2;
946 bintval = le16toh(*(u_int16_t *)frm); frm += 2;
947 if (reassoc)
948 frm += 6; /* ignore current AP info */
949 ssid = rates = xrates = NULL;
950 while (frm < efrm) {
951 switch (*frm) {
952 case IEEE80211_ELEMID_SSID:
953 ssid = frm;
954 break;
955 case IEEE80211_ELEMID_RATES:
956 rates = frm;
957 break;
958 case IEEE80211_ELEMID_XRATES:
959 xrates = frm;
960 break;
961 }
962 frm += frm[1] + 2;
963 }
964 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
965 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
966 if (ssid[1] != ic->ic_bss->ni_esslen ||
967 memcmp(ssid + 2, ic->ic_bss->ni_essid, ssid[1]) != 0) {
968 #ifdef IEEE80211_DEBUG
969 if (ieee80211_debug) {
970 printf("%s: ssid unmatch ", __func__);
971 ieee80211_print_essid(ssid + 2, ssid[1]);
972 printf(" from %s\n", ether_sprintf(wh->i_addr2));
973 }
974 #endif
975 ic->ic_stats.is_rx_ssidmismatch++;
976 return;
977 }
978 if (ni == ic->ic_bss) {
979 IEEE80211_DPRINTF(("%s: not authenticated for %s\n",
980 __func__, ether_sprintf(wh->i_addr2)));
981 ni = ieee80211_dup_bss(ic, wh->i_addr2);
982 if (ni != NULL) {
983 IEEE80211_SEND_MGMT(ic, ni,
984 IEEE80211_FC0_SUBTYPE_DEAUTH,
985 IEEE80211_REASON_ASSOC_NOT_AUTHED);
986 ieee80211_free_node(ic, ni);
987 }
988 ic->ic_stats.is_rx_assoc_notauth++;
989 return;
990 }
991 /* XXX per-node cipher suite */
992 /* XXX some stations use the privacy bit for handling APs
993 that suport both encrypted and unencrypted traffic */
994 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0 ||
995 (capinfo & IEEE80211_CAPINFO_PRIVACY) !=
996 ((ic->ic_flags & IEEE80211_F_WEPON) ?
997 IEEE80211_CAPINFO_PRIVACY : 0)) {
998 IEEE80211_DPRINTF(("%s: capability mismatch %x for %s\n",
999 __func__, capinfo, ether_sprintf(wh->i_addr2)));
1000 ni->ni_associd = 0;
1001 IEEE80211_SEND_MGMT(ic, ni, resp,
1002 IEEE80211_STATUS_CAPINFO);
1003 ic->ic_stats.is_rx_assoc_capmismatch++;
1004 return;
1005 }
1006 ieee80211_setup_rates(ic, ni, rates, xrates,
1007 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1008 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1009 if (ni->ni_rates.rs_nrates == 0) {
1010 IEEE80211_DPRINTF(("%s: rate unmatch for %s\n",
1011 __func__, ether_sprintf(wh->i_addr2)));
1012 ni->ni_associd = 0;
1013 IEEE80211_SEND_MGMT(ic, ni, resp,
1014 IEEE80211_STATUS_BASIC_RATE);
1015 ic->ic_stats.is_rx_assoc_norate++;
1016 return;
1017 }
1018 ni->ni_rssi = rssi;
1019 ni->ni_rstamp = rstamp;
1020 ni->ni_intval = bintval;
1021 ni->ni_capinfo = capinfo;
1022 ni->ni_chan = ic->ic_bss->ni_chan;
1023 ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
1024 ni->ni_fhindex = ic->ic_bss->ni_fhindex;
1025 if (ni->ni_associd == 0) {
1026 /* XXX handle rollover at 2007 */
1027 /* XXX guarantee uniqueness */
1028 ni->ni_associd = 0xc000 | ic->ic_bss->ni_associd++;
1029 newassoc = 1;
1030 } else
1031 newassoc = 0;
1032 /* XXX for 11g must turn off short slot time if long
1033 slot time sta associates */
1034 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1035 if (ifp->if_flags & IFF_DEBUG)
1036 if_printf(ifp, "station %s %s associated\n",
1037 (newassoc ? "newly" : "already"),
1038 ether_sprintf(ni->ni_macaddr));
1039 /* give driver a chance to setup state like ni_txrate */
1040 if (ic->ic_newassoc)
1041 (*ic->ic_newassoc)(ic, ni, newassoc);
1042 break;
1043 }
1044
1045 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1046 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1047 u_int16_t status;
1048
1049 if (ic->ic_opmode != IEEE80211_M_STA ||
1050 ic->ic_state != IEEE80211_S_ASSOC)
1051 return;
1052
1053 /*
1054 * asresp frame format
1055 * [2] capability information
1056 * [2] status
1057 * [2] association ID
1058 * [tlv] supported rates
1059 * [tlv] extended supported rates
1060 */
1061 IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
1062 ni = ic->ic_bss;
1063 ni->ni_capinfo = le16toh(*(u_int16_t *)frm);
1064 frm += 2;
1065
1066 status = le16toh(*(u_int16_t *)frm);
1067 frm += 2;
1068 if (status != 0) {
1069 if_printf(ifp, "association failed (reason %d) for %s\n",
1070 status, ether_sprintf(wh->i_addr3));
1071 if (ni != ic->ic_bss)
1072 ni->ni_fails++;
1073 ic->ic_stats.is_rx_auth_fail++;
1074 return;
1075 }
1076 ni->ni_associd = le16toh(*(u_int16_t *)frm);
1077 frm += 2;
1078
1079 rates = xrates = NULL;
1080 while (frm < efrm) {
1081 switch (*frm) {
1082 case IEEE80211_ELEMID_RATES:
1083 rates = frm;
1084 break;
1085 case IEEE80211_ELEMID_XRATES:
1086 xrates = frm;
1087 break;
1088 }
1089 frm += frm[1] + 2;
1090 }
1091
1092 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
1093 ieee80211_setup_rates(ic, ni, rates, xrates,
1094 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1095 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1096 if (ni->ni_rates.rs_nrates != 0)
1097 ieee80211_new_state(ic, IEEE80211_S_RUN,
1098 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1099 break;
1100 }
1101
1102 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1103 u_int16_t reason;
1104 /*
1105 * deauth frame format
1106 * [2] reason
1107 */
1108 IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
1109 reason = le16toh(*(u_int16_t *)frm);
1110 ic->ic_stats.is_rx_deauth++;
1111 switch (ic->ic_opmode) {
1112 case IEEE80211_M_STA:
1113 ieee80211_new_state(ic, IEEE80211_S_AUTH,
1114 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1115 break;
1116 case IEEE80211_M_HOSTAP:
1117 if (ni != ic->ic_bss) {
1118 if (ifp->if_flags & IFF_DEBUG)
1119 if_printf(ifp, "station %s deauthenticated"
1120 " by peer (reason %d)\n",
1121 ether_sprintf(ni->ni_macaddr), reason);
1122 /* node will be free'd on return */
1123 ieee80211_unref_node(&ni);
1124 }
1125 break;
1126 default:
1127 break;
1128 }
1129 break;
1130 }
1131
1132 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1133 u_int16_t reason;
1134 /*
1135 * disassoc frame format
1136 * [2] reason
1137 */
1138 IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
1139 reason = le16toh(*(u_int16_t *)frm);
1140 ic->ic_stats.is_rx_disassoc++;
1141 switch (ic->ic_opmode) {
1142 case IEEE80211_M_STA:
1143 ieee80211_new_state(ic, IEEE80211_S_ASSOC,
1144 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1145 break;
1146 case IEEE80211_M_HOSTAP:
1147 if (ni != ic->ic_bss) {
1148 if (ifp->if_flags & IFF_DEBUG)
1149 if_printf(ifp, "station %s disassociated"
1150 " by peer (reason %d)\n",
1151 ether_sprintf(ni->ni_macaddr), reason);
1152 ni->ni_associd = 0;
1153 /* XXX node reclaimed how? */
1154 }
1155 break;
1156 default:
1157 break;
1158 }
1159 break;
1160 }
1161 default:
1162 IEEE80211_DPRINTF(("%s: mgmt frame with subtype 0x%x not "
1163 "handled\n", __func__, subtype));
1164 ic->ic_stats.is_rx_badsubtype++;
1165 break;
1166 }
1167 #undef ISPROBE
1168 }
1169 #undef IEEE80211_VERIFY_LENGTH
1170 #undef IEEE80211_VERIFY_ELEMENT
Cache object: 1574eeed886712fd5271a8c5f866a80b
|