xref: /freebsd/sys/dev/wtap/if_wtap.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
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, const struct ieee80211_rx_stats *stats, 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, stats, 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 = &vap->iv_bcn_off;
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);
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, 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_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_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_WAITOK | M_ZERO);
330 	avp->id = sc->id;
331 	avp->av_md = sc->sc_md;
332 	avp->av_bcinterval = msecs_to_ticks(BEACON_INTRERVAL + 100*sc->id);
333 	vap = (struct ieee80211vap *) avp;
334 	error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
335 	    flags | IEEE80211_CLONE_NOBEACONS, bssid);
336 	if (error) {
337 		free(avp, M_80211_VAP);
338 		return (NULL);
339 	}
340 
341 	/* override various methods */
342 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
343 	vap->iv_recv_mgmt = wtap_recv_mgmt;
344 	vap->iv_reset = wtap_reset_vap;
345 	vap->iv_update_beacon = wtap_beacon_update;
346 	avp->av_newstate = vap->iv_newstate;
347 	vap->iv_newstate = wtap_newstate;
348 	avp->av_bmiss = vap->iv_bmiss;
349 	vap->iv_bmiss = wtap_bmiss;
350 
351 	/* complete setup */
352 	ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status,
353 	    mac);
354 	avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
355 	    "%s", (const char *)sc->name);
356 
357 	/* TODO this is a hack to force it to choose the rate we want */
358 	ni = ieee80211_ref_node(vap->iv_bss);
359 	ni->ni_txrate = 130;
360 	ieee80211_free_node(ni);
361 	return vap;
362 }
363 
364 static void
365 wtap_vap_delete(struct ieee80211vap *vap)
366 {
367 	struct wtap_vap *avp = WTAP_VAP(vap);
368 
369 	DWTAP_PRINTF("%s\n", __func__);
370 	destroy_dev(avp->av_dev);
371 	callout_stop(&avp->av_swba);
372 	ieee80211_vap_detach(vap);
373 	free((struct wtap_vap*) vap, M_80211_VAP);
374 }
375 
376 static void
377 wtap_parent(struct ieee80211com *ic)
378 {
379 	struct wtap_softc *sc = ic->ic_softc;
380 
381 	if (ic->ic_nrunning > 0) {
382 		sc->up = 1;
383 		ieee80211_start_all(ic);
384 	} else
385 		sc->up = 0;
386 }
387 
388 static void
389 wtap_scan_start(struct ieee80211com *ic)
390 {
391 
392 #if 0
393 	DWTAP_PRINTF("%s\n", __func__);
394 #endif
395 }
396 
397 static void
398 wtap_scan_end(struct ieee80211com *ic)
399 {
400 
401 #if 0
402 	DWTAP_PRINTF("%s\n", __func__);
403 #endif
404 }
405 
406 static void
407 wtap_set_channel(struct ieee80211com *ic)
408 {
409 
410 #if 0
411 	DWTAP_PRINTF("%s\n", __func__);
412 #endif
413 }
414 
415 static int
416 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
417 	const struct ieee80211_bpf_params *params)
418 {
419 #if 0
420 	DWTAP_PRINTF("%s, %p\n", __func__, m);
421 #endif
422 	struct ieee80211vap	*vap = ni->ni_vap;
423 	struct wtap_vap 	*avp = WTAP_VAP(vap);
424 
425 	if (ieee80211_radiotap_active_vap(vap)) {
426 		ieee80211_radiotap_tx(vap, m);
427 	}
428 	if (m->m_flags & M_TXCB)
429 		ieee80211_process_callback(ni, m, 0);
430 	ieee80211_free_node(ni);
431 	return wtap_medium_enqueue(avp, m);
432 }
433 
434 void
435 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
436 {
437       struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
438           M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
439       KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
440       bf->m = m;
441 
442       mtx_lock(&sc->sc_mtx);
443       STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
444       taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
445       mtx_unlock(&sc->sc_mtx);
446 }
447 
448 void
449 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
450 {
451 	struct ieee80211com *ic = &sc->sc_ic;
452 	struct ieee80211_node *ni;
453 	int type;
454 #if 0
455 	DWTAP_PRINTF("%s\n", __func__);
456 #endif
457 
458 	DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
459 	if (m == NULL) {		/* NB: shouldn't happen */
460 		ic_printf(ic, "%s: no mbuf!\n", __func__);
461 	}
462 
463 	ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
464 
465 	/*
466 	  * Locate the node for sender, track state, and then
467 	  * pass the (referenced) node up to the 802.11 layer
468 	  * for its use.
469 	  */
470 	ni = ieee80211_find_rxnode_withkey(ic,
471 	    mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
472 	if (ni != NULL) {
473 		/*
474 		 * Sending station is known, dispatch directly.
475 		 */
476 		type = ieee80211_input(ni, m, 1<<7, 10);
477 		ieee80211_free_node(ni);
478 	} else {
479 		type = ieee80211_input_all(ic, m, 1<<7, 10);
480 	}
481 }
482 
483 static void
484 wtap_rx_proc(void *arg, int npending)
485 {
486 	struct wtap_softc *sc = (struct wtap_softc *)arg;
487 	struct ieee80211com *ic = &sc->sc_ic;
488 	struct mbuf *m;
489 	struct ieee80211_node *ni;
490 	int type;
491 	struct wtap_buf *bf;
492 
493 #if 0
494 	DWTAP_PRINTF("%s\n", __func__);
495 #endif
496 
497 	for(;;) {
498 		mtx_lock(&sc->sc_mtx);
499 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
500 		if (bf == NULL) {
501 			mtx_unlock(&sc->sc_mtx);
502 			return;
503 		}
504 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
505 		mtx_unlock(&sc->sc_mtx);
506 		KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
507 		m = bf->m;
508 		DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
509 		if (m == NULL) {		/* NB: shouldn't happen */
510 			ic_printf(ic, "%s: no mbuf!\n", __func__);
511 			free(bf, M_WTAP_RXBUF);
512 			return;
513 		}
514 #if 0
515 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
516 #endif
517 
518 		/*
519 		 * Locate the node for sender, track state, and then
520 		 * pass the (referenced) node up to the 802.11 layer
521 		 * for its use.
522 		 */
523 		ni = ieee80211_find_rxnode_withkey(ic,
524 		    mtod(m, const struct ieee80211_frame_min *),
525 		    IEEE80211_KEYIX_NONE);
526 		if (ni != NULL) {
527 			/*
528 			 * Sending station is known, dispatch directly.
529 			 */
530 #if 0
531 			ieee80211_radiotap_rx(ni->ni_vap, m);
532 #endif
533 			type = ieee80211_input(ni, m, 1<<7, 10);
534 			ieee80211_free_node(ni);
535 		} else {
536 #if 0
537 			ieee80211_radiotap_rx_all(ic, m);
538 #endif
539 			type = ieee80211_input_all(ic, m, 1<<7, 10);
540 		}
541 
542 		/* The mbufs are freed by the Net80211 stack */
543 		free(bf, M_WTAP_RXBUF);
544 	}
545 }
546 
547 static void
548 wtap_newassoc(struct ieee80211_node *ni, int isnew)
549 {
550 
551 	DWTAP_PRINTF("%s\n", __func__);
552 }
553 
554 /*
555  * Callback from the 802.11 layer to update WME parameters.
556  */
557 static int
558 wtap_wme_update(struct ieee80211com *ic)
559 {
560 
561 	DWTAP_PRINTF("%s\n", __func__);
562 	return 0;
563 }
564 
565 static void
566 wtap_update_mcast(struct ieee80211com *ic)
567 {
568 
569 	DWTAP_PRINTF("%s\n", __func__);
570 }
571 
572 static void
573 wtap_update_promisc(struct ieee80211com *ic)
574 {
575 
576 	DWTAP_PRINTF("%s\n", __func__);
577 }
578 
579 static int
580 wtap_transmit(struct ieee80211com *ic, struct mbuf *m)
581 {
582 	struct ieee80211_node *ni =
583 	    (struct ieee80211_node *) m->m_pkthdr.rcvif;
584 	struct ieee80211vap *vap = ni->ni_vap;
585 	struct wtap_vap *avp = WTAP_VAP(vap);
586 
587 	if(ni == NULL){
588 		printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
589 	}else{
590 		if (ieee80211_radiotap_active_vap(vap))
591 			ieee80211_radiotap_tx(vap, m);
592 	}
593 	if (m->m_flags & M_TXCB)
594 		ieee80211_process_callback(ni, m, 0);
595 	ieee80211_free_node(ni);
596 	return wtap_medium_enqueue(avp, m);
597 }
598 
599 static struct ieee80211_node *
600 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
601 {
602 	struct ieee80211_node *ni;
603 
604 	DWTAP_PRINTF("%s\n", __func__);
605 
606 	ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
607 	    M_NOWAIT|M_ZERO);
608 
609 	ni->ni_txrate = 130;
610 	return ni;
611 }
612 
613 static void
614 wtap_node_free(struct ieee80211_node *ni)
615 {
616 	struct ieee80211com *ic = ni->ni_ic;
617 	struct wtap_softc *sc = ic->ic_softc;
618 
619 	DWTAP_PRINTF("%s\n", __func__);
620 	sc->sc_node_free(ni);
621 }
622 
623 int32_t
624 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
625 {
626 	struct ieee80211com *ic = &sc->sc_ic;
627 
628 	DWTAP_PRINTF("%s\n", __func__);
629 
630 	sc->up = 0;
631 	STAILQ_INIT(&sc->sc_rxbuf);
632 	sc->sc_tq = taskqueue_create("wtap_taskq", M_NOWAIT | M_ZERO,
633 	    taskqueue_thread_enqueue, &sc->sc_tq);
634 	taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ", sc->name);
635 	TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
636 
637 	ic->ic_softc = sc;
638 	ic->ic_name = sc->name;
639 	ic->ic_phytype = IEEE80211_T_DS;
640 	ic->ic_opmode = IEEE80211_M_MBSS;
641 	ic->ic_caps = IEEE80211_C_MBSS;
642 
643 	ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
644 
645 	ic->ic_regdomain.regdomain = SKU_ETSI;
646 	ic->ic_regdomain.country = CTRY_SWEDEN;
647 	ic->ic_regdomain.location = 1; /* Indoors */
648 	ic->ic_regdomain.isocc[0] = 'S';
649 	ic->ic_regdomain.isocc[1] = 'E';
650 
651 	ic->ic_nchans = 1;
652 	ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
653 	ic->ic_channels[0].ic_freq = 2412;
654 
655 	IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
656 	ieee80211_ifattach(ic);
657 
658 	/* override default methods */
659 	ic->ic_newassoc = wtap_newassoc;
660 	ic->ic_wme.wme_update = wtap_wme_update;
661 	ic->ic_vap_create = wtap_vap_create;
662 	ic->ic_vap_delete = wtap_vap_delete;
663 	ic->ic_raw_xmit = wtap_raw_xmit;
664 	ic->ic_update_mcast = wtap_update_mcast;
665 	ic->ic_update_promisc = wtap_update_promisc;
666 	ic->ic_transmit = wtap_transmit;
667 	ic->ic_parent = wtap_parent;
668 
669 	sc->sc_node_alloc = ic->ic_node_alloc;
670 	ic->ic_node_alloc = wtap_node_alloc;
671 	sc->sc_node_free = ic->ic_node_free;
672 	ic->ic_node_free = wtap_node_free;
673 
674 	ic->ic_scan_start = wtap_scan_start;
675 	ic->ic_scan_end = wtap_scan_end;
676 	ic->ic_set_channel = wtap_set_channel;
677 
678 	ieee80211_radiotap_attach(ic,
679 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
680 	    WTAP_TX_RADIOTAP_PRESENT,
681 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
682 	    WTAP_RX_RADIOTAP_PRESENT);
683 
684 	/* Work here, we must find a way to populate the rate table */
685 #if 0
686 	if(ic->ic_rt == NULL){
687 		printf("no table for ic_curchan\n");
688 		ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
689 	}
690 	printf("ic->ic_rt =%p\n", ic->ic_rt);
691 	printf("rate count %d\n", ic->ic_rt->rateCount);
692 
693 	uint8_t code = ic->ic_rt->info[0].dot11Rate;
694 	uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
695 	uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
696 	printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
697 
698 	uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
699 	uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
700 	uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
701 	uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
702 	printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
703 	printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
704 	printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
705 #endif
706 	return 0;
707 }
708 
709 int32_t
710 wtap_detach(struct wtap_softc *sc)
711 {
712 	struct ieee80211com *ic = &sc->sc_ic;
713 
714 	DWTAP_PRINTF("%s\n", __func__);
715 	ieee80211_ageq_drain(&ic->ic_stageq);
716 	ieee80211_ifdetach(ic);
717 	return 0;
718 }
719 
720 void
721 wtap_resume(struct wtap_softc *sc)
722 {
723 
724 	DWTAP_PRINTF("%s\n", __func__);
725 }
726 
727 void
728 wtap_suspend(struct wtap_softc *sc)
729 {
730 
731 	DWTAP_PRINTF("%s\n", __func__);
732 }
733 
734 void
735 wtap_shutdown(struct wtap_softc *sc)
736 {
737 
738 	DWTAP_PRINTF("%s\n", __func__);
739 }
740 
741 void
742 wtap_intr(struct wtap_softc *sc)
743 {
744 
745 	DWTAP_PRINTF("%s\n", __func__);
746 }
747