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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/*
* maintain.c - Teredo client qualification & maintenance
*
* See "Teredo: Tunneling IPv6 over UDP through NATs"
* for more information
*/
/***********************************************************************
* Copyright © 2004-2007 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; 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, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gettext.h>
#include <string.h> /* memcmp() */
#include <assert.h>
#include <stdbool.h>
#include <inttypes.h>
#include <time.h> /* clock_gettime() */
#include <sys/types.h>
#include <unistd.h> /* sysconf() */
#include <sys/socket.h> /* AF_INET */
#include <netinet/in.h> /* struct in6_addr */
#include <netinet/ip6.h> /* struct ip6_hdr */
#include <netdb.h> /* getaddrinfo(), gai_strerror() */
#include <syslog.h>
#include <stdlib.h> /* malloc(), free() */
#include <errno.h> /* EINTR */
#include <pthread.h>
#include <arpa/nameser.h>
#include <resolv.h> /* res_init() */
#include "teredo.h"
#include "teredo-udp.h"
#include "packets.h"
#include "security.h"
#include "maintain.h"
#include "v4global.h" // is_ipv4_global_unicast()
#include "debug.h"
static inline void gettime (struct timespec *now)
{
#if (_POSIX_CLOCK_SELECTION - 0 >= 0) && (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
if (clock_gettime (CLOCK_MONOTONIC, now) == 0)
return;
#else
# define pthread_condattr_setclock( a, c ) (((c) != CLOCK_REALTIME) ? EINVAL : 0)
# ifndef CLOCK_MONOTONIC
# define CLOCK_MONOTONIC CLOCK_REALTIME
# endif
# warning Monotonic clock is needed for proper Teredo maintenance!
#endif
clock_gettime (CLOCK_REALTIME, now);
}
struct teredo_maintenance
{
pthread_t thread;
pthread_mutex_t outer;
pthread_mutex_t inner;
pthread_cond_t received;
pthread_cond_t processed;
const teredo_packet *incoming;
int fd;
struct
{
teredo_state state;
teredo_state_cb cb;
void *opaque;
} state;
char *server;
unsigned qualification_delay;
unsigned qualification_retries;
unsigned refresh_delay;
unsigned restart_delay;
};
/**
* Resolves an IPv4 address (thread-safe).
*
* @return 0 on success, or an error value as defined for getaddrinfo().
*/
static int getipv4byname (const char *restrict name, uint32_t *restrict ipv4)
{
struct addrinfo hints =
{
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM
}, *res;
int val = getaddrinfo (name, NULL, &hints, &res);
if (val)
return val;
*ipv4 = ((const struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
freeaddrinfo (res);
return 0;
}
/**
* Checks and parses a received Router Advertisement.
*
* @return 0 if successful.
*/
static int
maintenance_recv (const teredo_packet *restrict packet, uint32_t server_ip,
const uint8_t *restrict nonce, bool cone,
teredo_state *restrict state)
{
assert (packet->auth_present);
if (memcmp (packet->auth_nonce, nonce, 8))
return EPERM;
/* TODO: fail instead of ignoring the packet? */
if (packet->auth_fail)
{
syslog (LOG_ERR, _("Authentication with server failed."));
return EACCES;
}
if (teredo_parse_ra (packet, &state->addr, cone, &state->mtu)
/* TODO: try to work-around incorrect server IP */
|| (state->addr.teredo.server_ip != server_ip))
return EINVAL;
/* Valid router advertisement received! */
state->ipv4 = packet->dest_ipv4;
return 0;
}
/**
* Waits until the clock reaches deadline or a RS packet is received.
* @return 0 if a packet was received, ETIMEDOUT if deadline was reached.
*/
static int wait_reply (teredo_maintenance *restrict m,
const struct timespec *restrict deadline)
{
while (m->incoming == NULL)
{
switch (pthread_cond_timedwait (&m->received, &m->inner, deadline))
{
case 0:
break;
case ETIMEDOUT:
return ETIMEDOUT;
}
}
return 0; // dead code
}
/**
* Waits until the clock reaches deadline and ignore any RS packet received
* in the mean time.
*/
static void wait_reply_ignore (teredo_maintenance *restrict m,
const struct timespec *restrict deadline)
{
while (wait_reply (m, deadline) == 0)
{
m->incoming = NULL;
pthread_cond_signal (&m->processed);
}
}
/**
* Make sure ts is in the future. If not, set it to the current time.
* @return false if (*ts) was changed, true otherwise.
*/
static bool
checkTimeDrift (struct timespec *ts)
{
struct timespec now;
gettime (&now);
if ((now.tv_sec > ts->tv_sec)
|| ((now.tv_sec == ts->tv_sec) && (now.tv_nsec > ts->tv_nsec)))
{
/* process stopped, CPU starved, or (ACPI, APM, etc) suspend */
syslog (LOG_WARNING, _("Too much time drift. Resynchronizing."));
*ts = now;
return false;
}
return true;
}
static void
cleanup_unlock (void *o)
{
(void)pthread_mutex_unlock ((pthread_mutex_t *)o);
}
/*
* Implementation notes:
* - Optional Teredo interval determination procedure was never implemented.
* It adds NAT binding maintenance brittleness in addition to implementation
* complexity, and is not necessary for RFC4380 compliance.
* Also STUN RFC3489bis deprecates this type of behavior.
* - NAT cone type probing was removed in Miredo version 0.9.5. This violated
* RFC4380. Since then, draft-krishnan-v6ops-teredo-update has nevertheless
* confirmed that the cone type should be dropped.
* - NAT symmetric probing was removed in Miredo version 1.1.0, which deepens
* the gap between Miredo and RFC4380. Still, this is fairly consistent with
* RFC3489bis.
*/
/*
* Teredo client maintenance procedure
*/
static inline LIBTEREDO_NORETURN
void maintenance_thread (teredo_maintenance *m)
{
struct timespec deadline = { 0, 0 };
teredo_state *c_state = &m->state.state;
uint32_t server_ip = 0;
unsigned count = 0;
enum
{
TERR_NONE,
TERR_BLACKHOLE
} last_error = TERR_NONE;
pthread_mutex_lock (&m->inner);
/*
* Qualification/maintenance procedure
*/
pthread_cleanup_push (cleanup_unlock, &m->inner);
for (;;)
{
/* Resolve server IPv4 addresses */
while (server_ip == 0)
{
/* FIXME: mutex kept while resolving - very bad */
int val = getipv4byname (m->server, &server_ip);
gettime (&deadline);
if (val)
{
/* DNS resolution failed */
syslog (LOG_ERR,
_("Cannot resolve Teredo server address \"%s\": %s"),
m->server, gai_strerror (val));
}
else
if (!is_ipv4_global_unicast (server_ip))
{
syslog (LOG_ERR,
_("Teredo server has a non global IPv4 address."));
}
else
{
/* DNS resolution succeeded */
/* Tells Teredo client about the new server's IP */
assert (!c_state->up);
c_state->addr.teredo.server_ip = server_ip;
m->state.cb (c_state, m->state.opaque);
break; /* Done! */
}
/* wait some time before next resolution attempt */
deadline.tv_sec += m->restart_delay;
server_ip = 0;
wait_reply_ignore (m, &deadline);
}
/* SEND ROUTER SOLICATION */
do
deadline.tv_sec += m->qualification_delay;
while (!checkTimeDrift (&deadline));
uint8_t nonce[8];
teredo_get_nonce (deadline.tv_sec, server_ip, htons (IPPORT_TEREDO),
nonce);
teredo_send_rs (m->fd, server_ip, nonce, false);
int val = 0;
teredo_state newst;
newst.mtu = 1280;
newst.up = true;
/* RECEIVE ROUTER ADVERTISEMENT */
do
{
val = wait_reply (m, &deadline);
if (val)
continue; // time out
/* check received packet */
val = maintenance_recv (m->incoming, server_ip,
nonce, false, &newst);
m->incoming = NULL;
pthread_cond_signal (&m->processed);
}
while ((val != 0) && (val != ETIMEDOUT));
unsigned delay = 0;
/* UPDATE FINITE STATE MACHINE */
if (val /* == ETIMEDOUT */)
{
/* no response */
count++;
if (count >= m->qualification_retries)
{
count = 0;
/* No response from server */
if (last_error != TERR_BLACKHOLE)
{
syslog (LOG_INFO, _("No reply from Teredo server"));
last_error = TERR_BLACKHOLE;
}
if (c_state->up)
{
syslog (LOG_NOTICE, _("Lost Teredo connectivity"));
c_state->up = false;
m->state.cb (c_state, m->state.opaque);
server_ip = 0;
}
/* Wait some time before retrying */
delay = m->restart_delay;
}
}
else
/* RA received and parsed succesfully */
{
count = 0;
/* 12-bits Teredo flags randomization */
newst.addr.teredo.flags = c_state->addr.teredo.flags;
if (!IN6_ARE_ADDR_EQUAL (&c_state->addr.ip6, &newst.addr.ip6))
{
uint16_t f = teredo_get_flbits (deadline.tv_sec);
newst.addr.teredo.flags =
f & htons (TEREDO_RANDOM_MASK);
}
if ((!c_state->up)
|| !IN6_ARE_ADDR_EQUAL (&c_state->addr.ip6, &newst.addr.ip6)
|| (c_state->mtu != newst.mtu))
{
memcpy(c_state, &newst, sizeof (*c_state));
syslog (LOG_NOTICE, _("New Teredo address/MTU"));
m->state.cb (c_state, m->state.opaque);
}
/* Success: schedule next NAT binding maintenance */
last_error = TERR_NONE;
delay = m->refresh_delay;
}
/* WAIT UNTIL NEXT SOLICITATION */
/* TODO: watch for new interface events
* (netlink on Linux, PF_ROUTE on BSD) */
if (delay)
{
deadline.tv_sec -= m->qualification_delay;
deadline.tv_sec += delay;
wait_reply_ignore (m, &deadline);
}
}
/* dead code */
pthread_cleanup_pop (1);
}
static LIBTEREDO_NORETURN void *do_maintenance (void *opaque)
{
maintenance_thread ((teredo_maintenance *)opaque);
}
static const unsigned QualificationDelay = 4; // seconds
static const unsigned QualificationRetries = 3;
static const unsigned RefreshDelay = 30; // seconds
static const unsigned RestartDelay = 100; // seconds
teredo_maintenance *
teredo_maintenance_start (int fd, teredo_state_cb cb, void *opaque,
const char *s1, const char *s2,
unsigned q_sec, unsigned q_retries,
unsigned refresh_sec, unsigned restart_sec)
{
teredo_maintenance *m = (teredo_maintenance *)malloc (sizeof (*m));
if (m == NULL)
return NULL;
memset (m, 0, sizeof (*m));
m->fd = fd;
m->state.cb = cb;
m->state.opaque = opaque;
assert (s1 != NULL);
m->server = strdup (s1);
(void)s2;
m->qualification_delay = q_sec ?: QualificationDelay;
m->qualification_retries = q_retries ?: QualificationRetries;
m->refresh_delay = refresh_sec ?: RefreshDelay;
m->restart_delay = restart_sec ?: RestartDelay;
if (m->server == NULL)
{
free (m);
return NULL;
}
else
{
pthread_condattr_t attr;
pthread_condattr_init (&attr);
(void)pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
/* EINVAL: CLOCK_MONOTONIC unknown */
pthread_cond_init (&m->received, &attr);
pthread_condattr_destroy (&attr);
}
pthread_cond_init (&m->processed, NULL);
pthread_mutex_init (&m->outer, NULL);
pthread_mutex_init (&m->inner, NULL);
int err = pthread_create (&m->thread, NULL, do_maintenance, m);
if (err == 0)
return m;
errno = err;
syslog (LOG_ALERT, _("Error (%s): %m"), "pthread_create");
pthread_cond_destroy (&m->processed);
pthread_cond_destroy (&m->received);
pthread_mutex_destroy (&m->outer);
pthread_mutex_destroy (&m->inner);
free (m->server);
free (m);
return NULL;
}
void teredo_maintenance_stop (teredo_maintenance *m)
{
pthread_cancel (m->thread);
pthread_join (m->thread, NULL);
pthread_cond_destroy (&m->processed);
pthread_cond_destroy (&m->received);
pthread_mutex_destroy (&m->inner);
pthread_mutex_destroy (&m->outer);
free (m->server);
free (m);
}
int teredo_maintenance_process (teredo_maintenance *restrict m,
const teredo_packet *restrict packet)
{
assert (m != NULL);
assert (packet != NULL);
/*
* We don't accept router advertisement without nonce.
* It is far too easy to spoof such packets.
*/
if ((packet->source_port != htons (IPPORT_TEREDO))
/* TODO: check for primary or secondary server address */
|| !packet->auth_present
|| !IN6_ARE_ADDR_EQUAL (&packet->ip6->ip6_dst, &teredo_restrict))
return -1;
pthread_mutex_lock (&m->outer);
pthread_mutex_lock (&m->inner);
m->incoming = packet;
pthread_cond_signal (&m->received);
/* Waits for maintenance thread to process packet... */
do
pthread_cond_wait (&m->processed, &m->inner);
while (m->incoming != NULL);
pthread_mutex_unlock (&m->inner);
pthread_mutex_unlock (&m->outer);
return 0;
}
|