From owner-p4-projects@FreeBSD.ORG Fri Aug 29 03:02:26 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 5E62E1065676; Fri, 29 Aug 2008 03:02:26 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0A8BE1065678 for ; Fri, 29 Aug 2008 03:02:26 +0000 (UTC) (envelope-from sam@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id E768E8FC17 for ; Fri, 29 Aug 2008 03:02:25 +0000 (UTC) (envelope-from sam@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.2/8.14.2) with ESMTP id m7T32Pj3033796 for ; Fri, 29 Aug 2008 03:02:25 GMT (envelope-from sam@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.2/8.14.1/Submit) id m7T32P13033794 for perforce@freebsd.org; Fri, 29 Aug 2008 03:02:25 GMT (envelope-from sam@freebsd.org) Date: Fri, 29 Aug 2008 03:02:25 GMT Message-Id: <200808290302.m7T32P13033794@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to sam@freebsd.org using -f From: Sam Leffler To: Perforce Change Reviews Cc: Subject: PERFORCE change 148767 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Aug 2008 03:02:27 -0000 http://perforce.freebsd.org/chv.cgi?CH=148767 Change 148767 by sam@sam_ebb on 2008/08/29 03:01:36 update Affected files ... .. //depot/projects/vap/tools/regression/net80211/wep/test_wep.c#3 edit Differences ... ==== //depot/projects/vap/tools/regression/net80211/wep/test_wep.c#3 (text+ko) ==== @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2004 Sam Leffler, Errno Consulting + * Copyright (c) 2004-2008 Sam Leffler, Errno Consulting * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -54,8 +54,11 @@ #include #include +#include +#include #include +#include /* MPDU data @@ -177,18 +180,72 @@ dumpdata("Reference", ref, reflen); } +static struct mbuf * +formpacket(const struct ieee80211_cipher *cip, const void *plaintext, + int len, int remainder) +{ + struct mbuf *m, *n; + const uint8_t *p = plaintext; + + printf("(packet %d+%d) ", len-remainder, remainder); + + m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); + KASSERT(m != NULL, ("cannot allocate mbuf!")); + + m->m_data += cip->ic_header; + m_append(m, len-remainder, p); + + if (remainder) { + n = m_get(M_NOWAIT, MT_DATA); + KASSERT(n != NULL, ("cannot split mbuf chain!")); + m_append(n, remainder, p + (len-remainder)); + m->m_next = n; + m->m_pkthdr.len += remainder; + } + + return m; +} + +static int +verifypacket(const struct mbuf *m, const void *data, int len, const char *what) +{ + void *buf; + int ok; + + if (m->m_pkthdr.len != len) { + printf("FAIL: %s botch; length mismatch\n", what); + cmpfail(mtod(m, const void *), m->m_pkthdr.len, data, len); + return 0; + } + + buf = malloc(m->m_pkthdr.len, M_TEMP, M_NOWAIT); + if (buf == NULL) { + printf("ERROR: cannot allocate temp buffer for packet check\n"); + return 0; + } + m_copydata(m, 0, m->m_pkthdr.len, buf); + ok = (memcmp(buf, data, len) == 0); + free(buf, M_TEMP); + if (!ok) { + printf("FAIL: %s does not compare\n", what); + cmpfail(buf, m->m_pkthdr.len, data, len); + } + return ok; +} + struct wep_ctx_hw { /* for use with h/w support */ - struct ieee80211com *wc_ic; /* for diagnostics */ + struct ieee80211vap *wc_vap; /* for diagnostics+statistics */ + struct ieee80211com *wc_ic; u_int32_t wc_iv; /* initial vector for crypto */ }; static int -runtest(struct ieee80211com *ic, struct ciphertest *t) +runtest(struct ieee80211vap *vap, struct ciphertest *t, int remainder) { struct ieee80211_key key; struct mbuf *m = NULL; const struct ieee80211_cipher *cip; - u_int8_t mac[IEEE80211_ADDR_LEN]; + int hdrlen, result = 0; struct wep_ctx_hw *ctx; printf("%s: ", t->name); @@ -199,7 +256,7 @@ memset(&key, 0, sizeof(key)); key.wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV; key.wk_cipher = &ieee80211_cipher_none; - if (!ieee80211_crypto_newkey(ic, t->cipher, + if (!ieee80211_crypto_newkey(vap, t->cipher, IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, &key)) { printf("FAIL: ieee80211_crypto_newkey failed\n"); goto bad; @@ -207,25 +264,23 @@ memcpy(key.wk_key, t->key, t->key_len); key.wk_keylen = t->key_len; - if (!ieee80211_crypto_setkey(ic, &key, mac)) { + if (!ieee80211_crypto_setkey(vap, &key)) { printf("FAIL: ieee80211_crypto_setkey failed\n"); goto bad; } cip = key.wk_cipher; /* - * Craft frame from plaintext data. + * Craft frame from encrypted data. */ cip = key.wk_cipher; - m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR); - memcpy(mtod(m, void *), t->encrypted, t->encrypted_len); - m->m_len = t->encrypted_len; - m->m_pkthdr.len = m->m_len; + m = formpacket(cip, t->encrypted, t->encrypted_len, remainder); + hdrlen = ieee80211_anyhdrsize(t->encrypted); /* * Decrypt frame. */ - if (!cip->ic_decap(&key, m)) { + if (!cip->ic_decap(&key, m, hdrlen)) { printf("FAIL: wep decap failed\n"); cmpfail(mtod(m, const void *), m->m_pkthdr.len, t->plaintext, t->plaintext_len); @@ -234,17 +289,8 @@ /* * Verify: frame length, frame contents. */ - if (m->m_pkthdr.len != t->plaintext_len) { - printf("FAIL: decap botch; length mismatch\n"); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->plaintext, t->plaintext_len); + if (!verifypacket(m, t->plaintext, t->plaintext_len, "decap")) goto bad; - } else if (memcmp(mtod(m, const void *), t->plaintext, t->plaintext_len)) { - printf("FAIL: decap botch; data does not compare\n"); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->plaintext, sizeof(t->plaintext)); - goto bad; - } /* * Encrypt frame. @@ -258,27 +304,17 @@ /* * Verify: frame length, frame contents. */ - if (m->m_pkthdr.len != t->encrypted_len) { - printf("FAIL: encap data length mismatch\n"); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->encrypted, t->encrypted_len); - goto bad; - } else if (memcmp(mtod(m, const void *), t->encrypted, m->m_pkthdr.len)) { - printf("FAIL: encrypt data does not compare\n"); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->encrypted, t->encrypted_len); + if (!verifypacket(m, t->encrypted, t->encrypted_len, "encrypt data")) { dumpdata("Plaintext", t->plaintext, t->plaintext_len); goto bad; } - m_freem(m); - ieee80211_crypto_delkey(ic, &key); printf("PASS\n"); - return 1; + result = 1; bad: if (m != NULL) m_freem(m); - ieee80211_crypto_delkey(ic, &key); - return 0; + ieee80211_crypto_delkey(vap, &key); + return result; } /* @@ -288,27 +324,98 @@ static int tests = -1; static int debug = 0; +static struct ieee80211vap * +fake_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, + int opmode, int flags, + const uint8_t bssid[IEEE80211_ADDR_LEN], + const uint8_t mac[IEEE80211_ADDR_LEN]) +{ + struct ieee80211vap *vap; + + vap = malloc(sizeof(*vap), M_80211_VAP, M_WAITOK | M_ZERO); + ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); + ieee80211_vap_attach(vap, + ieee80211_media_change, ieee80211_media_status); + return vap; +} + +static void +fake_vap_delete(struct ieee80211vap *vap) +{ + ieee80211_vap_detach(vap); + free(vap, M_80211_VAP); +} + static int init_crypto_wep_test(void) { #define N(a) (sizeof(a)/sizeof(a[0])) - struct ieee80211com ic; - int i, pass, total; + struct ifnet *ifp, *vifp; + struct ieee80211com *ic; + struct ieee80211_clone_params icp; + char ifname[IFNAMSIZ]; + struct ieee80211vap *vap; + int pass = 0, total = 0, i, error; + uint8_t bands; + + ifp = if_alloc(IFT_IEEE80211); + if (ifp == NULL) { + printf("%s: unable to allocate ifnet!\n", __func__); + return -1; + } + if_initname(ifp, "fake", 0); + ifp->if_snd.ifq_maxlen = ifqmaxlen; + + ic = ifp->if_l2com; + ic->ic_caps = IEEE80211_C_STA; + ic->ic_opmode = IEEE80211_M_STA; + ic->ic_vap_create = fake_vap_create; + ic->ic_vap_delete = fake_vap_delete; + bands = 0; + setbit(&bands, IEEE80211_MODE_11B); + ieee80211_init_channels(ic, NULL, &bands); + ieee80211_ifattach(ic); + + memset(&icp, 0, sizeof(icp)); + strlcpy(icp.icp_parent, ifp->if_xname, sizeof(icp.icp_parent)); + icp.icp_opmode = IEEE80211_M_STA; - memset(&ic, 0, sizeof(ic)); + strlcpy(ifname, "wlan", sizeof(ifname)); + error = if_clone_create_sys(ifname, sizeof(ifname), (caddr_t) &icp); + if (error != 0) { + printf("%s: unable to clone vap (error %d)!\n", + __func__, error); + pass = -1; + goto bad; + } + vifp = ifunit(ifname); + if (vifp == NULL) { + printf("%s: unable to locate vap %s!\n", __func__, ifname); + pass = -1; + goto bad; + } + vap = vifp->if_softc; if (debug) - ic.ic_debug = IEEE80211_MSG_CRYPTO; - ieee80211_crypto_attach(&ic); + vap->iv_debug = IEEE80211_MSG_CRYPTO; + pass = 0; total = 0; for (i = 0; i < N(weptests); i++) if (tests & (1<