1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2002-03-19 11:45:20 +00:00
|
|
|
* Copyright (C) 1993-2002 by Darren Reed.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 11:59:33 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
|
|
|
* Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
|
|
|
|
*/
|
2002-03-19 11:45:20 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
1998-06-20 18:29:38 +00:00
|
|
|
#include <errno.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#include <sys/types.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#if !defined(__SVR4) && !defined(__svr4__)
|
|
|
|
#include <strings.h>
|
|
|
|
#else
|
|
|
|
#include <sys/byteorder.h>
|
|
|
|
#endif
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <sys/time.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#if defined(sun) && (defined(__svr4__) || defined(__SVR4))
|
|
|
|
# include <sys/ioccom.h>
|
|
|
|
# include <sys/sysmacros.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <net/if.h>
|
1999-11-08 20:51:23 +00:00
|
|
|
#if __FreeBSD_version >= 300000
|
|
|
|
# include <net/if_var.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#include <ctype.h>
|
2002-03-19 11:45:20 +00:00
|
|
|
#include <nlist.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#include "netinet/ip_compat.h"
|
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "netinet/ip_nat.h"
|
2002-03-19 11:45:20 +00:00
|
|
|
#include "netinet/ip_state.h"
|
|
|
|
#include "netinet/ip_proxy.h"
|
2000-05-24 02:14:22 +00:00
|
|
|
#include "ipf.h"
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "kmem.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1998-06-20 18:29:38 +00:00
|
|
|
#if defined(sun) && !SOLARIS2
|
|
|
|
# define STRERROR(x) sys_errlist[x]
|
|
|
|
extern char *sys_errlist[];
|
|
|
|
#else
|
|
|
|
# define STRERROR(x) strerror(x)
|
|
|
|
#endif
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed";
|
2002-03-19 11:45:20 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id: ipnat.c,v 2.16.2.20 2002/02/22 15:32:55 darrenr Exp $";
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
#if SOLARIS
|
|
|
|
#define bzero(a,b) memset(a,0,b)
|
|
|
|
#endif
|
2000-05-24 02:14:22 +00:00
|
|
|
int use_inet6 = 0;
|
2002-03-19 11:45:20 +00:00
|
|
|
char thishost[MAXHOSTNAMELEN];
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
extern char *optarg;
|
1999-11-08 20:51:23 +00:00
|
|
|
extern ipnat_t *natparse __P((char *, int));
|
|
|
|
extern void natparsefile __P((int, char *, int));
|
2002-03-19 11:45:20 +00:00
|
|
|
extern void printnat __P((ipnat_t *, int));
|
|
|
|
extern void printactivenat __P((nat_t *, int));
|
|
|
|
extern void printhostmap __P((hostmap_t *, u_int));
|
|
|
|
extern char *getsumd __P((u_32_t));
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
void dostats __P((natstat_t *, int)), flushtable __P((int, int));
|
1997-04-03 10:22:02 +00:00
|
|
|
void usage __P((char *));
|
1998-06-20 18:29:38 +00:00
|
|
|
int countbits __P((u_32_t));
|
1997-04-03 10:22:02 +00:00
|
|
|
char *getnattype __P((ipnat_t *));
|
|
|
|
int main __P((int, char*[]));
|
1999-11-08 20:51:23 +00:00
|
|
|
void printaps __P((ap_session_t *, int));
|
2002-03-19 11:45:20 +00:00
|
|
|
void showhostmap __P((natstat_t *nsp));
|
|
|
|
void natstat_dead __P((natstat_t *, char *));
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
void usage(name)
|
|
|
|
char *name;
|
|
|
|
{
|
1999-11-08 20:51:23 +00:00
|
|
|
fprintf(stderr, "%s: [-CFhlnrsv] [-f filename]\n", name);
|
1997-02-09 22:50:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
natstat_t ns, *nsp = &ns;
|
|
|
|
char *file, *core, *kernel;
|
|
|
|
int fd, opts, c, mode;
|
|
|
|
|
|
|
|
fd = -1;
|
|
|
|
opts = 0;
|
|
|
|
file = NULL;
|
|
|
|
core = NULL;
|
|
|
|
kernel = NULL;
|
|
|
|
mode = O_RDWR;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
while ((c = getopt(argc, argv, "CdFf:hlM:N:nrsv")) != -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'C' :
|
|
|
|
opts |= OPT_CLEAR;
|
|
|
|
break;
|
2000-05-24 02:14:22 +00:00
|
|
|
case 'd' :
|
|
|
|
opts |= OPT_DEBUG;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'f' :
|
|
|
|
file = optarg;
|
|
|
|
break;
|
|
|
|
case 'F' :
|
|
|
|
opts |= OPT_FLUSH;
|
|
|
|
break;
|
1999-11-08 20:51:23 +00:00
|
|
|
case 'h' :
|
|
|
|
opts |=OPT_HITS;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'l' :
|
|
|
|
opts |= OPT_LIST;
|
2000-08-13 04:58:02 +00:00
|
|
|
mode = O_RDONLY;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
2001-07-28 11:59:33 +00:00
|
|
|
case 'M' :
|
|
|
|
core = optarg;
|
|
|
|
break;
|
2002-03-19 11:45:20 +00:00
|
|
|
case 'N' :
|
|
|
|
kernel = optarg;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'n' :
|
|
|
|
opts |= OPT_NODO;
|
2000-08-13 04:58:02 +00:00
|
|
|
mode = O_RDONLY;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'r' :
|
2000-05-24 02:14:22 +00:00
|
|
|
opts |= OPT_REMOVE;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 's' :
|
|
|
|
opts |= OPT_STAT;
|
2000-08-13 04:58:02 +00:00
|
|
|
mode = O_RDONLY;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'v' :
|
|
|
|
opts |= OPT_VERBOSE;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
if ((kernel != NULL) || (core != NULL)) {
|
2001-07-28 11:59:33 +00:00
|
|
|
(void) setgid(getgid());
|
|
|
|
(void) setuid(getuid());
|
|
|
|
}
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
bzero((char *)&ns, sizeof(ns));
|
|
|
|
|
2000-05-24 02:14:22 +00:00
|
|
|
gethostname(thishost, sizeof(thishost));
|
|
|
|
thishost[sizeof(thishost) - 1] = '\0';
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
if (!(opts & OPT_NODO) && (kernel == NULL) && (core == NULL)) {
|
|
|
|
if (openkmem(kernel, core) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (((fd = open(IPL_NAT, mode)) == -1) &&
|
|
|
|
((fd = open(IPL_NAT, O_RDONLY)) == -1)) {
|
|
|
|
(void) fprintf(stderr, "%s: open: %s\n", IPL_NAT,
|
|
|
|
STRERROR(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (ioctl(fd, SIOCGNATS, &nsp) == -1) {
|
|
|
|
perror("ioctl(SIOCGNATS)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
(void) setgid(getgid());
|
|
|
|
(void) setuid(getuid());
|
|
|
|
} else if ((kernel != NULL) || (core != NULL)) {
|
|
|
|
if (openkmem(kernel, core) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
natstat_dead(nsp, kernel);
|
|
|
|
if (opts & (OPT_LIST|OPT_STAT))
|
|
|
|
dostats(nsp, opts);
|
|
|
|
exit(0);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts & (OPT_FLUSH|OPT_CLEAR))
|
|
|
|
flushtable(fd, opts);
|
|
|
|
if (file)
|
1999-11-08 20:51:23 +00:00
|
|
|
natparsefile(fd, file, opts);
|
1997-02-09 22:50:16 +00:00
|
|
|
if (opts & (OPT_LIST|OPT_STAT))
|
2002-03-19 11:45:20 +00:00
|
|
|
dostats(nsp, opts);
|
1997-02-09 22:50:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
/*
|
|
|
|
* Read nat statistic information in using a symbol table and memory file
|
|
|
|
* rather than doing ioctl's.
|
|
|
|
*/
|
|
|
|
void natstat_dead(nsp, kernel)
|
|
|
|
natstat_t *nsp;
|
|
|
|
char *kernel;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
struct nlist nat_nlist[10] = {
|
|
|
|
{ "nat_table" }, /* 0 */
|
|
|
|
{ "nat_list" },
|
|
|
|
{ "maptable" },
|
|
|
|
{ "ipf_nattable_sz" },
|
|
|
|
{ "ipf_natrules_sz" },
|
|
|
|
{ "ipf_rdrrules_sz" }, /* 5 */
|
|
|
|
{ "ipf_hostmap_sz" },
|
|
|
|
{ "nat_instances" },
|
|
|
|
{ "ap_sess_list" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
void *tables[2];
|
|
|
|
|
|
|
|
if (nlist(kernel, nat_nlist) == -1) {
|
|
|
|
fprintf(stderr, "nlist error\n");
|
1999-11-08 20:51:23 +00:00
|
|
|
return;
|
1997-05-25 15:45:04 +00:00
|
|
|
}
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
/*
|
|
|
|
* Normally the ioctl copies all of these values into the structure
|
|
|
|
* for us, before returning it to useland, so here we must copy each
|
|
|
|
* one in individually.
|
|
|
|
*/
|
|
|
|
kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
|
|
|
|
nsp->ns_table[0] = tables[0];
|
|
|
|
nsp->ns_table[1] = tables[1];
|
|
|
|
|
|
|
|
kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
|
|
|
|
sizeof(nsp->ns_list));
|
|
|
|
kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
|
|
|
|
sizeof(nsp->ns_maptable));
|
|
|
|
kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
|
|
|
|
sizeof(nsp->ns_nattab_sz));
|
|
|
|
kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
|
|
|
|
sizeof(nsp->ns_rultab_sz));
|
|
|
|
kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
|
|
|
|
sizeof(nsp->ns_rdrtab_sz));
|
|
|
|
kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
|
|
|
|
sizeof(nsp->ns_hostmap_sz));
|
|
|
|
kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
|
|
|
|
sizeof(nsp->ns_instances));
|
|
|
|
kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
|
|
|
|
sizeof(nsp->ns_apslist));
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-03-19 11:45:20 +00:00
|
|
|
* Display NAT statistics.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
2002-03-19 11:45:20 +00:00
|
|
|
void dostats(nsp, opts)
|
|
|
|
natstat_t *nsp;
|
|
|
|
int opts;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2000-05-24 02:14:22 +00:00
|
|
|
nat_t **nt[2], *np, nat;
|
1997-02-09 22:50:16 +00:00
|
|
|
ipnat_t ipn;
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
/*
|
|
|
|
* Show statistics ?
|
|
|
|
*/
|
1997-02-09 22:50:16 +00:00
|
|
|
if (opts & OPT_STAT) {
|
|
|
|
printf("mapped\tin\t%lu\tout\t%lu\n",
|
2002-03-19 11:45:20 +00:00
|
|
|
nsp->ns_mapped[0], nsp->ns_mapped[1]);
|
1997-02-09 22:50:16 +00:00
|
|
|
printf("added\t%lu\texpired\t%lu\n",
|
2002-03-19 11:45:20 +00:00
|
|
|
nsp->ns_added, nsp->ns_expire);
|
2000-05-24 02:14:22 +00:00
|
|
|
printf("no memory\t%lu\tbad nat\t%lu\n",
|
2002-03-19 11:45:20 +00:00
|
|
|
nsp->ns_memfail, nsp->ns_badnat);
|
|
|
|
printf("inuse\t%lu\nrules\t%lu\n",
|
|
|
|
nsp->ns_inuse, nsp->ns_rules);
|
|
|
|
printf("wilds\t%u\n", nsp->ns_wilds);
|
1997-02-09 22:50:16 +00:00
|
|
|
if (opts & OPT_VERBOSE)
|
2002-03-19 11:45:20 +00:00
|
|
|
printf("table %p list %p\n",
|
|
|
|
nsp->ns_table, nsp->ns_list);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Show list of NAT rules and NAT sessions ?
|
|
|
|
*/
|
1997-02-09 22:50:16 +00:00
|
|
|
if (opts & OPT_LIST) {
|
|
|
|
printf("List of active MAP/Redirect filters:\n");
|
2002-03-19 11:45:20 +00:00
|
|
|
while (nsp->ns_list) {
|
|
|
|
if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
|
1997-04-03 10:22:02 +00:00
|
|
|
sizeof(ipn))) {
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("kmemcpy");
|
|
|
|
break;
|
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
if (opts & OPT_HITS)
|
|
|
|
printf("%d ", ipn.in_hits);
|
2002-03-19 11:45:20 +00:00
|
|
|
printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
|
|
|
|
nsp->ns_list = ipn.in_next;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nt[0] = (nat_t **)malloc(sizeof(*nt) * NAT_SIZE);
|
2002-03-19 11:45:20 +00:00
|
|
|
if (kmemcpy((char *)nt[0], (long)nsp->ns_table[0],
|
1997-04-03 10:22:02 +00:00
|
|
|
sizeof(**nt) * NAT_SIZE)) {
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("kmemcpy");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nList of active sessions:\n");
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
for (np = nsp->ns_instances; np; np = nat.nat_next) {
|
1999-11-08 20:51:23 +00:00
|
|
|
if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
|
|
|
|
break;
|
2002-03-19 11:45:20 +00:00
|
|
|
printactivenat(&nat, opts);
|
1999-11-08 20:51:23 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
if (opts & OPT_VERBOSE)
|
|
|
|
showhostmap(nsp);
|
|
|
|
free(nt[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* display the active host mapping table.
|
|
|
|
*/
|
|
|
|
void showhostmap(nsp)
|
|
|
|
natstat_t *nsp;
|
|
|
|
{
|
|
|
|
hostmap_t hm, *hmp, **maptable;
|
|
|
|
u_int hv;
|
|
|
|
|
|
|
|
printf("\nList of active host mappings:\n");
|
|
|
|
|
|
|
|
maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
|
|
|
|
nsp->ns_hostmap_sz);
|
|
|
|
if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
|
|
|
|
sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
|
|
|
|
perror("kmemcpy (maptable)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
|
|
|
|
hmp = maptable[hv];
|
|
|
|
|
|
|
|
while (hmp) {
|
|
|
|
if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
|
|
|
|
perror("kmemcpy (hostmap)");
|
2001-07-28 11:59:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
printhostmap(&hm, hv);
|
|
|
|
hmp = hm.hm_next;
|
2001-07-28 11:59:33 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
free(maptable);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
/*
|
|
|
|
* Issue an ioctl to flush either the NAT rules table or the active mapping
|
|
|
|
* table or both.
|
|
|
|
*/
|
1997-02-09 22:50:16 +00:00
|
|
|
void flushtable(fd, opts)
|
|
|
|
int fd, opts;
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (opts & OPT_FLUSH) {
|
|
|
|
n = 0;
|
2000-05-24 02:14:22 +00:00
|
|
|
if (!(opts & OPT_NODO) && ioctl(fd, SIOCIPFFL, &n) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("ioctl(SIOCFLNAT)");
|
|
|
|
else
|
|
|
|
printf("%d entries flushed from NAT table\n", n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts & OPT_CLEAR) {
|
2000-05-24 02:14:22 +00:00
|
|
|
n = 1;
|
|
|
|
if (!(opts & OPT_NODO) && ioctl(fd, SIOCIPFFL, &n) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("ioctl(SIOCCNATL)");
|
|
|
|
else
|
|
|
|
printf("%d entries flushed from NAT list\n", n);
|
|
|
|
}
|
|
|
|
}
|