code:
/*-
 * Copyright (c) 2001 Atsushi Onoe
 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_ioctl.h 223145 2011-06-16 09:37:20Z kevlo $
 */
#ifndef _NET80211_IEEE80211_IOCTL_H_
#define _NET80211_IEEE80211_IOCTL_H_

/*
 * IEEE 802.11 ioctls.
 */
#include <net80211/_ieee80211.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_crypto.h>

/*
 * Per/node (station) statistics.
 */
struct ieee80211_nodestats {
	uint32_t	ns_rx_data;		/* rx data frames */
	uint32_t	ns_rx_mgmt;		/* rx management frames */
	uint32_t	ns_rx_ctrl;		/* rx control frames */
	uint32_t	ns_rx_ucast;		/* rx unicast frames */
	uint32_t	ns_rx_mcast;		/* rx multi/broadcast frames */
	uint64_t	ns_rx_bytes;		/* rx data count (bytes) */
	uint64_t	ns_rx_beacons;		/* rx beacon frames */
	uint32_t	ns_rx_proberesp;	/* rx probe response frames */

	uint32_t	ns_rx_dup;		/* rx discard 'cuz dup */
	uint32_t	ns_rx_noprivacy;	/* rx w/ wep but privacy off */
	uint32_t	ns_rx_wepfail;		/* rx wep processing failed */
	uint32_t	ns_rx_demicfail;	/* rx demic failed */
	uint32_t	ns_rx_decap;		/* rx decapsulation failed */
	uint32_t	ns_rx_defrag;		/* rx defragmentation failed */
	uint32_t	ns_rx_disassoc;		/* rx disassociation */
	uint32_t	ns_rx_deauth;		/* rx deauthentication */
	uint32_t	ns_rx_action;		/* rx action */
	uint32_t	ns_rx_decryptcrc;	/* rx decrypt failed on crc */
	uint32_t	ns_rx_unauth;		/* rx on unauthorized port */
	uint32_t	ns_rx_unencrypted;	/* rx unecrypted w/ privacy */
	uint32_t	ns_rx_drop;		/* rx discard other reason */

	uint32_t	ns_tx_data;		/* tx data frames */
	uint32_t	ns_tx_mgmt;		/* tx management frames */
	uint32_t	ns_tx_ctrl;		/* tx control frames */
	uint32_t	ns_tx_ucast;		/* tx unicast frames */
	uint32_t	ns_tx_mcast;		/* tx multi/broadcast frames */
	uint64_t	ns_tx_bytes;		/* tx data count (bytes) */
	uint32_t	ns_tx_probereq;		/* tx probe request frames */

	uint32_t	ns_tx_novlantag;	/* tx discard 'cuz no tag */
	uint32_t	ns_tx_vlanmismatch;	/* tx discard 'cuz bad tag */

	uint32_t	ns_ps_discard;		/* ps discard 'cuz of age */

	/* MIB-related state */
	uint32_t	ns_tx_assoc;		/* [re]associations */
	uint32_t	ns_tx_assoc_fail;	/* [re]association failures */
	uint32_t	ns_tx_auth;		/* [re]authentications */
	uint32_t	ns_tx_auth_fail;	/* [re]authentication failures*/
	uint32_t	ns_tx_deauth;		/* deauthentications */
	uint32_t	ns_tx_deauth_code;	/* last deauth reason */
	uint32_t	ns_tx_disassoc;		/* disassociations */
	uint32_t	ns_tx_disassoc_code;	/* last disassociation reason */
	uint32_t	ns_spare[8];
};

/*
 * Summary statistics.
 */
struct ieee80211_stats {
	uint32_t	is_rx_badversion;	/* rx frame with bad version */
	uint32_t	is_rx_tooshort;		/* rx frame too short */
	uint32_t	is_rx_wrongbss;		/* rx from wrong bssid */
	uint32_t	is_rx_dup;		/* rx discard 'cuz dup */
	uint32_t	is_rx_wrongdir;		/* rx w/ wrong direction */
	uint32_t	is_rx_mcastecho;	/* rx discard 'cuz mcast echo */
	uint32_t	is_rx_notassoc;		/* rx discard 'cuz sta !assoc */
	uint32_t	is_rx_noprivacy;	/* rx w/ wep but privacy off */
	uint32_t	is_rx_unencrypted;	/* rx w/o wep and privacy on */
	uint32_t	is_rx_wepfail;		/* rx wep processing failed */
	uint32_t	is_rx_decap;		/* rx decapsulation failed */
	uint32_t	is_rx_mgtdiscard;	/* rx discard mgt frames */
	uint32_t	is_rx_ctl;		/* rx ctrl frames */
	uint32_t	is_rx_beacon;		/* rx beacon frames */
	uint32_t	is_rx_rstoobig;		/* rx rate set truncated */
	uint32_t	is_rx_elem_missing;	/* rx required element missing*/
	uint32_t	is_rx_elem_toobig;	/* rx element too big */
	uint32_t	is_rx_elem_toosmall;	/* rx element too small */
	uint32_t	is_rx_elem_unknown;	/* rx element unknown */
	uint32_t	is_rx_badchan;		/* rx frame w/ invalid chan */
	uint32_t	is_rx_chanmismatch;	/* rx frame chan mismatch */
	uint32_t	is_rx_nodealloc;	/* rx frame dropped */
	uint32_t	is_rx_ssidmismatch;	/* rx frame ssid mismatch  */
	uint32_t	is_rx_auth_unsupported;	/* rx w/ unsupported auth alg */
	uint32_t	is_rx_auth_fail;	/* rx sta auth failure */
	uint32_t	is_rx_auth_countermeasures;/* rx auth discard 'cuz CM */
	uint32_t	is_rx_assoc_bss;	/* rx assoc from wrong bssid */
	uint32_t	is_rx_assoc_notauth;	/* rx assoc w/o auth */
	uint32_t	is_rx_assoc_capmismatch;/* rx assoc w/ cap mismatch */
	uint32_t	is_rx_assoc_norate;	/* rx assoc w/ no rate match */
	uint32_t	is_rx_assoc_badwpaie;	/* rx assoc w/ bad WPA IE */
	uint32_t	is_rx_deauth;		/* rx deauthentication */
	uint32_t	is_rx_disassoc;		/* rx disassociation */
	uint32_t	is_rx_badsubtype;	/* rx frame w/ unknown subtype*/
	uint32_t	is_rx_nobuf;		/* rx failed for lack of buf */
	uint32_t	is_rx_decryptcrc;	/* rx decrypt failed on crc */
	uint32_t	is_rx_ahdemo_mgt;	/* rx discard ahdemo mgt frame*/
	uint32_t	is_rx_bad_auth;		/* rx bad auth request */
	uint32_t	is_rx_unauth;		/* rx on unauthorized port */
	uint32_t	is_rx_badkeyid;		/* rx w/ incorrect keyid */
	uint32_t	is_rx_ccmpreplay;	/* rx seq# violation (CCMP) */
	uint32_t	is_rx_ccmpformat;	/* rx format bad (CCMP) */
	uint32_t	is_rx_ccmpmic;		/* rx MIC check failed (CCMP) */
	uint32_t	is_rx_tkipreplay;	/* rx seq# violation (TKIP) */
	uint32_t	is_rx_tkipformat;	/* rx format bad (TKIP) */
	uint32_t	is_rx_tkipmic;		/* rx MIC check failed (TKIP) */
	uint32_t	is_rx_tkipicv;		/* rx ICV check failed (TKIP) */
	uint32_t	is_rx_badcipher;	/* rx failed 'cuz key type */
	uint32_t	is_rx_nocipherctx;	/* rx failed 'cuz key !setup */
	uint32_t	is_rx_acl;		/* rx discard 'cuz acl policy */
	uint32_t	is_tx_nobuf;		/* tx failed for lack of buf */
	uint32_t	is_tx_nonode;		/* tx failed for no node */
	uint32_t	is_tx_unknownmgt;	/* tx of unknown mgt frame */
	uint32_t	is_tx_badcipher;	/* tx failed 'cuz key type */
	uint32_t	is_tx_nodefkey;		/* tx failed 'cuz no defkey */
	uint32_t	is_tx_noheadroom;	/* tx failed 'cuz no space */
	uint32_t	is_tx_fragframes;	/* tx frames fragmented */
	uint32_t	is_tx_frags;		/* tx fragments created */
	uint32_t	is_scan_active;		/* active scans started */
	uint32_t	is_scan_passive;	/* passive scans started */
	uint32_t	is_node_timeout;	/* nodes timed out inactivity */
	uint32_t	is_crypto_nomem;	/* no memory for crypto ctx */
	uint32_t	is_crypto_tkip;		/* tkip crypto done in s/w */
	uint32_t	is_crypto_tkipenmic;	/* tkip en-MIC done in s/w */
	uint32_t	is_crypto_tkipdemic;	/* tkip de-MIC done in s/w */
	uint32_t	is_crypto_tkipcm;	/* tkip counter measures */
	uint32_t	is_crypto_ccmp;		/* ccmp crypto done in s/w */
	uint32_t	is_crypto_wep;		/* wep crypto done in s/w */
	uint32_t	is_crypto_setkey_cipher;/* cipher rejected key */
	uint32_t	is_crypto_setkey_nokey;	/* no key index for setkey */
	uint32_t	is_crypto_delkey;	/* driver key delete failed */
	uint32_t	is_crypto_badcipher;	/* unknown cipher */
	uint32_t	is_crypto_nocipher;	/* cipher not available */
	uint32_t	is_crypto_attachfail;	/* cipher attach failed */
	uint32_t	is_crypto_swfallback;	/* cipher fallback to s/w */
	uint32_t	is_crypto_keyfail;	/* driver key alloc failed */
	uint32_t	is_crypto_enmicfail;	/* en-MIC failed */
	uint32_t	is_ibss_capmismatch;	/* merge failed-cap mismatch */
	uint32_t	is_ibss_norate;		/* merge failed-rate mismatch */
	uint32_t	is_ps_unassoc;		/* ps-poll for unassoc. sta */
	uint32_t	is_ps_badaid;		/* ps-poll w/ incorrect aid */
	uint32_t	is_ps_qempty;		/* ps-poll w/ nothing to send */
	uint32_t	is_ff_badhdr;		/* fast frame rx'd w/ bad hdr */
	uint32_t	is_ff_tooshort;		/* fast frame rx decap error */
	uint32_t	is_ff_split;		/* fast frame rx split error */
	uint32_t	is_ff_decap;		/* fast frames decap'd */
	uint32_t	is_ff_encap;		/* fast frames encap'd for tx */
	uint32_t	is_rx_badbintval;	/* rx frame w/ bogus bintval */
	uint32_t	is_rx_demicfail;	/* rx demic failed */
	uint32_t	is_rx_defrag;		/* rx defragmentation failed */
	uint32_t	is_rx_mgmt;		/* rx management frames */
	uint32_t	is_rx_action;		/* rx action mgt frames */
	uint32_t	is_amsdu_tooshort;	/* A-MSDU rx decap error */
	uint32_t	is_amsdu_split;		/* A-MSDU rx split error */
	uint32_t	is_amsdu_decap;		/* A-MSDU decap'd */
	uint32_t	is_amsdu_encap;		/* A-MSDU encap'd for tx */
	uint32_t	is_ampdu_bar_bad;	/* A-MPDU BAR out of window */
	uint32_t	is_ampdu_bar_oow;	/* A-MPDU BAR before ADDBA */
	uint32_t	is_ampdu_bar_move;	/* A-MPDU BAR moved window */
	uint32_t	is_ampdu_bar_rx;	/* A-MPDU BAR frames handled */
	uint32_t	is_ampdu_rx_flush;	/* A-MPDU frames flushed */
	uint32_t	is_ampdu_rx_oor;	/* A-MPDU frames out-of-order */
	uint32_t	is_ampdu_rx_copy;	/* A-MPDU frames copied down */
	uint32_t	is_ampdu_rx_drop;	/* A-MPDU frames dropped */
	uint32_t	is_tx_badstate;		/* tx discard state != RUN */
	uint32_t	is_tx_notassoc;		/* tx failed, sta not assoc */
	uint32_t	is_tx_classify;		/* tx classification failed */
	uint32_t	is_dwds_mcast;		/* discard mcast over dwds */
	uint32_t	is_dwds_qdrop;		/* dwds pending frame q full */
	uint32_t	is_ht_assoc_nohtcap;	/* non-HT sta rejected */
	uint32_t	is_ht_assoc_downgrade;	/* HT sta forced to legacy */
	uint32_t	is_ht_assoc_norate;	/* HT assoc w/ rate mismatch */
	uint32_t	is_ampdu_rx_age;	/* A-MPDU sent up 'cuz of age */
	uint32_t	is_ampdu_rx_move;	/* A-MPDU MSDU moved window */
	uint32_t	is_addba_reject;	/* ADDBA reject 'cuz disabled */
	uint32_t	is_addba_norequest;	/* ADDBA response w/o ADDBA */
	uint32_t	is_addba_badtoken;	/* ADDBA response w/ wrong
						   dialogtoken */
	uint32_t	is_addba_badpolicy;	/* ADDBA resp w/ wrong policy */
	uint32_t	is_ampdu_stop;		/* A-MPDU stream stopped */
	uint32_t	is_ampdu_stop_failed;	/* A-MPDU stream not running */
	uint32_t	is_ampdu_rx_reorder;	/* A-MPDU held for rx reorder */
	uint32_t	is_scan_bg;		/* background scans started */
	uint8_t		is_rx_deauth_code;	/* last rx'd deauth reason */
	uint8_t		is_rx_disassoc_code;	/* last rx'd disassoc reason */
	uint8_t		is_rx_authfail_code;	/* last rx'd auth fail reason */
	uint32_t	is_beacon_miss;		/* beacon miss notification */
	uint32_t	is_rx_badstate;		/* rx discard state != RUN */
	uint32_t	is_ff_flush;		/* ff's flush'd from stageq */
	uint32_t	is_tx_ctl;		/* tx ctrl frames */
	uint32_t	is_ampdu_rexmt;		/* A-MPDU frames rexmt ok */
	uint32_t	is_ampdu_rexmt_fail;	/* A-MPDU frames rexmt fail */

