summaryrefslogtreecommitdiffstats
path: root/daemon/src/sync-nack.c
blob: 288dba450f298cc2c67af9d39d41b14acfd26357 (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
/*
 * (C) 2006-2007 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 <errno.h>
#include "conntrackd.h"
#include "sync.h"
#include "linux_list.h"
#include "us-conntrack.h"
#include "buffer.h"
#include "debug.h"
#include "network.h"
#include <libnfnetlink/libnfnetlink.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>

#if 0 
#define dp printf
#else
#define dp
#endif

static LIST_HEAD(queue);

struct cache_nack {
	struct list_head 	head;
	u_int32_t 		seq;
};

static void cache_nack_add(struct us_conntrack *u, void *data)
{
	struct cache_nack *cn = data;

	INIT_LIST_HEAD(&cn->head);
	list_add(&cn->head, &queue);
}

static void cache_nack_update(struct us_conntrack *u, void *data)
{
	struct cache_nack *cn = data;

	if (cn->head.next != LIST_POISON1 &&
	    cn->head.prev != LIST_POISON2)
		list_del(&cn->head);

	INIT_LIST_HEAD(&cn->head);
	list_add(&cn->head, &queue);
}

static void cache_nack_destroy(struct us_conntrack *u, void *data)
{
	struct cache_nack *cn = data;

	if (cn->head.next != LIST_POISON1 &&
	    cn->head.prev != LIST_POISON2)
		list_del(&cn->head);
}

static struct cache_extra cache_nack_extra = {
	.size 		= sizeof(struct cache_nack),
	.add		= cache_nack_add,
	.update		= cache_nack_update,
	.destroy	= cache_nack_destroy
};

static int nack_init()
{
	STATE_SYNC(buffer) = buffer_create(CONFIG(resend_buffer_size));
	if (STATE_SYNC(buffer) == NULL)
		return -1;

	return 0;
}

static void nack_kill()
{
	buffer_destroy(STATE_SYNC(buffer));
}

static void mcast_send_nack(u_int32_t expt_seq, u_int32_t recv_seq)
{
	struct nlnetwork_ack nack = {
		.flags = NET_NACK,
		.from  = expt_seq,
		.to    = recv_seq,
	};

	mcast_send_error(STATE_SYNC(mcast_client), &nack);
	buffer_add(STATE_SYNC(buffer), &nack, sizeof(struct nlnetwork_ack));
}

static void mcast_send_ack(u_int32_t from, u_int32_t to)
{
	struct nlnetwork_ack ack = {
		.flags = NET_ACK,
		.from   = from,
		.to	= to,
	};

	mcast_send_error(STATE_SYNC(mcast_client), &ack);
	buffer_add(STATE_SYNC(buffer), &ack, sizeof(struct nlnetwork_ack));
}

static void mcast_send_resync()
{
	struct nlnetwork net = {
		.flags = NET_RESYNC,
	};

	mcast_send_error(STATE_SYNC(mcast_client), &net);
	buffer_add(STATE_SYNC(buffer), &net, sizeof(struct nlnetwork));
}

int nack_local(int fd, int type, void *data)
{
	int ret = 1;

	switch(type) {
		case REQUEST_DUMP:
			mcast_send_resync();
			dlog(STATE(log), "[REQ] request resync");
			break;
		default:
			ret = 0;
			break;
	}

	return ret;
}

static int buffer_compare(void *data1, void *data2)
{
	struct nlnetwork *net = data1;
	struct nlnetwork_ack *nack = data2;
	struct nlmsghdr *nlh = data1 + sizeof(struct nlnetwork);

	unsigned old_seq = ntohl(net->seq);

	if (ntohl(net->seq) >= nack->from && ntohl(net->seq) <= nack->to) {
		if (mcast_resend_netmsg(STATE_SYNC(mcast_client), net))
			dp("resend destroy (old seq=%u) (seq=%u)\n", 
			   old_seq, ntohl(net->seq));
	}
	return 0;
}

static int buffer_remove(void *data1, void *data2)
{
	struct nlnetwork *net = data1;
	struct nlnetwork_ack *h = data2;

	if (ntohl(net->seq) >= h->from && ntohl(net->seq) <= h->to) {
		dp("remove from buffer (seq=%u)\n", ntohl(net->seq));
		__buffer_del(STATE_SYNC(buffer), data1);
	}
	return 0;
}

static void queue_resend(struct cache *c, unsigned int from, unsigned int to)
{
	struct list_head *n;
	struct us_conntrack *u;
	unsigned int *seq;

	lock();
	list_for_each(n, &queue) {
		struct cache_nack *cn = (struct cache_nack *) n;
		struct us_conntrack *u;
		
		u = cache_get_conntrack(STATE_SYNC(internal), cn);

		if (cn->seq >= from && cn->seq <= to) {
			debug_ct(u->ct, "resend nack");
			dp("resending nack'ed (oldseq=%u) ", cn->seq);

			char buf[4096];
			struct nlnetwork *net = (struct nlnetwork *) buf;

			int ret = build_network_msg(NFCT_Q_UPDATE,
						    STATE(subsys_event),
						    u->ct,
						    buf,
						    sizeof(buf));
			if (ret == -1) {
				unlock();
				break;
			}

			mcast_send_netmsg(STATE_SYNC(mcast_client), buf); 
			STATE_SYNC(mcast_sync)->post_send(net, u);
			dp("(newseq=%u)\n", *seq);
		} 
	}
	unlock();
}

static void queue_empty(struct cache *c, unsigned int from, unsigned int to)
{
	struct list_head *n, *tmp;
	struct us_conntrack *u;
	unsigned int *seq;

	lock();
	dp("ACK from %u to %u\n", from, to);
	list_for_each_safe(n, tmp, &queue) {
		struct cache_nack *cn = (struct cache_nack *) n;

		u = cache_get_conntrack(STATE_SYNC(internal), cn);
		if (cn->seq >= from && cn->seq <= to) {
			dp("remove %u\n", cn->seq);
			debug_ct(u->ct, "ack received: empty queue");
			dp("queue: deleting from queue (seq=%u)\n", cn->seq);
			list_del(&cn->head);
		} 
	}
	unlock();
}

static int nack_pre_recv(const struct nlnetwork *net)
{
	static unsigned int window = 0;
	unsigned int exp_seq;

	if (window == 0)
		window = CONFIG(window_size);

	if (!mcast_track_seq(net->seq, &exp_seq)) {
		dp("OOS: sending nack (seq=%u)\n", exp_seq);
		mcast_send_nack(exp_seq, net->seq - 1);
		window = CONFIG(window_size);
	} else {
		/* received a window, send an acknowledgement */
		if (--window == 0) {
			dp("sending ack (seq=%u)\n", net->seq);
			mcast_send_ack(net->seq-CONFIG(window_size), net->seq);
		}
	}

	if (net->flags & NET_NACK) {
		struct nlnetwork_ack *nack = (struct nlnetwork_ack *) net;

		dp("NACK: from seq=%u to seq=%u\n", nack->from, nack->to);
		queue_resend(STATE_SYNC(internal), nack->from, nack->to);
		buffer_iterate(STATE_SYNC(buffer), nack, buffer_compare);
		return 1;
	} else if (net->flags & NET_RESYNC) {
		dp("RESYNC ALL\n");
		cache_bulk(STATE_SYNC(internal));
		return 1;
	} else if (net->flags & NET_ACK) {
		struct nlnetwork_ack *h = (struct nlnetwork_ack *) net;

		dp("ACK: from seq=%u to seq=%u\n", h->from, h->to);
		queue_empty(STATE_SYNC(internal), h->from, h->to);
		buffer_iterate(STATE_SYNC(buffer), h, buffer_remove);
		return 1;
	}

	return 0;
}

