1 /* $NetBSD: awi_wep.c,v 1.4 2000/08/14 11:28:03 onoe Exp $ */
2 /* $FreeBSD: src/sys/dev/awi/awi_wep.c,v 1.3.2.2 2003/01/23 21:06:42 sam Exp $ */
3 /* $DragonFly: src/sys/dev/netif/awi/Attic/awi_wep.c,v 1.8 2004/04/07 05:45:26 dillon Exp $ */
6 * Copyright (c) 2000 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * WEP support framework for the awi driver.
44 * No actual encryption capability is provided here, but any can be added
45 * to awi_wep_algo table below.
47 * Note that IEEE802.11 specification states WEP uses RC4 with 40bit key,
48 * which is a proprietary encryption algorithm available under license
49 * from RSA Data Security Inc. Using another algorithm, includes null
50 * encryption provided here, the awi driver cannot be able to communicate
51 * with other stations.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/socket.h>
61 #include <sys/errno.h>
62 #include <sys/sockio.h>
63 #if defined(__DragonFly__) || (defined(__FreeBSD__) && __FreeBSD__ >= 4)
66 #include <sys/device.h>
70 #include <net/if_dl.h>
71 #if defined(__DragonFly__) || defined(__FreeBSD__)
72 #include <net/ethernet.h>
73 #include <net/if_arp.h>
75 #include <net/if_ether.h>
77 #include <net/if_media.h>
78 #include <net/if_ieee80211.h>
80 #include <machine/cpu.h>
81 #include <machine/bus.h>
82 #if defined(__DragonFly__) || defined(__FreeBSD__)
83 #include <machine/clock.h>
87 #include <dev/ic/am79c930reg.h>
88 #include <dev/ic/am79c930var.h>
89 #include <dev/ic/awireg.h>
90 #include <dev/ic/awivar.h>
92 #include <crypto/arc4/arc4.h>
95 #if defined(__DragonFly__) || defined(__FreeBSD__)
96 #include "am79c930reg.h"
97 #include "am79c930var.h"
101 #include <crypto/rc4/rc4.h>
105 return sizeof(struct rc4_state);
109 arc4_setkey(void *ctx, u_int8_t *key, int keylen)
111 rc4_init(ctx, key, keylen);
115 arc4_encrypt(void *ctx, u_int8_t *dst, u_int8_t *src, int len)
117 rc4_crypt(ctx, src, dst, len);
121 static void awi_crc_init (void);
122 static u_int32_t awi_crc_update (u_int32_t crc, u_int8_t *buf, int len);
124 static int awi_null_ctxlen (void);
125 static void awi_null_setkey (void *ctx, u_int8_t *key, int keylen);
126 static void awi_null_copy (void *ctx, u_int8_t *dst, u_int8_t *src, int len);
128 /* XXX: the order should be known to wiconfig/user */
130 static struct awi_wep_algo awi_wep_algo[] = {
132 { "no" }, /* dummy for no wep */
134 /* 1: normal wep (arc4) */
135 { "arc4", arc4_ctxlen, arc4_setkey,
136 arc4_encrypt, arc4_encrypt },
138 /* 2: debug wep (null) */
139 { "null", awi_null_ctxlen, awi_null_setkey,
140 awi_null_copy, awi_null_copy },
141 /* dummy for wep without encryption */
145 awi_wep_setnwkey(sc, nwkey)
146 struct awi_softc *sc;
147 struct ieee80211_nwkey *nwkey;
150 u_int8_t keybuf[AWI_MAX_KEYLEN];
152 if (nwkey->i_defkid <= 0 ||
153 nwkey->i_defkid > IEEE80211_WEP_NKID)
156 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
157 if (nwkey->i_key[i].i_keydat == NULL)
159 len = nwkey->i_key[i].i_keylen;
160 if (len > sizeof(keybuf)) {
164 error = copyin(nwkey->i_key[i].i_keydat, keybuf, len);
167 error = awi_wep_setkey(sc, i, keybuf, len);
172 sc->sc_wep_defkid = nwkey->i_defkid - 1;
173 error = awi_wep_setalgo(sc, nwkey->i_wepon);
174 if (error == 0 && sc->sc_enabled) {
176 error = awi_init(sc);
183 awi_wep_getnwkey(sc, nwkey)
184 struct awi_softc *sc;
185 struct ieee80211_nwkey *nwkey;
187 int i, len, error, suerr;
188 u_int8_t keybuf[AWI_MAX_KEYLEN];
190 nwkey->i_wepon = awi_wep_getalgo(sc);
191 nwkey->i_defkid = sc->sc_wep_defkid + 1;
192 /* do not show any keys to non-root user */
193 #if defined(__DragonFly__) || defined(__FreeBSD__)
194 suerr = suser(curthread); /* note: EPERM if no proc */
196 suerr = suser(curproc->p_ucred, &curproc->p_acflag);
199 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
200 if (nwkey->i_key[i].i_keydat == NULL)
206 len = sizeof(keybuf);
207 error = awi_wep_getkey(sc, i, keybuf, &len);
210 if (nwkey->i_key[i].i_keylen < len) {
214 nwkey->i_key[i].i_keylen = len;
215 error = copyout(keybuf, nwkey->i_key[i].i_keydat, len);
224 struct awi_softc *sc;
227 if (sc->sc_wep_algo == NULL)
229 return sc->sc_wep_algo - awi_wep_algo;
233 awi_wep_setalgo(sc, algo)
234 struct awi_softc *sc;
237 struct awi_wep_algo *awa;
240 awi_crc_init(); /* XXX: not belongs here */
241 if (algo < 0 || algo > sizeof(awi_wep_algo)/sizeof(awi_wep_algo[0]))
243 awa = &awi_wep_algo[algo];
244 if (awa->awa_name == NULL)
246 if (awa->awa_ctxlen == NULL) {
250 ctxlen = awa->awa_ctxlen();
251 if (sc->sc_wep_ctx != NULL) {
252 free(sc->sc_wep_ctx, M_DEVBUF);
253 sc->sc_wep_ctx = NULL;
256 sc->sc_wep_ctx = malloc(ctxlen, M_DEVBUF, M_INTWAIT);
257 if (sc->sc_wep_ctx == NULL)
260 sc->sc_wep_algo = awa;
265 awi_wep_setkey(sc, kid, key, keylen)
266 struct awi_softc *sc;
272 if (kid < 0 || kid >= IEEE80211_WEP_NKID)
274 if (keylen < 0 || keylen + IEEE80211_WEP_IVLEN > AWI_MAX_KEYLEN)
276 sc->sc_wep_keylen[kid] = keylen;
278 memcpy(sc->sc_wep_key[kid] + IEEE80211_WEP_IVLEN, key, keylen);
283 awi_wep_getkey(sc, kid, key, keylen)
284 struct awi_softc *sc;
290 if (kid < 0 || kid >= IEEE80211_WEP_NKID)
292 if (*keylen < sc->sc_wep_keylen[kid])
294 *keylen = sc->sc_wep_keylen[kid];
296 memcpy(key, sc->sc_wep_key[kid] + IEEE80211_WEP_IVLEN, *keylen);
301 awi_wep_encrypt(sc, m0, txflag)
302 struct awi_softc *sc;
306 struct mbuf *m, *n, *n0;
307 struct ieee80211_frame *wh;
308 struct awi_wep_algo *awa;
309 int left, len, moff, noff, keylen, kid;
313 u_int8_t crcbuf[IEEE80211_WEP_CRCLEN];
316 awa = sc->sc_wep_algo;
319 ctx = sc->sc_wep_ctx;
321 left = m->m_pkthdr.len;
322 MGET(n, M_DONTWAIT, m->m_type);
327 len = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
329 n->m_pkthdr.len += len;
331 n->m_pkthdr.len -= len;
335 if (n->m_pkthdr.len >= MINCLSIZE) {
336 MCLGET(n, M_DONTWAIT);
337 if (n->m_flags & M_EXT)
338 n->m_len = n->m_ext.ext_size;
340 len = sizeof(struct ieee80211_frame);
341 memcpy(mtod(n, caddr_t), mtod(m, caddr_t), len);
346 kid = sc->sc_wep_defkid;
347 wh = mtod(n, struct ieee80211_frame *);
348 wh->i_fc[1] |= IEEE80211_FC1_WEP;
351 * store IV, byte order is not the matter since it's random.
352 * assuming IEEE80211_WEP_IVLEN is 3
354 ivp = mtod(n, u_int8_t *) + noff;
355 ivp[0] = (iv >> 16) & 0xff;
356 ivp[1] = (iv >> 8) & 0xff;
358 ivp[3] = kid & 0x03; /* clear pad and keyid */
359 noff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
361 ivp = mtod(m, u_int8_t *) + moff;
362 moff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
363 kid = ivp[IEEE80211_WEP_IVLEN] & 0x03;
365 key = sc->sc_wep_key[kid];
366 keylen = sc->sc_wep_keylen[kid];
367 /* assuming IEEE80211_WEP_IVLEN is 3 */
371 awa->awa_setkey(ctx, key, IEEE80211_WEP_IVLEN + keylen);
373 /* encrypt with calculating CRC */
376 len = m->m_len - moff;
382 if (len > n->m_len - noff) {
383 len = n->m_len - noff;
385 MGET(n->m_next, M_DONTWAIT, n->m_type);
386 if (n->m_next == NULL)
390 if (left >= MINCLSIZE) {
391 MCLGET(n, M_DONTWAIT);
392 if (n->m_flags & M_EXT)
393 n->m_len = n->m_ext.ext_size;
402 awa->awa_encrypt(ctx, mtod(n, caddr_t) + noff,
403 mtod(m, caddr_t) + moff, len);
404 crc = awi_crc_update(crc, mtod(m, caddr_t) + moff, len);
406 awa->awa_decrypt(ctx, mtod(n, caddr_t) + noff,
407 mtod(m, caddr_t) + moff, len);
408 crc = awi_crc_update(crc, mtod(n, caddr_t) + noff, len);
416 LE_WRITE_4(crcbuf, crc);
417 if (n->m_len >= noff + sizeof(crcbuf))
418 n->m_len = noff + sizeof(crcbuf);
421 MGET(n->m_next, M_DONTWAIT, n->m_type);
422 if (n->m_next == NULL)
425 n->m_len = sizeof(crcbuf);
428 awa->awa_encrypt(ctx, mtod(n, caddr_t) + noff, crcbuf,
432 for (noff = 0; noff < sizeof(crcbuf); noff += len) {
433 len = sizeof(crcbuf) - noff;
434 if (len > m->m_len - moff)
435 len = m->m_len - moff;
437 awa->awa_decrypt(ctx, crcbuf + noff,
438 mtod(m, caddr_t) + moff, len);
442 if (crc != LE_READ_4(crcbuf))
455 * CRC 32 -- routine from RFC 2083
458 /* Table of CRCs of all 8-bit messages */
459 static u_int32_t awi_crc_table[256];
460 static int awi_crc_table_computed = 0;
462 /* Make the table for a fast CRC. */
469 if (awi_crc_table_computed)
471 for (n = 0; n < 256; n++) {
473 for (k = 0; k < 8; k++) {
475 c = 0xedb88320UL ^ (c >> 1);
479 awi_crc_table[n] = c;
481 awi_crc_table_computed = 1;
485 * Update a running CRC with the bytes buf[0..len-1]--the CRC
486 * should be initialized to all 1's, and the transmitted value
487 * is the 1's complement of the final running CRC
491 awi_crc_update(crc, buf, len)
498 for (endbuf = buf + len; buf < endbuf; buf++)
499 crc = awi_crc_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
504 * Null -- do nothing but copy.
515 awi_null_setkey(ctx, key, keylen)
523 awi_null_copy(ctx, dst, src, len)
530 memcpy(dst, src, len);