summaryrefslogtreecommitdiff
path: root/src/nfct-extensions/helper.c
blob: bfb153f1a2c99fef2e377fa33b898a59955eaae3 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * (C) 2012 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.
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <netinet/in.h>
#include <errno.h>
#include <dlfcn.h>

#include <libmnl/libmnl.h>
#include <linux/netfilter/nfnetlink_cthelper.h>
#include <libnetfilter_cthelper/libnetfilter_cthelper.h>

#include "nfct.h"
#include "helper.h"

static void
nfct_cmd_helper_usage(char *argv[])
{
	fprintf(stderr, "nfct v%s: Missing command\n"
			"%s helper list|add|delete|get|flush "
			"[parameters...]\n", VERSION, argv[0]);
}

static int nfct_cmd_helper_list(struct mnl_socket *nl, int argc, char *argv[]);
static int nfct_cmd_helper_add(struct mnl_socket *nl, int argc, char *argv[]);
static int nfct_cmd_helper_delete(struct mnl_socket *nl, int argc, char *argv[]);
static int nfct_cmd_helper_get(struct mnl_socket *nl, int argc, char *argv[]);
static int nfct_cmd_helper_flush(struct mnl_socket *nl, int argc, char *argv[]);
static int nfct_cmd_helper_disable(struct mnl_socket *nl, int argc, char *argv[]);

