summaryrefslogtreecommitdiffstats
path: root/ipsec-tools/src/racoon/evt.c
blob: f11966622bb528a04002b8008ee6f7bd9fcb2de0 (plain) (blame)
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
#include <machine/rtems-bsd-user-space.h>
#ifdef __rtems__
#include <machine/rtems-bsd-program.h>
#include "rtems-bsd-racoon-namespace.h"
#endif /* __rtems__ */

/*	$NetBSD: evt.c,v 1.10 2010/10/21 06:15:28 tteras Exp $	*/

/* Id: evt.c,v 1.5 2006/06/22 20:11:35 manubsd Exp */

/*
 * Copyright (C) 2004 Emmanuel Dreyfus
 * Copyright (C) 2008 Timo Teras
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <sys/socket.h>

#include "vmbuf.h"
#include "plog.h"
#include "misc.h"
#include "admin.h"
#include "handler.h"
#include "session.h"
#include "gcmalloc.h"
#include "evt.h"
#include "var.h"

#ifdef ENABLE_ADMINPORT

static EVT_LISTENER_LIST(evt_listeners);

struct evt_message {
	struct admin_com adm;
	struct evt_async evt;
};

struct evt {
	struct evtdump *dump;
	TAILQ_ENTRY(evt) next;
};

TAILQ_HEAD(evtlist, evt);

#define EVTLIST_MAX	32

static struct evtlist evtlist = TAILQ_HEAD_INITIALIZER(evtlist);
static int evtlist_len = 0;
static int evtlist_inuse = 0;

static struct {
	int newtype, oldtype;
} evttype_map[] = {
	{ EVT_RACOON_QUIT,		EVTT_RACOON_QUIT },
	{ EVT_PHASE1_UP,		EVTT_PHASE1_UP },
	{ EVT_PHASE1_DOWN,		EVTT_PHASE1_DOWN },
	{ EVT_PHASE1_NO_RESPONSE,	EVTT_PEER_NO_RESPONSE },
	{ EVT_PHASE1_NO_PROPOSAL,	EVTT_PEERPH1_NOPROP },
	{ EVT_PHASE1_AUTH_FAILED,	EVTT_PEERPH1AUTH_FAILED },
	{ EVT_PHASE1_DPD_TIMEOUT,	EVTT_DPD_TIMEOUT },
	{ EVT_PHASE1_PEER_DELETED,	EVTT_PEER_DELETE },
	{ EVT_PHASE1_MODE_CFG,		EVTT_ISAKMP_CFG_DONE },
	{ EVT_PHASE1_XAUTH_SUCCESS,	EVTT_XAUTH_SUCCESS },
	{ EVT_PHASE1_XAUTH_FAILED,	EVTT_XAUTH_FAILED },
	{ EVT_PHASE2_NO_PHASE1,		-1 },
	{ EVT_PHASE2_UP,		EVTT_PHASE2_UP },
	{ EVT_PHASE2_DOWN,		EVTT_PHASE2_DOWN },
	{ EVT_PHASE2_NO_RESPONSE,	EVTT_PEER_NO_RESPONSE },
};

static void
evt_push(src, dst, type, optdata)
	struct sockaddr *src;
	struct sockaddr *dst;
	int type;
	vchar_t *optdata;
{
	struct evtdump *evtdump;
	struct evt *evt;
	size_t len;
	int i;

	/* If admin socket is disabled, silently discard anything */
	if (adminsock_path == NULL || !evtlist_inuse)
		return;

	/* Map the event type to old */
	for (i = 0; i < sizeof(evttype_map) / sizeof(evttype_map[0]); i++)
		if (evttype_map[i].newtype == type)
			break;
	if (i >= sizeof(evttype_map) / sizeof(evttype_map[0]))
		return;

	type = evttype_map[i].oldtype;
	if (type < 0)
		return;

	/* If we are above the limit, don't record anything */
	if (evtlist_len > EVTLIST_MAX) {
		plog(LLV_DEBUG, LOCATION, NULL, 
		    "Cannot record event: event queue overflowed\n");
		return;
	}

	/* If we hit the limit, record an overflow event instead */
	if (evtlist_len == EVTLIST_MAX) {
		plog(LLV_ERROR, LOCATION, NULL, 
		    "Cannot record event: event queue overflow\n");
		src = NULL;
		dst = NULL;
		type = EVTT_OVERFLOW;
		optdata = NULL;
	}

	len = sizeof(*evtdump);
	if (optdata)
		len += optdata->l;

	if ((evtdump = racoon_malloc(len)) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n",
		    strerror(errno));
		return;
	}

	if ((evt = racoon_malloc(sizeof(*evt))) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n",
		    strerror(errno));
		racoon_free(evtdump);
		return;
	}

	if (src)
		memcpy(&evtdump->src, src, sysdep_sa_len(src));
	if (dst)
		memcpy(&evtdump->dst, dst, sysdep_sa_len(dst));
	evtdump->len = len;
	evtdump->type = type;
	time(&evtdump->timestamp);

	if (optdata)
		memcpy(evtdump + 1, optdata->v, optdata->l);

	evt->dump = evtdump;
	TAILQ_INSERT_TAIL(&evtlist, evt, next);

	evtlist_len++;

	return;
}

