Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.c revision 1.4
      1 /*	$NetBSD: ieee80211_crypto.c,v 1.4 2003/09/23 16:03:46 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.4 2003/09/23 16:03:46 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 #ifdef __FreeBSD__
     53 #include <sys/bus.h>
     54 #endif
     55 #include <sys/proc.h>
     56 #include <sys/sysctl.h>
     57 
     58 #ifdef __FreeBSD__
     59 #include <machine/atomic.h>
     60 #endif
     61 
     62 #include <net/if.h>
     63 #include <net/if_dl.h>
     64 #include <net/if_media.h>
     65 #include <net/if_arp.h>
     66 #ifdef __FreeBSD__
     67 #include <net/ethernet.h>
     68 #else
     69 #include <net/if_ether.h>
     70 #endif
     71 #include <net/if_llc.h>
     72 
     73 #include <net80211/ieee80211_var.h>
     74 #include <net80211/ieee80211_compat.h>
     75 
     76 #include <net/bpf.h>
     77 
     78 #ifdef INET
     79 #include <netinet/in.h>
     80 #ifdef __FreeBSD__
     81 #include <netinet/if_ether.h>
     82 #else
     83 #include <net/if_ether.h>
     84 #endif
     85 #endif
     86 
     87 #ifdef __FreeBSD__
     88 #include <crypto/rc4/rc4.h>
     89 #define	arc4_ctxlen()			sizeof (struct rc4_state)
     90 #define	arc4_setkey(_c,_k,_l)		rc4_init(_c,_k,_l)
     91 #define	arc4_encrypt(_c,_d,_s,_l)	rc4_crypt(_c,_s,_d,_l)
     92 #else
     93 #include <crypto/arc4/arc4.h>
     94 #endif
     95 
     96 static	void ieee80211_crc_init(void);
     97 static	u_int32_t ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len);
     98 
     99 void
    100 ieee80211_crypto_attach(struct ifnet *ifp)
    101 {
    102 	struct ieee80211com *ic = (void *)ifp;
    103 
    104 	/*
    105 	 * Setup crypto support.
    106 	 */
    107 	ieee80211_crc_init();
    108 	ic->ic_iv = arc4random();
    109 }
    110 
    111 void
    112 ieee80211_crypto_detach(struct ifnet *ifp)
    113 {
    114 	struct ieee80211com *ic = (void *)ifp;
    115 
    116 	if (ic->ic_wep_ctx != NULL) {
    117 		free(ic->ic_wep_ctx, M_DEVBUF);
    118 		ic->ic_wep_ctx = NULL;
    119 	}
    120 }
    121 
    122 struct mbuf *
    123 ieee80211_wep_crypt(struct ifnet *ifp, struct mbuf *m0, int txflag)
    124 {
    125 	struct ieee80211com *ic = (void *)ifp;
    126 	struct mbuf *m, *n, *n0;
    127 	struct ieee80211_frame *wh;
    128 	int i, left, len, moff, noff, kid;
    129 	u_int32_t iv, crc;
    130 	u_int8_t *ivp;
    131 	void *ctx;
    132 	u_int8_t keybuf[IEEE80211_WEP_IVLEN + IEEE80211_KEYBUF_SIZE];
    133 	u_int8_t crcbuf[IEEE80211_WEP_CRCLEN];
    134 
    135 	n0 = NULL;
    136 	if ((ctx = ic->ic_wep_ctx) == NULL) {
    137 		ctx = malloc(arc4_ctxlen(), M_DEVBUF, M_NOWAIT);
    138 		if (ctx == NULL)
    139 			goto fail;
    140 		ic->ic_wep_ctx = ctx;
    141 	}
    142 	m = m0;
    143 	left = m->m_pkthdr.len;
    144 	MGET(n, M_DONTWAIT, m->m_type);
    145 	n0 = n;
    146 	if (n == NULL)
    147 		goto fail;
    148 #ifdef __FreeBSD__
    149 	M_MOVE_PKTHDR(n, m);
    150 #else
    151 	M_COPY_PKTHDR(n, m);
    152 #endif
    153 	len = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
    154 	if (txflag) {
    155 		n->m_pkthdr.len += len;
    156 	} else {
    157 		n->m_pkthdr.len -= len;
    158 		left -= len;
    159 	}
    160 	n->m_len = MHLEN;
    161 	if (n->m_pkthdr.len >= MINCLSIZE) {
    162 		MCLGET(n, M_DONTWAIT);
    163 		if (n->m_flags & M_EXT)
    164 			n->m_len = n->m_ext.ext_size;
    165 	}
    166 	len = sizeof(struct ieee80211_frame);
    167 	memcpy(mtod(n, caddr_t), mtod(m, caddr_t), len);
    168 	wh = mtod(n, struct ieee80211_frame *);
    169 	left -= len;
    170 	moff = len;
    171 	noff = len;
    172 	if (txflag) {
    173 		kid = ic->ic_wep_txkey;
    174 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    175                 iv = ic->ic_iv;
    176 		/*
    177 		 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
    178 		 * (B, 255, N) with 3 <= B < 8
    179 		 */
    180 		if (iv >= 0x03ff00 &&
    181 		    (iv & 0xf8ff00) == 0x00ff00)
    182 			iv += 0x000100;
    183 		ic->ic_iv = iv + 1;
    184 		/* put iv in little endian to prepare 802.11i */
    185 		ivp = mtod(n, u_int8_t *) + noff;
    186 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
    187 			ivp[i] = iv & 0xff;
    188 			iv >>= 8;
    189 		}
    190 		ivp[IEEE80211_WEP_IVLEN] = kid << 6;	/* pad and keyid */
    191 		noff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
    192 	} else {
    193 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
    194 		ivp = mtod(m, u_int8_t *) + moff;
    195 		kid = ivp[IEEE80211_WEP_IVLEN] >> 6;
    196 		moff += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
    197 	}
    198 	memcpy(keybuf, ivp, IEEE80211_WEP_IVLEN);
    199 	memcpy(keybuf + IEEE80211_WEP_IVLEN, ic->ic_nw_keys[kid].wk_key,
    200 	    ic->ic_nw_keys[kid].wk_len);
    201 	arc4_setkey(ctx, keybuf,
    202 	    IEEE80211_WEP_IVLEN + ic->ic_nw_keys[kid].wk_len);
    203 
    204 	/* encrypt with calculating CRC */
    205 	crc = ~0;
    206 	while (left > 0) {
    207 		len = m->m_len - moff;
    208 		if (len == 0) {
    209 			m = m->m_next;
    210 			moff = 0;
    211 			continue;
    212 		}
    213 		if (len > n->m_len - noff) {
    214 			len = n->m_len - noff;
    215 			if (len == 0) {
    216 				MGET(n->m_next, M_DONTWAIT, n->m_type);
    217 				if (n->m_next == NULL)
    218 					goto fail;
    219 				n = n->m_next;
    220 				n->m_len = MLEN;
    221 				if (left >= MINCLSIZE) {
    222 					MCLGET(n, M_DONTWAIT);
    223 					if (n->m_flags & M_EXT)
    224 						n->m_len = n->m_ext.ext_size;
    225 				}
    226 				noff = 0;
    227 				continue;
    228 			}
    229 		}
    230 		if (len > left)
    231 			len = left;
    232 		arc4_encrypt(ctx, mtod(n, caddr_t) + noff,
    233 		    mtod(m, caddr_t) + moff, len);
    234 		if (txflag)
    235 			crc = ieee80211_crc_update(crc,
    236 			    mtod(m, u_int8_t *) + moff, len);
    237 		else
    238 			crc = ieee80211_crc_update(crc,
    239 			    mtod(n, u_int8_t *) + noff, len);
    240 		left -= len;
    241 		moff += len;
    242 		noff += len;
    243 	}
    244 	crc = ~crc;
    245 	if (txflag) {
    246 		*(u_int32_t *)crcbuf = htole32(crc);
    247 		if (n->m_len >= noff + sizeof(crcbuf))
    248 			n->m_len = noff + sizeof(crcbuf);
    249 		else {
    250 			n->m_len = noff;
    251 			MGET(n->m_next, M_DONTWAIT, n->m_type);
    252 			if (n->m_next == NULL)
    253 				goto fail;
    254 			n = n->m_next;
    255 			n->m_len = sizeof(crcbuf);
    256 			noff = 0;
    257 		}
    258 		arc4_encrypt(ctx, mtod(n, caddr_t) + noff, crcbuf,
    259 		    sizeof(crcbuf));
    260 	} else {
    261 		n->m_len = noff;
    262 		for (noff = 0; noff < sizeof(crcbuf); noff += len) {
    263 			len = sizeof(crcbuf) - noff;
    264 			if (len > m->m_len - moff)
    265 				len = m->m_len - moff;
    266 			if (len > 0)
    267 				arc4_encrypt(ctx, crcbuf + noff,
    268 				    mtod(m, caddr_t) + moff, len);
    269 			m = m->m_next;
    270 			moff = 0;
    271 		}
    272 		if (crc != le32toh(*(u_int32_t *)crcbuf)) {
    273 #ifdef IEEE80211_DEBUG
    274 			if (ieee80211_debug) {
    275 				if_printf(ifp, "decrypt CRC error\n");
    276 				if (ieee80211_debug > 1)
    277 					ieee80211_dump_pkt(n0->m_data,
    278 					    n0->m_len, -1, -1);
    279 			}
    280 #endif
    281 			goto fail;
    282 		}
    283 	}
    284 	m_freem(m0);
    285 	return n0;
    286 
    287   fail:
    288 	m_freem(m0);
    289 	m_freem(n0);
    290 	return NULL;
    291 }
    292 
    293 /*
    294  * CRC 32 -- routine from RFC 2083
    295  */
    296 
    297 /* Table of CRCs of all 8-bit messages */
    298 static u_int32_t ieee80211_crc_table[256];
    299 
    300 /* Make the table for a fast CRC. */
    301 static void
    302 ieee80211_crc_init(void)
    303 {
    304 	u_int32_t c;
    305 	int n, k;
    306 
    307 	for (n = 0; n < 256; n++) {
    308 		c = (u_int32_t)n;
    309 		for (k = 0; k < 8; k++) {
    310 			if (c & 1)
    311 				c = 0xedb88320UL ^ (c >> 1);
    312 			else
    313 				c = c >> 1;
    314 		}
    315 		ieee80211_crc_table[n] = c;
    316 	}
    317 }
    318 
    319 /*
    320  * Update a running CRC with the bytes buf[0..len-1]--the CRC
    321  * should be initialized to all 1's, and the transmitted value
    322  * is the 1's complement of the final running CRC
    323  */
    324 
    325 static u_int32_t
    326 ieee80211_crc_update(u_int32_t crc, u_int8_t *buf, int len)
    327 {
    328 	u_int8_t *endbuf;
    329 
    330 	for (endbuf = buf + len; buf < endbuf; buf++)
    331 		crc = ieee80211_crc_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
    332 	return crc;
    333 }
    334