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 { "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 */
75 * Search for interface with name "ifn", and fill n accordingly:
77 * n->ip ip address of interface "ifn"
78 * n->if_name copy of interface name "ifn"
81 set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n)
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;
96 mib[4] = NET_RT_IFLIST;
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");
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.
116 ifm = (struct if_msghdr *)next;
117 next += ifm->ifm_msglen;
118 if (ifm->ifm_version != RTM_VERSION) {
120 warnx("routing message version %d "
121 "not understood", ifm->ifm_version);
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;
134 errx(1, "unknown interface name %s", ifn);
136 * Get interface address.
140 ifam = (struct ifa_msghdr *)next;
141 next += ifam->ifam_msglen;
142 if (ifam->ifam_version != RTM_VERSION) {
144 warnx("routing message version %d "
145 "not understood", ifam->ifam_version);
148 if (ifam->ifam_type != RTM_NEWADDR)
150 if (ifam->ifam_addrs & RTA_IFA) {
152 char *cp = (char *)(ifam + 1);
154 for (i = 1; i < RTA_IFA; i <<= 1) {
155 if (ifam->ifam_addrs & i)
156 cp += SA_SIZE((struct sockaddr *)cp);
158 if (((struct sockaddr *)cp)->sa_family == AF_INET) {
159 sin = (struct sockaddr_in *)cp;
165 n->ip.s_addr = htonl(INADDR_ANY);
167 n->ip = sin->sin_addr;
168 strncpy(n->if_name, ifn, IF_NAMESIZE);
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
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.
187 #define port_range u_long
189 #define GETLOPORT(x) ((x) >> 0x10)
190 #define GETNUMPORTS(x) ((x) & 0x0000ffff)
191 #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x)))
193 /* Set y to be the low-port value in port_range variable x. */
194 #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10))
196 /* Set y to be the number of ports in port_range variable x. */
197 #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y))
200 StrToAddr (const char* str, struct in_addr* addr)
204 if (inet_aton (str, addr))
207 hp = gethostbyname (str);
209 errx (1, "unknown host %s", str);
211 memcpy (addr, hp->h_addr, sizeof (struct in_addr));
215 StrToPortRange (const char* str, const char* proto, port_range *portRange)
223 /* First see if this is a service, return corresponding port if so. */
224 sp = getservbyname (str,proto);
226 SETLOPORT(*portRange, ntohs(sp->s_port));
227 SETNUMPORTS(*portRange, 1);
231 /* Not a service, see if it's a single port or port range. */
232 sep = strchr (str, '-');
234 SETLOPORT(*portRange, strtol(str, &end, 10));
237 SETNUMPORTS(*portRange, 1);
241 /* Error in port range field. */
242 errx (EX_DATAERR, "%s/%s: unknown service", str, proto);
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);
252 if (GETNUMPORTS(*portRange) == 0)
253 errx (EX_DATAERR, "invalid port range %s", str);
259 StrToProto (const char* str)
261 if (!strcmp (str, "tcp"))
264 if (!strcmp (str, "udp"))
267 if (!strcmp (str, "sctp"))
269 errx (EX_DATAERR, "unknown protocol %s. Expected sctp, tcp or udp", str);
273 StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto,
274 port_range *portRange)
278 ptr = strchr (str, ':');
280 errx (EX_DATAERR, "%s is missing port number", str);
285 StrToAddr (str, addr);
286 return StrToPortRange (ptr, proto, portRange);
289 /* End of stuff taken from natd.c. */
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.
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.
300 * The format of data in buf is:
302 * nat44_cfg_nat nat44_cfg_redir nat44_cfg_spool ...... nat44_cfg_spool
304 * ------------------------------------- ------------
305 * | | .....X ..... | | | | .....
306 * ------------------------------------- ...... ------------
308 * spool_cnt n=0 ...... n=(X-1)
310 * len points to the amount of available space in buf
311 * space counts the memory consumed by every function
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
321 estimate_redir_addr(int *ac, char ***av)
323 size_t space = sizeof(struct nat44_cfg_redir);
327 (void)ac; /* UNUSED */
328 while ((sep = strchr(sep, ',')) != NULL) {
336 space += c * sizeof(struct nat44_cfg_spool);
342 setup_redir_addr(char *buf, int *ac, char ***av)
344 struct nat44_cfg_redir *r;
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);
354 /* Extract local address. */
355 if (strchr(**av, ',') != NULL) {
356 struct nat44_cfg_spool *spool;
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);
367 /* Point to the next possible nat44_cfg_spool. */
368 buf = &buf[sizeof(struct nat44_cfg_spool)];
369 sep = strtok(NULL, ",");
372 StrToAddr(**av, &r->laddr);
375 /* Extract public address. */
376 StrToAddr(**av, &r->paddr);
383 estimate_redir_port(int *ac, char ***av)
385 size_t space = sizeof(struct nat44_cfg_redir);
389 (void)ac; /* UNUSED */
390 while ((sep = strchr(sep, ',')) != NULL) {
398 space += c * sizeof(struct nat44_cfg_spool);
404 setup_redir_port(char *buf, int *ac, char ***av)
406 struct nat44_cfg_redir *r;
407 char *sep, *protoName, *lsnat = NULL;
409 u_short numLocalPorts;
410 port_range portRange;
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);
423 r->proto = StrToProto(**av);
428 * Extract local address.
430 if (strchr(**av, ',') != NULL) {
431 r->laddr.s_addr = INADDR_NONE;
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.
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");
447 StrToAddr(**av, &r->laddr);
449 if (StrToAddrAndPortRange(**av, &r->laddr, protoName,
451 errx(EX_DATAERR, "redirect_port: "
452 "invalid local port range");
454 r->lport = GETLOPORT(portRange);
455 numLocalPorts = GETNUMPORTS(portRange);
461 * Extract public port and optionally address.
463 if (strchr(**av, ':') != NULL) {
464 if (StrToAddrAndPortRange(**av, &r->paddr, protoName,
466 errx(EX_DATAERR, "redirect_port: "
467 "invalid public port range");
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");
475 r->pport = GETLOPORT(portRange);
476 if (r->proto == IPPROTO_SCTP) { /* so the logic below still works */
477 numLocalPorts = GETNUMPORTS(portRange);
480 r->pport_cnt = GETNUMPORTS(portRange);
484 * Extract remote address and optionally port.
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].
490 if (*ac != 0 && isdigit(***av)) {
491 if (strchr(**av, ':') != NULL) {
492 if (StrToAddrAndPortRange(**av, &r->raddr, protoName,
494 errx(EX_DATAERR, "redirect_port: "
495 "invalid remote port range");
497 SETLOPORT(portRange, 0);
498 SETNUMPORTS(portRange, 1);
499 StrToAddr(**av, &r->raddr);
503 SETLOPORT(portRange, 0);
504 SETNUMPORTS(portRange, 1);
505 r->raddr.s_addr = INADDR_ANY;
507 r->rport = GETLOPORT(portRange);
508 r->rport_cnt = GETNUMPORTS(portRange);
511 * Make sure port ranges match up, then add the redirect ports.
513 if (numLocalPorts != r->pport_cnt)
514 errx(EX_DATAERR, "redirect_port: "
515 "port ranges must be equal in size");
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");
523 /* Setup LSNAT server pool. */
525 struct nat44_cfg_spool *spool;
527 sep = strtok(lsnat, ",");
528 while (sep != NULL) {
529 spool = (struct nat44_cfg_spool *)buf;
530 space += sizeof(struct nat44_cfg_spool);
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.
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");
543 StrToAddr(sep, &spool->addr);
544 spool->port = r->pport;
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 "
555 spool->port = GETLOPORT(portRange);
558 /* Point to the next possible nat44_cfg_spool. */
559 buf = &buf[sizeof(struct nat44_cfg_spool)];
560 sep = strtok(NULL, ",");
568 setup_redir_proto(char *buf, int *ac, char ***av)
570 struct nat44_cfg_redir *r;
571 struct protoent *protoent;
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);
583 protoent = getprotobyname(**av);
584 if (protoent == NULL)
585 errx(EX_DATAERR, "redirect_proto: unknown protocol %s", **av);
587 r->proto = protoent->p_proto;
592 * Extract local address.
594 StrToAddr(**av, &r->laddr);
599 * Extract optional public address.
602 r->paddr.s_addr = INADDR_ANY;
603 r->raddr.s_addr = INADDR_ANY;
605 /* see above in setup_redir_port() */
606 if (isdigit(***av)) {
607 StrToAddr(**av, &r->paddr);
611 * Extract optional remote address.
613 /* see above in setup_redir_port() */
614 if (*ac != 0 && isdigit(***av)) {
615 StrToAddr(**av, &r->raddr);
625 nat_show_log(struct nat44_cfg_nat *n, void *arg)
629 buf = (char *)(n + 1);
631 printf("nat %s: %s\n", n->name, buf);
635 nat_show_cfg(struct nat44_cfg_nat *n, void *arg)
638 struct nat44_cfg_redir *t;
639 struct nat44_cfg_spool *s;
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) {
653 n->mode &= ~PKT_ALIAS_LOG;
654 } else if (n->mode & PKT_ALIAS_DENY_INCOMING) {
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) {
668 n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE;
669 } else if (n->mode & PKT_ALIAS_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;
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);
683 printf(" redirect_addr");
684 if (t->spool_cnt == 0)
685 printf(" %s", inet_ntoa(t->laddr));
687 for (i = 0; i < t->spool_cnt; i++) {
688 s = (struct nat44_cfg_spool *)&buf[off];
693 printf("%s", inet_ntoa(s->addr));
694 off += sizeof(struct nat44_cfg_spool);
696 printf(" %s", inet_ntoa(t->paddr));
699 p = getprotobynumber(t->proto);
700 printf(" redirect_port %s ", p->p_name);
702 printf("%s:%u", inet_ntoa(t->laddr), t->lport);
703 if (t->pport_cnt > 1)
704 printf("-%u", t->lport +
707 for (i=0; i < t->spool_cnt; i++) {
708 s = (struct nat44_cfg_spool *)&buf[off];
711 printf("%s:%u", inet_ntoa(s->addr),
713 off += sizeof(struct nat44_cfg_spool);
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);
723 if (t->raddr.s_addr) {
724 printf(" %s", inet_ntoa(t->raddr));
726 printf(":%u", t->rport);
727 if (!t->spool_cnt && t->rport_cnt > 1)
728 printf("-%u", t->rport +
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));
740 printf(" %s", inet_ntoa(t->raddr));
744 errx(EX_DATAERR, "unknown redir mode");
752 ipfw_config_nat(int ac, char **av)
755 struct nat44_cfg_nat *n; /* Nat instance configuration. */
756 int i, off, tok, ac1;
757 char *id, *buf, **av1, *end;
764 errx(EX_DATAERR, "missing nat id");
766 i = (int)strtol(id, &end, 0);
767 if (i <= 0 || *end != '\0')
768 errx(EX_DATAERR, "illegal nat id: %s", id);
772 errx(EX_DATAERR, "missing option");
774 len = sizeof(*oh) + sizeof(*n);
778 tok = match_token(nat_params, *av1);
790 case TOK_SKIP_GLOBAL:
798 errx(EX_DATAERR, "redirect_addr: "
799 "not enough arguments");
800 len += estimate_redir_addr(&ac1, &av1);
806 errx(EX_DATAERR, "redirect_port: "
807 "not enough arguments");
810 len += estimate_redir_port(&ac1, &av1);
813 /* Skip optional remoteIP/port */
814 if (ac1 != 0 && isdigit(**av1)) {
819 case TOK_REDIR_PROTO:
821 errx(EX_DATAERR, "redirect_proto: "
822 "not enough arguments");
823 len += sizeof(struct nat44_cfg_redir);
826 /* Skip optional remoteIP/port */
827 if (ac1 != 0 && isdigit(**av1)) {
831 if (ac1 != 0 && isdigit(**av1)) {
837 errx(EX_DATAERR, "unrecognised option ``%s''", av1[-1]);
841 if ((buf = malloc(len)) == NULL)
842 errx(EX_OSERR, "malloc failed");
844 /* Offset in buf: save space for header at the beginning. */
845 off = sizeof(*oh) + sizeof(*n);
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);
854 tok = match_token(nat_params, *av);
860 errx(EX_DATAERR, "missing option");
861 if (!inet_aton(av[0], &(n->ip)))
862 errx(EX_DATAERR, "bad ip address ``%s''",
869 errx(EX_DATAERR, "missing option");
870 set_addr_dynamic(av[0], n);
875 n->mode |= PKT_ALIAS_LOG;
878 n->mode |= PKT_ALIAS_DENY_INCOMING;
881 n->mode |= PKT_ALIAS_SAME_PORTS;
884 n->mode |= PKT_ALIAS_UNREGISTERED_ONLY;
886 case TOK_SKIP_GLOBAL:
887 n->mode |= PKT_ALIAS_SKIP_GLOBAL;
890 n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
893 n->mode |= PKT_ALIAS_REVERSE;
896 n->mode |= PKT_ALIAS_PROXY_ONLY;
899 * All the setup_redir_* functions work directly in
900 * the final buffer, see above for details.
904 case TOK_REDIR_PROTO:
907 i = setup_redir_addr(&buf[off], &ac, &av);
910 i = setup_redir_port(&buf[off], &ac, &av);
912 case TOK_REDIR_PROTO:
913 i = setup_redir_proto(&buf[off], &ac, &av);
922 i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len);
924 err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG");
927 /* After every modification, we show the resultant rule. */
929 const char *_av[] = {"show", "config", id};
930 ipfw_show_nat(_ac, (char **)(void *)_av);
934 struct nat_list_arg {
940 nat_show_data(struct nat44_cfg_nat *cfg, void *arg)
942 struct nat_list_arg *nla;
945 nla = (struct nat_list_arg *)arg;
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);
953 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
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);
963 if (nla->is_all != 0 && errno == ENOENT)
965 warn("Error getting nat instance %s info", cfg->name);
974 * Honor number comparison.
977 natname_cmp(const void *a, const void *b)
979 struct nat44_cfg_nat *ia, *ib;
981 ia = (struct nat44_cfg_nat *)a;
982 ib = (struct nat44_cfg_nat *)b;
984 return (stringnum_cmp(ia->name, ib->name));
988 * Retrieves nat list from kernel,
989 * optionally sorts it and calls requested function for each table.
990 * Returns 0 on success.
993 nat_foreach(nat_cb_t *f, void *arg, int sort)
995 ipfw_obj_lheader *olh;
996 struct nat44_cfg_nat *cfg;
1000 /* Start with reasonable default */
1001 sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat);
1004 if ((olh = calloc(1, sz)) == NULL)
1008 if (do_get3(IP_FW_NAT44_LIST_NAT, &olh->opheader, &sz) != 0) {
1011 if (errno == ENOMEM)
1017 qsort(olh + 1, olh->count, olh->objsize, natname_cmp);
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 +
1034 nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh)
1036 ipfw_obj_header *oh;
1037 struct nat44_cfg_nat *cfg;
1040 /* Start with reasonable default */
1041 sz = sizeof(*oh) + sizeof(*cfg) + 128;
1044 if ((oh = calloc(1, sz)) == NULL)
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));
1051 if (do_get3(cmd, &oh->opheader, &sz) != 0) {
1054 if (errno == ENOMEM)
1067 ipfw_show_nat(int ac, char **av)
1069 ipfw_obj_header *oh;
1072 struct nat_list_arg nla;
1080 /* Parse parameters. */
1081 cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */
1083 for ( ; ac != 0; ac--, av++) {
1084 if (!strncmp(av[0], "config", strlen(av[0]))) {
1085 cmd = IP_FW_NAT44_XGETCONFIG;
1088 if (strcmp(av[0], "log") == 0) {
1089 cmd = IP_FW_NAT44_XGETLOG;
1093 err(EX_USAGE,"only one instance name may be specified");
1098 errx(EX_USAGE, "Please specify action. Available: config,log");
1101 memset(&nla, 0, sizeof(nla));
1104 nat_foreach(nat_show_data, &nla, 1);
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);