| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP/sniffer) |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011-2013, 2017, Qualcomm Atheros, Inc. |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 4 | * Copyright (c) 2018-2019, The Linux Foundation |
| 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" |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 10 | #include <ctype.h> |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 11 | #include "miracast.h" |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 12 | #include <sys/wait.h> |
| 13 | #include "wpa_ctrl.h" |
| 14 | #include "wpa_helpers.h" |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 15 | |
| 16 | |
| Jouni Malinen | fee3c4a | 2019-06-20 01:06:26 +0300 | [diff] [blame] | 17 | extern char *sigma_cert_path; |
| 18 | |
| 19 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 20 | static enum sigma_cmd_result cmd_dev_send_frame(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 | { |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 24 | #ifdef MIRACAST |
| 25 | const char *program = get_param(cmd, "Program"); |
| 26 | |
| 27 | if (program && (strcasecmp(program, "WFD") == 0 || |
| 28 | strcasecmp(program, "DisplayR2") == 0)) |
| 29 | return miracast_dev_send_frame(dut, conn, cmd); |
| 30 | #endif /* MIRACAST */ |
| 31 | |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 32 | if (dut->mode == SIGMA_MODE_STATION || |
| 33 | dut->mode == SIGMA_MODE_UNKNOWN) { |
| 34 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert " |
| 35 | "dev_send_frame to sta_send_frame"); |
| 36 | return cmd_sta_send_frame(dut, conn, cmd); |
| 37 | } |
| 38 | |
| 39 | if (dut->mode == SIGMA_MODE_AP) { |
| 40 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert " |
| 41 | "dev_send_frame to ap_send_frame"); |
| 42 | return cmd_ap_send_frame(dut, conn, cmd); |
| 43 | } |
| 44 | |
| 45 | #ifdef CONFIG_WLANTEST |
| 46 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert dev_send_frame to " |
| 47 | "wlantest_send_frame"); |
| 48 | return cmd_wlantest_send_frame(dut, conn, cmd); |
| 49 | #else /* CONFIG_WLANTEST */ |
| 50 | send_resp(dut, conn, SIGMA_ERROR, |
| 51 | "errorCode,Unsupported dev_send_frame"); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 52 | return STATUS_SENT; |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 53 | #endif /* CONFIG_WLANTEST */ |
| 54 | } |
| 55 | |
| 56 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 57 | static enum sigma_cmd_result cmd_dev_set_parameter(struct sigma_dut *dut, |
| 58 | struct sigma_conn *conn, |
| 59 | struct sigma_cmd *cmd) |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 60 | { |
| 61 | const char *device = get_param(cmd, "Device"); |
| 62 | |
| 63 | if (device && strcasecmp(device, "STA") == 0) { |
| 64 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert " |
| 65 | "dev_set_parameter to sta_set_parameter"); |
| 66 | return cmd_sta_set_parameter(dut, conn, cmd); |
| 67 | } |
| 68 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 69 | return INVALID_SEND_STATUS; |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 73 | static enum sigma_cmd_result sta_server_cert_trust(struct sigma_dut *dut, |
| 74 | struct sigma_conn *conn, |
| 75 | const char *val) |
| 76 | { |
| Jouni Malinen | fee3c4a | 2019-06-20 01:06:26 +0300 | [diff] [blame] | 77 | char buf[200]; |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 78 | struct wpa_ctrl *ctrl = NULL; |
| 79 | int e; |
| 80 | char resp[200]; |
| 81 | int num_disconnected = 0; |
| 82 | |
| 83 | strlcpy(resp, "ServerCertTrustResult,Accepted", sizeof(resp)); |
| 84 | |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 85 | if (strcasecmp(val, "Accept") != 0 && strcasecmp(val, "Reject") != 0) { |
| 86 | sigma_dut_print(dut, DUT_MSG_INFO, |
| 87 | "Unknown ServerCertTrust value '%s'", val); |
| 88 | return INVALID_SEND_STATUS; |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| Jouni Malinen | fee3c4a | 2019-06-20 01:06:26 +0300 | [diff] [blame] | 91 | snprintf(buf, sizeof(buf), "%s/uosc-disabled", sigma_cert_path); |
| 92 | if (file_exists(buf)) { |
| 93 | strlcpy(resp, |
| 94 | "ServerCertTrustResult,OverrideNotAllowed,Reason,UOSC disabled on device", |
| 95 | sizeof(resp)); |
| 96 | goto done; |
| 97 | } |
| 98 | |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 99 | if (!dut->server_cert_hash[0]) { |
| 100 | strlcpy(resp, |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 101 | "ServerCertTrustResult,OverrideNotAllowed,Reason,No server certificate stored", |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 102 | sizeof(resp)); |
| 103 | goto done; |
| 104 | } |
| 105 | |
| 106 | if (dut->sta_tod_policy) { |
| 107 | strlcpy(resp, |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 108 | "ServerCertTrustResult,OverrideNotAllowed,Reason,TOD policy", |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 109 | sizeof(resp)); |
| 110 | goto done; |
| 111 | } |
| 112 | |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 113 | if (strcasecmp(val, "Accept") != 0) { |
| 114 | strlcpy(resp, "ServerCertTrustResult,Rejected", sizeof(resp)); |
| 115 | goto done; |
| 116 | } |
| 117 | |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 118 | snprintf(buf, sizeof(buf), "hash://server/sha256/%s", |
| 119 | dut->server_cert_hash); |
| 120 | if (set_network_quoted(get_station_ifname(), dut->infra_network_id, |
| 121 | "ca_cert", buf) < 0) { |
| 122 | strlcpy(resp, |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 123 | "ServerCertTrustResult,OverrideNotAllowed,Reason,Could not configure server certificate hash for the network profile", |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 124 | sizeof(resp)); |
| 125 | goto done; |
| 126 | } |
| 127 | |
| 128 | wpa_command(get_station_ifname(), "DISCONNECT"); |
| 129 | snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", dut->infra_network_id); |
| 130 | if (wpa_command(get_station_ifname(), buf) < 0) { |
| 131 | sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select " |
| 132 | "network id %d on %s", |
| 133 | dut->infra_network_id, |
| 134 | get_station_ifname()); |
| 135 | strlcpy(resp, |
| 136 | "ServerCertTrustResult,Accepted,Result,Could not request reconnection", |
| 137 | sizeof(resp)); |
| 138 | goto done; |
| 139 | } |
| 140 | |
| 141 | ctrl = open_wpa_mon(get_station_ifname()); |
| 142 | if (!ctrl) |
| 143 | goto done; |
| 144 | |
| 145 | for (e = 0; e < 20; e++) { |
| 146 | const char *events[] = { |
| 147 | "CTRL-EVENT-EAP-TLS-CERT-ERROR", |
| 148 | "CTRL-EVENT-DISCONNECTED", |
| 149 | "CTRL-EVENT-CONNECTED", |
| 150 | NULL |
| 151 | }; |
| 152 | char buf[1024]; |
| 153 | int res; |
| 154 | |
| 155 | res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf)); |
| 156 | if (res < 0) { |
| 157 | strlcpy(resp, |
| 158 | "ServerCertTrustResult,Accepted,Result,Association did not complete", |
| 159 | sizeof(resp)); |
| 160 | goto done; |
| 161 | } |
| 162 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Connection event: %s", |
| 163 | buf); |
| 164 | |
| 165 | if (strstr(buf, "CTRL-EVENT-EAP-TLS-CERT-ERROR")) { |
| 166 | strlcpy(resp, |
| Jouni Malinen | 6a3005f | 2019-06-13 23:39:34 +0300 | [diff] [blame] | 167 | "ServerCertTrustResult,Accepted,Result,TLS server certificate validation failed with updated profile", |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 168 | sizeof(resp)); |
| 169 | goto done; |
| 170 | } |
| 171 | |
| 172 | if (strstr(buf, "CTRL-EVENT-DISCONNECTED")) { |
| 173 | num_disconnected++; |
| 174 | |
| 175 | if (num_disconnected > 2) { |
| 176 | strlcpy(resp, |
| 177 | "ServerCertTrustResult,Accepted,Result,Connection failed", |
| 178 | sizeof(resp)); |
| 179 | goto done; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (strstr(buf, "CTRL-EVENT-CONNECTED")) { |
| 184 | strlcpy(resp, |
| 185 | "ServerCertTrustResult,Accepted,Result,Connected", |
| 186 | sizeof(resp)); |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | done: |
| 192 | if (ctrl) { |
| 193 | wpa_ctrl_detach(ctrl); |
| 194 | wpa_ctrl_close(ctrl); |
| 195 | } |
| 196 | |
| 197 | send_resp(dut, conn, SIGMA_COMPLETE, resp); |
| 198 | return STATUS_SENT; |
| 199 | } |
| 200 | |
| 201 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 202 | static enum sigma_cmd_result cmd_dev_exec_action(struct sigma_dut *dut, |
| 203 | struct sigma_conn *conn, |
| 204 | struct sigma_cmd *cmd) |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 205 | { |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 206 | const char *program = get_param(cmd, "Program"); |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 207 | const char *val; |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 208 | |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 209 | #ifdef MIRACAST |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 210 | if (program && (strcasecmp(program, "WFD") == 0 || |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 211 | strcasecmp(program, "DisplayR2") == 0)) { |
| 212 | if (get_param(cmd, "interface") == NULL) |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 213 | return INVALID_SEND_STATUS; |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 214 | return miracast_dev_exec_action(dut, conn, cmd); |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 215 | } |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 216 | #endif /* MIRACAST */ |
| 217 | |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 218 | if (program && strcasecmp(program, "DPP") == 0) |
| 219 | return dpp_dev_exec_action(dut, conn, cmd); |
| 220 | |
| Jouni Malinen | 134fe3c | 2019-06-12 04:16:49 +0300 | [diff] [blame] | 221 | val = get_param(cmd, "ServerCertTrust"); |
| 222 | if (val) |
| 223 | return sta_server_cert_trust(dut, conn, val); |
| 224 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 225 | return ERROR_SEND_STATUS; |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 229 | static enum sigma_cmd_result cmd_dev_configure_ie(struct sigma_dut *dut, |
| 230 | struct sigma_conn *conn, |
| 231 | struct sigma_cmd *cmd) |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 232 | { |
| 233 | const char *ie_name = get_param(cmd, "IE_Name"); |
| 234 | const char *contents = get_param(cmd, "Contents"); |
| 235 | |
| 236 | if (!ie_name || !contents) |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 237 | return INVALID_SEND_STATUS; |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 238 | |
| 239 | if (strcasecmp(ie_name, "RSNE") != 0) { |
| 240 | send_resp(dut, conn, SIGMA_ERROR, |
| 241 | "errorCode,Unsupported IE_Name value"); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 242 | return STATUS_SENT; |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | free(dut->rsne_override); |
| 246 | dut->rsne_override = strdup(contents); |
| 247 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 248 | return dut->rsne_override ? SUCCESS_SEND_STATUS : ERROR_SEND_STATUS; |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 252 | static enum sigma_cmd_result cmd_dev_ble_action(struct sigma_dut *dut, |
| 253 | struct sigma_conn *conn, |
| 254 | struct sigma_cmd *cmd) |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 255 | { |
| 256 | #ifdef ANDROID |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 257 | const char *ble_op = get_param(cmd, "BLEOp"); |
| 258 | const char *prog = get_param(cmd, "Prog"); |
| 259 | const char *service_name = get_param(cmd, "ServiceName"); |
| 260 | const char *ble_role = get_param(cmd, "BLERole"); |
| 261 | const char *discovery_type = get_param(cmd, "DiscoveryType"); |
| 262 | const char *msg_type = get_param(cmd, "messagetype"); |
| 263 | const char *action = get_param(cmd, "action"); |
| 264 | const char *M2Transmit = get_param(cmd, "M2Transmit"); |
| 265 | char *argv[17]; |
| 266 | pid_t pid; |
| 267 | |
| 268 | if (prog && ble_role && action && msg_type) { |
| 269 | send_resp(dut, conn, SIGMA_COMPLETE, |
| 270 | "OrgID,0x00,TransDataHeader,0x00,BloomFilterElement,NULL"); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 271 | return STATUS_SENT; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 272 | } |
| 273 | if (!ble_op || !prog || !service_name || !ble_role || !discovery_type) { |
| 274 | sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid arguments"); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 275 | return INVALID_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if ((strcasecmp(prog, "NAN") != 0)) { |
| 279 | sigma_dut_print(dut, DUT_MSG_ERROR, "Program %s not supported", |
| 280 | prog); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 281 | return INVALID_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (strcasecmp(ble_role, "seeker") != 0 && |
| 285 | strcasecmp(ble_role, "provider") != 0 && |
| 286 | strcasecmp(ble_role, "browser") != 0) { |
| 287 | sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid BLERole: %s", |
| 288 | ble_role); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 289 | return INVALID_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if (strcasecmp(discovery_type, "active") != 0 && |
| 293 | strcasecmp(discovery_type, "passive") != 0) { |
| 294 | sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid DiscoveryType: %s", |
| 295 | discovery_type); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 296 | return INVALID_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (!M2Transmit) |
| 300 | M2Transmit = "disable"; |
| 301 | |
| 302 | argv[0] = "am"; |
| 303 | argv[1] = "start"; |
| 304 | argv[2] = "-n"; |
| 305 | argv[3] = "org.codeaurora.nanservicediscovery/org.codeaurora.nanservicediscovery.MainActivity"; |
| 306 | argv[4] = "--es"; |
| 307 | argv[5] = "service"; |
| 308 | argv[6] = (char *) service_name; |
| 309 | argv[7] = "--es"; |
| 310 | argv[8] = "role"; |
| 311 | argv[9] = (char *) ble_role; |
| 312 | argv[10] = "--es"; |
| 313 | argv[11] = "scantype"; |
| 314 | argv[12] = (char *) discovery_type; |
| 315 | argv[13] = "--es"; |
| 316 | argv[14] = "M2Transmit"; |
| 317 | argv[15] = (char *) M2Transmit; |
| 318 | argv[16] = NULL; |
| 319 | |
| 320 | pid = fork(); |
| 321 | if (pid == -1) { |
| 322 | sigma_dut_print(dut, DUT_MSG_ERROR, "fork: %s", |
| 323 | strerror(errno)); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 324 | return ERROR_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (pid == 0) { |
| 328 | execv("/system/bin/am", argv); |
| 329 | sigma_dut_print(dut, DUT_MSG_ERROR, "execv: %s", |
| 330 | strerror(errno)); |
| 331 | exit(0); |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 332 | return ERROR_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | dut->nanservicediscoveryinprogress = 1; |
| 336 | #endif /* ANDROID */ |
| 337 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 338 | return SUCCESS_SEND_STATUS; |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 342 | /* Runtime ID must contain only numbers */ |
| 343 | static int is_runtime_id_valid(struct sigma_dut *dut, const char *val) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | for (i = 0; val[i] != '\0'; i++) { |
| 348 | if (!isdigit(val[i])) { |
| 349 | sigma_dut_print(dut, DUT_MSG_DEBUG, |
| 350 | "Invalid Runtime_ID %s", val); |
| 351 | return 0; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | static int build_log_dir(struct sigma_dut *dut, char *dir, size_t dir_size) |
| 360 | { |
| 361 | int res; |
| Jouni Malinen | ce791a3 | 2019-06-20 01:19:00 +0300 | [diff] [blame^] | 362 | const char *vendor = dut->vendor_name; |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 363 | int i; |
| 364 | |
| Jouni Malinen | ce791a3 | 2019-06-20 01:19:00 +0300 | [diff] [blame^] | 365 | if (!vendor) |
| 366 | return -1; |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 367 | |
| 368 | if (dut->log_file_dir) { |
| 369 | res = snprintf(dir, dir_size, "%s/%s", dut->log_file_dir, |
| 370 | vendor); |
| 371 | } else { |
| 372 | #ifdef ANDROID |
| 373 | res = snprintf(dir, dir_size, "/data/vendor/wifi/%s", |
| 374 | vendor); |
| 375 | #else /* ANDROID */ |
| 376 | res = snprintf(dir, dir_size, "/var/log/%s", vendor); |
| 377 | #endif /* ANDROID */ |
| 378 | } |
| 379 | |
| 380 | if (res < 0 || res >= dir_size) |
| 381 | return -1; |
| 382 | |
| 383 | /* Check for valid vendor name in log dir path since the log dir |
| 384 | * (/var/log/vendor) is deleted in dev_stop routine. This check is to |
| 385 | * avoid any unintended file deletion. |
| 386 | */ |
| 387 | for (i = 0; vendor[i] != '\0'; i++) { |
| 388 | if (!isalpha(vendor[i])) { |
| 389 | sigma_dut_print(dut, DUT_MSG_DEBUG, |
| 390 | "Invalid char %c in vendor name %s", |
| 391 | vendor[i], vendor); |
| 392 | return -1; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /* User has to redirect wpa_supplicant logs to the following file. */ |
| 401 | #ifndef WPA_SUPPLICANT_LOG_FILE |
| 402 | #define WPA_SUPPLICANT_LOG_FILE "/var/log/supplicant_log/wpa_log.txt" |
| 403 | #endif /* WPA_SUPPLICANT_LOG_FILE */ |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 404 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 405 | static enum sigma_cmd_result cmd_dev_start_test(struct sigma_dut *dut, |
| 406 | struct sigma_conn *conn, |
| 407 | struct sigma_cmd *cmd) |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 408 | { |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 409 | const char *val; |
| 410 | char buf[250]; |
| 411 | char dir[200]; |
| 412 | FILE *supp_log; |
| 413 | int res; |
| 414 | |
| 415 | val = get_param(cmd, "Runtime_ID"); |
| 416 | if (!(val && is_runtime_id_valid(dut, val))) |
| 417 | return INVALID_SEND_STATUS; |
| 418 | |
| Jouni Malinen | ce791a3 | 2019-06-20 01:19:00 +0300 | [diff] [blame^] | 419 | if (!dut->vendor_name) { |
| 420 | sigma_dut_print(dut, DUT_MSG_INFO, |
| 421 | "Log collection not supported without vendor name specified on the command line (-N)"); |
| 422 | return SUCCESS_SEND_STATUS; |
| 423 | } |
| 424 | |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 425 | if (build_log_dir(dut, dir, sizeof(dir)) < 0) |
| 426 | return ERROR_SEND_STATUS; |
| 427 | |
| 428 | supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r"); |
| 429 | if (!supp_log) { |
| 430 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 431 | "Failed to open wpa_log file %s", |
| 432 | WPA_SUPPLICANT_LOG_FILE); |
| 433 | } else { |
| 434 | /* Get the wpa_supplicant log file size */ |
| 435 | if (fseek(supp_log, 0, SEEK_END)) |
| 436 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 437 | "Failed to get file size for read"); |
| 438 | else |
| 439 | dut->wpa_log_size = ftell(supp_log); |
| 440 | |
| 441 | fclose(supp_log); |
| 442 | } |
| 443 | |
| 444 | strlcpy(dut->dev_start_test_runtime_id, val, |
| 445 | sizeof(dut->dev_start_test_runtime_id)); |
| 446 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Runtime_ID %s", |
| 447 | dut->dev_start_test_runtime_id); |
| 448 | |
| 449 | run_system_wrapper(dut, "rm -rf %s", dir); |
| 450 | run_system_wrapper(dut, "mkdir -p %s", dir); |
| 451 | |
| 452 | #ifdef ANDROID |
| 453 | run_system_wrapper(dut, "logcat -v time > %s/logcat_%s.txt &", |
| 454 | dir, dut->dev_start_test_runtime_id); |
| 455 | #else /* ANDROID */ |
| 456 | /* Open log file for sigma_dut logs. This is not needed for Android, as |
| 457 | * we are already collecting logcat. */ |
| 458 | res = snprintf(buf, sizeof(buf), "%s/sigma_%s.txt", dir, |
| 459 | dut->dev_start_test_runtime_id); |
| 460 | if (res >= 0 && res < sizeof(buf)) { |
| 461 | if (dut->log_file_fd) |
| 462 | fclose(dut->log_file_fd); |
| 463 | |
| 464 | dut->log_file_fd = fopen(buf, "a"); |
| 465 | if (!dut->log_file_fd) |
| 466 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 467 | "Failed to create sigma_dut log %s", |
| 468 | buf); |
| 469 | } |
| 470 | |
| 471 | run_system_wrapper(dut, "killall -9 cnss_diag_lite"); |
| 472 | run_system_wrapper(dut, |
| 473 | "cnss_diag_lite -c -x 31 > %s/cnss_diag_id_%s.txt &", |
| 474 | dir, dut->dev_start_test_runtime_id); |
| 475 | #endif /* ANDROID */ |
| 476 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 477 | return SUCCESS_SEND_STATUS; |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 481 | static int is_allowed_char(char ch) |
| 482 | { |
| 483 | return strchr("./-_", ch) != NULL; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | static int is_destpath_valid(struct sigma_dut *dut, const char *val) |
| 488 | { |
| 489 | int i; |
| 490 | |
| 491 | for (i = 0; val[i] != '\0'; i++) { |
| 492 | if (!(isalnum(val[i]) || is_allowed_char(val[i]))) { |
| 493 | sigma_dut_print(dut, DUT_MSG_DEBUG, |
| 494 | "Invalid char %c in destpath %s", |
| 495 | val[i], val); |
| 496 | return 0; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return 1; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | #ifndef ANDROID |
| 505 | #define SUPP_LOG_BUFF_SIZE 4 * 1024 |
| 506 | |
| 507 | static int save_supplicant_log(struct sigma_dut *dut) |
| 508 | { |
| 509 | char dir[200]; |
| 510 | char buf[300]; |
| 511 | FILE *wpa_log = NULL; |
| 512 | FILE *supp_log; |
| 513 | char *buff_ptr = NULL; |
| 514 | unsigned int file_size; |
| 515 | unsigned int file_size_orig; |
| 516 | int status = -1, res; |
| 517 | |
| 518 | if (build_log_dir(dut, dir, sizeof(dir)) < 0) |
| 519 | return -1; |
| 520 | |
| 521 | res = snprintf(buf, sizeof(buf), "%s/wpa_supplicant_log_%s.txt", dir, |
| 522 | dut->dev_start_test_runtime_id); |
| 523 | if (res < 0 || res >= sizeof(buf)) |
| 524 | return -1; |
| 525 | |
| 526 | supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r"); |
| 527 | if (!supp_log) { |
| 528 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 529 | "Failed to open wpa_log file %s", |
| 530 | WPA_SUPPLICANT_LOG_FILE); |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /* Get the wpa_supplicant log file size */ |
| 535 | if (fseek(supp_log, 0, SEEK_END)) { |
| 536 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 537 | "Failed to get file size for read"); |
| 538 | goto exit; |
| 539 | } |
| 540 | file_size_orig = ftell(supp_log); |
| 541 | |
| 542 | if (file_size_orig < dut->wpa_log_size) { |
| 543 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 544 | "file size err, new size %u, old size %u", |
| 545 | file_size_orig, dut->wpa_log_size); |
| 546 | goto exit; |
| 547 | } |
| 548 | |
| 549 | /* Get the wpa_supplicant file size for current test */ |
| 550 | file_size = file_size_orig - dut->wpa_log_size; |
| 551 | |
| 552 | wpa_log = fopen(buf, "w"); |
| 553 | if (!wpa_log) { |
| 554 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 555 | "Failed to create tmp wpa_log file %s", buf); |
| 556 | goto exit; |
| 557 | } |
| 558 | |
| 559 | if (fseek(supp_log, dut->wpa_log_size, SEEK_SET)) { |
| 560 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 561 | "Failed to set wpa_log file ptr for read"); |
| 562 | goto exit; |
| 563 | } |
| 564 | |
| 565 | buff_ptr = malloc(SUPP_LOG_BUFF_SIZE); |
| 566 | if (!buff_ptr) { |
| 567 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 568 | "Failed to alloc buffer of size %d", |
| 569 | SUPP_LOG_BUFF_SIZE); |
| 570 | goto exit; |
| 571 | } |
| 572 | |
| 573 | /* Read wpa_supplicant log file in 4K byte chunks */ |
| 574 | do { |
| 575 | unsigned int num_bytes_to_read; |
| 576 | unsigned int bytes_read; |
| 577 | |
| 578 | num_bytes_to_read = (file_size > SUPP_LOG_BUFF_SIZE) ? |
| 579 | SUPP_LOG_BUFF_SIZE : file_size; |
| 580 | bytes_read = fread(buff_ptr, 1, num_bytes_to_read, supp_log); |
| 581 | if (!bytes_read) { |
| 582 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 583 | "Failed to read wpa_supplicant log"); |
| 584 | goto exit; |
| 585 | } |
| 586 | if (bytes_read != num_bytes_to_read) { |
| 587 | sigma_dut_print(dut, DUT_MSG_DEBUG, |
| 588 | "wpa_supplicant log read err, read %d, num_bytes_to_read %d", |
| 589 | bytes_read, num_bytes_to_read); |
| 590 | goto exit; |
| 591 | } |
| 592 | fwrite(buff_ptr, 1, bytes_read, wpa_log); |
| 593 | file_size -= bytes_read; |
| 594 | } while (file_size > 0); |
| 595 | status = 0; |
| 596 | |
| 597 | exit: |
| 598 | if (wpa_log) |
| 599 | fclose(wpa_log); |
| 600 | fclose(supp_log); |
| 601 | free(buff_ptr); |
| 602 | |
| 603 | return status; |
| 604 | } |
| 605 | #endif /* !ANDROID */ |
| 606 | |
| 607 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 608 | static enum sigma_cmd_result cmd_dev_stop_test(struct sigma_dut *dut, |
| 609 | struct sigma_conn *conn, |
| 610 | struct sigma_cmd *cmd) |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 611 | { |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 612 | const char *val; |
| 613 | char buf[300]; |
| 614 | char out_file[100]; |
| 615 | char dir[200]; |
| 616 | int res; |
| 617 | |
| Jouni Malinen | ce791a3 | 2019-06-20 01:19:00 +0300 | [diff] [blame^] | 618 | if (!dut->vendor_name) { |
| 619 | sigma_dut_print(dut, DUT_MSG_INFO, |
| 620 | "Log collection not supported without vendor name specified on the command line (-N)"); |
| 621 | return SUCCESS_SEND_STATUS; |
| 622 | } |
| 623 | |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 624 | val = get_param(cmd, "Runtime_ID"); |
| 625 | if (!val || strcmp(val, dut->dev_start_test_runtime_id) != 0) { |
| 626 | sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid runtime id"); |
| 627 | return ERROR_SEND_STATUS; |
| 628 | } |
| 629 | |
| 630 | if (build_log_dir(dut, dir, sizeof(dir)) < 0) |
| 631 | return ERROR_SEND_STATUS; |
| 632 | |
| 633 | #ifdef ANDROID |
| 634 | /* Copy all cnss_diag logs to dir */ |
| 635 | run_system_wrapper(dut, "cp -a /data/vendor/wifi/wlan_logs/* %s", dir); |
| 636 | #else /* ANDROID */ |
| 637 | if (dut->log_file_fd) { |
| 638 | fclose(dut->log_file_fd); |
| 639 | dut->log_file_fd = NULL; |
| 640 | } |
| 641 | if (save_supplicant_log(dut)) |
| 642 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 643 | "Failed to save wpa_supplicant log"); |
| 644 | #endif /* ANDROID */ |
| 645 | |
| 646 | res = snprintf(out_file, sizeof(out_file), "%s_%s_%s.tar.gz", |
| Jouni Malinen | ce791a3 | 2019-06-20 01:19:00 +0300 | [diff] [blame^] | 647 | dut->vendor_name, |
| Kiran Kumar Lokere | 0128bf9 | 2019-04-08 18:36:16 -0700 | [diff] [blame] | 648 | dut->model_name ? dut->model_name : "Unknown", |
| 649 | dut->dev_start_test_runtime_id); |
| 650 | if (res < 0 || res >= sizeof(out_file)) |
| 651 | return ERROR_SEND_STATUS; |
| 652 | |
| 653 | if (run_system_wrapper(dut, "tar -czvf %s/../%s %s", dir, out_file, |
| 654 | dir) < 0) { |
| 655 | sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to create tar: %s", |
| 656 | buf); |
| 657 | return ERROR_SEND_STATUS; |
| 658 | } |
| 659 | |
| 660 | val = get_param(cmd, "destpath"); |
| 661 | if (!(val && is_destpath_valid(dut, val))) { |
| 662 | sigma_dut_print(dut, DUT_MSG_DEBUG, "Invalid path for TFTP %s", |
| 663 | val); |
| 664 | return ERROR_SEND_STATUS; |
| 665 | } |
| 666 | |
| 667 | res = snprintf(buf, sizeof(buf), "tftp %s -c put %s/%s %s/%s", |
| 668 | inet_ntoa(conn->addr.sin_addr), dir, out_file, val, |
| 669 | out_file); |
| 670 | if (res < 0 || res >= sizeof(buf)) |
| 671 | return ERROR_SEND_STATUS; |
| 672 | if (run_system_wrapper(dut, buf) < 0) { |
| 673 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 674 | "TFTP file transfer failed: %s", buf); |
| 675 | return ERROR_SEND_STATUS; |
| 676 | } |
| 677 | sigma_dut_print(dut, DUT_MSG_DEBUG, "TFTP file transfer: %s", buf); |
| 678 | snprintf(buf, sizeof(buf), "filename,%s", out_file); |
| 679 | send_resp(dut, conn, SIGMA_COMPLETE, buf); |
| 680 | run_system_wrapper(dut, "rm -f %s/../%s", dir, out_file); |
| 681 | run_system_wrapper(dut, "rm -rf %s", dir); |
| 682 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 683 | return SUCCESS_SEND_STATUS; |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 687 | static enum sigma_cmd_result cmd_dev_get_log(struct sigma_dut *dut, |
| 688 | struct sigma_conn *conn, |
| 689 | struct sigma_cmd *cmd) |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 690 | { |
| Jouni Malinen | a0bf244 | 2019-02-19 12:03:26 +0200 | [diff] [blame] | 691 | return SUCCESS_SEND_STATUS; |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 695 | static int req_intf(struct sigma_cmd *cmd) |
| 696 | { |
| 697 | return get_param(cmd, "interface") == NULL ? -1 : 0; |
| 698 | } |
| 699 | |
| 700 | |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 701 | static int req_role_svcname(struct sigma_cmd *cmd) |
| 702 | { |
| 703 | if (!get_param(cmd, "BLERole")) |
| 704 | return -1; |
| 705 | if (get_param(cmd, "BLEOp") && !get_param(cmd, "ServiceName")) |
| 706 | return -1; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 711 | static int req_intf_prog(struct sigma_cmd *cmd) |
| 712 | { |
| 713 | if (get_param(cmd, "interface") == NULL) |
| 714 | return -1; |
| 715 | if (get_param(cmd, "program") == NULL) |
| 716 | return -1; |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 721 | static int req_prog(struct sigma_cmd *cmd) |
| 722 | { |
| 723 | if (get_param(cmd, "program") == NULL) |
| 724 | return -1; |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 729 | void dev_register_cmds(void) |
| 730 | { |
| Alexei Avshalom Lazar | 4a3c2f8 | 2019-05-02 13:35:37 +0300 | [diff] [blame] | 731 | sigma_dut_reg_cmd("dev_send_frame", req_prog, cmd_dev_send_frame); |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 732 | sigma_dut_reg_cmd("dev_set_parameter", req_intf_prog, |
| 733 | cmd_dev_set_parameter); |
| Jouni Malinen | d86e582 | 2017-08-29 03:55:32 +0300 | [diff] [blame] | 734 | sigma_dut_reg_cmd("dev_exec_action", req_prog, |
| Amarnath Hullur Subramanyam | 9c381f5 | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 735 | cmd_dev_exec_action); |
| Jouni Malinen | 3c367e8 | 2017-06-23 17:01:47 +0300 | [diff] [blame] | 736 | sigma_dut_reg_cmd("dev_configure_ie", req_intf, cmd_dev_configure_ie); |
| Peng Xu | a7ac56e | 2018-06-26 16:07:19 -0700 | [diff] [blame] | 737 | sigma_dut_reg_cmd("dev_start_test", NULL, cmd_dev_start_test); |
| 738 | sigma_dut_reg_cmd("dev_stop_test", NULL, cmd_dev_stop_test); |
| 739 | sigma_dut_reg_cmd("dev_get_log", NULL, cmd_dev_get_log); |
| Anurag Das | 2052daa | 2018-08-31 14:35:25 +0530 | [diff] [blame] | 740 | sigma_dut_reg_cmd("dev_ble_action", req_role_svcname, |
| 741 | cmd_dev_ble_action); |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 742 | } |