summaryrefslogtreecommitdiffstats
path: root/src/cthelper.c
blob: 54eb830842dc1cde351494d42361a0da1de02a24 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 * (C) 2006-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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */

#include "conntrackd.h"
#include "log.h"
#include "fds.h"
#include "helper.h"

#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#define _GNU_SOURCE
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/ethernet.h>

#ifndef __aligned_be64
#define __aligned_be64 unsigned long long __attribute__((aligned(8)))
#endif

#include <linux/netfilter/nfnetlink_queue.h>

#include <libmnl/libmnl.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <libnetfilter_queue/pktbuff.h>
#include <libnetfilter_cthelper/libnetfilter_cthelper.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/pktbuff.h>

void cthelper_kill(void)
{
	mnl_socket_close(STATE_CTH(nl));
	free(state.cthelper);
}

int cthelper_local(int fd, int type, void *data)
{
	/* No services to obtain information on helpers yet, sorry */
	return LOCAL_RET_OK;
}

static struct nlmsghdr *
nfq_hdr_put(char *buf, int type, uint32_t queue_num)
{
	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type = (NFNL_SUBSYS_QUEUE << 8) | type;
	nlh->nlmsg_flags = NLM_F_REQUEST;

	struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
	nfg->nfgen_family = AF_UNSPEC;
	nfg->version = NFNETLINK_V0;
	nfg->res_id = htons(queue_num);

	return nlh;
}

static int
pkt_get(void *pkt, uint32_t pktlen, uint16_t proto, uint32_t *protoff)
{
	uint8_t protocol;

	switch(proto) {
	case ETHERTYPE_IP: {
		struct iphdr *ip = (struct iphdr *) pkt;

		/* No room for IPv4 header. */
		if (pktlen < sizeof(struct iphdr)) {
			dlog(LOG_ERR, "no room for IPv4 header");
			return -1;
		}

		/* this is not IPv4, skip. */
		if (ip->version != 4) {
			dlog(LOG_ERR, "not IPv4, skipping");
			return -1;
		}

		*protoff = 4 * ip->ihl;
		protocol = ip->protocol;
		break;
	}
	case ETHERTYPE_IPV6: {
		struct iphdr *ip = (struct iphdr *) pkt;
		struct ip6_hdr *ip6 = (struct ip6_hdr *) pkt;

		/* No room for IPv6 header. */
		if (pktlen < sizeof(struct ip6_hdr)) {
			dlog(LOG_ERR, "no room for IPv6 header");
			return -1;
		}

		/* this is not IPv6, skip. */
		if (ip->version != 6) {
			dlog(LOG_ERR, "not IPv6, skipping");
			return -1;
		}

		*protoff = sizeof(struct ip6_hdr);
		protocol = ip6->ip6_nxt;
		break;
	}
	default:
		/* Unknown layer 3 protocol. */
		dlog(LOG_ERR, "unknown layer 3 protocol (%d), skipping", proto);
		return -1;
	}

	switch (protocol) {
	case IPPROTO_TCP: {
		struct tcphdr *tcph =
			(struct tcphdr *) ((char *)pkt + *protoff);

		/* No room for IPv4 header plus TCP header. */
		if (pktlen < *protoff + sizeof(struct tcphdr) ||
		    pktlen < *protoff + tcph->doff * 4) {
			dlog(LOG_ERR, "no room for IPv4 + TCP header, skip");
			return -1;
		}
		return 0;
	}
	case IPPROTO_UDP:
		/* No room for IPv4 header plus UDP header. */
		if (pktlen < *protoff + sizeof(struct udphdr)) {
			dlog(LOG_ERR, "no room for IPv4 + UDP header, skip");
			return -1;
		}
		return 0;
	default:
		dlog(LOG_ERR, "not TCP/UDP, skipping");
		return -1;
	}

	return 0;
}

static int
pkt_verdict_issue(struct ctd_helper_instance *cur, struct myct *myct,
		  uint16_t queue_num, uint32_t id, uint32_t verdict,
		  struct pkt_buff *pktb)
{
	struct nlmsghdr *nlh;
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlattr *nest;

