]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
make it possible to pass WEP keys to connect/join
[thirdparty/iw.git] / util.c
1 #include <ctype.h>
2 #include <netlink/attr.h>
3 #include <errno.h>
4 #include <stdbool.h>
5 #include "iw.h"
6 #include "nl80211.h"
7
8 int mac_addr_n2a(char *mac_addr, unsigned char *arg)
9 {
10 int i, l;
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
15 sprintf(mac_addr+l, "%02x", arg[i]);
16 l += 2;
17 } else {
18 sprintf(mac_addr+l, ":%02x", arg[i]);
19 l += 3;
20 }
21 }
22 return 0;
23 }
24
25 int mac_addr_a2n(unsigned char *mac_addr, char *arg)
26 {
27 int i;
28
29 for (i = 0; i < ETH_ALEN ; i++) {
30 int temp;
31 char *cp = strchr(arg, ':');
32 if (cp) {
33 *cp = 0;
34 cp++;
35 }
36 if (sscanf(arg, "%x", &temp) != 1)
37 return -1;
38 if (temp < 0 || temp > 255)
39 return -1;
40
41 mac_addr[i] = temp;
42 if (!cp)
43 break;
44 arg = cp;
45 }
46 if (i < ETH_ALEN - 1)
47 return -1;
48
49 return 0;
50 }
51
52 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
53 "unspecified",
54 "IBSS",
55 "managed",
56 "AP",
57 "AP/VLAN",
58 "WDS",
59 "monitor",
60 "mesh point"
61 };
62
63 static char modebuf[100];
64
65 const char *iftype_name(enum nl80211_iftype iftype)
66 {
67 if (iftype <= NL80211_IFTYPE_MAX)
68 return ifmodes[iftype];
69 sprintf(modebuf, "Unknown mode (%d)", iftype);
70 return modebuf;
71 }
72
73 int ieee80211_channel_to_frequency(int chan)
74 {
75 if (chan < 14)
76 return 2407 + chan * 5;
77
78 if (chan == 14)
79 return 2484;
80
81 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
82 return (chan + 1000) * 5;
83 }
84
85 int ieee80211_frequency_to_channel(int freq)
86 {
87 if (freq == 2484)
88 return 14;
89
90 if (freq < 2484)
91 return (freq - 2407) / 5;
92
93 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
94 return freq/5 - 1000;
95 }
96
97 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
98 {
99 int i;
100
101 for (i = 0; i < len; i++) {
102 if (isprint(data[i]))
103 printf("%c", data[i]);
104 else
105 printf("\\x%.2x", data[i]);
106 }
107 }
108
109 static int hex2num(char digit)
110 {
111 if (!isxdigit(digit))
112 return -1;
113 if (isdigit(digit))
114 return digit - '0';
115 return tolower(digit) - 'a' + 10;
116 }
117
118 static int hex2byte(char *hex)
119 {
120 int d1, d2;
121
122 d1 = hex2num(hex[0]);
123 if (d1 < 0)
124 return -1;
125 d2 = hex2num(hex[1]);
126 if (d2 < 0)
127 return -1;
128 return (d1 << 4) | d2;
129 }
130
131 static char *hex2bin(char *hex, char *buf)
132 {
133 char *result = buf;
134 int d;
135
136 while (hex[0]) {
137 d = hex2byte(hex);
138 if (d < 0)
139 return NULL;
140 buf[0] = d;
141 buf++;
142 hex += 2;
143 }
144
145 return result;
146 }
147
148 int parse_keys(struct nl_msg *msg, char **argv, int argc)
149 {
150 struct nlattr *keys;
151 int i = 0;
152 char keybuf[13];
153
154 if (!argc)
155 return 1;
156
157 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
158
159 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
160 if (!keys)
161 return -ENOBUFS;
162
163 do {
164 char *arg = *argv;
165 int pos = 0, keylen;
166 struct nlattr *key = nla_nest_start(msg, ++i);
167 char *keydata;
168
169 if (!key)
170 return -ENOBUFS;
171
172 if (arg[pos] == 'd') {
173 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
174 pos++;
175 if (arg[pos] == ':')
176 pos++;
177 }
178
179 if (!isdigit(arg[pos]))
180 goto explain;
181 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
182 if (arg[pos++] != ':')
183 goto explain;
184 keydata = arg + pos;
185 switch (strlen(keydata)) {
186 case 10:
187 keydata = hex2bin(keydata, keybuf);
188 case 5:
189 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
190 keylen = 5;
191 break;
192 case 26:
193 keydata = hex2bin(keydata, keybuf);
194 case 13:
195 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
196 keylen = 13;
197 break;
198 default:
199 goto explain;
200 }
201
202 if (!keydata)
203 goto explain;
204
205 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
206
207 nla_nest_end(msg, key);
208 argv++;
209 argc--;
210 } while (argc);
211
212 nla_nest_end(msg, keys);
213
214 return 0;
215 nla_put_failure:
216 return -ENOBUFS;
217 explain:
218 fprintf(stderr, "key must be [d:]index:data where\n"
219 " 'd:' means default (transmit) key\n"
220 " 'index:' is a single digit (0-3)\n"
221 " 'data' must be 5 or 13 ascii chars\n"
222 " or 10 or 26 hex digits\n"
223 "for example: d:2:6162636465 is the same as d:2:abcde\n");
224 return 2;
225 }