forked from rickettm/SendIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
372 lines (346 loc) · 10 KB
/
tcp.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* tcp.c - tcp support for sendip
* Author: Mike Ricketts <[email protected]>
* TCP options taken from code by Alexander Talos <[email protected]>
* ChangeLog since 2.0 release:
* 27/11/2001: Added -tonum option
* 02/12/2001: Only check 1 layer for enclosing IPV4 header
* ChangeLog since 2.1 release:
* 16/04/2002: Tidy up checksum code (like in icmp.c)
* 16/04/2002: Add support for TCP over IPV6 (code from armite <[email protected]>)
* 26/08/2002: Fix bug where tcp length was wrong with tcp options
* ChangeLog since 2.2 release:
* 24/11/2002: made it compile on archs that care about alignment
* ChangeLog since 2.4 release:
* 21/04/2003: fix errors found by valgrind
* 10/06/2003: fix -tonum (pointed out by Yaniv Kaul <[email protected]>)
* ChangeLog since 2.5 release:
* 02/03/2004: fix segfault in -tonum (pointed out by zamez)
*/
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include "sendip_module.h"
#include "tcp.h"
#include "ipv4.h"
#include "ipv6.h"
/* Character that identifies our options
*/
const char opt_char='t';
static void tcpcsum(sendip_data *ip_hdr, sendip_data *tcp_hdr,
sendip_data *data) {
tcp_header *tcp = (tcp_header *)tcp_hdr->data;
ip_header *ip = (ip_header *)ip_hdr->data;
u_int16_t *buf = malloc(12+tcp_hdr->alloc_len+data->alloc_len);
u_int8_t *tempbuf = (u_int8_t *)buf;
tcp->check=0;
if(tempbuf == NULL) {
fprintf(stderr,"Out of memory: TCP checksum not computed\n");
return;
}
/* Set up the pseudo header */
memcpy(tempbuf,&(ip->saddr),sizeof(u_int32_t));
memcpy(&(tempbuf[4]),&(ip->daddr),sizeof(u_int32_t));
tempbuf[8]=0;
tempbuf[9]=(u_int16_t)ip->protocol;
tempbuf[10]=(u_int16_t)((tcp_hdr->alloc_len+data->alloc_len)&0xFF00)>>8;
tempbuf[11]=(u_int16_t)((tcp_hdr->alloc_len+data->alloc_len)&0x00FF);
/* Copy the TCP header and data */
memcpy(tempbuf+12,tcp_hdr->data,tcp_hdr->alloc_len);
memcpy(tempbuf+12+tcp_hdr->alloc_len,data->data,data->alloc_len);
/* CheckSum it */
tcp->check = csum(buf,12+tcp_hdr->alloc_len+data->alloc_len);
free(buf);
}
static void tcp6csum(sendip_data *ipv6_hdr, sendip_data *tcp_hdr,
sendip_data *data) {
tcp_header *tcp = (tcp_header *)tcp_hdr->data;
ipv6_header *ipv6 = (ipv6_header *)ipv6_hdr->data;
struct ipv6_pseudo_hdr phdr;
u_int16_t *buf = malloc(sizeof(phdr)+tcp_hdr->alloc_len+data->alloc_len);
u_int8_t *tempbuf = (u_int8_t *)buf;
tcp->check=0;
if(tempbuf == NULL) {
fprintf(stderr,"Out of memory: TCP checksum not computed\n");
return;
}
/* Set up the pseudo header */
memset(&phdr,0,sizeof(phdr));
memcpy(&phdr.source,&ipv6->ip6_src,sizeof(struct in6_addr));
memcpy(&phdr.destination,&ipv6->ip6_dst,sizeof(struct in6_addr));
phdr.ulp_length=IPPROTO_TCP;
memcpy(tempbuf,&phdr,sizeof(phdr));
/* Copy the TCP header and data */
memcpy(tempbuf+sizeof(phdr),tcp_hdr->data,tcp_hdr->alloc_len);
memcpy(tempbuf+sizeof(phdr)+tcp_hdr->alloc_len,data->data,data->alloc_len);
/* CheckSum it */
tcp->check = csum(buf,sizeof(phdr)+tcp_hdr->alloc_len+data->alloc_len);
free(buf);
}
static void addoption(u_int8_t opt, u_int8_t len, u_int8_t *data,
sendip_data *pack) {
pack->data = realloc(pack->data, pack->alloc_len + len);
*((u_int8_t *)pack->data+pack->alloc_len) = opt;
if(len > 1)
*((u_int8_t *)pack->data+pack->alloc_len+1)=len;
if(len > 2)
memcpy((u_int8_t *)pack->data+pack->alloc_len+2,data,len-2);
pack->alloc_len += len;
}
sendip_data *initialize(void) {
sendip_data *ret = malloc(sizeof(sendip_data));
tcp_header *tcp = malloc(sizeof(tcp_header));
memset(tcp,0,sizeof(tcp_header));
ret->alloc_len = sizeof(tcp_header);
ret->data = (void *)tcp;
ret->modified=0;
return ret;
}
bool do_opt(char *opt, char *arg, sendip_data *pack) {
tcp_header *tcp = (tcp_header *)pack->data;
// opt[0]==t
switch(opt[1]) {
case 's':
tcp->source = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_SOURCE;
break;
case 'd':
tcp->dest = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_DEST;
break;
case 'n':
tcp->seq = htonl((u_int32_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_SEQ;
break;
case 'a':
tcp->ack_seq = htonl((u_int32_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_ACKSEQ;
if(!(pack->modified&TCP_MOD_ACK)) {
tcp->ack = 1;
pack->modified |= TCP_MOD_ACK;
}
break;
case 't':
tcp->off = (u_int16_t)strtoul(arg, (char **)NULL, 0) &0xF;
pack->modified |= TCP_MOD_OFF;
break;
case 'r':
tcp->res = (u_int16_t)(strtoul(arg, (char **)NULL, 0) & 0x000F);
pack->modified |= TCP_MOD_RES;
break;
case 'f':
switch(opt[2]) {
case 'e':
tcp->ecn=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_ECN;
break;
case 'c':
tcp->cwr=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_CWR;
break;
case 'u':
tcp->urg=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_URG;
break;
case 'a':
tcp->ack=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_ACK;
break;
case 'p':
tcp->psh=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_PSH;
break;
case 'r':
tcp->rst=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_RST;
break;
case 's':
tcp->syn=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_SYN;
break;
case 'f':
tcp->fin=(u_int16_t)*arg&1;
pack->modified |= TCP_MOD_FIN;
break;
default:
usage_error("TCP flag not known\n");
return FALSE;
}
break;
case 'w':
tcp->window = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_WINDOW;
break;
case 'c':
tcp->check = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_CHECK;
break;
case 'u':
tcp->urg_ptr = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= TCP_MOD_URGPTR;
if(!(pack->modified&TCP_MOD_URG)) {
tcp->urg = 1;
pack->modified |= TCP_MOD_URG;
}
break;
case 'o':
/* TCP OPTIONS */
if(!strcmp(opt+2, "num")) {
/* Other options (auto length) */
u_int8_t *data = malloc(strlen(arg)+3);
int len;
if(!data) {
fprintf(stderr,"Out of memory!\n");
return FALSE;
}
sprintf((char*)data,"0x%s",arg);
len = compact_string((char*)data);
if(len==1)
addoption(*data,1,NULL,pack);
else
addoption(*data,len+1,data+1,pack);
free(data);
} else if (!strcmp(opt+2, "eol")) {
/* End of options list RFC 793 kind 0, no length */
addoption(0,1,NULL,pack);
} else if (!strcmp(opt+2, "nop")) {
/* No op RFC 793 kind 1, no length */
addoption(1,1,NULL,pack);
} else if (!strcmp(opt+2, "mss")) {
/* Maximum segment size RFC 793 kind 2 */
u_int16_t mss=htons(atoi(arg));
addoption(2,4,(u_int8_t *)&mss,pack);
} else if (!strcmp(opt+2, "wscale")) {
/* Window scale rfc1323 */
u_int8_t wscale=atoi(arg);
addoption(3,3,&wscale,pack);
} else if (!strcmp(opt+2, "sackok")) {
/* Selective Acknowledge permitted rfc1323 */
addoption(4,2,NULL,pack);
} else if (!strcmp(opt+2, "sack")) {
/* Selective Acknowledge rfc1323 */
unsigned char *next;
u_int32_t le, re;
u_int8_t *comb, *c;
int count=0;
/* count the options */
next=(unsigned char *)arg;
while(next) {
next=(unsigned char *)strchr((char *)next,',');
count++;
if(next) next++;
}
comb = malloc(count*8);
c = comb;
next=(unsigned char *)arg;
while(*next) {
/* get left edge */
next=(unsigned char *)strchr(arg, ':');
if (!next) {
fprintf(stderr,
"Value in tcp sack option incorrect. Usage: \n");
fprintf(stderr,
" -tosack left:right[,left:right...]\n");
return FALSE;
}
*next++=0;
le=atoi(arg);
arg=(char *)next;
/* get right edge */
next=(unsigned char *)strchr(arg, ',');
if (!next)
next=(unsigned char *)arg-1; /* Finito - next points to \0 */
else
*next++=0;
re=atoi(arg);
arg=(char *)next;
le=htonl(le);
re=htonl(re);
memcpy(c, &le, 4);
memcpy(c+4, &re, 4);
c+=8;
}
addoption(5,count*8+2,comb,pack);
free(comb);
} else if (!strcmp(opt+2, "ts")) {
/* Timestamp rfc1323 */
u_int32_t tsval=0, tsecr=0;
u_int8_t comb[8];
if (2!=sscanf(arg, "%d:%d", &tsval, &tsecr)) {
fprintf(stderr,
"Invalid value for tcp timestamp option.\n");
fprintf(stderr,
"Usage: -tots tsval:tsecr\n");
return FALSE;
}
tsval=htonl(tsval);
memcpy(comb, &tsval, 4);
tsecr=htonl(tsecr);
memcpy(comb+4, &tsecr, 4);
addoption(8,10,comb,pack);
} else {
/* Unrecognized -to* */
fprintf(stderr, "unsupported TCP Option %s val %s\n",
opt, arg);
return FALSE;
}
break;
default:
usage_error("unknown TCP option\n");
return FALSE;
break;
}
return TRUE;
}
bool finalize(char *hdrs, sendip_data *headers[], sendip_data *data,
sendip_data *pack) {
tcp_header *tcp = (tcp_header *)pack->data;
/* Set relevant fields */
if(!(pack->modified&TCP_MOD_SEQ)) {
tcp->seq = (u_int32_t)rand();
}
if(!(pack->modified&TCP_MOD_OFF)) {
tcp->off = (u_int16_t)((pack->alloc_len+3)/4) & 0x0F;
}
if(!(pack->modified&TCP_MOD_SYN)) {
tcp->syn=1;
}
if(!(pack->modified&TCP_MOD_WINDOW)) {
tcp->window=htons((u_int16_t)65535);
}
/* Find enclosing IP header and do the checksum */
if(hdrs[strlen(hdrs)-1]=='i') {
int i = strlen(hdrs)-1;
if(!(headers[i]->modified&IP_MOD_PROTOCOL)) {
((ip_header *)(headers[i]->data))->protocol=IPPROTO_TCP;
headers[i]->modified |= IP_MOD_PROTOCOL;
}
if(!(pack->modified&TCP_MOD_CHECK)) {
tcpcsum(headers[i],pack,data);
}
} else if(hdrs[strlen(hdrs)-1]=='6') {
int i = strlen(hdrs)-1;
if(!(headers[i]->modified&IPV6_MOD_NXT)) {
((ipv6_header *)(headers[i]->data))->ip6_nxt=IPPROTO_TCP;
headers[i]->modified |= IPV6_MOD_NXT;
}
if(!(pack->modified&TCP_MOD_CHECK)) {
tcp6csum(headers[i],pack,data);
}
} else {
if(!(pack->modified&TCP_MOD_CHECK)) {
usage_error("TCP checksum not defined when TCP is not embedded in IP\n");
return FALSE;
}
}
return TRUE;
}
int num_opts() {
return sizeof(tcp_opts)/sizeof(sendip_option);
}
sendip_option *get_opts() {
return tcp_opts;
}
char get_optchar() {
return opt_char;
}