blob: dae9ec3f98b8dff0afc28b16ced6d6fd4fa64f2a [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];
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +053084 char ver_buf[256];
Jouni Malinen3aa72862019-05-29 23:14:51 +030085 int res;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020086#endif /* __linux__ */
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++;
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 Lazar9b85c412019-01-31 15:20:52 +0200156 host_fw_ver[0] = '\0';
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200157 if (get_driver_type(dut) == DRIVER_WCN ||
158 get_driver_type(dut) == DRIVER_LINUX_WCN) {
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +0530159 get_ver("iwpriv wlan0 version", host_fw_ver,
160 sizeof(host_fw_ver));
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200161 } else if (get_driver_type(dut) == DRIVER_WIL6210) {
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200162 struct ethtool_drvinfo drvinfo;
163 struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
164 int fd; /* socket suitable for ethtool ioctl */
Pradeep Reddy Potteti551b9862017-01-24 17:09:44 +0530165
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200166 memset(&drvinfo, 0, sizeof(drvinfo));
167 drvinfo.cmd = ETHTOOL_GDRVINFO;
168
169 memset(&ifr, 0, sizeof(ifr));
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200170 strlcpy(ifr.ifr_name, get_main_ifname(dut),
Amarnath Hullur Subramanyam67212372019-05-30 18:27:24 -0700171 sizeof(ifr.ifr_name));
Alexei Avshalom Lazar9b85c412019-01-31 15:20:52 +0200172
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 Malinen3aa72862019-05-29 23:14:51 +0300185 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 Malinencd4e3c32015-10-29 12:39:56 +0200198 version = ver_buf;
199 }
200#endif /* __linux__ */
201
Jouni Malinen5db3b102016-08-04 12:27:18 +0300202 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 Malinencd4e3c32015-10-29 12:39:56 +0200210
211 send_resp(dut, conn, SIGMA_COMPLETE, resp);
Jouni Malinencd444902019-02-19 01:20:45 +0200212 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200213}
214
215
216static 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 Malinencd444902019-02-19 01:20:45 +0200224static enum sigma_cmd_result cmd_device_list_interfaces(struct sigma_dut *dut,
225 struct sigma_conn *conn,
226 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200227{
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 Malinencd444902019-02-19 01:20:45 +0200237 return ERROR_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200238
Jouni Malinen016ae6c2019-11-04 17:00:01 +0200239 snprintf(resp, sizeof(resp), "interfaceType,802.11,interfaceID,%s",
240 get_main_ifname(dut));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200241 send_resp(dut, conn, SIGMA_COMPLETE, resp);
Jouni Malinencd444902019-02-19 01:20:45 +0200242 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200243}
244
245
246void 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}