2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
7 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
18 * redistribution must be conditioned upon including a substantially
19 * similar Disclaimer requirement for further binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
27 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGES.
36 #include "if_wtapvar.h"
37 #include <sys/uio.h> /* uio struct */
39 #include <net/if_var.h>
42 #include <net80211/ieee80211_ratectl.h>
43 #include "if_medium.h"
46 * This _requires_ vimage to be useful.
49 #error if_wtap requires VIMAGE.
52 /* device for IOCTL and read/write for debuggin purposes */
53 /* Function prototypes */
54 static d_open_t wtap_node_open;
55 static d_close_t wtap_node_close;
56 static d_write_t wtap_node_write;
57 static d_ioctl_t wtap_node_ioctl;
59 static struct cdevsw wtap_cdevsw = {
60 .d_version = D_VERSION,
62 .d_open = wtap_node_open,
63 .d_close = wtap_node_close,
64 .d_write = wtap_node_write,
65 .d_ioctl = wtap_node_ioctl,
70 wtap_node_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
74 uprintf("Opened device \"echo\" successfully.\n");
79 wtap_node_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
82 uprintf("Closing device \"echo.\"\n");
87 wtap_node_write(struct cdev *dev, struct uio *uio, int ioflag)
92 struct wtap_softc *sc;
94 struct epoch_tracker et;
97 uprintf("write device %s \"echo.\"\n", devtoname(dev));
98 buf_len = MIN(uio->uio_iov->iov_len, 1024);
99 err = copyin(uio->uio_iov->iov_base, buf, buf_len);
102 uprintf("Write failed: bad address!\n");
106 MGETHDR(m, M_NOWAIT, MT_DATA);
107 m_copyback(m, 0, buf_len, buf);
109 CURVNET_SET(TD_TO_VNET(curthread));
112 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
113 printf("ifp->if_xname = %s\n", ifp->if_xname);
114 if(strcmp(devtoname(dev), ifp->if_xname) == 0){
115 printf("found match, correspoding wtap = %s\n",
117 sc = (struct wtap_softc *)ifp->if_softc;
118 printf("wtap id = %d\n", sc->id);
130 wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
131 int fflag, struct thread *td)
137 DWTAP_PRINTF("Unknown WTAP IOCTL\n");
143 static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
144 const struct ieee80211_bpf_params *params);
147 wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
150 return medium_transmit(avp->av_md, avp->id, m);
154 wtap_media_change(struct ifnet *ifp)
157 DWTAP_PRINTF("%s\n", __func__);
158 int error = ieee80211_media_change(ifp);
159 /* NB: only the fixed rate can change and that doesn't need a reset */
160 return (error == ENETRESET ? 0 : error);
164 * Intercept management frames to collect beacon rssi data
165 * and to do ibss merges.
168 wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
169 int subtype, const struct ieee80211_rx_stats *stats, int rssi, int nf)
171 struct ieee80211vap *vap = ni->ni_vap;
173 DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
175 WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, stats, rssi, nf);
179 wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
182 DWTAP_PRINTF("%s\n", __func__);
187 wtap_beacon_update(struct ieee80211vap *vap, int item)
189 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
191 DWTAP_PRINTF("%s\n", __func__);
192 setbit(bo->bo_flags, item);
196 * Allocate and setup an initial beacon frame.
199 wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
201 struct ieee80211vap *vap = ni->ni_vap;
202 struct wtap_vap *avp = WTAP_VAP(vap);
204 DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
207 * NB: the beacon data buffer must be 32-bit aligned;
208 * we assume the mbuf routines will return us something
209 * with this alignment (perhaps should assert).
211 avp->beacon = ieee80211_beacon_alloc(ni);
212 if (avp->beacon == NULL) {
213 printf("%s: cannot get mbuf\n", __func__);
216 callout_init(&avp->av_swba, 0);
217 avp->bf_node = ieee80211_ref_node(ni);
223 wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
226 DWTAP_PRINTF("%s\n", __func__);
230 wtap_beacon_intrp(void *arg)
232 struct wtap_vap *avp = arg;
233 struct ieee80211vap *vap = arg;
236 if (vap->iv_state < IEEE80211_S_RUN) {
237 DWTAP_PRINTF("Skip beacon, not running, state %d", vap->iv_state);
240 DWTAP_PRINTF("[%d] beacon intrp\n", avp->id); //burst mode
242 * Update dynamic beacon contents. If this returns
243 * non-zero then we need to remap the memory because
244 * the beacon frame changed size (probably because
245 * of the TIM bitmap).
247 m = m_dup(avp->beacon, M_NOWAIT);
248 if (ieee80211_beacon_update(avp->bf_node, m, 0)) {
249 printf("%s, need to remap the memory because the beacon frame"
250 " changed size.\n",__func__);
253 if (ieee80211_radiotap_active_vap(vap))
254 ieee80211_radiotap_tx(vap, m);
257 medium_transmit(avp->av_md, avp->id, m);
259 wtap_medium_enqueue(avp, m);
260 callout_schedule(&avp->av_swba, avp->av_bcinterval);
264 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
266 struct ieee80211com *ic = vap->iv_ic;
267 struct wtap_softc *sc = ic->ic_softc;
268 struct wtap_vap *avp = WTAP_VAP(vap);
269 struct ieee80211_node *ni = NULL;
272 DWTAP_PRINTF("%s\n", __func__);
274 ni = ieee80211_ref_node(vap->iv_bss);
276 * Invoke the parent method to do net80211 work.
278 error = avp->av_newstate(vap, nstate, arg);
282 if (nstate == IEEE80211_S_RUN) {
283 /* NB: collect bss node again, it may have changed */
284 ieee80211_free_node(ni);
285 ni = ieee80211_ref_node(vap->iv_bss);
286 switch (vap->iv_opmode) {
287 case IEEE80211_M_MBSS:
288 error = wtap_beacon_alloc(sc, ni);
291 wtap_beacon_config(sc, vap);
292 callout_reset(&avp->av_swba, avp->av_bcinterval,
293 wtap_beacon_intrp, vap);
298 } else if (nstate == IEEE80211_S_INIT) {
299 callout_stop(&avp->av_swba);
301 ieee80211_free_node(ni);
304 printf("%s: bad\n", __func__);
305 ieee80211_free_node(ni);
310 wtap_bmiss(struct ieee80211vap *vap)
312 struct wtap_vap *avp = (struct wtap_vap *)vap;
314 DWTAP_PRINTF("%s\n", __func__);
318 static struct ieee80211vap *
319 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
320 int unit, enum ieee80211_opmode opmode, int flags,
321 const uint8_t bssid[IEEE80211_ADDR_LEN],
322 const uint8_t mac[IEEE80211_ADDR_LEN])
324 struct wtap_softc *sc = ic->ic_softc;
325 struct ieee80211vap *vap;
326 struct wtap_vap *avp;
328 struct ieee80211_node *ni;
330 DWTAP_PRINTF("%s\n", __func__);
332 avp = malloc(sizeof(struct wtap_vap), M_80211_VAP, M_WAITOK | M_ZERO);
334 avp->av_md = sc->sc_md;
335 avp->av_bcinterval = msecs_to_ticks(BEACON_INTRERVAL + 100*sc->id);
336 vap = (struct ieee80211vap *) avp;
337 error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
338 flags | IEEE80211_CLONE_NOBEACONS, bssid);
340 free(avp, M_80211_VAP);
344 /* override various methods */
345 avp->av_recv_mgmt = vap->iv_recv_mgmt;
346 vap->iv_recv_mgmt = wtap_recv_mgmt;
347 vap->iv_reset = wtap_reset_vap;
348 vap->iv_update_beacon = wtap_beacon_update;
349 avp->av_newstate = vap->iv_newstate;
350 vap->iv_newstate = wtap_newstate;
351 avp->av_bmiss = vap->iv_bmiss;
352 vap->iv_bmiss = wtap_bmiss;
355 ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status,
357 avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
358 "%s", (const char *)sc->name);
360 /* TODO this is a hack to force it to choose the rate we want */
361 ni = ieee80211_ref_node(vap->iv_bss);
363 ieee80211_free_node(ni);
368 wtap_vap_delete(struct ieee80211vap *vap)
370 struct wtap_vap *avp = WTAP_VAP(vap);
372 DWTAP_PRINTF("%s\n", __func__);
373 destroy_dev(avp->av_dev);
374 callout_stop(&avp->av_swba);
375 ieee80211_vap_detach(vap);
376 free(avp, M_80211_VAP);
380 wtap_parent(struct ieee80211com *ic)
382 struct wtap_softc *sc = ic->ic_softc;
384 if (ic->ic_nrunning > 0) {
386 ieee80211_start_all(ic);
392 wtap_scan_start(struct ieee80211com *ic)
396 DWTAP_PRINTF("%s\n", __func__);
401 wtap_scan_end(struct ieee80211com *ic)
405 DWTAP_PRINTF("%s\n", __func__);
410 wtap_set_channel(struct ieee80211com *ic)
414 DWTAP_PRINTF("%s\n", __func__);
419 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
420 const struct ieee80211_bpf_params *params)
423 DWTAP_PRINTF("%s, %p\n", __func__, m);
425 struct ieee80211vap *vap = ni->ni_vap;
426 struct wtap_vap *avp = WTAP_VAP(vap);
428 if (ieee80211_radiotap_active_vap(vap)) {
429 ieee80211_radiotap_tx(vap, m);
431 if (m->m_flags & M_TXCB)
432 ieee80211_process_callback(ni, m, 0);
433 ieee80211_free_node(ni);
434 return wtap_medium_enqueue(avp, m);
438 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
440 struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
441 M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
442 KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
445 mtx_lock(&sc->sc_mtx);
446 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
447 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
448 mtx_unlock(&sc->sc_mtx);
452 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
454 struct ieee80211com *ic = &sc->sc_ic;
455 struct ieee80211_node *ni;
458 DWTAP_PRINTF("%s\n", __func__);
461 DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
462 if (m == NULL) { /* NB: shouldn't happen */
463 ic_printf(ic, "%s: no mbuf!\n", __func__);
466 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
469 * Locate the node for sender, track state, and then
470 * pass the (referenced) node up to the 802.11 layer
473 ni = ieee80211_find_rxnode_withkey(ic,
474 mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
477 * Sending station is known, dispatch directly.
479 type = ieee80211_input(ni, m, 1<<7, 10);
480 ieee80211_free_node(ni);
482 type = ieee80211_input_all(ic, m, 1<<7, 10);
487 wtap_rx_proc(void *arg, int npending)
489 struct wtap_softc *sc = (struct wtap_softc *)arg;
490 struct ieee80211com *ic = &sc->sc_ic;
492 struct ieee80211_node *ni;
497 DWTAP_PRINTF("%s\n", __func__);
501 mtx_lock(&sc->sc_mtx);
502 bf = STAILQ_FIRST(&sc->sc_rxbuf);
504 mtx_unlock(&sc->sc_mtx);
507 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
508 mtx_unlock(&sc->sc_mtx);
509 KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
511 DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
512 if (m == NULL) { /* NB: shouldn't happen */
513 ic_printf(ic, "%s: no mbuf!\n", __func__);
514 free(bf, M_WTAP_RXBUF);
518 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
522 * Locate the node for sender, track state, and then
523 * pass the (referenced) node up to the 802.11 layer
526 ni = ieee80211_find_rxnode_withkey(ic,
527 mtod(m, const struct ieee80211_frame_min *),
528 IEEE80211_KEYIX_NONE);
531 * Sending station is known, dispatch directly.
533 type = ieee80211_input(ni, m, 1<<7, 10);
534 ieee80211_free_node(ni);
536 type = ieee80211_input_all(ic, m, 1<<7, 10);
539 /* The mbufs are freed by the Net80211 stack */
540 free(bf, M_WTAP_RXBUF);
545 wtap_newassoc(struct ieee80211_node *ni, int isnew)
548 DWTAP_PRINTF("%s\n", __func__);
552 * Callback from the 802.11 layer to update WME parameters.
555 wtap_wme_update(struct ieee80211com *ic)
558 DWTAP_PRINTF("%s\n", __func__);
563 wtap_update_mcast(struct ieee80211com *ic)
566 DWTAP_PRINTF("%s\n", __func__);
570 wtap_update_promisc(struct ieee80211com *ic)
573 DWTAP_PRINTF("%s\n", __func__);
577 wtap_transmit(struct ieee80211com *ic, struct mbuf *m)
579 struct ieee80211_node *ni =
580 (struct ieee80211_node *) m->m_pkthdr.rcvif;
581 struct ieee80211vap *vap = ni->ni_vap;
582 struct wtap_vap *avp = WTAP_VAP(vap);
585 printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
587 if (ieee80211_radiotap_active_vap(vap))
588 ieee80211_radiotap_tx(vap, m);
590 if (m->m_flags & M_TXCB)
591 ieee80211_process_callback(ni, m, 0);
592 ieee80211_free_node(ni);
593 return wtap_medium_enqueue(avp, m);
596 static struct ieee80211_node *
597 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
599 struct ieee80211_node *ni;
601 DWTAP_PRINTF("%s\n", __func__);
603 ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
613 wtap_node_free(struct ieee80211_node *ni)
615 struct ieee80211com *ic = ni->ni_ic;
616 struct wtap_softc *sc = ic->ic_softc;
618 DWTAP_PRINTF("%s\n", __func__);
619 sc->sc_node_free(ni);
623 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
625 struct ieee80211com *ic = &sc->sc_ic;
627 DWTAP_PRINTF("%s\n", __func__);
630 STAILQ_INIT(&sc->sc_rxbuf);
631 sc->sc_tq = taskqueue_create("wtap_taskq", M_NOWAIT | M_ZERO,
632 taskqueue_thread_enqueue, &sc->sc_tq);
633 taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ", sc->name);
634 TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
637 ic->ic_name = sc->name;
638 ic->ic_phytype = IEEE80211_T_DS;
639 ic->ic_opmode = IEEE80211_M_MBSS;
640 ic->ic_caps = IEEE80211_C_MBSS;
642 ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
644 ic->ic_regdomain.regdomain = SKU_ETSI;
645 ic->ic_regdomain.country = CTRY_SWEDEN;
646 ic->ic_regdomain.location = 1; /* Indoors */
647 ic->ic_regdomain.isocc[0] = 'S';
648 ic->ic_regdomain.isocc[1] = 'E';
651 ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
652 ic->ic_channels[0].ic_freq = 2412;
654 IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
655 ieee80211_ifattach(ic);
657 /* override default methods */
658 ic->ic_newassoc = wtap_newassoc;
659 ic->ic_wme.wme_update = wtap_wme_update;
660 ic->ic_vap_create = wtap_vap_create;
661 ic->ic_vap_delete = wtap_vap_delete;
662 ic->ic_raw_xmit = wtap_raw_xmit;
663 ic->ic_update_mcast = wtap_update_mcast;
664 ic->ic_update_promisc = wtap_update_promisc;
665 ic->ic_transmit = wtap_transmit;
666 ic->ic_parent = wtap_parent;
668 sc->sc_node_alloc = ic->ic_node_alloc;
669 ic->ic_node_alloc = wtap_node_alloc;
670 sc->sc_node_free = ic->ic_node_free;
671 ic->ic_node_free = wtap_node_free;
673 ic->ic_scan_start = wtap_scan_start;
674 ic->ic_scan_end = wtap_scan_end;
675 ic->ic_set_channel = wtap_set_channel;
677 ieee80211_radiotap_attach(ic,
678 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
679 WTAP_TX_RADIOTAP_PRESENT,
680 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
681 WTAP_RX_RADIOTAP_PRESENT);
683 /* Work here, we must find a way to populate the rate table */
685 if(ic->ic_rt == NULL){
686 printf("no table for ic_curchan\n");
687 ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
689 printf("ic->ic_rt =%p\n", ic->ic_rt);
690 printf("rate count %d\n", ic->ic_rt->rateCount);
692 uint8_t code = ic->ic_rt->info[0].dot11Rate;
693 uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
694 uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
695 printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
697 uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
698 uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
699 uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
700 uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
701 printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
702 printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
703 printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
709 wtap_detach(struct wtap_softc *sc)
711 struct ieee80211com *ic = &sc->sc_ic;
713 DWTAP_PRINTF("%s\n", __func__);
714 ieee80211_ageq_drain(&ic->ic_stageq);
715 ieee80211_ifdetach(ic);
720 wtap_resume(struct wtap_softc *sc)
723 DWTAP_PRINTF("%s\n", __func__);
727 wtap_suspend(struct wtap_softc *sc)
730 DWTAP_PRINTF("%s\n", __func__);
734 wtap_shutdown(struct wtap_softc *sc)
737 DWTAP_PRINTF("%s\n", __func__);
741 wtap_intr(struct wtap_softc *sc)
744 DWTAP_PRINTF("%s\n", __func__);