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;
96 uprintf("write device %s \"echo.\"\n", devtoname(dev));
97 buf_len = MIN(uio->uio_iov->iov_len, 1024);
98 err = copyin(uio->uio_iov->iov_base, buf, buf_len);
101 uprintf("Write failed: bad address!\n");
105 MGETHDR(m, M_NOWAIT, MT_DATA);
106 m_copyback(m, 0, buf_len, buf);
108 CURVNET_SET(TD_TO_VNET(curthread));
109 IFNET_RLOCK_NOSLEEP();
111 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
112 printf("ifp->if_xname = %s\n", ifp->if_xname);
113 if(strcmp(devtoname(dev), ifp->if_xname) == 0){
114 printf("found match, correspoding wtap = %s\n",
116 sc = (struct wtap_softc *)ifp->if_softc;
117 printf("wtap id = %d\n", sc->id);
122 IFNET_RUNLOCK_NOSLEEP();
129 wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
130 int fflag, struct thread *td)
136 DWTAP_PRINTF("Unknown WTAP IOCTL\n");
142 static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
143 const struct ieee80211_bpf_params *params);
146 wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
149 return medium_transmit(avp->av_md, avp->id, m);
153 wtap_media_change(struct ifnet *ifp)
156 DWTAP_PRINTF("%s\n", __func__);
157 int error = ieee80211_media_change(ifp);
158 /* NB: only the fixed rate can change and that doesn't need a reset */
159 return (error == ENETRESET ? 0 : error);
163 * Intercept management frames to collect beacon rssi data
164 * and to do ibss merges.
167 wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
168 int subtype, const struct ieee80211_rx_stats *stats, int rssi, int nf)
170 struct ieee80211vap *vap = ni->ni_vap;
172 DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
174 WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, stats, rssi, nf);
178 wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
181 DWTAP_PRINTF("%s\n", __func__);
186 wtap_beacon_update(struct ieee80211vap *vap, int item)
188 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
190 DWTAP_PRINTF("%s\n", __func__);
191 setbit(bo->bo_flags, item);
195 * Allocate and setup an initial beacon frame.
198 wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
200 struct ieee80211vap *vap = ni->ni_vap;
201 struct wtap_vap *avp = WTAP_VAP(vap);
203 DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
206 * NB: the beacon data buffer must be 32-bit aligned;
207 * we assume the mbuf routines will return us something
208 * with this alignment (perhaps should assert).
210 avp->beacon = ieee80211_beacon_alloc(ni);
211 if (avp->beacon == NULL) {
212 printf("%s: cannot get mbuf\n", __func__);
215 callout_init(&avp->av_swba, 0);
216 avp->bf_node = ieee80211_ref_node(ni);
222 wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
225 DWTAP_PRINTF("%s\n", __func__);
229 wtap_beacon_intrp(void *arg)
231 struct wtap_vap *avp = arg;
232 struct ieee80211vap *vap = arg;
235 if (vap->iv_state < IEEE80211_S_RUN) {
236 DWTAP_PRINTF("Skip beacon, not running, state %d", vap->iv_state);
239 DWTAP_PRINTF("[%d] beacon intrp\n", avp->id); //burst mode
241 * Update dynamic beacon contents. If this returns
242 * non-zero then we need to remap the memory because
243 * the beacon frame changed size (probably because
244 * of the TIM bitmap).
246 m = m_dup(avp->beacon, M_NOWAIT);
247 if (ieee80211_beacon_update(avp->bf_node, m, 0)) {
248 printf("%s, need to remap the memory because the beacon frame"
249 " changed size.\n",__func__);
252 if (ieee80211_radiotap_active_vap(vap))
253 ieee80211_radiotap_tx(vap, m);
256 medium_transmit(avp->av_md, avp->id, m);
258 wtap_medium_enqueue(avp, m);
259 callout_schedule(&avp->av_swba, avp->av_bcinterval);
263 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
265 struct ieee80211com *ic = vap->iv_ic;
266 struct wtap_softc *sc = ic->ic_softc;
267 struct wtap_vap *avp = WTAP_VAP(vap);
268 struct ieee80211_node *ni = NULL;
271 DWTAP_PRINTF("%s\n", __func__);
273 ni = ieee80211_ref_node(vap->iv_bss);
275 * Invoke the parent method to do net80211 work.
277 error = avp->av_newstate(vap, nstate, arg);
281 if (nstate == IEEE80211_S_RUN) {
282 /* NB: collect bss node again, it may have changed */
283 ieee80211_free_node(ni);
284 ni = ieee80211_ref_node(vap->iv_bss);
285 switch (vap->iv_opmode) {
286 case IEEE80211_M_MBSS:
287 error = wtap_beacon_alloc(sc, ni);
290 wtap_beacon_config(sc, vap);
291 callout_reset(&avp->av_swba, avp->av_bcinterval,
292 wtap_beacon_intrp, vap);
297 } else if (nstate == IEEE80211_S_INIT) {
298 callout_stop(&avp->av_swba);
300 ieee80211_free_node(ni);
303 printf("%s: bad\n", __func__);
304 ieee80211_free_node(ni);
309 wtap_bmiss(struct ieee80211vap *vap)
311 struct wtap_vap *avp = (struct wtap_vap *)vap;
313 DWTAP_PRINTF("%s\n", __func__);
317 static struct ieee80211vap *
318 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
319 int unit, enum ieee80211_opmode opmode, int flags,
320 const uint8_t bssid[IEEE80211_ADDR_LEN],
321 const uint8_t mac[IEEE80211_ADDR_LEN])
323 struct wtap_softc *sc = ic->ic_softc;
324 struct ieee80211vap *vap;
325 struct wtap_vap *avp;
327 struct ieee80211_node *ni;
329 DWTAP_PRINTF("%s\n", __func__);
331 avp = malloc(sizeof(struct wtap_vap), M_80211_VAP, M_WAITOK | M_ZERO);
333 avp->av_md = sc->sc_md;
334 avp->av_bcinterval = msecs_to_ticks(BEACON_INTRERVAL + 100*sc->id);
335 vap = (struct ieee80211vap *) avp;
336 error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
337 flags | IEEE80211_CLONE_NOBEACONS, bssid);
339 free(avp, M_80211_VAP);
343 /* override various methods */
344 avp->av_recv_mgmt = vap->iv_recv_mgmt;
345 vap->iv_recv_mgmt = wtap_recv_mgmt;
346 vap->iv_reset = wtap_reset_vap;
347 vap->iv_update_beacon = wtap_beacon_update;
348 avp->av_newstate = vap->iv_newstate;
349 vap->iv_newstate = wtap_newstate;
350 avp->av_bmiss = vap->iv_bmiss;
351 vap->iv_bmiss = wtap_bmiss;
354 ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status,
356 avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
357 "%s", (const char *)sc->name);
359 /* TODO this is a hack to force it to choose the rate we want */
360 ni = ieee80211_ref_node(vap->iv_bss);
362 ieee80211_free_node(ni);
367 wtap_vap_delete(struct ieee80211vap *vap)
369 struct wtap_vap *avp = WTAP_VAP(vap);
371 DWTAP_PRINTF("%s\n", __func__);
372 destroy_dev(avp->av_dev);
373 callout_stop(&avp->av_swba);
374 ieee80211_vap_detach(vap);
375 free((struct wtap_vap*) vap, M_80211_VAP);
379 wtap_parent(struct ieee80211com *ic)
381 struct wtap_softc *sc = ic->ic_softc;
383 if (ic->ic_nrunning > 0) {
385 ieee80211_start_all(ic);
391 wtap_scan_start(struct ieee80211com *ic)
395 DWTAP_PRINTF("%s\n", __func__);
400 wtap_scan_end(struct ieee80211com *ic)
404 DWTAP_PRINTF("%s\n", __func__);
409 wtap_set_channel(struct ieee80211com *ic)
413 DWTAP_PRINTF("%s\n", __func__);
418 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
419 const struct ieee80211_bpf_params *params)
422 DWTAP_PRINTF("%s, %p\n", __func__, m);
424 struct ieee80211vap *vap = ni->ni_vap;
425 struct wtap_vap *avp = WTAP_VAP(vap);
427 if (ieee80211_radiotap_active_vap(vap)) {
428 ieee80211_radiotap_tx(vap, m);
430 if (m->m_flags & M_TXCB)
431 ieee80211_process_callback(ni, m, 0);
432 ieee80211_free_node(ni);
433 return wtap_medium_enqueue(avp, m);
437 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
439 struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
440 M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
441 KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
444 mtx_lock(&sc->sc_mtx);
445 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
446 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
447 mtx_unlock(&sc->sc_mtx);
451 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
453 struct ieee80211com *ic = &sc->sc_ic;
454 struct ieee80211_node *ni;
457 DWTAP_PRINTF("%s\n", __func__);
460 DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
461 if (m == NULL) { /* NB: shouldn't happen */
462 ic_printf(ic, "%s: no mbuf!\n", __func__);
465 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
468 * Locate the node for sender, track state, and then
469 * pass the (referenced) node up to the 802.11 layer
472 ni = ieee80211_find_rxnode_withkey(ic,
473 mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
476 * Sending station is known, dispatch directly.
478 type = ieee80211_input(ni, m, 1<<7, 10);
479 ieee80211_free_node(ni);
481 type = ieee80211_input_all(ic, m, 1<<7, 10);
486 wtap_rx_proc(void *arg, int npending)
488 struct wtap_softc *sc = (struct wtap_softc *)arg;
489 struct ieee80211com *ic = &sc->sc_ic;
491 struct ieee80211_node *ni;
496 DWTAP_PRINTF("%s\n", __func__);
500 mtx_lock(&sc->sc_mtx);
501 bf = STAILQ_FIRST(&sc->sc_rxbuf);
503 mtx_unlock(&sc->sc_mtx);
506 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
507 mtx_unlock(&sc->sc_mtx);
508 KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
510 DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
511 if (m == NULL) { /* NB: shouldn't happen */
512 ic_printf(ic, "%s: no mbuf!\n", __func__);
513 free(bf, M_WTAP_RXBUF);
517 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
521 * Locate the node for sender, track state, and then
522 * pass the (referenced) node up to the 802.11 layer
525 ni = ieee80211_find_rxnode_withkey(ic,
526 mtod(m, const struct ieee80211_frame_min *),
527 IEEE80211_KEYIX_NONE);
530 * Sending station is known, dispatch directly.
532 type = ieee80211_input(ni, m, 1<<7, 10);
533 ieee80211_free_node(ni);
535 type = ieee80211_input_all(ic, m, 1<<7, 10);
538 /* The mbufs are freed by the Net80211 stack */
539 free(bf, M_WTAP_RXBUF);
544 wtap_newassoc(struct ieee80211_node *ni, int isnew)
547 DWTAP_PRINTF("%s\n", __func__);
551 * Callback from the 802.11 layer to update WME parameters.
554 wtap_wme_update(struct ieee80211com *ic)
557 DWTAP_PRINTF("%s\n", __func__);
562 wtap_update_mcast(struct ieee80211com *ic)
565 DWTAP_PRINTF("%s\n", __func__);
569 wtap_update_promisc(struct ieee80211com *ic)
572 DWTAP_PRINTF("%s\n", __func__);
576 wtap_transmit(struct ieee80211com *ic, struct mbuf *m)
578 struct ieee80211_node *ni =
579 (struct ieee80211_node *) m->m_pkthdr.rcvif;
580 struct ieee80211vap *vap = ni->ni_vap;
581 struct wtap_vap *avp = WTAP_VAP(vap);
584 printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
586 if (ieee80211_radiotap_active_vap(vap))
587 ieee80211_radiotap_tx(vap, m);
589 if (m->m_flags & M_TXCB)
590 ieee80211_process_callback(ni, m, 0);
591 ieee80211_free_node(ni);
592 return wtap_medium_enqueue(avp, m);
595 static struct ieee80211_node *
596 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
598 struct ieee80211_node *ni;
600 DWTAP_PRINTF("%s\n", __func__);
602 ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
610 wtap_node_free(struct ieee80211_node *ni)
612 struct ieee80211com *ic = ni->ni_ic;
613 struct wtap_softc *sc = ic->ic_softc;
615 DWTAP_PRINTF("%s\n", __func__);
616 sc->sc_node_free(ni);
620 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
622 struct ieee80211com *ic = &sc->sc_ic;
624 DWTAP_PRINTF("%s\n", __func__);
627 STAILQ_INIT(&sc->sc_rxbuf);
628 sc->sc_tq = taskqueue_create("wtap_taskq", M_NOWAIT | M_ZERO,
629 taskqueue_thread_enqueue, &sc->sc_tq);
630 taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ", sc->name);
631 TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
634 ic->ic_name = sc->name;
635 ic->ic_phytype = IEEE80211_T_DS;
636 ic->ic_opmode = IEEE80211_M_MBSS;
637 ic->ic_caps = IEEE80211_C_MBSS;
639 ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
641 ic->ic_regdomain.regdomain = SKU_ETSI;
642 ic->ic_regdomain.country = CTRY_SWEDEN;
643 ic->ic_regdomain.location = 1; /* Indoors */
644 ic->ic_regdomain.isocc[0] = 'S';
645 ic->ic_regdomain.isocc[1] = 'E';
648 ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
649 ic->ic_channels[0].ic_freq = 2412;
651 IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
652 ieee80211_ifattach(ic);
654 /* override default methods */
655 ic->ic_newassoc = wtap_newassoc;
656 ic->ic_wme.wme_update = wtap_wme_update;
657 ic->ic_vap_create = wtap_vap_create;
658 ic->ic_vap_delete = wtap_vap_delete;
659 ic->ic_raw_xmit = wtap_raw_xmit;
660 ic->ic_update_mcast = wtap_update_mcast;
661 ic->ic_update_promisc = wtap_update_promisc;
662 ic->ic_transmit = wtap_transmit;
663 ic->ic_parent = wtap_parent;
665 sc->sc_node_alloc = ic->ic_node_alloc;
666 ic->ic_node_alloc = wtap_node_alloc;
667 sc->sc_node_free = ic->ic_node_free;
668 ic->ic_node_free = wtap_node_free;
670 ic->ic_scan_start = wtap_scan_start;
671 ic->ic_scan_end = wtap_scan_end;
672 ic->ic_set_channel = wtap_set_channel;
674 ieee80211_radiotap_attach(ic,
675 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
676 WTAP_TX_RADIOTAP_PRESENT,
677 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
678 WTAP_RX_RADIOTAP_PRESENT);
680 /* Work here, we must find a way to populate the rate table */
682 if(ic->ic_rt == NULL){
683 printf("no table for ic_curchan\n");
684 ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
686 printf("ic->ic_rt =%p\n", ic->ic_rt);
687 printf("rate count %d\n", ic->ic_rt->rateCount);
689 uint8_t code = ic->ic_rt->info[0].dot11Rate;
690 uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
691 uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
692 printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
694 uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
695 uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
696 uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
697 uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
698 printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
699 printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
700 printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
706 wtap_detach(struct wtap_softc *sc)
708 struct ieee80211com *ic = &sc->sc_ic;
710 DWTAP_PRINTF("%s\n", __func__);
711 ieee80211_ageq_drain(&ic->ic_stageq);
712 ieee80211_ifdetach(ic);
717 wtap_resume(struct wtap_softc *sc)
720 DWTAP_PRINTF("%s\n", __func__);
724 wtap_suspend(struct wtap_softc *sc)
727 DWTAP_PRINTF("%s\n", __func__);
731 wtap_shutdown(struct wtap_softc *sc)
734 DWTAP_PRINTF("%s\n", __func__);
738 wtap_intr(struct wtap_softc *sc)
741 DWTAP_PRINTF("%s\n", __func__);