xref: /freebsd/sys/dev/wtap/if_wtap.c (revision 38d120bc13ac1de5b739b67b87016b9122149374)
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_NOWAIT, 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 	if (vap->iv_state < IEEE80211_S_RUN) {
234 	    DWTAP_PRINTF("Skip beacon, not running, state %d", vap->iv_state);
235 	    return ;
236 	}
237 	DWTAP_PRINTF("[%d] beacon intrp\n", avp->id);	//burst mode
238 	/*
239 	 * Update dynamic beacon contents.  If this returns
240 	 * non-zero then we need to remap the memory because
241 	 * the beacon frame changed size (probably because
242 	 * of the TIM bitmap).
243 	 */
244 	m = m_dup(avp->beacon, M_NOWAIT);
245 	if (ieee80211_beacon_update(avp->bf_node, &avp->av_boff, m, 0)) {
246 		printf("%s, need to remap the memory because the beacon frame"
247 		    " changed size.\n",__func__);
248 	}
249 
250 	if (ieee80211_radiotap_active_vap(vap))
251 	    ieee80211_radiotap_tx(vap, m);
252 
253 #if 0
254 	medium_transmit(avp->av_md, avp->id, m);
255 #endif
256 	wtap_medium_enqueue(avp, m);
257 	callout_schedule(&avp->av_swba, avp->av_bcinterval);
258 }
259 
260 static int
261 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
262 {
263 	struct ieee80211com *ic = vap->iv_ic;
264 	struct wtap_softc *sc = ic->ic_ifp->if_softc;
265 	struct wtap_vap *avp = WTAP_VAP(vap);
266 	struct ieee80211_node *ni = NULL;
267 	int error;
268 
269 	DWTAP_PRINTF("%s\n", __func__);
270 
271 	ni = ieee80211_ref_node(vap->iv_bss);
272 	/*
273 	 * Invoke the parent method to do net80211 work.
274 	 */
275 	error = avp->av_newstate(vap, nstate, arg);
276 	if (error != 0)
277 		goto bad;
278 
279 	if (nstate == IEEE80211_S_RUN) {
280 		/* NB: collect bss node again, it may have changed */
281 		ieee80211_free_node(ni);
282 		ni = ieee80211_ref_node(vap->iv_bss);
283 		switch (vap->iv_opmode) {
284 		case IEEE80211_M_MBSS:
285 			error = wtap_beacon_alloc(sc, ni);
286 			if (error != 0)
287 				goto bad;
288 			wtap_beacon_config(sc, vap);
289 			callout_reset(&avp->av_swba, avp->av_bcinterval,
290 			    wtap_beacon_intrp, vap);
291 			break;
292 		default:
293 			goto bad;
294 		}
295 	} else if (nstate == IEEE80211_S_INIT) {
296 		callout_stop(&avp->av_swba);
297 	}
298 	ieee80211_free_node(ni);
299 	return 0;
300 bad:
301 	printf("%s: bad\n", __func__);
302 	ieee80211_free_node(ni);
303 	return error;
304 }
305 
306 static void
307 wtap_bmiss(struct ieee80211vap *vap)
308 {
309 	struct wtap_vap *avp = (struct wtap_vap *)vap;
310 
311 	DWTAP_PRINTF("%s\n", __func__);
312 	avp->av_bmiss(vap);
313 }
314 
315 static struct ieee80211vap *
316 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
317     int unit, enum ieee80211_opmode opmode, int flags,
318     const uint8_t bssid[IEEE80211_ADDR_LEN],
319     const uint8_t mac[IEEE80211_ADDR_LEN])
320 {
321 	 struct wtap_softc *sc = ic->ic_ifp->if_softc;
322 	 struct ieee80211vap *vap;
323 	 struct wtap_vap *avp;
324 	 int error;
325 	struct ieee80211_node *ni;
326 
327 	 DWTAP_PRINTF("%s\n", __func__);
328 
329 	avp = malloc(sizeof(struct wtap_vap), M_80211_VAP, M_NOWAIT | M_ZERO);
330 	if (avp == NULL)
331 		return (NULL);
332 	avp->id = sc->id;
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, mac);
338 	if (error) {
339 		free(avp, M_80211_VAP);
340 		return (NULL);
341 	}
342 
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;
352 
353 	/* complete setup */
354 	ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status);
355 	avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
356 	    "%s", (const char *)ic->ic_ifp->if_xname);
357 
358 	/* TODO this is a hack to force it to choose the rate we want */
359 	ni = ieee80211_ref_node(vap->iv_bss);
360 	ni->ni_txrate = 130;
361 	ieee80211_free_node(ni);
362 	return vap;
363 }
364 
365 static void
366 wtap_vap_delete(struct ieee80211vap *vap)
367 {
368 	struct wtap_vap *avp = WTAP_VAP(vap);
369 
370 	DWTAP_PRINTF("%s\n", __func__);
371 	destroy_dev(avp->av_dev);
372 	callout_stop(&avp->av_swba);
373 	ieee80211_vap_detach(vap);
374 	free((struct wtap_vap*) vap, M_80211_VAP);
375 }
376 
377 /* NB: This function is not used.
378  * I had the problem of the queue
379  * being empty all the time.
380  * Maybe I am setting the queue wrong?
381  */
382 static void
383 wtap_start(struct ifnet *ifp)
384 {
385 	struct ieee80211com *ic = ifp->if_l2com;
386 	struct ifnet *icifp = ic->ic_ifp;
387 	struct wtap_softc *sc = icifp->if_softc;
388 	struct ieee80211_node *ni;
389 	struct mbuf *m;
390 
391 	DWTAP_PRINTF("my_start, with id=%u\n", sc->id);
392 
393 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->up == 0)
394 		return;
395 	for (;;) {
396 		if(IFQ_IS_EMPTY(&ifp->if_snd)){
397 		    printf("queue empty, just trying to see "
398 		        "if the other queue is empty\n");
399 #if 0
400 		    printf("queue for id=1, %u\n",
401 		        IFQ_IS_EMPTY(&global_mscs[1]->ifp->if_snd));
402 		    printf("queue for id=0, %u\n",
403 		        IFQ_IS_EMPTY(&global_mscs[0]->ifp->if_snd));
404 #endif
405 		    break;
406 		}
407 		IFQ_DEQUEUE(&ifp->if_snd, m);
408 		if (m == NULL) {
409 			printf("error dequeueing from ifp->snd\n");
410 			break;
411 		}
412 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
413 		/*
414 		 * Check for fragmentation.  If this frame
415 		 * has been broken up verify we have enough
416 		 * buffers to send all the fragments so all
417 		 * go out or none...
418 		 */
419 #if 0
420 		STAILQ_INIT(&frags);
421 #endif
422 		if ((m->m_flags & M_FRAG)){
423 			printf("dont support frags\n");
424 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
425 			return;
426 		}
427 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
428 		if(wtap_raw_xmit(ni, m, NULL) < 0){
429 			printf("error raw_xmiting\n");
430 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
431 			return;
432 		}
433 	}
434 }
435 
436 static int
437 wtap_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
438 {
439 #if 0
440 	DWTAP_PRINTF("%s\n", __func__);
441 	uprintf("%s, command %lu\n", __func__, cmd);
442 #endif
443 #define	IS_RUNNING(ifp) \
444 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
445 	struct ieee80211com *ic = ifp->if_l2com;
446 	struct wtap_softc *sc = ifp->if_softc;
447 	struct ifreq *ifr = (struct ifreq *)data;
448 	int error = 0;
449 
450 	switch (cmd) {
451 	case SIOCSIFFLAGS:
452 		//printf("%s: %s\n", __func__, "SIOCSIFFLAGS");
453 		if (IS_RUNNING(ifp)) {
454 			DWTAP_PRINTF("running\n");
455 #if 0
456 			/*
457 			 * To avoid rescanning another access point,
458 			 * do not call ath_init() here.  Instead,
459 			 * only reflect promisc mode settings.
460 			 */
461 			//ath_mode_init(sc);
462 #endif
463 			} else if (ifp->if_flags & IFF_UP) {
464 			DWTAP_PRINTF("up\n");
465 			sc->up = 1;
466 #if 0
467 			/*
468 			 * Beware of being called during attach/detach
469 			 * to reset promiscuous mode.  In that case we
470 			 * will still be marked UP but not RUNNING.
471 			 * However trying to re-init the interface
472 			 * is the wrong thing to do as we've already
473 			 * torn down much of our state.  There's
474 			 * probably a better way to deal with this.
475 			 */
476 			//if (!sc->sc_invalid)
477 			//	ath_init(sc);	/* XXX lose error */
478 #endif
479 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
480 			ieee80211_start_all(ic);
481 		} else {
482 			DWTAP_PRINTF("stoping\n");
483 #if 0
484 			ath_stop_locked(ifp);
485 #ifdef notyet
486 			/* XXX must wakeup in places like ath_vap_delete */
487 			if (!sc->sc_invalid)
488 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
489 #endif
490 #endif
491 		}
492 		break;
493 	case SIOCGIFMEDIA:
494 	case SIOCSIFMEDIA:
495 #if 0
496 		DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFMEDIA|SIOCSIFMEDIA");
497 #endif
498 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
499 		break;
500 	case SIOCGIFADDR:
501 #if 0
502 		DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFADDR");
503 #endif
504 		error = ether_ioctl(ifp, cmd, data);
505 		break;
506 	default:
507 		DWTAP_PRINTF("%s: %s [%lu]\n", __func__, "EINVAL", cmd);
508 		error = EINVAL;
509 		break;
510 	}
511 	return error;
512 #undef IS_RUNNING
513 }
514 
515 static void
516 wtap_init(void *arg){
517 
518 	DWTAP_PRINTF("%s\n", __func__);
519 }
520 
521 static void
522 wtap_scan_start(struct ieee80211com *ic)
523 {
524 
525 #if 0
526 	DWTAP_PRINTF("%s\n", __func__);
527 #endif
528 }
529 
530 static void
531 wtap_scan_end(struct ieee80211com *ic)
532 {
533 
534 #if 0
535 	DWTAP_PRINTF("%s\n", __func__);
536 #endif
537 }
538 
539 static void
540 wtap_set_channel(struct ieee80211com *ic)
541 {
542 
543 #if 0
544 	DWTAP_PRINTF("%s\n", __func__);
545 #endif
546 }
547 
548 static int
549 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
550 	const struct ieee80211_bpf_params *params)
551 {
552 #if 0
553 	DWTAP_PRINTF("%s, %p\n", __func__, m);
554 #endif
555 	struct ieee80211vap	*vap = ni->ni_vap;
556 	struct wtap_vap 	*avp = WTAP_VAP(vap);
557 
558 	if (ieee80211_radiotap_active_vap(vap)) {
559 		ieee80211_radiotap_tx(vap, m);
560 	}
561 	if (m->m_flags & M_TXCB)
562 		ieee80211_process_callback(ni, m, 0);
563 	ieee80211_free_node(ni);
564 	return wtap_medium_enqueue(avp, m);
565 }
566 
567 void
568 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
569 {
570       struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
571           M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
572       KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
573       bf->m = m;
574 
575       mtx_lock(&sc->sc_mtx);
576       STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
577       taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
578       mtx_unlock(&sc->sc_mtx);
579 }
580 
581 void
582 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
583 {
584 	struct ifnet *ifp = sc->sc_ifp;
585 	struct ieee80211com *ic = ifp->if_l2com;
586 	struct ieee80211_node *ni;
587 	int type;
588 #if 0
589 	DWTAP_PRINTF("%s\n", __func__);
590 #endif
591 
592 	DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
593 	if (m == NULL) {		/* NB: shouldn't happen */
594 		if_printf(ifp, "%s: no mbuf!\n", __func__);
595 	}
596 
597 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
598 
599 	ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
600 
601 	/*
602 	  * Locate the node for sender, track state, and then
603 	  * pass the (referenced) node up to the 802.11 layer
604 	  * for its use.
605 	  */
606 	ni = ieee80211_find_rxnode_withkey(ic,
607 	    mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
608 	if (ni != NULL) {
609 		/*
610 		 * Sending station is known, dispatch directly.
611 		 */
612 		type = ieee80211_input(ni, m, 1<<7, 10);
613 		ieee80211_free_node(ni);
614 	} else {
615 		type = ieee80211_input_all(ic, m, 1<<7, 10);
616 	}
617 }
618 
619 static void
620 wtap_rx_proc(void *arg, int npending)
621 {
622 	struct wtap_softc *sc = (struct wtap_softc *)arg;
623 	struct ifnet *ifp = sc->sc_ifp;
624 	struct ieee80211com *ic = ifp->if_l2com;
625 	struct mbuf *m;
626 	struct ieee80211_node *ni;
627 	int type;
628 	struct wtap_buf *bf;
629 
630 #if 0
631 	DWTAP_PRINTF("%s\n", __func__);
632 #endif
633 
634 	for(;;) {
635 		mtx_lock(&sc->sc_mtx);
636 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
637 		if (bf == NULL) {
638 			mtx_unlock(&sc->sc_mtx);
639 			return;
640 		}
641 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
642 		mtx_unlock(&sc->sc_mtx);
643 		KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
644 		m = bf->m;
645 		DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
646 		if (m == NULL) {		/* NB: shouldn't happen */
647 			if_printf(ifp, "%s: no mbuf!\n", __func__);
648 			free(bf, M_WTAP_RXBUF);
649 			return;
650 		}
651 
652 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
653 #if 0
654 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
655 #endif
656 
657 		/*
658 		 * Locate the node for sender, track state, and then
659 		 * pass the (referenced) node up to the 802.11 layer
660 		 * for its use.
661 		 */
662 		ni = ieee80211_find_rxnode_withkey(ic,
663 		    mtod(m, const struct ieee80211_frame_min *),
664 		    IEEE80211_KEYIX_NONE);
665 		if (ni != NULL) {
666 			/*
667 			 * Sending station is known, dispatch directly.
668 			 */
669 #if 0
670 			ieee80211_radiotap_rx(ni->ni_vap, m);
671 #endif
672 			type = ieee80211_input(ni, m, 1<<7, 10);
673 			ieee80211_free_node(ni);
674 		} else {
675 #if 0
676 			ieee80211_radiotap_rx_all(ic, m);
677 #endif
678 			type = ieee80211_input_all(ic, m, 1<<7, 10);
679 		}
680 
681 		/* The mbufs are freed by the Net80211 stack */
682 		free(bf, M_WTAP_RXBUF);
683 	}
684 }
685 
686 static void
687 wtap_newassoc(struct ieee80211_node *ni, int isnew)
688 {
689 
690 	DWTAP_PRINTF("%s\n", __func__);
691 }
692 
693 /*
694  * Callback from the 802.11 layer to update WME parameters.
695  */
696 static int
697 wtap_wme_update(struct ieee80211com *ic)
698 {
699 
700 	DWTAP_PRINTF("%s\n", __func__);
701 	return 0;
702 }
703 
704 static void
705 wtap_update_mcast(struct ifnet *ifp)
706 {
707 
708 	DWTAP_PRINTF("%s\n", __func__);
709 }
710 
711 static void
712 wtap_update_promisc(struct ifnet *ifp)
713 {
714 
715 	DWTAP_PRINTF("%s\n", __func__);
716 }
717 
718 static int
719 wtap_if_transmit(struct ifnet *ifp, struct mbuf *m)
720 {
721 	struct ieee80211_node *ni =
722 	    (struct ieee80211_node *) m->m_pkthdr.rcvif;
723 	struct ieee80211vap *vap = ni->ni_vap;
724 	struct wtap_vap *avp = WTAP_VAP(vap);
725 
726 	if(ni == NULL){
727 		printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
728 	}else{
729 		if (ieee80211_radiotap_active_vap(vap))
730 			ieee80211_radiotap_tx(vap, m);
731 	}
732 	if (m->m_flags & M_TXCB)
733 		ieee80211_process_callback(ni, m, 0);
734 	ieee80211_free_node(ni);
735 	return wtap_medium_enqueue(avp, m);
736 }
737 
738 static struct ieee80211_node *
739 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
740 {
741 	struct ieee80211_node *ni;
742 
743 	DWTAP_PRINTF("%s\n", __func__);
744 
745 	ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
746 	    M_NOWAIT|M_ZERO);
747 
748 	ni->ni_txrate = 130;
749 	return ni;
750 }
751 
752 static void
753 wtap_node_free(struct ieee80211_node *ni)
754 {
755 	struct ieee80211com *ic = ni->ni_ic;
756 	struct wtap_softc *sc = ic->ic_ifp->if_softc;
757 
758 	DWTAP_PRINTF("%s\n", __func__);
759 	sc->sc_node_free(ni);
760 }
761 
762 int32_t
763 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
764 {
765 	struct ifnet *ifp;
766 	struct ieee80211com *ic;
767 	char wtap_name[] = {'w','T','a','p',sc->id,
768 	    '_','t','a','s','k','q','\0'};
769 
770 	DWTAP_PRINTF("%s\n", __func__);
771 
772 	ifp = if_alloc(IFT_IEEE80211);
773 	if (ifp == NULL) {
774 		printf("can not if_alloc()\n");
775 		return -1;
776 	}
777 	ic = ifp->if_l2com;
778 	if_initname(ifp, "wtap", sc->id);
779 
780 	sc->sc_ifp = ifp;
781 	sc->up = 0;
782 
783 	STAILQ_INIT(&sc->sc_rxbuf);
784 	sc->sc_tq = taskqueue_create(wtap_name, M_NOWAIT | M_ZERO,
785 	    taskqueue_thread_enqueue, &sc->sc_tq);
786 	taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ",
787 	    ifp->if_xname);
788 	TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
789 
790 	ifp->if_softc = sc;
791 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
792 	ifp->if_start = wtap_start;
793 	ifp->if_ioctl = wtap_ioctl;
794 	ifp->if_init = wtap_init;
795 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
796 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
797 	IFQ_SET_READY(&ifp->if_snd);
798 
799 	ic->ic_ifp = ifp;
800 	ic->ic_phytype = IEEE80211_T_DS;
801 	ic->ic_opmode = IEEE80211_M_MBSS;
802 	ic->ic_caps = IEEE80211_C_MBSS;
803 
804 	ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
805 
806 	ic->ic_regdomain.regdomain = SKU_ETSI;
807 	ic->ic_regdomain.country = CTRY_SWEDEN;
808 	ic->ic_regdomain.location = 1; /* Indoors */
809 	ic->ic_regdomain.isocc[0] = 'S';
810 	ic->ic_regdomain.isocc[1] = 'E';
811 
812 	ic->ic_nchans = 1;
813 	ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
814 	ic->ic_channels[0].ic_freq = 2412;
815 
816 	ieee80211_ifattach(ic, macaddr);
817 
818 #if 0
819 	/* new prototype hook-ups */
820 	msc->if_input = ifp->if_input;
821 	ifp->if_input = myath_if_input;
822 	msc->if_output = ifp->if_output;
823 	ifp->if_output = myath_if_output;
824 #endif
825 	sc->if_transmit = ifp->if_transmit;
826 	ifp->if_transmit = wtap_if_transmit;
827 
828 	/* override default methods */
829 	ic->ic_newassoc = wtap_newassoc;
830 #if 0
831 	ic->ic_updateslot = myath_updateslot;
832 #endif
833 	ic->ic_wme.wme_update = wtap_wme_update;
834 	ic->ic_vap_create = wtap_vap_create;
835 	ic->ic_vap_delete = wtap_vap_delete;
836 	ic->ic_raw_xmit = wtap_raw_xmit;
837 	ic->ic_update_mcast = wtap_update_mcast;
838 	ic->ic_update_promisc = wtap_update_promisc;
839 
840 	sc->sc_node_alloc = ic->ic_node_alloc;
841 	ic->ic_node_alloc = wtap_node_alloc;
842 	sc->sc_node_free = ic->ic_node_free;
843 	ic->ic_node_free = wtap_node_free;
844 
845 #if 0
846 	ic->ic_node_getsignal = myath_node_getsignal;
847 #endif
848 	ic->ic_scan_start = wtap_scan_start;
849 	ic->ic_scan_end = wtap_scan_end;
850 	ic->ic_set_channel = wtap_set_channel;
851 
852 	ieee80211_radiotap_attach(ic,
853 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
854 	    WTAP_TX_RADIOTAP_PRESENT,
855 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
856 	    WTAP_RX_RADIOTAP_PRESENT);
857 
858 	/* Work here, we must find a way to populate the rate table */
859 #if 0
860 	if(ic->ic_rt == NULL){
861 		printf("no table for ic_curchan\n");
862 		ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
863 	}
864 	printf("ic->ic_rt =%p\n", ic->ic_rt);
865 	printf("rate count %d\n", ic->ic_rt->rateCount);
866 
867 	uint8_t code = ic->ic_rt->info[0].dot11Rate;
868 	uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
869 	uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
870 	printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
871 
872 	uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
873 	uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
874 	uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
875 	uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
876 	printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
877 	printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
878 	printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
879 #endif
880 	return 0;
881 }
882 
883 int32_t
884 wtap_detach(struct wtap_softc *sc)
885 {
886 	struct ifnet *ifp = sc->sc_ifp;
887 	struct ieee80211com *ic = ifp->if_l2com;
888 
889 	DWTAP_PRINTF("%s\n", __func__);
890 	ieee80211_ageq_drain(&ic->ic_stageq);
891 	ieee80211_ifdetach(ic);
892 	if_free(ifp);
893 	return 0;
894 }
895 
896 void
897 wtap_resume(struct wtap_softc *sc)
898 {
899 
900 	DWTAP_PRINTF("%s\n", __func__);
901 }
902 
903 void
904 wtap_suspend(struct wtap_softc *sc)
905 {
906 
907 	DWTAP_PRINTF("%s\n", __func__);
908 }
909 
910 void
911 wtap_shutdown(struct wtap_softc *sc)
912 {
913 
914 	DWTAP_PRINTF("%s\n", __func__);
915 }
916 
917 void
918 wtap_intr(struct wtap_softc *sc)
919 {
920 
921 	DWTAP_PRINTF("%s\n", __func__);
922 }
923