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