static void nack_post_send(const struct nlnetwork *net, struct us_conntrack *u)
{
	unsigned int size = sizeof(struct nlnetwork);
	struct nlmsghdr *nlh = (struct nlmsghdr *) ((void *) net + size);

	if (NFNL_MSG_TYPE(ntohs(nlh->nlmsg_type)) == IPCTNL_MSG_CT_DELETE) {
		buffer_add(STATE_SYNC(buffer), net, 
			   ntohl(nlh->nlmsg_len) + size); 
	} else if (u != NULL) {
		unsigned int *seq;
		struct list_head *n;
		struct cache_nack *cn;

		cn = (struct cache_nack *)
			cache_get_extra(STATE_SYNC(internal), u);
		cn->seq = ntohl(net->seq);
		if (cn->head.next != LIST_POISON1 &&
		    cn->head.prev != LIST_POISON2)
		    	list_del(&cn->head);

		INIT_LIST_HEAD(&cn->head);
		list_add(&cn->head, &queue);
	}
}

struct sync_mode nack = {
	.internal_cache_flags	= LIFETIME,
	.external_cache_flags	= LIFETIME,
	.internal_cache_extra	= &cache_nack_extra,
	.init			= nack_init,
	.kill			= nack_kill,
	.local			= nack_local,
	.pre_recv		= nack_pre_recv,
	.post_send		= nack_post_send,
};