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
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
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.
16 * This software is provided ``AS IS'' without any warranties of any kind.
18 * NEW command line interface for IP firewall facility
22 * In-kernel nat support
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/sysctl.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>
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);
56 static struct _s_x nat_params[] = {
60 { "deny_in", TOK_DENY_INC },
61 { "same_ports", TOK_SAME_PORTS },
62 { "unreg_only", TOK_UNREG_ONLY },
63 { "unreg_cgn", TOK_UNREG_CGN },
64 { "skip_global", TOK_SKIP_GLOBAL },
65 { "reset", TOK_RESET_ADDR },
66 { "reverse", TOK_ALIAS_REV },
67 { "proxy_only", TOK_PROXY_ONLY },
68 { "redirect_addr", TOK_REDIR_ADDR },
69 { "redirect_port", TOK_REDIR_PORT },
70 { "redirect_proto", TOK_REDIR_PROTO },
71 { NULL, 0 } /* terminator */
76 * Search for interface with name "ifn", and fill n accordingly:
78 * n->ip ip address of interface "ifn"
79 * n->if_name copy of interface name "ifn"
82 set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n)
86 char *buf, *lim, *next;
87 struct if_msghdr *ifm;
88 struct ifa_msghdr *ifam;
89 struct sockaddr_dl *sdl;
90 struct sockaddr_in *sin;
97 mib[4] = NET_RT_IFLIST;
100 * Get interface data.
102 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
103 err(1, "iflist-sysctl-estimate");
104 buf = safe_calloc(1, needed);
105 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
106 err(1, "iflist-sysctl-get");
109 * Loop through interfaces until one with
110 * given name is found. This is done to
111 * find correct interface index for routing
112 * message processing.
117 ifm = (struct if_msghdr *)next;
118 next += ifm->ifm_msglen;
119 if (ifm->ifm_version != RTM_VERSION) {
121 warnx("routing message version %d "
122 "not understood", ifm->ifm_version);
125 if (ifm->ifm_type == RTM_IFINFO) {
126 sdl = (struct sockaddr_dl *)(ifm + 1);
127 if (strlen(ifn) == sdl->sdl_nlen &&
128 strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) {
129 ifIndex = ifm->ifm_index;
135 errx(1, "unknown interface name %s", ifn);
137 * Get interface address.
141 ifam = (struct ifa_msghdr *)next;
142 next += ifam->ifam_msglen;
143 if (ifam->ifam_version != RTM_VERSION) {
145 warnx("routing message version %d "
146 "not understood", ifam->ifam_version);
149 if (ifam->ifam_type != RTM_NEWADDR)
151 if (ifam->ifam_addrs & RTA_IFA) {
153 char *cp = (char *)(ifam + 1);
155 for (i = 1; i < RTA_IFA; i <<= 1) {
156 if (ifam->ifam_addrs & i)
157 cp += SA_SIZE((struct sockaddr *)cp);
159 if (((struct sockaddr *)cp)->sa_family == AF_INET) {
160 sin = (struct sockaddr_in *)cp;
166 n->ip.s_addr = htonl(INADDR_ANY);
168 n->ip = sin->sin_addr;
169 strncpy(n->if_name, ifn, IF_NAMESIZE);
175 * XXX - The following functions, macros and definitions come from natd.c:
176 * it would be better to move them outside natd.c, in a file
177 * (redirect_support.[ch]?) shared by ipfw and natd, but for now i can live
182 * Definition of a port range, and macros to deal with values.
183 * FORMAT: HI 16-bits == first port in range, 0 == all ports.
184 * LO 16-bits == number of ports in range
185 * NOTES: - Port values are not stored in network byte order.
188 #define port_range u_long
190 #define GETLOPORT(x) ((x) >> 0x10)
191 #define GETNUMPORTS(x) ((x) & 0x0000ffff)
192 #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x)))
194 /* Set y to be the low-port value in port_range variable x. */
195 #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10))
197 /* Set y to be the number of ports in port_range variable x. */
198 #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y))
201 StrToAddr (const char* str, struct in_addr* addr)
205 if (inet_aton (str, addr))
208 hp = gethostbyname (str);
210 errx (1, "unknown host %s", str);
212 memcpy (addr, hp->h_addr, sizeof (struct in_addr));
216 StrToPortRange (const char* str, const char* proto, port_range *portRange)
224 /* First see if this is a service, return corresponding port if so. */
225 sp = getservbyname (str,proto);
227 SETLOPORT(*portRange, ntohs(sp->s_port));
228 SETNUMPORTS(*portRange, 1);
232 /* Not a service, see if it's a single port or port range. */
233 sep = strchr (str, '-');
235 SETLOPORT(*portRange, strtol(str, &end, 10));
238 SETNUMPORTS(*portRange, 1);
242 /* Error in port range field. */
243 errx (EX_DATAERR, "%s/%s: unknown service", str, proto);
246 /* Port range, get the values and sanity check. */
247 sscanf (str, "%hu-%hu", &loPort, &hiPort);
248 SETLOPORT(*portRange, loPort);
249 SETNUMPORTS(*portRange, 0); /* Error by default */
250 if (loPort <= hiPort)
251 SETNUMPORTS(*portRange, hiPort - loPort + 1);
253 if (GETNUMPORTS(*portRange) == 0)
254 errx (EX_DATAERR, "invalid port range %s", str);
260 StrToProto (const char* str)
262 if (!strcmp (str, "tcp"))
265 if (!strcmp (str, "udp"))
268 if (!strcmp (str, "sctp"))
270 errx (EX_DATAERR, "unknown protocol %s. Expected sctp, tcp or udp", str);
274 StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto,
275 port_range *portRange)
279 ptr = strchr (str, ':');
281 errx (EX_DATAERR, "%s is missing port number", str);
286 StrToAddr (str, addr);
287 return StrToPortRange (ptr, proto, portRange);
290 /* End of stuff taken from natd.c. */
293 * The next 3 functions add support for the addr, port and proto redirect and
294 * their logic is loosely based on SetupAddressRedirect(), SetupPortRedirect()
295 * and SetupProtoRedirect() from natd.c.
297 * Every setup_* function fills at least one redirect entry
298 * (struct nat44_cfg_redir) and zero or more server pool entry
299 * (struct nat44_cfg_spool) in buf.
301 * The format of data in buf is:
303 * nat44_cfg_nat nat44_cfg_redir nat44_cfg_spool ...... nat44_cfg_spool
305 * ------------------------------------- ------------
306 * | | .....X ..... | | | | .....
307 * ------------------------------------- ...... ------------
309 * spool_cnt n=0 ...... n=(X-1)
311 * len points to the amount of available space in buf
312 * space counts the memory consumed by every function
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
322 estimate_redir_addr(int *ac, char ***av)
324 size_t space = sizeof(struct nat44_cfg_redir);
328 (void)ac; /* UNUSED */
329 while ((sep = strchr(sep, ',')) != NULL) {
337 space += c * sizeof(struct nat44_cfg_spool);
343 setup_redir_addr(char *buf, int *ac, char ***av)
345 struct nat44_cfg_redir *r;
349 r = (struct nat44_cfg_redir *)buf;
350 r->mode = REDIR_ADDR;
351 /* Skip nat44_cfg_redir at beginning of buf. */
352 buf = &buf[sizeof(struct nat44_cfg_redir)];
353 space = sizeof(struct nat44_cfg_redir);
355 /* Extract local address. */
356 if (strchr(**av, ',') != NULL) {
357 struct nat44_cfg_spool *spool;
359 /* Setup LSNAT server pool. */
360 r->laddr.s_addr = INADDR_NONE;
361 sep = strtok(**av, ",");
362 while (sep != NULL) {
363 spool = (struct nat44_cfg_spool *)buf;
364 space += sizeof(struct nat44_cfg_spool);
365 StrToAddr(sep, &spool->addr);
368 /* Point to the next possible nat44_cfg_spool. */
369 buf = &buf[sizeof(struct nat44_cfg_spool)];
370 sep = strtok(NULL, ",");
373 StrToAddr(**av, &r->laddr);
376 /* Extract public address. */
377 StrToAddr(**av, &r->paddr);
384 estimate_redir_port(int *ac, char ***av)
386 size_t space = sizeof(struct nat44_cfg_redir);
390 (void)ac; /* UNUSED */
391 while ((sep = strchr(sep, ',')) != NULL) {
399 space += c * sizeof(struct nat44_cfg_spool);
405 setup_redir_port(char *buf, int *ac, char ***av)
407 struct nat44_cfg_redir *r;
408 char *sep, *protoName, *lsnat = NULL;
410 u_short numLocalPorts;
411 port_range portRange;
415 r = (struct nat44_cfg_redir *)buf;
416 r->mode = REDIR_PORT;
417 /* Skip nat44_cfg_redir at beginning of buf. */
418 buf = &buf[sizeof(struct nat44_cfg_redir)];
419 space = sizeof(struct nat44_cfg_redir);
424 r->proto = StrToProto(**av);
429 * Extract local address.
431 if (strchr(**av, ',') != NULL) {
432 r->laddr.s_addr = INADDR_NONE;
438 * The sctp nat does not allow the port numbers to be mapped to
439 * new port numbers. Therefore, no ports are to be specified
440 * in the target port field.
442 if (r->proto == IPPROTO_SCTP) {
443 if (strchr(**av, ':'))
444 errx(EX_DATAERR, "redirect_port:"
445 "port numbers do not change in sctp, so do "
446 "not specify them as part of the target");
448 StrToAddr(**av, &r->laddr);
450 if (StrToAddrAndPortRange(**av, &r->laddr, protoName,
452 errx(EX_DATAERR, "redirect_port: "
453 "invalid local port range");
455 r->lport = GETLOPORT(portRange);
456 numLocalPorts = GETNUMPORTS(portRange);
462 * Extract public port and optionally address.
464 if (strchr(**av, ':') != NULL) {
465 if (StrToAddrAndPortRange(**av, &r->paddr, protoName,
467 errx(EX_DATAERR, "redirect_port: "
468 "invalid public port range");
470 r->paddr.s_addr = INADDR_ANY;
471 if (StrToPortRange(**av, protoName, &portRange) != 0)
472 errx(EX_DATAERR, "redirect_port: "
473 "invalid public port range");
476 r->pport = GETLOPORT(portRange);
477 if (r->proto == IPPROTO_SCTP) { /* so the logic below still works */
478 numLocalPorts = GETNUMPORTS(portRange);
481 r->pport_cnt = GETNUMPORTS(portRange);
485 * Extract remote address and optionally port.
488 * NB: isdigit(**av) => we've to check that next parameter is really an
489 * option for this redirect entry, else stop here processing arg[cv].
491 if (*ac != 0 && isdigit(***av)) {
492 if (strchr(**av, ':') != NULL) {
493 if (StrToAddrAndPortRange(**av, &r->raddr, protoName,
495 errx(EX_DATAERR, "redirect_port: "
496 "invalid remote port range");
498 SETLOPORT(portRange, 0);
499 SETNUMPORTS(portRange, 1);
500 StrToAddr(**av, &r->raddr);
504 SETLOPORT(portRange, 0);
505 SETNUMPORTS(portRange, 1);
506 r->raddr.s_addr = INADDR_ANY;
508 r->rport = GETLOPORT(portRange);
509 r->rport_cnt = GETNUMPORTS(portRange);
512 * Make sure port ranges match up, then add the redirect ports.
514 if (numLocalPorts != r->pport_cnt)
515 errx(EX_DATAERR, "redirect_port: "
516 "port ranges must be equal in size");
518 /* Remote port range is allowed to be '0' which means all ports. */
519 if (r->rport_cnt != numLocalPorts &&
520 (r->rport_cnt != 1 || r->rport != 0))
521 errx(EX_DATAERR, "redirect_port: remote port must"
522 "be 0 or equal to local port range in size");
524 /* Setup LSNAT server pool. */
526 struct nat44_cfg_spool *spool;
528 sep = strtok(lsnat, ",");
529 while (sep != NULL) {
530 spool = (struct nat44_cfg_spool *)buf;
531 space += sizeof(struct nat44_cfg_spool);
533 * The sctp nat does not allow the port numbers to
534 * be mapped to new port numbers. Therefore, no ports
535 * are to be specified in the target port field.
537 if (r->proto == IPPROTO_SCTP) {
538 if (strchr (sep, ':')) {
539 errx(EX_DATAERR, "redirect_port:"
540 "port numbers do not change in "
541 "sctp, so do not specify them as "
542 "part of the target");
544 StrToAddr(sep, &spool->addr);
545 spool->port = r->pport;
548 if (StrToAddrAndPortRange(sep, &spool->addr,
549 protoName, &portRange) != 0)
550 errx(EX_DATAERR, "redirect_port:"
551 "invalid local port range");
552 if (GETNUMPORTS(portRange) != 1)
553 errx(EX_DATAERR, "redirect_port: "
554 "local port must be single in "
556 spool->port = GETLOPORT(portRange);
559 /* Point to the next possible nat44_cfg_spool. */
560 buf = &buf[sizeof(struct nat44_cfg_spool)];
561 sep = strtok(NULL, ",");
569 setup_redir_proto(char *buf, int *ac, char ***av)
571 struct nat44_cfg_redir *r;
572 struct protoent *protoent;
575 r = (struct nat44_cfg_redir *)buf;
576 r->mode = REDIR_PROTO;
577 /* Skip nat44_cfg_redir at beginning of buf. */
578 buf = &buf[sizeof(struct nat44_cfg_redir)];
579 space = sizeof(struct nat44_cfg_redir);
584 protoent = getprotobyname(**av);
585 if (protoent == NULL)
586 errx(EX_DATAERR, "redirect_proto: unknown protocol %s", **av);
588 r->proto = protoent->p_proto;
593 * Extract local address.
595 StrToAddr(**av, &r->laddr);
600 * Extract optional public address.
603 r->paddr.s_addr = INADDR_ANY;
604 r->raddr.s_addr = INADDR_ANY;
606 /* see above in setup_redir_port() */
607 if (isdigit(***av)) {
608 StrToAddr(**av, &r->paddr);
612 * Extract optional remote address.
614 /* see above in setup_redir_port() */
615 if (*ac != 0 && isdigit(***av)) {
616 StrToAddr(**av, &r->raddr);
626 nat_show_log(struct nat44_cfg_nat *n, void *arg)
630 buf = (char *)(n + 1);
632 printf("nat %s: %s\n", n->name, buf);
636 nat_show_cfg(struct nat44_cfg_nat *n, void *arg)
639 struct nat44_cfg_redir *t;
640 struct nat44_cfg_spool *s;
646 printf("ipfw nat %s config", n->name);
647 if (strlen(n->if_name) != 0)
648 printf(" if %s", n->if_name);
649 else if (n->ip.s_addr != 0)
650 printf(" ip %s", inet_ntoa(n->ip));
651 while (n->mode != 0) {
652 if (n->mode & PKT_ALIAS_LOG) {
654 n->mode &= ~PKT_ALIAS_LOG;
655 } else if (n->mode & PKT_ALIAS_DENY_INCOMING) {
657 n->mode &= ~PKT_ALIAS_DENY_INCOMING;
658 } else if (n->mode & PKT_ALIAS_SAME_PORTS) {
659 printf(" same_ports");
660 n->mode &= ~PKT_ALIAS_SAME_PORTS;
661 } else if (n->mode & PKT_ALIAS_SKIP_GLOBAL) {
662 printf(" skip_global");
663 n->mode &= ~PKT_ALIAS_SKIP_GLOBAL;
664 } else if (n->mode & PKT_ALIAS_UNREGISTERED_ONLY) {
665 printf(" unreg_only");
666 n->mode &= ~PKT_ALIAS_UNREGISTERED_ONLY;
667 } else if (n->mode & PKT_ALIAS_UNREGISTERED_CGN) {
668 printf(" unreg_cgn");
669 n->mode &= ~PKT_ALIAS_UNREGISTERED_CGN;
670 } else if (n->mode & PKT_ALIAS_RESET_ON_ADDR_CHANGE) {
672 n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE;
673 } else if (n->mode & PKT_ALIAS_REVERSE) {
675 n->mode &= ~PKT_ALIAS_REVERSE;
676 } else if (n->mode & PKT_ALIAS_PROXY_ONLY) {
677 printf(" proxy_only");
678 n->mode &= ~PKT_ALIAS_PROXY_ONLY;
681 /* Print all the redirect's data configuration. */
682 for (cnt = 0; cnt < n->redir_cnt; cnt++) {
683 t = (struct nat44_cfg_redir *)&buf[off];
684 off += sizeof(struct nat44_cfg_redir);
687 printf(" redirect_addr");
688 if (t->spool_cnt == 0)
689 printf(" %s", inet_ntoa(t->laddr));
691 for (i = 0; i < t->spool_cnt; i++) {
692 s = (struct nat44_cfg_spool *)&buf[off];
697 printf("%s", inet_ntoa(s->addr));
698 off += sizeof(struct nat44_cfg_spool);
700 printf(" %s", inet_ntoa(t->paddr));
703 p = getprotobynumber(t->proto);
704 printf(" redirect_port %s ", p->p_name);
706 printf("%s:%u", inet_ntoa(t->laddr), t->lport);
707 if (t->pport_cnt > 1)
708 printf("-%u", t->lport +
711 for (i=0; i < t->spool_cnt; i++) {
712 s = (struct nat44_cfg_spool *)&buf[off];
715 printf("%s:%u", inet_ntoa(s->addr),
717 off += sizeof(struct nat44_cfg_spool);
722 printf("%s:", inet_ntoa(t->paddr));
723 printf("%u", t->pport);
724 if (!t->spool_cnt && t->pport_cnt > 1)
725 printf("-%u", t->pport + t->pport_cnt - 1);
727 if (t->raddr.s_addr) {
728 printf(" %s", inet_ntoa(t->raddr));
730 printf(":%u", t->rport);
731 if (!t->spool_cnt && t->rport_cnt > 1)
732 printf("-%u", t->rport +
738 p = getprotobynumber(t->proto);
739 printf(" redirect_proto %s %s", p->p_name,
740 inet_ntoa(t->laddr));
741 if (t->paddr.s_addr != 0) {
742 printf(" %s", inet_ntoa(t->paddr));
744 printf(" %s", inet_ntoa(t->raddr));
748 errx(EX_DATAERR, "unknown redir mode");
756 ipfw_config_nat(int ac, char **av)
759 struct nat44_cfg_nat *n; /* Nat instance configuration. */
760 int i, off, tok, ac1;
761 char *id, *buf, **av1, *end;
768 errx(EX_DATAERR, "missing nat id");
770 i = (int)strtol(id, &end, 0);
771 if (i <= 0 || *end != '\0')
772 errx(EX_DATAERR, "illegal nat id: %s", id);
776 errx(EX_DATAERR, "missing option");
778 len = sizeof(*oh) + sizeof(*n);
782 tok = match_token(nat_params, *av1);
794 case TOK_SKIP_GLOBAL:
803 errx(EX_DATAERR, "redirect_addr: "
804 "not enough arguments");
805 len += estimate_redir_addr(&ac1, &av1);
811 errx(EX_DATAERR, "redirect_port: "
812 "not enough arguments");
815 len += estimate_redir_port(&ac1, &av1);
818 /* Skip optional remoteIP/port */
819 if (ac1 != 0 && isdigit(**av1)) {
824 case TOK_REDIR_PROTO:
826 errx(EX_DATAERR, "redirect_proto: "
827 "not enough arguments");
828 len += sizeof(struct nat44_cfg_redir);
831 /* Skip optional remoteIP/port */
832 if (ac1 != 0 && isdigit(**av1)) {
836 if (ac1 != 0 && isdigit(**av1)) {
842 errx(EX_DATAERR, "unrecognised option ``%s''", av1[-1]);
846 if ((buf = malloc(len)) == NULL)
847 errx(EX_OSERR, "malloc failed");
849 /* Offset in buf: save space for header at the beginning. */
850 off = sizeof(*oh) + sizeof(*n);
852 oh = (ipfw_obj_header *)buf;
853 n = (struct nat44_cfg_nat *)(oh + 1);
854 oh->ntlv.head.length = sizeof(oh->ntlv);
855 snprintf(oh->ntlv.name, sizeof(oh->ntlv.name), "%d", i);
856 snprintf(n->name, sizeof(n->name), "%d", i);
859 tok = match_token(nat_params, *av);
865 errx(EX_DATAERR, "missing option");
866 if (!inet_aton(av[0], &(n->ip)))
867 errx(EX_DATAERR, "bad ip address ``%s''",
874 errx(EX_DATAERR, "missing option");
875 set_addr_dynamic(av[0], n);
880 n->mode |= PKT_ALIAS_LOG;
883 n->mode |= PKT_ALIAS_DENY_INCOMING;
886 n->mode |= PKT_ALIAS_SAME_PORTS;
889 n->mode |= PKT_ALIAS_UNREGISTERED_ONLY;
892 n->mode |= PKT_ALIAS_UNREGISTERED_CGN;
894 case TOK_SKIP_GLOBAL:
895 n->mode |= PKT_ALIAS_SKIP_GLOBAL;
898 n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
901 n->mode |= PKT_ALIAS_REVERSE;
904 n->mode |= PKT_ALIAS_PROXY_ONLY;
907 * All the setup_redir_* functions work directly in
908 * the final buffer, see above for details.
912 case TOK_REDIR_PROTO:
915 i = setup_redir_addr(&buf[off], &ac, &av);
918 i = setup_redir_port(&buf[off], &ac, &av);
920 case TOK_REDIR_PROTO:
921 i = setup_redir_proto(&buf[off], &ac, &av);
930 i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len);
932 err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG");
935 /* After every modification, we show the resultant rule. */
937 const char *_av[] = {"show", "config", id};
938 ipfw_show_nat(_ac, (char **)(void *)_av);
943 nat_fill_ntlv(ipfw_obj_ntlv *ntlv, int i)
946 ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
947 ntlv->head.length = sizeof(ipfw_obj_ntlv);
949 ntlv->set = 0; /* not yet */
950 snprintf(ntlv->name, sizeof(ntlv->name), "%d", i);
954 ipfw_delete_nat(int i)
959 memset(&oh, 0, sizeof(oh));
960 nat_fill_ntlv(&oh.ntlv, i);
961 ret = do_set3(IP_FW_NAT44_DESTROY, &oh.opheader, sizeof(oh));
964 warn("nat %u not available", i);
965 return (EX_UNAVAILABLE);
970 struct nat_list_arg {
976 nat_show_data(struct nat44_cfg_nat *cfg, void *arg)
978 struct nat_list_arg *nla;
981 nla = (struct nat_list_arg *)arg;
984 case IP_FW_NAT44_XGETCONFIG:
985 if (nat_get_cmd(cfg->name, nla->cmd, &oh) != 0) {
986 warnx("Error getting nat instance %s info", cfg->name);
989 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
992 case IP_FW_NAT44_XGETLOG:
993 if (nat_get_cmd(cfg->name, nla->cmd, &oh) == 0) {
994 nat_show_log((struct nat44_cfg_nat *)(oh + 1), NULL);
999 if (nla->is_all != 0 && errno == ENOENT)
1001 warn("Error getting nat instance %s info", cfg->name);
1009 * Compare nat names.
1010 * Honor number comparison.
1013 natname_cmp(const void *a, const void *b)
1015 struct nat44_cfg_nat *ia, *ib;
1017 ia = (struct nat44_cfg_nat *)a;
1018 ib = (struct nat44_cfg_nat *)b;
1020 return (stringnum_cmp(ia->name, ib->name));
1024 * Retrieves nat list from kernel,
1025 * optionally sorts it and calls requested function for each table.
1026 * Returns 0 on success.
1029 nat_foreach(nat_cb_t *f, void *arg, int sort)
1031 ipfw_obj_lheader *olh;
1032 struct nat44_cfg_nat *cfg;
1036 /* Start with reasonable default */
1037 sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat);
1040 if ((olh = calloc(1, sz)) == NULL)
1044 if (do_get3(IP_FW_NAT44_LIST_NAT, &olh->opheader, &sz) != 0) {
1047 if (errno == ENOMEM)
1053 qsort(olh + 1, olh->count, olh->objsize, natname_cmp);
1055 cfg = (struct nat44_cfg_nat*)(olh + 1);
1056 for (i = 0; i < olh->count; i++) {
1057 error = f(cfg, arg); /* Ignore errors for now */
1058 cfg = (struct nat44_cfg_nat *)((caddr_t)cfg +
1070 nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh)
1072 ipfw_obj_header *oh;
1073 struct nat44_cfg_nat *cfg;
1076 /* Start with reasonable default */
1077 sz = sizeof(*oh) + sizeof(*cfg) + 128;
1080 if ((oh = calloc(1, sz)) == NULL)
1082 cfg = (struct nat44_cfg_nat *)(oh + 1);
1083 oh->ntlv.head.length = sizeof(oh->ntlv);
1084 strlcpy(oh->ntlv.name, name, sizeof(oh->ntlv.name));
1085 strlcpy(cfg->name, name, sizeof(cfg->name));
1087 if (do_get3(cmd, &oh->opheader, &sz) != 0) {
1090 if (errno == ENOMEM)
1103 ipfw_show_nat(int ac, char **av)
1105 ipfw_obj_header *oh;
1108 struct nat_list_arg nla;
1116 /* Parse parameters. */
1117 cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */
1119 for ( ; ac != 0; ac--, av++) {
1120 if (!strncmp(av[0], "config", strlen(av[0]))) {
1121 cmd = IP_FW_NAT44_XGETCONFIG;
1124 if (strcmp(av[0], "log") == 0) {
1125 cmd = IP_FW_NAT44_XGETLOG;
1129 err(EX_USAGE,"only one instance name may be specified");
1134 errx(EX_USAGE, "Please specify action. Available: config,log");
1137 memset(&nla, 0, sizeof(nla));
1140 nat_foreach(nat_show_data, &nla, 1);
1142 if (nat_get_cmd(name, cmd, &oh) != 0)
1143 err(EX_OSERR, "Error getting nat %s instance info", name);
1144 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);