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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/* information about connections between hosts and clients
*
* Copyright (C) 1998-2002,2013,2015 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2003-2007 Michael Richardson <mcr@xelerance.com>
* Copyright (C) 2009 Paul Wouters <paul@xelerance.com>
* Copyright (C) 2012 Paul Wouters <paul@libreswan.org>
* Copyright (C) 2012-2018 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2011 Anthony Tong <atong@TrustedCS.com>
* Copyright (C) 2017-2018 Antony Antony <antony@phenome.org>
* Copyright (C) 2019 Andrew Cagney <cagney@gnu.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. See <https://www.gnu.org/licenses/gpl2.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 <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
#include <errno.h>
#include "sysdep.h"
#include "constants.h"
#include "defs.h"
#include "id.h"
#include "x509.h"
#include "certs.h"
#include "connections.h" /* needs id.h */
#include "pending.h"
#include "log.h"
#include "state.h"
#include "demux.h"
#include "ikev1_quick.h"
#include "timer.h"
#include "ikev2.h"
#include "ip_address.h"
#include "ikev2_create_child_sa.h" /* for initiate_v2_CREATE_CHILD_SA_create_child() */
#include "initiate.h"
#include "show.h"
#include "initiated_by.h"
/*
* queue an IPsec SA negotiation pending completion of a
* suitable phase 1 (IKE SA)
*/
void append_pending(struct ike_sa *ike,
struct connection *c,
const struct child_policy *policy,
so_serial_t replacing,
const shunk_t sec_label,
bool part_of_initiating_ike_sa,
bool detach_whack)
{
if (c->pending != NULL) {
address_buf b;
connection_buf cib;
bool duplicate = (c->pending->ike == ike);
ldbg_sa(ike, "connection "PRI_CONNECTION" is already pending: waiting on IKE SA #%lu connecting to %s; %s",
pri_connection(c, &cib),
c->pending->ike->sa.st_serialno,
str_address(&c->remote->host.addr, &b),
duplicate ? "ignoring duplicate" : "this IKE SA is different");
if (duplicate) {
return;
}
}
struct pending *p = alloc_thing(struct pending, "struct pending");
c->pending = p;
/*
* Clone C's logger but strip it of any whack attached by the
* caller (initiate_connection() say) that will be detached
* after this code returns.
*/
p->logger = clone_logger(c->logger, HERE);
if (detach_whack) {
release_whack(p->logger, HERE);
}
p->ike = ike;
p->connection = connection_addref(c, p->logger); /* no pending logger */
p->policy = (*policy);
p->replacing = replacing;
p->pend_time = mononow();
p->sec_label = sec_label;
/*
* If this is part of an initiate then there's already enough
* going on; no need to log this action.
*/
enum stream only = (!part_of_initiating_ike_sa ? ALL_STREAMS :
DBGP(DBG_BASE) ? DEBUG_STREAM :
NO_STREAM);
if (only != NO_STREAM) {
address_buf b;
state_buf sab;
llog(only, p->logger,
"queue %s; waiting on %s "PRI_STATE" negotiating with %s",
/* "Child SA" or "IPsec SA" */
c->config->ike_info->child_sa_name,
/* "IKE SA" or "ISAKMP SA" */
c->config->ike_info->parent_sa_name,
pri_state(&ike->sa, &sab),
str_address(&c->remote->host.addr, &b));
}
/*
* Hopefully the list is short.
*
* Append SAs as they arrive so that things are processed
* first-in first-out.
*
* Since, for the IKE SA, the first child is immediately
* added, the IKE SA's connection's Child SA is always first.
*/
struct pending **end = &ike->sa.st_pending;
while ((*end) != NULL) {
end = &(*end)->next;
}
*end = p;
ldbg_sa(ike, "pending: %s() ike %p pending %p connection %p ike %p",
__func__, ike, p, p->connection, p->ike);
}
/*
* Release all the whacks awaiting the completion of this state. This
* is accomplished by closing all the whack socket file descriptors.
* We go to some trouble to tell each whack, but to not tell it twice.
*/
void release_pending_whacks(struct state *st, err_t story)
{
if (!whack_attached(st->logger)) {
pdbg(st->logger, "%s: state has no whack fd", __func__);
return;
}
/*
* Check for the SA's parent and if that needs to disconnect.
*
* For instance, when the IKE_SA establishes but the first
* CHILD_SA fails with a timeout then this code will be called
* with the CHILD_SA.
*
* XXX: Since this is meant to release pending whacks, should
* this check for, and release the whacks for any pending
* CHILD_SA attached to this ST's IKE SA?
*/
struct ike_sa *ike_with_same_whack = NULL;
if (IS_CHILD_SA(st)) {
struct ike_sa *ike = ike_sa(st, HERE);
if (ike == NULL || !same_whack(st->logger, ike->sa.logger)) {
release_whack(st->logger, HERE);
return;
}
ike_with_same_whack = ike;
release_whack(ike->sa.logger, HERE);
} else {
ike_with_same_whack = pexpect_ike_sa(st);
}
pexpect(ike_with_same_whack != NULL);
/*
* Now go through pending children and close the whack socket
* of any that are going to be assigned this ST as the parent.
*
* XXX: Is this because the parent is dying so anything
* waiting on it should be detached.
*
* SAME_FD() is used to identify whack sockets that are
* different to ST - when found a further release message is
* printed.
*/
for (struct pending *p = st->st_pending; p != NULL; p = p->next) {
ldbg(st->logger, "pending: %s() ike %p pending %p connection %p ike %p",
__func__, st, p, p->connection, p->ike);
bool has_whack = whack_attached(p->logger);
pdbg(p->logger,
"pending: %s: %s SA "PRI_SO" "PRI_LOGGER" has %s SA with whack "PRI_LOGGER,
__func__,
p->ike->sa.st_connection->config->ike_info->parent_name,
pri_so(p->ike->sa.st_serialno),
pri_logger(p->ike->sa.logger),
p->ike->sa.st_connection->config->ike_info->parent_name,
pri_logger(p->logger));
if (p->ike != ike_with_same_whack) {
/* none of our business */
continue;
}
if (!has_whack) {
/* nothing to do */
continue;
}
if (!same_whack(st->logger, p->logger)) {
/* XXX: why not the log file? */
llog(WHACK_STREAM, p->logger,
"%s for IKE SA, but releasing whack for pending %s",
story,
/* "IPsec SA" or "CHILD SA" */
p->connection->config->ike_info->child_sa_name);
}
release_whack(p->logger, HERE);
}
/* last gasp */
release_whack(st->logger, HERE);
}
/*
* Remove a pending from a linked list.
*
* pp points to the link to the entry.
* *pp will be updated to point to the successor to the original *pp.
* In effect, we advance *pp.
*
* Note: If you are traversing a linked list and deleting some
* entries, you should not advance pp after calling delete_pending.
*/
static void delete_pending(struct pending **pp, const char *what)
{
/* remove from list */
struct pending *p = *pp;
*pp = p->next;
struct connection *c = p->connection;
c->pending = NULL;
connection_buf cib;
ldbg(c->logger,
"pending: %s pending [%p] %s connection "PRI_CONNECTION" [%p]",
what, p, (c->config->ike_version == IKEv2 ? "Child SA" : "Quick Mode"),
pri_connection(c, &cib), c);
connection_delref(&p->connection, &global_logger);
free_logger(&p->logger, HERE);
pfree(p);
}
/*
* Look for phase2s that were waiting for a phase 1.
*
* XXX instead of doing this work NOW, we should simply create an event
* in zero future time to unpend the state.
* YYY but, in fact, quick_mode will enqueue a cryptographic operation
* anyway, which will get done "later" anyway, so make it is just fine
* as it is.
* In IKEv2 it called when AUTH is complete, child is established.
* Established child get removed not unpend.
*/
void unpend(struct ike_sa *ike, struct connection *cc)
{
if (cc == NULL) {
ldbg_sa(ike, "pending: unpending state %p #%lu pending %p",
ike, ike->sa.st_serialno, ike->sa.st_pending);
} else {
connection_buf cib;
ldbg_sa(ike, "pending: unpending state #%lu connection "PRI_CONNECTION"",
ike->sa.st_serialno, pri_connection(cc, &cib));
}
struct pending **pp = &ike->sa.st_pending;
while ((*pp) != NULL) {
struct pending *p = (*pp);
ldbg_sa(ike, "pending: %s() ike %p pending %p connection %p ike %p",
__func__, ike, p, p->connection, p->ike);
p->pend_time = mononow();
char *what ="unqueuing";
switch (ike->sa.st_ike_version) {
case IKEv2:
if (cc == p->connection) {
/*
* IKEv2 AUTH negotiation
* include child. nothing to
* upend, like in IKEv1,
* delete it
*/
what = "delete from";
} else if (!already_has_larval_v2_child(ike, p->connection)) {
connection_attach(p->connection, p->logger);
struct child_sa *child =
submit_v2_CREATE_CHILD_SA_new_child(ike, p->connection,
&p->policy,
/*detach_whack*/false);
connection_initiated_child(ike, child,
INITIATED_BY_PENDING,
HERE);
connection_detach(p->connection, p->logger);
}
break;
case IKEv1:
#ifdef USE_IKEv1
{
connection_attach(p->connection, p->logger);
struct child_sa *child =
quick_outI1(ike, p->connection,
&p->policy,
p->replacing);
connection_initiated_child(ike, child,
INITIATED_BY_PENDING,
HERE);
connection_detach(p->connection, p->logger);
break;
}
#endif
default:
bad_case(ike->sa.st_ike_version);
}
delete_pending(pp, what); /* in effect, advances pp */
}
}
struct connection *first_pending(const struct ike_sa *ike)
{
struct pending *p = ike->sa.st_pending;
if (p != NULL) {
connection_attach(p->connection, p->logger);
ldbg_sa(ike, "pending: %s() ike %p pending %p connection %p ike %p",
__func__, ike, p, p->connection, p->ike);
return p->connection;
}
ldbg_sa(ike, "pending: no first pending from state");
return NULL;
}
/* a IKE SA negotiation has been replaced; update any pending */
void move_pending(struct ike_sa *old, struct ike_sa *new)
{
if (pbad(old == NULL)) {
return;
}
if (pbad(old == new)) {
return;
}
ldbg(old->sa.logger, "pending: update ike %p pending %p -> ike %p pending %p",
old, old->sa.st_pending,
new, new->sa.st_pending);
struct pending **pp = &new->sa.st_pending;
while ((*pp) != NULL) {
pp = &(*pp)->next;
}
(*pp) = old->sa.st_pending;
old->sa.st_pending = NULL;
}
/*
* An IKE SA negotiation has failed; discard any pending.
*
* Danger: this code deletes connections.
*/
void flush_pending_by_state(struct ike_sa *ike)
{
ldbg(ike->sa.logger, "pending: %s() ike %p pending %p",
__func__, ike, ike->sa.st_pending);
struct pending **pp = &ike->sa.st_pending;
while ((*pp) != NULL) {
struct pending *p = (*pp);
ldbg_sa(ike, "pending: %s() ike %p pending %p connection %p ike %p",
__func__, ike, p, p->connection, p->ike);
/*
* We don't have to worry about deref to
* free'ed *pp, because delete_pending updates
* pp to point to the next element before it
* frees *pp
*
* We don't need to worry about delrefing
* .connection because delete_pending() will
* do it for us.
*/
if ((*pp)->connection != ike->sa.st_connection) {
/*
* Find another IKE SA willing to care
* for the CUCKOO, or initiate our
* own.
*/
connection_reschedule((*pp)->connection, (*pp)->logger, HERE);
}
delete_pending(pp, "flush"); /* in effect, advances pp */
}
}
/*
* A connection is been taken down or deleted; look for and remove the
* connection from the host-pair pending list.
*
* The host-pair pending list contains all connections waiting for an
* IKE SA to establish between the two ends.
*
* Note: this code DOES NOT delete the connection (ya!).
*/
void remove_connection_from_pending(const struct connection *c)
{
if (c->pending == NULL) {
return;
}
struct ike_sa *ike = c->pending->ike;
ldbg_sa(ike, "pending: %s() ike %p pending %p connection %p connection %p",
__func__, ike, c->pending, c->pending->connection, c);
struct pending **pp = &c->pending->ike->sa.st_pending;
while ((*pp) != NULL && (*pp) != c->pending) {
pp = &(*pp)->next;
}
/* should have been found */
if (PBAD(c->logger, (*pp) == NULL)) {
return;
}
delete_pending(pp, "flush");
}
bool connection_is_pending(const struct connection *c)
{
return (c->pending != NULL);
}
|