static struct evtdump *
evt_pop(void) {
	struct evtdump *evtdump;
	struct evt *evt;

	if ((evt = TAILQ_FIRST(&evtlist)) == NULL)
		return NULL;

	evtdump = evt->dump;
	TAILQ_REMOVE(&evtlist, evt, next);
	racoon_free(evt);
	evtlist_len--;

	return evtdump;
}

vchar_t *
evt_dump(void) {
	struct evtdump *evtdump;
	vchar_t *buf = NULL;

	if (!evtlist_inuse) {
		evtlist_inuse = 1;
		plog(LLV_ERROR, LOCATION, NULL,
		     "evt_dump: deprecated event polling used\n");
	}

	if ((evtdump = evt_pop()) != NULL) {
		if ((buf = vmalloc(evtdump->len)) == NULL) {
			plog(LLV_ERROR, LOCATION, NULL, 
			    "evt_dump failed: %s\n", strerror(errno));
			return NULL;
		}
		memcpy(buf->v, evtdump, evtdump->len);	
		racoon_free(evtdump);
	}

	return buf;
}

static struct evt_message *
evtmsg_create(type, optdata)
	int type;
	vchar_t *optdata;
{
	struct evt_message *e;
	size_t len;

	len = sizeof(struct evt_message);
	if (optdata != NULL)
		len += optdata->l;

	if ((e = racoon_malloc(len)) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL, "Cannot allocate event: %s\n",
		     strerror(errno));
		return NULL;
	}

	memset(e, 0, sizeof(struct evt_message));
	e->adm.ac_len = len;
	e->adm.ac_cmd = ADMIN_SHOW_EVT;
	e->adm.ac_errno = 0;
	e->adm.ac_proto = 0;
	e->evt.ec_type = type;
	time(&e->evt.ec_timestamp);
	if (optdata != NULL)
		memcpy(e + 1, optdata->v, optdata->l);

	return e;
}

static void
evt_unsubscribe(l)
	struct evt_listener *l;
{
	plog(LLV_DEBUG, LOCATION, NULL,
	     "[%d] admin connection released\n", l->fd);

	LIST_REMOVE(l, ll_chain);
	unmonitor_fd(l->fd);
	close(l->fd);
	racoon_free(l);
}

static int
evt_unsubscribe_cb(ctx, fd)
	void *ctx;
	int fd;
{
	evt_unsubscribe((struct evt_listener *) ctx);
	return 0;
}

