summaryrefslogtreecommitdiffstats
path: root/tests/nssocket.c
blob: 114938c3839505ed75368dcc9d3157da80f0acbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include <libmnl/libmnl.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
#include "config.h"
#include "nssocket.h"

int fdpair[2];
#define PARENT_FD (fdpair[0])
#define CHILD_FD (fdpair[1])

pid_t child_pid;

void add_child(pid_t pid)
{
	/* XXX: check excess MAX_CHILD */
	children[nchild++] = pid;
}

static int get_unaligned_int(const void *s)
{
	int x;
	memcpy(&x, s, sizeof(x));
	return x;
}

static void put_unaligned_int(void *d, int x)
{
	memcpy(d, &x, sizeof(x));
}

/*
 * message exchange via socketpair using send/recv msg()
 *
 * - use cdata:
 *   cdata represents a file descriptor
 *   cmd[0] means -errno
 *
 * - without cdata:
 *   cmd[0] means:
 *   > 0:  command
 *   == 0: sync, echo
 *   < 0:  -errno
 *
 * it's an given fact that tx() and rx() never fail.
 */
ssize_t tx(int fd, int *cmd, uint8_t cmdlen, int cdata)
{
	struct msghdr msg;
	struct iovec iov[cmdlen];
	size_t cmsglen = CMSG_SPACE(sizeof(int));
	char control[CMSG_SPACE(sizeof(int))];
	struct cmsghdr *cmsg;
	int i;

	memset(&msg, 0, sizeof(struct msghdr));
	memset(iov, 0, sizeof(struct iovec) * cmdlen);

	msg.msg_iov = iov;
	msg.msg_iovlen = cmdlen;
	for (i = 0; i < cmdlen; i++) {
		iov[i].iov_len = sizeof(int);
		iov[i].iov_base = &cmd[i];
	}
	if (cdata) {
		msg.msg_control = control;
		msg.msg_controllen = cmsglen;
		cmsg = CMSG_FIRSTHDR(&msg);
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_RIGHTS;
		put_unaligned_int(CMSG_DATA(cmsg), cdata);
	}

	return sendmsg(fd, &msg, 0);
}

ssize_t rx(int fd, int *cmd, uint8_t cmdlen, int *cdata)
{
	struct msghdr msg;
	struct iovec iov[cmdlen];
	size_t cmsglen = CMSG_SPACE(sizeof(int));
	char control[CMSG_SPACE(sizeof(int))];
	struct cmsghdr *cmsg;
	ssize_t ret;
	int i;

	memset(&msg, 0, sizeof(struct msghdr));
	memset(iov, 0, sizeof(struct iovec));

	msg.msg_iov = iov;
	msg.msg_iovlen = cmdlen;
	for (i = 0; i < cmdlen; i++) {
		iov[i].iov_len = sizeof(int);
		iov[i].iov_base = &cmd[i];
	}
	if (cdata != NULL) {
		msg.msg_control = control;
		msg.msg_controllen = cmsglen;
	}

	ret = recvmsg(fd, &msg, 0);
	if (ret == -1) {
		perror("recvmsg");
		return ret;
	}

	if (cdata == NULL)
		return ret;

	cmsg = CMSG_FIRSTHDR(&msg);
	if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN(sizeof(int))
	    || cmsg->cmsg_level != SOL_SOCKET
	    || cmsg->cmsg_type != SCM_RIGHTS) {
		errno = EBADMSG;
		return -1;
	}
	*cdata = get_unaligned_int(CMSG_DATA(cmsg));

	return ret;
}

int tx_cmd(int fd, int cmd)
{
	return tx(fd, &cmd, 1, 0);
}

int rx_cmd(int fd)
{
	int cmd;
	if (rx((fd), &cmd, 1, NULL) == -1)
		return -1;
	return cmd;
}

int tx_fd(int fd1, int fd2, int e)
{
	return tx(fd1, &e, 1, fd2);
}