	uint32_t	is_mesh_wrongmesh;	/* dropped 'cuz not mesh sta*/
	uint32_t	is_mesh_nolink;		/* dropped 'cuz link not estab*/
	uint32_t	is_mesh_fwd_ttl;	/* mesh not fwd'd 'cuz ttl 0 */
	uint32_t	is_mesh_fwd_nobuf;	/* mesh not fwd'd 'cuz no mbuf*/
	uint32_t	is_mesh_fwd_tooshort;	/* mesh not fwd'd 'cuz no hdr */
	uint32_t	is_mesh_fwd_disabled;	/* mesh not fwd'd 'cuz disabled */
	uint32_t	is_mesh_fwd_nopath;	/* mesh not fwd'd 'cuz path unknown */

	uint32_t	is_hwmp_wrongseq;	/* wrong hwmp seq no. */
	uint32_t	is_hwmp_rootreqs;	/* root PREQs sent */
	uint32_t	is_hwmp_rootrann;	/* root RANNs sent */

	uint32_t	is_mesh_badae;		/* dropped 'cuz invalid AE */
	uint32_t	is_mesh_rtaddfailed;	/* route add failed */
	uint32_t	is_mesh_notproxy;	/* dropped 'cuz not proxying */
	uint32_t	is_rx_badalign;		/* dropped 'cuz misaligned */
	uint32_t	is_hwmp_proxy;		/* PREP for proxy route */

	uint32_t	is_spare[11];
};

/*
 * Max size of optional information elements.  We artificially
 * constrain this; it's limited only by the max frame size (and
 * the max parameter size of the wireless extensions).
 */
#define	IEEE80211_MAX_OPT_IE	256

/*
 * WPA/RSN get/set key request.  Specify the key/cipher
 * type and whether the key is to be used for sending and/or
 * receiving.  The key index should be set only when working
 * with global keys (use IEEE80211_KEYIX_NONE for ``no index'').
 * Otherwise a unicast/pairwise key is specified by the bssid
 * (on a station) or mac address (on an ap).  They key length
 * must include any MIC key data; otherwise it should be no
 * more than IEEE80211_KEYBUF_SIZE.
 */
