Johannes Berg | 748f848 | 2009-05-24 16:48:17 +0200 | [diff] [blame] | 1 | #include <ctype.h> |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 2 | #include <netlink/attr.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdbool.h> |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 5 | #include "iw.h" |
Johannes Berg | f5f7b1d | 2009-04-08 13:01:18 +0200 | [diff] [blame] | 6 | #include "nl80211.h" |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 7 | |
Johannes Berg | febeb0c | 2009-07-25 17:31:08 +0200 | [diff] [blame] | 8 | void mac_addr_n2a(char *mac_addr, unsigned char *arg) |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 9 | { |
Johannes Berg | 53e5ce7 | 2008-04-02 16:49:50 +0200 | [diff] [blame] | 10 | int i, l; |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 11 | |
| 12 | l = 0; |
| 13 | for (i = 0; i < ETH_ALEN ; i++) { |
| 14 | if (i == 0) { |
Johannes Berg | 53e5ce7 | 2008-04-02 16:49:50 +0200 | [diff] [blame] | 15 | sprintf(mac_addr+l, "%02x", arg[i]); |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 16 | l += 2; |
| 17 | } else { |
Johannes Berg | 53e5ce7 | 2008-04-02 16:49:50 +0200 | [diff] [blame] | 18 | sprintf(mac_addr+l, ":%02x", arg[i]); |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 19 | l += 3; |
| 20 | } |
| 21 | } |
Luis Carlos Cobo | 3d1e870 | 2008-04-01 12:03:41 -0700 | [diff] [blame] | 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 | } |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 50 | |
| 51 | static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = { |
| 52 | "unspecified", |
| 53 | "IBSS", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 54 | "managed", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 55 | "AP", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 56 | "AP/VLAN", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 57 | "WDS", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 58 | "monitor", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 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 | } |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 71 | |
Marcel Holtmann | 9990c1e | 2009-11-14 20:10:21 +0100 | [diff] [blame] | 72 | static const char *commands[NL80211_CMD_MAX + 1] = { |
| 73 | "unspecified", |
| 74 | "get_wiphy", |
| 75 | "set_wiphy", |
| 76 | "new_wiphy", |
| 77 | "del_wiphy", |
| 78 | "get_interface", |
| 79 | "set_interface", |
| 80 | "new_interface", |
| 81 | "del_interface", |
| 82 | "get_key", |
| 83 | "set_key", |
| 84 | "new_key", |
| 85 | "del_key", |
| 86 | "get_beacon", |
| 87 | "set_beacon", |
| 88 | "new_beacon", |
| 89 | "del_beacon", |
| 90 | "get_station", |
| 91 | "set_station", |
| 92 | "new_station", |
| 93 | "del_station", |
| 94 | "get_mpath", |
| 95 | "set_mpath", |
| 96 | "new_mpath", |
| 97 | "del_mpath", |
| 98 | "set_bss", |
| 99 | "set_reg", |
| 100 | "reg_set_reg", |
| 101 | "get_mesh_params", |
| 102 | "set_mesh_params", |
| 103 | "set_mgmt_extra_ie", |
| 104 | "get_reg", |
| 105 | "get_scan", |
| 106 | "trigger_scan", |
| 107 | "new_scan_results", |
| 108 | "scan_aborted", |
| 109 | "reg_change", |
| 110 | "authenticate", |
| 111 | "associate", |
| 112 | "deauthenticate", |
| 113 | "disassociate", |
| 114 | "michael_mic_failure", |
| 115 | "reg_beacon_hint", |
| 116 | "join_ibss", |
| 117 | "leave_ibss", |
| 118 | "testmode", |
| 119 | "connect", |
| 120 | "roam", |
| 121 | "disconnect", |
| 122 | "set_wiphy_netns" |
| 123 | }; |
| 124 | |
| 125 | static char cmdbuf[100]; |
| 126 | |
| 127 | const char *command_name(enum nl80211_commands cmd) |
| 128 | { |
| 129 | if (cmd <= NL80211_CMD_MAX) |
| 130 | return commands[cmd]; |
| 131 | sprintf(cmdbuf, "Unknown command (%d)", cmd); |
| 132 | return cmdbuf; |
| 133 | } |
| 134 | |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 135 | int ieee80211_channel_to_frequency(int chan) |
| 136 | { |
| 137 | if (chan < 14) |
| 138 | return 2407 + chan * 5; |
| 139 | |
| 140 | if (chan == 14) |
| 141 | return 2484; |
| 142 | |
| 143 | /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */ |
| 144 | return (chan + 1000) * 5; |
| 145 | } |
| 146 | |
| 147 | int ieee80211_frequency_to_channel(int freq) |
| 148 | { |
| 149 | if (freq == 2484) |
| 150 | return 14; |
| 151 | |
| 152 | if (freq < 2484) |
| 153 | return (freq - 2407) / 5; |
| 154 | |
| 155 | /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */ |
| 156 | return freq/5 - 1000; |
| 157 | } |
Johannes Berg | 748f848 | 2009-05-24 16:48:17 +0200 | [diff] [blame] | 158 | |
| 159 | void print_ssid_escaped(const uint8_t len, const uint8_t *data) |
| 160 | { |
| 161 | int i; |
| 162 | |
| 163 | for (i = 0; i < len; i++) { |
| 164 | if (isprint(data[i])) |
| 165 | printf("%c", data[i]); |
| 166 | else |
| 167 | printf("\\x%.2x", data[i]); |
| 168 | } |
| 169 | } |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 170 | |
| 171 | static int hex2num(char digit) |
| 172 | { |
| 173 | if (!isxdigit(digit)) |
| 174 | return -1; |
| 175 | if (isdigit(digit)) |
| 176 | return digit - '0'; |
| 177 | return tolower(digit) - 'a' + 10; |
| 178 | } |
| 179 | |
| 180 | static int hex2byte(char *hex) |
| 181 | { |
| 182 | int d1, d2; |
| 183 | |
| 184 | d1 = hex2num(hex[0]); |
| 185 | if (d1 < 0) |
| 186 | return -1; |
| 187 | d2 = hex2num(hex[1]); |
| 188 | if (d2 < 0) |
| 189 | return -1; |
| 190 | return (d1 << 4) | d2; |
| 191 | } |
| 192 | |
| 193 | static char *hex2bin(char *hex, char *buf) |
| 194 | { |
| 195 | char *result = buf; |
| 196 | int d; |
| 197 | |
| 198 | while (hex[0]) { |
| 199 | d = hex2byte(hex); |
| 200 | if (d < 0) |
| 201 | return NULL; |
| 202 | buf[0] = d; |
| 203 | buf++; |
| 204 | hex += 2; |
| 205 | } |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | int parse_keys(struct nl_msg *msg, char **argv, int argc) |
| 211 | { |
| 212 | struct nlattr *keys; |
| 213 | int i = 0; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 214 | bool have_default = false; |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 215 | char keybuf[13]; |
| 216 | |
| 217 | if (!argc) |
| 218 | return 1; |
| 219 | |
| 220 | NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); |
| 221 | |
| 222 | keys = nla_nest_start(msg, NL80211_ATTR_KEYS); |
| 223 | if (!keys) |
| 224 | return -ENOBUFS; |
| 225 | |
| 226 | do { |
| 227 | char *arg = *argv; |
| 228 | int pos = 0, keylen; |
| 229 | struct nlattr *key = nla_nest_start(msg, ++i); |
| 230 | char *keydata; |
| 231 | |
| 232 | if (!key) |
| 233 | return -ENOBUFS; |
| 234 | |
| 235 | if (arg[pos] == 'd') { |
| 236 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); |
| 237 | pos++; |
| 238 | if (arg[pos] == ':') |
| 239 | pos++; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 240 | have_default = true; |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (!isdigit(arg[pos])) |
| 244 | goto explain; |
| 245 | NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0'); |
| 246 | if (arg[pos++] != ':') |
| 247 | goto explain; |
| 248 | keydata = arg + pos; |
| 249 | switch (strlen(keydata)) { |
| 250 | case 10: |
| 251 | keydata = hex2bin(keydata, keybuf); |
| 252 | case 5: |
| 253 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01); |
| 254 | keylen = 5; |
| 255 | break; |
| 256 | case 26: |
| 257 | keydata = hex2bin(keydata, keybuf); |
| 258 | case 13: |
| 259 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05); |
| 260 | keylen = 13; |
| 261 | break; |
| 262 | default: |
| 263 | goto explain; |
| 264 | } |
| 265 | |
| 266 | if (!keydata) |
| 267 | goto explain; |
| 268 | |
| 269 | NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata); |
| 270 | |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 271 | argv++; |
| 272 | argc--; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 273 | |
| 274 | /* one key should be TX key */ |
| 275 | if (!have_default && !argc) |
| 276 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); |
| 277 | |
| 278 | nla_nest_end(msg, key); |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 279 | } while (argc); |
| 280 | |
| 281 | nla_nest_end(msg, keys); |
| 282 | |
| 283 | return 0; |
| 284 | nla_put_failure: |
| 285 | return -ENOBUFS; |
| 286 | explain: |
| 287 | fprintf(stderr, "key must be [d:]index:data where\n" |
| 288 | " 'd:' means default (transmit) key\n" |
| 289 | " 'index:' is a single digit (0-3)\n" |
| 290 | " 'data' must be 5 or 13 ascii chars\n" |
| 291 | " or 10 or 26 hex digits\n" |
| 292 | "for example: d:2:6162636465 is the same as d:2:abcde\n"); |
| 293 | return 2; |
| 294 | } |
Luis R. Rodriguez | deb3501 | 2009-12-07 21:13:56 -0500 | [diff] [blame] | 295 | |
| 296 | void print_mcs_set(const uint8_t *data) |
| 297 | { |
| 298 | unsigned int i; |
| 299 | |
| 300 | for (i = 15; i != 0; i--) { |
| 301 | printf(" %.2x", data[i]); |
| 302 | } |
| 303 | } |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame^] | 304 | |
| 305 | /* |
| 306 | * There are only 4 possible values, we just use a case instead of computing it, |
| 307 | * but technically this can also be computed through the formula: |
| 308 | * |
| 309 | * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes |
| 310 | */ |
| 311 | static __u32 compute_ampdu_length(__u8 exponent) |
| 312 | { |
| 313 | switch (exponent) { |
| 314 | case 0: return 8191; /* (2 ^(13 + 0)) -1 */ |
| 315 | case 1: return 16383; /* (2 ^(13 + 1)) -1 */ |
| 316 | case 2: return 32767; /* (2 ^(13 + 2)) -1 */ |
| 317 | case 3: return 65535; /* (2 ^(13 + 3)) -1 */ |
| 318 | default: return 0; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | static const char *print_ampdu_space(__u8 space) |
| 323 | { |
| 324 | switch (space) { |
| 325 | case 0: return "No restriction"; |
| 326 | case 1: return "1/4 usec"; |
| 327 | case 2: return "1/2 usec"; |
| 328 | case 3: return "1 usec"; |
| 329 | case 4: return "2 usec"; |
| 330 | case 5: return "4 usec"; |
| 331 | case 6: return "8 usec"; |
| 332 | case 7: return "16 usec"; |
| 333 | default: |
| 334 | return "Uknown"; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void print_ampdu_length(__u8 exponent) |
| 339 | { |
| 340 | __u8 max_ampdu_length; |
| 341 | |
| 342 | max_ampdu_length = compute_ampdu_length(exponent); |
| 343 | |
| 344 | if (max_ampdu_length) { |
| 345 | printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n", |
| 346 | max_ampdu_length, exponent); |
| 347 | } else { |
| 348 | printf("\t\tMaximum RX AMPDU length: unrecognized bytes " |
| 349 | "(exponent: %d)\n", exponent); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void print_ampdu_spacing(__u8 spacing) |
| 354 | { |
| 355 | printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n", |
| 356 | print_ampdu_space(spacing), spacing); |
| 357 | } |