	nlh = nfq_hdr_put(buf, NFQNL_MSG_VERDICT, queue_num);

	/* save private data and send it back to kernel-space, if any. */
	if (myct->priv_data) {
		nfct_set_attr_l(myct->ct, ATTR_HELPER_INFO, myct->priv_data,
				cur->helper->priv_data_len);
	}

	nfq_nlmsg_verdict_put(nlh, id, verdict);
	if (pktb_mangled(pktb))
		nfq_nlmsg_verdict_put_pkt(nlh, pktb_data(pktb), pktb_len(pktb));

	nest = mnl_attr_nest_start(nlh, NFQA_CT);
	if (nest == NULL)
		return -1;

	nfct_nlmsg_build(nlh, myct->ct);
	mnl_attr_nest_end(nlh, nest);

	if (myct->exp) {
		nest = mnl_attr_nest_start(nlh, NFQA_EXP);
		if (nest == NULL)
			return -1;

		nfexp_nlmsg_build(nlh, myct->exp);
		mnl_attr_nest_end(nlh, nest);
	}

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send verdict: %s", strerror(errno));
		return -1;
	}

	return 0;
}

static int
pkt_verdict_error(uint16_t queue_num, uint32_t id)
{
	struct nlmsghdr *nlh;
	char buf[MNL_SOCKET_BUFFER_SIZE];

	nlh = nfq_hdr_put(buf, NFQNL_MSG_VERDICT, queue_num);
	nfq_nlmsg_verdict_put(nlh, id, NF_ACCEPT);

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send verdict: %s", strerror(errno));
		return -1;
	}
	return 0;
}

static struct ctd_helper_instance *
helper_run(struct pkt_buff *pktb, uint32_t protoff, struct myct *myct,
	   uint32_t ctinfo, uint32_t queue_num, int *verdict)
{
	struct ctd_helper_instance *cur, *helper = NULL;

	list_for_each_entry(cur, &CONFIG(cthelper).list, head) {
		if (cur->queue_num == queue_num) {
			const void *priv_data;

			/* retrieve helper private data. */
			priv_data = nfct_get_attr(myct->ct, ATTR_HELPER_INFO);
			if (priv_data != NULL) {
				myct->priv_data =
					calloc(1, cur->helper->priv_data_len);

				if (myct->priv_data == NULL)
					continue;

				memcpy(myct->priv_data, priv_data,
					cur->helper->priv_data_len);
			}

			*verdict = cur->helper->cb(pktb, protoff, myct, ctinfo);
			helper = cur;
			break;
		}
	}
	return helper;
}

