diff options
Diffstat (limited to 'usr.sbin/ipsend/common')
-rw-r--r-- | usr.sbin/ipsend/common/44arp.c | 111 | ||||
-rw-r--r-- | usr.sbin/ipsend/common/ip.c | 349 | ||||
-rw-r--r-- | usr.sbin/ipsend/common/iplang.h | 56 | ||||
-rw-r--r-- | usr.sbin/ipsend/common/ipsend.h | 71 | ||||
-rw-r--r-- | usr.sbin/ipsend/common/sbpf.c | 144 | ||||
-rw-r--r-- | usr.sbin/ipsend/common/sock.c | 410 |
6 files changed, 0 insertions, 1141 deletions
diff --git a/usr.sbin/ipsend/common/44arp.c b/usr.sbin/ipsend/common/44arp.c deleted file mode 100644 index 751a03549d3..00000000000 --- a/usr.sbin/ipsend/common/44arp.c +++ /dev/null @@ -1,111 +0,0 @@ -/* $OpenBSD: 44arp.c,v 1.3 2001/01/17 06:01:24 fgsch Exp $ */ - -/* - * Based upon 4.4BSD's /usr/sbin/arp - */ -#include <unistd.h> -#include <string.h> -#include <stdlib.h> -#include <sys/param.h> -#include <sys/file.h> -#include <sys/socket.h> -#include <sys/sysctl.h> -#include <net/if.h> -#include <net/if_dl.h> -#include <net/if_types.h> -#include <net/route.h> -#include <netinet/in.h> -#include <netinet/if_ether.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <errno.h> -#include <nlist.h> -#include <stdio.h> -#include <netinet/in.h> -#include <netinet/ip_var.h> -#include <netinet/tcp.h> -#if __FreeBSD_version >= 300000 -# include <net/if_var.h> -#endif -#include "ipsend.h" -#include "iplang.h" - - -/* - * lookup host and return - * its IP address in address - * (4 bytes) - */ -int resolve(host, address) -char *host, *address; -{ - struct hostent *hp; - u_long add; - - add = inet_addr(host); - if (add == -1) - { - if (!(hp = gethostbyname(host))) - { - fprintf(stderr, "unknown host: %s\n", host); - return -1; - } - bcopy((char *)hp->h_addr, (char *)address, 4); - return 0; - } - bcopy((char*)&add, address, 4); - return 0; -} - - -int arp(addr, eaddr) -char *addr, *eaddr; -{ - int mib[6]; - size_t needed; - char *lim, *buf, *next; - struct rt_msghdr *rtm; - struct sockaddr_inarp *sin; - struct sockaddr_dl *sdl; - -#ifdef IPSEND - if (arp_getipv4(addr, ether) == 0) - return 0; -#endif - - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = AF_INET; - mib[4] = NET_RT_FLAGS; - mib[5] = RTF_LLINFO; - if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) - { - perror("route-sysctl-estimate"); - exit(-1); - } - if ((buf = malloc(needed)) == NULL) - { - perror("malloc"); - exit(-1); - } - if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) - { - perror("actual retrieval of routing table"); - exit(-1); - } - lim = buf + needed; - for (next = buf; next < lim; next += rtm->rtm_msglen) - { - rtm = (struct rt_msghdr *)next; - sin = (struct sockaddr_inarp *)(rtm + 1); - sdl = (struct sockaddr_dl *)(sin + 1); - if (addr && !bcmp(addr, (char *)&sin->sin_addr, - sizeof(struct in_addr))) - { - bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen); - return 0; - } - } - return -1; -} diff --git a/usr.sbin/ipsend/common/ip.c b/usr.sbin/ipsend/common/ip.c deleted file mode 100644 index 86bd2fef8b9..00000000000 --- a/usr.sbin/ipsend/common/ip.c +++ /dev/null @@ -1,349 +0,0 @@ -/* $OpenBSD: ip.c,v 1.6 2001/01/17 06:01:24 fgsch Exp $ */ - -/* - * ip.c (C) 1995-1998 Darren Reed - * - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and due credit is given - * to the original author and the contributors. - */ -#if !defined(lint) -static const char sccsid[] = "%W% %G% (C)1995"; -static const char rcsid[] = "@(#)$IPFilter: ip.c,v 2.1 1999/08/04 17:31:04 darrenr Exp $"; -#endif -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <sys/types.h> -#include <netinet/in_systm.h> -#include <sys/socket.h> -#include <net/if.h> -#include <netinet/in.h> -#include <netinet/ip.h> -#include <netinet/tcp.h> -#include <netinet/udp.h> -#include <netinet/ip_icmp.h> -#include <sys/param.h> -#ifndef linux -# include <netinet/if_ether.h> -# include <netinet/ip_var.h> -# if __FreeBSD_version >= 300000 -# include <net/if_var.h> -# endif -#endif -#include "ipsend.h" - - -static char *ipbuf = NULL, *ethbuf = NULL; - - -u_short chksum(buf,len) -u_short *buf; -int len; -{ - u_long sum = 0; - int nwords = len >> 1; - - for(; nwords > 0; nwords--) - sum += *buf++; - sum = (sum>>16) + (sum & 0xffff); - sum += (sum >>16); - return (~sum); -} - - -int send_ether(nfd, buf, len, gwip) -int nfd, len; -char *buf; -struct in_addr gwip; -{ - static struct in_addr last_gw; - static char last_arp[6] = { 0, 0, 0, 0, 0, 0}; - ether_header_t *eh; - char *s; - int err; - - if (!ethbuf) - ethbuf = (char *)calloc(1, 65536+1024); - s = ethbuf; - eh = (ether_header_t *)s; - - bcopy((char *)buf, s + sizeof(*eh), len); - if (gwip.s_addr == last_gw.s_addr) - bcopy(last_arp, (char *)A_A eh->ether_dhost, 6); - else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1) - { - perror("arp"); - return -2; - } - eh->ether_type = htons(ETHERTYPE_IP); - last_gw.s_addr = gwip.s_addr; - err = sendip(nfd, s, sizeof(*eh) + len); - return err; -} - - -/* - */ -int send_ip(nfd, mtu, ip, gwip, frag) -int nfd, mtu; -ip_t *ip; -struct in_addr gwip; -int frag; -{ - static struct in_addr last_gw; - static char last_arp[6] = { 0, 0, 0, 0, 0, 0}; - static u_short id = 0; - ether_header_t *eh; - ip_t ipsv; - int err, iplen; - - if (!ipbuf) - ipbuf = (char *)malloc(65536); - eh = (ether_header_t *)ipbuf; - - bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost)); - if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr)) - bcopy(last_arp, (char *)A_A eh->ether_dhost, 6); - else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1) - { - perror("arp"); - return -2; - } - bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp)); - eh->ether_type = htons(ETHERTYPE_IP); - - bcopy((char *)ip, (char *)&ipsv, sizeof(*ip)); - last_gw.s_addr = gwip.s_addr; - iplen = ip->ip_len; - ip->ip_len = htons(iplen); - if (!(frag & 2)) { - if (!ip->ip_v) - ip->ip_v = IPVERSION; - if (!ip->ip_id) - ip->ip_id = htons(id++); - if (!ip->ip_ttl) - ip->ip_ttl = 60; - } - - if (!frag || (sizeof(*eh) + iplen < mtu)) - { - ip->ip_sum = 0; - ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2); - - bcopy((char *)ip, ipbuf + sizeof(*eh), iplen); - err = sendip(nfd, ipbuf, sizeof(*eh) + iplen); - } - else - { - /* - * Actually, this is bogus because we're putting all IP - * options in every packet, which isn't always what should be - * done. Will do for now. - */ - ether_header_t eth; - char optcpy[48], ol; - char *s; - int i, sent = 0, ts, hlen, olen; - - hlen = ip->ip_hl << 2; - if (mtu < (hlen + 8)) { - fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n", - mtu, hlen); - fprintf(stderr, "can't fragment data\n"); - return -2; - } - ol = (ip->ip_hl << 2) - sizeof(*ip); - for (i = 0, s = (char*)(ip + 1); ol > 0; ) - if (*s == IPOPT_EOL) { - optcpy[i++] = *s; - break; - } else if (*s == IPOPT_NOP) { - s++; - ol--; - } else - { - olen = (int)(*(u_char *)(s + 1)); - ol -= olen; - if (IPOPT_COPIED(*s)) - { - bcopy(s, optcpy + i, olen); - i += olen; - s += olen; - } - } - if (i) - { - /* - * pad out - */ - while ((i & 3) && (i & 3) != 3) - optcpy[i++] = IPOPT_NOP; - if ((i & 3) == 3) - optcpy[i++] = IPOPT_EOL; - } - - bcopy((char *)eh, (char *)ð, sizeof(eth)); - s = (char *)ip + hlen; - iplen = ntohs(ip->ip_len) - hlen; - ip->ip_off |= htons(IP_MF); - - while (1) - { - if ((sent + (mtu - hlen)) >= iplen) - { - ip->ip_off ^= htons(IP_MF); - ts = iplen - sent; - } - else - ts = (mtu - hlen); - ip->ip_off &= htons(0xe000); - ip->ip_off |= htons(sent >> 3); - ts += hlen; - ip->ip_len = htons(ts); - ip->ip_sum = 0; - ip->ip_sum = chksum((u_short *)ip, hlen); - bcopy((char *)ip, ipbuf + sizeof(*eh), hlen); - bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen); - err = sendip(nfd, ipbuf, sizeof(*eh) + ts); - - bcopy((char *)ð, ipbuf, sizeof(eth)); - sent += (ts - hlen); - if (!(ntohs(ip->ip_off) & IP_MF)) - break; - else if (!(ip->ip_off & htons(0x1fff))) - { - hlen = i + sizeof(*ip); - ip->ip_hl = (sizeof(*ip) + i) >> 2; - bcopy(optcpy, (char *)(ip + 1), i); - } - } - } - - bcopy((char *)&ipsv, (char *)ip, sizeof(*ip)); - return err; -} - - -/* - * send a tcp packet. - */ -int send_tcp(nfd, mtu, ip, gwip) -int nfd, mtu; -ip_t *ip; -struct in_addr gwip; -{ - static tcp_seq iss = 2; - struct tcpiphdr *ti; - tcphdr_t *t; - int thlen, i, iplen, hlen; - u_32_t lbuf[20]; - - iplen = ip->ip_len; - hlen = ip->ip_hl << 2; - t = (tcphdr_t *)((char *)ip + hlen); - ti = (struct tcpiphdr *)lbuf; - thlen = t->th_off << 2; - if (!thlen) - thlen = sizeof(tcphdr_t); - bzero((char *)ti, sizeof(*ti)); - ip->ip_p = IPPROTO_TCP; - ti->ti_pr = ip->ip_p; - ti->ti_src = ip->ip_src; - ti->ti_dst = ip->ip_dst; - bcopy((char *)ip + hlen, (char *)&ti->ti_sport, thlen); - - if (!ti->ti_win) - ti->ti_win = htons(4096); - iss += 63; - - i = sizeof(struct tcpiphdr) / sizeof(long); - - if ((ti->ti_flags == TH_SYN) && !ntohs(ip->ip_off) && - (lbuf[i] != htonl(0x020405b4))) { - lbuf[i] = htonl(0x020405b4); - bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4, - iplen - thlen - hlen); - thlen += 4; - } - ti->ti_off = thlen >> 2; - ti->ti_len = htons(thlen); - ip->ip_len = hlen + thlen; - ti->ti_sum = 0; - ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t)); - - bcopy((char *)&ti->ti_sport, (char *)ip + hlen, thlen); - return send_ip(nfd, mtu, ip, gwip, 1); -} - - -/* - * send a udp packet. - */ -int send_udp(nfd, mtu, ip, gwip) -int nfd, mtu; -ip_t *ip; -struct in_addr gwip; -{ - struct tcpiphdr *ti; - int thlen; - u_long lbuf[20]; - - ti = (struct tcpiphdr *)lbuf; - bzero((char *)ti, sizeof(*ti)); - thlen = sizeof(udphdr_t); - ti->ti_pr = ip->ip_p; - ti->ti_src = ip->ip_src; - ti->ti_dst = ip->ip_dst; - bcopy((char *)ip + (ip->ip_hl << 2), - (char *)&ti->ti_sport, sizeof(udphdr_t)); - - ti->ti_len = htons(thlen); - ip->ip_len = (ip->ip_hl << 2) + thlen; - ti->ti_sum = 0; - ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t)); - - bcopy((char *)&ti->ti_sport, - (char *)ip + (ip->ip_hl << 2), sizeof(udphdr_t)); - return send_ip(nfd, mtu, ip, gwip, 1); -} - - -/* - * send an icmp packet. - */ -int send_icmp(nfd, mtu, ip, gwip) -int nfd, mtu; -ip_t *ip; -struct in_addr gwip; -{ - struct icmp *ic; - - ic = (struct icmp *)((char *)ip + (ip->ip_hl << 2)); - - ic->icmp_cksum = 0; - ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp)); - - return send_ip(nfd, mtu, ip, gwip, 1); -} - - -int send_packet(nfd, mtu, ip, gwip) -int nfd, mtu; -ip_t *ip; -struct in_addr gwip; -{ - switch (ip->ip_p) - { - case IPPROTO_TCP : - return send_tcp(nfd, mtu, ip, gwip); - case IPPROTO_UDP : - return send_udp(nfd, mtu, ip, gwip); - case IPPROTO_ICMP : - return send_icmp(nfd, mtu, ip, gwip); - default : - return send_ip(nfd, mtu, ip, gwip, 1); - } -} diff --git a/usr.sbin/ipsend/common/iplang.h b/usr.sbin/ipsend/common/iplang.h deleted file mode 100644 index e52fd4be33f..00000000000 --- a/usr.sbin/ipsend/common/iplang.h +++ /dev/null @@ -1,56 +0,0 @@ -/* $OpenBSD: iplang.h,v 1.2 2001/01/17 06:01:25 fgsch Exp $ */ - -/* - * Copyright (C) 1997-1998 by Darren Reed. - * - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and due credit is given - * to the original author and the contributors. - */ -typedef struct iface { - int if_MTU; - char *if_name; - struct in_addr if_addr; - struct ether_addr if_eaddr; - struct iface *if_next; - int if_fd; -} iface_t; - - -typedef struct send { - struct iface *snd_if; - struct in_addr snd_gw; -} send_t; - - -typedef struct arp { - struct in_addr arp_addr; - struct ether_addr arp_eaddr; - struct arp *arp_next; -} arp_t; - - -typedef struct aniphdr { - union { - ip_t *ahu_ip; - char *ahu_data; - tcphdr_t *ahu_tcp; - udphdr_t *ahu_udp; - icmphdr_t *ahu_icmp; - } ah_un; - int ah_optlen; - int ah_lastopt; - int ah_p; - size_t ah_len; - struct aniphdr *ah_next; - struct aniphdr *ah_prev; -} aniphdr_t; - -#define ah_ip ah_un.ahu_ip -#define ah_data ah_un.ahu_data -#define ah_tcp ah_un.ahu_tcp -#define ah_udp ah_un.ahu_udp -#define ah_icmp ah_un.ahu_icmp - -extern int get_arpipv4 __P((char *, char *)); - diff --git a/usr.sbin/ipsend/common/ipsend.h b/usr.sbin/ipsend/common/ipsend.h deleted file mode 100644 index e6e64917df6..00000000000 --- a/usr.sbin/ipsend/common/ipsend.h +++ /dev/null @@ -1,71 +0,0 @@ -/* $OpenBSD: ipsend.h,v 1.4 2001/01/30 14:58:23 kjell Exp $ */ - -/* - * ipsend.h (C) 1997-1998 Darren Reed - * - * This was written to test what size TCP fragments would get through - * various TCP/IP packet filters, as used in IP firewalls. In certain - * conditions, enough of the TCP header is missing for unpredictable - * results unless the filter is aware that this can happen. - * - * The author provides this program as-is, with no gaurantee for its - * suitability for any specific purpose. The author takes no responsibility - * for the misuse/abuse of this program and provides it for the sole purpose - * of testing packet filter policies. This file maybe distributed freely - * providing it is not modified and that this notice remains in tact. - * - */ -#ifndef __P -# ifdef __STDC__ -# define __P(x) x -# else -# define __P(x) () -# endif -#endif - -#include "ip_fil_compat.h" -#ifdef linux -#include <linux/sockios.h> -#endif -#include "tcpip.h" -#include "ipt.h" -#include "ipf.h" - -extern int resolve __P((char *, char *)); -extern int arp __P((char *, char *)); -extern u_short chksum __P((u_short *, int)); -extern int send_ether __P((int, char *, int, struct in_addr)); -extern int send_ip __P((int, int, ip_t *, struct in_addr, int)); -extern int send_tcp __P((int, int, ip_t *, struct in_addr)); -extern int send_udp __P((int, int, ip_t *, struct in_addr)); -extern int send_icmp __P((int, int, ip_t *, struct in_addr)); -extern int send_packet __P((int, int, ip_t *, struct in_addr)); -extern int send_packets __P((char *, int, ip_t *, struct in_addr)); -extern u_short seclevel __P((char *)); -extern u_32_t buildopts __P((char *, char *, int)); -extern int addipopt __P((char *, struct ipopt_names *, int, char *)); -extern int initdevice __P((char *, int, int)); -extern int sendip __P((int, char *, int)); -#ifdef linux -extern struct sock *find_tcp __P((int, struct tcpiphdr *)); -#else -extern struct tcpcb *find_tcp __P((int, struct tcpiphdr *)); -#endif -extern int ip_resend __P((char *, int, struct ipread *, struct in_addr, char *)); - -extern void ip_test1 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test2 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test3 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test4 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test5 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test6 __P((char *, int, ip_t *, struct in_addr, int)); -extern void ip_test7 __P((char *, int, ip_t *, struct in_addr, int)); -extern int do_socket __P((char *, int, struct tcpiphdr *, struct in_addr)); -extern int openkmem __P((void)); -extern int kmemcpy __P((char *, void *, int)); - -#define KMCPY(a,b,c) kmemcpy((char *)(a), (void *)(b), (int)(c)) - -#ifndef OPT_RAW -#define OPT_RAW 0x80000 -#endif diff --git a/usr.sbin/ipsend/common/sbpf.c b/usr.sbin/ipsend/common/sbpf.c deleted file mode 100644 index fc4c0e85c2b..00000000000 --- a/usr.sbin/ipsend/common/sbpf.c +++ /dev/null @@ -1,144 +0,0 @@ -/* $OpenBSD: sbpf.c,v 1.5 2001/01/17 06:01:25 fgsch Exp $ */ - -/* - * (C)opyright 1995-1998 Darren Reed. (from tcplog) - * - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and due credit is given - * to the original author and the contributors. - */ -#include <stdio.h> -#include <netdb.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> -#include <ctype.h> -#include <signal.h> -#include <errno.h> -#include <sys/types.h> -#include <sys/param.h> -#include <sys/mbuf.h> -#include <sys/time.h> -#include <sys/timeb.h> -#include <sys/socket.h> -#include <sys/file.h> -#include <sys/ioctl.h> -#if BSD < 199103 -#include <sys/fcntlcom.h> -#endif -#if (__FreeBSD_version >= 300000) -# include <sys/dirent.h> -#else -# include <sys/dir.h> -#endif -#include <net/bpf.h> - -#include <net/if.h> -#include <netinet/in.h> -#include <netinet/in_systm.h> -#include <netinet/ip.h> -#include <netinet/ip_var.h> -#include <netinet/udp.h> -#include <netinet/udp_var.h> -#include <netinet/tcp.h> -#include "ipsend.h" - -#if !defined(lint) -static const char sccsid[] = "@(#)sbpf.c 1.3 8/25/95 (C)1995 Darren Reed"; -static const char rcsid[] = "@(#)$IPFilter: sbpf.c,v 2.1 1999/08/04 17:31:13 darrenr Exp $"; -#endif - -/* - * the code herein is dervied from libpcap. - */ -static u_char *buf = NULL; -static int bufsize = 0, timeout = 1; - - -int initdevice(device, sport, tout) -char *device; -int sport, tout; -{ - struct bpf_version bv; - struct timeval to; - struct ifreq ifr; - char bpfname[16]; - int fd, i; - - fd = 0; /* shutup gcc */ - - for (i = 0; i < 16; i++) - { - (void) sprintf(bpfname, "/dev/bpf%d", i); - if ((fd = open(bpfname, O_RDWR)) >= 0) - break; - } - if (i == 16) - { - fprintf(stderr, "no bpf devices available as /dev/bpfxx\n"); - return -1; - } - - if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) - { - perror("BIOCVERSION"); - return -1; - } - if (bv.bv_major != BPF_MAJOR_VERSION || - bv.bv_minor < BPF_MINOR_VERSION) - { - fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n", - bv.bv_major, bv.bv_minor); - fprintf(stderr, "current version: %d.%d\n", - BPF_MAJOR_VERSION, BPF_MINOR_VERSION); - return -1; - } - - (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); - if (ioctl(fd, BIOCSETIF, &ifr) == -1) - { - fprintf(stderr, "%s(%d):", ifr.ifr_name, fd); - perror("BIOCSETIF"); - exit(1); - } - /* - * get kernel buffer size - */ - if (ioctl(fd, BIOCGBLEN, &bufsize) == -1) - { - perror("BIOCSBLEN"); - exit(-1); - } - buf = (u_char*)malloc(bufsize); - /* - * set the timeout - */ - timeout = tout; - to.tv_sec = 1; - to.tv_usec = 0; - if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1) - { - perror("BIOCSRTIMEOUT"); - exit(-1); - } - - (void) ioctl(fd, BIOCFLUSH, 0); - return fd; -} - - -/* - * output an IP packet onto a fd opened for /dev/bpf - */ -int sendip(fd, pkt, len) -int fd, len; -char *pkt; -{ - if (write(fd, pkt, len) == -1) - { - perror("send"); - return -1; - } - - return len; -} diff --git a/usr.sbin/ipsend/common/sock.c b/usr.sbin/ipsend/common/sock.c deleted file mode 100644 index 8c169cef19a..00000000000 --- a/usr.sbin/ipsend/common/sock.c +++ /dev/null @@ -1,410 +0,0 @@ -/* $OpenBSD: sock.c,v 1.4 2001/01/17 06:01:25 fgsch Exp $ */ - -/* - * sock.c (C) 1995-1998 Darren Reed - * - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and due credit is given - * to the original author and the contributors. - */ -#if !defined(lint) -static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed"; -static const char rcsid[] = "@(#)$IPFilter: sock.c,v 2.1.4.1 2000/12/16 21:05:44 darrenr Exp $"; -#endif -#include <stdio.h> -#include <unistd.h> -#include <string.h> -#include <stdlib.h> -#include <stddef.h> -#include <pwd.h> -#include <sys/types.h> -#include <sys/time.h> -#include <sys/param.h> -#include <sys/stat.h> -#ifndef ultrix -#include <fcntl.h> -#endif -#if (__FreeBSD_version >= 300000) -# include <sys/dirent.h> -#else -# include <sys/dir.h> -#endif -#define _KERNEL -#define KERNEL -#ifdef ultrix -# undef LOCORE -# include <sys/smp_lock.h> -#endif -#include <sys/file.h> -#undef _KERNEL -#undef KERNEL -#include <nlist.h> -#include <sys/user.h> -#include <sys/socket.h> -#include <sys/socketvar.h> -#include <sys/proc.h> -#if !defined(ultrix) && !defined(hpux) -# include <kvm.h> -#endif -#ifdef sun -#include <sys/systm.h> -#include <sys/session.h> -#endif -#if BSD >= 199103 -#include <sys/sysctl.h> -#include <sys/filedesc.h> -#include <paths.h> -#endif -#include <math.h> -#include <netinet/in.h> -#include <netinet/in_systm.h> -#include <netinet/ip.h> -#include <netinet/tcp.h> -#include <net/if.h> -#include <net/route.h> -#include <netinet/ip_var.h> -#include <netinet/in_pcb.h> -#include <netinet/tcp_timer.h> -#include <netinet/tcp_var.h> -#include "ipsend.h" - -int nproc; -struct proc *proc; - -#ifndef KMEM -# ifdef _PATH_KMEM -# define KMEM _PATH_KMEM -# endif -#endif -#ifndef KERNEL -# ifdef _PATH_UNIX -# define KERNEL _PATH_UNIX -# endif -#endif -#ifndef KMEM -# define KMEM "/dev/kmem" -#endif -#ifndef KERNEL -# define KERNEL "/vmunix" -#endif - - -#if BSD < 199103 -static struct proc *getproc __P((void)); -#else -static struct kinfo_proc *getproc __P((void)); -#endif - - -int kmemcpy(buf, pos, n) -char *buf; -void *pos; -int n; -{ - static int kfd = -1; - off_t offset = (u_long)pos; - - if (kfd == -1) - kfd = open(KMEM, O_RDONLY); - - if (lseek(kfd, offset, SEEK_SET) == -1) - { - perror("lseek"); - return -1; - } - if (read(kfd, buf, n) == -1) - { - perror("read"); - return -1; - } - return n; -} - -struct nlist names[4] = { - { "_proc" }, - { "_nproc" }, -#ifdef ultrix - { "_u" }, -#else - { NULL }, -#endif - { NULL } - }; - -#if BSD < 199103 -static struct proc *getproc() -{ - struct proc *p; - pid_t pid = getpid(); - int siz, n; - - n = nlist(KERNEL, names); - if (n != 0) - { - fprintf(stderr, "nlist(%#x) == %d\n", names, n); - return NULL; - } - if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1) - { - fprintf(stderr, "read nproc (%#x)\n", names[1].n_value); - return NULL; - } - siz = nproc * sizeof(struct proc); - if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) proc\n", - names[0].n_value, &p, sizeof(p)); - return NULL; - } - proc = (struct proc *)malloc(siz); - if (KMCPY(proc, p, siz) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) proc\n", - p, proc, siz); - return NULL; - } - - p = proc; - - for (n = nproc; n; n--, p++) - if (p->p_pid == pid) - break; - if (!n) - return NULL; - - return p; -} - - -struct tcpcb *find_tcp(fd, ti) -int fd; -struct tcpiphdr *ti; -{ - struct tcpcb *t; - struct inpcb *i; - struct socket *s; - struct user *up; - struct proc *p; - struct file *f, **o; - - if (!(p = getproc())) - return NULL; -printf("fl %x ty %x cn %d mc %d\n", -f->f_flag, f->f_type, f->f_count, f->f_msgcount); - up = (struct user *)malloc(sizeof(*up)); -#ifndef ultrix - if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1) - { - fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea); - return NULL; - } -#else - if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1) - { - fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value); - return NULL; - } -#endif - - o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1)); - if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n", - up->u_ofile, o, sizeof(*o)); - return NULL; - } - f = (struct file *)calloc(1, sizeof(*f)); - if (KMCPY(f, o[fd], sizeof(*f)) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n", - up->u_ofile[fd], f, sizeof(*f)); - return NULL; - } - - s = (struct socket *)calloc(1, sizeof(*s)); - if (KMCPY(s, f->f_data, sizeof(*s)) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n", - o[fd], s, sizeof(*s)); - return NULL; - } - - i = (struct inpcb *)calloc(1, sizeof(*i)); - if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1) - { - fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n", - s->so_pcb, i, sizeof(*i)); - return NULL; - } - - t = (struct tcpcb *)calloc(1, sizeof(*t)); - if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1) - { - fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n", - i->inp_ppcb, t, sizeof(*t)); - return NULL; - } - return (struct tcpcb *)i->inp_ppcb; -} -#else -static struct kinfo_proc *getproc() -{ - static struct kinfo_proc kp; - pid_t pid = getpid(); - int mib[4]; - size_t n; - - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PID; - mib[3] = pid; - - n = sizeof(kp); - if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1) - { - perror("sysctl"); - return NULL; - } - return &kp; -} - - -struct tcpcb *find_tcp(tfd, ti) -int tfd; -struct tcpiphdr *ti; -{ - struct tcpcb *t; - struct inpcb *i; - struct socket *s; - struct filedesc *fd; - struct kinfo_proc *p; - struct file *f, **o; - - if (!(p = getproc())) - return NULL; - - fd = (struct filedesc *)malloc(sizeof(*fd)); -#if defined( __FreeBSD_version) && __FreeBSD_version >= 500013 - if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx) failed\n", - (u_long)p, (u_long)p->ki_fd); - return NULL; - } -#else - if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx) failed\n", - (u_long)p, (u_long)p->kp_proc.p_fd); - return NULL; - } -#endif - - o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1)); - if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n", - (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o)); - return NULL; - } - f = (struct file *)calloc(1, sizeof(*f)); - if (KMCPY(f, o[tfd], sizeof(*f)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n", - (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f)); - return NULL; - } - - s = (struct socket *)calloc(1, sizeof(*s)); - if (KMCPY(s, f->f_data, sizeof(*s)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n", - (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s)); - return NULL; - } - - i = (struct inpcb *)calloc(1, sizeof(*i)); - if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1) - { - fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n", - (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i)); - return NULL; - } - - t = (struct tcpcb *)calloc(1, sizeof(*t)); - if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1) - { - fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n", - (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t)); - return NULL; - } - return (struct tcpcb *)i->inp_ppcb; -} -#endif /* BSD < 199301 */ - -int do_socket(dev, mtu, ti, gwip) -char *dev; -int mtu; -struct tcpiphdr *ti; -struct in_addr gwip; -{ - struct sockaddr_in rsin, lsin; - struct tcpcb *t, tcb; - int fd, nfd, len; - - printf("Dest. Port: %d\n", ti->ti_dport); - - fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd == -1) - { - perror("socket"); - return -1; - } - - if (fcntl(fd, F_SETFL, FNDELAY) == -1) - { - perror("fcntl"); - return -1; - } - - bzero((char *)&lsin, sizeof(lsin)); - lsin.sin_family = AF_INET; - bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr, - sizeof(struct in_addr)); - if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1) - { - perror("bind"); - return -1; - } - len = sizeof(lsin); - (void) getsockname(fd, (struct sockaddr *)&lsin, &len); - ti->ti_sport = lsin.sin_port; - printf("sport %d\n", ntohs(lsin.sin_port)); - nfd = initdevice(dev, ntohs(lsin.sin_port), 1); - - if (!(t = find_tcp(fd, ti))) - return -1; - - bzero((char *)&rsin, sizeof(rsin)); - rsin.sin_family = AF_INET; - bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr, - sizeof(struct in_addr)); - rsin.sin_port = ti->ti_dport; - if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 && - errno != EINPROGRESS) - { - perror("connect"); - return -1; - } - KMCPY(&tcb, t, sizeof(tcb)); - ti->ti_win = tcb.rcv_adv; - ti->ti_seq = tcb.snd_nxt - 1; - ti->ti_ack = tcb.rcv_nxt; - - if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1) - return -1; - (void)write(fd, "Hello World\n", 12); - sleep(2); - close(fd); - return 0; -} |