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]; |
Vamsi Krishna | 5f4e003 | 2019-11-18 22:35:57 +0530 | [diff] [blame] | 84 | char ver_buf[512]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 85 | #endif /* __linux__ */ |
Jouni Malinen | 77dda64 | 2020-01-07 11:21:55 +0200 | [diff] [blame] | 86 | int res; |
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++; |
Jouni Malinen | 77dda64 | 2020-01-07 11:21:55 +0200 | [diff] [blame] | 122 | res = snprintf(model_buf, sizeof(model_buf), |
| 123 | "Linux/%s", pos); |
| 124 | if (res >= 0 && res < sizeof(model_buf)) |
| 125 | model = model_buf; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 126 | } |
| 127 | } else |
| 128 | model = "Linux"; |
| 129 | |
| 130 | /* TODO: get version from wpa_supplicant (+ driver via wpa_s) |
| 131 | */ |
| 132 | |
| 133 | f = fopen("/sys/module/compat/parameters/" |
| 134 | "backported_kernel_version", "r"); |
| 135 | if (f == NULL) |
| 136 | f = fopen("/sys/module/compat/parameters/" |
| 137 | "compat_version", "r"); |
| 138 | if (f) { |
| 139 | if (fgets(compat_ver, sizeof(compat_ver), f) == NULL) |
| 140 | compat_ver[0] = '\0'; |
| 141 | else |
| 142 | first_line(compat_ver); |
| 143 | fclose(f); |
| 144 | } else |
| 145 | compat_ver[0] = '\0'; |
| 146 | |
| 147 | get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver)); |
| 148 | if (hostapd_ver[0] == '\0') |
| 149 | get_ver("hostapd -v 2>&1", hostapd_ver, |
| 150 | sizeof(hostapd_ver)); |
| 151 | get_ver("./wpa_supplicant -v", wpa_supplicant_ver, |
| 152 | sizeof(wpa_supplicant_ver)); |
| 153 | if (wpa_supplicant_ver[0] == '\0') |
| 154 | get_ver("wpa_supplicant -v", wpa_supplicant_ver, |
| 155 | sizeof(wpa_supplicant_ver)); |
| 156 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 157 | host_fw_ver[0] = '\0'; |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame] | 158 | if (get_driver_type(dut) == DRIVER_WCN || |
| 159 | get_driver_type(dut) == DRIVER_LINUX_WCN) { |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 160 | get_ver("iwpriv wlan0 version", host_fw_ver, |
| 161 | sizeof(host_fw_ver)); |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame] | 162 | } else if (get_driver_type(dut) == DRIVER_WIL6210) { |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 163 | struct ethtool_drvinfo drvinfo; |
| 164 | struct ifreq ifr; /* ifreq suitable for ethtool ioctl */ |
| 165 | int fd; /* socket suitable for ethtool ioctl */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 166 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 167 | memset(&drvinfo, 0, sizeof(drvinfo)); |
| 168 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
| 169 | |
| 170 | memset(&ifr, 0, sizeof(ifr)); |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame] | 171 | strlcpy(ifr.ifr_name, get_main_ifname(dut), |
Amarnath Hullur Subramanyam | 6721237 | 2019-05-30 18:27:24 -0700 | [diff] [blame] | 172 | sizeof(ifr.ifr_name)); |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 173 | |
| 174 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 175 | if (fd < 0) |
| 176 | fd = socket(AF_NETLINK, SOCK_RAW, |
| 177 | NETLINK_GENERIC); |
| 178 | if (fd >= 0) { |
| 179 | ifr.ifr_data = (void *) &drvinfo; |
| 180 | if (ioctl(fd, SIOCETHTOOL, &ifr) == 0) |
| 181 | strlcpy(host_fw_ver, drvinfo.fw_version, |
| 182 | sizeof(host_fw_ver)); |
| 183 | close(fd); |
| 184 | } |
| 185 | } |
Jouni Malinen | 3aa7286 | 2019-05-29 23:14:51 +0300 | [diff] [blame] | 186 | res = snprintf(ver_buf, sizeof(ver_buf), |
| 187 | "drv=%s%s%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s", |
| 188 | compat_ver, |
| 189 | wpa_supplicant_ver[0] ? "/wpas=" : "", |
| 190 | wpa_supplicant_ver, |
| 191 | hostapd_ver[0] ? "/hapd=" : "", |
| 192 | hostapd_ver, |
| 193 | host_fw_ver[0] ? "/wlan=" : "", |
| 194 | host_fw_ver, |
| 195 | dut->version ? "@" : "", |
| 196 | dut->version ? dut->version : ""); |
| 197 | if (res < 0 || res >= sizeof(ver_buf)) |
| 198 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 199 | version = ver_buf; |
| 200 | } |
| 201 | #endif /* __linux__ */ |
| 202 | |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 203 | if (dut->vendor_name) |
| 204 | vendor = dut->vendor_name; |
| 205 | if (dut->model_name) |
| 206 | model = dut->model_name; |
| 207 | if (dut->version_name) |
| 208 | version = dut->version_name; |
Jouni Malinen | 77dda64 | 2020-01-07 11:21:55 +0200 | [diff] [blame] | 209 | res = snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s", |
| 210 | vendor, model, version); |
| 211 | if (res < 0 || res >= sizeof(resp)) |
| 212 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 213 | |
| 214 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 215 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | static int check_device_list_interfaces(struct sigma_cmd *cmd) |
| 220 | { |
| 221 | if (get_param(cmd, "interfaceType") == NULL) |
| 222 | return -1; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 227 | static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut, |
| 228 | struct sigma_conn *conn, |
| 229 | struct sigma_cmd *cmd) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 230 | { |
Jouni Malinen | b21f054 | 2019-11-04 17:53:38 +0200 | [diff] [blame] | 231 | const char *type, *band; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 232 | char resp[200]; |
| 233 | |
| 234 | type = get_param(cmd, "interfaceType"); |
| 235 | if (type == NULL) |
| 236 | return -1; |
| 237 | sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - " |
| 238 | "interfaceType=%s", type); |
| 239 | if (strcmp(type, "802.11") != 0) |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 240 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 241 | |
Jouni Malinen | b21f054 | 2019-11-04 17:53:38 +0200 | [diff] [blame] | 242 | band = get_param(cmd, "band"); |
| 243 | if (!band) { |
| 244 | } else if (strcasecmp(band, "24g") == 0) { |
| 245 | dut->use_5g = 0; |
| 246 | } else if (strcasecmp(band, "5g") == 0) { |
| 247 | dut->use_5g = 1; |
| 248 | } else { |
| 249 | send_resp(dut, conn, SIGMA_COMPLETE, |
| 250 | "errorCode,Unsupported band value"); |
| 251 | return STATUS_SENT_ERROR; |
| 252 | } |
Jouni Malinen | 016ae6c | 2019-11-04 17:00:01 +0200 | [diff] [blame] | 253 | snprintf(resp, sizeof(resp), "interfaceType,802.11,interfaceID,%s", |
| 254 | get_main_ifname(dut)); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 255 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 256 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | void basic_register_cmds(void) |
| 261 | { |
| 262 | sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version); |
| 263 | sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info); |
| 264 | sigma_dut_reg_cmd("device_list_interfaces", |
| 265 | check_device_list_interfaces, |
| 266 | cmd_device_list_interfaces); |
| 267 | } |