summaryrefslogtreecommitdiffstats
path: root/input/packet/ulogd_inppkt_NFLOG.c
blob: 99bbc24bf5af4a67e79c7bce00b246feac68da74 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * ulogd_inppkt_NFLOG.c
 *
 * (C) 2004-2005 by Harald Welte <laforge@gnumonks.org>
 */
#include "config.h"
#include <ulogd/ulogd.h>
#include <ulogd/common.h>
#include <ulogd/plugin.h>

#include <netinet/ether.h>

#include <netinet/in.h>
#include <netlink/utils.h>
#include <netlink/msg.h>
#include <netlink/netfilter/nfnl.h>
#include <netlink/netfilter/log.h>

#define NFLOG_GROUP_DEFAULT	0

struct nflog_priv {
	struct nl_handle *nfnlh;
	struct ulogd_fd ufd;
};

/* configuration entries */
static const struct config_keyset nflog_kset = {
	.num_ces = 5,
	.ces = {
		CONFIG_KEY_INT("group", NFLOG_GROUP_DEFAULT),
		CONFIG_KEY_INT("af", AF_INET),
		CONFIG_KEY_INT("unbind", 1),
		CONFIG_KEY_INT("seq_local", 0),
		CONFIG_KEY_INT("seq_global", 0),
	}
};

#define group_ce(x)	(x->ces[0])
#define af_ce(x)	(x->ces[1])
#define unbind_ce(x)	(x->ces[2])
#define seq_ce(x)	(x->ces[3])
#define seq_global_ce(x)	(x->ces[4])


enum {
	K_RAW_MAC = 0,
	K_RAW_PKT,
	K_RAW_PKTLEN,
	K_RAW_PKTCNT,
	K_OOB_PREFIX,
	K_OOB_TIME_SEC,
	K_OOB_TIME_USEC,
	K_OOB_MARK,
	K_OOB_IFI_IN,
	K_OOB_IFI_OUT,
	K_OOB_FAMILY,
	K_OOB_HOOK,
	K_RAW_MAC_LEN,
	K_OOB_SEQ,
	K_OOB_SEQ_GLOBAL,
	K_OOB_LOGMARK,
	__K_LAST
};

static struct ulogd_key output_keys[] = {
	[K_RAW_MAC] = KEY_IPFIX(RAW, "raw.mac", IETF, sourceMacAddress),
	[K_RAW_PKT] = KEY_IPFIX(RAW,  "raw.pkt", NETFILTER, NF_rawpacket),
	[K_RAW_PKTLEN] KEY_IPFIX(UINT32, "raw.pktlen", NETFILTER,
							 NF_rawpacket_length),
	[K_RAW_PKTCNT] = KEY_IPFIX(UINT32, "raw.pktcount", IETF, packetDeltaCount),
	[K_OOB_PREFIX] = KEY_IPFIX(STRING, "oob.prefix", NETFILTER, NF_prefix),
	[K_OOB_TIME_SEC] = KEY_IPFIX(UINT32, "oob.time.sec", IETF,
								 flowStartSeconds),
	[K_OOB_TIME_USEC] = KEY_IPFIX(UINT32, "oob.time.usec", IETF,
								  flowStartMicroSeconds),
	[K_OOB_MARK] = KEY_IPFIX(UINT32, "oob.mark", NETFILTER, NF_mark),
	[K_OOB_IFI_IN] = KEY_IPFIX(UINT32, "oob.ifindex_in", IETF,
							   ingressInterface),
	[K_OOB_IFI_OUT] = KEY_IPFIX(UINT32, "oob.ifindex_out", IETF,
								egressInterface),
	[K_OOB_FAMILY] = KEY(UINT8, "oob.family"),
	[K_OOB_HOOK] = KEY_IPFIX(UINT8, "oob.hook", NETFILTER, NF_hook),
	[K_RAW_MAC_LEN] = KEY(UINT16, "raw.mac_len"),
	[K_OOB_SEQ] = KEY_IPFIX(UINT32, "oob.seq.local", NETFILTER, NF_seq_local),
	[K_OOB_SEQ_GLOBAL] = KEY_IPFIX(UINT32, "oob.seq.global", NETFILTER,
								   NF_seq_global),
	[K_OOB_LOGMARK] = KEY(UINT32, "oob.logmark"),
};

void nflog_dump(const char *prefix, const struct nl_object *obj)
	__ulogd_unused;

/* the libnl nl_object_dump() may not be used, because of the lacking
   cache_mgr support here. */
