3 * ipsend.c (C) 1995-1998 Darren Reed
5 * See the IPFILTER.LICENCE file for details on licencing.
8 static const char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed";
9 static const char rcsid[] = "@(#)$Id: ipsend.c,v 2.8.2.2 2004/11/13 16:50:10 darrenr Exp $";
11 #include <sys/param.h>
12 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netinet/in_systm.h>
23 #include <netinet/ip.h>
25 # include <netinet/ip_var.h>
30 # include <netinet/udp_var.h>
36 extern void iplang __P((FILE *));
41 char default_device[] = "eth0";
44 char default_device[] = "ln0";
47 char default_device[] = "ef0";
50 char default_device[] = "ec0";
53 char default_device[] = "lan0";
55 char default_device[] = "le0";
58 # endif /* __bsdi__ */
63 static void usage __P((char *));
64 static void do_icmp __P((ip_t *, char *));
65 void udpcksum(ip_t *, struct udphdr *, int);
66 int main __P((int, char **));
69 static void usage(prog)
72 fprintf(stderr, "Usage: %s [options] dest [flags]\n\
75 \t\t-i device\tSend out on this device\n\
76 \t\t-f fragflags\tcan set IP_MF or IP_DF\n\
77 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
78 \t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
79 \t\t-m mtu\t\tfake MTU to use when sending out\n\
80 \t\t-P protocol\tSet protocol by name\n\
81 \t\t-s src\t\tsource address for IP packet\n\
82 \t\t-T\t\tSet TCP protocol\n\
83 \t\t-t port\t\tdestination port\n\
84 \t\t-U\t\tSet UDP protocol\n\
85 \t\t-v\tverbose mode\n\
86 \t\t-w <window>\tSet the TCP window size\n\
88 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
91 \t\t-L filename\tUse IP language for sending packets\n\
92 \t\t-v\tverbose mode\n\
98 static void do_icmp(ip, args)
105 ip->ip_p = IPPROTO_ICMP;
106 ip->ip_len += sizeof(*ic);
107 ic = (struct icmp *)(ip + 1);
108 bzero((char *)ic, sizeof(*ic));
109 if (!(s = strchr(args, ',')))
111 fprintf(stderr, "ICMP args missing: ,\n");
115 ic->icmp_type = atoi(args);
116 ic->icmp_code = atoi(s);
117 if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
122 t = strtok(NULL, ",");
123 if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
125 fprintf(stderr,"Cant resolve %s\n", t);
128 if ((t = strtok(NULL, ",")))
130 if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
132 fprintf(stderr,"Cant resolve %s\n", t);
135 if ((t = strtok(NULL, ",")))
138 (char *)&ic->icmp_ip.ip_src) == -1)
140 fprintf(stderr,"Cant resolve %s\n", t);
149 int send_packets(dev, mtu, ip, gwip)
157 wfd = initdevice(dev, 5);
158 return send_packet(wfd, mtu, ip, gwip);
162 udpcksum(ip_t *ip, struct udphdr *udp, int len)
177 ph.h.len = htons(len);
179 ph.h.proto = IPPROTO_UDP;
180 ph.h.src = ip->ip_src.s_addr;
181 ph.h.dst = ip->ip_dst.s_addr;
184 temp32 += opts[0] + opts[1] + opts[2] + opts[3] + opts[4] + opts[5];
185 temp32 = (temp32 >> 16) + (temp32 & 65535);
186 temp32 += (temp32 >> 16);
187 udp->uh_sum = temp32 & 65535;
188 udp->uh_sum = chksum((u_short *)udp, len);
189 if (udp->uh_sum == 0)
190 udp->uh_sum = 0xffff;
197 FILE *langfile = NULL;
202 char *name = argv[0], host[MAXHOSTNAMELEN + 1];
203 char *gateway = NULL, *dev = NULL;
204 char *src = NULL, *dst, *s;
205 int mtu = 1500, olen = 0, c, nonl = 0;
208 * 65535 is maximum packet size...you never know...
210 ip = (ip_t *)calloc(1, 65536);
211 tcp = (tcphdr_t *)(ip + 1);
212 udp = (udphdr_t *)tcp;
213 ip->ip_len = sizeof(*ip);
214 IP_HL_A(ip, sizeof(*ip) >> 2);
216 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1) {
223 fprintf(stderr, "Protocol already set: %d\n",
232 "Incorrect usage of -L option.\n");
235 if (!strcmp(optarg, "-"))
237 else if (!(langfile = fopen(optarg, "r"))) {
238 fprintf(stderr, "can't open file %s\n",
251 fprintf(stderr, "Protocol already set: %d\n",
255 if ((p = getprotobyname(optarg)))
256 ip->ip_p = p->p_proto;
258 fprintf(stderr, "Unknown protocol: %s\n",
266 fprintf(stderr, "Protocol already set: %d\n",
270 ip->ip_p = IPPROTO_TCP;
271 ip->ip_len += sizeof(tcphdr_t);
277 fprintf(stderr, "Protocol already set: %d\n",
281 ip->ip_p = IPPROTO_UDP;
282 ip->ip_len += sizeof(udphdr_t);
289 ip->ip_off = strtol(optarg, NULL, 0);
304 fprintf(stderr, "mtu must be > 28\n");
310 olen = buildopts(optarg, options, (IP_HL(ip) - 5) << 2);
318 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
319 tcp->th_dport = htons(atoi(optarg));
326 if (ip->ip_p == IPPROTO_TCP)
327 tcp->th_win = atoi(optarg);
329 fprintf(stderr, "set protocol to TCP first\n");
332 fprintf(stderr, "Unknown option \"%c\"\n", c);
337 if (argc - optind < 1)
339 dst = argv[optind++];
343 gethostname(host, sizeof(host));
347 if (resolve(src, (char *)&ip->ip_src) == -1)
349 fprintf(stderr,"Cant resolve %s\n", src);
353 if (resolve(dst, (char *)&ip->ip_dst) == -1)
355 fprintf(stderr,"Cant resolve %s\n", dst);
361 else if (resolve(gateway, (char *)&gwip) == -1)
363 fprintf(stderr,"Cant resolve %s\n", gateway);
372 printf("Options: %d\n", olen);
373 hlen = sizeof(*ip) + olen;
374 IP_HL_A(ip, hlen >> 2);
376 p = (char *)malloc(65536);
379 fprintf(stderr, "malloc failed\n");
383 bcopy(ip, p, sizeof(*ip));
384 bcopy(options, p + sizeof(*ip), olen);
385 bcopy(ip + 1, p + hlen, ip->ip_len - hlen);
388 if (ip->ip_p == IPPROTO_TCP) {
389 tcp = (tcphdr_t *)(p + hlen);
390 } else if (ip->ip_p == IPPROTO_UDP) {
391 udp = (udphdr_t *)(p + hlen);
395 if (ip->ip_p == IPPROTO_TCP)
396 for (s = argv[optind]; s && (c = *s); s++)
399 case 'S' : case 's' :
400 tcp->th_flags |= TH_SYN;
402 case 'A' : case 'a' :
403 tcp->th_flags |= TH_ACK;
405 case 'F' : case 'f' :
406 tcp->th_flags |= TH_FIN;
408 case 'R' : case 'r' :
409 tcp->th_flags |= TH_RST;
411 case 'P' : case 'p' :
412 tcp->th_flags |= TH_PUSH;
414 case 'U' : case 'u' :
415 tcp->th_flags |= TH_URG;
420 dev = default_device;
421 printf("Device: %s\n", dev);
422 printf("Source: %s\n", inet_ntoa(ip->ip_src));
423 printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
424 printf("Gateway: %s\n", inet_ntoa(gwip));
425 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
426 printf("Flags: %#x\n", tcp->th_flags);
427 printf("mtu: %d\n", mtu);
429 if (ip->ip_p == IPPROTO_UDP) {
431 udpcksum(ip, udp, ip->ip_len - (IP_HL(ip) << 2));
434 if (ip->ip_p == IPPROTO_TCP && tcp->th_dport)
435 return do_socket(dev, mtu, ip, gwip);
437 return send_packets(dev, mtu, ip, gwip);