summaryrefslogtreecommitdiffstats
path: root/src/external_cache.c
blob: e2902491dd7264dee8bdd828ec7b21acd0bc6b85 (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
/*
 * (C) 2006-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
 * (C) 2011 by Vyatta Inc. <http://www.vyatta.com>
 *
 * 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 "log.h"
#include "cache.h"
#include "external.h"

#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include <stdlib.h>

static struct cache *external;
static struct cache *external_exp;

static int external_cache_init(void)
{
	external = cache_create("external", CACHE_T_CT,
				STATE_SYNC(sync)->external_cache_flags,
				NULL, &cache_sync_external_ct_ops);
	if (external == NULL) {
		dlog(LOG_ERR, "can't allocate memory for the external cache");
		return -1;
	}
	external_exp = cache_create("external", CACHE_T_EXP,
				STATE_SYNC(sync)->external_cache_flags,
				NULL, &cache_sync_external_exp_ops);
	if (external_exp == NULL) {
		dlog(LOG_ERR, "can't allocate memory for the external cache");
		return -1;
	}

	return 0;
}

static void external_cache_close(void)
{
	cache_destroy(external);
	cache_destroy(external_exp);
}

static void external_cache_ct_new(struct nf_conntrack *ct)
{
	struct cache_object *obj;
	int id;

	obj = cache_find(external, ct, &id);
	if (obj == NULL) {
retry:
		obj = cache_object_new(external, ct);
		if (obj == NULL)
			return;

		if (cache_add(external, obj, id) == -1) {
			cache_object_free(obj);
			return;
		}
	} else {
		cache_del(external, obj);
		cache_object_free(obj);
		goto retry;
	}
}

static void external_cache_ct_upd(struct nf_conntrack *ct)
{
	cache_update_force(external, ct);
}

static void external_cache_ct_del(struct nf_conntrack *ct)
{
	struct cache_object *obj;
	int id;

	obj = cache_find(external, ct, &id);
	if (obj) {
		cache_del(external, obj);
		cache_object_free(obj);
	}
}

static void external_cache_ct_dump(int fd, int type)
{
	cache_dump(external, fd, type);
}

static int external_cache_ct_commit(struct nfct_handle *h, int fd)
{
	return cache_commit(external, h, fd);
}

static void external_cache_ct_flush(void)
{
	cache_flush(external);
}

static void external_cache_ct_stats(int fd)
{
	cache_stats(external, fd);
}

static void external_cache_ct_stats_ext(int fd)
{
	cache_stats_extended(external, fd);
}

static void external_cache_exp_new(struct nf_expect *exp)
{
	struct cache_object *obj;
	int id;

	obj = cache_find(external_exp, exp, &id);
	if (obj == NULL) {
retry:
		obj = cache_object_new(external_exp, exp);
		if (obj == NULL)
			return;

		if (cache_add(external_exp, obj, id) == -1) {
			cache_object_free(obj);
			return;
		}
	} else {
		cache_del(external_exp, obj);
		cache_object_free(obj);
		goto retry;
	}
}

static void external_cache_exp_upd(struct nf_expect *exp)
{
	cache_update_force(external_exp, exp);
}

static void external_cache_exp_del(struct nf_expect *exp)
{
	struct cache_object *obj;
	int id;

	obj = cache_find(external_exp, exp, &id);
	if (obj) {
		cache_del(external_exp, obj);
		cache_object_free(obj);
	}
}

static void external_cache_exp_dump(int fd, int type)
{
	cache_dump(external_exp, fd, type);
}

static int external_cache_exp_commit(struct nfct_handle *h, int fd)
{
	return cache_commit(external_exp, h, fd);
}

static void external_cache_exp_flush(void)
{
	cache_flush(external_exp);
}

static void external_cache_exp_stats(int fd)
{
	cache_stats(external_exp, fd);
}

static void external_cache_exp_stats_ext(int fd)
{
	cache_stats_extended(external_exp, fd);
}

struct external_handler external_cache = {
	.init		= external_cache_init,
	.close		= external_cache_close,
	.ct = {
		.new		= external_cache_ct_new,
		.upd		= external_cache_ct_upd,
		.del		= external_cache_ct_del,
		.dump		= external_cache_ct_dump,
		.commit		= external_cache_ct_commit,
		.flush		= external_cache_ct_flush,
		.stats		= external_cache_ct_stats,
		.stats_ext	= external_cache_ct_stats_ext,
	},
	.exp = {
		.new		= external_cache_exp_new,
		.upd		= external_cache_exp_upd,
		.del		= external_cache_exp_del,
		.dump		= external_cache_exp_dump,
		.commit		= external_cache_exp_commit,
		.flush		= external_cache_exp_flush,
		.stats		= external_cache_exp_stats,
		.stats_ext	= external_cache_exp_stats_ext,
	},
};