summaryrefslogtreecommitdiffstats
path: root/src/deprecated/extensions/libnetfilter_conntrack_tcp.c
blob: 9efdbb7be1990c3ddd9024e8a6bd7daf5fb64028 (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
/*
 * (C) 2005 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 */
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h> /* For htons */
#include <libnetfilter_conntrack/linux_nfnetlink_conntrack.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack_extensions.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>

static const char *states[] = {
	"NONE",
	"SYN_SENT",
	"SYN_RECV",
	"ESTABLISHED",
	"FIN_WAIT",
	"CLOSE_WAIT",
	"LAST_ACK",
	"TIME_WAIT",
	"CLOSE",
	"LISTEN"
};

static void parse_proto(struct nfattr *cda[], struct nfct_tuple *tuple)
{
	if (cda[CTA_PROTO_SRC_PORT-1])
		tuple->l4src.tcp.port =
			*(u_int16_t *)NFA_DATA(cda[CTA_PROTO_SRC_PORT-1]);
	if (cda[CTA_PROTO_DST_PORT-1])
		tuple->l4dst.tcp.port =
			*(u_int16_t *)NFA_DATA(cda[CTA_PROTO_DST_PORT-1]);
}

static void parse_protoinfo(struct nfattr *cda[], struct nfct_conntrack *ct)
{
	struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];

	/*
	 * Listen to me carefully: This is easy to trigger with events ;). 
	 * The conntrack event messages don't always contain all the
	 * information about a conntrack, just those fields that have changed.
	 * So you can receive a message about a TCP connection with no bits 
	 * talking about the private protocol information. 
	 *
	 * 						--pablo 05/10/31
	 */
	if (!cda[CTA_PROTOINFO_TCP-1])
		return;
	
	nfnl_parse_nested(tb,CTA_PROTOINFO_TCP_MAX, cda[CTA_PROTOINFO_TCP-1]);
	
	if (tb[CTA_PROTOINFO_TCP_STATE-1])
                ct->protoinfo.tcp.state =
                        *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
}

static void build_tuple_proto(struct nfnlhdr *req, int size,
			      struct nfct_tuple *t)
{
	nfnl_addattr_l(&req->nlh, size, CTA_PROTO_SRC_PORT,
		       &t->l4src.tcp.port, sizeof(u_int16_t));
	nfnl_addattr_l(&req->nlh, size, CTA_PROTO_DST_PORT,
		       &t->l4dst.tcp.port, sizeof(u_int16_t));
}

static void build_protoinfo(struct nfnlhdr *req, int size, 
			    struct nfct_conntrack *ct)
{
	struct nfattr *nest_proto;

	nest_proto = nfnl_nest(&req->nlh, size, CTA_PROTOINFO_TCP);
	nfnl_addattr_l(&req->nlh, size, CTA_PROTOINFO_TCP_STATE,
		       &ct->protoinfo.tcp.state, sizeof(u_int8_t));
	nfnl_nest_end(&req->nlh, nest_proto);
}

static int print_protoinfo(char *buf, union nfct_protoinfo *protoinfo)
{
	return(sprintf(buf, "%s ", states[protoinfo->tcp.state]));
}

static int print_proto(char *buf, struct nfct_tuple *tuple)
{
	return(sprintf(buf, "sport=%u dport=%u ", htons(tuple->l4src.tcp.port),
					          htons(tuple->l4dst.tcp.port)));
}

static int compare(struct nfct_conntrack *ct1,
		   struct nfct_conntrack *ct2,
		   unsigned int flags)
{
	if (flags & TCP_ORIG_SPORT)
		if (ct1->tuple[NFCT_DIR_ORIGINAL].l4src.tcp.port !=
		    ct2->tuple[NFCT_DIR_ORIGINAL].l4src.tcp.port)
			return 0;
	if (flags & TCP_ORIG_DPORT)
		if (ct1->tuple[NFCT_DIR_ORIGINAL].l4dst.tcp.port !=
		    ct2->tuple[NFCT_DIR_ORIGINAL].l4dst.tcp.port)
			return 0;
	if (flags & TCP_REPL_SPORT)
		if (ct1->tuple[NFCT_DIR_REPLY].l4src.tcp.port !=
		    ct2->tuple[NFCT_DIR_REPLY].l4src.tcp.port)
			return 0;
	if (flags & TCP_REPL_DPORT)
		if (ct1->tuple[NFCT_DIR_REPLY].l4dst.tcp.port !=
		    ct2->tuple[NFCT_DIR_REPLY].l4dst.tcp.port)
			return 0;
	if (flags & TCP_STATE)
		if (ct1->protoinfo.tcp.state != ct2->protoinfo.tcp.state)
			return 0;

	return 1;
}

static struct nfct_proto tcp = {
	.name 			= "tcp",
	.protonum		= IPPROTO_TCP,
	.parse_protoinfo	= parse_protoinfo,
	.parse_proto		= parse_proto,
	.build_tuple_proto	= build_tuple_proto,
	.build_protoinfo	= build_protoinfo,
	.print_protoinfo	= print_protoinfo,
	.print_proto		= print_proto,
	.compare		= compare,
	.version		= VERSION
};

static void __attribute__ ((constructor)) init(void);

static void init(void)
{
	nfct_register_proto(&tcp);
}