]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_freebsd.c
This commit was generated by cvs2svn to compensate for changes in r171537,
[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                 ic->ic_sysctl = NULL;
140         }
141 }
142
143 int
144 ieee80211_node_dectestref(struct ieee80211_node *ni)
145 {
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);
149 }
150
151 void
152 ieee80211_drain_ifq(struct ifqueue *ifq)
153 {
154         struct ieee80211_node *ni;
155         struct mbuf *m;
156
157         for (;;) {
158                 IF_DEQUEUE(ifq, m);
159                 if (m == NULL)
160                         break;
161
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;
166
167                 m_freem(m);
168         }
169 }
170
171 /*
172  * As above, for mbufs allocated with m_gethdr/MGETHDR
173  * or initialized by M_COPY_PKTHDR.
174  */
175 #define MC_ALIGN(m, len)                                                \
176 do {                                                                    \
177         (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);        \
178 } while (/* CONSTCOND */ 0)
179
180 /*
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.
188  */
189 struct mbuf *
190 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
191 {
192         struct mbuf *m;
193         u_int len;
194
195         /*
196          * NB: we know the mbuf routines will align the data area
197          *     so we don't need to do anything special.
198          */
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);
203                 /*
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.
208                  */
209                 if (m != NULL)
210                         MH_ALIGN(m, len);
211         } else {
212                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
213                 if (m != NULL)
214                         MC_ALIGN(m, len);
215         }
216         if (m != NULL) {
217                 m->m_data += sizeof(struct ieee80211_frame);
218                 *frm = m->m_data;
219         }
220         return m;
221 }
222
223 int
224 ieee80211_add_callback(struct mbuf *m,
225         void (*func)(struct ieee80211_node *, void *, int), void *arg)
226 {
227         struct m_tag *mtag;
228         struct ieee80211_cb *cb;
229
230         mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
231                         sizeof(struct ieee80211_cb), M_NOWAIT);
232         if (mtag == NULL)
233                 return 0;
234
235         cb = (struct ieee80211_cb *)(mtag+1);
236         cb->func = func;
237         cb->arg = arg;
238         m_tag_prepend(m, mtag);
239         m->m_flags |= M_TXCB;
240         return 1;
241 }
242
243 void
244 ieee80211_process_callback(struct ieee80211_node *ni,
245         struct mbuf *m, int status)
246 {
247         struct m_tag *mtag;
248
249         mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
250         if (mtag != NULL) {
251                 struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
252                 cb->func(ni, cb->arg, status);
253         }
254 }
255
256 #include <sys/libkern.h>
257
258 void
259 get_random_bytes(void *p, size_t n)
260 {
261         uint8_t *dp = p;
262
263         while (n > 0) {
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;
268         }
269 }
270
271 void
272 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
273 {
274         struct ifnet *ifp = ic->ic_ifp;
275         struct ieee80211_join_event iev;
276
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,
282                         &iev, sizeof(iev));
283                 if_link_state_change(ifp, LINK_STATE_UP);
284         } else {
285                 IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
286                 rt_ieee80211msg(ifp, newassoc ?
287                         RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
288                         &iev, sizeof(iev));
289         }
290 }
291
292 void
293 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
294 {
295         struct ifnet *ifp = ic->ic_ifp;
296         struct ieee80211_leave_event iev;
297
298         if (ni == ic->ic_bss) {
299                 rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
300                 if_link_state_change(ifp, LINK_STATE_DOWN);
301         } else {
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));
306         }
307 }
308
309 void
310 ieee80211_notify_scan_done(struct ieee80211com *ic)
311 {
312         struct ifnet *ifp = ic->ic_ifp;
313
314         IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
315
316         /* dispatch wireless event indicating scan completed */
317         rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
318 }
319
320 void
321 ieee80211_notify_replay_failure(struct ieee80211com *ic,
322         const struct ieee80211_frame *wh, const struct ieee80211_key *k,
323         u_int64_t rsc)
324 {
325         struct ifnet *ifp = ic->ic_ifp;
326
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);
332
333         if (ifp != NULL) {              /* NB: for cipher test modules */
334                 struct ieee80211_replay_event iev;
335
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;
341                 else
342                         iev.iev_keyix = k->wk_keyix;
343                 iev.iev_keyrsc = k->wk_keyrsc;
344                 iev.iev_rsc = rsc;
345                 rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
346         }
347 }
348
349 void
350 ieee80211_notify_michael_failure(struct ieee80211com *ic,
351         const struct ieee80211_frame *wh, u_int keyix)
352 {
353         struct ifnet *ifp = ic->ic_ifp;
354
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++;
359
360         if (ifp != NULL) {              /* NB: for cipher test modules */
361                 struct ieee80211_michael_event iev;
362
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));
368         }
369 }
370
371 void
372 ieee80211_load_module(const char *modname)
373 {
374
375 #ifdef notyet
376         (void)kern_kldload(curthread, modname, NULL);
377 #else
378         printf("%s: load the %s module by hand for now.\n", __func__, modname);
379 #endif
380 }
381
382 /*
383  * Module glue.
384  *
385  * NB: the module name is "wlan" for compatibility with NetBSD.
386  */
387 static int
388 wlan_modevent(module_t mod, int type, void *unused)
389 {
390         switch (type) {
391         case MOD_LOAD:
392                 if (bootverbose)
393                         printf("wlan: <802.11 Link Layer>\n");
394                 return 0;
395         case MOD_UNLOAD:
396                 return 0;
397         }
398         return EINVAL;
399 }
400
401 static moduledata_t wlan_mod = {
402         "wlan",
403         wlan_modevent,
404         0
405 };
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);