struct ieee80211req_key {
	uint8_t		ik_type;	/* key/cipher type */
	uint8_t		ik_pad;
	uint16_t	ik_keyix;	/* key index */
	uint8_t		ik_keylen;	/* key length in bytes */
	uint8_t		ik_flags;
/* NB: IEEE80211_KEY_XMIT and IEEE80211_KEY_RECV defined elsewhere */
#define	IEEE80211_KEY_DEFAULT	0x80	/* default xmit key */
	uint8_t		ik_macaddr[IEEE80211_ADDR_LEN];
	uint64_t	ik_keyrsc;	/* key receive sequence counter */
	uint64_t	ik_keytsc;	/* key transmit sequence counter */
	uint8_t		ik_keydata[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
};

/*
 * Delete a key either by index or address.  Set the index
 * to IEEE80211_KEYIX_NONE when deleting a unicast key.
 */
struct ieee80211req_del_key {
	uint8_t		idk_keyix;	/* key index */
	uint8_t		idk_macaddr[IEEE80211_ADDR_LEN];
};

/*
 * MLME state manipulation request.  IEEE80211_MLME_ASSOC
 * only makes sense when operating as a station.  The other
 * requests can be used when operating as a station or an
 * ap (to effect a station).
 */
struct ieee80211req_mlme {
	uint8_t		im_op;		/* operation to perform */
#define	IEEE80211_MLME_ASSOC		1	/* associate station */
#define	IEEE80211_MLME_DISASSOC		2	/* disassociate station */
#define	IEEE80211_MLME_DEAUTH		3	/* deauthenticate station */
#define	IEEE80211_MLME_AUTHORIZE	4	/* authorize station */
#define	IEEE80211_MLME_UNAUTHORIZE	5	/* unauthorize station */
#define	IEEE80211_MLME_AUTH		6	/* authenticate station */
	uint8_t		im_ssid_len;	/* length of optional ssid */
	uint16_t	im_reason;	/* 802.11 reason code */
	uint8_t		im_macaddr[IEEE80211_ADDR_LEN];
	uint8_t		im_ssid[IEEE80211_NWID_LEN];
};

/*
 * MAC ACL operations.
 */
enum {
	IEEE80211_MACCMD_POLICY_OPEN	= 0,	/* set policy: no ACL's */
	IEEE80211_MACCMD_POLICY_ALLOW	= 1,	/* set policy: allow traffic */
	IEEE80211_MACCMD_POLICY_DENY	= 2,	/* set policy: deny traffic */
	IEEE80211_MACCMD_FLUSH		= 3,	/* flush ACL database */
	IEEE80211_MACCMD_DETACH		= 4,	/* detach ACL policy */
	IEEE80211_MACCMD_POLICY		= 5,	/* get ACL policy */
	IEEE80211_MACCMD_LIST		= 6,	/* get ACL database */
	IEEE80211_MACCMD_POLICY_RADIUS	= 7,	/* set policy: RADIUS managed */
};

struct ieee80211req_maclist {
	uint8_t		ml_macaddr[IEEE80211_ADDR_LEN];
} __packed;

/*
 * Mesh Routing Table Operations.
 */
enum {
	IEEE80211_MESH_RTCMD_LIST   = 0, /* list HWMP routing table */
	IEEE80211_MESH_RTCMD_FLUSH  = 1, /* flush HWMP routing table */
	IEEE80211_MESH_RTCMD_ADD    = 2, /* add entry to the table */
	IEEE80211_MESH_RTCMD_DELETE = 3, /* delete an entry from the table */
};

struct ieee80211req_mesh_route {
	uint8_t		imr_flags;
#define	IEEE80211_MESHRT_FLAGS_VALID	0x01
#define	IEEE80211_MESHRT_FLAGS_PROXY	0x02
	uint8_t		imr_dest[IEEE80211_ADDR_LEN];
	uint8_t		imr_nexthop[IEEE80211_ADDR_LEN];
	uint16_t	imr_nhops;
	uint8_t		imr_pad;
	uint32_t	imr_metric;
	uint32_t	imr_lifetime;
	uint32_t	imr_lastmseq;
};

/*
 * HWMP root modes
 */
enum {
	IEEE80211_HWMP_ROOTMODE_DISABLED	= 0, 	/* disabled */
	IEEE80211_HWMP_ROOTMODE_NORMAL		= 1,	/* normal PREPs */
	IEEE80211_HWMP_ROOTMODE_PROACTIVE	= 2,	/* proactive PREPS */
	IEEE80211_HWMP_ROOTMODE_RANN		= 3,	/* use RANN elemid */
};

/*
 * Set the active channel list by IEEE channel #: each channel
 * to be marked active is set in a bit vector.  Note this list is
 * intersected with the available channel list in calculating
 * the set of channels actually used in scanning.
 */
struct ieee80211req_chanlist {
	uint8_t		ic_channels[32];	/* NB: can be variable length */
};

/*
 * Get the active channel list info.
 */
struct ieee80211req_chaninfo {
	u_int	ic_nchans;
	struct ieee80211_channel ic_chans[1];	/* NB: variable length */
};
#define	IEEE80211_CHANINFO_SIZE(_nchan) \
	(sizeof(struct ieee80211req_chaninfo) + \
	 (((_nchan)-1) * sizeof(struct ieee80211_channel)))
#define	IEEE80211_CHANINFO_SPACE(_ci) \
	IEEE80211_CHANINFO_SIZE((_ci)->ic_nchans)

/*
 * Retrieve the WPA/RSN information element for an associated station.
 */
struct ieee80211req_wpaie {	/* old version w/ only one ie */
	uint8_t		wpa_macaddr[IEEE80211_ADDR_LEN];
	uint8_t		wpa_ie[IEEE80211_MAX_OPT_IE];
};
struct ieee80211req_wpaie2 {
	uint8_t		wpa_macaddr[IEEE80211_ADDR_LEN];
	uint8_t		wpa_ie[IEEE80211_MAX_OPT_IE];
	uint8_t		rsn_ie[IEEE80211_MAX_OPT_IE];
};

/*
 * Retrieve per-node statistics.
 */
struct ieee80211req_sta_stats {
	union {
		/* NB: explicitly force 64-bit alignment */
		uint8_t		macaddr[IEEE80211_ADDR_LEN];
		uint64_t	pad;
	} is_u;
	struct ieee80211_nodestats is_stats;
};

/*
 * Station information block; the mac address is used
 * to retrieve other data like stats, unicast key, etc.
 */
struct ieee80211req_sta_info {
	uint16_t	isi_len;		/* total length (mult of 4) */
	uint16_t	isi_ie_off;		/* offset to IE data */
	uint16_t	isi_ie_len;		/* IE length */
	uint16_t	isi_freq;		/* MHz */
	uint32_t	isi_flags;		/* channel flags */
	uint32_t	isi_state;		/* state flags */
	uint8_t		isi_authmode;		/* authentication algorithm */
	int8_t		isi_rssi;		/* receive signal strength */
	int8_t		isi_noise;		/* noise floor */
	uint8_t		isi_capinfo;		/* capabilities */
	uint8_t		isi_erp;		/* ERP element */
	uint8_t		isi_macaddr[IEEE80211_ADDR_LEN];
	uint8_t		isi_nrates;
						/* negotiated rates */
	uint8_t		isi_rates[IEEE80211_RATE_MAXSIZE];
	uint8_t		isi_txrate;		/* legacy/IEEE rate or MCS */
	uint16_t	isi_associd;		/* assoc response */
	uint16_t	isi_txpower;		/* current tx power */
	uint16_t	isi_vlan;		/* vlan tag */
	/* NB: [IEEE80211_NONQOS_TID] holds seq#'s for non-QoS stations */
	uint16_t	isi_txseqs[IEEE80211_TID_SIZE];/* tx seq #/TID */
	uint16_t	isi_rxseqs[IEEE80211_TID_SIZE];/* rx seq#/TID */
	uint16_t	isi_inact;		/* inactivity timer */
	uint16_t	isi_txmbps;		/* current tx rate in .5 Mb/s */
	uint16_t	isi_pad;
	uint32_t	isi_jointime;		/* time of assoc/join */
	struct ieee80211_mimo_info isi_mimo;	/* MIMO info for 11n sta's */
	/* 11s info */
	uint16_t	isi_peerid;
	uint16_t	isi_localid;
	uint8_t		isi_peerstate;
	/* XXX frag state? */
	/* variable length IE data */
};

/*
 * Retrieve per-station information; to retrieve all
 * specify a mac address of ff:ff:ff:ff:ff:ff.
 */
struct ieee80211req_sta_req {
	union {
		/* NB: explicitly force 64-bit alignment */
		uint8_t		macaddr[IEEE80211_ADDR_LEN];
		uint64_t	pad;
	} is_u;
	struct ieee80211req_sta_info info[1];	/* variable length */
};

/*
 * Get/set per-station tx power cap.
 */
struct ieee80211req_sta_txpow {
	uint8_t		it_macaddr[IEEE80211_ADDR_LEN];
	uint8_t		it_txpow;
};

/*
 * WME parameters manipulated with IEEE80211_IOC_WME_CWMIN
 * through IEEE80211_IOC_WME_ACKPOLICY are set and return
 * using i_val and i_len.  i_val holds the value itself.
 * i_len specifies the AC and, as appropriate, then high bit
 * specifies whether the operation is to be applied to the
 * BSS or ourself.
 */
#define	IEEE80211_WMEPARAM_SELF	0x0000		/* parameter applies to self */
#define	IEEE80211_WMEPARAM_BSS	0x8000		/* parameter applies to BSS */
#define	IEEE80211_WMEPARAM_VAL	0x7fff		/* parameter value */

/*
 * Application Information Elements can be appended to a variety
 * of frames with the IEE80211_IOC_APPIE request.  This request
 * piggybacks on a normal ieee80211req; the frame type is passed
 * in i_val as the 802.11 FC0 bytes and the length of the IE data
 * is passed in i_len.  The data is referenced in i_data.  If i_len
 * is zero then any previously configured IE data is removed.  At
 * most IEEE80211_MAX_APPIE data be appened.  Note that multiple
 * IE's can be supplied; the data is treated opaquely.
 */
#define	IEEE80211_MAX_APPIE	1024		/* max app IE data */
/*
 * Hack: the WPA authenticator uses this mechanism to specify WPA
 * ie's that are used instead of the ones normally constructed using
 * the cipher state setup with separate ioctls.  This avoids issues
 * like the authenticator ordering ie data differently than the
 * net80211 layer and needing to keep separate state for WPA and RSN.
 */
#define	IEEE80211_APPIE_WPA \
	(IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_BEACON | \
	 IEEE80211_FC0_SUBTYPE_PROBE_RESP)

/*
 * Station mode roaming parameters.  These are maintained
 * per band/mode and control the roaming algorithm.
 */
struct ieee80211_roamparams_req {
	struct ieee80211_roamparam params[IEEE80211_MODE_MAX];
};

/*
 * Transmit parameters.  These can be used to set fixed transmit
 * rate for each operating mode when operating as client or on a
 * per-client basis according to the capabilities of the client
 * (e.g. an 11b client associated to an 11g ap) when operating as
 * an ap.
 *
 * MCS are distinguished from legacy rates by or'ing in 0x80.
 */
struct ieee80211_txparams_req {
	struct ieee80211_txparam params[IEEE80211_MODE_MAX];
};

/*
 * Set regulatory domain state with IEEE80211_IOC_REGDOMAIN.
 * Note this is both the regulatory description and the channel
 * list.  The get request for IEEE80211_IOC_REGDOMAIN returns
 * only the regdomain info; the channel list is obtained
 * separately with IEEE80211_IOC_CHANINFO.
 */
struct ieee80211_regdomain_req {
	struct ieee80211_regdomain	rd;
	struct ieee80211req_chaninfo	chaninfo;
};
#define	IEEE80211_REGDOMAIN_SIZE(_nchan) \
	(sizeof(struct ieee80211_regdomain_req) + \
	 (((_nchan)-1) * sizeof(struct ieee80211_channel)))
#define	IEEE80211_REGDOMAIN_SPACE(_req) \
	IEEE80211_REGDOMAIN_SIZE((_req)->chaninfo.ic_nchans)

/*
 * Get driver capabilities.  Driver, hardware crypto, and
 * HT/802.11n capabilities, and a table that describes what
 * the radio can do.
 */
struct ieee80211_devcaps_req {
	uint32_t	dc_drivercaps;		/* general driver caps */
	uint32_t	dc_cryptocaps;		/* hardware crypto support */
	uint32_t	dc_htcaps;		/* HT/802.11n support */
	struct ieee80211req_chaninfo dc_chaninfo;
};
#define	IEEE80211_DEVCAPS_SIZE(_nchan) \
	(sizeof(struct ieee80211_devcaps_req) + \
	 (((_nchan)-1) * sizeof(struct ieee80211_channel)))
#define	IEEE80211_DEVCAPS_SPACE(_dc) \
	IEEE80211_DEVCAPS_SIZE((_dc)->dc_chaninfo.ic_nchans)

struct ieee80211_chanswitch_req {
	struct ieee80211_channel csa_chan;	/* new channel */
	int		csa_mode;		/* CSA mode */
	int		csa_count;		/* beacon count to switch */
};

/*
 * Get/set per-station vlan tag.
 */
struct ieee80211req_sta_vlan {
	uint8_t		sv_macaddr[IEEE80211_ADDR_LEN];
	uint16_t	sv_vlan;
};

#ifdef __FreeBSD__
/*
 * FreeBSD-style ioctls.
 */
/* the first member must be matched with struct ifreq */
struct ieee80211req {
	char		i_name[IFNAMSIZ];	/* if_name, e.g. "wi0" */
	uint16_t	i_type;			/* req type */
	int16_t		i_val;			/* Index or simple value */
	uint16_t	i_len;			/* Index or simple value */
	void		*i_data;		/* Extra data */
};
#define	SIOCS80211		 _IOW('i', 234, struct ieee80211req)
#define	SIOCG80211		_IOWR('i', 235, struct ieee80211req)
#define	SIOCG80211STATS		_IOWR('i', 236, struct ifreq)

#define IEEE80211_IOC_SSID		1
#define IEEE80211_IOC_NUMSSIDS		2
#define IEEE80211_IOC_WEP		3
#define 	IEEE80211_WEP_NOSUP	-1
#define 	IEEE80211_WEP_OFF	0
#define 	IEEE80211_WEP_ON	1
#define 	IEEE80211_WEP_MIXED	2
#define IEEE80211_IOC_WEPKEY		4
#define IEEE80211_IOC_NUMWEPKEYS	5
#define IEEE80211_IOC_WEPTXKEY		6
#define IEEE80211_IOC_AUTHMODE		7
#define IEEE80211_IOC_STATIONNAME	8
#define IEEE80211_IOC_CHANNEL		9
#define IEEE80211_IOC_POWERSAVE		10
#define 	IEEE80211_POWERSAVE_NOSUP	-1
#define 	IEEE80211_POWERSAVE_OFF		0
#define 	IEEE80211_POWERSAVE_CAM		1
#define 	IEEE80211_POWERSAVE_PSP		2
#define 	IEEE80211_POWERSAVE_PSP_CAM	3
#define 	IEEE80211_POWERSAVE_ON		IEEE80211_POWERSAVE_CAM
#define IEEE80211_IOC_POWERSAVESLEEP	11
#define	IEEE80211_IOC_RTSTHRESHOLD	12
#define IEEE80211_IOC_PROTMODE		13
#define 	IEEE80211_PROTMODE_OFF		0
#define 	IEEE80211_PROTMODE_CTS		1
#define 	IEEE80211_PROTMODE_RTSCTS	2
#define	IEEE80211_IOC_TXPOWER		14	/* global tx power limit */
#define	IEEE80211_IOC_BSSID		15
#define	IEEE80211_IOC_ROAMING		16	/* roaming mode */
#define	IEEE80211_IOC_PRIVACY		17	/* privacy invoked */
#define	IEEE80211_IOC_DROPUNENCRYPTED	18	/* discard unencrypted frames */
#define	IEEE80211_IOC_WPAKEY		19
#define	IEEE80211_IOC_DELKEY		20
#define	IEEE80211_IOC_MLME		21
/* 22 was IEEE80211_IOC_OPTIE, replaced by IEEE80211_IOC_APPIE */
/* 23 was IEEE80211_IOC_SCAN_REQ */
/* 24 was IEEE80211_IOC_SCAN_RESULTS */
#define	IEEE80211_IOC_COUNTERMEASURES	25	/* WPA/TKIP countermeasures */
#define	IEEE80211_IOC_WPA		26	/* WPA mode (0,1,2) */
#define	IEEE80211_IOC_CHANLIST		27	/* channel list */
#define	IEEE80211_IOC_WME		28	/* WME mode (on, off) */
#define	IEEE80211_IOC_HIDESSID		29	/* hide SSID mode (on, off) */
#define	IEEE80211_IOC_APBRIDGE		30	/* AP inter-sta bridging */
/* 31-35,37-38 were for WPA authenticator settings */
/* 36 was IEEE80211_IOC_DRIVER_CAPS */
#define	IEEE80211_IOC_WPAIE		39	/* WPA information element */
#define	IEEE80211_IOC_STA_STATS		40	/* per-station statistics */
#define	IEEE80211_IOC_MACCMD		41	/* MAC ACL operation */
#define	IEEE80211_IOC_CHANINFO		42	/* channel info list */
#define	IEEE80211_IOC_TXPOWMAX		43	/* max tx power for channel */
#define	IEEE80211_IOC_STA_TXPOW		44	/* per-station tx power limit */
/* 45 was IEEE80211_IOC_STA_INFO */
#define	IEEE80211_IOC_WME_CWMIN		46	/* WME: ECWmin */
#define	IEEE80211_IOC_WME_CWMAX		47	/* WME: ECWmax */
#define	IEEE80211_IOC_WME_AIFS		48	/* WME: AIFSN */
#define	IEEE80211_IOC_WME_TXOPLIMIT	49	/* WME: txops limit */
#define	IEEE80211_IOC_WME_ACM		50	/* WME: ACM (bss only) */
#define	IEEE80211_IOC_WME_ACKPOLICY	51	/* WME: ACK policy (!bss only)*/
#define	IEEE80211_IOC_DTIM_PERIOD	52	/* DTIM period (beacons) */
#define	IEEE80211_IOC_BEACON_INTERVAL	53	/* beacon interval (ms) */
#define	IEEE80211_IOC_ADDMAC		54	/* add sta to MAC ACL table */
#define	IEEE80211_IOC_DELMAC		55	/* del sta from MAC ACL table */
#define	IEEE80211_IOC_PUREG		56	/* pure 11g (no 11b stations) */
#define	IEEE80211_IOC_FF		57	/* ATH fast frames (on, off) */
#define	IEEE80211_IOC_TURBOP		58	/* ATH turbo' (on, off) */
#define	IEEE80211_IOC_BGSCAN		59	/* bg scanning (on, off) */
#define	IEEE80211_IOC_BGSCAN_IDLE	60	/* bg scan idle threshold */
#define	IEEE80211_IOC_BGSCAN_INTERVAL	61	/* bg scan interval */
#define	IEEE80211_IOC_SCANVALID		65	/* scan cache valid threshold */
/* 66-72 were IEEE80211_IOC_ROAM_* and IEEE80211_IOC_MCAST_RATE */
#define	IEEE80211_IOC_FRAGTHRESHOLD	73	/* tx fragmentation threshold */
#define	IEEE80211_IOC_BURST		75	/* packet bursting */
#define	IEEE80211_IOC_SCAN_RESULTS	76	/* get scan results */
#define	IEEE80211_IOC_BMISSTHRESHOLD	77	/* beacon miss threshold */
#define	IEEE80211_IOC_STA_INFO		78	/* station/neighbor info */
#define	IEEE80211_IOC_WPAIE2		79	/* WPA+RSN info elements */
#define	IEEE80211_IOC_CURCHAN		80	/* current channel */
#define	IEEE80211_IOC_SHORTGI		81	/* 802.11n half GI */
#define	IEEE80211_IOC_AMPDU		82	/* 802.11n A-MPDU (on, off) */
#define	IEEE80211_IOC_AMPDU_LIMIT	83	/* A-MPDU length limit */
#define	IEEE80211_IOC_AMPDU_DENSITY	84	/* A-MPDU density */
#define	IEEE80211_IOC_AMSDU		85	/* 802.11n A-MSDU (on, off) */
#define	IEEE80211_IOC_AMSDU_LIMIT	86	/* A-MSDU length limit */
#define	IEEE80211_IOC_PUREN		87	/* pure 11n (no legacy sta's) */
#define	IEEE80211_IOC_DOTH		88	/* 802.11h (on, off) */
/* 89-91 were regulatory items */
#define	IEEE80211_IOC_HTCOMPAT		92	/* support pre-D1.10 HT ie's */
#define	IEEE80211_IOC_DWDS		93	/* DWDS/4-address handling */
#define	IEEE80211_IOC_INACTIVITY	94	/* sta inactivity handling */
#define	IEEE80211_IOC_APPIE		95	/* application IE's */
#define	IEEE80211_IOC_WPS		96	/* WPS operation */
#define	IEEE80211_IOC_TSN		97	/* TSN operation */
#define	IEEE80211_IOC_DEVCAPS		98	/* driver+device capabilities */
#define	IEEE80211_IOC_CHANSWITCH	99	/* start 11h channel switch */
#define	IEEE80211_IOC_DFS		100	/* DFS (on, off) */
#define	IEEE80211_IOC_DOTD		101	/* 802.11d (on, off) */
#define IEEE80211_IOC_HTPROTMODE	102	/* HT protection (off, rts) */
#define	IEEE80211_IOC_SCAN_REQ		103	/* scan w/ specified params */
#define	IEEE80211_IOC_SCAN_CANCEL	104	/* cancel ongoing scan */
#define	IEEE80211_IOC_HTCONF		105	/* HT config (off, HT20, HT40)*/
#define	IEEE80211_IOC_REGDOMAIN		106	/* regulatory domain info */
#define	IEEE80211_IOC_ROAM		107	/* roaming params en masse */
#define	IEEE80211_IOC_TXPARAMS		108	/* tx parameters */
#define	IEEE80211_IOC_STA_VLAN		109	/* per-station vlan tag */
#define	IEEE80211_IOC_SMPS		110	/* MIMO power save */
#define	IEEE80211_IOC_RIFS		111	/* RIFS config (on, off) */
#define	IEEE80211_IOC_GREENFIELD	112	/* Greenfield (on, off) */
#define	IEEE80211_IOC_STBC		113	/* STBC Tx/RX (on, off) */

#define	IEEE80211_IOC_MESH_ID		170	/* mesh identifier */
#define	IEEE80211_IOC_MESH_AP		171	/* accepting peerings */
#define	IEEE80211_IOC_MESH_FWRD		172	/* forward frames */
#define	IEEE80211_IOC_MESH_PROTO	173	/* mesh protocols */
#define	IEEE80211_IOC_MESH_TTL		174	/* mesh TTL */
#define	IEEE80211_IOC_MESH_RTCMD	175	/* mesh routing table commands*/
#define	IEEE80211_IOC_MESH_PR_METRIC	176	/* mesh metric protocol */
#define	IEEE80211_IOC_MESH_PR_PATH	177	/* mesh path protocol */
#define	IEEE80211_IOC_MESH_PR_SIG	178	/* mesh sig protocol */
#define	IEEE80211_IOC_MESH_PR_CC	179	/* mesh congestion protocol */
#define	IEEE80211_IOC_MESH_PR_AUTH	180	/* mesh auth protocol */

#define	IEEE80211_IOC_HWMP_ROOTMODE	190	/* HWMP root mode */
#define	IEEE80211_IOC_HWMP_MAXHOPS	191	/* number of hops before drop */
#define	IEEE80211_IOC_HWMP_TTL		192	/* HWMP TTL */

#define	IEEE80211_IOC_TDMA_SLOT		201	/* TDMA: assigned slot */
#define	IEEE80211_IOC_TDMA_SLOTCNT	202	/* TDMA: slots in bss */
#define	IEEE80211_IOC_TDMA_SLOTLEN	203	/* TDMA: slot length (usecs) */
#define	IEEE80211_IOC_TDMA_BINTERVAL	204	/* TDMA: beacon intvl (slots) */

/*
 * Parameters for controlling a scan requested with
 * IEEE80211_IOC_SCAN_REQ.
 *
 * Active scans cause ProbeRequest frames to be issued for each
 * specified ssid and, by default, a broadcast ProbeRequest frame.
 * The set of ssid's is specified in the request.
 *
 * By default the scan will cause a BSS to be joined (in station/adhoc
 * mode) or a channel to be selected for operation (hostap mode).
 * To disable that specify IEEE80211_IOC_SCAN_NOPICK and if the
 *
 * If the station is currently associated to an AP then a scan request
 * will cause the station to leave the current channel and potentially
 * miss frames from the AP.  Alternatively the station may notify the
 * AP that it is going into power save mode before it leaves the channel.
 * This ensures frames for the station are buffered by the AP.  This is
 * termed a ``bg scan'' and is requested with the IEEE80211_IOC_SCAN_BGSCAN
 * flag.  Background scans may take longer than foreground scans and may
 * be preempted by traffic.  If a station is not associated to an AP
 * then a request for a background scan is automatically done in the
 * foreground.
 *
 * The results of the scan request are cached by the system.  This
 * information is aged out and/or invalidated based on events like not
 * being able to associated to an AP.  To flush the current cache
 * contents before doing a scan the IEEE80211_IOC_SCAN_FLUSH flag may
 * be specified.
 *
 * By default the scan will be done until a suitable AP is located
 * or a channel is found for use.  A scan can also be constrained
 * to be done once (IEEE80211_IOC_SCAN_ONCE) or to last for no more
 * than a specified duration.
 */
struct ieee80211_scan_req {
	int		sr_flags;
#define	IEEE80211_IOC_SCAN_NOPICK	0x00001	/* scan only, no selection */
#define	IEEE80211_IOC_SCAN_ACTIVE	0x00002	/* active scan (probe req) */
#define	IEEE80211_IOC_SCAN_PICK1ST	0x00004	/* ``hey sailor'' mode */
#define	IEEE80211_IOC_SCAN_BGSCAN	0x00008	/* bg scan, exit ps at end */
#define	IEEE80211_IOC_SCAN_ONCE		0x00010	/* do one complete pass */
#define	IEEE80211_IOC_SCAN_NOBCAST	0x00020	/* don't send bcast probe req */
#define	IEEE80211_IOC_SCAN_NOJOIN	0x00040	/* no auto-sequencing */
#define	IEEE80211_IOC_SCAN_FLUSH	0x10000	/* flush scan cache first */
#define	IEEE80211_IOC_SCAN_CHECK	0x20000	/* check scan cache first */
	u_int		sr_duration;		/* duration (ms) */
#define	IEEE80211_IOC_SCAN_DURATION_MIN	1
#define	IEEE80211_IOC_SCAN_DURATION_MAX	0x7fffffff
#define	IEEE80211_IOC_SCAN_FOREVER	IEEE80211_IOC_SCAN_DURATION_MAX
	u_int		sr_mindwell;		/* min channel dwelltime (ms) */
	u_int		sr_maxdwell;		/* max channel dwelltime (ms) */
	int		sr_nssid;
#define	IEEE80211_IOC_SCAN_MAX_SSID	3
	struct {
		int	 len;				/* length in bytes */
		uint8_t ssid[IEEE80211_NWID_LEN];	/* ssid contents */
	} sr_ssid[IEEE80211_IOC_SCAN_MAX_SSID];
};

/*
 * Scan result data returned for IEEE80211_IOC_SCAN_RESULTS.
 * Each result is a fixed size structure followed by a variable
 * length SSID and one or more variable length information elements.
 * The size of each variable length item is found in the fixed
 * size structure and the entire length of the record is specified
 * in isr_len.  Result records are rounded to a multiple of 4 bytes.
 */
struct ieee80211req_scan_result {
	uint16_t	isr_len;		/* total length (mult of 4) */
	uint16_t	isr_ie_off;		/* offset to SSID+IE data */
	uint16_t	isr_ie_len;		/* IE length */
	uint16_t	isr_freq;		/* MHz */
	uint16_t	isr_flags;		/* channel flags */
	int8_t		isr_noise;
	int8_t		isr_rssi;
	uint16_t	isr_intval;		/* beacon interval */
	uint8_t		isr_capinfo;		/* capabilities */
	uint8_t		isr_erp;		/* ERP element */
	uint8_t		isr_bssid[IEEE80211_ADDR_LEN];
	uint8_t		isr_nrates;
	uint8_t		isr_rates[IEEE80211_RATE_MAXSIZE];
	uint8_t		isr_ssid_len;		/* SSID length */
	uint8_t		isr_meshid_len;		/* MESH ID length */
	/* variable length SSID, followed by variable length MESH ID,
	  followed by IE data */
};

/*
 * Virtual AP cloning parameters.  The parent device must
 * be a vap-capable device.  All parameters specified with
 * the clone request are fixed for the lifetime of the vap.
 *
 * There are two flavors of WDS vaps: legacy and dynamic.
 * Legacy WDS operation implements a static binding between
 * two stations encapsulating traffic in 4-address frames.
 * Dynamic WDS vaps are created when a station associates to
 * an AP and sends a 4-address frame.  If the AP vap is
 * configured to support WDS then this will generate an
 * event to user programs listening on the routing socket
 * and a Dynamic WDS vap will be created to handle traffic
 * to/from that station.  In both cases the bssid of the
 * peer must be specified when creating the vap.
 *
 * By default a vap will inherit the mac address/bssid of
 * the underlying device.  To request a unique address the
 * IEEE80211_CLONE_BSSID flag should be supplied.  This is
 * meaningless for WDS vaps as they share the bssid of an
 * AP vap that must otherwise exist.  Note that some devices
 * may not be able to support multiple addresses.
 *
 * Station mode vap's normally depend on the device to notice
 * when the AP stops sending beacon frames.  If IEEE80211_CLONE_NOBEACONS
 * is specified the net80211 layer will do this in s/w.  This
 * is mostly useful when setting up a WDS repeater/extender where
 * an AP vap is combined with a sta vap and the device isn't able
 * to track beacon frames in hardware.
 */
struct ieee80211_clone_params {
	char	icp_parent[IFNAMSIZ];		/* parent device */
	uint16_t icp_opmode;			/* operating mode */
	uint16_t icp_flags;			/* see below */
	uint8_t	icp_bssid[IEEE80211_ADDR_LEN];	/* for WDS links */
	uint8_t	icp_macaddr[IEEE80211_ADDR_LEN];/* local address */
};
#define	IEEE80211_CLONE_BSSID		0x0001	/* allocate unique mac/bssid */
#define	IEEE80211_CLONE_NOBEACONS	0x0002	/* don't setup beacon timers */
#define	IEEE80211_CLONE_WDSLEGACY	0x0004	/* legacy WDS processing */
#define	IEEE80211_CLONE_MACADDR		0x0008	/* use specified mac addr */
#define	IEEE80211_CLONE_TDMA		0x0010	/* operate in TDMA mode */
#endif /* __FreeBSD__ */

#endif /* _NET80211_IEEE80211_IOCTL_H_ */


code:
/*-
 * Copyright (c) 2009 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Rui Paulo under sponsorship from the
 * FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_mesh.h 202178 2010-01-12 22:22:27Z rpaulo $
 */
#ifndef _NET80211_IEEE80211_MESH_H_
#define _NET80211_IEEE80211_MESH_H_

#define	IEEE80211_MESH_DEFAULT_TTL	31

/*
 * NB: all structures are __packed  so sizeof works on arm, et. al.
 */
/*
 * 802.11s Information Elements.
*/
/* Mesh Configuration */
struct ieee80211_meshconf_ie {
	uint8_t		conf_ie;	/* IEEE80211_ELEMID_MESHCONF */
	uint8_t		conf_len;
	uint8_t		conf_pselid;	/* Active Path Sel. Proto. ID */
	uint8_t		conf_pmetid;	/* Active Metric Identifier */
	uint8_t		conf_ccid;	/* Congestion Control Mode ID  */
	uint8_t		conf_syncid;	/* Sync. Protocol ID */
	uint8_t		conf_authid;	/* Auth. Protocol ID */
	uint8_t		conf_form;	/* Formation Information */
	uint16_t	conf_cap;
} __packed;

/* Hybrid Wireless Mesh Protocol */
#define	IEEE80211_MESHCONF_PATH_HWMP		0x00
/* Airtime Link Metric */
#define	IEEE80211_MESHCONF_METRIC_AIRTIME	0x00
/* Congestion Control */
#define	IEEE80211_MESHCONF_CC_DISABLED		0x00
#define	IEEE80211_MESHCONF_CC_SIG		0x01
/* Neighbour Offset */
#define	IEEE80211_MESHCONF_SYNC_NEIGHOFF	0x00
#define	IEEE80211_MESHCONF_AUTH_DISABLED	0x00
/* Simultaneous Authenticaction of Equals */
#define	IEEE80211_MESHCONF_AUTH_SAE		0x01
#define	IEEE80211_MESHCONF_FORM_MP		0x01 /* Connected to Portal */
#define	IEEE80211_MESHCONF_FORM_NNEIGH_MASK	0x04 /* Number of Neighbours */
#define	IEEE80211_MESHCONF_CAP_AP	0x01	/* Accepting Peers */
#define	IEEE80211_MESHCONF_CAP_MCCAS	0x02	/* MCCA supported */
#define	IEEE80211_MESHCONF_CAP_MCCAE	0x04	/* MCCA enabled */
#define	IEEE80211_MESHCONF_CAP_FWRD 	0x08	/* forwarding enabled */
#define	IEEE80211_MESHCONF_CAP_BTR	0x10	/* Beacon Timing Report Enab */
#define	IEEE80211_MESHCONF_CAP_TBTTA	0x20	/* TBTT Adj. Enabled */
#define	IEEE80211_MESHCONF_CAP_TBTT	0x40	/* TBTT Adjusting  */
#define	IEEE80211_MESHCONF_CAP_PSL	0x80	/* Power Save Level */

/* Mesh Identifier */
struct ieee80211_meshid_ie {
	uint8_t		id_ie;		/* IEEE80211_ELEMID_MESHID */
	uint8_t		id_len;
} __packed;

/* Link Metric Report */
struct ieee80211_meshlmetric_ie {
	uint8_t		lm_ie;	/* IEEE80211_ELEMID_MESHLINK */
	uint8_t		lm_len;
	uint32_t	lm_metric;
#define	IEEE80211_MESHLMETRIC_INITIALVAL	0
} __packed;

/* Congestion Notification */
struct ieee80211_meshcngst_ie {
	uint8_t		cngst_ie;	/* IEEE80211_ELEMID_MESHCNGST */
	uint8_t		cngst_len;
	uint16_t	cngst_timer[4];	/* Expiration Timers: AC_BK,
					   AC_BE, AC_VI, AC_VO */
} __packed;

/* Peer Link Management */
struct ieee80211_meshpeer_ie {
	uint8_t		peer_ie;	/* IEEE80211_ELEMID_MESHPEER */
	uint8_t		peer_len;
	uint8_t		peer_proto[4];	/* Peer Management Protocol */
	uint16_t	peer_llinkid;	/* Local Link ID */
	uint16_t	peer_linkid;	/* Peer Link ID */
	uint16_t	peer_rcode;
} __packed;

enum {
	IEEE80211_MESH_PEER_LINK_OPEN		= 0,
	IEEE80211_MESH_PEER_LINK_CONFIRM	= 1,
	IEEE80211_MESH_PEER_LINK_CLOSE		= 2,
	/* values 3-255 are reserved */
};

/* Mesh Peering Management Protocol */
#define	IEEE80211_MESH_PEER_PROTO_OUI		0x00, 0x0f, 0xac
#define	IEEE80211_MESH_PEER_PROTO_VALUE		0x2a
#define	IEEE80211_MESH_PEER_PROTO	{ IEEE80211_MESH_PEER_PROTO_OUI, \
					  IEEE80211_MESH_PEER_PROTO_VALUE }
