1 /* $FreeBSD: src/contrib/ipfilter/ipsend/ipsend.c,v 1.4.2.5 2003/03/01 03:55:53 darrenr Exp $ */
3 * ipsend.c (C) 1995-1998 Darren Reed
5 * This was written to test what size TCP fragments would get through
6 * various TCP/IP packet filters, as used in IP firewalls. In certain
7 * conditions, enough of the TCP header is missing for unpredictable
8 * results unless the filter is aware that this can happen.
10 * See the IPFILTER.LICENCE file for details on licencing.
12 #if defined(__sgi) && (IRIX > 602)
13 # include <sys/ptimers.h>
20 #include <sys/param.h>
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <netinet/tcp.h>
29 #include <netinet/udp.h>
30 #include <netinet/ip_icmp.h>
32 #include <netinet/ip_var.h>
37 static const char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed";
38 static const char rcsid[] = "@(#)$Id: ipsend.c,v 2.2.2.6 2002/12/06 11:40:35 darrenr Exp $";
44 extern void iplang __P((FILE *));
49 char default_device[] = "eth0";
52 char default_device[] = "le0";
55 char default_device[] = "ln0";
58 char default_device[] = "ef0";
61 char default_device[] = "ec0";
63 char default_device[] = "lan0";
71 static void usage __P((char *));
72 static void do_icmp __P((ip_t *, char *));
73 int main __P((int, char **));
76 static void usage(prog)
79 fprintf(stderr, "Usage: %s [options] dest [flags]\n\
82 \t\t-i device\tSend out on this device\n\
83 \t\t-f fragflags\tcan set IP_MF or IP_DF\n\
84 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
85 \t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
86 \t\t-m mtu\t\tfake MTU to use when sending out\n\
87 \t\t-P protocol\tSet protocol by name\n\
88 \t\t-s src\t\tsource address for IP packet\n\
89 \t\t-T\t\tSet TCP protocol\n\
90 \t\t-t port\t\tdestination port\n\
91 \t\t-U\t\tSet UDP protocol\n\
92 \t\t-v\tverbose mode\n\
93 \t\t-w <window>\tSet the TCP window size\n\
95 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
98 \t\t-L filename\tUse IP language for sending packets\n\
99 \t\t-v\tverbose mode\n\
105 static void do_icmp(ip, args)
112 ip->ip_p = IPPROTO_ICMP;
113 ip->ip_len += sizeof(*ic);
114 ic = (struct icmp *)(ip + 1);
115 bzero((char *)ic, sizeof(*ic));
116 if (!(s = strchr(args, ',')))
118 fprintf(stderr, "ICMP args missing: ,\n");
122 ic->icmp_type = atoi(args);
123 ic->icmp_code = atoi(s);
124 if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
129 t = strtok(NULL, ",");
130 if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
132 fprintf(stderr,"Cant resolve %s\n", t);
135 if ((t = strtok(NULL, ",")))
137 if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
139 fprintf(stderr,"Cant resolve %s\n", t);
142 if ((t = strtok(NULL, ",")))
145 (char *)&ic->icmp_ip.ip_src) == -1)
147 fprintf(stderr,"Cant resolve %s\n", t);
156 int send_packets(dev, mtu, ip, gwip)
165 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
166 sport = ((struct tcpiphdr *)ip)->ti_sport;
167 wfd = initdevice(dev, sport, 5);
169 return send_packet(wfd, mtu, ip, gwip);
177 FILE *langfile = NULL;
182 char *name = argv[0], host[MAXHOSTNAMELEN + 1];
183 char *gateway = NULL, *dev = NULL;
184 char *src = NULL, *dst, *s;
185 int mtu = 1500, olen = 0, c, nonl = 0;
188 * 65535 is maximum packet size...you never know...
190 ip = (ip_t *)calloc(1, 65536);
191 ti = (struct tcpiphdr *)ip;
192 tcp = (tcphdr_t *)&ti->ti_sport;
193 ip->ip_len = sizeof(*ip);
194 ip->ip_hl = sizeof(*ip) >> 2;
196 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1)
203 fprintf(stderr, "Protocol already set: %d\n",
212 "Incorrect usage of -L option.\n");
215 if (!strcmp(optarg, "-"))
217 else if (!(langfile = fopen(optarg, "r"))) {
218 fprintf(stderr, "can't open file %s\n",
231 fprintf(stderr, "Protocol already set: %d\n",
235 if ((p = getprotobyname(optarg)))
236 ip->ip_p = p->p_proto;
238 fprintf(stderr, "Unknown protocol: %s\n",
246 fprintf(stderr, "Protocol already set: %d\n",
250 ip->ip_p = IPPROTO_TCP;
251 ip->ip_len += sizeof(tcphdr_t);
257 fprintf(stderr, "Protocol already set: %d\n",
261 ip->ip_p = IPPROTO_UDP;
262 ip->ip_len += sizeof(udphdr_t);
269 ip->ip_off = strtol(optarg, NULL, 0);
284 fprintf(stderr, "mtu must be > 28\n");
290 olen = buildopts(optarg, options, (ip->ip_hl - 5) << 2);
298 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
299 tcp->th_dport = htons(atoi(optarg));
306 if (ip->ip_p == IPPROTO_TCP)
307 tcp->th_win = atoi(optarg);
309 fprintf(stderr, "set protocol to TCP first\n");
312 fprintf(stderr, "Unknown option \"%c\"\n", c);
316 if (argc - optind < 1)
318 dst = argv[optind++];
322 gethostname(host, sizeof(host));
326 if (resolve(src, (char *)&ip->ip_src) == -1)
328 fprintf(stderr,"Cant resolve %s\n", src);
332 if (resolve(dst, (char *)&ip->ip_dst) == -1)
334 fprintf(stderr,"Cant resolve %s\n", dst);
340 else if (resolve(gateway, (char *)&gwip) == -1)
342 fprintf(stderr,"Cant resolve %s\n", gateway);
348 caddr_t ipo = (caddr_t)ip;
350 printf("Options: %d\n", olen);
351 ti = (struct tcpiphdr *)malloc(olen + ip->ip_len);
354 fprintf(stderr,"malloc failed\n");
358 bcopy((char *)ip, (char *)ti, sizeof(*ip));
360 ip->ip_hl = (olen >> 2);
361 bcopy(options, (char *)(ip + 1), olen);
362 bcopy((char *)tcp, (char *)(ip + 1) + olen, sizeof(*tcp));
364 bcopy((char *)ip, (char *)ipo, ip->ip_len);
366 tcp = (tcphdr_t *)((char *)(ip + 1) + olen);
369 if (ip->ip_p == IPPROTO_TCP)
370 for (s = argv[optind]; s && (c = *s); s++)
373 case 'S' : case 's' :
374 tcp->th_flags |= TH_SYN;
376 case 'A' : case 'a' :
377 tcp->th_flags |= TH_ACK;
379 case 'F' : case 'f' :
380 tcp->th_flags |= TH_FIN;
382 case 'R' : case 'r' :
383 tcp->th_flags |= TH_RST;
385 case 'P' : case 'p' :
386 tcp->th_flags |= TH_PUSH;
388 case 'U' : case 'u' :
389 tcp->th_flags |= TH_URG;
394 dev = default_device;
395 printf("Device: %s\n", dev);
396 printf("Source: %s\n", inet_ntoa(ip->ip_src));
397 printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
398 printf("Gateway: %s\n", inet_ntoa(gwip));
399 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
400 printf("Flags: %#x\n", tcp->th_flags);
401 printf("mtu: %d\n", mtu);
405 return do_socket(dev, mtu, ti, gwip);
407 return send_packets(dev, mtu, (ip_t *)ti, gwip);