/* * Handle bookkeeping for station deauthentication/disassociation * when operating as an ap. */ void ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) { if (ic->ic_opmode != IEEE80211_M_HOSTAP) panic("not in ap mode, mode %u", ic->ic_opmode); /* * If node wasn't previously associated all we need to do is * reclaim the reference. */ if (ni->ni_associd == 0) { ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); return; } if (ni->ni_pwrsave == IEEE80211_PS_DOZE) { ic->ic_pssta--; ni->ni_pwrsave = IEEE80211_PS_AWAKE; } if (!IF_IS_EMPTY(&ni->ni_savedq)) { IF_PURGE(&ni->ni_savedq); if (ic->ic_set_tim != NULL) (*ic->ic_set_tim)(ic, ni->ni_associd, 0); } if (ic->ic_flags & IEEE80211_F_RSNON) ieee80211_node_leave_rsn(ic, ni); if (ic->ic_curmode == IEEE80211_MODE_11G) ieee80211_node_leave_11g(ic, ni); #ifndef IEEE80211_NO_HT if (ni->ni_flags & IEEE80211_NODE_HT) ieee80211_node_leave_ht(ic, ni); #endif if (ic->ic_node_leave != NULL) (*ic->ic_node_leave)(ic, ni); IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); ni->ni_associd = 0; ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); #if NBRIDGE > 0 /* * If the parent interface is a bridgeport, delete * any dynamically learned address for this node. */ if (ic->ic_if.if_bridgeport != NULL) bridge_update(&ic->ic_if, (struct ether_addr *)ni->ni_macaddr, 1); #endif }
void ieee80211_setup_node(struct ieee80211com *ic, struct ieee80211_node *ni, const u_int8_t *macaddr) { int s; DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr))); IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); ni->ni_ic = ic; /* back-pointer */ #ifndef IEEE80211_STA_ONLY IFQ_SET_MAXLEN(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE); timeout_set(&ni->ni_eapol_to, ieee80211_eapol_timeout, ni); timeout_set(&ni->ni_sa_query_to, ieee80211_sa_query_timeout, ni); #endif /* * Note we don't enable the inactive timer when acting * as a station. Nodes created in this mode represent * AP's identified while scanning. If we time them out * then several things happen: we can't return the data * to users to show the list of AP's we encountered, and * more importantly, we'll incorrectly deauthenticate * ourself because the inactivity timer will kick us off. */ s = splnet(); if (ic->ic_opmode != IEEE80211_M_STA && RB_EMPTY(&ic->ic_tree)) ic->ic_inact_timer = IEEE80211_INACT_WAIT; RB_INSERT(ieee80211_tree, &ic->ic_tree, ni); splx(s); }
/* * Handle bookkeeping for station deauthentication/disassociation * when operating as an ap. */ void ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) { if (ic->ic_opmode != IEEE80211_M_HOSTAP) panic("not in ap mode, mode %u", ic->ic_opmode); /* * If node wasn't previously associated all * we need to do is reclaim the reference. */ if (ni->ni_associd == 0) return; IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); ni->ni_associd = 0; if (ic->ic_curmode == IEEE80211_MODE_11G) ieee80211_node_leave_11g(ic, ni); ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); #if NBRIDGE > 0 /* * If the parent interface belongs to a bridge, delete * any dynamically learned address for this node. */ if (ic->ic_if.if_bridge != NULL) bridge_update(&ic->ic_if, (struct ether_addr *)ni->ni_macaddr, 1); #endif }
void ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp) { int newassoc; if (ni->ni_associd == 0) { u_int16_t aid; /* * It would be clever to search the bitmap * more efficiently, but this will do for now. */ for (aid = 1; aid < ic->ic_max_aid; aid++) { if (!IEEE80211_AID_ISSET(aid, ic->ic_aid_bitmap)) break; } if (aid >= ic->ic_max_aid) { IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_REASON_ASSOC_TOOMANY); ieee80211_node_leave(ic, ni); return; } ni->ni_associd = aid | 0xc000; IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); newassoc = 1; if (ic->ic_curmode == IEEE80211_MODE_11G) ieee80211_node_join_11g(ic, ni); } else newassoc = 0; IEEE80211_DPRINTF(("station %s %s associated at aid %d\n", ether_sprintf(ni->ni_macaddr), (newassoc ? "newly" : "already"), ni->ni_associd & ~0xc000)); /* give driver a chance to setup state like ni_txrate */ if (ic->ic_newassoc) (*ic->ic_newassoc)(ic, ni, newassoc); IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); ieee80211_node_newstate(ni, IEEE80211_STA_ASSOC); #if NBRIDGE > 0 /* * If the parent interface belongs to a bridge, learn * the node's address dynamically on this interface. */ if (ic->ic_if.if_bridge != NULL) bridge_update(&ic->ic_if, (struct ether_addr *)ni->ni_macaddr, 0); #endif }
void ieee80211_setup_node(struct ieee80211com *ic, struct ieee80211_node *ni, const u_int8_t *macaddr) { int s; DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr))); IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); ni->ni_ic = ic; /* back-pointer */ #ifndef IEEE80211_STA_ONLY IFQ_SET_MAXLEN(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE); timeout_set(&ni->ni_eapol_to, ieee80211_eapol_timeout, ni); timeout_set(&ni->ni_sa_query_to, ieee80211_sa_query_timeout, ni); #endif s = splnet(); RB_INSERT(ieee80211_tree, &ic->ic_tree, ni); ic->ic_nnodes++; splx(s); }
static void ieee80211_setup_node(struct ieee80211com *ic, struct ieee80211_node *ni, u_int8_t *macaddr) { IEEE80211_DPRINTF(("%s %s\n", __func__, ether_sprintf(macaddr))); IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); IEEE80211_NODE_LOCK_BH(ic); /* * Note we don't enable the inactive timer when acting * as a station. Nodes created in this mode represent * AP's identified while scanning. If we time them out * then several things happen: we can't return the data * to users to show the list of AP's we encountered, and * more importantly, we'll incorrectly deauthenticate * ourself because the inactivity timer will kick us off. */ if (ic->ic_opmode != IEEE80211_M_STA && RB_EMPTY(&ic->ic_tree)) ic->ic_inact_timer = IEEE80211_INACT_WAIT; RB_INSERT(ieee80211_tree, &ic->ic_tree, ni); IEEE80211_NODE_UNLOCK_BH(ic); }
/* * Complete a scan of potential channels. */ void ieee80211_end_scan(struct ifnet *ifp) { struct ieee80211com *ic = (void *)ifp; struct ieee80211_node *ni, *nextbs, *selbs; if (ifp->if_flags & IFF_DEBUG) printf("%s: end %s scan\n", ifp->if_xname, (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); if (ic->ic_scan_count) ic->ic_flags &= ~IEEE80211_F_ASCAN; ni = RB_MIN(ieee80211_tree, &ic->ic_tree); #ifndef IEEE80211_STA_ONLY if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* XXX off stack? */ u_char occupied[howmany(IEEE80211_CHAN_MAX, NBBY)]; int i, fail; /* * The passive scan to look for existing AP's completed, * select a channel to camp on. Identify the channels * that already have one or more AP's and try to locate * an unnoccupied one. If that fails, pick a random * channel from the active set. */ memset(occupied, 0, sizeof(occupied)); RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); for (i = 0; i < IEEE80211_CHAN_MAX; i++) if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) break; if (i == IEEE80211_CHAN_MAX) { fail = arc4random() & 3; /* random 0-3 */ for (i = 0; i < IEEE80211_CHAN_MAX; i++) if (isset(ic->ic_chan_active, i) && fail-- == 0) break; } ieee80211_create_ibss(ic, &ic->ic_channels[i]); goto wakeup; } #endif if (ni == NULL) { DPRINTF(("no scan candidate\n")); notfound: #ifndef IEEE80211_STA_ONLY if (ic->ic_opmode == IEEE80211_M_IBSS && (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen != 0) { ieee80211_create_ibss(ic, ic->ic_ibss_chan); goto wakeup; } #endif /* * Scan the next mode if nothing has been found. This * is necessary if the device supports different * incompatible modes in the same channel range, like * like 11b and "pure" 11G mode. This will loop * forever except for user-initiated scans. */ if (ieee80211_next_mode(ifp) == IEEE80211_MODE_AUTO) { if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST && ic->ic_scan_lock & IEEE80211_SCAN_RESUME) { ic->ic_scan_lock = IEEE80211_SCAN_LOCKED; /* Return from an user-initiated scan */ wakeup(&ic->ic_scan_lock); } else if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) goto wakeup; ic->ic_scan_count++; } /* * Reset the list of channels to scan and start again. */ ieee80211_next_scan(ifp); return; } selbs = NULL; for (; ni != NULL; ni = nextbs) { nextbs = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni); if (ni->ni_fails) { /* * The configuration of the access points may change * during my scan. So delete the entry for the AP * and retry to associate if there is another beacon. */ if (ni->ni_fails++ > 2) ieee80211_free_node(ic, ni); continue; } if (ieee80211_match_bss(ic, ni) == 0) { if (selbs == NULL) selbs = ni; else if (ni->ni_rssi > selbs->ni_rssi) selbs = ni; } } if (selbs == NULL) goto notfound; (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); ni = ic->ic_bss; /* * Set the erp state (mostly the slot time) to deal with * the auto-select case; this should be redundant if the * mode is locked. */ ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan); ieee80211_reset_erp(ic); if (ic->ic_flags & IEEE80211_F_RSNON) ieee80211_choose_rsnparams(ic); else if (ic->ic_flags & IEEE80211_F_WEPON) ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; ieee80211_node_newstate(selbs, IEEE80211_STA_BSS); #ifndef IEEE80211_STA_ONLY if (ic->ic_opmode == IEEE80211_M_IBSS) { ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO | IEEE80211_F_DODEL); if (ni->ni_rates.rs_nrates == 0) goto notfound; ieee80211_new_state(ic, IEEE80211_S_RUN, -1); } else #endif ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); wakeup: if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) { /* Return from an user-initiated scan */ wakeup(&ic->ic_scan_lock); } ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; }
static int ieee80211_do_slow_print(struct ieee80211com *ic, int *did_print) { static const struct timeval merge_print_intvl = { .tv_sec = 1, .tv_usec = 0 }; if ((ic->ic_if.if_flags & IFF_LINK0) == 0) return 0; if (!*did_print && (ic->ic_if.if_flags & IFF_DEBUG) == 0 && !ratecheck(&ic->ic_last_merge_print, &merge_print_intvl)) return 0; *did_print = 1; return 1; } /* ieee80211_ibss_merge helps merge 802.11 ad hoc networks. The * convention, set by the Wireless Ethernet Compatibility Alliance * (WECA), is that an 802.11 station will change its BSSID to match * the "oldest" 802.11 ad hoc network, on the same channel, that * has the station's desired SSID. The "oldest" 802.11 network * sends beacons with the greatest TSF timestamp. * * Return ENETRESET if the BSSID changed, 0 otherwise. * * XXX Perhaps we should compensate for the time that elapses * between the MAC receiving the beacon and the host processing it * in ieee80211_ibss_merge. */ int ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni, u_int64_t local_tsft) { u_int64_t beacon_tsft; int did_print = 0, sign; union { u_int64_t word; u_int8_t tstamp[8]; } u; /* ensure alignment */ (void)memcpy(&u, &ni->ni_tstamp[0], sizeof(u)); beacon_tsft = letoh64(u.word); /* we are faster, let the other guy catch up */ if (beacon_tsft < local_tsft) sign = -1; else sign = 1; if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { if (!ieee80211_do_slow_print(ic, &did_print)) return 0; printf("%s: tsft offset %s%llu\n", ic->ic_if.if_xname, (sign < 0) ? "-" : "", (sign < 0) ? (local_tsft - beacon_tsft) : (beacon_tsft - local_tsft)); return 0; } if (sign < 0) return 0; if (ieee80211_match_bss(ic, ni) != 0) return 0; if (ieee80211_do_slow_print(ic, &did_print)) { printf("%s: ieee80211_ibss_merge: bssid mismatch %s\n", ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); printf("%s: my tsft %llu beacon tsft %llu\n", ic->ic_if.if_xname, local_tsft, beacon_tsft); printf("%s: sync TSF with %s\n", ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr)); } ic->ic_flags &= ~IEEE80211_F_SIBSS; /* negotiate rates with new IBSS */ ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO | IEEE80211_F_DODEL); if (ni->ni_rates.rs_nrates == 0) { if (ieee80211_do_slow_print(ic, &did_print)) { printf("%s: rates mismatch, BSSID %s\n", ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); } return 0; } if (ieee80211_do_slow_print(ic, &did_print)) { printf("%s: sync BSSID %s -> ", ic->ic_if.if_xname, ether_sprintf(ic->ic_bss->ni_bssid)); printf("%s ", ether_sprintf(ni->ni_bssid)); printf("(from %s)\n", ether_sprintf(ni->ni_macaddr)); } ieee80211_node_newstate(ni, IEEE80211_STA_BSS); (*ic->ic_node_copy)(ic, ic->ic_bss, ni); return ENETRESET; }
/* * Complete a scan of potential channels. */ void Voodoo80211Device:: ieee80211_end_scan(struct ieee80211com *ic) { struct ieee80211_node *ni, *nextbs, *selbs; /* TODO if (ifp->if_flags & IFF_DEBUG) printf("%s: end %s scan\n", ifp->if_xname, (ic->ic_flags & IEEE80211_F_ASCAN) ? "active" : "passive"); */ if (ic->ic_scan_count) ic->ic_flags &= ~IEEE80211_F_ASCAN; ni = RB_MIN(ieee80211_tree, &ic->ic_tree); if (ni == NULL) { DPRINTF(("no scan candidate\n")); notfound: /* * Scan the next mode if nothing has been found. This * is necessary if the device supports different * incompatible modes in the same channel range, like * like 11b and "pure" 11G mode. This will loop * forever except for user-initiated scans. */ if (ieee80211_next_mode(ic) == IEEE80211_MODE_AUTO) { if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST && ic->ic_scan_lock & IEEE80211_SCAN_RESUME) { ic->ic_scan_lock = IEEE80211_SCAN_LOCKED; /* Return from an user-initiated scan */ wakeupOn(&ic->ic_scan_lock); // XXX: pvaibhav: do this here? fInterface->postMessage(APPLE80211_M_SCAN_DONE); } else if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) goto wakeup; ic->ic_scan_count++; } /* * Reset the list of channels to scan and start again. */ ieee80211_next_scan(ic); return; } selbs = NULL; for (; ni != NULL; ni = nextbs) { nextbs = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni); if (ni->ni_fails) { /* * The configuration of the access points may change * during my scan. So delete the entry for the AP * and retry to associate if there is another beacon. */ if (ni->ni_fails++ > 2) ieee80211_free_node(ic, ni); continue; } if (ieee80211_match_bss(ic, ni) == 0) { if (selbs == NULL) selbs = ni; else if (ni->ni_rssi > selbs->ni_rssi) selbs = ni; } } if (selbs == NULL) goto notfound; ieee80211_node_copy(ic, ic->ic_bss, selbs); ni = ic->ic_bss; /* * Set the erp state (mostly the slot time) to deal with * the auto-select case; this should be redundant if the * mode is locked. */ ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan); ieee80211_reset_erp(ic); if (ic->ic_flags & IEEE80211_F_RSNON) ieee80211_choose_rsnparams(ic); else if (ic->ic_flags & IEEE80211_F_WEPON) ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; ieee80211_node_newstate(selbs, IEEE80211_STA_BSS); ieee80211_newstate(ic, IEEE80211_S_AUTH, -1); wakeup: if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) { /* Return from an user-initiated scan */ wakeupOn(&ic->ic_scan_lock); // XXX: pvaibhav: do this here? fInterface->postMessage(APPLE80211_M_SCAN_DONE); } ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; }