int rx_fd(int fd1)
{
	int e, fd2;

	if (rx(fd1, &e, 1, &fd2) == -1)
		return -1;

	errno = -e;
	return fd2;
}

/*
 * copy from ip/ipnetns.c::iproute2
 */
#ifndef HAVE_SETNS
#include <sys/syscall.h>
static int setns(int fd, int nstype)
{
#ifdef __NR_setns
	return syscall(__NR_setns, fd, nstype);
#else
	errno = ENOSYS;
	return -1;
#endif
}
#endif /* HAVE_SETNS */

#define NETNS_RUN_DIR "/var/run/netns"
static int netns_setup(const char *name)
{
	/* Setup the proper environment for apps that are not netns
	 * aware, and execute a program in that environment.
	 */
	char net_path[MAXPATHLEN];
	int netns;

	snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
	netns = open(net_path, O_RDONLY | O_CLOEXEC);
	if (netns < 0) {
		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
			name, strerror(errno));
		return -1;
	}

	if (setns(netns, CLONE_NEWNET) < 0) {
		fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
			name, strerror(errno));
		return -1;
	}

	if (unshare(CLONE_NEWNS) < 0) {
		fprintf(stderr, "unshare failed: %s\n", strerror(errno));
		return -1;
	}
	/* Don't let any mounts propagate back to the parent */
	if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
		fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
			strerror(errno));
		return -1;
	}
	/* Mount a version of /sys that describes the network namespace */
	if (umount2("/sys", MNT_DETACH) < 0) {
		fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
		return -1;
	}
	if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
		fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
		return -1;
	}

	return 0;
}

static void child(const char *nsname)
{
	int cmd = CMD_SYNC;
	int params[3]; /* XXX: magic number, see enum CALL_ */
	int sockfd;

	if (netns_setup(nsname) == -1)
		child_exit("netns_setup", EXIT_FAILURE);

	/* sync with parent */
	if (tx_cmd(CHILD_FD, CMD_SYNC) == -1)
		child_exit("tx_cmd", EXIT_FAILURE);

	/* waiting cmd */
	while (1) {
		debug_ns("child waiting for cmd...\n");
		cmd = rx_cmd(CHILD_FD);
		switch (cmd) {
		case CMD_DONE:
			debug_ns("child received CMD_DONE - exiting\n");
			close(CHILD_FD);
			child_exit("receive CMD_DONE", EXIT_SUCCESS);
			break;
		case CMD_SOCKET:
			if (rx(CHILD_FD, params, 3, NULL) == -1)
				child_exit("rx", EXIT_FAILURE);
			debug_ns("child received CMD_SOCKET -"
				 " domain: %d, type: %d, protocol: %d\n",
				 params[0], params[1], params[2]);
			sockfd = socket(params[0], params[1], params[2]);
			if (tx_fd(CHILD_FD, sockfd, -errno) == -1)
				child_exit("tx_fd", EXIT_FAILURE);
			break;
		default:
			debug_ns("child received unknown cmd: %d\n", cmd);
			child_exit("receive unknown cmd", EXIT_FAILURE);
			break;
		}
	}
}

/*
 * kill all the other registered child by SIGKILL
 *
 * SIGCHLD will not be raised if child has killed in SIGABRT handler
 */
static void sigchld_handler(int signum)
{
	pid_t pid;
	int status, i, fail = 0;

	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
		debug_ns("receive SIGCHLD - pid: %d\n", pid);
		if (WIFEXITED(status))
			fail |= WEXITSTATUS(status);
		else if (WIFSIGNALED(status) || WCOREDUMP(status))
			fail |= status;
		if (pid == child_pid)
			child_pid = 0;
		for (i = 0; i < nchild; i++)
			if (children[i] == pid)
				children[i] = 0;
			else
				kill(children[i], SIGKILL);
	}
	if (pid == -1 && errno != ECHILD)
		fail |= errno;

	/* overdoing? kill myself
	 * if (fail) kill(0, SIGKILL);
	 */
}

/*
 * core public API
 */
