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 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 51 | int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len, |
| 52 | unsigned char **mask) |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 53 | { |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 54 | size_t len = strlen(hexmask) / 2; |
| 55 | unsigned char *result_val; |
| 56 | unsigned char *result_mask = NULL; |
| 57 | |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 58 | int pos = 0; |
| 59 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 60 | *result_len = 0; |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 61 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 62 | result_val = calloc(len + 2, 1); |
| 63 | if (!result_val) |
| 64 | goto error; |
| 65 | *result = result_val; |
| 66 | if (mask) { |
| 67 | result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1); |
| 68 | if (!result_mask) |
| 69 | goto error; |
| 70 | *mask = result_mask; |
| 71 | } |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 72 | |
| 73 | while (1) { |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 74 | char *cp = strchr(hexmask, ':'); |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 75 | if (cp) { |
| 76 | *cp = 0; |
| 77 | cp++; |
| 78 | } |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 79 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 80 | if (result_mask && (strcmp(hexmask, "-") == 0 || |
| 81 | strcmp(hexmask, "xx") == 0 || |
| 82 | strcmp(hexmask, "--") == 0)) { |
| 83 | /* skip this byte and leave mask bit unset */ |
| 84 | } else { |
| 85 | int temp, mask_pos; |
| 86 | char *end; |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 87 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 88 | temp = strtoul(hexmask, &end, 16); |
| 89 | if (*end) |
| 90 | goto error; |
| 91 | if (temp < 0 || temp > 255) |
| 92 | goto error; |
| 93 | result_val[pos] = temp; |
| 94 | |
| 95 | mask_pos = pos / 8; |
| 96 | if (result_mask) |
| 97 | result_mask[mask_pos] |= 1 << (pos % 8); |
| 98 | } |
| 99 | |
| 100 | (*result_len)++; |
| 101 | pos++; |
| 102 | |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 103 | if (!cp) |
| 104 | break; |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 105 | hexmask = cp; |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 108 | return 0; |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 109 | error: |
Johannes Berg | 3ff2456 | 2011-04-12 13:04:50 +0200 | [diff] [blame] | 110 | free(result_val); |
| 111 | free(result_mask); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | unsigned char *parse_hex(char *hex, size_t *outlen) |
| 116 | { |
| 117 | unsigned char *result; |
| 118 | |
| 119 | if (parse_hex_mask(hex, &result, outlen, NULL)) |
| 120 | return NULL; |
| 121 | return result; |
Johannes Berg | 236d419 | 2010-03-24 23:38:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 124 | static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = { |
| 125 | "unspecified", |
| 126 | "IBSS", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 127 | "managed", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 128 | "AP", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 129 | "AP/VLAN", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 130 | "WDS", |
Johannes Berg | 34e78ed | 2009-04-19 15:47:18 +0200 | [diff] [blame] | 131 | "monitor", |
Johannes Berg | a446424 | 2010-09-29 14:45:28 +0200 | [diff] [blame] | 132 | "mesh point", |
| 133 | "P2P-client", |
| 134 | "P2P-GO", |
Johannes Berg | add40bb | 2012-10-16 20:09:38 +0200 | [diff] [blame] | 135 | "P2P-device", |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static char modebuf[100]; |
| 139 | |
| 140 | const char *iftype_name(enum nl80211_iftype iftype) |
| 141 | { |
Johannes Berg | a66b3a3 | 2012-11-06 11:29:13 +0100 | [diff] [blame] | 142 | if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype]) |
Johannes Berg | 541ef42 | 2008-09-16 14:50:11 +0200 | [diff] [blame] | 143 | return ifmodes[iftype]; |
| 144 | sprintf(modebuf, "Unknown mode (%d)", iftype); |
| 145 | return modebuf; |
| 146 | } |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 147 | |
Marcel Holtmann | 9990c1e | 2009-11-14 20:10:21 +0100 | [diff] [blame] | 148 | static const char *commands[NL80211_CMD_MAX + 1] = { |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 149 | /* |
| 150 | * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h |
| 151 | */ |
| 152 | [NL80211_CMD_UNSPEC] = "unspec", |
Johannes Berg | 0633949 | 2010-02-18 11:54:53 +0100 | [diff] [blame] | 153 | [NL80211_CMD_GET_WIPHY] = "get_wiphy", |
| 154 | [NL80211_CMD_SET_WIPHY] = "set_wiphy", |
| 155 | [NL80211_CMD_NEW_WIPHY] = "new_wiphy", |
| 156 | [NL80211_CMD_DEL_WIPHY] = "del_wiphy", |
| 157 | [NL80211_CMD_GET_INTERFACE] = "get_interface", |
| 158 | [NL80211_CMD_SET_INTERFACE] = "set_interface", |
| 159 | [NL80211_CMD_NEW_INTERFACE] = "new_interface", |
| 160 | [NL80211_CMD_DEL_INTERFACE] = "del_interface", |
| 161 | [NL80211_CMD_GET_KEY] = "get_key", |
| 162 | [NL80211_CMD_SET_KEY] = "set_key", |
| 163 | [NL80211_CMD_NEW_KEY] = "new_key", |
| 164 | [NL80211_CMD_DEL_KEY] = "del_key", |
| 165 | [NL80211_CMD_GET_BEACON] = "get_beacon", |
| 166 | [NL80211_CMD_SET_BEACON] = "set_beacon", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 167 | [NL80211_CMD_START_AP] = "start_ap", |
| 168 | [NL80211_CMD_STOP_AP] = "stop_ap", |
Johannes Berg | 0633949 | 2010-02-18 11:54:53 +0100 | [diff] [blame] | 169 | [NL80211_CMD_GET_STATION] = "get_station", |
| 170 | [NL80211_CMD_SET_STATION] = "set_station", |
| 171 | [NL80211_CMD_NEW_STATION] = "new_station", |
| 172 | [NL80211_CMD_DEL_STATION] = "del_station", |
| 173 | [NL80211_CMD_GET_MPATH] = "get_mpath", |
| 174 | [NL80211_CMD_SET_MPATH] = "set_mpath", |
| 175 | [NL80211_CMD_NEW_MPATH] = "new_mpath", |
| 176 | [NL80211_CMD_DEL_MPATH] = "del_mpath", |
| 177 | [NL80211_CMD_SET_BSS] = "set_bss", |
| 178 | [NL80211_CMD_SET_REG] = "set_reg", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 179 | [NL80211_CMD_REQ_SET_REG] = "req_set_reg", |
| 180 | [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config", |
| 181 | [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config", |
Johannes Berg | 0633949 | 2010-02-18 11:54:53 +0100 | [diff] [blame] | 182 | [NL80211_CMD_GET_REG] = "get_reg", |
| 183 | [NL80211_CMD_GET_SCAN] = "get_scan", |
| 184 | [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan", |
| 185 | [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results", |
| 186 | [NL80211_CMD_SCAN_ABORTED] = "scan_aborted", |
| 187 | [NL80211_CMD_REG_CHANGE] = "reg_change", |
| 188 | [NL80211_CMD_AUTHENTICATE] = "authenticate", |
| 189 | [NL80211_CMD_ASSOCIATE] = "associate", |
| 190 | [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate", |
| 191 | [NL80211_CMD_DISASSOCIATE] = "disassociate", |
| 192 | [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure", |
| 193 | [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint", |
| 194 | [NL80211_CMD_JOIN_IBSS] = "join_ibss", |
| 195 | [NL80211_CMD_LEAVE_IBSS] = "leave_ibss", |
| 196 | [NL80211_CMD_TESTMODE] = "testmode", |
| 197 | [NL80211_CMD_CONNECT] = "connect", |
| 198 | [NL80211_CMD_ROAM] = "roam", |
| 199 | [NL80211_CMD_DISCONNECT] = "disconnect", |
| 200 | [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns", |
| 201 | [NL80211_CMD_GET_SURVEY] = "get_survey", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 202 | [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results", |
Johannes Berg | 0633949 | 2010-02-18 11:54:53 +0100 | [diff] [blame] | 203 | [NL80211_CMD_SET_PMKSA] = "set_pmksa", |
| 204 | [NL80211_CMD_DEL_PMKSA] = "del_pmksa", |
| 205 | [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa", |
| 206 | [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel", |
| 207 | [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel", |
| 208 | [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 209 | [NL80211_CMD_REGISTER_FRAME] = "register_frame", |
| 210 | [NL80211_CMD_FRAME] = "frame", |
| 211 | [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status", |
| 212 | [NL80211_CMD_SET_POWER_SAVE] = "set_power_save", |
| 213 | [NL80211_CMD_GET_POWER_SAVE] = "get_power_save", |
| 214 | [NL80211_CMD_SET_CQM] = "set_cqm", |
| 215 | [NL80211_CMD_NOTIFY_CQM] = "notify_cqm", |
Bruno Randolf | 7b96e9e | 2010-05-12 17:27:41 +0900 | [diff] [blame] | 216 | [NL80211_CMD_SET_CHANNEL] = "set_channel", |
Johannes Berg | dcf5703 | 2011-01-17 16:46:03 +0100 | [diff] [blame] | 217 | [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer", |
| 218 | [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel", |
| 219 | [NL80211_CMD_JOIN_MESH] = "join_mesh", |
| 220 | [NL80211_CMD_LEAVE_MESH] = "leave_mesh", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 221 | [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate", |
| 222 | [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate", |
| 223 | [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate", |
| 224 | [NL80211_CMD_GET_WOWLAN] = "get_wowlan", |
| 225 | [NL80211_CMD_SET_WOWLAN] = "set_wowlan", |
| 226 | [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan", |
| 227 | [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan", |
| 228 | [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results", |
| 229 | [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped", |
Johannes Berg | 850ab15 | 2011-07-12 13:03:23 +0200 | [diff] [blame] | 230 | [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload", |
Johannes Berg | 02aef46 | 2012-06-19 15:38:01 +0200 | [diff] [blame] | 231 | [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate", |
| 232 | [NL80211_CMD_TDLS_OPER] = "tdls_oper", |
| 233 | [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt", |
| 234 | [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame", |
| 235 | [NL80211_CMD_PROBE_CLIENT] = "probe_client", |
| 236 | [NL80211_CMD_REGISTER_BEACONS] = "register_beacons", |
| 237 | [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame", |
| 238 | [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map", |
| 239 | [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify", |
Johannes Berg | 65d0795 | 2012-11-06 11:31:00 +0100 | [diff] [blame] | 240 | [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device", |
| 241 | [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device", |
| 242 | [NL80211_CMD_CONN_FAILED] = "conn_failed", |
Johannes Berg | 240aa0e | 2013-05-16 23:33:14 +0200 | [diff] [blame] | 243 | [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate", |
| 244 | [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl", |
| 245 | [NL80211_CMD_RADAR_DETECT] = "radar_detect", |
| 246 | [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features", |
| 247 | [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies", |
| 248 | [NL80211_CMD_FT_EVENT] = "ft_event", |
| 249 | [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start", |
| 250 | [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop", |
Johannes Berg | 0624c45 | 2014-01-24 15:56:12 +0100 | [diff] [blame] | 251 | [NL80211_CMD_GET_COALESCE] = "get_coalesce", |
| 252 | [NL80211_CMD_SET_COALESCE] = "set_coalesce", |
| 253 | [NL80211_CMD_CHANNEL_SWITCH] = "channel_switch", |
| 254 | [NL80211_CMD_VENDOR] = "vendor", |
Marcel Holtmann | 9990c1e | 2009-11-14 20:10:21 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | static char cmdbuf[100]; |
| 258 | |
| 259 | const char *command_name(enum nl80211_commands cmd) |
| 260 | { |
Johannes Berg | 7378039 | 2010-01-29 14:48:54 +0100 | [diff] [blame] | 261 | if (cmd <= NL80211_CMD_MAX && commands[cmd]) |
Marcel Holtmann | 9990c1e | 2009-11-14 20:10:21 +0100 | [diff] [blame] | 262 | return commands[cmd]; |
| 263 | sprintf(cmdbuf, "Unknown command (%d)", cmd); |
| 264 | return cmdbuf; |
| 265 | } |
| 266 | |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 267 | int ieee80211_channel_to_frequency(int chan, enum nl80211_band band) |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 268 | { |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 269 | /* see 802.11 17.3.8.3.2 and Annex J |
| 270 | * there are overlapping channel numbers in 5GHz and 2GHz bands */ |
| 271 | if (chan <= 0) |
| 272 | return 0; /* not supported */ |
| 273 | switch (band) { |
| 274 | case NL80211_BAND_2GHZ: |
| 275 | if (chan == 14) |
| 276 | return 2484; |
| 277 | else if (chan < 14) |
| 278 | return 2407 + chan * 5; |
| 279 | break; |
| 280 | case NL80211_BAND_5GHZ: |
| 281 | if (chan >= 182 && chan <= 196) |
| 282 | return 4000 + chan * 5; |
| 283 | else |
| 284 | return 5000 + chan * 5; |
| 285 | break; |
| 286 | case NL80211_BAND_60GHZ: |
| 287 | if (chan < 5) |
| 288 | return 56160 + chan * 2160; |
| 289 | break; |
| 290 | default: |
| 291 | ; |
| 292 | } |
| 293 | return 0; /* not supported */ |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | int ieee80211_frequency_to_channel(int freq) |
| 297 | { |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 298 | /* see 802.11-2007 17.3.8.3.2 and Annex J */ |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 299 | if (freq == 2484) |
| 300 | return 14; |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 301 | else if (freq < 2484) |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 302 | return (freq - 2407) / 5; |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 303 | else if (freq >= 4910 && freq <= 4980) |
| 304 | return (freq - 4000) / 5; |
| 305 | else if (freq <= 45000) /* DMG band lower limit */ |
| 306 | return (freq - 5000) / 5; |
| 307 | else if (freq >= 58320 && freq <= 64800) |
Vladimir Kondratiev | d56e86b | 2012-07-05 14:36:21 +0300 | [diff] [blame] | 308 | return (freq - 56160) / 2160; |
Bruno Randolf | 58b46da | 2013-09-26 17:45:45 +0100 | [diff] [blame] | 309 | else |
| 310 | return 0; |
Johannes Berg | 379f839 | 2008-12-08 12:53:58 +0100 | [diff] [blame] | 311 | } |
Johannes Berg | 748f848 | 2009-05-24 16:48:17 +0200 | [diff] [blame] | 312 | |
| 313 | void print_ssid_escaped(const uint8_t len, const uint8_t *data) |
| 314 | { |
| 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < len; i++) { |
Johannes Berg | 3f61273 | 2011-10-16 17:04:37 +0200 | [diff] [blame] | 318 | if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\') |
Johannes Berg | 748f848 | 2009-05-24 16:48:17 +0200 | [diff] [blame] | 319 | printf("%c", data[i]); |
Johannes Berg | 3f61273 | 2011-10-16 17:04:37 +0200 | [diff] [blame] | 320 | else if (data[i] == ' ' && |
| 321 | (i != 0 && i != len -1)) |
| 322 | printf(" "); |
Johannes Berg | 748f848 | 2009-05-24 16:48:17 +0200 | [diff] [blame] | 323 | else |
| 324 | printf("\\x%.2x", data[i]); |
| 325 | } |
| 326 | } |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 327 | |
| 328 | static int hex2num(char digit) |
| 329 | { |
| 330 | if (!isxdigit(digit)) |
| 331 | return -1; |
| 332 | if (isdigit(digit)) |
| 333 | return digit - '0'; |
| 334 | return tolower(digit) - 'a' + 10; |
| 335 | } |
| 336 | |
| 337 | static int hex2byte(char *hex) |
| 338 | { |
| 339 | int d1, d2; |
| 340 | |
| 341 | d1 = hex2num(hex[0]); |
| 342 | if (d1 < 0) |
| 343 | return -1; |
| 344 | d2 = hex2num(hex[1]); |
| 345 | if (d2 < 0) |
| 346 | return -1; |
| 347 | return (d1 << 4) | d2; |
| 348 | } |
| 349 | |
| 350 | static char *hex2bin(char *hex, char *buf) |
| 351 | { |
| 352 | char *result = buf; |
| 353 | int d; |
| 354 | |
| 355 | while (hex[0]) { |
| 356 | d = hex2byte(hex); |
| 357 | if (d < 0) |
| 358 | return NULL; |
| 359 | buf[0] = d; |
| 360 | buf++; |
| 361 | hex += 2; |
| 362 | } |
| 363 | |
| 364 | return result; |
| 365 | } |
| 366 | |
| 367 | int parse_keys(struct nl_msg *msg, char **argv, int argc) |
| 368 | { |
| 369 | struct nlattr *keys; |
| 370 | int i = 0; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 371 | bool have_default = false; |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 372 | char keybuf[13]; |
| 373 | |
| 374 | if (!argc) |
| 375 | return 1; |
| 376 | |
| 377 | NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); |
| 378 | |
| 379 | keys = nla_nest_start(msg, NL80211_ATTR_KEYS); |
| 380 | if (!keys) |
| 381 | return -ENOBUFS; |
| 382 | |
| 383 | do { |
| 384 | char *arg = *argv; |
| 385 | int pos = 0, keylen; |
| 386 | struct nlattr *key = nla_nest_start(msg, ++i); |
| 387 | char *keydata; |
| 388 | |
| 389 | if (!key) |
| 390 | return -ENOBUFS; |
| 391 | |
| 392 | if (arg[pos] == 'd') { |
| 393 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); |
| 394 | pos++; |
| 395 | if (arg[pos] == ':') |
| 396 | pos++; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 397 | have_default = true; |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | if (!isdigit(arg[pos])) |
| 401 | goto explain; |
| 402 | NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0'); |
| 403 | if (arg[pos++] != ':') |
| 404 | goto explain; |
| 405 | keydata = arg + pos; |
| 406 | switch (strlen(keydata)) { |
| 407 | case 10: |
| 408 | keydata = hex2bin(keydata, keybuf); |
| 409 | case 5: |
| 410 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01); |
| 411 | keylen = 5; |
| 412 | break; |
| 413 | case 26: |
| 414 | keydata = hex2bin(keydata, keybuf); |
| 415 | case 13: |
| 416 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05); |
| 417 | keylen = 13; |
| 418 | break; |
| 419 | default: |
| 420 | goto explain; |
| 421 | } |
| 422 | |
| 423 | if (!keydata) |
| 424 | goto explain; |
| 425 | |
| 426 | NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata); |
| 427 | |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 428 | argv++; |
| 429 | argc--; |
Johannes Berg | 041581c | 2009-08-16 15:17:38 +0200 | [diff] [blame] | 430 | |
| 431 | /* one key should be TX key */ |
| 432 | if (!have_default && !argc) |
| 433 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); |
| 434 | |
| 435 | nla_nest_end(msg, key); |
Johannes Berg | 51e9bd8 | 2009-07-08 00:44:25 +0200 | [diff] [blame] | 436 | } while (argc); |
| 437 | |
| 438 | nla_nest_end(msg, keys); |
| 439 | |
| 440 | return 0; |
| 441 | nla_put_failure: |
| 442 | return -ENOBUFS; |
| 443 | explain: |
| 444 | fprintf(stderr, "key must be [d:]index:data where\n" |
| 445 | " 'd:' means default (transmit) key\n" |
| 446 | " 'index:' is a single digit (0-3)\n" |
| 447 | " 'data' must be 5 or 13 ascii chars\n" |
| 448 | " or 10 or 26 hex digits\n" |
| 449 | "for example: d:2:6162636465 is the same as d:2:abcde\n"); |
| 450 | return 2; |
| 451 | } |
Luis R. Rodriguez | deb3501 | 2009-12-07 21:13:56 -0500 | [diff] [blame] | 452 | |
Johannes Berg | 7ddfb67 | 2009-12-08 10:11:22 +0100 | [diff] [blame] | 453 | static void print_mcs_index(const __u8 *mcs) |
Luis R. Rodriguez | deb3501 | 2009-12-07 21:13:56 -0500 | [diff] [blame] | 454 | { |
Nick Black | 9fea977 | 2011-12-07 09:03:34 +0100 | [diff] [blame] | 455 | int mcs_bit, prev_bit = -2, prev_cont = 0; |
Luis R. Rodriguez | deb3501 | 2009-12-07 21:13:56 -0500 | [diff] [blame] | 456 | |
Johannes Berg | 04953e9 | 2009-12-08 09:58:20 +0100 | [diff] [blame] | 457 | for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) { |
| 458 | unsigned int mcs_octet = mcs_bit/8; |
| 459 | unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8; |
| 460 | bool mcs_rate_idx_set; |
| 461 | |
| 462 | mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT); |
| 463 | |
| 464 | if (!mcs_rate_idx_set) |
| 465 | continue; |
| 466 | |
| 467 | if (prev_bit != mcs_bit - 1) { |
| 468 | if (prev_bit != -2) |
| 469 | printf("%d, ", prev_bit); |
| 470 | else |
| 471 | printf(" "); |
| 472 | printf("%d", mcs_bit); |
| 473 | prev_cont = 0; |
| 474 | } else if (!prev_cont) { |
| 475 | printf("-"); |
| 476 | prev_cont = 1; |
| 477 | } |
| 478 | |
| 479 | prev_bit = mcs_bit; |
| 480 | } |
| 481 | |
| 482 | if (prev_cont) |
| 483 | printf("%d", prev_bit); |
| 484 | printf("\n"); |
Luis R. Rodriguez | deb3501 | 2009-12-07 21:13:56 -0500 | [diff] [blame] | 485 | } |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * There are only 4 possible values, we just use a case instead of computing it, |
| 489 | * but technically this can also be computed through the formula: |
| 490 | * |
| 491 | * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes |
| 492 | */ |
| 493 | static __u32 compute_ampdu_length(__u8 exponent) |
| 494 | { |
| 495 | switch (exponent) { |
| 496 | case 0: return 8191; /* (2 ^(13 + 0)) -1 */ |
| 497 | case 1: return 16383; /* (2 ^(13 + 1)) -1 */ |
| 498 | case 2: return 32767; /* (2 ^(13 + 2)) -1 */ |
| 499 | case 3: return 65535; /* (2 ^(13 + 3)) -1 */ |
| 500 | default: return 0; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static const char *print_ampdu_space(__u8 space) |
| 505 | { |
| 506 | switch (space) { |
| 507 | case 0: return "No restriction"; |
| 508 | case 1: return "1/4 usec"; |
| 509 | case 2: return "1/2 usec"; |
| 510 | case 3: return "1 usec"; |
| 511 | case 4: return "2 usec"; |
| 512 | case 5: return "4 usec"; |
| 513 | case 6: return "8 usec"; |
| 514 | case 7: return "16 usec"; |
| 515 | default: |
Johannes Berg | 7ae93cd | 2009-12-08 10:19:51 +0100 | [diff] [blame] | 516 | return "BUG (spacing more than 3 bits!)"; |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | void print_ampdu_length(__u8 exponent) |
| 521 | { |
Johannes Berg | 04953e9 | 2009-12-08 09:58:20 +0100 | [diff] [blame] | 522 | __u32 max_ampdu_length; |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame] | 523 | |
| 524 | max_ampdu_length = compute_ampdu_length(exponent); |
| 525 | |
| 526 | if (max_ampdu_length) { |
| 527 | printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n", |
| 528 | max_ampdu_length, exponent); |
Nick Black | 3f362f8 | 2011-12-07 09:04:29 +0100 | [diff] [blame] | 529 | } else { |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame] | 530 | printf("\t\tMaximum RX AMPDU length: unrecognized bytes " |
| 531 | "(exponent: %d)\n", exponent); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | void print_ampdu_spacing(__u8 spacing) |
| 536 | { |
Nick Black | 3f362f8 | 2011-12-07 09:04:29 +0100 | [diff] [blame] | 537 | printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n", |
| 538 | print_ampdu_space(spacing), spacing); |
Luis R. Rodriguez | 0950993 | 2009-12-07 22:05:18 -0500 | [diff] [blame] | 539 | } |
Luis R. Rodriguez | 357c1a5 | 2009-12-07 22:05:42 -0500 | [diff] [blame] | 540 | |
| 541 | void print_ht_capability(__u16 cap) |
| 542 | { |
| 543 | #define PRINT_HT_CAP(_cond, _str) \ |
| 544 | do { \ |
| 545 | if (_cond) \ |
| 546 | printf("\t\t\t" _str "\n"); \ |
| 547 | } while (0) |
| 548 | |
| 549 | printf("\t\tCapabilities: 0x%02x\n", cap); |
| 550 | |
Luis R. Rodriguez | 028c0de | 2010-04-08 16:10:25 -0400 | [diff] [blame] | 551 | PRINT_HT_CAP((cap & BIT(0)), "RX LDPC"); |
Luis R. Rodriguez | 357c1a5 | 2009-12-07 22:05:42 -0500 | [diff] [blame] | 552 | PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40"); |
| 553 | PRINT_HT_CAP(!(cap & BIT(1)), "HT20"); |
| 554 | |
| 555 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save"); |
| 556 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save"); |
| 557 | PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled"); |
| 558 | |
| 559 | PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield"); |
| 560 | PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI"); |
| 561 | PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI"); |
| 562 | PRINT_HT_CAP((cap & BIT(7)), "TX STBC"); |
| 563 | |
| 564 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC"); |
| 565 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream"); |
| 566 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams"); |
| 567 | PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams"); |
| 568 | |
| 569 | PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack"); |
| 570 | |
Christian Lamparter | c79c746 | 2010-06-27 00:51:23 +0200 | [diff] [blame] | 571 | PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes"); |
| 572 | PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes"); |
Luis R. Rodriguez | 357c1a5 | 2009-12-07 22:05:42 -0500 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * For beacons and probe response this would mean the BSS |
| 576 | * does or does not allow the usage of DSSS/CCK HT40. |
| 577 | * Otherwise it means the STA does or does not use |
| 578 | * DSSS/CCK HT40. |
| 579 | */ |
| 580 | PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40"); |
| 581 | PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40"); |
| 582 | |
| 583 | /* BIT(13) is reserved */ |
| 584 | |
| 585 | PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant"); |
| 586 | |
| 587 | PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection"); |
| 588 | #undef PRINT_HT_CAP |
| 589 | } |
Johannes Berg | 7ddfb67 | 2009-12-08 10:11:22 +0100 | [diff] [blame] | 590 | |
| 591 | void print_ht_mcs(const __u8 *mcs) |
| 592 | { |
| 593 | /* As defined in 7.3.2.57.4 Supported MCS Set field */ |
| 594 | unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate; |
| 595 | bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation; |
| 596 | |
John W. Linville | 012f8bf | 2013-06-26 11:07:40 -0400 | [diff] [blame] | 597 | max_rx_supp_data_rate = (mcs[10] & ((mcs[11] & 0x3) << 8)); |
Johannes Berg | 7ddfb67 | 2009-12-08 10:11:22 +0100 | [diff] [blame] | 598 | tx_mcs_set_defined = !!(mcs[12] & (1 << 0)); |
| 599 | tx_mcs_set_equal = !(mcs[12] & (1 << 1)); |
| 600 | tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1; |
| 601 | tx_unequal_modulation = !!(mcs[12] & (1 << 4)); |
| 602 | |
| 603 | if (max_rx_supp_data_rate) |
| 604 | printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate); |
| 605 | /* XXX: else see 9.6.0e.5.3 how to get this I think */ |
| 606 | |
| 607 | if (tx_mcs_set_defined) { |
| 608 | if (tx_mcs_set_equal) { |
Johannes Berg | 2a79feb | 2009-12-08 17:17:19 +0100 | [diff] [blame] | 609 | printf("\t\tHT TX/RX MCS rate indexes supported:"); |
Johannes Berg | 7ddfb67 | 2009-12-08 10:11:22 +0100 | [diff] [blame] | 610 | print_mcs_index(mcs); |
| 611 | } else { |
| 612 | printf("\t\tHT RX MCS rate indexes supported:"); |
| 613 | print_mcs_index(mcs); |
| 614 | |
| 615 | if (tx_unequal_modulation) |
| 616 | printf("\t\tTX unequal modulation supported\n"); |
| 617 | else |
| 618 | printf("\t\tTX unequal modulation not supported\n"); |
| 619 | |
| 620 | printf("\t\tHT TX Max spatial streams: %d\n", |
| 621 | tx_max_num_spatial_streams); |
| 622 | |
| 623 | printf("\t\tHT TX MCS rate indexes supported may differ\n"); |
| 624 | } |
| 625 | } else { |
| 626 | printf("\t\tHT RX MCS rate indexes supported:"); |
| 627 | print_mcs_index(mcs); |
Johannes Berg | 089bb35 | 2009-12-08 17:59:07 +0100 | [diff] [blame] | 628 | printf("\t\tHT TX MCS rate indexes are undefined\n"); |
Johannes Berg | 7ddfb67 | 2009-12-08 10:11:22 +0100 | [diff] [blame] | 629 | } |
| 630 | } |
Johannes Berg | 54eb161 | 2012-11-12 13:07:18 +0100 | [diff] [blame] | 631 | |
| 632 | void print_vht_info(__u32 capa, const __u8 *mcs) |
| 633 | { |
| 634 | __u16 tmp; |
| 635 | int i; |
| 636 | |
| 637 | printf("\t\tVHT Capabilities (0x%.8x):\n", capa); |
| 638 | |
| 639 | #define PRINT_VHT_CAPA(_bit, _str) \ |
| 640 | do { \ |
| 641 | if (capa & BIT(_bit)) \ |
| 642 | printf("\t\t\t" _str "\n"); \ |
| 643 | } while (0) |
| 644 | |
| 645 | printf("\t\t\tMax MPDU length: "); |
| 646 | switch (capa & 3) { |
| 647 | case 0: printf("3895\n"); break; |
| 648 | case 1: printf("7991\n"); break; |
| 649 | case 2: printf("11454\n"); break; |
| 650 | case 3: printf("(reserved)\n"); |
| 651 | } |
| 652 | printf("\t\t\tSupported Channel Width: "); |
| 653 | switch ((capa >> 2) & 3) { |
| 654 | case 0: printf("neither 160 nor 80+80\n"); break; |
| 655 | case 1: printf("160 MHz\n"); break; |
| 656 | case 2: printf("160 MHz, 80+80 MHz\n"); break; |
| 657 | case 3: printf("(reserved)\n"); |
| 658 | } |
| 659 | PRINT_VHT_CAPA(4, "RX LDPC"); |
| 660 | PRINT_VHT_CAPA(5, "short GI (80 MHz)"); |
| 661 | PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)"); |
| 662 | PRINT_VHT_CAPA(7, "TX STBC"); |
| 663 | /* RX STBC */ |
| 664 | PRINT_VHT_CAPA(11, "SU Beamformer"); |
| 665 | PRINT_VHT_CAPA(12, "SU Beamformee"); |
| 666 | /* compressed steering */ |
| 667 | /* # of sounding dimensions */ |
| 668 | PRINT_VHT_CAPA(19, "MU Beamformer"); |
| 669 | PRINT_VHT_CAPA(20, "MU Beamformee"); |
| 670 | PRINT_VHT_CAPA(21, "VHT TXOP PS"); |
| 671 | PRINT_VHT_CAPA(22, "+HTC-VHT"); |
| 672 | /* max A-MPDU */ |
| 673 | /* VHT link adaptation */ |
Maxime Bizon | 7527105 | 2014-02-17 22:52:02 +0100 | [diff] [blame] | 674 | PRINT_VHT_CAPA(28, "RX antenna pattern consistency"); |
| 675 | PRINT_VHT_CAPA(29, "TX antenna pattern consistency"); |
Johannes Berg | 54eb161 | 2012-11-12 13:07:18 +0100 | [diff] [blame] | 676 | |
| 677 | printf("\t\tVHT RX MCS set:\n"); |
| 678 | tmp = mcs[0] | (mcs[1] << 8); |
| 679 | for (i = 1; i <= 8; i++) { |
| 680 | printf("\t\t\t%d streams: ", i); |
| 681 | switch ((tmp >> ((i-1)*2) ) & 3) { |
| 682 | case 0: printf("MCS 0-7\n"); break; |
| 683 | case 1: printf("MCS 0-8\n"); break; |
| 684 | case 2: printf("MCS 0-9\n"); break; |
| 685 | case 3: printf("not supported\n"); break; |
| 686 | } |
| 687 | } |
| 688 | tmp = mcs[2] | (mcs[3] << 8); |
| 689 | printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff); |
| 690 | |
| 691 | printf("\t\tVHT TX MCS set:\n"); |
| 692 | tmp = mcs[4] | (mcs[5] << 8); |
| 693 | for (i = 1; i <= 8; i++) { |
| 694 | printf("\t\t\t%d streams: ", i); |
| 695 | switch ((tmp >> ((i-1)*2) ) & 3) { |
| 696 | case 0: printf("MCS 0-7\n"); break; |
| 697 | case 1: printf("MCS 0-8\n"); break; |
| 698 | case 2: printf("MCS 0-9\n"); break; |
| 699 | case 3: printf("not supported\n"); break; |
| 700 | } |
| 701 | } |
| 702 | tmp = mcs[6] | (mcs[7] << 8); |
| 703 | printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff); |
| 704 | } |
Janusz Dziedzic | 492354d | 2014-08-27 09:58:28 +0200 | [diff] [blame] | 705 | |
| 706 | void iw_hexdump(const char *prefix, const __u8 *buf, size_t size) |
| 707 | { |
| 708 | int i; |
| 709 | |
| 710 | printf("%s: ", prefix); |
| 711 | for (i = 0; i < size; i++) { |
| 712 | if (i && i % 16 == 0) |
| 713 | printf("\n%s: ", prefix); |
| 714 | printf("%02x ", buf[i]); |
| 715 | } |
| 716 | printf("\n\n"); |
| 717 | } |