2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 * IEEE 802.11 IBSS mode support.
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/endian.h>
48 #include <sys/errno.h>
50 #include <sys/sysctl.h>
53 #include <net/if_var.h>
54 #include <net/if_media.h>
55 #include <net/if_llc.h>
56 #include <net/if_private.h>
57 #include <net/ethernet.h>
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_adhoc.h>
63 #include <net80211/ieee80211_input.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <net80211/ieee80211_superg.h>
67 #ifdef IEEE80211_SUPPORT_TDMA
68 #include <net80211/ieee80211_tdma.h>
70 #include <net80211/ieee80211_sta.h>
72 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
74 static void adhoc_vattach(struct ieee80211vap *);
75 static int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
76 static int adhoc_input(struct ieee80211_node *, struct mbuf *,
77 const struct ieee80211_rx_stats *, int, int);
78 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
79 int subtype, const struct ieee80211_rx_stats *, int, int);
80 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
81 int subtype, const struct ieee80211_rx_stats *rxs, int, int);
82 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
85 ieee80211_adhoc_attach(struct ieee80211com *ic)
87 ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
88 ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
92 ieee80211_adhoc_detach(struct ieee80211com *ic)
97 adhoc_vdetach(struct ieee80211vap *vap)
102 adhoc_vattach(struct ieee80211vap *vap)
104 vap->iv_newstate = adhoc_newstate;
105 vap->iv_input = adhoc_input;
106 if (vap->iv_opmode == IEEE80211_M_IBSS)
107 vap->iv_recv_mgmt = adhoc_recv_mgmt;
109 vap->iv_recv_mgmt = ahdemo_recv_mgmt;
110 vap->iv_recv_ctl = adhoc_recv_ctl;
111 vap->iv_opdetach = adhoc_vdetach;
112 #ifdef IEEE80211_SUPPORT_TDMA
114 * Throw control to tdma support. Note we do this
115 * after setting up our callbacks so it can piggyback
118 if (vap->iv_caps & IEEE80211_C_TDMA)
119 ieee80211_tdma_vattach(vap);
124 sta_leave(void *arg, struct ieee80211_node *ni)
126 struct ieee80211vap *vap = ni->ni_vap;
128 if (ni != vap->iv_bss)
129 ieee80211_node_leave(ni);
133 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
136 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
138 struct ieee80211com *ic = vap->iv_ic;
139 struct ieee80211_node *ni;
140 enum ieee80211_state ostate;
142 IEEE80211_LOCK_ASSERT(vap->iv_ic);
144 ostate = vap->iv_state;
145 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
146 __func__, ieee80211_state_name[ostate],
147 ieee80211_state_name[nstate], arg);
148 vap->iv_state = nstate; /* state transition */
149 if (ostate != IEEE80211_S_SCAN)
150 ieee80211_cancel_scan(vap); /* background scan */
151 ni = vap->iv_bss; /* NB: no reference held */
153 case IEEE80211_S_INIT:
155 case IEEE80211_S_SCAN:
156 ieee80211_cancel_scan(vap);
161 if (ostate != IEEE80211_S_INIT) {
162 /* NB: optimize INIT -> INIT case */
163 ieee80211_reset_bss(vap);
166 case IEEE80211_S_SCAN:
168 case IEEE80211_S_RUN: /* beacon miss */
169 /* purge station table; entries are stale */
170 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
173 case IEEE80211_S_INIT:
174 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
175 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
177 * Already have a channel; bypass the
178 * scan and startup immediately.
180 ieee80211_create_ibss(vap,
181 ieee80211_ht_adjust_channel(ic,
182 vap->iv_des_chan, vap->iv_flags_ht));
186 * Initiate a scan. We can come here as a result
187 * of an IEEE80211_IOC_SCAN_REQ too in which case
188 * the vap will be marked with IEEE80211_FEXT_SCANREQ
189 * and the scan request parameters will be present
190 * in iv_scanreq. Otherwise we do the default.
192 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
193 ieee80211_check_scan(vap,
194 vap->iv_scanreq_flags,
195 vap->iv_scanreq_duration,
196 vap->iv_scanreq_mindwell,
197 vap->iv_scanreq_maxdwell,
198 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
199 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
201 ieee80211_check_scan_current(vap);
203 case IEEE80211_S_SCAN:
205 * This can happen because of a change in state
206 * that requires a reset. Trigger a new scan
207 * unless we're in manual roaming mode in which
208 * case an application must issue an explicit request.
210 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
211 ieee80211_check_scan_current(vap);
217 case IEEE80211_S_RUN:
218 if (vap->iv_flags & IEEE80211_F_WPA) {
219 /* XXX validate prerequisites */
222 case IEEE80211_S_INIT:
224 * Already have a channel; bypass the
225 * scan and startup immediately.
226 * Note that ieee80211_create_ibss will call
227 * back to do a RUN->RUN state change.
229 ieee80211_create_ibss(vap,
230 ieee80211_ht_adjust_channel(ic,
231 ic->ic_curchan, vap->iv_flags_ht));
232 /* NB: iv_bss is changed on return */
235 case IEEE80211_S_SCAN:
236 #ifdef IEEE80211_DEBUG
237 if (ieee80211_msg_debug(vap)) {
239 "synchronized with %s ssid ",
240 ether_sprintf(ni->ni_bssid));
241 ieee80211_print_essid(vap->iv_bss->ni_essid,
244 printf(" channel %d start %uMb\n",
245 ieee80211_chan2ieee(ic, ic->ic_curchan),
246 IEEE80211_RATE2MBS(ni->ni_txrate));
250 case IEEE80211_S_RUN: /* IBSS merge */
256 * When 802.1x is not in use mark the port authorized
257 * at this point so traffic can flow.
259 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
260 ieee80211_node_authorize(ni);
262 * Fake association when joining an existing bss.
264 if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
265 ic->ic_newassoc != NULL)
266 ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
268 case IEEE80211_S_SLEEP:
269 vap->iv_sta_ps(vap, 0);
273 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
274 "%s: unexpected state transition %s -> %s\n", __func__,
275 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
282 * Decide if a received management frame should be
283 * printed when debugging is enabled. This filters some
284 * of the less interesting frames that come frequently
288 doprint(struct ieee80211vap *vap, int subtype)
291 case IEEE80211_FC0_SUBTYPE_BEACON:
292 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
293 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
300 * Process a received frame. The node associated with the sender
301 * should be supplied. If nothing was found in the node table then
302 * the caller is assumed to supply a reference to iv_bss instead.
303 * The RSSI and a timestamp are also supplied. The RSSI data is used
304 * during AP scanning to select a AP to associate with; it can have
305 * any units so long as values have consistent units and higher values
306 * mean ``better signal''. The receive timestamp is currently not used
307 * by the 802.11 layer.
310 adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
311 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
313 struct ieee80211vap *vap = ni->ni_vap;
314 struct ieee80211com *ic = ni->ni_ic;
315 struct ifnet *ifp = vap->iv_ifp;
316 struct ieee80211_frame *wh;
317 struct ieee80211_key *key;
318 struct ether_header *eh;
319 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
320 uint8_t dir, type, subtype, qos;
322 int is_hw_decrypted = 0;
323 int has_decrypted = 0;
326 * Some devices do hardware decryption all the way through
327 * to pretending the frame wasn't encrypted in the first place.
328 * So, tag it appropriately so it isn't discarded inappropriately.
330 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
333 if (m->m_flags & M_AMPDU_MPDU) {
335 * Fastpath for A-MPDU reorder q resubmission. Frames
336 * w/ M_AMPDU_MPDU marked have already passed through
337 * here but were received out of order and been held on
338 * the reorder queue. When resubmitted they are marked
339 * with the M_AMPDU_MPDU flag and we can bypass most of
340 * the normal processing.
342 wh = mtod(m, struct ieee80211_frame *);
343 type = IEEE80211_FC0_TYPE_DATA;
344 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
345 subtype = IEEE80211_FC0_SUBTYPE_QOS_DATA;
346 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
350 KASSERT(ni != NULL, ("null node"));
351 ni->ni_inact = ni->ni_inact_reload;
353 type = -1; /* undefined */
355 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
356 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
357 ni->ni_macaddr, NULL,
358 "too short (1): len %u", m->m_pkthdr.len);
359 vap->iv_stats.is_rx_tooshort++;
363 * Bit of a cheat here, we use a pointer for a 3-address
364 * frame format but don't reference fields past outside
365 * ieee80211_frame_min w/o first validating the data is
368 wh = mtod(m, struct ieee80211_frame *);
370 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
371 IEEE80211_FC0_VERSION_0) {
372 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
373 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
374 wh->i_fc[0], wh->i_fc[1]);
375 vap->iv_stats.is_rx_badversion++;
379 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
380 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
381 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
382 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
383 if (dir != IEEE80211_FC1_DIR_NODS)
385 else if (type == IEEE80211_FC0_TYPE_CTL)
388 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
389 IEEE80211_DISCARD_MAC(vap,
390 IEEE80211_MSG_ANY, ni->ni_macaddr,
391 NULL, "too short (2): len %u",
393 vap->iv_stats.is_rx_tooshort++;
399 * Validate the bssid.
401 if (!(type == IEEE80211_FC0_TYPE_MGT &&
402 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
403 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
404 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
405 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
406 /* not interested in */
407 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
408 bssid, NULL, "%s", "not to bss");
409 vap->iv_stats.is_rx_wrongbss++;
413 * Data frame, cons up a node when it doesn't
414 * exist. This should probably done after an ACL check.
416 if (type == IEEE80211_FC0_TYPE_DATA &&
418 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
420 * Beware of frames that come in too early; we
421 * can receive broadcast frames and creating sta
422 * entries will blow up because there is no bss
425 if (vap->iv_state != IEEE80211_S_RUN) {
426 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
427 wh, "data", "not in RUN state (%s)",
428 ieee80211_state_name[vap->iv_state]);
429 vap->iv_stats.is_rx_badstate++;
433 * Fake up a node for this newly discovered member
436 * Note: This doesn't "upgrade" the node to 11n;
437 * that will happen after a probe request/response
440 ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
442 /* NB: stat kept for alloc failure */
446 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
448 if (IEEE80211_HAS_SEQ(type, subtype) &&
449 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
450 uint8_t tid = ieee80211_gettid(wh);
451 if (IEEE80211_QOS_HAS_SEQ(wh) &&
452 TID_TO_WME_AC(tid) >= WME_AC_VI)
453 ic->ic_wme.wme_hipri_traffic++;
454 if (! ieee80211_check_rxseq(ni, wh, bssid, rxs))
460 case IEEE80211_FC0_TYPE_DATA:
461 hdrspace = ieee80211_hdrspace(ic, wh);
462 if (m->m_len < hdrspace &&
463 (m = m_pullup(m, hdrspace)) == NULL) {
464 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
465 ni->ni_macaddr, NULL,
466 "data too short: expecting %u", hdrspace);
467 vap->iv_stats.is_rx_tooshort++;
470 if (dir != IEEE80211_FC1_DIR_NODS) {
471 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
472 wh, "data", "incorrect dir 0x%x", dir);
473 vap->iv_stats.is_rx_wrongdir++;
476 /* XXX no power-save support */
479 * Handle A-MPDU re-ordering. If the frame is to be
480 * processed directly then ieee80211_ampdu_reorder
481 * will return 0; otherwise it has consumed the mbuf
482 * and we should do nothing more with it.
484 if ((m->m_flags & M_AMPDU) &&
485 ieee80211_ampdu_reorder(ni, m, rxs) != 0) {
492 * Handle privacy requirements. Note that we
493 * must not be preempted from here until after
494 * we (potentially) call ieee80211_crypto_demic;
495 * otherwise we may violate assumptions in the
496 * crypto cipher modules used to do delayed update
497 * of replay sequence numbers.
499 if (is_hw_decrypted || IEEE80211_IS_PROTECTED(wh)) {
500 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
502 * Discard encrypted frames when privacy is off.
504 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
505 wh, "WEP", "%s", "PRIVACY off");
506 vap->iv_stats.is_rx_noprivacy++;
507 IEEE80211_NODE_STAT(ni, rx_noprivacy);
510 if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
511 /* NB: stats+msgs handled in crypto_decap */
512 IEEE80211_NODE_STAT(ni, rx_wepfail);
515 wh = mtod(m, struct ieee80211_frame *);
516 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
519 /* XXX M_WEP and IEEE80211_F_PRIVACY */
524 * Save QoS bits for use below--before we strip the header.
526 if (subtype == IEEE80211_FC0_SUBTYPE_QOS_DATA)
527 qos = ieee80211_getqos(wh)[0];
532 * Next up, any fragmentation.
534 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
535 m = ieee80211_defrag(ni, m, hdrspace, has_decrypted);
537 /* Fragment dropped or frame not complete yet */
541 wh = NULL; /* no longer valid, catch any uses */
544 * Next strip any MSDU crypto bits.
546 if (!ieee80211_crypto_demic(vap, key, m, 0)) {
547 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
548 ni->ni_macaddr, "data", "%s", "demic error");
549 vap->iv_stats.is_rx_demicfail++;
550 IEEE80211_NODE_STAT(ni, rx_demicfail);
554 /* copy to listener after decrypt */
555 if (ieee80211_radiotap_active_vap(vap))
556 ieee80211_radiotap_rx(vap, m);
560 * Finally, strip the 802.11 header.
562 m = ieee80211_decap(vap, m, hdrspace, qos);
564 /* XXX mask bit to check for both */
565 /* don't count Null data frames as errors */
566 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
567 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
569 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
570 ni->ni_macaddr, "data", "%s", "decap error");
571 vap->iv_stats.is_rx_decap++;
572 IEEE80211_NODE_STAT(ni, rx_decap);
575 if (!(qos & IEEE80211_QOS_AMSDU))
576 eh = mtod(m, struct ether_header *);
579 if (!ieee80211_node_is_authorized(ni)) {
581 * Deny any non-PAE frames received prior to
582 * authorization. For open/shared-key
583 * authentication the port is mark authorized
584 * after authentication completes. For 802.1x
585 * the port is not marked authorized by the
586 * authenticator until the handshake has completed.
589 eh->ether_type != htons(ETHERTYPE_PAE)) {
590 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
591 ni->ni_macaddr, "data", "unauthorized or "
592 "unknown port: ether type 0x%x len %u",
593 eh == NULL ? -1 : eh->ether_type,
595 vap->iv_stats.is_rx_unauth++;
596 IEEE80211_NODE_STAT(ni, rx_unauth);
601 * When denying unencrypted frames, discard
602 * any non-PAE frames received without encryption.
604 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
605 ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
606 (is_hw_decrypted == 0) &&
608 eh->ether_type != htons(ETHERTYPE_PAE))) {
610 * Drop unencrypted frames.
612 vap->iv_stats.is_rx_unencrypted++;
613 IEEE80211_NODE_STAT(ni, rx_unencrypted);
617 /* XXX require HT? */
618 if (qos & IEEE80211_QOS_AMSDU) {
619 m = ieee80211_decap_amsdu(ni, m);
621 return IEEE80211_FC0_TYPE_DATA;
623 #ifdef IEEE80211_SUPPORT_SUPERG
624 m = ieee80211_decap_fastframe(vap, ni, m);
626 return IEEE80211_FC0_TYPE_DATA;
629 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
630 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
632 ieee80211_deliver_data(vap, ni, m);
633 return IEEE80211_FC0_TYPE_DATA;
635 case IEEE80211_FC0_TYPE_MGT:
636 vap->iv_stats.is_rx_mgmt++;
637 IEEE80211_NODE_STAT(ni, rx_mgmt);
638 if (dir != IEEE80211_FC1_DIR_NODS) {
639 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
640 wh, "data", "incorrect dir 0x%x", dir);
641 vap->iv_stats.is_rx_wrongdir++;
644 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
645 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
646 ni->ni_macaddr, "mgt", "too short: len %u",
648 vap->iv_stats.is_rx_tooshort++;
651 #ifdef IEEE80211_DEBUG
652 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
653 ieee80211_msg_dumppkts(vap)) {
654 if_printf(ifp, "received %s from %s rssi %d\n",
655 ieee80211_mgt_subtype_name(subtype),
656 ether_sprintf(wh->i_addr2), rssi);
659 if (IEEE80211_IS_PROTECTED(wh)) {
660 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
661 wh, NULL, "%s", "WEP set but not permitted");
662 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
665 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
668 case IEEE80211_FC0_TYPE_CTL:
669 vap->iv_stats.is_rx_ctl++;
670 IEEE80211_NODE_STAT(ni, rx_ctrl);
671 vap->iv_recv_ctl(ni, m, subtype);
675 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
676 wh, "bad", "frame type 0x%x", type);
677 /* should not come here */
681 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
684 if (need_tap && ieee80211_radiotap_active_vap(vap))
685 ieee80211_radiotap_rx(vap, m);
692 is11bclient(const uint8_t *rates, const uint8_t *xrates)
694 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
697 /* NB: the 11b clients we care about will not have xrates */
698 if (xrates != NULL || rates == NULL)
700 for (i = 0; i < rates[1]; i++) {
701 int r = rates[2+i] & IEEE80211_RATE_VAL;
702 if (r > 2*11 || ((1<<r) & brates) == 0)
709 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
710 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
712 struct ieee80211vap *vap = ni->ni_vap;
713 struct ieee80211com *ic = ni->ni_ic;
714 struct ieee80211_channel *rxchan = ic->ic_curchan;
715 struct ieee80211_frame *wh;
717 uint8_t *ssid, *rates, *xrates;
719 int ht_state_change = 0;
722 wh = mtod(m0, struct ieee80211_frame *);
723 frm = (uint8_t *)&wh[1];
724 efrm = mtod(m0, uint8_t *) + m0->m_len;
726 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
727 "%s: recv mgmt frame, addr2=%6D, ni=%p (%6D) fc=%.02x %.02x\n",
735 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
736 case IEEE80211_FC0_SUBTYPE_BEACON: {
737 struct ieee80211_scanparams scan;
738 struct ieee80211_channel *c;
740 * We process beacon/probe response
741 * frames to discover neighbors.
744 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
748 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
751 * Count frame now that we know it's to be processed.
753 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
754 vap->iv_stats.is_rx_beacon++; /* XXX remove */
755 IEEE80211_NODE_STAT(ni, rx_beacons);
757 IEEE80211_NODE_STAT(ni, rx_proberesp);
759 * If scanning, just pass information to the scan module.
761 if (ic->ic_flags & IEEE80211_F_SCAN) {
762 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
764 * Actively scanning a channel marked passive;
765 * send a probe request now that we know there
766 * is 802.11 traffic present.
768 * XXX check if the beacon we recv'd gives
769 * us what we need and suppress the probe req
771 ieee80211_probe_curchan(vap, 1);
772 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
774 ieee80211_add_scan(vap, rxchan, &scan, wh,
778 if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
779 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
781 * Create a new entry in the neighbor table.
785 * Here we're not scanning; so if we have an
786 * SSID then make sure it matches our SSID.
787 * Otherwise this code will match on all IBSS
788 * beacons/probe requests for all SSIDs,
789 * filling the node table with nodes that
792 if (ieee80211_ibss_node_check_new(ni, &scan)) {
793 ni = ieee80211_add_neighbor(vap, wh, &scan);
795 * Send a probe request so we announce 11n
798 ieee80211_send_probereq(ni, /* node */
799 vap->iv_myaddr, /* SA */
800 ni->ni_macaddr, /* DA */
801 vap->iv_bss->ni_bssid, /* BSSID */
802 vap->iv_bss->ni_essid,
803 vap->iv_bss->ni_esslen); /* SSID */
808 * Send a probe request so we announce 11n
811 * Don't do this if we're scanning.
813 if (! (ic->ic_flags & IEEE80211_F_SCAN))
814 ieee80211_send_probereq(ni, /* node */
815 vap->iv_myaddr, /* SA */
816 ni->ni_macaddr, /* DA */
817 vap->iv_bss->ni_bssid, /* BSSID */
818 vap->iv_bss->ni_essid,
819 vap->iv_bss->ni_esslen); /* SSID */
821 } else if (ni->ni_capinfo == 0) {
823 * Update faked node created on transmit.
824 * Note this also updates the tsf.
826 ieee80211_init_neighbor(ni, wh, &scan);
829 * Send a probe request so we announce 11n
832 ieee80211_send_probereq(ni, /* node */
833 vap->iv_myaddr, /* SA */
834 ni->ni_macaddr, /* DA */
835 vap->iv_bss->ni_bssid, /* BSSID */
836 vap->iv_bss->ni_essid,
837 vap->iv_bss->ni_esslen); /* SSID */
840 * Record tsf for potential resync.
842 memcpy(ni->ni_tstamp.data, scan.tstamp,
843 sizeof(ni->ni_tstamp));
846 * This isn't enabled yet - otherwise it would
847 * update the HT parameters and channel width
848 * from any node, which could lead to lots of
849 * strange behaviour if the 11n nodes aren't
850 * exactly configured to match.
853 if (scan.htcap != NULL && scan.htinfo != NULL &&
854 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
855 ieee80211_ht_updateparams(ni,
856 scan.htcap, scan.htinfo));
857 if (ieee80211_ht_updateparams_final(ni,
858 scan.htcap, scan.htinfo))
862 /* XXX same for VHT? */
865 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
869 * Same here - the channel width change should
870 * be applied to the specific peer node, not
871 * to the ic. Ie, the interface configuration
872 * should stay in its current channel width;
873 * but it should change the rate control and
874 * any queued frames for the given node only.
876 * Since there's no (current) way to inform
877 * the driver that a channel width change has
878 * occurred for a single node, just stub this
883 ieee80211_update_chw(ic);
889 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
890 if (vap->iv_state != IEEE80211_S_RUN) {
891 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
892 wh, NULL, "wrong state %s",
893 ieee80211_state_name[vap->iv_state]);
894 vap->iv_stats.is_rx_mgtdiscard++;
897 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
898 /* frame must be directed */
899 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
900 wh, NULL, "%s", "not unicast");
901 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
908 * [tlv] supported rates
909 * [tlv] extended supported rates
911 ssid = rates = xrates = NULL;
912 while (efrm - frm > 1) {
913 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
915 case IEEE80211_ELEMID_SSID:
918 case IEEE80211_ELEMID_RATES:
921 case IEEE80211_ELEMID_XRATES:
927 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
929 IEEE80211_VERIFY_ELEMENT(xrates,
930 IEEE80211_RATE_MAXSIZE - rates[1], return);
931 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
932 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
933 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
934 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
936 "%s", "no ssid with ssid suppression enabled");
937 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
941 /* XXX find a better class or define it's own */
942 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
943 "%s", "recv probe req");
945 * Some legacy 11b clients cannot hack a complete
946 * probe response frame. When the request includes
947 * only a bare-bones rate set, communicate this to
950 ieee80211_send_proberesp(vap, wh->i_addr2,
951 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
954 * Note: we don't benefit from stashing the probe request
955 * IEs away to use for IBSS negotiation, because we
956 * typically don't get all of the IEs.
960 case IEEE80211_FC0_SUBTYPE_ACTION:
961 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
962 if ((ni == vap->iv_bss) &&
963 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
964 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
965 wh, NULL, "%s", "unknown node");
966 vap->iv_stats.is_rx_mgtdiscard++;
967 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
968 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
969 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
970 wh, NULL, "%s", "not for us");
971 vap->iv_stats.is_rx_mgtdiscard++;
972 } else if (vap->iv_state != IEEE80211_S_RUN) {
973 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
974 wh, NULL, "wrong state %s",
975 ieee80211_state_name[vap->iv_state]);
976 vap->iv_stats.is_rx_mgtdiscard++;
978 if (ieee80211_parse_action(ni, m0) == 0)
979 (void)ic->ic_recv_action(ni, wh, frm, efrm);
983 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
984 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
985 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
986 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
987 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
988 case IEEE80211_FC0_SUBTYPE_ATIM:
989 case IEEE80211_FC0_SUBTYPE_DISASSOC:
990 case IEEE80211_FC0_SUBTYPE_AUTH:
991 case IEEE80211_FC0_SUBTYPE_DEAUTH:
992 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
993 wh, NULL, "%s", "not handled");
994 vap->iv_stats.is_rx_mgtdiscard++;
998 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
999 wh, "mgt", "subtype 0x%x not handled", subtype);
1000 vap->iv_stats.is_rx_badsubtype++;
1004 #undef IEEE80211_VERIFY_LENGTH
1005 #undef IEEE80211_VERIFY_ELEMENT
1008 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1009 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1011 struct ieee80211vap *vap = ni->ni_vap;
1012 struct ieee80211com *ic = ni->ni_ic;
1015 * Process management frames when scanning; useful for doing
1018 if (ic->ic_flags & IEEE80211_F_SCAN)
1019 adhoc_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
1021 #ifdef IEEE80211_DEBUG
1022 struct ieee80211_frame *wh;
1024 wh = mtod(m0, struct ieee80211_frame *);
1027 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1028 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1029 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1030 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1031 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1032 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1033 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1034 case IEEE80211_FC0_SUBTYPE_BEACON:
1035 case IEEE80211_FC0_SUBTYPE_ATIM:
1036 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1037 case IEEE80211_FC0_SUBTYPE_AUTH:
1038 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1039 case IEEE80211_FC0_SUBTYPE_ACTION:
1040 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1041 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1042 wh, NULL, "%s", "not handled");
1043 vap->iv_stats.is_rx_mgtdiscard++;
1046 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1047 wh, "mgt", "subtype 0x%x not handled", subtype);
1048 vap->iv_stats.is_rx_badsubtype++;
1055 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1059 case IEEE80211_FC0_SUBTYPE_BAR:
1060 ieee80211_recv_bar(ni, m);