2 * Copyright (c) 2003-2007 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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 support (FreeBSD-specific code)
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/linker.h>
37 #include <sys/module.h>
39 #include <sys/sysctl.h>
41 #include <sys/socket.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
46 #include <net/route.h>
48 #include <net80211/ieee80211_var.h>
50 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
52 #ifdef IEEE80211_DEBUG
53 int ieee80211_debug = 0;
54 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
55 0, "debugging printfs");
59 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
61 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
64 error = sysctl_handle_int(oidp, &inact, 0, req);
65 if (error || !req->newptr)
67 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
72 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
74 struct ieee80211com *ic = arg1;
75 const char *name = ic->ic_ifp->if_xname;
77 return SYSCTL_OUT(req, name, strlen(name));
81 ieee80211_sysctl_attach(struct ieee80211com *ic)
83 struct sysctl_ctx_list *ctx;
84 struct sysctl_oid *oid;
85 char num[14]; /* sufficient for 32 bits */
87 MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
88 M_DEVBUF, M_NOWAIT | M_ZERO);
90 if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
95 snprintf(num, sizeof(num), "%u", ic->ic_vap);
96 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
97 OID_AUTO, num, CTLFLAG_RD, NULL, "");
98 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
99 "%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
101 #ifdef IEEE80211_DEBUG
102 ic->ic_debug = ieee80211_debug;
103 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
104 "debug", CTLFLAG_RW, &ic->ic_debug, 0,
105 "control debugging printfs");
107 /* XXX inherit from tunables */
108 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
109 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
110 ieee80211_sysctl_inact, "I",
111 "station inactivity timeout (sec)");
112 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
113 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
114 ieee80211_sysctl_inact, "I",
115 "station inactivity probe timeout (sec)");
116 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
117 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
118 ieee80211_sysctl_inact, "I",
119 "station authentication timeout (sec)");
120 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
121 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
122 ieee80211_sysctl_inact, "I",
123 "station initial state timeout (sec)");
124 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
125 "driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
126 "driver capabilities");
127 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
128 "bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0,
129 "consecutive beacon misses before scanning");
134 ieee80211_sysctl_detach(struct ieee80211com *ic)
137 if (ic->ic_sysctl != NULL) {
138 sysctl_ctx_free(ic->ic_sysctl);
139 ic->ic_sysctl = NULL;
144 ieee80211_node_dectestref(struct ieee80211_node *ni)
146 /* XXX need equivalent of atomic_dec_and_test */
147 atomic_subtract_int(&ni->ni_refcnt, 1);
148 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
152 ieee80211_drain_ifq(struct ifqueue *ifq)
154 struct ieee80211_node *ni;
162 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
163 KASSERT(ni != NULL, ("frame w/o node"));
164 ieee80211_free_node(ni);
165 m->m_pkthdr.rcvif = NULL;
172 * As above, for mbufs allocated with m_gethdr/MGETHDR
173 * or initialized by M_COPY_PKTHDR.
175 #define MC_ALIGN(m, len) \
177 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \
178 } while (/* CONSTCOND */ 0)
181 * Allocate and setup a management frame of the specified
182 * size. We return the mbuf and a pointer to the start
183 * of the contiguous data area that's been reserved based
184 * on the packet length. The data area is forced to 32-bit
185 * alignment and the buffer length to a multiple of 4 bytes.
186 * This is done mainly so beacon frames (that require this)
187 * can use this interface too.
190 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
196 * NB: we know the mbuf routines will align the data area
197 * so we don't need to do anything special.
199 len = roundup2(headroom + pktlen, 4);
200 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
201 if (len < MINCLSIZE) {
202 m = m_gethdr(M_NOWAIT, MT_DATA);
204 * Align the data in case additional headers are added.
205 * This should only happen when a WEP header is added
206 * which only happens for shared key authentication mgt
207 * frames which all fit in MHLEN.
212 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
217 m->m_data += sizeof(struct ieee80211_frame);
224 ieee80211_add_callback(struct mbuf *m,
225 void (*func)(struct ieee80211_node *, void *, int), void *arg)
228 struct ieee80211_cb *cb;
230 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
231 sizeof(struct ieee80211_cb), M_NOWAIT);
235 cb = (struct ieee80211_cb *)(mtag+1);
238 m_tag_prepend(m, mtag);
239 m->m_flags |= M_TXCB;
244 ieee80211_process_callback(struct ieee80211_node *ni,
245 struct mbuf *m, int status)
249 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
251 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
252 cb->func(ni, cb->arg, status);
256 #include <sys/libkern.h>
259 get_random_bytes(void *p, size_t n)
264 uint32_t v = arc4random();
265 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
266 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
267 dp += sizeof(uint32_t), n -= nb;
272 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
274 struct ifnet *ifp = ic->ic_ifp;
275 struct ieee80211_join_event iev;
277 memset(&iev, 0, sizeof(iev));
278 if (ni == ic->ic_bss) {
279 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
280 rt_ieee80211msg(ifp, newassoc ?
281 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
283 if_link_state_change(ifp, LINK_STATE_UP);
285 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
286 rt_ieee80211msg(ifp, newassoc ?
287 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
293 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
295 struct ifnet *ifp = ic->ic_ifp;
296 struct ieee80211_leave_event iev;
298 if (ni == ic->ic_bss) {
299 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
300 if_link_state_change(ifp, LINK_STATE_DOWN);
302 /* fire off wireless event station leaving */
303 memset(&iev, 0, sizeof(iev));
304 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
305 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
310 ieee80211_notify_scan_done(struct ieee80211com *ic)
312 struct ifnet *ifp = ic->ic_ifp;
314 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
316 /* dispatch wireless event indicating scan completed */
317 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
321 ieee80211_notify_replay_failure(struct ieee80211com *ic,
322 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
325 struct ifnet *ifp = ic->ic_ifp;
327 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
328 "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
329 ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
330 (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
331 k->wk_keyix, k->wk_rxkeyix);
333 if (ifp != NULL) { /* NB: for cipher test modules */
334 struct ieee80211_replay_event iev;
336 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
337 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
338 iev.iev_cipher = k->wk_cipher->ic_cipher;
339 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
340 iev.iev_keyix = k->wk_rxkeyix;
342 iev.iev_keyix = k->wk_keyix;
343 iev.iev_keyrsc = k->wk_keyrsc;
345 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
350 ieee80211_notify_michael_failure(struct ieee80211com *ic,
351 const struct ieee80211_frame *wh, u_int keyix)
353 struct ifnet *ifp = ic->ic_ifp;
355 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
356 "[%s] michael MIC verification failed <keyix %u>\n",
357 ether_sprintf(wh->i_addr2), keyix);
358 ic->ic_stats.is_rx_tkipmic++;
360 if (ifp != NULL) { /* NB: for cipher test modules */
361 struct ieee80211_michael_event iev;
363 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
364 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
365 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
366 iev.iev_keyix = keyix;
367 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
372 ieee80211_load_module(const char *modname)
376 (void)kern_kldload(curthread, modname, NULL);
378 printf("%s: load the %s module by hand for now.\n", __func__, modname);
385 * NB: the module name is "wlan" for compatibility with NetBSD.
388 wlan_modevent(module_t mod, int type, void *unused)
393 printf("wlan: <802.11 Link Layer>\n");
401 static moduledata_t wlan_mod = {
406 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
407 MODULE_VERSION(wlan, 1);
408 MODULE_DEPEND(wlan, ether, 1, 1, 1);