2 * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 support (FreeBSD-specific code)
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/linker.h>
39 #include <sys/module.h>
41 #include <sys/sysctl.h>
43 #include <sys/socket.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
48 #include <net/route.h>
50 #include <net80211/ieee80211_var.h>
52 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
54 #ifdef IEEE80211_DEBUG
55 int ieee80211_debug = 0;
56 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
57 0, "debugging printfs");
61 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
63 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
66 error = sysctl_handle_int(oidp, &inact, 0, req);
67 if (error || !req->newptr)
69 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
74 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
76 struct ieee80211com *ic = arg1;
77 const char *name = ic->ic_ifp->if_xname;
79 return SYSCTL_OUT(req, name, strlen(name));
83 ieee80211_sysctl_attach(struct ieee80211com *ic)
85 struct sysctl_ctx_list *ctx;
86 struct sysctl_oid *oid;
87 char num[14]; /* sufficient for 32 bits */
89 MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
90 M_DEVBUF, M_NOWAIT | M_ZERO);
92 if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
97 snprintf(num, sizeof(num), "%u", ic->ic_vap);
98 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
99 OID_AUTO, num, CTLFLAG_RD, NULL, "");
100 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
101 "%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
103 #ifdef IEEE80211_DEBUG
104 ic->ic_debug = ieee80211_debug;
105 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
106 "debug", CTLFLAG_RW, &ic->ic_debug, 0,
107 "control debugging printfs");
109 /* XXX inherit from tunables */
110 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
111 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
112 ieee80211_sysctl_inact, "I",
113 "station inactivity timeout (sec)");
114 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
115 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
116 ieee80211_sysctl_inact, "I",
117 "station inactivity probe timeout (sec)");
118 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
119 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
120 ieee80211_sysctl_inact, "I",
121 "station authentication timeout (sec)");
122 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
123 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
124 ieee80211_sysctl_inact, "I",
125 "station initial state timeout (sec)");
126 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
127 "driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
128 "driver capabilities");
133 ieee80211_sysctl_detach(struct ieee80211com *ic)
136 if (ic->ic_sysctl != NULL) {
137 sysctl_ctx_free(ic->ic_sysctl);
138 ic->ic_sysctl = NULL;
143 ieee80211_node_dectestref(struct ieee80211_node *ni)
145 /* XXX need equivalent of atomic_dec_and_test */
146 atomic_subtract_int(&ni->ni_refcnt, 1);
147 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
151 * Allocate and setup a management frame of the specified
152 * size. We return the mbuf and a pointer to the start
153 * of the contiguous data area that's been reserved based
154 * on the packet length. The data area is forced to 32-bit
155 * alignment and the buffer length to a multiple of 4 bytes.
156 * This is done mainly so beacon frames (that require this)
157 * can use this interface too.
160 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
166 * NB: we know the mbuf routines will align the data area
167 * so we don't need to do anything special.
169 /* XXX 4-address frame? */
170 len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
171 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
172 if (len < MINCLSIZE) {
173 m = m_gethdr(M_NOWAIT, MT_HEADER);
175 * Align the data in case additional headers are added.
176 * This should only happen when a WEP header is added
177 * which only happens for shared key authentication mgt
178 * frames which all fit in MHLEN.
183 m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
185 m->m_data += sizeof(struct ieee80211_frame);
191 #include <sys/libkern.h>
194 get_random_bytes(void *p, size_t n)
199 u_int32_t v = arc4random();
200 size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
201 bcopy(&v, dp, n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n);
202 dp += sizeof(u_int32_t), n -= nb;
207 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
209 struct ifnet *ifp = ic->ic_ifp;
210 struct ieee80211_join_event iev;
212 memset(&iev, 0, sizeof(iev));
213 if (ni == ic->ic_bss) {
214 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
215 rt_ieee80211msg(ifp, newassoc ?
216 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
218 if_link_state_change(ifp, LINK_STATE_UP);
220 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
221 rt_ieee80211msg(ifp, newassoc ?
222 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
228 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
230 struct ifnet *ifp = ic->ic_ifp;
231 struct ieee80211_leave_event iev;
233 if (ni == ic->ic_bss) {
234 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
235 if_link_state_change(ifp, LINK_STATE_DOWN);
237 /* fire off wireless event station leaving */
238 memset(&iev, 0, sizeof(iev));
239 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
240 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
245 ieee80211_notify_scan_done(struct ieee80211com *ic)
247 struct ifnet *ifp = ic->ic_ifp;
249 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
250 "%s: notify scan done\n", ic->ic_ifp->if_xname);
252 /* dispatch wireless event indicating scan completed */
253 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
257 ieee80211_notify_replay_failure(struct ieee80211com *ic,
258 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
261 struct ifnet *ifp = ic->ic_ifp;
263 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
264 "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u>\n",
265 ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
266 (intmax_t) rsc, (intmax_t) k->wk_keyrsc, k->wk_keyix);
268 if (ifp != NULL) { /* NB: for cipher test modules */
269 struct ieee80211_replay_event iev;
271 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
272 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
273 iev.iev_cipher = k->wk_cipher->ic_cipher;
274 iev.iev_keyix = k->wk_keyix;
275 iev.iev_keyrsc = k->wk_keyrsc;
277 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
282 ieee80211_notify_michael_failure(struct ieee80211com *ic,
283 const struct ieee80211_frame *wh, u_int keyix)
285 struct ifnet *ifp = ic->ic_ifp;
287 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
288 "[%s] michael MIC verification failed <keyix %u>\n",
289 ether_sprintf(wh->i_addr2), keyix);
290 ic->ic_stats.is_rx_tkipmic++;
292 if (ifp != NULL) { /* NB: for cipher test modules */
293 struct ieee80211_michael_event iev;
295 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
296 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
297 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
298 iev.iev_keyix = keyix;
299 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
304 ieee80211_load_module(const char *modname)
307 struct thread *td = curthread;
309 if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
311 (void) linker_load_module(modname, NULL, NULL, NULL, NULL);
315 printf("%s: load the %s module by hand for now.\n", __func__, modname);
322 * NB: the module name is "wlan" for compatibility with NetBSD.
325 wlan_modevent(module_t mod, int type, void *unused)
330 printf("wlan: <802.11 Link Layer>\n");
338 static moduledata_t wlan_mod = {
343 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
344 MODULE_VERSION(wlan, 1);
345 MODULE_DEPEND(wlan, ether, 1, 1, 1);