/* Abbreviated Handshake Protocol */
#define	IEEE80211_MESH_PEER_PROTO_AH_OUI	0x00, 0x0f, 0xac
#define	IEEE80211_MESH_PEER_PROTO_AH_VALUE	0x2b
#define	IEEE80211_MESH_PEER_PROTO_AH	{ IEEE80211_MESH_PEER_PROTO_AH_OUI, \
					  IEEE80211_MESH_PEER_PROTO_AH_VALUE }
#ifdef notyet
/* Mesh Channel Switch Annoucement */
struct ieee80211_meshcsa_ie {
	uint8_t		csa_ie;		/* IEEE80211_ELEMID_MESHCSA */
	uint8_t		csa_len;
	uint8_t		csa_mode;
	uint8_t		csa_newclass;	/* New Regulatory Class */
	uint8_t		csa_newchan;
	uint8_t		csa_precvalue;	/* Precedence Value */
	uint8_t		csa_count;
} __packed;

/* Mesh TIM */
/* Equal to the non Mesh version */

/* Mesh Awake Window */
struct ieee80211_meshawakew_ie {
	uint8_t		awakew_ie;		/* IEEE80211_ELEMID_MESHAWAKEW */
	uint8_t		awakew_len;
	uint8_t		awakew_windowlen;	/* in TUs */
} __packed;