int init_nssocket(const char *nsname)
{
	pid_t pid;
	struct sigaction sa;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1)
		return -1;

	sigemptyset(&sa.sa_mask);
	sa.sa_handler = sigchld_handler;
	sa.sa_flags = SA_NOCLDSTOP;
	if (sigaction(SIGCHLD, &sa, NULL) == -1)
		return -1;

	fflush(stdout);
	pid = fork();
	switch (pid) {
	case -1:
		return -1;
		break;
	case 0:
		child(nsname); /* not return */
		break;
	default:
		child_pid = pid;
		add_child(pid);
		if (rx_cmd(PARENT_FD) < 0) {
			parent_fail("rx_cmd");
			return -1;
		}
		break;
	}

	return 0;
}

int fini_nssocket(void)
{
	int status;
	sigset_t block_mask;
	pid_t pid;

	sigemptyset(&block_mask);
	sigaddset(&block_mask, SIGCHLD);
	if (sigprocmask(SIG_SETMASK, &block_mask, NULL) == -1)
		return -1;
	tx_cmd(PARENT_FD, CMD_DONE);
	close(PARENT_FD);
	pid = waitpid(child_pid, &status, 0);
	child_pid = 0;
	if (pid < 0)
		return -1;
	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
		return 0;

	return status;
}

int nssocket(int domain, int type, int protocol)
{
	int cmd[] = {CMD_SOCKET, domain, type, protocol};

	if (child_pid == 0 || kill(child_pid, 0) == -1) {
		errno = ECHILD;
		return -1;
	}
	tx(PARENT_FD, cmd, 4, 0);
	return rx_fd(PARENT_FD);
}

/*
 * utils API
 */
int debug_nfct_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct;
	uint32_t type = NFCT_T_UNKNOWN;
	char buf[4096];

	switch(nlh->nlmsg_type & 0xFF) {
	case IPCTNL_MSG_CT_NEW:
		if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
			type = NFCT_T_NEW;
		else
			type = NFCT_T_UPDATE;
		break;
	case IPCTNL_MSG_CT_DELETE:
		type = NFCT_T_DESTROY;
		break;
	}

	ct = nfct_new();
	if (ct == NULL)
		return MNL_CB_OK;

	nfct_nlmsg_parse(nlh, ct);
	nfct_snprintf(buf, sizeof(buf), ct, type, NFCT_O_DEFAULT, 0);
	debug("%s\n", buf);
	nfct_destroy(ct);

	return MNL_CB_OK;
}

struct mnl_socket *mnl_nssocket_open(int bus)
{
	int fd;
	struct mnl_socket *nl;

	fd = nssocket(AF_NETLINK, SOCK_RAW, bus);
	if (fd == -1)
		return NULL;

	nl = mnl_socket_fdopen(fd);
	if (nl == NULL) {
		close(fd);
		return NULL;
	}
	return nl;
}

/*
 * assert utilities
 */
struct nf_conntrack *author_new(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct;

	assert((nlh->nlmsg_type & 0xFF) == IPCTNL_MSG_CT_NEW);
	assert(nlh->nlmsg_flags == (NLM_F_CREATE | NLM_F_EXCL));
	ct = nfct_new();
	assert(ct != NULL);
	assert(nfct_nlmsg_parse((nlh), ct) == 0);
	assert_proto(ct, AF_INET, *(uint8_t *) data);
	assert_inaddr(ct, VETH_PARENT_ADDR, VETH_CHILD_ADDR);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_SEEN_REPLY) == 0);
	timeout.tv_sec = nfct_get_attr_u32(ct, ATTR_TIMEOUT) + 1;

	return ct;
}

struct nf_conntrack *author_update(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct;

	assert((nlh->nlmsg_type & 0xFF) == IPCTNL_MSG_CT_NEW);
	assert(nlh->nlmsg_flags == 0);
	ct = nfct_new();
	assert(ct != NULL);
	assert(nfct_nlmsg_parse((nlh), ct) == 0);
	assert_proto(ct, AF_INET, *(uint8_t *) data);
	assert_inaddr(ct, VETH_PARENT_ADDR, VETH_CHILD_ADDR);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_SEEN_REPLY));
	timeout.tv_sec = nfct_get_attr_u32(ct, ATTR_TIMEOUT) + 1;

	return ct;
}

