test_nat.c 24.1 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE test_nat.c
Pekka Pessi's avatar
Pekka Pessi committed
26
 * @brief Simulated NAT for testing
Pekka Pessi's avatar
Pekka Pessi committed
27
 *
28 29 30 31 32 33
 * NAT thing works so that we set the outgoing proxy URI to point
 * towards its "private" address and give the real address of the proxy
 * as its "public" address. If we use different IP families here, we may
 * even manage to test real connectivity problems as proxy and endpoint
 * can not talk to each other.
 *
Pekka Pessi's avatar
Pekka Pessi committed
34 35 36 37 38 39 40 41 42 43 44 45 46
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 19:54:28 EET 2006
 */

#include "config.h"

struct nat;
struct binding;

#define SU_ROOT_MAGIC_T struct nat
#define SU_WAKEUP_ARG_T struct binding

Pekka Pessi's avatar
Pekka Pessi committed
47 48
#include <sofia-sip/su.h>
#include <sofia-sip/su_errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
49 50 51 52 53 54 55
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_localinfo.h>
#include <sofia-sip/su_log.h>

#include <stdlib.h>
#include <assert.h>
Pekka Pessi's avatar
Pekka Pessi committed
56
#include <string.h>
57

Pekka Pessi's avatar
Pekka Pessi committed
58 59
#define LIST_PROTOS(STORAGE, PREFIX, T)			 \
STORAGE void PREFIX ##_insert(T **list, T *node),	 \
60
        PREFIX ##_remove(T *node)
Pekka Pessi's avatar
Pekka Pessi committed
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

#define LIST_BODIES(STORAGE, PREFIX, T, NEXT, PREV)	  \
STORAGE void PREFIX ##_insert(T **list, T *node)   \
{							 \
  if ((node->NEXT = *list)) {				 \
    node->PREV = node->NEXT->PREV;			 \
    node->NEXT->PREV = &node->NEXT;			 \
  }							 \
  else							 \
    node->PREV = list;					 \
  *list = node;						 \
}							 \
STORAGE void PREFIX ##_remove(T *node)			 \
{							 \
  if (node->PREV)					 \
    if ((*node->PREV = node->NEXT))			 \
      node->NEXT->PREV = node->PREV;			 \
  node->PREV = NULL;					 \
}							 \
extern int LIST_DUMMY_VARIABLE

#include <test_nat.h>

struct nat {
  su_home_t    home[1];
  su_root_t   *parent;
  su_clone_r   clone;
  tagi_t      *tags;

  su_root_t   *root;

  struct binding *bindings;

94 95
  struct nat_filter *in_filters, *out_filters;

96 97
  /* True if we act in symmetric way */
  int symmetric;
Pekka Pessi's avatar
Pekka Pessi committed
98 99
  /* True if we do logging */
  int logging;
100

Pekka Pessi's avatar
Pekka Pessi committed
101 102 103 104 105 106 107 108
  /* Everything sent to in_address will be forwarded to out_address */
  su_sockaddr_t in_address[1], out_address[1];
  socklen_t in_addrlen, out_addrlen;

  int family;			/* Preferred private family */

  /* ...but source address will be "fake" */
  su_localinfo_t *localinfo, *private, *fake;
109

110
  su_socket_t udp_socket, tcp_socket;
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118 119 120 121
  int udp_register, tcp_register;

  char buffer[65536];
};

LIST_PROTOS(static, nat_binding, struct binding);

struct binding
{
  struct binding *next, **prev;
  struct nat *nat;		/* backpointer */
Pekka Pessi's avatar
Pekka Pessi committed
122
  int socktype, protocol;
123
  su_socket_t in_socket, out_socket;
Pekka Pessi's avatar
Pekka Pessi committed
124 125 126 127 128
  int in_register, out_register;
  int in_closed, out_closed;
  char in_name[64], out_name[64];
};

Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132
static struct binding *nat_binding_new(struct nat *nat,
				       char const *protoname,
				       int socktype, int protocol, 
				       int connected,
133
				       su_socket_t in_socket,
Pekka Pessi's avatar
Pekka Pessi committed
134 135
				       su_sockaddr_t *from,
				       socklen_t fromlen);
Pekka Pessi's avatar
Pekka Pessi committed
136 137
static void nat_binding_destroy(struct binding *);

Pekka Pessi's avatar
Pekka Pessi committed
138 139 140 141 142 143 144
static int binding_init(struct binding *b,
			char const *protoname,
			int connected,
			su_localinfo_t *li,
			su_sockaddr_t *from,
			socklen_t fromlen);

