]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/wtap/if_wtap.c
Enforce that wtap requires VIMAGE to be useful.
[FreeBSD/FreeBSD.git] / sys / dev / wtap / if_wtap.c
1 /*-
2  * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
3  * All rights reserved.
4  *
5  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16  *    redistribution must be conditioned upon including a substantially
17  *    similar Disclaimer requirement for further binary redistribution.
18  *
19  * NO WARRANTY
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * $FreeBSD$
33  */
34 #include "if_wtapvar.h"
35 #include <sys/uio.h>    /* uio struct */
36 #include <sys/jail.h>
37 #include <net/if_var.h>
38 #include <net/vnet.h>
39
40 #include <net80211/ieee80211_ratectl.h>
41 #include "if_medium.h"
42
43 /*
44  * This _requires_ vimage to be useful.
45  */
46 #ifndef VIMAGE
47 #error  if_wtap requires VIMAGE.
48 #endif  /* VIMAGE */
49
50 /* device for IOCTL and read/write for debuggin purposes */
51 /* Function prototypes */
52 static  d_open_t        wtap_node_open;
53 static  d_close_t       wtap_node_close;
54 static  d_write_t       wtap_node_write;
55 static  d_ioctl_t       wtap_node_ioctl;
56
57 static struct cdevsw wtap_cdevsw = {
58         .d_version =    D_VERSION,
59         .d_flags =      0,
60         .d_open =       wtap_node_open,
61         .d_close =      wtap_node_close,
62         .d_write =      wtap_node_write,
63         .d_ioctl =      wtap_node_ioctl,
64         .d_name =       "wtapnode",
65 };
66
67 static int
68 wtap_node_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
69 {
70
71         int err = 0;
72         uprintf("Opened device \"echo\" successfully.\n");
73         return(err);
74 }
75
76 static int
77 wtap_node_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
78 {
79
80         uprintf("Closing device \"echo.\"\n");
81         return(0);
82 }
83
84 static int
85 wtap_node_write(struct cdev *dev, struct uio *uio, int ioflag)
86 {
87         int err = 0;
88         struct mbuf *m;
89         struct ifnet *ifp;
90         struct wtap_softc *sc;
91         uint8_t buf[1024];
92         int buf_len;
93
94         uprintf("write device %s \"echo.\"\n", devtoname(dev));
95         buf_len = MIN(uio->uio_iov->iov_len, 1024);
96         err = copyin(uio->uio_iov->iov_base, buf, buf_len);
97
98         if (err != 0) {
99                 uprintf("Write failed: bad address!\n");
100                 return (err);
101         }
102
103         MGETHDR(m, M_DONTWAIT, MT_DATA);
104         m_copyback(m, 0, buf_len, buf);
105
106         CURVNET_SET(TD_TO_VNET(curthread));
107         IFNET_RLOCK_NOSLEEP();
108
109         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
110                 printf("ifp->if_xname = %s\n", ifp->if_xname);
111                 if(strcmp(devtoname(dev), ifp->if_xname) == 0){
112                         printf("found match, correspoding wtap = %s\n",
113                             ifp->if_xname);
114                         sc = (struct wtap_softc *)ifp->if_softc;
115                         printf("wtap id = %d\n", sc->id);
116                         wtap_inject(sc, m);
117                 }
118         }
119
120         IFNET_RUNLOCK_NOSLEEP();
121         CURVNET_RESTORE();
122
123         return(err);
124 }
125
126 int
127 wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
128     int fflag, struct thread *td)
129 {
130         int error = 0;
131
132         switch(cmd) {
133         default:
134                 DWTAP_PRINTF("Unkown WTAP IOCTL\n");
135                 error = EINVAL;
136         }
137         return error;
138 }
139
140 static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
141         const struct ieee80211_bpf_params *params);
142
143 static int
144 wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
145 {
146
147         return medium_transmit(avp->av_md, avp->id, m);
148 }
149
150 static int
151 wtap_media_change(struct ifnet *ifp)
152 {
153
154         DWTAP_PRINTF("%s\n", __func__);
155         int error = ieee80211_media_change(ifp);
156         /* NB: only the fixed rate can change and that doesn't need a reset */
157         return (error == ENETRESET ? 0 : error);
158 }
159
160 /*
161  * Intercept management frames to collect beacon rssi data
162  * and to do ibss merges.
163  */
164 static void
165 wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
166     int subtype, int rssi, int nf)
167 {
168         struct ieee80211vap *vap = ni->ni_vap;
169 #if 0
170         DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
171 #endif
172         WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
173 }
174
175 static int
176 wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
177 {
178
179         DWTAP_PRINTF("%s\n", __func__);
180         return 0;
181 }
182
183 static void
184 wtap_beacon_update(struct ieee80211vap *vap, int item)
185 {
186         struct ieee80211_beacon_offsets *bo = &WTAP_VAP(vap)->av_boff;
187
188         DWTAP_PRINTF("%s\n", __func__);
189         setbit(bo->bo_flags, item);
190 }
191
192 /*
193  * Allocate and setup an initial beacon frame.
194  */
195 static int
196 wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
197 {
198         struct ieee80211vap *vap = ni->ni_vap;
199         struct wtap_vap *avp = WTAP_VAP(vap);
200
201         DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
202
203         /*
204          * NB: the beacon data buffer must be 32-bit aligned;
205          * we assume the mbuf routines will return us something
206          * with this alignment (perhaps should assert).
207          */
208         avp->beacon = ieee80211_beacon_alloc(ni, &avp->av_boff);
209         if (avp->beacon == NULL) {
210                 printf("%s: cannot get mbuf\n", __func__);
211                 return ENOMEM;
212         }
213         callout_init(&avp->av_swba, 0);
214         avp->bf_node = ieee80211_ref_node(ni);
215
216         return 0;
217 }
218
219 static void
220 wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
221 {
222
223         DWTAP_PRINTF("%s\n", __func__);
224 }
225
226 static void
227 wtap_beacon_intrp(void *arg)
228 {
229         struct wtap_vap *avp = arg;
230         struct ieee80211vap *vap = arg;
231         struct mbuf *m;
232
233         KASSERT(vap->iv_state >= IEEE80211_S_RUN,
234             ("not running, state %d", vap->iv_state));
235         DWTAP_PRINTF("[%d] beacon intrp\n", avp->id);   //burst mode
236         /*
237          * Update dynamic beacon contents.  If this returns
238          * non-zero then we need to remap the memory because
239          * the beacon frame changed size (probably because
240          * of the TIM bitmap).
241          */
242         m = m_dup(avp->beacon, M_DONTWAIT);
243         if (ieee80211_beacon_update(avp->bf_node, &avp->av_boff, m, 0)) {
244                 printf("%s, need to remap the memory because the beacon frame"
245                     " changed size.\n",__func__);
246         }
247
248         if (ieee80211_radiotap_active_vap(vap))
249             ieee80211_radiotap_tx(vap, m);
250
251 #if 0
252         medium_transmit(avp->av_md, avp->id, m);
253 #endif
254         wtap_medium_enqueue(avp, m);
255         callout_schedule(&avp->av_swba, avp->av_bcinterval);
256 }
257
258 static int
259 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
260 {
261         struct ieee80211com *ic = vap->iv_ic;
262         struct wtap_softc *sc = ic->ic_ifp->if_softc;
263         struct wtap_vap *avp = WTAP_VAP(vap);
264         struct ieee80211_node *ni = NULL;
265         int error;
266
267         DWTAP_PRINTF("%s\n", __func__);
268
269         ni = vap->iv_bss;
270         /*
271          * Invoke the parent method to do net80211 work.
272          */
273         error = avp->av_newstate(vap, nstate, arg);
274         if (error != 0)
275                 goto bad;
276
277         if (nstate == IEEE80211_S_RUN) {
278                 /* NB: collect bss node again, it may have changed */
279                 ni = vap->iv_bss;
280                 switch (vap->iv_opmode) {
281                 case IEEE80211_M_MBSS:
282                         error = wtap_beacon_alloc(sc, ni);
283                         if (error != 0)
284                                 goto bad;
285                         wtap_beacon_config(sc, vap);
286                         callout_reset(&avp->av_swba, avp->av_bcinterval,
287                             wtap_beacon_intrp, vap);
288                         break;
289                 default:
290                         goto bad;
291                 }
292         }
293         return 0;
294 bad:
295         printf("%s: bad\n", __func__);
296         return error;
297 }
298
299 static void
300 wtap_bmiss(struct ieee80211vap *vap)
301 {
302         struct wtap_vap *avp = (struct wtap_vap *)vap;
303
304         DWTAP_PRINTF("%s\n", __func__);
305         avp->av_bmiss(vap);
306 }
307
308 static struct ieee80211vap *
309 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
310     int unit, enum ieee80211_opmode opmode, int flags,
311     const uint8_t bssid[IEEE80211_ADDR_LEN],
312     const uint8_t mac[IEEE80211_ADDR_LEN])
313 {
314          struct wtap_softc *sc = ic->ic_ifp->if_softc;
315          struct ieee80211vap *vap;
316          struct wtap_vap *avp;
317          int error;
318
319          DWTAP_PRINTF("%s\n", __func__);
320
321         avp = (struct wtap_vap *) malloc(sizeof(struct wtap_vap),
322             M_80211_VAP, M_NOWAIT | M_ZERO);
323         avp->id = sc->id;
324         avp->av_md = sc->sc_md;
325         avp->av_bcinterval = BEACON_INTRERVAL + 100*sc->id;
326         vap = (struct ieee80211vap *) avp;
327         error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
328             flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
329
330         /* override various methods */
331         avp->av_recv_mgmt = vap->iv_recv_mgmt;
332         vap->iv_recv_mgmt = wtap_recv_mgmt;
333         vap->iv_reset = wtap_reset_vap;
334         vap->iv_update_beacon = wtap_beacon_update;
335         avp->av_newstate = vap->iv_newstate;
336         vap->iv_newstate = wtap_newstate;
337         avp->av_bmiss = vap->iv_bmiss;
338         vap->iv_bmiss = wtap_bmiss;
339
340         /* complete setup */
341         ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status);
342         avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
343             (const char *)ic->ic_ifp->if_xname);
344
345         /* TODO this is a hack to force it to choose the rate we want */
346         vap->iv_bss->ni_txrate = 130;
347         return vap;
348 }
349
350 static void
351 wtap_vap_delete(struct ieee80211vap *vap)
352 {
353         struct wtap_vap *avp = WTAP_VAP(vap);
354
355         DWTAP_PRINTF("%s\n", __func__);
356         destroy_dev(avp->av_dev);
357         callout_stop(&avp->av_swba);
358         ieee80211_vap_detach(vap);
359         free((struct wtap_vap*) vap, M_80211_VAP);
360 }
361
362 /* NB: This function is not used.
363  * I had the problem of the queue
364  * being empty all the time.
365  * Maybe I am setting the queue wrong?
366  */
367 static void
368 wtap_start(struct ifnet *ifp)
369 {
370         struct ieee80211com *ic = ifp->if_l2com;
371         struct ifnet *icifp = ic->ic_ifp;
372         struct wtap_softc *sc = icifp->if_softc;
373         struct ieee80211_node *ni;
374         struct mbuf *m;
375
376         DWTAP_PRINTF("my_start, with id=%u\n", sc->id);
377
378         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->up == 0)
379                 return;
380         for (;;) {
381                 if(IFQ_IS_EMPTY(&ifp->if_snd)){
382                     printf("queue empty, just trying to see "
383                         "if the other queue is empty\n");
384 #if 0
385                     printf("queue for id=1, %u\n",
386                         IFQ_IS_EMPTY(&global_mscs[1]->ifp->if_snd));
387                     printf("queue for id=0, %u\n",
388                         IFQ_IS_EMPTY(&global_mscs[0]->ifp->if_snd));
389 #endif
390                     break;
391                 }
392                 IFQ_DEQUEUE(&ifp->if_snd, m);
393                 if (m == NULL) {
394                         printf("error dequeueing from ifp->snd\n");
395                         break;
396                 }
397                 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
398                 /*
399                  * Check for fragmentation.  If this frame
400                  * has been broken up verify we have enough
401                  * buffers to send all the fragments so all
402                  * go out or none...
403                  */
404 #if 0
405                 STAILQ_INIT(&frags);
406 #endif
407                 if ((m->m_flags & M_FRAG)){
408                         printf("dont support frags\n");
409                         ifp->if_oerrors++;
410                         return;
411                 }
412                 ifp->if_opackets++;
413                 if(wtap_raw_xmit(ni, m, NULL) < 0){
414                         printf("error raw_xmiting\n");
415                         ifp->if_oerrors++;
416                         return;
417                 }
418         }
419 }
420
421 static int
422 wtap_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
423 {
424 #if 0
425         DWTAP_PRINTF("%s\n", __func__);
426         uprintf("%s, command %lu\n", __func__, cmd);
427 #endif
428 #define IS_RUNNING(ifp) \
429         ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
430         struct ieee80211com *ic = ifp->if_l2com;
431         struct wtap_softc *sc = ifp->if_softc;
432         struct ifreq *ifr = (struct ifreq *)data;
433         int error = 0;
434
435         switch (cmd) {
436         case SIOCSIFFLAGS:
437                 //printf("%s: %s\n", __func__, "SIOCSIFFLAGS");
438                 if (IS_RUNNING(ifp)) {
439                         DWTAP_PRINTF("running\n");
440 #if 0
441                         /*
442                          * To avoid rescanning another access point,
443                          * do not call ath_init() here.  Instead,
444                          * only reflect promisc mode settings.
445                          */
446                         //ath_mode_init(sc);
447 #endif
448                         } else if (ifp->if_flags & IFF_UP) {
449                         DWTAP_PRINTF("up\n");
450                         sc->up = 1;
451 #if 0
452                         /*
453                          * Beware of being called during attach/detach
454                          * to reset promiscuous mode.  In that case we
455                          * will still be marked UP but not RUNNING.
456                          * However trying to re-init the interface
457                          * is the wrong thing to do as we've already
458                          * torn down much of our state.  There's
459                          * probably a better way to deal with this.
460                          */
461                         //if (!sc->sc_invalid)
462                         //      ath_init(sc);   /* XXX lose error */
463 #endif
464                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
465                         ieee80211_start_all(ic);
466                 } else {
467                         DWTAP_PRINTF("stoping\n");
468 #if 0
469                         ath_stop_locked(ifp);
470 #ifdef notyet
471                         /* XXX must wakeup in places like ath_vap_delete */
472                         if (!sc->sc_invalid)
473                                 ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
474 #endif
475 #endif
476                 }
477                 break;
478         case SIOCGIFMEDIA:
479         case SIOCSIFMEDIA:
480 #if 0
481                 DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFMEDIA|SIOCSIFMEDIA");
482 #endif
483                 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
484                 break;
485         case SIOCGIFADDR:
486 #if 0
487                 DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFADDR");
488 #endif
489                 error = ether_ioctl(ifp, cmd, data);
490                 break;
491         default:
492                 DWTAP_PRINTF("%s: %s [%lu]\n", __func__, "EINVAL", cmd);
493                 error = EINVAL;
494                 break;
495         }
496         return error;
497 #undef IS_RUNNING
498 }
499
500 static void
501 wtap_init(void *arg){
502
503         DWTAP_PRINTF("%s\n", __func__);
504 }
505
506 static void
507 wtap_scan_start(struct ieee80211com *ic)
508 {
509
510 #if 0
511         DWTAP_PRINTF("%s\n", __func__);
512 #endif
513 }
514
515 static void
516 wtap_scan_end(struct ieee80211com *ic)
517 {
518
519 #if 0
520         DWTAP_PRINTF("%s\n", __func__);
521 #endif
522 }
523
524 static void
525 wtap_set_channel(struct ieee80211com *ic)
526 {
527
528 #if 0
529         DWTAP_PRINTF("%s\n", __func__);
530 #endif
531 }
532
533 static int
534 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
535         const struct ieee80211_bpf_params *params)
536 {
537 #if 0
538         DWTAP_PRINTF("%s, %p\n", __func__, m);
539 #endif
540         struct ieee80211vap     *vap = ni->ni_vap;
541         struct wtap_vap         *avp = WTAP_VAP(vap);
542
543         if (ieee80211_radiotap_active_vap(vap)) {
544                 ieee80211_radiotap_tx(vap, m);
545         }
546         if (m->m_flags & M_TXCB)
547                 ieee80211_process_callback(ni, m, 0);
548         ieee80211_free_node(ni);
549         return wtap_medium_enqueue(avp, m);
550 }
551
552 void
553 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
554 {
555       struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
556           M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
557       KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
558       bf->m = m;
559
560       mtx_lock(&sc->sc_mtx);
561       STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
562       taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
563       mtx_unlock(&sc->sc_mtx);
564 }
565
566 void
567 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
568 {
569         struct ifnet *ifp = sc->sc_ifp;
570         struct ieee80211com *ic = ifp->if_l2com;
571         struct ieee80211_node *ni;
572         int type;
573 #if 0
574         DWTAP_PRINTF("%s\n", __func__);
575 #endif
576
577         DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
578         if (m == NULL) {                /* NB: shouldn't happen */
579                 if_printf(ifp, "%s: no mbuf!\n", __func__);
580         }
581
582         ifp->if_ipackets++;
583
584         ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
585
586         /*
587           * Locate the node for sender, track state, and then
588           * pass the (referenced) node up to the 802.11 layer
589           * for its use.
590           */
591         ni = ieee80211_find_rxnode_withkey(ic,
592             mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
593         if (ni != NULL) {
594                 /*
595                  * Sending station is known, dispatch directly.
596                  */
597                 type = ieee80211_input(ni, m, 1<<7, 10);
598                 ieee80211_free_node(ni);
599         } else {
600                 type = ieee80211_input_all(ic, m, 1<<7, 10);
601         }
602 }
603
604 static void
605 wtap_rx_proc(void *arg, int npending)
606 {
607         struct wtap_softc *sc = (struct wtap_softc *)arg;
608         struct ifnet *ifp = sc->sc_ifp;
609         struct ieee80211com *ic = ifp->if_l2com;
610         struct mbuf *m;
611         struct ieee80211_node *ni;
612         int type;
613         struct wtap_buf *bf;
614
615 #if 0
616         DWTAP_PRINTF("%s\n", __func__);
617 #endif
618
619         for(;;) {
620                 mtx_lock(&sc->sc_mtx);
621                 bf = STAILQ_FIRST(&sc->sc_rxbuf);
622                 if (bf == NULL) {
623                         mtx_unlock(&sc->sc_mtx);
624                         return;
625                 }
626                 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
627                 mtx_unlock(&sc->sc_mtx);
628                 KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
629                 m = bf->m;
630                 DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
631                 if (m == NULL) {                /* NB: shouldn't happen */
632                         if_printf(ifp, "%s: no mbuf!\n", __func__);
633                         free(bf, M_WTAP_RXBUF);
634                         return;
635                 }
636
637                 ifp->if_ipackets++;
638 #if 0
639                 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
640 #endif
641
642                 /*
643                  * Locate the node for sender, track state, and then
644                  * pass the (referenced) node up to the 802.11 layer
645                  * for its use.
646                  */
647                 ni = ieee80211_find_rxnode_withkey(ic,
648                     mtod(m, const struct ieee80211_frame_min *),
649                     IEEE80211_KEYIX_NONE);
650                 if (ni != NULL) {
651                         /*
652                          * Sending station is known, dispatch directly.
653                          */
654 #if 0
655                         ieee80211_radiotap_rx(ni->ni_vap, m);
656 #endif
657                         type = ieee80211_input(ni, m, 1<<7, 10);
658                         ieee80211_free_node(ni);
659                 } else {
660 #if 0
661                         ieee80211_radiotap_rx_all(ic, m);
662 #endif
663                         type = ieee80211_input_all(ic, m, 1<<7, 10);
664                 }
665                 
666                 /* The mbufs are freed by the Net80211 stack */
667                 free(bf, M_WTAP_RXBUF);
668         }
669 }
670
671 static void
672 wtap_newassoc(struct ieee80211_node *ni, int isnew)
673 {
674
675         DWTAP_PRINTF("%s\n", __func__);
676 }
677
678 /*
679  * Callback from the 802.11 layer to update WME parameters.
680  */
681 static int
682 wtap_wme_update(struct ieee80211com *ic)
683 {
684
685         DWTAP_PRINTF("%s\n", __func__);
686         return 0;
687 }
688
689 static void
690 wtap_update_mcast(struct ifnet *ifp)
691 {
692
693         DWTAP_PRINTF("%s\n", __func__);
694 }
695
696 static void
697 wtap_update_promisc(struct ifnet *ifp)
698 {
699
700         DWTAP_PRINTF("%s\n", __func__);
701 }
702
703 static int
704 wtap_if_transmit(struct ifnet *ifp, struct mbuf *m)
705 {
706         struct ieee80211_node *ni =
707             (struct ieee80211_node *) m->m_pkthdr.rcvif;
708         struct ieee80211vap *vap = ni->ni_vap;
709         struct wtap_vap *avp = WTAP_VAP(vap);
710
711         if(ni == NULL){
712                 printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
713         }else{
714                 if (ieee80211_radiotap_active_vap(vap))
715                         ieee80211_radiotap_tx(vap, m);
716         }
717         if (m->m_flags & M_TXCB)
718                 ieee80211_process_callback(ni, m, 0);
719         ieee80211_free_node(ni);
720         return wtap_medium_enqueue(avp, m);
721 }
722
723 static struct ieee80211_node *
724 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
725 {
726         struct ieee80211_node *ni;
727
728         DWTAP_PRINTF("%s\n", __func__);
729
730         ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
731             M_NOWAIT|M_ZERO);
732
733         ni->ni_txrate = 130;
734         return ni;
735 }
736
737 static void
738 wtap_node_free(struct ieee80211_node *ni)
739 {
740         struct ieee80211com *ic = ni->ni_ic;
741         struct wtap_softc *sc = ic->ic_ifp->if_softc;
742
743         DWTAP_PRINTF("%s\n", __func__);
744         sc->sc_node_free(ni);
745 }
746
747 int32_t
748 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
749 {
750         struct ifnet *ifp;
751         struct ieee80211com *ic;
752         char wtap_name[] = {'w','T','a','p',sc->id,
753             '_','t','a','s','k','q','\0'};
754
755         DWTAP_PRINTF("%s\n", __func__);
756
757         ifp = if_alloc(IFT_IEEE80211);
758         if (ifp == NULL) {
759                 printf("can not if_alloc()\n");
760                 return -1;
761         }
762         ic = ifp->if_l2com;
763         if_initname(ifp, "wtap", sc->id);
764
765         sc->sc_ifp = ifp;
766         sc->up = 0;
767
768         STAILQ_INIT(&sc->sc_rxbuf);
769         sc->sc_tq = taskqueue_create(wtap_name, M_NOWAIT | M_ZERO,
770             taskqueue_thread_enqueue, &sc->sc_tq);
771         taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ",
772             ifp->if_xname);
773         TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
774
775         ifp->if_softc = sc;
776         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
777         ifp->if_start = wtap_start;
778         ifp->if_ioctl = wtap_ioctl;
779         ifp->if_init = wtap_init;
780         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
781         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
782         IFQ_SET_READY(&ifp->if_snd);
783
784         ic->ic_ifp = ifp;
785         ic->ic_phytype = IEEE80211_T_DS;
786         ic->ic_opmode = IEEE80211_M_MBSS;
787         ic->ic_caps = IEEE80211_C_MBSS;
788
789         ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
790
791         ic->ic_regdomain.regdomain = SKU_ETSI;
792         ic->ic_regdomain.country = CTRY_SWEDEN;
793         ic->ic_regdomain.location = 1; /* Indoors */
794         ic->ic_regdomain.isocc[0] = 'S';
795         ic->ic_regdomain.isocc[1] = 'E';
796         /*
797          * Indicate we need the 802.11 header padded to a
798          * 32-bit boundary for 4-address and QoS frames.
799          */
800         ic->ic_flags |= IEEE80211_F_DATAPAD;
801         ic->ic_nchans = 1;
802         ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
803         ic->ic_channels[0].ic_freq = 2412;
804
805         ieee80211_ifattach(ic, macaddr);
806
807 #if 0
808         /* new prototype hook-ups */
809         msc->if_input = ifp->if_input;
810         ifp->if_input = myath_if_input;
811         msc->if_output = ifp->if_output;
812         ifp->if_output = myath_if_output;
813 #endif
814         sc->if_transmit = ifp->if_transmit;
815         ifp->if_transmit = wtap_if_transmit;
816
817         /* override default methods */
818         ic->ic_newassoc = wtap_newassoc;
819 #if 0
820         ic->ic_updateslot = myath_updateslot;
821 #endif
822         ic->ic_wme.wme_update = wtap_wme_update;
823         ic->ic_vap_create = wtap_vap_create;
824         ic->ic_vap_delete = wtap_vap_delete;
825         ic->ic_raw_xmit = wtap_raw_xmit;
826         ic->ic_update_mcast = wtap_update_mcast;
827         ic->ic_update_promisc = wtap_update_promisc;
828
829         sc->sc_node_alloc = ic->ic_node_alloc;
830         ic->ic_node_alloc = wtap_node_alloc;
831         sc->sc_node_free = ic->ic_node_free;
832         ic->ic_node_free = wtap_node_free;
833
834 #if 0
835         ic->ic_node_getsignal = myath_node_getsignal;
836 #endif
837         ic->ic_scan_start = wtap_scan_start;
838         ic->ic_scan_end = wtap_scan_end;
839         ic->ic_set_channel = wtap_set_channel;
840
841         ieee80211_radiotap_attach(ic,
842             &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
843             WTAP_TX_RADIOTAP_PRESENT,
844             &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
845             WTAP_RX_RADIOTAP_PRESENT);
846
847         /* Work here, we must find a way to populate the rate table */
848 #if 0
849         if(ic->ic_rt == NULL){
850                 printf("no table for ic_curchan\n");
851                 ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
852         }
853         printf("ic->ic_rt =%p\n", ic->ic_rt);
854         printf("rate count %d\n", ic->ic_rt->rateCount);
855
856         uint8_t code = ic->ic_rt->info[0].dot11Rate;
857         uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
858         uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
859         printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
860
861         uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
862         uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
863         uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
864         uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
865         printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
866         printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
867         printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
868 #endif
869         return 0;
870 }
871
872 int32_t
873 wtap_detach(struct wtap_softc *sc)
874 {
875         struct ifnet *ifp = sc->sc_ifp;
876         struct ieee80211com *ic = ifp->if_l2com;
877
878         DWTAP_PRINTF("%s\n", __func__);
879         ieee80211_ageq_drain(&ic->ic_stageq);
880         ieee80211_ifdetach(ic);
881         if_free(ifp);
882         return 0;
883 }
884
885 void
886 wtap_resume(struct wtap_softc *sc)
887 {
888
889         DWTAP_PRINTF("%s\n", __func__);
890 }
891
892 void
893 wtap_suspend(struct wtap_softc *sc)
894 {
895
896         DWTAP_PRINTF("%s\n", __func__);
897 }
898
899 void
900 wtap_shutdown(struct wtap_softc *sc)
901 {
902
903         DWTAP_PRINTF("%s\n", __func__);
904 }
905
906 void
907 wtap_intr(struct wtap_softc *sc)
908 {
909
910         DWTAP_PRINTF("%s\n", __func__);
911 }