ipsend.c (26119) | ipsend.c (31183) |
---|---|
1/* | 1/* |
2 * ipsend.c (C) 1995 Darren Reed | 2 * ipsend.c (C) 1995-1997 Darren Reed |
3 * 4 * This was written to test what size TCP fragments would get through 5 * various TCP/IP packet filters, as used in IP firewalls. In certain 6 * conditions, enough of the TCP header is missing for unpredictable 7 * results unless the filter is aware that this can happen. 8 * | 3 * 4 * This was written to test what size TCP fragments would get through 5 * various TCP/IP packet filters, as used in IP firewalls. In certain 6 * conditions, enough of the TCP header is missing for unpredictable 7 * results unless the filter is aware that this can happen. 8 * |
9 * The author provides this program as-is, with no gaurantee for its 10 * suitability for any specific purpose. The author takes no responsibility 11 * for the misuse/abuse of this program and provides it for the sole purpose 12 * of testing packet filter policies. This file maybe distributed freely 13 * providing it is not modified and that this notice remains in tact. 14 * 15 * This was written and tested (successfully) on SunOS 4.1.x. | 9 * Redistribution and use in source and binary forms are permitted 10 * provided that this notice is preserved and due credit is given 11 * to the original author and the contributors. |
16 */ | 12 */ |
17#if !defined(lint) && defined(LIBC_SCCS) 18static char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed"; | 13#if !defined(lint) 14static const char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed"; 15static const char rcsid[] = "@(#)$Id: ipsend.c,v 2.0.2.19 1997/10/12 09:48:38 darrenr Exp $"; |
19#endif 20#include <stdio.h> 21#include <stdlib.h> 22#include <unistd.h> 23#include <netdb.h> 24#include <string.h> 25#include <sys/types.h> 26#include <sys/time.h> --- 4 unchanged lines hidden (view full) --- 31#include <netinet/ip.h> 32#include <netinet/tcp.h> 33#include <netinet/udp.h> 34#include <netinet/ip_icmp.h> 35#ifndef linux 36#include <netinet/ip_var.h> 37#endif 38#include "ipsend.h" | 16#endif 17#include <stdio.h> 18#include <stdlib.h> 19#include <unistd.h> 20#include <netdb.h> 21#include <string.h> 22#include <sys/types.h> 23#include <sys/time.h> --- 4 unchanged lines hidden (view full) --- 28#include <netinet/ip.h> 29#include <netinet/tcp.h> 30#include <netinet/udp.h> 31#include <netinet/ip_icmp.h> 32#ifndef linux 33#include <netinet/ip_var.h> 34#endif 35#include "ipsend.h" |
36#include "ipf.h" |
|
39 40 41extern char *optarg; 42extern int optind; | 37 38 39extern char *optarg; 40extern int optind; |
41extern void iplang __P((FILE *)); |
|
43 44char options[68]; | 42 43char options[68]; |
44int opts; |
|
45#ifdef linux 46char default_device[] = "eth0"; 47#else 48# ifdef sun 49char default_device[] = "le0"; 50# else 51# ifdef ultrix 52char default_device[] = "ln0"; 53# else 54# ifdef __bsdi__ 55char default_device[] = "ef0"; 56# else | 45#ifdef linux 46char default_device[] = "eth0"; 47#else 48# ifdef sun 49char default_device[] = "le0"; 50# else 51# ifdef ultrix 52char default_device[] = "ln0"; 53# else 54# ifdef __bsdi__ 55char default_device[] = "ef0"; 56# else |
57# ifdef __sgi 58char default_device[] = "ec0"; 59# else |
|
57char default_device[] = "lan0"; | 60char default_device[] = "lan0"; |
61# endif |
|
58# endif 59# endif 60# endif 61#endif 62 63 64static void usage __P((char *)); 65static void do_icmp __P((ip_t *, char *)); 66int main __P((int, char **)); 67 68 69static void usage(prog) 70char *prog; 71{ 72 fprintf(stderr, "Usage: %s [options] dest [flags]\n\ 73\toptions:\n\ | 62# endif 63# endif 64# endif 65#endif 66 67 68static void usage __P((char *)); 69static void do_icmp __P((ip_t *, char *)); 70int main __P((int, char **)); 71 72 73static void usage(prog) 74char *prog; 75{ 76 fprintf(stderr, "Usage: %s [options] dest [flags]\n\ 77\toptions:\n\ |
74\t\t-d device\tSend out on this device\n\ | 78\t\t-d\tdebug mode\n\ 79\t\t-i device\tSend out on this device\n\ |
75\t\t-f fragflags\tcan set IP_MF or IP_DF\n\ 76\t\t-g gateway\tIP gateway to use if non-local dest.\n\ 77\t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\ 78\t\t-m mtu\t\tfake MTU to use when sending out\n\ 79\t\t-P protocol\tSet protocol by name\n\ 80\t\t-s src\t\tsource address for IP packet\n\ 81\t\t-T\t\tSet TCP protocol\n\ 82\t\t-t port\t\tdestination port\n\ 83\t\t-U\t\tSet UDP protocol\n\ | 80\t\t-f fragflags\tcan set IP_MF or IP_DF\n\ 81\t\t-g gateway\tIP gateway to use if non-local dest.\n\ 82\t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\ 83\t\t-m mtu\t\tfake MTU to use when sending out\n\ 84\t\t-P protocol\tSet protocol by name\n\ 85\t\t-s src\t\tsource address for IP packet\n\ 86\t\t-T\t\tSet TCP protocol\n\ 87\t\t-t port\t\tdestination port\n\ 88\t\t-U\t\tSet UDP protocol\n\ |
89\t\t-v\tverbose mode\n\ 90\t\t-w <window>\tSet the TCP window size\n\ |
|
84", prog); | 91", prog); |
92 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\ 93\toptions:\n\ 94\t\t-d\tdebug mode\n\ 95\t\t-L filename\tUse IP language for sending packets\n\ 96\t\t-v\tverbose mode\n\ 97", prog); |
|
85 exit(1); 86} 87 88 | 98 exit(1); 99} 100 101 |
89void do_icmp(ip, args) | 102static void do_icmp(ip, args) |
90ip_t *ip; 91char *args; 92{ 93 struct icmp *ic; 94 char *s; 95 96 ip->ip_p = IPPROTO_ICMP; 97 ip->ip_len += sizeof(*ic); --- 55 unchanged lines hidden (view full) --- 153 return send_packet(wfd, mtu, ip, gwip); 154} 155 156 157int main(argc, argv) 158int argc; 159char **argv; 160{ | 103ip_t *ip; 104char *args; 105{ 106 struct icmp *ic; 107 char *s; 108 109 ip->ip_p = IPPROTO_ICMP; 110 ip->ip_len += sizeof(*ic); --- 55 unchanged lines hidden (view full) --- 166 return send_packet(wfd, mtu, ip, gwip); 167} 168 169 170int main(argc, argv) 171int argc; 172char **argv; 173{ |
174 FILE *langfile = NULL; |
|
161 struct tcpiphdr *ti; 162 struct in_addr gwip; 163 tcphdr_t *tcp; 164 ip_t *ip; 165 char *name = argv[0], host[64], *gateway = NULL, *dev = NULL; | 175 struct tcpiphdr *ti; 176 struct in_addr gwip; 177 tcphdr_t *tcp; 178 ip_t *ip; 179 char *name = argv[0], host[64], *gateway = NULL, *dev = NULL; |
166 char *src = NULL, *dst, c, *s; 167 int mtu = 1500, olen = 0; | 180 char *src = NULL, *dst, *s; 181 int mtu = 1500, olen = 0, c, nonl = 0; |
168 169 /* 170 * 65535 is maximum packet size...you never know... 171 */ 172 ip = (ip_t *)calloc(1, 65536); 173 ti = (struct tcpiphdr *)ip; 174 tcp = (tcphdr_t *)&ti->ti_sport; 175 ip->ip_len = sizeof(*ip); 176 ip->ip_hl = sizeof(*ip) >> 2; 177 | 182 183 /* 184 * 65535 is maximum packet size...you never know... 185 */ 186 ip = (ip_t *)calloc(1, 65536); 187 ti = (struct tcpiphdr *)ip; 188 tcp = (tcphdr_t *)&ti->ti_sport; 189 ip->ip_len = sizeof(*ip); 190 ip->ip_hl = sizeof(*ip) >> 2; 191 |
178 while ((c = (char)getopt(argc, argv, "IP:TUd:f:g:m:o:s:t:")) != -1) | 192 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1) |
179 switch (c) 180 { 181 case 'I' : | 193 switch (c) 194 { 195 case 'I' : |
196 nonl++; |
|
182 if (ip->ip_p) 183 { 184 fprintf(stderr, "Protocol already set: %d\n", 185 ip->ip_p); 186 break; 187 } 188 do_icmp(ip, optarg); 189 break; | 197 if (ip->ip_p) 198 { 199 fprintf(stderr, "Protocol already set: %d\n", 200 ip->ip_p); 201 break; 202 } 203 do_icmp(ip, optarg); 204 break; |
205 case 'L' : 206 if (nonl) { 207 fprintf(stderr, 208 "Incorrect usage of -L option.\n"); 209 usage(name); 210 } 211 if (!strcmp(optarg, "-")) 212 langfile = stdin; 213 else if (!(langfile = fopen(optarg, "r"))) { 214 fprintf(stderr, "can't open file %s\n", 215 optarg); 216 exit(1); 217 } 218 iplang(langfile); 219 return 0; |
|
190 case 'P' : 191 { 192 struct protoent *p; 193 | 220 case 'P' : 221 { 222 struct protoent *p; 223 |
224 nonl++; |
|
194 if (ip->ip_p) 195 { 196 fprintf(stderr, "Protocol already set: %d\n", 197 ip->ip_p); 198 break; 199 } 200 if ((p = getprotobyname(optarg))) 201 ip->ip_p = p->p_proto; 202 else 203 fprintf(stderr, "Unknown protocol: %s\n", 204 optarg); 205 break; 206 } 207 case 'T' : | 225 if (ip->ip_p) 226 { 227 fprintf(stderr, "Protocol already set: %d\n", 228 ip->ip_p); 229 break; 230 } 231 if ((p = getprotobyname(optarg))) 232 ip->ip_p = p->p_proto; 233 else 234 fprintf(stderr, "Unknown protocol: %s\n", 235 optarg); 236 break; 237 } 238 case 'T' : |
239 nonl++; |
|
208 if (ip->ip_p) 209 { 210 fprintf(stderr, "Protocol already set: %d\n", 211 ip->ip_p); 212 break; 213 } 214 ip->ip_p = IPPROTO_TCP; 215 ip->ip_len += sizeof(tcphdr_t); 216 break; 217 case 'U' : | 240 if (ip->ip_p) 241 { 242 fprintf(stderr, "Protocol already set: %d\n", 243 ip->ip_p); 244 break; 245 } 246 ip->ip_p = IPPROTO_TCP; 247 ip->ip_len += sizeof(tcphdr_t); 248 break; 249 case 'U' : |
250 nonl++; |
|
218 if (ip->ip_p) 219 { 220 fprintf(stderr, "Protocol already set: %d\n", 221 ip->ip_p); 222 break; 223 } 224 ip->ip_p = IPPROTO_UDP; 225 ip->ip_len += sizeof(udphdr_t); 226 break; 227 case 'd' : | 251 if (ip->ip_p) 252 { 253 fprintf(stderr, "Protocol already set: %d\n", 254 ip->ip_p); 255 break; 256 } 257 ip->ip_p = IPPROTO_UDP; 258 ip->ip_len += sizeof(udphdr_t); 259 break; 260 case 'd' : |
228 dev = optarg; | 261 opts |= OPT_DEBUG; |
229 break; 230 case 'f' : | 262 break; 263 case 'f' : |
264 nonl++; |
|
231 ip->ip_off = strtol(optarg, NULL, 0); 232 break; 233 case 'g' : | 265 ip->ip_off = strtol(optarg, NULL, 0); 266 break; 267 case 'g' : |
268 nonl++; |
|
234 gateway = optarg; 235 break; | 269 gateway = optarg; 270 break; |
271 case 'i' : 272 nonl++; 273 dev = optarg; 274 break; |
|
236 case 'm' : | 275 case 'm' : |
276 nonl++; |
|
237 mtu = atoi(optarg); 238 if (mtu < 28) 239 { 240 fprintf(stderr, "mtu must be > 28\n"); 241 exit(1); 242 } 243 break; 244 case 'o' : | 277 mtu = atoi(optarg); 278 if (mtu < 28) 279 { 280 fprintf(stderr, "mtu must be > 28\n"); 281 exit(1); 282 } 283 break; 284 case 'o' : |
245 olen = optname(optarg, options); | 285 nonl++; 286 olen = buildopts(optarg, options, (ip->ip_hl - 5) << 2); |
246 break; 247 case 's' : | 287 break; 288 case 's' : |
289 nonl++; |
|
248 src = optarg; 249 break; 250 case 't' : | 290 src = optarg; 291 break; 292 case 't' : |
293 nonl++; |
|
251 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) 252 tcp->th_dport = htons(atoi(optarg)); 253 break; | 294 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) 295 tcp->th_dport = htons(atoi(optarg)); 296 break; |
297 case 'v' : 298 opts |= OPT_VERBOSE; 299 break; |
|
254 case 'w' : | 300 case 'w' : |
301 nonl++; |
|
255 if (ip->ip_p == IPPROTO_TCP) 256 tcp->th_win = atoi(optarg); 257 else 258 fprintf(stderr, "set protocol to TCP first\n"); 259 break; 260 default : 261 fprintf(stderr, "Unknown option \"%c\"\n", c); 262 usage(name); 263 } 264 | 302 if (ip->ip_p == IPPROTO_TCP) 303 tcp->th_win = atoi(optarg); 304 else 305 fprintf(stderr, "set protocol to TCP first\n"); 306 break; 307 default : 308 fprintf(stderr, "Unknown option \"%c\"\n", c); 309 usage(name); 310 } 311 |
265 if (argc - optind < 2) | 312 if (argc - optind < 1) |
266 usage(name); 267 dst = argv[optind++]; 268 269 if (!src) 270 { 271 gethostname(host, sizeof(host)); 272 src = host; 273 } --- 13 unchanged lines hidden (view full) --- 287 if (!gateway) 288 gwip = ip->ip_dst; 289 else if (resolve(gateway, (char *)&gwip) == -1) 290 { 291 fprintf(stderr,"Cant resolve %s\n", gateway); 292 exit(2); 293 } 294 | 313 usage(name); 314 dst = argv[optind++]; 315 316 if (!src) 317 { 318 gethostname(host, sizeof(host)); 319 src = host; 320 } --- 13 unchanged lines hidden (view full) --- 334 if (!gateway) 335 gwip = ip->ip_dst; 336 else if (resolve(gateway, (char *)&gwip) == -1) 337 { 338 fprintf(stderr,"Cant resolve %s\n", gateway); 339 exit(2); 340 } 341 |
342 if (olen) 343 { 344 caddr_t ipo = (caddr_t)ip; 345 346 printf("Options: %d\n", olen); 347 ti = (struct tcpiphdr *)malloc(olen + ip->ip_len); 348 bcopy((char *)ip, (char *)ti, sizeof(*ip)); 349 ip = (ip_t *)ti; 350 ip->ip_hl = (olen >> 2); 351 bcopy(options, (char *)(ip + 1), olen); 352 bcopy((char *)tcp, (char *)(ip + 1) + olen, sizeof(*tcp)); 353 ip->ip_len += olen; 354 bcopy((char *)ip, (char *)ipo, ip->ip_len); 355 ip = (ip_t *)ipo; 356 tcp = (tcphdr_t *)((char *)(ip + 1) + olen); 357 } 358 |
|
295 if (ip->ip_p == IPPROTO_TCP) 296 for (s = argv[optind]; (c = *s); s++) 297 switch(c) 298 { 299 case 'S' : case 's' : 300 tcp->th_flags |= TH_SYN; 301 break; 302 case 'A' : case 'a' : --- 18 unchanged lines hidden (view full) --- 321 printf("Device: %s\n", dev); 322 printf("Source: %s\n", inet_ntoa(ip->ip_src)); 323 printf("Dest: %s\n", inet_ntoa(ip->ip_dst)); 324 printf("Gateway: %s\n", inet_ntoa(gwip)); 325 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags) 326 printf("Flags: %#x\n", tcp->th_flags); 327 printf("mtu: %d\n", mtu); 328 | 359 if (ip->ip_p == IPPROTO_TCP) 360 for (s = argv[optind]; (c = *s); s++) 361 switch(c) 362 { 363 case 'S' : case 's' : 364 tcp->th_flags |= TH_SYN; 365 break; 366 case 'A' : case 'a' : --- 18 unchanged lines hidden (view full) --- 385 printf("Device: %s\n", dev); 386 printf("Source: %s\n", inet_ntoa(ip->ip_src)); 387 printf("Dest: %s\n", inet_ntoa(ip->ip_dst)); 388 printf("Gateway: %s\n", inet_ntoa(gwip)); 389 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags) 390 printf("Flags: %#x\n", tcp->th_flags); 391 printf("mtu: %d\n", mtu); 392 |
329 if (olen) 330 { 331 printf("Options: %d\n", olen); 332 ti = (struct tcpiphdr *)malloc(olen + ip->ip_len); 333 bcopy((char *)ip, (char *)ti, sizeof(*ip)); 334 ip = (ip_t *)ti; 335 ip->ip_hl += (olen >> 2); 336 bcopy(options, (char *)(ip + 1), olen); 337 bcopy((char *)tcp, (char *)(ip + 1) + olen, sizeof(*tcp)); 338 tcp = (tcphdr_t *)((char *)(ip + 1) + olen); 339 ip->ip_len += olen; 340 } 341 | |
342#ifdef DOSOCKET 343 if (tcp->th_dport) 344 return do_socket(dev, mtu, ti, gwip); 345#endif 346 return send_packets(dev, mtu, (ip_t *)ti, gwip); 347} | 393#ifdef DOSOCKET 394 if (tcp->th_dport) 395 return do_socket(dev, mtu, ti, gwip); 396#endif 397 return send_packets(dev, mtu, (ip_t *)ti, gwip); 398} |