4 * Copyright (C) 2001-2006 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.11 2007/09/25 08:27:34 darrenr Exp $";
75 #define bzero(a,b) memset(a,0,b)
78 char thishost[MAXHOSTNAMELEN];
82 void dostats __P((int, natstat_t *, int, int));
83 void dotable __P((natstat_t *, int, int));
84 void flushtable __P((int, int));
85 void usage __P((char *));
86 int main __P((int, char*[]));
87 void showhostmap __P((natstat_t *nsp));
88 void natstat_dead __P((natstat_t *, char *));
89 void dostats_live __P((int, natstat_t *, int));
90 void showhostmap_dead __P((natstat_t *));
91 void showhostmap_live __P((int, natstat_t *));
92 void dostats_dead __P((natstat_t *, int));
93 void showtqtable_live __P((int));
100 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
109 char *file, *core, *kernel;
122 while ((c = getopt(argc, argv, "CdFf:hlM:N:nrRsv")) != -1)
151 opts |= OPT_DONOTHING;
155 opts |= OPT_NORESOLVE;
173 if ((kernel != NULL) || (core != NULL)) {
174 (void) setgid(getgid());
175 (void) setuid(getuid());
178 if (!(opts & OPT_DONOTHING)) {
179 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
180 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
181 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
187 bzero((char *)&ns, sizeof(ns));
189 if ((opts & OPT_DONOTHING) == 0) {
190 if (checkrev(IPL_NAME) == -1) {
191 fprintf(stderr, "User/kernel version check failed\n");
196 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
197 bzero((char *)&obj, sizeof(obj));
198 obj.ipfo_rev = IPFILTER_VERSION;
199 obj.ipfo_type = IPFOBJ_NATSTAT;
200 obj.ipfo_size = sizeof(*nsp);
201 obj.ipfo_ptr = (void *)nsp;
202 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
203 perror("ioctl(SIOCGNATS)");
206 (void) setgid(getgid());
207 (void) setuid(getuid());
208 } else if ((kernel != NULL) || (core != NULL)) {
209 if (openkmem(kernel, core) == -1)
212 natstat_dead(nsp, kernel);
213 if (opts & (OPT_LIST|OPT_STAT))
214 dostats(fd, nsp, opts, 0);
218 if (opts & (OPT_FLUSH|OPT_CLEAR))
219 flushtable(fd, opts);
221 ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
223 if (opts & (OPT_LIST|OPT_STAT))
224 dostats(fd, nsp, opts, 1);
230 * Read NAT statistic information in using a symbol table and memory file
231 * rather than doing ioctl's.
233 void natstat_dead(nsp, kernel)
237 struct nlist nat_nlist[10] = {
238 { "nat_table" }, /* 0 */
241 { "ipf_nattable_sz" },
242 { "ipf_natrules_sz" },
243 { "ipf_rdrrules_sz" }, /* 5 */
244 { "ipf_hostmap_sz" },
251 if (nlist(kernel, nat_nlist) == -1) {
252 fprintf(stderr, "nlist error\n");
257 * Normally the ioctl copies all of these values into the structure
258 * for us, before returning it to userland, so here we must copy each
259 * one in individually.
261 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
262 nsp->ns_table[0] = tables[0];
263 nsp->ns_table[1] = tables[1];
265 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
266 sizeof(nsp->ns_list));
267 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
268 sizeof(nsp->ns_maptable));
269 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
270 sizeof(nsp->ns_nattab_sz));
271 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
272 sizeof(nsp->ns_rultab_sz));
273 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
274 sizeof(nsp->ns_rdrtab_sz));
275 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
276 sizeof(nsp->ns_hostmap_sz));
277 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
278 sizeof(nsp->ns_instances));
279 kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
280 sizeof(nsp->ns_apslist));
285 * Issue an ioctl to flush either the NAT rules table or the active mapping
288 void flushtable(fd, opts)
293 if (opts & OPT_FLUSH) {
295 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
296 perror("ioctl(SIOCFLNAT)");
298 printf("%d entries flushed from NAT table\n", n);
301 if (opts & OPT_CLEAR) {
303 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
304 perror("ioctl(SIOCCNATL)");
306 printf("%d entries flushed from NAT list\n", n);
312 * Display NAT statistics.
314 void dostats_dead(nsp, opts)
321 printf("List of active MAP/Redirect filters:\n");
322 while (nsp->ns_list) {
323 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
329 printf("%lu ", ipn.in_hits);
330 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
331 nsp->ns_list = ipn.in_next;
334 printf("\nList of active sessions:\n");
336 for (np = nsp->ns_instances; np; np = nat.nat_next) {
337 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
339 printactivenat(&nat, opts, 0, nsp->ns_ticks);
341 printaps(nat.nat_aps, opts);
344 if (opts & OPT_VERBOSE)
345 showhostmap_dead(nsp);
349 void dostats(fd, nsp, opts, alive)
356 if (opts & OPT_STAT) {
357 printf("mapped\tin\t%lu\tout\t%lu\n",
358 nsp->ns_mapped[0], nsp->ns_mapped[1]);
359 printf("added\t%lu\texpired\t%lu\n",
360 nsp->ns_added, nsp->ns_expire);
361 printf("no memory\t%lu\tbad nat\t%lu\n",
362 nsp->ns_memfail, nsp->ns_badnat);
363 printf("inuse\t%lu\norphans\t%u\nrules\t%lu\n",
364 nsp->ns_inuse, nsp->ns_orphans, nsp->ns_rules);
365 printf("wilds\t%u\n", nsp->ns_wilds);
366 dotable(nsp, fd, alive);
367 if (opts & OPT_VERBOSE)
368 printf("table %p list %p\n",
369 nsp->ns_table, nsp->ns_list);
371 showtqtable_live(fd);
374 if (opts & OPT_LIST) {
376 dostats_live(fd, nsp, opts);
378 dostats_dead(nsp, opts);
383 void dotable(nsp, fd, alive)
387 int sz, i, used, totallen, maxlen, minlen;
392 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
393 buckets = (u_long *)malloc(sz);
395 obj.ipfo_rev = IPFILTER_VERSION;
396 obj.ipfo_type = IPFOBJ_GTABLE;
397 obj.ipfo_size = sizeof(table);
398 obj.ipfo_ptr = &table;
400 table.ita_type = IPFTABLE_BUCKETS_NATIN;
401 table.ita_table = buckets;
404 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
409 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
417 minlen = nsp->ns_inuse;
420 for (i = 0; i < nsp->ns_nattab_sz; i++) {
421 if (buckets[i] > maxlen)
423 if (buckets[i] < minlen)
427 totallen += buckets[i];
430 printf("hash efficiency\t%2.2f%%\n",
431 totallen ? ((float)used / totallen) * 100.0 : 0.0);
432 printf("bucket usage\t%2.2f%%\n",
433 ((float)used / nsp->ns_nattab_sz) * 100.0);
434 printf("minimal length\t%d\n", minlen);
435 printf("maximal length\t%d\n", maxlen);
436 printf("average length\t%.3f\n", used ? (float)totallen / used : 0.0);
441 * Display NAT statistics.
443 void dostats_live(fd, nsp, opts)
452 bzero((char *)&obj, sizeof(obj));
453 obj.ipfo_rev = IPFILTER_VERSION;
454 obj.ipfo_type = IPFOBJ_GENITER;
455 obj.ipfo_size = sizeof(iter);
456 obj.ipfo_ptr = &iter;
458 iter.igi_type = IPFGENITER_IPNAT;
460 iter.igi_data = &ipn;
463 * Show list of NAT rules and NAT sessions ?
465 printf("List of active MAP/Redirect filters:\n");
466 while (nsp->ns_list) {
467 if (ioctl(fd, SIOCGENITER, &obj) == -1)
470 printf("%lu ", ipn.in_hits);
471 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
472 nsp->ns_list = ipn.in_next;
475 printf("\nList of active sessions:\n");
477 iter.igi_type = IPFGENITER_NAT;
479 iter.igi_data = &nat;
481 while (nsp->ns_instances != NULL) {
482 if (ioctl(fd, SIOCGENITER, &obj) == -1)
484 printactivenat(&nat, opts, 1, nsp->ns_ticks);
486 printaps(nat.nat_aps, opts);
487 nsp->ns_instances = nat.nat_next;
490 if (opts & OPT_VERBOSE)
491 showhostmap_live(fd, nsp);
496 * Display the active host mapping table.
498 void showhostmap_dead(nsp)
501 hostmap_t hm, *hmp, **maptable;
504 printf("\nList of active host mappings:\n");
506 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
508 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
509 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
510 perror("kmemcpy (maptable)");
514 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
518 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
519 perror("kmemcpy (hostmap)");
523 printhostmap(&hm, hv);
532 * Display the active host mapping table.
534 void showhostmap_live(fd, nsp)
542 bzero((char *)&obj, sizeof(obj));
543 obj.ipfo_rev = IPFILTER_VERSION;
544 obj.ipfo_type = IPFOBJ_GENITER;
545 obj.ipfo_size = sizeof(iter);
546 obj.ipfo_ptr = &iter;
548 iter.igi_type = IPFGENITER_HOSTMAP;
552 printf("\nList of active host mappings:\n");
554 while (nsp->ns_maplist != NULL) {
555 if (ioctl(fd, SIOCGENITER, &obj) == -1)
557 printhostmap(&hm, 0);
558 nsp->ns_maplist = hm.hm_next;
563 void showtqtable_live(fd)
566 ipftq_t table[IPF_TCP_NSTATES];
569 bzero((char *)&obj, sizeof(obj));
570 obj.ipfo_rev = IPFILTER_VERSION;
571 obj.ipfo_size = sizeof(table);
572 obj.ipfo_ptr = (void *)table;
573 obj.ipfo_type = IPFOBJ_STATETQTAB;
575 if (ioctl(fd, SIOCGTQTAB, &obj) == 0) {