tor-fw-helper-natpmp.c 7.1 KB

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