xref: /titanic_51/usr/src/cmd/ipf/tools/ipnat.c (revision b60f2a0b921611326383e4789e0874e9e8a2e708)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
7  *
8  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
9  * Use is subject to license terms.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #include <stdio.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #if !defined(__SVR4) && !defined(__svr4__)
20 #include <strings.h>
21 #else
22 #include <sys/byteorder.h>
23 #endif
24 #include <sys/time.h>
25 #include <sys/param.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stddef.h>
29 #include <sys/file.h>
30 #define _KERNEL
31 #include <sys/uio.h>
32 #undef _KERNEL
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
36 # include <sys/ioccom.h>
37 # include <sys/sysmacros.h>
38 #endif
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/tcp.h>
43 #include <net/if.h>
44 #if __FreeBSD_version >= 300000
45 # include <net/if_var.h>
46 #endif
47 #include <netdb.h>
48 #include <arpa/nameser.h>
49 #include <arpa/inet.h>
50 #include <resolv.h>
51 #include <ctype.h>
52 #if defined(linux)
53 # include <linux/a.out.h>
54 #else
55 # include <nlist.h>
56 #endif
57 #include "ipf.h"
58 #include "netinet/ipl.h"
59 #include "kmem.h"
60 
61 #ifdef	__hpux
62 # define	nlist	nlist64
63 #endif
64 
65 #if	defined(sun) && !SOLARIS2
66 # define	STRERROR(x)	sys_errlist[x]
67 extern	char	*sys_errlist[];
68 #else
69 # define	STRERROR(x)	strerror(x)
70 #endif
71 
72 #if !defined(lint)
73 static const char sccsid[] ="@(#)ipnat.c	1.9 6/5/96 (C) 1993 Darren Reed";
74 static const char rcsid[] = "@(#)$Id: ipnat.c,v 1.24.2.2 2005/05/10 21:19:30 darrenr Exp $";
75 #endif
76 
77 
78 #if	SOLARIS
79 #define	bzero(a,b)	memset(a,0,b)
80 #endif
81 int	use_inet6 = 0;
82 char	thishost[MAXHOSTNAMELEN];
83 
84 extern	char	*optarg;
85 
86 void	dostats __P((int, natstat_t *, int, int));
87 void	flushtable __P((int, int));
88 void	usage __P((char *));
89 int	main __P((int, char*[]));
90 void	showhostmap __P((natstat_t *nsp));
91 void	natstat_dead __P((natstat_t *, char *));
92 void	dostats_live __P((int, natstat_t *, int));
93 void	showhostmap_live __P((int, natstat_t *));
94 
95 int	opts;
96 
97 void usage(name)
98 char *name;
99 {
100 	fprintf(stderr, "Usage: %s [-CdFhlnrRsv] [-f filename]\n", name);
101 	exit(1);
102 }
103 
104 
105 int main(argc, argv)
106 int argc;
107 char *argv[];
108 {
109 	char *file, *core, *kernel;
110 	natstat_t ns, *nsp;
111 	int fd, c, mode;
112 	ipfobj_t obj;
113 
114 	fd = -1;
115 	opts = 0;
116 	nsp = &ns;
117 	file = NULL;
118 	core = NULL;
119 	kernel = NULL;
120 	mode = O_RDWR;
121 
122 	while ((c = getopt(argc, argv, "CdFf:hlM:N:nrRsv")) != -1)
123 		switch (c)
124 		{
125 		case 'C' :
126 			opts |= OPT_CLEAR;
127 			break;
128 		case 'd' :
129 			opts |= OPT_DEBUG;
130 			break;
131 		case 'f' :
132 			file = optarg;
133 			break;
134 		case 'F' :
135 			opts |= OPT_FLUSH;
136 			break;
137 		case 'h' :
138 			opts |=OPT_HITS;
139 			break;
140 		case 'l' :
141 			opts |= OPT_LIST;
142 			mode = O_RDONLY;
143 			break;
144 		case 'M' :
145 			core = optarg;
146 			break;
147 		case 'N' :
148 			kernel = optarg;
149 			break;
150 		case 'n' :
151 			opts |= OPT_DONOTHING;
152 			mode = O_RDONLY;
153 			break;
154 		case 'R' :
155 			opts |= OPT_NORESOLVE;
156 			break;
157 		case 'r' :
158 			opts |= OPT_REMOVE;
159 			break;
160 		case 's' :
161 			opts |= OPT_STAT;
162 			mode = O_RDONLY;
163 			break;
164 		case 'v' :
165 			opts |= OPT_VERBOSE;
166 			break;
167 		default :
168 			usage(argv[0]);
169 		}
170 
171 	initparse();
172 
173 	if ((kernel != NULL) || (core != NULL)) {
174 		(void) setgid(getgid());
175 		(void) setreuid(getuid(), getuid());
176 	}
177 
178 	bzero((char *)&ns, sizeof(ns));
179 
180 	if ((opts & OPT_DONOTHING) == 0) {
181 		if (checkrev(IPL_NAME) == -1) {
182 			fprintf(stderr, "User/kernel version check failed\n");
183 			exit(1);
184 		}
185 	}
186 
187 
188 	if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
189 #ifdef notdef
190 		if (openkmem(kernel, core) == -1)
191 			exit(1);
192 #endif
193 		if (((fd = open(IPNAT_NAME, mode)) == -1) &&
194 		    ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
195 			(void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
196 				STRERROR(errno));
197 			exit(1);
198 		}
199 
200 		bzero((char *)&obj, sizeof(obj));
201 		obj.ipfo_rev = IPFILTER_VERSION;
202 		obj.ipfo_size = sizeof(*nsp);
203 		obj.ipfo_type = IPFOBJ_NATSTAT;
204 		obj.ipfo_ptr = (void *)nsp;
205 		if (ioctl(fd, SIOCGNATS, &obj) == -1) {
206 			perror("ioctl(SIOCGNATS)");
207 			exit(1);
208 		}
209 		(void) setgid(getgid());
210 		(void) setreuid(getuid(), getuid());
211 	} else if ((kernel != NULL) || (core != NULL)) {
212 		if (openkmem(kernel, core) == -1)
213 			exit(1);
214 
215 		natstat_dead(nsp, kernel);
216 		if (opts & (OPT_LIST|OPT_STAT))
217 			dostats(fd, nsp, opts, 0);
218 		exit(0);
219 	}
220 
221 	if (opts & (OPT_FLUSH|OPT_CLEAR))
222 		flushtable(fd, opts);
223 	if (file) {
224 		ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
225 	}
226 	if (opts & (OPT_LIST|OPT_STAT))
227 		dostats(fd, nsp, opts, 1);
228 	return 0;
229 }
230 
231 
232 /*
233  * Read NAT statistic information in using a symbol table and memory file
234  * rather than doing ioctl's.
235  */
236 void natstat_dead(nsp, kernel)
237 natstat_t *nsp;
238 char *kernel;
239 {
240 	struct nlist nat_nlist[10] = {
241 		{ "nat_table" },		/* 0 */
242 		{ "nat_list" },
243 		{ "maptable" },
244 		{ "ipf_nattable_sz" },
245 		{ "ipf_natrules_sz" },
246 		{ "ipf_rdrrules_sz" },		/* 5 */
247 		{ "ipf_hostmap_sz" },
248 		{ "nat_instances" },
249 		{ "ap_sess_list" },
250 		{ NULL }
251 	};
252 	void *tables[2];
253 
254 	if (nlist(kernel, nat_nlist) == -1) {
255 		fprintf(stderr, "nlist error\n");
256 		return;
257 	}
258 
259 	/*
260 	 * Normally the ioctl copies all of these values into the structure
261 	 * for us, before returning it to userland, so here we must copy each
262 	 * one in individually.
263 	 */
264 	kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
265 	nsp->ns_table[0] = tables[0];
266 	nsp->ns_table[1] = tables[1];
267 
268 	kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
269 		sizeof(nsp->ns_list));
270 	kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
271 		sizeof(nsp->ns_maptable));
272 	kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
273 		sizeof(nsp->ns_nattab_sz));
274 	kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
275 		sizeof(nsp->ns_rultab_sz));
276 	kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
277 		sizeof(nsp->ns_rdrtab_sz));
278 	kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
279 		sizeof(nsp->ns_hostmap_sz));
280 	kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
281 		sizeof(nsp->ns_instances));
282 	kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
283 		sizeof(nsp->ns_apslist));
284 }
285 
286 
287 /*
288  * Display NAT statistics.
289  */
290 void dostats(fd, nsp, opts, alive)
291 natstat_t *nsp;
292 int fd, opts, alive;
293 {
294 	nat_t *np, nat;
295 	ipnat_t	ipn;
296 
297 	/*
298 	 * Show statistics ?
299 	 */
300 	if (opts & OPT_STAT) {
301 		printf("mapped\tin\t%lu\tout\t%lu\n",
302 			nsp->ns_mapped[0], nsp->ns_mapped[1]);
303 		printf("added\t%lu\texpired\t%lu\n",
304 			nsp->ns_added, nsp->ns_expire);
305 		printf("no memory\t%lu\tbad nat\t%lu\n",
306 			nsp->ns_memfail, nsp->ns_badnat);
307 		printf("inuse\t%lu\nrules\t%lu\n",
308 			nsp->ns_inuse, nsp->ns_rules);
309 		printf("wilds\t%u\n", nsp->ns_wilds);
310 		if (opts & OPT_VERBOSE)
311 			printf("table %p list %p\n",
312 				nsp->ns_table, nsp->ns_list);
313 	}
314 
315 	/*
316 	 * Show list of NAT rules and NAT sessions ?
317 	 */
318 	if (opts & OPT_LIST) {
319 		if (alive) {
320 			dostats_live(fd, nsp, opts);
321 			return;
322 		}
323 		printf("List of active MAP/Redirect filters:\n");
324 		while (nsp->ns_list) {
325 			if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
326 				    sizeof(ipn))) {
327 				perror("kmemcpy");
328 				break;
329 			}
330 			if (opts & OPT_HITS)
331 				printf("%lu ", ipn.in_hits);
332 			printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
333 			nsp->ns_list = ipn.in_next;
334 		}
335 
336 		printf("\nList of active sessions:\n");
337 
338 		for (np = nsp->ns_instances; np; np = nat.nat_next) {
339 			if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
340 				break;
341 			printactivenat(&nat, opts, 0);
342 			if (nat.nat_aps)
343 				printaps(nat.nat_aps, opts);
344 		}
345 
346 		if (opts & OPT_VERBOSE)
347 			showhostmap(nsp);
348 	}
349 }
350 
351 
352 /*
353  * Display the active host mapping table.
354  */
355 void showhostmap(nsp)
356 natstat_t *nsp;
357 {
358 	hostmap_t hm, *hmp, **maptable;
359 	u_int hv;
360 
361 	printf("\nList of active host mappings:\n");
362 
363 	maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
364 					nsp->ns_hostmap_sz);
365 	if (maptable == NULL) {
366 		perror("malloc");
367 		exit(1);
368 	}
369 	if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
370 		    sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
371 		perror("kmemcpy (maptable)");
372 		return;
373 	}
374 
375 	for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
376 		hmp = maptable[hv];
377 
378 		while (hmp) {
379 			if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
380 				perror("kmemcpy (hostmap)");
381 				return;
382 			}
383 
384 			printhostmap(&hm, hv);
385 			hmp = hm.hm_next;
386 		}
387 	}
388 	free(maptable);
389 }
390 
391 
392 /*
393  * Issue an ioctl to flush either the NAT rules table or the active mapping
394  * table or both.
395  */
396 void flushtable(fd, opts)
397 int fd, opts;
398 {
399 	int n = 0;
400 
401 	if (opts & OPT_FLUSH) {
402 		n = 0;
403 		if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
404 			perror("ioctl(SIOCFLNAT)");
405 		else
406 			printf("%d entries flushed from NAT table\n", n);
407 	}
408 
409 	if (opts & OPT_CLEAR) {
410 		n = 1;
411 		if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
412 			perror("ioctl(SIOCCNATL)");
413 		else
414 			printf("%d entries flushed from NAT list\n", n);
415 	}
416 }
417 
418 /*
419  * Display NAT statistics.
420  */
421 void dostats_live(fd, nsp, opts)
422 natstat_t *nsp;
423 int fd, opts;
424 {
425 	ipfgeniter_t iter;
426 	ipfobj_t obj;
427 	ipnat_t	ipn;
428 	nat_t nat;
429 
430 	bzero((char *)&obj, sizeof(obj));
431 	obj.ipfo_rev = IPFILTER_VERSION;
432 	obj.ipfo_type = IPFOBJ_GENITER;
433 	obj.ipfo_size = sizeof(iter);
434 	obj.ipfo_ptr = &iter;
435 
436 	iter.igi_type = IPFGENITER_IPNAT;
437 	iter.igi_nitems = 1;
438 	iter.igi_data = &ipn;
439 
440 	/*
441 	 * Show list of NAT rules and NAT sessions ?
442 	 */
443 	printf("List of active MAP/Redirect filters:\n");
444 	while (nsp->ns_list) {
445 		if (ioctl(fd, SIOCGENITER, &obj) == -1)
446 			break;
447 		if (opts & OPT_HITS)
448 			printf("%lu ", ipn.in_hits);
449 		printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
450 		nsp->ns_list = ipn.in_next;
451 	}
452 
453 	printf("\nList of active sessions:\n");
454 
455 	iter.igi_type = IPFGENITER_NAT;
456 	iter.igi_nitems = 1;
457 	iter.igi_data = &nat;
458 
459 	while (nsp->ns_instances != NULL) {
460 		if (ioctl(fd, SIOCGENITER, &obj) == -1)
461 			break;
462 		printactivenat(&nat, opts, 1);
463 		if (nat.nat_aps)
464 			printaps(nat.nat_aps, opts);
465 		nsp->ns_instances = nat.nat_next;
466 	}
467 
468 	if (opts & OPT_VERBOSE)
469 		showhostmap_live(fd, nsp);
470 }
471 
472 /*
473  * Display the active host mapping table.
474  */
475 void showhostmap_live(fd, nsp)
476 int fd;
477 natstat_t *nsp;
478 {
479 	hostmap_t hm, *hmp;
480 	ipfgeniter_t iter;
481 	ipfobj_t obj;
482 
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;
488 
489 	iter.igi_type = IPFGENITER_HOSTMAP;
490 	iter.igi_nitems = 1;
491 	iter.igi_data = &hm;
492 
493 	printf("\nList of active host mappings:\n");
494 
495 	while (nsp->ns_maplist != NULL) {
496 		if (ioctl(fd, SIOCGENITER, &obj) == -1)
497 			break;
498 		printhostmap(&hm, 0);
499 		nsp->ns_maplist = hm.hm_next;
500 	}
501 }
502