]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/tools/ipnat.c
This commit was generated by cvs2svn to compensate for changes in r171322,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / tools / ipnat.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2001-2006 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 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 $";
71 #endif
72
73
74 #if     SOLARIS
75 #define bzero(a,b)      memset(a,0,b)
76 #endif
77 int     use_inet6 = 0;
78 char    thishost[MAXHOSTNAMELEN];
79
80 extern  char    *optarg;
81
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));
93
94 int     opts;
95
96 void usage(name)
97 char *name;
98 {
99         fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
100         exit(1);
101 }
102
103
104 int main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108         char *file, *core, *kernel;
109         natstat_t ns, *nsp;
110         int fd, c, mode;
111         ipfobj_t obj;
112
113         fd = -1;
114         opts = 0;
115         nsp = &ns;
116         file = NULL;
117         core = NULL;
118         kernel = NULL;
119         mode = O_RDWR;
120
121         while ((c = getopt(argc, argv, "CdFf:hlM:N:nrRsv")) != -1)
122                 switch (c)
123                 {
124                 case 'C' :
125                         opts |= OPT_CLEAR;
126                         break;
127                 case 'd' :
128                         opts |= OPT_DEBUG;
129                         break;
130                 case 'f' :
131                         file = optarg;
132                         break;
133                 case 'F' :
134                         opts |= OPT_FLUSH;
135                         break;
136                 case 'h' :
137                         opts |=OPT_HITS;
138                         break;
139                 case 'l' :
140                         opts |= OPT_LIST;
141                         mode = O_RDONLY;
142                         break;
143                 case 'M' :
144                         core = optarg;
145                         break;
146                 case 'N' :
147                         kernel = optarg;
148                         break;
149                 case 'n' :
150                         opts |= OPT_DONOTHING;
151                         mode = O_RDONLY;
152                         break;
153                 case 'R' :
154                         opts |= OPT_NORESOLVE;
155                         break;
156                 case 'r' :
157                         opts |= OPT_REMOVE;
158                         break;
159                 case 's' :
160                         opts |= OPT_STAT;
161                         mode = O_RDONLY;
162                         break;
163                 case 'v' :
164                         opts |= OPT_VERBOSE;
165                         break;
166                 default :
167                         usage(argv[0]);
168                 }
169
170         initparse();
171
172         if ((kernel != NULL) || (core != NULL)) {
173                 (void) setgid(getgid());
174                 (void) setuid(getuid());
175         }
176
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,
181                                 STRERROR(errno));
182                         exit(1);
183                 }
184         }
185
186         bzero((char *)&ns, sizeof(ns));
187
188         if ((opts & OPT_DONOTHING) == 0) {
189                 if (checkrev(IPL_NAME) == -1) {
190                         fprintf(stderr, "User/kernel version check failed\n");
191                         exit(1);
192                 }
193         }
194
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)");
203                         exit(1);
204                 }
205                 (void) setgid(getgid());
206                 (void) setuid(getuid());
207         } else if ((kernel != NULL) || (core != NULL)) {
208                 if (openkmem(kernel, core) == -1)
209                         exit(1);
210
211                 natstat_dead(nsp, kernel);
212                 if (opts & (OPT_LIST|OPT_STAT))
213                         dostats(fd, nsp, opts, 0);
214                 exit(0);
215         }
216
217         if (opts & (OPT_FLUSH|OPT_CLEAR))
218                 flushtable(fd, opts);
219         if (file) {
220                 ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
221         }
222         if (opts & (OPT_LIST|OPT_STAT))
223                 dostats(fd, nsp, opts, 1);
224         return 0;
225 }
226
227
228 /*
229  * Read NAT statistic information in using a symbol table and memory file
230  * rather than doing ioctl's.
231  */
232 void natstat_dead(nsp, kernel)
233 natstat_t *nsp;
234 char *kernel;
235 {
236         struct nlist nat_nlist[10] = {
237                 { "nat_table" },                /* 0 */
238                 { "nat_list" },
239                 { "maptable" },
240                 { "ipf_nattable_sz" },
241                 { "ipf_natrules_sz" },
242                 { "ipf_rdrrules_sz" },          /* 5 */
243                 { "ipf_hostmap_sz" },
244                 { "nat_instances" },
245                 { "ap_sess_list" },
246                 { NULL }
247         };
248         void *tables[2];
249
250         if (nlist(kernel, nat_nlist) == -1) {
251                 fprintf(stderr, "nlist error\n");
252                 return;
253         }
254
255         /*
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.
259          */
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];
263
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));
280 }
281
282
283 /*
284  * Issue an ioctl to flush either the NAT rules table or the active mapping
285  * table or both.
286  */
287 void flushtable(fd, opts)
288 int fd, opts;
289 {
290         int n = 0;
291
292         if (opts & OPT_FLUSH) {
293                 n = 0;
294                 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
295                         perror("ioctl(SIOCFLNAT)");
296                 else
297                         printf("%d entries flushed from NAT table\n", n);
298         }
299
300         if (opts & OPT_CLEAR) {
301                 n = 1;
302                 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
303                         perror("ioctl(SIOCCNATL)");
304                 else
305                         printf("%d entries flushed from NAT list\n", n);
306         }
307 }
308
309
310 /*
311  * Display NAT statistics.
312  */
313 void dostats_dead(nsp, opts)
314 natstat_t *nsp;
315 int opts;
316 {
317         nat_t *np, nat;
318         ipnat_t ipn;
319
320         printf("List of active MAP/Redirect filters:\n");
321         while (nsp->ns_list) {
322                 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
323                             sizeof(ipn))) {
324                         perror("kmemcpy");
325                         break;
326                 }
327                 if (opts & OPT_HITS)
328                         printf("%lu ", ipn.in_hits);
329                 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
330                 nsp->ns_list = ipn.in_next;
331         }
332
333         printf("\nList of active sessions:\n");
334
335         for (np = nsp->ns_instances; np; np = nat.nat_next) {
336                 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
337                         break;
338                 printactivenat(&nat, opts, 0, nsp->ns_ticks);
339                 if (nat.nat_aps)
340                         printaps(nat.nat_aps, opts);
341         }
342
343         if (opts & OPT_VERBOSE)
344                 showhostmap_dead(nsp);
345 }
346
347
348 void dostats(fd, nsp, opts, alive)
349 natstat_t *nsp;
350 int fd, opts, alive;
351 {
352         /*
353          * Show statistics ?
354          */
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);
368                 if (alive)
369                         showtqtable_live(fd);
370         }
371
372         if (opts & OPT_LIST) {
373                 if (alive)
374                         dostats_live(fd, nsp, opts);
375                 else
376                         dostats_dead(nsp, opts);
377         }
378 }
379
380
381 /*
382  * Display NAT statistics.
383  */
384 void dostats_live(fd, nsp, opts)
385 natstat_t *nsp;
386 int fd, opts;
387 {
388         ipfgeniter_t iter;
389         ipfobj_t obj;
390         ipnat_t ipn;
391         nat_t nat;
392
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;
398
399         iter.igi_type = IPFGENITER_IPNAT;
400         iter.igi_nitems = 1;
401         iter.igi_data = &ipn;
402
403         /*
404          * Show list of NAT rules and NAT sessions ?
405          */
406         printf("List of active MAP/Redirect filters:\n");
407         while (nsp->ns_list) {
408                 if (ioctl(fd, SIOCGENITER, &obj) == -1)
409                         break;
410                 if (opts & OPT_HITS)
411                         printf("%lu ", ipn.in_hits);
412                 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
413                 nsp->ns_list = ipn.in_next;
414         }
415
416         printf("\nList of active sessions:\n");
417
418         iter.igi_type = IPFGENITER_NAT;
419         iter.igi_nitems = 1;
420         iter.igi_data = &nat;
421
422         while (nsp->ns_instances != NULL) {
423                 if (ioctl(fd, SIOCGENITER, &obj) == -1)
424                         break;
425                 printactivenat(&nat, opts, 1, nsp->ns_ticks);
426                 if (nat.nat_aps)
427                         printaps(nat.nat_aps, opts);
428                 nsp->ns_instances = nat.nat_next;
429         }
430
431         if (opts & OPT_VERBOSE)
432                 showhostmap_live(fd, nsp);
433 }
434
435
436 /*
437  * Display the active host mapping table.
438  */
439 void showhostmap_dead(nsp)
440 natstat_t *nsp;
441 {
442         hostmap_t hm, *hmp, **maptable;
443         u_int hv;
444
445         printf("\nList of active host mappings:\n");
446
447         maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
448                                         nsp->ns_hostmap_sz);
449         if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
450                     sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
451                 perror("kmemcpy (maptable)");
452                 return;
453         }
454
455         for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
456                 hmp = maptable[hv];
457
458                 while (hmp) {
459                         if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
460                                 perror("kmemcpy (hostmap)");
461                                 return;
462                         }
463
464                         printhostmap(&hm, hv);
465                         hmp = hm.hm_next;
466                 }
467         }
468         free(maptable);
469 }
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         ipfgeniter_t iter;
480         hostmap_t hm;
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
503
504 void showtqtable_live(fd)
505 int fd;
506 {
507         ipftq_t table[IPF_TCP_NSTATES];
508         ipfobj_t obj;
509
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;
515
516         if (ioctl(fd, SIOCGTQTAB, &obj) == 0) {
517                 printtqtable(table);      
518         }
519 }