aboutsummaryrefslogtreecommitdiffstats
path: root/src/pluto/kernel_pfkey.c
blob: 77fff2f9ee2154849c1e49a7626b54749c843ba6 (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
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
/*
 * Copyright (C) 2010 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 * Copyright (C) 2003 Herbert Xu.
 * Copyright (C) 1998-2002  D. Hugh Redelmeier.
 * Copyright (C) 1997 Angelos D. Keromytis.
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <errno.h>
#include <unistd.h>

#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <freeswan.h>
#include <pfkeyv2.h>
#include <pfkey.h>

#include "constants.h"
#include "kernel.h"
#include "kernel_pfkey.h"
#include "log.h"
#include "whack.h"      /* for RC_LOG_SERIOUS */
#include "kernel_alg.h"


static int pfkeyfd = NULL_FD;

typedef u_int32_t pfkey_seq_t;
static pfkey_seq_t pfkey_seq = 0; /* sequence number for our PF_KEY messages */

static pid_t pid;

#define NE(x) { x, #x } /* Name Entry -- shorthand for sparse_names */

static sparse_names pfkey_type_names = {
		NE(SADB_RESERVED),
		NE(SADB_GETSPI),
		NE(SADB_UPDATE),
		NE(SADB_ADD),
		NE(SADB_DELETE),
		NE(SADB_GET),
		NE(SADB_ACQUIRE),
		NE(SADB_REGISTER),
		NE(SADB_EXPIRE),
		NE(SADB_FLUSH),
		NE(SADB_DUMP),
		NE(SADB_X_PROMISC),
		NE(SADB_X_PCHANGE),
		NE(SADB_X_GRPSA),
		NE(SADB_X_ADDFLOW),
		NE(SADB_X_DELFLOW),
		NE(SADB_X_DEBUG),
		NE(SADB_X_NAT_T_NEW_MAPPING),
		NE(SADB_MAX),
		{ 0, sparse_end }
};

#undef NE

typedef union {
		unsigned char bytes[PFKEYv2_MAX_MSGSIZE];
		struct sadb_msg msg;
	} pfkey_buf;

static bool
pfkey_input_ready(void)
{
	int ndes;
	fd_set readfds;
	struct timeval tm = { .tv_sec = 0 }; /* don't wait, polling */

	FD_ZERO(&readfds);  /* we only care about pfkeyfd */
	FD_SET(pfkeyfd, &readfds);

	do {
		ndes = select(pfkeyfd + 1, &readfds, NULL, NULL, &tm);
	} while (ndes == -1 && errno == EINTR);

	if (ndes < 0)
	{
		log_errno((e, "select() failed in pfkey_get()"));
		return FALSE;
	}
	else if (ndes == 0)
	{
		return FALSE;   /* nothing to read */
	}
	passert(ndes == 1 && FD_ISSET(pfkeyfd, &readfds));
	return TRUE;
}

/* get a PF_KEY message from kernel.
 * Returns TRUE if message found, FALSE if no message pending,
 * and aborts or keeps trying when an error is encountered.
 * The only validation of the message is that the message length
 * received matches that in the message header, and that the message
 * is for this process.
 */
static bool
pfkey_get(pfkey_buf *buf)
{
	for (;;)
	{
		/* len must be less than PFKEYv2_MAX_MSGSIZE,
		 * so it should fit in an int.  We use this fact when printing it.
		 */
		ssize_t len;

		if (!pfkey_input_ready())
		{
			return FALSE;
		}

		len = read(pfkeyfd, buf->bytes, sizeof(buf->bytes));

		if (len < 0)
		{
			if (errno == EAGAIN)
			{
				return FALSE;
			}
			log_errno((e, "read() failed in pfkey_get()"));
			return FALSE;
		}
		else if ((size_t)len < sizeof(buf->msg))
		{
			plog("pfkey_get read truncated PF_KEY message: %d bytes; ignoring",
				 (int)len);
		}
		else if ((size_t)len != buf->msg.sadb_msg_len * IPSEC_PFKEYv2_ALIGN)
		{
			plog("pfkey_get read PF_KEY message with length %d that doesn't"
				 " equal sadb_msg_len %u * %u; ignoring message", (int)len,
				 (unsigned)buf->msg.sadb_msg_len, (unsigned)IPSEC_PFKEYv2_ALIGN);
		}
		else if (buf->msg.sadb_msg_pid != (unsigned)pid)
		{
			/* not for us: ignore */
			DBG(DBG_KERNEL,
				DBG_log("pfkey_get: ignoring PF_KEY %s message %u for process"
						" %u", sparse_val_show(pfkey_type_names,
											   buf->msg.sadb_msg_type),
						buf->msg.sadb_msg_seq, buf->msg.sadb_msg_pid));
		}
		else
		{
			DBG(DBG_KERNEL,
				DBG_log("pfkey_get: %s message %u",
						sparse_val_show(pfkey_type_names,
										buf->msg.sadb_msg_type),
						buf->msg.sadb_msg_seq));
			return TRUE;
		}
	}
}

/* get a response to a specific message */
static bool
pfkey_get_response(pfkey_buf *buf, pfkey_seq_t seq)
{
	while (pfkey_get(buf))
	{
		if (buf->msg.sadb_msg_seq == seq)
		{
			return TRUE;
		}
	}
	return FALSE;
}

static bool
pfkey_build(int error, const char *description, const char *text_said,
			struct sadb_ext *extensions[SADB_EXT_MAX + 1])
{
	if (error != 0)
	{
		loglog(RC_LOG_SERIOUS, "building of %s %s failed, code %d", description,
							   text_said, error);
		pfkey_extensions_free(extensions);
		return FALSE;
	}
	return TRUE;
}

/* pfkey_extensions_init + pfkey_build + pfkey_msg_hdr_build */
static bool
pfkey_msg_start(u_int8_t msg_type, u_int8_t satype, const char *description,
				const char *text_said,
				struct sadb_ext *extensions[SADB_EXT_MAX + 1])
{
	pfkey_extensions_init(extensions);
	return pfkey_build(pfkey_msg_hdr_build(&extensions[0], msg_type, satype, 0,
										   ++pfkey_seq, pid),
					   description, text_said, extensions);
}

/* Finish (building, sending, accepting response for) PF_KEY message.
 * If response isn't NULL, the response from the kernel will be
 * placed there (and its errno field will not be examined).
 * Returns TRUE iff all appears well.
 */
static bool
finish_pfkey_msg(struct sadb_ext *extensions[SADB_EXT_MAX + 1],
				 const char *description, const char *text_said,
				 pfkey_buf *response)
{
	struct sadb_msg *pfkey_msg;
	bool success = TRUE;
	int error;

	error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_IN);

	if (error != 0)
	{
		loglog(RC_LOG_SERIOUS, "pfkey_msg_build of %s %s failed, code %d",
							   description, text_said, error);
		success = FALSE;
	}
	else
	{
		size_t len = pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN;

		DBG(DBG_KERNEL,
			DBG_log("finish_pfkey_msg: %s message %u for %s %s",
					sparse_val_show(pfkey_type_names, pfkey_msg->sadb_msg_type),
					pfkey_msg->sadb_msg_seq, description, text_said);
			DBG_dump(NULL, (void *) pfkey_msg, len));

		ssize_t r = write(pfkeyfd, pfkey_msg, len);

		if (r != (ssize_t)len)
		{
			if (r < 0)
			{
				log_errno((e, "pfkey write() of %s message %u for %s %s"
						  " failed", sparse_val_show(pfkey_type_names,
							pfkey_msg->sadb_msg_type), pfkey_msg->sadb_msg_seq,
						  description, text_said));
			}
			else
			{
				loglog(RC_LOG_SERIOUS, "ERROR: pfkey write() of %s message"
					   " %u for %s %s truncated: %ld instead of %ld",
					   sparse_val_show(pfkey_type_names,
							pfkey_msg->sadb_msg_type), pfkey_msg->sadb_msg_seq,
						description, text_said, (long)r, (long)len);
			}
			success = FALSE;

			/* if we were compiled with debugging, but we haven't already
			 * dumped the command, do so.
			 */
#ifdef DEBUG
			if ((cur_debugging & DBG_KERNEL) == 0)
				DBG_dump(NULL, (void *) pfkey_msg, len);
#endif
		}
		else
		{
			/* Check response from kernel.
			 * It ought to be an echo, perhaps with additional info.
			 * If the caller wants it, response will point to space.
			 */
			pfkey_buf b;
			pfkey_buf *bp = response != NULL? response : &b;

			if (!pfkey_get_response(bp,
						((struct sadb_msg *)extensions[0])->sadb_msg_seq))
			{
				loglog(RC_LOG_SERIOUS, "ERROR: no response to our PF_KEY %s"
					   " message for %s %s", sparse_val_show(pfkey_type_names,
							pfkey_msg->sadb_msg_type), description, text_said);
				success = FALSE;
			}
			else if (pfkey_msg->sadb_msg_type != bp->msg.sadb_msg_type)
			{
				loglog(RC_LOG_SERIOUS, "ERROR: response to our PF_KEY %s"
					   " message for %s %s was of wrong type (%s)",
					   sparse_name(pfkey_type_names, pfkey_msg->sadb_msg_type),
					   description, text_said, sparse_val_show(pfkey_type_names,
							bp->msg.sadb_msg_type));
				success = FALSE;
			}
			else if (response == NULL && bp->msg.sadb_msg_errno != 0)
			{
				/* Kernel is signalling a problem */
				loglog(RC_LOG_SERIOUS, "ERROR: PF_KEY %s response for %s %s"
					   " included errno %u: %s",
					   sparse_val_show(pfkey_type_names,
							pfkey_msg->sadb_msg_type), description, text_said,
					   (unsigned) bp->msg.sadb_msg_errno,
					   strerror(bp->msg.sadb_msg_errno));
				success = FALSE;
			}
		}
	}
	pfkey_extensions_free(extensions);
	pfkey_msg_free(&pfkey_msg);
	return success;
}

