From owner-p4-projects@FreeBSD.ORG Fri Aug 29 00:47:59 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 90969106566C; Fri, 29 Aug 2008 00:47:59 +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 3C4EF1065676 for ; Fri, 29 Aug 2008 00:47:59 +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 22A748FC16 for ; Fri, 29 Aug 2008 00:47:59 +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 m7T0lxPR020844 for ; Fri, 29 Aug 2008 00:47:59 GMT (envelope-from sam@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.2/8.14.1/Submit) id m7T0lwG2020842 for perforce@freebsd.org; Fri, 29 Aug 2008 00:47:58 GMT (envelope-from sam@freebsd.org) Date: Fri, 29 Aug 2008 00:47:58 GMT Message-Id: <200808290047.m7T0lwG2020842@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 148762 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 00:47:59 -0000 http://perforce.freebsd.org/chv.cgi?CH=148762 Change 148762 by sam@sam_ebb on 2008/08/29 00:47:10 update Affected files ... .. //depot/projects/vap/tools/regression/net80211/ccmp/test_ccmp.c#3 edit Differences ... ==== //depot/projects/vap/tools/regression/net80211/ccmp/test_ccmp.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 /* ==== CCMP test mpdu 1 ==== @@ -590,13 +593,65 @@ ); } +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(m, remainder, p + (len-remainder)); + m->m_next = n; + } + + return m; +} + static int -runtest(struct ieee80211com *ic, struct ciphertest *t) +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; +} + +static int +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; printf("%s: ", t->name); @@ -606,7 +661,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; @@ -614,9 +669,8 @@ memcpy(key.wk_key, t->key, t->key_len); key.wk_keylen = t->key_len; - key.wk_keyrsc = 0; key.wk_keytsc = t->pn-1; /* PN-1 since we do encap */ - if (!ieee80211_crypto_setkey(ic, &key, mac)) { + if (!ieee80211_crypto_setkey(vap, &key)) { printf("FAIL: ieee80211_crypto_setkey failed\n"); goto bad; } @@ -625,11 +679,8 @@ * Craft frame from plaintext data. */ cip = key.wk_cipher; - m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR); - m->m_data += cip->ic_header; - memcpy(mtod(m, void *), t->plaintext, t->plaintext_len); - m->m_len = t->plaintext_len; - m->m_pkthdr.len = m->m_len; + m = formpacket(cip, t->plaintext, t->plaintext_len, remainder); + hdrlen = ieee80211_anyhdrsize(t->plaintext); /* * Encrypt frame w/ MIC. @@ -642,17 +693,7 @@ /* * Verify: frame length, frame contents. */ - if (m->m_pkthdr.len != t->encrypted_len) { - printf("FAIL: encap data length mismatch\n"); - printtest(t); - 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, t->encrypted_len)) { - printf("FAIL: encrypt data does not compare\n"); - printtest(t); - 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; } @@ -660,7 +701,7 @@ /* * Decrypt frame; strip MIC. */ - if (!cip->ic_decap(&key, m)) { + if (!cip->ic_decap(&key, m, hdrlen)) { printf("FAIL: ccmp decap failed\n"); printtest(t); cmpfail(mtod(m, const void *), m->m_len, @@ -670,28 +711,15 @@ /* * Verify: frame length, frame contents. */ - if (m->m_pkthdr.len != t->plaintext_len) { - printf("FAIL: decap botch; length mismatch\n"); - printtest(t); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->plaintext, t->plaintext_len); - goto bad; - } else if (memcmp(mtod(m, const void *), t->plaintext, t->plaintext_len)) { - printf("FAIL: decap botch; data does not compare\n"); - printtest(t); - cmpfail(mtod(m, const void *), m->m_pkthdr.len, - t->plaintext, sizeof(t->plaintext)); + if (!verifypacket(m, t->plaintext, t->plaintext_len, "decap")) 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; } /* @@ -701,28 +729,97 @@ 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_ccmp_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(ccmptests); i++) if (tests & (1<