xref: /aosp_15_r20/external/ltp/lib/tst_net.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017-2019 Petr Vorel <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2019 Martin Doucha <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <netdb.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_net.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_private.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
tst_print_svar(const char * name,const char * val)19*49cdfc7eSAndroid Build Coastguard Worker void tst_print_svar(const char *name, const char *val)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker 	if (name && val)
22*49cdfc7eSAndroid Build Coastguard Worker 		printf("export %s=\"%s\"\n", name, val);
23*49cdfc7eSAndroid Build Coastguard Worker }
24*49cdfc7eSAndroid Build Coastguard Worker 
tst_print_svar_change(const char * name,const char * val)25*49cdfc7eSAndroid Build Coastguard Worker void tst_print_svar_change(const char *name, const char *val)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	if (name && val)
28*49cdfc7eSAndroid Build Coastguard Worker 		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
29*49cdfc7eSAndroid Build Coastguard Worker }
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker /*
32*49cdfc7eSAndroid Build Coastguard Worker  * Function bit_count is from ipcalc project, ipcalc.c.
33*49cdfc7eSAndroid Build Coastguard Worker  */
tst_bit_count(uint32_t i)34*49cdfc7eSAndroid Build Coastguard Worker static int tst_bit_count(uint32_t i)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	int c = 0;
37*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int seen_one = 0;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	while (i > 0) {
40*49cdfc7eSAndroid Build Coastguard Worker 		if (i & 1) {
41*49cdfc7eSAndroid Build Coastguard Worker 			seen_one = 1;
42*49cdfc7eSAndroid Build Coastguard Worker 			c++;
43*49cdfc7eSAndroid Build Coastguard Worker 		} else {
44*49cdfc7eSAndroid Build Coastguard Worker 			if (seen_one)
45*49cdfc7eSAndroid Build Coastguard Worker 				return -1;
46*49cdfc7eSAndroid Build Coastguard Worker 		}
47*49cdfc7eSAndroid Build Coastguard Worker 		i >>= 1;
48*49cdfc7eSAndroid Build Coastguard Worker 	}
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	return c;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker /*
54*49cdfc7eSAndroid Build Coastguard Worker  * Function mask2prefix is from ipcalc project, ipcalc.c.
55*49cdfc7eSAndroid Build Coastguard Worker  */
tst_mask2prefix(struct in_addr mask)56*49cdfc7eSAndroid Build Coastguard Worker static int tst_mask2prefix(struct in_addr mask)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	return tst_bit_count(ntohl(mask.s_addr));
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker /*
62*49cdfc7eSAndroid Build Coastguard Worker  * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
63*49cdfc7eSAndroid Build Coastguard Worker  */
tst_ipv4_mask_to_int(const char * prefix)64*49cdfc7eSAndroid Build Coastguard Worker static int tst_ipv4_mask_to_int(const char *prefix)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
67*49cdfc7eSAndroid Build Coastguard Worker 	struct in_addr in;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	ret = inet_pton(AF_INET, prefix, &in);
70*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == 0)
71*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	return tst_mask2prefix(in);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker /*
77*49cdfc7eSAndroid Build Coastguard Worker  * Function safe_atoi is from ipcalc project, ipcalc.c.
78*49cdfc7eSAndroid Build Coastguard Worker  */
tst_safe_atoi(const char * s,int * ret_i)79*49cdfc7eSAndroid Build Coastguard Worker static int tst_safe_atoi(const char *s, int *ret_i)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	char *x = NULL;
82*49cdfc7eSAndroid Build Coastguard Worker 	long l;
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
85*49cdfc7eSAndroid Build Coastguard Worker 	l = strtol(s, &x, 0);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if (!x || x == s || *x || errno)
88*49cdfc7eSAndroid Build Coastguard Worker 		return errno > 0 ? -errno : -EINVAL;
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	if ((long)(int)l != l)
91*49cdfc7eSAndroid Build Coastguard Worker 		return -ERANGE;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	*ret_i = (int)l;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker /*
99*49cdfc7eSAndroid Build Coastguard Worker  * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
100*49cdfc7eSAndroid Build Coastguard Worker  */
tst_get_prefix(const char * ip_str,int is_ipv6)101*49cdfc7eSAndroid Build Coastguard Worker int tst_get_prefix(const char *ip_str, int is_ipv6)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker 	char *prefix_str = NULL;
104*49cdfc7eSAndroid Build Coastguard Worker 	int prefix = -1, r;
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	prefix_str = strchr(ip_str, '/');
107*49cdfc7eSAndroid Build Coastguard Worker 	if (!prefix_str)
108*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	*(prefix_str++) = '\0';
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	if (!is_ipv6 && strchr(prefix_str, '.'))
113*49cdfc7eSAndroid Build Coastguard Worker 		prefix = tst_ipv4_mask_to_int(prefix_str);
114*49cdfc7eSAndroid Build Coastguard Worker 	else {
115*49cdfc7eSAndroid Build Coastguard Worker 		r = tst_safe_atoi(prefix_str, &prefix);
116*49cdfc7eSAndroid Build Coastguard Worker 		if (r != 0)
117*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk_comment("conversion error: '%s' is not integer",
118*49cdfc7eSAndroid Build Coastguard Worker 					prefix_str);
119*49cdfc7eSAndroid Build Coastguard Worker 	}
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
122*49cdfc7eSAndroid Build Coastguard Worker 		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
123*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_comment("bad %s prefix: %s", is_ipv6 ?  "IPv6" : "IPv4",
124*49cdfc7eSAndroid Build Coastguard Worker 				prefix_str);
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	return prefix;
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker 
tst_get_in_addr(const char * ip_str,struct in_addr * ip)129*49cdfc7eSAndroid Build Coastguard Worker void tst_get_in_addr(const char *ip_str, struct in_addr *ip)
130*49cdfc7eSAndroid Build Coastguard Worker {
131*49cdfc7eSAndroid Build Coastguard Worker 	if (inet_pton(AF_INET, ip_str, ip) <= 0)
132*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_comment("bad IPv4 address: '%s'", ip_str);
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker 
tst_get_in6_addr(const char * ip_str,struct in6_addr * ip6)135*49cdfc7eSAndroid Build Coastguard Worker void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker 	if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
138*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_comment("bad IPv6 address: '%s'", ip_str);
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
tst_get_connect_address(int sock,struct sockaddr_storage * addr)141*49cdfc7eSAndroid Build Coastguard Worker socklen_t tst_get_connect_address(int sock, struct sockaddr_storage *addr)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in *inet_ptr;
144*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in6 *inet6_ptr;
145*49cdfc7eSAndroid Build Coastguard Worker 	size_t tmp_size;
146*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t ret = sizeof(*addr);
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(sock, (struct sockaddr*)addr, &ret);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	/* Sanitize wildcard addresses */
151*49cdfc7eSAndroid Build Coastguard Worker 	switch (addr->ss_family) {
152*49cdfc7eSAndroid Build Coastguard Worker 	case AF_INET:
153*49cdfc7eSAndroid Build Coastguard Worker 		inet_ptr = (struct sockaddr_in*)addr;
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 		switch (ntohl(inet_ptr->sin_addr.s_addr)) {
156*49cdfc7eSAndroid Build Coastguard Worker 		case INADDR_ANY:
157*49cdfc7eSAndroid Build Coastguard Worker 		case INADDR_BROADCAST:
158*49cdfc7eSAndroid Build Coastguard Worker 			inet_ptr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
159*49cdfc7eSAndroid Build Coastguard Worker 			break;
160*49cdfc7eSAndroid Build Coastguard Worker 		}
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 		break;
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	case AF_INET6:
165*49cdfc7eSAndroid Build Coastguard Worker 		inet6_ptr = (struct sockaddr_in6*)addr;
166*49cdfc7eSAndroid Build Coastguard Worker 		tmp_size = sizeof(struct in6_addr);
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 		if (!memcmp(&inet6_ptr->sin6_addr, &in6addr_any, tmp_size)) {
169*49cdfc7eSAndroid Build Coastguard Worker 			memcpy(&inet6_ptr->sin6_addr, &in6addr_loopback,
170*49cdfc7eSAndroid Build Coastguard Worker 				tmp_size);
171*49cdfc7eSAndroid Build Coastguard Worker 		}
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 		break;
174*49cdfc7eSAndroid Build Coastguard Worker 	}
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
177*49cdfc7eSAndroid Build Coastguard Worker }
178*49cdfc7eSAndroid Build Coastguard Worker 
tst_init_sockaddr_inet(struct sockaddr_in * sa,const char * ip_str,uint16_t port)179*49cdfc7eSAndroid Build Coastguard Worker void tst_init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str, uint16_t port)
180*49cdfc7eSAndroid Build Coastguard Worker {
181*49cdfc7eSAndroid Build Coastguard Worker 	memset(sa, 0, sizeof(struct sockaddr_in));
182*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin_family = AF_INET;
183*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin_port = htons(port);
184*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_in_addr(ip_str, &sa->sin_addr);
185*49cdfc7eSAndroid Build Coastguard Worker }
186*49cdfc7eSAndroid Build Coastguard Worker 
tst_init_sockaddr_inet_bin(struct sockaddr_in * sa,uint32_t ip_val,uint16_t port)187*49cdfc7eSAndroid Build Coastguard Worker void tst_init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val, uint16_t port)
188*49cdfc7eSAndroid Build Coastguard Worker {
189*49cdfc7eSAndroid Build Coastguard Worker 	memset(sa, 0, sizeof(struct sockaddr_in));
190*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin_family = AF_INET;
191*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin_port = htons(port);
192*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin_addr.s_addr = htonl(ip_val);
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker 
tst_init_sockaddr_inet6(struct sockaddr_in6 * sa,const char * ip_str,uint16_t port)195*49cdfc7eSAndroid Build Coastguard Worker void tst_init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str, uint16_t port)
196*49cdfc7eSAndroid Build Coastguard Worker {
197*49cdfc7eSAndroid Build Coastguard Worker 	memset(sa, 0, sizeof(struct sockaddr_in6));
198*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin6_family = AF_INET6;
199*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin6_port = htons(port);
200*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_in6_addr(ip_str, &sa->sin6_addr);
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker 
tst_init_sockaddr_inet6_bin(struct sockaddr_in6 * sa,const struct in6_addr * ip_val,uint16_t port)203*49cdfc7eSAndroid Build Coastguard Worker void tst_init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct in6_addr *ip_val, uint16_t port)
204*49cdfc7eSAndroid Build Coastguard Worker {
205*49cdfc7eSAndroid Build Coastguard Worker 	memset(sa, 0, sizeof(struct sockaddr_in6));
206*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin6_family = AF_INET6;
207*49cdfc7eSAndroid Build Coastguard Worker 	sa->sin6_port = htons(port);
208*49cdfc7eSAndroid Build Coastguard Worker 	memcpy(&sa->sin6_addr, ip_val, sizeof(struct in6_addr));
209*49cdfc7eSAndroid Build Coastguard Worker }
210*49cdfc7eSAndroid Build Coastguard Worker 
safe_getaddrinfo(const char * file,const int lineno,const char * src_addr,const char * port,const struct addrinfo * hints,struct addrinfo ** addr_info)211*49cdfc7eSAndroid Build Coastguard Worker void safe_getaddrinfo(const char *file, const int lineno, const char *src_addr,
212*49cdfc7eSAndroid Build Coastguard Worker 					  const char *port, const struct addrinfo *hints,
213*49cdfc7eSAndroid Build Coastguard Worker 					  struct addrinfo **addr_info)
214*49cdfc7eSAndroid Build Coastguard Worker {
215*49cdfc7eSAndroid Build Coastguard Worker 	int err = getaddrinfo(src_addr, port, hints, addr_info);
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	if (err) {
218*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno, TBROK, "getaddrinfo failed, %s",
219*49cdfc7eSAndroid Build Coastguard Worker 			gai_strerror(err));
220*49cdfc7eSAndroid Build Coastguard Worker 	}
221*49cdfc7eSAndroid Build Coastguard Worker 
222*49cdfc7eSAndroid Build Coastguard Worker 	if (!*addr_info)
223*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno, TBROK, "failed to get the address");
224*49cdfc7eSAndroid Build Coastguard Worker }
225*49cdfc7eSAndroid Build Coastguard Worker 
tst_setup_netns(void)226*49cdfc7eSAndroid Build Coastguard Worker void tst_setup_netns(void)
227*49cdfc7eSAndroid Build Coastguard Worker {
228*49cdfc7eSAndroid Build Coastguard Worker 	int real_uid = getuid();
229*49cdfc7eSAndroid Build Coastguard Worker 	int real_gid = getgid();
230*49cdfc7eSAndroid Build Coastguard Worker 	int nscount = 1;
231*49cdfc7eSAndroid Build Coastguard Worker 
232*49cdfc7eSAndroid Build Coastguard Worker 	if (!access("/proc/sys/user/max_user_namespaces", F_OK)) {
233*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_SCANF("/proc/sys/user/max_user_namespaces", "%d",
234*49cdfc7eSAndroid Build Coastguard Worker 			&nscount);
235*49cdfc7eSAndroid Build Coastguard Worker 	}
236*49cdfc7eSAndroid Build Coastguard Worker 
237*49cdfc7eSAndroid Build Coastguard Worker 	if (!nscount)
238*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "User namespaces are disabled");
239*49cdfc7eSAndroid Build Coastguard Worker 
240*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNSHARE(CLONE_NEWUSER);
241*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNSHARE(CLONE_NEWNET);
242*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF("/proc/self/setgroups", "deny");
243*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
244*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
245*49cdfc7eSAndroid Build Coastguard Worker }
246