Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / ipfilter / ipnat.c
1 /*
2  * Copyright (C) 1993-2002 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 #if defined(__sgi) && (IRIX > 602)
9 # include <sys/ptimers.h>
10 #endif
11 #include <stdio.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #if !defined(__SVR4) && !defined(__svr4__)
17 #include <strings.h>
18 #else
19 #include <sys/byteorder.h>
20 #endif
21 #include <sys/time.h>
22 #include <sys/param.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stddef.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
29 # include <sys/ioccom.h>
30 # include <sys/sysmacros.h>
31 #endif
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/tcp.h>
36 #include <net/if.h>
37 #if __FreeBSD_version >= 300000
38 # include <net/if_var.h>
39 #endif
40 #include <netdb.h>
41 #include <arpa/nameser.h>
42 #include <arpa/inet.h>
43 #include <resolv.h>
44 #include <ctype.h>
45 #include <nlist.h>
46 #include "netinet/ip_compat.h"
47 #include "netinet/ip_fil.h"
48 #include "netinet/ip_nat.h"
49 #include "netinet/ip_state.h"
50 #include "netinet/ip_proxy.h"
51 #include "ipf.h"
52 #include "kmem.h"
53
54 #if     defined(sun) && !SOLARIS2
55 # define        STRERROR(x)     sys_errlist[x]
56 extern  char    *sys_errlist[];
57 #else
58 # define        STRERROR(x)     strerror(x)
59 #endif
60
61 #if !defined(lint)
62 static const char sccsid[] ="@(#)ipnat.c        1.9 6/5/96 (C) 1993 Darren Reed";
63 static const char rcsid[] = "@(#)$Id: ipnat.c,v 2.16.2.22 2002/12/06 11:40:26 darrenr Exp $";
64 #endif
65
66
67 #if     SOLARIS
68 #define bzero(a,b)      memset(a,0,b)
69 #endif
70 int     use_inet6 = 0;
71 char    thishost[MAXHOSTNAMELEN];
72
73 extern  char    *optarg;
74 extern  ipnat_t *natparse __P((char *, int));
75 extern  void    natparsefile __P((int, char *, int));
76 extern  void    printnat __P((ipnat_t *, int));
77 extern  void    printactivenat __P((nat_t *, int));
78 extern  void    printhostmap __P((hostmap_t *, u_int));
79 extern  char    *getsumd __P((u_32_t));
80
81 void    dostats __P((natstat_t *, int)), flushtable __P((int, int));
82 void    usage __P((char *));
83 int     countbits __P((u_32_t));
84 char    *getnattype __P((ipnat_t *));
85 int     main __P((int, char*[]));
86 void    printaps __P((ap_session_t *, int));
87 void    showhostmap __P((natstat_t *nsp));
88 void    natstat_dead __P((natstat_t *, char *));
89
90
91 void usage(name)
92 char *name;
93 {
94         fprintf(stderr, "%s: [-CFhlnrsv] [-f filename]\n", name);
95         exit(1);
96 }
97
98
99 int main(argc, argv)
100 int argc;
101 char *argv[];
102 {
103         natstat_t ns, *nsp = &ns;
104         char    *file, *core, *kernel;
105         int     fd, opts, c, mode;
106
107         fd = -1;
108         opts = 0;
109         file = NULL;
110         core = NULL;
111         kernel = NULL;
112         mode = O_RDWR;
113
114         while ((c = getopt(argc, argv, "CdFf:hlM:N:nrsv")) != -1)
115                 switch (c)
116                 {
117                 case 'C' :
118                         opts |= OPT_CLEAR;
119                         break;
120                 case 'd' :
121                         opts |= OPT_DEBUG;
122                         break;
123                 case 'f' :
124                         file = optarg;
125                         break;
126                 case 'F' :
127                         opts |= OPT_FLUSH;
128                         break;
129                 case 'h' :
130                         opts |=OPT_HITS;
131                         break;
132                 case 'l' :
133                         opts |= OPT_LIST;
134                         mode = O_RDONLY;
135                         break;
136                 case 'M' :
137                         core = optarg;
138                         break;
139                 case 'N' :
140                         kernel = optarg;
141                         break;
142                 case 'n' :
143                         opts |= OPT_NODO;
144                         mode = O_RDONLY;
145                         break;
146                 case 'r' :
147                         opts |= OPT_REMOVE;
148                         break;
149                 case 's' :
150                         opts |= OPT_STAT;
151                         mode = O_RDONLY;
152                         break;
153                 case 'v' :
154                         opts |= OPT_VERBOSE;
155                         break;
156                 default :
157                         usage(argv[0]);
158                 }
159
160         if ((kernel != NULL) || (core != NULL)) {
161                 (void) setgid(getgid());
162                 (void) setuid(getuid());
163         }
164
165         bzero((char *)&ns, sizeof(ns));
166
167         gethostname(thishost, sizeof(thishost));
168         thishost[sizeof(thishost) - 1] = '\0';
169
170         if (!(opts & OPT_NODO) && (kernel == NULL) && (core == NULL)) {
171                 if (openkmem(kernel, core) == -1)
172                         exit(1);
173
174                 if (((fd = open(IPL_NAT, mode)) == -1) &&
175                     ((fd = open(IPL_NAT, O_RDONLY)) == -1)) {
176                         (void) fprintf(stderr, "%s: open: %s\n", IPL_NAT,
177                                 STRERROR(errno));
178                         if (errno == ENODEV)
179                                 fprintf(stderr, "IPFilter enabled?\n");
180                         exit(1);
181                 }
182                 if (ioctl(fd, SIOCGNATS, &nsp) == -1) {
183                         perror("ioctl(SIOCGNATS)");
184                         exit(1);
185                 }
186                 (void) setgid(getgid());
187                 (void) setuid(getuid());
188         } else if ((kernel != NULL) || (core != NULL)) {
189                 if (openkmem(kernel, core) == -1)
190                         exit(1);
191
192                 natstat_dead(nsp, kernel);
193                 if (opts & (OPT_LIST|OPT_STAT))
194                         dostats(nsp, opts);
195                 exit(0);
196         }
197
198         if (opts & (OPT_FLUSH|OPT_CLEAR))
199                 flushtable(fd, opts);
200         if (file)
201                 natparsefile(fd, file, opts);
202         if (opts & (OPT_LIST|OPT_STAT))
203                 dostats(nsp, opts);
204         return 0;
205 }
206
207
208 /*
209  * Read nat statistic information in using a symbol table and memory file
210  * rather than doing ioctl's.
211  */
212 void natstat_dead(nsp, kernel)
213 natstat_t *nsp;
214 char *kernel;
215 {
216         struct nlist nat_nlist[10] = {
217                 { "nat_table" },                /* 0 */
218                 { "nat_list" },
219                 { "maptable" },
220                 { "ipf_nattable_sz" },
221                 { "ipf_natrules_sz" },
222                 { "ipf_rdrrules_sz" },          /* 5 */
223                 { "ipf_hostmap_sz" },
224                 { "nat_instances" },
225                 { "ap_sess_list" },
226                 { NULL }
227         };
228         void *tables[2];
229
230         if (nlist(kernel, nat_nlist) == -1) {
231                 fprintf(stderr, "nlist error\n");
232                 return;
233         }
234
235         /*
236          * Normally the ioctl copies all of these values into the structure
237          * for us, before returning it to useland, so here we must copy each
238          * one in individually.
239          */
240         kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
241         nsp->ns_table[0] = tables[0];
242         nsp->ns_table[1] = tables[1];
243
244         kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
245                 sizeof(nsp->ns_list));
246         kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
247                 sizeof(nsp->ns_maptable));
248         kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
249                 sizeof(nsp->ns_nattab_sz));
250         kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
251                 sizeof(nsp->ns_rultab_sz));
252         kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
253                 sizeof(nsp->ns_rdrtab_sz));
254         kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
255                 sizeof(nsp->ns_hostmap_sz));
256         kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
257                 sizeof(nsp->ns_instances));
258         kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value,
259                 sizeof(nsp->ns_apslist));
260 }
261
262
263 /*
264  * Display NAT statistics.
265  */
266 void dostats(nsp, opts)
267 natstat_t *nsp;
268 int opts;
269 {
270         nat_t **nt[2], *np, nat;
271         ipnat_t ipn;
272
273         /*
274          * Show statistics ?
275          */
276         if (opts & OPT_STAT) {
277                 printf("mapped\tin\t%lu\tout\t%lu\n",
278                         nsp->ns_mapped[0], nsp->ns_mapped[1]);
279                 printf("added\t%lu\texpired\t%lu\n",
280                         nsp->ns_added, nsp->ns_expire);
281                 printf("no memory\t%lu\tbad nat\t%lu\n",
282                         nsp->ns_memfail, nsp->ns_badnat);
283                 printf("inuse\t%lu\nrules\t%lu\n",
284                         nsp->ns_inuse, nsp->ns_rules);
285                 printf("wilds\t%u\n", nsp->ns_wilds);
286                 if (opts & OPT_VERBOSE)
287                         printf("table %p list %p\n",
288                                 nsp->ns_table, nsp->ns_list);
289         }
290
291         /*
292          * Show list of NAT rules and NAT sessions ?
293          */
294         if (opts & OPT_LIST) {
295                 printf("List of active MAP/Redirect filters:\n");
296                 while (nsp->ns_list) {
297                         if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
298                                     sizeof(ipn))) {
299                                 perror("kmemcpy");
300                                 break;
301                         }
302                         if (opts & OPT_HITS)
303                                 printf("%d ", ipn.in_hits);
304                         printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
305                         nsp->ns_list = ipn.in_next;
306                 }
307
308                 nt[0] = (nat_t **)malloc(sizeof(*nt) * NAT_SIZE);
309                 if (kmemcpy((char *)nt[0], (long)nsp->ns_table[0],
310                             sizeof(**nt) * NAT_SIZE)) {
311                         perror("kmemcpy");
312                         return;
313                 }
314
315                 printf("\nList of active sessions:\n");
316
317                 for (np = nsp->ns_instances; np; np = nat.nat_next) {
318                         if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
319                                 break;
320                         printactivenat(&nat, opts);
321                 }
322
323                 if (opts & OPT_VERBOSE)
324                         showhostmap(nsp);
325                 free(nt[0]);
326         }
327 }
328
329
330 /*
331  * display the active host mapping table.
332  */
333 void showhostmap(nsp)
334 natstat_t *nsp;
335 {
336         hostmap_t hm, *hmp, **maptable;
337         u_int hv;
338
339         printf("\nList of active host mappings:\n");
340
341         maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
342                                         nsp->ns_hostmap_sz);
343         if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
344                     sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
345                 perror("kmemcpy (maptable)");
346                 return;
347         }
348
349         for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
350                 hmp = maptable[hv];
351
352                 while (hmp) {
353                         if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
354                                 perror("kmemcpy (hostmap)");
355                                 return;
356                         }
357
358                         printhostmap(&hm, hv);
359                         hmp = hm.hm_next;
360                 }
361         }
362         free(maptable);
363 }
364
365
366 /*
367  * Issue an ioctl to flush either the NAT rules table or the active mapping
368  * table or both.
369  */
370 void flushtable(fd, opts)
371 int fd, opts;
372 {
373         int n = 0;
374
375         if (opts & OPT_FLUSH) {
376                 n = 0;
377                 if (!(opts & OPT_NODO) && ioctl(fd, SIOCIPFFL, &n) == -1)
378                         perror("ioctl(SIOCFLNAT)");
379                 else
380                         printf("%d entries flushed from NAT table\n", n);
381         }
382
383         if (opts & OPT_CLEAR) {
384                 n = 1;
385                 if (!(opts & OPT_NODO) && ioctl(fd, SIOCIPFFL, &n) == -1)
386                         perror("ioctl(SIOCCNATL)");
387                 else
388                         printf("%d entries flushed from NAT list\n", n);
389         }
390 }