Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.c revision 1.3
      1 /*	$NetBSD: ieee80211_crypto.c,v 1.3 2003/09/14 01:14:54 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.2 2003/06/27 05:13:52 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto.c,v 1.3 2003/09/14 01:14:54 dyoung Exp $");
     39 #endif
     40 
     41 #include "opt_inet.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/malloc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/sockio.h>
     50 #include <sys/endian.h>
     51 #include <sys/errno.h>
     52 #include <sys/bus.h>
     53 #include <sys/proc.h>
     54 #include <sys/sysctl.h>
     55 
     56 #ifdef __FreeBSD__
     57 #include <machine/atomic.h>
     58 #endif
     59 
     60 #include <net/if.h>
     61 #include <net/if_dl.h>
     62 #include <net/if_media.h>
     63 #include <net/if_arp.h>
     64 #ifdef __FreeBSD__
     65 #include <net/ethernet.h>
     66 #endif
     67 #include <net/if_llc.h>
     68 
     69 #include <net80211/ieee80211_var.h>
     70 
     71 #include <net/bpf.h>
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #include <netinet/if_ether.h>
     76 #endif
     77 
     78 #ifdef __FreeBSD__
     79 #include <crypto/rc4/rc4.h>
     80 #define	arc4_ctxlen()			sizeof (struct rc4_state)
     81 #define	arc4_setkey(_c,_k,_l)		rc4_init(_c,_k,_l)
     82 #define	arc4_encrypt(_c,_d,_s,_l)	rc4_crypt(_c,_s,_d,_l)
     83 #else
     84 #include <crypto/arc4/arc4.h>
     85 #endif
     86 
     87 static	void ieee80211_crc_init(void);
     88 static	u_int32_t ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len);
     89 
     90 void
     91 ieee80211_crypto_attach(struct ifnet *ifp)
     92 {
     93 	struct ieee80211com *ic = (void *)ifp;
     94 
     95 	/*
     96 	 * Setup crypto support.
     97 	 */
     98 	ieee80211_crc_init();
     99 	ic->ic_iv = arc4random();
    100 }
    101 
    102 void
    103 ieee80211_crypto_detach(struct ifnet *ifp)
    104 {
    105 	struct ieee80211com *ic = (void *)ifp;
    106 
    107 	if (ic->ic_wep_ctx != NULL) {
    108 		free(ic->ic_wep_ctx, M_DEVBUF);
    109 		ic->ic_wep_ctx = NULL;
    110 	}
    111 }
    112 
    113 struct mbuf *
    114 ieee80211_wep_crypt(struct ifnet *ifp, struct mbuf *m0, int txflag)
    115 {
    116 	struct ieee80211com *ic = (void *)ifp;
    117 	struct mbuf *m, *n, *n0;
    118 	struct ieee80211_frame *wh;
    119 	int i, left, len, moff, noff, kid;
    120 	u_int32_t iv, crc;
    121 	u_int8_t *ivp;
    122 	void *ctx;
    123 	u_int8_t keybuf[IEEE80211_WEP_IVLEN + IEEE80211_KEYBUF_SIZE];
    124 	u_int8_t crcbuf[IEEE80211_WEP_CRCLEN];
    125 
    126 	n0 = NULL;
    127 	if ((ctx = ic->ic_wep_ctx) == NULL) {
    128 		ctx = malloc(arc4_ctxlen(), M_DEVBUF, M_NOWAIT);
    129 		if (ctx == NULL)
    130 			goto fail;
    131 		ic->ic_wep_ctx = ctx;
    132 	}
    133 	m = m0;
    134 	left = m->m_pkthdr.len;
    135 	MGET(n, M_DONTWAIT, m->m_type);
    136 	n0 = n;
    137 	if (n == NULL)
    138 		goto fail;
    139 	M_MOVE_PKTHDR(n, m);
    140 	len = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
    141 	if (txflag) {
    142 		n->m_pkthdr.len += len;
    143 	} else {
    144 		n->m_pkthdr.len -= len;
    145 		left -= len;
    146 	}
    147 	n->m_len = MHLEN;
    148 	if (n->m_pkthdr.len >= MINCLSIZE) {
    149 		MCLGET(n, M_DONTWAIT);
    150 		if (n->m_flags & M_EXT)
    151 			n->m_len = n->m_ext.ext_size;
    152 	}
    153 	len = sizeof(struct ieee80211_frame);
    154 	memcpy(mtod(n, caddr_t), mtod(m, caddr_t), len);
    155 	wh = mtod(n, struct ieee80211_frame *);
    156 	left -= len;
    157 	moff = len;
    158 	noff = len;
    159 	if (txflag) {
    160 		kid = ic->ic_wep_txkey;
    161 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    162                 iv = ic->ic_iv;
    163 		/*
    164 		 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
    165 		 * (B, 255, N) with 3 <= B < 8
    166 		 */
    167 		if (iv >= 0x03ff00 &&
    168 		    (iv & 0xf8ff00) == 0x00ff00)
    169 			iv += 0x000100;
    170 		ic->ic_iv = iv + 1;
    171 		/* put iv in little endian to prepare 802.11i */
    172 		ivp = mtod(n, u_int8_t *) + noff;
    173 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
    174 			ivp[i] = iv & 0xff;
    175 			iv >>= 8;
    176 		}
    177 		ivp[IEEE80211_WEP_IVLEN] = kid << 6;	/* pad and keyid */
    178 		noff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
    179 	} else {
    180 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
    181 		ivp = mtod(m, u_int8_t *) + moff;
    182 		kid = ivp[IEEE80211_WEP_IVLEN] >> 6;
    183 		moff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
    184 	}
    185 	memcpy(keybuf, ivp, IEEE80211_WEP_IVLEN);
    186 	memcpy(keybuf + IEEE80211_WEP_IVLEN, ic->ic_nw_keys[kid].wk_key,
    187 	    ic->ic_nw_keys[kid].wk_len);
    188 	arc4_setkey(ctx, keybuf,
    189 	    IEEE80211_WEP_IVLEN + ic->ic_nw_keys[kid].wk_len);
    190 
    191 	/* encrypt with calculating CRC */
    192 	crc = ~0;
    193 	while (left > 0) {
    194 		len = m->m_len - moff;
    195 		if (len == 0) {
    196 			m = m->m_next;
    197 			moff = 0;
    198 			continue;
    199 		}
    200 		if (len > n->m_len - noff) {
    201 			len = n->m_len - noff;
    202 			if (len == 0) {
    203 				MGET(n->m_next, M_DONTWAIT, n->m_type);
    204 				if (n->m_next == NULL)
    205 					goto fail;
    206 				n = n->m_next;
    207 				n->m_len = MLEN;
    208 				if (left >= MINCLSIZE) {
    209 					MCLGET(n, M_DONTWAIT);
    210 					if (n->m_flags & M_EXT)
    211 						n->m_len = n->m_ext.ext_size;
    212 				}
    213 				noff = 0;
    214 				continue;
    215 			}
    216 		}
    217 		if (len > left)
    218 			len = left;
    219 		arc4_encrypt(ctx, mtod(n, caddr_t) + noff,
    220 		    mtod(m, caddr_t) + moff, len);
    221 		if (txflag)
    222 			crc = ieee80211_crc_update(crc,
    223 			    mtod(m, u_int8_t *) + moff, len);
    224 		else
    225 			crc = ieee80211_crc_update(crc,
    226 			    mtod(n, u_int8_t *) + noff, len);
    227 		left -= len;
    228 		moff += len;
    229 		noff += len;
    230 	}
    231 	crc = ~crc;
    232 	if (txflag) {
    233 		*(u_int32_t *)crcbuf = htole32(crc);
    234 		if (n->m_len >= noff + sizeof(crcbuf))
    235 			n->m_len = noff + sizeof(crcbuf);
    236 		else {
    237 			n->m_len = noff;
    238 			MGET(n->m_next, M_DONTWAIT, n->m_type);
    239 			if (n->m_next == NULL)
    240 				goto fail;
    241 			n = n->m_next;
    242 			n->m_len = sizeof(crcbuf);
    243 			noff = 0;
    244 		}
    245 		arc4_encrypt(ctx, mtod(n, caddr_t) + noff, crcbuf,
    246 		    sizeof(crcbuf));
    247 	} else {
    248 		n->m_len = noff;
    249 		for (noff = 0; noff < sizeof(crcbuf); noff += len) {
    250 			len = sizeof(crcbuf) - noff;
    251 			if (len > m->m_len - moff)
    252 				len = m->m_len - moff;
    253 			if (len > 0)
    254 				arc4_encrypt(ctx, crcbuf + noff,
    255 				    mtod(m, caddr_t) + moff, len);
    256 			m = m->m_next;
    257 			moff = 0;
    258 		}
    259 		if (crc != le32toh(*(u_int32_t *)crcbuf)) {
    260 #ifdef IEEE80211_DEBUG
    261 			if (ieee80211_debug) {
    262 				if_printf(ifp, "decrypt CRC error\n");
    263 				if (ieee80211_debug > 1)
    264 					ieee80211_dump_pkt(n0->m_data,
    265 					    n0->m_len, -1, -1);
    266 			}
    267 #endif
    268 			goto fail;
    269 		}
    270 	}
    271 	m_freem(m0);
    272 	return n0;
    273 
    274   fail:
    275 	m_freem(m0);
    276 	m_freem(n0);
    277 	return NULL;
    278 }
    279 
    280 /*
    281  * CRC 32 -- routine from RFC 2083
    282  */
    283 
    284 /* Table of CRCs of all 8-bit messages */
    285 static u_int32_t ieee80211_crc_table[256];
    286 
    287 /* Make the table for a fast CRC. */
    288 static void
    289 ieee80211_crc_init(void)
    290 {
    291 	u_int32_t c;
    292 	int n, k;
    293 
    294 	for (n = 0; n < 256; n++) {
    295 		c = (u_int32_t)n;
    296 		for (k = 0; k < 8; k++) {
    297 			if (c & 1)
    298 				c = 0xedb88320UL ^ (c >> 1);
    299 			else
    300 				c = c >> 1;
    301 		}
    302 		ieee80211_crc_table[n] = c;
    303 	}
    304 }
    305 
    306 /*
    307  * Update a running CRC with the bytes buf[0..len-1]--the CRC
    308  * should be initialized to all 1's, and the transmitted value
    309  * is the 1's complement of the final running CRC
    310  */
    311 
    312 static u_int32_t
    313 ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len)
    314 {
    315 	u_int8_t *endbuf;
    316 
    317 	for (endbuf = buf + len; buf < endbuf; buf++)
    318 		crc = ieee80211_crc_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
    319 	return crc;
    320 }
    321