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