2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
35 #include "ah_internal.h"
48 return isdigit(c) ? c - '0' : isupper(c) ? c - 'A' + 10 : c - 'a' + 10;
52 getdata(const char *arg, u_int8_t *data, size_t maxlen)
57 if (cp[0] == '0' && (cp[1] == 'x' || cp[1] == 'X'))
62 if (cp[0] == ':' || cp[0] == '-' || cp[0] == '.') {
66 if (!isxdigit(cp[0])) {
67 fprintf(stderr, "%s: invalid data value %c (not hex)\n",
73 if (!isxdigit(cp[1])) {
74 fprintf(stderr, "%s: invalid data value %c "
75 "(not hex)\n", progname, cp[1]);
80 } else { /* fake up 0<n> */
86 "%s: too much data in %s, max %llu bytes\n",
87 progname, arg, (unsigned long long) maxlen);
89 data[len++] = (b0<<4) | b1;
94 /* XXX this assumes 5212 key types are common to 5211 and 5210 */
97 getcipher(const char *name)
99 #define streq(a,b) (strcasecmp(a,b) == 0)
101 if (streq(name, "wep"))
102 return HAL_CIPHER_WEP;
103 if (streq(name, "tkip"))
104 return HAL_CIPHER_TKIP;
105 if (streq(name, "aes-ocb") || streq(name, "ocb"))
106 return HAL_CIPHER_AES_OCB;
107 if (streq(name, "aes-ccm") || streq(name, "ccm") ||
109 return HAL_CIPHER_AES_CCM;
110 if (streq(name, "ckip"))
111 return HAL_CIPHER_CKIP;
112 if (streq(name, "none") || streq(name, "clr"))
113 return HAL_CIPHER_CLR;
115 fprintf(stderr, "%s: unknown cipher %s\n", progname, name);
123 fprintf(stderr, "usage: %s [-i device] keyix cipher keyval [mac]\n",
129 main(int argc, char *argv[])
133 HAL_DIAG_KEYVAL setkey;
137 int op = HAL_DIAG_SETKEY;
140 s = socket(AF_INET, SOCK_DGRAM, 0);
143 ifname = getenv("ATH");
145 ifname = ATH_DEFAULT;
148 while ((c = getopt(argc, argv, "di:x")) != -1)
151 op = HAL_DIAG_RESETKEY;
168 keyix = (u_int16_t) atoi(argv[0]);
170 errx(-1, "%s: invalid key index %s, must be [0..127]",
172 strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
173 atd.ad_id = op | ATH_DIAG_IN | ATH_DIAG_DYN;
174 atd.ad_out_data = NULL;
177 case HAL_DIAG_RESETKEY:
178 atd.ad_in_data = (caddr_t) &keyix;
179 atd.ad_in_size = sizeof(u_int16_t);
180 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
181 err(1, "ioctl: %s", atd.ad_name);
183 case HAL_DIAG_SETKEY:
184 if (argc != 3 && argc != 4)
186 memset(&setkey, 0, sizeof(setkey));
187 setkey.dk_keyix = keyix;
189 setkey.dk_keyval.kv_type = getcipher(argv[1]);
190 setkey.dk_keyval.kv_len = getdata(argv[2],
191 setkey.dk_keyval.kv_val, sizeof(setkey.dk_keyval.kv_val));
194 (void) getdata(argv[3], setkey.dk_mac,
196 atd.ad_in_data = (caddr_t) &setkey;
197 atd.ad_in_size = sizeof(setkey);
198 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
199 err(1, "ioctl: %s", atd.ad_name);