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