void
nflog_dump(const char *prefix, const struct nl_object *obj)
{
	static char line[128], *end = line + sizeof(line);
	struct nfnl_log *nflog_obj = (struct nfnl_log *)obj;
	struct ether_addr *mac;
	char buf[64], *pch = line;
	const char *nflog_prefix;
	int len, family;

#define AVAIL	(end - pch)

	if (prefix)
		pch += snprintf(pch, AVAIL, "%s: ", prefix);

	if (nfnl_log_get_indev(nflog_obj))
		pch += snprintf(pch, AVAIL, "in=%u ", nfnl_log_get_indev(nflog_obj));
	if (nfnl_log_get_physindev(nflog_obj))
		pch += snprintf(pch, AVAIL, "physin=%u ",
						nfnl_log_get_indev(nflog_obj));
	if (nfnl_log_get_outdev(nflog_obj))
		pch += snprintf(pch, AVAIL, "out=%u ",
						nfnl_log_get_outdev(nflog_obj));
	if (nfnl_log_get_physoutdev(nflog_obj))
		pch += snprintf(pch, AVAIL, "physout=%u ",
						nfnl_log_get_physoutdev(nflog_obj));

	if (nfnl_log_test_hook(nflog_obj))
		pch += snprintf(pch, AVAIL, "hook=%u ",
						nfnl_log_get_hook(nflog_obj));

	family = nfnl_log_get_family(nflog_obj);
	pch += snprintf(pch, AVAIL, "family=%s ",
					nl_af2str(family, buf, sizeof(buf)));

	if (nfnl_log_test_hwproto(nflog_obj)) {
		uint16_t proto = nfnl_log_get_hwproto(nflog_obj);

		pch += snprintf(pch, AVAIL, "proto=%s ",
						nl_ether_proto2str(proto, buf, sizeof(buf)));
	}

	mac = (struct ether_addr *)nfnl_log_get_hwaddr(nflog_obj, &len);
	if (mac)
		pch += snprintf(pch, AVAIL, "mac=%s ", ether_ntoa(mac));

	if (nfnl_log_test_mark(nflog_obj))
		pch += snprintf(pch, AVAIL, "mark=%u ",	nfnl_log_get_mark(nflog_obj));

	if (nfnl_log_test_logmark(nflog_obj))
		pch += snprintf(pch, AVAIL, "logmark=%u ",
						nfnl_log_get_logmark(nflog_obj));

	if (nfnl_log_get_payload(nflog_obj, &len))
		pch += snprintf(pch, AVAIL, "payloadlen=%d ", len);

	if ((nflog_prefix = nfnl_log_get_prefix(nflog_obj)) && *nflog_prefix)
		pch += snprintf(pch, AVAIL, "prefix='%s' ", nflog_prefix);

	*(end - 1) = '\0';

#undef AVAIL

	ulogd_log(ULOGD_INFO, "%s\n", line);
}

static void
nflog_handle_msg(struct nl_object *obj, void *arg)
{
	struct ulogd_pluginstance *upi = arg;
	struct nfnl_log *nflog_obj = (struct nfnl_log *)obj;
	struct ulogd_key *out = upi->output.keys;
	const struct timeval *tv;
	const char *prefix;
	unsigned flags = 0;
	int len;

	pr_fn_debug("pi=%p\n", upi);

	key_set_u8(&out[K_OOB_HOOK], nfnl_log_get_hook(nflog_obj));
	key_set_u8(&out[K_OOB_FAMILY], nfnl_log_get_family(nflog_obj));
	BUG_ON(!key_u8(&out[K_OOB_FAMILY]));

	if (nfnl_log_get_hwaddr(nflog_obj, &len)) {
		key_set_ptr(&out[K_RAW_MAC], (void *)nfnl_log_get_hwaddr(nflog_obj,
																 &len));
		key_set_u16(&out[K_RAW_MAC_LEN], len);
	}

	key_set_ptr(&out[K_RAW_PKT], (void*)nfnl_log_get_payload(nflog_obj, &len));
	key_set_u32(&out[K_RAW_PKTLEN], len);

	key_set_u32(&out[K_RAW_PKTCNT], 1);

	if ((prefix = nfnl_log_get_prefix(nflog_obj)) != NULL && *prefix)
		key_set_str(&out[K_OOB_PREFIX], (void*)nfnl_log_get_prefix(nflog_obj));

	if ((tv = nfnl_log_get_timestamp(nflog_obj)) != NULL) {
		/* FIXME: convert endianness */
		key_set_u32(&out[K_OOB_TIME_SEC], tv->tv_sec);
		key_set_u32(&out[K_OOB_TIME_USEC], tv->tv_usec);
	}

	if (nfnl_log_get_indev(nflog_obj))
		key_set_u32(&out[K_OOB_IFI_IN], nfnl_log_get_indev(nflog_obj));
	if (nfnl_log_get_outdev(nflog_obj))
		key_set_u32(&out[K_OOB_IFI_OUT], nfnl_log_get_outdev(nflog_obj));

	if (nfnl_log_test_seq(nflog_obj))
		key_set_u32(&out[K_OOB_SEQ], nfnl_log_get_seq(nflog_obj));
	if (nfnl_log_test_seq_global(nflog_obj))
		key_set_u32(&out[K_OOB_SEQ_GLOBAL], nfnl_log_get_seq_global(nflog_obj));

	if (nfnl_log_test_logmark(nflog_obj))
		key_set_u32(&out[K_OOB_LOGMARK], nfnl_log_get_logmark(nflog_obj));

	ulogd_propagate_results(upi, &flags);
}

/*
 * Called from libnl for every valid netlink message.
 */
