| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sigma Control API DUT (station/AP) |
| Amarnath Hullur Subramanyam | 659a34c | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Qualcomm Atheros, Inc. |
| Ankita Bajaj | 1bde794 | 2018-01-09 19:15:01 +0530 | [diff] [blame] | 4 | * Copyright (c) 2018, 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" |
| 10 | #include <sys/stat.h> |
| 11 | #include "wpa_helpers.h" |
| 12 | |
| 13 | enum driver_type wifi_chip_type = DRIVER_NOT_SET; |
| 14 | enum openwrt_driver_type openwrt_chip_type = OPENWRT_DRIVER_NOT_SET; |
| 15 | |
| 16 | |
| 17 | int file_exists(const char *fname) |
| 18 | { |
| 19 | struct stat s; |
| 20 | return stat(fname, &s) == 0; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | int set_wifi_chip(const char *chip_type) |
| 25 | { |
| 26 | if (!strncmp(chip_type, "WCN", strlen("WCN"))) |
| 27 | wifi_chip_type = DRIVER_WCN; |
| 28 | else if (!strncmp(chip_type, "ATHEROS", strlen("ATHEROS"))) |
| 29 | wifi_chip_type = DRIVER_ATHEROS; |
| 30 | else if (!strncmp(chip_type, "AR6003", strlen("AR6003"))) |
| 31 | wifi_chip_type = DRIVER_AR6003; |
| 32 | else if (strcmp(chip_type, "MAC80211") == 0) |
| 33 | wifi_chip_type = DRIVER_MAC80211; |
| 34 | else if (strcmp(chip_type, "QNXNTO") == 0) |
| 35 | wifi_chip_type = DRIVER_QNXNTO; |
| 36 | else if (strcmp(chip_type, "OPENWRT") == 0) |
| 37 | wifi_chip_type = DRIVER_OPENWRT; |
| Sreelakshmi Konamki | b692f10 | 2016-04-26 19:47:00 +0530 | [diff] [blame] | 38 | else if (!strncmp(chip_type, "LINUX-WCN", strlen("LINUX-WCN"))) |
| 39 | wifi_chip_type = DRIVER_LINUX_WCN; |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 40 | else |
| 41 | return -1; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | enum driver_type get_driver_type(void) |
| 48 | { |
| 49 | struct stat s; |
| 50 | if (wifi_chip_type == DRIVER_NOT_SET) { |
| 51 | /* Check for 60G driver */ |
| 52 | ssize_t len; |
| 53 | char link[256]; |
| 54 | char buf[256]; |
| 55 | char *ifname = get_station_ifname(); |
| 56 | |
| 57 | snprintf(buf, sizeof(buf), "/sys/class/net/%s/device/driver", |
| 58 | ifname); |
| 59 | len = readlink(buf, link, sizeof(link) - 1); |
| 60 | if (len >= 0) { |
| 61 | link[len] = '\0'; |
| 62 | if (strstr(link, DRIVER_NAME_60G)) |
| 63 | return DRIVER_WIL6210; |
| 64 | } |
| 65 | |
| 66 | if (stat("/sys/module/mac80211", &s) == 0) |
| 67 | return DRIVER_MAC80211; |
| 68 | return DRIVER_ATHEROS; |
| 69 | } |
| 70 | return wifi_chip_type; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | enum openwrt_driver_type get_openwrt_driver_type(void) |
| 75 | { |
| 76 | struct stat s; |
| 77 | |
| 78 | if (openwrt_chip_type == OPENWRT_DRIVER_NOT_SET) { |
| Sarvepalli, Rajesh Babu | a62e20a | 2016-07-08 18:10:30 +0530 | [diff] [blame] | 79 | if (stat("/sys/module/umac", &s) == 0) |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 80 | openwrt_chip_type = OPENWRT_DRIVER_ATHEROS; |
| 81 | } |
| 82 | |
| 83 | return openwrt_chip_type; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | enum sigma_program sigma_program_to_enum(const char *prog) |
| 88 | { |
| 89 | if (prog == NULL) |
| 90 | return PROGRAM_UNKNOWN; |
| 91 | |
| 92 | if (strcasecmp(prog, "TDLS") == 0) |
| 93 | return PROGRAM_TDLS; |
| 94 | if (strcasecmp(prog, "HS2") == 0) |
| 95 | return PROGRAM_HS2; |
| 96 | if (strcasecmp(prog, "HS2_R2") == 0 || |
| 97 | strcasecmp(prog, "HS2-R2") == 0) |
| 98 | return PROGRAM_HS2_R2; |
| 99 | if (strcasecmp(prog, "WFD") == 0) |
| 100 | return PROGRAM_WFD; |
| Amarnath Hullur Subramanyam | 659a34c | 2017-03-17 00:04:41 -0700 | [diff] [blame] | 101 | if (strcasecmp(prog, "DisplayR2") == 0) |
| 102 | return PROGRAM_DISPLAYR2; |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 103 | if (strcasecmp(prog, "PMF") == 0) |
| 104 | return PROGRAM_PMF; |
| 105 | if (strcasecmp(prog, "WPS") == 0) |
| 106 | return PROGRAM_WPS; |
| 107 | if (strcasecmp(prog, "11n") == 0) |
| 108 | return PROGRAM_HT; |
| 109 | if (strcasecmp(prog, "VHT") == 0) |
| 110 | return PROGRAM_VHT; |
| 111 | if (strcasecmp(prog, "60GHZ") == 0) |
| 112 | return PROGRAM_60GHZ; |
| 113 | if (strcasecmp(prog, "NAN") == 0) |
| 114 | return PROGRAM_NAN; |
| priyadharshini gowthaman | 12dd414 | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 115 | if (strcasecmp(prog, "LOC") == 0) |
| 116 | return PROGRAM_LOC; |
| priyadharshini gowthaman | b4e05fc | 2016-10-13 15:02:35 -0700 | [diff] [blame] | 117 | if (strcasecmp(prog, "MBO") == 0) |
| 118 | return PROGRAM_MBO; |
| Adil Saeed Musthafa | 33ea287 | 2017-04-10 23:13:24 -0700 | [diff] [blame] | 119 | if (strcasecmp(prog, "IoTLP") == 0) |
| 120 | return PROGRAM_IOTLP; |
| Jouni Malinen | 5c3813a | 2017-08-26 13:30:39 +0300 | [diff] [blame] | 121 | if (strcasecmp(prog, "DPP") == 0) |
| 122 | return PROGRAM_DPP; |
| Ankita Bajaj | a2cb567 | 2017-10-25 16:08:28 +0530 | [diff] [blame] | 123 | if (strcasecmp(prog, "OCE") == 0) |
| 124 | return PROGRAM_OCE; |
| Jouni Malinen | cd4e3c3 | 2015-10-29 12:39:56 +0200 | [diff] [blame] | 125 | |
| 126 | return PROGRAM_UNKNOWN; |
| 127 | } |
| priyadharshini gowthaman | 72462ef | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | static int parse_hex(char c) |
| 131 | { |
| 132 | if (c >= '0' && c <= '9') |
| 133 | return c - '0'; |
| 134 | if (c >= 'a' && c <= 'f') |
| 135 | return c - 'a' + 10; |
| 136 | if (c >= 'A' && c <= 'F') |
| 137 | return c - 'A' + 10; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static int hex_byte(const char *str) |
| 143 | { |
| 144 | int res1, res2; |
| 145 | |
| 146 | res1 = parse_hex(str[0]); |
| 147 | if (res1 < 0) |
| 148 | return -1; |
| 149 | res2 = parse_hex(str[1]); |
| 150 | if (res2 < 0) |
| 151 | return -1; |
| 152 | return (res1 << 4) | res2; |
| 153 | } |
| 154 | |
| 155 | |
| Jouni Malinen | 08cf231 | 2017-09-04 13:39:47 +0300 | [diff] [blame] | 156 | int parse_hexstr(const char *hex, unsigned char *buf, size_t buflen) |
| 157 | { |
| 158 | size_t i; |
| 159 | const char *pos = hex; |
| 160 | |
| 161 | for (i = 0; i < buflen; i++) { |
| 162 | int val; |
| 163 | |
| 164 | if (*pos == '\0') |
| 165 | break; |
| 166 | val = hex_byte(pos); |
| 167 | if (val < 0) |
| 168 | return -1; |
| 169 | buf[i] = val; |
| 170 | pos += 2; |
| 171 | } |
| 172 | |
| 173 | return i; |
| 174 | } |
| 175 | |
| 176 | |
| priyadharshini gowthaman | 72462ef | 2016-06-22 22:47:14 -0700 | [diff] [blame] | 177 | int parse_mac_address(struct sigma_dut *dut, const char *arg, |
| 178 | unsigned char *addr) |
| 179 | { |
| 180 | int i; |
| 181 | const char *pos = arg; |
| 182 | |
| 183 | if (strlen(arg) != 17) |
| 184 | goto fail; |
| 185 | |
| 186 | for (i = 0; i < ETH_ALEN; i++) { |
| 187 | int val; |
| 188 | |
| 189 | val = hex_byte(pos); |
| 190 | if (val < 0) |
| 191 | goto fail; |
| 192 | addr[i] = val; |
| 193 | if (i + 1 < ETH_ALEN) { |
| 194 | pos += 2; |
| 195 | if (*pos != ':') |
| 196 | goto fail; |
| 197 | pos++; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | |
| 203 | fail: |
| 204 | sigma_dut_print(dut, DUT_MSG_ERROR, |
| 205 | "Invalid MAC address %s (expected format xx:xx:xx:xx:xx:xx)", |
| 206 | arg); |
| 207 | return -1; |
| 208 | } |
| Rakesh Sunki | 7192dc4 | 2017-03-30 14:47:55 -0700 | [diff] [blame] | 209 | |
| 210 | |
| 211 | unsigned int channel_to_freq(unsigned int channel) |
| 212 | { |
| 213 | if (channel >= 1 && channel <= 13) |
| 214 | return 2407 + 5 * channel; |
| 215 | if (channel == 14) |
| 216 | return 2484; |
| 217 | if (channel >= 36 && channel <= 165) |
| 218 | return 5000 + 5 * channel; |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | unsigned int freq_to_channel(unsigned int freq) |
| 225 | { |
| 226 | if (freq >= 2412 && freq <= 2472) |
| 227 | return (freq - 2407) / 5; |
| 228 | if (freq == 2484) |
| 229 | return 14; |
| 230 | if (freq >= 5180 && freq <= 5825) |
| 231 | return (freq - 5000) / 5; |
| 232 | return 0; |
| 233 | } |
| Peng Xu | 769731a | 2017-05-10 17:27:28 -0700 | [diff] [blame] | 234 | |
| 235 | |
| Rakesh Sunki | 8f8e74b | 2017-05-16 15:42:12 -0700 | [diff] [blame] | 236 | void convert_mac_addr_to_ipv6_lladdr(u8 *mac_addr, char *ipv6_buf, |
| 237 | size_t buf_len) |
| 238 | { |
| 239 | u8 temp = mac_addr[0] ^ 0x02; |
| 240 | |
| 241 | snprintf(ipv6_buf, buf_len, "fe80::%02x%02x:%02xff:fe%02x:%02x%02x", |
| 242 | temp, mac_addr[1], mac_addr[2], |
| 243 | mac_addr[3], mac_addr[4], mac_addr[5]); |
| 244 | } |
| 245 | |
| 246 | |
| Peng Xu | 769731a | 2017-05-10 17:27:28 -0700 | [diff] [blame] | 247 | #ifndef ANDROID |
| 248 | |
| 249 | size_t strlcpy(char *dest, const char *src, size_t siz) |
| 250 | { |
| 251 | const char *s = src; |
| 252 | size_t left = siz; |
| 253 | |
| 254 | if (left) { |
| 255 | /* Copy string up to the maximum size of the dest buffer */ |
| 256 | while (--left != 0) { |
| 257 | if ((*dest++ = *s++) == '\0') |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (left == 0) { |
| 263 | /* Not enough room for the string; force NUL-termination */ |
| 264 | if (siz != 0) |
| 265 | *dest = '\0'; |
| 266 | while (*s++) |
| 267 | ; /* determine total src string length */ |
| 268 | } |
| 269 | |
| 270 | return s - src - 1; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | size_t strlcat(char *dst, const char *str, size_t size) |
| 275 | { |
| 276 | char *pos; |
| 277 | size_t dstlen, srclen, copy; |
| 278 | |
| 279 | srclen = strlen(str); |
| 280 | for (pos = dst; pos - dst < size && *dst; pos++) |
| 281 | ; |
| 282 | dstlen = pos - dst; |
| 283 | if (*dst) |
| 284 | return dstlen + srclen; |
| 285 | if (dstlen + srclen + 1 > size) |
| 286 | copy = size - dstlen - 1; |
| 287 | else |
| 288 | copy = srclen; |
| 289 | memcpy(pos, str, copy); |
| 290 | pos[copy] = '\0'; |
| 291 | return dstlen + srclen; |
| 292 | } |
| 293 | |
| 294 | #endif /* ANDROID */ |
| Ankita Bajaj | 1bde794 | 2018-01-09 19:15:01 +0530 | [diff] [blame] | 295 | |
| 296 | |
| 297 | void hex_dump(struct sigma_dut *dut, u8 *data, size_t len) |
| 298 | { |
| 299 | char buf[1024]; |
| 300 | size_t index; |
| 301 | u8 *ptr; |
| 302 | int pos; |
| 303 | |
| 304 | memset(buf, 0, sizeof(buf)); |
| 305 | ptr = data; |
| 306 | pos = 0; |
| 307 | for (index = 0; index < len; index++) { |
| 308 | pos += snprintf(&(buf[pos]), sizeof(buf) - pos, |
| 309 | "%02x ", *ptr++); |
| 310 | if (pos > 1020) |
| 311 | break; |
| 312 | } |
| 313 | sigma_dut_print(dut, DUT_MSG_INFO, "HEXDUMP len=[%d]", (int) len); |
| 314 | sigma_dut_print(dut, DUT_MSG_INFO, "buf:%s", buf); |
| 315 | } |