summaryrefslogtreecommitdiffstats
path: root/src/sync-notrack.c
blob: 2d3783e77f0faf7e4dd8ec1519b2fcf4b3882b2a (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
/*
 * (C) 2008 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 "sync.h"
#include "queue.h"
#include "debug.h"
#include "network.h"
#include "log.h"
#include "cache.h"
#include "event.h"

#include <string.h>

static LIST_HEAD(tx_list);
static unsigned int tx_list_len;
static struct queue *tx_queue;

struct cache_notrack {
	struct list_head	tx_list;
};

static void cache_notrack_add(struct cache_object *obj, void *data)
{
	struct cache_notrack *cn = data;
	INIT_LIST_HEAD(&cn->tx_list);
}

static void cache_notrack_del(struct cache_object *obj, void *data)
{
	struct cache_notrack *cn = data;

	if (!list_empty(&cn->tx_list)) {
		list_del(&cn->tx_list);
		tx_list_len--;
	}
}

static struct cache_extra cache_notrack_extra = {
	.size 		= sizeof(struct cache_notrack),
	.add		= cache_notrack_add,
	.destroy	= cache_notrack_del
};

static void tx_queue_add_ctlmsg(uint32_t flags, uint32_t from, uint32_t to)
{
	struct nethdr_ack ack = {
		.type  = NET_T_CTL,
		.flags = flags,
		.from  = from,
		.to    = to,
	};

	queue_add(tx_queue, &ack, NETHDR_ACK_SIZ);
	write_evfd(STATE_SYNC(evfd));
}

static int notrack_init(void)
{
	tx_queue = queue_create(~0U);
	if (tx_queue == NULL) {
		dlog(LOG_ERR, "cannot create tx queue");
		return -1;
	}

	return 0;
}

static void notrack_kill(void)
{
	queue_destroy(tx_queue);
}

static int do_cache_to_tx(void *data1, void *data2)
{
	struct cache_object *obj = data2;
	struct cache_notrack *cn = cache_get_extra(STATE_SYNC(internal), obj);

	if (!list_empty(&cn->tx_list))
		return 0;

	/* add to tx list */
	list_add_tail(&cn->tx_list, &tx_list);
	tx_list_len++;

	write_evfd(STATE_SYNC(evfd));

	return 0;
}

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

	switch(type) {
	case REQUEST_DUMP:
		dlog(LOG_NOTICE, "request resync");
		tx_queue_add_ctlmsg(NET_F_RESYNC, 0, 0);
		break;
	case SEND_BULK:
		dlog(LOG_NOTICE, "sending bulk update");
		cache_iterate(STATE_SYNC(internal), NULL, do_cache_to_tx);
		break;
	default:
		ret = 0;
		break;
	}

	return ret;
}

static int digest_msg(const struct nethdr *net)
{
	if (IS_DATA(net))
		return MSG_DATA;

	if (IS_RESYNC(net)) {
		cache_iterate(STATE_SYNC(internal), NULL, do_cache_to_tx);
		return MSG_CTL;
	}

	return MSG_BAD;
}

static int notrack_recv(const struct nethdr *net)
{
	int ret;
	unsigned int exp_seq;

	mcast_track_seq(net->seq, &exp_seq);

	ret = digest_msg(net);

	if (ret != MSG_BAD)
		mcast_track_update_seq(net->seq);

	return ret;
}

static int tx_queue_xmit(void *data1, const void *data2)
{
	struct nethdr *net = data1;
	nethdr_set_ack(net);
	HDR_HOST2NETWORK(net);
	mcast_buffered_send_netmsg(STATE_SYNC(mcast_client), net);
	queue_del(tx_queue, net);

	return 0;
}

static int tx_list_xmit(struct list_head *i, struct cache_object *obj, int type)
{
	int ret;
	struct nethdr *net = BUILD_NETMSG(obj->ct, type);

	list_del_init(i);
	tx_list_len--;

	ret = mcast_buffered_send_netmsg(STATE_SYNC(mcast_client), net);

	return ret;
}

static void notrack_run(void)
{
	struct cache_notrack *cn, *tmp;

	/* send messages in the tx_queue */
	queue_iterate(tx_queue, NULL, tx_queue_xmit);

	/* send conntracks in the tx_list */
	list_for_each_entry_safe(cn, tmp, &tx_list, tx_list) {
		struct cache_object *obj;

		obj = cache_data_get_object(STATE_SYNC(internal), cn);
		tx_list_xmit(&cn->tx_list, obj, NET_T_STATE_UPD);
	}
}

struct sync_mode sync_notrack = {
	.internal_cache_flags	= LIFETIME,
	.external_cache_flags	= LIFETIME,
	.internal_cache_extra	= &cache_notrack_extra,
	.init			= notrack_init,
	.kill			= notrack_kill,
	.local			= notrack_local,
	.recv			= notrack_recv,
	.run			= notrack_run,
};