Home | History | Annotate | Line # | Download | only in net80211
ieee80211_netbsd.h revision 1.21.2.12
      1 /* $NetBSD: ieee80211_netbsd.h,v 1.21.2.12 2020/04/16 15:30:00 nat Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2003-2008 Sam Leffler, Errno Consulting
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * $FreeBSD:  ieee80211_freebsd.h$
     30  */
     31 #ifndef _NET80211_IEEE80211_NETBSD_H_
     32 #define _NET80211_IEEE80211_NETBSD_H_
     33 
     34 #ifdef _KERNEL
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/atomic.h>
     38 #include <sys/cprng.h>
     39 #include <sys/lock.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mallocvar.h>
     43 #include <sys/mutex.h>
     44 #include <sys/rwlock.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/workqueue.h>
     47 
     48 // #include <net80211/_ieee80211.h>
     49 
     50 #include <net/if.h>
     51 
     52 /*
     53  * Defines to make the FreeBSD code work on NetBSD
     54  */
     55 
     56 #define PI_NET IPL_NET
     57 #define EDOOFUS EINVAL
     58 #define IFF_PPROMISC IFF_PROMISC
     59 
     60 #define __offsetof(type, field)  __builtin_offsetof(type, field)
     61 #define arc4random  cprng_fast32
     62 #define atomic_subtract_int(var,val) atomic_add_int(var,-(val))
     63 #define caddr_t void *
     64 #define callout_drain(x)  callout_halt(x, NULL)
     65 #define m_catpkt(x,y)    m_cat(x,y)
     66 #define mtx_lock(mtx) 		mutex_enter(mtx)
     67 #define mtx_unlock(mtx)		mutex_exit(mtx)
     68 #define mtx_owned(mtx)		mutex_owned(mtx)
     69 #define mtx_destroy(mtx)	mutex_destroy(mtx)
     70 #define mtx_sleep(a1, a2, a3, a4, a5) /* NNN not sure what it should be. */
     71 #define nitems(x)    (sizeof((x)) / sizeof((x)[0]))
     72 #define ovbcopy(dst,src,size)  memmove(dst,src,size)
     73 #define ticks   hardclock_ticks
     74 
     75 /*
     76  * task stuff needs major work NNN!
     77  */
     78 
     79 typedef void task_fn_t(void *context, int pending);
     80 
     81 // NNN use more standard feature for getting pointers from fields ...???
     82 struct task {
     83 	struct work t_work;    /* Must be first so we can cast a work to a task */
     84 	task_fn_t  *t_func;
     85 	void       *t_arg;
     86 	kmutex_t    t_mutex;
     87 	int         t_onqueue;
     88 	const char *t_func_name;
     89 };
     90 
     91 struct timeout_task {
     92 	struct task to_task;	/* Must be first so we can cast to a task. */
     93 	struct workqueue *to_wq;
     94 	callout_t   to_callout;
     95 	int	    to_scheduled;
     96 };
     97 
     98 
     99 static __inline int dummy(void);
    100 static __inline int dummy(void) { return 0; }
    101 
    102 void ieee80211_runwork(struct work *, void *);
    103 void taskqueue_enqueue(struct workqueue *, struct task *);
    104 void taskqueue_drain(struct workqueue *, struct task *);
    105 
    106 int  taskqueue_enqueue_timeout(struct workqueue	*queue,
    107 	 struct	timeout_task *timeout_task, int	nticks);
    108 int  taskqueue_cancel_timeout(struct workqueue *queue,
    109 	 struct	timeout_task *timeout_task, u_int *pendp);
    110 void taskqueue_drain_timeout(struct workqueue *queue,
    111 	 struct	timeout_task *timeout_task);
    112 
    113 /* NNN ---- Need to add a way to mutex_destroy at the right time. */
    114 
    115 #define TASK_INIT(var, pri, func, arg) do { \
    116 	(var)->t_func = func; \
    117         (var)->t_arg = arg; \
    118 	mutex_init(&(var)->t_mutex, MUTEX_DEFAULT, IPL_SOFTNET);\
    119 	(var)->t_onqueue = 0;\
    120 	(var)->t_func_name = #func; \
    121 } while(0)
    122 
    123 #define TIMEOUT_TASK_INIT(queue, task, pri, func, arg) do { \
    124 	(task)->to_task.t_func = func; \
    125         (task)->to_task.t_arg = arg; \
    126 	mutex_init(&(task)->to_task.t_mutex, MUTEX_DEFAULT, IPL_SOFTNET);\
    127 	(task)->to_task.t_onqueue = 0;\
    128 	(task)->to_task.t_func_name = #func; \
    129 	(task)->to_wq = queue;\
    130 	callout_init(&(task)->to_callout, CALLOUT_MPSAFE);\
    131 	(task)->to_scheduled = 0;\
    132 } while (0)
    133 
    134 #define taskqueue workqueue
    135 #define taskqueue_free(queue)         workqueue_destroy(queue)
    136 
    137 #define taskqueue_block(queue)        /* */
    138 #define taskqueue_unblock(queue)      /* */
    139 
    140 /*  Other stuff that needs to be fixed NNN */
    141 #define priv_check(x,y) 0
    142 
    143 /* Coult it be this simple? NNN */
    144 #define if_addr_rlock(ifp) IFNET_LOCK(ifp)
    145 #define if_addr_runlock(ifp) IFNET_UNLOCK(ifp)
    146 
    147 /* VNET defines to remove them ... NNN may need a lot of work! */
    148 
    149 #define CURVNET_SET(x)		/* */
    150 #define CURVNET_RESTORE() 	/* */
    151 #define CURVNET_SET_QUIET(x) 	/* */
    152 
    153 /* counters */
    154 typedef uint64_t counter_u64_t;
    155 #define counter_u64_free(x) /* */
    156 #define counter_u64_fetch(x) x
    157 #define counter_u64_alloc(x) 0
    158 
    159 typedef enum {
    160         IFCOUNTER_IPACKETS = 0,
    161         IFCOUNTER_IERRORS,
    162         IFCOUNTER_OPACKETS,
    163         IFCOUNTER_OERRORS,
    164         IFCOUNTER_COLLISIONS,
    165         IFCOUNTER_IBYTES,
    166         IFCOUNTER_OBYTES,
    167         IFCOUNTER_IMCASTS,
    168         IFCOUNTER_OMCASTS,
    169         IFCOUNTER_IQDROPS,
    170         IFCOUNTER_OQDROPS,
    171         IFCOUNTER_NOPROTO,
    172         IFCOUNTERS /* Array size. */
    173 } ift_counter;
    174 void       if_inc_counter(struct ifnet *, ift_counter, int64_t);
    175 int64_t    if_get_counter_default(struct ifnet *, ift_counter);
    176 
    177 #define IF_LLADDR(ifp)     (((struct ieee80211vap *)ifp->if_softc)->iv_myaddr)
    178 
    179 /* Scanners ... needed because no module support; */
    180 extern const struct ieee80211_scanner sta_default;
    181 extern const struct ieee80211_scanner ap_default;
    182 extern const struct ieee80211_scanner adhoc_default;
    183 extern const struct ieee80211_scanner mesh_default;
    184 
    185 /*
    186  *  Sysctl support??? NNN
    187  */
    188 
    189 #define SYSCTL_INT(a1, a2, a3, a4, a5, a6, a7)  /* notyet */
    190 #define SYSCTL_PROC(a1, a2, a3, a4, a5, a6, a7, a8, a9)  /* notyet */
    191 #undef SYSCTL_NODE
    192 #define SYSCTL_NODE(a1, a2, a3, a4, a5, a6) int a2 __unused
    193 
    194 /* another unknown macro ... at least notyet */
    195 #define SYSINIT(a1, a2, a3, a4, a5)  /* notyet */
    196 #define TEXT_SET(set, sym)   /* notyet ... linker magic, supported?  */
    197 
    198 /*
    199  * FreeBSD code uses KASSERT but different from NetBSD so ...
    200  */
    201 #define FBSDKASSERT(_cond, _complaint)        \
    202         do {                                  \
    203                 if (!(_cond))                 \
    204                         panic _complaint;     \
    205         } while (/*CONSTCOND*/0)
    206 
    207 /*
    208  * Common state locking definitions.
    209  */
    210 typedef struct {
    211 	char		name[16];		/* e.g. "ath0_com_lock" */
    212 	kmutex_t	mtx;
    213 } ieee80211_com_lock_t;
    214 #define	IEEE80211_LOCK_INIT(_ic, _name) do {				\
    215 	ieee80211_com_lock_t *cl = &(_ic)->ic_comlock;			\
    216 	snprintf(cl->name, sizeof(cl->name), "%s_com_lock", _name);	\
    217         mutex_init(&cl->mtx, MUTEX_DEFAULT, IPL_SOFTNET);               \
    218 } while (0)
    219 #define	IEEE80211_LOCK_OBJ(_ic)	(&(_ic)->ic_comlock.mtx)
    220 #define	IEEE80211_LOCK_DESTROY(_ic) mutex_destroy(IEEE80211_LOCK_OBJ(_ic))
    221 #define	IEEE80211_LOCK(_ic)	   mutex_enter(IEEE80211_LOCK_OBJ(_ic))
    222 #define	IEEE80211_UNLOCK(_ic)	   mutex_exit(IEEE80211_LOCK_OBJ(_ic))
    223 #define	IEEE80211_LOCK_ASSERT(_ic)    \
    224         FBSDKASSERT(mutex_owned(IEEE80211_LOCK_OBJ(_ic)), ("Lock is not owned"))
    225 #define	IEEE80211_UNLOCK_ASSERT(_ic)  \
    226 	FBSDKASSERT(!mutex_owned(IEEE80211_LOCK_OBJ(_ic)), ("Lock is owned"))
    227 
    228 /*
    229  * Transmit lock.
    230  *
    231  * This is a (mostly) temporary lock designed to serialise all of the
    232  * transmission operations throughout the stack.
    233  */
    234 typedef struct {
    235 	char		name[16];		/* e.g. "ath0_tx_lock" */
    236 	kmutex_t	mtx;
    237 } ieee80211_tx_lock_t;
    238 #define	IEEE80211_TX_LOCK_INIT(_ic, _name) do {				\
    239 	ieee80211_tx_lock_t *cl = &(_ic)->ic_txlock;			\
    240 	snprintf(cl->name, sizeof(cl->name), "%s_tx_lock", _name);	\
    241 	mutex_init(&cl->mtx, MUTEX_DEFAULT, IPL_SOFTNET);		\
    242 } while (0)
    243 #define	IEEE80211_TX_LOCK_OBJ(_ic)	(&(_ic)->ic_txlock.mtx)
    244 #define	IEEE80211_TX_LOCK_DESTROY(_ic) mutex_destroy(IEEE80211_TX_LOCK_OBJ(_ic))
    245 #define	IEEE80211_TX_LOCK(_ic)	   mutex_enter(IEEE80211_TX_LOCK_OBJ(_ic))
    246 #define	IEEE80211_TX_UNLOCK(_ic)	   mutex_exit(IEEE80211_TX_LOCK_OBJ(_ic))
    247 #define	IEEE80211_TX_LOCK_ASSERT(_ic) \
    248 	FBSDKASSERT(mutex_owned(IEEE80211_TX_LOCK_OBJ(_ic)), ("lock not owned"))
    249 #define	IEEE80211_TX_UNLOCK_ASSERT(_ic) \
    250 	FBSDKASSERT(!mutex_owned(IEEE80211_TX_LOCK_OBJ(_ic)), ("lock is owned"))
    251 
    252 /*
    253  * Stageq / ni_tx_superg lock
    254  */
    255 typedef struct {
    256 	char		name[16];		/* e.g. "ath0_ff_lock" */
    257 	kmutex_t	mtx;
    258 } ieee80211_ff_lock_t;
    259 #define IEEE80211_FF_LOCK_INIT(_ic, _name) do {				\
    260 	ieee80211_ff_lock_t *fl = &(_ic)->ic_fflock;			\
    261 	snprintf(fl->name, sizeof(fl->name), "%s_ff_lock", _name);	\
    262 	mutex_init(&fl->mtx, MUTEX_DEFAULT, IPL_SOFTNET);               \
    263 } while (0)
    264 #define IEEE80211_FF_LOCK_OBJ(_ic)	(&(_ic)->ic_fflock.mtx)
    265 #define IEEE80211_FF_LOCK_DESTROY(_ic)	mutex_destroy(IEEE80211_FF_LOCK_OBJ(_ic))
    266 #define IEEE80211_FF_LOCK(_ic)		mutex_enter(IEEE80211_FF_LOCK_OBJ(_ic))
    267 #define IEEE80211_FF_UNLOCK(_ic)	mutex_exit(IEEE80211_FF_LOCK_OBJ(_ic))
    268 #define IEEE80211_FF_LOCK_ASSERT(_ic) \
    269 	FBSDKASSERT(mutex_owned(IEEE80211_FF_LOCK_OBJ(_ic)), ("lock not owned"))
    270 
    271 /*
    272  * Node locking definitions.
    273  */
    274 typedef struct {
    275 	char		name[16];		/* e.g. "ath0_node_lock" */
    276 	kmutex_t	mtx;
    277 } ieee80211_node_lock_t;
    278 #define	IEEE80211_NODE_LOCK_INIT(_nt, _name) do {			\
    279 	ieee80211_node_lock_t *nl = &(_nt)->nt_nodelock;		\
    280 	snprintf(nl->name, sizeof(nl->name), "%s_node_lock", _name);	\
    281 	mutex_init(&nl->mtx, MUTEX_DEFAULT, IPL_SOFTNET);               \
    282 } while (0)
    283 #define	IEEE80211_NODE_LOCK_OBJ(_nt)	(&(_nt)->nt_nodelock.mtx)
    284 #define	IEEE80211_NODE_LOCK_DESTROY(_nt) \
    285 	mutex_destroy(IEEE80211_NODE_LOCK_OBJ(_nt))
    286 #define	IEEE80211_NODE_LOCK(_nt) \
    287 	mutex_enter(IEEE80211_NODE_LOCK_OBJ(_nt))
    288 #define	IEEE80211_NODE_IS_LOCKED(_nt) \
    289 	mtx_owned(IEEE80211_NODE_LOCK_OBJ(_nt))
    290 #define	IEEE80211_NODE_UNLOCK(_nt) \
    291 	mutex_exit(IEEE80211_NODE_LOCK_OBJ(_nt))
    292 #define	IEEE80211_NODE_LOCK_ASSERT(_nt)	\
    293 	FBSDKASSERT(mutex_owned(IEEE80211_NODE_LOCK_OBJ(_nt)), ("lock not owned"))
    294 
    295 /*
    296  * Power-save queue definitions.
    297  */
    298 typedef kmutex_t ieee80211_psq_lock_t;
    299 #define	IEEE80211_PSQ_INIT(_psq, _name) \
    300 	mutex_init(&(_psq)->psq_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    301 #define	IEEE80211_PSQ_DESTROY(_psq)	mutex_destroy(&(_psq)->psq_lock)
    302 #define	IEEE80211_PSQ_LOCK(_psq)	mutex_enter(&(_psq)->psq_lock)
    303 #define	IEEE80211_PSQ_UNLOCK(_psq)	mutex_exit(&(_psq)->psq_lock)
    304 
    305 #ifndef IF_PREPEND_LIST
    306 #define _IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {	\
    307 	(mtail)->m_nextpkt = (ifq)->ifq_head;			\
    308 	if ((ifq)->ifq_tail == NULL)				\
    309 		(ifq)->ifq_tail = (mtail);			\
    310 	(ifq)->ifq_head = (mhead);				\
    311 	(ifq)->ifq_len += (mcount);				\
    312 } while (0)
    313 #define IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {		\
    314 	IF_LOCK(ifq);						\
    315 	_IF_PREPEND_LIST(ifq, mhead, mtail, mcount);		\
    316 	IF_UNLOCK(ifq);						\
    317 } while (0)
    318 #endif /* IF_PREPEND_LIST */
    319 
    320 /*
    321  * Age queue definitions.
    322  */
    323 typedef kmutex_t ieee80211_ageq_lock_t;
    324 #define	IEEE80211_AGEQ_INIT(_aq, _name) \
    325 	mutex_init(&(_aq)->aq_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    326 #define	IEEE80211_AGEQ_DESTROY(_aq)	mutex_destroy(&(_aq)->aq_lock)
    327 #define	IEEE80211_AGEQ_LOCK(_aq)	mutex_enter(&(_aq)->aq_lock)
    328 #define	IEEE80211_AGEQ_UNLOCK(_aq)	mutex_exit(&(_aq)->aq_lock)
    329 
    330 /*
    331  * 802.1x MAC ACL database locking definitions.
    332  */
    333 typedef kmutex_t acl_lock_t;
    334 #define	ACL_LOCK_INIT(_as, _name) \
    335 	mutex_init(&(_as)->as_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    336 #define	ACL_LOCK_DESTROY(_as)		mutex_destroy(&(_as)->as_lock)
    337 #define	ACL_LOCK(_as)			mutex_enter(&(_as)->as_lock)
    338 #define	ACL_UNLOCK(_as)			mutex_exit(&(_as)->as_lock)
    339 #define	ACL_LOCK_ASSERT(_as) \
    340 	FBSDKASSERT(mutex_owned((&(_as)->as_lock)), ("lock not owned"))
    341 
    342 /*
    343  * Scan table definitions.
    344  */
    345 typedef kmutex_t ieee80211_scan_table_lock_t;
    346 #define	IEEE80211_SCAN_TABLE_LOCK_INIT(_st, _name) \
    347 	mutex_init(&(_st)->st_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    348 #define	IEEE80211_SCAN_TABLE_LOCK_DESTROY(_st)	mutex_destroy(&(_st)->st_lock)
    349 #define	IEEE80211_SCAN_TABLE_LOCK(_st)		mutex_enter(&(_st)->st_lock)
    350 #define	IEEE80211_SCAN_TABLE_UNLOCK(_st)	mutex_exit(&(_st)->st_lock)
    351 
    352 typedef kmutex_t ieee80211_scan_iter_lock_t;
    353 #define	IEEE80211_SCAN_ITER_LOCK_INIT(_st, _name) \
    354 	mutex_init(&(_st)->st_scanlock, MUTEX_DEFAULT, IPL_SOFTNET)
    355 #define	IEEE80211_SCAN_ITER_LOCK_DESTROY(_st)	mutex_destroy(&(_st)->st_scanlock)
    356 #define	IEEE80211_SCAN_ITER_LOCK(_st)		mutex_enter(&(_st)->st_scanlock)
    357 #define	IEEE80211_SCAN_ITER_UNLOCK(_st)	mutex_exit(&(_st)->st_scanlock)
    358 
    359 /*
    360  * Mesh node/routing definitions.
    361  */
    362 typedef kmutex_t ieee80211_rte_lock_t;
    363 #define	MESH_RT_ENTRY_LOCK_INIT(_rt, _name) \
    364 	mutex_init(&(rt)->rt_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    365 #define	MESH_RT_ENTRY_LOCK_DESTROY(_rt) \
    366 	mutex_destroy(&(_rt)->rt_lock)
    367 #define	MESH_RT_ENTRY_LOCK(rt)	mutex_enter(&(rt)->rt_lock)
    368 #define	MESH_RT_ENTRY_LOCK_ASSERT(rt)   \
    369 	FBSDKASSERT(mutex_owned(&(rt)->rt_lock), ("mutex not owned"))
    370 #define	MESH_RT_ENTRY_UNLOCK(rt)	mutex_exit(&(rt)->rt_lock)
    371 
    372 typedef kmutex_t ieee80211_rt_lock_t;
    373 #define	MESH_RT_LOCK(ms)	mutex_enter(&(ms)->ms_rt_lock)
    374 #define	MESH_RT_LOCK_ASSERT(ms)	\
    375 	FBSDKASSERT(mutex_owned(&(ms)->ms_rt_lock), ("lock not owned"))
    376 #define	MESH_RT_UNLOCK(ms)	mutex_exit(&(ms)->ms_rt_lock)
    377 #define	MESH_RT_LOCK_INIT(ms, name) \
    378 	mutex_init(&(ms)->ms_rt_lock, MUTEX_DEFAULT, IPL_SOFTNET)
    379 #define	MESH_RT_LOCK_DESTROY(ms) \
    380 	mutex_destroy(&(ms)->ms_rt_lock)
    381 
    382 /*
    383  * Node reference counting definitions.
    384  *
    385  * ieee80211_node_initref	initialize the reference count to 1
    386  * ieee80211_node_incref	add a reference
    387  * ieee80211_node_decref	remove a reference
    388  * ieee80211_node_dectestref	remove a reference and return 1 if this
    389  *				is the last reference, otherwise 0
    390  * ieee80211_node_refcnt	reference count for printing (only)
    391  */
    392 
    393 #define ieee80211_node_initref(_ni) \
    394 	do { ((_ni)->ni_refcnt = 1); } while (0)
    395 #define ieee80211_node_incref(_ni) \
    396 	atomic_add_int(&(_ni)->ni_refcnt, 1)
    397 #define	ieee80211_node_decref(_ni) \
    398 	atomic_subtract_int(&(_ni)->ni_refcnt, 1)
    399 struct ieee80211_node;
    400 int	ieee80211_node_dectestref(struct ieee80211_node *ni);
    401 #define	ieee80211_node_refcnt(_ni)	(_ni)->ni_refcnt
    402 
    403 /*
    404  * Media locking definitions.
    405  */
    406 typedef kmutex_t ieee80211_media_lock_t;
    407 
    408 /*
    409  * Media locking definitions.
    410  */
    411 typedef kmutex_t ieee80211_media_lock_t;
    412 
    413 struct ifqueue;
    414 struct ieee80211vap;
    415 void	ieee80211_drain_ifq(struct ifqueue *);
    416 void	ieee80211_flush_ifq(struct ifqueue *, struct ieee80211vap *);
    417 
    418 void	ieee80211_vap_destroy(struct ieee80211vap *);
    419 
    420 #define	IFNET_IS_UP_RUNNING(_ifp) \
    421 	(((_ifp)->if_flags & IFF_UP) && \
    422 	 ((_ifp)->if_flags & IFF_RUNNING))
    423 
    424 /* XXX TODO: cap these at 1, as hz may not be 1000 */
    425 #define	msecs_to_ticks(ms)	(((ms)*hz)/1000)
    426 #define	ticks_to_msecs(t)	(1000*(t) / hz)
    427 #define	ticks_to_secs(t)	((t) / hz)
    428 
    429 #define ieee80211_time_after(a,b) 	((long)(b) - (long)(a) < 0)
    430 #define ieee80211_time_before(a,b)	ieee80211_time_after(b,a)
    431 #define ieee80211_time_after_eq(a,b)	((long)(a) - (long)(b) >= 0)
    432 #define ieee80211_time_before_eq(a,b)	ieee80211_time_after_eq(b,a)
    433 
    434 struct mbuf *ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen);
    435 
    436 /* tx path usage */
    437 #define	M_ENCAP		M_LINK0		/* 802.11 encap done */
    438 #define	M_EAPOL		M_LINK3		/* PAE/EAPOL frame */
    439 #define	M_PWR_SAV	M_LINK4		/* bypass PS handling */
    440 #define	M_MORE_DATA	M_LINK5		/* more data frames to follow */
    441 #define	M_FF		M_LINK6		/* fast frame / A-MSDU */
    442 #define	M_TXCB		M_LINK7		/* do tx complete callback */
    443 #define	M_AMPDU_MPDU	M_LINK8		/* ok for A-MPDU aggregation */
    444 #define	M_FRAG		M_LINK9		/* frame fragmentation */
    445 #define	M_FIRSTFRAG	M_LINK10	/* first frame fragment */
    446 #define	M_LASTFRAG	M_LINK11	/* last frame fragment */
    447 
    448 #define	M_80211_TX \
    449 	(M_ENCAP|M_EAPOL|M_PWR_SAV|M_MORE_DATA|M_FF|M_TXCB| \
    450 	 M_AMPDU_MPDU|M_FRAG|M_FIRSTFRAG|M_LASTFRAG)
    451 
    452 /* rx path usage */
    453 #define	M_AMPDU		M_LINK1		/* A-MPDU subframe */
    454 #define	M_WEP		M_LINK2		/* WEP done by hardware */
    455 #if 0
    456 #define	M_AMPDU_MPDU	M_LINK8		/* A-MPDU re-order done */
    457 #endif
    458 #define	M_80211_RX	(M_AMPDU|M_WEP|M_AMPDU_MPDU)
    459 
    460 #define	IEEE80211_MBUF_TX_FLAG_BITS \
    461 	M_FLAG_BITS \
    462 	"\15M_ENCAP\17M_EAPOL\20M_PWR_SAV\21M_MORE_DATA\22M_FF\23M_TXCB" \
    463 	"\24M_AMPDU_MPDU\25M_FRAG\26M_FIRSTFRAG\27M_LASTFRAG"
    464 
    465 #define	IEEE80211_MBUF_RX_FLAG_BITS \
    466 	M_FLAG_BITS \
    467 	"\15M_AMPDU\16M_WEP\24M_AMPDU_MPDU"
    468 
    469 /*
    470  * Store WME access control bits in the vlan tag.
    471  * This is safe since it's done after the packet is classified
    472  * (where we use any previous tag) and because it's passed
    473  * directly in to the driver and there's no chance someone
    474  * else will clobber them on us.
    475  */
    476 #define	M_WME_SETAC(m, ac) \
    477 	((m)->m_pkthdr.ether_vtag = (ac))
    478 #define	M_WME_GETAC(m)	((m)->m_pkthdr.ether_vtag)
    479 
    480 /*
    481  * Mbufs on the power save queue are tagged with an age and
    482  * timed out.  We reuse the hardware checksum field in the
    483  * mbuf packet header to store this data.
    484  */
    485 #define	M_AGE_SET(m,v)		(m->m_pkthdr.csum_data = v)
    486 #define	M_AGE_GET(m)		(m->m_pkthdr.csum_data)
    487 #define	M_AGE_SUB(m,adj)	(m->m_pkthdr.csum_data -= adj)
    488 
    489 /*
    490  * Store the sequence number.  XXX?  correct to use segsz?
    491  */
    492 #define	M_SEQNO_SET(m, seqno) \
    493 	((m)->m_pkthdr.segsz = (seqno))
    494 #define	M_SEQNO_GET(m)	((m)->m_pkthdr.segsz)
    495 
    496 #define	MTAG_ABI_NET80211	1132948340	/* net80211 ABI */
    497 
    498 struct ieee80211_cb {
    499 	void	(*func)(struct ieee80211_node *, void *, int status);
    500 	void	*arg;
    501 };
    502 #define	NET80211_TAG_CALLBACK	0	/* xmit complete callback */
    503 int	ieee80211_add_callback(struct mbuf *m,
    504 		void (*func)(struct ieee80211_node *, void *, int), void *arg);
    505 void	ieee80211_process_callback(struct ieee80211_node *, struct mbuf *, int);
    506 
    507 #define	NET80211_TAG_XMIT_PARAMS	1
    508 /* See below; this is after the bpf_params definition */
    509 
    510 #define	NET80211_TAG_RECV_PARAMS	2
    511 
    512 #define	NET80211_TAG_TOA_PARAMS		3
    513 
    514 struct ieee80211com;
    515 int	ieee80211_parent_xmitpkt(struct ieee80211com *, struct mbuf *);
    516 int	ieee80211_vap_xmitpkt(struct ieee80211vap *, struct mbuf *);
    517 
    518 void	get_random_bytes(void *, size_t);
    519 
    520 void	ieee80211_sysctl_attach(struct ieee80211com *);
    521 void	ieee80211_sysctl_detach(struct ieee80211com *);
    522 void	ieee80211_sysctl_vattach(struct ieee80211vap *);
    523 void	ieee80211_sysctl_vdetach(struct ieee80211vap *);
    524 
    525 #if notyet
    526 SYSCTL_DECL(_net_wlan);
    527 int	ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS);
    528 #endif
    529 
    530 
    531 #ifdef notyet
    532 
    533 /*
    534  * A "policy module" is an adjunct module to net80211 that provides
    535  * functionality that typically includes policy decisions.  This
    536  * modularity enables extensibility and vendor-supplied functionality.
    537  */
    538 #define	_IEEE80211_POLICY_MODULE(policy, name, version)			\
    539 typedef void (*policy##_setup)(int);					\
    540 SET_DECLARE(policy##_set, policy##_setup);				\
    541 static int								\
    542 wlan_##name##_modevent(module_t mod, int type, void *unused)		\
    543 {									\
    544 	policy##_setup * const *iter, f;				\
    545 	switch (type) {							\
    546 	case MOD_LOAD:							\
    547 		SET_FOREACH(iter, policy##_set) {			\
    548 			f = (void*) *iter;				\
    549 			f(type);					\
    550 		}							\
    551 		return 0;						\
    552 	case MOD_UNLOAD:						\
    553 	case MOD_QUIESCE:						\
    554 		if (nrefs) {						\
    555 			printf("wlan_" #name ": still in use "		\
    556 				"(%u dynamic refs)\n", nrefs);		\
    557 			return EBUSY;					\
    558 		}							\
    559 		if (type == MOD_UNLOAD) {				\
    560 			SET_FOREACH(iter, policy##_set) {		\
    561 				f = (void*) *iter;			\
    562 				f(type);				\
    563 			}						\
    564 		}							\
    565 		return 0;						\
    566 	}								\
    567 	return EINVAL;							\
    568 }									\
    569 static moduledata_t name##_mod = {					\
    570 	"wlan_" #name,							\
    571 	wlan_##name##_modevent,						\
    572 	0								\
    573 };									\
    574 DECLARE_MODULE(wlan_##name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
    575 MODULE_VERSION(wlan_##name, version);					\
    576 MODULE_DEPEND(wlan_##name, wlan, 1, 1, 1)
    577 
    578 /*
    579  * Crypto modules implement cipher support.
    580  */
    581 #define	IEEE80211_CRYPTO_MODULE(name, version)				\
    582 _IEEE80211_POLICY_MODULE(crypto, name, version);			\
    583 static void								\
    584 name##_modevent(int type)						\
    585 {									\
    586 	if (type == MOD_LOAD)						\
    587 		ieee80211_crypto_register(&name);			\
    588 	else								\
    589 		ieee80211_crypto_unregister(&name);			\
    590 }									\
    591 TEXT_SET(crypto##_set, name##_modevent)
    592 
    593 /*
    594  * Scanner modules provide scanning policy.
    595  */
    596 #define	IEEE80211_SCANNER_MODULE(name, version)				\
    597 	_IEEE80211_POLICY_MODULE(scanner, name, version)
    598 
    599 #define	IEEE80211_SCANNER_ALG(name, alg, v)				\
    600 static void								\
    601 name##_modevent(int type)						\
    602 {									\
    603 	if (type == MOD_LOAD)						\
    604 		ieee80211_scanner_register(alg, &v);			\
    605 	else								\
    606 		ieee80211_scanner_unregister(alg, &v);			\
    607 }									\
    608 TEXT_SET(scanner_set, name##_modevent);					\
    609 
    610 /*
    611  * ACL modules implement acl policy.
    612  */
    613 #define	IEEE80211_ACL_MODULE(name, alg, version)			\
    614 _IEEE80211_POLICY_MODULE(acl, name, version);				\
    615 static void								\
    616 alg##_modevent(int type)						\
    617 {									\
    618 	if (type == MOD_LOAD)						\
    619 		ieee80211_aclator_register(&alg);			\
    620 	else								\
    621 		ieee80211_aclator_unregister(&alg);			\
    622 }									\
    623 TEXT_SET(acl_set, alg##_modevent);					\
    624 
    625 /*
    626  * Authenticator modules handle 802.1x/WPA authentication.
    627  */
    628 #define	IEEE80211_AUTH_MODULE(name, version)				\
    629 	_IEEE80211_POLICY_MODULE(auth, name, version)
    630 
    631 #define	IEEE80211_AUTH_ALG(name, alg, v)				\
    632 static void								\
    633 name##_modevent(int type)						\
    634 {									\
    635 	if (type == MOD_LOAD)						\
    636 		ieee80211_authenticator_register(alg, &v);		\
    637 	else								\
    638 		ieee80211_authenticator_unregister(alg);		\
    639 }									\
    640 TEXT_SET(auth_set, name##_modevent)
    641 
    642 /*
    643  * Rate control modules provide tx rate control support.
    644  */
    645 #define	IEEE80211_RATECTL_MODULE(alg, version)				\
    646 	_IEEE80211_POLICY_MODULE(ratectl, alg, version);		\
    647 
    648 #define	IEEE80211_RATECTL_ALG(name, alg, v)				\
    649 static void								\
    650 alg##_modevent(int type)						\
    651 {									\
    652 	if (type == MOD_LOAD)						\
    653 		ieee80211_ratectl_register(alg, &v);			\
    654 	else								\
    655 		ieee80211_ratectl_unregister(alg);			\
    656 }									\
    657 TEXT_SET(ratectl##_set, alg##_modevent)
    658 
    659 #else
    660 /* NNN This looks like module load/unload support ... notyet supported  */
    661 #define _IEEE80211_POLICY_MODULE(policy, name, version)	/* unsupported */
    662 #define IEEE80211_CRYPTO_MODULE(name, version) 		/* unsupported */
    663 #define IEEE80211_SCANNER_MODULE(name, version) 	/* unsupported */
    664 #define IEEE80211_SCANNER_ALG(name, alg, v) 		/* unsupported */
    665 #define IEEE80211_ACL_MODULE(name, alg, version) 	const void *const temp = &alg
    666 #define IEEE80211_AUTH_MODULE(name, version)          	/* unsupported */
    667 #define IEEE80211_AUTH_ALG(name, alg, v) 		/* unsupported */
    668 #define IEEE80211_RATECTL_MODULE(alg, version)          /* unsupported */
    669 #define IEEE80211_RATECTL_ALG(name, alg, v)  		/* unsupported */
    670 #endif
    671 
    672 /*
    673  * IOCTL support
    674  */
    675 
    676 struct ieee80211req;
    677 
    678 typedef int ieee80211_ioctl_getfunc(struct ieee80211vap *,  struct ieee80211req *);
    679 #if notyet
    680 SET_DECLARE(ieee80211_ioctl_getset, ieee80211_ioctl_getfunc);
    681 #endif
    682 #define	IEEE80211_IOCTL_GET(_name, _get) TEXT_SET(ieee80211_ioctl_getset, _get)
    683 
    684 typedef int ieee80211_ioctl_setfunc(struct ieee80211vap *,  struct ieee80211req *);
    685 #if notyet
    686 SET_DECLARE(ieee80211_ioctl_setset, ieee80211_ioctl_setfunc);
    687 #endif
    688 #define	IEEE80211_IOCTL_SET(_name, _set) TEXT_SET(ieee80211_ioctl_setset, _set)
    689 
    690 #endif /* _KERNEL */
    691 
    692 /* XXX this stuff belongs elsewhere */
    693 /*
    694  * Message formats for messages from the net80211 layer to user
    695  * applications via the routing socket.  These messages are appended
    696  * to an if_announcemsghdr structure.
    697  */
    698 struct ieee80211_join_event {
    699 	uint8_t		iev_addr[6];
    700 };
    701 
    702 struct ieee80211_leave_event {
    703 	uint8_t		iev_addr[6];
    704 };
    705 
    706 struct ieee80211_replay_event {
    707 	uint8_t		iev_src[6];	/* src MAC */
    708 	uint8_t		iev_dst[6];	/* dst MAC */
    709 	uint8_t		iev_cipher;	/* cipher type */
    710 	uint8_t		iev_keyix;	/* key id/index */
    711 	uint64_t	iev_keyrsc;	/* RSC from key */
    712 	uint64_t	iev_rsc;	/* RSC from frame */
    713 };
    714 
    715 struct ieee80211_michael_event {
    716 	uint8_t		iev_src[6];	/* src MAC */
    717 	uint8_t		iev_dst[6];	/* dst MAC */
    718 	uint8_t		iev_cipher;	/* cipher type */
    719 	uint8_t		iev_keyix;	/* key id/index */
    720 };
    721 
    722 struct ieee80211_wds_event {
    723 	uint8_t		iev_addr[6];
    724 };
    725 
    726 struct ieee80211_csa_event {
    727 	uint32_t	iev_flags;	/* channel flags */
    728 	uint16_t	iev_freq;	/* setting in Mhz */
    729 	uint8_t		iev_ieee;	/* IEEE channel number */
    730 	uint8_t		iev_mode;	/* CSA mode */
    731 	uint8_t		iev_count;	/* CSA count */
    732 };
    733 
    734 struct ieee80211_cac_event {
    735 	uint32_t	iev_flags;	/* channel flags */
    736 	uint16_t	iev_freq;	/* setting in Mhz */
    737 	uint8_t		iev_ieee;	/* IEEE channel number */
    738 	/* XXX timestamp? */
    739 	uint8_t		iev_type;	/* IEEE80211_NOTIFY_CAC_* */
    740 };
    741 
    742 struct ieee80211_radar_event {
    743 	uint32_t	iev_flags;	/* channel flags */
    744 	uint16_t	iev_freq;	/* setting in Mhz */
    745 	uint8_t		iev_ieee;	/* IEEE channel number */
    746 	/* XXX timestamp? */
    747 };
    748 
    749 struct ieee80211_auth_event {
    750 	uint8_t		iev_addr[6];
    751 };
    752 
    753 struct ieee80211_deauth_event {
    754 	uint8_t		iev_addr[6];
    755 };
    756 
    757 struct ieee80211_country_event {
    758 	uint8_t		iev_addr[6];
    759 	uint8_t		iev_cc[2];	/* ISO country code */
    760 };
    761 
    762 struct ieee80211_radio_event {
    763 	uint8_t		iev_state;	/* 1 on, 0 off */
    764 };
    765 
    766 #define	RTM_IEEE80211_ASSOC	100	/* station associate (bss mode) */
    767 #define	RTM_IEEE80211_REASSOC	101	/* station re-associate (bss mode) */
    768 #define	RTM_IEEE80211_DISASSOC	102	/* station disassociate (bss mode) */
    769 #define	RTM_IEEE80211_JOIN	103	/* station join (ap mode) */
    770 #define	RTM_IEEE80211_LEAVE	104	/* station leave (ap mode) */
    771 #define	RTM_IEEE80211_SCAN	105	/* scan complete, results available */
    772 #define	RTM_IEEE80211_REPLAY	106	/* sequence counter replay detected */
    773 #define	RTM_IEEE80211_MICHAEL	107	/* Michael MIC failure detected */
    774 #define	RTM_IEEE80211_REJOIN	108	/* station re-associate (ap mode) */
    775 #define	RTM_IEEE80211_WDS	109	/* WDS discovery (ap mode) */
    776 #define	RTM_IEEE80211_CSA	110	/* Channel Switch Announcement event */
    777 #define	RTM_IEEE80211_RADAR	111	/* radar event */
    778 #define	RTM_IEEE80211_CAC	112	/* Channel Availability Check event */
    779 #define	RTM_IEEE80211_DEAUTH	113	/* station deauthenticate */
    780 #define	RTM_IEEE80211_AUTH	114	/* station authenticate (ap mode) */
    781 #define	RTM_IEEE80211_COUNTRY	115	/* discovered country code (sta mode) */
    782 #define	RTM_IEEE80211_RADIO	116	/* RF kill switch state change */
    783 
    784 /*
    785  * Structure prepended to raw packets sent through the bpf
    786  * interface when set to DLT_IEEE802_11_RADIO.  This allows
    787  * user applications to specify pretty much everything in
    788  * an Atheros tx descriptor.  XXX need to generalize.
    789  *
    790  * XXX cannot be more than 14 bytes as it is copied to a sockaddr's
    791  * XXX sa_data area.
    792  */
    793 struct ieee80211_bpf_params {
    794 	uint8_t		ibp_vers;	/* version */
    795 #define	IEEE80211_BPF_VERSION	0
    796 	uint8_t		ibp_len;	/* header length in bytes */
    797 	uint8_t		ibp_flags;
    798 #define	IEEE80211_BPF_SHORTPRE	0x01	/* tx with short preamble */
    799 #define	IEEE80211_BPF_NOACK	0x02	/* tx with no ack */
    800 #define	IEEE80211_BPF_CRYPTO	0x04	/* tx with h/w encryption */
    801 #define	IEEE80211_BPF_FCS	0x10	/* frame incldues FCS */
    802 #define	IEEE80211_BPF_DATAPAD	0x20	/* frame includes data padding */
    803 #define	IEEE80211_BPF_RTS	0x40	/* tx with RTS/CTS */
    804 #define	IEEE80211_BPF_CTS	0x80	/* tx with CTS only */
    805 	uint8_t		ibp_pri;	/* WME/WMM AC+tx antenna */
    806 	uint8_t		ibp_try0;	/* series 1 try count */
    807 	uint8_t		ibp_rate0;	/* series 1 IEEE tx rate */
    808 	uint8_t		ibp_power;	/* tx power (device units) */
    809 	uint8_t		ibp_ctsrate;	/* IEEE tx rate for CTS */
    810 	uint8_t		ibp_try1;	/* series 2 try count */
    811 	uint8_t		ibp_rate1;	/* series 2 IEEE tx rate */
    812 	uint8_t		ibp_try2;	/* series 3 try count */
    813 	uint8_t		ibp_rate2;	/* series 3 IEEE tx rate */
    814 	uint8_t		ibp_try3;	/* series 4 try count */
    815 	uint8_t		ibp_rate3;	/* series 4 IEEE tx rate */
    816 };
    817 
    818 #ifdef _KERNEL
    819 struct ieee80211_tx_params {
    820 	struct ieee80211_bpf_params params;
    821 };
    822 int	ieee80211_add_xmit_params(struct mbuf *m,
    823 	    const struct ieee80211_bpf_params *);
    824 int	ieee80211_get_xmit_params(struct mbuf *m,
    825 	    struct ieee80211_bpf_params *);
    826 
    827 struct ieee80211_rx_params;
    828 struct ieee80211_rx_stats;
    829 
    830 int	ieee80211_add_rx_params(struct mbuf *m,
    831 	    const struct ieee80211_rx_stats *rxs);
    832 int	ieee80211_get_rx_params(struct mbuf *m,
    833 	    struct ieee80211_rx_stats *rxs);
    834 const struct ieee80211_rx_stats * ieee80211_get_rx_params_ptr(struct mbuf *m);
    835 
    836 struct ieee80211_toa_params {
    837 	int request_id;
    838 };
    839 int	ieee80211_add_toa_params(struct mbuf *m,
    840 	    const struct ieee80211_toa_params *p);
    841 int	ieee80211_get_toa_params(struct mbuf *m,
    842 	    struct ieee80211_toa_params *p);
    843 
    844 #define	IEEE80211_F_SURVEY_TIME		0x00000001
    845 #define	IEEE80211_F_SURVEY_TIME_BUSY	0x00000002
    846 #define	IEEE80211_F_SURVEY_NOISE_DBM	0x00000004
    847 #define	IEEE80211_F_SURVEY_TSC		0x00000008
    848 struct ieee80211_channel_survey {
    849 	uint32_t s_flags;
    850 	uint32_t s_time;
    851 	uint32_t s_time_busy;
    852 	int32_t s_noise;
    853 	uint64_t s_tsc;
    854 };
    855 
    856 
    857 /*
    858  * Malloc API.  Other BSD operating systems have slightly
    859  * different malloc/free namings (eg DragonflyBSD.)
    860  */
    861 #define	IEEE80211_MALLOC	malloc
    862 #define	IEEE80211_FREE		free
    863 
    864 /* XXX TODO: get rid of WAITOK, fix all the users of it? */
    865 #define	IEEE80211_M_NOWAIT	M_NOWAIT
    866 #define	IEEE80211_M_WAITOK	M_WAITOK
    867 #define	IEEE80211_M_ZERO	M_ZERO
    868 
    869 /* XXX TODO: the type fields */
    870 
    871 /*
    872  * Startup function for NetBSD
    873  */
    874 
    875 int ieee80211_init0(void);
    876 
    877 
    878 /*
    879  * Functions FreeBSD uses that NetBSD doesn't have ...
    880  */
    881 
    882 int	if_printf(struct ifnet *ifp, const char *fmt, ...)  __printflike(2, 3);
    883 void	m_align(struct mbuf *m, int len);
    884 int	m_append(struct mbuf *m0, int len, const void *cpv);
    885 struct mbuf * m_unshare(struct mbuf *m0, int how);
    886 
    887 static __inline void m_clrprotoflags(struct mbuf *m)
    888 {
    889 	m->m_flags &= ~(M_LINK0|M_LINK1|M_LINK2|M_LINK3|M_LINK4|M_LINK5
    890 	    |M_LINK6|M_LINK7|M_LINK8|M_LINK9|M_LINK10|M_LINK11);
    891 }
    892 
    893 /*-
    894  * Macro for type conversion: convert mbuf pointer to data pointer of correct
    895  * type:
    896  *
    897  * mtod(m, t)   -- Convert mbuf pointer to data pointer of correct type.
    898  * mtodo(m, o) -- Same as above but with offset 'o' into data.
    899  */
    900 #define mtod(m, t)      ((t)((m)->m_data))
    901 #define mtodo(m, o)     ((void *)(((m)->m_data) + (o)))
    902 
    903 /* Berkeley Packet Filter shim  */
    904 
    905 #define BPF_MTAP(_ifp,_m) do {                          \
    906 	bpf_mtap((_ifp), (_m), BPF_D_INOUT);            \
    907 } while (0)
    908 
    909 /*  Missing define in net/if_ether.h   */
    910 
    911 #define ETHER_IS_BROADCAST(addr) \
    912         (((addr)[0] & (addr)[1] & (addr)[2] & \
    913           (addr)[3] & (addr)[4] & (addr)[5]) == 0xff)
    914 
    915 #endif /* _KERNEL */
    916 
    917 #endif /* _NET80211_IEEE80211_NETBSD_H_ */
    918