]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_freebsd.c
This commit was generated by cvs2svn to compensate for changes in r172314,
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_freebsd.c
1 /*-
2  * Copyright (c) 2003-2007 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
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.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 /*
30  * IEEE 802.11 support (FreeBSD-specific code)
31  */
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h> 
35 #include <sys/linker.h>
36 #include <sys/mbuf.h>   
37 #include <sys/module.h>
38 #include <sys/proc.h>
39 #include <sys/sysctl.h>
40
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
46 #include <net/route.h>
47
48 #include <net80211/ieee80211_var.h>
49
50 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
51
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");
56 #endif
57
58 static int
59 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
60 {
61         int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
62         int error;
63
64         error = sysctl_handle_int(oidp, &inact, 0, req);
65         if (error || !req->newptr)
66                 return error;
67         *(int *)arg1 = inact / IEEE80211_INACT_WAIT;
68         return 0;
69 }
70
71 static int
72 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
73 {
74         struct ieee80211com *ic = arg1;
75         const char *name = ic->ic_ifp->if_xname;
76
77         return SYSCTL_OUT(req, name, strlen(name));
78 }
79
80 void
81 ieee80211_sysctl_attach(struct ieee80211com *ic)
82 {
83         struct sysctl_ctx_list *ctx;
84         struct sysctl_oid *oid;
85         char num[14];                   /* sufficient for 32 bits */
86
87         MALLOC(ctx, struct sysctl_ctx_list *, sizeof(struct sysctl_ctx_list),
88                 M_DEVBUF, M_NOWAIT | M_ZERO);
89         if (ctx == NULL) {
90                 if_printf(ic->ic_ifp, "%s: cannot allocate sysctl context!\n",
91                         __func__);
92                 return;
93         }
94         sysctl_ctx_init(ctx);
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",
100                 "parent device");
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");
106 #endif
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");
130         ic->ic_sysctl = ctx;
131 }
132
133 void
134 ieee80211_sysctl_detach(struct ieee80211com *ic)
135 {
136
137         if (ic->ic_sysctl != NULL) {
138                 sysctl_ctx_free(ic->ic_sysctl);
139                 FREE(ic->ic_sysctl, M_DEVBUF);
140                 ic->ic_sysctl = NULL;
141         }
142 }
143
144 int
145 ieee80211_node_dectestref(struct ieee80211_node *ni)
146 {
147         /* XXX need equivalent of atomic_dec_and_test */
148         atomic_subtract_int(&ni->ni_refcnt, 1);
149         return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
150 }
151
152 void
153 ieee80211_drain_ifq(struct ifqueue *ifq)
154 {
155         struct ieee80211_node *ni;
156         struct mbuf *m;
157
158         for (;;) {
159                 IF_DEQUEUE(ifq, m);
160                 if (m == NULL)
161                         break;
162
163                 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
164                 KASSERT(ni != NULL, ("frame w/o node"));
165                 ieee80211_free_node(ni);
166                 m->m_pkthdr.rcvif = NULL;
167
168                 m_freem(m);
169         }
170 }
171
172 /*
173  * As above, for mbufs allocated with m_gethdr/MGETHDR
174  * or initialized by M_COPY_PKTHDR.
175  */
176 #define MC_ALIGN(m, len)                                                \
177 do {                                                                    \
178         (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);        \
179 } while (/* CONSTCOND */ 0)
180
181 /*
182  * Allocate and setup a management frame of the specified
183  * size.  We return the mbuf and a pointer to the start
184  * of the contiguous data area that's been reserved based
185  * on the packet length.  The data area is forced to 32-bit
186  * alignment and the buffer length to a multiple of 4 bytes.
187  * This is done mainly so beacon frames (that require this)
188  * can use this interface too.
189  */
190 struct mbuf *
191 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
192 {
193         struct mbuf *m;
194         u_int len;
195
196         /*
197          * NB: we know the mbuf routines will align the data area
198          *     so we don't need to do anything special.
199          */
200         len = roundup2(headroom + pktlen, 4);
201         KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
202         if (len < MINCLSIZE) {
203                 m = m_gethdr(M_NOWAIT, MT_DATA);
204                 /*
205                  * Align the data in case additional headers are added.
206                  * This should only happen when a WEP header is added
207                  * which only happens for shared key authentication mgt
208                  * frames which all fit in MHLEN.
209                  */
210                 if (m != NULL)
211                         MH_ALIGN(m, len);
212         } else {
213                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
214                 if (m != NULL)
215                         MC_ALIGN(m, len);
216         }
217         if (m != NULL) {
218                 m->m_data += headroom;
219                 *frm = m->m_data;
220         }
221         return m;
222 }
223
224 int
225 ieee80211_add_callback(struct mbuf *m,
226         void (*func)(struct ieee80211_node *, void *, int), void *arg)
227 {
228         struct m_tag *mtag;
229         struct ieee80211_cb *cb;
230
231         mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
232                         sizeof(struct ieee80211_cb), M_NOWAIT);
233         if (mtag == NULL)
234                 return 0;
235
236         cb = (struct ieee80211_cb *)(mtag+1);
237         cb->func = func;
238         cb->arg = arg;
239         m_tag_prepend(m, mtag);
240         m->m_flags |= M_TXCB;
241         return 1;
242 }
243
244 void
245 ieee80211_process_callback(struct ieee80211_node *ni,
246         struct mbuf *m, int status)
247 {
248         struct m_tag *mtag;
249
250         mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
251         if (mtag != NULL) {
252                 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
253                 cb->func(ni, cb->arg, status);
254         }
255 }
256
257 #include <sys/libkern.h>
258
259 void
260 get_random_bytes(void *p, size_t n)
261 {
262         uint8_t *dp = p;
263
264         while (n > 0) {
265                 uint32_t v = arc4random();
266                 size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
267                 bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
268                 dp += sizeof(uint32_t), n -= nb;
269         }
270 }
271
272 void
273 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
274 {
275         struct ifnet *ifp = ic->ic_ifp;
276         struct ieee80211_join_event iev;
277
278         memset(&iev, 0, sizeof(iev));
279         if (ni == ic->ic_bss) {
280                 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
281                 rt_ieee80211msg(ifp, newassoc ?
282                         RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
283                         &iev, sizeof(iev));
284                 if_link_state_change(ifp, LINK_STATE_UP);
285         } else {
286                 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
287                 rt_ieee80211msg(ifp, newassoc ?
288                         RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
289                         &iev, sizeof(iev));
290         }
291 }
292
293 void
294 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
295 {
296         struct ifnet *ifp = ic->ic_ifp;
297         struct ieee80211_leave_event iev;
298
299         if (ni == ic->ic_bss) {
300                 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
301                 if_link_state_change(ifp, LINK_STATE_DOWN);
302         } else {
303                 /* fire off wireless event station leaving */
304                 memset(&iev, 0, sizeof(iev));
305                 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
306                 rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
307         }
308 }
309
310 void
311 ieee80211_notify_scan_done(struct ieee80211com *ic)
312 {
313         struct ifnet *ifp = ic->ic_ifp;
314
315         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
316
317         /* dispatch wireless event indicating scan completed */
318         rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
319 }
320
321 void
322 ieee80211_notify_replay_failure(struct ieee80211com *ic,
323         const struct ieee80211_frame *wh, const struct ieee80211_key *k,
324         u_int64_t rsc)
325 {
326         struct ifnet *ifp = ic->ic_ifp;
327
328         IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
329             "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
330             ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
331             (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
332             k->wk_keyix, k->wk_rxkeyix);
333
334         if (ifp != NULL) {              /* NB: for cipher test modules */
335                 struct ieee80211_replay_event iev;
336
337                 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
338                 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
339                 iev.iev_cipher = k->wk_cipher->ic_cipher;
340                 if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
341                         iev.iev_keyix = k->wk_rxkeyix;
342                 else
343                         iev.iev_keyix = k->wk_keyix;
344                 iev.iev_keyrsc = k->wk_keyrsc;
345                 iev.iev_rsc = rsc;
346                 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
347         }
348 }
349
350 void
351 ieee80211_notify_michael_failure(struct ieee80211com *ic,
352         const struct ieee80211_frame *wh, u_int keyix)
353 {
354         struct ifnet *ifp = ic->ic_ifp;
355
356         IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
357                 "[%s] michael MIC verification failed <keyix %u>\n",
358                ether_sprintf(wh->i_addr2), keyix);
359         ic->ic_stats.is_rx_tkipmic++;
360
361         if (ifp != NULL) {              /* NB: for cipher test modules */
362                 struct ieee80211_michael_event iev;
363
364                 IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
365                 IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
366                 iev.iev_cipher = IEEE80211_CIPHER_TKIP;
367                 iev.iev_keyix = keyix;
368                 rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
369         }
370 }
371
372 void
373 ieee80211_load_module(const char *modname)
374 {
375
376 #ifdef notyet
377         (void)kern_kldload(curthread, modname, NULL);
378 #else
379         printf("%s: load the %s module by hand for now.\n", __func__, modname);
380 #endif
381 }
382
383 /*
384  * Module glue.
385  *
386  * NB: the module name is "wlan" for compatibility with NetBSD.
387  */
388 static int
389 wlan_modevent(module_t mod, int type, void *unused)
390 {
391         switch (type) {
392         case MOD_LOAD:
393                 if (bootverbose)
394                         printf("wlan: <802.11 Link Layer>\n");
395                 return 0;
396         case MOD_UNLOAD:
397                 return 0;
398         }
399         return EINVAL;
400 }
401
402 static moduledata_t wlan_mod = {
403         "wlan",
404         wlan_modevent,
405         0
406 };
407 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
408 MODULE_VERSION(wlan, 1);
409 MODULE_DEPEND(wlan, ether, 1, 1, 1);