4 * Copyright (C) 1993-2001 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
14 #include <sys/types.h>
15 #if !defined(__SVR4) && !defined(__svr4__)
18 #include <sys/byteorder.h>
21 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
32 # include <sys/ioccom.h>
33 # include <sys/sysmacros.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
40 #if __FreeBSD_version >= 300000
41 # include <net/if_var.h>
44 #include <arpa/nameser.h>
45 #include <arpa/inet.h>
49 # include <linux/a.out.h>
54 #include "netinet/ipl.h"
58 # define nlist nlist64
61 #if defined(sun) && !SOLARIS2
62 # define STRERROR(x) sys_errlist[x]
63 extern char *sys_errlist[];
65 # define STRERROR(x) strerror(x)
69 static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed";
70 static const char rcsid[] = "@(#)$Id: ipnat.c,v 1.24.2.2 2005/05/10 21:19:30 darrenr Exp $";
75 #define bzero(a,b) memset(a,0,b)
78 char thishost[MAXHOSTNAMELEN];
82 void dostats __P((natstat_t *, int)), flushtable __P((int, int));
83 void usage __P((char *));
84 int main __P((int, char*[]));
85 void showhostmap __P((natstat_t *nsp));
86 void natstat_dead __P((natstat_t *, char *));
93 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
102 char *file, *core, *kernel;
115 while ((c = getopt(argc, argv, "CdFf:hlM:N:nrRsv")) != -1)
144 opts |= OPT_DONOTHING;
148 opts |= OPT_NORESOLVE;
166 if ((kernel != NULL) || (core != NULL)) {
167 (void) setgid(getgid());
168 (void) setuid(getuid());
171 bzero((char *)&ns, sizeof(ns));
173 if ((opts & OPT_DONOTHING) == 0) {
174 if (checkrev(IPL_NAME) == -1) {
175 fprintf(stderr, "User/kernel version check failed\n");
181 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
182 if (openkmem(kernel, core) == -1)
185 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
186 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
187 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
192 bzero((char *)&obj, sizeof(obj));
193 obj.ipfo_rev = IPFILTER_VERSION;
194 obj.ipfo_size = sizeof(*nsp);
195 obj.ipfo_type = IPFOBJ_NATSTAT;
196 obj.ipfo_ptr = (void *)nsp;
197 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
198 perror("ioctl(SIOCGNATS)");
201 (void) setgid(getgid());
202 (void) setuid(getuid());
203 } else if ((kernel != NULL) || (core != NULL)) {
204 if (openkmem(kernel, core) == -1)
207 natstat_dead(nsp, kernel);
208 if (opts & (OPT_LIST|OPT_STAT))
213 if (opts & (OPT_FLUSH|OPT_CLEAR))
214 flushtable(fd, opts);
216 ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
218 if (opts & (OPT_LIST|OPT_STAT))
225 * Read NAT statistic information in using a symbol table and memory file
226 * rather than doing ioctl's.
228 void natstat_dead(nsp, kernel)
232 struct nlist nat_nlist[10] = {
233 { "nat_table" }, /* 0 */
236 { "ipf_nattable_sz" },
237 { "ipf_natrules_sz" },
238 { "ipf_rdrrules_sz" }, /* 5 */
239 { "ipf_hostmap_sz" },
246 if (nlist(kernel, nat_nlist) == -1) {
247 fprintf(stderr, "nlist error\n");
252 * Normally the ioctl copies all of these values into the structure
253 * for us, before returning it to userland, so here we must copy each
254 * one in individually.
256 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
257 nsp->ns_table[0] = tables[0];
258 nsp->ns_table[1] = tables[1];
260 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
261 sizeof(nsp->ns_list));
262 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
263 sizeof(nsp->ns_maptable));
264 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
265 sizeof(nsp->ns_nattab_sz));
266 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
267 sizeof(nsp->ns_rultab_sz));
268 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
269 sizeof(nsp->ns_rdrtab_sz));
270 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
271 sizeof(nsp->ns_hostmap_sz));
272 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
273 sizeof(nsp->ns_instances));
274 kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
275 sizeof(nsp->ns_apslist));
280 * Display NAT statistics.
282 void dostats(nsp, opts)
292 if (opts & OPT_STAT) {
293 printf("mapped\tin\t%lu\tout\t%lu\n",
294 nsp->ns_mapped[0], nsp->ns_mapped[1]);
295 printf("added\t%lu\texpired\t%lu\n",
296 nsp->ns_added, nsp->ns_expire);
297 printf("no memory\t%lu\tbad nat\t%lu\n",
298 nsp->ns_memfail, nsp->ns_badnat);
299 printf("inuse\t%lu\nrules\t%lu\n",
300 nsp->ns_inuse, nsp->ns_rules);
301 printf("wilds\t%u\n", nsp->ns_wilds);
302 if (opts & OPT_VERBOSE)
303 printf("table %p list %p\n",
304 nsp->ns_table, nsp->ns_list);
308 * Show list of NAT rules and NAT sessions ?
310 if (opts & OPT_LIST) {
311 printf("List of active MAP/Redirect filters:\n");
312 while (nsp->ns_list) {
313 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
319 printf("%lu ", ipn.in_hits);
320 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
321 nsp->ns_list = ipn.in_next;
324 printf("\nList of active sessions:\n");
326 for (np = nsp->ns_instances; np; np = nat.nat_next) {
327 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
329 printactivenat(&nat, opts);
331 printaps(nat.nat_aps, opts);
334 if (opts & OPT_VERBOSE)
341 * Display the active host mapping table.
343 void showhostmap(nsp)
346 hostmap_t hm, *hmp, **maptable;
349 printf("\nList of active host mappings:\n");
351 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
353 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
354 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
355 perror("kmemcpy (maptable)");
359 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
363 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
364 perror("kmemcpy (hostmap)");
368 printhostmap(&hm, hv);
377 * Issue an ioctl to flush either the NAT rules table or the active mapping
380 void flushtable(fd, opts)
385 if (opts & OPT_FLUSH) {
387 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
388 perror("ioctl(SIOCFLNAT)");
390 printf("%d entries flushed from NAT table\n", n);
393 if (opts & OPT_CLEAR) {
395 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
396 perror("ioctl(SIOCCNATL)");
398 printf("%d entries flushed from NAT list\n", n);