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