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.6 2007/05/11 11:16:55 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 flushtable __P((int, int));
84 void usage __P((char *));
85 int main __P((int, char*[]));
86 void showhostmap __P((natstat_t *nsp));
87 void natstat_dead __P((natstat_t *, char *));
88 void dostats_live __P((int, natstat_t *, int));
89 void showhostmap_dead __P((natstat_t *));
90 void showhostmap_live __P((int, natstat_t *));
91 void dostats_dead __P((natstat_t *, int));
92 void showtqtable_live __P((int));
99 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
108 char *file, *core, *kernel;
121 while ((c = getopt(argc, argv, "CdFf:hlM:N:nrRsv")) != -1)
150 opts |= OPT_DONOTHING;
154 opts |= OPT_NORESOLVE;
172 if ((kernel != NULL) || (core != NULL)) {
173 (void) setgid(getgid());
174 (void) setuid(getuid());
177 if (!(opts & OPT_DONOTHING)) {
178 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
179 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
180 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
186 bzero((char *)&ns, sizeof(ns));
188 if ((opts & OPT_DONOTHING) == 0) {
189 if (checkrev(IPL_NAME) == -1) {
190 fprintf(stderr, "User/kernel version check failed\n");
195 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
196 bzero((char *)&obj, sizeof(obj));
197 obj.ipfo_rev = IPFILTER_VERSION;
198 obj.ipfo_type = IPFOBJ_NATSTAT;
199 obj.ipfo_size = sizeof(*nsp);
200 obj.ipfo_ptr = (void *)nsp;
201 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
202 perror("ioctl(SIOCGNATS)");
205 (void) setgid(getgid());
206 (void) setuid(getuid());
207 } else if ((kernel != NULL) || (core != NULL)) {
208 if (openkmem(kernel, core) == -1)
211 natstat_dead(nsp, kernel);
212 if (opts & (OPT_LIST|OPT_STAT))
213 dostats(fd, nsp, opts, 0);
217 if (opts & (OPT_FLUSH|OPT_CLEAR))
218 flushtable(fd, opts);
220 ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
222 if (opts & (OPT_LIST|OPT_STAT))
223 dostats(fd, nsp, opts, 1);
229 * Read NAT statistic information in using a symbol table and memory file
230 * rather than doing ioctl's.
232 void natstat_dead(nsp, kernel)
236 struct nlist nat_nlist[10] = {
237 { "nat_table" }, /* 0 */
240 { "ipf_nattable_sz" },
241 { "ipf_natrules_sz" },
242 { "ipf_rdrrules_sz" }, /* 5 */
243 { "ipf_hostmap_sz" },
250 if (nlist(kernel, nat_nlist) == -1) {
251 fprintf(stderr, "nlist error\n");
256 * Normally the ioctl copies all of these values into the structure
257 * for us, before returning it to userland, so here we must copy each
258 * one in individually.
260 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
261 nsp->ns_table[0] = tables[0];
262 nsp->ns_table[1] = tables[1];
264 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
265 sizeof(nsp->ns_list));
266 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
267 sizeof(nsp->ns_maptable));
268 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
269 sizeof(nsp->ns_nattab_sz));
270 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
271 sizeof(nsp->ns_rultab_sz));
272 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
273 sizeof(nsp->ns_rdrtab_sz));
274 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
275 sizeof(nsp->ns_hostmap_sz));
276 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
277 sizeof(nsp->ns_instances));
278 kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
279 sizeof(nsp->ns_apslist));
284 * Issue an ioctl to flush either the NAT rules table or the active mapping
287 void flushtable(fd, opts)
292 if (opts & OPT_FLUSH) {
294 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
295 perror("ioctl(SIOCFLNAT)");
297 printf("%d entries flushed from NAT table\n", n);
300 if (opts & OPT_CLEAR) {
302 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
303 perror("ioctl(SIOCCNATL)");
305 printf("%d entries flushed from NAT list\n", n);
311 * Display NAT statistics.
313 void dostats_dead(nsp, opts)
320 printf("List of active MAP/Redirect filters:\n");
321 while (nsp->ns_list) {
322 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
328 printf("%lu ", ipn.in_hits);
329 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
330 nsp->ns_list = ipn.in_next;
333 printf("\nList of active sessions:\n");
335 for (np = nsp->ns_instances; np; np = nat.nat_next) {
336 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
338 printactivenat(&nat, opts, 0, nsp->ns_ticks);
340 printaps(nat.nat_aps, opts);
343 if (opts & OPT_VERBOSE)
344 showhostmap_dead(nsp);
348 void dostats(fd, nsp, opts, alive)
355 if (opts & OPT_STAT) {
356 printf("mapped\tin\t%lu\tout\t%lu\n",
357 nsp->ns_mapped[0], nsp->ns_mapped[1]);
358 printf("added\t%lu\texpired\t%lu\n",
359 nsp->ns_added, nsp->ns_expire);
360 printf("no memory\t%lu\tbad nat\t%lu\n",
361 nsp->ns_memfail, nsp->ns_badnat);
362 printf("inuse\t%lu\nrules\t%lu\n",
363 nsp->ns_inuse, nsp->ns_rules);
364 printf("wilds\t%u\n", nsp->ns_wilds);
365 if (opts & OPT_VERBOSE)
366 printf("table %p list %p\n",
367 nsp->ns_table, nsp->ns_list);
369 showtqtable_live(fd);
372 if (opts & OPT_LIST) {
374 dostats_live(fd, nsp, opts);
376 dostats_dead(nsp, opts);
382 * Display NAT statistics.
384 void dostats_live(fd, nsp, opts)
393 bzero((char *)&obj, sizeof(obj));
394 obj.ipfo_rev = IPFILTER_VERSION;
395 obj.ipfo_type = IPFOBJ_GENITER;
396 obj.ipfo_size = sizeof(iter);
397 obj.ipfo_ptr = &iter;
399 iter.igi_type = IPFGENITER_IPNAT;
401 iter.igi_data = &ipn;
404 * Show list of NAT rules and NAT sessions ?
406 printf("List of active MAP/Redirect filters:\n");
407 while (nsp->ns_list) {
408 if (ioctl(fd, SIOCGENITER, &obj) == -1)
411 printf("%lu ", ipn.in_hits);
412 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
413 nsp->ns_list = ipn.in_next;
416 printf("\nList of active sessions:\n");
418 iter.igi_type = IPFGENITER_NAT;
420 iter.igi_data = &nat;
422 while (nsp->ns_instances != NULL) {
423 if (ioctl(fd, SIOCGENITER, &obj) == -1)
425 printactivenat(&nat, opts, 1, nsp->ns_ticks);
427 printaps(nat.nat_aps, opts);
428 nsp->ns_instances = nat.nat_next;
431 if (opts & OPT_VERBOSE)
432 showhostmap_live(fd, nsp);
437 * Display the active host mapping table.
439 void showhostmap_dead(nsp)
442 hostmap_t hm, *hmp, **maptable;
445 printf("\nList of active host mappings:\n");
447 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
449 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
450 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
451 perror("kmemcpy (maptable)");
455 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
459 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
460 perror("kmemcpy (hostmap)");
464 printhostmap(&hm, hv);
473 * Display the active host mapping table.
475 void showhostmap_live(fd, nsp)
483 bzero((char *)&obj, sizeof(obj));
484 obj.ipfo_rev = IPFILTER_VERSION;
485 obj.ipfo_type = IPFOBJ_GENITER;
486 obj.ipfo_size = sizeof(iter);
487 obj.ipfo_ptr = &iter;
489 iter.igi_type = IPFGENITER_HOSTMAP;
493 printf("\nList of active host mappings:\n");
495 while (nsp->ns_maplist != NULL) {
496 if (ioctl(fd, SIOCGENITER, &obj) == -1)
498 printhostmap(&hm, 0);
499 nsp->ns_maplist = hm.hm_next;
504 void showtqtable_live(fd)
507 ipftq_t table[IPF_TCP_NSTATES];
510 bzero((char *)&obj, sizeof(obj));
511 obj.ipfo_rev = IPFILTER_VERSION;
512 obj.ipfo_size = sizeof(table);
513 obj.ipfo_ptr = (void *)table;
514 obj.ipfo_type = IPFOBJ_STATETQTAB;
516 if (ioctl(fd, SIOCGTQTAB, &obj) == 0) {