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