]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/network_namespace_framework.c
Update invite API in blackbox test cases
[meshlink] / test / blackbox / common / network_namespace_framework.c
1 /*
2     test_optimal_pmtu.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2019  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "network_namespace_framework.h"
20
21 #define DEFAULT_PUB_NET_ADDR "203.0.113.0/24"
22 #define DEFAULT_GATEWAY_NET_ADDR "203.0.113.254"
23 #define NS_PEER0  " ns_peer0 "
24 #define NS_ETH0   " ns_eth0 "
25 #define PEER_INDEX i ? 0 : 1
26 #define get_namespace_handle_by_index(state_ptr, index) index < state_ptr->num_namespaces ? &(state_ptr->namespaces[index]) : NULL
27 #define get_interface_handle_by_index(namespace, index) index < namespace->interfaces_no  ? &((namespace->interfaces)[index]) : NULL
28
29 static int ipv4_str_check_cidr(const char *ip_addr) {
30         int cidr = 0;
31         sscanf(ip_addr, "%*d.%*d.%*d.%*d/%d", &cidr);
32         return cidr;
33 }
34
35 static char *ipv4_str_remove_cidr(const char *ipv4_addr) {
36         char *ptr = strdup(ipv4_addr);
37         assert(ptr);
38
39         if(ipv4_str_check_cidr(ptr)) {
40                 char *end = strchr(ptr, '/');
41                 *end = '\0';
42         }
43
44         return ptr;
45 }
46
47 namespace_t *find_namespace(netns_state_t *state, const char *namespace_name) {
48         int i;
49
50         for(i = 0; i < state->num_namespaces; i++) {
51                 if(!strcmp((state->namespaces[i]).name, namespace_name)) {
52                         return &(state->namespaces[i]);
53                 }
54         }
55
56         return NULL;
57 }
58
59 static int netns_delete_namespace(namespace_t *namespace_handle) {
60         char cmd[200];
61
62         if(namespace_handle->type != BRIDGE) {
63                 assert(sprintf(cmd, "ip netns del %s 2>/dev/null", namespace_handle->name) >= 0);
64         } else {
65                 assert(sprintf(cmd, "ip link del %s 2>/dev/null", namespace_handle->name) >= 0);
66         }
67
68         return system(cmd);
69 }
70
71 /* Create new network namespace using namespace handle */
72 static void netns_create_namespace(netns_state_t *test_state, namespace_t *namespace_handle) {
73         char cmd[200];
74         int cmd_ret;
75
76         // Add the network namespace
77
78         sprintf(cmd, "ip netns add %s", namespace_handle->name);
79         assert(system(cmd) == 0);
80
81         sprintf(cmd, "ip netns exec %s ip link set dev lo up", namespace_handle->name);
82         assert(system(cmd) == 0);
83 }
84
85 static void netns_create_bridge(netns_state_t *test_state, namespace_t *namespace_handle) {
86         char cmd[200];
87         int cmd_ret;
88
89         sprintf(cmd, "ip link add name %s type bridge", namespace_handle->name);
90         assert(system(cmd) == 0);
91
92         sprintf(cmd, "ip link set %s up", namespace_handle->name);
93         assert(system(cmd) == 0);
94 }
95
96 interface_t *get_peer_interface_handle(netns_state_t *test_state, namespace_t *namespace, namespace_t *peer_namespace) {
97         int i;
98         interface_t *interfaces = namespace->interfaces;
99         int if_no = namespace->interfaces_no;
100         char *peer_name = peer_namespace->name;
101
102         for(i = 0; i < if_no; i++) {
103                 if(!strcasecmp(interfaces[i].if_peer, peer_name)) {
104                         return &interfaces[i];
105                 }
106         }
107
108         return NULL;
109 }
110
111 interface_t *get_interface_handle_by_name(netns_state_t *test_state, namespace_t *namespace, const char *peer_name) {
112         namespace_t *peer_ns;
113         peer_ns = find_namespace(test_state, peer_name);
114         assert(peer_ns);
115
116         return get_peer_interface_handle(test_state, namespace, peer_ns);
117 }
118
119 bool check_interfaces_visited(netns_state_t *test_state, namespace_t *ns1, namespace_t *ns2) {
120         interface_t *iface, *peer_iface;
121         iface = get_peer_interface_handle(test_state, ns1, ns2);
122         peer_iface = get_peer_interface_handle(test_state, ns2, ns1);
123         assert(iface && peer_iface);
124
125         return iface->priv || peer_iface->priv;
126 }
127
128 void netns_connect_namespaces(netns_state_t *test_state, namespace_t *ns1, namespace_t *ns2) {
129         char buff[20], cmd[200], ns_eth0[20], ns_peer0[20];
130         int cmd_ret, if_no, i;
131         char eth_pairs[2][20];
132         namespace_t *ns[2] = { ns1, ns2 };
133         interface_t *interface;
134         char *set = "set";
135
136         // Check if visited already
137         if(check_interfaces_visited(test_state, ns1, ns2)) {
138                 return;
139         }
140
141         assert(sprintf(eth_pairs[0], "%.9s_eth0", ns2->name) >= 0);
142         assert(sprintf(eth_pairs[1], "%.9s_peer0", ns1->name) >= 0);
143
144         // Delete veth pair if already exists
145         for(i = 0; i < 2; i++) {
146                 assert(sprintf(cmd, "ip link del %s 2>/dev/null", eth_pairs[i]) >= 0);
147                 cmd_ret = system(cmd);
148         }
149
150         // Create veth pair
151         assert(sprintf(cmd, "ip link add %s type veth peer name %s", eth_pairs[0], eth_pairs[1]) >= 0);
152         assert(system(cmd) == 0);
153
154         for(i = 0; i < 2; i++) {
155
156                 // Find interface handle that with it's peer interface
157                 interface =  get_peer_interface_handle(test_state, ns[i], ns[PEER_INDEX]);
158                 assert(interface);
159
160                 if(ns[i]->type != BRIDGE) {
161
162                         // Define interface name
163                         char *if_name;
164
165                         if(interface->if_name) {
166                                 if_name = interface->if_name;
167                         } else {
168                                 assert(sprintf(buff, "eth_%s", interface->if_peer) >= 0);
169                                 if_name = buff;
170                         }
171
172                         interface->if_name = strdup(if_name);
173
174                         assert(interface->if_name);
175
176                         // Connect one end of the the veth pair to the namespace's interface
177                         assert(sprintf(cmd, "ip link set %s netns %s name %s", eth_pairs[i], ns[i]->name, interface->if_name) >= 0);
178
179                         assert(system(cmd) == 0);
180                 } else {
181
182                         // Connect one end of the the veth pair to the bridge
183                         assert(sprintf(cmd, "ip link set %s master %s up\n", eth_pairs[i], ns[i]->name) >= 0);
184                         assert(system(cmd) == 0);
185                 }
186
187                 // Mark interfaces as connected
188                 interface->priv = set;
189                 interface = get_peer_interface_handle(test_state, ns[PEER_INDEX], ns[i]);
190                 assert(interface);
191                 interface->priv = set;
192         }
193 }
194
195 void netns_configure_ip_address(netns_state_t *test_state) {
196         int i, if_no, cmd_ret;
197         namespace_t *namespace;
198         interface_t *if_handle;
199         char cmd[200];
200
201         for(i = 0; i < test_state->num_namespaces; i++) {
202                 namespace = get_namespace_handle_by_index(test_state, i);
203
204                 for(if_no = 0; if_no < namespace->interfaces_no; if_no++) {
205                         if_handle = get_interface_handle_by_index(namespace, if_no);
206                         assert(if_handle);
207
208                         if(if_handle->if_addr && namespace->type != BRIDGE) {
209                                 assert(sprintf(cmd, "ip netns exec %s ip addr add %s dev %s", namespace->name, if_handle->if_addr, if_handle->if_name) >= 0);
210                                 assert(system(cmd) == 0);
211                                 assert(sprintf(cmd, "ip netns exec %s ip link set dev %s up", namespace->name, if_handle->if_name) >= 0);
212                                 assert(system(cmd) == 0);
213
214                                 if(if_handle->if_default_route_ip) {
215                                         char *route_ip = ipv4_str_remove_cidr(if_handle->if_default_route_ip);
216                                         assert(sprintf(cmd, "ip netns exec %s ip route add default via %s", namespace->name, route_ip) >= 0);
217                                         assert(system(cmd) == 0);
218                                         free(route_ip);
219                                 }
220                         }
221                 }
222         }
223 }
224
225 void netns_enable_all_nats(netns_state_t *test_state) {
226         int i, j;
227         namespace_t *namespace, *peer_namespace;
228         interface_t *interface_handle;
229         char cmd[200];
230         char *ip_addr;
231
232         for(i = 0; i < test_state->num_namespaces; i++) {
233                 namespace = get_namespace_handle_by_index(test_state, i);
234
235                 if(namespace->type == FULL_CONE) {
236                         assert(namespace->nat_arg);
237                         netns_fullcone_handle_t **nat_rules = namespace->nat_arg;
238                         char *eth0;
239
240                         for(j = 0; nat_rules[j]; j++) {
241                                 assert(nat_rules[j]->snat_to_source && nat_rules[j]->dnat_to_destination);
242
243                                 interface_handle = get_interface_handle_by_name(test_state, namespace, nat_rules[j]->snat_to_source);
244                                 assert(interface_handle);
245                                 eth0 = interface_handle->if_name;
246                                 ip_addr = ipv4_str_remove_cidr(interface_handle->if_addr);
247                                 assert(sprintf(cmd, "ip netns exec %s iptables -t nat -A POSTROUTING -o %s -j SNAT --to-source %s", namespace->name, eth0, ip_addr) >= 0);
248                                 assert(system(cmd) == 0);
249                                 free(ip_addr);
250
251                                 peer_namespace = find_namespace(test_state, nat_rules[j]->dnat_to_destination);
252                                 interface_handle = get_interface_handle_by_name(test_state, peer_namespace, namespace->name);
253                                 assert(interface_handle);
254
255                                 ip_addr = ipv4_str_remove_cidr(interface_handle->if_addr);
256                                 assert(sprintf(cmd, "ip netns exec %s iptables -t nat -A PREROUTING  -i %s -j DNAT --to-destination %s", namespace->name, eth0, ip_addr) >= 0);
257                                 assert(system(cmd) == 0);
258                                 free(ip_addr);
259                         }
260                 }
261         }
262 }
263
264 void netns_create_all_namespaces(netns_state_t *test_state) {
265         int i, j;
266         namespace_t *namespace, *peer_namespace;
267         interface_t *interfaces;
268
269         for(i = 0; i < test_state->num_namespaces; i++) {
270                 namespace = get_namespace_handle_by_index(test_state, i);
271
272                 // Delete the namespace if already exists
273                 netns_delete_namespace(namespace);
274
275                 // Create namespace
276
277                 if(namespace->type != BRIDGE) {
278                         netns_create_namespace(test_state, namespace);
279                 } else {
280                         netns_create_bridge(test_state, namespace);
281                 }
282         }
283 }
284
285 void netns_connect_all_namespaces(netns_state_t *test_state) {
286         int i, j;
287         namespace_t *namespace, *peer_namespace;
288         interface_t *interfaces;
289         interface_t *interface_handle;
290
291         for(i = 0; i < test_state->num_namespaces; i++) {
292                 namespace = get_namespace_handle_by_index(test_state, i);
293                 assert(namespace->interfaces);
294                 interfaces = namespace->interfaces;
295
296                 for(j = 0; j < namespace->interfaces_no; j++) {
297                         peer_namespace = find_namespace(test_state, interfaces[j].if_peer);
298                         assert(peer_namespace);
299                         netns_connect_namespaces(test_state, namespace, peer_namespace);
300                 }
301         }
302
303         // Reset all priv members of the interfaces
304
305         for(i = 0; i < test_state->num_namespaces; i++) {
306                 namespace = get_namespace_handle_by_index(test_state, i);
307                 assert(namespace->interfaces);
308
309                 for(j = 0; j < namespace->interfaces_no; j++) {
310                         interface_handle = get_interface_handle_by_index(namespace, j);
311                         assert(interface_handle);
312                         interface_handle->priv = NULL;
313                 }
314         }
315 }
316
317 void increment_ipv4_str(char *ip_addr, int ip_addr_size) {
318         uint32_t addr_int_n, addr_int_h;
319
320         assert(inet_pton(AF_INET, ip_addr, &addr_int_n) > 0);
321         addr_int_h = ntohl(addr_int_n);
322         addr_int_h = addr_int_h + 1;
323         addr_int_n = htonl(addr_int_h);
324         assert(inet_ntop(AF_INET, &addr_int_n, ip_addr, ip_addr_size));
325 }
326
327 void increment_ipv4_cidr_str(char *ip) {
328         int subnet;
329         assert(sscanf(ip, "%*d.%*d.%*d.%*d/%d", &subnet) >= 0);
330         char *ptr = strchr(ip, '/');
331         *ptr = '\0';
332         increment_ipv4_str(ip, INET6_ADDRSTRLEN);
333         sprintf(ip, "%s/%d", ip, subnet);
334 }
335
336 interface_t *netns_get_priv_addr(netns_state_t *test_state, const char *namespace_name) {
337         namespace_t *namespace_handle;
338         interface_t *interface_handle;
339         int if_no;
340
341         namespace_handle = find_namespace(test_state, namespace_name);
342         assert(namespace_handle);
343
344         for(if_no = 0; if_no < namespace_handle->interfaces_no; if_no++) {
345                 interface_handle = get_interface_handle_by_index(namespace_handle, if_no);
346
347                 if(!strcmp(namespace_handle->name, interface_handle->fetch_ip_netns_name)) {
348                         return interface_handle;
349                 }
350         }
351
352         return NULL;
353 }
354
355 void netns_add_default_route_addr(netns_state_t *test_state) {
356         int ns, if_no;
357         namespace_t *namespace_handle;
358         interface_t *interface_handle, *peer_interface_handle;
359
360         for(ns = 0; ns < test_state->num_namespaces; ns++) {
361                 namespace_handle = get_namespace_handle_by_index(test_state, ns);
362                 assert(namespace_handle);
363
364                 if(namespace_handle->type != HOST) {
365                         continue;
366                 }
367
368                 for(if_no = 0; if_no < namespace_handle->interfaces_no; if_no++) {
369                         interface_handle = get_interface_handle_by_index(namespace_handle, if_no);
370
371                         if(interface_handle->if_default_route_ip == NULL) {
372                                 peer_interface_handle = netns_get_priv_addr(test_state, interface_handle->fetch_ip_netns_name);
373                                 assert(peer_interface_handle);
374                                 interface_handle->if_default_route_ip  = ipv4_str_remove_cidr(peer_interface_handle->if_addr);
375                         } else {
376                                 char *dup = strdup(interface_handle->if_default_route_ip);
377                                 assert(dup);
378                                 interface_handle->if_default_route_ip = dup;
379                         }
380                 }
381         }
382 }
383
384 void netns_assign_ip_addresses(netns_state_t *test_state) {
385         int ns, j;
386         namespace_t *namespace_handle;
387         interface_t *interface_handle, *peer_interface_handle;
388         int sub_net;
389
390
391         char *addr = malloc(INET6_ADDRSTRLEN);
392         assert(addr);
393
394         if(test_state->public_net_addr) {
395                 assert(strncpy(addr, test_state->public_net_addr, INET6_ADDRSTRLEN));
396         } else {
397                 assert(strncpy(addr, DEFAULT_PUB_NET_ADDR, INET6_ADDRSTRLEN));
398         }
399
400         test_state->public_net_addr = addr;
401
402         for(ns = 0; ns < test_state->num_namespaces; ns++) {
403                 namespace_handle = get_namespace_handle_by_index(test_state, ns);
404                 assert(namespace_handle);
405
406                 if(namespace_handle->type == BRIDGE) {
407                         continue;
408                 }
409
410                 for(j = 0; j < namespace_handle->interfaces_no; j++) {
411                         interface_handle = get_interface_handle_by_index(namespace_handle, j);
412                         assert(interface_handle);
413
414                         if(interface_handle->if_addr) {
415                                 continue;
416                         }
417
418                         // If fetch ip net namespace name is given get IP address from it, else get a public IP address
419
420                         if(interface_handle->fetch_ip_netns_name) {
421                                 namespace_t *gw_netns_handle = find_namespace(test_state, interface_handle->fetch_ip_netns_name);
422                                 assert(gw_netns_handle);
423                                 assert(gw_netns_handle->static_config_net_addr);
424
425                                 increment_ipv4_cidr_str(gw_netns_handle->static_config_net_addr);
426                                 interface_handle->if_addr = strdup(gw_netns_handle->static_config_net_addr);
427                         } else {
428                                 increment_ipv4_cidr_str(test_state->public_net_addr);
429                                 interface_handle->if_addr = strdup(test_state->public_net_addr);
430
431                                 if(namespace_handle->type == HOST) {
432                                         if(interface_handle->if_default_route_ip) {
433                                                 char *dup = strdup(interface_handle->if_default_route_ip);
434                                                 assert(dup);
435                                                 interface_handle->if_default_route_ip = dup;
436                                         } else {
437                                                 interface_handle->if_default_route_ip = strdup(DEFAULT_GATEWAY_NET_ADDR);
438                                         }
439                                 }
440                         }
441                 }
442         }
443
444         netns_add_default_route_addr(test_state);
445 }
446
447 static void netns_namespace_init_pids(netns_state_t *test_state) {
448         int if_no;
449         namespace_t *namespace_handle;
450
451         for(if_no = 0; if_no < test_state->num_namespaces; if_no++) {
452                 namespace_handle = get_namespace_handle_by_index(test_state, if_no);
453                 assert(namespace_handle);
454                 namespace_handle->pid_nos = 0;
455                 namespace_handle->pids = NULL;
456         }
457 }
458
459 pid_t run_cmd_in_netns(netns_state_t *test_state, char *namespace_name, char *cmd_str) {
460         pid_t pid;
461         namespace_t *namespace_handle;
462         char cmd[1000];
463
464         assert(namespace_name && cmd_str);
465         namespace_handle = find_namespace(test_state, namespace_name);
466         assert(namespace_handle);
467
468         if((pid = fork()) == 0) {
469                 assert(daemon(1, 0) != -1);
470                 assert(sprintf(cmd, "ip netns exec %s %s", namespace_name, cmd_str) >= 0);
471                 assert(system(cmd) == 0);
472                 exit(0);
473         }
474
475         pid_t *pid_ptr;
476         pid_ptr = realloc(namespace_handle->pids, (namespace_handle->pid_nos + 1) * sizeof(pid_t));
477         assert(pid_ptr);
478         namespace_handle->pids = pid_ptr;
479         (namespace_handle->pids)[namespace_handle->pid_nos] = pid;
480         namespace_handle->pid_nos = namespace_handle->pid_nos + 1;
481
482         return pid;
483 }
484
485 static void *pthread_fun(void *arg) {
486         netns_thread_t *netns_arg = (netns_thread_t *)arg;
487         char namespace_path[100];
488         void *ret;
489         assert(sprintf(namespace_path, "/var/run/netns/%s", netns_arg->namespace_name) >= 0);
490         int fd = open(namespace_path, O_RDONLY);
491         assert(fd != -1);
492         assert(setns(fd, CLONE_NEWNET) != -1);
493
494         ret = (netns_arg->netns_thread)(netns_arg->arg);
495         pthread_detach(netns_arg->thread_handle);
496         pthread_exit(ret);
497 }
498
499 void run_node_in_namespace_thread(netns_thread_t *netns_arg) {
500         assert(netns_arg->namespace_name && netns_arg->netns_thread);
501         assert(!pthread_create(&(netns_arg->thread_handle), NULL, pthread_fun, netns_arg));
502 }
503
504 void netns_destroy_topology(netns_state_t *test_state) {
505         namespace_t *namespace_handle;
506         interface_t *interface_handle;
507         int if_no, j, i;
508         pid_t pid, pid_ret;
509
510         for(if_no = 0; if_no < test_state->num_namespaces; if_no++) {
511                 namespace_handle = get_namespace_handle_by_index(test_state, if_no);
512                 assert(namespace_handle->interfaces);
513
514                 for(i = 0; i < namespace_handle->pid_nos; i++) {
515                         pid = (namespace_handle->pids)[i];
516                         assert(kill(pid, SIGINT) != -1);
517                         pid_ret = waitpid(pid, NULL, WNOHANG);
518                         assert(pid_ret != -1);
519
520                         if(pid_ret == 0) {
521                                 fprintf(stderr, "pid: %d, is still running\n", pid);
522                         }
523                 }
524
525                 // Free interface name, interface address, interface default address etc.,
526                 // which are dynamically allocated and set the values to NULL
527
528                 for(j = 0; j < namespace_handle->interfaces_no; j++) {
529                         interface_handle = get_interface_handle_by_index(namespace_handle, j);
530                         assert(interface_handle);
531
532                         free(interface_handle->if_name);
533                         interface_handle->if_name = NULL;
534                         free(interface_handle->if_addr);
535                         interface_handle->if_addr = NULL;
536                         free(interface_handle->if_default_route_ip);
537                         interface_handle->if_default_route_ip = NULL;
538                 }
539
540                 // Delete namespace
541                 assert(netns_delete_namespace(namespace_handle) == 0);
542         }
543
544         free(test_state->public_net_addr);
545         test_state->public_net_addr = NULL;
546 }
547
548 bool netns_create_topology(netns_state_t *test_state) {
549
550         // (Re)create name-spaces and bridges
551         netns_create_all_namespaces(test_state);
552
553         // Connect namespaces and bridges(if any) with their interfaces
554         netns_connect_all_namespaces(test_state);
555
556         // Assign IP addresses for the interfaces in namespaces
557         netns_assign_ip_addresses(test_state);
558
559         // Configure assigned IP addresses with the interfaces in netns
560         netns_configure_ip_address(test_state);
561
562         // Enable all NATs
563         netns_enable_all_nats(test_state);
564
565         netns_namespace_init_pids(test_state);
566
567         return true;
568 }