summaryrefslogtreecommitdiffstats
path: root/input/flow/ulogd_inpflow_CTNL.c
blob: 6f47bad7fcb5b0341012c0948ccbc82549064cd8 (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
/* 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 <ulogd/ulogd.h>
#include <libnfnetlink/libnfnetlink.h>
#include <libnfnetlink_conntrack/libnfnetlink_conntrack.h>

static struct ulogd_ctnl_pluginstance {
	struct ulogd_pluginstance upi;
	struct ctnl_handle cth;
	struct ulogd_fd ctnl_fd;
};

static int ctnl_parser(struct ulogd_pluginstance *pi,
		       struct nfattr *attr, struct nlmsghdr *nlh)
{
	struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
	unsigned int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
	struct ip_conntrack_tuple *orig;
	struct cta_countrs *ctr;

	/* FIXME: what about reply direction ? */
	while (NFA_OK(attr, attrlen)) {
		switch (attr->nfa_type) {
		case CTA_ORIG:
			orig = NFA_DATA(attr);
			pi->output.keys[0].u.ui32 = orig->src.ip;
			pi->output.keys[1].u.ui32 = orig->dst.ip;
			pi->output.keys[2].u.value.ui8 = orig->dst.protonum;
			/* FIXME: l4 port numbers */
			break;
		case CTA_COUNTERS:
			ctr = NFA_DATA(attr);
			pi->output.keys[5].u.value.ui32 = ctr->orig.bytes;
			pi->output.keys[6].u.value.ui32 = ctr->prog.packets;
			break;
		}
		attr = NFA_NEXT(attr, attrlen);
	}
	return 0;
}

static int event_handler(struct sockaddr_nl *sock, struct nlmsghdr *nlh,
			 void *arg)
{
	struct nfgenmsg *nfmsg;
	struct nfattr *nfa;
	int min_len = 0;
	int type = NFNL_MSG_TYPE(nlh->nlmsg_type);

	nfmsg = NLMSG_DATA(nlh);

	min_len = sizeof(struct nfgenmsg);
	if (nlh->nlmsg_len < min_len)
		return -EINVAL;

	if (type == IPCTNL_MSG_CT_NEW && flags & NLM_F_CREATE) {
		/* FIXME: build hash table with timestamp of start of
		 * connection */
	} else if (type == IPCTNL_MSG_CT_DELETE) {
		/* We have the final count of bytes for this connection */
	}
	return 0;
}

static struct ctnl_msg_handler new_h = {
	.type = IPCTNL_MSG_CT_NEW,
	.handler = event_handler,
};
static struct ctnl_msg_Handler destroy_h = {
	.type = IPCTNL_MSG_CT_DELETE,
	.handler = event_handler,
};

static int read_cb_ctnl(int fd, unsigned int what, void *param)
{
	struct ulogd_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 struct ulogd_pluginstance *constructor_ctnl(struct ulogd_plugin *pl)
{
	struct ulogd_ctnl_pluginstance *cpi = malloc(sizeof *cpi);

	if (!cpu)
		return NULL;

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

	cpi->plugin = pl;
	cpi->input = FIXME;
	cpi->output = FIXME;

	if (ctnl_open(&cpi->cth, NFGRP_IPV4_CT_TCP|NFGRP_IPV4_CT_UDP) < 0) {
		print("error\n");
		return NULL; 
	}

	ctnl_register_handler(&cpi->cth, &new_h);
	ctnl_register_handler(&cpi->cth, &destroy_h);
	
	/* FIXME: ctnl interface must allow usage of external select
	 * loop */
	cpi->ctnl_fd.fd = ctnl_get_fd(&cpi->cth);
	cpi->ctnl_fd.cb = &read_cb_ctnl;
	cpi->ctnl_fd.data = cpi;

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


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

	if (ctnl_close(&cpi->cth) < 0) {
		print("error2\n");
		return -1;
	}

	return 0;
}

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 struct ulogd_plugin ctnl_plugin = {
	.name = "CTNL",
	.input = {
		.keys = NULL,
		.num_keys = 0,
		.type = ULOGD_DTYPE_NULL,
	},
	.output = {
		.keys = &ctnl_okeys,
		.num_keys = sizeof(ctnl_okeys)/sizeof(struct ulogd_key),
		.type = ULOGD_DTYPE_FLOW,
	},
	.interp = ,
	.constructor = &constructor_ctnl,
	.descructor = &destructor_ctnl,
	.config_kset = ,
};