1 /* $FreeBSD: src/contrib/ipfilter/ipsend/ipsend.c,v 1.4.2.5 2003/03/01 03:55:53 darrenr Exp $ */
2 /* $DragonFly: src/contrib/ipfilter/ipsend/ipsend.c,v 1.2 2003/06/17 04:24:02 dillon Exp $ */
4 * ipsend.c (C) 1995-1998 Darren Reed
6 * This was written to test what size TCP fragments would get through
7 * various TCP/IP packet filters, as used in IP firewalls. In certain
8 * conditions, enough of the TCP header is missing for unpredictable
9 * results unless the filter is aware that this can happen.
11 * See the IPFILTER.LICENCE file for details on licencing.
13 #if defined(__sgi) && (IRIX > 602)
14 # include <sys/ptimers.h>
21 #include <sys/param.h>
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <netinet/tcp.h>
30 #include <netinet/udp.h>
31 #include <netinet/ip_icmp.h>
33 #include <netinet/ip_var.h>
38 static const char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed";
39 static const char rcsid[] = "@(#)$Id: ipsend.c,v 2.2.2.6 2002/12/06 11:40:35 darrenr Exp $";
45 extern void iplang __P((FILE *));
50 char default_device[] = "eth0";
53 char default_device[] = "le0";
56 char default_device[] = "ln0";
59 char default_device[] = "ef0";
62 char default_device[] = "ec0";
64 char default_device[] = "lan0";
72 static void usage __P((char *));
73 static void do_icmp __P((ip_t *, char *));
74 int main __P((int, char **));
77 static void usage(prog)
80 fprintf(stderr, "Usage: %s [options] dest [flags]\n\
83 \t\t-i device\tSend out on this device\n\
84 \t\t-f fragflags\tcan set IP_MF or IP_DF\n\
85 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
86 \t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
87 \t\t-m mtu\t\tfake MTU to use when sending out\n\
88 \t\t-P protocol\tSet protocol by name\n\
89 \t\t-s src\t\tsource address for IP packet\n\
90 \t\t-T\t\tSet TCP protocol\n\
91 \t\t-t port\t\tdestination port\n\
92 \t\t-U\t\tSet UDP protocol\n\
93 \t\t-v\tverbose mode\n\
94 \t\t-w <window>\tSet the TCP window size\n\
96 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
99 \t\t-L filename\tUse IP language for sending packets\n\
100 \t\t-v\tverbose mode\n\
106 static void do_icmp(ip, args)
113 ip->ip_p = IPPROTO_ICMP;
114 ip->ip_len += sizeof(*ic);
115 ic = (struct icmp *)(ip + 1);
116 bzero((char *)ic, sizeof(*ic));
117 if (!(s = strchr(args, ',')))
119 fprintf(stderr, "ICMP args missing: ,\n");
123 ic->icmp_type = atoi(args);
124 ic->icmp_code = atoi(s);
125 if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
130 t = strtok(NULL, ",");
131 if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
133 fprintf(stderr,"Cant resolve %s\n", t);
136 if ((t = strtok(NULL, ",")))
138 if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
140 fprintf(stderr,"Cant resolve %s\n", t);
143 if ((t = strtok(NULL, ",")))
146 (char *)&ic->icmp_ip.ip_src) == -1)
148 fprintf(stderr,"Cant resolve %s\n", t);
157 int send_packets(dev, mtu, ip, gwip)
166 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
167 sport = ((struct tcpiphdr *)ip)->ti_sport;
168 wfd = initdevice(dev, sport, 5);
170 return send_packet(wfd, mtu, ip, gwip);
178 FILE *langfile = NULL;
183 char *name = argv[0], host[MAXHOSTNAMELEN + 1];
184 char *gateway = NULL, *dev = NULL;
185 char *src = NULL, *dst, *s;
186 int mtu = 1500, olen = 0, c, nonl = 0;
189 * 65535 is maximum packet size...you never know...
191 ip = (ip_t *)calloc(1, 65536);
192 ti = (struct tcpiphdr *)ip;
193 tcp = (tcphdr_t *)&ti->ti_sport;
194 ip->ip_len = sizeof(*ip);
195 ip->ip_hl = sizeof(*ip) >> 2;
197 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1)
204 fprintf(stderr, "Protocol already set: %d\n",
213 "Incorrect usage of -L option.\n");
216 if (!strcmp(optarg, "-"))
218 else if (!(langfile = fopen(optarg, "r"))) {
219 fprintf(stderr, "can't open file %s\n",
232 fprintf(stderr, "Protocol already set: %d\n",
236 if ((p = getprotobyname(optarg)))
237 ip->ip_p = p->p_proto;
239 fprintf(stderr, "Unknown protocol: %s\n",
247 fprintf(stderr, "Protocol already set: %d\n",
251 ip->ip_p = IPPROTO_TCP;
252 ip->ip_len += sizeof(tcphdr_t);
258 fprintf(stderr, "Protocol already set: %d\n",
262 ip->ip_p = IPPROTO_UDP;
263 ip->ip_len += sizeof(udphdr_t);
270 ip->ip_off = strtol(optarg, NULL, 0);
285 fprintf(stderr, "mtu must be > 28\n");
291 olen = buildopts(optarg, options, (ip->ip_hl - 5) << 2);
299 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
300 tcp->th_dport = htons(atoi(optarg));
307 if (ip->ip_p == IPPROTO_TCP)
308 tcp->th_win = atoi(optarg);
310 fprintf(stderr, "set protocol to TCP first\n");
313 fprintf(stderr, "Unknown option \"%c\"\n", c);
317 if (argc - optind < 1)
319 dst = argv[optind++];
323 gethostname(host, sizeof(host));
327 if (resolve(src, (char *)&ip->ip_src) == -1)
329 fprintf(stderr,"Cant resolve %s\n", src);
333 if (resolve(dst, (char *)&ip->ip_dst) == -1)
335 fprintf(stderr,"Cant resolve %s\n", dst);
341 else if (resolve(gateway, (char *)&gwip) == -1)
343 fprintf(stderr,"Cant resolve %s\n", gateway);
349 caddr_t ipo = (caddr_t)ip;
351 printf("Options: %d\n", olen);
352 ti = (struct tcpiphdr *)malloc(olen + ip->ip_len);
355 fprintf(stderr,"malloc failed\n");
359 bcopy((char *)ip, (char *)ti, sizeof(*ip));
361 ip->ip_hl = (olen >> 2);
362 bcopy(options, (char *)(ip + 1), olen);
363 bcopy((char *)tcp, (char *)(ip + 1) + olen, sizeof(*tcp));
365 bcopy((char *)ip, (char *)ipo, ip->ip_len);
367 tcp = (tcphdr_t *)((char *)(ip + 1) + olen);
370 if (ip->ip_p == IPPROTO_TCP)
371 for (s = argv[optind]; s && (c = *s); s++)
374 case 'S' : case 's' :
375 tcp->th_flags |= TH_SYN;
377 case 'A' : case 'a' :
378 tcp->th_flags |= TH_ACK;
380 case 'F' : case 'f' :
381 tcp->th_flags |= TH_FIN;
383 case 'R' : case 'r' :
384 tcp->th_flags |= TH_RST;
386 case 'P' : case 'p' :
387 tcp->th_flags |= TH_PUSH;
389 case 'U' : case 'u' :
390 tcp->th_flags |= TH_URG;
395 dev = default_device;
396 printf("Device: %s\n", dev);
397 printf("Source: %s\n", inet_ntoa(ip->ip_src));
398 printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
399 printf("Gateway: %s\n", inet_ntoa(gwip));
400 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
401 printf("Flags: %#x\n", tcp->th_flags);
402 printf("mtu: %d\n", mtu);
406 return do_socket(dev, mtu, ti, gwip);
408 return send_packets(dev, mtu, (ip_t *)ti, gwip);