Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP) |
| 3 | * Copyright (c) 2010, Atheros Communications, Inc. |
Jouni Malinen | 9d7e31d | 2017-12-22 18:55:04 +0200 | [diff] [blame] | 4 | * Copyright (c) 2011-2014, 2016-2017, Qualcomm Atheros, Inc. |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 5 | * All Rights Reserved. |
| 6 | * Licensed under the Clear BSD license. See README for more details. |
| 7 | */ |
| 8 | |
| 9 | #include "sigma_dut.h" |
| 10 | #ifdef __linux__ |
| 11 | #include <sys/stat.h> |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 12 | #include <linux/ethtool.h> |
| 13 | #include <linux/netlink.h> |
| 14 | #include <linux/sockios.h> |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 15 | #endif /* __linux__ */ |
| 16 | #include "wpa_helpers.h" |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 17 | #include <sys/ioctl.h> |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 18 | |
| 19 | |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 20 | static enum sigma_cmd_result cmd_ca_get_version(struct sigma_dut *dut, |
| 21 | struct sigma_conn *conn, |
| 22 | struct sigma_cmd *cmd) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 23 | { |
| 24 | const char *info; |
| 25 | |
| 26 | info = get_param(cmd, "TestInfo"); |
| 27 | if (info) { |
| 28 | char buf[200]; |
| 29 | snprintf(buf, sizeof(buf), "NOTE CAPI:TestInfo:%s", info); |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 30 | wpa_command(get_main_ifname(dut), buf); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | send_resp(dut, conn, SIGMA_COMPLETE, "version,1.0"); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 34 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | |
| 38 | #ifdef __linux__ |
| 39 | |
| 40 | static void first_line(char *s) |
| 41 | { |
| 42 | while (*s) { |
| 43 | if (*s == '\r' || *s == '\n') { |
| 44 | *s = '\0'; |
| 45 | return; |
| 46 | } |
| 47 | s++; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
Jouni Malinen | c54710d | 2019-01-23 12:34:21 +0200 | [diff] [blame] | 52 | void get_ver(const char *cmd, char *buf, size_t buflen) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 53 | { |
| 54 | FILE *f; |
| 55 | char *pos; |
| 56 | |
| 57 | buf[0] = '\0'; |
| 58 | f = popen(cmd, "r"); |
| 59 | if (f == NULL) |
| 60 | return; |
| 61 | if (fgets(buf, buflen, f)) |
| 62 | first_line(buf); |
| 63 | pclose(f); |
| 64 | |
| 65 | pos = strstr(buf, " v"); |
| 66 | if (pos == NULL) |
| 67 | buf[0] = '\0'; |
| 68 | else |
| 69 | memmove(buf, pos + 1, strlen(pos)); |
| 70 | } |
| 71 | |
| 72 | #endif /* __linux__ */ |
| 73 | |
| 74 | |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 75 | static enum sigma_cmd_result cmd_device_get_info(struct sigma_dut *dut, |
| 76 | struct sigma_conn *conn, |
| 77 | struct sigma_cmd *cmd) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 78 | { |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 79 | const char *vendor = "Qualcomm Atheros"; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 80 | const char *model = "N/A"; |
| 81 | const char *version = "N/A"; |
| 82 | #ifdef __linux__ |
| 83 | char model_buf[128]; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 84 | char ver_buf[256]; |
Jouni Malinen | 3aa7286 | 2019-05-29 23:14:51 +0300 | [diff] [blame] | 85 | int res; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 86 | #endif /* __linux__ */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 87 | char resp[512]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 88 | |
| 89 | #ifdef __linux__ |
| 90 | { |
Jouni Malinen | e39cd56 | 2019-05-29 23:39:56 +0300 | [diff] [blame] | 91 | char fname[128], path[128]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 92 | struct stat s; |
| 93 | FILE *f; |
| 94 | char compat_ver[128]; |
| 95 | char wpa_supplicant_ver[128]; |
| 96 | char hostapd_ver[128]; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 97 | char host_fw_ver[128]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 98 | |
| 99 | snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 100 | get_main_ifname(dut)); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 101 | if (stat(path, &s) == 0) { |
| 102 | ssize_t res; |
| 103 | char *pos; |
Jouni Malinen | e39cd56 | 2019-05-29 23:39:56 +0300 | [diff] [blame] | 104 | |
| 105 | res = snprintf(fname, sizeof(fname), |
| 106 | "/sys/class/net/%s/device/driver", |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 107 | get_main_ifname(dut)); |
Jouni Malinen | e39cd56 | 2019-05-29 23:39:56 +0300 | [diff] [blame] | 108 | if (res < 0 || res >= sizeof(fname)) { |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 109 | model = "Linux/"; |
Jouni Malinen | e39cd56 | 2019-05-29 23:39:56 +0300 | [diff] [blame] | 110 | } else if ((res = readlink(fname, path, |
| 111 | sizeof(path))) < 0) { |
| 112 | model = "Linux/"; |
| 113 | } else { |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 114 | if (res >= (int) sizeof(path)) |
| 115 | res = sizeof(path) - 1; |
| 116 | path[res] = '\0'; |
| 117 | pos = strrchr(path, '/'); |
| 118 | if (pos == NULL) |
| 119 | pos = path; |
| 120 | else |
| 121 | pos++; |
| 122 | snprintf(model_buf, sizeof(model_buf), |
| 123 | "Linux/%s", pos); |
| 124 | model = model_buf; |
| 125 | } |
| 126 | } else |
| 127 | model = "Linux"; |
| 128 | |
| 129 | /* TODO: get version from wpa_supplicant (+ driver via wpa_s) |
| 130 | */ |
| 131 | |
| 132 | f = fopen("/sys/module/compat/parameters/" |
| 133 | "backported_kernel_version", "r"); |
| 134 | if (f == NULL) |
| 135 | f = fopen("/sys/module/compat/parameters/" |
| 136 | "compat_version", "r"); |
| 137 | if (f) { |
| 138 | if (fgets(compat_ver, sizeof(compat_ver), f) == NULL) |
| 139 | compat_ver[0] = '\0'; |
| 140 | else |
| 141 | first_line(compat_ver); |
| 142 | fclose(f); |
| 143 | } else |
| 144 | compat_ver[0] = '\0'; |
| 145 | |
| 146 | get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver)); |
| 147 | if (hostapd_ver[0] == '\0') |
| 148 | get_ver("hostapd -v 2>&1", hostapd_ver, |
| 149 | sizeof(hostapd_ver)); |
| 150 | get_ver("./wpa_supplicant -v", wpa_supplicant_ver, |
| 151 | sizeof(wpa_supplicant_ver)); |
| 152 | if (wpa_supplicant_ver[0] == '\0') |
| 153 | get_ver("wpa_supplicant -v", wpa_supplicant_ver, |
| 154 | sizeof(wpa_supplicant_ver)); |
| 155 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 156 | host_fw_ver[0] = '\0'; |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 157 | if (get_driver_type(dut) == DRIVER_WCN || |
| 158 | get_driver_type(dut) == DRIVER_LINUX_WCN) { |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 159 | get_ver("iwpriv wlan0 version", host_fw_ver, |
| 160 | sizeof(host_fw_ver)); |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 161 | } else if (get_driver_type(dut) == DRIVER_WIL6210) { |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 162 | struct ethtool_drvinfo drvinfo; |
| 163 | struct ifreq ifr; /* ifreq suitable for ethtool ioctl */ |
| 164 | int fd; /* socket suitable for ethtool ioctl */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 165 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 166 | memset(&drvinfo, 0, sizeof(drvinfo)); |
| 167 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
| 168 | |
| 169 | memset(&ifr, 0, sizeof(ifr)); |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 170 | strlcpy(ifr.ifr_name, get_main_ifname(dut), |
Amarnath Hullur Subramanyam | 6721237 | 2019-05-30 18:27:24 -0700 | [diff] [blame] | 171 | sizeof(ifr.ifr_name)); |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 172 | |
| 173 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 174 | if (fd < 0) |
| 175 | fd = socket(AF_NETLINK, SOCK_RAW, |
| 176 | NETLINK_GENERIC); |
| 177 | if (fd >= 0) { |
| 178 | ifr.ifr_data = (void *) &drvinfo; |
| 179 | if (ioctl(fd, SIOCETHTOOL, &ifr) == 0) |
| 180 | strlcpy(host_fw_ver, drvinfo.fw_version, |
| 181 | sizeof(host_fw_ver)); |
| 182 | close(fd); |
| 183 | } |
| 184 | } |
Jouni Malinen | 3aa7286 | 2019-05-29 23:14:51 +0300 | [diff] [blame] | 185 | res = snprintf(ver_buf, sizeof(ver_buf), |
| 186 | "drv=%s%s%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s", |
| 187 | compat_ver, |
| 188 | wpa_supplicant_ver[0] ? "/wpas=" : "", |
| 189 | wpa_supplicant_ver, |
| 190 | hostapd_ver[0] ? "/hapd=" : "", |
| 191 | hostapd_ver, |
| 192 | host_fw_ver[0] ? "/wlan=" : "", |
| 193 | host_fw_ver, |
| 194 | dut->version ? "@" : "", |
| 195 | dut->version ? dut->version : ""); |
| 196 | if (res < 0 || res >= sizeof(ver_buf)) |
| 197 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 198 | version = ver_buf; |
| 199 | } |
| 200 | #endif /* __linux__ */ |
| 201 | |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 202 | if (dut->vendor_name) |
| 203 | vendor = dut->vendor_name; |
| 204 | if (dut->model_name) |
| 205 | model = dut->model_name; |
| 206 | if (dut->version_name) |
| 207 | version = dut->version_name; |
| 208 | snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s", |
| 209 | vendor, model, version); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 210 | |
| 211 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 212 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | static int check_device_list_interfaces(struct sigma_cmd *cmd) |
| 217 | { |
| 218 | if (get_param(cmd, "interfaceType") == NULL) |
| 219 | return -1; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 224 | static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut, |
| 225 | struct sigma_conn *conn, |
| 226 | struct sigma_cmd *cmd) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 227 | { |
| 228 | const char *type; |
| 229 | char resp[200]; |
| 230 | |
| 231 | type = get_param(cmd, "interfaceType"); |
| 232 | if (type == NULL) |
| 233 | return -1; |
| 234 | sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - " |
| 235 | "interfaceType=%s", type); |
| 236 | if (strcmp(type, "802.11") != 0) |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 237 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 238 | |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame^] | 239 | snprintf(resp, sizeof(resp), "interfaceType,802.11,interfaceID,%s", |
| 240 | get_main_ifname(dut)); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 241 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 242 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
| 246 | void basic_register_cmds(void) |
| 247 | { |
| 248 | sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version); |
| 249 | sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info); |
| 250 | sigma_dut_reg_cmd("device_list_interfaces", |
| 251 | check_device_list_interfaces, |
| 252 | cmd_device_list_interfaces); |
| 253 | } |