summaryrefslogtreecommitdiffstats
path: root/src/netlink.c
blob: 94200b994144584f8ca111f7692276f95302e299 (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
/*
 * (C) 2006 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.
 */

#include "conntrackd.h"
#include <libnfnetlink/libnfnetlink.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include <errno.h>
#include "us-conntrack.h"
#include <signal.h>
#include <stdlib.h>
#include "network.h"

static int ignore_conntrack(struct nf_conntrack *ct)
{
	/* ignore a certain protocol */
	if (CONFIG(ignore_protocol)[nfct_get_attr_u8(ct, ATTR_ORIG_L4PROTO)])
		return 1;

	/* Accept DNAT'ed traffic: not really coming to the local machine */
	if ((CONFIG(flags) & STRIP_NAT) && 
	    nfct_getobjopt(ct, NFCT_GOPT_IS_DNAT)) {
		debug_ct(ct, "DNAT");
		return 0;
	}

        /* Accept SNAT'ed traffic: not really coming to the local machine */
	if ((CONFIG(flags) & STRIP_NAT) && 
	    nfct_getobjopt(ct, NFCT_GOPT_IS_SNAT)) {
		debug_ct(ct, "SNAT");
		return 0;
	}

	/* Ignore traffic */
	if (ignore_pool_test(STATE(ignore_pool), ct)) {
		debug_ct(ct, "ignore traffic");
		return 1;
	}

	return 0;
}

static int nl_event_handler(struct nlmsghdr *nlh,
			    struct nfattr *nfa[],
			    void *data)
{
	char tmp[1024];
	struct nf_conntrack *ct = (struct nf_conntrack *) tmp;
	int type;

	memset(tmp, 0, sizeof(tmp));

	if ((type = nfct_parse_conntrack(NFCT_T_ALL, nlh, ct)) == NFCT_T_ERROR)
		return NFCT_CB_STOP;

	/* 
	 * Ignore this conntrack: it talks about a
	 * connection that is not interesting for us.
	 */
	if (ignore_conntrack(ct))
		return NFCT_CB_STOP;

	switch(type) {
	case NFCT_T_NEW:
		STATE(mode)->event_new(ct, nlh);
		break;
	case NFCT_T_UPDATE:
		STATE(mode)->event_upd(ct, nlh);
		break;
	case NFCT_T_DESTROY:
		if (STATE(mode)->event_dst(ct, nlh))
			update_traffic_stats(ct);
		break;
	default:
		dlog(STATE(log), "received unknown msg from ctnetlink\n");
		break;
	}

	return NFCT_CB_STOP;
}

int nl_init_event_handler(void)
{
	struct nfnl_callback cb_events = {
		.call		= nl_event_handler,
		.attr_count	= CTA_MAX
	};

	/* open event netlink socket */
	STATE(event) = nfnl_open();
	if (!STATE(event))
		return -1;

	/* set up socket buffer size */
	if (CONFIG(netlink_buffer_size))
		nfnl_rcvbufsiz(STATE(event), CONFIG(netlink_buffer_size));
	else {
		socklen_t socklen = sizeof(unsigned int);
		unsigned int read_size;

		/* get current buffer size */
		getsockopt(nfnl_fd(STATE(event)), SOL_SOCKET,
			   SO_RCVBUF, &read_size, &socklen);

		CONFIG(netlink_buffer_size) = read_size;
	}

	/* ensure that maximum grown size is >= than maximum size */
	if (CONFIG(netlink_buffer_size_max_grown) < CONFIG(netlink_buffer_size))
		CONFIG(netlink_buffer_size_max_grown) = 
					CONFIG(netlink_buffer_size);

	/* open event subsystem */
	STATE(subsys_event) = nfnl_subsys_open(STATE(event),
					       NFNL_SUBSYS_CTNETLINK,
					       IPCTNL_MSG_MAX,
					       NFCT_ALL_CT_GROUPS);
	if (STATE(subsys_event) == NULL)
		return -1;

	/* register callback for new and update events */
	nfnl_callback_register(STATE(subsys_event),
			       IPCTNL_MSG_CT_NEW,
			       &cb_events);

	/* register callback for delete events */
	nfnl_callback_register(STATE(subsys_event),
			       IPCTNL_MSG_CT_DELETE,
			       &cb_events);

	return 0;
}

static int nl_dump_handler(struct nlmsghdr *nlh,
			   struct nfattr *nfa[],
			   void *data)
{
	char buf[1024];
	struct nf_conntrack *ct = (struct nf_conntrack *) buf;
	int type;

	memset(buf, 0, sizeof(buf));

	if ((type = nfct_parse_conntrack(NFCT_T_ALL, nlh, ct)) == NFCT_T_ERROR)
		return NFCT_CB_CONTINUE;

	/* 
	 * Ignore this conntrack: it talks about a
	 * connection that is not interesting for us.
	 */
	if (ignore_conntrack(ct))
		return NFCT_CB_CONTINUE;

	switch(type) {
	case NFCT_T_UPDATE:
		STATE(mode)->dump(ct, nlh);
		break;
	default:
		dlog(STATE(log), "received unknown msg from ctnetlink");
		break;
	}
	return NFCT_CB_CONTINUE;
}

int nl_init_dump_handler(void)
{
	struct nfnl_callback cb_dump = {
		.call		= nl_dump_handler,
		.attr_count	= CTA_MAX
	};

	/* open dump netlink socket */
	STATE(dump) = nfnl_open();
	if (!STATE(dump))
		return -1;

	/* open dump subsystem */
	STATE(subsys_dump) = nfnl_subsys_open(STATE(dump),
					      NFNL_SUBSYS_CTNETLINK,
					      IPCTNL_MSG_MAX,
					      0);
	if (STATE(subsys_dump) == NULL)
		return -1;

	/* register callback for dumped entries */
	nfnl_callback_register(STATE(subsys_dump),
			       IPCTNL_MSG_CT_NEW,
			       &cb_dump);

	if (nl_dump_conntrack_table(STATE(dump), STATE(subsys_dump)) == -1)
		return -1;

	return 0;
}

static int warned = 0;

void nl_resize_socket_buffer(struct nfnl_handle *h)
{
	unsigned int s = CONFIG(netlink_buffer_size) * 2;

	/* already warned that we have reached the maximum buffer size */
	if (warned)
		return;

	if (s > CONFIG(netlink_buffer_size_max_grown)) {
		dlog(STATE(log), "WARNING: maximum netlink socket buffer "
				 "size has been reached. We are likely to "
				 "be losing events, this may lead to "
				 "unsynchronized replicas. Please, consider "
				 "increasing netlink socket buffer size via "
				 "SocketBufferSize and "
				 "SocketBufferSizeMaxGrown clauses in "
				 "conntrackd.conf");
		s = CONFIG(netlink_buffer_size_max_grown);
		warned = 1;
	}

	CONFIG(netlink_buffer_size) = nfnl_rcvbufsiz(h, s);

	/* notify the sysadmin */
	dlog(STATE(log), "netlink socket buffer size has been set to %u bytes", 
			  CONFIG(netlink_buffer_size));
}

int nl_dump_conntrack_table(struct nfnl_handle *h, 
			    struct nfnl_subsys_handle *subsys)
{
	struct nfnlhdr req;

	memset(&req, 0, sizeof(req));
	nfct_build_query(subsys, 
			 NFCT_Q_DUMP, 
			 &CONFIG(family), 
			 &req, 
			 sizeof(req));

	if (nfnl_query(h, &req.nlh) == -1)
		return -1;

	return 0;
}

int nl_flush_master_conntrack_table(void)
{
	struct nfnlhdr req;

	memset(&req, 0, sizeof(req));
	nfct_build_query(STATE(subsys_dump), 
			 NFCT_Q_FLUSH, 
			 &CONFIG(family), 
			 &req, 
			 sizeof(req));

	if (nfnl_query(STATE(dump), &req.nlh) == -1)
		return -1;

	return 0;
}