static void
evtmsg_broadcast(ll, e)
	const struct evt_listener_list *ll;
	struct evt_message *e;
{
	struct evt_listener *l, *nl;

	for (l = LIST_FIRST(ll); l != NULL; l = nl) {
		nl = LIST_NEXT(l, ll_chain);

		if (send(l->fd, e, e->adm.ac_len, MSG_DONTWAIT) < 0) {
			plog(LLV_DEBUG, LOCATION, NULL, "Cannot send event to fd: %s\n",
				strerror(errno));
			evt_unsubscribe(l);
		}
	}
}

void
evt_generic(type, optdata)
	int type;
	vchar_t *optdata;
{
	struct evt_message *e;

	if ((e = evtmsg_create(type, optdata)) == NULL)
		return;

	evtmsg_broadcast(&evt_listeners, e);
	evt_push(&e->evt.ec_ph1src, &e->evt.ec_ph1dst, type, optdata);

	racoon_free(e);
}

void
evt_phase1(ph1, type, optdata)
	const struct ph1handle *ph1;
	int type;
	vchar_t *optdata;
{
	struct evt_message *e;

	if ((e = evtmsg_create(type, optdata)) == NULL)
		return;

	if (ph1->local)
		memcpy(&e->evt.ec_ph1src, ph1->local, sysdep_sa_len(ph1->local));
	if (ph1->remote)
		memcpy(&e->evt.ec_ph1dst, ph1->remote, sysdep_sa_len(ph1->remote));

	evtmsg_broadcast(&ph1->evt_listeners, e);
	evtmsg_broadcast(&evt_listeners, e);
	evt_push(&e->evt.ec_ph1src, &e->evt.ec_ph1dst, type, optdata);

	racoon_free(e);
}

void
evt_phase2(ph2, type, optdata)
	const struct ph2handle *ph2;
	int type;
	vchar_t *optdata;
{
	struct evt_message *e;
	struct ph1handle *ph1 = ph2->ph1;

	if ((e = evtmsg_create(type, optdata)) == NULL)
		return;

	if (ph1) {
		if (ph1->local)
			memcpy(&e->evt.ec_ph1src, ph1->local, sysdep_sa_len(ph1->local));
		if (ph1->remote)
			memcpy(&e->evt.ec_ph1dst, ph1->remote, sysdep_sa_len(ph1->remote));
	}
	e->evt.ec_ph2msgid = ph2->msgid;

	evtmsg_broadcast(&ph2->evt_listeners, e);
	if (ph1)
		evtmsg_broadcast(&ph1->evt_listeners, e);
	evtmsg_broadcast(&evt_listeners, e);
	evt_push(&e->evt.ec_ph1src, &e->evt.ec_ph1dst, type, optdata);

	racoon_free(e);
}

int
evt_subscribe(list, fd)
	struct evt_listener_list *list;
	int fd;
{
	struct evt_listener *l;

	if ((l = racoon_malloc(sizeof(*l))) == NULL) {
		plog(LLV_ERROR, LOCATION, NULL,
		     "Cannot allocate event listener: %s\n",
		     strerror(errno));
		return errno;
	}

	if (list == NULL)
		list = &evt_listeners;

	LIST_INSERT_HEAD(list, l, ll_chain);
	l->fd = fd;
	monitor_fd(l->fd, evt_unsubscribe_cb, l, 0);

	plog(LLV_DEBUG, LOCATION, NULL,
	     "[%d] admin connection is polling events\n", fd);

	return -2;
}

void
evt_list_init(list)
	struct evt_listener_list *list;
{
	LIST_INIT(list);
}

void
evt_list_cleanup(list)
	struct evt_listener_list *list;
{
	while (!LIST_EMPTY(list))
		evt_unsubscribe(LIST_FIRST(list));
}

#endif /* ENABLE_ADMINPORT */
#ifdef __rtems__
#include "rtems-bsd-racoon-evt-data.h"
#endif /* __rtems__ */