/* Mesh Beacon Timing */
struct ieee80211_meshbeacont_ie {
	uint8_t		beacont_ie;		/* IEEE80211_ELEMID_MESHBEACONT */
	uint8_t		beacont_len;
	struct {
		uint8_t		mp_aid;		/* Least Octet of AID */
		uint16_t	mp_btime;	/* Beacon Time */
		uint16_t	mp_bint;	/* Beacon Interval */
	} __packed mp[1];			/* NB: variable size */
} __packed;
#endif

/* Portal (MP) Annoucement */
struct ieee80211_meshpann_ie {
	uint8_t		pann_ie;		/* IEEE80211_ELEMID_MESHPANN */
	uint8_t		pann_len;
	uint8_t		pann_flags;
	uint8_t		pann_hopcount;
	uint8_t		pann_ttl;
	uint8_t		pann_addr[IEEE80211_ADDR_LEN];
	uint8_t		pann_seq;		/* PANN Sequence Number */
} __packed;

/* Root (MP) Annoucement */
struct ieee80211_meshrann_ie {
	uint8_t		rann_ie;		/* IEEE80211_ELEMID_MESHRANN */
	uint8_t		rann_len;
	uint8_t		rann_flags;
#define	IEEE80211_MESHRANN_FLAGS_PR	0x01	/* Portal Role */
	uint8_t		rann_hopcount;
	uint8_t		rann_ttl;
	uint8_t		rann_addr[IEEE80211_ADDR_LEN];
	uint32_t	rann_seq;		/* HWMP Sequence Number */
	uint32_t	rann_metric;
} __packed;

/* Mesh Path Request */
struct ieee80211_meshpreq_ie {
	uint8_t		preq_ie;	/* IEEE80211_ELEMID_MESHPREQ */
	uint8_t		preq_len;
	uint8_t		preq_flags;
#define	IEEE80211_MESHPREQ_FLAGS_PR	0x01	/* Portal Role */
#define	IEEE80211_MESHPREQ_FLAGS_AM	0x02	/* 0 = ucast / 1 = bcast */
#define	IEEE80211_MESHPREQ_FLAGS_PP	0x04	/* Proactive PREP */
#define	IEEE80211_MESHPREQ_FLAGS_AE	0x40	/* Address Extension */
	uint8_t		preq_hopcount;
	uint8_t		preq_ttl;
	uint32_t	preq_id;
	uint8_t		preq_origaddr[IEEE80211_ADDR_LEN];
	uint32_t	preq_origseq;	/* HWMP Sequence Number */
	/* NB: may have Originator Proxied Address */
	uint32_t	preq_lifetime;
	uint32_t	preq_metric;
	uint8_t		preq_tcount;	/* target count */
	struct {
		uint8_t		target_flags;
#define	IEEE80211_MESHPREQ_TFLAGS_TO	0x01	/* Target Only */
#define	IEEE80211_MESHPREQ_TFLAGS_RF	0x02	/* Reply and Forward */
#define	IEEE80211_MESHPREQ_TFLAGS_USN	0x04	/* Unknown HWMP seq number */
		uint8_t		target_addr[IEEE80211_ADDR_LEN];
		uint32_t	target_seq;	/* HWMP Sequence Number */
	} __packed preq_targets[1];		/* NB: variable size */
} __packed;

/* Mesh Path Reply */
struct ieee80211_meshprep_ie {
	uint8_t		prep_ie;	/* IEEE80211_ELEMID_MESHPREP */
	uint8_t		prep_len;
	uint8_t		prep_flags;
	uint8_t		prep_hopcount;
	uint8_t		prep_ttl;
	uint8_t		prep_targetaddr[IEEE80211_ADDR_LEN];
	uint32_t	prep_targetseq;
	/* NB: May have Target Proxied Address */
	uint32_t	prep_lifetime;
	uint32_t	prep_metric;
	uint8_t		prep_origaddr[IEEE80211_ADDR_LEN];
	uint32_t	prep_origseq;	/* HWMP Sequence Number */
} __packed;

/* Mesh Path Error */
struct ieee80211_meshperr_ie {
	uint8_t		perr_ie;	/* IEEE80211_ELEMID_MESHPERR */
	uint8_t		perr_len;
	uint8_t		perr_ttl;
	uint8_t		perr_ndests;	/* Number of Destinations */
	struct {
		uint8_t		dest_flags;
#define	IEEE80211_MESHPERR_DFLAGS_USN	0x01
#define	IEEE80211_MESHPERR_DFLAGS_RC	0x02
		uint8_t		dest_addr[IEEE80211_ADDR_LEN];
		uint32_t	dest_seq;	/* HWMP Sequence Number */
		uint16_t	dest_rcode;
	} __packed perr_dests[1];		/* NB: variable size */
} __packed;

#ifdef notyet
/* Mesh Proxy Update */
struct ieee80211_meshpu_ie {
	uint8_t		pu_ie;		/* IEEE80211_ELEMID_MESHPU */
	uint8_t		pu_len;
	uint8_t		pu_flags;
#define	IEEE80211_MESHPU_FLAGS_MASK		0x1
#define	IEEE80211_MESHPU_FLAGS_DEL		0x0
#define	IEEE80211_MESHPU_FLAGS_ADD		0x1
	uint8_t		pu_seq;		/* PU Sequence Number */
	uint8_t		pu_addr[IEEE80211_ADDR_LEN];
	uint8_t		pu_naddr;	/* Number of Proxied Addresses */
	/* NB: proxied address follows */
} __packed;

/* Mesh Proxy Update Confirmation */
struct ieee80211_meshpuc_ie {
	uint8_t		puc_ie;		/* IEEE80211_ELEMID_MESHPUC */
	uint8_t		puc_len;
	uint8_t		puc_flags;
	uint8_t		puc_seq;	/* PU Sequence Number */
	uint8_t		puc_daddr[IEEE80211_ADDR_LEN];
} __packed;
#endif

/*
 * 802.11s Action Frames
 */
#define	IEEE80211_ACTION_CAT_MESHPEERING	30	/* XXX Linux */
#define	IEEE80211_ACTION_CAT_MESHLMETRIC	13
#define	IEEE80211_ACTION_CAT_MESHPATH		32	/* XXX Linux */
#define	IEEE80211_ACTION_CAT_INTERWORK		15
#define	IEEE80211_ACTION_CAT_RESOURCE		16
#define	IEEE80211_ACTION_CAT_PROXY		17

/*
 * Mesh Peering Action codes.
 */
enum {
	IEEE80211_ACTION_MESHPEERING_OPEN	= 0,
	IEEE80211_ACTION_MESHPEERING_CONFIRM	= 1,
	IEEE80211_ACTION_MESHPEERING_CLOSE	= 2,
	/* 3-255 reserved */
};

/*
 * Mesh Path Selection Action code.
 */
enum {
	IEEE80211_ACTION_MESHPATH_SEL	= 0,
	/* 1-255 reserved */
};

/*
 * Mesh Link Metric Action codes.
 */
enum {
	IEEE80211_ACTION_MESHLMETRIC_REQ = 0,	/* Link Metric Request */
	IEEE80211_ACTION_MESHLMETRIC_REP = 1,	/* Link Metric Report */
	/* 2-255 reserved */
};

/*
 * Mesh Portal Annoucement Action codes.
 */
enum {
	IEEE80211_ACTION_MESHPANN	= 0,
	/* 1-255 reserved */
};

/*
 * Different mesh control structures based on the AE
 * (Address Extension) bits.
 */
struct ieee80211_meshcntl {
	uint8_t		mc_flags;	/* Address Extension 00 */
	uint8_t		mc_ttl;		/* TTL */
	uint8_t		mc_seq[4];	/* Sequence No. */
	/* NB: more addresses may follow */
} __packed;

struct ieee80211_meshcntl_ae01 {
	uint8_t		mc_flags;	/* Address Extension 01 */
	uint8_t		mc_ttl;		/* TTL */
	uint8_t		mc_seq[4];	/* Sequence No. */
	uint8_t		mc_addr4[IEEE80211_ADDR_LEN];
} __packed;

struct ieee80211_meshcntl_ae10 {
	uint8_t		mc_flags;	/* Address Extension 10 */
	uint8_t		mc_ttl;		/* TTL */
	uint8_t		mc_seq[4];	/* Sequence No. */
	uint8_t		mc_addr4[IEEE80211_ADDR_LEN];
	uint8_t		mc_addr5[IEEE80211_ADDR_LEN];
} __packed;

struct ieee80211_meshcntl_ae11 {
	uint8_t		mc_flags;	/* Address Extension 11 */
	uint8_t		mc_ttl;		/* TTL */
	uint8_t		mc_seq[4];	/* Sequence No. */
	uint8_t		mc_addr4[IEEE80211_ADDR_LEN];
	uint8_t		mc_addr5[IEEE80211_ADDR_LEN];
	uint8_t		mc_addr6[IEEE80211_ADDR_LEN];
} __packed;

