network_helpers.c 4.1 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0-only
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
7

8 9
#include <arpa/inet.h>

10 11 12 13
#include <linux/err.h>
#include <linux/in.h>
#include <linux/in6.h>

14
#include "bpf_util.h"
15 16 17
#include "network_helpers.h"

#define clean_errno() (errno == 0 ? "None" : strerror(errno))
18 19 20 21 22 23 24
#define log_err(MSG, ...) ({						\
			int __save = errno;				\
			fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
				__FILE__, __LINE__, clean_errno(),	\
				##__VA_ARGS__);				\
			errno = __save;					\
})
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
struct ipv4_packet pkt_v4 = {
	.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
	.iph.ihl = 5,
	.iph.protocol = IPPROTO_TCP,
	.iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
	.tcp.urg_ptr = 123,
	.tcp.doff = 5,
};

struct ipv6_packet pkt_v6 = {
	.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
	.iph.nexthdr = IPPROTO_TCP,
	.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
	.tcp.urg_ptr = 123,
	.tcp.doff = 5,
};

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static int settimeo(int fd, int timeout_ms)
{
	struct timeval timeout = { .tv_sec = 3 };

	if (timeout_ms > 0) {
		timeout.tv_sec = timeout_ms / 1000;
		timeout.tv_usec = (timeout_ms % 1000) * 1000;
	}

	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
		       sizeof(timeout))) {
		log_err("Failed to set SO_RCVTIMEO");
		return -1;
	}

	if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout,
		       sizeof(timeout))) {
		log_err("Failed to set SO_SNDTIMEO");
		return -1;
	}

	return 0;
}

#define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; })

int start_server(int family, int type, const char *addr_str, __u16 port,
		 int timeout_ms)
71 72 73 74 75 76 77 78 79
{
	struct sockaddr_storage addr = {};
	socklen_t len;
	int fd;

	if (family == AF_INET) {
		struct sockaddr_in *sin = (void *)&addr;

		sin->sin_family = AF_INET;
80
		sin->sin_port = htons(port);
81 82 83 84 85
		if (addr_str &&
		    inet_pton(AF_INET, addr_str, &sin->sin_addr) != 1) {
			log_err("inet_pton(AF_INET, %s)", addr_str);
			return -1;
		}
86 87 88 89 90
		len = sizeof(*sin);
	} else {
		struct sockaddr_in6 *sin6 = (void *)&addr;

		sin6->sin6_family = AF_INET6;
91
		sin6->sin6_port = htons(port);
92 93 94 95 96
		if (addr_str &&
		    inet_pton(AF_INET6, addr_str, &sin6->sin6_addr) != 1) {
			log_err("inet_pton(AF_INET6, %s)", addr_str);
			return -1;
		}
97 98 99
		len = sizeof(*sin6);
	}

100
	fd = socket(family, type, 0);
101 102 103 104 105
	if (fd < 0) {
		log_err("Failed to create server socket");
		return -1;
	}

106 107 108
	if (settimeo(fd, timeout_ms))
		goto error_close;

109 110
	if (bind(fd, (const struct sockaddr *)&addr, len) < 0) {
		log_err("Failed to bind socket");
111
		goto error_close;
112 113 114 115 116
	}

	if (type == SOCK_STREAM) {
		if (listen(fd, 1) < 0) {
			log_err("Failed to listed on socket");
117
			goto error_close;
118 119 120 121 122
		}
	}

	return fd;

123 124 125
error_close:
	save_errno_close(fd);
	return -1;
126 127
}

128 129 130
static int connect_fd_to_addr(int fd,
			      const struct sockaddr_storage *addr,
			      socklen_t addrlen)
131
{
132 133
	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
		log_err("Failed to connect to server");
134 135 136
		return -1;
	}

137
	return 0;
138 139
}

140
int connect_to_fd(int server_fd, int timeout_ms)
141 142
{
	struct sockaddr_storage addr;
143 144 145
	struct sockaddr_in *addr_in;
	socklen_t addrlen, optlen;
	int fd, type;
146

147 148 149
	optlen = sizeof(type);
	if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
		log_err("getsockopt(SOL_TYPE)");
150
		return -1;
151 152
	}

153 154
	addrlen = sizeof(addr);
	if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
155
		log_err("Failed to get server addr");
156
		return -1;
157 158
	}

159 160 161 162
	addr_in = (struct sockaddr_in *)&addr;
	fd = socket(addr_in->sin_family, type, 0);
	if (fd < 0) {
		log_err("Failed to create client socket");
163
		return -1;
164 165
	}

166 167 168 169 170 171 172 173 174 175 176
	if (settimeo(fd, timeout_ms))
		goto error_close;

	if (connect_fd_to_addr(fd, &addr, addrlen))
		goto error_close;

	return fd;

error_close:
	save_errno_close(fd);
	return -1;
177 178
}

179
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
180
{
181 182
	struct sockaddr_storage addr;
	socklen_t len = sizeof(addr);
183

184
	if (settimeo(client_fd, timeout_ms))
185 186
		return -1;

187 188
	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
		log_err("Failed to get server addr");
189 190 191
		return -1;
	}

192 193
	if (connect_fd_to_addr(client_fd, &addr, len))
		return -1;
194

195
	return 0;
196
}