struct nf_conntrack *author_destroy(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct;

	assert((nlh->nlmsg_type & 0xFF) == IPCTNL_MSG_CT_DELETE);
	assert(nlh->nlmsg_flags == 0);
	ct = nfct_new();
	assert(ct != NULL);
	assert(nfct_nlmsg_parse((nlh), ct) == 0);
	assert_proto(ct, AF_INET, *(uint8_t *) data);
	assert_inaddr(ct, VETH_PARENT_ADDR, VETH_CHILD_ADDR);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_SEEN_REPLY));

	return ct;
}

void assert_proto(const struct nf_conntrack *ct,
		  uint8_t l3proto, uint8_t l4proto)
{
	assert(nfct_get_attr_u8(ct, ATTR_ORIG_L3PROTO) == l3proto);
	assert(nfct_get_attr_u8(ct, ATTR_REPL_L3PROTO) == l3proto);
	assert(nfct_get_attr_u8(ct, ATTR_ORIG_L4PROTO) == l4proto);
	assert(nfct_get_attr_u8(ct, ATTR_REPL_L4PROTO) == l4proto);
}

void assert_inaddr(const struct nf_conntrack *ct,
		   const char *src, const char *dst)
{
	struct in_addr addr;
	assert(inet_aton((src), &addr) != 0);
	assert(nfct_get_attr_u32((ct), ATTR_ORIG_IPV4_SRC) == addr.s_addr);
	assert(nfct_get_attr_u32((ct), ATTR_REPL_IPV4_DST) == addr.s_addr);
	assert(inet_aton((dst), &addr) != 0);
	assert(nfct_get_attr_u32((ct), ATTR_ORIG_IPV4_DST) == addr.s_addr);
	assert(nfct_get_attr_u32((ct), ATTR_REPL_IPV4_SRC) == addr.s_addr);
}

void assert_port(const struct nf_conntrack *ct,
		 uint16_t src, uint16_t dst)
{
	if ((src)) {
		assert(nfct_get_attr_u16((ct), ATTR_ORIG_PORT_SRC) == htons((src)));
		assert(nfct_get_attr_u16((ct), ATTR_REPL_PORT_DST) == htons((src)));
	}
	if ((dst)) {
		assert(nfct_get_attr_u16((ct), ATTR_ORIG_PORT_DST) == htons((dst)));
		assert(nfct_get_attr_u16((ct), ATTR_REPL_PORT_SRC) == htons((dst)));
	}
}

void assert_typecode(const struct nf_conntrack *ct,
		     uint8_t type, uint8_t code)
{
	assert(nfct_get_attr_u8((ct), ATTR_ICMP_TYPE) == type);
	assert(nfct_get_attr_u8((ct), ATTR_ICMP_CODE) == code);
}

int cb_icmp_new(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_new(nlh, data);
	assert_typecode(ct, ICMP_TYPE, ICMP_CODE);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_icmp_update(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_typecode(ct, ICMP_TYPE, ICMP_CODE);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_icmp_destroy(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_destroy(nlh, data);
	assert_typecode(ct, ICMP_TYPE, ICMP_CODE);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_udp_new(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_new(nlh, data);
	assert_port(ct, 0, DSTPORT);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_udp_update(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_udp_destroy(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_destroy(nlh, data);
	assert_port(ct, 0, DSTPORT);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_new(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_new(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_SYN_SENT);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_syn_recv(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_SYN_RECV);
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_established(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_ESTABLISHED);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_ASSURED));
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_fin_wait(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_FIN_WAIT);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_ASSURED));
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_close_wait(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_CLOSE_WAIT);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_ASSURED));
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_close(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_update(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_get_attr_u8(ct, ATTR_TCP_STATE) == TCP_CONNTRACK_CLOSE);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_ASSURED));
	nfct_destroy(ct);
	return MNL_CB_OK;
}