#ifdef _KERNEL
MALLOC_DECLARE(M_80211_MESH_RT);
struct ieee80211_mesh_route {
	TAILQ_ENTRY(ieee80211_mesh_route)	rt_next;
	int			rt_crtime;	/* creation time */
	uint8_t			rt_dest[IEEE80211_ADDR_LEN];
	uint8_t			rt_nexthop[IEEE80211_ADDR_LEN];
	uint32_t		rt_metric;	/* path metric */
	uint16_t		rt_nhops;	/* number of hops */
	uint16_t		rt_flags;
#define	IEEE80211_MESHRT_FLAGS_VALID	0x01	/* patch discovery complete */
#define	IEEE80211_MESHRT_FLAGS_PROXY	0x02	/* proxy entry */
	uint32_t		rt_lifetime;
	uint32_t		rt_lastmseq;	/* last seq# seen dest */
	void			*rt_priv;	/* private data */
};
#define	IEEE80211_MESH_ROUTE_PRIV(rt, cast)	((cast *)rt->rt_priv)

#define	IEEE80211_MESH_PROTO_DSZ	12	/* description size */
/*
 * Mesh Path Selection Protocol.
 */
enum ieee80211_state;
struct ieee80211_mesh_proto_path {
	uint8_t		mpp_active;
	char 		mpp_descr[IEEE80211_MESH_PROTO_DSZ];
	uint8_t		mpp_ie;
	struct ieee80211_node *
	    		(*mpp_discover)(struct ieee80211vap *,
				const uint8_t [IEEE80211_ADDR_LEN],
				struct mbuf *);
	void		(*mpp_peerdown)(struct ieee80211_node *);
	void		(*mpp_vattach)(struct ieee80211vap *);
	void		(*mpp_vdetach)(struct ieee80211vap *);
	int		(*mpp_newstate)(struct ieee80211vap *,
			    enum ieee80211_state, int);
	const size_t	mpp_privlen;	/* size required in the routing table
					   for private data */
	int		mpp_inact;	/* inact. timeout for invalid routes
					   (ticks) */
};

/*
 * Mesh Link Metric Report Protocol.
 */
struct ieee80211_mesh_proto_metric {
	uint8_t		mpm_active;
	char		mpm_descr[IEEE80211_MESH_PROTO_DSZ];
	uint8_t		mpm_ie;
	uint32_t	(*mpm_metric)(struct ieee80211_node *);
};

#ifdef notyet
/*
 * Mesh Authentication Protocol.
 */
struct ieee80211_mesh_proto_auth {
	uint8_t		mpa_ie[4];
};

struct ieee80211_mesh_proto_congestion {
};

struct ieee80211_mesh_proto_sync {
};
#endif

typedef uint32_t ieee80211_mesh_seq;
#define	IEEE80211_MESH_SEQ_LEQ(a, b)	((int32_t)((a)-(b)) <= 0)
#define	IEEE80211_MESH_SEQ_GEQ(a, b)	((int32_t)((a)-(b)) >= 0)

struct ieee80211_mesh_state {
	int				ms_idlen;
	uint8_t				ms_id[IEEE80211_MESHID_LEN];
	ieee80211_mesh_seq		ms_seq;	/* seq no for meshcntl */
	uint16_t			ms_neighbors;
	uint8_t				ms_ttl;	/* mesh ttl set in packets */
#define IEEE80211_MESHFLAGS_AP		0x01	/* accept peers */
#define IEEE80211_MESHFLAGS_PORTAL	0x02	/* mesh portal role */
#define IEEE80211_MESHFLAGS_FWD		0x04	/* forward packets */
	uint8_t				ms_flags;
	struct mtx			ms_rt_lock;
	struct callout			ms_cleantimer;
	TAILQ_HEAD(, ieee80211_mesh_route)  ms_routes;
	struct ieee80211_mesh_proto_metric *ms_pmetric;
	struct ieee80211_mesh_proto_path   *ms_ppath;
};
void		ieee80211_mesh_attach(struct ieee80211com *);
void		ieee80211_mesh_detach(struct ieee80211com *);

struct ieee80211_mesh_route *
		ieee80211_mesh_rt_find(struct ieee80211vap *,
		    const uint8_t [IEEE80211_ADDR_LEN]);
struct ieee80211_mesh_route *
                ieee80211_mesh_rt_add(struct ieee80211vap *,
		    const uint8_t [IEEE80211_ADDR_LEN]);
void		ieee80211_mesh_rt_del(struct ieee80211vap *,
		    const uint8_t [IEEE80211_ADDR_LEN]);
void		ieee80211_mesh_rt_flush(struct ieee80211vap *);
void		ieee80211_mesh_rt_flush_peer(struct ieee80211vap *,
		    const uint8_t [IEEE80211_ADDR_LEN]);
void		ieee80211_mesh_proxy_check(struct ieee80211vap *,
		    const uint8_t [IEEE80211_ADDR_LEN]);

int		ieee80211_mesh_register_proto_path(const
		    struct ieee80211_mesh_proto_path *);
int		ieee80211_mesh_register_proto_metric(const
		    struct ieee80211_mesh_proto_metric *);

uint8_t *	ieee80211_add_meshid(uint8_t *, struct ieee80211vap *);
uint8_t *	ieee80211_add_meshconf(uint8_t *, struct ieee80211vap *);
uint8_t *	ieee80211_add_meshpeer(uint8_t *, uint8_t, uint16_t, uint16_t,
		    uint16_t);
uint8_t *	ieee80211_add_meshlmetric(uint8_t *, uint32_t);

void		ieee80211_mesh_node_init(struct ieee80211vap *,
		    struct ieee80211_node *);
void		ieee80211_mesh_node_cleanup(struct ieee80211_node *);
void		ieee80211_parse_meshid(struct ieee80211_node *,
		    const uint8_t *);
struct ieee80211_scanparams;
void		ieee80211_mesh_init_neighbor(struct ieee80211_node *,
		   const struct ieee80211_frame *,
		   const struct ieee80211_scanparams *);
void		ieee80211_mesh_update_beacon(struct ieee80211vap *,
		    struct ieee80211_beacon_offsets *);

/*
 * Return non-zero if proxy operation is enabled.
 */
static __inline int
ieee80211_mesh_isproxyena(struct ieee80211vap *vap)
{
	struct ieee80211_mesh_state *ms = vap->iv_mesh;
	return (ms->ms_flags &
	    (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_PORTAL)) != 0;
}

/*
 * Process an outbound frame: if a path is known to the
 * destination then return a reference to the next hop
 * for immediate transmission.  Otherwise initiate path
 * discovery and, if possible queue the packet to be
 * sent when path discovery completes.
 */
static __inline struct ieee80211_node *
ieee80211_mesh_discover(struct ieee80211vap *vap,
    const uint8_t dest[IEEE80211_ADDR_LEN], struct mbuf *m)
{
	struct ieee80211_mesh_state *ms = vap->iv_mesh;
	return ms->ms_ppath->mpp_discover(vap, dest, m);
}

#endif /* _KERNEL */
#endif /* !_NET80211_IEEE80211_MESH_H_ */

repo_name: veiledmerc/mira

license: mit


code:
/*-
 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_monitor.h 178354 2008-04-20 20:35:46Z sam $
 */
#ifndef _NET80211_IEEE80211_MONITOR_H_
#define _NET80211_IEEE80211_MONITOR_H_

/*
 * Monitor implementation definitions.
 */
void	ieee80211_monitor_attach(struct ieee80211com *);
void	ieee80211_monitor_detach(struct ieee80211com *);
#endif /* !_NET80211_IEEE80211_MONITOR_H_ */


code:
/*-
 * Copyright (c) 2001 Atsushi Onoe
 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_node.h 220445 2011-04-08 09:20:45Z adrian $
 */
#ifndef _NET80211_IEEE80211_NODE_H_
#define _NET80211_IEEE80211_NODE_H_

#include <net80211/ieee80211_ioctl.h>		/* for ieee80211_nodestats */
#include <net80211/ieee80211_ht.h>		/* for aggregation state */

/*
 * Each ieee80211com instance has a single timer that fires every
 * IEEE80211_INACT_WAIT seconds to handle "inactivity processing".
 * This is used to do node inactivity processing when operating
 * as an AP, adhoc or mesh mode.  For inactivity processing each node
 * has a timeout set in it's ni_inact field that is decremented
 * on each timeout and the node is reclaimed when the counter goes
 * to zero.  We use different inactivity timeout values depending
 * on whether the node is associated and authorized (either by
 * 802.1x or open/shared key authentication) or associated but yet
 * to be authorized.  The latter timeout is shorter to more aggressively
 * reclaim nodes that leave part way through the 802.1x exchange.
 */
#define	IEEE80211_INACT_WAIT	15		/* inactivity interval (secs) */
#define	IEEE80211_INACT_INIT	(30/IEEE80211_INACT_WAIT)	/* initial */
#define	IEEE80211_INACT_AUTH	(180/IEEE80211_INACT_WAIT)	/* associated but not authorized */
#define	IEEE80211_INACT_RUN	(300/IEEE80211_INACT_WAIT)	/* authorized */
#define	IEEE80211_INACT_PROBE	(30/IEEE80211_INACT_WAIT)	/* probe */
#define	IEEE80211_INACT_SCAN	(300/IEEE80211_INACT_WAIT)	/* scanned */

#define	IEEE80211_TRANS_WAIT 	2		/* mgt frame tx timer (secs) */

/* threshold for aging overlapping non-ERP bss */
#define	IEEE80211_NONERP_PRESENT_AGE	msecs_to_ticks(60*1000)

#define	IEEE80211_NODE_HASHSIZE	32		/* NB: hash size must be pow2 */
/* simple hash is enough for variation of macaddr */
#define	IEEE80211_NODE_HASH(ic, addr)	\
	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % \
		IEEE80211_NODE_HASHSIZE)

struct ieee80211_node_table;
struct ieee80211com;
struct ieee80211vap;

/*
 * Information element ``blob''.  We use this structure
 * to capture management frame payloads that need to be
 * retained.  Information elements within the payload that
 * we need to consult have references recorded.
 */
struct ieee80211_ies {
	/* the following are either NULL or point within data */
	uint8_t	*wpa_ie;	/* captured WPA ie */
	uint8_t	*rsn_ie;	/* captured RSN ie */
	uint8_t	*wme_ie;	/* captured WME ie */
	uint8_t	*ath_ie;	/* captured Atheros ie */
	uint8_t	*htcap_ie;	/* captured HTCAP ie */
	uint8_t	*htinfo_ie;	/* captured HTINFO ie */
	uint8_t	*tdma_ie;	/* captured TDMA ie */
	uint8_t *meshid_ie;	/* captured MESH ID ie */
	uint8_t	*spare[4];
	/* NB: these must be the last members of this structure */
	uint8_t	*data;		/* frame data > 802.11 header */
	int	len;		/* data size in bytes */
};

/*
 * 802.11s (Mesh) Peer Link FSM state.
 */
enum ieee80211_mesh_mlstate {
	IEEE80211_NODE_MESH_IDLE	= 0,
	IEEE80211_NODE_MESH_OPENSNT	= 1,	/* open frame sent */
	IEEE80211_NODE_MESH_OPENRCV	= 2,	/* open frame received */
	IEEE80211_NODE_MESH_CONFIRMRCV	= 3,	/* confirm frame received */
	IEEE80211_NODE_MESH_ESTABLISHED	= 4,	/* link established */
	IEEE80211_NODE_MESH_HOLDING	= 5,	/* link closing */
};
#define	IEEE80211_MESH_MLSTATE_BITS \
	"\20\1IDLE\2OPENSNT\2OPENRCV\3CONFIRMRCV\4ESTABLISHED\5HOLDING"

/*
 * Node specific information.  Note that drivers are expected
 * to derive from this structure to add device-specific per-node
 * state.  This is done by overriding the ic_node_* methods in
 * the ieee80211com structure.
 */