Pekka Pessi's avatar
Pekka Pessi committed
145
static void flush_bindings(struct nat *nat);
146
static int invalidate_bindings(void *nat);
Pekka Pessi's avatar
Pekka Pessi committed
147

Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154 155
static int new_udp(struct nat *, su_wait_t *wait, struct binding *dummy);
static int udp_in_to_out(struct nat *, su_wait_t *wait, struct binding *);
static int udp_out_to_in(struct nat *, su_wait_t *wait, struct binding *);

static int new_tcp(struct nat *, su_wait_t *wait, struct binding *dummy);
static int tcp_in_to_out(struct nat *, su_wait_t *wait, struct binding *);
static int tcp_out_to_in(struct nat *, su_wait_t *wait, struct binding *);

Pekka Pessi's avatar
Pekka Pessi committed
156 157
static int invalidate_binding(struct binding *b);

158 159 160 161 162
LIST_PROTOS(static, nat_filter, struct nat_filter);

struct nat_filter
{
  struct nat_filter *next, **prev;
163
  size_t (*condition)(void *message, size_t len);
164 165
};

Pekka Pessi's avatar
Pekka Pessi committed
166
/* nat entry point */
167
static int
Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172
test_nat_init(su_root_t *root, struct nat *nat)
{
  su_localinfo_t *li, hints[1] = {{ 0 }};
  int error;
  unsigned port = 0, port0 = 0;
173
  su_sockaddr_t su[1];
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177
  socklen_t sulen;
  su_wait_t wait[1];

  nat->root = root;
178
  nat->udp_socket = INVALID_SOCKET, nat->tcp_socket = INVALID_SOCKET;
179

Pekka Pessi's avatar
Pekka Pessi committed
180 181 182 183 184
  tl_gets(nat->tags, 
	  TESTNATTAG_SYMMETRIC_REF(nat->symmetric),
	  TESTNATTAG_LOGGING_REF(nat->logging),
	  TAG_END());

Pekka Pessi's avatar
Pekka Pessi committed
185 186
  hints->li_scope = LI_SCOPE_HOST | LI_SCOPE_SITE | LI_SCOPE_GLOBAL;

Pekka Pessi's avatar
Pekka Pessi committed
187 188 189 190 191
  error = su_getlocalinfo(hints, &nat->localinfo);
  if (error) {
    fprintf(stderr, "test_nat: su_getlocalinfo: %s\n", su_gli_strerror(error));
    return -1;
  }
192

Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197 198 199 200 201 202 203 204 205
  /* We must have two different IP addresses. */
  if (!nat->localinfo || !nat->localinfo->li_next) {
    fprintf(stderr, "test_nat: only one IP address available\n");
    return -1;
  }

  for (li = nat->localinfo; li; li = li->li_next) {
    if (nat->family == 0 || nat->family == li->li_family)
      break;
  }
  if (li == NULL)
    li = nat->localinfo;

206 207
  memcpy(su, li->li_addr, sulen = li->li_addrlen);
  memset(SU_ADDR(su), 0, SU_ADDRLEN(su));
Pekka Pessi's avatar
Pekka Pessi committed
208 209 210 211 212 213

  nat->private = li;

  /* Bind TCP and UDP to same port */
  for (;;) {
    nat->udp_socket = su_socket(li->li_family, SOCK_DGRAM, IPPROTO_UDP);
214
    if (nat->udp_socket == INVALID_SOCKET)
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218 219 220 221 222
      return -1;

    if (bind(nat->udp_socket, (void *)su, sulen) < 0) {
      if (port0 == 0) {
	su_perror("nat: bind(udp_socket)");
	return -1;
      }

223
      fprintf(stderr, "test_nat: port %u: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
224 225
	      port, su_strerror(su_errno()));
      su_close(nat->udp_socket);
226

227
      nat->udp_socket = INVALID_SOCKET;
Pekka Pessi's avatar
Pekka Pessi committed
228 229 230 231 232 233 234 235 236 237 238 239

      if (++port > 65535)
	port = 1024;
      if (port == port0) {
	fprintf(stderr, "test_nat: could not find free port pairt\n");
	return -1;
      }

      continue;
    }

    if (getsockname(nat->udp_socket, (void *)su, &sulen) < 0) {
Pekka Pessi's avatar
Pekka Pessi committed
240
      su_perror("nat: getsockname(udp_socket)");
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245 246 247 248 249 250 251 252
      return -1;
    }

    if (port0 == 0) {
      port = port0 = ntohs(su->su_port);
      if (port0 == 0) {
	fprintf(stderr, "test_nat: bind did not return port\n");
	return -1;
      }
    }

    nat->tcp_socket = su_socket(li->li_family, SOCK_STREAM, IPPROTO_TCP);
253
    if (nat->tcp_socket == INVALID_SOCKET)
Pekka Pessi's avatar
Pekka Pessi committed
254
      return -1;
255

Pekka Pessi's avatar
Pekka Pessi committed
256 257
    if (bind(nat->tcp_socket, (void *)su, sulen) < 0) {
      su_close(nat->tcp_socket);
258
      nat->tcp_socket = INVALID_SOCKET;
Pekka Pessi's avatar
Pekka Pessi committed
259

260
      fprintf(stderr, "test_nat: port %u: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265 266 267 268 269 270 271 272 273
	      port, su_strerror(su_errno()));

      if (++port > 65535)
	port = 1024;
      if (port == port0) {
	fprintf(stderr, "test_nat: could not find free port pair\n");
	return -1;
      }

      continue;
    }

    break;
274
  }
Pekka Pessi's avatar
Pekka Pessi committed
275

276 277
  memcpy(nat->in_address, li->li_addr, nat->in_addrlen = li->li_addrlen);
  nat->in_address->su_port = su->su_port;
Pekka Pessi's avatar
Pekka Pessi committed
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

  if (su_setreuseaddr(nat->udp_socket, 1) < 0) {
    su_perror("nat: su_setreuseaddr(udp_socket)");
    return -1;
  }

  if (listen(nat->tcp_socket, 5) < 0) {
    su_perror("nat: listen(tcp_socket)");
    return -1;
  }

  if (su_wait_create(wait, nat->udp_socket, SU_WAIT_IN) < 0) {
    su_perror("nat: su_wait_create");
    return -1;
  }

  nat->udp_register = su_root_register(root, wait, new_udp, NULL, 0);
  if (nat->udp_register < 0) {
    su_perror("nat: su_root_register");
    return -1;
  }

  if (su_wait_create(wait, nat->tcp_socket, SU_WAIT_IN) < 0) {
    su_perror("nat: su_wait_create");
    return -1;
  }

  nat->tcp_register = su_root_register(root, wait, new_tcp, NULL, 0);
  if (nat->tcp_register < 0) {
    su_perror("nat: su_root_register");
    return -1;
  }
310

Pekka Pessi's avatar
Pekka Pessi committed
311 312 313 314 315 316
  return 0;
}

