blob: f0ac45f6a62c578fb78e4bc273bc1b07c62f8d55 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP)
3 * Copyright (c) 2010, Atheros Communications, Inc.
Jouni Malinen9d7e31d2017-12-22 18:55:04 +02004 * Copyright (c) 2011-2014, 2016-2017, Qualcomm Atheros, Inc.
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005 * 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 Lazar9b85c412019-01-31 15:20:52 +020012#include <linux/ethtool.h>
13#include <linux/netlink.h>
14#include <linux/sockios.h>
Jouni Malinencd4e3c32015-10-29 12:39:56 +020015#endif /* __linux__ */
16#include "wpa_helpers.h"
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +020017#include <sys/ioctl.h>
Jouni Malinencd4e3c32015-10-29 12:39:56 +020018
19
Jouni Malinencd444902019-02-19 01:20:45 +020020static enum sigma_cmd_result cmd_ca_get_version(struct sigma_dut *dut,
21 struct sigma_conn *conn,
22 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020023{
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 Malinen016ae6c2019-11-04 17:00:01 +020030 wpa_command(get_main_ifname(dut), buf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +020031 }
32
33 send_resp(dut, conn, SIGMA_COMPLETE, "version,1.0");
Jouni Malinencd444902019-02-19 01:20:45 +020034 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020035}
36
37
38#ifdef __linux__
39
40static 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 Malinenc54710d2019-01-23 12:34:21 +020052void get_ver(const char *cmd, char *buf, size_t buflen)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020053{
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 Malinencd444902019-02-19 01:20:45 +020075static enum sigma_cmd_result cmd_device_get_info(struct sigma_dut *dut,
76 struct sigma_conn *conn,
77 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020078{
Jouni Malinen5db3b102016-08-04 12:27:18 +030079 const char *vendor = "Qualcomm Atheros";
Jouni Malinencd4e3c32015-10-29 12:39:56 +020080 const char *model = "N/A";
81 const char *version = "N/A";
82#ifdef __linux__
83 char model_buf[128];
Vamsi Krishna5f4e0032019-11-18 22:35:57 +053084 char ver_buf[512];
Jouni Malinencd4e3c32015-10-29 12:39:56 +020085#endif /* __linux__ */
Jouni Malinen77dda642020-01-07 11:21:55 +020086 int res;
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +053087 char resp[512];
Jouni Malinencd4e3c32015-10-29 12:39:56 +020088
89#ifdef __linux__
90 {
Jouni Malinene39cd562019-05-29 23:39:56 +030091 char fname[128], path[128];
Jouni Malinencd4e3c32015-10-29 12:39:56 +020092 struct stat s;
93 FILE *f;
94 char compat_ver[128];
95 char wpa_supplicant_ver[128];
96 char hostapd_ver[128];
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +053097 char host_fw_ver[128];
Jouni Malinencd4e3c32015-10-29 12:39:56 +020098
99 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211",
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200100 get_main_ifname(dut));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200101 if (stat(path, &s) == 0) {
102 ssize_t res;
103 char *pos;
Jouni Malinene39cd562019-05-29 23:39:56 +0300104
105 res = snprintf(fname, sizeof(fname),
106 "/sys/class/net/%s/device/driver",
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200107 get_main_ifname(dut));
Jouni Malinene39cd562019-05-29 23:39:56 +0300108 if (res < 0 || res >= sizeof(fname)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200109 model = "Linux/";
Jouni Malinene39cd562019-05-29 23:39:56 +0300110 } else if ((res = readlink(fname, path,
111 sizeof(path))) < 0) {
112 model = "Linux/";
113 } else {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200114 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 Malinen77dda642020-01-07 11:21:55 +0200122 res = snprintf(model_buf, sizeof(model_buf),
123 "Linux/%s", pos);
124 if (res >= 0 && res < sizeof(model_buf))
125 model = model_buf;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200126 }
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 Lazar9b85c412019-01-31 15:20:52 +0200157 host_fw_ver[0] = '\0';
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200158 if (get_driver_type(dut) == DRIVER_WCN ||
159 get_driver_type(dut) == DRIVER_LINUX_WCN) {
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +0530160 get_ver("iwpriv wlan0 version", host_fw_ver,
161 sizeof(host_fw_ver));
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200162 } else if (get_driver_type(dut) == DRIVER_WIL6210) {
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200163 struct ethtool_drvinfo drvinfo;
164 struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
165 int fd; /* socket suitable for ethtool ioctl */
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +0530166
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200167 memset(&drvinfo, 0, sizeof(drvinfo));
168 drvinfo.cmd = ETHTOOL_GDRVINFO;
169
170 memset(&ifr, 0, sizeof(ifr));
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200171 strlcpy(ifr.ifr_name, get_main_ifname(dut),
Amarnath Hullur Subramanyam67212372019-05-30 18:27:24 -0700172 sizeof(ifr.ifr_name));
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200173
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 Malinen3aa72862019-05-29 23:14:51 +0300186 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 Malinencd4e3c32015-10-29 12:39:56 +0200199 version = ver_buf;
200 }
201#endif /* __linux__ */
202
Jouni Malinen5db3b102016-08-04 12:27:18 +0300203 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 Malinen77dda642020-01-07 11:21:55 +0200209 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 Malinencd4e3c32015-10-29 12:39:56 +0200213
214 send_resp(dut, conn, SIGMA_COMPLETE, resp);
Jouni Malinencd444902019-02-19 01:20:45 +0200215 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200216}
217
218
219static 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 Malinencd444902019-02-19 01:20:45 +0200227static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut,
228 struct sigma_conn *conn,
229 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200230{
Jouni Malinenb21f0542019-11-04 17:53:38 +0200231 const char *type, *band;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200232 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 Malinencd444902019-02-19 01:20:45 +0200240 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200241
Jouni Malinenb21f0542019-11-04 17:53:38 +0200242 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 Malinen016ae6c2019-11-04 17:00:01 +0200253 snprintf(resp, sizeof(resp), "interfaceType,802.11,interfaceID,%s",
254 get_main_ifname(dut));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200255 send_resp(dut, conn, SIGMA_COMPLETE, resp);
Jouni Malinencd444902019-02-19 01:20:45 +0200256 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200257}
258
259
260void 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}