wlan - Update wlan from Adrian / FreeBSD
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_hostap.c
CommitLineData
32176cfd
RP
1/*-
2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32176cfd
RP
24 */
25
085ff963
MD
26#include <sys/cdefs.h>
27#ifdef __FreeBSD__
28__FBSDID("$FreeBSD$");
29#endif
30
32176cfd
RP
31/*
32 * IEEE 802.11 HOSTAP mode support.
33 */
34#include "opt_inet.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/mbuf.h>
40#include <sys/malloc.h>
41#include <sys/kernel.h>
42
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/endian.h>
46#include <sys/errno.h>
47#include <sys/proc.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
085ff963 51#include <net/if_var.h>
32176cfd
RP
52#include <net/if_media.h>
53#include <net/if_llc.h>
54#include <net/ethernet.h>
32176cfd
RP
55
56#include <net/bpf.h>
57
58#include <netproto/802_11/ieee80211_var.h>
59#include <netproto/802_11/ieee80211_hostap.h>
60#include <netproto/802_11/ieee80211_input.h>
61#ifdef IEEE80211_SUPPORT_SUPERG
62#include <netproto/802_11/ieee80211_superg.h>
63#endif
64#include <netproto/802_11/ieee80211_wds.h>
65
66#define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
67
68static void hostap_vattach(struct ieee80211vap *);
69static int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70static int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
71 int rssi, int nf);
72static void hostap_deliver_data(struct ieee80211vap *,
73 struct ieee80211_node *, struct mbuf *);
74static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
75 int subtype, int rssi, int nf);
76static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
32176cfd
RP
77
78void
79ieee80211_hostap_attach(struct ieee80211com *ic)
80{
81 ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
82}
83
84void
85ieee80211_hostap_detach(struct ieee80211com *ic)
86{
87}
88
89static void
90hostap_vdetach(struct ieee80211vap *vap)
91{
92}
93
94static void
95hostap_vattach(struct ieee80211vap *vap)
96{
97 vap->iv_newstate = hostap_newstate;
98 vap->iv_input = hostap_input;
99 vap->iv_recv_mgmt = hostap_recv_mgmt;
100 vap->iv_recv_ctl = hostap_recv_ctl;
101 vap->iv_opdetach = hostap_vdetach;
102 vap->iv_deliver_data = hostap_deliver_data;
085ff963 103 vap->iv_recv_pspoll = ieee80211_recv_pspoll;
32176cfd
RP
104}
105
106static void
107sta_disassoc(void *arg, struct ieee80211_node *ni)
108{
109 struct ieee80211vap *vap = arg;
110
111 if (ni->ni_vap == vap && ni->ni_associd != 0) {
112 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
113 IEEE80211_REASON_ASSOC_LEAVE);
114 ieee80211_node_leave(ni);
115 }
116}
117
118static void
119sta_csa(void *arg, struct ieee80211_node *ni)
120{
121 struct ieee80211vap *vap = arg;
122
123 if (ni->ni_vap == vap && ni->ni_associd != 0)
124 if (ni->ni_inact > vap->iv_inact_init) {
125 ni->ni_inact = vap->iv_inact_init;
126 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
127 "%s: inact %u", __func__, ni->ni_inact);
128 }
129}
130
131static void
132sta_drop(void *arg, struct ieee80211_node *ni)
133{
134 struct ieee80211vap *vap = arg;
135
136 if (ni->ni_vap == vap && ni->ni_associd != 0)
137 ieee80211_node_leave(ni);
138}
139
140/*
141 * Does a channel change require associated stations to re-associate
142 * so protocol state is correct. This is used when doing CSA across
143 * bands or similar (e.g. HT -> legacy).
144 */
145static int
146isbandchange(struct ieee80211com *ic)
147{
148 return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
149 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
150 IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
151}
152
153/*
154 * IEEE80211_M_HOSTAP vap state machine handler.
155 */
156static int
157hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
158{
159 struct ieee80211com *ic = vap->iv_ic;
160 enum ieee80211_state ostate;
085ff963
MD
161
162 IEEE80211_LOCK_ASSERT(ic);
32176cfd 163
32176cfd
RP
164 ostate = vap->iv_state;
165 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
166 __func__, ieee80211_state_name[ostate],
167 ieee80211_state_name[nstate], arg);
168 vap->iv_state = nstate; /* state transition */
169 if (ostate != IEEE80211_S_SCAN)
170 ieee80211_cancel_scan(vap); /* background scan */
171 switch (nstate) {
172 case IEEE80211_S_INIT:
173 switch (ostate) {
174 case IEEE80211_S_SCAN:
175 ieee80211_cancel_scan(vap);
176 break;
177 case IEEE80211_S_CAC:
178 ieee80211_dfs_cac_stop(vap);
179 break;
180 case IEEE80211_S_RUN:
181 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
182 break;
183 default:
184 break;
185 }
186 if (ostate != IEEE80211_S_INIT) {
187 /* NB: optimize INIT -> INIT case */
188 ieee80211_reset_bss(vap);
189 }
190 if (vap->iv_auth->ia_detach != NULL)
191 vap->iv_auth->ia_detach(vap);
192 break;
193 case IEEE80211_S_SCAN:
194 switch (ostate) {
195 case IEEE80211_S_CSA:
196 case IEEE80211_S_RUN:
197 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
198 /*
199 * Clear overlapping BSS state; the beacon frame
200 * will be reconstructed on transition to the RUN
201 * state and the timeout routines check if the flag
202 * is set before doing anything so this is sufficient.
203 */
204 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
205 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
206 /* fall thru... */
207 case IEEE80211_S_CAC:
208 /*
209 * NB: We may get here because of a manual channel
210 * change in which case we need to stop CAC
211 * XXX no need to stop if ostate RUN but it's ok
212 */
213 ieee80211_dfs_cac_stop(vap);
214 /* fall thru... */
215 case IEEE80211_S_INIT:
216 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
217 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
218 /*
219 * Already have a channel; bypass the
220 * scan and startup immediately.
221 * ieee80211_create_ibss will call back to
222 * move us to RUN state.
223 */
224 ieee80211_create_ibss(vap, vap->iv_des_chan);
225 break;
226 }
227 /*
228 * Initiate a scan. We can come here as a result
229 * of an IEEE80211_IOC_SCAN_REQ too in which case
230 * the vap will be marked with IEEE80211_FEXT_SCANREQ
231 * and the scan request parameters will be present
232 * in iv_scanreq. Otherwise we do the default.
233 */
234 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
235 ieee80211_check_scan(vap,
236 vap->iv_scanreq_flags,
237 vap->iv_scanreq_duration,
238 vap->iv_scanreq_mindwell,
239 vap->iv_scanreq_maxdwell,
240 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
241 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
242 } else
243 ieee80211_check_scan_current(vap);
244 break;
245 case IEEE80211_S_SCAN:
246 /*
247 * A state change requires a reset; scan.
248 */
249 ieee80211_check_scan_current(vap);
250 break;
251 default:
252 break;
253 }
254 break;
255 case IEEE80211_S_CAC:
256 /*
257 * Start CAC on a DFS channel. We come here when starting
258 * a bss on a DFS channel (see ieee80211_create_ibss).
259 */
260 ieee80211_dfs_cac_start(vap);
261 break;
262 case IEEE80211_S_RUN:
263 if (vap->iv_flags & IEEE80211_F_WPA) {
264 /* XXX validate prerequisites */
265 }
266 switch (ostate) {
267 case IEEE80211_S_INIT:
268 /*
269 * Already have a channel; bypass the
270 * scan and startup immediately.
271 * Note that ieee80211_create_ibss will call
272 * back to do a RUN->RUN state change.
273 */
274 ieee80211_create_ibss(vap,
275 ieee80211_ht_adjust_channel(ic,
276 ic->ic_curchan, vap->iv_flags_ht));
277 /* NB: iv_bss is changed on return */
278 break;
279 case IEEE80211_S_CAC:
280 /*
281 * NB: This is the normal state change when CAC
282 * expires and no radar was detected; no need to
283 * clear the CAC timer as it's already expired.
284 */
285 /* fall thru... */
286 case IEEE80211_S_CSA:
287 /*
288 * Shorten inactivity timer of associated stations
289 * to weed out sta's that don't follow a CSA.
290 */
291 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
292 /*
293 * Update bss node channel to reflect where
294 * we landed after CSA.
295 */
296 ieee80211_node_set_chan(vap->iv_bss,
297 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
298 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
299 /* XXX bypass debug msgs */
300 break;
301 case IEEE80211_S_SCAN:
302 case IEEE80211_S_RUN:
303#ifdef IEEE80211_DEBUG
304 if (ieee80211_msg_debug(vap)) {
305 struct ieee80211_node *ni = vap->iv_bss;
306 ieee80211_note(vap,
1e290df3 307 "synchronized with %s ssid ",
085ff963 308 ether_sprintf(ni->ni_bssid));
32176cfd
RP
309 ieee80211_print_essid(ni->ni_essid,
310 ni->ni_esslen);
311 /* XXX MCS/HT */
6168f72e 312 kprintf(" channel %d start %uMb\n",
32176cfd
RP
313 ieee80211_chan2ieee(ic, ic->ic_curchan),
314 IEEE80211_RATE2MBS(ni->ni_txrate));
315 }
316#endif
317 break;
318 default:
319 break;
320 }
321 /*
322 * Start/stop the authenticator. We delay until here
323 * to allow configuration to happen out of order.
324 */
325 if (vap->iv_auth->ia_attach != NULL) {
326 /* XXX check failure */
327 vap->iv_auth->ia_attach(vap);
328 } else if (vap->iv_auth->ia_detach != NULL) {
329 vap->iv_auth->ia_detach(vap);
330 }
331 ieee80211_node_authorize(vap->iv_bss);
332 break;
333 case IEEE80211_S_CSA:
334 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
335 /*
336 * On a ``band change'' silently drop associated
337 * stations as they must re-associate before they
338 * can pass traffic (as otherwise protocol state
339 * such as capabilities and the negotiated rate
340 * set may/will be wrong).
341 */
342 ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
343 }
344 break;
345 default:
346 break;
347 }
348 return 0;
349}
350
351static void
352hostap_deliver_data(struct ieee80211vap *vap,
353 struct ieee80211_node *ni, struct mbuf *m)
354{
355 struct ether_header *eh = mtod(m, struct ether_header *);
356 struct ifnet *ifp = vap->iv_ifp;
357
358 /* clear driver/net80211 flags before passing up */
085ff963
MD
359#if __FreeBSD_version >= 1000046
360 m->m_flags &= ~(M_MCAST | M_BCAST);
361 m_clrprotoflags(m);
362#else
32176cfd 363 m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
085ff963 364#endif
32176cfd
RP
365
366 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
367 ("gack, opmode %d", vap->iv_opmode));
368 /*
369 * Do accounting.
370 */
d40991ef 371 IFNET_STAT_INC(ifp, ipackets, 1);
32176cfd
RP
372 IEEE80211_NODE_STAT(ni, rx_data);
373 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
374 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
375 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
376 IEEE80211_NODE_STAT(ni, rx_mcast);
377 } else
378 IEEE80211_NODE_STAT(ni, rx_ucast);
379
380 /* perform as a bridge within the AP */
381 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
382 struct mbuf *mcopy = NULL;
383
384 if (m->m_flags & M_MCAST) {
385 mcopy = m_dup(m, MB_DONTWAIT);
386 if (mcopy == NULL)
d40991ef 387 IFNET_STAT_INC(ifp, oerrors, 1);
32176cfd
RP
388 else
389 mcopy->m_flags |= M_MCAST;
390 } else {
391 /*
392 * Check if the destination is associated with the
393 * same vap and authorized to receive traffic.
394 * Beware of traffic destined for the vap itself;
395 * sending it will not work; just let it be delivered
396 * normally.
397 */
398 struct ieee80211_node *sta = ieee80211_find_vap_node(
399 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
400 if (sta != NULL) {
401 if (ieee80211_node_is_authorized(sta)) {
402 /*
403 * Beware of sending to ourself; this
404 * needs to happen via the normal
405 * input path.
406 */
407 if (sta != vap->iv_bss) {
408 mcopy = m;
409 m = NULL;
410 }
411 } else {
412 vap->iv_stats.is_rx_unauth++;
413 IEEE80211_NODE_STAT(sta, rx_unauth);
414 }
415 ieee80211_free_node(sta);
416 }
417 }
418 if (mcopy != NULL) {
085ff963
MD
419 int len, err;
420 len = mcopy->m_pkthdr.len;
421 err = ieee80211_vap_xmitpkt(vap, mcopy);
32176cfd
RP
422 if (err) {
423 /* NB: IFQ_HANDOFF reclaims mcopy */
424 } else {
d40991ef 425 IFNET_STAT_INC(ifp, opackets, 1);
32176cfd
RP
426 }
427 }
428 }
429 if (m != NULL) {
430 /*
431 * Mark frame as coming from vap's interface.
432 */
433 m->m_pkthdr.rcvif = ifp;
434 if (m->m_flags & M_MCAST) {
435 /*
436 * Spam DWDS vap's w/ multicast traffic.
437 */
438 /* XXX only if dwds in use? */
439 ieee80211_dwds_mcast(vap, m);
440 }
441 if (ni->ni_vlan != 0) {
442 /* attach vlan tag */
085ff963
MD
443#if defined(__DragonFly__)
444 /* XXX ntohs() needed? */
34a60cf6 445 m->m_pkthdr.ether_vlantag = ni->ni_vlan;
085ff963
MD
446#else
447 m->m_pkthdr.ether_vtag = ni->ni_vlan;
448#endif
32176cfd
RP
449 m->m_flags |= M_VLANTAG;
450 }
73029d08 451 ifp->if_input(ifp, m, NULL, -1);
32176cfd
RP
452 }
453}
454
455/*
456 * Decide if a received management frame should be
457 * printed when debugging is enabled. This filters some
458 * of the less interesting frames that come frequently
459 * (e.g. beacons).
460 */
461static __inline int
462doprint(struct ieee80211vap *vap, int subtype)
463{
464 switch (subtype) {
465 case IEEE80211_FC0_SUBTYPE_BEACON:
466 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
467 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
468 return 0;
469 }
470 return 1;
471}
472
473/*
474 * Process a received frame. The node associated with the sender
475 * should be supplied. If nothing was found in the node table then
476 * the caller is assumed to supply a reference to iv_bss instead.
477 * The RSSI and a timestamp are also supplied. The RSSI data is used
478 * during AP scanning to select a AP to associate with; it can have
479 * any units so long as values have consistent units and higher values
480 * mean ``better signal''. The receive timestamp is currently not used
481 * by the 802.11 layer.
482 */
483static int
484hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
485{
32176cfd
RP
486#define HAS_SEQ(type) ((type & 0x4) == 0)
487 struct ieee80211vap *vap = ni->ni_vap;
488 struct ieee80211com *ic = ni->ni_ic;
489 struct ifnet *ifp = vap->iv_ifp;
490 struct ieee80211_frame *wh;
491 struct ieee80211_key *key;
492 struct ether_header *eh;
493 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
494 uint8_t dir, type, subtype, qos;
495 uint8_t *bssid;
496 uint16_t rxseq;
497
498 if (m->m_flags & M_AMPDU_MPDU) {
499 /*
500 * Fastpath for A-MPDU reorder q resubmission. Frames
501 * w/ M_AMPDU_MPDU marked have already passed through
502 * here but were received out of order and been held on
503 * the reorder queue. When resubmitted they are marked
504 * with the M_AMPDU_MPDU flag and we can bypass most of
505 * the normal processing.
506 */
507 wh = mtod(m, struct ieee80211_frame *);
508 type = IEEE80211_FC0_TYPE_DATA;
509 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
510 subtype = IEEE80211_FC0_SUBTYPE_QOS;
511 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
512 goto resubmit_ampdu;
513 }
514
515 KASSERT(ni != NULL, ("null node"));
516 ni->ni_inact = ni->ni_inact_reload;
517
518 type = -1; /* undefined */
519
520 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
521 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
522 ni->ni_macaddr, NULL,
523 "too short (1): len %u", m->m_pkthdr.len);
524 vap->iv_stats.is_rx_tooshort++;
525 goto out;
526 }
527 /*
528 * Bit of a cheat here, we use a pointer for a 3-address
529 * frame format but don't reference fields past outside
530 * ieee80211_frame_min w/o first validating the data is
531 * present.
532 */
533 wh = mtod(m, struct ieee80211_frame *);
534
535 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
536 IEEE80211_FC0_VERSION_0) {
537 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
538 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
539 wh->i_fc[0], wh->i_fc[1]);
540 vap->iv_stats.is_rx_badversion++;
541 goto err;
542 }
543
544 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
545 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
546 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
547 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
548 if (dir != IEEE80211_FC1_DIR_NODS)
549 bssid = wh->i_addr1;
550 else if (type == IEEE80211_FC0_TYPE_CTL)
551 bssid = wh->i_addr1;
552 else {
553 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
554 IEEE80211_DISCARD_MAC(vap,
555 IEEE80211_MSG_ANY, ni->ni_macaddr,
556 NULL, "too short (2): len %u",
557 m->m_pkthdr.len);
558 vap->iv_stats.is_rx_tooshort++;
559 goto out;
560 }
561 bssid = wh->i_addr3;
562 }
563 /*
564 * Validate the bssid.
565 */
566 if (!(type == IEEE80211_FC0_TYPE_MGT &&
567 subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
568 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
569 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
570 /* not interested in */
571 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
572 bssid, NULL, "%s", "not to bss");
573 vap->iv_stats.is_rx_wrongbss++;
574 goto out;
575 }
576
577 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
578 ni->ni_noise = nf;
579 if (HAS_SEQ(type)) {
580 uint8_t tid = ieee80211_gettid(wh);
581 if (IEEE80211_QOS_HAS_SEQ(wh) &&
582 TID_TO_WME_AC(tid) >= WME_AC_VI)
583 ic->ic_wme.wme_hipri_traffic++;
584 rxseq = le16toh(*(uint16_t *)wh->i_seq);
085ff963 585 if (! ieee80211_check_rxseq(ni, wh)) {
32176cfd
RP
586 /* duplicate, discard */
587 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
588 bssid, "duplicate",
589 "seqno <%u,%u> fragno <%u,%u> tid %u",
590 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
591 ni->ni_rxseqs[tid] >>
592 IEEE80211_SEQ_SEQ_SHIFT,
593 rxseq & IEEE80211_SEQ_FRAG_MASK,
594 ni->ni_rxseqs[tid] &
595 IEEE80211_SEQ_FRAG_MASK,
596 tid);
597 vap->iv_stats.is_rx_dup++;
598 IEEE80211_NODE_STAT(ni, rx_dup);
599 goto out;
600 }
601 ni->ni_rxseqs[tid] = rxseq;
602 }
603 }
604
605 switch (type) {
606 case IEEE80211_FC0_TYPE_DATA:
607 hdrspace = ieee80211_hdrspace(ic, wh);
608 if (m->m_len < hdrspace &&
609 (m = m_pullup(m, hdrspace)) == NULL) {
610 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
611 ni->ni_macaddr, NULL,
612 "data too short: expecting %u", hdrspace);
613 vap->iv_stats.is_rx_tooshort++;
614 goto out; /* XXX */
615 }
616 if (!(dir == IEEE80211_FC1_DIR_TODS ||
617 (dir == IEEE80211_FC1_DIR_DSTODS &&
618 (vap->iv_flags & IEEE80211_F_DWDS)))) {
619 if (dir != IEEE80211_FC1_DIR_DSTODS) {
620 IEEE80211_DISCARD(vap,
621 IEEE80211_MSG_INPUT, wh, "data",
622 "incorrect dir 0x%x", dir);
623 } else {
624 IEEE80211_DISCARD(vap,
625 IEEE80211_MSG_INPUT |
626 IEEE80211_MSG_WDS, wh,
627 "4-address data",
628 "%s", "DWDS not enabled");
629 }
630 vap->iv_stats.is_rx_wrongdir++;
631 goto out;
632 }
633 /* check if source STA is associated */
634 if (ni == vap->iv_bss) {
635 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
636 wh, "data", "%s", "unknown src");
637 ieee80211_send_error(ni, wh->i_addr2,
638 IEEE80211_FC0_SUBTYPE_DEAUTH,
639 IEEE80211_REASON_NOT_AUTHED);
640 vap->iv_stats.is_rx_notassoc++;
641 goto err;
642 }
643 if (ni->ni_associd == 0) {
644 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
645 wh, "data", "%s", "unassoc src");
646 IEEE80211_SEND_MGMT(ni,
647 IEEE80211_FC0_SUBTYPE_DISASSOC,
648 IEEE80211_REASON_NOT_ASSOCED);
649 vap->iv_stats.is_rx_notassoc++;
650 goto err;
651 }
652
653 /*
654 * Check for power save state change.
655 * XXX out-of-order A-MPDU frames?
656 */
657 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
658 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
085ff963 659 vap->iv_node_ps(ni,
32176cfd
RP
660 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
661 /*
662 * For 4-address packets handle WDS discovery
663 * notifications. Once a WDS link is setup frames
664 * are just delivered to the WDS vap (see below).
665 */
666 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
667 if (!ieee80211_node_is_authorized(ni)) {
668 IEEE80211_DISCARD(vap,
669 IEEE80211_MSG_INPUT |
670 IEEE80211_MSG_WDS, wh,
671 "4-address data",
672 "%s", "unauthorized port");
673 vap->iv_stats.is_rx_unauth++;
674 IEEE80211_NODE_STAT(ni, rx_unauth);
675 goto err;
676 }
677 ieee80211_dwds_discover(ni, m);
678 return type;
679 }
680
681 /*
682 * Handle A-MPDU re-ordering. If the frame is to be
683 * processed directly then ieee80211_ampdu_reorder
684 * will return 0; otherwise it has consumed the mbuf
685 * and we should do nothing more with it.
686 */
687 if ((m->m_flags & M_AMPDU) &&
688 ieee80211_ampdu_reorder(ni, m) != 0) {
689 m = NULL;
690 goto out;
691 }
692 resubmit_ampdu:
693
694 /*
695 * Handle privacy requirements. Note that we
696 * must not be preempted from here until after
697 * we (potentially) call ieee80211_crypto_demic;
698 * otherwise we may violate assumptions in the
699 * crypto cipher modules used to do delayed update
700 * of replay sequence numbers.
701 */
085ff963 702 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
32176cfd
RP
703 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
704 /*
705 * Discard encrypted frames when privacy is off.
706 */
707 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
708 wh, "WEP", "%s", "PRIVACY off");
709 vap->iv_stats.is_rx_noprivacy++;
710 IEEE80211_NODE_STAT(ni, rx_noprivacy);
711 goto out;
712 }
713 key = ieee80211_crypto_decap(ni, m, hdrspace);
714 if (key == NULL) {
715 /* NB: stats+msgs handled in crypto_decap */
716 IEEE80211_NODE_STAT(ni, rx_wepfail);
717 goto out;
718 }
719 wh = mtod(m, struct ieee80211_frame *);
085ff963 720 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
32176cfd
RP
721 } else {
722 /* XXX M_WEP and IEEE80211_F_PRIVACY */
723 key = NULL;
724 }
725
726 /*
727 * Save QoS bits for use below--before we strip the header.
728 */
729 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
730 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
731 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
732 ((struct ieee80211_qosframe *)wh)->i_qos[0];
733 } else
734 qos = 0;
735
736 /*
737 * Next up, any fragmentation.
738 */
739 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
740 m = ieee80211_defrag(ni, m, hdrspace);
741 if (m == NULL) {
742 /* Fragment dropped or frame not complete yet */
743 goto out;
744 }
745 }
746 wh = NULL; /* no longer valid, catch any uses */
747
748 /*
749 * Next strip any MSDU crypto bits.
750 */
751 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
752 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
753 ni->ni_macaddr, "data", "%s", "demic error");
754 vap->iv_stats.is_rx_demicfail++;
755 IEEE80211_NODE_STAT(ni, rx_demicfail);
756 goto out;
757 }
758 /* copy to listener after decrypt */
759 if (ieee80211_radiotap_active_vap(vap))
760 ieee80211_radiotap_rx(vap, m);
761 need_tap = 0;
762 /*
763 * Finally, strip the 802.11 header.
764 */
765 m = ieee80211_decap(vap, m, hdrspace);
766 if (m == NULL) {
767 /* XXX mask bit to check for both */
768 /* don't count Null data frames as errors */
769 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
770 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
771 goto out;
772 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
773 ni->ni_macaddr, "data", "%s", "decap error");
774 vap->iv_stats.is_rx_decap++;
775 IEEE80211_NODE_STAT(ni, rx_decap);
776 goto err;
777 }
778 eh = mtod(m, struct ether_header *);
779 if (!ieee80211_node_is_authorized(ni)) {
780 /*
781 * Deny any non-PAE frames received prior to
782 * authorization. For open/shared-key
783 * authentication the port is mark authorized
784 * after authentication completes. For 802.1x
785 * the port is not marked authorized by the
786 * authenticator until the handshake has completed.
787 */
788 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
789 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
790 eh->ether_shost, "data",
791 "unauthorized port: ether type 0x%x len %u",
792 eh->ether_type, m->m_pkthdr.len);
793 vap->iv_stats.is_rx_unauth++;
794 IEEE80211_NODE_STAT(ni, rx_unauth);
795 goto err;
796 }
797 } else {
798 /*
799 * When denying unencrypted frames, discard
800 * any non-PAE frames received without encryption.
801 */
802 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
803 (key == NULL && (m->m_flags & M_WEP) == 0) &&
804 eh->ether_type != htons(ETHERTYPE_PAE)) {
805 /*
806 * Drop unencrypted frames.
807 */
808 vap->iv_stats.is_rx_unencrypted++;
809 IEEE80211_NODE_STAT(ni, rx_unencrypted);
810 goto out;
811 }
812 }
813 /* XXX require HT? */
814 if (qos & IEEE80211_QOS_AMSDU) {
815 m = ieee80211_decap_amsdu(ni, m);
816 if (m == NULL)
817 return IEEE80211_FC0_TYPE_DATA;
818 } else {
819#ifdef IEEE80211_SUPPORT_SUPERG
820 m = ieee80211_decap_fastframe(vap, ni, m);
821 if (m == NULL)
822 return IEEE80211_FC0_TYPE_DATA;
823#endif
824 }
825 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
826 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
827 else
828 hostap_deliver_data(vap, ni, m);
829 return IEEE80211_FC0_TYPE_DATA;
830
831 case IEEE80211_FC0_TYPE_MGT:
832 vap->iv_stats.is_rx_mgmt++;
833 IEEE80211_NODE_STAT(ni, rx_mgmt);
834 if (dir != IEEE80211_FC1_DIR_NODS) {
835 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
836 wh, "mgt", "incorrect dir 0x%x", dir);
837 vap->iv_stats.is_rx_wrongdir++;
838 goto err;
839 }
840 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
841 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
842 ni->ni_macaddr, "mgt", "too short: len %u",
843 m->m_pkthdr.len);
844 vap->iv_stats.is_rx_tooshort++;
845 goto out;
846 }
847 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
848 /* ensure return frames are unicast */
849 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1e290df3 850 wh, NULL, "source is multicast: %s",
085ff963 851 ether_sprintf(wh->i_addr2));
32176cfd
RP
852 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
853 goto out;
854 }
855#ifdef IEEE80211_DEBUG
856 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
857 ieee80211_msg_dumppkts(vap)) {
1e290df3 858 if_printf(ifp, "received %s from %s rssi %d\n",
32176cfd
RP
859 ieee80211_mgt_subtype_name[subtype >>
860 IEEE80211_FC0_SUBTYPE_SHIFT],
085ff963 861 ether_sprintf(wh->i_addr2), rssi);
32176cfd
RP
862 }
863#endif
085ff963 864 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
32176cfd
RP
865 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
866 /*
867 * Only shared key auth frames with a challenge
868 * should be encrypted, discard all others.
869 */
870 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
871 wh, NULL,
872 "%s", "WEP set but not permitted");
873 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
874 goto out;
875 }
876 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
877 /*
878 * Discard encrypted frames when privacy is off.
879 */
880 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
881 wh, NULL, "%s", "WEP set but PRIVACY off");
882 vap->iv_stats.is_rx_noprivacy++;
883 goto out;
884 }
885 hdrspace = ieee80211_hdrspace(ic, wh);
886 key = ieee80211_crypto_decap(ni, m, hdrspace);
887 if (key == NULL) {
888 /* NB: stats+msgs handled in crypto_decap */
889 goto out;
890 }
891 wh = mtod(m, struct ieee80211_frame *);
085ff963 892 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
32176cfd 893 }
085ff963
MD
894 /*
895 * Pass the packet to radiotap before calling iv_recv_mgmt().
896 * Otherwise iv_recv_mgmt() might pass another packet to
897 * radiotap, resulting in out of order packet captures.
898 */
899 if (ieee80211_radiotap_active_vap(vap))
900 ieee80211_radiotap_rx(vap, m);
901 need_tap = 0;
32176cfd
RP
902 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
903 goto out;
904
905 case IEEE80211_FC0_TYPE_CTL:
906 vap->iv_stats.is_rx_ctl++;
907 IEEE80211_NODE_STAT(ni, rx_ctrl);
908 vap->iv_recv_ctl(ni, m, subtype);
909 goto out;
910 default:
911 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
912 wh, "bad", "frame type 0x%x", type);
913 /* should not come here */
914 break;
915 }
916err:
d40991ef 917 IFNET_STAT_INC(ifp, ierrors, 1);
32176cfd
RP
918out:
919 if (m != NULL) {
920 if (need_tap && ieee80211_radiotap_active_vap(vap))
921 ieee80211_radiotap_rx(vap, m);
922 m_freem(m);
923 }
924 return type;
32176cfd
RP
925}
926
927static void
928hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
929 int rssi, int nf, uint16_t seq, uint16_t status)
930{
931 struct ieee80211vap *vap = ni->ni_vap;
932
933 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
934
935 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
936 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
937 ni->ni_macaddr, "open auth",
938 "bad sta auth mode %u", ni->ni_authmode);
939 vap->iv_stats.is_rx_bad_auth++; /* XXX */
940 /*
941 * Clear any challenge text that may be there if
942 * a previous shared key auth failed and then an
943 * open auth is attempted.
944 */
945 if (ni->ni_challenge != NULL) {
946 kfree(ni->ni_challenge, M_80211_NODE);
947 ni->ni_challenge = NULL;
948 }
949 /* XXX hack to workaround calling convention */
950 ieee80211_send_error(ni, wh->i_addr2,
951 IEEE80211_FC0_SUBTYPE_AUTH,
952 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
953 return;
954 }
955 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
956 vap->iv_stats.is_rx_bad_auth++;
957 return;
958 }
959 /* always accept open authentication requests */
960 if (ni == vap->iv_bss) {
961 ni = ieee80211_dup_bss(vap, wh->i_addr2);
962 if (ni == NULL)
963 return;
964 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
965 (void) ieee80211_ref_node(ni);
966 /*
967 * Mark the node as referenced to reflect that it's
968 * reference count has been bumped to insure it remains
969 * after the transaction completes.
970 */
971 ni->ni_flags |= IEEE80211_NODE_AREF;
972 /*
973 * Mark the node as requiring a valid association id
974 * before outbound traffic is permitted.
975 */
976 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
977
978 if (vap->iv_acl != NULL &&
979 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
980 /*
981 * When the ACL policy is set to RADIUS we defer the
982 * authorization to a user agent. Dispatch an event,
983 * a subsequent MLME call will decide the fate of the
984 * station. If the user agent is not present then the
985 * node will be reclaimed due to inactivity.
986 */
987 IEEE80211_NOTE_MAC(vap,
988 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
989 "%s", "station authentication defered (radius acl)");
990 ieee80211_notify_node_auth(ni);
991 } else {
992 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
993 IEEE80211_NOTE_MAC(vap,
994 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
995 "%s", "station authenticated (open)");
996 /*
997 * When 802.1x is not in use mark the port
998 * authorized at this point so traffic can flow.
999 */
1000 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1001 ieee80211_node_authorize(ni);
1002 }
1003}
1004
1005static void
1006hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
1007 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1008 uint16_t seq, uint16_t status)
1009{
1010 struct ieee80211vap *vap = ni->ni_vap;
1011 uint8_t *challenge;
085ff963 1012 int allocbs, estatus;
32176cfd
RP
1013
1014 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1015
1016 /*
1017 * NB: this can happen as we allow pre-shared key
1018 * authentication to be enabled w/o wep being turned
1019 * on so that configuration of these can be done
1020 * in any order. It may be better to enforce the
1021 * ordering in which case this check would just be
1022 * for sanity/consistency.
1023 */
1024 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1025 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1026 ni->ni_macaddr, "shared key auth",
1027 "%s", " PRIVACY is disabled");
1028 estatus = IEEE80211_STATUS_ALG;
1029 goto bad;
1030 }
1031 /*
1032 * Pre-shared key authentication is evil; accept
1033 * it only if explicitly configured (it is supported
1034 * mainly for compatibility with clients like Mac OS X).
1035 */
1036 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1037 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1038 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1039 ni->ni_macaddr, "shared key auth",
1040 "bad sta auth mode %u", ni->ni_authmode);
1041 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1042 estatus = IEEE80211_STATUS_ALG;
1043 goto bad;
1044 }
1045
1046 challenge = NULL;
1047 if (frm + 1 < efrm) {
1048 if ((frm[1] + 2) > (efrm - frm)) {
1049 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1050 ni->ni_macaddr, "shared key auth",
1051 "ie %d/%d too long",
085ff963 1052 frm[0], (frm[1] + 2) - (efrm - frm));
32176cfd
RP
1053 vap->iv_stats.is_rx_bad_auth++;
1054 estatus = IEEE80211_STATUS_CHALLENGE;
1055 goto bad;
1056 }
1057 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1058 challenge = frm;
1059 frm += frm[1] + 2;
1060 }
1061 switch (seq) {
1062 case IEEE80211_AUTH_SHARED_CHALLENGE:
1063 case IEEE80211_AUTH_SHARED_RESPONSE:
1064 if (challenge == NULL) {
1065 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1066 ni->ni_macaddr, "shared key auth",
1067 "%s", "no challenge");
1068 vap->iv_stats.is_rx_bad_auth++;
1069 estatus = IEEE80211_STATUS_CHALLENGE;
1070 goto bad;
1071 }
1072 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1073 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1074 ni->ni_macaddr, "shared key auth",
1075 "bad challenge len %d", challenge[1]);
1076 vap->iv_stats.is_rx_bad_auth++;
1077 estatus = IEEE80211_STATUS_CHALLENGE;
1078 goto bad;
1079 }
1080 default:
1081 break;
1082 }
1083 switch (seq) {
1084 case IEEE80211_AUTH_SHARED_REQUEST:
1085 if (ni == vap->iv_bss) {
1086 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1087 if (ni == NULL) {
1088 /* NB: no way to return an error */
1089 return;
1090 }
1091 allocbs = 1;
1092 } else {
1093 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1094 (void) ieee80211_ref_node(ni);
1095 allocbs = 0;
1096 }
1097 /*
1098 * Mark the node as referenced to reflect that it's
1099 * reference count has been bumped to insure it remains
1100 * after the transaction completes.
1101 */
1102 ni->ni_flags |= IEEE80211_NODE_AREF;
1103 /*
1104 * Mark the node as requiring a valid associatio id
1105 * before outbound traffic is permitted.
1106 */
1107 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1108 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1109 ni->ni_noise = nf;
1110 if (!ieee80211_alloc_challenge(ni)) {
1111 /* NB: don't return error so they rexmit */
1112 return;
1113 }
1114 get_random_bytes(ni->ni_challenge,
1115 IEEE80211_CHALLENGE_LEN);
1116 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1117 ni, "shared key %sauth request", allocbs ? "" : "re");
1118 /*
1119 * When the ACL policy is set to RADIUS we defer the
1120 * authorization to a user agent. Dispatch an event,
1121 * a subsequent MLME call will decide the fate of the
1122 * station. If the user agent is not present then the
1123 * node will be reclaimed due to inactivity.
1124 */
1125 if (vap->iv_acl != NULL &&
1126 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1127 IEEE80211_NOTE_MAC(vap,
1128 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1129 ni->ni_macaddr,
1130 "%s", "station authentication defered (radius acl)");
1131 ieee80211_notify_node_auth(ni);
1132 return;
1133 }
1134 break;
1135 case IEEE80211_AUTH_SHARED_RESPONSE:
1136 if (ni == vap->iv_bss) {
1137 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1138 ni->ni_macaddr, "shared key response",
1139 "%s", "unknown station");
1140 /* NB: don't send a response */
1141 return;
1142 }
1143 if (ni->ni_challenge == NULL) {
1144 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1145 ni->ni_macaddr, "shared key response",
1146 "%s", "no challenge recorded");
1147 vap->iv_stats.is_rx_bad_auth++;
1148 estatus = IEEE80211_STATUS_CHALLENGE;
1149 goto bad;
1150 }
1151 if (memcmp(ni->ni_challenge, &challenge[2],
1152 challenge[1]) != 0) {
1153 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1154 ni->ni_macaddr, "shared key response",
1155 "%s", "challenge mismatch");
1156 vap->iv_stats.is_rx_auth_fail++;
1157 estatus = IEEE80211_STATUS_CHALLENGE;
1158 goto bad;
1159 }
1160 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1161 ni, "%s", "station authenticated (shared key)");
1162 ieee80211_node_authorize(ni);
1163 break;
1164 default:
1165 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1166 ni->ni_macaddr, "shared key auth",
1167 "bad seq %d", seq);
1168 vap->iv_stats.is_rx_bad_auth++;
1169 estatus = IEEE80211_STATUS_SEQUENCE;
1170 goto bad;
1171 }
1172 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1173 return;
1174bad:
1175 /*
1176 * Send an error response; but only when operating as an AP.
1177 */
1178 /* XXX hack to workaround calling convention */
1179 ieee80211_send_error(ni, wh->i_addr2,
1180 IEEE80211_FC0_SUBTYPE_AUTH,
1181 (seq + 1) | (estatus<<16));
1182}
1183
1184/*
1185 * Convert a WPA cipher selector OUI to an internal
1186 * cipher algorithm. Where appropriate we also
1187 * record any key length.
1188 */
1189static int
1190wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1191{
1192#define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1193 uint32_t w = LE_READ_4(sel);
1194
1195 switch (w) {
1196 case WPA_SEL(WPA_CSE_NULL):
1197 return IEEE80211_CIPHER_NONE;
1198 case WPA_SEL(WPA_CSE_WEP40):
1199 if (keylen)
1200 *keylen = 40 / NBBY;
1201 return IEEE80211_CIPHER_WEP;
1202 case WPA_SEL(WPA_CSE_WEP104):
1203 if (keylen)
1204 *keylen = 104 / NBBY;
1205 return IEEE80211_CIPHER_WEP;
1206 case WPA_SEL(WPA_CSE_TKIP):
1207 return IEEE80211_CIPHER_TKIP;
1208 case WPA_SEL(WPA_CSE_CCMP):
1209 return IEEE80211_CIPHER_AES_CCM;
1210 }
1211 return 32; /* NB: so 1<< is discarded */
1212#undef WPA_SEL
1213}
1214
1215/*
1216 * Convert a WPA key management/authentication algorithm
1217 * to an internal code.
1218 */
1219static int
1220wpa_keymgmt(const uint8_t *sel)
1221{
1222#define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1223 uint32_t w = LE_READ_4(sel);
1224
1225 switch (w) {
1226 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1227 return WPA_ASE_8021X_UNSPEC;
1228 case WPA_SEL(WPA_ASE_8021X_PSK):
1229 return WPA_ASE_8021X_PSK;
1230 case WPA_SEL(WPA_ASE_NONE):
1231 return WPA_ASE_NONE;
1232 }
1233 return 0; /* NB: so is discarded */
1234#undef WPA_SEL
1235}
1236
1237/*
1238 * Parse a WPA information element to collect parameters.
1239 * Note that we do not validate security parameters; that
1240 * is handled by the authenticator; the parsing done here
1241 * is just for internal use in making operational decisions.
1242 */
1243static int
1244ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1245 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1246{
1247 uint8_t len = frm[1];
1248 uint32_t w;
1249 int n;
1250
1251 /*
1252 * Check the length once for fixed parts: OUI, type,
1253 * version, mcast cipher, and 2 selector counts.
1254 * Other, variable-length data, must be checked separately.
1255 */
1256 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1257 IEEE80211_DISCARD_IE(vap,
1258 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1259 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1260 return IEEE80211_REASON_IE_INVALID;
1261 }
1262 if (len < 14) {
1263 IEEE80211_DISCARD_IE(vap,
1264 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1265 wh, "WPA", "too short, len %u", len);
1266 return IEEE80211_REASON_IE_INVALID;
1267 }
1268 frm += 6, len -= 4; /* NB: len is payload only */
1269 /* NB: iswpaoui already validated the OUI and type */
1270 w = LE_READ_2(frm);
1271 if (w != WPA_VERSION) {
1272 IEEE80211_DISCARD_IE(vap,
1273 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1274 wh, "WPA", "bad version %u", w);
1275 return IEEE80211_REASON_IE_INVALID;
1276 }
1277 frm += 2, len -= 2;
1278
1279 memset(rsn, 0, sizeof(*rsn));
1280
1281 /* multicast/group cipher */
1282 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1283 frm += 4, len -= 4;
1284
1285 /* unicast ciphers */
1286 n = LE_READ_2(frm);
1287 frm += 2, len -= 2;
1288 if (len < n*4+2) {
1289 IEEE80211_DISCARD_IE(vap,
1290 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1291 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1292 len, n);
1293 return IEEE80211_REASON_IE_INVALID;
1294 }
1295 w = 0;
1296 for (; n > 0; n--) {
1297 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1298 frm += 4, len -= 4;
1299 }
1300 if (w & (1<<IEEE80211_CIPHER_TKIP))
1301 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1302 else
1303 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1304
1305 /* key management algorithms */
1306 n = LE_READ_2(frm);
1307 frm += 2, len -= 2;
1308 if (len < n*4) {
1309 IEEE80211_DISCARD_IE(vap,
1310 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1311 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1312 len, n);
1313 return IEEE80211_REASON_IE_INVALID;
1314 }
1315 w = 0;
1316 for (; n > 0; n--) {
1317 w |= wpa_keymgmt(frm);
1318 frm += 4, len -= 4;
1319 }
1320 if (w & WPA_ASE_8021X_UNSPEC)
1321 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1322 else
1323 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1324
1325 if (len > 2) /* optional capabilities */
1326 rsn->rsn_caps = LE_READ_2(frm);
1327
1328 return 0;
1329}
1330
1331/*
1332 * Convert an RSN cipher selector OUI to an internal
1333 * cipher algorithm. Where appropriate we also
1334 * record any key length.
1335 */
1336static int
1337rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1338{
1339#define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1340 uint32_t w = LE_READ_4(sel);
1341
1342 switch (w) {
1343 case RSN_SEL(RSN_CSE_NULL):
1344 return IEEE80211_CIPHER_NONE;
1345 case RSN_SEL(RSN_CSE_WEP40):
1346 if (keylen)
1347 *keylen = 40 / NBBY;
1348 return IEEE80211_CIPHER_WEP;
1349 case RSN_SEL(RSN_CSE_WEP104):
1350 if (keylen)
1351 *keylen = 104 / NBBY;
1352 return IEEE80211_CIPHER_WEP;
1353 case RSN_SEL(RSN_CSE_TKIP):
1354 return IEEE80211_CIPHER_TKIP;
1355 case RSN_SEL(RSN_CSE_CCMP):
1356 return IEEE80211_CIPHER_AES_CCM;
1357 case RSN_SEL(RSN_CSE_WRAP):
1358 return IEEE80211_CIPHER_AES_OCB;
1359 }
1360 return 32; /* NB: so 1<< is discarded */
1361#undef WPA_SEL
1362}
1363
1364/*
1365 * Convert an RSN key management/authentication algorithm
1366 * to an internal code.
1367 */
1368static int
1369rsn_keymgmt(const uint8_t *sel)
1370{
1371#define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1372 uint32_t w = LE_READ_4(sel);
1373
1374 switch (w) {
1375 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1376 return RSN_ASE_8021X_UNSPEC;
1377 case RSN_SEL(RSN_ASE_8021X_PSK):
1378 return RSN_ASE_8021X_PSK;
1379 case RSN_SEL(RSN_ASE_NONE):
1380 return RSN_ASE_NONE;
1381 }
1382 return 0; /* NB: so is discarded */
1383#undef RSN_SEL
1384}
1385
1386/*
1387 * Parse a WPA/RSN information element to collect parameters
1388 * and validate the parameters against what has been
1389 * configured for the system.
1390 */
1391static int
1392ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1393 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1394{
1395 uint8_t len = frm[1];
1396 uint32_t w;
1397 int n;
1398
1399 /*
1400 * Check the length once for fixed parts:
1401 * version, mcast cipher, and 2 selector counts.
1402 * Other, variable-length data, must be checked separately.
1403 */
1404 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1405 IEEE80211_DISCARD_IE(vap,
1406 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1407 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1408 return IEEE80211_REASON_IE_INVALID;
1409 }
1410 if (len < 10) {
1411 IEEE80211_DISCARD_IE(vap,
1412 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1413 wh, "RSN", "too short, len %u", len);
1414 return IEEE80211_REASON_IE_INVALID;
1415 }
1416 frm += 2;
1417 w = LE_READ_2(frm);
1418 if (w != RSN_VERSION) {
1419 IEEE80211_DISCARD_IE(vap,
1420 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1421 wh, "RSN", "bad version %u", w);
1422 return IEEE80211_REASON_IE_INVALID;
1423 }
1424 frm += 2, len -= 2;
1425
1426 memset(rsn, 0, sizeof(*rsn));
1427
1428 /* multicast/group cipher */
1429 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1430 frm += 4, len -= 4;
1431
1432 /* unicast ciphers */
1433 n = LE_READ_2(frm);
1434 frm += 2, len -= 2;
1435 if (len < n*4+2) {
1436 IEEE80211_DISCARD_IE(vap,
1437 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1438 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1439 len, n);
1440 return IEEE80211_REASON_IE_INVALID;
1441 }
1442 w = 0;
1443 for (; n > 0; n--) {
1444 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1445 frm += 4, len -= 4;
1446 }
1447 if (w & (1<<IEEE80211_CIPHER_TKIP))
1448 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1449 else
1450 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1451
1452 /* key management algorithms */
1453 n = LE_READ_2(frm);
1454 frm += 2, len -= 2;
1455 if (len < n*4) {
1456 IEEE80211_DISCARD_IE(vap,
1457 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1458 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1459 len, n);
1460 return IEEE80211_REASON_IE_INVALID;
1461 }
1462 w = 0;
1463 for (; n > 0; n--) {
1464 w |= rsn_keymgmt(frm);
1465 frm += 4, len -= 4;
1466 }
1467 if (w & RSN_ASE_8021X_UNSPEC)
1468 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1469 else
1470 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1471
1472 /* optional RSN capabilities */
1473 if (len > 2)
1474 rsn->rsn_caps = LE_READ_2(frm);
1475 /* XXXPMKID */
1476
1477 return 0;
1478}
1479
1480/*
1481 * WPA/802.11i assocation request processing.
1482 */
1483static int
1484wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1485 const struct ieee80211_frame *wh, const uint8_t *wpa,
1486 const uint8_t *rsn, uint16_t capinfo)
1487{
1488 struct ieee80211vap *vap = ni->ni_vap;
1489 uint8_t reason;
1490 int badwparsn;
1491
1492 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1493 if (wpa == NULL && rsn == NULL) {
1494 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1495 /*
1496 * W-Fi Protected Setup (WPS) permits
1497 * clients to associate and pass EAPOL frames
1498 * to establish initial credentials.
1499 */
1500 ni->ni_flags |= IEEE80211_NODE_WPS;
1501 return 1;
1502 }
1503 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1504 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1505 /*
1506 * Transitional Security Network. Permits clients
1507 * to associate and use WEP while WPA is configured.
1508 */
1509 ni->ni_flags |= IEEE80211_NODE_TSN;
1510 return 1;
1511 }
1512 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1513 wh, NULL, "%s", "no WPA/RSN IE in association request");
1514 vap->iv_stats.is_rx_assoc_badwpaie++;
1515 reason = IEEE80211_REASON_IE_INVALID;
1516 goto bad;
1517 }
1518 /* assert right association security credentials */
1519 badwparsn = 0; /* NB: to silence compiler */
1520 switch (vap->iv_flags & IEEE80211_F_WPA) {
1521 case IEEE80211_F_WPA1:
1522 badwparsn = (wpa == NULL);
1523 break;
1524 case IEEE80211_F_WPA2:
1525 badwparsn = (rsn == NULL);
1526 break;
1527 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1528 badwparsn = (wpa == NULL && rsn == NULL);
1529 break;
1530 }
1531 if (badwparsn) {
1532 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1533 wh, NULL,
1534 "%s", "missing WPA/RSN IE in association request");
1535 vap->iv_stats.is_rx_assoc_badwpaie++;
1536 reason = IEEE80211_REASON_IE_INVALID;
1537 goto bad;
1538 }
1539 /*
1540 * Parse WPA/RSN information element.
1541 */
1542 if (wpa != NULL)
1543 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1544 else
1545 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1546 if (reason != 0) {
1547 /* XXX distinguish WPA/RSN? */
1548 vap->iv_stats.is_rx_assoc_badwpaie++;
1549 goto bad;
1550 }
1551 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1552 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1553 wpa != NULL ? "WPA" : "RSN",
1554 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1555 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1556 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1557
1558 return 1;
1559bad:
1560 ieee80211_node_deauth(ni, reason);
1561 return 0;
1562}
1563
1564/* XXX find a better place for definition */
1565struct l2_update_frame {
1566 struct ether_header eh;
1567 uint8_t dsap;
1568 uint8_t ssap;
1569 uint8_t control;
1570 uint8_t xid[3];
1571} __packed;
1572
1573/*
1574 * Deliver a TGf L2UF frame on behalf of a station.
1575 * This primes any bridge when the station is roaming
1576 * between ap's on the same wired network.
1577 */
1578static void
1579ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1580{
1581 struct ieee80211vap *vap = ni->ni_vap;
1582 struct ifnet *ifp = vap->iv_ifp;
1583 struct mbuf *m;
1584 struct l2_update_frame *l2uf;
1585 struct ether_header *eh;
1586
543d1dec 1587 m = m_gethdr(MB_DONTWAIT, MT_DATA);
32176cfd
RP
1588 if (m == NULL) {
1589 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1590 "%s", "no mbuf for l2uf frame");
1591 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1592 return;
1593 }
1594 l2uf = mtod(m, struct l2_update_frame *);
1595 eh = &l2uf->eh;
1596 /* dst: Broadcast address */
1597 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1598 /* src: associated STA */
1599 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1600 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1601
1602 l2uf->dsap = 0;
1603 l2uf->ssap = 0;
1604 l2uf->control = 0xf5;
1605 l2uf->xid[0] = 0x81;
1606 l2uf->xid[1] = 0x80;
1607 l2uf->xid[2] = 0x00;
1608
1609 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1610 hostap_deliver_data(vap, ni, m);
1611}
1612
1613static void
1614ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1615 int reassoc, int resp, const char *tag, int rate)
1616{
1617 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1618 "deny %s request, %s rate set mismatch, rate/MCS %d",
1619 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1620 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1621 ieee80211_node_leave(ni);
1622}
1623
1624static void
1625capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1626 int reassoc, int resp, const char *tag, int capinfo)
1627{
1628 struct ieee80211vap *vap = ni->ni_vap;
1629
1630 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1631 "deny %s request, %s mismatch 0x%x",
1632 reassoc ? "reassoc" : "assoc", tag, capinfo);
1633 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1634 ieee80211_node_leave(ni);
1635 vap->iv_stats.is_rx_assoc_capmismatch++;
1636}
1637
1638static void
1639htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1640 int reassoc, int resp)
1641{
1642 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
085ff963 1643 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
32176cfd
RP
1644 /* XXX no better code */
1645 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1646 ieee80211_node_leave(ni);
1647}
1648
1649static void
1650authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1651 int algo, int seq, int status)
1652{
1653 struct ieee80211vap *vap = ni->ni_vap;
1654
1655 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1656 wh, NULL, "unsupported alg %d", algo);
1657 vap->iv_stats.is_rx_auth_unsupported++;
1658 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1659 seq | (status << 16));
1660}
1661
1662static __inline int
1663ishtmixed(const uint8_t *ie)
1664{
1665 const struct ieee80211_ie_htinfo *ht =
1666 (const struct ieee80211_ie_htinfo *) ie;
1667 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1668 IEEE80211_HTINFO_OPMODE_MIXED;
1669}
1670
1671static int
1672is11bclient(const uint8_t *rates, const uint8_t *xrates)
1673{
1674 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1675 int i;
1676
1677 /* NB: the 11b clients we care about will not have xrates */
1678 if (xrates != NULL || rates == NULL)
1679 return 0;
1680 for (i = 0; i < rates[1]; i++) {
1681 int r = rates[2+i] & IEEE80211_RATE_VAL;
1682 if (r > 2*11 || ((1<<r) & brates) == 0)
1683 return 0;
1684 }
1685 return 1;
1686}
1687
1688static void
1689hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1690 int subtype, int rssi, int nf)
1691{
1692 struct ieee80211vap *vap = ni->ni_vap;
1693 struct ieee80211com *ic = ni->ni_ic;
1694 struct ieee80211_frame *wh;
1695 uint8_t *frm, *efrm, *sfrm;
1696 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1697 int reassoc, resp;
1698 uint8_t rate;
1699
1700 wh = mtod(m0, struct ieee80211_frame *);
1701 frm = (uint8_t *)&wh[1];
1702 efrm = mtod(m0, uint8_t *) + m0->m_len;
1703 switch (subtype) {
1704 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1705 case IEEE80211_FC0_SUBTYPE_BEACON: {
1706 struct ieee80211_scanparams scan;
1707 /*
1708 * We process beacon/probe response frames when scanning;
1709 * otherwise we check beacon frames for overlapping non-ERP
1710 * BSS in 11g and/or overlapping legacy BSS when in HT.
1711 */
1712 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1713 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1714 vap->iv_stats.is_rx_mgtdiscard++;
1715 return;
1716 }
1717 /* NB: accept off-channel frames */
1718 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1719 return;
1720 /*
1721 * Count frame now that we know it's to be processed.
1722 */
1723 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1724 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1725 IEEE80211_NODE_STAT(ni, rx_beacons);
1726 } else
1727 IEEE80211_NODE_STAT(ni, rx_proberesp);
1728 /*
1729 * If scanning, just pass information to the scan module.
1730 */
1731 if (ic->ic_flags & IEEE80211_F_SCAN) {
1732 if (scan.status == 0 && /* NB: on channel */
1733 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1734 /*
1735 * Actively scanning a channel marked passive;
1736 * send a probe request now that we know there
1737 * is 802.11 traffic present.
1738 *
1739 * XXX check if the beacon we recv'd gives
1740 * us what we need and suppress the probe req
1741 */
1742 ieee80211_probe_curchan(vap, 1);
1743 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1744 }
1745 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1746 return;
1747 }
1748 /*
1749 * Check beacon for overlapping bss w/ non ERP stations.
1750 * If we detect one and protection is configured but not
1751 * enabled, enable it and start a timer that'll bring us
1752 * out if we stop seeing the bss.
1753 */
1754 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1755 scan.status == 0 && /* NB: on-channel */
1756 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1757 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1758 ic->ic_lastnonerp = ticks;
1759 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1760 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1761 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1762 IEEE80211_NOTE_FRAME(vap,
1763 IEEE80211_MSG_ASSOC, wh,
1764 "non-ERP present on channel %d "
1765 "(saw erp 0x%x from channel %d), "
1766 "enable use of protection",
1767 ic->ic_curchan->ic_ieee,
1768 scan.erp, scan.chan);
1769 ic->ic_flags |= IEEE80211_F_USEPROT;
1770 ieee80211_notify_erp(ic);
1771 }
1772 }
1773 /*
1774 * Check beacon for non-HT station on HT channel
1775 * and update HT BSS occupancy as appropriate.
1776 */
1777 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1778 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1779 /*
1780 * Off control channel; only check frames
1781 * that come in the extension channel when
1782 * operating w/ HT40.
1783 */
1784 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1785 break;
1786 if (scan.chan != ic->ic_curchan->ic_extieee)
1787 break;
1788 }
1789 if (scan.htinfo == NULL) {
1790 ieee80211_htprot_update(ic,
1791 IEEE80211_HTINFO_OPMODE_PROTOPT |
1792 IEEE80211_HTINFO_NONHT_PRESENT);
1793 } else if (ishtmixed(scan.htinfo)) {
1794 /* XXX? take NONHT_PRESENT from beacon? */
1795 ieee80211_htprot_update(ic,
1796 IEEE80211_HTINFO_OPMODE_MIXED |
1797 IEEE80211_HTINFO_NONHT_PRESENT);
1798 }
1799 }
1800 break;
1801 }
1802
1803 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1804 if (vap->iv_state != IEEE80211_S_RUN) {
1805 vap->iv_stats.is_rx_mgtdiscard++;
1806 return;
1807 }
085ff963
MD
1808 /*
1809 * Consult the ACL policy module if setup.
1810 */
1811 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1812 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1813 wh, NULL, "%s", "disallowed by ACL");
1814 vap->iv_stats.is_rx_acl++;
1815 return;
1816 }
32176cfd
RP
1817 /*
1818 * prreq frame format
1819 * [tlv] ssid
1820 * [tlv] supported rates
1821 * [tlv] extended supported rates
1822 */
1823 ssid = rates = xrates = NULL;
1824 sfrm = frm;
1825 while (efrm - frm > 1) {
1826 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1827 switch (*frm) {
1828 case IEEE80211_ELEMID_SSID:
1829 ssid = frm;
1830 break;
1831 case IEEE80211_ELEMID_RATES:
1832 rates = frm;
1833 break;
1834 case IEEE80211_ELEMID_XRATES:
1835 xrates = frm;
1836 break;
1837 }
1838 frm += frm[1] + 2;
1839 }
1840 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1841 if (xrates != NULL)
1842 IEEE80211_VERIFY_ELEMENT(xrates,
1843 IEEE80211_RATE_MAXSIZE - rates[1], return);
1844 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1845 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1846 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1847 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1848 wh, NULL,
1849 "%s", "no ssid with ssid suppression enabled");
1850 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1851 return;
1852 }
1853
1854 /* XXX find a better class or define it's own */
1855 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1856 "%s", "recv probe req");
1857 /*
1858 * Some legacy 11b clients cannot hack a complete
1859 * probe response frame. When the request includes
1860 * only a bare-bones rate set, communicate this to
1861 * the transmit side.
1862 */
1863 ieee80211_send_proberesp(vap, wh->i_addr2,
1864 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1865 break;
1866
1867 case IEEE80211_FC0_SUBTYPE_AUTH: {
1868 uint16_t algo, seq, status;
1869
1870 if (vap->iv_state != IEEE80211_S_RUN) {
1871 vap->iv_stats.is_rx_mgtdiscard++;
1872 return;
1873 }
1874 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1875 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1876 wh, NULL, "%s", "wrong bssid");
1877 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1878 return;
1879 }
1880 /*
1881 * auth frame format
1882 * [2] algorithm
1883 * [2] sequence
1884 * [2] status
1885 * [tlv*] challenge
1886 */
1887 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1888 algo = le16toh(*(uint16_t *)frm);
1889 seq = le16toh(*(uint16_t *)(frm + 2));
1890 status = le16toh(*(uint16_t *)(frm + 4));
1891 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1892 "recv auth frame with algorithm %d seq %d", algo, seq);
1893 /*
1894 * Consult the ACL policy module if setup.
1895 */
085ff963 1896 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
32176cfd
RP
1897 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1898 wh, NULL, "%s", "disallowed by ACL");
1899 vap->iv_stats.is_rx_acl++;
1900 ieee80211_send_error(ni, wh->i_addr2,
1901 IEEE80211_FC0_SUBTYPE_AUTH,
1902 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1903 return;
1904 }
1905 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1906 IEEE80211_DISCARD(vap,
1907 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1908 wh, NULL, "%s", "TKIP countermeasures enabled");
1909 vap->iv_stats.is_rx_auth_countermeasures++;
1910 ieee80211_send_error(ni, wh->i_addr2,
1911 IEEE80211_FC0_SUBTYPE_AUTH,
1912 IEEE80211_REASON_MIC_FAILURE);
1913 return;
1914 }
1915 if (algo == IEEE80211_AUTH_ALG_SHARED)
1916 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1917 seq, status);
1918 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1919 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1920 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1921 authalgreject(ni, wh, algo,
1922 seq+1, IEEE80211_STATUS_ALG);
1923 return;
1924 } else {
1925 /*
1926 * We assume that an unknown algorithm is the result
1927 * of a decryption failure on a shared key auth frame;
1928 * return a status code appropriate for that instead
1929 * of IEEE80211_STATUS_ALG.
1930 *
1931 * NB: a seq# of 4 is intentional; the decrypted
1932 * frame likely has a bogus seq value.
1933 */
1934 authalgreject(ni, wh, algo,
1935 4, IEEE80211_STATUS_CHALLENGE);
1936 return;
1937 }
1938 break;
1939 }
1940
1941 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1942 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1943 uint16_t capinfo, lintval;
1944 struct ieee80211_rsnparms rsnparms;
1945
1946 if (vap->iv_state != IEEE80211_S_RUN) {
1947 vap->iv_stats.is_rx_mgtdiscard++;
1948 return;
1949 }
1950 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1951 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1952 wh, NULL, "%s", "wrong bssid");
1953 vap->iv_stats.is_rx_assoc_bss++;
1954 return;
1955 }
1956 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1957 reassoc = 1;
1958 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1959 } else {
1960 reassoc = 0;
1961 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1962 }
1963 if (ni == vap->iv_bss) {
1964 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1965 "deny %s request, sta not authenticated",
1966 reassoc ? "reassoc" : "assoc");
1967 ieee80211_send_error(ni, wh->i_addr2,
1968 IEEE80211_FC0_SUBTYPE_DEAUTH,
1969 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1970 vap->iv_stats.is_rx_assoc_notauth++;
1971 return;
1972 }
1973
1974 /*
1975 * asreq frame format
1976 * [2] capability information
1977 * [2] listen interval
1978 * [6*] current AP address (reassoc only)
1979 * [tlv] ssid
1980 * [tlv] supported rates
1981 * [tlv] extended supported rates
1982 * [tlv] WPA or RSN
1983 * [tlv] HT capabilities
1984 * [tlv] Atheros capabilities
1985 */
1986 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1987 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1988 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1989 if (reassoc)
1990 frm += 6; /* ignore current AP info */
1991 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1992 sfrm = frm;
1993 while (efrm - frm > 1) {
1994 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1995 switch (*frm) {
1996 case IEEE80211_ELEMID_SSID:
1997 ssid = frm;
1998 break;
1999 case IEEE80211_ELEMID_RATES:
2000 rates = frm;
2001 break;
2002 case IEEE80211_ELEMID_XRATES:
2003 xrates = frm;
2004 break;
2005 case IEEE80211_ELEMID_RSN:
2006 rsn = frm;
2007 break;
2008 case IEEE80211_ELEMID_HTCAP:
2009 htcap = frm;
2010 break;
2011 case IEEE80211_ELEMID_VENDOR:
2012 if (iswpaoui(frm))
2013 wpa = frm;
2014 else if (iswmeinfo(frm))
2015 wme = frm;
2016#ifdef IEEE80211_SUPPORT_SUPERG
2017 else if (isatherosoui(frm))
2018 ath = frm;
2019#endif
2020 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2021 if (ishtcapoui(frm) && htcap == NULL)
2022 htcap = frm;
2023 }
2024 break;
2025 }
2026 frm += frm[1] + 2;
2027 }
2028 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2029 if (xrates != NULL)
2030 IEEE80211_VERIFY_ELEMENT(xrates,
2031 IEEE80211_RATE_MAXSIZE - rates[1], return);
2032 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2033 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2034 if (htcap != NULL) {
2035 IEEE80211_VERIFY_LENGTH(htcap[1],
2036 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2037 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2038 sizeof(struct ieee80211_ie_htcap)-2,
2039 return); /* XXX just NULL out? */
2040 }
2041
2042 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2043 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2044 return;
2045 /* discard challenge after association */
2046 if (ni->ni_challenge != NULL) {
2047 kfree(ni->ni_challenge, M_80211_NODE);
2048 ni->ni_challenge = NULL;
2049 }
2050 /* NB: 802.11 spec says to ignore station's privacy bit */
2051 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2052 capinfomismatch(ni, wh, reassoc, resp,
2053 "capability", capinfo);
2054 return;
2055 }
2056 /*
2057 * Disallow re-associate w/ invalid slot time setting.
2058 */
2059 if (ni->ni_associd != 0 &&
2060 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2061 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2062 capinfomismatch(ni, wh, reassoc, resp,
2063 "slot time", capinfo);
2064 return;
2065 }
2066 rate = ieee80211_setup_rates(ni, rates, xrates,
2067 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2068 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2069 if (rate & IEEE80211_RATE_BASIC) {
2070 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2071 vap->iv_stats.is_rx_assoc_norate++;
2072 return;
2073 }
2074 /*
2075 * If constrained to 11g-only stations reject an
2076 * 11b-only station. We cheat a bit here by looking
2077 * at the max negotiated xmit rate and assuming anyone
2078 * with a best rate <24Mb/s is an 11b station.
2079 */
2080 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2081 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2082 vap->iv_stats.is_rx_assoc_norate++;
2083 return;
2084 }
2085 /*
2086 * Do HT rate set handling and setup HT node state.
2087 */
2088 ni->ni_chan = vap->iv_bss->ni_chan;
2089 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2090 rate = ieee80211_setup_htrates(ni, htcap,
2091 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2092 IEEE80211_F_DOBRS);
2093 if (rate & IEEE80211_RATE_BASIC) {
2094 ratesetmismatch(ni, wh, reassoc, resp,
2095 "HT", rate);
2096 vap->iv_stats.is_ht_assoc_norate++;
2097 return;
2098 }
2099 ieee80211_ht_node_init(ni);
2100 ieee80211_ht_updatehtcap(ni, htcap);
2101 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2102 ieee80211_ht_node_cleanup(ni);
2103#ifdef IEEE80211_SUPPORT_SUPERG
2104 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2105 ieee80211_ff_node_cleanup(ni);
2106#endif
2107 /*
2108 * Allow AMPDU operation only with unencrypted traffic
2109 * or AES-CCM; the 11n spec only specifies these ciphers
2110 * so permitting any others is undefined and can lead
2111 * to interoperability problems.
2112 */
2113 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2114 (((vap->iv_flags & IEEE80211_F_WPA) &&
2115 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2116 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2117 IEEE80211_NOTE(vap,
2118 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2119 "disallow HT use because WEP or TKIP requested, "
2120 "capinfo 0x%x ucastcipher %d", capinfo,
2121 rsnparms.rsn_ucastcipher);
2122 ieee80211_ht_node_cleanup(ni);
2123 vap->iv_stats.is_ht_assoc_downgrade++;
2124 }
2125 /*
2126 * If constrained to 11n-only stations reject legacy stations.
2127 */
2128 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2129 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2130 htcapmismatch(ni, wh, reassoc, resp);
2131 vap->iv_stats.is_ht_assoc_nohtcap++;
2132 return;
2133 }
2134 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2135 ni->ni_noise = nf;
2136 ni->ni_intval = lintval;
2137 ni->ni_capinfo = capinfo;
2138 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2139 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2140 /*
2141 * Store the IEs.
2142 * XXX maybe better to just expand
2143 */
2144 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2145#define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2146 if (wpa != NULL)
2147 setie(wpa_ie, wpa - sfrm);
2148 if (rsn != NULL)
2149 setie(rsn_ie, rsn - sfrm);
2150 if (htcap != NULL)
2151 setie(htcap_ie, htcap - sfrm);
2152 if (wme != NULL) {
2153 setie(wme_ie, wme - sfrm);
2154 /*
2155 * Mark node as capable of QoS.
2156 */
2157 ni->ni_flags |= IEEE80211_NODE_QOS;
2158 } else
2159 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2160#ifdef IEEE80211_SUPPORT_SUPERG
2161 if (ath != NULL) {
2162 setie(ath_ie, ath - sfrm);
2163 /*
2164 * Parse ATH station parameters.
2165 */
2166 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2167 } else
2168#endif
2169 ni->ni_ath_flags = 0;
2170#undef setie
2171 } else {
2172 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2173 ni->ni_ath_flags = 0;
2174 }
2175 ieee80211_node_join(ni, resp);
2176 ieee80211_deliver_l2uf(ni);
2177 break;
2178 }
2179
2180 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2181 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2182 uint16_t reason;
2183
2184 if (vap->iv_state != IEEE80211_S_RUN ||
2185 /* NB: can happen when in promiscuous mode */
2186 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2187 vap->iv_stats.is_rx_mgtdiscard++;
2188 break;
2189 }
2190 /*
2191 * deauth/disassoc frame format
2192 * [2] reason
2193 */
2194 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2195 reason = le16toh(*(uint16_t *)frm);
2196 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2197 vap->iv_stats.is_rx_deauth++;
2198 IEEE80211_NODE_STAT(ni, rx_deauth);
2199 } else {
2200 vap->iv_stats.is_rx_disassoc++;
2201 IEEE80211_NODE_STAT(ni, rx_disassoc);
2202 }
2203 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2204 "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2205 IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2206 if (ni != vap->iv_bss)
2207 ieee80211_node_leave(ni);
2208 break;
2209 }
2210
2211 case IEEE80211_FC0_SUBTYPE_ACTION:
085ff963
MD
2212 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2213 if (ni == vap->iv_bss) {
2214 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2215 wh, NULL, "%s", "unknown node");
2216 vap->iv_stats.is_rx_mgtdiscard++;
2217 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2218 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2219 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2220 wh, NULL, "%s", "not for us");
2221 vap->iv_stats.is_rx_mgtdiscard++;
2222 } else if (vap->iv_state != IEEE80211_S_RUN) {
2223 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2224 wh, NULL, "wrong state %s",
2225 ieee80211_state_name[vap->iv_state]);
32176cfd 2226 vap->iv_stats.is_rx_mgtdiscard++;
085ff963
MD
2227 } else {
2228 if (ieee80211_parse_action(ni, m0) == 0)
2229 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2230 }
32176cfd
RP
2231 break;
2232
2233 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2234 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
085ff963
MD
2235 case IEEE80211_FC0_SUBTYPE_ATIM:
2236 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2237 wh, NULL, "%s", "not handled");
2238 vap->iv_stats.is_rx_mgtdiscard++;
2239 break;
2240
32176cfd
RP
2241 default:
2242 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
085ff963 2243 wh, "mgt", "subtype 0x%x not handled", subtype);
32176cfd
RP
2244 vap->iv_stats.is_rx_badsubtype++;
2245 break;
2246 }
2247}
2248
2249static void
2250hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2251{
2252 switch (subtype) {
2253 case IEEE80211_FC0_SUBTYPE_PS_POLL:
085ff963 2254 ni->ni_vap->iv_recv_pspoll(ni, m);
32176cfd
RP
2255 break;
2256 case IEEE80211_FC0_SUBTYPE_BAR:
2257 ieee80211_recv_bar(ni, m);
2258 break;
2259 }
2260}
2261
2262/*
2263 * Process a received ps-poll frame.
2264 */
085ff963
MD
2265void
2266ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
32176cfd
RP
2267{
2268 struct ieee80211vap *vap = ni->ni_vap;
085ff963 2269 struct ieee80211com *ic = vap->iv_ic;
32176cfd 2270 struct ieee80211_frame_min *wh;
32176cfd
RP
2271 struct mbuf *m;
2272 uint16_t aid;
2273 int qlen;
2274
2275 wh = mtod(m0, struct ieee80211_frame_min *);
2276 if (ni->ni_associd == 0) {
2277 IEEE80211_DISCARD(vap,
2278 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2279 (struct ieee80211_frame *) wh, NULL,
2280 "%s", "unassociated station");
2281 vap->iv_stats.is_ps_unassoc++;
2282 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2283 IEEE80211_REASON_NOT_ASSOCED);
2284 return;
2285 }
2286
2287 aid = le16toh(*(uint16_t *)wh->i_dur);
2288 if (aid != ni->ni_associd) {
2289 IEEE80211_DISCARD(vap,
2290 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2291 (struct ieee80211_frame *) wh, NULL,
2292 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2293 ni->ni_associd, aid);
2294 vap->iv_stats.is_ps_badaid++;
2295 /*
2296 * NB: We used to deauth the station but it turns out
2297 * the Blackberry Curve 8230 (and perhaps other devices)
2298 * sometimes send the wrong AID when WME is negotiated.
2299 * Being more lenient here seems ok as we already check
2300 * the station is associated and we only return frames
2301 * queued for the station (i.e. we don't use the AID).
2302 */
2303 return;
2304 }
2305
2306 /* Okay, take the first queued packet and put it out... */
2307 m = ieee80211_node_psq_dequeue(ni, &qlen);
2308 if (m == NULL) {
2309 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2310 "%s", "recv ps-poll, but queue empty");
2311 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2312 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2313 if (vap->iv_set_tim != NULL)
2314 vap->iv_set_tim(ni, 0); /* just in case */
2315 return;
2316 }
2317 /*
2318 * If there are more packets, set the more packets bit
2319 * in the packet dispatched to the station; otherwise
2320 * turn off the TIM bit.
2321 */
2322 if (qlen != 0) {
2323 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2324 "recv ps-poll, send packet, %u still queued", qlen);
2325 m->m_flags |= M_MORE_DATA;
2326 } else {
2327 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2328 "%s", "recv ps-poll, send packet, queue empty");
2329 if (vap->iv_set_tim != NULL)
2330 vap->iv_set_tim(ni, 0);
2331 }
2332 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2333
085ff963
MD
2334 /*
2335 * Do the right thing; if it's an encap'ed frame then
2336 * call ieee80211_parent_xmitpkt() (and free the ref) else
2337 * call ieee80211_vap_xmitpkt().
2338 */
2339 if (m->m_flags & M_ENCAP) {
2340 if (ieee80211_parent_xmitpkt(ic, m) != 0)
2341 ieee80211_free_node(ni);
2342 } else {
2343 (void) ieee80211_vap_xmitpkt(vap, m);
2344 }
32176cfd 2345}