summaryrefslogtreecommitdiffstats
path: root/src/libnfnetlink_conntrack.c
blob: 098505ff16210e18d21e21363f346441593daf4e (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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* libctnetlink.c: generic library for access to connection tracking.
 *
 * (C) 2001 by Jay Schulist <jschlst@samba.org>
 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
 * (C) 2005 by Pablo Neira Ayuso <pablo@eurodev.net>
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com)
 *
 * this software may be used and distributed according to the terms
 * of the gnu general public license, incorporated herein by reference.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <asm/types.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/netfilter/nfnetlink_conntrack.h>

#include <libnfnetlink/libnfnetlink.h>
#include <libnfnetlink_conntrack/libnfnetlink_conntrack.h>

#define ctnl_error(format, args...) fprintf(stderr, format, ## args)

struct nfnlhdr {
	struct nlmsghdr nlh;
	struct nfgenmsg nfmsg;
};

/***********************************************************************
 * low level stuff 
 ***********************************************************************/
int ctnl_send(struct ctnl_handle *cth, struct nlmsghdr *n)
{
	return nfnl_send(&cth->nfnlh, n);
}

int ctnl_wilddump_request(struct ctnl_handle *cth, int family, int type)
{
	struct nfnlhdr req;

	nfnl_fill_hdr(&cth->nfnlh, &req.nlh, 0, AF_INET, 0,
		      type, NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST);

	return nfnl_send(&cth->nfnlh, &req.nlh);
}

/* handler used for nfnl_listen */
static int callback_handler(struct sockaddr_nl *nladdr, 
			    struct nlmsghdr *n, void *arg)
{
	struct ctnl_handle *cth = (struct ctnl_handle *) arg;
	int type = NFNL_MSG_TYPE(n->nlmsg_type);
	struct ctnl_msg_handler *hdlr = cth->handler[type];
	int ret;

	if (NFNL_SUBSYS_ID(n->nlmsg_type) != NFNL_SUBSYS_CTNETLINK &&
	    NFNL_SUBSYS_ID(n->nlmsg_type) != NFNL_SUBSYS_CTNETLINK_EXP) {
		ctnl_error("received message for wrong subsys, skipping\n");
		nfnl_dump_packet(n, n->nlmsg_len, "callback_handler");
		return 0;
	}

	if (!hdlr) {
		ctnl_error("no handler for type %d\n", type);
		return 0;
	}

	if (!hdlr->handler) {
		ctnl_error("no handler function for type %d\n", type);
		return 0;
	}

	ret = hdlr->handler(nladdr, n, arg);

	return ret;
}

/***********************************************************************
 * high level stuff 
 ***********************************************************************/

/**
 * ctnl_open - open a libctnetlink handle
 *
 * cth: pointer to already allocated library handle
 * subsys_id: ID of the subsystem
 * subscriptions: netlink groups we are interested in
 */
int ctnl_open(struct ctnl_handle *cth, u_int8_t subsys_id, 
	      unsigned subscriptions)
{
	int err;
	u_int8_t cb_count;

	switch(subsys_id) {
		case NFNL_SUBSYS_CTNETLINK:
			cb_count = IPCTNL_MSG_MAX;
			break;
		case NFNL_SUBSYS_CTNETLINK_EXP:
			cb_count = IPCTNL_MSG_EXP_MAX;
			break;
		default:
			return -ENOENT;
			break;
	}
	memset(cth, 0, sizeof(*cth));

	err = nfnl_open(&cth->nfnlh, subsys_id, cb_count, subscriptions);
	if (err < 0) {
		return err;
	}

	return 0;
}

/**
 * ctnl_close - close a libctnetlink handle
 *
 * cth: libctnetlink handle
 */
int ctnl_close(struct ctnl_handle *cth)
{
	int err;

	err = nfnl_close(&cth->nfnlh);

	return err;
}

/* ctnl_register_handler - register handler for ctnetlink mesage type
 *
 * cth: libctnetlink handle
 * hndlr: handler structure
 */
int ctnl_register_handler(struct ctnl_handle *cth, 
			  struct ctnl_msg_handler *hndlr)
{
	if (hndlr->type >= IPCTNL_MSG_MAX)
		return -EINVAL;

