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); |
| 30 | wpa_command(get_main_ifname(), buf); |
| 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 | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 85 | #endif /* __linux__ */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 86 | char resp[512]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 87 | |
| 88 | #ifdef __linux__ |
| 89 | { |
| 90 | char path[128]; |
| 91 | struct stat s; |
| 92 | FILE *f; |
| 93 | char compat_ver[128]; |
| 94 | char wpa_supplicant_ver[128]; |
| 95 | char hostapd_ver[128]; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 96 | char host_fw_ver[128]; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 97 | |
| 98 | snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", |
| 99 | get_main_ifname()); |
| 100 | if (stat(path, &s) == 0) { |
| 101 | ssize_t res; |
| 102 | char *pos; |
| 103 | snprintf(path, sizeof(path), |
| 104 | "/sys/class/net/%s/device/driver", |
| 105 | get_main_ifname()); |
| 106 | res = readlink(path, path, sizeof(path)); |
| 107 | if (res < 0) |
| 108 | model = "Linux/"; |
| 109 | else { |
| 110 | if (res >= (int) sizeof(path)) |
| 111 | res = sizeof(path) - 1; |
| 112 | path[res] = '\0'; |
| 113 | pos = strrchr(path, '/'); |
| 114 | if (pos == NULL) |
| 115 | pos = path; |
| 116 | else |
| 117 | pos++; |
| 118 | snprintf(model_buf, sizeof(model_buf), |
| 119 | "Linux/%s", pos); |
| 120 | model = model_buf; |
| 121 | } |
| 122 | } else |
| 123 | model = "Linux"; |
| 124 | |
| 125 | /* TODO: get version from wpa_supplicant (+ driver via wpa_s) |
| 126 | */ |
| 127 | |
| 128 | f = fopen("/sys/module/compat/parameters/" |
| 129 | "backported_kernel_version", "r"); |
| 130 | if (f == NULL) |
| 131 | f = fopen("/sys/module/compat/parameters/" |
| 132 | "compat_version", "r"); |
| 133 | if (f) { |
| 134 | if (fgets(compat_ver, sizeof(compat_ver), f) == NULL) |
| 135 | compat_ver[0] = '\0'; |
| 136 | else |
| 137 | first_line(compat_ver); |
| 138 | fclose(f); |
| 139 | } else |
| 140 | compat_ver[0] = '\0'; |
| 141 | |
| 142 | get_ver("./hostapd -v 2>&1", hostapd_ver, sizeof(hostapd_ver)); |
| 143 | if (hostapd_ver[0] == '\0') |
| 144 | get_ver("hostapd -v 2>&1", hostapd_ver, |
| 145 | sizeof(hostapd_ver)); |
| 146 | get_ver("./wpa_supplicant -v", wpa_supplicant_ver, |
| 147 | sizeof(wpa_supplicant_ver)); |
| 148 | if (wpa_supplicant_ver[0] == '\0') |
| 149 | get_ver("wpa_supplicant -v", wpa_supplicant_ver, |
| 150 | sizeof(wpa_supplicant_ver)); |
| 151 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 152 | host_fw_ver[0] = '\0'; |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 153 | if (get_driver_type() == DRIVER_WCN || |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 154 | get_driver_type() == DRIVER_LINUX_WCN) { |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 155 | get_ver("iwpriv wlan0 version", host_fw_ver, |
| 156 | sizeof(host_fw_ver)); |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 157 | } else if (get_driver_type() == DRIVER_WIL6210) { |
| 158 | struct ethtool_drvinfo drvinfo; |
| 159 | struct ifreq ifr; /* ifreq suitable for ethtool ioctl */ |
| 160 | int fd; /* socket suitable for ethtool ioctl */ |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 161 | |
Alexei Avshalom Lazar | 9b85c41 | 2019-01-31 15:20:52 +0200 | [diff] [blame] | 162 | memset(&drvinfo, 0, sizeof(drvinfo)); |
| 163 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
| 164 | |
| 165 | memset(&ifr, 0, sizeof(ifr)); |
| 166 | strcpy(ifr.ifr_name, get_main_ifname()); |
| 167 | |
| 168 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 169 | if (fd < 0) |
| 170 | fd = socket(AF_NETLINK, SOCK_RAW, |
| 171 | NETLINK_GENERIC); |
| 172 | if (fd >= 0) { |
| 173 | ifr.ifr_data = (void *) &drvinfo; |
| 174 | if (ioctl(fd, SIOCETHTOOL, &ifr) == 0) |
| 175 | strlcpy(host_fw_ver, drvinfo.fw_version, |
| 176 | sizeof(host_fw_ver)); |
| 177 | close(fd); |
| 178 | } |
| 179 | } |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 180 | snprintf(ver_buf, sizeof(ver_buf), |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 181 | "drv=%s%s%s%s%s%s%s/sigma=" SIGMA_DUT_VER "%s%s", |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 182 | compat_ver, |
| 183 | wpa_supplicant_ver[0] ? "/wpas=" : "", |
| 184 | wpa_supplicant_ver, |
| 185 | hostapd_ver[0] ? "/hapd=" : "", |
| 186 | hostapd_ver, |
Pradeep Reddy Potteti | 551b986 | 2017-01-24 17:09:44 +0530 | [diff] [blame] | 187 | host_fw_ver[0] ? "/wlan=" : "", |
| 188 | host_fw_ver, |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 189 | dut->version ? "@" : "", |
| 190 | dut->version ? dut->version : ""); |
| 191 | version = ver_buf; |
| 192 | } |
| 193 | #endif /* __linux__ */ |
| 194 | |
Jouni Malinen | 5db3b10 | 2016-08-04 12:27:18 +0300 | [diff] [blame] | 195 | if (dut->vendor_name) |
| 196 | vendor = dut->vendor_name; |
| 197 | if (dut->model_name) |
| 198 | model = dut->model_name; |
| 199 | if (dut->version_name) |
| 200 | version = dut->version_name; |
| 201 | snprintf(resp, sizeof(resp), "vendor,%s,model,%s,version,%s", |
| 202 | vendor, model, version); |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 203 | |
| 204 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 205 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | static int check_device_list_interfaces(struct sigma_cmd *cmd) |
| 210 | { |
| 211 | if (get_param(cmd, "interfaceType") == NULL) |
| 212 | return -1; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 217 | static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut, |
| 218 | struct sigma_conn *conn, |
| 219 | struct sigma_cmd *cmd) |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 220 | { |
| 221 | const char *type; |
| 222 | char resp[200]; |
| 223 | |
| 224 | type = get_param(cmd, "interfaceType"); |
| 225 | if (type == NULL) |
| 226 | return -1; |
| 227 | sigma_dut_print(dut, DUT_MSG_DEBUG, "device_list_interfaces - " |
| 228 | "interfaceType=%s", type); |
| 229 | if (strcmp(type, "802.11") != 0) |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 230 | return ERROR_SEND_STATUS; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 231 | |
| 232 | snprintf(resp, sizeof(resp), "interfaceType,802.11," |
| 233 | "interfaceID,%s", get_main_ifname()); |
| 234 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
Jouni Malinen | cd44490 | 2019-02-19 01:20:45 +0200 | [diff] [blame] | 235 | return STATUS_SENT; |
Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | void basic_register_cmds(void) |
| 240 | { |
| 241 | sigma_dut_reg_cmd("ca_get_version", NULL, cmd_ca_get_version); |
| 242 | sigma_dut_reg_cmd("device_get_info", NULL, cmd_device_get_info); |
| 243 | sigma_dut_reg_cmd("device_list_interfaces", |
| 244 | check_device_list_interfaces, |
| 245 | cmd_device_list_interfaces); |
| 246 | } |