static int
nflog_parse_valid_msg(struct nl_msg *msg, void *arg)
{
	struct ulogd_pluginstance *upi = arg;

	pr_fn_debug("pi=%p msg=%p arg=%p\n", upi, msg, arg);

	if (nl_msg_parse(msg, nflog_handle_msg, upi) < 0)
		upi_log(upi, ULOGD_ERROR, "parse: %s\n", nl_geterror());

	return NL_OK;
}

/* callback called from ulogd core when fd is readable */
static int
nflog_ufd_cb(int fd, unsigned int what, void *arg)
{
	struct ulogd_pluginstance *upi = arg;
	struct nflog_priv *priv = upi_priv(upi);

	pr_fn_debug("fd=%d arg=%p\n", fd, arg);

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

	return nl_recvmsgs_default(priv->nfnlh);
}

static int
nflog_configure(struct ulogd_pluginstance *upi)
{
	struct nflog_priv *priv = upi_priv(upi);

	ulogd_init_fd(&priv->ufd, -1, ULOGD_FD_READ, nflog_ufd_cb, upi);

	return 0;
}

static int
nflog_start(struct ulogd_pluginstance *upi)
{
	struct nflog_priv *priv = upi_priv(upi);

	pr_fn_debug("pi=%p\n", upi);

	upi_log(upi, ULOGD_DEBUG, "opening nfnetlink socket\n");
	if ((priv->nfnlh = nl_handle_alloc()) == NULL) {
		upi_log(upi, ULOGD_ERROR, "open: %s\n", nl_geterror());
		return ULOGD_IRET_ERR;
	}

	nl_disable_sequence_check(priv->nfnlh);

	if (nl_socket_modify_cb(priv->nfnlh, NL_CB_VALID, NL_CB_CUSTOM,
							nflog_parse_valid_msg, upi) < 0) {
		upi_log(upi, ULOGD_ERROR, "callback: %s\n", nl_geterror());
		return ULOGD_IRET_ERR;
	}

	if (nfnl_connect(priv->nfnlh) < 0) {
		upi_log(upi, ULOGD_ERROR, "connect: %s\n", nl_geterror());
		return ULOGD_IRET_ERR;
	}

	if (nl_socket_set_nonblocking(priv->nfnlh) < 0) {
		upi_log(upi, ULOGD_ERROR, "%s\n", nl_geterror());
		return -1;
	}

	upi_log(upi, ULOGD_DEBUG, "binding to protocol family %d\n",
		  af_ce(upi->config_kset).u.value);
	if (nfnl_log_pf_unbind(priv->nfnlh, af_ce(upi->config_kset).u.value) < 0) {
		upi_log(upi, ULOGD_ERROR, "unbind: %s\n", nl_geterror());
		return ULOGD_IRET_ERR;
	}

	if (nfnl_log_pf_bind(priv->nfnlh, af_ce(upi->config_kset).u.value) < 0) {
		upi_log(upi, ULOGD_ERROR, "unable to bind to family %d\n",
				af_ce(upi->config_kset).u.value);
		return ULOGD_IRET_ERR;
	}

	upi_log(upi, ULOGD_DEBUG, "binding to log group %d\n",
			group_ce(upi->config_kset).u.value);
	if (nfnl_log_bind(priv->nfnlh, group_ce(upi->config_kset).u.value) < 0) {
		upi_log(upi, ULOGD_ERROR, "unable to bind to log group '%d'\n",
				group_ce(upi->config_kset).u.value);
		return ULOGD_IRET_ERR;
	}

	/* TODO use COPY_PACKET define */
	nfnl_log_set_mode(priv->nfnlh, 0, 2 /* COPY_PACKET */, 0xffff);

	/* TODO set flags */

	priv->ufd.fd = nl_socket_get_fd(priv->nfnlh);
	if (ulogd_register_fd(&priv->ufd) < 0)
		goto err_free;

	return 0;

err_free:
	/* free nl_handle */

	return ULOGD_IRET_ERR;
}

static int
nflog_stop(struct ulogd_pluginstance *upi)
{
	struct nflog_priv *priv = upi_priv(upi);

	pr_fn_debug("pi=%p\n", upi);

	ulogd_unregister_fd(&priv->ufd);

	nfnl_log_pf_unbind(priv->nfnlh, af_ce(upi->config_kset).u.value);
	nl_close(priv->nfnlh);

	return 0;
}

struct ulogd_plugin nflog_plugin = {
	.name = "NFLOG",
	.flags = ULOGD_PF_RECONF,
	.input = {
			.type = ULOGD_DTYPE_SOURCE,
		},
	.output = {
			.type = ULOGD_DTYPE_RAW,
			.keys = output_keys,
			.num_keys = ARRAY_SIZE(output_keys),
		},
	.priv_size 	= sizeof(struct nflog_priv),
	.configure = &nflog_configure,
	.start 		= &nflog_start,
	.stop 		= &nflog_stop,
	.config_kset = &nflog_kset,
	.rev		= ULOGD_PLUGIN_REVISION,
};

void __upi_ctor init(void);

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