static int
nfct_cmd_helper_parse_params(struct mnl_socket *nl, int argc, char *argv[])
{
	int cmd = NFCT_CMD_NONE, ret = 0;

	if (argc < 3) {
		fprintf(stderr, "nfct v%s: Missing command\n"
				"%s helper list|add|delete|get|flush "
				"[parameters...]\n", VERSION, argv[0]);
		exit(EXIT_FAILURE);
	}
	if (strncmp(argv[2], "list", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_LIST;
	else if (strncmp(argv[2], "add", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_ADD;
	else if (strncmp(argv[2], "delete", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_DELETE;
	else if (strncmp(argv[2], "get", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_GET;
	else if (strncmp(argv[2], "flush", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_FLUSH;
	else if (strncmp(argv[2], "disable", strlen(argv[2])) == 0)
		cmd = NFCT_CMD_DISABLE;
	else {
		fprintf(stderr, "nfct v%s: Unknown command: %s\n",
			VERSION, argv[2]);
		nfct_cmd_helper_usage(argv);
		exit(EXIT_FAILURE);
	}

	switch(cmd) {
	case NFCT_CMD_LIST:
		ret = nfct_cmd_helper_list(nl, argc, argv);
		break;
	case NFCT_CMD_ADD:
		ret = nfct_cmd_helper_add(nl, argc, argv);
		break;
	case NFCT_CMD_DELETE:
		ret = nfct_cmd_helper_delete(nl, argc, argv);
		break;
	case NFCT_CMD_GET:
		ret = nfct_cmd_helper_get(nl, argc, argv);
		break;
	case NFCT_CMD_FLUSH:
		ret = nfct_cmd_helper_flush(nl, argc, argv);
		break;
	case NFCT_CMD_DISABLE:
		ret = nfct_cmd_helper_disable(nl, argc, argv);
		break;
	}

	return ret;
}

static int nfct_helper_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nfct_helper *t;
	char buf[4096];

	t = nfct_helper_alloc();
	if (t == NULL) {
		nfct_perror("OOM");
		goto err;
	}

	if (nfct_helper_nlmsg_parse_payload(nlh, t) < 0) {
		nfct_perror("nfct_helper_nlmsg_parse_payload");
		goto err_free;
	}

	nfct_helper_snprintf(buf, sizeof(buf), t, 0, 0);
	printf("%s\n", buf);

err_free:
	nfct_helper_free(t);
err:
	return MNL_CB_OK;
}

static int nfct_cmd_helper_list(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	unsigned int seq, portid;

	if (argc > 3) {
		nfct_perror("too many arguments");
		return -1;
	}

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_GET,
						NLM_F_DUMP, seq);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, nfct_helper_cb, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static int nfct_cmd_helper_add(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t portid, seq;
	struct nfct_helper *t;
	uint16_t l3proto;
	uint8_t l4proto;
	struct ctd_helper *helper;
	int j;

	if (argc < 6) {
		nfct_perror("missing parameters\n"
			    "syntax: nfct helper add name "
			    "family protocol");
		return -1;
	}

	if (strcmp(argv[4], "inet") == 0)
		l3proto = AF_INET;
	else if (strcmp(argv[4], "inet6") == 0)
		l3proto = AF_INET6;
	else {
		nfct_perror("unknown layer 3 protocol");
		return -1;
	}

	if (strcmp(argv[5], "tcp") == 0)
		l4proto = IPPROTO_TCP;
	else if (strcmp(argv[5], "udp") == 0)
		l4proto = IPPROTO_UDP;
	else {
		nfct_perror("unsupported layer 4 protocol");
		return -1;
	}

	helper = helper_find(CONNTRACKD_LIB_DIR, argv[3], l4proto, RTLD_LAZY);
	if (helper == NULL) {
		nfct_perror("that helper is not supported");
		return -1;
	}

	t = nfct_helper_alloc();
	if (t == NULL) {
		nfct_perror("OOM");
		return -1;
	}
	nfct_helper_attr_set(t, NFCTH_ATTR_NAME, argv[3]);
	nfct_helper_attr_set_u16(t, NFCTH_ATTR_PROTO_L3NUM, l3proto);
	nfct_helper_attr_set_u8(t, NFCTH_ATTR_PROTO_L4NUM, l4proto);
	nfct_helper_attr_set_u32(t, NFCTH_ATTR_PRIV_DATA_LEN,
				 helper->priv_data_len);

	for (j=0; j<CTD_HELPER_POLICY_MAX; j++) {
		struct nfct_helper_policy *p;

		if (!helper->policy[j].name[0])
			break;

		p = nfct_helper_policy_alloc();
		if (p == NULL) {
			nfct_perror("OOM");
			return -1;
		}

		nfct_helper_policy_attr_set(p, NFCTH_ATTR_POLICY_NAME,
					helper->policy[j].name);
		nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_TIMEOUT,
					helper->policy[j].expect_timeout);
		nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_MAX,
					helper->policy[j].expect_max);

		nfct_helper_attr_set(t, NFCTH_ATTR_POLICY+j, p);
	}

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_NEW,
					  NLM_F_CREATE | NLM_F_ACK, seq);
	nfct_helper_nlmsg_build_payload(nlh, t);

	nfct_helper_free(t);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, NULL, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static int
nfct_cmd_helper_delete(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t portid, seq;
	struct nfct_helper *t;

	if (argc < 4) {
		nfct_perror("missing helper policy name");
		return -1;
	} else if (argc > 6) {
		nfct_perror("too many arguments");
		return -1;
	}

	t = nfct_helper_alloc();
	if (t == NULL) {
		nfct_perror("OOM");
		return -1;
	}

	nfct_helper_attr_set(t, NFCTH_ATTR_NAME, argv[3]);

	if (argc >= 5) {
		uint16_t l3proto;

		if (strcmp(argv[4], "inet") == 0)
			l3proto = AF_INET;
		else if (strcmp(argv[4], "inet6") == 0)
			l3proto = AF_INET6;
		else {
			nfct_perror("unknown layer 3 protocol");
			return -1;
		}
		nfct_helper_attr_set_u16(t, NFCTH_ATTR_PROTO_L3NUM, l3proto);
	}

	if (argc == 6) {
		uint8_t l4proto;

		if (strcmp(argv[5], "tcp") == 0)
			l4proto = IPPROTO_TCP;
		else if (strcmp(argv[5], "udp") == 0)
			l4proto = IPPROTO_UDP;
		else {
			nfct_perror("unsupported layer 4 protocol");
			return -1;
		}
		nfct_helper_attr_set_u32(t, NFCTH_ATTR_PROTO_L4NUM, l4proto);
	}

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_DEL,
					  NLM_F_ACK, seq);
	nfct_helper_nlmsg_build_payload(nlh, t);

	nfct_helper_free(t);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, NULL, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static int nfct_cmd_helper_get(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t portid, seq;
	struct nfct_helper *t;

	if (argc < 4) {
		nfct_perror("missing helper policy name");
		return -1;
	} else if (argc > 6) {
		nfct_perror("too many arguments");
		return -1;
	}

	t = nfct_helper_alloc();
	if (t == NULL) {
		nfct_perror("OOM");
		return -1;
	}
	nfct_helper_attr_set(t, NFCTH_ATTR_NAME, argv[3]);

	if (argc >= 5) {
		uint16_t l3proto;

		if (strcmp(argv[4], "inet") == 0)
			l3proto = AF_INET;
		else if (strcmp(argv[4], "inet6") == 0)
			l3proto = AF_INET6;
		else {
			nfct_perror("unknown layer 3 protocol");
			return -1;
		}
		nfct_helper_attr_set_u16(t, NFCTH_ATTR_PROTO_L3NUM, l3proto);
	}

	if (argc == 6) {
		uint8_t l4proto;

		if (strcmp(argv[5], "tcp") == 0)
			l4proto = IPPROTO_TCP;
		else if (strcmp(argv[5], "udp") == 0)
			l4proto = IPPROTO_UDP;
		else {
			nfct_perror("unsupported layer 4 protocol");
			return -1;
		}
		nfct_helper_attr_set_u32(t, NFCTH_ATTR_PROTO_L4NUM, l4proto);
	}

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_GET,
					  NLM_F_ACK, seq);

	nfct_helper_nlmsg_build_payload(nlh, t);

	nfct_helper_free(t);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, nfct_helper_cb, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static int
nfct_cmd_helper_flush(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t portid, seq;

	if (argc > 3) {
		nfct_perror("too many arguments");
		return -1;
	}

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_DEL,
					   NLM_F_ACK, seq);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, NULL, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static int
nfct_cmd_helper_disable(struct mnl_socket *nl, int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t portid, seq;
	struct nfct_helper *t;
	uint16_t l3proto;
	uint8_t l4proto;
	struct ctd_helper *helper;

	if (argc < 6) {
		nfct_perror("missing parameters\n"
			    "syntax: nfct helper add name "
			    "family protocol");
		return -1;
	}

	if (strcmp(argv[4], "inet") == 0)
		l3proto = AF_INET;
	else if (strcmp(argv[4], "inet6") == 0)
		l3proto = AF_INET6;
	else {
		nfct_perror("unknown layer 3 protocol");
		return -1;
	}

	if (strcmp(argv[5], "tcp") == 0)
		l4proto = IPPROTO_TCP;
	else if (strcmp(argv[5], "udp") == 0)
		l4proto = IPPROTO_UDP;
	else {
		nfct_perror("unsupported layer 4 protocol");
		return -1;
	}

	helper = helper_find(CONNTRACKD_LIB_DIR, argv[3], l4proto, RTLD_LAZY);
	if (helper == NULL) {
		nfct_perror("that helper is not supported");
		return -1;
	}

	t = nfct_helper_alloc();
	if (t == NULL) {
		nfct_perror("OOM");
		return -1;
	}
	nfct_helper_attr_set(t, NFCTH_ATTR_NAME, argv[3]);
	nfct_helper_attr_set_u16(t, NFCTH_ATTR_PROTO_L3NUM, l3proto);
	nfct_helper_attr_set_u8(t, NFCTH_ATTR_PROTO_L4NUM, l4proto);
	nfct_helper_attr_set_u32(t, NFCTH_ATTR_STATUS,
					NFCT_HELPER_STATUS_DISABLED);

	seq = time(NULL);
	nlh = nfct_helper_nlmsg_build_hdr(buf, NFNL_MSG_CTHELPER_NEW,
					  NLM_F_CREATE | NLM_F_ACK, seq);
	nfct_helper_nlmsg_build_payload(nlh, t);

	nfct_helper_free(t);

	portid = mnl_socket_get_portid(nl);
	if (nfct_mnl_talk(nl, nlh, seq, portid, NULL, NULL) < 0) {
		nfct_perror("netlink error");
		return -1;
	}

	return 0;
}

static struct nfct_extension helper = {
	.type		= NFCT_SUBSYS_HELPER,
	.parse_params	= nfct_cmd_helper_parse_params,
};

static void __init helper_init(void)
{
	nfct_extension_register(&helper);
}