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/sysctl.h> 28 29 #include "ipfw2.h" 30 31 #include <ctype.h> 32 #include <err.h> 33 #include <errno.h> 34 #include <netdb.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sysexits.h> 39 40 #include <net/if.h> 41 #include <net/if_dl.h> 42 #include <net/route.h> /* def. of struct route */ 43 #include <netinet/in.h> 44 #include <netinet/ip_fw.h> 45 #include <arpa/inet.h> 46 #include <alias.h> 47 48 typedef int (nat_cb_t)(struct nat44_cfg_nat *cfg, void *arg); 49 static void nat_show_cfg(struct nat44_cfg_nat *n, void *arg); 50 static void nat_show_log(struct nat44_cfg_nat *n, void *arg); 51 static int nat_show_data(struct nat44_cfg_nat *cfg, void *arg); 52 static int natname_cmp(const void *a, const void *b); 53 static int nat_foreach(nat_cb_t *f, void *arg, int sort); 54 static int nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh); 55 56 static struct _s_x nat_params[] = { 57 { "ip", TOK_IP }, 58 { "if", TOK_IF }, 59 { "log", TOK_ALOG }, 60 { "deny_in", TOK_DENY_INC }, 61 { "same_ports", TOK_SAME_PORTS }, 62 { "unreg_only", TOK_UNREG_ONLY }, 63 { "skip_global", TOK_SKIP_GLOBAL }, 64 { "reset", TOK_RESET_ADDR }, 65 { "reverse", TOK_ALIAS_REV }, 66 { "proxy_only", TOK_PROXY_ONLY }, 67 { "redirect_addr", TOK_REDIR_ADDR }, 68 { "redirect_port", TOK_REDIR_PORT }, 69 { "redirect_proto", TOK_REDIR_PROTO }, 70 { NULL, 0 } /* terminator */ 71 }; 72 73 74 /* 75 * Search for interface with name "ifn", and fill n accordingly: 76 * 77 * n->ip ip address of interface "ifn" 78 * n->if_name copy of interface name "ifn" 79 */ 80 static void 81 set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n) 82 { 83 size_t needed; 84 int mib[6]; 85 char *buf, *lim, *next; 86 struct if_msghdr *ifm; 87 struct ifa_msghdr *ifam; 88 struct sockaddr_dl *sdl; 89 struct sockaddr_in *sin; 90 int ifIndex; 91 92 mib[0] = CTL_NET; 93 mib[1] = PF_ROUTE; 94 mib[2] = 0; 95 mib[3] = AF_INET; 96 mib[4] = NET_RT_IFLIST; 97 mib[5] = 0; 98 /* 99 * Get interface data. 100 */ 101 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) 102 err(1, "iflist-sysctl-estimate"); 103 buf = safe_calloc(1, needed); 104 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) 105 err(1, "iflist-sysctl-get"); 106 lim = buf + needed; 107 /* 108 * Loop through interfaces until one with 109 * given name is found. This is done to 110 * find correct interface index for routing 111 * message processing. 112 */ 113 ifIndex = 0; 114 next = buf; 115 while (next < lim) { 116 ifm = (struct if_msghdr *)next; 117 next += ifm->ifm_msglen; 118 if (ifm->ifm_version != RTM_VERSION) { 119 if (co.verbose) 120 warnx("routing message version %d " 121 "not understood", ifm->ifm_version); 122 continue; 123 } 124 if (ifm->ifm_type == RTM_IFINFO) { 125 sdl = (struct sockaddr_dl *)(ifm + 1); 126 if (strlen(ifn) == sdl->sdl_nlen && 127 strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) { 128 ifIndex = ifm->ifm_index; 129 break; 130 } 131 } 132 } 133 if (!ifIndex) 134 errx(1, "unknown interface name %s", ifn); 135 /* 136 * Get interface address. 137 */ 138 sin = NULL; 139 while (next < lim) { 140 ifam = (struct ifa_msghdr *)next; 141 next += ifam->ifam_msglen; 142 if (ifam->ifam_version != RTM_VERSION) { 143 if (co.verbose) 144 warnx("routing message version %d " 145 "not understood", ifam->ifam_version); 146 continue; 147 } 148 if (ifam->ifam_type != RTM_NEWADDR) 149 break; 150 if (ifam->ifam_addrs & RTA_IFA) { 151 int i; 152 char *cp = (char *)(ifam + 1); 153 154 for (i = 1; i < RTA_IFA; i <<= 1) { 155 if (ifam->ifam_addrs & i) 156 cp += SA_SIZE((struct sockaddr *)cp); 157 } 158 if (((struct sockaddr *)cp)->sa_family == AF_INET) { 159 sin = (struct sockaddr_in *)cp; 160 break; 161 } 162 } 163 } 164 if (sin == NULL) 165 n->ip.s_addr = htonl(INADDR_ANY); 166 else 167 n->ip = sin->sin_addr; 168 strncpy(n->if_name, ifn, IF_NAMESIZE); 169 170 free(buf); 171 } 172 173 /* 174 * XXX - The following functions, macros and definitions come from natd.c: 175 * it would be better to move them outside natd.c, in a file 176 * (redirect_support.[ch]?) shared by ipfw and natd, but for now i can live 177 * with it. 178 */ 179 180 /* 181 * Definition of a port range, and macros to deal with values. 182 * FORMAT: HI 16-bits == first port in range, 0 == all ports. 183 * LO 16-bits == number of ports in range 184 * NOTES: - Port values are not stored in network byte order. 185 */ 186 187 #define port_range u_long 188 189 #define GETLOPORT(x) ((x) >> 0x10) 190 #define GETNUMPORTS(x) ((x) & 0x0000ffff) 191 #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x))) 192 193 /* Set y to be the low-port value in port_range variable x. */ 194 #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10)) 195 196 /* Set y to be the number of ports in port_range variable x. */ 197 #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y)) 198 199 static void 200 StrToAddr (const char* str, struct in_addr* addr) 201 { 202 struct hostent* hp; 203 204 if (inet_aton (str, addr)) 205 return; 206 207 hp = gethostbyname (str); 208 if (!hp) 209 errx (1, "unknown host %s", str); 210 211 memcpy (addr, hp->h_addr, sizeof (struct in_addr)); 212 } 213 214 static int 215 StrToPortRange (const char* str, const char* proto, port_range *portRange) 216 { 217 char* sep; 218 struct servent* sp; 219 char* end; 220 u_short loPort; 221 u_short hiPort; 222 223 /* First see if this is a service, return corresponding port if so. */ 224 sp = getservbyname (str,proto); 225 if (sp) { 226 SETLOPORT(*portRange, ntohs(sp->s_port)); 227 SETNUMPORTS(*portRange, 1); 228 return 0; 229 } 230 231 /* Not a service, see if it's a single port or port range. */ 232 sep = strchr (str, '-'); 233 if (sep == NULL) { 234 SETLOPORT(*portRange, strtol(str, &end, 10)); 235 if (end != str) { 236 /* Single port. */ 237 SETNUMPORTS(*portRange, 1); 238 return 0; 239 } 240 241 /* Error in port range field. */ 242 errx (EX_DATAERR, "%s/%s: unknown service", str, proto); 243 } 244 245 /* Port range, get the values and sanity check. */ 246 sscanf (str, "%hu-%hu", &loPort, &hiPort); 247 SETLOPORT(*portRange, loPort); 248 SETNUMPORTS(*portRange, 0); /* Error by default */ 249 if (loPort <= hiPort) 250 SETNUMPORTS(*portRange, hiPort - loPort + 1); 251 252 if (GETNUMPORTS(*portRange) == 0) 253 errx (EX_DATAERR, "invalid port range %s", str); 254 255 return 0; 256 } 257 258 static int 259 StrToProto (const char* str) 260 { 261 if (!strcmp (str, "tcp")) 262 return IPPROTO_TCP; 263 264 if (!strcmp (str, "udp")) 265 return IPPROTO_UDP; 266 267 if (!strcmp (str, "sctp")) 268 return IPPROTO_SCTP; 269 errx (EX_DATAERR, "unknown protocol %s. Expected sctp, tcp or udp", str); 270 } 271 272 static int 273 StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto, 274 port_range *portRange) 275 { 276 char* ptr; 277 278 ptr = strchr (str, ':'); 279 if (!ptr) 280 errx (EX_DATAERR, "%s is missing port number", str); 281 282 *ptr = '\0'; 283 ++ptr; 284 285 StrToAddr (str, addr); 286 return StrToPortRange (ptr, proto, portRange); 287 } 288 289 /* End of stuff taken from natd.c. */ 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 nat44_cfg_redir) and zero or more server pool entry 298 * (struct nat44_cfg_spool) in buf. 299 * 300 * The format of data in buf is: 301 * 302 * nat44_cfg_nat nat44_cfg_redir nat44_cfg_spool ...... nat44_cfg_spool 303 * 304 * ------------------------------------- ------------ 305 * | | .....X ..... | | | | ..... 306 * ------------------------------------- ...... ------------ 307 * ^ 308 * spool_cnt n=0 ...... n=(X-1) 309 * 310 * len points to the amount of available space in buf 311 * space counts the memory consumed by every function 312 * 313 * XXX - Every function get all the argv params so it 314 * has to check, in optional parameters, that the next 315 * args is a valid option for the redir entry and not 316 * another token. Only redir_port and redir_proto are 317 * affected by this. 318 */ 319 320 static int 321 estimate_redir_addr(int *ac, char ***av) 322 { 323 size_t space = sizeof(struct nat44_cfg_redir); 324 char *sep = **av; 325 u_int c = 0; 326 327 (void)ac; /* UNUSED */ 328 while ((sep = strchr(sep, ',')) != NULL) { 329 c++; 330 sep++; 331 } 332 333 if (c > 0) 334 c++; 335 336 space += c * sizeof(struct nat44_cfg_spool); 337 338 return (space); 339 } 340 341 static int 342 setup_redir_addr(char *buf, int *ac, char ***av) 343 { 344 struct nat44_cfg_redir *r; 345 char *sep; 346 size_t space; 347 348 r = (struct nat44_cfg_redir *)buf; 349 r->mode = REDIR_ADDR; 350 /* Skip nat44_cfg_redir at beginning of buf. */ 351 buf = &buf[sizeof(struct nat44_cfg_redir)]; 352 space = sizeof(struct nat44_cfg_redir); 353 354 /* Extract local address. */ 355 if (strchr(**av, ',') != NULL) { 356 struct nat44_cfg_spool *spool; 357 358 /* Setup LSNAT server pool. */ 359 r->laddr.s_addr = INADDR_NONE; 360 sep = strtok(**av, ","); 361 while (sep != NULL) { 362 spool = (struct nat44_cfg_spool *)buf; 363 space += sizeof(struct nat44_cfg_spool); 364 StrToAddr(sep, &spool->addr); 365 spool->port = ~0; 366 r->spool_cnt++; 367 /* Point to the next possible nat44_cfg_spool. */ 368 buf = &buf[sizeof(struct nat44_cfg_spool)]; 369 sep = strtok(NULL, ","); 370 } 371 } else 372 StrToAddr(**av, &r->laddr); 373 (*av)++; (*ac)--; 374 375 /* Extract public address. */ 376 StrToAddr(**av, &r->paddr); 377 (*av)++; (*ac)--; 378 379 return (space); 380 } 381 382 static int 383 estimate_redir_port(int *ac, char ***av) 384 { 385 size_t space = sizeof(struct nat44_cfg_redir); 386 char *sep = **av; 387 u_int c = 0; 388 389 (void)ac; /* UNUSED */ 390 while ((sep = strchr(sep, ',')) != NULL) { 391 c++; 392 sep++; 393 } 394 395 if (c > 0) 396 c++; 397 398 space += c * sizeof(struct nat44_cfg_spool); 399 400 return (space); 401 } 402 403 static int 404 setup_redir_port(char *buf, int *ac, char ***av) 405 { 406 struct nat44_cfg_redir *r; 407 char *sep, *protoName, *lsnat = NULL; 408 size_t space; 409 u_short numLocalPorts; 410 port_range portRange; 411 412 numLocalPorts = 0; 413 414 r = (struct nat44_cfg_redir *)buf; 415 r->mode = REDIR_PORT; 416 /* Skip nat44_cfg_redir at beginning of buf. */ 417 buf = &buf[sizeof(struct nat44_cfg_redir)]; 418 space = sizeof(struct nat44_cfg_redir); 419 420 /* 421 * Extract protocol. 422 */ 423 r->proto = StrToProto(**av); 424 protoName = **av; 425 (*av)++; (*ac)--; 426 427 /* 428 * Extract local address. 429 */ 430 if (strchr(**av, ',') != NULL) { 431 r->laddr.s_addr = INADDR_NONE; 432 r->lport = ~0; 433 numLocalPorts = 1; 434 lsnat = **av; 435 } else { 436 /* 437 * The sctp nat does not allow the port numbers to be mapped to 438 * new port numbers. Therefore, no ports are to be specified 439 * in the target port field. 440 */ 441 if (r->proto == IPPROTO_SCTP) { 442 if (strchr(**av, ':')) 443 errx(EX_DATAERR, "redirect_port:" 444 "port numbers do not change in sctp, so do " 445 "not specify them as part of the target"); 446 else 447 StrToAddr(**av, &r->laddr); 448 } else { 449 if (StrToAddrAndPortRange(**av, &r->laddr, protoName, 450 &portRange) != 0) 451 errx(EX_DATAERR, "redirect_port: " 452 "invalid local port range"); 453 454 r->lport = GETLOPORT(portRange); 455 numLocalPorts = GETNUMPORTS(portRange); 456 } 457 } 458 (*av)++; (*ac)--; 459 460 /* 461 * Extract public port and optionally address. 462 */ 463 if (strchr(**av, ':') != NULL) { 464 if (StrToAddrAndPortRange(**av, &r->paddr, protoName, 465 &portRange) != 0) 466 errx(EX_DATAERR, "redirect_port: " 467 "invalid public port range"); 468 } else { 469 r->paddr.s_addr = INADDR_ANY; 470 if (StrToPortRange(**av, protoName, &portRange) != 0) 471 errx(EX_DATAERR, "redirect_port: " 472 "invalid public port range"); 473 } 474 475 r->pport = GETLOPORT(portRange); 476 if (r->proto == IPPROTO_SCTP) { /* so the logic below still works */ 477 numLocalPorts = GETNUMPORTS(portRange); 478 r->lport = r->pport; 479 } 480 r->pport_cnt = GETNUMPORTS(portRange); 481 (*av)++; (*ac)--; 482 483 /* 484 * Extract remote address and optionally port. 485 */ 486 /* 487 * NB: isdigit(**av) => we've to check that next parameter is really an 488 * option for this redirect entry, else stop here processing arg[cv]. 489 */ 490 if (*ac != 0 && isdigit(***av)) { 491 if (strchr(**av, ':') != NULL) { 492 if (StrToAddrAndPortRange(**av, &r->raddr, protoName, 493 &portRange) != 0) 494 errx(EX_DATAERR, "redirect_port: " 495 "invalid remote port range"); 496 } else { 497 SETLOPORT(portRange, 0); 498 SETNUMPORTS(portRange, 1); 499 StrToAddr(**av, &r->raddr); 500 } 501 (*av)++; (*ac)--; 502 } else { 503 SETLOPORT(portRange, 0); 504 SETNUMPORTS(portRange, 1); 505 r->raddr.s_addr = INADDR_ANY; 506 } 507 r->rport = GETLOPORT(portRange); 508 r->rport_cnt = GETNUMPORTS(portRange); 509 510 /* 511 * Make sure port ranges match up, then add the redirect ports. 512 */ 513 if (numLocalPorts != r->pport_cnt) 514 errx(EX_DATAERR, "redirect_port: " 515 "port ranges must be equal in size"); 516 517 /* Remote port range is allowed to be '0' which means all ports. */ 518 if (r->rport_cnt != numLocalPorts && 519 (r->rport_cnt != 1 || r->rport != 0)) 520 errx(EX_DATAERR, "redirect_port: remote port must" 521 "be 0 or equal to local port range in size"); 522 523 /* Setup LSNAT server pool. */ 524 if (lsnat != NULL) { 525 struct nat44_cfg_spool *spool; 526 527 sep = strtok(lsnat, ","); 528 while (sep != NULL) { 529 spool = (struct nat44_cfg_spool *)buf; 530 space += sizeof(struct nat44_cfg_spool); 531 /* 532 * The sctp nat does not allow the port numbers to 533 * be mapped to new port numbers. Therefore, no ports 534 * are to be specified in the target port field. 535 */ 536 if (r->proto == IPPROTO_SCTP) { 537 if (strchr (sep, ':')) { 538 errx(EX_DATAERR, "redirect_port:" 539 "port numbers do not change in " 540 "sctp, so do not specify them as " 541 "part of the target"); 542 } else { 543 StrToAddr(sep, &spool->addr); 544 spool->port = r->pport; 545 } 546 } else { 547 if (StrToAddrAndPortRange(sep, &spool->addr, 548 protoName, &portRange) != 0) 549 errx(EX_DATAERR, "redirect_port:" 550 "invalid local port range"); 551 if (GETNUMPORTS(portRange) != 1) 552 errx(EX_DATAERR, "redirect_port: " 553 "local port must be single in " 554 "this context"); 555 spool->port = GETLOPORT(portRange); 556 } 557 r->spool_cnt++; 558 /* Point to the next possible nat44_cfg_spool. */ 559 buf = &buf[sizeof(struct nat44_cfg_spool)]; 560 sep = strtok(NULL, ","); 561 } 562 } 563 564 return (space); 565 } 566 567 static int 568 setup_redir_proto(char *buf, int *ac, char ***av) 569 { 570 struct nat44_cfg_redir *r; 571 struct protoent *protoent; 572 size_t space; 573 574 r = (struct nat44_cfg_redir *)buf; 575 r->mode = REDIR_PROTO; 576 /* Skip nat44_cfg_redir at beginning of buf. */ 577 buf = &buf[sizeof(struct nat44_cfg_redir)]; 578 space = sizeof(struct nat44_cfg_redir); 579 580 /* 581 * Extract protocol. 582 */ 583 protoent = getprotobyname(**av); 584 if (protoent == NULL) 585 errx(EX_DATAERR, "redirect_proto: unknown protocol %s", **av); 586 else 587 r->proto = protoent->p_proto; 588 589 (*av)++; (*ac)--; 590 591 /* 592 * Extract local address. 593 */ 594 StrToAddr(**av, &r->laddr); 595 596 (*av)++; (*ac)--; 597 598 /* 599 * Extract optional public address. 600 */ 601 if (*ac == 0) { 602 r->paddr.s_addr = INADDR_ANY; 603 r->raddr.s_addr = INADDR_ANY; 604 } else { 605 /* see above in setup_redir_port() */ 606 if (isdigit(***av)) { 607 StrToAddr(**av, &r->paddr); 608 (*av)++; (*ac)--; 609 610 /* 611 * Extract optional remote address. 612 */ 613 /* see above in setup_redir_port() */ 614 if (*ac != 0 && isdigit(***av)) { 615 StrToAddr(**av, &r->raddr); 616 (*av)++; (*ac)--; 617 } 618 } 619 } 620 621 return (space); 622 } 623 624 static void 625 nat_show_log(struct nat44_cfg_nat *n, void *arg) 626 { 627 char *buf; 628 629 buf = (char *)(n + 1); 630 if (buf[0] != '\0') 631 printf("nat %s: %s\n", n->name, buf); 632 } 633 634 static void 635 nat_show_cfg(struct nat44_cfg_nat *n, void *arg) 636 { 637 int i, cnt, off; 638 struct nat44_cfg_redir *t; 639 struct nat44_cfg_spool *s; 640 caddr_t buf; 641 struct protoent *p; 642 643 buf = (caddr_t)n; 644 off = sizeof(*n); 645 printf("ipfw nat %s config", n->name); 646 if (strlen(n->if_name) != 0) 647 printf(" if %s", n->if_name); 648 else if (n->ip.s_addr != 0) 649 printf(" ip %s", inet_ntoa(n->ip)); 650 while (n->mode != 0) { 651 if (n->mode & PKT_ALIAS_LOG) { 652 printf(" log"); 653 n->mode &= ~PKT_ALIAS_LOG; 654 } else if (n->mode & PKT_ALIAS_DENY_INCOMING) { 655 printf(" deny_in"); 656 n->mode &= ~PKT_ALIAS_DENY_INCOMING; 657 } else if (n->mode & PKT_ALIAS_SAME_PORTS) { 658 printf(" same_ports"); 659 n->mode &= ~PKT_ALIAS_SAME_PORTS; 660 } else if (n->mode & PKT_ALIAS_SKIP_GLOBAL) { 661 printf(" skip_global"); 662 n->mode &= ~PKT_ALIAS_SKIP_GLOBAL; 663 } else if (n->mode & PKT_ALIAS_UNREGISTERED_ONLY) { 664 printf(" unreg_only"); 665 n->mode &= ~PKT_ALIAS_UNREGISTERED_ONLY; 666 } else if (n->mode & PKT_ALIAS_RESET_ON_ADDR_CHANGE) { 667 printf(" reset"); 668 n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE; 669 } else if (n->mode & PKT_ALIAS_REVERSE) { 670 printf(" reverse"); 671 n->mode &= ~PKT_ALIAS_REVERSE; 672 } else if (n->mode & PKT_ALIAS_PROXY_ONLY) { 673 printf(" proxy_only"); 674 n->mode &= ~PKT_ALIAS_PROXY_ONLY; 675 } 676 } 677 /* Print all the redirect's data configuration. */ 678 for (cnt = 0; cnt < n->redir_cnt; cnt++) { 679 t = (struct nat44_cfg_redir *)&buf[off]; 680 off += sizeof(struct nat44_cfg_redir); 681 switch (t->mode) { 682 case REDIR_ADDR: 683 printf(" redirect_addr"); 684 if (t->spool_cnt == 0) 685 printf(" %s", inet_ntoa(t->laddr)); 686 else 687 for (i = 0; i < t->spool_cnt; i++) { 688 s = (struct nat44_cfg_spool *)&buf[off]; 689 if (i) 690 printf(","); 691 else 692 printf(" "); 693 printf("%s", inet_ntoa(s->addr)); 694 off += sizeof(struct nat44_cfg_spool); 695 } 696 printf(" %s", inet_ntoa(t->paddr)); 697 break; 698 case REDIR_PORT: 699 p = getprotobynumber(t->proto); 700 printf(" redirect_port %s ", p->p_name); 701 if (!t->spool_cnt) { 702 printf("%s:%u", inet_ntoa(t->laddr), t->lport); 703 if (t->pport_cnt > 1) 704 printf("-%u", t->lport + 705 t->pport_cnt - 1); 706 } else 707 for (i=0; i < t->spool_cnt; i++) { 708 s = (struct nat44_cfg_spool *)&buf[off]; 709 if (i) 710 printf(","); 711 printf("%s:%u", inet_ntoa(s->addr), 712 s->port); 713 off += sizeof(struct nat44_cfg_spool); 714 } 715 716 printf(" "); 717 if (t->paddr.s_addr) 718 printf("%s:", inet_ntoa(t->paddr)); 719 printf("%u", t->pport); 720 if (!t->spool_cnt && t->pport_cnt > 1) 721 printf("-%u", t->pport + t->pport_cnt - 1); 722 723 if (t->raddr.s_addr) { 724 printf(" %s", inet_ntoa(t->raddr)); 725 if (t->rport) { 726 printf(":%u", t->rport); 727 if (!t->spool_cnt && t->rport_cnt > 1) 728 printf("-%u", t->rport + 729 t->rport_cnt - 1); 730 } 731 } 732 break; 733 case REDIR_PROTO: 734 p = getprotobynumber(t->proto); 735 printf(" redirect_proto %s %s", p->p_name, 736 inet_ntoa(t->laddr)); 737 if (t->paddr.s_addr != 0) { 738 printf(" %s", inet_ntoa(t->paddr)); 739 if (t->raddr.s_addr) 740 printf(" %s", inet_ntoa(t->raddr)); 741 } 742 break; 743 default: 744 errx(EX_DATAERR, "unknown redir mode"); 745 break; 746 } 747 } 748 printf("\n"); 749 } 750 751 void 752 ipfw_config_nat(int ac, char **av) 753 { 754 ipfw_obj_header *oh; 755 struct nat44_cfg_nat *n; /* Nat instance configuration. */ 756 int i, off, tok, ac1; 757 char *id, *buf, **av1, *end; 758 size_t len; 759 760 av++; 761 ac--; 762 /* Nat id. */ 763 if (ac == 0) 764 errx(EX_DATAERR, "missing nat id"); 765 id = *av; 766 i = (int)strtol(id, &end, 0); 767 if (i <= 0 || *end != '\0') 768 errx(EX_DATAERR, "illegal nat id: %s", id); 769 av++; 770 ac--; 771 if (ac == 0) 772 errx(EX_DATAERR, "missing option"); 773 774 len = sizeof(*oh) + sizeof(*n); 775 ac1 = ac; 776 av1 = av; 777 while (ac1 > 0) { 778 tok = match_token(nat_params, *av1); 779 ac1--; 780 av1++; 781 switch (tok) { 782 case TOK_IP: 783 case TOK_IF: 784 ac1--; 785 av1++; 786 break; 787 case TOK_ALOG: 788 case TOK_DENY_INC: 789 case TOK_SAME_PORTS: 790 case TOK_SKIP_GLOBAL: 791 case TOK_UNREG_ONLY: 792 case TOK_RESET_ADDR: 793 case TOK_ALIAS_REV: 794 case TOK_PROXY_ONLY: 795 break; 796 case TOK_REDIR_ADDR: 797 if (ac1 < 2) 798 errx(EX_DATAERR, "redirect_addr: " 799 "not enough arguments"); 800 len += estimate_redir_addr(&ac1, &av1); 801 av1 += 2; 802 ac1 -= 2; 803 break; 804 case TOK_REDIR_PORT: 805 if (ac1 < 3) 806 errx(EX_DATAERR, "redirect_port: " 807 "not enough arguments"); 808 av1++; 809 ac1--; 810 len += estimate_redir_port(&ac1, &av1); 811 av1 += 2; 812 ac1 -= 2; 813 /* Skip optional remoteIP/port */ 814 if (ac1 != 0 && isdigit(**av1)) { 815 av1++; 816 ac1--; 817 } 818 break; 819 case TOK_REDIR_PROTO: 820 if (ac1 < 2) 821 errx(EX_DATAERR, "redirect_proto: " 822 "not enough arguments"); 823 len += sizeof(struct nat44_cfg_redir); 824 av1 += 2; 825 ac1 -= 2; 826 /* Skip optional remoteIP/port */ 827 if (ac1 != 0 && isdigit(**av1)) { 828 av1++; 829 ac1--; 830 } 831 if (ac1 != 0 && isdigit(**av1)) { 832 av1++; 833 ac1--; 834 } 835 break; 836 default: 837 errx(EX_DATAERR, "unrecognised option ``%s''", av1[-1]); 838 } 839 } 840 841 if ((buf = malloc(len)) == NULL) 842 errx(EX_OSERR, "malloc failed"); 843 844 /* Offset in buf: save space for header at the beginning. */ 845 off = sizeof(*oh) + sizeof(*n); 846 memset(buf, 0, len); 847 oh = (ipfw_obj_header *)buf; 848 n = (struct nat44_cfg_nat *)(oh + 1); 849 oh->ntlv.head.length = sizeof(oh->ntlv); 850 snprintf(oh->ntlv.name, sizeof(oh->ntlv.name), "%d", i); 851 snprintf(n->name, sizeof(n->name), "%d", i); 852 853 while (ac > 0) { 854 tok = match_token(nat_params, *av); 855 ac--; 856 av++; 857 switch (tok) { 858 case TOK_IP: 859 if (ac == 0) 860 errx(EX_DATAERR, "missing option"); 861 if (!inet_aton(av[0], &(n->ip))) 862 errx(EX_DATAERR, "bad ip address ``%s''", 863 av[0]); 864 ac--; 865 av++; 866 break; 867 case TOK_IF: 868 if (ac == 0) 869 errx(EX_DATAERR, "missing option"); 870 set_addr_dynamic(av[0], n); 871 ac--; 872 av++; 873 break; 874 case TOK_ALOG: 875 n->mode |= PKT_ALIAS_LOG; 876 break; 877 case TOK_DENY_INC: 878 n->mode |= PKT_ALIAS_DENY_INCOMING; 879 break; 880 case TOK_SAME_PORTS: 881 n->mode |= PKT_ALIAS_SAME_PORTS; 882 break; 883 case TOK_UNREG_ONLY: 884 n->mode |= PKT_ALIAS_UNREGISTERED_ONLY; 885 break; 886 case TOK_SKIP_GLOBAL: 887 n->mode |= PKT_ALIAS_SKIP_GLOBAL; 888 break; 889 case TOK_RESET_ADDR: 890 n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE; 891 break; 892 case TOK_ALIAS_REV: 893 n->mode |= PKT_ALIAS_REVERSE; 894 break; 895 case TOK_PROXY_ONLY: 896 n->mode |= PKT_ALIAS_PROXY_ONLY; 897 break; 898 /* 899 * All the setup_redir_* functions work directly in 900 * the final buffer, see above for details. 901 */ 902 case TOK_REDIR_ADDR: 903 case TOK_REDIR_PORT: 904 case TOK_REDIR_PROTO: 905 switch (tok) { 906 case TOK_REDIR_ADDR: 907 i = setup_redir_addr(&buf[off], &ac, &av); 908 break; 909 case TOK_REDIR_PORT: 910 i = setup_redir_port(&buf[off], &ac, &av); 911 break; 912 case TOK_REDIR_PROTO: 913 i = setup_redir_proto(&buf[off], &ac, &av); 914 break; 915 } 916 n->redir_cnt++; 917 off += i; 918 break; 919 } 920 } 921 922 i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len); 923 if (i != 0) 924 err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG"); 925 926 if (!co.do_quiet) { 927 /* After every modification, we show the resultant rule. */ 928 int _ac = 3; 929 const char *_av[] = {"show", "config", id}; 930 ipfw_show_nat(_ac, (char **)(void *)_av); 931 } 932 } 933 934 struct nat_list_arg { 935 uint16_t cmd; 936 int is_all; 937 }; 938 939 static int 940 nat_show_data(struct nat44_cfg_nat *cfg, void *arg) 941 { 942 struct nat_list_arg *nla; 943 ipfw_obj_header *oh; 944 945 nla = (struct nat_list_arg *)arg; 946 947 switch (nla->cmd) { 948 case IP_FW_NAT44_XGETCONFIG: 949 if (nat_get_cmd(cfg->name, nla->cmd, &oh) != 0) { 950 warnx("Error getting nat instance %s info", cfg->name); 951 break; 952 } 953 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL); 954 free(oh); 955 break; 956 case IP_FW_NAT44_XGETLOG: 957 if (nat_get_cmd(cfg->name, nla->cmd, &oh) == 0) { 958 nat_show_log((struct nat44_cfg_nat *)(oh + 1), NULL); 959 free(oh); 960 break; 961 } 962 /* Handle error */ 963 if (nla->is_all != 0 && errno == ENOENT) 964 break; 965 warn("Error getting nat instance %s info", cfg->name); 966 break; 967 } 968 969 return (0); 970 } 971 972 /* 973 * Compare nat names. 974 * Honor number comparison. 975 */ 976 static int 977 natname_cmp(const void *a, const void *b) 978 { 979 struct nat44_cfg_nat *ia, *ib; 980 981 ia = (struct nat44_cfg_nat *)a; 982 ib = (struct nat44_cfg_nat *)b; 983 984 return (stringnum_cmp(ia->name, ib->name)); 985 } 986 987 /* 988 * Retrieves nat list from kernel, 989 * optionally sorts it and calls requested function for each table. 990 * Returns 0 on success. 991 */ 992 static int 993 nat_foreach(nat_cb_t *f, void *arg, int sort) 994 { 995 ipfw_obj_lheader *olh; 996 struct nat44_cfg_nat *cfg; 997 size_t sz; 998 int i, error; 999 1000 /* Start with reasonable default */ 1001 sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat); 1002 1003 for (;;) { 1004 if ((olh = calloc(1, sz)) == NULL) 1005 return (ENOMEM); 1006 1007 olh->size = sz; 1008 if (do_get3(IP_FW_NAT44_LIST_NAT, &olh->opheader, &sz) != 0) { 1009 sz = olh->size; 1010 free(olh); 1011 if (errno == ENOMEM) 1012 continue; 1013 return (errno); 1014 } 1015 1016 if (sort != 0) 1017 qsort(olh + 1, olh->count, olh->objsize, natname_cmp); 1018 1019 cfg = (struct nat44_cfg_nat*)(olh + 1); 1020 for (i = 0; i < olh->count; i++) { 1021 error = f(cfg, arg); /* Ignore errors for now */ 1022 cfg = (struct nat44_cfg_nat *)((caddr_t)cfg + 1023 olh->objsize); 1024 } 1025 1026 free(olh); 1027 break; 1028 } 1029 1030 return (0); 1031 } 1032 1033 static int 1034 nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh) 1035 { 1036 ipfw_obj_header *oh; 1037 struct nat44_cfg_nat *cfg; 1038 size_t sz; 1039 1040 /* Start with reasonable default */ 1041 sz = sizeof(*oh) + sizeof(*cfg) + 128; 1042 1043 for (;;) { 1044 if ((oh = calloc(1, sz)) == NULL) 1045 return (ENOMEM); 1046 cfg = (struct nat44_cfg_nat *)(oh + 1); 1047 oh->ntlv.head.length = sizeof(oh->ntlv); 1048 strlcpy(oh->ntlv.name, name, sizeof(oh->ntlv.name)); 1049 strlcpy(cfg->name, name, sizeof(cfg->name)); 1050 1051 if (do_get3(cmd, &oh->opheader, &sz) != 0) { 1052 sz = cfg->size; 1053 free(oh); 1054 if (errno == ENOMEM) 1055 continue; 1056 return (errno); 1057 } 1058 1059 *ooh = oh; 1060 break; 1061 } 1062 1063 return (0); 1064 } 1065 1066 void 1067 ipfw_show_nat(int ac, char **av) 1068 { 1069 ipfw_obj_header *oh; 1070 char *name; 1071 int cmd; 1072 struct nat_list_arg nla; 1073 1074 ac--; 1075 av++; 1076 1077 if (co.test_only) 1078 return; 1079 1080 /* Parse parameters. */ 1081 cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */ 1082 name = NULL; 1083 for ( ; ac != 0; ac--, av++) { 1084 if (!strncmp(av[0], "config", strlen(av[0]))) { 1085 cmd = IP_FW_NAT44_XGETCONFIG; 1086 continue; 1087 } 1088 if (strcmp(av[0], "log") == 0) { 1089 cmd = IP_FW_NAT44_XGETLOG; 1090 continue; 1091 } 1092 if (name != NULL) 1093 err(EX_USAGE,"only one instance name may be specified"); 1094 name = av[0]; 1095 } 1096 1097 if (cmd == 0) 1098 errx(EX_USAGE, "Please specify action. Available: config,log"); 1099 1100 if (name == NULL) { 1101 memset(&nla, 0, sizeof(nla)); 1102 nla.cmd = cmd; 1103 nla.is_all = 1; 1104 nat_foreach(nat_show_data, &nla, 1); 1105 } else { 1106 if (nat_get_cmd(name, cmd, &oh) != 0) 1107 err(EX_OSERR, "Error getting nat %s instance info", name); 1108 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL); 1109 free(oh); 1110 } 1111 } 1112 1113