struct ieee80211_node {
	struct ieee80211vap	*ni_vap;	/* associated vap */
	struct ieee80211com	*ni_ic;		/* copy from vap to save deref*/
	struct ieee80211_node_table *ni_table;	/* NB: may be NULL */
	TAILQ_ENTRY(ieee80211_node) ni_list;	/* list of all nodes */
	LIST_ENTRY(ieee80211_node) ni_hash;	/* hash collision list */
	u_int			ni_refcnt;	/* count of held references */
	u_int			ni_scangen;	/* gen# for timeout scan */
	u_int			ni_flags;
#define	IEEE80211_NODE_AUTH	0x000001	/* authorized for data */
#define	IEEE80211_NODE_QOS	0x000002	/* QoS enabled */
#define	IEEE80211_NODE_ERP	0x000004	/* ERP enabled */
/* NB: this must have the same value as IEEE80211_FC1_PWR_MGT */
#define	IEEE80211_NODE_PWR_MGT	0x000010	/* power save mode enabled */
#define	IEEE80211_NODE_AREF	0x000020	/* authentication ref held */
#define	IEEE80211_NODE_HT	0x000040	/* HT enabled */
#define	IEEE80211_NODE_HTCOMPAT	0x000080	/* HT setup w/ vendor OUI's */
#define	IEEE80211_NODE_WPS	0x000100	/* WPS association */
#define	IEEE80211_NODE_TSN	0x000200	/* TSN association */
#define	IEEE80211_NODE_AMPDU_RX	0x000400	/* AMPDU rx enabled */
#define	IEEE80211_NODE_AMPDU_TX	0x000800	/* AMPDU tx enabled */
#define	IEEE80211_NODE_MIMO_PS	0x001000	/* MIMO power save enabled */
#define	IEEE80211_NODE_MIMO_RTS	0x002000	/* send RTS in MIMO PS */
#define	IEEE80211_NODE_RIFS	0x004000	/* RIFS enabled */
#define	IEEE80211_NODE_SGI20	0x008000	/* Short GI in HT20 enabled */
#define	IEEE80211_NODE_SGI40	0x010000	/* Short GI in HT40 enabled */
#define	IEEE80211_NODE_ASSOCID	0x020000	/* xmit requires associd */
#define	IEEE80211_NODE_AMSDU_RX	0x040000	/* AMSDU rx enabled */
#define	IEEE80211_NODE_AMSDU_TX	0x080000	/* AMSDU tx enabled */
	uint16_t		ni_associd;	/* association ID */
	uint16_t		ni_vlan;	/* vlan tag */
	uint16_t		ni_txpower;	/* current transmit power */
	uint8_t			ni_authmode;	/* authentication algorithm */
	uint8_t			ni_ath_flags;	/* Atheros feature flags */
	/* NB: These must have the same values as IEEE80211_ATHC_* */
#define IEEE80211_NODE_TURBOP	0x0001		/* Turbo prime enable */
#define IEEE80211_NODE_COMP	0x0002		/* Compresssion enable */
#define IEEE80211_NODE_FF	0x0004          /* Fast Frame capable */
#define IEEE80211_NODE_XR	0x0008		/* Atheros WME enable */
#define IEEE80211_NODE_AR	0x0010		/* AR capable */
#define IEEE80211_NODE_BOOST	0x0080		/* Dynamic Turbo boosted */
	uint16_t		ni_ath_defkeyix;/* Atheros def key index */
	const struct ieee80211_txparam *ni_txparms;
	uint32_t		ni_jointime;	/* time of join (secs) */
	uint32_t		*ni_challenge;	/* shared-key challenge */
	struct ieee80211_ies	ni_ies;		/* captured ie's */
						/* tx seq per-tid */
	ieee80211_seq		ni_txseqs[IEEE80211_TID_SIZE];
						/* rx seq previous per-tid*/
	ieee80211_seq		ni_rxseqs[IEEE80211_TID_SIZE];
	uint32_t		ni_rxfragstamp;	/* time stamp of last rx frag */
	struct mbuf		*ni_rxfrag[3];	/* rx frag reassembly */
	struct ieee80211_key	ni_ucastkey;	/* unicast key */

	/* hardware */
	uint32_t		ni_avgrssi;	/* recv ssi state */
	int8_t			ni_noise;	/* noise floor */

	/* mimo statistics */
	uint32_t		ni_mimo_rssi_ctl[IEEE80211_MAX_CHAINS];
	uint32_t		ni_mimo_rssi_ext[IEEE80211_MAX_CHAINS];
	uint8_t			ni_mimo_noise_ctl[IEEE80211_MAX_CHAINS];
	uint8_t			ni_mimo_noise_ext[IEEE80211_MAX_CHAINS];
	uint8_t			ni_mimo_chains;

	/* header */
	uint8_t			ni_macaddr[IEEE80211_ADDR_LEN];
	uint8_t			ni_bssid[IEEE80211_ADDR_LEN];

	/* beacon, probe response */
	union {
		uint8_t		data[8];
		u_int64_t	tsf;
	} ni_tstamp;				/* from last rcv'd beacon */
	uint16_t		ni_intval;	/* beacon interval */
	uint16_t		ni_capinfo;	/* capabilities */
	uint8_t			ni_esslen;
	uint8_t			ni_essid[IEEE80211_NWID_LEN];
	struct ieee80211_rateset ni_rates;	/* negotiated rate set */
	struct ieee80211_channel *ni_chan;
	uint16_t		ni_fhdwell;	/* FH only */
	uint8_t			ni_fhindex;	/* FH only */
	uint16_t		ni_erp;		/* ERP from beacon/probe resp */
	uint16_t		ni_timoff;	/* byte offset to TIM ie */
	uint8_t			ni_dtim_period;	/* DTIM period */
	uint8_t			ni_dtim_count;	/* DTIM count for last bcn */

	/* 11s state */
	uint8_t			ni_meshidlen;
	uint8_t			ni_meshid[IEEE80211_MESHID_LEN];
	enum ieee80211_mesh_mlstate ni_mlstate;	/* peering management state */
	uint16_t		ni_mllid;	/* link local ID */
	uint16_t		ni_mlpid;	/* link peer ID */
	struct callout		ni_mltimer;	/* link mesh timer */
	uint8_t			ni_mlrcnt;	/* link mesh retry counter */
	uint8_t			ni_mltval;	/* link mesh timer value */

	/* 11n state */
	uint16_t		ni_htcap;	/* HT capabilities */
	uint8_t			ni_htparam;	/* HT params */
	uint8_t			ni_htctlchan;	/* HT control channel */
	uint8_t			ni_ht2ndchan;	/* HT 2nd channel */
	uint8_t			ni_htopmode;	/* HT operating mode */
	uint8_t			ni_htstbc;	/* HT */
	uint8_t			ni_chw;		/* negotiated channel width */
	struct ieee80211_htrateset ni_htrates;	/* negotiated ht rate set */
	struct ieee80211_tx_ampdu ni_tx_ampdu[WME_NUM_AC];
	struct ieee80211_rx_ampdu ni_rx_ampdu[WME_NUM_TID];

	/* others */
	short			ni_inact;	/* inactivity mark count */
	short			ni_inact_reload;/* inactivity reload value */
	int			ni_txrate;	/* legacy rate/MCS */
	struct ieee80211_psq	ni_psq;		/* power save queue */
	struct ieee80211_nodestats ni_stats;	/* per-node statistics */

	struct ieee80211vap	*ni_wdsvap;	/* associated WDS vap */
	void			*ni_rctls;	/* private ratectl state */
	uint64_t		ni_spare[3];
};
MALLOC_DECLARE(M_80211_NODE);
MALLOC_DECLARE(M_80211_NODE_IE);

#define	IEEE80211_NODE_ATH	(IEEE80211_NODE_FF | IEEE80211_NODE_TURBOP)
#define	IEEE80211_NODE_AMPDU \
	(IEEE80211_NODE_AMPDU_RX | IEEE80211_NODE_AMPDU_TX)
#define	IEEE80211_NODE_AMSDU \
	(IEEE80211_NODE_AMSDU_RX | IEEE80211_NODE_AMSDU_TX)
#define	IEEE80211_NODE_HT_ALL \
	(IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT | \
	 IEEE80211_NODE_AMPDU | IEEE80211_NODE_AMSDU | \
	 IEEE80211_NODE_MIMO_PS | IEEE80211_NODE_MIMO_RTS | \
	 IEEE80211_NODE_RIFS | IEEE80211_NODE_SGI20 | IEEE80211_NODE_SGI40)

#define	IEEE80211_NODE_BITS \
	"\20\1AUTH\2QOS\3ERP\5PWR_MGT\6AREF\7HT\10HTCOMPAT\11WPS\12TSN" \
	"\13AMPDU_RX\14AMPDU_TX\15MIMO_PS\16MIMO_RTS\17RIFS\20SGI20\21SGI40" \
	"\22ASSOCID"

#define	IEEE80211_NODE_AID(ni)	IEEE80211_AID(ni->ni_associd)

#define	IEEE80211_NODE_STAT(ni,stat)	(ni->ni_stats.ns_##stat++)
#define	IEEE80211_NODE_STAT_ADD(ni,stat,v)	(ni->ni_stats.ns_##stat += v)
#define	IEEE80211_NODE_STAT_SET(ni,stat,v)	(ni->ni_stats.ns_##stat = v)

/*
 * Filtered rssi calculation support.  The receive rssi is maintained
 * as an average over the last 10 frames received using a low pass filter
 * (all frames for now, possibly need to be more selective).  Calculations
 * are designed such that a good compiler can optimize them.  The avg
 * rssi state should be initialized to IEEE80211_RSSI_DUMMY_MARKER and
 * each sample incorporated with IEEE80211_RSSI_LPF.  Use IEEE80211_RSSI_GET
 * to extract the current value.
 *
 * Note that we assume rssi data are in the range [-127..127] and we
 * discard values <-20.  This is consistent with assumptions throughout
 * net80211 that signal strength data are in .5 dBm units relative to
 * the current noise floor (linear, not log).
 */
#define IEEE80211_RSSI_LPF_LEN		10
#define	IEEE80211_RSSI_DUMMY_MARKER	127
/* NB: pow2 to optimize out * and / */
#define	IEEE80211_RSSI_EP_MULTIPLIER	(1<<7)
#define IEEE80211_RSSI_IN(x)		((x) * IEEE80211_RSSI_EP_MULTIPLIER)
#define _IEEE80211_RSSI_LPF(x, y, len) \
    (((x) != IEEE80211_RSSI_DUMMY_MARKER) ? (((x) * ((len) - 1) + (y)) / (len)) : (y))
