summaryrefslogtreecommitdiffstats
path: root/tests/ct_mark_filter.c
blob: cd6dd273a223384bdeac2317c287849dced7c49d (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
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>

#include <libmnl/libmnl.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>

#include "nssocket.h"

static void tcp_echo_before_fin(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, false, NULL, NULL);
	sync_fifo(post);
}

static void tcp_echo_after_fin(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_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);
}

static void filter_mark_zero(const struct mnl_socket *nl,
			     const char *pre, const char *post)
{
	struct nfct_filter *filter = nfct_filter_create();
	struct nfct_filter_dump_mark mark = {val: 0, mask: 0};

	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);
	assert(nfct_filter_attach(mnl_socket_get_fd(nl), filter) != -1);
	nfct_filter_destroy(filter);
	tcp_echo(nl, pre, post);
	assert(nfct_filter_detach(mnl_socket_get_fd(nl)) != -1);
}

static void filter_mark_1_1(const struct mnl_socket *nl,
			    const char *pre, const char *post)
{
	struct nfct_filter *filter = nfct_filter_create();
	struct nfct_filter_dump_mark mark = {val: 1, mask: 1};

	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);
	assert(nfct_filter_attach(mnl_socket_get_fd(nl), filter) != -1);
	nfct_filter_destroy(filter);
	tcp_echo_after_fin(nl, pre, post);
	assert(nfct_filter_detach(mnl_socket_get_fd(nl)) != -1);
}

static void filter_mark_neg_1_1(const struct mnl_socket *nl,
				const char *pre, const char *post)
{
	struct nfct_filter *filter = nfct_filter_create();
	struct nfct_filter_dump_mark mark = {val: 1, mask: 1};

	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);
	assert(nfct_filter_set_logic(filter, NFCT_FILTER_MARK,
				     NFCT_FILTER_LOGIC_NEGATIVE) != -1);
	assert(nfct_filter_attach(mnl_socket_get_fd(nl), filter) != -1);
	nfct_filter_destroy(filter);
	tcp_echo_before_fin(nl, pre, post);
	assert(nfct_filter_detach(mnl_socket_get_fd(nl)) != -1);
}

static void filter_mark_neg_0_fffffffd(const struct mnl_socket *nl,
				       const char *pre, const char *post)
{
	struct nfct_filter *filter = nfct_filter_create();
	struct nfct_filter_dump_mark mark = {val: 0, mask: 0xfffffffd};

	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);
	assert(nfct_filter_set_logic(filter, NFCT_FILTER_MARK,
				     NFCT_FILTER_LOGIC_NEGATIVE) != -1);
	assert(nfct_filter_attach(mnl_socket_get_fd(nl), filter) != -1);
	nfct_filter_destroy(filter);
	tcp_echo_after_fin(nl, pre, post);
	assert(nfct_filter_detach(mnl_socket_get_fd(nl)) != -1);
}

static void filter_mark_max(const struct mnl_socket *nl,
			    const char *pre, const char *post)
{
	struct nfct_filter *filter = nfct_filter_create();
	struct nfct_filter_dump_mark mark;
	int i;

	for (i = 0; i < 126; i++) {
		/* does not match to mark value 3 */
		mark = (struct nfct_filter_dump_mark){val: 0, mask: 3};
		nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);
	}

	/* __FILTER_MARK_MAX      127, should be added */
	mark = (struct nfct_filter_dump_mark){val: 1, mask: 1};
	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);

	/* over __FILTER_MARK_MAX, should be ignored */
	mark = (struct nfct_filter_dump_mark){val: 0, mask: 0};
	nfct_filter_add_attr(filter, NFCT_FILTER_MARK, &mark);

	assert(nfct_filter_attach(mnl_socket_get_fd(nl), filter) != -1);
	nfct_filter_destroy(filter);
	tcp_echo_after_fin(nl, pre, post);
	assert(nfct_filter_detach(mnl_socket_get_fd(nl)) != -1);
}

int main(int argc, char *argv[])
{
	struct mnl_socket *nl;
	char *pre, *post;

	if (argc != 4) {
		fprintf(stderr, "usage: %s <netns> <pre_fifo> <post_fifo>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	pre = argv[2];
	post = argv[3];

	nl = mnl_event_nssocket(argv[1]);
	if (nl == NULL) {
		perror("init_mnl_socket");
		exit(EXIT_FAILURE);
	}

	filter_mark_zero(nl, pre, post);
	filter_mark_1_1(nl, pre, post);
	filter_mark_neg_1_1(nl, pre, post);
	filter_mark_neg_0_fffffffd(nl, pre, post);
	filter_mark_max(nl, pre, post);

	return fini_nssocket();
}