pdesc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * pdesc.c
  3. * ptunnel is licensed under the BSD license:
  4. *
  5. * Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>,
  6. * Yellow Lemon Software. All rights reserved.
  7. *
  8. * Copyright (c) 2017-2019, Toni Uhlig <matzeton@googlemail.com>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * - Neither the name of the Yellow Lemon Software nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Contacting the author:
  37. * You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe,
  38. * in case your text editor didn't realize), here: <daniels@cs.uit.no>
  39. *
  40. * The official ptunnel website is here:
  41. * <http://www.cs.uit.no/~daniels/PingTunnel/>
  42. *
  43. * Note that the source code is best viewed with tabs set to 4 spaces.
  44. */
  45. #include <stdlib.h>
  46. #include <sys/time.h>
  47. #include "pdesc.h"
  48. #include "options.h"
  49. #include "utils.h"
  50. #include "ptunnel.h"
  51. /* create_and_insert_proxy_desc: Creates a new proxy descriptor, linking it into
  52. * the descriptor chain. If the sock argument is 0, the function will establish
  53. * a TCP connection to the ip and port given by dst_ip, dst_port.
  54. */
  55. proxy_desc_t* create_and_insert_proxy_desc(uint16_t id_no, uint16_t icmp_id,
  56. int sock, struct sockaddr_in *addr,
  57. uint32_t dst_ip, uint32_t dst_port,
  58. uint32_t init_state, uint32_t type) {
  59. proxy_desc_t *cur;
  60. pthread_mutex_lock(&chain_lock);
  61. if (num_tunnels >= opts.max_tunnels) {
  62. pt_log(kLog_info, "Discarding incoming connection - too many tunnels! Maximum count is %u (adjust with the -m switch).\n", opts.max_tunnels);
  63. if (sock)
  64. close(sock);
  65. pthread_mutex_unlock(&chain_lock);
  66. return 0;
  67. }
  68. num_tunnels++;
  69. pthread_mutex_unlock(&chain_lock);
  70. pt_log(kLog_debug, "Adding proxy desc to run loop. Type is %s. Will create socket: %s\n", (type == kUser_flag ? "user" : "proxy"), (sock ? "No" : "Yes"));
  71. cur = (proxy_desc_t *) calloc(1, sizeof(proxy_desc_t));
  72. cur->id_no = id_no;
  73. cur->dest_addr = *addr;
  74. cur->dst_ip = dst_ip;
  75. cur->dst_port = dst_port;
  76. cur->icmp_id = icmp_id;
  77. if (!sock) {
  78. cur->sock = socket(AF_INET, SOCK_STREAM, 0);
  79. memset(addr, 0, sizeof(struct sockaddr_in));
  80. addr->sin_port = htons((uint16_t)dst_port);
  81. addr->sin_addr.s_addr = dst_ip;
  82. addr->sin_family = AF_INET;
  83. /* Let's just assume success, shall we? */
  84. if (cur->sock >= 0 &&
  85. connect(cur->sock, (struct sockaddr*)addr, sizeof(struct sockaddr_in)) < 0)
  86. {
  87. pt_log(kLog_error, "Connect to %s:%d failed: %s\n", inet_ntoa(*(struct in_addr*)&addr->sin_addr.s_addr) , ntohs(addr->sin_port), strerror(errno));
  88. }
  89. }
  90. else
  91. cur->sock = sock;
  92. cur->state = init_state;
  93. cur->type_flag = type;
  94. if (cur->type_flag == kUser_flag)
  95. cur->pkt_type = kICMP_echo_request;
  96. else
  97. cur->pkt_type = (opts.unprivileged ? kICMP_echo_request : kICMP_echo_reply);
  98. cur->buf = (char *) malloc(icmp_receive_buf_len);
  99. cur->last_activity = time_as_double();
  100. cur->authenticated = 0;
  101. pthread_mutex_lock(&chain_lock);
  102. cur->next = chain;
  103. chain = cur;
  104. pthread_mutex_unlock(&chain_lock);
  105. cur->xfer.bytes_in = 0.0;
  106. cur->xfer.bytes_out = 0.0;
  107. return cur;
  108. }
  109. /* remove_proxy_desc: Removes the given proxy desc, freeing its resources.
  110. * Assumes that we hold the chain_lock.
  111. */
  112. void remove_proxy_desc(proxy_desc_t *cur, proxy_desc_t *prev) {
  113. int i;
  114. struct timeval tt;
  115. pt_log(kLog_debug, "Removing proxy descriptor.\n");
  116. /* Get a timestamp, for making an entry in the seq_expiry_tbl */
  117. gettimeofday(&tt, 0);
  118. seq_expiry_tbl[cur->id_no] = tt.tv_sec+(2*kAutomatic_close_timeout);
  119. /* Free resources associated with connection */
  120. if (cur->buf)
  121. free(cur->buf);
  122. cur->buf = 0;
  123. for (i=0;i<kPing_window_size;i++) {
  124. if (cur->send_ring[i].pkt)
  125. free(cur->send_ring[i].pkt);
  126. cur->send_ring[i].pkt = 0;
  127. if (cur->recv_ring[i])
  128. free(cur->recv_ring[i]);
  129. cur->recv_ring[i] = 0;
  130. }
  131. close(cur->sock);
  132. cur->sock = 0;
  133. /* Keep list up-to-date */
  134. if (prev)
  135. prev->next = cur->next;
  136. else
  137. chain = cur->next;
  138. if (cur->challenge)
  139. free(cur->challenge);
  140. free(cur);
  141. num_tunnels--;
  142. }
  143. forward_desc_t* create_fwd_desc(uint16_t seq_no, uint32_t data_len, char *data) {
  144. forward_desc_t *fwd_desc;
  145. fwd_desc = (forward_desc_t *) malloc(sizeof(forward_desc_t)+data_len);
  146. fwd_desc->seq_no = seq_no;
  147. fwd_desc->length = data_len;
  148. fwd_desc->remaining = data_len;
  149. if (data_len > 0)
  150. memcpy(fwd_desc->data, data, data_len);
  151. return fwd_desc;
  152. }
  153. /* queue_packet:
  154. * Creates an ICMP packet descriptor, and sends it. The packet descriptor is added
  155. * to the given send ring, for potential resends later on.
  156. */
  157. int queue_packet(int icmp_sock, uint8_t type, char *buf, int num_bytes,
  158. uint16_t id_no, uint16_t icmp_id, uint16_t *seq, icmp_desc_t ring[],
  159. int *insert_idx, int *await_send, uint32_t ip, uint32_t port,
  160. uint32_t state, struct sockaddr_in *dest_addr, uint16_t next_expected_seq,
  161. int *first_ack, uint16_t *ping_seq)
  162. {
  163. int pkt_len = sizeof(icmp_echo_packet_t) +
  164. sizeof(ping_tunnel_pkt_t) + num_bytes;
  165. int err = 0;
  166. icmp_echo_packet_t *pkt = 0;
  167. ping_tunnel_pkt_t *pt_pkt = 0;
  168. uint16_t ack_val = next_expected_seq - 1;
  169. if (pkt_len % 2)
  170. pkt_len++;
  171. pkt = (icmp_echo_packet_t *) calloc(1, pkt_len);
  172. /* ICMP Echo request or reply */
  173. pkt->type = type;
  174. /* Must be zero (non-zero requires root) */
  175. pkt->code = 0;
  176. pkt->identifier = htons(icmp_id);
  177. pkt->seq = htons(*ping_seq);
  178. pkt->checksum = 0;
  179. (*ping_seq)++;
  180. /* Add our information */
  181. pt_pkt = (ping_tunnel_pkt_t*)pkt->data;
  182. pt_pkt->magic = htonl(opts.magic);
  183. pt_pkt->dst_ip = ip;
  184. pt_pkt->dst_port = htonl(port);
  185. pt_pkt->ack = htonl(ack_val);
  186. pt_pkt->data_len = htonl(num_bytes);
  187. pt_pkt->state = htonl(state);
  188. pt_pkt->seq_no = htons(*seq);
  189. pt_pkt->id_no = htons(id_no);
  190. /* Copy user data */
  191. if (buf && num_bytes > 0)
  192. memcpy(pt_pkt->data, buf, num_bytes);
  193. pkt->checksum = htons(calc_icmp_checksum((uint16_t*)pkt, pkt_len));
  194. /* Send it! */
  195. pt_log(kLog_sendrecv, "Send: %d [%d] bytes [seq = %d] "
  196. "[type = %s] [ack = %d] [icmp = %d] [user = %s]\n",
  197. pkt_len, num_bytes, *seq, state_name[state & (~kFlag_mask)],
  198. ack_val, type, ((state & kUser_flag) == kUser_flag ? "yes" : "no"));
  199. err = sendto(icmp_sock, (const void*)pkt, pkt_len, 0,
  200. (struct sockaddr*)dest_addr, sizeof(struct sockaddr));
  201. if (err < 0) {
  202. pt_log(kLog_error, "Failed to send ICMP packet: %s\n", strerror(errno));
  203. free(pkt);
  204. return -1;
  205. }
  206. else if (err != pkt_len)
  207. pt_log(kLog_error, "WARNING WARNING, didn't send entire packet\n");
  208. /* Update sequence no's and so on */
  209. ring[*insert_idx].pkt = pkt;
  210. ring[*insert_idx].pkt_len = pkt_len;
  211. ring[*insert_idx].last_resend = time_as_double();
  212. ring[*insert_idx].seq_no = *seq;
  213. ring[*insert_idx].icmp_id = icmp_id;
  214. (*seq)++;
  215. if (!ring[*first_ack].pkt)
  216. *first_ack = *insert_idx;
  217. (*await_send)++;
  218. (*insert_idx)++;
  219. if (*insert_idx >= kPing_window_size)
  220. *insert_idx = 0;
  221. return 0;
  222. }
  223. /* send_packets:
  224. * Examines the passed-in ring, and forwards data in it over TCP.
  225. */
  226. uint32_t send_packets(forward_desc_t *ring[], int *xfer_idx, int *await_send, int *sock) {
  227. forward_desc_t *fwd_desc;
  228. int bytes, total = 0;
  229. while (*await_send > 0) {
  230. fwd_desc = ring[*xfer_idx];
  231. if (!fwd_desc)/* We haven't got this packet yet.. */
  232. break;
  233. if (fwd_desc->length > 0) {
  234. bytes = send(*sock, &fwd_desc->data[fwd_desc->length - fwd_desc->remaining],
  235. fwd_desc->remaining, 0);
  236. if (bytes < 0) {
  237. printf("Weirdness.\n");
  238. /* TODO: send close stuff */
  239. close(*sock);
  240. *sock = 0;
  241. break;
  242. }
  243. fwd_desc->remaining -= bytes;
  244. total += bytes;
  245. }
  246. if (!fwd_desc->remaining) {
  247. ring[*xfer_idx] = 0;
  248. free(fwd_desc);
  249. (*xfer_idx)++;
  250. (*await_send)--;
  251. if (*xfer_idx >= kPing_window_size)
  252. *xfer_idx = 0;
  253. }
  254. else
  255. break;
  256. }
  257. return total;
  258. }