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");
57 extern int ieee80211_recv_bar_ena;
58 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
59 0, "BAR frame processing (ena/dis)");
61 #ifdef IEEE80211_AMPDU_AGE
63 ieee80211_sysctl_ampdu_age(SYSCTL_HANDLER_ARGS)
65 extern int ieee80211_ampdu_age;
66 int ampdu_age = ticks_to_msecs(ieee80211_ampdu_age);
69 error = sysctl_handle_int(oidp, &du_age, 0, req);
70 if (error || !req->newptr)
72 ieee80211_ampdu_age = msecs_to_ticks(ampdu_age);
75 SYSCTL_PROC(_net_wlan, OID_AUTO, "ampdu_age", CTLFLAG_RW, NULL, 0,
76 ieee80211_sysctl_ampdu_age, "A", "AMPDU max reorder age (ms)");
80 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
82 int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
85 error = sysctl_handle_int(oidp, &inact, 0, req);
86 if (error || !req->newptr)
88 *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
93 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
95 struct ieee80211com *ic = arg1;
96 const char *name = ic->ic_ifp->if_xname;
98 return SYSCTL_OUT(req, name, strlen(name));
102 ieee80211_sysctl_attach(struct ieee80211com *ic)
104 struct sysctl_ctx_list *ctx;
105 struct sysctl_oid *oid;
106 char num[14]; /* sufficient for 32 bits */
108 MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
109 M_DEVBUF, M_NOWAIT | M_ZERO);
111 if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
115 sysctl_ctx_init(ctx);
116 snprintf(num, sizeof(num), "%u", ic->ic_vap);
117 oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
118 OID_AUTO, num, CTLFLAG_RD, NULL, "");
119 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
120 "%parent", CTLFLAG_RD, ic, 0, ieee80211_sysctl_parent, "A",
122 #ifdef IEEE80211_DEBUG
123 ic->ic_debug = ieee80211_debug;
124 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
125 "debug", CTLFLAG_RW, &ic->ic_debug, 0,
126 "control debugging printfs");
128 /* XXX inherit from tunables */
129 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
130 "inact_run", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_run, 0,
131 ieee80211_sysctl_inact, "I",
132 "station inactivity timeout (sec)");
133 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
134 "inact_probe", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_probe, 0,
135 ieee80211_sysctl_inact, "I",
136 "station inactivity probe timeout (sec)");
137 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
138 "inact_auth", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_auth, 0,
139 ieee80211_sysctl_inact, "I",
140 "station authentication timeout (sec)");
141 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
142 "inact_init", CTLTYPE_INT | CTLFLAG_RW, &ic->ic_inact_init, 0,
143 ieee80211_sysctl_inact, "I",
144 "station initial state timeout (sec)");
145 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
146 "driver_caps", CTLFLAG_RW, &ic->ic_caps, 0,
147 "driver capabilities");
148 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
149 "bmiss_max", CTLFLAG_RW, &ic->ic_bmiss_max, 0,
150 "consecutive beacon misses before scanning");
155 ieee80211_sysctl_detach(struct ieee80211com *ic)
158 if (ic->ic_sysctl != NULL) {
159 sysctl_ctx_free(ic->ic_sysctl);
160 FREE(ic->ic_sysctl, M_DEVBUF);
161 ic->ic_sysctl = NULL;
166 ieee80211_node_dectestref(struct ieee80211_node *ni)
168 /* XXX need equivalent of atomic_dec_and_test */
169 atomic_subtract_int(&ni->ni_refcnt, 1);
170 return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
174 ieee80211_drain_ifq(struct ifqueue *ifq)
176 struct ieee80211_node *ni;
184 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
185 KASSERT(ni != NULL, ("frame w/o node"));
186 ieee80211_free_node(ni);
187 m->m_pkthdr.rcvif = NULL;
194 * As above, for mbufs allocated with m_gethdr/MGETHDR
195 * or initialized by M_COPY_PKTHDR.
197 #define MC_ALIGN(m, len) \
199 (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1); \
200 } while (/* CONSTCOND */ 0)
203 * Allocate and setup a management frame of the specified
204 * size. We return the mbuf and a pointer to the start
205 * of the contiguous data area that's been reserved based
206 * on the packet length. The data area is forced to 32-bit
207 * alignment and the buffer length to a multiple of 4 bytes.
208 * This is done mainly so beacon frames (that require this)
209 * can use this interface too.
212 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
218 * NB: we know the mbuf routines will align the data area
219 * so we don't need to do anything special.
221 len = roundup2(headroom + pktlen, 4);
222 KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
223 if (len < MINCLSIZE) {
224 m = m_gethdr(M_NOWAIT, MT_DATA);
226 * Align the data in case additional headers are added.
227 * This should only happen when a WEP header is added
228 * which only happens for shared key authentication mgt
229 * frames which all fit in MHLEN.
234 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
239 m->m_data += headroom;
246 ieee80211_add_callback(struct mbuf *m,
247 void (*func)(struct ieee80211_node *, void *, int), void *arg)
250 struct ieee80211_cb *cb;
252 mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
253 sizeof(struct ieee80211_cb), M_NOWAIT);
257 cb = (struct ieee80211_cb *)(mtag+1);
260 m_tag_prepend(m, mtag);
261 m->m_flags |= M_TXCB;
266 ieee80211_process_callback(struct ieee80211_node *ni,
267 struct mbuf *m, int status)
271 mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
273 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
274 cb->func(ni, cb->arg, status);
278 #include <sys/libkern.h>
281 get_random_bytes(void *p, size_t n)
286 uint32_t v = arc4random();
287 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
288 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
289 dp += sizeof(uint32_t), n -= nb;
294 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
296 struct ifnet *ifp = ic->ic_ifp;
297 struct ieee80211_join_event iev;
299 memset(&iev, 0, sizeof(iev));
300 if (ni == ic->ic_bss) {
301 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
302 rt_ieee80211msg(ifp, newassoc ?
303 RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
305 if_link_state_change(ifp, LINK_STATE_UP);
307 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
308 rt_ieee80211msg(ifp, newassoc ?
309 RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
315 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
317 struct ifnet *ifp = ic->ic_ifp;
318 struct ieee80211_leave_event iev;
320 if (ni == ic->ic_bss) {
321 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
322 if_link_state_change(ifp, LINK_STATE_DOWN);
324 /* fire off wireless event station leaving */
325 memset(&iev, 0, sizeof(iev));
326 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
327 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
332 ieee80211_notify_scan_done(struct ieee80211com *ic)
334 struct ifnet *ifp = ic->ic_ifp;
336 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
338 /* dispatch wireless event indicating scan completed */
339 rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
343 ieee80211_notify_replay_failure(struct ieee80211com *ic,
344 const struct ieee80211_frame *wh, const struct ieee80211_key *k,
347 struct ifnet *ifp = ic->ic_ifp;
349 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
350 "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
351 ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
352 (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
353 k->wk_keyix, k->wk_rxkeyix);
355 if (ifp != NULL) { /* NB: for cipher test modules */
356 struct ieee80211_replay_event iev;
358 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
359 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
360 iev.iev_cipher = k->wk_cipher->ic_cipher;
361 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
362 iev.iev_keyix = k->wk_rxkeyix;
364 iev.iev_keyix = k->wk_keyix;
365 iev.iev_keyrsc = k->wk_keyrsc;
367 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
372 ieee80211_notify_michael_failure(struct ieee80211com *ic,
373 const struct ieee80211_frame *wh, u_int keyix)
375 struct ifnet *ifp = ic->ic_ifp;
377 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
378 "[%s] michael MIC verification failed <keyix %u>\n",
379 ether_sprintf(wh->i_addr2), keyix);
380 ic->ic_stats.is_rx_tkipmic++;
382 if (ifp != NULL) { /* NB: for cipher test modules */
383 struct ieee80211_michael_event iev;
385 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
386 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
387 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
388 iev.iev_keyix = keyix;
389 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
394 ieee80211_load_module(const char *modname)
398 (void)kern_kldload(curthread, modname, NULL);
400 printf("%s: load the %s module by hand for now.\n", __func__, modname);
407 * NB: the module name is "wlan" for compatibility with NetBSD.
410 wlan_modevent(module_t mod, int type, void *unused)
415 printf("wlan: <802.11 Link Layer>\n");
423 static moduledata_t wlan_mod = {
428 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
429 MODULE_VERSION(wlan, 1);
430 MODULE_DEPEND(wlan, ether, 1, 1, 1);