static int nfq_queue_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nfqnl_msg_packet_hdr *ph = NULL;
	struct nlattr *attr[NFQA_MAX+1] = {};
	struct nfgenmsg *nfg;
	uint8_t *pkt;
	uint16_t l3num;
	uint32_t id, ctinfo, queue_num = 0, protoff = 0, pktlen;
	struct nf_conntrack *ct = NULL;
	struct myct *myct;
	struct ctd_helper_instance *helper;
	struct pkt_buff *pktb;
	int verdict = NF_ACCEPT;

	if (nfq_nlmsg_parse(nlh, attr) < 0) {
		dlog(LOG_ERR, "problems parsing message from kernel");
		return MNL_CB_ERROR;
	}

	ph = (struct nfqnl_msg_packet_hdr *)
		mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
	if (ph == NULL) {
		dlog(LOG_ERR, "problems retrieving metaheader");
		return MNL_CB_ERROR;
	}

	id = ntohl(ph->packet_id);

	if (!attr[NFQA_PAYLOAD]) {
		dlog(LOG_ERR, "packet with no payload");
		goto err1;
	}
	if (!attr[NFQA_CT] || !attr[NFQA_CT_INFO]) {
		dlog(LOG_ERR, "no CT attached to this packet");
		goto err1;
	}

	pkt = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
	pktlen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);

	nfg = mnl_nlmsg_get_payload(nlh);
	l3num = nfg->nfgen_family;
	queue_num = ntohs(nfg->res_id);

	if (pkt_get(pkt, pktlen, ntohs(ph->hw_protocol), &protoff))
		goto err1;

	ct = nfct_new();
	if (ct == NULL)
		goto err1;

	if (nfct_payload_parse(mnl_attr_get_payload(attr[NFQA_CT]),
			       mnl_attr_get_payload_len(attr[NFQA_CT]),
			       l3num, ct) < 0) {
		dlog(LOG_ERR, "cannot convert message to CT");
		goto err2;
	}

	myct = calloc(1, sizeof(struct myct));
	if (myct == NULL)
		goto err2;

	myct->ct = ct;
	ctinfo = ntohl(mnl_attr_get_u32(attr[NFQA_CT_INFO]));

	/* XXX: 256 bytes enough for possible NAT mangling in helpers? */
	pktb = pktb_alloc(AF_INET, pkt, pktlen, 256);
	if (pktb == NULL)
		goto err3;

	/* Misconfiguration: if no helper found, accept the packet. */
	helper = helper_run(pktb, protoff, myct, ctinfo, queue_num, &verdict);
	if (!helper)
		goto err4;

	if (pkt_verdict_issue(helper, myct, queue_num, id, verdict, pktb) < 0)
		goto err4;

	nfct_destroy(ct);
	if (myct->exp != NULL)
		nfexp_destroy(myct->exp);
	if (myct->priv_data != NULL)
		free(myct->priv_data);
	free(myct);

	return MNL_CB_OK;
err4:
	pktb_free(pktb);
err3:
	free(myct);
err2:
	nfct_destroy(ct);
err1:
	/* In case of error, we don't want to disrupt traffic. We accept all.
	 * This is connection tracking after all. The policy is not to drop
	 * packet unless we enter some inconsistent state.
	 */
	pkt_verdict_error(queue_num, id);

	return MNL_CB_OK;
}

static void nfq_cb(void *data)
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	int ret;

	ret = mnl_socket_recvfrom(STATE_CTH(nl), buf, sizeof(buf));
	if (ret == -1) {
		dlog(LOG_ERR, "failed to receive message: %s", strerror(errno));
		return;
	}

	ret = mnl_cb_run(buf, ret, 0, STATE_CTH(portid), nfq_queue_cb, NULL);
	if (ret < 0){
		dlog(LOG_ERR, "failed to process message");
		return;
	}
}

static int cthelper_setup(struct ctd_helper_instance *cur)
{
	struct nfct_helper *t;
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;
	uint32_t seq;
	int j, ret;

	t = nfct_helper_alloc();
	if (t == NULL) {
		dlog(LOG_ERR, "cannot allocate object for helper");
		return -1;
	}

	nfct_helper_attr_set(t, NFCTH_ATTR_NAME, cur->helper->name);
	nfct_helper_attr_set_u32(t, NFCTH_ATTR_QUEUE_NUM, cur->queue_num);
	nfct_helper_attr_set_u16(t, NFCTH_ATTR_PROTO_L3NUM, cur->l3proto);
	nfct_helper_attr_set_u8(t, NFCTH_ATTR_PROTO_L4NUM, cur->l4proto);
	nfct_helper_attr_set_u32(t, NFCTH_ATTR_STATUS,
					NFCT_HELPER_STATUS_ENABLED);

	dlog(LOG_NOTICE, "configuring helper `%s' with queuenum=%d and "
			 "queuelen=%d", cur->helper->name, cur->queue_num,
			 cur->queue_len);

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

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

		p = nfct_helper_policy_alloc();
		if (p == NULL) {
			dlog(LOG_ERR, "cannot allocate object for helper");
			return -1;
		}
		/* FIXME: get existing policy values from the kernel first. */
		nfct_helper_policy_attr_set(p, NFCTH_ATTR_POLICY_NAME,
					cur->helper->policy[j].name);
		nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_TIMEOUT,
					cur->helper->policy[j].expect_timeout);
		nfct_helper_policy_attr_set_u32(p, NFCTH_ATTR_POLICY_MAX,
					cur->helper->policy[j].expect_max);

		dlog(LOG_NOTICE, "policy name=%s expect_timeout=%d expect_max=%d",
			cur->helper->policy[j].name,
			cur->helper->policy[j].expect_timeout,
			cur->helper->policy[j].expect_max);

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

	if (j == 0) {
		dlog(LOG_ERR, "you have to define one policy for helper");
		return -1;
	}

	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);

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "sending cthelper configuration");
		return -1;
	}

	ret = mnl_socket_recvfrom(STATE_CTH(nl), buf, sizeof(buf));
	while (ret > 0) {
		ret = mnl_cb_run(buf, ret, seq, STATE_CTH(portid), NULL, NULL);
		if (ret <= 0)
			break;
		ret = mnl_socket_recvfrom(STATE_CTH(nl), buf, sizeof(buf));
	}
	if (ret == -1) {
		dlog(LOG_ERR, "trying to configure cthelper `%s': %s",
			cur->helper->name, strerror(errno));
		return -1;
	}

	return 0;
}

