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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/* Libreswan Virtual IP Management
* Copyright (C) 2002 Mathieu Lafon - Arkoon Network Security
* Copyright (C) 2004 Xelerance Corporation
* Copyright (C) 2010 Tuomo Soini <tis@foobar.fi>
* Copyright (C) 2011 Wolfgang Nothdurft <wolfgang@linogate.de>
* Copyright (C) 2012 Bram <bram-bcrafjna-erqzvar@spam.wizbit.be>
* Copyright (C) 2013 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2013 Paul Wouters <pwouters@redhat.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. 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 "defs.h"
#include "log.h"
#include "connections.h"
#include "virtual_ip.h"
#include "nat_traversal.h" /* for nat_traversal_enabled */
#include "refcnt.h"
#include "ip_info.h"
#define F_VIRTUAL_NO 1 /* %no (subnet must be host/32) */
#define F_VIRTUAL_PRIVATE 2 /* %priv (list held in private_net_{incl,excl} */
#define F_VIRTUAL_ALL 4 /* %all [only for testing] */
#define F_VIRTUAL_HOST 8 /* vhost (vnet has no representation) */
struct virtual_ip {
refcnt_t refcnt;
unsigned short flags; /* union of F_VIRTUAL_* */
unsigned short n_net;
ip_subnet net[0];
};
/*
* subnets to include and to exclude as virtual-private
*
* From from ipsec.conf's config setup's virtual-private= )
*/
static ip_subnet *private_net_incl = NULL; /* [private_net_incl_len] */
static int private_net_incl_len = 0;
static ip_subnet *private_net_excl = NULL; /* [private_net_excl_len] */
static int private_net_excl_len = 0;
/*
* Read a subnet (IPv4/IPv6)
* inclusion form: [%v4:]x.x.x.x/y or [%v6]:xxxxxxxxx/yy
* exclusion form: [%v4:]!x.x.x.x/y or [%v6:]!xxxxxxxxx/yy
*
* @param src String in format (see above)
* @param len Length of src string
* @param dst out: IP Subnet * Destination
* @param dstexcl out: IP Subnet * for ! form (required if ! is to be accepted)
* @param isincl out: bool * inclusive form or not
* @return bool If the format string is valid.
*/
static bool read_subnet(const char *src, size_t len,
ip_subnet *dst,
ip_subnet *dstexcl,
bool *isincl,
struct logger *logger)
{
const char *p = src; /* cursor */
/*
* Note: len might not be sufficient for each of these strncmp calls
* but that's OK because the character in src[len] is either ',' or '\0'
* so the result will be a non-match, safely and correctly.
*/
const struct ip_info *afi;
if (eat(p, "%v4:")) {
afi = &ipv4_info;
} else if (eat(p, "%v6:")) {
afi = &ipv6_info;
} else {
afi = NULL; /* "guess from src" */
}
bool incl = TRUE;
if (dstexcl != NULL)
*isincl = incl = !eat(p, "!");
err_t ugh = ttosubnet(shunk2(p, len - (p - src)), afi, 'x',
incl ? dst : dstexcl, logger);
if (ugh != NULL) {
llog(RC_LOG_SERIOUS, logger,
"virtual-private entry is not a proper subnet: %s", ugh);
return FALSE;
}
return TRUE;
}
void free_virtual_ip(void)
{
/* These might be NULL if empty in ipsec.conf */
private_net_incl_len = 0;
pfreeany(private_net_incl);
private_net_excl_len = 0;
pfreeany(private_net_excl);
}
/*
* Initialize Virtual IP Support
*
* @param private_list String (contents of virtual-private= from ipsec.conf)
*/
void init_virtual_ip(const char *private_list,
struct logger *logger)
{
free_virtual_ip();
/** Count **/
int bad = 0; /* count of errors */
for (const char *str = private_list; str != NULL; ) {
const char *next = strchr(str, ',');
if (next == NULL)
next = str + strlen(str);
bool incl = FALSE;
ip_subnet sub; /* sink: value never used */
if (read_subnet(str, next - str, &sub, &sub, &incl, logger)) {
if (incl)
private_net_incl_len++;
else
private_net_excl_len++;
} else {
bad++;
}
str = *next != '\0' ? next + 1 : NULL;
}
if (bad == 0) {
/** Allocate **/
if (private_net_incl_len != 0) {
private_net_incl = (ip_subnet *)alloc_bytes(
(private_net_incl_len * sizeof(ip_subnet)),
"private_net_incl subnets");
}
if (private_net_excl_len != 0) {
private_net_excl = (ip_subnet *)alloc_bytes(
(private_net_excl_len * sizeof(ip_subnet)),
"private_net_excl subnets");
}
/** Fill **/
int i_incl = 0;
int i_excl = 0;
for (const char *str = private_list; str != NULL; ) {
const char *next = strchr(str, ',');
if (next == NULL)
next = str + strlen(str);
bool incl = FALSE;
if (read_subnet(str, next - str,
&(private_net_incl[i_incl]),
&(private_net_excl[i_excl]),
&incl, logger)) {
if (incl)
i_incl++;
else
i_excl++;
}
str = *next != '\0' ? next + 1 : NULL;
}
} else {
llog(RC_LOG_SERIOUS, logger,
"%d bad entries in virtual-private - none loaded", bad);
pfreeany(private_net_incl);
private_net_incl = NULL;
}
}
/*
* virtual string must be:
* {vhost,vnet}:[method [, method]* ]
*
* vhost = accept only a host (/32)
* vnet = accept any network
*
* method:
* %no (means no virtual IP (accept IP of host/32))
* %priv (means accept system-wide private net list)
* [%v4:]x (means accept literal IPv4 subnet x)
* [%v6:]x (means accept literal IPv6 subnet x)
* %all (means accept all IPs [only for testing])
*
* examples:
* vhost:%priv,%no
* vnet:%priv,%v4:192.168.1.0/24
*
* @param c Connection Struct
* @param string (virtual_private= from ipsec.conf)
* @return virtual_ip
*/
struct virtual_ip *create_virtual(const char *string, struct logger *logger)
{
if (string == NULL || string[0] == '\0')
return NULL;
unsigned short flags = 0;
const char *str = string;
if (eat(str, "vhost:")) {
flags |= F_VIRTUAL_HOST;
} else if (eat(str, "vnet:")) {
/* represented in flags by the absence of F_VIRTUAL_HOST */
} else {
llog(RC_LOG, logger,
"virtual string \"%s\" is missing \"vhost:\" or \"vnet:\" - virtual selection is disabled for connection",
string);
return NULL;
}
/*
* Parse string: fill flags & count subnets
*/
unsigned short n_net = 0;
const char *first_net = NULL;
while (*str != '\0') {
const char *next = strchr(str, ',');
if (next == NULL)
next = str + strlen(str);
ptrdiff_t len = next - str;
ip_subnet sub; /* sink -- value never used */
if (eat(str, "%no")) {
flags |= F_VIRTUAL_NO;
} else if (eat(str, "%priv")) {
flags |= F_VIRTUAL_PRIVATE;
} else if (eat(str, "%all")) {
flags |= F_VIRTUAL_ALL;
} else if (read_subnet(str, len, &sub, NULL,
NULL, logger)) {
n_net++;
if (first_net == NULL)
first_net = str;
str += len;
} else {
/* nothing matched: force failure */
str = NULL;
}
if (str != next) {
llog(RC_LOG, logger,
"invalid virtual string \"%s\" - virtual selection is disabled for connection",
string);
return NULL;
}
/* clang 3.5 thinks that next might be NULL; wrong */
if (*next == '\0')
break;
str = next + 1;
}
/* allocate struct + array */
struct virtual_ip *v = (struct virtual_ip *)alloc_bytes(
sizeof(struct virtual_ip) + (n_net * sizeof(ip_subnet)),
"virtual description");
refcnt_init("vip", v, &v->refcnt, HERE);
v->flags = flags;
v->n_net = n_net;
if (n_net != 0 && first_net != NULL) {
/*
* Save subnets in newly allocated struct
*/
int i = 0;
for (str = first_net; str != NULL && *str != '\0'; ) {
const char *next = strchr(str, ',');
if (next == NULL)
next = str + strlen(str);
if (read_subnet(str, next - str, &(v->net[i]), NULL,
NULL, logger))
i++;
str = *next == '\0' ? NULL : next + 1;
}
}
return v;
}
/*
* is_virtual_end - Do we have a virtual IP on the other end?
*
* @param that end structure
* @return bool True if we do
*/
bool is_virtual_end(const struct end *that)
{
return that->virt != NULL;
}
/*
* Does this connection have a virtual IP ?
*
* @param c Active Connection struct
* @return bool True if we do
*/
bool is_virtual_connection(const struct connection *c)
{
const struct spd_route *sr;
for (sr = &c->spd; sr != NULL; sr = sr->spd_next)
if (sr->that.virt != NULL)
return TRUE;
return FALSE;
}
/*
* Does this spd have a virtual IP ?
*
* @param c Active Connection struct
* @return bool True if we do
*/
bool is_virtual_sr(const struct spd_route *sr)
{
return is_virtual_end(&sr->that);
}
/*
* is_virtual_vhost - is the virt set to a host or a net?
*
* @param that end structure
* @return bool True if we do
*/
bool is_virtual_vhost(const struct end *that)
{
return that->virt != NULL && (that->virt->flags & F_VIRTUAL_HOST) != 0;
}
/*
* net_in_list - Check if a subnet is in a list
*
* @param peer_net IP Subnet to check
* @param list IP Subnet list to search within
* @param len # of subnets in list
* @return bool True if peer_net is in list
*/
static bool net_in_list(const ip_subnet *peer_net, const ip_subnet *list,
int len)
{
for (int i = 0; i < len; i++)
if (subnetinsubnet(peer_net, &list[i]))
return TRUE;
return FALSE;
}
/*
* check_virtual_net_allowed -
* Check if the virtual network the client proposes is acceptable to us
*
* @param c Connection structure (active)
* @param peer_net IP Subnet the peer proposes
* @param peers_addr Peers IP Address
* @return err_t NULL if allowed, diagnostic otherwise
*/
err_t check_virtual_net_allowed(const struct connection *c,
const ip_subnet *peer_net,
const ip_address *peers_addr)
{
const struct virtual_ip *virt = c->spd.that.virt;
if (virt == NULL)
return NULL;
if (virt->flags & F_VIRTUAL_HOST && !subnetishost(peer_net)) {
return "only virtual host single IPs are allowed";
}
if (private_net_incl == NULL)
return NULL;
if (virt->flags & F_VIRTUAL_NO) {
if (subnetishost(peer_net) && addrinsubnet(peers_addr, peer_net))
{
return NULL;
}
/* ??? why isn't this case an error? */
}
/* last failure; ignored on subsequent success; ??? default is success */
err_t why = NULL;
if (virt->flags & F_VIRTUAL_PRIVATE) {
if (!net_in_list(peer_net, private_net_incl,
private_net_incl_len)) {
why = "a private network virtual IP was required, but the proposed IP did not match our list (virtual-private=) since it is in use elsewhere";
} else if (net_in_list(peer_net, private_net_excl,
private_net_excl_len)) {
why = "a private network virtual IP was required, but our list (virtual-private=) excludes their IP (e.g. %v4!...) since it is in use elsewhere";
} else {
return NULL; /* success */
}
}
if (virt->n_net != 0) {
if (net_in_list(peer_net, virt->net, virt->n_net))
return NULL; /* success */
why = "a specific network IP was required, but the proposed IP did not match our list (subnet=vhost:list)";
}
if (virt->flags & F_VIRTUAL_ALL) {
/* %all must only be used for testing - log it */
llog(RC_LOG_SERIOUS, c->logger,
"WARNING: v%s:%%all must only be used for testing",
(virt->flags & F_VIRTUAL_HOST) ? "host" : "net");
return NULL; /* success */
}
/* ??? if why is NULL, this seems to be success-by-default. Is that intended? */
return why;
}
static void show_virtual_private_kind(struct show *s,
const char *kind,
const ip_subnet *private_net,
int private_net_len)
{
if (private_net != NULL) {
char all[256] = ""; /* arbitrary limit */
struct jambuf buf = ARRAY_AS_JAMBUF(all);
int i;
for (i = 0; i < private_net_len; i++) {
jampos_t start = jambuf_get_pos(&buf);
if (i > 0) {
jam(&buf, ", ");
}
jam_subnet(&buf, &private_net[i]);
if (!jambuf_ok(&buf)) {
/* oops overflowed, discard last */
jambuf_set_pos(&buf, &start);
break;
}
}
show_comment(s, "- %s subnet%s: %s",
kind, i == 1? "" : "s", all);
if (i < private_net_len) {
show_comment(s, "showing only %d of %d!",
i, private_net_len);
}
}
}
void show_virtual_private(struct show *s)
{
if (nat_traversal_enabled) {
show_comment(s, "virtual-private (%%priv):");
show_virtual_private_kind(s, "allowed",
private_net_incl,
private_net_incl_len);
show_virtual_private_kind(s, "excluded",
private_net_excl,
private_net_excl_len);
}
}
static void virtual_ip_free(struct virtual_ip **vip, where_t where UNUSED)
{
pfree(*vip);
*vip = NULL;
}
struct virtual_ip *virtual_ip_addref(struct virtual_ip *vip, where_t where)
{
return refcnt_addref(vip, where);
}
void virtual_ip_delref(struct virtual_ip **vip, where_t where)
{
refcnt_delref(vip, virtual_ip_free, where);
}
|