#define IEEE80211_RSSI_LPF(x, y) do {					\
    if ((y) >= -20) {							\
    	x = _IEEE80211_RSSI_LPF((x), IEEE80211_RSSI_IN((y)), 		\
		IEEE80211_RSSI_LPF_LEN);				\
    }									\
} while (0)
#define	IEEE80211_RSSI_EP_RND(x, mul) \
	((((x) % (mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
#define	IEEE80211_RSSI_GET(x) \
	IEEE80211_RSSI_EP_RND(x, IEEE80211_RSSI_EP_MULTIPLIER)

static __inline struct ieee80211_node *
ieee80211_ref_node(struct ieee80211_node *ni)
{
	ieee80211_node_incref(ni);
	return ni;
}

static __inline void
ieee80211_unref_node(struct ieee80211_node **ni)
{
	ieee80211_node_decref(*ni);
	*ni = NULL;			/* guard against use */
}

struct ieee80211com;

void	ieee80211_node_attach(struct ieee80211com *);
void	ieee80211_node_lateattach(struct ieee80211com *);
void	ieee80211_node_detach(struct ieee80211com *);
void	ieee80211_node_vattach(struct ieee80211vap *);
void	ieee80211_node_latevattach(struct ieee80211vap *);
void	ieee80211_node_vdetach(struct ieee80211vap *);

static __inline int
ieee80211_node_is_authorized(const struct ieee80211_node *ni)
{
	return (ni->ni_flags & IEEE80211_NODE_AUTH);
}

void	ieee80211_node_authorize(struct ieee80211_node *);
void	ieee80211_node_unauthorize(struct ieee80211_node *);

void	ieee80211_node_setuptxparms(struct ieee80211_node *);
void	ieee80211_node_set_chan(struct ieee80211_node *,
		struct ieee80211_channel *);
void	ieee80211_create_ibss(struct ieee80211vap*, struct ieee80211_channel *);
void	ieee80211_reset_bss(struct ieee80211vap *);
void	ieee80211_sync_curchan(struct ieee80211com *);
void	ieee80211_setupcurchan(struct ieee80211com *,
	    struct ieee80211_channel *);
void	ieee80211_setcurchan(struct ieee80211com *, struct ieee80211_channel *);
int	ieee80211_ibss_merge(struct ieee80211_node *);
struct ieee80211_scan_entry;
int	ieee80211_sta_join(struct ieee80211vap *, struct ieee80211_channel *,
		const struct ieee80211_scan_entry *);
void	ieee80211_sta_leave(struct ieee80211_node *);
void	ieee80211_node_deauth(struct ieee80211_node *, int);

int	ieee80211_ies_init(struct ieee80211_ies *, const uint8_t *, int);
void	ieee80211_ies_cleanup(struct ieee80211_ies *);
void	ieee80211_ies_expand(struct ieee80211_ies *);
#define	ieee80211_ies_setie(_ies, _ie, _off) do {		\
	(_ies)._ie = (_ies).data + (_off);			\
} while (0)

/*
 * Table of ieee80211_node instances.  Each ieee80211com
 * has one that holds association stations (when operating
 * as an ap) or neighbors (in ibss mode).
 *
 * XXX embed this in ieee80211com instead of indirect?
 */
struct ieee80211_node_table {
	struct ieee80211com	*nt_ic;		/* back reference */
	ieee80211_node_lock_t	nt_nodelock;	/* on node table */
	TAILQ_HEAD(, ieee80211_node) nt_node;	/* information of all nodes */
	LIST_HEAD(, ieee80211_node) nt_hash[IEEE80211_NODE_HASHSIZE];
	struct ieee80211_node	**nt_keyixmap;	/* key ix -> node map */
	int			nt_keyixmax;	/* keyixmap size */
	const char		*nt_name;	/* table name for debug msgs */
	ieee80211_scan_lock_t	nt_scanlock;	/* on nt_scangen */
	u_int			nt_scangen;	/* gen# for iterators */
	int			nt_inact_init;	/* initial node inact setting */
};

struct ieee80211_node *ieee80211_alloc_node(struct ieee80211_node_table *,
		struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_tmp_node(struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_dup_bss(struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_node_create_wds(struct ieee80211vap *,
		const uint8_t bssid[IEEE80211_ADDR_LEN],
		struct ieee80211_channel *);
#ifdef IEEE80211_DEBUG_REFCNT
void	ieee80211_free_node_debug(struct ieee80211_node *,
		const char *func, int line);
struct ieee80211_node *ieee80211_find_node_locked_debug(
		struct ieee80211_node_table *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN],
		const char *func, int line);
struct ieee80211_node *ieee80211_find_node_debug(struct ieee80211_node_table *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN],
		const char *func, int line);
struct ieee80211_node *ieee80211_find_vap_node_locked_debug(
		struct ieee80211_node_table *,
		const struct ieee80211vap *vap,
		const uint8_t macaddr[IEEE80211_ADDR_LEN],
		const char *func, int line);
struct ieee80211_node *ieee80211_find_vap_node_debug(
		struct ieee80211_node_table *,
		const struct ieee80211vap *vap,
		const uint8_t macaddr[IEEE80211_ADDR_LEN],
		const char *func, int line);
struct ieee80211_node * ieee80211_find_rxnode_debug(struct ieee80211com *,
		const struct ieee80211_frame_min *,
		const char *func, int line);
struct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
		struct ieee80211com *,
		const struct ieee80211_frame_min *, uint16_t keyix,
		const char *func, int line);
struct ieee80211_node *ieee80211_find_txnode_debug(struct ieee80211vap *,
		const uint8_t *,
		const char *func, int line);
#define	ieee80211_free_node(ni) \
	ieee80211_free_node_debug(ni, __func__, __LINE__)
#define	ieee80211_find_node_locked(nt, mac) \
	ieee80211_find_node_locked_debug(nt, mac, __func__, __LINE__)
#define	ieee80211_find_node(nt, mac) \
	ieee80211_find_node_debug(nt, mac, __func__, __LINE__)
#define	ieee80211_find_vap_node_locked(nt, vap, mac) \
	ieee80211_find_vap_node_locked_debug(nt, vap, mac, __func__, __LINE__)
#define	ieee80211_find_vap_node(nt, vap, mac) \
	ieee80211_find_vap_node_debug(nt, vap, mac, __func__, __LINE__)
#define	ieee80211_find_rxnode(ic, wh) \
	ieee80211_find_rxnode_debug(ic, wh, __func__, __LINE__)
#define	ieee80211_find_rxnode_withkey(ic, wh, keyix) \
	ieee80211_find_rxnode_withkey_debug(ic, wh, keyix, __func__, __LINE__)
#define	ieee80211_find_txnode(vap, mac) \
	ieee80211_find_txnode_debug(vap, mac, __func__, __LINE__)
#else
void	ieee80211_free_node(struct ieee80211_node *);
struct ieee80211_node *ieee80211_find_node_locked(struct ieee80211_node_table *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_find_node(struct ieee80211_node_table *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_find_vap_node_locked(
		struct ieee80211_node_table *, const struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node *ieee80211_find_vap_node(
		struct ieee80211_node_table *, const struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_node * ieee80211_find_rxnode(struct ieee80211com *,
		const struct ieee80211_frame_min *);
struct ieee80211_node * ieee80211_find_rxnode_withkey(struct ieee80211com *,
		const struct ieee80211_frame_min *, uint16_t keyix);
struct ieee80211_node *ieee80211_find_txnode(struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
#endif
int	ieee80211_node_delucastkey(struct ieee80211_node *);
void	ieee80211_node_timeout(void *arg);

typedef void ieee80211_iter_func(void *, struct ieee80211_node *);
void	ieee80211_iterate_nodes(struct ieee80211_node_table *,
		ieee80211_iter_func *, void *);

void	ieee80211_notify_erp(struct ieee80211com *);
void	ieee80211_dump_node(struct ieee80211_node_table *,
		struct ieee80211_node *);
void	ieee80211_dump_nodes(struct ieee80211_node_table *);

struct ieee80211_node *ieee80211_fakeup_adhoc_node(struct ieee80211vap *,
		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
struct ieee80211_scanparams;
void	ieee80211_init_neighbor(struct ieee80211_node *,
		const struct ieee80211_frame *,
		const struct ieee80211_scanparams *);
struct ieee80211_node *ieee80211_add_neighbor(struct ieee80211vap *,
		const struct ieee80211_frame *,
		const struct ieee80211_scanparams *);
void	ieee80211_node_join(struct ieee80211_node *,int);
void	ieee80211_node_leave(struct ieee80211_node *);
int8_t	ieee80211_getrssi(struct ieee80211vap *);
void	ieee80211_getsignal(struct ieee80211vap *, int8_t *, int8_t *);
#endif /* _NET80211_IEEE80211_NODE_H_ */


code:
/*-
 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_phy.h 193072 2009-05-29 23:39:16Z sam $
 */

#ifndef _NET80211_IEEE80211_PHY_H_
#define _NET80211_IEEE80211_PHY_H_

#ifdef _KERNEL
/*
 * IEEE 802.11 PHY-related definitions.
 */

/*
 * Contention window (slots).
 */
#define IEEE80211_CW_MAX	1023	/* aCWmax */
#define IEEE80211_CW_MIN_0	31	/* DS/CCK aCWmin, ERP aCWmin(0) */
#define IEEE80211_CW_MIN_1	15	/* OFDM aCWmin, ERP aCWmin(1) */

/*
 * SIFS (microseconds).
 */
#define IEEE80211_DUR_SIFS	10	/* DS/CCK/ERP SIFS */
#define IEEE80211_DUR_OFDM_SIFS	16	/* OFDM SIFS */

/*
 * Slot time (microseconds).
 */
#define IEEE80211_DUR_SLOT	20	/* DS/CCK slottime, ERP long slottime */
#define IEEE80211_DUR_SHSLOT	9	/* ERP short slottime */
#define IEEE80211_DUR_OFDM_SLOT	9	/* OFDM slottime */

/*
 * DIFS (microseconds).
 */
#define IEEE80211_DUR_DIFS(sifs, slot)	((sifs) + 2 * (slot))

struct ieee80211_channel;

struct ieee80211_rate_table {
	int		rateCount;		/* NB: for proper padding */
	uint8_t		rateCodeToIndex[256];	/* back mapping */
	struct {
		uint8_t		phy;		/* CCK/OFDM/TURBO */
		uint32_t	rateKbps;	/* transfer rate in kbs */
		uint8_t		shortPreamble;	/* mask for enabling short
						 * preamble in CCK rate code */
		uint8_t		dot11Rate;	/* value for supported rates
						 * info element of MLME */
		uint8_t		ctlRateIndex;	/* index of next lower basic
						 * rate; used for dur. calcs */
		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
		uint16_t	spAckDuration;	/* short preamble ACK dur. */
	} info[32];
};

const struct ieee80211_rate_table *ieee80211_get_ratetable(
			struct ieee80211_channel *);

static __inline__ uint8_t
ieee80211_ack_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
{
	uint8_t cix = rt->info[rt->rateCodeToIndex[rate]].ctlRateIndex;
	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
	return rt->info[cix].dot11Rate;
}

static __inline__ uint8_t
ieee80211_ctl_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
{
	uint8_t cix = rt->info[rt->rateCodeToIndex[rate]].ctlRateIndex;
	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
	return rt->info[cix].dot11Rate;
}

static __inline__ enum ieee80211_phytype
ieee80211_rate2phytype(const struct ieee80211_rate_table *rt, uint8_t rate)
{
	uint8_t rix = rt->rateCodeToIndex[rate];
	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
	return rt->info[rix].phy;
}

static __inline__ int
ieee80211_isratevalid(const struct ieee80211_rate_table *rt, uint8_t rate)
{
	return rt->rateCodeToIndex[rate] != (uint8_t)-1;
}

/*
 * Calculate ACK field for
 * o  non-fragment data frames
 * o  management frames
 * sent using rate, phy and short preamble setting.
 */
static __inline__ uint16_t
ieee80211_ack_duration(const struct ieee80211_rate_table *rt,
    uint8_t rate, int isShortPreamble)
{
	uint8_t rix = rt->rateCodeToIndex[rate];

	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
	if (isShortPreamble) {
		KASSERT(rt->info[rix].spAckDuration != 0,
			("shpreamble ack dur is not computed!\n"));
		return rt->info[rix].spAckDuration;
	} else {
		KASSERT(rt->info[rix].lpAckDuration != 0,
			("lgpreamble ack dur is not computed!\n"));
		return rt->info[rix].lpAckDuration;
	}
}

/*
 * Compute the time to transmit a frame of length frameLen bytes
 * using the specified 802.11 rate code, phy, and short preamble
 * setting.
 *
 * NB: SIFS is included.
 */
uint16_t	ieee80211_compute_duration(const struct ieee80211_rate_table *,
			uint32_t frameLen, uint16_t rate, int isShortPreamble);
/*
 * Convert PLCP signal/rate field to 802.11 rate code (.5Mbits/s)
 */
uint8_t		ieee80211_plcp2rate(uint8_t, enum ieee80211_phytype);
/*
 * Convert 802.11 rate code to PLCP signal.
 */
uint8_t		ieee80211_rate2plcp(int, enum ieee80211_phytype);
#endif	/* _KERNEL */
#endif	/* !_NET80211_IEEE80211_PHY_H_ */


code:
/*-
 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD: release/9.0.0/sys/net80211/ieee80211_power.h 184288 2008-10-26 01:04:46Z sam $
 */
#ifndef _NET80211_IEEE80211_POWER_H_
#define _NET80211_IEEE80211_POWER_H_

struct ieee80211com;
struct ieee80211vap;
struct ieee80211_node;
struct mbuf;

/*
 * Power save packet queues.  There are two queues, one
 * for frames coming from the net80211 layer and the other
 * for frames that come from the driver. Frames from the
 * driver are expected to have M_ENCAP marked to indicate
 * they have already been encapsulated and are treated as
 * higher priority: they are sent first when flushing the
 * queue on a power save state change or in response to a
 * ps-poll frame.
 *
 * Note that frames sent from the high priority queue are
 * fed directly to the driver without going through
 * ieee80211_start again; drivers that send up encap'd
 * frames are required to handle them when they come back.
 */
struct ieee80211_psq {
	ieee80211_psq_lock_t psq_lock;
	int	psq_len;
	int	psq_maxlen;
	int	psq_drops;
	struct ieee80211_psq_head {
		struct mbuf *head;
		struct mbuf *tail;
		int len;
	} psq_head[2];			/* 2 priorities */
};

void	ieee80211_psq_init(struct ieee80211_psq *, const char *);
void	ieee80211_psq_cleanup(struct ieee80211_psq *);

void	ieee80211_power_attach(struct ieee80211com *);
void	ieee80211_power_detach(struct ieee80211com *);
void	ieee80211_power_vattach(struct ieee80211vap *);
void	ieee80211_power_vdetach(struct ieee80211vap *);
void	ieee80211_power_latevattach(struct ieee80211vap *);

struct mbuf *ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen);
int	ieee80211_node_psq_drain(struct ieee80211_node *);
int	ieee80211_node_psq_age(struct ieee80211_node *);
int	ieee80211_pwrsave(struct ieee80211_node *, struct mbuf *);
void	ieee80211_node_pwrsave(struct ieee80211_node *, int enable);
void	ieee80211_sta_pwrsave(struct ieee80211vap *, int enable);

void	ieee80211_power_poll(struct ieee80211com *);
#endif /* _NET80211_IEEE80211_POWER_H_ */