-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipv4.c
462 lines (436 loc) · 11 KB
/
ipv4.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* ipv4.c - IPV4 code for sendip
* Author: Mike Ricketts <[email protected]>
* ChangeLog from 2.0 release:
* 26/11/2001 IP options
* 23/01/2002 Spelling fix (Dax Kelson <[email protected]>)
* 26/08/2002 Put tot_len field in host byte order on FreeBSD
* ChangeLog since 2.2 release:
* 24/11/2002 make it compile on archs that care about alignment
* ChangeLog since 2.3 release:
* 23/12/2002 fix bug with -iossr and -iolsr
* 20/01/2003 fix FreeBSD sendto(): invalid argument error. Again.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include "sendip_module.h"
#include "ipv4.h"
/* Character that identifies our options
*/
const char opt_char='i';
static void ipcsum(sendip_data *ip_hdr) {
ip_header *ip = (ip_header *)ip_hdr->data;
ip->check=0;
ip->check=csum((u_int16_t *)ip_hdr->data, ip_hdr->alloc_len);
}
/* This builds a source route format option from an argument */
static u_int8_t buildroute(char *data) {
char *data_out = data;
char *data_in = data;
char *next;
u_int8_t p='0';
int i;
/* First, the first 2 bytes give us the pointer */
for(i=0;i<2;i++) {
p<<=4;
if('0'<=*data_in && *data_in<='9') {
p+=*data_in-'0';
} else if('A'<=*data_in && *data_in<='F') {
p+=*data_in-'A'+0x0A;
} else if('a'<=*data_in && *data_in<='f') {
p+=*data_in-'a'+0x0a;
} else {
fprintf(stderr,"First 2 chars of record route options must be hex pointer\n");
return 0;
}
data_in++;
}
*(data_out++)=p;
/* Now loop through IP addresses... */
if(*data_in != ':') {
fprintf(stderr,"Third char of a record route option must be a :\n");
return 0;
}
data_in++;
next=data_in;
while(next) {
u_int32_t ip;
next=strchr(data_in,':');
if(next) {
*(next++)=0;
}
ip=inet_addr(data_in);
memcpy(data_out,&ip,4);
data_out+=4;
data_in = next;
}
return (data_out-data);
}
/* This bears an incredible resemblance to the TCP addoption function... */
static void addoption(u_int8_t copy, u_int8_t class, u_int8_t num,
u_int8_t len, u_int8_t *data,
sendip_data *pack) {
/* opt is copy flag (1bit) + class (2 bit) + number (5 bit) */
u_int8_t opt = ((copy&1)<<7) | ((class&3)<<5) | (num&31);
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));
ip_header *ip = malloc(sizeof(ip_header));
memset(ip,0,sizeof(ip_header));
ret->alloc_len = sizeof(ip_header);
ret->data = (void *)ip;
ret->modified=0;
return ret;
}
bool set_addr(char *hostname, sendip_data *pack) {
ip_header *ip = (ip_header *)pack->data;
struct hostent *host = gethostbyname2(hostname,AF_INET);
if(!(pack->modified & IP_MOD_SADDR)) {
ip->saddr = inet_addr("127.0.0.1");
}
if(!(pack->modified & IP_MOD_DADDR)) {
if(host==NULL) return FALSE;
if(host->h_length != sizeof(ip->daddr)) {
fprintf(stderr,"IPV4 destination address is the wrong size!!!");
return FALSE;
}
memcpy(&(ip->daddr),host->h_addr,host->h_length);
}
return TRUE;
}
bool do_opt(char *opt, char *arg, sendip_data *pack) {
ip_header *iph = (ip_header *)pack->data;
switch(opt[1]) {
case 's':
iph->saddr = inet_addr(arg);
pack->modified |= IP_MOD_SADDR;
break;
case 'd':
iph->daddr = inet_addr(arg);
pack->modified |= IP_MOD_DADDR;
break;
case 'h':
iph->header_len = (unsigned int)strtoul(arg, (char **)NULL, 0) & 0xF;
pack->modified |= IP_MOD_HEADERLEN;
break;
case 'v':
iph->version = (unsigned int)strtoul(arg, (char **)NULL, 0) & 0xF;
pack->modified |= IP_MOD_VERSION;
break;
case 'y':
iph->tos = (u_int8_t)strtoul(arg, (char **)NULL, 0);
pack->modified |= IP_MOD_TOS;
break;
case 'l':
iph->tot_len = (u_int16_t)strtoul(arg, (char **)NULL, 0);
#ifndef __FreeBSD__
#ifndef __FreeBSD
iph->tot_len = htons(iph->tot_len);
#endif
#endif
pack->modified |= IP_MOD_TOTLEN;
break;
case 'i':
iph->id = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= IP_MOD_ID;
break;
case 'f':
if(opt[2]) {
/* Note: *arg&1 is what we want because:
if arg=="0", *arg&1==0
if arg=="1", *arg&1==1
otherwise, it doesn't really matter...
*/
switch(opt[2]) {
case 'r':
iph->res=*arg&1;
pack->modified |= IP_MOD_RES;
break;
case 'd':
iph->df=*arg&1;
pack->modified |= IP_MOD_DF;
break;
case 'm':
iph->mf=*arg&1;
pack->modified |= IP_MOD_MF;
break;
}
} else {
IP_SET_FRAGOFF(iph,(u_int16_t)strtoul(arg, (char **)NULL, 0) &
(u_int16_t)0x1FFF);
pack->modified |= IP_MOD_FRAGOFF;
break;
}
break;
case 't':
iph->ttl = (u_int8_t)strtoul(arg, (char **)NULL, 0);
pack->modified |= IP_MOD_TTL;
break;
case 'p':
iph->protocol = (u_int8_t)strtoul(arg, (char **)NULL, 0);
pack->modified |= IP_MOD_PROTOCOL;
break;
case 'c':
iph->check = htons((u_int16_t)strtoul(arg, (char **)NULL, 0));
pack->modified |= IP_MOD_CHECK;
break;
case 'o':
/* IP options */
if(!strcmp(opt+2, "num")) {
/* Other options (auto length) */
u_int8_t cp, cls, num, len;
u_int8_t *data = malloc(strlen(arg)+2);
if(!data) {
fprintf(stderr,"Out of memory!\n");
return FALSE;
}
sprintf((char *)data,"0x%s",arg);
len = compact_string((char *)data);
cp=(*data&0x80)>>7;
cls=(*data&0x60)>>5;
num=(*data&0x1F);
addoption(cp,cls,num,len+1,data+1,pack);
free(data);
} else if(!strcmp(opt+2, "eol")) {
/* End of list */
addoption(0,0,0,1,NULL,pack);
} else if(!strcmp(opt+2, "nop")) {
/* NOP */
addoption(0,0,1,1,NULL,pack);
} else if(!strcmp(opt+2, "rr")) {
/* Record route
* Format is the same as for loose source route
*/
char *data = strdup(arg);
u_int8_t len;
if(!data) {
fprintf(stderr,"Out of memory!\n");
return FALSE;
}
len = buildroute(data);
if(len==0) {
free(data);
return FALSE;
} else {
addoption(0,0,7,len+2,(u_int8_t *)data,pack);
free(data);
}
} else if(!strcmp(opt+2, "ts")) {
/* Time stamp (RFC791)
* Format is:
* type (68, 8bit)
* length (automatic, 8bit)
* pointer (8bit)
* overflow (4bit), flag (4bit)
* if(flag) ip1 (32bit)
* timestamp1 (32bit)
* if(flag) ip2 (32bit)
* timestamp2 (32bit)
* ...
*/
char *data = strdup(arg);
char *data_in = data;
char *data_out = data;
char *next;
u_int8_t p=0;
int i;
if(data==NULL) {
fprintf(stderr,"Out of memory\n");
return FALSE;
}
/* First, get the pointer */
for(i=0;i<2;i++) {
p<<=4;
if('0'<=*data_in && *data_in<='9') {
p+=*data_in-'0';
} else if('A'<=*data_in && *data_in<='F') {
p+=*data_in-'A'+0x0A;
} else if('a'<=*data_in && *data_in<='f') {
p+=*data_in-'a'+0x0a;
} else {
fprintf(stderr,
"First 2 chars of IP timestamp must be hex pointer\n");
free(data);
return FALSE;
}
data_in++;
}
*(data_out++)=p;
/* Skip a : */
if(*(data_in++) != ':') {
fprintf(stderr,"Third char of IP timestamp must be :\n");
free(data);
return FALSE;
}
/* Get the overflow and skip a : */
next = strchr(data_in,':');
if(!next) {
fprintf(stderr,"IP timestamp option incorrect\n");
free(data);
return FALSE;
}
*(next++)=0;
i = atoi(data_in);
if(i > 15) {
fprintf(stderr,"IP timestamp overflow too big (max 15)\n");
free(data);
return FALSE;
}
*data_out=(u_int8_t)(i<<4);
data_in=next;
/* Now get the flag and skip a : */
next = strchr(data_in,':');
if(!next) {
fprintf(stderr,"IP timestamp option incorrect\n");
free(data);
return FALSE;
}
*(next++)=0;
i = atoi(data_in);
if(i > 15) {
fprintf(stderr,"IP timestamp flag too big (max 3)\n");
free(data);
return FALSE;
} else if(i!=0 && i!=1 && i!=3) {
fprintf(stderr,
"Warning: IP timestamp flag value %d not understood\n",i);
}
(*data_out)+=(u_int8_t)i;
data_in=next; data_out++;
/* Fill in (ip?) timestamp pairs */
while(next) {
u_int32_t ts;
if(i) { /* if we need IPs */
u_int32_t ip;
next=strchr(data_in,':');
if(!next) {
fprintf(stderr,"IP address in IP timestamp option must be followed by a timesamp\n");
free(data);
return FALSE;
}
*(next++)=0;
ip=inet_addr(data_in);
memcpy(data_out,&ip,4);
data_out+=4;
data_in = next;
}
next=strchr(next,':');
if(next) *(next++)=0;
ts = htonl(atoi(data_in));
memcpy(data_out,&ts,4);
data_out+=4;
data_in = next;
}
addoption(0,2,4,data_out-data+2,(u_int8_t *)data,pack);
free(data);
/* End of timestamp parsing */
} else if(!strcmp(opt+2, "lsr")) {
/* Loose Source Route
* Format is:
* type (131, 8bit)
* length (automatic, 8bit)
* pointer (>=4, 8bit)
* ip address0 (32bit)
* ip address1 (32bit)
* ...
*/
char *data = strdup(arg);
u_int8_t len;
if(!data) {
fprintf(stderr,"Out of memory!\n");
return FALSE;
}
len = buildroute(data);
if(len==0) {
free(data);
return FALSE;
} else {
addoption(1,0,3,len+2,(u_int8_t *)data,pack);
free(data);
}
} else if(!strcmp(opt+2, "sid")) {
/* Stream ID (RFC791) */
u_int16_t sid = htons(atoi(arg));
addoption(1,0,8,4,(u_int8_t *)&sid,pack);
} else if(!strcmp(opt+2, "ssr")) {
/* Strict Source Route
* Format is identical to loose source route
*/
char *data = strdup(arg);
u_int8_t len;
if(!data) {
fprintf(stderr,"Out of memory!\n");
return FALSE;
}
len = buildroute(data);
if(len==0) {
free(data);
return FALSE;
} else {
addoption(1,0,9,len+2,(u_int8_t *)data,pack);
free(data);
}
} else {
fprintf(stderr, "unsupported IP option %s val %s\n", opt, arg);
return FALSE;
}
break;
default:
usage_error("unknown IP option\n");
return FALSE;
break;
}
return TRUE;
}
bool finalize(char *hdrs, sendip_data *headers[], int index,
sendip_data *data, sendip_data *pack) {
ip_header *iph = (ip_header *)pack->data;
if(!(pack->modified & IP_MOD_VERSION)) {
iph->version=4;
}
if(!(pack->modified & IP_MOD_HEADERLEN)) {
iph->header_len=(pack->alloc_len+3)/4;
}
if(!(pack->modified & IP_MOD_TOTLEN)) {
iph->tot_len=pack->alloc_len + data->alloc_len;
#ifndef __FreeBSD__
#ifndef __FreeBSD
iph->tot_len = htons(iph->tot_len);
#endif
#endif
}
if(!(pack->modified & IP_MOD_ID)) {
iph->id = rand();
}
if(!(pack->modified & IP_MOD_TTL)) {
iph->ttl = 255;
}
if(!(pack->modified & IP_MOD_CHECK)) {
ipcsum(pack);
}
if(!(pack->modified&IP_MOD_PROTOCOL)) {
/* New default: actual type of following header */
iph->protocol = header_type(hdrs[index+1]);
}
return TRUE;
}
int num_opts() {
return sizeof(ip_opts)/sizeof(sendip_option);
}
sendip_option *get_opts() {
return ip_opts;
}
char get_optchar() {
return opt_char;
}