summaryrefslogtreecommitdiff
path: root/tests/conntrackd/cthelper/l4_tcp.c
blob: f27c85dc1c0c79368cbf57f6780934300820446c (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
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include "proto.h"

#include <libnetfilter_conntrack/libnetfilter_conntrack.h>

#define PRINT_CMP(...)

static void l4_tcp_ct_build_tuple(const uint8_t *pkt, struct nf_conntrack *ct)
{
	const struct tcphdr *tcph = (const struct tcphdr *)pkt;

	nfct_set_attr_u8(ct, ATTR_ORIG_L4PROTO, IPPROTO_TCP);
	nfct_set_attr_u8(ct, ATTR_REPL_L4PROTO, IPPROTO_TCP);
	nfct_set_attr_u16(ct, ATTR_ORIG_PORT_SRC, tcph->source);
	nfct_set_attr_u16(ct, ATTR_ORIG_PORT_DST, tcph->dest);
	nfct_set_attr_u16(ct, ATTR_REPL_PORT_SRC, tcph->dest);
	nfct_set_attr_u16(ct, ATTR_REPL_PORT_DST, tcph->source);
}

static int l4_tcp_ct_cmp_tuple_orig(const uint8_t *pkt, struct nf_conntrack *ct)
{
	const struct tcphdr *tcph = (const struct tcphdr *)pkt;

	PRINT_CMP("cmp_orig tcph->source: %u == %u\n",
		tcph->source, nfct_get_attr_u16(ct, ATTR_ORIG_PORT_SRC));
	PRINT_CMP("cmp_orig tcph->dest: %u == %u\n",
		tcph->dest, nfct_get_attr_u16(ct, ATTR_ORIG_PORT_DST));

	if (tcph->source == nfct_get_attr_u16(ct, ATTR_ORIG_PORT_SRC) &&
	    tcph->dest == nfct_get_attr_u16(ct, ATTR_ORIG_PORT_DST))
		return 1;

	return 0;
}

static int
l4_tcp_ct_cmp_tuple_repl(const uint8_t *pkt, struct nf_conntrack *ct)
{
	const struct tcphdr *tcph = (const struct tcphdr *)pkt;

	PRINT_CMP("cmp_repl tcph->source: %u == %u\n",
		tcph->source, nfct_get_attr_u16(ct, ATTR_REPL_PORT_SRC));
	PRINT_CMP("cmp_repl tcph->dest: %u == %u\n",
		tcph->dest, nfct_get_attr_u16(ct, ATTR_REPL_PORT_DST));

	if (tcph->source == nfct_get_attr_u16(ct, ATTR_REPL_PORT_SRC) &&
	    tcph->dest == nfct_get_attr_u16(ct, ATTR_REPL_PORT_DST))
		return 1;

	return 0;
}

static int
l4_tcp_ct_cmp_port(struct nf_conntrack *ct, uint16_t port)
{
	PRINT_CMP("cmp_port src: %u == %u\n",
		port, nfct_get_attr_u16(ct, ATTR_ORIG_PORT_SRC));
	PRINT_CMP("cmp_port dst: %u == %u\n",
		port, nfct_get_attr_u16(ct, ATTR_ORIG_PORT_DST));

	if (port == nfct_get_attr_u16(ct, ATTR_ORIG_PORT_SRC) ||
	    port == nfct_get_attr_u16(ct, ATTR_ORIG_PORT_DST))
		return 1;

	return 0;
}

static int l4_tcp_pkt_no_data(const uint8_t *pkt)
{
	const struct tcphdr *tcph = (const struct tcphdr *)pkt;
	return tcph->syn || tcph->fin || tcph->rst || !tcph->psh;
}

static struct cthelper_proto_l4_helper tcp = {
	.l4protonum	= IPPROTO_TCP,
	.l4ct_build	= l4_tcp_ct_build_tuple,
	.l4ct_cmp_orig	= l4_tcp_ct_cmp_tuple_orig,
	.l4ct_cmp_repl	= l4_tcp_ct_cmp_tuple_repl,
	.l4ct_cmp_port	= l4_tcp_ct_cmp_port,
	.l4pkt_no_data	= l4_tcp_pkt_no_data,
};

void l4_tcp_init(void)
{
	cthelper_proto_l4_helper_register(&tcp);
}