summaryrefslogtreecommitdiffstats
path: root/input/flow/ulogd_inpflow_CTNL.c
blob: a31548edacd6d6696e142f699ee440c660086ddf (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
/* ulogd_input_CTNL.c, Version $Revision$
 *
 * ulogd input plugin for ctnetlink
 *
 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation
 */

#include <errno.h>
#include <ulogd/ulogd.h>
#include <libnfnetlink/libnfnetlink.h>
#include <libnfnetlink_conntrack/libnfnetlink_conntrack.h>

struct ctnl_pluginstance {
	struct nfct_handle *cth;
	struct ulogd_fd nfct_fd;
};


static struct ulogd_key ctnl_okeys[] = {
	{
		.type 	= ULOGD_RET_IPADDR,
		.flags 	= ULOGD_RETF_NONE,
		.key	= "ip.saddr",
		.ipfix	= { },
	},
	{
		.type	= ULOGD_RET_IPADDR,
		.flags	= ULOGD_RETF_NONE,
		.key	= "ip.daddr",
		.ipfix	= { },
	},
	{
		.type	= ULOGD_RET_UINT8,
		.flags	= ULOGD_RETF_NONE,
		.key	= "ip.protocol",
		.ipfix	= { },
	},
	{
		.type	= ULOGD_RET_UINT16,
		.flags 	= ULOGD_RETF_NONE,
		.key	= "tcp.sport",
		.ipfix	= {
			.vendor 	= IPFIX_VENDOR_IETF,
			.field_id 	= 7,
		},
	},
	{
		.type	= ULOGD_RET_UINT16,
		.flags 	= ULOGD_RETF_NONE,
		.key	= "tcp.dport",
		.ipfix	= {
			.vendor 	= IPFIX_VENDOR_IETF,
			.field_id 	= 11,
		},
	},
	{
		.type = ULOGD_RET_UINT32,
		.flags = ULOGD_RETF_NONE,
		.name = "raw.pktlen",
		.ipfix = { 
			.vendor 	= IPFIX_VENDOR_IETF,
			.field_id 	= 1,
		},
	},
	{
		.type = ULOGD_RET_UINT32,
		.flags = ULOGD_RETF_NONE,
		.name = "raw.pktcount",
		.ipfix = { 
			.vendor 	= IPFIX_VENDOR_IETF,
			.field_id 	= 2,
		},
	},

};

static int propagate_ct_flow(struct ulogd_pluginstance *upi, 
		             struct nfct_conntrack *ct,
			     int dir)
{
	struct ulogd_key *ret = upi->output;

	ret[0].u.value.ui32 = ct->tuple[dir].src.v4;
	ret[0].flags |= ULOGD_RETF_VALID;

	ret[1].u.value.ui32 = ct->tuple[dir].dst.v4;
	ret[1].flags |= ULOGD_RETF_VALID;

	ret[2].u.value.ui8 = ct->tuple[dir].protonum;
	ret[2].flags |= ULOGD_RETF_VALID;

	switch (ct->tuple[1].protonum) {
	case IPPROTO_TCP:
		ret[3].u.value.ui16 = ct->tuple[dir].l4src.tcp.port;
		ret[3].flags |= ULOGD_RETF_VALID;
		ret[4].u.value.ui16 = ct->tuple[dir].l4dst.tcp.port;
		ret[4].flags |= ULOGD_RETF_VALID;
		break;
	case IPPROTO_UDP:
		break;
	case IPPROTO_SCTP:
		break;
	}

	if (dir == NF_CT_DIR_ORIG && flags & NFCT_COUNTERS_ORIG ||
	    dir == NF_CT_DIR_REPLY && flags & NFCT_COUNTERS_REPLY) {
		ret[5].u.value.ui64 = ct->counters[dir].bytes;
		ret[5].flags |= ULOGD_RETF_VALID;

		ret[6].u.value.ui64 = ct->counters[dir].packets;
		ret[6].flags |= ULOGD_RETF_VALID;
	}
	
	return ulogd_propagate_results(upi);
}

static int propagate_ct(struct ulogd_pluginstance *upi,
			struct nfct_conntrack *ct)
{
	int rc;

	rc = propagate_ct_flow(upi, ct, NF_CT_DIR_ORIGINAL);
	if (rc < 0)
		return rc;
	return propagate_ct_flow(upi, ct, NF_CT_DIR_REPLY);
}

static int event_handler(void *arg, unsigned int flags, int type)
{
	struct nfct_conntrack *ct = foo;

	if (type == NFCT_MSG_NEW)
		/* FIXME: build hash table with timestamp of start of
		 * connection */
	} else if (type == NFCT_MSG_DESTROY)
		/* We have the final count of bytes for this connection */
	}

	return 0;
}

static int read_cb_ctnl(int fd, unsigned int what, void *param)
{
	struct ctnl_pluginstance *cpi = 
				(struct ulogd_ctnl_pluginstance *) param;

	if (!(what & ULOGD_FD_READ))
		return 0;

	/* FIXME: implement this */
	ctnl_event_conntrack(&cpi->cth, AF_INET);
}

static int constructor_ctnl(struct ulogd_pluginstance *upi)
{
	struct ctnl_pluginstance *cpi = 
			(struct ctnl_pluginstance *)upi->private;

	memset(cpi, NULL, sizeof(*cpi));

	/* FIXME: make eventmask configurable */
	cpi->cth = nfct_open(NFNL_SUBSYS_CTNETLINK, NF_NETLINK_CONNTRACK_NEW|
			     NF_NETLINK_CONNTRACK_DESTROY);
	if (!cph->cth) {
		print("error opening ctnetlink\n");
		return -1;
	}

	ctnl_register_callback(cpi->cth, &event_handler);

	cpi->nfct_fd.fd = nfct_fd(cpi->cth);
	cpi->nfct_fd.cb = &read_cb_ctnl;
	cpi->nfct_fd.data = cpi;
	cpi->nfct_fd.when = ULOGD_FD_READ;

	ulogd_register_fd(&cpi->nfct_fd);
	
	return &cpi->upi;
}


static int destructor_ctnl(struct ulogd_pluginstance *pi)
{
	struct ctnl_pluginstance *cpi = (void *) pi;
	int rc;

	rc = nfct_close(cpi->cth);
	if (rc < 0)
		return rc;

	return 0;
}

static struct ulogd_plugin ctnl_plugin = {
	.name = "CTNL",
	.input = {
		.type = ULOGD_DTYPE_SOURCE,
	},
	.output = {
		.keys = &ctnl_okeys,
		.num_keys = ARRAY_SIZE(ctnl_okeys),
		.type = ULOGD_DTYPE_FLOW,
	},
	.config_kset 	= ,
	.interp 	= ,
	.configure	=
	.start		= &constructor_ctnl,
	.stop		= &destructor_ctnl,
};

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

void init(void)
{
	ulogd_register_plugin(&ctnl_plugin);
}