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$";
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>
24 # include <netinet/ip_var.h>
27 # include <netinet/udp_var.h>
32 extern void iplang(FILE *);
36 char default_device[] = "le0";
39 static void usage(char *);
40 static void do_icmp(ip_t *, char *);
41 void udpcksum(ip_t *, struct udphdr *, int);
42 int main(int, char **);
45 static void usage(prog)
48 fprintf(stderr, "Usage: %s [options] dest [flags]\n\
51 \t\t-i device\tSend out on this device\n\
52 \t\t-f fragflags\tcan set IP_MF or IP_DF\n\
53 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
54 \t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
55 \t\t-m mtu\t\tfake MTU to use when sending out\n\
56 \t\t-P protocol\tSet protocol by name\n\
57 \t\t-s src\t\tsource address for IP packet\n\
58 \t\t-T\t\tSet TCP protocol\n\
59 \t\t-t port\t\tdestination port\n\
60 \t\t-U\t\tSet UDP protocol\n\
61 \t\t-v\tverbose mode\n\
62 \t\t-w <window>\tSet the TCP window size\n\
64 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
67 \t\t-L filename\tUse IP language for sending packets\n\
68 \t\t-v\tverbose mode\n\
74 static void do_icmp(ip, args)
81 ip->ip_p = IPPROTO_ICMP;
82 ip->ip_len += sizeof(*ic);
83 ic = (struct icmp *)(ip + 1);
84 bzero((char *)ic, sizeof(*ic));
85 if (!(s = strchr(args, ',')))
87 fprintf(stderr, "ICMP args missing: ,\n");
91 ic->icmp_type = atoi(args);
92 ic->icmp_code = atoi(s);
93 if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
98 t = strtok(NULL, ",");
99 if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
101 fprintf(stderr,"Cant resolve %s\n", t);
104 if ((t = strtok(NULL, ",")))
106 if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
108 fprintf(stderr,"Cant resolve %s\n", t);
111 if ((t = strtok(NULL, ",")))
114 (char *)&ic->icmp_ip.ip_src) == -1)
116 fprintf(stderr,"Cant resolve %s\n", t);
125 int send_packets(dev, mtu, ip, gwip)
133 wfd = initdevice(dev, 5);
136 return send_packet(wfd, mtu, ip, gwip);
140 udpcksum(ip_t *ip, struct udphdr *udp, int len)
155 ph.h.len = htons(len);
157 ph.h.proto = IPPROTO_UDP;
158 ph.h.src = ip->ip_src.s_addr;
159 ph.h.dst = ip->ip_dst.s_addr;
162 temp32 += opts[0] + opts[1] + opts[2] + opts[3] + opts[4] + opts[5];
163 temp32 = (temp32 >> 16) + (temp32 & 65535);
164 temp32 += (temp32 >> 16);
165 udp->uh_sum = temp32 & 65535;
166 udp->uh_sum = chksum((u_short *)udp, len);
167 if (udp->uh_sum == 0)
168 udp->uh_sum = 0xffff;
175 FILE *langfile = NULL;
180 char *name = argv[0], host[MAXHOSTNAMELEN + 1];
181 char *gateway = NULL, *dev = NULL;
182 char *src = NULL, *dst, *s;
183 int mtu = 1500, olen = 0, c, nonl = 0;
186 * 65535 is maximum packet size...you never know...
188 ip = (ip_t *)calloc(1, 65536);
189 tcp = (tcphdr_t *)(ip + 1);
190 udp = (udphdr_t *)tcp;
191 ip->ip_len = sizeof(*ip);
192 IP_HL_A(ip, sizeof(*ip) >> 2);
194 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1) {
201 fprintf(stderr, "Protocol already set: %d\n",
210 "Incorrect usage of -L option.\n");
213 if (!strcmp(optarg, "-"))
215 else if (!(langfile = fopen(optarg, "r"))) {
216 fprintf(stderr, "can't open file %s\n",
229 fprintf(stderr, "Protocol already set: %d\n",
233 if ((p = getprotobyname(optarg)))
234 ip->ip_p = p->p_proto;
236 fprintf(stderr, "Unknown protocol: %s\n",
244 fprintf(stderr, "Protocol already set: %d\n",
248 ip->ip_p = IPPROTO_TCP;
249 ip->ip_len += sizeof(tcphdr_t);
255 fprintf(stderr, "Protocol already set: %d\n",
259 ip->ip_p = IPPROTO_UDP;
260 ip->ip_len += sizeof(udphdr_t);
267 ip->ip_off = strtol(optarg, NULL, 0);
282 fprintf(stderr, "mtu must be > 28\n");
288 olen = buildopts(optarg, options, (IP_HL(ip) - 5) << 2);
296 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
297 tcp->th_dport = htons(atoi(optarg));
304 if (ip->ip_p == IPPROTO_TCP)
305 tcp->th_win = atoi(optarg);
307 fprintf(stderr, "set protocol to TCP first\n");
310 fprintf(stderr, "Unknown option \"%c\"\n", c);
315 if (argc - optind < 1)
317 dst = argv[optind++];
321 gethostname(host, sizeof(host));
325 if (resolve(src, (char *)&ip->ip_src) == -1)
327 fprintf(stderr,"Cant resolve %s\n", src);
331 if (resolve(dst, (char *)&ip->ip_dst) == -1)
333 fprintf(stderr,"Cant resolve %s\n", dst);
339 else if (resolve(gateway, (char *)&gwip) == -1)
341 fprintf(stderr,"Cant resolve %s\n", gateway);
350 printf("Options: %d\n", olen);
351 hlen = sizeof(*ip) + olen;
352 IP_HL_A(ip, hlen >> 2);
354 p = (char *)malloc(65536);
357 fprintf(stderr, "malloc failed\n");
361 bcopy(ip, p, sizeof(*ip));
362 bcopy(options, p + sizeof(*ip), olen);
363 bcopy(ip + 1, p + hlen, ip->ip_len - hlen);
366 if (ip->ip_p == IPPROTO_TCP) {
367 tcp = (tcphdr_t *)(p + hlen);
368 } else if (ip->ip_p == IPPROTO_UDP) {
369 udp = (udphdr_t *)(p + hlen);
373 if (ip->ip_p == IPPROTO_TCP)
374 for (s = argv[optind]; s && (c = *s); s++)
377 case 'S' : case 's' :
378 tcp->th_flags |= TH_SYN;
380 case 'A' : case 'a' :
381 tcp->th_flags |= TH_ACK;
383 case 'F' : case 'f' :
384 tcp->th_flags |= TH_FIN;
386 case 'R' : case 'r' :
387 tcp->th_flags |= TH_RST;
389 case 'P' : case 'p' :
390 tcp->th_flags |= TH_PUSH;
392 case 'U' : case 'u' :
393 tcp->th_flags |= TH_URG;
398 dev = default_device;
399 printf("Device: %s\n", dev);
400 printf("Source: %s\n", inet_ntoa(ip->ip_src));
401 printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
402 printf("Gateway: %s\n", inet_ntoa(gwip));
403 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
404 printf("Flags: %#x\n", tcp->th_flags);
405 printf("mtu: %d\n", mtu);
407 if (ip->ip_p == IPPROTO_UDP) {
409 udpcksum(ip, udp, ip->ip_len - (IP_HL(ip) << 2));
412 if (ip->ip_p == IPPROTO_TCP && tcp->th_dport)
413 return do_socket(dev, mtu, ip, gwip);
415 return send_packets(dev, mtu, ip, gwip);