tor-fw-helper-natpmp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
  2. * Copyright (c) 2010-2011, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file tor-fw-helper-natpmp.c
  6. * \brief The implementation of our NAT-PMP firewall helper.
  7. **/
  8. #include "orconfig.h"
  9. #ifdef NAT_PMP
  10. #ifdef _WIN32
  11. #define STATICLIB
  12. #endif
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #ifndef _WIN32
  18. #include <arpa/inet.h>
  19. #endif
  20. // debugging stuff
  21. #include <assert.h>
  22. #include "tor-fw-helper.h"
  23. #include "tor-fw-helper-natpmp.h"
  24. /** This hooks NAT-PMP into our multi-backend API. */
  25. static tor_fw_backend_t tor_natpmp_backend = {
  26. "natpmp",
  27. sizeof(struct natpmp_state_t),
  28. tor_natpmp_init,
  29. tor_natpmp_cleanup,
  30. tor_natpmp_fetch_public_ip,
  31. tor_natpmp_add_tcp_mapping
  32. };
  33. /** Return the backend for NAT-PMP. */
  34. const tor_fw_backend_t *
  35. tor_fw_get_natpmp_backend(void)
  36. {
  37. return &tor_natpmp_backend;
  38. }
  39. /** Initialize the NAT-PMP backend and store the results in
  40. * <b>backend_state</b>.*/
  41. int
  42. tor_natpmp_init(tor_fw_options_t *tor_fw_options, void *backend_state)
  43. {
  44. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  45. int r = 0;
  46. memset(&(state->natpmp), 0, sizeof(natpmp_t));
  47. memset(&(state->response), 0, sizeof(natpmpresp_t));
  48. state->init = 0;
  49. state->protocol = NATPMP_PROTOCOL_TCP;
  50. state->lease = NATPMP_DEFAULT_LEASE;
  51. if (tor_fw_options->verbose)
  52. fprintf(stdout, "V: natpmp init...\n");
  53. r = initnatpmp(&(state->natpmp), 0, 0);
  54. if (r == 0) {
  55. state->init = 1;
  56. fprintf(stdout, "tor-fw-helper: natpmp initialized...\n");
  57. return r;
  58. } else {
  59. fprintf(stderr, "tor-fw-helper: natpmp failed to initialize...\n");
  60. return r;
  61. }
  62. }
  63. /** Tear down the NAT-PMP connection stored in <b>backend_state</b>.*/
  64. int
  65. tor_natpmp_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state)
  66. {
  67. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  68. int r = 0;
  69. if (tor_fw_options->verbose)
  70. fprintf(stdout, "V: natpmp cleanup...\n");
  71. r = closenatpmp(&(state->natpmp));
  72. if (tor_fw_options->verbose)
  73. fprintf(stdout, "V: closing natpmp socket: %d\n", r);
  74. return r;
  75. }
  76. /** Use select() to wait until we can read on fd. */
  77. static int
  78. wait_until_fd_readable(tor_socket_t fd, struct timeval *timeout)
  79. {
  80. int r;
  81. fd_set fds;
  82. if (fd >= FD_SETSIZE) {
  83. fprintf(stderr, "E: NAT-PMP FD_SETSIZE error %d\n", fd);
  84. return -1;
  85. }
  86. FD_ZERO(&fds);
  87. FD_SET(fd, &fds);
  88. r = select(fd+1, &fds, NULL, NULL, timeout);
  89. if (r == -1) {
  90. fprintf(stdout, "V: select failed in wait_until_fd_readable: %s\n",
  91. strerror(errno));
  92. return -1;
  93. }
  94. /* XXXX we should really check to see whether fd was readable, or we timed
  95. out. */
  96. return 0;
  97. }
  98. /** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
  99. * using the <b>natpmp_t</b> stored in <b>backend_state</b>. */
  100. int
  101. tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
  102. void *backend_state)
  103. {
  104. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  105. int r = 0;
  106. int x = 0;
  107. int sav_errno;
  108. struct timeval timeout;
  109. if (tor_fw_options->verbose)
  110. fprintf(stdout, "V: sending natpmp portmapping request...\n");
  111. r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
  112. tor_fw_options->internal_port,
  113. tor_fw_options->external_port,
  114. state->lease);
  115. if (tor_fw_options->verbose)
  116. fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
  117. "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");
  118. do {
  119. getnatpmprequesttimeout(&(state->natpmp), &timeout);
  120. x = wait_until_fd_readable(state->natpmp.s, &timeout);
  121. if (x == -1)
  122. return -1;
  123. if (tor_fw_options->verbose)
  124. fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
  125. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  126. sav_errno = errno;
  127. if (r<0 && r!=NATPMP_TRYAGAIN) {
  128. fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
  129. fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
  130. strerror(sav_errno));
  131. }
  132. } while (r == NATPMP_TRYAGAIN);
  133. if (r != 0) {
  134. /* XXX TODO: NATPMP_* should be formatted into useful error strings */
  135. fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
  136. " %d\n", r);
  137. if (r == -51)
  138. fprintf(stderr, "E: NAT-PMP It appears that the request was "
  139. "unauthorized\n");
  140. return r;
  141. }
  142. if (r == NATPMP_SUCCESS) {
  143. fprintf(stdout, "tor-fw-helper: NAT-PMP mapped public port %hu to"
  144. " localport %hu liftime %u\n",
  145. (state->response).pnu.newportmapping.mappedpublicport,
  146. (state->response).pnu.newportmapping.privateport,
  147. (state->response).pnu.newportmapping.lifetime);
  148. }
  149. tor_fw_options->nat_pmp_status = 1;
  150. return r;
  151. }
  152. /** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
  153. * Use the connection context stored in <b>backend_state</b>. */
  154. int
  155. tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
  156. void *backend_state)
  157. {
  158. int r = 0;
  159. int x = 0;
  160. int sav_errno;
  161. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  162. struct timeval timeout;
  163. r = sendpublicaddressrequest(&(state->natpmp));
  164. fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
  165. " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");
  166. do {
  167. getnatpmprequesttimeout(&(state->natpmp), &timeout);
  168. x = wait_until_fd_readable(state->natpmp.s, &timeout);
  169. if (x == -1)
  170. return -1;
  171. if (tor_fw_options->verbose)
  172. fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
  173. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  174. sav_errno = errno;
  175. if (tor_fw_options->verbose)
  176. fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
  177. " %d\n", r);
  178. if ( r < 0 && r != NATPMP_TRYAGAIN) {
  179. fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
  180. r);
  181. fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
  182. strerror(sav_errno));
  183. }
  184. } while (r == NATPMP_TRYAGAIN );
  185. if (r != 0) {
  186. fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
  187. " %d\n", r);
  188. return r;
  189. }
  190. fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
  191. inet_ntoa((state->response).pnu.publicaddress.addr));
  192. tor_fw_options->public_ip_status = 1;
  193. if (tor_fw_options->verbose) {
  194. fprintf(stdout, "V: result = %u\n", r);
  195. fprintf(stdout, "V: type = %u\n", (state->response).type);
  196. fprintf(stdout, "V: resultcode = %u\n", (state->response).resultcode);
  197. fprintf(stdout, "V: epoch = %u\n", (state->response).epoch);
  198. }
  199. return r;
  200. }
  201. #endif