ieee80211_freebsd.c revision 144302
1138568Ssam/*- 2139530Ssam * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting 3138568Ssam * All rights reserved. 4138568Ssam * 5138568Ssam * Redistribution and use in source and binary forms, with or without 6138568Ssam * modification, are permitted provided that the following conditions 7138568Ssam * are met: 8138568Ssam * 1. Redistributions of source code must retain the above copyright 9138568Ssam * notice, this list of conditions and the following disclaimer. 10138568Ssam * 2. Redistributions in binary form must reproduce the above copyright 11138568Ssam * notice, this list of conditions and the following disclaimer in the 12138568Ssam * documentation and/or other materials provided with the distribution. 13138568Ssam * 3. The name of the author may not be used to endorse or promote products 14138568Ssam * derived from this software without specific prior written permission. 15138568Ssam * 16138568Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17138568Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18138568Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19138568Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20138568Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21138568Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22138568Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23138568Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24138568Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25138568Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26138568Ssam */ 27138568Ssam 28138568Ssam#include <sys/cdefs.h> 29138568Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_freebsd.c 144302 2005-03-29 19:36:42Z sam $"); 30138568Ssam 31138568Ssam/* 32138568Ssam * IEEE 802.11 support (FreeBSD-specific code) 33138568Ssam */ 34138568Ssam#include <sys/param.h> 35138568Ssam#include <sys/kernel.h> 36138568Ssam#include <sys/systm.h> 37138568Ssam#include <sys/linker.h> 38138568Ssam#include <sys/mbuf.h> 39138568Ssam#include <sys/module.h> 40138568Ssam#include <sys/proc.h> 41138568Ssam#include <sys/sysctl.h> 42138568Ssam 43138568Ssam#include <sys/socket.h> 44138568Ssam 45138568Ssam#include <net/if.h> 46138568Ssam#include <net/if_media.h> 47138568Ssam#include <net/ethernet.h> 48138568Ssam#include <net/route.h> 49138568Ssam 50138568Ssam#include <net80211/ieee80211_var.h> 51138568Ssam 52138568SsamSYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters"); 53138568Ssam 54138568Ssam#ifdef IEEE80211_DEBUG 55138568Ssamint ieee80211_debug = 0; 56138568SsamSYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug, 57138568Ssam 0, "debugging printfs"); 58138568Ssam#endif 59138568Ssam 60138568Ssamstatic int 61138568Ssamieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS) 62138568Ssam{ 63138568Ssam int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT; 64138568Ssam int error; 65138568Ssam 66138568Ssam error = sysctl_handle_int(oidp, &inact, 0, req); 67138568Ssam if (error || !req->newptr) 68138568Ssam return error; 69138568Ssam *(int *)arg1 = inact / IEEE80211_INACT_WAIT; 70138568Ssam return 0; 71138568Ssam} 72138568Ssam 73138568Ssamstatic int 74138568Ssamieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS) 75138568Ssam{ 76138568Ssam struct ieee80211com *ic = arg1; 77138568Ssam const char *name = ic->ic_ifp->if_xname; 78138568Ssam 79138568Ssam return SYSCTL_OUT(req, name, strlen(name)); 80138568Ssam} 81138568Ssam 82138568Ssamvoid 83138568Ssamieee80211_sysctl_attach(struct ieee80211com *ic) 84138568Ssam{ 85138568Ssam struct sysctl_ctx_list *ctx; 86138568Ssam struct sysctl_oid *oid; 87138568Ssam char num[14]; /* sufficient for 32 bits */ 88138568Ssam 89138568Ssam MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list), 90138568Ssam M_DEVBUF, M_NOWAIT | M_ZERO); 91138568Ssam if (ctx == NULL) { 92138568Ssam if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n", 93138568Ssam __func__); 94138568Ssam return; 95138568Ssam } 96138568Ssam sysctl_ctx_init(ctx); 97138568Ssam snprintf(num, sizeof(num), "%u", ic->ic_vap); 98138568Ssam oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan), 99138568Ssam OID_AUTO, num, CTLFLAG_RD, NULL, ""); 100138568Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 101138568Ssam "%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A", 102138568Ssam "parent device"); 103138568Ssam#ifdef IEEE80211_DEBUG 104138568Ssam ic->ic_debug = ieee80211_debug; 105138568Ssam SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 106138568Ssam "debug", CTLFLAG_RW, &ic->ic_debug, 0, 107138568Ssam "control debugging printfs"); 108138568Ssam#endif 109138568Ssam /* XXX inherit from tunables */ 110138568Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 111140633Ssam "inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0, 112138568Ssam ieee80211_sysctl_inact, "I", 113138568Ssam "station inactivity timeout (sec)"); 114138568Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 115138568Ssam "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0, 116138568Ssam ieee80211_sysctl_inact, "I", 117138568Ssam "station inactivity probe timeout (sec)"); 118138568Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 119138568Ssam "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0, 120138568Ssam ieee80211_sysctl_inact, "I", 121138568Ssam "station authentication timeout (sec)"); 122138568Ssam SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 123138568Ssam "inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0, 124138568Ssam ieee80211_sysctl_inact, "I", 125138568Ssam "station initial state timeout (sec)"); 126139512Ssam SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 127139512Ssam "driver_caps", CTLFLAG_RW, &ic->ic_caps, 0, 128139512Ssam "driver capabilities"); 129138568Ssam ic->ic_sysctl = ctx; 130138568Ssam} 131138568Ssam 132138568Ssamvoid 133138568Ssamieee80211_sysctl_detach(struct ieee80211com *ic) 134138568Ssam{ 135138568Ssam 136138568Ssam if (ic->ic_sysctl != NULL) { 137138568Ssam sysctl_ctx_free(ic->ic_sysctl); 138138568Ssam ic->ic_sysctl = NULL; 139138568Ssam } 140138568Ssam} 141138568Ssam 142138568Ssamint 143138568Ssamieee80211_node_dectestref(struct ieee80211_node *ni) 144138568Ssam{ 145138568Ssam /* XXX need equivalent of atomic_dec_and_test */ 146138568Ssam atomic_subtract_int(&ni->ni_refcnt, 1); 147138568Ssam return atomic_cmpset_int(&ni->ni_refcnt, 0, 1); 148138568Ssam} 149138568Ssam 150138568Ssam/* 151138568Ssam * Allocate and setup a management frame of the specified 152138568Ssam * size. We return the mbuf and a pointer to the start 153138568Ssam * of the contiguous data area that's been reserved based 154138568Ssam * on the packet length. The data area is forced to 32-bit 155138568Ssam * alignment and the buffer length to a multiple of 4 bytes. 156138568Ssam * This is done mainly so beacon frames (that require this) 157138568Ssam * can use this interface too. 158138568Ssam */ 159138568Ssamstruct mbuf * 160138568Ssamieee80211_getmgtframe(u_int8_t **frm, u_int pktlen) 161138568Ssam{ 162138568Ssam struct mbuf *m; 163138568Ssam u_int len; 164138568Ssam 165138568Ssam /* 166138568Ssam * NB: we know the mbuf routines will align the data area 167138568Ssam * so we don't need to do anything special. 168138568Ssam */ 169138568Ssam /* XXX 4-address frame? */ 170138568Ssam len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4); 171138568Ssam KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len)); 172138568Ssam if (len < MINCLSIZE) { 173138568Ssam m = m_gethdr(M_NOWAIT, MT_HEADER); 174138568Ssam /* 175138568Ssam * Align the data in case additional headers are added. 176138568Ssam * This should only happen when a WEP header is added 177138568Ssam * which only happens for shared key authentication mgt 178138568Ssam * frames which all fit in MHLEN. 179138568Ssam */ 180138568Ssam if (m != NULL) 181138568Ssam MH_ALIGN(m, len); 182138568Ssam } else 183138568Ssam m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR); 184138568Ssam if (m != NULL) { 185138568Ssam m->m_data += sizeof(struct ieee80211_frame); 186138568Ssam *frm = m->m_data; 187138568Ssam } 188138568Ssam return m; 189138568Ssam} 190138568Ssam 191138568Ssam#include <sys/libkern.h> 192138568Ssam 193138568Ssamvoid 194138568Ssamget_random_bytes(void *p, size_t n) 195138568Ssam{ 196138568Ssam u_int8_t *dp = p; 197138568Ssam 198138568Ssam while (n > 0) { 199138568Ssam u_int32_t v = arc4random(); 200138568Ssam size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n; 201138568Ssam bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n); 202138568Ssam dp += sizeof(u_int32_t), n -= nb; 203138568Ssam } 204138568Ssam} 205138568Ssam 206138568Ssamvoid 207138568Ssamieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc) 208138568Ssam{ 209138568Ssam struct ifnet *ifp = ic->ic_ifp; 210138568Ssam struct ieee80211_join_event iev; 211138568Ssam 212144302Ssam memset(&iev, 0, sizeof(iev)); 213138568Ssam if (ni == ic->ic_bss) { 214138568Ssam IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid); 215138568Ssam rt_ieee80211msg(ifp, newassoc ? 216138568Ssam RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, 217138568Ssam &iev, sizeof(iev)); 218138568Ssam if_link_state_change(ifp, LINK_STATE_UP); 219144302Ssam } else { 220138568Ssam IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 221144302Ssam rt_ieee80211msg(ifp, newassoc ? 222144302Ssam RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, 223144302Ssam &iev, sizeof(iev)); 224138568Ssam } 225138568Ssam} 226138568Ssam 227138568Ssamvoid 228138568Ssamieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 229138568Ssam{ 230138568Ssam struct ifnet *ifp = ic->ic_ifp; 231138568Ssam struct ieee80211_leave_event iev; 232138568Ssam 233138568Ssam if (ni == ic->ic_bss) { 234138568Ssam rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0); 235138568Ssam if_link_state_change(ifp, LINK_STATE_DOWN); 236138568Ssam } else { 237138568Ssam /* fire off wireless event station leaving */ 238138568Ssam memset(&iev, 0, sizeof(iev)); 239138568Ssam IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr); 240138568Ssam rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev)); 241138568Ssam } 242138568Ssam} 243138568Ssam 244138568Ssamvoid 245138568Ssamieee80211_notify_scan_done(struct ieee80211com *ic) 246138568Ssam{ 247138568Ssam struct ifnet *ifp = ic->ic_ifp; 248138568Ssam 249138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, 250138568Ssam "%s: notify scan done\n", ic->ic_ifp->if_xname); 251138568Ssam 252138568Ssam /* dispatch wireless event indicating scan completed */ 253138568Ssam rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0); 254138568Ssam} 255138568Ssam 256138568Ssamvoid 257138568Ssamieee80211_notify_replay_failure(struct ieee80211com *ic, 258138568Ssam const struct ieee80211_frame *wh, const struct ieee80211_key *k, 259138568Ssam u_int64_t rsc) 260138568Ssam{ 261138568Ssam struct ifnet *ifp = ic->ic_ifp; 262138568Ssam 263138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 264139512Ssam "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u>\n", 265138568Ssam ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name, 266139512Ssam (intmax_t) rsc, (intmax_t) k->wk_keyrsc, k->wk_keyix); 267138568Ssam 268138568Ssam if (ifp != NULL) { /* NB: for cipher test modules */ 269138568Ssam struct ieee80211_replay_event iev; 270138568Ssam 271138568Ssam IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 272138568Ssam IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 273138568Ssam iev.iev_cipher = k->wk_cipher->ic_cipher; 274138568Ssam iev.iev_keyix = k->wk_keyix; 275138568Ssam iev.iev_keyrsc = k->wk_keyrsc; 276138568Ssam iev.iev_rsc = rsc; 277138568Ssam rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev)); 278138568Ssam } 279138568Ssam} 280138568Ssam 281138568Ssamvoid 282138568Ssamieee80211_notify_michael_failure(struct ieee80211com *ic, 283138568Ssam const struct ieee80211_frame *wh, u_int keyix) 284138568Ssam{ 285138568Ssam struct ifnet *ifp = ic->ic_ifp; 286138568Ssam 287138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 288139512Ssam "[%s] michael MIC verification failed <keyix %u>\n", 289138568Ssam ether_sprintf(wh->i_addr2), keyix); 290138568Ssam ic->ic_stats.is_rx_tkipmic++; 291138568Ssam 292138568Ssam if (ifp != NULL) { /* NB: for cipher test modules */ 293138568Ssam struct ieee80211_michael_event iev; 294138568Ssam 295138568Ssam IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1); 296138568Ssam IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2); 297138568Ssam iev.iev_cipher = IEEE80211_CIPHER_TKIP; 298138568Ssam iev.iev_keyix = keyix; 299138568Ssam rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev)); 300138568Ssam } 301138568Ssam} 302138568Ssam 303138568Ssamvoid 304138568Ssamieee80211_load_module(const char *modname) 305138568Ssam{ 306138777Ssam#ifdef notyet 307138568Ssam struct thread *td = curthread; 308138568Ssam 309138568Ssam if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) { 310138568Ssam mtx_lock(&Giant); 311138568Ssam (void) linker_load_module(modname, NULL, NULL, NULL, NULL); 312138568Ssam mtx_unlock(&Giant); 313138568Ssam } 314138777Ssam#else 315138777Ssam printf("%s: load the %s module by hand for now.\n", __func__, modname); 316138777Ssam#endif 317138568Ssam} 318138568Ssam 319138568Ssam/* 320138568Ssam * Module glue. 321138568Ssam * 322138568Ssam * NB: the module name is "wlan" for compatibility with NetBSD. 323138568Ssam */ 324138568Ssamstatic int 325138568Ssamwlan_modevent(module_t mod, int type, void *unused) 326138568Ssam{ 327138568Ssam switch (type) { 328138568Ssam case MOD_LOAD: 329138568Ssam if (bootverbose) 330138568Ssam printf("wlan: <802.11 Link Layer>\n"); 331138568Ssam return 0; 332138568Ssam case MOD_UNLOAD: 333138568Ssam return 0; 334138568Ssam } 335138568Ssam return EINVAL; 336138568Ssam} 337138568Ssam 338138568Ssamstatic moduledata_t wlan_mod = { 339138568Ssam "wlan", 340138568Ssam wlan_modevent, 341138568Ssam 0 342138568Ssam}; 343138568SsamDECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 344138568SsamMODULE_VERSION(wlan, 1); 345138568SsamMODULE_DEPEND(wlan, ether, 1, 1, 1); 346