static void
test_nat_deinit(su_root_t *root, struct nat *nat)
{
Pekka Pessi's avatar
Pekka Pessi committed
317
  flush_bindings(nat);
Pekka Pessi's avatar
Pekka Pessi committed
318

Pekka Pessi's avatar
Pekka Pessi committed
319 320 321 322 323
  if (nat->tcp_register)
    su_root_deregister(root, nat->tcp_register);
  if (nat->udp_register)
    su_root_deregister(root, nat->udp_register);

324
  if (nat->udp_socket != INVALID_SOCKET)
Pekka Pessi's avatar
Pekka Pessi committed
325
    su_close(nat->udp_socket);
326
  if (nat->tcp_socket != INVALID_SOCKET)
Pekka Pessi's avatar
Pekka Pessi committed
327
    su_close(nat->tcp_socket);
Pekka Pessi's avatar
Pekka Pessi committed
328

Pekka Pessi's avatar
Pekka Pessi committed
329
  su_freelocalinfo(nat->localinfo);
Pekka Pessi's avatar
Pekka Pessi committed
330

Pekka Pessi's avatar
Pekka Pessi committed
331 332 333
  free(nat->tags);
}

334
struct nat *test_nat_create(su_root_t *root,
Pekka Pessi's avatar
Pekka Pessi committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348
			    int family,
			    tag_type_t tag, tag_value_t value, ...)
{
  struct nat *nat = su_home_new(sizeof *nat);

  if (nat) {
    ta_list ta;

    nat->parent = root;
    nat->family = family;

    ta_start(ta, tag, value);
    nat->tags = tl_llist(ta_tags(ta));
    ta_end(ta);
349

Pekka Pessi's avatar
Pekka Pessi committed
350 351
    if (su_clone_start(root,
		       nat->clone,
352
		       nat,
Pekka Pessi's avatar
Pekka Pessi committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		       test_nat_init,
		       test_nat_deinit) == -1)
      su_home_unref(nat->home), nat = NULL;
  }

  return nat;
}

