blob: 81ae9bdcf8fa2ad9085507503381ab260c4b5d01 [file] [log] [blame]
Jukka Rissanen0f664262018-02-21 16:18:35 +02001/*
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
Jukka Rissanen0f664262018-02-21 16:18:35 +020014/* Host include files */
15#include <stdio.h>
Jukka Rissanen9135b172018-07-23 13:58:21 +030016#include <stdlib.h>
Jukka Rissanen0f664262018-02-21 16:18:35 +020017#include <stdarg.h>
18#include <errno.h>
19#include <string.h>
20#include <stdbool.h>
Alberto Escolar Piedras7c776c82018-08-02 12:07:35 +020021#include <unistd.h>
Jukka Rissanen0f664262018-02-21 16:18:35 +020022#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/socket.h>
Alberto Escolar Piedrasc065ebd2018-09-25 16:52:27 +020025#include <sys/select.h>
Jukka Rissanen0f664262018-02-21 16:18:35 +020026#include <net/if.h>
Jukka Rissanen16f31f12018-03-12 21:54:19 +020027#include <time.h>
Jukka Rissanenc345f592023-10-03 14:58:25 +030028#include <inttypes.h>
29#include <nsi_tracing.h>
Jukka Rissanen0f664262018-02-21 16:18:35 +020030
31#ifdef __linux
Jukka Rissanenc345f592023-10-03 14:58:25 +030032#include <linux/if.h>
Jukka Rissanen0f664262018-02-21 16:18:35 +020033#include <linux/if_tun.h>
34#endif
35
Jukka Rissanen0f664262018-02-21 16:18:35 +020036#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 */
Jukka Rissanenc345f592023-10-03 14:58:25 +030041int eth_iface_create(const char *dev_name, const char *if_name, bool tun_only)
Jukka Rissanen0f664262018-02-21 16:18:35 +020042{
43 struct ifreq ifr;
44 int fd, ret = -EINVAL;
45
Jukka Rissanenc345f592023-10-03 14:58:25 +030046 fd = open(dev_name, O_RDWR);
Jukka Rissanen0f664262018-02-21 16:18:35 +020047 if (fd < 0) {
48 return -errno;
49 }
50
Flavio Ceolinda49f2e2018-09-11 19:09:03 -070051 (void)memset(&ifr, 0, sizeof(ifr));
Jukka Rissanen0f664262018-02-21 16:18:35 +020052
53#ifdef __linux
54 ifr.ifr_flags = (tun_only ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
55
Jukka Rissanen179d5202019-03-12 23:36:51 +020056 strncpy(ifr.ifr_name, if_name, IFNAMSIZ - 1);
Jukka Rissanen0f664262018-02-21 16:18:35 +020057
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
69int eth_iface_remove(int fd)
70{
71 return close(fd);
72}
73
74static int ssystem(const char *fmt, ...)
75 __attribute__((__format__(__printf__, 1, 2)));
76
77static int ssystem(const char *fmt, ...)
78{
79 char cmd[255];
80 va_list ap;
Jukka Rissanen9135b172018-07-23 13:58:21 +030081 int ret;
Jukka Rissanen0f664262018-02-21 16:18:35 +020082
83 va_start(ap, fmt);
84 vsnprintf(cmd, sizeof(cmd), fmt, ap);
85 va_end(ap);
86
Jukka Rissanenc345f592023-10-03 14:58:25 +030087 nsi_print_trace("%s\n", cmd);
Jukka Rissanen0f664262018-02-21 16:18:35 +020088
Jukka Rissanen9135b172018-07-23 13:58:21 +030089 ret = system(cmd);
90
91 return -WEXITSTATUS(ret);
Jukka Rissanen0f664262018-02-21 16:18:35 +020092}
93
Jukka Rissanen0f664262018-02-21 16:18:35 +020094int 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;
Jukka Rissanen5afa30c2018-03-26 13:04:00 +0300105 timeout.tv_usec = 0;
Jukka Rissanen0f664262018-02-21 16:18:35 +0200106
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
Jukka Rissanenc345f592023-10-03 14:58:25 +0300119int eth_clock_gettime(uint64_t *second, uint32_t *nanosecond)
Jukka Rissanen16f31f12018-03-12 21:54:19 +0200120{
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
Jukka Rissanenc345f592023-10-03 14:58:25 +0300129 *second = (uint64_t)tp.tv_sec;
130 *nanosecond = (uint32_t)tp.tv_nsec;
Jukka Rissanen16f31f12018-03-12 21:54:19 +0200131
132 return 0;
133}
Jukka Rissanenaf44d7c2018-07-23 14:00:19 +0300134
Jukka Rissanenaf44d7c2018-07-23 14:00:19 +0300135int 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}