aboutsummaryrefslogtreecommitdiff
path: root/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c
blob: 0e0b385f9bc56a49341ccb010cb33c2e7e96480e (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
/* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
 * Copyright (c) 2010-2012, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
  * \file tor-fw-helper-natpmp.c
  * \brief The implementation of our NAT-PMP firewall helper.
  **/

#include "orconfig.h"
#ifdef NAT_PMP
#ifdef _WIN32
#define STATICLIB
#endif
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifndef _WIN32
#include <arpa/inet.h>
#endif

// debugging stuff
#include <assert.h>

#include "compat.h"

#include "tor-fw-helper.h"
#include "tor-fw-helper-natpmp.h"

/** This hooks NAT-PMP into our multi-backend API. */
static tor_fw_backend_t tor_natpmp_backend = {
  "natpmp",
  sizeof(struct natpmp_state_t),
  tor_natpmp_init,
  tor_natpmp_cleanup,
  tor_natpmp_fetch_public_ip,
  tor_natpmp_add_tcp_mapping
};

/** Return the backend for NAT-PMP. */
const tor_fw_backend_t *
tor_fw_get_natpmp_backend(void)
{
  return &tor_natpmp_backend;
}

/** Initialize the NAT-PMP backend and store the results in
 * <b>backend_state</b>.*/
int
tor_natpmp_init(tor_fw_options_t *tor_fw_options, void *backend_state)
{
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
  int r = 0;

  memset(&(state->natpmp), 0, sizeof(natpmp_t));
  memset(&(state->response), 0, sizeof(natpmpresp_t));
  state->init = 0;
  state->protocol = NATPMP_PROTOCOL_TCP;
  state->lease = NATPMP_DEFAULT_LEASE;

  if (tor_fw_options->verbose)
    fprintf(stdout, "V: natpmp init...\n");

  r = initnatpmp(&(state->natpmp), 0, 0);
  if (r == 0) {
    state->init = 1;
    fprintf(stdout, "tor-fw-helper: natpmp initialized...\n");
    return r;
  } else {
    fprintf(stderr, "tor-fw-helper: natpmp failed to initialize...\n");
    return r;
  }
}

/** Tear down the NAT-PMP connection stored in <b>backend_state</b>.*/
int
tor_natpmp_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state)
{
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
  int r = 0;
  if (tor_fw_options->verbose)
    fprintf(stdout, "V: natpmp cleanup...\n");
  r = closenatpmp(&(state->natpmp));
  if (tor_fw_options->verbose)
    fprintf(stdout, "V: closing natpmp socket: %d\n", r);
  return r;
}

/** Use select() to wait until we can read on fd. */
static int
wait_until_fd_readable(tor_socket_t fd, struct timeval *timeout)
{
  int r;
  fd_set fds;
  if (fd >= FD_SETSIZE) {
    fprintf(stderr, "E: NAT-PMP FD_SETSIZE error %d\n", fd);
    return -1;
  }
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  r = select(fd+1, &fds, NULL, NULL, timeout);
  if (r == -1) {
    fprintf(stdout, "V: select failed in wait_until_fd_readable: %s\n",
            strerror(errno));
    return -1;
  }
  /* XXXX we should really check to see whether fd was readable, or we timed
     out. */
  return 0;
}

/** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
 * using the <b>natpmp_t</b> stored in <b>backend_state</b>. */
int
tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
                           void *backend_state)
{
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
  int r = 0;
  int x = 0;
  int sav_errno;

  struct timeval timeout;

  if (tor_fw_options->verbose)
    fprintf(stdout, "V: sending natpmp portmapping request...\n");
  r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
                                tor_fw_options->internal_port,
                                tor_fw_options->external_port,
                                state->lease);
  if (tor_fw_options->verbose)
    fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
            "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");

  do {
    getnatpmprequesttimeout(&(state->natpmp), &timeout);
    x = wait_until_fd_readable(state->natpmp.s, &timeout);
    if (x == -1)
      return -1;

    if (tor_fw_options->verbose)
      fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
    sav_errno = errno;

    if (r<0 && r!=NATPMP_TRYAGAIN) {
      fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
      fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
              strerror(sav_errno));
    }

  } while (r == NATPMP_TRYAGAIN);

  if (r != 0) {
    /* XXX TODO: NATPMP_* should be formatted into useful error strings */
    fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
            " %d\n", r);
    if (r == -51)
      fprintf(stderr, "E: NAT-PMP It appears that the request was "
              "unauthorized\n");
    return r;
  }

  if (r == NATPMP_SUCCESS) {
    fprintf(stdout, "tor-fw-helper: NAT-PMP mapped public port %hu to"
            " localport %hu liftime %u\n",
            (state->response).pnu.newportmapping.mappedpublicport,
            (state->response).pnu.newportmapping.privateport,
            (state->response).pnu.newportmapping.lifetime);
  }

  tor_fw_options->nat_pmp_status = 1;

  return r;
}

/** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
 * Use the connection context stored in <b>backend_state</b>. */
int
tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
                           void *backend_state)
{
  int r = 0;
  int x = 0;
  int sav_errno;
  natpmp_state_t *state = (natpmp_state_t *) backend_state;

  struct timeval timeout;

  r = sendpublicaddressrequest(&(state->natpmp));
  fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
          " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");

  do {
    getnatpmprequesttimeout(&(state->natpmp), &timeout);

    x = wait_until_fd_readable(state->natpmp.s, &timeout);
    if (x == -1)
      return -1;

    if (tor_fw_options->verbose)
      fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
    sav_errno = errno;

    if (tor_fw_options->verbose)
      fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
              " %d\n", r);

    if ( r < 0 && r != NATPMP_TRYAGAIN) {
      fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
              r);
      fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
              strerror(sav_errno));
    }

  } while (r == NATPMP_TRYAGAIN );

  if (r != 0) {
    fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
            " %d\n", r);
    return r;
  }

  fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
          inet_ntoa((state->response).pnu.publicaddress.addr));
  tor_fw_options->public_ip_status = 1;

  if (tor_fw_options->verbose) {
    fprintf(stdout, "V: result = %u\n", r);
    fprintf(stdout, "V: type = %u\n", (state->response).type);
    fprintf(stdout, "V: resultcode = %u\n", (state->response).resultcode);
    fprintf(stdout, "V: epoch = %u\n", (state->response).epoch);
  }

  return r;
}
#endif