2 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 IBSS mode support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
48 #include <sys/sysctl.h>
51 #include <net/if_var.h>
52 #include <net/if_media.h>
53 #include <net/if_llc.h>
54 #include <net/ethernet.h>
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_adhoc.h>
60 #include <net80211/ieee80211_input.h>
61 #ifdef IEEE80211_SUPPORT_SUPERG
62 #include <net80211/ieee80211_superg.h>
64 #ifdef IEEE80211_SUPPORT_TDMA
65 #include <net80211/ieee80211_tdma.h>
67 #include <net80211/ieee80211_sta.h>
69 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
71 static void adhoc_vattach(struct ieee80211vap *);
72 static int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73 static int adhoc_input(struct ieee80211_node *, struct mbuf *,
74 const struct ieee80211_rx_stats *, int, int);
75 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 int subtype, const struct ieee80211_rx_stats *, int, int);
77 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
78 int subtype, const struct ieee80211_rx_stats *rxs, int, int);
79 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
82 ieee80211_adhoc_attach(struct ieee80211com *ic)
84 ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
85 ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
89 ieee80211_adhoc_detach(struct ieee80211com *ic)
94 adhoc_vdetach(struct ieee80211vap *vap)
99 adhoc_vattach(struct ieee80211vap *vap)
101 vap->iv_newstate = adhoc_newstate;
102 vap->iv_input = adhoc_input;
103 if (vap->iv_opmode == IEEE80211_M_IBSS)
104 vap->iv_recv_mgmt = adhoc_recv_mgmt;
106 vap->iv_recv_mgmt = ahdemo_recv_mgmt;
107 vap->iv_recv_ctl = adhoc_recv_ctl;
108 vap->iv_opdetach = adhoc_vdetach;
109 #ifdef IEEE80211_SUPPORT_TDMA
111 * Throw control to tdma support. Note we do this
112 * after setting up our callbacks so it can piggyback
115 if (vap->iv_caps & IEEE80211_C_TDMA)
116 ieee80211_tdma_vattach(vap);
121 sta_leave(void *arg, struct ieee80211_node *ni)
123 struct ieee80211vap *vap = ni->ni_vap;
125 if (ni != vap->iv_bss)
126 ieee80211_node_leave(ni);
130 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
133 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
135 struct ieee80211com *ic = vap->iv_ic;
136 struct ieee80211_node *ni;
137 enum ieee80211_state ostate;
139 IEEE80211_LOCK_ASSERT(vap->iv_ic);
141 ostate = vap->iv_state;
142 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
143 __func__, ieee80211_state_name[ostate],
144 ieee80211_state_name[nstate], arg);
145 vap->iv_state = nstate; /* state transition */
146 if (ostate != IEEE80211_S_SCAN)
147 ieee80211_cancel_scan(vap); /* background scan */
148 ni = vap->iv_bss; /* NB: no reference held */
150 case IEEE80211_S_INIT:
152 case IEEE80211_S_SCAN:
153 ieee80211_cancel_scan(vap);
158 if (ostate != IEEE80211_S_INIT) {
159 /* NB: optimize INIT -> INIT case */
160 ieee80211_reset_bss(vap);
163 case IEEE80211_S_SCAN:
165 case IEEE80211_S_RUN: /* beacon miss */
166 /* purge station table; entries are stale */
167 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
170 case IEEE80211_S_INIT:
171 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
172 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
174 * Already have a channel; bypass the
175 * scan and startup immediately.
177 ieee80211_create_ibss(vap,
178 ieee80211_ht_adjust_channel(ic,
179 vap->iv_des_chan, vap->iv_flags_ht));
183 * Initiate a scan. We can come here as a result
184 * of an IEEE80211_IOC_SCAN_REQ too in which case
185 * the vap will be marked with IEEE80211_FEXT_SCANREQ
186 * and the scan request parameters will be present
187 * in iv_scanreq. Otherwise we do the default.
189 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
190 ieee80211_check_scan(vap,
191 vap->iv_scanreq_flags,
192 vap->iv_scanreq_duration,
193 vap->iv_scanreq_mindwell,
194 vap->iv_scanreq_maxdwell,
195 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
196 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
198 ieee80211_check_scan_current(vap);
200 case IEEE80211_S_SCAN:
202 * This can happen because of a change in state
203 * that requires a reset. Trigger a new scan
204 * unless we're in manual roaming mode in which
205 * case an application must issue an explicit request.
207 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
208 ieee80211_check_scan_current(vap);
214 case IEEE80211_S_RUN:
215 if (vap->iv_flags & IEEE80211_F_WPA) {
216 /* XXX validate prerequisites */
219 case IEEE80211_S_INIT:
221 * Already have a channel; bypass the
222 * scan and startup immediately.
223 * Note that ieee80211_create_ibss will call
224 * back to do a RUN->RUN state change.
226 ieee80211_create_ibss(vap,
227 ieee80211_ht_adjust_channel(ic,
228 ic->ic_curchan, vap->iv_flags_ht));
229 /* NB: iv_bss is changed on return */
232 case IEEE80211_S_SCAN:
233 #ifdef IEEE80211_DEBUG
234 if (ieee80211_msg_debug(vap)) {
236 "synchronized with %s ssid ",
237 ether_sprintf(ni->ni_bssid));
238 ieee80211_print_essid(vap->iv_bss->ni_essid,
241 printf(" channel %d start %uMb\n",
242 ieee80211_chan2ieee(ic, ic->ic_curchan),
243 IEEE80211_RATE2MBS(ni->ni_txrate));
247 case IEEE80211_S_RUN: /* IBSS merge */
253 * When 802.1x is not in use mark the port authorized
254 * at this point so traffic can flow.
256 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
257 ieee80211_node_authorize(ni);
259 * Fake association when joining an existing bss.
261 if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
262 ic->ic_newassoc != NULL)
263 ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
265 case IEEE80211_S_SLEEP:
266 vap->iv_sta_ps(vap, 0);
270 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
271 "%s: unexpected state transition %s -> %s\n", __func__,
272 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
279 * Decide if a received management frame should be
280 * printed when debugging is enabled. This filters some
281 * of the less interesting frames that come frequently
285 doprint(struct ieee80211vap *vap, int subtype)
288 case IEEE80211_FC0_SUBTYPE_BEACON:
289 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
290 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
297 * Process a received frame. The node associated with the sender
298 * should be supplied. If nothing was found in the node table then
299 * the caller is assumed to supply a reference to iv_bss instead.
300 * The RSSI and a timestamp are also supplied. The RSSI data is used
301 * during AP scanning to select a AP to associate with; it can have
302 * any units so long as values have consistent units and higher values
303 * mean ``better signal''. The receive timestamp is currently not used
304 * by the 802.11 layer.
307 adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
308 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
310 struct ieee80211vap *vap = ni->ni_vap;
311 struct ieee80211com *ic = ni->ni_ic;
312 struct ifnet *ifp = vap->iv_ifp;
313 struct ieee80211_frame *wh;
314 struct ieee80211_key *key;
315 struct ether_header *eh;
316 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
317 uint8_t dir, type, subtype, qos;
319 int is_hw_decrypted = 0;
320 int has_decrypted = 0;
323 * Some devices do hardware decryption all the way through
324 * to pretending the frame wasn't encrypted in the first place.
325 * So, tag it appropriately so it isn't discarded inappropriately.
327 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
330 if (m->m_flags & M_AMPDU_MPDU) {
332 * Fastpath for A-MPDU reorder q resubmission. Frames
333 * w/ M_AMPDU_MPDU marked have already passed through
334 * here but were received out of order and been held on
335 * the reorder queue. When resubmitted they are marked
336 * with the M_AMPDU_MPDU flag and we can bypass most of
337 * the normal processing.
339 wh = mtod(m, struct ieee80211_frame *);
340 type = IEEE80211_FC0_TYPE_DATA;
341 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
342 subtype = IEEE80211_FC0_SUBTYPE_QOS;
343 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
347 KASSERT(ni != NULL, ("null node"));
348 ni->ni_inact = ni->ni_inact_reload;
350 type = -1; /* undefined */
352 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
353 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
354 ni->ni_macaddr, NULL,
355 "too short (1): len %u", m->m_pkthdr.len);
356 vap->iv_stats.is_rx_tooshort++;
360 * Bit of a cheat here, we use a pointer for a 3-address
361 * frame format but don't reference fields past outside
362 * ieee80211_frame_min w/o first validating the data is
365 wh = mtod(m, struct ieee80211_frame *);
367 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
368 IEEE80211_FC0_VERSION_0) {
369 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
370 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
371 wh->i_fc[0], wh->i_fc[1]);
372 vap->iv_stats.is_rx_badversion++;
376 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
377 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
378 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
379 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
380 if (dir != IEEE80211_FC1_DIR_NODS)
382 else if (type == IEEE80211_FC0_TYPE_CTL)
385 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
386 IEEE80211_DISCARD_MAC(vap,
387 IEEE80211_MSG_ANY, ni->ni_macaddr,
388 NULL, "too short (2): len %u",
390 vap->iv_stats.is_rx_tooshort++;
396 * Validate the bssid.
398 if (!(type == IEEE80211_FC0_TYPE_MGT &&
399 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
400 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
401 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
402 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
403 /* not interested in */
404 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
405 bssid, NULL, "%s", "not to bss");
406 vap->iv_stats.is_rx_wrongbss++;
410 * Data frame, cons up a node when it doesn't
411 * exist. This should probably done after an ACL check.
413 if (type == IEEE80211_FC0_TYPE_DATA &&
415 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
417 * Beware of frames that come in too early; we
418 * can receive broadcast frames and creating sta
419 * entries will blow up because there is no bss
422 if (vap->iv_state != IEEE80211_S_RUN) {
423 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
424 wh, "data", "not in RUN state (%s)",
425 ieee80211_state_name[vap->iv_state]);
426 vap->iv_stats.is_rx_badstate++;
430 * Fake up a node for this newly discovered member
433 * Note: This doesn't "upgrade" the node to 11n;
434 * that will happen after a probe request/response
437 ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
439 /* NB: stat kept for alloc failure */
443 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
445 if (IEEE80211_HAS_SEQ(type, subtype) &&
446 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
447 uint8_t tid = ieee80211_gettid(wh);
448 if (IEEE80211_QOS_HAS_SEQ(wh) &&
449 TID_TO_WME_AC(tid) >= WME_AC_VI)
450 ic->ic_wme.wme_hipri_traffic++;
451 if (! ieee80211_check_rxseq(ni, wh, bssid))
457 case IEEE80211_FC0_TYPE_DATA:
458 hdrspace = ieee80211_hdrspace(ic, wh);
459 if (m->m_len < hdrspace &&
460 (m = m_pullup(m, hdrspace)) == NULL) {
461 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
462 ni->ni_macaddr, NULL,
463 "data too short: expecting %u", hdrspace);
464 vap->iv_stats.is_rx_tooshort++;
467 if (dir != IEEE80211_FC1_DIR_NODS) {
468 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
469 wh, "data", "incorrect dir 0x%x", dir);
470 vap->iv_stats.is_rx_wrongdir++;
473 /* XXX no power-save support */
476 * Handle A-MPDU re-ordering. If the frame is to be
477 * processed directly then ieee80211_ampdu_reorder
478 * will return 0; otherwise it has consumed the mbuf
479 * and we should do nothing more with it.
481 if ((m->m_flags & M_AMPDU) &&
482 ieee80211_ampdu_reorder(ni, m) != 0) {
489 * Handle privacy requirements. Note that we
490 * must not be preempted from here until after
491 * we (potentially) call ieee80211_crypto_demic;
492 * otherwise we may violate assumptions in the
493 * crypto cipher modules used to do delayed update
494 * of replay sequence numbers.
496 if (is_hw_decrypted || wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
497 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
499 * Discard encrypted frames when privacy is off.
501 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
502 wh, "WEP", "%s", "PRIVACY off");
503 vap->iv_stats.is_rx_noprivacy++;
504 IEEE80211_NODE_STAT(ni, rx_noprivacy);
507 if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
508 /* NB: stats+msgs handled in crypto_decap */
509 IEEE80211_NODE_STAT(ni, rx_wepfail);
512 wh = mtod(m, struct ieee80211_frame *);
513 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
516 /* XXX M_WEP and IEEE80211_F_PRIVACY */
521 * Save QoS bits for use below--before we strip the header.
523 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
524 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
525 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
526 ((struct ieee80211_qosframe *)wh)->i_qos[0];
531 * Next up, any fragmentation.
533 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
534 m = ieee80211_defrag(ni, m, hdrspace);
536 /* Fragment dropped or frame not complete yet */
540 wh = NULL; /* no longer valid, catch any uses */
543 * Next strip any MSDU crypto bits.
545 if (!ieee80211_crypto_demic(vap, key, m, 0)) {
546 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
547 ni->ni_macaddr, "data", "%s", "demic error");
548 vap->iv_stats.is_rx_demicfail++;
549 IEEE80211_NODE_STAT(ni, rx_demicfail);
553 /* copy to listener after decrypt */
554 if (ieee80211_radiotap_active_vap(vap))
555 ieee80211_radiotap_rx(vap, m);
559 * Finally, strip the 802.11 header.
561 m = ieee80211_decap(vap, m, hdrspace);
563 /* XXX mask bit to check for both */
564 /* don't count Null data frames as errors */
565 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
566 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
568 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
569 ni->ni_macaddr, "data", "%s", "decap error");
570 vap->iv_stats.is_rx_decap++;
571 IEEE80211_NODE_STAT(ni, rx_decap);
574 eh = mtod(m, struct ether_header *);
575 if (!ieee80211_node_is_authorized(ni)) {
577 * Deny any non-PAE frames received prior to
578 * authorization. For open/shared-key
579 * authentication the port is mark authorized
580 * after authentication completes. For 802.1x
581 * the port is not marked authorized by the
582 * authenticator until the handshake has completed.
584 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
585 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
586 eh->ether_shost, "data",
587 "unauthorized port: ether type 0x%x len %u",
588 eh->ether_type, m->m_pkthdr.len);
589 vap->iv_stats.is_rx_unauth++;
590 IEEE80211_NODE_STAT(ni, rx_unauth);
595 * When denying unencrypted frames, discard
596 * any non-PAE frames received without encryption.
598 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
599 ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
600 (is_hw_decrypted == 0) &&
601 eh->ether_type != htons(ETHERTYPE_PAE)) {
603 * Drop unencrypted frames.
605 vap->iv_stats.is_rx_unencrypted++;
606 IEEE80211_NODE_STAT(ni, rx_unencrypted);
610 /* XXX require HT? */
611 if (qos & IEEE80211_QOS_AMSDU) {
612 m = ieee80211_decap_amsdu(ni, m);
614 return IEEE80211_FC0_TYPE_DATA;
616 #ifdef IEEE80211_SUPPORT_SUPERG
617 m = ieee80211_decap_fastframe(vap, ni, m);
619 return IEEE80211_FC0_TYPE_DATA;
622 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
623 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
625 ieee80211_deliver_data(vap, ni, m);
626 return IEEE80211_FC0_TYPE_DATA;
628 case IEEE80211_FC0_TYPE_MGT:
629 vap->iv_stats.is_rx_mgmt++;
630 IEEE80211_NODE_STAT(ni, rx_mgmt);
631 if (dir != IEEE80211_FC1_DIR_NODS) {
632 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
633 wh, "data", "incorrect dir 0x%x", dir);
634 vap->iv_stats.is_rx_wrongdir++;
637 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
638 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
639 ni->ni_macaddr, "mgt", "too short: len %u",
641 vap->iv_stats.is_rx_tooshort++;
644 #ifdef IEEE80211_DEBUG
645 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
646 ieee80211_msg_dumppkts(vap)) {
647 if_printf(ifp, "received %s from %s rssi %d\n",
648 ieee80211_mgt_subtype_name(subtype),
649 ether_sprintf(wh->i_addr2), rssi);
652 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
653 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
654 wh, NULL, "%s", "WEP set but not permitted");
655 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
658 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
661 case IEEE80211_FC0_TYPE_CTL:
662 vap->iv_stats.is_rx_ctl++;
663 IEEE80211_NODE_STAT(ni, rx_ctrl);
664 vap->iv_recv_ctl(ni, m, subtype);
668 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
669 wh, "bad", "frame type 0x%x", type);
670 /* should not come here */
674 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
677 if (need_tap && ieee80211_radiotap_active_vap(vap))
678 ieee80211_radiotap_rx(vap, m);
685 is11bclient(const uint8_t *rates, const uint8_t *xrates)
687 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
690 /* NB: the 11b clients we care about will not have xrates */
691 if (xrates != NULL || rates == NULL)
693 for (i = 0; i < rates[1]; i++) {
694 int r = rates[2+i] & IEEE80211_RATE_VAL;
695 if (r > 2*11 || ((1<<r) & brates) == 0)
702 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
703 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
705 struct ieee80211vap *vap = ni->ni_vap;
706 struct ieee80211com *ic = ni->ni_ic;
707 struct ieee80211_channel *rxchan = ic->ic_curchan;
708 struct ieee80211_frame *wh;
710 uint8_t *ssid, *rates, *xrates;
712 int ht_state_change = 0;
715 wh = mtod(m0, struct ieee80211_frame *);
716 frm = (uint8_t *)&wh[1];
717 efrm = mtod(m0, uint8_t *) + m0->m_len;
719 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
720 case IEEE80211_FC0_SUBTYPE_BEACON: {
721 struct ieee80211_scanparams scan;
722 struct ieee80211_channel *c;
724 * We process beacon/probe response
725 * frames to discover neighbors.
728 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
732 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
735 * Count frame now that we know it's to be processed.
737 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
738 vap->iv_stats.is_rx_beacon++; /* XXX remove */
739 IEEE80211_NODE_STAT(ni, rx_beacons);
741 IEEE80211_NODE_STAT(ni, rx_proberesp);
743 * If scanning, just pass information to the scan module.
745 if (ic->ic_flags & IEEE80211_F_SCAN) {
746 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
748 * Actively scanning a channel marked passive;
749 * send a probe request now that we know there
750 * is 802.11 traffic present.
752 * XXX check if the beacon we recv'd gives
753 * us what we need and suppress the probe req
755 ieee80211_probe_curchan(vap, 1);
756 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
758 ieee80211_add_scan(vap, rxchan, &scan, wh,
762 if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
763 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
765 * Create a new entry in the neighbor table.
769 * Here we're not scanning; so if we have an
770 * SSID then make sure it matches our SSID.
771 * Otherwise this code will match on all IBSS
772 * beacons/probe requests for all SSIDs,
773 * filling the node table with nodes that
776 if (ieee80211_ibss_node_check_new(ni, &scan)) {
777 ni = ieee80211_add_neighbor(vap, wh, &scan);
779 * Send a probe request so we announce 11n
782 ieee80211_send_probereq(ni, /* node */
783 vap->iv_myaddr, /* SA */
784 ni->ni_macaddr, /* DA */
785 vap->iv_bss->ni_bssid, /* BSSID */
786 vap->iv_bss->ni_essid,
787 vap->iv_bss->ni_esslen); /* SSID */
791 } else if (ni->ni_capinfo == 0) {
793 * Update faked node created on transmit.
794 * Note this also updates the tsf.
796 ieee80211_init_neighbor(ni, wh, &scan);
799 * Send a probe request so we announce 11n
802 ieee80211_send_probereq(ni, /* node */
803 vap->iv_myaddr, /* SA */
804 ni->ni_macaddr, /* DA */
805 vap->iv_bss->ni_bssid, /* BSSID */
806 vap->iv_bss->ni_essid,
807 vap->iv_bss->ni_esslen); /* SSID */
810 * Record tsf for potential resync.
812 memcpy(ni->ni_tstamp.data, scan.tstamp,
813 sizeof(ni->ni_tstamp));
816 * This isn't enabled yet - otherwise it would
817 * update the HT parameters and channel width
818 * from any node, which could lead to lots of
819 * strange behaviour if the 11n nodes aren't
820 * exactly configured to match.
823 if (scan.htcap != NULL && scan.htinfo != NULL &&
824 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
825 if (ieee80211_ht_updateparams(ni,
826 scan.htcap, scan.htinfo))
831 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
835 * Same here - the channel width change should
836 * be applied to the specific peer node, not
837 * to the ic. Ie, the interface configuration
838 * should stay in its current channel width;
839 * but it should change the rate control and
840 * any queued frames for the given node only.
842 * Since there's no (current) way to inform
843 * the driver that a channel width change has
844 * occurred for a single node, just stub this
849 ieee80211_update_chw(ic);
855 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
856 if (vap->iv_state != IEEE80211_S_RUN) {
857 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
858 wh, NULL, "wrong state %s",
859 ieee80211_state_name[vap->iv_state]);
860 vap->iv_stats.is_rx_mgtdiscard++;
863 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
864 /* frame must be directed */
865 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
866 wh, NULL, "%s", "not unicast");
867 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
874 * [tlv] supported rates
875 * [tlv] extended supported rates
877 ssid = rates = xrates = NULL;
878 while (efrm - frm > 1) {
879 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
881 case IEEE80211_ELEMID_SSID:
884 case IEEE80211_ELEMID_RATES:
887 case IEEE80211_ELEMID_XRATES:
893 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
895 IEEE80211_VERIFY_ELEMENT(xrates,
896 IEEE80211_RATE_MAXSIZE - rates[1], return);
897 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
898 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
899 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
900 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
902 "%s", "no ssid with ssid suppression enabled");
903 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
907 /* XXX find a better class or define it's own */
908 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
909 "%s", "recv probe req");
911 * Some legacy 11b clients cannot hack a complete
912 * probe response frame. When the request includes
913 * only a bare-bones rate set, communicate this to
916 ieee80211_send_proberesp(vap, wh->i_addr2,
917 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
920 * Note: we don't benefit from stashing the probe request
921 * IEs away to use for IBSS negotiation, because we
922 * typically don't get all of the IEs.
926 case IEEE80211_FC0_SUBTYPE_ACTION:
927 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
928 if ((ni == vap->iv_bss) &&
929 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
930 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
931 wh, NULL, "%s", "unknown node");
932 vap->iv_stats.is_rx_mgtdiscard++;
933 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
934 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
935 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
936 wh, NULL, "%s", "not for us");
937 vap->iv_stats.is_rx_mgtdiscard++;
938 } else if (vap->iv_state != IEEE80211_S_RUN) {
939 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
940 wh, NULL, "wrong state %s",
941 ieee80211_state_name[vap->iv_state]);
942 vap->iv_stats.is_rx_mgtdiscard++;
944 if (ieee80211_parse_action(ni, m0) == 0)
945 (void)ic->ic_recv_action(ni, wh, frm, efrm);
949 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
950 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
951 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
952 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
953 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
954 case IEEE80211_FC0_SUBTYPE_ATIM:
955 case IEEE80211_FC0_SUBTYPE_DISASSOC:
956 case IEEE80211_FC0_SUBTYPE_AUTH:
957 case IEEE80211_FC0_SUBTYPE_DEAUTH:
958 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
959 wh, NULL, "%s", "not handled");
960 vap->iv_stats.is_rx_mgtdiscard++;
964 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
965 wh, "mgt", "subtype 0x%x not handled", subtype);
966 vap->iv_stats.is_rx_badsubtype++;
970 #undef IEEE80211_VERIFY_LENGTH
971 #undef IEEE80211_VERIFY_ELEMENT
974 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
975 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
977 struct ieee80211vap *vap = ni->ni_vap;
978 struct ieee80211com *ic = ni->ni_ic;
979 struct ieee80211_frame *wh;
982 * Process management frames when scanning; useful for doing
985 if (ic->ic_flags & IEEE80211_F_SCAN)
986 adhoc_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
988 wh = mtod(m0, struct ieee80211_frame *);
990 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
991 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
992 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
993 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
994 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
995 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
996 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
997 case IEEE80211_FC0_SUBTYPE_BEACON:
998 case IEEE80211_FC0_SUBTYPE_ATIM:
999 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1000 case IEEE80211_FC0_SUBTYPE_AUTH:
1001 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1002 case IEEE80211_FC0_SUBTYPE_ACTION:
1003 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1004 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1005 wh, NULL, "%s", "not handled");
1006 vap->iv_stats.is_rx_mgtdiscard++;
1009 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1010 wh, "mgt", "subtype 0x%x not handled", subtype);
1011 vap->iv_stats.is_rx_badsubtype++;
1018 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1022 case IEEE80211_FC0_SUBTYPE_BAR:
1023 ieee80211_recv_bar(ni, m);