	cth->handler[hndlr->type] = hndlr;
	
	return 0;
}

/**
 * ctnl_unregister_handler - unregister handler for ctnetlink msgtype
 *
 * cth: libctnetlink handle
 * type: message type
 */
int ctnl_unregister_handler(struct ctnl_handle *cth, int type)
{
	if (type >= IPCTNL_MSG_MAX)
		return -EINVAL;

	cth->handler[type] = NULL;
	return 0;
}

int ctnl_flush_conntrack(struct ctnl_handle *cth)
{
	struct nfnlhdr *req;
	char buf[sizeof(*req)];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
			0, AF_INET, 0, IPCTNL_MSG_CT_DELETE,
			NLM_F_REQUEST|NLM_F_ACK);

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0 )
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

/**
 * ctnl_list_conntrack - list connection tracking table
 * cth: libctnetlink handle
 * family: AF_INET, ...
 */
int ctnl_list_conntrack(struct ctnl_handle *cth, int family)
{
	if (ctnl_wilddump_request(cth, family, IPCTNL_MSG_CT_GET) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

int ctnl_list_conntrack_zero_counters(struct ctnl_handle *cth, int family)
{
	if (ctnl_wilddump_request(cth, family, IPCTNL_MSG_CT_GET_CTRZERO) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

int ctnl_event_conntrack(struct ctnl_handle *cth, int family)
{
	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

static void ctnl_build_tuple_ip(struct nfnlhdr *req, int size,
			        struct ctnl_tuple *t)
{
	struct nfattr *nest;

	nest = nfnl_nest(&req->nlh, size, CTA_TUPLE_IP);

	nfnl_addattr_l(&req->nlh, size, CTA_IP_V4_SRC, &t->src.v4, 
		       sizeof(u_int32_t));

	nfnl_addattr_l(&req->nlh, size, CTA_IP_V4_DST, &t->dst.v4,
		       sizeof(u_int32_t));

	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_tuple_proto(struct nfnlhdr *req, int size,
				   struct ctnl_tuple *t)
{
	struct nfattr *nest;

	nest = nfnl_nest(&req->nlh, size, CTA_TUPLE_PROTO);

	nfnl_addattr_l(&req->nlh, size, CTA_PROTO_NUM, &t->protonum,
		       sizeof(u_int16_t));

	switch(t->protonum) {
	case IPPROTO_TCP:
	case IPPROTO_UDP:
	case IPPROTO_SCTP:
		nfnl_addattr_l(&req->nlh, size, CTA_PROTO_SRC_PORT,
			       &t->l4src.tcp.port, sizeof(u_int16_t));
		nfnl_addattr_l(&req->nlh, size, CTA_PROTO_DST_PORT,
			       &t->l4dst.tcp.port, sizeof(u_int16_t));
		break;
	case IPPROTO_ICMP:
		nfnl_addattr_l(&req->nlh, size, CTA_PROTO_ICMP_CODE,
			       &t->l4dst.icmp.code, sizeof(u_int8_t));
		nfnl_addattr_l(&req->nlh, size, CTA_PROTO_ICMP_TYPE,
			       &t->l4dst.icmp.type, sizeof(u_int8_t));
		/* This is an ICMP echo */
		if (t->l4dst.icmp.type == 8)
			nfnl_addattr_l(&req->nlh, size, CTA_PROTO_ICMP_ID,
				       &t->l4src.icmp.id, sizeof(u_int16_t));
		break;
	}
	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_tuple(struct nfnlhdr *req, int size, 
			     struct ctnl_tuple *t, int type)
{
	struct nfattr *nest;

	nest = nfnl_nest(&req->nlh, size, type);

	ctnl_build_tuple_ip(req, size, t);
	ctnl_build_tuple_proto(req, size, t);

	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_protoinfo(struct nfnlhdr *req, int size,
				 struct ctnl_conntrack *ct)
{
	struct nfattr *nest;
	
	nest = nfnl_nest(&req->nlh, size, CTA_PROTOINFO);

	switch (ct->tuple[CTNL_DIR_ORIGINAL].protonum) {
	case IPPROTO_TCP: {
		struct nfattr *nest_proto;
		nest_proto = nfnl_nest(&req->nlh, size, CTA_PROTOINFO_TCP);
		nfnl_addattr_l(&req->nlh, size, CTA_PROTOINFO_TCP_STATE,
			       &ct->protoinfo.tcp.state, sizeof(u_int8_t));
		nfnl_nest_end(&req->nlh, nest_proto);
		break;
		}
	default:
		break;
	}

	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_protonat(struct nfnlhdr *req, int size,
				 struct ctnl_conntrack *ct)
{
	struct nfattr *nest;
	
	nest = nfnl_nest(&req->nlh, size, CTA_NAT_PROTO);

	switch (ct->tuple[CTNL_DIR_ORIGINAL].protonum) {
#if 0
	case IPPROTO_TCP:
		nfnl_addattr_l(&req->nlh, size, CTA_PROTONAT_TCP_MIN,
			       &ct->nat.l4min.tcp.port, sizeof(u_int16_t));
		nfnl_addattr_l(&req->nlh, size, CTA_PROTONAT_TCP_MAX,
			       &ct->nat.l4max.tcp.port, sizeof(u_int16_t));
		break;
	case IPPROTO_UDP:
		nfnl_addattr_l(&req->nlh, size, CTA_PROTONAT_UDP_MIN,
			       &ct->nat.l4min.udp.port, sizeof(u_int16_t));
		nfnl_addattr_l(&req->nlh, size, CTA_PROTONAT_UDP_MAX,
			       &ct->nat.l4max.udp.port, sizeof(u_int16_t));
		break;
#endif
	}
	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_nat(struct nfnlhdr *req, int size,
			   struct ctnl_conntrack *ct)
{
	struct nfattr *nest;

	nest = nfnl_nest(&req->nlh, size, CTA_NAT);

	nfnl_addattr_l(&req->nlh, size, CTA_NAT_MINIP,
		       &ct->nat.min_ip, sizeof(u_int32_t));
	
	if (ct->nat.min_ip != ct->nat.max_ip)
		nfnl_addattr_l(&req->nlh, size, CTA_NAT_MAXIP,
			       &ct->nat.max_ip, sizeof(u_int32_t));

	if (ct->nat.l4min.all != ct->nat.l4max.all)
		ctnl_build_protonat(req, size, ct);

	nfnl_nest_end(&req->nlh, nest);
}

static void ctnl_build_conntrack(struct nfnlhdr *req, int size, 
				 struct ctnl_conntrack *ct)
{
	ctnl_build_tuple(req, size, &ct->tuple[CTNL_DIR_ORIGINAL], 
			 CTA_TUPLE_ORIG);
	ctnl_build_tuple(req, size, &ct->tuple[CTNL_DIR_REPLY],
			 CTA_TUPLE_REPLY);
	
	nfnl_addattr_l(&req->nlh, size, CTA_STATUS, &ct->status, 
		       sizeof(unsigned int));
	nfnl_addattr_l(&req->nlh, size, CTA_TIMEOUT, &ct->timeout, 
		       sizeof(unsigned long));

	ctnl_build_protoinfo(req, size, ct);
	if (ct->nat.min_ip != 0)
		ctnl_build_nat(req, size, ct);
}

/**
 * ctnl_get_conntrack - get a connection from conntrack hashtable
 * cth: libctnetlink handle
 * t: tuple of connection to get
 * cb: a struct nfattr to put the connection in
 */
int ctnl_get_conntrack(struct ctnl_handle *cth, 
		       struct ctnl_tuple *tuple,
		       int dir)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];
	
	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
			0, AF_INET, 0, IPCTNL_MSG_CT_GET,
			NLM_F_REQUEST|NLM_F_ACK);

	ctnl_build_tuple(req, sizeof(buf), tuple, dir); 

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

/**
 * ctnl_del_conntrack - delete a connection from conntrack hashtable
 * cth: libctnetlink handle
 * t: tuple of to-be-deleted connection
 */
int ctnl_del_conntrack(struct ctnl_handle *cth, 
		       struct ctnl_tuple *tuple,
		       int dir)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];
	int type = dir ? CTA_TUPLE_REPLY : CTA_TUPLE_ORIG;

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
		      0, AF_INET, 0, IPCTNL_MSG_CT_DELETE,
		      NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK);

	ctnl_build_tuple(req, sizeof(buf), tuple, type); 

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}
static int new_update_conntrack(struct ctnl_handle *cth,
				struct ctnl_conntrack *ct,
				u_int16_t msg_flags)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
		      0, AF_INET, 0, IPCTNL_MSG_CT_NEW,
		      NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK|msg_flags);

	ctnl_build_conntrack(req, sizeof(buf), ct);

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0 )
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

/**
 * ctnl_new_conntrack - create a connection in the conntrack hashtable
 * cth: libctnetlink handle
 * t: tuple of to-be-created connection
 */
int ctnl_new_conntrack(struct ctnl_handle *cth, struct ctnl_conntrack *ct)
{
	return new_update_conntrack(cth, ct, NLM_F_EXCL);
}

int ctnl_upd_conntrack(struct ctnl_handle *cth, struct ctnl_conntrack *ct)
{
	return new_update_conntrack(cth, ct, 0);
}

/**
 * ctnl_list_expect - retrieve a list of expectations from conntrack subsys
 * cth: libctnetlink handle
 * family: AF_INET, ...
 */
int ctnl_list_expect(struct ctnl_handle *cth, int family)
{
	if (ctnl_wilddump_request(cth, family, IPCTNL_MSG_EXP_GET) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);

}

int ctnl_event_expect(struct ctnl_handle *cth, int family)
{
	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

int ctnl_flush_expect(struct ctnl_handle *cth)
{
	struct nfnlhdr *req;
	char buf[sizeof(*req)];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
			0, AF_INET, 0, IPCTNL_MSG_EXP_DELETE,
			NLM_F_REQUEST|NLM_F_ACK);

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0 )
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

/**
 * ctnl_new_expect - create a new expectation
 *
 * cth: libctnetlink handle
 * master_tuple: tuple of the master original direction
 * t: direction, original or reply.
 * exp_tuple: tuple of to-be-created expectation
 * mask: mask of to-be-created expectation
 * timeout: timeout of new expectation
 */
int ctnl_new_expect(struct ctnl_handle *cth,
		    struct ctnl_tuple *master,
		    struct ctnl_tuple *tuple,
		    struct ctnl_tuple *mask,
		    unsigned long timeout)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
		      0, AF_INET, 0, IPCTNL_MSG_EXP_NEW,
		      NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK);

	ctnl_build_tuple(req, sizeof(buf), master, CTA_EXPECT_MASTER);
	ctnl_build_tuple(req, sizeof(buf), tuple, CTA_EXPECT_TUPLE);
	ctnl_build_tuple(req, sizeof(buf), mask, CTA_EXPECT_MASK);
	
	if (nfnl_addattr_l(&req->nlh, sizeof(buf), CTA_EXPECT_TIMEOUT, &timeout,
			   sizeof(timeout)) < 0)
		return -1;

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0 )
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

/**
 * ctnl_del_expect - delete an expectation from conntrack subsystem
 *
 * cth: libctnetlink handle
 * t: tuple of to-be-deleted expectation
 */
int ctnl_del_expect(struct ctnl_handle *cth, 
		    struct ctnl_tuple *tuple)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
		      0, AF_INET, 0, IPCTNL_MSG_EXP_DELETE,
		      NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_ACK);

	ctnl_build_tuple(req, sizeof(buf), tuple, CTA_EXPECT_MASTER);

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0)
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}

int ctnl_get_expect(struct ctnl_handle *cth, 
		    struct ctnl_tuple *tuple)
{
	struct nfnlhdr *req;
	char buf[CTNL_BUFFSIZE];

	memset(&buf, 0, sizeof(buf));
	req = (void *) &buf;

	nfnl_fill_hdr(&cth->nfnlh, (struct nlmsghdr *) &buf,
			0, AF_INET, 0, IPCTNL_MSG_EXP_GET,
			NLM_F_REQUEST|NLM_F_ACK);

	ctnl_build_tuple(req, sizeof(buf), tuple, CTA_EXPECT_MASTER);

	if (nfnl_send(&cth->nfnlh, (struct nlmsghdr *)&buf) < 0 )
		return -1;

	return nfnl_listen(&cth->nfnlh, &callback_handler, cth);
}