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 __P((FILE *));
37 char default_device[] = "ln0";
40 char default_device[] = "ef0";
42 char default_device[] = "le0";
43 # endif /* __bsdi__ */
47 static void usage __P((char *));
48 static void do_icmp __P((ip_t *, char *));
49 void udpcksum(ip_t *, struct udphdr *, int);
50 int main __P((int, char **));
53 static void usage(prog)
56 fprintf(stderr, "Usage: %s [options] dest [flags]\n\
59 \t\t-i device\tSend out on this device\n\
60 \t\t-f fragflags\tcan set IP_MF or IP_DF\n\
61 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
62 \t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
63 \t\t-m mtu\t\tfake MTU to use when sending out\n\
64 \t\t-P protocol\tSet protocol by name\n\
65 \t\t-s src\t\tsource address for IP packet\n\
66 \t\t-T\t\tSet TCP protocol\n\
67 \t\t-t port\t\tdestination port\n\
68 \t\t-U\t\tSet UDP protocol\n\
69 \t\t-v\tverbose mode\n\
70 \t\t-w <window>\tSet the TCP window size\n\
72 fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
75 \t\t-L filename\tUse IP language for sending packets\n\
76 \t\t-v\tverbose mode\n\
82 static void do_icmp(ip, args)
89 ip->ip_p = IPPROTO_ICMP;
90 ip->ip_len += sizeof(*ic);
91 ic = (struct icmp *)(ip + 1);
92 bzero((char *)ic, sizeof(*ic));
93 if (!(s = strchr(args, ',')))
95 fprintf(stderr, "ICMP args missing: ,\n");
99 ic->icmp_type = atoi(args);
100 ic->icmp_code = atoi(s);
101 if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
106 t = strtok(NULL, ",");
107 if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
109 fprintf(stderr,"Cant resolve %s\n", t);
112 if ((t = strtok(NULL, ",")))
114 if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
116 fprintf(stderr,"Cant resolve %s\n", t);
119 if ((t = strtok(NULL, ",")))
122 (char *)&ic->icmp_ip.ip_src) == -1)
124 fprintf(stderr,"Cant resolve %s\n", t);
133 int send_packets(dev, mtu, ip, gwip)
141 wfd = initdevice(dev, 5);
144 return send_packet(wfd, mtu, ip, gwip);
148 udpcksum(ip_t *ip, struct udphdr *udp, int len)
163 ph.h.len = htons(len);
165 ph.h.proto = IPPROTO_UDP;
166 ph.h.src = ip->ip_src.s_addr;
167 ph.h.dst = ip->ip_dst.s_addr;
170 temp32 += opts[0] + opts[1] + opts[2] + opts[3] + opts[4] + opts[5];
171 temp32 = (temp32 >> 16) + (temp32 & 65535);
172 temp32 += (temp32 >> 16);
173 udp->uh_sum = temp32 & 65535;
174 udp->uh_sum = chksum((u_short *)udp, len);
175 if (udp->uh_sum == 0)
176 udp->uh_sum = 0xffff;
183 FILE *langfile = NULL;
188 char *name = argv[0], host[MAXHOSTNAMELEN + 1];
189 char *gateway = NULL, *dev = NULL;
190 char *src = NULL, *dst, *s;
191 int mtu = 1500, olen = 0, c, nonl = 0;
194 * 65535 is maximum packet size...you never know...
196 ip = (ip_t *)calloc(1, 65536);
197 tcp = (tcphdr_t *)(ip + 1);
198 udp = (udphdr_t *)tcp;
199 ip->ip_len = sizeof(*ip);
200 IP_HL_A(ip, sizeof(*ip) >> 2);
202 while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1) {
209 fprintf(stderr, "Protocol already set: %d\n",
218 "Incorrect usage of -L option.\n");
221 if (!strcmp(optarg, "-"))
223 else if (!(langfile = fopen(optarg, "r"))) {
224 fprintf(stderr, "can't open file %s\n",
237 fprintf(stderr, "Protocol already set: %d\n",
241 if ((p = getprotobyname(optarg)))
242 ip->ip_p = p->p_proto;
244 fprintf(stderr, "Unknown protocol: %s\n",
252 fprintf(stderr, "Protocol already set: %d\n",
256 ip->ip_p = IPPROTO_TCP;
257 ip->ip_len += sizeof(tcphdr_t);
263 fprintf(stderr, "Protocol already set: %d\n",
267 ip->ip_p = IPPROTO_UDP;
268 ip->ip_len += sizeof(udphdr_t);
275 ip->ip_off = strtol(optarg, NULL, 0);
290 fprintf(stderr, "mtu must be > 28\n");
296 olen = buildopts(optarg, options, (IP_HL(ip) - 5) << 2);
304 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
305 tcp->th_dport = htons(atoi(optarg));
312 if (ip->ip_p == IPPROTO_TCP)
313 tcp->th_win = atoi(optarg);
315 fprintf(stderr, "set protocol to TCP first\n");
318 fprintf(stderr, "Unknown option \"%c\"\n", c);
323 if (argc - optind < 1)
325 dst = argv[optind++];
329 gethostname(host, sizeof(host));
333 if (resolve(src, (char *)&ip->ip_src) == -1)
335 fprintf(stderr,"Cant resolve %s\n", src);
339 if (resolve(dst, (char *)&ip->ip_dst) == -1)
341 fprintf(stderr,"Cant resolve %s\n", dst);
347 else if (resolve(gateway, (char *)&gwip) == -1)
349 fprintf(stderr,"Cant resolve %s\n", gateway);
358 printf("Options: %d\n", olen);
359 hlen = sizeof(*ip) + olen;
360 IP_HL_A(ip, hlen >> 2);
362 p = (char *)malloc(65536);
365 fprintf(stderr, "malloc failed\n");
369 bcopy(ip, p, sizeof(*ip));
370 bcopy(options, p + sizeof(*ip), olen);
371 bcopy(ip + 1, p + hlen, ip->ip_len - hlen);
374 if (ip->ip_p == IPPROTO_TCP) {
375 tcp = (tcphdr_t *)(p + hlen);
376 } else if (ip->ip_p == IPPROTO_UDP) {
377 udp = (udphdr_t *)(p + hlen);
381 if (ip->ip_p == IPPROTO_TCP)
382 for (s = argv[optind]; s && (c = *s); s++)
385 case 'S' : case 's' :
386 tcp->th_flags |= TH_SYN;
388 case 'A' : case 'a' :
389 tcp->th_flags |= TH_ACK;
391 case 'F' : case 'f' :
392 tcp->th_flags |= TH_FIN;
394 case 'R' : case 'r' :
395 tcp->th_flags |= TH_RST;
397 case 'P' : case 'p' :
398 tcp->th_flags |= TH_PUSH;
400 case 'U' : case 'u' :
401 tcp->th_flags |= TH_URG;
406 dev = default_device;
407 printf("Device: %s\n", dev);
408 printf("Source: %s\n", inet_ntoa(ip->ip_src));
409 printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
410 printf("Gateway: %s\n", inet_ntoa(gwip));
411 if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
412 printf("Flags: %#x\n", tcp->th_flags);
413 printf("mtu: %d\n", mtu);
415 if (ip->ip_p == IPPROTO_UDP) {
417 udpcksum(ip, udp, ip->ip_len - (IP_HL(ip) << 2));
420 if (ip->ip_p == IPPROTO_TCP && tcp->th_dport)
421 return do_socket(dev, mtu, ip, gwip);
423 return send_packets(dev, mtu, ip, gwip);