int cb_tcp_destroy(const struct nlmsghdr *nlh, void *data)
{
	struct nf_conntrack *ct = author_destroy(nlh, data);
	assert_port(ct, 0, DSTPORT);
	assert(nfct_attr_is_set(ct, ATTR_TCP_STATE) == 0);
	assert((nfct_get_attr_u32(ct, ATTR_STATUS) & IPS_ASSURED));
	nfct_destroy(ct);
	return MNL_CB_OK;
}

void tcp_echo(const struct mnl_socket *nl,
	      const char *pre, const char *post)
{
	uint8_t proto = IPPROTO_TCP;

	sync_fifo(pre);
	timeout.tv_sec = INIT_TIMEOUT;
	handle_qacb(nl, true, cb_tcp_new, &proto);
	handle_qacb(nl, true, cb_tcp_syn_recv, &proto);
	handle_qacb(nl, true, cb_tcp_established, &proto);
	handle_qacb(nl, true, cb_tcp_fin_wait, &proto);
	handle_qacb(nl, true, cb_tcp_close_wait, &proto);
	handle_qacb(nl, true, cb_tcp_close, &proto);
	handle_qacb(nl, true, cb_tcp_destroy, &proto);
	handle_qacb(nl, false, NULL, NULL);
	sync_fifo(post);
}

int handle_qacb(const struct mnl_socket *nl, bool should_receive,
		int(*cb)(const struct nlmsghdr *nlh, void *data), void *data)
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	fd_set rfds;
	int ret, fd = mnl_socket_get_fd(nl);
	bool receive_nfnl;

	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);
	if (select(fd + 1, &rfds, NULL, NULL, &timeout) < 0)
		child_exit("select", EXIT_FAILURE);
	receive_nfnl = FD_ISSET(fd, &rfds);
	if (should_receive) {
		assert(receive_nfnl == true);
	} else {
		assert(receive_nfnl == false);
		return MNL_CB_ERROR;
	}

	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
	if (ret == -1)
		child_exit("mnl_socket_recvfrom", EXIT_FAILURE);
	mnl_cb_run(buf, ret, 0, 0, debug_nfct_cb, NULL);
	if (cb != NULL) {
		ret = mnl_cb_run(buf, ret, 0, 0, cb, data);
		if (ret == -1)
			child_exit("mnl_cb_run", EXIT_FAILURE);
		return ret;
	}

	return MNL_CB_OK;
}

static void sigabrt_handler(int signum)
{
	fini_nssocket();
}

struct mnl_socket *mnl_event_nssocket(const char *nsname)
{
	struct mnl_socket *nl;
	struct sigaction sa;

	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = sigabrt_handler;
	if (sigaction(SIGABRT, &sa, NULL) == -1)
		return NULL;

	if (init_nssocket(nsname) == -1)
		return NULL;

	nl = mnl_nssocket_open(NETLINK_NETFILTER);
	if (nl == NULL)
		return NULL;
	if (mnl_socket_bind(nl, NF_NETLINK_CONNTRACK_NEW |
			    NF_NETLINK_CONNTRACK_UPDATE |
			    NF_NETLINK_CONNTRACK_DESTROY,
			    MNL_SOCKET_AUTOPID) < 0) {
		parent_fail("mnl_socket_bind");
		mnl_socket_close(nl);
		return NULL;
	}

	return nl;
}

void sync_fifo(const char *name)
{
	struct stat statbuf;
	int fd = open(name, O_WRONLY);
	if (fd == -1) {
		parent_fail("open fifo");
		exit(EXIT_FAILURE);
	}
	if (fstat(fd, &statbuf) == -1) {
		parent_fail("fstat fifo");
		exit(EXIT_FAILURE);
	}
	if (!S_ISFIFO(statbuf.st_mode)) {
		parent_fail("S_ISFIFO");
		exit(EXIT_FAILURE);
	}
	close(fd);
}