ipnat.c revision 172776
1/* $FreeBSD: head/contrib/ipfilter/tools/ipnat.c 172776 2007-10-18 21:52:14Z darrenr $ */ 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] 63extern char *sys_errlist[]; 64#else 65# define STRERROR(x) strerror(x) 66#endif 67 68#if !defined(lint) 69static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed"; 70static const char rcsid[] = "@(#)$Id: ipnat.c,v 1.24.2.11 2007/09/25 08:27:34 darrenr Exp $"; 71#endif 72 73 74#if SOLARIS 75#define bzero(a,b) memset(a,0,b) 76#endif 77int use_inet6 = 0; 78char thishost[MAXHOSTNAMELEN]; 79 80extern char *optarg; 81 82void dostats __P((int, natstat_t *, int, int)); 83void dotable __P((natstat_t *, int, int)); 84void flushtable __P((int, int)); 85void usage __P((char *)); 86int main __P((int, char*[])); 87void showhostmap __P((natstat_t *nsp)); 88void natstat_dead __P((natstat_t *, char *)); 89void dostats_live __P((int, natstat_t *, int)); 90void showhostmap_dead __P((natstat_t *)); 91void showhostmap_live __P((int, natstat_t *)); 92void dostats_dead __P((natstat_t *, int)); 93void showtqtable_live __P((int)); 94 95int opts; 96 97void usage(name) 98char *name; 99{ 100 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name); 101 exit(1); 102} 103 104 105int main(argc, argv) 106int argc; 107char *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) setuid(getuid()); 176 } 177 178 if (!(opts & OPT_DONOTHING)) { 179 if (((fd = open(IPNAT_NAME, mode)) == -1) && 180 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) { 181 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME, 182 STRERROR(errno)); 183 exit(1); 184 } 185 } 186 187 bzero((char *)&ns, sizeof(ns)); 188 189 if ((opts & OPT_DONOTHING) == 0) { 190 if (checkrev(IPL_NAME) == -1) { 191 fprintf(stderr, "User/kernel version check failed\n"); 192 exit(1); 193 } 194 } 195 196 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) { 197 bzero((char *)&obj, sizeof(obj)); 198 obj.ipfo_rev = IPFILTER_VERSION; 199 obj.ipfo_type = IPFOBJ_NATSTAT; 200 obj.ipfo_size = sizeof(*nsp); 201 obj.ipfo_ptr = (void *)nsp; 202 if (ioctl(fd, SIOCGNATS, &obj) == -1) { 203 perror("ioctl(SIOCGNATS)"); 204 exit(1); 205 } 206 (void) setgid(getgid()); 207 (void) setuid(getuid()); 208 } else if ((kernel != NULL) || (core != NULL)) { 209 if (openkmem(kernel, core) == -1) 210 exit(1); 211 212 natstat_dead(nsp, kernel); 213 if (opts & (OPT_LIST|OPT_STAT)) 214 dostats(fd, nsp, opts, 0); 215 exit(0); 216 } 217 218 if (opts & (OPT_FLUSH|OPT_CLEAR)) 219 flushtable(fd, opts); 220 if (file) { 221 ipnat_parsefile(fd, ipnat_addrule, ioctl, file); 222 } 223 if (opts & (OPT_LIST|OPT_STAT)) 224 dostats(fd, nsp, opts, 1); 225 return 0; 226} 227 228 229/* 230 * Read NAT statistic information in using a symbol table and memory file 231 * rather than doing ioctl's. 232 */ 233void natstat_dead(nsp, kernel) 234natstat_t *nsp; 235char *kernel; 236{ 237 struct nlist nat_nlist[10] = { 238 { "nat_table" }, /* 0 */ 239 { "nat_list" }, 240 { "maptable" }, 241 { "ipf_nattable_sz" }, 242 { "ipf_natrules_sz" }, 243 { "ipf_rdrrules_sz" }, /* 5 */ 244 { "ipf_hostmap_sz" }, 245 { "nat_instances" }, 246 { "ap_sess_list" }, 247 { NULL } 248 }; 249 void *tables[2]; 250 251 if (nlist(kernel, nat_nlist) == -1) { 252 fprintf(stderr, "nlist error\n"); 253 return; 254 } 255 256 /* 257 * Normally the ioctl copies all of these values into the structure 258 * for us, before returning it to userland, so here we must copy each 259 * one in individually. 260 */ 261 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables)); 262 nsp->ns_table[0] = tables[0]; 263 nsp->ns_table[1] = tables[1]; 264 265 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value, 266 sizeof(nsp->ns_list)); 267 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value, 268 sizeof(nsp->ns_maptable)); 269 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value, 270 sizeof(nsp->ns_nattab_sz)); 271 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value, 272 sizeof(nsp->ns_rultab_sz)); 273 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value, 274 sizeof(nsp->ns_rdrtab_sz)); 275 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value, 276 sizeof(nsp->ns_hostmap_sz)); 277 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value, 278 sizeof(nsp->ns_instances)); 279 kmemcpy((char *)&nsp->ns_apslist, nat_nlist[8].n_value, 280 sizeof(nsp->ns_apslist)); 281} 282 283 284/* 285 * Issue an ioctl to flush either the NAT rules table or the active mapping 286 * table or both. 287 */ 288void flushtable(fd, opts) 289int fd, opts; 290{ 291 int n = 0; 292 293 if (opts & OPT_FLUSH) { 294 n = 0; 295 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1) 296 perror("ioctl(SIOCFLNAT)"); 297 else 298 printf("%d entries flushed from NAT table\n", n); 299 } 300 301 if (opts & OPT_CLEAR) { 302 n = 1; 303 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1) 304 perror("ioctl(SIOCCNATL)"); 305 else 306 printf("%d entries flushed from NAT list\n", n); 307 } 308} 309 310 311/* 312 * Display NAT statistics. 313 */ 314void dostats_dead(nsp, opts) 315natstat_t *nsp; 316int opts; 317{ 318 nat_t *np, nat; 319 ipnat_t ipn; 320 321 printf("List of active MAP/Redirect filters:\n"); 322 while (nsp->ns_list) { 323 if (kmemcpy((char *)&ipn, (long)nsp->ns_list, 324 sizeof(ipn))) { 325 perror("kmemcpy"); 326 break; 327 } 328 if (opts & OPT_HITS) 329 printf("%lu ", ipn.in_hits); 330 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE)); 331 nsp->ns_list = ipn.in_next; 332 } 333 334 printf("\nList of active sessions:\n"); 335 336 for (np = nsp->ns_instances; np; np = nat.nat_next) { 337 if (kmemcpy((char *)&nat, (long)np, sizeof(nat))) 338 break; 339 printactivenat(&nat, opts, 0, nsp->ns_ticks); 340 if (nat.nat_aps) 341 printaps(nat.nat_aps, opts); 342 } 343 344 if (opts & OPT_VERBOSE) 345 showhostmap_dead(nsp); 346} 347 348 349void dostats(fd, nsp, opts, alive) 350natstat_t *nsp; 351int fd, opts, alive; 352{ 353 /* 354 * Show statistics ? 355 */ 356 if (opts & OPT_STAT) { 357 printf("mapped\tin\t%lu\tout\t%lu\n", 358 nsp->ns_mapped[0], nsp->ns_mapped[1]); 359 printf("added\t%lu\texpired\t%lu\n", 360 nsp->ns_added, nsp->ns_expire); 361 printf("no memory\t%lu\tbad nat\t%lu\n", 362 nsp->ns_memfail, nsp->ns_badnat); 363 printf("inuse\t%lu\norphans\t%u\nrules\t%lu\n", 364 nsp->ns_inuse, nsp->ns_orphans, nsp->ns_rules); 365 printf("wilds\t%u\n", nsp->ns_wilds); 366 dotable(nsp, fd, alive); 367 if (opts & OPT_VERBOSE) 368 printf("table %p list %p\n", 369 nsp->ns_table, nsp->ns_list); 370 if (alive) 371 showtqtable_live(fd); 372 } 373 374 if (opts & OPT_LIST) { 375 if (alive) 376 dostats_live(fd, nsp, opts); 377 else 378 dostats_dead(nsp, opts); 379 } 380} 381 382 383void dotable(nsp, fd, alive) 384natstat_t *nsp; 385int fd, alive; 386{ 387 int sz, i, used, totallen, maxlen, minlen; 388 ipftable_t table; 389 u_long *buckets; 390 ipfobj_t obj; 391 392 sz = sizeof(*buckets) * nsp->ns_nattab_sz; 393 buckets = (u_long *)malloc(sz); 394 395 obj.ipfo_rev = IPFILTER_VERSION; 396 obj.ipfo_type = IPFOBJ_GTABLE; 397 obj.ipfo_size = sizeof(table); 398 obj.ipfo_ptr = &table; 399 400 table.ita_type = IPFTABLE_BUCKETS_NATIN; 401 table.ita_table = buckets; 402 403 if (alive) { 404 if (ioctl(fd, SIOCGTABL, &obj) != 0) { 405 free(buckets); 406 return; 407 } 408 } else { 409 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) { 410 free(buckets); 411 return; 412 } 413 } 414 415 totallen = 0; 416 maxlen = 0; 417 minlen = nsp->ns_inuse; 418 used = 0; 419 420 for (i = 0; i < nsp->ns_nattab_sz; i++) { 421 if (buckets[i] > maxlen) 422 maxlen = buckets[i]; 423 if (buckets[i] < minlen) 424 minlen = buckets[i]; 425 if (buckets[i] != 0) 426 used++; 427 totallen += buckets[i]; 428 } 429 430 printf("hash efficiency\t%2.2f%%\n", 431 totallen ? ((float)used / totallen) * 100.0 : 0.0); 432 printf("bucket usage\t%2.2f%%\n", 433 ((float)used / nsp->ns_nattab_sz) * 100.0); 434 printf("minimal length\t%d\n", minlen); 435 printf("maximal length\t%d\n", maxlen); 436 printf("average length\t%.3f\n", used ? (float)totallen / used : 0.0); 437} 438 439 440/* 441 * Display NAT statistics. 442 */ 443void dostats_live(fd, nsp, opts) 444natstat_t *nsp; 445int fd, opts; 446{ 447 ipfgeniter_t iter; 448 ipfobj_t obj; 449 ipnat_t ipn; 450 nat_t nat; 451 452 bzero((char *)&obj, sizeof(obj)); 453 obj.ipfo_rev = IPFILTER_VERSION; 454 obj.ipfo_type = IPFOBJ_GENITER; 455 obj.ipfo_size = sizeof(iter); 456 obj.ipfo_ptr = &iter; 457 458 iter.igi_type = IPFGENITER_IPNAT; 459 iter.igi_nitems = 1; 460 iter.igi_data = &ipn; 461 462 /* 463 * Show list of NAT rules and NAT sessions ? 464 */ 465 printf("List of active MAP/Redirect filters:\n"); 466 while (nsp->ns_list) { 467 if (ioctl(fd, SIOCGENITER, &obj) == -1) 468 break; 469 if (opts & OPT_HITS) 470 printf("%lu ", ipn.in_hits); 471 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE)); 472 nsp->ns_list = ipn.in_next; 473 } 474 475 printf("\nList of active sessions:\n"); 476 477 iter.igi_type = IPFGENITER_NAT; 478 iter.igi_nitems = 1; 479 iter.igi_data = &nat; 480 481 while (nsp->ns_instances != NULL) { 482 if (ioctl(fd, SIOCGENITER, &obj) == -1) 483 break; 484 printactivenat(&nat, opts, 1, nsp->ns_ticks); 485 if (nat.nat_aps) 486 printaps(nat.nat_aps, opts); 487 nsp->ns_instances = nat.nat_next; 488 } 489 490 if (opts & OPT_VERBOSE) 491 showhostmap_live(fd, nsp); 492} 493 494 495/* 496 * Display the active host mapping table. 497 */ 498void showhostmap_dead(nsp) 499natstat_t *nsp; 500{ 501 hostmap_t hm, *hmp, **maptable; 502 u_int hv; 503 504 printf("\nList of active host mappings:\n"); 505 506 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) * 507 nsp->ns_hostmap_sz); 508 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable, 509 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) { 510 perror("kmemcpy (maptable)"); 511 return; 512 } 513 514 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) { 515 hmp = maptable[hv]; 516 517 while (hmp) { 518 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) { 519 perror("kmemcpy (hostmap)"); 520 return; 521 } 522 523 printhostmap(&hm, hv); 524 hmp = hm.hm_next; 525 } 526 } 527 free(maptable); 528} 529 530 531/* 532 * Display the active host mapping table. 533 */ 534void showhostmap_live(fd, nsp) 535int fd; 536natstat_t *nsp; 537{ 538 ipfgeniter_t iter; 539 hostmap_t hm; 540 ipfobj_t obj; 541 542 bzero((char *)&obj, sizeof(obj)); 543 obj.ipfo_rev = IPFILTER_VERSION; 544 obj.ipfo_type = IPFOBJ_GENITER; 545 obj.ipfo_size = sizeof(iter); 546 obj.ipfo_ptr = &iter; 547 548 iter.igi_type = IPFGENITER_HOSTMAP; 549 iter.igi_nitems = 1; 550 iter.igi_data = &hm; 551 552 printf("\nList of active host mappings:\n"); 553 554 while (nsp->ns_maplist != NULL) { 555 if (ioctl(fd, SIOCGENITER, &obj) == -1) 556 break; 557 printhostmap(&hm, 0); 558 nsp->ns_maplist = hm.hm_next; 559 } 560} 561 562 563void showtqtable_live(fd) 564int fd; 565{ 566 ipftq_t table[IPF_TCP_NSTATES]; 567 ipfobj_t obj; 568 569 bzero((char *)&obj, sizeof(obj)); 570 obj.ipfo_rev = IPFILTER_VERSION; 571 obj.ipfo_size = sizeof(table); 572 obj.ipfo_ptr = (void *)table; 573 obj.ipfo_type = IPFOBJ_STATETQTAB; 574 575 if (ioctl(fd, SIOCGTQTAB, &obj) == 0) { 576 printtqtable(table); 577 } 578} 579