File: it.c

package info (click to toggle)
icmptx 0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: ansic: 236; makefile: 26
file content (230 lines) | stat: -rw-r--r-- 7,384 bytes parent folder | download | duplicates (2)
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
/*
    This file is part of ICMPTX

    Original code copyright date unknown, edi/teso.
    Copyright (C) 2006       Thomer M. Gil <thomer@thomer.com>
    Copyright (C) 2008       John Plaxco <john@johnplaxco.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this ICMPTX.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "tun_dev.h"

/*
 * standard ICMP header
 */
struct icmp {
  u_int8_t	type;
  u_int8_t	code;
  u_int16_t	cksum;
  u_int16_t	id;
  u_int16_t	seq;
};

/*
 * standard IP header
 */
struct ip {
  unsigned int	ip_hl:4,
    ip_v:4;			
  unsigned char	ip_tos;			
  unsigned short	ip_len;			
  unsigned short	ip_id;			
  unsigned short	ip_off;			
  unsigned char	ip_ttl;			
  unsigned char	ip_p;			
  unsigned short	ip_sum;			
  struct	in_addr ip_src,ip_dst;
};

unsigned short in_cksum(unsigned short *, int);

/* int sock - ICMP socket used to communicate
   int proxy - 0 means send echo requests, 1 means send echo replies
   struct sockaddr_in *target - For the client, points to the server. For the server, the default place to send replies
   int tun_fd - Input/output file descriptor
   int packetsize - maximum size of ICMP data payload
   u_int16_t id - tunnel id field
*/
int icmp_tunnel(int sock, int proxy, struct sockaddr_in *target, int tun_fd, int packetsize, u_int16_t id) {
  int len, result, fromlen, num;
  char* packet;
  fd_set fs;
  unsigned char didSend, didReceive;
  struct icmp *icmp, *icmpr;
  struct timeval tv;
  struct sockaddr_in from;

  len = sizeof (struct icmp);

  if ((packet = malloc (len+packetsize)) == NULL) {
    fprintf(stderr, "Error allocating packet buffer");
    exit(1);
  }
  memset (packet, 0, len+packetsize);

  icmp = (struct icmp*)(packet);
  icmpr = (struct icmp*)(packet+sizeof(struct ip));

  /* here's the infinite loop that shovels packets back and forth while the tunnel's up */
  while (1) {
    FD_ZERO (&fs);
    FD_SET (tun_fd, &fs);
    FD_SET (sock, &fs);

    didSend = didReceive = 0;

    tv.tv_sec = 1;
    tv.tv_usec = 0;

    select (tun_fd>sock?tun_fd+1:sock+1, &fs, NULL, NULL, &tv);/* block until data's available in one direction or the other, or it's time to poll */

    /* data available on tunnel device, need to transmit over icmp */
    if (FD_ISSET(tun_fd, &fs)) {
      result = tun_read(tun_fd, packet+len, packetsize);
      if (!result) {/*eof*/
        return 0;
      } else if (result==-1) {
        perror("read");
        return -1;
      }
      icmp->type = proxy ? 0 : 8;/*echo response or echo request*/
      icmp->code = 0;
      icmp->id = id;/* mark the packet so the other end knows it's a tunnel packet */
      icmp->seq = 0;
      icmp->cksum = 0;
      icmp->cksum = in_cksum((unsigned short*)packet, len+result);
      result = sendto(sock, (char*)packet, len+result, 0, (struct sockaddr*)target, sizeof (struct sockaddr_in));
      if (result==-1) {
        perror ("sendto");
        return -1;
      }
      didSend = 1;
    }

    /* data available on socket from icmp, need to pass along to tunnel device */
    if (FD_ISSET(sock, &fs)) {
      fromlen = sizeof (struct sockaddr_in);
      num = recvfrom(sock, packet, len+packetsize, 0, (struct sockaddr*)&from, (socklen_t*) &fromlen);

      /* make the destination be the source of the most recently received packet (this can be dangerous) */
      memcpy(&(target->sin_addr.s_addr), &(from.sin_addr.s_addr), 4*sizeof(char));
      if (icmpr->id == id) {/*this filters out all of the other ICMP packets I don't care about*/
        tun_write(tun_fd, packet+sizeof(struct ip)+sizeof(struct icmp), num-sizeof(struct ip)-sizeof(struct icmp));
        didReceive = 1;
      } else if (icmpr->type == 8) {/* some normal ping request */
        icmpr->type = 0;/*echo response*/
        icmpr->code = 0;
        icmpr->id = icmpr->id;
        icmpr->seq = icmpr->seq;
        icmpr->cksum = 0;
        icmpr->cksum = in_cksum((unsigned short*)packet, num);
        result = sendto(sock, (char*)packet+sizeof(struct ip), len+num-sizeof(struct ip)-sizeof(struct icmp), 0, (struct sockaddr*)target, sizeof (struct sockaddr_in));
      }
    }    /* end of data available */

    /*
     * if we didn't send or receive anything, the select timed out
     * so lets send an echo request poll to the server (helps with
     * stateful firewalls)
     */
    if (!proxy && !didSend && !didReceive) {
      icmp->type = 8;/*echo request*/
      icmp->code = 0;
      icmp->id = id;/*mark the packet so the other end knows it's a tunnel packet*/
      icmp->seq = 0;
      icmp->cksum = 0;
      icmp->cksum = in_cksum((unsigned short*)packet, len);
      result = sendto(sock, (char*)packet, len, 0, (struct sockaddr*)target, sizeof (struct sockaddr_in));
      if (result==-1) {
        perror ("sendto");
        return -1;
      }
    }
  }  /* end of while(1) */

  return 0;
}

/*
 * this is the function that starts it all rolling
 * id - the id value for the icmp stream, to distinguish it from any other incoming ICMP packets
 * packetsize - I think this is the mtu value for the packets going across the tunnel, seems to be used in buffer allocations
 * isServer - 0 for client mode, 1 for server mode
 * serverNameOrIP - the server's host name or IP address
 * tun_fd - the file descriptor of the socket we read and write from
 */
int run_icmp_tunnel (int id, int packetsize, int isServer, char *serverNameOrIP, int tun_fd) {
  struct sockaddr_in target;
  struct in_addr inp;
  int s;

  if (!inet_aton(serverNameOrIP, &inp)) {
    struct hostent* he;
    if (!(he = gethostbyname (serverNameOrIP))) {
      herror ("gethostbyname");
      return -1;
    }
    memcpy (&target.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
  } else {
    target.sin_addr.s_addr = inp.s_addr;
  }
  target.sin_family = AF_INET;

  if ( (s = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
    perror ("socket");
    return -1;
  }

  icmp_tunnel(s, isServer, &target, tun_fd, packetsize, (u_int16_t) id);

  close(s);

  return 0;
}

/*
 * calculate the icmp checksum for the packet, including data
 */
unsigned short in_cksum (unsigned short *addr, int len) {
  int nleft = len;
  unsigned short *w = addr;
  int sum = 0;
  unsigned short answer = 0;
  while (nleft > 1) {
    sum += *w++;
    nleft -= 2;
  }
  if (nleft == 1) {
    *(unsigned char *) (&answer) = *(unsigned char *) w;
    sum += answer;
  }
  sum = (sum >> 16) + (sum & 0xffff);   /* add hi 16 to low 16 */
  sum += (sum >> 16);           /* add carry */
  answer = ~(sum & 0xffff);                /* truncate to 16 bits */
  return (answer);
}