xref: /netbsd-src/external/bsd/ipf/dist/tools/ipnat.c (revision b62b29447ba33a97f4c95898332c3cf3b6444fb2)
1 /*	$NetBSD: ipnat.c,v 1.4 2021/04/12 04:09:26 mrg Exp $	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 #if !defined(__SVR4) && !defined(__svr4__)
16 #include <strings.h>
17 #else
18 #include <sys/byteorder.h>
19 #endif
20 #include <sys/time.h>
21 #include <sys/param.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stddef.h>
25 #include <sys/file.h>
26 #define _KERNEL
27 #include <sys/uio.h>
28 #undef _KERNEL
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>
34 #endif
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
39 #include <net/if.h>
40 #if __FreeBSD_version >= 300000
41 # include <net/if_var.h>
42 #endif
43 #include <netdb.h>
44 #include <arpa/nameser.h>
45 #include <arpa/inet.h>
46 #include <resolv.h>
47 #include <ctype.h>
48 #if defined(linux)
49 # include <linux/a.out.h>
50 #else
51 # include <nlist.h>
52 #endif
53 #include "ipf.h"
54 #include "netinet/ipl.h"
55 #include "kmem.h"
56 
57 #ifdef	__hpux
58 # define	nlist	nlist64
59 #endif
60 
61 #if	defined(sun) && !SOLARIS2
62 # define	STRERROR(x)	sys_errlist[x]
63 extern	char	*sys_errlist[];
64 #else
65 # define	STRERROR(x)	strerror(x)
66 #endif
67 
68 #if !defined(lint)
69 static __attribute__((__used__)) const char sccsid[] ="@(#)ipnat.c	1.9 6/5/96 (C) 1993 Darren Reed";
70 static __attribute__((__used__)) const char rcsid[] = "@(#)Id: ipnat.c,v 1.1.1.2 2012/07/22 13:44:56 darrenr Exp $";
71 #endif
72 
73 
74 #if	SOLARIS
75 #define	bzero(a,b)	memset(a,0,b)
76 #endif
77 int	use_inet6 = 0;
78 
79 extern	char	*optarg;
80 
81 void	dostats __P((int, natstat_t *, int, int, int *));
82 void	dotable __P((natstat_t *, int, int, int, char *));
83 void	flushtable __P((int, 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, int *));
89 void	showhostmap_dead __P((natstat_t *));
90 void	showhostmap_live __P((int, natstat_t *));
91 void	dostats_dead __P((natstat_t *, int, int *));
92 int	nat_matcharray __P((nat_t *, int *));
93 
94 int		opts;
95 int		nohdrfields = 0;
96 wordtab_t	*nat_fields = NULL;
97 
usage(name)98 void usage(name)
99 	char *name;
100 {
101 	fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
102 	exit(1);
103 }
104 
105 
main(argc,argv)106 int main(argc, argv)
107 	int argc;
108 	char *argv[];
109 {
110 	int fd, c, mode, *natfilter;
111 	char *file, *core, *kernel;
112 	natstat_t ns, *nsp;
113 	ipfobj_t obj;
114 
115 	fd = -1;
116 	opts = 0;
117 	nsp = &ns;
118 	file = NULL;
119 	core = NULL;
120 	kernel = NULL;
121 	mode = O_RDWR;
122 	natfilter = NULL;
123 
124 	assigndefined(getenv("IPNAT_PREDEFINED"));
125 
126 	while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
127 		switch (c)
128 		{
129 		case 'C' :
130 			opts |= OPT_CLEAR;
131 			break;
132 		case 'd' :
133 			opts |= OPT_DEBUG;
134 			break;
135 		case 'f' :
136 			file = optarg;
137 			break;
138 		case 'F' :
139 			opts |= OPT_FLUSH;
140 			break;
141 		case 'h' :
142 			opts |=OPT_HITS;
143 			break;
144 		case 'l' :
145 			opts |= OPT_LIST;
146 			mode = O_RDONLY;
147 			break;
148 		case 'm' :
149 			natfilter = parseipfexpr(optarg, NULL);
150 			break;
151 		case 'M' :
152 			core = optarg;
153 			break;
154 		case 'N' :
155 			kernel = optarg;
156 			break;
157 		case 'n' :
158 			opts |= OPT_DONOTHING|OPT_DONTOPEN;
159 			mode = O_RDONLY;
160 			break;
161 		case 'O' :
162 			nat_fields = parsefields(natfields, optarg);
163 			break;
164 		case 'p' :
165 			opts |= OPT_PURGE;
166 			break;
167 		case 'R' :
168 			opts |= OPT_NORESOLVE;
169 			break;
170 		case 'r' :
171 			opts |= OPT_REMOVE;
172 			break;
173 		case 's' :
174 			opts |= OPT_STAT;
175 			mode = O_RDONLY;
176 			break;
177 		case 'v' :
178 			opts |= OPT_VERBOSE;
179 			break;
180 		default :
181 			usage(argv[0]);
182 		}
183 
184 	if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
185 		(void) fprintf(stderr, "%s: -p must be used with -r\n",
186 			       argv[0]);
187 		exit(1);
188 	}
189 
190 	initparse();
191 
192 	if ((kernel != NULL) || (core != NULL)) {
193 		(void) setgid(getgid());
194 		(void) setuid(getuid());
195 	}
196 
197 	if (!(opts & OPT_DONOTHING)) {
198 		if (((fd = open(IPNAT_NAME, mode)) == -1) &&
199 		    ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
200 			(void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
201 				STRERROR(errno));
202 			exit(1);
203 		}
204 	}
205 
206 	bzero((char *)&ns, sizeof(ns));
207 
208 	if ((opts & OPT_DONOTHING) == 0) {
209 		if (checkrev(IPL_NAME) == -1) {
210 			fprintf(stderr, "User/kernel version check failed\n");
211 			exit(1);
212 		}
213 	}
214 
215 	if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
216 		bzero((char *)&obj, sizeof(obj));
217 		obj.ipfo_rev = IPFILTER_VERSION;
218 		obj.ipfo_type = IPFOBJ_NATSTAT;
219 		obj.ipfo_size = sizeof(*nsp);
220 		obj.ipfo_ptr = (void *)nsp;
221 		if (ioctl(fd, SIOCGNATS, &obj) == -1) {
222 			ipferror(fd, "ioctl(SIOCGNATS)");
223 			exit(1);
224 		}
225 		(void) setgid(getgid());
226 		(void) setuid(getuid());
227 	} else if ((kernel != NULL) || (core != NULL)) {
228 		if (openkmem(kernel, core) == -1)
229 			exit(1);
230 
231 		natstat_dead(nsp, kernel);
232 		if (opts & (OPT_LIST|OPT_STAT))
233 			dostats(fd, nsp, opts, 0, natfilter);
234 		exit(0);
235 	}
236 
237 	if (opts & (OPT_FLUSH|OPT_CLEAR))
238 		flushtable(fd, opts, natfilter);
239 	if (file) {
240 		return ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
241 	}
242 	if (opts & (OPT_LIST|OPT_STAT))
243 		dostats(fd, nsp, opts, 1, natfilter);
244 	return 0;
245 }
246 
247 
248 /*
249  * Read NAT statistic information in using a symbol table and memory file
250  * rather than doing ioctl's.
251  */
natstat_dead(nsp,kernel)252 void natstat_dead(nsp, kernel)
253 	natstat_t *nsp;
254 	char *kernel;
255 {
256 	struct nlist nat_nlist[10] = {
257 		{ "nat_table" },		/* 0 */
258 		{ "nat_list" },
259 		{ "maptable" },
260 		{ "ipf_nattable_sz" },
261 		{ "ipf_natrules_sz" },
262 		{ "ipf_rdrrules_sz" },		/* 5 */
263 		{ "ipf_hostmap_sz" },
264 		{ "nat_instances" },
265 		{ NULL }
266 	};
267 	void *tables[2];
268 
269 	if (nlist(kernel, nat_nlist) == -1) {
270 		fprintf(stderr, "nlist error\n");
271 		return;
272 	}
273 
274 	/*
275 	 * Normally the ioctl copies all of these values into the structure
276 	 * for us, before returning it to userland, so here we must copy each
277 	 * one in individually.
278 	 */
279 	kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
280 	nsp->ns_side[0].ns_table = tables[0];
281 	nsp->ns_side[1].ns_table = tables[1];
282 
283 	kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
284 		sizeof(nsp->ns_list));
285 	kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
286 		sizeof(nsp->ns_maptable));
287 	kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
288 		sizeof(nsp->ns_nattab_sz));
289 	kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
290 		sizeof(nsp->ns_rultab_sz));
291 	kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
292 		sizeof(nsp->ns_rdrtab_sz));
293 	kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
294 		sizeof(nsp->ns_hostmap_sz));
295 	kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
296 		sizeof(nsp->ns_instances));
297 }
298 
299 
300 /*
301  * Issue an ioctl to flush either the NAT rules table or the active mapping
302  * table or both.
303  */
flushtable(fd,opts,match)304 void flushtable(fd, opts, match)
305 	int fd, opts, *match;
306 {
307 	int n = 0;
308 
309 	if (opts & OPT_FLUSH) {
310 		n = 0;
311 		if (!(opts & OPT_DONOTHING)) {
312 			if (match != NULL) {
313 				ipfobj_t obj;
314 
315 				obj.ipfo_rev = IPFILTER_VERSION;
316 				obj.ipfo_size = match[0] * sizeof(int);
317 				obj.ipfo_type = IPFOBJ_IPFEXPR;
318 				obj.ipfo_ptr = match;
319 				if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
320 					ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
321 					n = -1;
322 				} else {
323 					n = obj.ipfo_retval;
324 				}
325 			} else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
326 				ipferror(fd, "ioctl(SIOCIPFFL)");
327 				n = -1;
328 			}
329 		}
330 		if (n >= 0)
331 			printf("%d entries flushed from NAT table\n", n);
332 	}
333 
334 	if (opts & OPT_CLEAR) {
335 		n = 1;
336 		if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
337 			ipferror(fd, "ioctl(SIOCCNATL)");
338 		else
339 			printf("%d entries flushed from NAT list\n", n);
340 	}
341 }
342 
343 
344 /*
345  * Display NAT statistics.
346  */
dostats_dead(nsp,opts,filter)347 void dostats_dead(nsp, opts, filter)
348 	natstat_t *nsp;
349 	int opts, *filter;
350 {
351 	nat_t *np, nat;
352 	ipnat_t	ipn;
353 	int i;
354 
355 	if (nat_fields == NULL) {
356 		printf("List of active MAP/Redirect filters:\n");
357 		while (nsp->ns_list) {
358 			if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
359 				    sizeof(ipn))) {
360 				perror("kmemcpy");
361 				break;
362 			}
363 			if (opts & OPT_HITS)
364 				printf("%lu ", ipn.in_hits);
365 			printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
366 			nsp->ns_list = ipn.in_next;
367 		}
368 	}
369 
370 	if (nat_fields == NULL) {
371 		printf("\nList of active sessions:\n");
372 
373 	} else if (nohdrfields == 0) {
374 		for (i = 0; nat_fields[i].w_value != 0; i++) {
375 			printfieldhdr(natfields, nat_fields + i);
376 			if (nat_fields[i + 1].w_value != 0)
377 				printf("\t");
378 		}
379 		printf("\n");
380 	}
381 
382 	for (np = nsp->ns_instances; np; np = nat.nat_next) {
383 		if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
384 			break;
385 		if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
386 			continue;
387 		if (nat_fields != NULL) {
388 			for (i = 0; nat_fields[i].w_value != 0; i++) {
389 				printnatfield(&nat, nat_fields[i].w_value);
390 				if (nat_fields[i + 1].w_value != 0)
391 					printf("\t");
392 			}
393 			printf("\n");
394 		} else {
395 			printactivenat(&nat, opts, nsp->ns_ticks);
396 			if (nat.nat_aps) {
397 				int proto;
398 
399 				if (nat.nat_dir & NAT_OUTBOUND)
400 					proto = nat.nat_pr[1];
401 				else
402 					proto = nat.nat_pr[0];
403 				printaps(nat.nat_aps, opts, proto);
404 			}
405 		}
406 	}
407 
408 	if (opts & OPT_VERBOSE)
409 		showhostmap_dead(nsp);
410 }
411 
412 
dotable(nsp,fd,alive,which,side)413 void dotable(nsp, fd, alive, which, side)
414 	natstat_t *nsp;
415 	int fd, alive, which;
416 	char *side;
417 {
418 	int sz, i, used, maxlen, minlen, totallen;
419 	ipftable_t table;
420 	u_int *buckets;
421 	ipfobj_t obj;
422 
423 	sz = sizeof(*buckets) * nsp->ns_nattab_sz;
424 	buckets = (u_int *)malloc(sz);
425 	if (buckets == NULL) {
426 		fprintf(stderr,
427 			"cannot allocate memory (%d) for buckets\n", sz);
428 		return;
429 	}
430 
431 	obj.ipfo_rev = IPFILTER_VERSION;
432 	obj.ipfo_type = IPFOBJ_GTABLE;
433 	obj.ipfo_size = sizeof(table);
434 	obj.ipfo_ptr = &table;
435 
436 	if (which == 0) {
437 		table.ita_type = IPFTABLE_BUCKETS_NATIN;
438 	} else if (which == 1) {
439 		table.ita_type = IPFTABLE_BUCKETS_NATOUT;
440 	}
441 	table.ita_table = buckets;
442 
443 	if (alive) {
444 		if (ioctl(fd, SIOCGTABL, &obj) != 0) {
445 			ipferror(fd, "SIOCFTABL");
446 			free(buckets);
447 			return;
448 		}
449 	} else {
450 		if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
451 			free(buckets);
452 			return;
453 		}
454 	}
455 
456 	minlen = nsp->ns_side[which].ns_inuse;
457 	totallen = 0;
458 	maxlen = 0;
459 	used = 0;
460 
461 	for (i = 0; i < nsp->ns_nattab_sz; i++) {
462 		if (buckets[i] > maxlen)
463 			maxlen = buckets[i];
464 		if (buckets[i] < minlen)
465 			minlen = buckets[i];
466 		if (buckets[i] != 0)
467 			used++;
468 		totallen += buckets[i];
469 	}
470 
471 	printf("%d%%\thash efficiency %s\n",
472 	       totallen ? used * 100 / totallen : 0, side);
473 	printf("%2.2f%%\tbucket usage %s\n",
474 	       ((float)used / nsp->ns_nattab_sz) * 100.0, side);
475 	printf("%d\tminimal length %s\n", minlen, side);
476 	printf("%d\tmaximal length %s\n", maxlen, side);
477 	printf("%.3f\taverage length %s\n",
478 	       used ? ((float)totallen / used) : 0.0, side);
479 
480 	free(buckets);
481 }
482 
483 
dostats(fd,nsp,opts,alive,filter)484 void dostats(fd, nsp, opts, alive, filter)
485 	natstat_t *nsp;
486 	int fd, opts, alive, *filter;
487 {
488 	/*
489 	 * Show statistics ?
490 	 */
491 	if (opts & OPT_STAT) {
492 		printnatside("in", &nsp->ns_side[0]);
493 		dotable(nsp, fd, alive, 0, "in");
494 
495 		printnatside("out", &nsp->ns_side[1]);
496 		dotable(nsp, fd, alive, 1, "out");
497 
498 		printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
499 		printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
500 		printf("%lu\tadded in\n%lu\tadded out\n",
501 			nsp->ns_side[0].ns_added,
502 			nsp->ns_side[1].ns_added);
503 		printf("%u\tactive\n", nsp->ns_active);
504 		printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
505 		printf("%lu\tdivert build\n", nsp->ns_divert_build);
506 		printf("%lu\texpired\n", nsp->ns_expire);
507 		printf("%lu\tflush all\n", nsp->ns_flush_all);
508 		printf("%lu\tflush closing\n", nsp->ns_flush_closing);
509 		printf("%lu\tflush queue\n", nsp->ns_flush_queue);
510 		printf("%lu\tflush state\n", nsp->ns_flush_state);
511 		printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
512 		printf("%lu\thostmap new\n", nsp->ns_hm_new);
513 		printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
514 		printf("%lu\thostmap add\n", nsp->ns_hm_addref);
515 		printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
516 		printf("%lu\tlog ok\n", nsp->ns_log_ok);
517 		printf("%lu\tlog fail\n", nsp->ns_log_fail);
518 		printf("%u\torphan count\n", nsp->ns_orphans);
519 		printf("%u\trule count\n", nsp->ns_rules);
520 		printf("%u\tmap rules\n", nsp->ns_rules_map);
521 		printf("%u\trdr rules\n", nsp->ns_rules_rdr);
522 		printf("%u\twilds\n", nsp->ns_wilds);
523 		if (opts & OPT_VERBOSE)
524 			printf("list %p\n", nsp->ns_list);
525 	}
526 
527 	if (opts & OPT_LIST) {
528 		if (alive)
529 			dostats_live(fd, nsp, opts, filter);
530 		else
531 			dostats_dead(nsp, opts, filter);
532 	}
533 }
534 
535 
536 /*
537  * Display NAT statistics.
538  */
dostats_live(fd,nsp,opts,filter)539 void dostats_live(fd, nsp, opts, filter)
540 	natstat_t *nsp;
541 	int fd, opts, *filter;
542 {
543 	ipfgeniter_t iter;
544 	char buffer[2000];
545 	ipfobj_t obj;
546 	ipnat_t	*ipn;
547 	nat_t nat;
548 	int i;
549 
550 	bzero((char *)&obj, sizeof(obj));
551 	obj.ipfo_rev = IPFILTER_VERSION;
552 	obj.ipfo_type = IPFOBJ_GENITER;
553 	obj.ipfo_size = sizeof(iter);
554 	obj.ipfo_ptr = &iter;
555 
556 	iter.igi_type = IPFGENITER_IPNAT;
557 	iter.igi_nitems = 1;
558 	iter.igi_data = buffer;
559 	ipn = (ipnat_t *)buffer;
560 
561 	/*
562 	 * Show list of NAT rules and NAT sessions ?
563 	 */
564 	if (nat_fields == NULL) {
565 		printf("List of active MAP/Redirect filters:\n");
566 		while (nsp->ns_list) {
567 			if (ioctl(fd, SIOCGENITER, &obj) == -1)
568 				break;
569 			if (opts & OPT_HITS)
570 				printf("%lu ", ipn->in_hits);
571 			printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
572 			nsp->ns_list = ipn->in_next;
573 		}
574 	}
575 
576 	if (nat_fields == NULL) {
577 		printf("\nList of active sessions:\n");
578 
579 	} else if (nohdrfields == 0) {
580 		for (i = 0; nat_fields[i].w_value != 0; i++) {
581 			printfieldhdr(natfields, nat_fields + i);
582 			if (nat_fields[i + 1].w_value != 0)
583 				printf("\t");
584 		}
585 		printf("\n");
586 	}
587 
588 	i = IPFGENITER_IPNAT;
589 	(void) ioctl(fd,SIOCIPFDELTOK, &i);
590 
591 
592 	iter.igi_type = IPFGENITER_NAT;
593 	iter.igi_nitems = 1;
594 	iter.igi_data = &nat;
595 
596 	while (nsp->ns_instances != NULL) {
597 		if (ioctl(fd, SIOCGENITER, &obj) == -1)
598 			break;
599 		if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
600 			continue;
601 		if (nat_fields != NULL) {
602 			for (i = 0; nat_fields[i].w_value != 0; i++) {
603 				printnatfield(&nat, nat_fields[i].w_value);
604 				if (nat_fields[i + 1].w_value != 0)
605 					printf("\t");
606 			}
607 			printf("\n");
608 		} else {
609 			printactivenat(&nat, opts, nsp->ns_ticks);
610 			if (nat.nat_aps) {
611 				int proto;
612 
613 				if (nat.nat_dir & NAT_OUTBOUND)
614 					proto = nat.nat_pr[1];
615 				else
616 					proto = nat.nat_pr[0];
617 				printaps(nat.nat_aps, opts, proto);
618 			}
619 		}
620 		nsp->ns_instances = nat.nat_next;
621 	}
622 
623 	if (opts & OPT_VERBOSE)
624 		showhostmap_live(fd, nsp);
625 
626 	i = IPFGENITER_NAT;
627 	(void) ioctl(fd,SIOCIPFDELTOK, &i);
628 }
629 
630 
631 /*
632  * Display the active host mapping table.
633  */
showhostmap_dead(nsp)634 void showhostmap_dead(nsp)
635 	natstat_t *nsp;
636 {
637 	hostmap_t hm, *hmp, **maptable;
638 	u_int hv;
639 
640 	printf("\nList of active host mappings:\n");
641 
642 	maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
643 					nsp->ns_hostmap_sz);
644 	if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
645 		    sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
646 		perror("kmemcpy (maptable)");
647 		return;
648 	}
649 
650 	for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
651 		hmp = maptable[hv];
652 
653 		while (hmp) {
654 			if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
655 				perror("kmemcpy (hostmap)");
656 				return;
657 			}
658 
659 			printhostmap(&hm, hv);
660 			hmp = hm.hm_next;
661 		}
662 	}
663 	free(maptable);
664 }
665 
666 
667 /*
668  * Display the active host mapping table.
669  */
showhostmap_live(fd,nsp)670 void showhostmap_live(fd, nsp)
671 	int fd;
672 	natstat_t *nsp;
673 {
674 	ipfgeniter_t iter;
675 	hostmap_t hm;
676 	ipfobj_t obj;
677 	int i;
678 
679 	bzero((char *)&obj, sizeof(obj));
680 	obj.ipfo_rev = IPFILTER_VERSION;
681 	obj.ipfo_type = IPFOBJ_GENITER;
682 	obj.ipfo_size = sizeof(iter);
683 	obj.ipfo_ptr = &iter;
684 
685 	iter.igi_type = IPFGENITER_HOSTMAP;
686 	iter.igi_nitems = 1;
687 	iter.igi_data = &hm;
688 
689 	printf("\nList of active host mappings:\n");
690 
691 	while (nsp->ns_maplist != NULL) {
692 		if (ioctl(fd, SIOCGENITER, &obj) == -1)
693 			break;
694 		printhostmap(&hm, hm.hm_hv);
695 		nsp->ns_maplist = hm.hm_next;
696 	}
697 
698 	i = IPFGENITER_HOSTMAP;
699 	(void) ioctl(fd,SIOCIPFDELTOK, &i);
700 }
701 
702 
nat_matcharray(nat,array)703 int nat_matcharray(nat, array)
704 	nat_t *nat;
705 	int *array;
706 {
707 	int i, n, *x, rv, p;
708 	ipfexp_t *e;
709 
710 	rv = 0;
711 	n = array[0];
712 	x = array + 1;
713 
714 	for (; n > 0; x += 3 + x[3], rv = 0) {
715 		e = (ipfexp_t *)x;
716 		if (e->ipfe_cmd == IPF_EXP_END)
717 			break;
718 		n -= e->ipfe_size;
719 
720 		p = e->ipfe_cmd >> 16;
721 		if ((p != 0) && (p != nat->nat_pr[1]))
722 			break;
723 
724 		switch (e->ipfe_cmd)
725 		{
726 		case IPF_EXP_IP_PR :
727 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
728 				rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
729 			}
730 			break;
731 
732 		case IPF_EXP_IP_SRCADDR :
733 			if (nat->nat_v[0] != 4)
734 				break;
735 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
736 				rv |= ((nat->nat_osrcaddr &
737 					e->ipfe_arg0[i * 2 + 1]) ==
738 				       e->ipfe_arg0[i * 2]) ||
739 				      ((nat->nat_nsrcaddr &
740 					e->ipfe_arg0[i * 2 + 1]) ==
741 				       e->ipfe_arg0[i * 2]);
742 			}
743 			break;
744 
745 		case IPF_EXP_IP_DSTADDR :
746 			if (nat->nat_v[0] != 4)
747 				break;
748 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
749 				rv |= ((nat->nat_odstaddr &
750 					e->ipfe_arg0[i * 2 + 1]) ==
751 				       e->ipfe_arg0[i * 2]) ||
752 				      ((nat->nat_ndstaddr &
753 					e->ipfe_arg0[i * 2 + 1]) ==
754 				       e->ipfe_arg0[i * 2]);
755 			}
756 			break;
757 
758 		case IPF_EXP_IP_ADDR :
759 			if (nat->nat_v[0] != 4)
760 				break;
761 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
762 				rv |= ((nat->nat_osrcaddr &
763 					e->ipfe_arg0[i * 2 + 1]) ==
764 				       e->ipfe_arg0[i * 2]) ||
765 				      ((nat->nat_nsrcaddr &
766 					e->ipfe_arg0[i * 2 + 1]) ==
767 				       e->ipfe_arg0[i * 2]) ||
768 				     ((nat->nat_odstaddr &
769 					e->ipfe_arg0[i * 2 + 1]) ==
770 				       e->ipfe_arg0[i * 2]) ||
771 				     ((nat->nat_ndstaddr &
772 					e->ipfe_arg0[i * 2 + 1]) ==
773 				       e->ipfe_arg0[i * 2]);
774 			}
775 			break;
776 
777 #ifdef USE_INET6
778 		case IPF_EXP_IP6_SRCADDR :
779 			if (nat->nat_v[0] != 6)
780 				break;
781 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
782 				rv |= IP6_MASKEQ(&nat->nat_osrc6,
783 						 &e->ipfe_arg0[i * 8 + 4],
784 						 &e->ipfe_arg0[i * 8]) ||
785 				      IP6_MASKEQ(&nat->nat_nsrc6,
786 						 &e->ipfe_arg0[i * 8 + 4],
787 						 &e->ipfe_arg0[i * 8]);
788 			}
789 			break;
790 
791 		case IPF_EXP_IP6_DSTADDR :
792 			if (nat->nat_v[0] != 6)
793 				break;
794 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
795 				rv |= IP6_MASKEQ(&nat->nat_odst6,
796 						 &e->ipfe_arg0[i * 8 + 4],
797 						 &e->ipfe_arg0[i * 8]) ||
798 				      IP6_MASKEQ(&nat->nat_ndst6,
799 						 &e->ipfe_arg0[i * 8 + 4],
800 						 &e->ipfe_arg0[i * 8]);
801 			}
802 			break;
803 
804 		case IPF_EXP_IP6_ADDR :
805 			if (nat->nat_v[0] != 6)
806 				break;
807 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
808 				rv |= IP6_MASKEQ(&nat->nat_osrc6,
809 						 &e->ipfe_arg0[i * 8 + 4],
810 						 &e->ipfe_arg0[i * 8]) ||
811 				      IP6_MASKEQ(&nat->nat_nsrc6,
812 						 &e->ipfe_arg0[i * 8 + 4],
813 						 &e->ipfe_arg0[i * 8]) ||
814 				      IP6_MASKEQ(&nat->nat_odst6,
815 						 &e->ipfe_arg0[i * 8 + 4],
816 						 &e->ipfe_arg0[i * 8]) ||
817 				      IP6_MASKEQ(&nat->nat_ndst6,
818 						 &e->ipfe_arg0[i * 8 + 4],
819 						 &e->ipfe_arg0[i * 8]);
820 			}
821 			break;
822 #endif
823 
824 		case IPF_EXP_UDP_PORT :
825 		case IPF_EXP_TCP_PORT :
826 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
827 				rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
828 				      (nat->nat_nsport == e->ipfe_arg0[i]) ||
829 				      (nat->nat_odport == e->ipfe_arg0[i]) ||
830 				      (nat->nat_ndport == e->ipfe_arg0[i]);
831 			}
832 			break;
833 
834 		case IPF_EXP_UDP_SPORT :
835 		case IPF_EXP_TCP_SPORT :
836 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
837 				rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
838 				      (nat->nat_nsport == e->ipfe_arg0[i]);
839 			}
840 			break;
841 
842 		case IPF_EXP_UDP_DPORT :
843 		case IPF_EXP_TCP_DPORT :
844 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
845 				rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
846 				      (nat->nat_ndport == e->ipfe_arg0[i]);
847 			}
848 			break;
849 		}
850 		rv ^= e->ipfe_not;
851 
852 		if (rv == 0)
853 			break;
854 	}
855 
856 	return rv;
857 }
858