void test_nat_destroy(struct nat *nat)
{
  if (nat) {
    su_clone_wait(nat->parent, nat->clone);
    su_home_unref(nat->home);
  }
}

/** Get "private" address. */
370
int test_nat_private(struct nat *nat, void *address, socklen_t *return_addrlen)
Pekka Pessi's avatar
Pekka Pessi committed
371 372 373
{
  if (nat == NULL || address == NULL || return_addrlen == NULL)
    return su_seterrno(EFAULT);
374

Pekka Pessi's avatar
Pekka Pessi committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  if (*return_addrlen < nat->in_addrlen)
    return su_seterrno(EINVAL);

  memcpy(address, nat->in_address, *return_addrlen = nat->in_addrlen);

  return 0;
}

/** Set "public" address. */
int test_nat_public(struct nat *nat, void const *address, int addrlen)
{
  su_sockaddr_t const *su = address;
  su_localinfo_t *li;

  if (nat == NULL)
    return su_seterrno(EFAULT);

  if (address == NULL) {
    nat->fake = NULL;
    return 0;
  }

Pekka Pessi's avatar
Pekka Pessi committed
397
  if ((size_t)addrlen > sizeof nat->out_address)
Pekka Pessi's avatar
Pekka Pessi committed
398
    return su_seterrno(EINVAL);
399

Pekka Pessi's avatar
Pekka Pessi committed
400
  for (li = nat->localinfo; li; li = li->li_next) {
401 402
    if (li != nat->private &&
	li->li_scope == LI_SCOPE_HOST &&
Pekka Pessi's avatar
Pekka Pessi committed
403 404 405
	li->li_family == su->su_family)
      break;
  }
406

Pekka Pessi's avatar
Pekka Pessi committed
407 408 409 410 411 412 413 414 415
  if (li == NULL)
    for (li = nat->localinfo; li; li = li->li_next) {
      if (li != nat->private && li->li_family == su->su_family)
	break;
    }

  if (li == NULL)
    return su_seterrno(EADDRNOTAVAIL);

Pekka Pessi's avatar
Pekka Pessi committed
416
  su_clone_pause(nat->clone);
Pekka Pessi's avatar
Pekka Pessi committed
417 418
  memcpy(nat->out_address, address, nat->out_addrlen = addrlen);
  nat->fake = li;
Pekka Pessi's avatar
Pekka Pessi committed
419
  su_clone_resume(nat->clone);
Pekka Pessi's avatar
Pekka Pessi committed
420 421 422 423

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
424 425 426 427 428 429
int test_nat_flush(struct nat *nat)
{
  if (nat == NULL)
    return su_seterrno(EFAULT);

  return su_task_execute(su_clone_task(nat->clone), 
430
			 invalidate_bindings, nat, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
431 432
}

Pekka Pessi's avatar
Pekka Pessi committed
433 434
/* ====================================================================== */

Pekka Pessi's avatar
Pekka Pessi committed
435 436 437 438 439
struct binding *nat_binding_new(struct nat *nat,
				char const *protoname,
				int socktype,
				int protocol,
				int connected,
440
				su_socket_t in_socket,
Pekka Pessi's avatar
Pekka Pessi committed
441 442
				su_sockaddr_t *from,
				socklen_t fromlen)
Pekka Pessi's avatar
Pekka Pessi committed
443
{
Pekka Pessi's avatar
Pekka Pessi committed
444
  struct binding *b;
Pekka Pessi's avatar
Pekka Pessi committed
445

Pekka Pessi's avatar
Pekka Pessi committed
446 447 448
  if (nat->fake == NULL) {	/* Xyzzy */
    fprintf(stderr, "test_nat: fake address missing\n");
    su_close(in_socket);
Pekka Pessi's avatar
Pekka Pessi committed
449
    return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
450
  }
Pekka Pessi's avatar
Pekka Pessi committed
451

Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455 456 457
  b = su_zalloc(nat->home, sizeof *b);
  if (b == NULL) {
    su_perror("nat_binding_new: su_zalloc");
    su_close(in_socket);
    return 0;
  }
Pekka Pessi's avatar
Pekka Pessi committed
458

Pekka Pessi's avatar
Pekka Pessi committed
459 460
  b->nat = nat;
  b->socktype = socktype;
Pekka Pessi's avatar
Pekka Pessi committed
461
  b->protocol = protocol;
462
  b->in_socket = in_socket, b->out_socket = INVALID_SOCKET;
Pekka Pessi's avatar
Pekka Pessi committed
463 464
  b->in_register = -1, b->out_register = -1;

Pekka Pessi's avatar
Pekka Pessi committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478
  if (binding_init(b, protoname, connected, nat->fake, from, fromlen) < 0)
    nat_binding_destroy(b), b = NULL;

  return b;
}

static int binding_init(struct binding *b,
			char const *protoname,
			int connected,
			su_localinfo_t *li,
			su_sockaddr_t *from,
			socklen_t fromlen)
{
  struct nat *nat = b->nat;
479
  su_socket_t out_socket;
Pekka Pessi's avatar
Pekka Pessi committed
480 481 482 483 484 485 486 487 488 489 490
  su_sockaddr_t addr[1];
  socklen_t addrlen = (sizeof addr);
  char ipname[64];
  su_wait_t wait[1];
  su_wakeup_f in_to_out, out_to_in;

  if (b->socktype == SOCK_STREAM)
    in_to_out = tcp_in_to_out, out_to_in = tcp_out_to_in;    
  else
    in_to_out = udp_in_to_out, out_to_in = udp_out_to_in;    
  
491
  if (b->in_socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
492 493 494
    int in_socket;

    in_socket = su_socket(from->su_family, b->socktype, b->protocol);
495
    if (in_socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
      su_perror("nat_binding_new: socket");
      return -1;
    }
    b->in_socket = in_socket;
    if (su_setreuseaddr(in_socket, 1) < 0) {
      su_perror("nat_binding_new: su_setreuseaddr(in_socket)");
      return -1;
    }
    if (bind(in_socket, (void *)nat->in_address, nat->in_addrlen) < 0) {
      su_perror("nat_binding_new: bind(in_socket)");
      return -1;
    }
    if (connect(in_socket, (void *)from, fromlen) < 0) {
      su_perror("nat_binding_new: connect(in_socket)");
      return -1;
    }
  }

  out_socket = su_socket(li->li_family, b->socktype, b->protocol);
515
  if (out_socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    su_perror("nat_binding_new: socket");
    return -1;
  }
  b->out_socket = out_socket;

  if (bind(out_socket, (void *)li->li_addr, li->li_addrlen) < 0) {
    su_perror("nat_binding_new: bind(to)");
    return -1;
  }

  if (connected)
    if (connect(out_socket, (void *)nat->out_address, nat->out_addrlen) < 0) {
      su_perror("nat_binding_new: connect(to)");
      return -1;
    }

  getpeername(b->in_socket, (void *)addr, &addrlen);
  inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
534
  snprintf(b->in_name, sizeof b->in_name,
Pekka Pessi's avatar
Pekka Pessi committed
535
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
Pekka Pessi's avatar
Pekka Pessi committed
536
	   ipname, ntohs(addr->su_port));
Pekka Pessi's avatar
Pekka Pessi committed
537 538

  getsockname(out_socket, (void *)addr, &addrlen);
Pekka Pessi's avatar
Pekka Pessi committed
539
  inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
Pekka Pessi's avatar
Pekka Pessi committed
540 541
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
Pekka Pessi's avatar
Pekka Pessi committed
542
	   ipname, ntohs(addr->su_port));
Pekka Pessi's avatar
Pekka Pessi committed
543

Pekka Pessi's avatar
Pekka Pessi committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  if (su_wait_create(wait, b->in_socket, SU_WAIT_IN) < 0) {
    su_perror("nat_binding_new: su_wait_create");
    return -1;
  }
  b->in_register = su_root_register(nat->root, wait, in_to_out, b, 0);
  if (b->in_register < 0) {
    su_perror("nat_binding_new: su_root_register");
    su_wait_destroy(wait); 
    return -1;
  }

  if (su_wait_create(wait, out_socket, SU_WAIT_IN) < 0) {
    su_perror("nat_binding_new: su_wait_create");
    return -1;
  }
  b->out_register = su_root_register(nat->root, wait, out_to_in, b, 0);
  if (b->out_register < 0) {
    su_perror("nat_binding_new: su_root_register");
    su_wait_destroy(wait);
    return -1;
  }

Pekka Pessi's avatar
Pekka Pessi committed
566 567
  nat_binding_insert(&nat->bindings, b);

Pekka Pessi's avatar
Pekka Pessi committed
568 569 570
  if (nat->logging)
    printf("nat: new %s binding %s <=> %s\n",
	   protoname, b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
571 572

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
573 574 575 576 577 578 579 580 581 582 583 584
}

static void nat_binding_destroy(struct binding *b)
{
  nat_binding_remove(b);
  if (b->in_register != -1)
    su_root_deregister(b->nat->root, b->in_register);
  if (b->out_register != -1)
    su_root_deregister(b->nat->root, b->out_register);
  su_close(b->in_socket), su_close(b->out_socket);
}

Pekka Pessi's avatar
Pekka Pessi committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598
static void flush_bindings(struct nat *nat)
{
  struct binding *b;

  for (b = nat->bindings; b; b = b->next) {
    if (b->in_register)
      su_root_deregister(nat->root, b->in_register);
    su_close(b->in_socket);
    if (b->out_register)
      su_root_deregister(nat->root, b->out_register);
    su_close(b->out_socket);
  }
}

599
static int invalidate_bindings(void *arg)
Pekka Pessi's avatar
Pekka Pessi committed
600
{
601
  struct nat *nat = arg;
Pekka Pessi's avatar
Pekka Pessi committed
602 603 604 605 606
  struct binding *b;

  for (b = nat->bindings; b; b = b->next) {
    invalidate_binding(b);
  }
607
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
608 609
}

Pekka Pessi's avatar
Pekka Pessi committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
#if 0
static struct binding *nat_binding_find(struct nat *nat,
					su_sockaddr_t *from, 
					int fromlen)
{
  char name[64], ipname[64];
  size_t namelen;
  struct binding *b;
				       
  inet_ntop(from->su_family, SU_ADDR(from), ipname, sizeof ipname);
  snprintf(name, sizeof name,
	   from->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
	   ipname, ntohs(from->su_port));
  namelen = strlen(name) + 1;

  for (b = nat->bindings; b; b = b->next) {
    if (memcmp(name, b->in_name, namelen) == 0)
      return b;
  }

  if (b == NULL)
    b = nat_binding_new(nat, "UDP", SOCK_DGRAM, IPPROTO_UDP, nat->symmetric, 
632
			INVALID_SOCKET, from, fromlen);
Pekka Pessi's avatar
Pekka Pessi committed
633 634 635 636 637

  return b;
}
#endif

Pekka Pessi's avatar
Pekka Pessi committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/* ====================================================================== */

LIST_BODIES(static, nat_binding, struct binding, next, prev);

/* ====================================================================== */

static int new_udp(struct nat *nat, su_wait_t *wait, struct binding *dummy)
{
  int events;
  su_sockaddr_t from[1];
  socklen_t fromlen = (sizeof from);
  struct binding *b;
  ssize_t n, m;

  events = su_wait_events(wait, nat->udp_socket);

654 655
  n = su_recvfrom(nat->udp_socket, nat->buffer, sizeof nat->buffer, 0,
		  from, &fromlen);
Pekka Pessi's avatar
Pekka Pessi committed
656 657 658 659 660
  if (n < 0) {
    su_perror("new_udp: recvfrom");
    return 0;
  }

Pekka Pessi's avatar
Pekka Pessi committed
661
  b = nat_binding_new(nat, "UDP", SOCK_DGRAM, IPPROTO_UDP, nat->symmetric, 
662
		      INVALID_SOCKET, from, fromlen);
Pekka Pessi's avatar
Pekka Pessi committed
663
  if (b == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
664 665
    return 0;

666
  if (nat->symmetric)
667
    m = su_send(b->out_socket, nat->buffer, n, 0);
668
  else
669 670
    m = su_sendto(b->out_socket, nat->buffer, n, 0, 
		  nat->out_address, nat->out_addrlen);
Pekka Pessi's avatar
Pekka Pessi committed
671

Pekka Pessi's avatar
Pekka Pessi committed
672 673 674
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
675 676 677 678 679 680 681 682

  return 0;
}

static int udp_in_to_out(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;
683
  size_t len, filtered;
684
  struct nat_filter *f;
Pekka Pessi's avatar
Pekka Pessi committed
685 686 687

  events = su_wait_events(wait, b->in_socket);

688
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
689
  if (n == -1) {
Pekka Pessi's avatar
Pekka Pessi committed
690 691 692 693
    su_perror("udp_in_to_out: recv");
    return 0;
  }

694 695
  len = (size_t)n;

696
  for (f = nat->out_filters; f; f = f->next) {
697 698
    filtered = f->condition(nat->buffer, len);
    if (filtered != len) {
699
      if (nat->logging)
700 701 702 703 704
	printf("nat: udp filtered "MOD_ZU" from %s => "MOD_ZU" to %s\n",
	       len, b->in_name, filtered, b->out_name);
      if (filtered == 0)
	return 0;
      len = filtered;
705 706 707
    }
  }

708
  if (nat->symmetric)
709
    m = su_send(b->out_socket, nat->buffer, len, 0);
710
  else
711
    m = su_sendto(b->out_socket, nat->buffer, len, 0,
712
		  nat->out_address, nat->out_addrlen);
Pekka Pessi's avatar
Pekka Pessi committed
713

Pekka Pessi's avatar
Pekka Pessi committed
714 715 716
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
717 718 719 720 721 722 723 724

  return 0;
}

static int udp_out_to_in(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;
725
  size_t len, filtered;
726
  struct nat_filter *f;
Pekka Pessi's avatar
Pekka Pessi committed
727 728 729

  events = su_wait_events(wait, b->out_socket);

730
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
Pekka Pessi's avatar
Pekka Pessi committed
731 732 733 734 735
  if (n < 0) {
    su_perror("udp_out_to_out: recv");
    return 0;
  }

736 737
  len = (size_t)n;

738
  for (f = nat->in_filters; f; f = f->next) {
739 740
    filtered = f->condition(nat->buffer, len);
    if (filtered != len) {
741
      if (nat->logging)
742 743 744 745 746
	printf("nat: udp filtered "MOD_ZU" from %s => "MOD_ZU" to %s\n",
	       len, b->out_name, filtered, b->in_name);
      if (filtered == 0)
	return 0;
      len = filtered;
747 748 749
    }
  }

750
  m = su_send(b->in_socket, nat->buffer, n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
751

Pekka Pessi's avatar
Pekka Pessi committed
752 753 754
  if (nat->logging)
    printf("nat: udp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
Pekka Pessi's avatar
Pekka Pessi committed
755 756 757 758 759 760 761 762 763

  return 0;
}

/* ====================================================================== */

static int new_tcp(struct nat *nat, su_wait_t *wait, struct binding *dummy)
{
  int events;
764
  su_socket_t in_socket;
Pekka Pessi's avatar
Pekka Pessi committed
765 766 767 768 769 770
  su_sockaddr_t from[1];
  socklen_t fromlen = (sizeof from);
  struct binding *b;

  events = su_wait_events(wait, nat->tcp_socket);

Pekka Pessi's avatar
Pekka Pessi committed
771
  in_socket = accept(nat->tcp_socket, (void *)from, &fromlen);
772
  if (in_socket == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
773
    su_perror("new_tcp: accept");
Pekka Pessi's avatar
Pekka Pessi committed
774 775 776
    return 0;
  }

Pekka Pessi's avatar
Pekka Pessi committed
777 778 779
  b = nat_binding_new(nat, "TCP", SOCK_STREAM, IPPROTO_TCP, 1,
		      in_socket, from, fromlen);
  if (b == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
780 781 782 783 784 785 786 787 788 789 790 791
    return 0;

  return 0;
}

static int tcp_in_to_out(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m, o;

  events = su_wait_events(wait, b->in_socket);

792
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
Pekka Pessi's avatar
Pekka Pessi committed
793 794 795 796 797 798
  if (n < 0) {
    su_perror("tcp_in_to_out: recv");
    return 0;
  }

  if (n == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
799 800
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
801 802 803 804 805 806 807
    shutdown(b->out_socket, 1);
    su_root_eventmask(nat->root, b->in_register, b->in_socket, 0);
    b->in_closed = 1;
    if (b->out_closed && b->in_closed)
      nat_binding_destroy(b);
    return 0;
  }
808

Pekka Pessi's avatar
Pekka Pessi committed
809
  for (m = 0; m < n; m += o) {
810
    o = su_send(b->out_socket, nat->buffer + m, n - m, 0);
Pekka Pessi's avatar
Pekka Pessi committed
811 812 813 814 815 816
    if (o < 0) {
      su_perror("tcp_in_to_out: send");
      break;
    }
  }

Pekka Pessi's avatar
Pekka Pessi committed
817 818 819
  if (nat->logging)
    printf("nat: tcp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
820 821 822 823 824 825 826 827 828 829 830

  return 0;
}

static int tcp_out_to_in(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m, o;

  events = su_wait_events(wait, b->out_socket);

831
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
Pekka Pessi's avatar
Pekka Pessi committed
832 833 834 835 836 837
  if (n < 0) {
    su_perror("tcp_out_to_in: recv");
    return 0;
  }

  if (n == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
838 839
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->out_name, b->in_name);
Pekka Pessi's avatar
Pekka Pessi committed
840 841 842 843 844 845 846 847 848
    shutdown(b->in_socket, 1);
    su_root_eventmask(nat->root, b->in_register, b->out_socket, 0);
    b->out_closed = 1;
    if (b->out_closed && b->in_closed)
      nat_binding_destroy(b);
    return 0;
  }

  for (m = 0; m < n; m += o) {
849
    o = su_send(b->in_socket, nat->buffer + m, n - m, 0);
Pekka Pessi's avatar
Pekka Pessi committed
850
    if (o < 0) {
Pekka Pessi's avatar
Pekka Pessi committed
851 852
      if (su_errno() != EPIPE)
	su_perror("tcp_in_to_out: send");
Pekka Pessi's avatar
Pekka Pessi committed
853 854 855 856
      break;
    }
  }

Pekka Pessi's avatar
Pekka Pessi committed
857 858 859
  if (nat->logging)
    printf("nat: tcp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
Pekka Pessi's avatar
Pekka Pessi committed
860 861 862

  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
863 864 865 866 867 868

static int invalidate_binding(struct binding *b)
{
  struct nat *nat = b->nat;
  su_sockaddr_t addr[1];
  socklen_t addrlen = (sizeof addr);
869 870
  su_socket_t out;
  int out_register;
Pekka Pessi's avatar
Pekka Pessi committed
871 872 873 874
  su_wait_t wout[1];
  char name[64];

  out = su_socket(nat->fake->li_family, b->socktype, 0);
875
  if (out == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    su_perror("new_udp: socket");
    return -1;
  }
  if (bind(out, (void *)nat->fake->li_addr, nat->fake->li_addrlen) < 0) {
    su_perror("new_udp: bind(to)");
    su_close(out);
    return -1;
  }

  if (nat->symmetric)
    if (connect(out, (void *)nat->out_address, nat->out_addrlen) < 0) {
      su_perror("new_udp: connect(to)");
      su_close(out);
      return -1;
    }

  if (su_wait_create(wout, out, SU_WAIT_IN) < 0) {
    su_perror("new_udp: su_wait_create");
    su_close(out);
    return -1;
  }

  if (b->socktype == SOCK_DGRAM)
    out_register = su_root_register(nat->root, wout, udp_out_to_in, b, 0);
  else
    out_register = su_root_register(nat->root, wout, tcp_out_to_in, b, 0);

  if (out_register < 0) {
    su_perror("new_udp: su_root_register");
    su_wait_destroy(wout);
    su_close(out);
    return -1;
  }

  su_root_deregister(nat->root, b->out_register);
  su_close(b->out_socket);

  b->out_socket = out;
  b->out_register = out_register;

  getsockname(out, (void *)addr, &addrlen);
  inet_ntop(addr->su_family, SU_ADDR(addr), name, sizeof name);
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
	   name, ntohs(addr->su_port));

Pekka Pessi's avatar
Pekka Pessi committed
922 923
  if (nat->logging)
    printf("nat: flushed binding %s <=> %s\n", b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
924 925 926

  return 0;
}
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

LIST_BODIES(static, nat_filter, struct nat_filter, next, prev);

struct args {
  struct nat *nat;
  struct nat_filter *f;
  int outbound;
};

int execute_nat_filter_insert(void *_args)
{
  struct args *a = (struct args *)_args;
  if (a->outbound)
    nat_filter_insert(&a->nat->out_filters, a->f);
  else
    nat_filter_insert(&a->nat->in_filters, a->f);
  return 0;
}

int execute_nat_filter_remove(void *_args)
{
  struct args *a = (struct args *)_args;
  nat_filter_remove(a->f);
  return 0;
}

struct nat_filter *test_nat_add_filter(struct nat *nat,
954 955
				       size_t (*condition)(void *message,
							   size_t len),
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
				       int outbound)
{
  struct args a[1];

  if (nat == NULL)
    return su_seterrno(EFAULT), NULL;

  a->nat = nat;
  a->f = su_zalloc(nat->home, sizeof *a->f);
  a->outbound = outbound;

  if (a->f) {
    a->f->condition = condition;
    if (su_task_execute(su_clone_task(nat->clone),
			execute_nat_filter_insert, a, NULL) < 0)
      su_free(nat->home, a->f), a->f = NULL;
  }

  return a->f;
}


int test_nat_remove_filter(struct nat *nat,
			   struct nat_filter *filter)
{
  struct args a[1];

  if (nat == NULL)
    return su_seterrno(EFAULT);

  a->nat = nat;
  a->f = filter;
  
  if (su_task_execute(su_clone_task(nat->clone),
		      execute_nat_filter_remove, a, NULL) < 0)
    return -1;

  su_free(nat->home, filter);
  return 0;
}