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