static int cthelper_nfqueue_setup(struct ctd_helper_instance *cur)
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;

	nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, cur->queue_num);
	nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_BIND);

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send bind command");
		return -1;
	}

	nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, cur->queue_num);
	nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
	mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_CONNTRACK));
	mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(0xffffffff));
	if (cur->queue_len > 0) {
		mnl_attr_put_u32(nlh, NFQA_CFG_QUEUE_MAXLEN,
				 htonl(cur->queue_len));
	}

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send configuration");
		return -1;
	}

	return 0;
}

static int cthelper_configure(struct ctd_helper_instance *cur)
{
	/* First, configure cthelper. */
	if (cthelper_setup(cur) < 0)
		return -1;

	/* Now, we are ready to configure nfqueue attached to this helper. */
	if (cthelper_nfqueue_setup(cur) < 0)
		return -1;

	dlog(LOG_NOTICE, "helper `%s' configured successfully",
		cur->helper->name);

	return 0;
}

static int nfq_configure(void)
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct nlmsghdr *nlh;

	nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, 0);
	nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_PF_UNBIND);

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send pf unbind command");
		return -1;
	}

	nlh = nfq_hdr_put(buf, NFQNL_MSG_CONFIG, 0);
	nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, NFQNL_CFG_CMD_PF_BIND);

	if (mnl_socket_sendto(STATE_CTH(nl), nlh, nlh->nlmsg_len) < 0) {
		dlog(LOG_ERR, "failed to send pf bind command");
		return -1;
	}

	return 0;
}

int cthelper_init(void)
{
	struct ctd_helper_instance *cur;
	int ret;

	state.cthelper = calloc(1, sizeof(struct ct_helper_state));
	if (state.cthelper == NULL) {
		dlog(LOG_ERR, "can't allocate memory for cthelper struct");
		return -1;
	}

	STATE_CTH(nl) = mnl_socket_open(NETLINK_NETFILTER);
	if (STATE_CTH(nl) == NULL) {
		dlog(LOG_ERR, "cannot open nfq socket");
		return -1;
	}
	fcntl(mnl_socket_get_fd(STATE_CTH(nl)), F_SETFL, O_NONBLOCK);

	if (mnl_socket_bind(STATE_CTH(nl), 0, MNL_SOCKET_AUTOPID) < 0) {
		dlog(LOG_ERR, "cannot bind nfq socket");
		return -1;
	}
	STATE_CTH(portid) = mnl_socket_get_portid(STATE_CTH(nl));

	if (nfq_configure())
		return -1;

	list_for_each_entry(cur, &CONFIG(cthelper).list, head) {
		ret = cthelper_configure(cur);
		if (ret < 0)
			return ret;
	}

	register_fd(mnl_socket_get_fd(STATE_CTH(nl)), nfq_cb, NULL, STATE(fds));

	return 0;
}