1 /* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * 9 * Redistribution and use in source forms, with and without modification, 10 * are permitted provided that this entire comment appears intact. 11 * 12 * Redistribution in binary form may occur without any restrictions. 13 * Obviously, it would be nice if you gave credit where credit is due 14 * but requiring it would be too onerous. 15 * 16 * This software is provided ``AS IS'' without any warranties of any kind. 17 * 18 * NEW command line interface for IP firewall facility 19 * 20 * $FreeBSD$ 21 * 22 * In-kernel nat support 23 */ 24 25 #include <sys/types.h> 26 #include <sys/socket.h> 27 #include <sys/sockio.h> 28 #include <sys/sysctl.h> 29 30 #include "ipfw2.h" 31 32 #include <ctype.h> 33 #include <err.h> 34 #include <netdb.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sysexits.h> 39 40 #define IPFW_INTERNAL /* Access to protected structures in ip_fw.h. */ 41 42 #include <net/if.h> 43 #include <net/if_dl.h> 44 #include <net/route.h> /* def. of struct route */ 45 #include <netinet/in.h> 46 #include <netinet/in_systm.h> 47 #include <netinet/ip_fw.h> 48 #include <arpa/inet.h> 49 #include <alias.h> 50 51 static struct _s_x nat_params[] = { 52 { "ip", TOK_IP }, 53 { "if", TOK_IF }, 54 { "log", TOK_ALOG }, 55 { "deny_in", TOK_DENY_INC }, 56 { "same_ports", TOK_SAME_PORTS }, 57 { "unreg_only", TOK_UNREG_ONLY }, 58 { "reset", TOK_RESET_ADDR }, 59 { "reverse", TOK_ALIAS_REV }, 60 { "proxy_only", TOK_PROXY_ONLY }, 61 { "redirect_addr", TOK_REDIR_ADDR }, 62 { "redirect_port", TOK_REDIR_PORT }, 63 { "redirect_proto", TOK_REDIR_PROTO }, 64 { NULL, 0 } /* terminator */ 65 }; 66 67 68 /* 69 * Search for interface with name "ifn", and fill n accordingly: 70 * 71 * n->ip ip address of interface "ifn" 72 * n->if_name copy of interface name "ifn" 73 */ 74 static void 75 set_addr_dynamic(const char *ifn, struct cfg_nat *n) 76 { 77 size_t needed; 78 int mib[6]; 79 char *buf, *lim, *next; 80 struct if_msghdr *ifm; 81 struct ifa_msghdr *ifam; 82 struct sockaddr_dl *sdl; 83 struct sockaddr_in *sin; 84 int ifIndex, ifMTU; 85 86 mib[0] = CTL_NET; 87 mib[1] = PF_ROUTE; 88 mib[2] = 0; 89 mib[3] = AF_INET; 90 mib[4] = NET_RT_IFLIST; 91 mib[5] = 0; 92 /* 93 * Get interface data. 94 */ 95 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) 96 err(1, "iflist-sysctl-estimate"); 97 buf = safe_calloc(1, needed); 98 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) 99 err(1, "iflist-sysctl-get"); 100 lim = buf + needed; 101 /* 102 * Loop through interfaces until one with 103 * given name is found. This is done to 104 * find correct interface index for routing 105 * message processing. 106 */ 107 ifIndex = 0; 108 next = buf; 109 while (next < lim) { 110 ifm = (struct if_msghdr *)next; 111 next += ifm->ifm_msglen; 112 if (ifm->ifm_version != RTM_VERSION) { 113 if (co.verbose) 114 warnx("routing message version %d " 115 "not understood", ifm->ifm_version); 116 continue; 117 } 118 if (ifm->ifm_type == RTM_IFINFO) { 119 sdl = (struct sockaddr_dl *)(ifm + 1); 120 if (strlen(ifn) == sdl->sdl_nlen && 121 strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) { 122 ifIndex = ifm->ifm_index; 123 ifMTU = ifm->ifm_data.ifi_mtu; 124 break; 125 } 126 } 127 } 128 if (!ifIndex) 129 errx(1, "unknown interface name %s", ifn); 130 /* 131 * Get interface address. 132 */ 133 sin = NULL; 134 while (next < lim) { 135 ifam = (struct ifa_msghdr *)next; 136 next += ifam->ifam_msglen; 137 if (ifam->ifam_version != RTM_VERSION) { 138 if (co.verbose) 139 warnx("routing message version %d " 140 "not understood", ifam->ifam_version); 141 continue; 142 } 143 if (ifam->ifam_type != RTM_NEWADDR) 144 break; 145 if (ifam->ifam_addrs & RTA_IFA) { 146 int i; 147 char *cp = (char *)(ifam + 1); 148 149 for (i = 1; i < RTA_IFA; i <<= 1) { 150 if (ifam->ifam_addrs & i) 151 cp += SA_SIZE((struct sockaddr *)cp); 152 } 153 if (((struct sockaddr *)cp)->sa_family == AF_INET) { 154 sin = (struct sockaddr_in *)cp; 155 break; 156 } 157 } 158 } 159 if (sin == NULL) 160 errx(1, "%s: cannot get interface address", ifn); 161 162 n->ip = sin->sin_addr; 163 strncpy(n->if_name, ifn, IF_NAMESIZE); 164 165 free(buf); 166 } 167 168 /* 169 * XXX - The following functions, macros and definitions come from natd.c: 170 * it would be better to move them outside natd.c, in a file 171 * (redirect_support.[ch]?) shared by ipfw and natd, but for now i can live 172 * with it. 173 */ 174 175 /* 176 * Definition of a port range, and macros to deal with values. 177 * FORMAT: HI 16-bits == first port in range, 0 == all ports. 178 * LO 16-bits == number of ports in range 179 * NOTES: - Port values are not stored in network byte order. 180 */ 181 182 #define port_range u_long 183 184 #define GETLOPORT(x) ((x) >> 0x10) 185 #define GETNUMPORTS(x) ((x) & 0x0000ffff) 186 #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x))) 187 188 /* Set y to be the low-port value in port_range variable x. */ 189 #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10)) 190 191 /* Set y to be the number of ports in port_range variable x. */ 192 #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y)) 193 194 static void 195 StrToAddr (const char* str, struct in_addr* addr) 196 { 197 struct hostent* hp; 198 199 if (inet_aton (str, addr)) 200 return; 201 202 hp = gethostbyname (str); 203 if (!hp) 204 errx (1, "unknown host %s", str); 205 206 memcpy (addr, hp->h_addr, sizeof (struct in_addr)); 207 } 208 209 static int 210 StrToPortRange (const char* str, const char* proto, port_range *portRange) 211 { 212 char* sep; 213 struct servent* sp; 214 char* end; 215 u_short loPort; 216 u_short hiPort; 217 218 /* First see if this is a service, return corresponding port if so. */ 219 sp = getservbyname (str,proto); 220 if (sp) { 221 SETLOPORT(*portRange, ntohs(sp->s_port)); 222 SETNUMPORTS(*portRange, 1); 223 return 0; 224 } 225 226 /* Not a service, see if it's a single port or port range. */ 227 sep = strchr (str, '-'); 228 if (sep == NULL) { 229 SETLOPORT(*portRange, strtol(str, &end, 10)); 230 if (end != str) { 231 /* Single port. */ 232 SETNUMPORTS(*portRange, 1); 233 return 0; 234 } 235 236 /* Error in port range field. */ 237 errx (EX_DATAERR, "%s/%s: unknown service", str, proto); 238 } 239 240 /* Port range, get the values and sanity check. */ 241 sscanf (str, "%hu-%hu", &loPort, &hiPort); 242 SETLOPORT(*portRange, loPort); 243 SETNUMPORTS(*portRange, 0); /* Error by default */ 244 if (loPort <= hiPort) 245 SETNUMPORTS(*portRange, hiPort - loPort + 1); 246 247 if (GETNUMPORTS(*portRange) == 0) 248 errx (EX_DATAERR, "invalid port range %s", str); 249 250 return 0; 251 } 252 253 static int 254 StrToProto (const char* str) 255 { 256 if (!strcmp (str, "tcp")) 257 return IPPROTO_TCP; 258 259 if (!strcmp (str, "udp")) 260 return IPPROTO_UDP; 261 262 errx (EX_DATAERR, "unknown protocol %s. Expected tcp or udp", str); 263 } 264 265 static int 266 StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto, 267 port_range *portRange) 268 { 269 char* ptr; 270 271 ptr = strchr (str, ':'); 272 if (!ptr) 273 errx (EX_DATAERR, "%s is missing port number", str); 274 275 *ptr = '\0'; 276 ++ptr; 277 278 StrToAddr (str, addr); 279 return StrToPortRange (ptr, proto, portRange); 280 } 281 282 /* End of stuff taken from natd.c. */ 283 284 #define INC_ARGCV() do { \ 285 (*_av)++; \ 286 (*_ac)--; \ 287 av = *_av; \ 288 ac = *_ac; \ 289 } while(0) 290 291 /* 292 * The next 3 functions add support for the addr, port and proto redirect and 293 * their logic is loosely based on SetupAddressRedirect(), SetupPortRedirect() 294 * and SetupProtoRedirect() from natd.c. 295 * 296 * Every setup_* function fills at least one redirect entry 297 * (struct cfg_redir) and zero or more server pool entry (struct cfg_spool) 298 * in buf. 299 * 300 * The format of data in buf is: 301 * 302 * 303 * cfg_nat cfg_redir cfg_spool ...... cfg_spool 304 * 305 * ------------------------------------- ------------ 306 * | | .....X ... | | | | ..... 307 * ------------------------------------- ...... ------------ 308 * ^ 309 * spool_cnt n=0 ...... n=(X-1) 310 * 311 * len points to the amount of available space in buf 312 * space counts the memory consumed by every function 313 * 314 * XXX - Every function get all the argv params so it 315 * has to check, in optional parameters, that the next 316 * args is a valid option for the redir entry and not 317 * another token. Only redir_port and redir_proto are 318 * affected by this. 319 */ 320 321 static int 322 setup_redir_addr(char *spool_buf, int len, 323 int *_ac, char ***_av) 324 { 325 char **av, *sep; /* Token separator. */ 326 /* Temporary buffer used to hold server pool ip's. */ 327 char tmp_spool_buf[NAT_BUF_LEN]; 328 int ac, space, lsnat; 329 struct cfg_redir *r; 330 struct cfg_spool *tmp; 331 332 av = *_av; 333 ac = *_ac; 334 space = 0; 335 lsnat = 0; 336 if (len >= SOF_REDIR) { 337 r = (struct cfg_redir *)spool_buf; 338 /* Skip cfg_redir at beginning of buf. */ 339 spool_buf = &spool_buf[SOF_REDIR]; 340 space = SOF_REDIR; 341 len -= SOF_REDIR; 342 } else 343 goto nospace; 344 r->mode = REDIR_ADDR; 345 /* Extract local address. */ 346 if (ac == 0) 347 errx(EX_DATAERR, "redirect_addr: missing local address"); 348 sep = strchr(*av, ','); 349 if (sep) { /* LSNAT redirection syntax. */ 350 r->laddr.s_addr = INADDR_NONE; 351 /* Preserve av, copy spool servers to tmp_spool_buf. */ 352 strncpy(tmp_spool_buf, *av, strlen(*av)+1); 353 lsnat = 1; 354 } else 355 StrToAddr(*av, &r->laddr); 356 INC_ARGCV(); 357 358 /* Extract public address. */ 359 if (ac == 0) 360 errx(EX_DATAERR, "redirect_addr: missing public address"); 361 StrToAddr(*av, &r->paddr); 362 INC_ARGCV(); 363 364 /* Setup LSNAT server pool. */ 365 if (sep) { 366 sep = strtok(tmp_spool_buf, ","); 367 while (sep != NULL) { 368 tmp = (struct cfg_spool *)spool_buf; 369 if (len < SOF_SPOOL) 370 goto nospace; 371 len -= SOF_SPOOL; 372 space += SOF_SPOOL; 373 StrToAddr(sep, &tmp->addr); 374 tmp->port = ~0; 375 r->spool_cnt++; 376 /* Point to the next possible cfg_spool. */ 377 spool_buf = &spool_buf[SOF_SPOOL]; 378 sep = strtok(NULL, ","); 379 } 380 } 381 return(space); 382 nospace: 383 errx(EX_DATAERR, "redirect_addr: buf is too small\n"); 384 } 385 386 static int 387 setup_redir_port(char *spool_buf, int len, 388 int *_ac, char ***_av) 389 { 390 char **av, *sep, *protoName; 391 char tmp_spool_buf[NAT_BUF_LEN]; 392 int ac, space, lsnat; 393 struct cfg_redir *r; 394 struct cfg_spool *tmp; 395 u_short numLocalPorts; 396 port_range portRange; 397 398 av = *_av; 399 ac = *_ac; 400 space = 0; 401 lsnat = 0; 402 numLocalPorts = 0; 403 404 if (len >= SOF_REDIR) { 405 r = (struct cfg_redir *)spool_buf; 406 /* Skip cfg_redir at beginning of buf. */ 407 spool_buf = &spool_buf[SOF_REDIR]; 408 space = SOF_REDIR; 409 len -= SOF_REDIR; 410 } else 411 goto nospace; 412 r->mode = REDIR_PORT; 413 /* 414 * Extract protocol. 415 */ 416 if (ac == 0) 417 errx (EX_DATAERR, "redirect_port: missing protocol"); 418 r->proto = StrToProto(*av); 419 protoName = *av; 420 INC_ARGCV(); 421 422 /* 423 * Extract local address. 424 */ 425 if (ac == 0) 426 errx (EX_DATAERR, "redirect_port: missing local address"); 427 428 sep = strchr(*av, ','); 429 /* LSNAT redirection syntax. */ 430 if (sep) { 431 r->laddr.s_addr = INADDR_NONE; 432 r->lport = ~0; 433 numLocalPorts = 1; 434 /* Preserve av, copy spool servers to tmp_spool_buf. */ 435 strncpy(tmp_spool_buf, *av, strlen(*av)+1); 436 lsnat = 1; 437 } else { 438 if (StrToAddrAndPortRange (*av, &r->laddr, protoName, 439 &portRange) != 0) 440 errx(EX_DATAERR, "redirect_port:" 441 "invalid local port range"); 442 443 r->lport = GETLOPORT(portRange); 444 numLocalPorts = GETNUMPORTS(portRange); 445 } 446 INC_ARGCV(); 447 448 /* 449 * Extract public port and optionally address. 450 */ 451 if (ac == 0) 452 errx (EX_DATAERR, "redirect_port: missing public port"); 453 454 sep = strchr (*av, ':'); 455 if (sep) { 456 if (StrToAddrAndPortRange (*av, &r->paddr, protoName, 457 &portRange) != 0) 458 errx(EX_DATAERR, "redirect_port:" 459 "invalid public port range"); 460 } else { 461 r->paddr.s_addr = INADDR_ANY; 462 if (StrToPortRange (*av, protoName, &portRange) != 0) 463 errx(EX_DATAERR, "redirect_port:" 464 "invalid public port range"); 465 } 466 467 r->pport = GETLOPORT(portRange); 468 r->pport_cnt = GETNUMPORTS(portRange); 469 INC_ARGCV(); 470 471 /* 472 * Extract remote address and optionally port. 473 */ 474 /* 475 * NB: isalpha(**av) => we've to check that next parameter is really an 476 * option for this redirect entry, else stop here processing arg[cv]. 477 */ 478 if (ac != 0 && !isalpha(**av)) { 479 sep = strchr (*av, ':'); 480 if (sep) { 481 if (StrToAddrAndPortRange (*av, &r->raddr, protoName, 482 &portRange) != 0) 483 errx(EX_DATAERR, "redirect_port:" 484 "invalid remote port range"); 485 } else { 486 SETLOPORT(portRange, 0); 487 SETNUMPORTS(portRange, 1); 488 StrToAddr (*av, &r->raddr); 489 } 490 INC_ARGCV(); 491 } else { 492 SETLOPORT(portRange, 0); 493 SETNUMPORTS(portRange, 1); 494 r->raddr.s_addr = INADDR_ANY; 495 } 496 r->rport = GETLOPORT(portRange); 497 r->rport_cnt = GETNUMPORTS(portRange); 498 499 /* 500 * Make sure port ranges match up, then add the redirect ports. 501 */ 502 if (numLocalPorts != r->pport_cnt) 503 errx(EX_DATAERR, "redirect_port:" 504 "port ranges must be equal in size"); 505 506 /* Remote port range is allowed to be '0' which means all ports. */ 507 if (r->rport_cnt != numLocalPorts && 508 (r->rport_cnt != 1 || r->rport != 0)) 509 errx(EX_DATAERR, "redirect_port: remote port must" 510 "be 0 or equal to local port range in size"); 511 512 /* 513 * Setup LSNAT server pool. 514 */ 515 if (lsnat) { 516 sep = strtok(tmp_spool_buf, ","); 517 while (sep != NULL) { 518 tmp = (struct cfg_spool *)spool_buf; 519 if (len < SOF_SPOOL) 520 goto nospace; 521 len -= SOF_SPOOL; 522 space += SOF_SPOOL; 523 if (StrToAddrAndPortRange(sep, &tmp->addr, protoName, 524 &portRange) != 0) 525 errx(EX_DATAERR, "redirect_port:" 526 "invalid local port range"); 527 if (GETNUMPORTS(portRange) != 1) 528 errx(EX_DATAERR, "redirect_port: local port" 529 "must be single in this context"); 530 tmp->port = GETLOPORT(portRange); 531 r->spool_cnt++; 532 /* Point to the next possible cfg_spool. */ 533 spool_buf = &spool_buf[SOF_SPOOL]; 534 sep = strtok(NULL, ","); 535 } 536 } 537 return (space); 538 nospace: 539 errx(EX_DATAERR, "redirect_port: buf is too small\n"); 540 } 541 542 static int 543 setup_redir_proto(char *spool_buf, int len, 544 int *_ac, char ***_av) 545 { 546 char **av; 547 int ac, space; 548 struct protoent *protoent; 549 struct cfg_redir *r; 550 551 av = *_av; 552 ac = *_ac; 553 if (len >= SOF_REDIR) { 554 r = (struct cfg_redir *)spool_buf; 555 /* Skip cfg_redir at beginning of buf. */ 556 spool_buf = &spool_buf[SOF_REDIR]; 557 space = SOF_REDIR; 558 len -= SOF_REDIR; 559 } else 560 goto nospace; 561 r->mode = REDIR_PROTO; 562 /* 563 * Extract protocol. 564 */ 565 if (ac == 0) 566 errx(EX_DATAERR, "redirect_proto: missing protocol"); 567 568 protoent = getprotobyname(*av); 569 if (protoent == NULL) 570 errx(EX_DATAERR, "redirect_proto: unknown protocol %s", *av); 571 else 572 r->proto = protoent->p_proto; 573 574 INC_ARGCV(); 575 576 /* 577 * Extract local address. 578 */ 579 if (ac == 0) 580 errx(EX_DATAERR, "redirect_proto: missing local address"); 581 else 582 StrToAddr(*av, &r->laddr); 583 584 INC_ARGCV(); 585 586 /* 587 * Extract optional public address. 588 */ 589 if (ac == 0) { 590 r->paddr.s_addr = INADDR_ANY; 591 r->raddr.s_addr = INADDR_ANY; 592 } else { 593 /* see above in setup_redir_port() */ 594 if (!isalpha(**av)) { 595 StrToAddr(*av, &r->paddr); 596 INC_ARGCV(); 597 598 /* 599 * Extract optional remote address. 600 */ 601 /* see above in setup_redir_port() */ 602 if (ac!=0 && !isalpha(**av)) { 603 StrToAddr(*av, &r->raddr); 604 INC_ARGCV(); 605 } 606 } 607 } 608 return (space); 609 nospace: 610 errx(EX_DATAERR, "redirect_proto: buf is too small\n"); 611 } 612 613 static void 614 print_nat_config(unsigned char *buf) 615 { 616 struct cfg_nat *n; 617 int i, cnt, flag, off; 618 struct cfg_redir *t; 619 struct cfg_spool *s; 620 struct protoent *p; 621 622 n = (struct cfg_nat *)buf; 623 flag = 1; 624 off = sizeof(*n); 625 printf("ipfw nat %u config", n->id); 626 if (strlen(n->if_name) != 0) 627 printf(" if %s", n->if_name); 628 else if (n->ip.s_addr != 0) 629 printf(" ip %s", inet_ntoa(n->ip)); 630 while (n->mode != 0) { 631 if (n->mode & PKT_ALIAS_LOG) { 632 printf(" log"); 633 n->mode &= ~PKT_ALIAS_LOG; 634 } else if (n->mode & PKT_ALIAS_DENY_INCOMING) { 635 printf(" deny_in"); 636 n->mode &= ~PKT_ALIAS_DENY_INCOMING; 637 } else if (n->mode & PKT_ALIAS_SAME_PORTS) { 638 printf(" same_ports"); 639 n->mode &= ~PKT_ALIAS_SAME_PORTS; 640 } else if (n->mode & PKT_ALIAS_UNREGISTERED_ONLY) { 641 printf(" unreg_only"); 642 n->mode &= ~PKT_ALIAS_UNREGISTERED_ONLY; 643 } else if (n->mode & PKT_ALIAS_RESET_ON_ADDR_CHANGE) { 644 printf(" reset"); 645 n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE; 646 } else if (n->mode & PKT_ALIAS_REVERSE) { 647 printf(" reverse"); 648 n->mode &= ~PKT_ALIAS_REVERSE; 649 } else if (n->mode & PKT_ALIAS_PROXY_ONLY) { 650 printf(" proxy_only"); 651 n->mode &= ~PKT_ALIAS_PROXY_ONLY; 652 } 653 } 654 /* Print all the redirect's data configuration. */ 655 for (cnt = 0; cnt < n->redir_cnt; cnt++) { 656 t = (struct cfg_redir *)&buf[off]; 657 off += SOF_REDIR; 658 switch (t->mode) { 659 case REDIR_ADDR: 660 printf(" redirect_addr"); 661 if (t->spool_cnt == 0) 662 printf(" %s", inet_ntoa(t->laddr)); 663 else 664 for (i = 0; i < t->spool_cnt; i++) { 665 s = (struct cfg_spool *)&buf[off]; 666 if (i) 667 printf(","); 668 else 669 printf(" "); 670 printf("%s", inet_ntoa(s->addr)); 671 off += SOF_SPOOL; 672 } 673 printf(" %s", inet_ntoa(t->paddr)); 674 break; 675 case REDIR_PORT: 676 p = getprotobynumber(t->proto); 677 printf(" redirect_port %s ", p->p_name); 678 if (!t->spool_cnt) { 679 printf("%s:%u", inet_ntoa(t->laddr), t->lport); 680 if (t->pport_cnt > 1) 681 printf("-%u", t->lport + 682 t->pport_cnt - 1); 683 } else 684 for (i=0; i < t->spool_cnt; i++) { 685 s = (struct cfg_spool *)&buf[off]; 686 if (i) 687 printf(","); 688 printf("%s:%u", inet_ntoa(s->addr), 689 s->port); 690 off += SOF_SPOOL; 691 } 692 693 printf(" "); 694 if (t->paddr.s_addr) 695 printf("%s:", inet_ntoa(t->paddr)); 696 printf("%u", t->pport); 697 if (!t->spool_cnt && t->pport_cnt > 1) 698 printf("-%u", t->pport + t->pport_cnt - 1); 699 700 if (t->raddr.s_addr) { 701 printf(" %s", inet_ntoa(t->raddr)); 702 if (t->rport) { 703 printf(":%u", t->rport); 704 if (!t->spool_cnt && t->rport_cnt > 1) 705 printf("-%u", t->rport + 706 t->rport_cnt - 1); 707 } 708 } 709 break; 710 case REDIR_PROTO: 711 p = getprotobynumber(t->proto); 712 printf(" redirect_proto %s %s", p->p_name, 713 inet_ntoa(t->laddr)); 714 if (t->paddr.s_addr != 0) { 715 printf(" %s", inet_ntoa(t->paddr)); 716 if (t->raddr.s_addr) 717 printf(" %s", inet_ntoa(t->raddr)); 718 } 719 break; 720 default: 721 errx(EX_DATAERR, "unknown redir mode"); 722 break; 723 } 724 } 725 printf("\n"); 726 } 727 728 void 729 ipfw_config_nat(int ac, char **av) 730 { 731 struct cfg_nat *n; /* Nat instance configuration. */ 732 int i, len, off, tok; 733 char *id, buf[NAT_BUF_LEN]; /* Buffer for serialized data. */ 734 735 len = NAT_BUF_LEN; 736 /* Offset in buf: save space for n at the beginning. */ 737 off = sizeof(*n); 738 memset(buf, 0, sizeof(buf)); 739 n = (struct cfg_nat *)buf; 740 741 av++; ac--; 742 /* Nat id. */ 743 if (ac && isdigit(**av)) { 744 id = *av; 745 i = atoi(*av); 746 ac--; av++; 747 n->id = i; 748 } else 749 errx(EX_DATAERR, "missing nat id"); 750 if (ac == 0) 751 errx(EX_DATAERR, "missing option"); 752 753 while (ac > 0) { 754 tok = match_token(nat_params, *av); 755 ac--; av++; 756 switch (tok) { 757 case TOK_IP: 758 if (ac == 0) 759 errx(EX_DATAERR, "missing option"); 760 if (!inet_aton(av[0], &(n->ip))) 761 errx(EX_DATAERR, "bad ip address ``%s''", 762 av[0]); 763 ac--; av++; 764 break; 765 case TOK_IF: 766 if (ac == 0) 767 errx(EX_DATAERR, "missing option"); 768 set_addr_dynamic(av[0], n); 769 ac--; av++; 770 break; 771 case TOK_ALOG: 772 n->mode |= PKT_ALIAS_LOG; 773 break; 774 case TOK_DENY_INC: 775 n->mode |= PKT_ALIAS_DENY_INCOMING; 776 break; 777 case TOK_SAME_PORTS: 778 n->mode |= PKT_ALIAS_SAME_PORTS; 779 break; 780 case TOK_UNREG_ONLY: 781 n->mode |= PKT_ALIAS_UNREGISTERED_ONLY; 782 break; 783 case TOK_RESET_ADDR: 784 n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE; 785 break; 786 case TOK_ALIAS_REV: 787 n->mode |= PKT_ALIAS_REVERSE; 788 break; 789 case TOK_PROXY_ONLY: 790 n->mode |= PKT_ALIAS_PROXY_ONLY; 791 break; 792 /* 793 * All the setup_redir_* functions work directly in the final 794 * buffer, see above for details. 795 */ 796 case TOK_REDIR_ADDR: 797 case TOK_REDIR_PORT: 798 case TOK_REDIR_PROTO: 799 switch (tok) { 800 case TOK_REDIR_ADDR: 801 i = setup_redir_addr(&buf[off], len, &ac, &av); 802 break; 803 case TOK_REDIR_PORT: 804 i = setup_redir_port(&buf[off], len, &ac, &av); 805 break; 806 case TOK_REDIR_PROTO: 807 i = setup_redir_proto(&buf[off], len, &ac, &av); 808 break; 809 } 810 n->redir_cnt++; 811 off += i; 812 len -= i; 813 break; 814 default: 815 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]); 816 } 817 } 818 819 i = do_cmd(IP_FW_NAT_CFG, buf, off); 820 if (i) 821 err(1, "setsockopt(%s)", "IP_FW_NAT_CFG"); 822 823 if (!co.do_quiet) { 824 /* After every modification, we show the resultant rule. */ 825 int _ac = 3; 826 char *_av[] = {"show", "config", id}; 827 ipfw_show_nat(_ac, _av); 828 } 829 } 830 831 832 void 833 ipfw_show_nat(int ac, char **av) 834 { 835 struct cfg_nat *n; 836 struct cfg_redir *e; 837 int cmd, i, nbytes, do_cfg, do_rule, frule, lrule, nalloc, size; 838 int nat_cnt, redir_cnt, r; 839 uint8_t *data, *p; 840 char *endptr; 841 842 do_rule = 0; 843 nalloc = 1024; 844 size = 0; 845 data = NULL; 846 frule = 0; 847 lrule = IPFW_DEFAULT_RULE; /* max ipfw rule number */ 848 ac--; av++; 849 850 if (co.test_only) 851 return; 852 853 /* Parse parameters. */ 854 for (cmd = IP_FW_NAT_GET_LOG, do_cfg = 0; ac != 0; ac--, av++) { 855 if (!strncmp(av[0], "config", strlen(av[0]))) { 856 cmd = IP_FW_NAT_GET_CONFIG, do_cfg = 1; 857 continue; 858 } 859 /* Convert command line rule #. */ 860 frule = lrule = strtoul(av[0], &endptr, 10); 861 if (*endptr == '-') 862 lrule = strtoul(endptr+1, &endptr, 10); 863 if (lrule == 0) 864 err(EX_USAGE, "invalid rule number: %s", av[0]); 865 do_rule = 1; 866 } 867 868 nbytes = nalloc; 869 while (nbytes >= nalloc) { 870 nalloc = nalloc * 2; 871 nbytes = nalloc; 872 data = safe_realloc(data, nbytes); 873 if (do_cmd(cmd, data, (uintptr_t)&nbytes) < 0) 874 err(EX_OSERR, "getsockopt(IP_FW_GET_%s)", 875 (cmd == IP_FW_NAT_GET_LOG) ? "LOG" : "CONFIG"); 876 } 877 if (nbytes == 0) 878 exit(0); 879 if (do_cfg) { 880 nat_cnt = *((int *)data); 881 for (i = sizeof(nat_cnt); nat_cnt; nat_cnt--) { 882 n = (struct cfg_nat *)&data[i]; 883 if (frule <= n->id && lrule >= n->id) 884 print_nat_config(&data[i]); 885 i += sizeof(struct cfg_nat); 886 for (redir_cnt = 0; redir_cnt < n->redir_cnt; redir_cnt++) { 887 e = (struct cfg_redir *)&data[i]; 888 i += sizeof(struct cfg_redir) + e->spool_cnt * 889 sizeof(struct cfg_spool); 890 } 891 } 892 } else { 893 for (i = 0; 1; i += LIBALIAS_BUF_SIZE + sizeof(int)) { 894 p = &data[i]; 895 if (p == data + nbytes) 896 break; 897 bcopy(p, &r, sizeof(int)); 898 if (do_rule) { 899 if (!(frule <= r && lrule >= r)) 900 continue; 901 } 902 printf("nat %u: %s\n", r, p+sizeof(int)); 903 } 904 } 905 } 906