1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 *
10 * Routines setting up the host system. Those are placed in separate file
11 * because there is naming conflicts between host and zephyr network stacks.
12 */
13
14 /* Host include files */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <stdbool.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/select.h>
26 #include <net/if.h>
27 #include <time.h>
28 #include <inttypes.h>
29 #include <nsi_tracing.h>
30
31 #ifdef __linux
32 #include <linux/if.h>
33 #include <linux/if_tun.h>
34 #endif
35
36 #include "eth_native_posix_priv.h"
37
38 /* Note that we cannot create the TUN/TAP device from the setup script
39 * as we need to get a file descriptor to communicate with the interface.
40 */
eth_iface_create(const char * dev_name,const char * if_name,bool tun_only)41 int eth_iface_create(const char *dev_name, const char *if_name, bool tun_only)
42 {
43 struct ifreq ifr;
44 int fd, ret = -EINVAL;
45
46 fd = open(dev_name, O_RDWR);
47 if (fd < 0) {
48 return -errno;
49 }
50
51 (void)memset(&ifr, 0, sizeof(ifr));
52
53 #ifdef __linux
54 ifr.ifr_flags = (tun_only ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
55
56 strncpy(ifr.ifr_name, if_name, IFNAMSIZ - 1);
57
58 ret = ioctl(fd, TUNSETIFF, (void *)&ifr);
59 if (ret < 0) {
60 ret = -errno;
61 close(fd);
62 return ret;
63 }
64 #endif
65
66 return fd;
67 }
68
eth_iface_remove(int fd)69 int eth_iface_remove(int fd)
70 {
71 return close(fd);
72 }
73
74 static int ssystem(const char *fmt, ...)
75 __attribute__((__format__(__printf__, 1, 2)));
76
ssystem(const char * fmt,...)77 static int ssystem(const char *fmt, ...)
78 {
79 char cmd[255];
80 va_list ap;
81 int ret;
82
83 va_start(ap, fmt);
84 vsnprintf(cmd, sizeof(cmd), fmt, ap);
85 va_end(ap);
86
87 nsi_print_trace("%s\n", cmd);
88
89 ret = system(cmd);
90
91 return -WEXITSTATUS(ret);
92 }
93
eth_wait_data(int fd)94 int eth_wait_data(int fd)
95 {
96 struct timeval timeout;
97 fd_set rset;
98 int ret;
99
100 FD_ZERO(&rset);
101
102 FD_SET(fd, &rset);
103
104 timeout.tv_sec = 0;
105 timeout.tv_usec = 0;
106
107 ret = select(fd + 1, &rset, NULL, NULL, &timeout);
108 if (ret < 0 && errno != EINTR) {
109 return -errno;
110 } else if (ret > 0) {
111 if (FD_ISSET(fd, &rset)) {
112 return 0;
113 }
114 }
115
116 return -EAGAIN;
117 }
118
eth_clock_gettime(uint64_t * second,uint32_t * nanosecond)119 int eth_clock_gettime(uint64_t *second, uint32_t *nanosecond)
120 {
121 struct timespec tp;
122 int ret;
123
124 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
125 if (ret < 0) {
126 return -errno;
127 }
128
129 *second = (uint64_t)tp.tv_sec;
130 *nanosecond = (uint32_t)tp.tv_nsec;
131
132 return 0;
133 }
134
eth_promisc_mode(const char * if_name,bool enable)135 int eth_promisc_mode(const char *if_name, bool enable)
136 {
137 return ssystem("ip link set dev %s promisc %s",
138 if_name, enable ? "on" : "off");
139 }
140