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.16 2006/09/05 00:55:39 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>
66 #include <net/if_dl.h>
67 #include <net/ethernet.h>
68 #include <net/if_arp.h>
69 #include <net/if_media.h>
70 #include <netproto/802_11/ieee80211.h>
71 #include <netproto/802_11/ieee80211_ioctl.h>
73 #include <machine/cpu.h>
74 #include <machine/bus.h>
76 #include <dev/netif/awi/am79c930reg.h>
77 #include <dev/netif/awi/am79c930var.h>
78 #include <dev/netif/awi/awireg.h>
79 #include <dev/netif/awi/awivar.h>
81 #include <crypto/rc4/rc4.h>
85 return sizeof(struct rc4_state);
89 arc4_setkey(void *ctx, u_int8_t *key, int keylen)
91 rc4_init(ctx, key, keylen);
95 arc4_encrypt(void *ctx, u_int8_t *dst, u_int8_t *src, int len)
97 rc4_crypt(ctx, src, dst, len);
100 static void awi_crc_init (void);
101 static u_int32_t awi_crc_update (u_int32_t crc, u_int8_t *buf, int len);
103 static int awi_null_ctxlen (void);
104 static void awi_null_setkey (void *ctx, u_int8_t *key, int keylen);
105 static void awi_null_copy (void *ctx, u_int8_t *dst, u_int8_t *src, int len);
107 /* XXX: the order should be known to wiconfig/user */
109 static struct awi_wep_algo awi_wep_algo[] = {
111 { "no" }, /* dummy for no wep */
113 /* 1: normal wep (arc4) */
114 { "arc4", arc4_ctxlen, arc4_setkey,
115 arc4_encrypt, arc4_encrypt },
117 /* 2: debug wep (null) */
118 { "null", awi_null_ctxlen, awi_null_setkey,
119 awi_null_copy, awi_null_copy },
120 /* dummy for wep without encryption */
124 awi_wep_setnwkey(struct awi_softc *sc, struct ieee80211_nwkey *nwkey)
127 u_int8_t keybuf[AWI_MAX_KEYLEN];
129 if (nwkey->i_defkid <= 0 ||
130 nwkey->i_defkid > IEEE80211_WEP_NKID)
133 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
134 if (nwkey->i_key[i].i_keydat == NULL)
136 len = nwkey->i_key[i].i_keylen;
137 if (len > sizeof(keybuf)) {
141 error = copyin(nwkey->i_key[i].i_keydat, keybuf, len);
144 error = awi_wep_setkey(sc, i, keybuf, len);
149 sc->sc_wep_defkid = nwkey->i_defkid - 1;
150 error = awi_wep_setalgo(sc, nwkey->i_wepon);
151 if (error == 0 && sc->sc_enabled) {
153 error = awi_init(sc);
160 awi_wep_getnwkey(struct awi_softc *sc, struct ieee80211_nwkey *nwkey)
162 int i, len, error, suerr;
163 u_int8_t keybuf[AWI_MAX_KEYLEN];
165 nwkey->i_wepon = awi_wep_getalgo(sc);
166 nwkey->i_defkid = sc->sc_wep_defkid + 1;
167 /* do not show any keys to non-root user */
168 suerr = suser(curthread); /* note: EPERM if no proc */
170 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
171 if (nwkey->i_key[i].i_keydat == NULL)
177 len = sizeof(keybuf);
178 error = awi_wep_getkey(sc, i, keybuf, &len);
181 if (nwkey->i_key[i].i_keylen < len) {
185 nwkey->i_key[i].i_keylen = len;
186 error = copyout(keybuf, nwkey->i_key[i].i_keydat, len);
194 awi_wep_getalgo(struct awi_softc *sc)
197 if (sc->sc_wep_algo == NULL)
199 return sc->sc_wep_algo - awi_wep_algo;
203 awi_wep_setalgo(struct awi_softc *sc, int algo)
205 struct awi_wep_algo *awa;
208 awi_crc_init(); /* XXX: not belongs here */
209 if (algo < 0 || algo > sizeof(awi_wep_algo)/sizeof(awi_wep_algo[0]))
211 awa = &awi_wep_algo[algo];
212 if (awa->awa_name == NULL)
214 if (awa->awa_ctxlen == NULL) {
218 ctxlen = awa->awa_ctxlen();
219 if (sc->sc_wep_ctx != NULL) {
220 kfree(sc->sc_wep_ctx, M_DEVBUF);
221 sc->sc_wep_ctx = NULL;
224 sc->sc_wep_ctx = kmalloc(ctxlen, M_DEVBUF, M_INTWAIT);
225 if (sc->sc_wep_ctx == NULL)
228 sc->sc_wep_algo = awa;
233 awi_wep_setkey(struct awi_softc *sc, int kid, unsigned char *key, int keylen)
236 if (kid < 0 || kid >= IEEE80211_WEP_NKID)
238 if (keylen < 0 || keylen + IEEE80211_WEP_IVLEN > AWI_MAX_KEYLEN)
240 sc->sc_wep_keylen[kid] = keylen;
242 memcpy(sc->sc_wep_key[kid] + IEEE80211_WEP_IVLEN, key, keylen);
247 awi_wep_getkey(struct awi_softc *sc, int kid, unsigned char *key, int *keylen)
250 if (kid < 0 || kid >= IEEE80211_WEP_NKID)
252 if (*keylen < sc->sc_wep_keylen[kid])
254 *keylen = sc->sc_wep_keylen[kid];
256 memcpy(key, sc->sc_wep_key[kid] + IEEE80211_WEP_IVLEN, *keylen);
261 awi_wep_encrypt(struct awi_softc *sc, struct mbuf *m0, int txflag)
263 struct mbuf *m, *n, *n0;
264 struct ieee80211_frame *wh;
265 struct awi_wep_algo *awa;
266 int left, len, plen, msize, moff, noff, keylen, kid;
270 u_int8_t crcbuf[IEEE80211_WEP_CRCLEN];
273 awa = sc->sc_wep_algo;
276 ctx = sc->sc_wep_ctx;
278 left = m->m_pkthdr.len;
280 len = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
282 plen = m->m_pkthdr.len + len;
284 plen = m->m_pkthdr.len - len;
287 n0 = n = m_getl(plen, MB_DONTWAIT, m->m_type, M_PKTHDR, &msize);
291 n->m_pkthdr.len = plen;
293 len = sizeof(struct ieee80211_frame);
295 memcpy(mtod(n, caddr_t), mtod(m, caddr_t), len);
300 kid = sc->sc_wep_defkid;
301 wh = mtod(n, struct ieee80211_frame *);
302 wh->i_fc[1] |= IEEE80211_FC1_WEP;
305 * store IV, byte order is not the matter since it's random.
306 * assuming IEEE80211_WEP_IVLEN is 3
308 ivp = mtod(n, u_int8_t *) + noff;
309 ivp[0] = (iv >> 16) & 0xff;
310 ivp[1] = (iv >> 8) & 0xff;
312 ivp[3] = kid & 0x03; /* clear pad and keyid */
313 noff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
315 ivp = mtod(m, u_int8_t *) + moff;
316 moff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
317 kid = ivp[IEEE80211_WEP_IVLEN] & 0x03;
319 key = sc->sc_wep_key[kid];
320 keylen = sc->sc_wep_keylen[kid];
321 /* assuming IEEE80211_WEP_IVLEN is 3 */
325 awa->awa_setkey(ctx, key, IEEE80211_WEP_IVLEN + keylen);
327 /* encrypt with calculating CRC */
330 len = m->m_len - moff;
336 if (len > n->m_len - noff) {
337 len = n->m_len - noff;
339 n->m_next = m_getl(left, MB_DONTWAIT,
340 n->m_type, 0, &msize);
341 if (n->m_next == NULL)
343 n->m_next->m_len = msize;
352 awa->awa_encrypt(ctx, mtod(n, caddr_t) + noff,
353 mtod(m, caddr_t) + moff, len);
354 crc = awi_crc_update(crc, mtod(m, caddr_t) + moff, len);
356 awa->awa_decrypt(ctx, mtod(n, caddr_t) + noff,
357 mtod(m, caddr_t) + moff, len);
358 crc = awi_crc_update(crc, mtod(n, caddr_t) + noff, len);
366 LE_WRITE_4(crcbuf, crc);
367 if (n->m_len >= noff + sizeof(crcbuf))
368 n->m_len = noff + sizeof(crcbuf);
371 MGET(n->m_next, MB_DONTWAIT, n->m_type);
372 if (n->m_next == NULL)
375 n->m_len = sizeof(crcbuf);
378 awa->awa_encrypt(ctx, mtod(n, caddr_t) + noff, crcbuf,
382 for (noff = 0; noff < sizeof(crcbuf); noff += len) {
383 len = sizeof(crcbuf) - noff;
384 if (len > m->m_len - moff)
385 len = m->m_len - moff;
387 awa->awa_decrypt(ctx, crcbuf + noff,
388 mtod(m, caddr_t) + moff, len);
392 if (crc != LE_READ_4(crcbuf))
405 * CRC 32 -- routine from RFC 2083
408 /* Table of CRCs of all 8-bit messages */
409 static u_int32_t awi_crc_table[256];
410 static int awi_crc_table_computed = 0;
412 /* Make the table for a fast CRC. */
419 if (awi_crc_table_computed)
421 for (n = 0; n < 256; n++) {
423 for (k = 0; k < 8; k++) {
425 c = 0xedb88320UL ^ (c >> 1);
429 awi_crc_table[n] = c;
431 awi_crc_table_computed = 1;
435 * Update a running CRC with the bytes buf[0..len-1]--the CRC
436 * should be initialized to all 1's, and the transmitted value
437 * is the 1's complement of the final running CRC
441 awi_crc_update(u_int32_t crc, u_int8_t *buf, int len)
445 for (endbuf = buf + len; buf < endbuf; buf++)
446 crc = awi_crc_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
451 * Null -- do nothing but copy.
455 awi_null_ctxlen(void)
462 awi_null_setkey(void *ctx, u_char *key, int keylen)
467 awi_null_copy(void *ctx, u_char *dst, u_char *src, int len)
470 memcpy(dst, src, len);