summaryrefslogtreecommitdiffstats
path: root/input/sum/ulogd_inpflow_NFACCT.c
blob: 152cd487fc7f5aae6f178acb85f0ffffd2f9ac01 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* ulogd_input_NFACCT.c
 *
 * ulogd input plugin for nfacct
 *
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 * (C) 2012 by Intra2net AG <http://www.intra2net.com>
 *
 * 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 <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>

#include <ulogd/ulogd.h>
#include <ulogd/timer.h>

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

struct nfacct_pluginstance {
	struct mnl_socket	*nl;
	uint32_t		portid;
	uint32_t		seq;
	struct ulogd_fd		ufd;
	struct ulogd_timer	timer;
};

static struct config_keyset nfacct_kset = {
	.ces = {
		{
			.key	 = "pollinterval",
			.type	 = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u.value = 0,
		},
	},
	.num_ces = 1,
};
#define pollint_ce(x)	(x->ces[0])

enum ulogd_nfacct_keys {
	ULOGD_NFACCT_NAME,
	ULOGD_NFACCT_PKTS,
	ULOGD_NFACCT_BYTES,
};

static struct ulogd_key nfacct_okeys[] = {
	[ULOGD_NFACCT_NAME] = {
		.type	= ULOGD_RET_STRING,
		.flags	= ULOGD_RETF_NONE,
		.name	= "sum.name",
	},
	[ULOGD_NFACCT_PKTS] = {
		.type	= ULOGD_RET_UINT64,
		.flags	= ULOGD_RETF_NONE,
		.name	= "sum.pkts",
	},
	[ULOGD_NFACCT_BYTES] = {
		.type	= ULOGD_RET_UINT64,
		.flags	= ULOGD_RETF_NONE,
		.name	= "sum.bytes",
	},
};

static void
propagate_nfacct(struct ulogd_pluginstance *upi, struct nfacct *nfacct)
{
	struct ulogd_key *ret = upi->output.keys;

	okey_set_ptr(&ret[ULOGD_NFACCT_NAME],
			(void *)nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME));
	okey_set_u64(&ret[ULOGD_NFACCT_PKTS],
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS));
	okey_set_u64(&ret[ULOGD_NFACCT_BYTES],
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES));

	ulogd_propagate_results(upi);
}

static void
do_propagate_nfacct(struct ulogd_pluginstance *upi, struct nfacct *nfacct)
{
	struct ulogd_pluginstance *npi = NULL;

	llist_for_each_entry(npi, &upi->plist, plist)
		propagate_nfacct(npi, nfacct);

	propagate_nfacct(upi, nfacct);
}

static int nfacct_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nfacct *nfacct;
	struct ulogd_pluginstance *upi = data;

	nfacct = nfacct_alloc();
	if (nfacct == NULL) {
		ulogd_log(ULOGD_ERROR, "OOM");
		goto err;
	}

	if (nfacct_nlmsg_parse_payload(nlh, nfacct) < 0) {
		ulogd_log(ULOGD_ERROR, "Error parsing nfacct message");
		goto err_free;
	}

	do_propagate_nfacct(upi, nfacct);

err_free:
	nfacct_free(nfacct);
err:
	return MNL_CB_OK;
}

static int nfacct_read_cb(int fd, unsigned int what, void *param)
{
	int ret;
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct ulogd_pluginstance *upi = param;
	struct nfacct_pluginstance *cpi =
		(struct nfacct_pluginstance *) upi->private;

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

	ret = mnl_socket_recvfrom(cpi->nl, buf, sizeof(buf));
	if (ret > 0) {
		ret = mnl_cb_run(buf, ret, cpi->seq,
				 cpi->portid, nfacct_cb, upi);
	}
	return ret;
}

static int nfacct_send_request(struct nfacct_pluginstance *cpi)
{
	struct nlmsghdr *nlh;
	char buf[MNL_SOCKET_BUFFER_SIZE];

	cpi->seq = time(NULL);
	nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_GET_CTRZERO,
				     NLM_F_DUMP, cpi->seq);

	if (mnl_socket_sendto(cpi->nl, nlh, nlh->nlmsg_len) < 0) {
		ulogd_log(ULOGD_ERROR, "Cannot send netlink message\n");
		return -1;
	}
	return 0;
}

static void polling_timer_cb(struct ulogd_timer *t, void *data)
{
	struct ulogd_pluginstance *upi = data;
	struct nfacct_pluginstance *cpi =
		(struct nfacct_pluginstance *)upi->private;

	nfacct_send_request(cpi);

	ulogd_add_timer(&cpi->timer, pollint_ce(upi->config_kset).u.value);
}

static int configure_nfacct(struct ulogd_pluginstance *upi,
			    struct ulogd_pluginstance_stack *stack)
{
	int ret;

	ret = config_parse_file(upi->id, upi->config_kset);
	if (ret < 0)
		return ret;

	if (pollint_ce(upi->config_kset).u.value <= 0) {
		ulogd_log(ULOGD_FATAL, "You have to set pollint\n");
		return -1;
	}
	return 0;
}

static int constructor_nfacct(struct ulogd_pluginstance *upi)
{
	struct nfacct_pluginstance *cpi =
		(struct nfacct_pluginstance *)upi->private;

	if (pollint_ce(upi->config_kset).u.value == 0)
		return -1;

	cpi->nl = mnl_socket_open(NETLINK_NETFILTER);
	if (cpi->nl == NULL) {
		ulogd_log(ULOGD_FATAL, "cannot open netlink socket\n");
		return -1;
	}

	if (mnl_socket_bind(cpi->nl, 0, MNL_SOCKET_AUTOPID) < 0) {
		ulogd_log(ULOGD_FATAL, "cannot bind netlink socket\n");
		return -1;
	}
	cpi->portid = mnl_socket_get_portid(cpi->nl);

	cpi->ufd.fd = mnl_socket_get_fd(cpi->nl);
	cpi->ufd.cb = &nfacct_read_cb;
	cpi->ufd.data = upi;
	cpi->ufd.when = ULOGD_FD_READ;

	ulogd_register_fd(&cpi->ufd);
	ulogd_init_timer(&cpi->timer, upi, polling_timer_cb);
	ulogd_add_timer(&cpi->timer,
			 pollint_ce(upi->config_kset).u.value);

	return 0;
}

static int destructor_nfacct(struct ulogd_pluginstance *upi)
{
	struct nfacct_pluginstance *cpi = (void *)upi->private;

	ulogd_del_timer(&cpi->timer);
	ulogd_unregister_fd(&cpi->ufd);
	mnl_socket_close(cpi->nl);

	return 0;
}

static void signal_nfacct(struct ulogd_pluginstance *upi, int signal)
{
	struct nfacct_pluginstance *cpi =
		(struct nfacct_pluginstance *)upi->private;

	switch (signal) {
	case SIGUSR2:
		nfacct_send_request(cpi);
		break;
	}
}

static struct ulogd_plugin nfacct_plugin = {
	.name = "NFACCT",
	.input = {
		.type = ULOGD_DTYPE_SOURCE,
	},
	.output = {
		.keys = nfacct_okeys,
		.num_keys = ARRAY_SIZE(nfacct_okeys),
		.type = ULOGD_DTYPE_FLOW,
	},
	.config_kset	= &nfacct_kset,
	.interp		= NULL,
	.configure	= &configure_nfacct,
	.start		= &constructor_nfacct,
	.stop		= &destructor_nfacct,
	.signal		= &signal_nfacct,
	.priv_size	= sizeof(struct nfacct_pluginstance),
	.version	= ULOGD_VERSION,
};

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

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