2000-02-10 03:17:51 +00:00
|
|
|
/* $FreeBSD$ */
|
1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2000-02-10 03:17:51 +00:00
|
|
|
* ipsend.c (C) 1995-1998 Darren Reed
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 12:08:15 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
2005-04-25 18:20:15 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] = "@(#)ipsend.c 1.5 12/10/95 (C)1995 Darren Reed";
|
2013-08-11 14:28:45 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id$";
|
2002-03-19 11:48:16 +00:00
|
|
|
#endif
|
2000-02-10 03:17:51 +00:00
|
|
|
#include <sys/param.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in_systm.h>
|
2005-04-25 18:20:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/ip.h>
|
2005-04-25 18:20:15 +00:00
|
|
|
# include <netinet/ip_var.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "ipsend.h"
|
2005-04-25 18:20:15 +00:00
|
|
|
#include "ipf.h"
|
|
|
|
# include <netinet/udp_var.h>
|
2001-07-28 12:08:15 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
2021-01-26 06:24:28 +00:00
|
|
|
extern void iplang(FILE *);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
char options[68];
|
1997-11-16 04:52:19 +00:00
|
|
|
int opts;
|
2005-04-25 18:20:15 +00:00
|
|
|
char default_device[] = "le0";
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
|
2021-01-26 06:24:28 +00:00
|
|
|
static void usage(char *);
|
|
|
|
static void do_icmp(ip_t *, char *);
|
2004-06-21 22:53:03 +00:00
|
|
|
void udpcksum(ip_t *, struct udphdr *, int);
|
2021-01-26 06:24:28 +00:00
|
|
|
int main(int, char **);
|
1997-04-03 10:22:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void usage(prog)
|
2013-08-11 14:28:45 +00:00
|
|
|
char *prog;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [options] dest [flags]\n\
|
|
|
|
\toptions:\n\
|
1997-11-16 04:52:19 +00:00
|
|
|
\t\t-d\tdebug mode\n\
|
|
|
|
\t\t-i device\tSend out on this device\n\
|
1997-02-09 22:50:16 +00:00
|
|
|
\t\t-f fragflags\tcan set IP_MF or IP_DF\n\
|
|
|
|
\t\t-g gateway\tIP gateway to use if non-local dest.\n\
|
|
|
|
\t\t-I code,type[,gw[,dst[,src]]]\tSet ICMP protocol\n\
|
|
|
|
\t\t-m mtu\t\tfake MTU to use when sending out\n\
|
|
|
|
\t\t-P protocol\tSet protocol by name\n\
|
|
|
|
\t\t-s src\t\tsource address for IP packet\n\
|
|
|
|
\t\t-T\t\tSet TCP protocol\n\
|
|
|
|
\t\t-t port\t\tdestination port\n\
|
|
|
|
\t\t-U\t\tSet UDP protocol\n\
|
1997-11-16 04:52:19 +00:00
|
|
|
\t\t-v\tverbose mode\n\
|
|
|
|
\t\t-w <window>\tSet the TCP window size\n\
|
|
|
|
", prog);
|
|
|
|
fprintf(stderr, "Usage: %s [-dv] -L <filename>\n\
|
|
|
|
\toptions:\n\
|
|
|
|
\t\t-d\tdebug mode\n\
|
|
|
|
\t\t-L filename\tUse IP language for sending packets\n\
|
|
|
|
\t\t-v\tverbose mode\n\
|
1997-02-09 22:50:16 +00:00
|
|
|
", prog);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-20 17:07:20 +00:00
|
|
|
static
|
|
|
|
void do_icmp(ip_t *ip, char *args)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
struct icmp *ic;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
ip->ip_p = IPPROTO_ICMP;
|
|
|
|
ip->ip_len += sizeof(*ic);
|
|
|
|
ic = (struct icmp *)(ip + 1);
|
|
|
|
bzero((char *)ic, sizeof(*ic));
|
|
|
|
if (!(s = strchr(args, ',')))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ICMP args missing: ,\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*s++ = '\0';
|
|
|
|
ic->icmp_type = atoi(args);
|
|
|
|
ic->icmp_code = atoi(s);
|
|
|
|
if (ic->icmp_type == ICMP_REDIRECT && strchr(s, ','))
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
t = strtok(s, ",");
|
|
|
|
t = strtok(NULL, ",");
|
|
|
|
if (resolve(t, (char *)&ic->icmp_gwaddr) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", t);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ((t = strtok(NULL, ",")))
|
|
|
|
{
|
|
|
|
if (resolve(t, (char *)&ic->icmp_ip.ip_dst) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", t);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ((t = strtok(NULL, ",")))
|
|
|
|
{
|
|
|
|
if (resolve(t,
|
|
|
|
(char *)&ic->icmp_ip.ip_src) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", t);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-20 17:07:20 +00:00
|
|
|
int
|
|
|
|
send_packets(char *dev, int mtu, ip_t *ip, struct in_addr gwip)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2005-04-25 18:20:15 +00:00
|
|
|
int wfd;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2005-04-25 18:20:15 +00:00
|
|
|
wfd = initdevice(dev, 5);
|
2006-08-16 12:23:02 +00:00
|
|
|
if (wfd == -1)
|
2021-12-22 02:37:19 +00:00
|
|
|
return(-1);
|
|
|
|
return(send_packet(wfd, mtu, ip, gwip));
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
2004-06-21 22:53:03 +00:00
|
|
|
void
|
|
|
|
udpcksum(ip_t *ip, struct udphdr *udp, int len)
|
|
|
|
{
|
|
|
|
union pseudoh {
|
|
|
|
struct hdr {
|
|
|
|
u_short len;
|
|
|
|
u_char ttl;
|
|
|
|
u_char proto;
|
|
|
|
u_32_t src;
|
|
|
|
u_32_t dst;
|
|
|
|
} h;
|
|
|
|
u_short w[6];
|
|
|
|
} ph;
|
|
|
|
u_32_t temp32;
|
2005-04-25 18:20:15 +00:00
|
|
|
u_short *opts;
|
2004-06-21 22:53:03 +00:00
|
|
|
|
|
|
|
ph.h.len = htons(len);
|
|
|
|
ph.h.ttl = 0;
|
|
|
|
ph.h.proto = IPPROTO_UDP;
|
|
|
|
ph.h.src = ip->ip_src.s_addr;
|
|
|
|
ph.h.dst = ip->ip_dst.s_addr;
|
|
|
|
temp32 = 0;
|
|
|
|
opts = &ph.w[0];
|
|
|
|
temp32 += opts[0] + opts[1] + opts[2] + opts[3] + opts[4] + opts[5];
|
|
|
|
temp32 = (temp32 >> 16) + (temp32 & 65535);
|
|
|
|
temp32 += (temp32 >> 16);
|
|
|
|
udp->uh_sum = temp32 & 65535;
|
|
|
|
udp->uh_sum = chksum((u_short *)udp, len);
|
|
|
|
if (udp->uh_sum == 0)
|
|
|
|
udp->uh_sum = 0xffff;
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2021-12-20 17:07:20 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
FILE *langfile = NULL;
|
1997-02-09 22:50:16 +00:00
|
|
|
struct in_addr gwip;
|
|
|
|
tcphdr_t *tcp;
|
2004-06-21 22:53:03 +00:00
|
|
|
udphdr_t *udp;
|
1997-02-09 22:50:16 +00:00
|
|
|
ip_t *ip;
|
2000-02-10 03:17:51 +00:00
|
|
|
char *name = argv[0], host[MAXHOSTNAMELEN + 1];
|
|
|
|
char *gateway = NULL, *dev = NULL;
|
1997-11-16 04:52:19 +00:00
|
|
|
char *src = NULL, *dst, *s;
|
|
|
|
int mtu = 1500, olen = 0, c, nonl = 0;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 65535 is maximum packet size...you never know...
|
|
|
|
*/
|
|
|
|
ip = (ip_t *)calloc(1, 65536);
|
2005-04-25 18:20:15 +00:00
|
|
|
tcp = (tcphdr_t *)(ip + 1);
|
|
|
|
udp = (udphdr_t *)tcp;
|
1997-02-09 22:50:16 +00:00
|
|
|
ip->ip_len = sizeof(*ip);
|
2005-04-25 18:20:15 +00:00
|
|
|
IP_HL_A(ip, sizeof(*ip) >> 2);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2005-04-25 18:20:15 +00:00
|
|
|
while ((c = getopt(argc, argv, "I:L:P:TUdf:i:g:m:o:s:t:vw:")) != -1) {
|
1997-02-09 22:50:16 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'I' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Protocol already set: %d\n",
|
|
|
|
ip->ip_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do_icmp(ip, optarg);
|
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
case 'L' :
|
|
|
|
if (nonl) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Incorrect usage of -L option.\n");
|
|
|
|
usage(name);
|
|
|
|
}
|
|
|
|
if (!strcmp(optarg, "-"))
|
|
|
|
langfile = stdin;
|
|
|
|
else if (!(langfile = fopen(optarg, "r"))) {
|
|
|
|
fprintf(stderr, "can't open file %s\n",
|
|
|
|
optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
iplang(langfile);
|
2021-12-22 02:37:19 +00:00
|
|
|
return(0);
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'P' :
|
|
|
|
{
|
|
|
|
struct protoent *p;
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Protocol already set: %d\n",
|
|
|
|
ip->ip_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((p = getprotobyname(optarg)))
|
|
|
|
ip->ip_p = p->p_proto;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Unknown protocol: %s\n",
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'T' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Protocol already set: %d\n",
|
|
|
|
ip->ip_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ip->ip_p = IPPROTO_TCP;
|
|
|
|
ip->ip_len += sizeof(tcphdr_t);
|
|
|
|
break;
|
|
|
|
case 'U' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Protocol already set: %d\n",
|
|
|
|
ip->ip_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ip->ip_p = IPPROTO_UDP;
|
|
|
|
ip->ip_len += sizeof(udphdr_t);
|
|
|
|
break;
|
|
|
|
case 'd' :
|
1997-11-16 04:52:19 +00:00
|
|
|
opts |= OPT_DEBUG;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'f' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
ip->ip_off = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'g' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
gateway = optarg;
|
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
case 'i' :
|
|
|
|
nonl++;
|
|
|
|
dev = optarg;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'm' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
mtu = atoi(optarg);
|
|
|
|
if (mtu < 28)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "mtu must be > 28\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
2005-04-25 18:20:15 +00:00
|
|
|
olen = buildopts(optarg, options, (IP_HL(ip) - 5) << 2);
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 's' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
src = optarg;
|
|
|
|
break;
|
|
|
|
case 't' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
|
|
|
|
tcp->th_dport = htons(atoi(optarg));
|
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
case 'v' :
|
|
|
|
opts |= OPT_VERBOSE;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'w' :
|
1997-11-16 04:52:19 +00:00
|
|
|
nonl++;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p == IPPROTO_TCP)
|
|
|
|
tcp->th_win = atoi(optarg);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "set protocol to TCP first\n");
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
fprintf(stderr, "Unknown option \"%c\"\n", c);
|
|
|
|
usage(name);
|
|
|
|
}
|
2005-04-25 18:20:15 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (argc - optind < 1)
|
1997-02-09 22:50:16 +00:00
|
|
|
usage(name);
|
|
|
|
dst = argv[optind++];
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
{
|
|
|
|
gethostname(host, sizeof(host));
|
|
|
|
src = host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolve(src, (char *)&ip->ip_src) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", src);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolve(dst, (char *)&ip->ip_dst) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", dst);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gateway)
|
|
|
|
gwip = ip->ip_dst;
|
|
|
|
else if (resolve(gateway, (char *)&gwip) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Cant resolve %s\n", gateway);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (olen)
|
|
|
|
{
|
2004-06-21 22:53:03 +00:00
|
|
|
int hlen;
|
|
|
|
char *p;
|
1997-11-16 04:52:19 +00:00
|
|
|
|
|
|
|
printf("Options: %d\n", olen);
|
2004-06-21 22:53:03 +00:00
|
|
|
hlen = sizeof(*ip) + olen;
|
2005-04-25 18:20:15 +00:00
|
|
|
IP_HL_A(ip, hlen >> 2);
|
2004-06-21 22:53:03 +00:00
|
|
|
ip->ip_len += olen;
|
|
|
|
p = (char *)malloc(65536);
|
2005-04-25 18:20:15 +00:00
|
|
|
if (p == NULL)
|
2001-02-04 14:26:56 +00:00
|
|
|
{
|
2005-04-25 18:20:15 +00:00
|
|
|
fprintf(stderr, "malloc failed\n");
|
2001-02-04 14:26:56 +00:00
|
|
|
exit(2);
|
2005-04-25 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2004-06-21 22:53:03 +00:00
|
|
|
bcopy(ip, p, sizeof(*ip));
|
|
|
|
bcopy(options, p + sizeof(*ip), olen);
|
|
|
|
bcopy(ip + 1, p + hlen, ip->ip_len - hlen);
|
|
|
|
ip = (ip_t *)p;
|
2005-04-25 18:20:15 +00:00
|
|
|
|
2004-06-21 22:53:03 +00:00
|
|
|
if (ip->ip_p == IPPROTO_TCP) {
|
2005-04-25 18:20:15 +00:00
|
|
|
tcp = (tcphdr_t *)(p + hlen);
|
|
|
|
} else if (ip->ip_p == IPPROTO_UDP) {
|
|
|
|
udp = (udphdr_t *)(p + hlen);
|
2004-06-21 22:53:03 +00:00
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
}
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_p == IPPROTO_TCP)
|
1998-06-20 18:43:25 +00:00
|
|
|
for (s = argv[optind]; s && (c = *s); s++)
|
1997-02-09 22:50:16 +00:00
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 'S' : case 's' :
|
|
|
|
tcp->th_flags |= TH_SYN;
|
|
|
|
break;
|
|
|
|
case 'A' : case 'a' :
|
|
|
|
tcp->th_flags |= TH_ACK;
|
|
|
|
break;
|
|
|
|
case 'F' : case 'f' :
|
|
|
|
tcp->th_flags |= TH_FIN;
|
|
|
|
break;
|
|
|
|
case 'R' : case 'r' :
|
|
|
|
tcp->th_flags |= TH_RST;
|
|
|
|
break;
|
|
|
|
case 'P' : case 'p' :
|
|
|
|
tcp->th_flags |= TH_PUSH;
|
|
|
|
break;
|
|
|
|
case 'U' : case 'u' :
|
|
|
|
tcp->th_flags |= TH_URG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
dev = default_device;
|
|
|
|
printf("Device: %s\n", dev);
|
|
|
|
printf("Source: %s\n", inet_ntoa(ip->ip_src));
|
|
|
|
printf("Dest: %s\n", inet_ntoa(ip->ip_dst));
|
|
|
|
printf("Gateway: %s\n", inet_ntoa(gwip));
|
|
|
|
if (ip->ip_p == IPPROTO_TCP && tcp->th_flags)
|
|
|
|
printf("Flags: %#x\n", tcp->th_flags);
|
|
|
|
printf("mtu: %d\n", mtu);
|
|
|
|
|
2004-06-21 22:53:03 +00:00
|
|
|
if (ip->ip_p == IPPROTO_UDP) {
|
|
|
|
udp->uh_sum = 0;
|
2005-04-25 18:20:15 +00:00
|
|
|
udpcksum(ip, udp, ip->ip_len - (IP_HL(ip) << 2));
|
2004-06-21 22:53:03 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
#ifdef DOSOCKET
|
2004-06-21 22:53:03 +00:00
|
|
|
if (ip->ip_p == IPPROTO_TCP && tcp->th_dport)
|
2021-12-22 02:37:19 +00:00
|
|
|
return(do_socket(dev, mtu, ip, gwip));
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
2021-12-22 02:37:19 +00:00
|
|
|
return(send_packets(dev, mtu, ip, gwip));
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|