/* Process a SADB_REGISTER message from the kernel.
 * This will be a response to one of ours, but it may be asynchronous
 * (if kernel modules are loaded and unloaded).
 * Some sanity checking has already been performed.
 */
static void
pfkey_register_response(const struct sadb_msg *msg)
{
	/* Find out what the kernel can support.
	 */
	switch (msg->sadb_msg_satype)
	{
	case SADB_SATYPE_ESP:
#ifndef NO_KERNEL_ALG
		kernel_alg_register_pfkey(msg, sizeof (pfkey_buf));
#endif
		break;
	case SADB_X_SATYPE_IPCOMP:
		/* ??? There ought to be an extension to list the
		 * supported algorithms, but RFC 2367 doesn't
		 * list one for IPcomp.
		 */
		can_do_IPcomp = TRUE;
		break;
	default:
		break;
	}
}

/**  register SA types that can be negotiated */
static void
pfkey_register_proto(unsigned satype, const char *satypename)
{
	struct sadb_ext *extensions[SADB_EXT_MAX + 1];
	pfkey_buf pfb;

	if (!(pfkey_msg_start(SADB_REGISTER, satype, satypename, NULL, extensions)
		  && finish_pfkey_msg(extensions, satypename, "", &pfb)))
	{
		/* ??? should this be loglog */
		plog("no kernel support for %s", satypename);
	}
	else
	{
		pfkey_register_response(&pfb.msg);
		DBG(DBG_KERNEL,
			DBG_log("%s registered with kernel.", satypename));
	}
}

void
pfkey_register(void)
{
	pid = getpid();

	pfkeyfd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
	if (pfkeyfd == -1)
	{
		exit_log_errno((e, "socket() in init_pfkeyfd()"));
	}

	pfkey_register_proto(SADB_SATYPE_AH, "AH");
	pfkey_register_proto(SADB_SATYPE_ESP, "ESP");
	pfkey_register_proto(SADB_X_SATYPE_IPCOMP, "IPCOMP");

	close(pfkeyfd);
}