xref: /freebsd/sys/dev/wtap/if_wtap.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
5  * All rights reserved.
6  *
7  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
18  *    redistribution must be conditioned upon including a substantially
19  *    similar Disclaimer requirement for further binary redistribution.
20  *
21  * NO WARRANTY
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
27  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * $FreeBSD$
35  */
36 #include "if_wtapvar.h"
37 #include <sys/uio.h>    /* uio struct */
38 #include <sys/jail.h>
39 #include <net/if_var.h>
40 #include <net/vnet.h>
41 
42 #include <net80211/ieee80211_ratectl.h>
43 #include "if_medium.h"
44 
45 /*
46  * This _requires_ vimage to be useful.
47  */
48 #ifndef	VIMAGE
49 #error	if_wtap requires VIMAGE.
50 #endif	/* VIMAGE */
51 
52 /* device for IOCTL and read/write for debuggin purposes */
53 /* Function prototypes */
54 static	d_open_t	wtap_node_open;
55 static	d_close_t	wtap_node_close;
56 static	d_write_t	wtap_node_write;
57 static	d_ioctl_t	wtap_node_ioctl;
58 
59 static struct cdevsw wtap_cdevsw = {
60 	.d_version =	D_VERSION,
61 	.d_flags =	0,
62 	.d_open = 	wtap_node_open,
63 	.d_close = 	wtap_node_close,
64 	.d_write = 	wtap_node_write,
65 	.d_ioctl =	wtap_node_ioctl,
66 	.d_name =	"wtapnode",
67 };
68 
69 static int
70 wtap_node_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
71 {
72 
73 	int err = 0;
74 	uprintf("Opened device \"echo\" successfully.\n");
75 	return(err);
76 }
77 
78 static int
79 wtap_node_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
80 {
81 
82 	uprintf("Closing device \"echo.\"\n");
83 	return(0);
84 }
85 
86 static int
87 wtap_node_write(struct cdev *dev, struct uio *uio, int ioflag)
88 {
89 	int err = 0;
90 	struct mbuf *m;
91 	struct ifnet *ifp;
92 	struct wtap_softc *sc;
93 	uint8_t buf[1024];
94 	struct epoch_tracker et;
95 	int buf_len;
96 
97 	uprintf("write device %s \"echo.\"\n", devtoname(dev));
98 	buf_len = MIN(uio->uio_iov->iov_len, 1024);
99 	err = copyin(uio->uio_iov->iov_base, buf, buf_len);
100 
101 	if (err != 0) {
102 		uprintf("Write failed: bad address!\n");
103 		return (err);
104 	}
105 
106 	MGETHDR(m, M_NOWAIT, MT_DATA);
107 	m_copyback(m, 0, buf_len, buf);
108 
109 	CURVNET_SET(TD_TO_VNET(curthread));
110 	NET_EPOCH_ENTER(et);
111 
112 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
113 		printf("ifp->if_xname = %s\n", ifp->if_xname);
114 		if(strcmp(devtoname(dev), ifp->if_xname) == 0){
115 			printf("found match, correspoding wtap = %s\n",
116 			    ifp->if_xname);
117 			sc = (struct wtap_softc *)ifp->if_softc;
118 			printf("wtap id = %d\n", sc->id);
119 			wtap_inject(sc, m);
120 		}
121 	}
122 
123 	NET_EPOCH_EXIT(et);
124 	CURVNET_RESTORE();
125 
126 	return(err);
127 }
128 
129 int
130 wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
131     int fflag, struct thread *td)
132 {
133 	int error = 0;
134 
135 	switch(cmd) {
136 	default:
137 		DWTAP_PRINTF("Unknown WTAP IOCTL\n");
138 		error = EINVAL;
139 	}
140 	return error;
141 }
142 
143 static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
144 	const struct ieee80211_bpf_params *params);
145 
146 static int
147 wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
148 {
149 
150 	return medium_transmit(avp->av_md, avp->id, m);
151 }
152 
153 static int
154 wtap_media_change(struct ifnet *ifp)
155 {
156 
157 	DWTAP_PRINTF("%s\n", __func__);
158 	int error = ieee80211_media_change(ifp);
159 	/* NB: only the fixed rate can change and that doesn't need a reset */
160 	return (error == ENETRESET ? 0 : error);
161 }
162 
163 /*
164  * Intercept management frames to collect beacon rssi data
165  * and to do ibss merges.
166  */
167 static void
168 wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
169     int subtype, const struct ieee80211_rx_stats *stats, int rssi, int nf)
170 {
171 	struct ieee80211vap *vap = ni->ni_vap;
172 #if 0
173 	DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
174 #endif
175 	WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, stats, rssi, nf);
176 }
177 
178 static int
179 wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
180 {
181 
182 	DWTAP_PRINTF("%s\n", __func__);
183 	return 0;
184 }
185 
186 static void
187 wtap_beacon_update(struct ieee80211vap *vap, int item)
188 {
189 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
190 
191 	DWTAP_PRINTF("%s\n", __func__);
192 	setbit(bo->bo_flags, item);
193 }
194 
195 /*
196  * Allocate and setup an initial beacon frame.
197  */
198 static int
199 wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
200 {
201 	struct ieee80211vap *vap = ni->ni_vap;
202 	struct wtap_vap *avp = WTAP_VAP(vap);
203 
204 	DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
205 
206 	/*
207 	 * NB: the beacon data buffer must be 32-bit aligned;
208 	 * we assume the mbuf routines will return us something
209 	 * with this alignment (perhaps should assert).
210 	 */
211 	avp->beacon = ieee80211_beacon_alloc(ni);
212 	if (avp->beacon == NULL) {
213 		printf("%s: cannot get mbuf\n", __func__);
214 		return ENOMEM;
215 	}
216 	callout_init(&avp->av_swba, 0);
217 	avp->bf_node = ieee80211_ref_node(ni);
218 
219 	return 0;
220 }
221 
222 static void
223 wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
224 {
225 
226 	DWTAP_PRINTF("%s\n", __func__);
227 }
228 
229 static void
230 wtap_beacon_intrp(void *arg)
231 {
232 	struct wtap_vap *avp = arg;
233 	struct ieee80211vap *vap = arg;
234 	struct mbuf *m;
235 
236 	if (vap->iv_state < IEEE80211_S_RUN) {
237 	    DWTAP_PRINTF("Skip beacon, not running, state %d", vap->iv_state);
238 	    return ;
239 	}
240 	DWTAP_PRINTF("[%d] beacon intrp\n", avp->id);	//burst mode
241 	/*
242 	 * Update dynamic beacon contents.  If this returns
243 	 * non-zero then we need to remap the memory because
244 	 * the beacon frame changed size (probably because
245 	 * of the TIM bitmap).
246 	 */
247 	m = m_dup(avp->beacon, M_NOWAIT);
248 	if (ieee80211_beacon_update(avp->bf_node, m, 0)) {
249 		printf("%s, need to remap the memory because the beacon frame"
250 		    " changed size.\n",__func__);
251 	}
252 
253 	if (ieee80211_radiotap_active_vap(vap))
254 	    ieee80211_radiotap_tx(vap, m);
255 
256 #if 0
257 	medium_transmit(avp->av_md, avp->id, m);
258 #endif
259 	wtap_medium_enqueue(avp, m);
260 	callout_schedule(&avp->av_swba, avp->av_bcinterval);
261 }
262 
263 static int
264 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
265 {
266 	struct ieee80211com *ic = vap->iv_ic;
267 	struct wtap_softc *sc = ic->ic_softc;
268 	struct wtap_vap *avp = WTAP_VAP(vap);
269 	struct ieee80211_node *ni = NULL;
270 	int error;
271 
272 	DWTAP_PRINTF("%s\n", __func__);
273 
274 	ni = ieee80211_ref_node(vap->iv_bss);
275 	/*
276 	 * Invoke the parent method to do net80211 work.
277 	 */
278 	error = avp->av_newstate(vap, nstate, arg);
279 	if (error != 0)
280 		goto bad;
281 
282 	if (nstate == IEEE80211_S_RUN) {
283 		/* NB: collect bss node again, it may have changed */
284 		ieee80211_free_node(ni);
285 		ni = ieee80211_ref_node(vap->iv_bss);
286 		switch (vap->iv_opmode) {
287 		case IEEE80211_M_MBSS:
288 			error = wtap_beacon_alloc(sc, ni);
289 			if (error != 0)
290 				goto bad;
291 			wtap_beacon_config(sc, vap);
292 			callout_reset(&avp->av_swba, avp->av_bcinterval,
293 			    wtap_beacon_intrp, vap);
294 			break;
295 		default:
296 			goto bad;
297 		}
298 	} else if (nstate == IEEE80211_S_INIT) {
299 		callout_stop(&avp->av_swba);
300 	}
301 	ieee80211_free_node(ni);
302 	return 0;
303 bad:
304 	printf("%s: bad\n", __func__);
305 	ieee80211_free_node(ni);
306 	return error;
307 }
308 
309 static void
310 wtap_bmiss(struct ieee80211vap *vap)
311 {
312 	struct wtap_vap *avp = (struct wtap_vap *)vap;
313 
314 	DWTAP_PRINTF("%s\n", __func__);
315 	avp->av_bmiss(vap);
316 }
317 
318 static struct ieee80211vap *
319 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
320     int unit, enum ieee80211_opmode opmode, int flags,
321     const uint8_t bssid[IEEE80211_ADDR_LEN],
322     const uint8_t mac[IEEE80211_ADDR_LEN])
323 {
324 	 struct wtap_softc *sc = ic->ic_softc;
325 	 struct ieee80211vap *vap;
326 	 struct wtap_vap *avp;
327 	 int error;
328 	struct ieee80211_node *ni;
329 
330 	 DWTAP_PRINTF("%s\n", __func__);
331 
332 	avp = malloc(sizeof(struct wtap_vap), M_80211_VAP, M_WAITOK | M_ZERO);
333 	avp->id = sc->id;
334 	avp->av_md = sc->sc_md;
335 	avp->av_bcinterval = msecs_to_ticks(BEACON_INTRERVAL + 100*sc->id);
336 	vap = (struct ieee80211vap *) avp;
337 	error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
338 	    flags | IEEE80211_CLONE_NOBEACONS, bssid);
339 	if (error) {
340 		free(avp, M_80211_VAP);
341 		return (NULL);
342 	}
343 
344 	/* override various methods */
345 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
346 	vap->iv_recv_mgmt = wtap_recv_mgmt;
347 	vap->iv_reset = wtap_reset_vap;
348 	vap->iv_update_beacon = wtap_beacon_update;
349 	avp->av_newstate = vap->iv_newstate;
350 	vap->iv_newstate = wtap_newstate;
351 	avp->av_bmiss = vap->iv_bmiss;
352 	vap->iv_bmiss = wtap_bmiss;
353 
354 	/* complete setup */
355 	ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status,
356 	    mac);
357 	avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
358 	    "%s", (const char *)sc->name);
359 
360 	/* TODO this is a hack to force it to choose the rate we want */
361 	ni = ieee80211_ref_node(vap->iv_bss);
362 	ni->ni_txrate = 130;
363 	ieee80211_free_node(ni);
364 	return vap;
365 }
366 
367 static void
368 wtap_vap_delete(struct ieee80211vap *vap)
369 {
370 	struct wtap_vap *avp = WTAP_VAP(vap);
371 
372 	DWTAP_PRINTF("%s\n", __func__);
373 	destroy_dev(avp->av_dev);
374 	callout_stop(&avp->av_swba);
375 	ieee80211_vap_detach(vap);
376 	free(avp, M_80211_VAP);
377 }
378 
379 static void
380 wtap_parent(struct ieee80211com *ic)
381 {
382 	struct wtap_softc *sc = ic->ic_softc;
383 
384 	if (ic->ic_nrunning > 0) {
385 		sc->up = 1;
386 		ieee80211_start_all(ic);
387 	} else
388 		sc->up = 0;
389 }
390 
391 static void
392 wtap_scan_start(struct ieee80211com *ic)
393 {
394 
395 #if 0
396 	DWTAP_PRINTF("%s\n", __func__);
397 #endif
398 }
399 
400 static void
401 wtap_scan_end(struct ieee80211com *ic)
402 {
403 
404 #if 0
405 	DWTAP_PRINTF("%s\n", __func__);
406 #endif
407 }
408 
409 static void
410 wtap_set_channel(struct ieee80211com *ic)
411 {
412 
413 #if 0
414 	DWTAP_PRINTF("%s\n", __func__);
415 #endif
416 }
417 
418 static int
419 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
420 	const struct ieee80211_bpf_params *params)
421 {
422 #if 0
423 	DWTAP_PRINTF("%s, %p\n", __func__, m);
424 #endif
425 	struct ieee80211vap	*vap = ni->ni_vap;
426 	struct wtap_vap 	*avp = WTAP_VAP(vap);
427 
428 	if (ieee80211_radiotap_active_vap(vap)) {
429 		ieee80211_radiotap_tx(vap, m);
430 	}
431 	if (m->m_flags & M_TXCB)
432 		ieee80211_process_callback(ni, m, 0);
433 	ieee80211_free_node(ni);
434 	return wtap_medium_enqueue(avp, m);
435 }
436 
437 void
438 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
439 {
440       struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
441           M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
442       KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
443       bf->m = m;
444 
445       mtx_lock(&sc->sc_mtx);
446       STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
447       taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
448       mtx_unlock(&sc->sc_mtx);
449 }
450 
451 void
452 wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
453 {
454 	struct ieee80211com *ic = &sc->sc_ic;
455 	struct ieee80211_node *ni;
456 	int type;
457 #if 0
458 	DWTAP_PRINTF("%s\n", __func__);
459 #endif
460 
461 	DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
462 	if (m == NULL) {		/* NB: shouldn't happen */
463 		ic_printf(ic, "%s: no mbuf!\n", __func__);
464 	}
465 
466 	ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
467 
468 	/*
469 	  * Locate the node for sender, track state, and then
470 	  * pass the (referenced) node up to the 802.11 layer
471 	  * for its use.
472 	  */
473 	ni = ieee80211_find_rxnode_withkey(ic,
474 	    mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
475 	if (ni != NULL) {
476 		/*
477 		 * Sending station is known, dispatch directly.
478 		 */
479 		type = ieee80211_input(ni, m, 1<<7, 10);
480 		ieee80211_free_node(ni);
481 	} else {
482 		type = ieee80211_input_all(ic, m, 1<<7, 10);
483 	}
484 }
485 
486 static void
487 wtap_rx_proc(void *arg, int npending)
488 {
489 	struct wtap_softc *sc = (struct wtap_softc *)arg;
490 	struct ieee80211com *ic = &sc->sc_ic;
491 	struct mbuf *m;
492 	struct ieee80211_node *ni;
493 	int type;
494 	struct wtap_buf *bf;
495 
496 #if 0
497 	DWTAP_PRINTF("%s\n", __func__);
498 #endif
499 
500 	for(;;) {
501 		mtx_lock(&sc->sc_mtx);
502 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
503 		if (bf == NULL) {
504 			mtx_unlock(&sc->sc_mtx);
505 			return;
506 		}
507 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
508 		mtx_unlock(&sc->sc_mtx);
509 		KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
510 		m = bf->m;
511 		DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
512 		if (m == NULL) {		/* NB: shouldn't happen */
513 			ic_printf(ic, "%s: no mbuf!\n", __func__);
514 			free(bf, M_WTAP_RXBUF);
515 			return;
516 		}
517 #if 0
518 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
519 #endif
520 
521 		/*
522 		 * Locate the node for sender, track state, and then
523 		 * pass the (referenced) node up to the 802.11 layer
524 		 * for its use.
525 		 */
526 		ni = ieee80211_find_rxnode_withkey(ic,
527 		    mtod(m, const struct ieee80211_frame_min *),
528 		    IEEE80211_KEYIX_NONE);
529 		if (ni != NULL) {
530 			/*
531 			 * Sending station is known, dispatch directly.
532 			 */
533 			type = ieee80211_input(ni, m, 1<<7, 10);
534 			ieee80211_free_node(ni);
535 		} else {
536 			type = ieee80211_input_all(ic, m, 1<<7, 10);
537 		}
538 
539 		/* The mbufs are freed by the Net80211 stack */
540 		free(bf, M_WTAP_RXBUF);
541 	}
542 }
543 
544 static void
545 wtap_newassoc(struct ieee80211_node *ni, int isnew)
546 {
547 
548 	DWTAP_PRINTF("%s\n", __func__);
549 }
550 
551 /*
552  * Callback from the 802.11 layer to update WME parameters.
553  */
554 static int
555 wtap_wme_update(struct ieee80211com *ic)
556 {
557 
558 	DWTAP_PRINTF("%s\n", __func__);
559 	return 0;
560 }
561 
562 static void
563 wtap_update_mcast(struct ieee80211com *ic)
564 {
565 
566 	DWTAP_PRINTF("%s\n", __func__);
567 }
568 
569 static void
570 wtap_update_promisc(struct ieee80211com *ic)
571 {
572 
573 	DWTAP_PRINTF("%s\n", __func__);
574 }
575 
576 static int
577 wtap_transmit(struct ieee80211com *ic, struct mbuf *m)
578 {
579 	struct ieee80211_node *ni =
580 	    (struct ieee80211_node *) m->m_pkthdr.rcvif;
581 	struct ieee80211vap *vap = ni->ni_vap;
582 	struct wtap_vap *avp = WTAP_VAP(vap);
583 
584 	if(ni == NULL){
585 		printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
586 	}else{
587 		if (ieee80211_radiotap_active_vap(vap))
588 			ieee80211_radiotap_tx(vap, m);
589 	}
590 	if (m->m_flags & M_TXCB)
591 		ieee80211_process_callback(ni, m, 0);
592 	ieee80211_free_node(ni);
593 	return wtap_medium_enqueue(avp, m);
594 }
595 
596 static struct ieee80211_node *
597 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
598 {
599 	struct ieee80211_node *ni;
600 
601 	DWTAP_PRINTF("%s\n", __func__);
602 
603 	ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
604 	    M_NOWAIT|M_ZERO);
605 	if (ni == NULL)
606 		return (NULL);
607 
608 	ni->ni_txrate = 130;
609 	return ni;
610 }
611 
612 static void
613 wtap_node_free(struct ieee80211_node *ni)
614 {
615 	struct ieee80211com *ic = ni->ni_ic;
616 	struct wtap_softc *sc = ic->ic_softc;
617 
618 	DWTAP_PRINTF("%s\n", __func__);
619 	sc->sc_node_free(ni);
620 }
621 
622 int32_t
623 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
624 {
625 	struct ieee80211com *ic = &sc->sc_ic;
626 
627 	DWTAP_PRINTF("%s\n", __func__);
628 
629 	sc->up = 0;
630 	STAILQ_INIT(&sc->sc_rxbuf);
631 	sc->sc_tq = taskqueue_create("wtap_taskq", M_NOWAIT | M_ZERO,
632 	    taskqueue_thread_enqueue, &sc->sc_tq);
633 	taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ", sc->name);
634 	TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
635 
636 	ic->ic_softc = sc;
637 	ic->ic_name = sc->name;
638 	ic->ic_phytype = IEEE80211_T_DS;
639 	ic->ic_opmode = IEEE80211_M_MBSS;
640 	ic->ic_caps = IEEE80211_C_MBSS;
641 
642 	ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
643 
644 	ic->ic_regdomain.regdomain = SKU_ETSI;
645 	ic->ic_regdomain.country = CTRY_SWEDEN;
646 	ic->ic_regdomain.location = 1; /* Indoors */
647 	ic->ic_regdomain.isocc[0] = 'S';
648 	ic->ic_regdomain.isocc[1] = 'E';
649 
650 	ic->ic_nchans = 1;
651 	ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
652 	ic->ic_channels[0].ic_freq = 2412;
653 
654 	IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
655 	ieee80211_ifattach(ic);
656 
657 	/* override default methods */
658 	ic->ic_newassoc = wtap_newassoc;
659 	ic->ic_wme.wme_update = wtap_wme_update;
660 	ic->ic_vap_create = wtap_vap_create;
661 	ic->ic_vap_delete = wtap_vap_delete;
662 	ic->ic_raw_xmit = wtap_raw_xmit;
663 	ic->ic_update_mcast = wtap_update_mcast;
664 	ic->ic_update_promisc = wtap_update_promisc;
665 	ic->ic_transmit = wtap_transmit;
666 	ic->ic_parent = wtap_parent;
667 
668 	sc->sc_node_alloc = ic->ic_node_alloc;
669 	ic->ic_node_alloc = wtap_node_alloc;
670 	sc->sc_node_free = ic->ic_node_free;
671 	ic->ic_node_free = wtap_node_free;
672 
673 	ic->ic_scan_start = wtap_scan_start;
674 	ic->ic_scan_end = wtap_scan_end;
675 	ic->ic_set_channel = wtap_set_channel;
676 
677 	ieee80211_radiotap_attach(ic,
678 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
679 	    WTAP_TX_RADIOTAP_PRESENT,
680 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
681 	    WTAP_RX_RADIOTAP_PRESENT);
682 
683 	/* Work here, we must find a way to populate the rate table */
684 #if 0
685 	if(ic->ic_rt == NULL){
686 		printf("no table for ic_curchan\n");
687 		ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
688 	}
689 	printf("ic->ic_rt =%p\n", ic->ic_rt);
690 	printf("rate count %d\n", ic->ic_rt->rateCount);
691 
692 	uint8_t code = ic->ic_rt->info[0].dot11Rate;
693 	uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
694 	uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
695 	printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
696 
697 	uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
698 	uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
699 	uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
700 	uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
701 	printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
702 	printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
703 	printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
704 #endif
705 	return 0;
706 }
707 
708 int32_t
709 wtap_detach(struct wtap_softc *sc)
710 {
711 	struct ieee80211com *ic = &sc->sc_ic;
712 
713 	DWTAP_PRINTF("%s\n", __func__);
714 	ieee80211_ageq_drain(&ic->ic_stageq);
715 	ieee80211_ifdetach(ic);
716 	return 0;
717 }
718 
719 void
720 wtap_resume(struct wtap_softc *sc)
721 {
722 
723 	DWTAP_PRINTF("%s\n", __func__);
724 }
725 
726 void
727 wtap_suspend(struct wtap_softc *sc)
728 {
729 
730 	DWTAP_PRINTF("%s\n", __func__);
731 }
732 
733 void
734 wtap_shutdown(struct wtap_softc *sc)
735 {
736 
737 	DWTAP_PRINTF("%s\n", __func__);
738 }
739 
740 void
741 wtap_intr(struct wtap_softc *sc)
742 {
743 
744 	DWTAP_PRINTF("%s\n", __func__);
745 }
746