📄 an.c
字号:
areq.an_len += ((sizeof(struct an_sigcache) * sc->an_sigitems) / 2) + 1; } else#endif if (an_read_record(sc, (struct an_ltv_gen *)&areq)) { error = EINVAL; break; } error = copyout(&areq, ifr->ifr_data, sizeof(areq)); break; case SIOCSAIRONET: error = suser(p->p_ucred, &p->p_acflag); if (error) break; error = copyin(ifr->ifr_data, &areq, sizeof(areq)); if (error) break; an_setdef(sc, &areq); break; default: error = EINVAL; break; } splx(s); return(error);}intan_init_tx_ring(sc) struct an_softc *sc;{ int i; int id; if (sc->an_gone) return (0); for (i = 0; i < AN_TX_RING_CNT; i++) { if (an_alloc_nicmem(sc, 1518 + 0x44, &id)) return(ENOMEM); sc->an_rdata.an_tx_fids[i] = id; sc->an_rdata.an_tx_ring[i] = 0; } sc->an_rdata.an_tx_prod = 0; sc->an_rdata.an_tx_cons = 0; return(0);}voidan_init(sc) struct an_softc *sc;{ struct ifnet *ifp = &sc->arpcom.ac_if; struct an_ltv_ssidlist ssid; struct an_ltv_aplist aplist; struct an_ltv_genconfig genconf; int s; if (sc->an_gone) return; s = splimp(); if (ifp->if_flags & IFF_RUNNING) an_stop(sc); sc->an_associated = 0; /* Allocate the TX buffers */ if (an_init_tx_ring(sc)) { printf("%s: tx buffer allocation failed\n", sc->sc_dev.dv_xname); splx(s); return; } /* Set our MAC address. */ bcopy((char *)&sc->arpcom.ac_enaddr, (char *)&sc->an_config.an_macaddr, ETHER_ADDR_LEN); if (ifp->if_flags & IFF_BROADCAST) sc->an_config.an_rxmode = AN_RXMODE_BC_ADDR; else sc->an_config.an_rxmode = AN_RXMODE_ADDR; if (ifp->if_flags & IFF_MULTICAST) sc->an_config.an_rxmode = AN_RXMODE_BC_MC_ADDR; /* Initialize promisc mode. */ if (ifp->if_flags & IFF_PROMISC) sc->an_config.an_rxmode |= AN_RXMODE_LAN_MONITOR_CURBSS; sc->an_rxmode = sc->an_config.an_rxmode; /* Set the ssid list */ ssid = sc->an_ssidlist; ssid.an_type = AN_RID_SSIDLIST; ssid.an_len = sizeof(struct an_ltv_ssidlist); if (an_write_record(sc, (struct an_ltv_gen *)&ssid)) { printf("%s: failed to set ssid list\n", sc->sc_dev.dv_xname); splx(s); return; } /* Set the AP list */ aplist = sc->an_aplist; aplist.an_type = AN_RID_APLIST; aplist.an_len = sizeof(struct an_ltv_aplist); if (an_write_record(sc, (struct an_ltv_gen *)&aplist)) { printf("%s: failed to set AP list\n", sc->sc_dev.dv_xname); splx(s); return; } /* Set the configuration in the NIC */ genconf = sc->an_config; genconf.an_len = sizeof(struct an_ltv_genconfig); genconf.an_type = AN_RID_GENCONFIG; if (an_write_record(sc, (struct an_ltv_gen *)&genconf)) { printf("%s: failed to set configuration\n", sc->sc_dev.dv_xname); splx(s); return; } /* Enable the MAC */ if (an_cmd(sc, AN_CMD_ENABLE, 0)) { printf("%s: failed to enable MAC\n", sc->sc_dev.dv_xname); splx(s); return; } /* enable interrupts */ CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS); splx(s); ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; TIMEOUT(sc->an_stat_ch, an_stats_update, sc, hz);}voidan_start(ifp) struct ifnet *ifp;{ struct an_softc *sc; struct mbuf *m0 = NULL; struct an_txframe_802_3 tx_frame_802_3; struct ether_header *eh; u_int16_t len; int id; int idx; unsigned char txcontrol; int pkts = 0; sc = ifp->if_softc; if (sc->an_gone) return; if (ifp->if_flags & IFF_OACTIVE) return; if (!sc->an_associated) return; idx = sc->an_rdata.an_tx_prod; bzero((char *)&tx_frame_802_3, sizeof(tx_frame_802_3)); while(sc->an_rdata.an_tx_ring[idx] == 0) { IFQ_DEQUEUE(&ifp->if_snd, m0); if (m0 == NULL) break; pkts++; id = sc->an_rdata.an_tx_fids[idx]; eh = mtod(m0, struct ether_header *); bcopy((char *)&eh->ether_dhost, (char *)&tx_frame_802_3.an_tx_dst_addr, ETHER_ADDR_LEN); bcopy((char *)&eh->ether_shost, (char *)&tx_frame_802_3.an_tx_src_addr, ETHER_ADDR_LEN); len = m0->m_pkthdr.len - 12; /* minus src/dest mac & type */ tx_frame_802_3.an_tx_802_3_payload_len = htole16(len); m_copydata(m0, sizeof(struct ether_header) - 2, len, (caddr_t)&sc->an_txbuf); txcontrol=AN_TXCTL_8023; /* write the txcontrol only */ an_write_data(sc, id, 0x08, (caddr_t)&txcontrol, sizeof(txcontrol)); /* 802_3 header */ an_write_data(sc, id, 0x34, (caddr_t)&tx_frame_802_3, sizeof(struct an_txframe_802_3)); /* in mbuf header type is just before payload */ an_write_data(sc, id, 0x44, (caddr_t)&sc->an_txbuf, len); /* * If there's a BPF listner, bounce a copy of * this frame to him. */#if NBPFILTER > 0 if (ifp->if_bpf) BPF_MTAP(ifp, m0);#endif m_freem(m0); m0 = NULL; sc->an_rdata.an_tx_ring[idx] = id; if (an_cmd(sc, AN_CMD_TX, id)) printf("%s: xmit failed\n", sc->sc_dev.dv_xname); AN_INC(idx, AN_TX_RING_CNT); } if (pkts == 0) return; if (m0 != NULL) ifp->if_flags |= IFF_OACTIVE; sc->an_rdata.an_tx_prod = idx; /* * Set a timeout in case the chip goes out to lunch. */ ifp->if_timer = 5;}voidan_stop(sc) struct an_softc *sc;{ struct ifnet *ifp; int i; if (sc->an_gone) return; ifp = &sc->arpcom.ac_if; an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0); CSR_WRITE_2(sc, AN_INT_EN, 0); an_cmd(sc, AN_CMD_DISABLE, 0); for (i = 0; i < AN_TX_RING_CNT; i++) an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->an_rdata.an_tx_fids[i]); UNTIMEOUT(an_stats_update, sc, sc->an_stat_ch); ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);}voidan_watchdog(ifp) struct ifnet *ifp;{ struct an_softc *sc; sc = ifp->if_softc; if (sc->an_gone) return; printf("%s: device timeout\n", sc->sc_dev.dv_xname); an_reset(sc); an_init(sc); ifp->if_oerrors++;}voidan_shutdown(self) void *self;{ an_stop(self);}#ifdef ANCACHE/* Aironet signal strength cache code. * store signal/noise/quality on per MAC src basis in * a small fixed cache. The cache wraps if > MAX slots * used. The cache may be zeroed out to start over. * Two simple filters exist to reduce computation: * 1. ip only (literally 0x800) which may be used * to ignore some packets. It defaults to ip only. * it could be used to focus on broadcast, non-IP 802.11 beacons. * 2. multicast/broadcast only. This may be used to * ignore unicast packets and only cache signal strength * for multicast/broadcast packets (beacons); e.g., Mobile-IP * beacons and not unicast traffic. * * The cache stores (MAC src(index), IP src (major clue), signal, * quality, noise) * * No apologies for storing IP src here. It's easy and saves much * trouble elsewhere. The cache is assumed to be INET dependent, * although it need not be. * * Note: the Aironet only has a single byte of signal strength value * in the rx frame header, and it's not scaled to anything sensible. * This is kind of lame, but it's all we've got. */#ifdef documentationint an_sigitems; /* number of cached entries */struct an_sigcache an_sigcache[MAXANCACHE]; /* array of cache entries */int an_nextitem; /* index/# of entries */#endif/* control variables for cache filtering. Basic idea is * to reduce cost (e.g., to only Mobile-IP agent beacons * which are broadcast or multicast). Still you might * want to measure signal strength anth unicast ping packets * on a pt. to pt. ant. setup. *//* set true if you want to limit cache items to broadcast/mcast * only packets (not unicast). Useful for mobile-ip beacons which * are broadcast/multicast at network layer. Default is all packets * so ping/unicast anll work say anth pt. to pt. antennae setup. */#if 0static int an_cache_mcastonly = 0;SYSCTL_INT(_machdep, OID_AUTO, an_cache_mcastonly, CTLFLAG_RW, &an_cache_mcastonly, 0, "");/* set true if you want to limit cache items to IP packets only*/static int an_cache_iponly = 1;SYSCTL_INT(_machdep, OID_AUTO, an_cache_iponly, CTLFLAG_RW, &an_cache_iponly, 0, "");#endif/* * an_cache_store, per rx packet store signal * strength in MAC (src) indexed cache. */voidan_cache_store (sc, eh, m, rx_quality) struct an_softc *sc; struct ether_header *eh; struct mbuf *m; unsigned short rx_quality;{ struct ip *ip = 0; int i; static int cache_slot = 0; /* use this cache entry */ static int wrapindex = 0; /* next "free" cache entry */ int saanp=0; /* filters: * 1. ip only * 2. configurable filter to throw out unicast packets, * keep multicast only. */ if ((ntohs(eh->ether_type) == 0x800)) saanp = 1; /* filter for ip packets only */ if (sc->an_cache_iponly && !saanp) return; /* filter for broadcast/multicast only */ if (sc->an_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) return;#ifdef SIGDEBUG printf("an: q value %x (MSB=0x%x, LSB=0x%x) \n", rx_quality & 0xffff, rx_quality >> 8, rx_quality & 0xff);#endif /* find the ip header. we want to store the ip_src address */ if (saanp) ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header)); /* do a linear search for a matching MAC address * in the cache table * . MAC address is 6 bytes, * . var w_nextitem holds total number of entries already cached */ for(i = 0; i < sc->an_nextitem; i++) if (!bcmp(eh->ether_shost , sc->an_sigcache[i].macsrc, 6)) /* Match!, * so we already have this entry, update the data */ break; /* did we find a matching mac address? * if yes, then overwrite a previously existing cache entry */ if (i < sc->an_nextitem ) cache_slot = i; /* else, have a new address entry,so * add this new entry, * if table full, then we need to replace LRU entry */ else { /* check for space in cache table * note: an_nextitem also holds number of entries * added in the cache table */ if ( sc->an_nextitem < MAXANCACHE ) { cache_slot = sc->an_nextitem; sc->an_nextitem++; sc->an_sigitems = sc->an_nextitem; } /* no space found, so simply wrap anth wrap index * and "zap" the next entry */ else { if (wrapindex == MAXANCACHE) wrapindex = 0; cache_slot = wrapindex++; } } /* invariant: cache_slot now points at some slot * in cache. */ if (cache_slot < 0 || cache_slot >= MAXANCACHE) { log(LOG_ERR, "an_cache_store, bad index: %d of " "[0..%d], gross cache error\n", cache_slot, MAXANCACHE); return; } /* store items in cache * .ip source address * .mac src * .signal, etc. */ if (saanp) sc->an_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr; bcopy( eh->ether_shost, sc->an_sigcache[cache_slot].macsrc, 6); sc->an_sigcache[cache_slot].signal = rx_quality;}#endifintan_media_change(ifp) struct ifnet *ifp;{ struct an_softc *sc = ifp->if_softc; int otype = sc->an_config.an_opmode; int orate = sc->an_tx_rate; if ((sc->an_ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0) sc->an_config.an_opmode = AN_OPMODE_IBSS_ADHOC; else sc->an_config.an_opmode = AN_OPMODE_INFRASTRUCTURE_STATION; switch (IFM_SUBTYPE(sc->an_ifmedia.ifm_cur->ifm_media)) { case IFM_IEEE80211_DS1: sc->an_tx_rate = AN_RATE_1MBPS; break; case IFM_IEEE80211_DS2: sc->an_tx_rate = AN_RATE_2MBPS; break; case IFM_IEEE80211_DS5: sc->an_tx_rate = AN_RATE_5_5MBPS; break; case IFM_IEEE80211_DS11: sc->an_tx_rate = AN_RATE_11MBPS; break; case IFM_AUTO: sc->an_tx_rate = 0; break; } if (otype != sc->an_config.an_opmode || orate != sc->an_tx_rate) an_init(sc); return(0);}voidan_media_status(ifp, imr) struct ifnet *ifp; struct ifmediareq *imr;{ struct an_ltv_status status; struct an_softc *sc = ifp->if_softc; status.an_len = sizeof(status); status.an_type = AN_RID_STATUS; if (an_read_record(sc, (struct an_ltv_gen *)&status)) { /* If the status read fails, just lie. */ imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media; imr->ifm_status = IFM_AVALID|IFM_ACTIVE; } if (sc->an_tx_rate == 0) { imr->ifm_active = IFM_IEEE80211|IFM_AUTO; if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC) imr->ifm_active |= IFM_IEEE80211_ADHOC; switch (status.an_current_tx_rate) { case AN_RATE_1MBPS: imr->ifm_active |= IFM_IEEE80211_DS1; break; case AN_RATE_2MBPS: imr->ifm_active |= IFM_IEEE80211_DS2; break; case AN_RATE_5_5MBPS: imr->ifm_active |= IFM_IEEE80211_DS5; break; case AN_RATE_11MBPS: imr->ifm_active |= IFM_IEEE80211_DS11; break; } } else { imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media; } imr->ifm_status = IFM_AVALID; if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC) imr->ifm_status |= IFM_ACTIVE; else if (status.an_opmode & AN_STATUS_OPMODE_ASSOCIATED) imr->ifm_status |= IFM_ACTIVE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -