xref: /freebsd/sys/dev/wtap/if_wtap.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 #include "wtap_hal/hal.h"
43 
44 /*
45  * This _requires_ vimage to be useful.
46  */
47 #ifndef	VIMAGE
48 #error	if_wtap requires VIMAGE.
49 #endif	/* VIMAGE */
50 
51 /* device for IOCTL and read/write for debuggin purposes */
52 /* Function prototypes */
53 static	d_open_t	wtap_node_open;
54 static	d_close_t	wtap_node_close;
55 static	d_write_t	wtap_node_write;
56 static	d_ioctl_t	wtap_node_ioctl;
57 
58 static struct cdevsw wtap_cdevsw = {
59 	.d_version =	D_VERSION,
60 	.d_flags =	0,
61 	.d_open = 	wtap_node_open,
62 	.d_close = 	wtap_node_close,
63 	.d_write = 	wtap_node_write,
64 	.d_ioctl =	wtap_node_ioctl,
65 	.d_name =	"wtapnode",
66 };
67 
68 static int
69 wtap_node_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
70 {
71 
72 	int err = 0;
73 	uprintf("Opened device \"echo\" successfully.\n");
74 	return(err);
75 }
76 
77 static int
78 wtap_node_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
79 {
80 
81 	uprintf("Closing device \"echo.\"\n");
82 	return(0);
83 }
84 
85 static int
86 wtap_node_write(struct cdev *dev, struct uio *uio, int ioflag)
87 {
88 	int err = 0;
89 	struct mbuf *m;
90 	struct wtap_softc *sc;
91 	uint8_t buf[1024];
92 	struct epoch_tracker et;
93 	int buf_len;
94 
95 	uprintf("write device %s \"echo.\"\n", devtoname(dev));
96 	buf_len = MIN(uio->uio_iov->iov_len, 1024);
97 	err = copyin(uio->uio_iov->iov_base, buf, buf_len);
98 
99 	if (err != 0) {
100 		uprintf("Write failed: bad address!\n");
101 		return (err);
102 	}
103 
104 	MGETHDR(m, M_NOWAIT, MT_DATA);
105 	m_copyback(m, 0, buf_len, buf);
106 
107 	NET_EPOCH_ENTER(et);
108 
109 	sc = (struct wtap_softc *)dev->si_drv1;
110 	printf("wtap id = %d\n", sc->id);
111 	wtap_inject(sc, m);
112 
113 	NET_EPOCH_EXIT(et);
114 
115 	return(err);
116 }
117 
118 int
119 wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
120     int fflag, struct thread *td)
121 {
122 	int error = 0;
123 
124 	switch(cmd) {
125 	default:
126 		DWTAP_PRINTF("Unknown WTAP IOCTL\n");
127 		error = EINVAL;
128 	}
129 	return error;
130 }
131 
132 static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
133 	const struct ieee80211_bpf_params *params);
134 
135 static int
136 wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
137 {
138 
139 	return medium_transmit(avp->av_md, avp->id, m);
140 }
141 
142 /*
143  * Intercept management frames to collect beacon rssi data
144  * and to do ibss merges.
145  */
146 static void
147 wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
148     int subtype, const struct ieee80211_rx_stats *stats, int rssi, int nf)
149 {
150 	struct ieee80211vap *vap = ni->ni_vap;
151 	struct wtap_softc *sc = vap->iv_ic->ic_softc;
152 #if 0
153 	DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
154 #endif
155 	/*
156 	 * Call up first so subsequent work can use information
157 	 * potentially stored in the node (e.g. for ibss merge).
158 	 */
159 	WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, stats, rssi, nf);
160 
161 	switch (subtype) {
162 	case IEEE80211_FC0_SUBTYPE_BEACON:
163 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
164 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
165 		    vap->iv_state == IEEE80211_S_RUN &&
166 		    ieee80211_ibss_merge_check(ni)) {
167 			uint64_t tsf = wtap_hal_get_tsf(sc->hal);
168 
169 			/*
170 			 * Handle ibss merge as needed; check the tsf on the
171 			 * frame before attempting the merge.  The 802.11 spec
172 			 * says the station should change it's bssid to match
173 			 * the oldest station with the same ssid, where oldest
174 			 * is determined by the tsf.  Note that hardware
175 			 * reconfiguration happens through callback to
176 			 * ath_newstate as the state machine will go from
177 			 * RUN -> RUN when this happens.
178 			 */
179 			if (le64toh(ni->ni_tstamp.tsf) >= tsf)
180 				(void) ieee80211_ibss_merge(ni);
181 		}
182 		break;
183 	}
184 }
185 
186 static int
187 wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
188 {
189 
190 	DWTAP_PRINTF("%s\n", __func__);
191 	return 0;
192 }
193 
194 static void
195 wtap_beacon_update(struct ieee80211vap *vap, int item)
196 {
197 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
198 
199 	DWTAP_PRINTF("%s\n", __func__);
200 	setbit(bo->bo_flags, item);
201 }
202 
203 /*
204  * Allocate and setup an initial beacon frame.
205  */
206 static int
207 wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
208 {
209 	struct ieee80211vap *vap = ni->ni_vap;
210 	struct wtap_vap *avp = WTAP_VAP(vap);
211 
212 	DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
213 
214 	/*
215 	 * NB: the beacon data buffer must be 32-bit aligned;
216 	 * we assume the mbuf routines will return us something
217 	 * with this alignment (perhaps should assert).
218 	 */
219 	avp->beacon = ieee80211_beacon_alloc(ni);
220 	if (avp->beacon == NULL) {
221 		printf("%s: cannot get mbuf\n", __func__);
222 		return ENOMEM;
223 	}
224 	avp->bf_node = ieee80211_ref_node(ni);
225 
226 	return 0;
227 }
228 
229 static void
230 wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
231 {
232 	DWTAP_PRINTF("%s\n", __func__);
233 }
234 
235 static void
236 wtap_beacon_intrp(void *arg)
237 {
238 	struct wtap_vap *avp = arg;
239 	struct ieee80211vap *vap = arg;
240 	struct wtap_softc *sc = vap->iv_ic->ic_softc;
241 	struct ieee80211_frame *wh;
242 	struct mbuf *m;
243 	uint64_t tsf;
244 
245 	if (vap->iv_state < IEEE80211_S_RUN) {
246 	    DWTAP_PRINTF("Skip beacon, not running, state %d", vap->iv_state);
247 	    return ;
248 	}
249 	DWTAP_PRINTF("[%d] beacon intrp\n", avp->id);	//burst mode
250 	/*
251 	 * Update dynamic beacon contents.  If this returns
252 	 * non-zero then we need to remap the memory because
253 	 * the beacon frame changed size (probably because
254 	 * of the TIM bitmap).
255 	 */
256 	m = m_dup(avp->beacon, M_NOWAIT);
257 	if (ieee80211_beacon_update(avp->bf_node, m, 0)) {
258 		printf("%s, need to remap the memory because the beacon frame"
259 		    " changed size.\n",__func__);
260 	}
261 
262 	/* Get TSF from HAL, and insert it into beacon frame */
263 	tsf = wtap_hal_get_tsf(sc->hal);
264 	wh = mtod(m, struct ieee80211_frame *);
265 	memcpy(&wh[1], &tsf, sizeof(tsf));
266 
267 	if (ieee80211_radiotap_active_vap(vap))
268 	    ieee80211_radiotap_tx(vap, m);
269 
270 #if 0
271 	medium_transmit(avp->av_md, avp->id, m);
272 #endif
273 	wtap_medium_enqueue(avp, m);
274 	callout_schedule(&avp->av_swba, avp->av_bcinterval);
275 }
276 
277 static int
278 wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
279 {
280 	struct ieee80211com *ic = vap->iv_ic;
281 	struct wtap_softc *sc = ic->ic_softc;
282 	struct wtap_vap *avp = WTAP_VAP(vap);
283 	struct ieee80211_node *ni = NULL;
284 	int error;
285 
286 	DWTAP_PRINTF("%s\n", __func__);
287 
288 	ni = ieee80211_ref_node(vap->iv_bss);
289 	/*
290 	 * Invoke the parent method to do net80211 work.
291 	 */
292 	error = avp->av_newstate(vap, nstate, arg);
293 	if (error != 0)
294 		goto bad;
295 
296 	if (nstate == IEEE80211_S_RUN) {
297 		/* NB: collect bss node again, it may have changed */
298 		ieee80211_free_node(ni);
299 		ni = ieee80211_ref_node(vap->iv_bss);
300 		switch (vap->iv_opmode) {
301 		case IEEE80211_M_IBSS:
302 		case IEEE80211_M_MBSS:
303 			/*
304 			 * Stop any previous beacon callout. This may be
305 			 * necessary, for example, when an ibss merge
306 			 * causes reconfiguration; there will be a state
307 			 * transition from RUN->RUN that means we may
308 			 * be called with beacon transmission active.
309 			 */
310 			callout_stop(&avp->av_swba);
311 
312 			error = wtap_beacon_alloc(sc, ni);
313 			if (error != 0)
314 				goto bad;
315 
316 			/*
317 			 * If joining an adhoc network defer beacon timer
318 			 * configuration to the next beacon frame so we
319 			 * have a current TSF to use.  Otherwise we're
320 			 * starting an ibss/bss so there's no need to delay;
321 			 * if this is the first vap moving to RUN state, then
322 			 * beacon state needs to be [re]configured.
323 			 */
324 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
325 			    ni->ni_tstamp.tsf != 0)
326 				break;
327 
328 			wtap_beacon_config(sc, vap);
329 
330 			/* Start TSF timer from now, and start s/w beacon alert */
331 			wtap_hal_reset_tsf(sc->hal);
332 			callout_reset(&avp->av_swba, avp->av_bcinterval,
333 			    wtap_beacon_intrp, vap);
334 			break;
335 		default:
336 			goto bad;
337 		}
338 	} else if (nstate == IEEE80211_S_INIT) {
339 		callout_stop(&avp->av_swba);
340 	}
341 	ieee80211_free_node(ni);
342 	return 0;
343 bad:
344 	printf("%s: bad\n", __func__);
345 	ieee80211_free_node(ni);
346 	return error;
347 }
348 
349 static void
350 wtap_bmiss(struct ieee80211vap *vap)
351 {
352 	struct wtap_vap *avp = (struct wtap_vap *)vap;
353 
354 	DWTAP_PRINTF("%s\n", __func__);
355 	avp->av_bmiss(vap);
356 }
357 
358 static struct ieee80211vap *
359 wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
360     int unit, enum ieee80211_opmode opmode, int flags,
361     const uint8_t bssid[IEEE80211_ADDR_LEN],
362     const uint8_t mac[IEEE80211_ADDR_LEN])
363 {
364 	 struct wtap_softc *sc = ic->ic_softc;
365 	 struct ieee80211vap *vap;
366 	 struct wtap_vap *avp;
367 	 int error;
368 	struct ieee80211_node *ni;
369 
370 	 DWTAP_PRINTF("%s\n", __func__);
371 
372 	avp = malloc(sizeof(struct wtap_vap), M_80211_VAP, M_WAITOK | M_ZERO);
373 	avp->id = sc->id;
374 	avp->av_md = sc->sc_md;
375 	avp->av_bcinterval = msecs_to_ticks(BEACON_INTRERVAL + 100*sc->id);
376 	vap = (struct ieee80211vap *) avp;
377 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode,
378 	    flags | IEEE80211_CLONE_NOBEACONS, bssid);
379 	if (error) {
380 		free(avp, M_80211_VAP);
381 		return (NULL);
382 	}
383 
384 	/* override various methods */
385 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
386 	vap->iv_recv_mgmt = wtap_recv_mgmt;
387 	vap->iv_reset = wtap_reset_vap;
388 	vap->iv_update_beacon = wtap_beacon_update;
389 	avp->av_newstate = vap->iv_newstate;
390 	vap->iv_newstate = wtap_newstate;
391 	avp->av_bmiss = vap->iv_bmiss;
392 	vap->iv_bmiss = wtap_bmiss;
393 
394 	/* complete setup */
395 	ieee80211_vap_attach(vap, ieee80211_media_change,
396 	    ieee80211_media_status, mac);
397 	avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
398 	    "%s", (const char *)vap->iv_ifp->if_xname);
399 	avp->av_dev->si_drv1 = sc;
400 	callout_init(&avp->av_swba, 0);
401 
402 	/* TODO this is a hack to force it to choose the rate we want */
403 	ni = ieee80211_ref_node(vap->iv_bss);
404 	ni->ni_txrate = 130;
405 	ieee80211_free_node(ni);
406 	return vap;
407 }
408 
409 static void
410 wtap_vap_delete(struct ieee80211vap *vap)
411 {
412 	struct wtap_vap *avp = WTAP_VAP(vap);
413 
414 	DWTAP_PRINTF("%s\n", __func__);
415 	destroy_dev(avp->av_dev);
416 	callout_stop(&avp->av_swba);
417 	ieee80211_vap_detach(vap);
418 	free(avp, M_80211_VAP);
419 }
420 
421 static void
422 wtap_parent(struct ieee80211com *ic)
423 {
424 	struct wtap_softc *sc = ic->ic_softc;
425 
426 	if (ic->ic_nrunning > 0) {
427 		sc->up = 1;
428 		ieee80211_start_all(ic);
429 	} else
430 		sc->up = 0;
431 }
432 
433 static void
434 wtap_scan_start(struct ieee80211com *ic)
435 {
436 
437 #if 0
438 	DWTAP_PRINTF("%s\n", __func__);
439 #endif
440 }
441 
442 static void
443 wtap_scan_end(struct ieee80211com *ic)
444 {
445 
446 #if 0
447 	DWTAP_PRINTF("%s\n", __func__);
448 #endif
449 }
450 
451 static void
452 wtap_set_channel(struct ieee80211com *ic)
453 {
454 
455 #if 0
456 	DWTAP_PRINTF("%s\n", __func__);
457 #endif
458 }
459 
460 static int
461 wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
462 	const struct ieee80211_bpf_params *params)
463 {
464 #if 0
465 	DWTAP_PRINTF("%s, %p\n", __func__, m);
466 #endif
467 	struct ieee80211vap	*vap = ni->ni_vap;
468 	struct wtap_vap 	*avp = WTAP_VAP(vap);
469 
470 	if (ieee80211_radiotap_active_vap(vap)) {
471 		ieee80211_radiotap_tx(vap, m);
472 	}
473 	if (m->m_flags & M_TXCB)
474 		ieee80211_process_callback(ni, m, 0);
475 	ieee80211_free_node(ni);
476 	return wtap_medium_enqueue(avp, m);
477 }
478 
479 void
480 wtap_inject(struct wtap_softc *sc, struct mbuf *m)
481 {
482       struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
483           M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
484       KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
485       bf->m = m;
486 
487       mtx_lock(&sc->sc_mtx);
488       STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
489       taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
490       mtx_unlock(&sc->sc_mtx);
491 }
492 
493 static void
494 wtap_rx_proc(void *arg, int npending)
495 {
496 	struct epoch_tracker et;
497 	struct wtap_softc *sc = (struct wtap_softc *)arg;
498 	struct ieee80211com *ic = &sc->sc_ic;
499 	struct mbuf *m;
500 	struct ieee80211_node *ni;
501 	struct wtap_buf *bf;
502 
503 #if 0
504 	DWTAP_PRINTF("%s\n", __func__);
505 #endif
506 
507 	for(;;) {
508 		mtx_lock(&sc->sc_mtx);
509 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
510 		if (bf == NULL) {
511 			mtx_unlock(&sc->sc_mtx);
512 			return;
513 		}
514 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
515 		mtx_unlock(&sc->sc_mtx);
516 		KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
517 		m = bf->m;
518 		DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
519 		if (m == NULL) {		/* NB: shouldn't happen */
520 			ic_printf(ic, "%s: no mbuf!\n", __func__);
521 			free(bf, M_WTAP_RXBUF);
522 			return;
523 		}
524 
525 		/*
526 		 * It's weird to do this, but sometimes wtap will
527 		 * receive AMPDU packets (like ping(8)) even when
528 		 * the ic does not supports 11n HT.
529 		 */
530 		m->m_flags &= ~M_AMPDU;
531 #if 0
532 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
533 #endif
534 
535 		/*
536 		 * Locate the node for sender, track state, and then
537 		 * pass the (referenced) node up to the 802.11 layer
538 		 * for its use.
539 		 */
540 		ni = ieee80211_find_rxnode_withkey(ic,
541 		    mtod(m, const struct ieee80211_frame_min *),
542 		    IEEE80211_KEYIX_NONE);
543 		NET_EPOCH_ENTER(et);
544 		if (ni != NULL) {
545 			/*
546 			 * Sending station is known, dispatch directly.
547 			 */
548 			ieee80211_input(ni, m, 1<<7, 10);
549 			ieee80211_free_node(ni);
550 		} else {
551 			ieee80211_input_all(ic, m, 1<<7, 10);
552 		}
553 		NET_EPOCH_EXIT(et);
554 
555 		/* The mbufs are freed by the Net80211 stack */
556 		free(bf, M_WTAP_RXBUF);
557 	}
558 }
559 
560 static void
561 wtap_newassoc(struct ieee80211_node *ni, int isnew)
562 {
563 
564 	DWTAP_PRINTF("%s\n", __func__);
565 }
566 
567 /*
568  * Callback from the 802.11 layer to update WME parameters.
569  */
570 static int
571 wtap_wme_update(struct ieee80211com *ic)
572 {
573 
574 	DWTAP_PRINTF("%s\n", __func__);
575 	return 0;
576 }
577 
578 static void
579 wtap_update_mcast(struct ieee80211com *ic)
580 {
581 
582 	DWTAP_PRINTF("%s\n", __func__);
583 }
584 
585 static void
586 wtap_update_promisc(struct ieee80211com *ic)
587 {
588 
589 	DWTAP_PRINTF("%s\n", __func__);
590 }
591 
592 static int
593 wtap_transmit(struct ieee80211com *ic, struct mbuf *m)
594 {
595 	struct ieee80211_node *ni =
596 	    (struct ieee80211_node *) m->m_pkthdr.rcvif;
597 	struct ieee80211vap *vap = ni->ni_vap;
598 	struct wtap_vap *avp = WTAP_VAP(vap);
599 
600 	if(ni == NULL){
601 		printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
602 	}else{
603 		if (ieee80211_radiotap_active_vap(vap))
604 			ieee80211_radiotap_tx(vap, m);
605 	}
606 	if (m->m_flags & M_TXCB)
607 		ieee80211_process_callback(ni, m, 0);
608 	ieee80211_free_node(ni);
609 	return wtap_medium_enqueue(avp, m);
610 }
611 
612 static struct ieee80211_node *
613 wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
614 {
615 	struct ieee80211_node *ni;
616 
617 	DWTAP_PRINTF("%s\n", __func__);
618 
619 	ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
620 	    M_NOWAIT|M_ZERO);
621 	if (ni == NULL)
622 		return (NULL);
623 
624 	ni->ni_txrate = 130;
625 	return ni;
626 }
627 
628 static void
629 wtap_node_free(struct ieee80211_node *ni)
630 {
631 	struct ieee80211com *ic = ni->ni_ic;
632 	struct wtap_softc *sc = ic->ic_softc;
633 
634 	DWTAP_PRINTF("%s\n", __func__);
635 	sc->sc_node_free(ni);
636 }
637 
638 int32_t
639 wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
640 {
641 	struct ieee80211com *ic = &sc->sc_ic;
642 
643 	DWTAP_PRINTF("%s\n", __func__);
644 
645 	sc->up = 0;
646 	STAILQ_INIT(&sc->sc_rxbuf);
647 	sc->sc_tq = taskqueue_create("wtap_taskq", M_NOWAIT | M_ZERO,
648 	    taskqueue_thread_enqueue, &sc->sc_tq);
649 	taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ", sc->name);
650 	NET_TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
651 
652 	ic->ic_softc = sc;
653 	ic->ic_name = sc->name;
654 	ic->ic_phytype = IEEE80211_T_DS;
655 	ic->ic_opmode = IEEE80211_M_MBSS;
656 	ic->ic_caps = IEEE80211_C_MBSS | IEEE80211_C_IBSS;
657 
658 	ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
659 
660 	ic->ic_regdomain.regdomain = SKU_ETSI;
661 	ic->ic_regdomain.country = CTRY_SWEDEN;
662 	ic->ic_regdomain.location = 1; /* Indoors */
663 	ic->ic_regdomain.isocc[0] = 'S';
664 	ic->ic_regdomain.isocc[1] = 'E';
665 
666 	ic->ic_nchans = 1;
667 	ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
668 	ic->ic_channels[0].ic_freq = 2412;
669 
670 	IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
671 	ieee80211_ifattach(ic);
672 
673 	/* override default methods */
674 	ic->ic_newassoc = wtap_newassoc;
675 	ic->ic_wme.wme_update = wtap_wme_update;
676 	ic->ic_vap_create = wtap_vap_create;
677 	ic->ic_vap_delete = wtap_vap_delete;
678 	ic->ic_raw_xmit = wtap_raw_xmit;
679 	ic->ic_update_mcast = wtap_update_mcast;
680 	ic->ic_update_promisc = wtap_update_promisc;
681 	ic->ic_transmit = wtap_transmit;
682 	ic->ic_parent = wtap_parent;
683 
684 	sc->sc_node_alloc = ic->ic_node_alloc;
685 	ic->ic_node_alloc = wtap_node_alloc;
686 	sc->sc_node_free = ic->ic_node_free;
687 	ic->ic_node_free = wtap_node_free;
688 
689 	ic->ic_scan_start = wtap_scan_start;
690 	ic->ic_scan_end = wtap_scan_end;
691 	ic->ic_set_channel = wtap_set_channel;
692 
693 	ieee80211_radiotap_attach(ic,
694 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
695 	    WTAP_TX_RADIOTAP_PRESENT,
696 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
697 	    WTAP_RX_RADIOTAP_PRESENT);
698 
699 	/* Work here, we must find a way to populate the rate table */
700 #if 0
701 	if(ic->ic_rt == NULL){
702 		printf("no table for ic_curchan\n");
703 		ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
704 	}
705 	printf("ic->ic_rt =%p\n", ic->ic_rt);
706 	printf("rate count %d\n", ic->ic_rt->rateCount);
707 
708 	uint8_t code = ic->ic_rt->info[0].dot11Rate;
709 	uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
710 	uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
711 	printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
712 
713 	uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
714 	uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
715 	uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
716 	uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
717 	printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
718 	printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
719 	printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
720 #endif
721 	return 0;
722 }
723 
724 int32_t
725 wtap_detach(struct wtap_softc *sc)
726 {
727 	struct ieee80211com *ic = &sc->sc_ic;
728 
729 	DWTAP_PRINTF("%s\n", __func__);
730 	ieee80211_ageq_drain(&ic->ic_stageq);
731 	ieee80211_ifdetach(ic);
732 	return 0;
733 }
734 
735 void
736 wtap_resume(struct wtap_softc *sc)
737 {
738 
739 	DWTAP_PRINTF("%s\n", __func__);
740 }
741 
742 void
743 wtap_suspend(struct wtap_softc *sc)
744 {
745 
746 	DWTAP_PRINTF("%s\n", __func__);
747 }
748 
749 void
750 wtap_shutdown(struct wtap_softc *sc)
751 {
752 
753 	DWTAP_PRINTF("%s\n", __func__);
754 }
755 
756 void
757 wtap_intr(struct wtap_softc *sc)
758 {
759 
760 	DWTAP_PRINTF("%s\n", __func__);
761 }
762