1d17aef79SPedro F. Giffuni /*-
2ead75a59SLuigi Rizzo * Copyright (c) 2002-2003 Luigi Rizzo
3ead75a59SLuigi Rizzo * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4ead75a59SLuigi Rizzo * Copyright (c) 1994 Ugen J.S.Antsilevich
5ead75a59SLuigi Rizzo *
6ead75a59SLuigi Rizzo * Idea and grammar partially left from:
7ead75a59SLuigi Rizzo * Copyright (c) 1993 Daniel Boulet
8ead75a59SLuigi Rizzo *
9ead75a59SLuigi Rizzo * Redistribution and use in source forms, with and without modification,
10ead75a59SLuigi Rizzo * are permitted provided that this entire comment appears intact.
11ead75a59SLuigi Rizzo *
12ead75a59SLuigi Rizzo * Redistribution in binary form may occur without any restrictions.
13ead75a59SLuigi Rizzo * Obviously, it would be nice if you gave credit where credit is due
14ead75a59SLuigi Rizzo * but requiring it would be too onerous.
15ead75a59SLuigi Rizzo *
16ead75a59SLuigi Rizzo * This software is provided ``AS IS'' without any warranties of any kind.
17ead75a59SLuigi Rizzo *
18ead75a59SLuigi Rizzo * NEW command line interface for IP firewall facility
19ead75a59SLuigi Rizzo *
20ead75a59SLuigi Rizzo * In-kernel nat support
21ead75a59SLuigi Rizzo */
22ead75a59SLuigi Rizzo
23ead75a59SLuigi Rizzo #include <sys/types.h>
24ead75a59SLuigi Rizzo #include <sys/socket.h>
25ead75a59SLuigi Rizzo #include <sys/sysctl.h>
26ead75a59SLuigi Rizzo
27ead75a59SLuigi Rizzo #include "ipfw2.h"
28ead75a59SLuigi Rizzo
29ead75a59SLuigi Rizzo #include <ctype.h>
30ead75a59SLuigi Rizzo #include <err.h>
31d6164b77SAlexander V. Chernikov #include <errno.h>
32ead75a59SLuigi Rizzo #include <netdb.h>
33ead75a59SLuigi Rizzo #include <stdio.h>
34ead75a59SLuigi Rizzo #include <stdlib.h>
35ead75a59SLuigi Rizzo #include <string.h>
36ead75a59SLuigi Rizzo #include <sysexits.h>
37ead75a59SLuigi Rizzo
38ead75a59SLuigi Rizzo #include <net/if.h>
39ead75a59SLuigi Rizzo #include <net/if_dl.h>
40ead75a59SLuigi Rizzo #include <net/route.h> /* def. of struct route */
41ead75a59SLuigi Rizzo #include <netinet/in.h>
42ead75a59SLuigi Rizzo #include <netinet/ip_fw.h>
43ead75a59SLuigi Rizzo #include <arpa/inet.h>
44ead75a59SLuigi Rizzo #include <alias.h>
45ead75a59SLuigi Rizzo
46d6164b77SAlexander V. Chernikov typedef int (nat_cb_t)(struct nat44_cfg_nat *cfg, void *arg);
47d6164b77SAlexander V. Chernikov static void nat_show_cfg(struct nat44_cfg_nat *n, void *arg);
48d6164b77SAlexander V. Chernikov static void nat_show_log(struct nat44_cfg_nat *n, void *arg);
49d6164b77SAlexander V. Chernikov static int nat_show_data(struct nat44_cfg_nat *cfg, void *arg);
50d6164b77SAlexander V. Chernikov static int natname_cmp(const void *a, const void *b);
51d6164b77SAlexander V. Chernikov static int nat_foreach(nat_cb_t *f, void *arg, int sort);
52d6164b77SAlexander V. Chernikov static int nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh);
53d6164b77SAlexander V. Chernikov
54ead75a59SLuigi Rizzo static struct _s_x nat_params[] = {
55ead75a59SLuigi Rizzo { "ip", TOK_IP },
56ead75a59SLuigi Rizzo { "if", TOK_IF },
57ead75a59SLuigi Rizzo { "log", TOK_ALOG },
58ead75a59SLuigi Rizzo { "deny_in", TOK_DENY_INC },
59ead75a59SLuigi Rizzo { "same_ports", TOK_SAME_PORTS },
60ead75a59SLuigi Rizzo { "unreg_only", TOK_UNREG_ONLY },
6175b89337SAlexander V. Chernikov { "unreg_cgn", TOK_UNREG_CGN },
621875bbfeSAndrey V. Elsukov { "skip_global", TOK_SKIP_GLOBAL },
63ead75a59SLuigi Rizzo { "reset", TOK_RESET_ADDR },
64ead75a59SLuigi Rizzo { "reverse", TOK_ALIAS_REV },
65ead75a59SLuigi Rizzo { "proxy_only", TOK_PROXY_ONLY },
66a08cdb6cSNeel Chauhan { "port_range", TOK_PORT_ALIAS },
67ead75a59SLuigi Rizzo { "redirect_addr", TOK_REDIR_ADDR },
68ead75a59SLuigi Rizzo { "redirect_port", TOK_REDIR_PORT },
69ead75a59SLuigi Rizzo { "redirect_proto", TOK_REDIR_PROTO },
70ead75a59SLuigi Rizzo { NULL, 0 } /* terminator */
71ead75a59SLuigi Rizzo };
72ead75a59SLuigi Rizzo
73ead75a59SLuigi Rizzo
74ead75a59SLuigi Rizzo /*
75ead75a59SLuigi Rizzo * Search for interface with name "ifn", and fill n accordingly:
76ead75a59SLuigi Rizzo *
77ead75a59SLuigi Rizzo * n->ip ip address of interface "ifn"
78ead75a59SLuigi Rizzo * n->if_name copy of interface name "ifn"
79ead75a59SLuigi Rizzo */
80ead75a59SLuigi Rizzo static void
set_addr_dynamic(const char * ifn,struct nat44_cfg_nat * n)81d6164b77SAlexander V. Chernikov set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n)
82ead75a59SLuigi Rizzo {
83ead75a59SLuigi Rizzo size_t needed;
84ead75a59SLuigi Rizzo int mib[6];
85ead75a59SLuigi Rizzo char *buf, *lim, *next;
86ead75a59SLuigi Rizzo struct if_msghdr *ifm;
87ead75a59SLuigi Rizzo struct ifa_msghdr *ifam;
88ead75a59SLuigi Rizzo struct sockaddr_dl *sdl;
89ead75a59SLuigi Rizzo struct sockaddr_in *sin;
90b7684f4bSMarcelo Araujo int ifIndex;
91ead75a59SLuigi Rizzo
92ead75a59SLuigi Rizzo mib[0] = CTL_NET;
93ead75a59SLuigi Rizzo mib[1] = PF_ROUTE;
94ead75a59SLuigi Rizzo mib[2] = 0;
95ead75a59SLuigi Rizzo mib[3] = AF_INET;
96ead75a59SLuigi Rizzo mib[4] = NET_RT_IFLIST;
97ead75a59SLuigi Rizzo mib[5] = 0;
98ead75a59SLuigi Rizzo /*
99ead75a59SLuigi Rizzo * Get interface data.
100ead75a59SLuigi Rizzo */
101ead75a59SLuigi Rizzo if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
102ead75a59SLuigi Rizzo err(1, "iflist-sysctl-estimate");
103ead75a59SLuigi Rizzo buf = safe_calloc(1, needed);
104ead75a59SLuigi Rizzo if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
105ead75a59SLuigi Rizzo err(1, "iflist-sysctl-get");
106ead75a59SLuigi Rizzo lim = buf + needed;
107ead75a59SLuigi Rizzo /*
108ead75a59SLuigi Rizzo * Loop through interfaces until one with
109ead75a59SLuigi Rizzo * given name is found. This is done to
110ead75a59SLuigi Rizzo * find correct interface index for routing
111ead75a59SLuigi Rizzo * message processing.
112ead75a59SLuigi Rizzo */
113ead75a59SLuigi Rizzo ifIndex = 0;
114ead75a59SLuigi Rizzo next = buf;
115ead75a59SLuigi Rizzo while (next < lim) {
116ead75a59SLuigi Rizzo ifm = (struct if_msghdr *)next;
117ead75a59SLuigi Rizzo next += ifm->ifm_msglen;
118ead75a59SLuigi Rizzo if (ifm->ifm_version != RTM_VERSION) {
11956707beeSMark Johnston if (g_co.verbose)
120ead75a59SLuigi Rizzo warnx("routing message version %d "
121ead75a59SLuigi Rizzo "not understood", ifm->ifm_version);
122ead75a59SLuigi Rizzo continue;
123ead75a59SLuigi Rizzo }
124ead75a59SLuigi Rizzo if (ifm->ifm_type == RTM_IFINFO) {
125ead75a59SLuigi Rizzo sdl = (struct sockaddr_dl *)(ifm + 1);
126ead75a59SLuigi Rizzo if (strlen(ifn) == sdl->sdl_nlen &&
127ead75a59SLuigi Rizzo strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) {
128ead75a59SLuigi Rizzo ifIndex = ifm->ifm_index;
129ead75a59SLuigi Rizzo break;
130ead75a59SLuigi Rizzo }
131ead75a59SLuigi Rizzo }
132ead75a59SLuigi Rizzo }
133ead75a59SLuigi Rizzo if (!ifIndex)
134ead75a59SLuigi Rizzo errx(1, "unknown interface name %s", ifn);
135ead75a59SLuigi Rizzo /*
136ead75a59SLuigi Rizzo * Get interface address.
137ead75a59SLuigi Rizzo */
138ead75a59SLuigi Rizzo sin = NULL;
139ead75a59SLuigi Rizzo while (next < lim) {
140ead75a59SLuigi Rizzo ifam = (struct ifa_msghdr *)next;
141ead75a59SLuigi Rizzo next += ifam->ifam_msglen;
142ead75a59SLuigi Rizzo if (ifam->ifam_version != RTM_VERSION) {
14356707beeSMark Johnston if (g_co.verbose)
144ead75a59SLuigi Rizzo warnx("routing message version %d "
145ead75a59SLuigi Rizzo "not understood", ifam->ifam_version);
146ead75a59SLuigi Rizzo continue;
147ead75a59SLuigi Rizzo }
148ead75a59SLuigi Rizzo if (ifam->ifam_type != RTM_NEWADDR)
149ead75a59SLuigi Rizzo break;
150ead75a59SLuigi Rizzo if (ifam->ifam_addrs & RTA_IFA) {
151ead75a59SLuigi Rizzo int i;
152ead75a59SLuigi Rizzo char *cp = (char *)(ifam + 1);
153ead75a59SLuigi Rizzo
154ead75a59SLuigi Rizzo for (i = 1; i < RTA_IFA; i <<= 1) {
155ead75a59SLuigi Rizzo if (ifam->ifam_addrs & i)
156ead75a59SLuigi Rizzo cp += SA_SIZE((struct sockaddr *)cp);
157ead75a59SLuigi Rizzo }
158ead75a59SLuigi Rizzo if (((struct sockaddr *)cp)->sa_family == AF_INET) {
159ead75a59SLuigi Rizzo sin = (struct sockaddr_in *)cp;
160ead75a59SLuigi Rizzo break;
161ead75a59SLuigi Rizzo }
162ead75a59SLuigi Rizzo }
163ead75a59SLuigi Rizzo }
164ead75a59SLuigi Rizzo if (sin == NULL)
1652aa533b9SJohn Baldwin n->ip.s_addr = htonl(INADDR_ANY);
1662aa533b9SJohn Baldwin else
167ead75a59SLuigi Rizzo n->ip = sin->sin_addr;
168ead75a59SLuigi Rizzo strncpy(n->if_name, ifn, IF_NAMESIZE);
169ead75a59SLuigi Rizzo
170ead75a59SLuigi Rizzo free(buf);
171ead75a59SLuigi Rizzo }
172ead75a59SLuigi Rizzo
173ead75a59SLuigi Rizzo /*
174ead75a59SLuigi Rizzo * XXX - The following functions, macros and definitions come from natd.c:
175ead75a59SLuigi Rizzo * it would be better to move them outside natd.c, in a file
176ead75a59SLuigi Rizzo * (redirect_support.[ch]?) shared by ipfw and natd, but for now i can live
177ead75a59SLuigi Rizzo * with it.
178ead75a59SLuigi Rizzo */
179ead75a59SLuigi Rizzo
180ead75a59SLuigi Rizzo /*
181ead75a59SLuigi Rizzo * Definition of a port range, and macros to deal with values.
182ead75a59SLuigi Rizzo * FORMAT: HI 16-bits == first port in range, 0 == all ports.
183ead75a59SLuigi Rizzo * LO 16-bits == number of ports in range
184ead75a59SLuigi Rizzo * NOTES: - Port values are not stored in network byte order.
185ead75a59SLuigi Rizzo */
186ead75a59SLuigi Rizzo
187ead75a59SLuigi Rizzo #define port_range u_long
188ead75a59SLuigi Rizzo
189ead75a59SLuigi Rizzo #define GETLOPORT(x) ((x) >> 0x10)
190ead75a59SLuigi Rizzo #define GETNUMPORTS(x) ((x) & 0x0000ffff)
191ead75a59SLuigi Rizzo #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x)))
192ead75a59SLuigi Rizzo
193ead75a59SLuigi Rizzo /* Set y to be the low-port value in port_range variable x. */
194ead75a59SLuigi Rizzo #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10))
195ead75a59SLuigi Rizzo
196ead75a59SLuigi Rizzo /* Set y to be the number of ports in port_range variable x. */
197ead75a59SLuigi Rizzo #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y))
198ead75a59SLuigi Rizzo
199ead75a59SLuigi Rizzo static void
StrToAddr(const char * str,struct in_addr * addr)200ead75a59SLuigi Rizzo StrToAddr (const char* str, struct in_addr* addr)
201ead75a59SLuigi Rizzo {
202ead75a59SLuigi Rizzo struct hostent* hp;
203ead75a59SLuigi Rizzo
204ead75a59SLuigi Rizzo if (inet_aton (str, addr))
205ead75a59SLuigi Rizzo return;
206ead75a59SLuigi Rizzo
207ead75a59SLuigi Rizzo hp = gethostbyname (str);
208ead75a59SLuigi Rizzo if (!hp)
209ead75a59SLuigi Rizzo errx (1, "unknown host %s", str);
210ead75a59SLuigi Rizzo
211ead75a59SLuigi Rizzo memcpy (addr, hp->h_addr, sizeof (struct in_addr));
212ead75a59SLuigi Rizzo }
213ead75a59SLuigi Rizzo
214ead75a59SLuigi Rizzo static int
StrToPortRange(const char * str,const char * proto,port_range * portRange)215ead75a59SLuigi Rizzo StrToPortRange (const char* str, const char* proto, port_range *portRange)
216ead75a59SLuigi Rizzo {
217ead75a59SLuigi Rizzo char* sep;
218ead75a59SLuigi Rizzo struct servent* sp;
219ead75a59SLuigi Rizzo char* end;
220ead75a59SLuigi Rizzo u_short loPort;
221ead75a59SLuigi Rizzo u_short hiPort;
222ead75a59SLuigi Rizzo
223ead75a59SLuigi Rizzo /* First see if this is a service, return corresponding port if so. */
224ead75a59SLuigi Rizzo sp = getservbyname (str,proto);
225ead75a59SLuigi Rizzo if (sp) {
226ead75a59SLuigi Rizzo SETLOPORT(*portRange, ntohs(sp->s_port));
227ead75a59SLuigi Rizzo SETNUMPORTS(*portRange, 1);
228ead75a59SLuigi Rizzo return 0;
229ead75a59SLuigi Rizzo }
230ead75a59SLuigi Rizzo
231ead75a59SLuigi Rizzo /* Not a service, see if it's a single port or port range. */
232ead75a59SLuigi Rizzo sep = strchr (str, '-');
233ead75a59SLuigi Rizzo if (sep == NULL) {
234ead75a59SLuigi Rizzo SETLOPORT(*portRange, strtol(str, &end, 10));
235ead75a59SLuigi Rizzo if (end != str) {
236ead75a59SLuigi Rizzo /* Single port. */
237ead75a59SLuigi Rizzo SETNUMPORTS(*portRange, 1);
238ead75a59SLuigi Rizzo return 0;
239ead75a59SLuigi Rizzo }
240ead75a59SLuigi Rizzo
241ead75a59SLuigi Rizzo /* Error in port range field. */
242ead75a59SLuigi Rizzo errx (EX_DATAERR, "%s/%s: unknown service", str, proto);
243ead75a59SLuigi Rizzo }
244ead75a59SLuigi Rizzo
245ead75a59SLuigi Rizzo /* Port range, get the values and sanity check. */
246ead75a59SLuigi Rizzo sscanf (str, "%hu-%hu", &loPort, &hiPort);
247ead75a59SLuigi Rizzo SETLOPORT(*portRange, loPort);
248ead75a59SLuigi Rizzo SETNUMPORTS(*portRange, 0); /* Error by default */
249ead75a59SLuigi Rizzo if (loPort <= hiPort)
250ead75a59SLuigi Rizzo SETNUMPORTS(*portRange, hiPort - loPort + 1);
251ead75a59SLuigi Rizzo
252ead75a59SLuigi Rizzo if (GETNUMPORTS(*portRange) == 0)
253ead75a59SLuigi Rizzo errx (EX_DATAERR, "invalid port range %s", str);
254ead75a59SLuigi Rizzo
255ead75a59SLuigi Rizzo return 0;
256ead75a59SLuigi Rizzo }
257ead75a59SLuigi Rizzo
258ead75a59SLuigi Rizzo static int
StrToProto(const char * str)259ead75a59SLuigi Rizzo StrToProto (const char* str)
260ead75a59SLuigi Rizzo {
261ead75a59SLuigi Rizzo if (!strcmp (str, "tcp"))
262ead75a59SLuigi Rizzo return IPPROTO_TCP;
263ead75a59SLuigi Rizzo
264ead75a59SLuigi Rizzo if (!strcmp (str, "udp"))
265ead75a59SLuigi Rizzo return IPPROTO_UDP;
266ead75a59SLuigi Rizzo
26737ce2656SPaolo Pisati if (!strcmp (str, "sctp"))
26837ce2656SPaolo Pisati return IPPROTO_SCTP;
26937ce2656SPaolo Pisati errx (EX_DATAERR, "unknown protocol %s. Expected sctp, tcp or udp", str);
270ead75a59SLuigi Rizzo }
271ead75a59SLuigi Rizzo
272ead75a59SLuigi Rizzo static int
StrToAddrAndPortRange(const char * str,struct in_addr * addr,char * proto,port_range * portRange)273ead75a59SLuigi Rizzo StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto,
274ead75a59SLuigi Rizzo port_range *portRange)
275ead75a59SLuigi Rizzo {
276ead75a59SLuigi Rizzo char* ptr;
277ead75a59SLuigi Rizzo
278ead75a59SLuigi Rizzo ptr = strchr (str, ':');
279ead75a59SLuigi Rizzo if (!ptr)
280ead75a59SLuigi Rizzo errx (EX_DATAERR, "%s is missing port number", str);
281ead75a59SLuigi Rizzo
282ead75a59SLuigi Rizzo *ptr = '\0';
283ead75a59SLuigi Rizzo ++ptr;
284ead75a59SLuigi Rizzo
285ead75a59SLuigi Rizzo StrToAddr (str, addr);
286ead75a59SLuigi Rizzo return StrToPortRange (ptr, proto, portRange);
287ead75a59SLuigi Rizzo }
288ead75a59SLuigi Rizzo
289ead75a59SLuigi Rizzo /* End of stuff taken from natd.c. */
290ead75a59SLuigi Rizzo
291ead75a59SLuigi Rizzo /*
292ead75a59SLuigi Rizzo * The next 3 functions add support for the addr, port and proto redirect and
293ead75a59SLuigi Rizzo * their logic is loosely based on SetupAddressRedirect(), SetupPortRedirect()
294ead75a59SLuigi Rizzo * and SetupProtoRedirect() from natd.c.
295ead75a59SLuigi Rizzo *
296ead75a59SLuigi Rizzo * Every setup_* function fills at least one redirect entry
297d6164b77SAlexander V. Chernikov * (struct nat44_cfg_redir) and zero or more server pool entry
298d6164b77SAlexander V. Chernikov * (struct nat44_cfg_spool) in buf.
299ead75a59SLuigi Rizzo *
300ead75a59SLuigi Rizzo * The format of data in buf is:
301ead75a59SLuigi Rizzo *
302d6164b77SAlexander V. Chernikov * nat44_cfg_nat nat44_cfg_redir nat44_cfg_spool ...... nat44_cfg_spool
303ead75a59SLuigi Rizzo *
304ead75a59SLuigi Rizzo * ------------------------------------- ------------
305d6164b77SAlexander V. Chernikov * | | .....X ..... | | | | .....
306ead75a59SLuigi Rizzo * ------------------------------------- ...... ------------
307ead75a59SLuigi Rizzo * ^
308ead75a59SLuigi Rizzo * spool_cnt n=0 ...... n=(X-1)
309ead75a59SLuigi Rizzo *
310ead75a59SLuigi Rizzo * len points to the amount of available space in buf
311ead75a59SLuigi Rizzo * space counts the memory consumed by every function
312ead75a59SLuigi Rizzo *
313ead75a59SLuigi Rizzo * XXX - Every function get all the argv params so it
314ead75a59SLuigi Rizzo * has to check, in optional parameters, that the next
315ead75a59SLuigi Rizzo * args is a valid option for the redir entry and not
316ead75a59SLuigi Rizzo * another token. Only redir_port and redir_proto are
317ead75a59SLuigi Rizzo * affected by this.
318ead75a59SLuigi Rizzo */
319ead75a59SLuigi Rizzo
320ead75a59SLuigi Rizzo static int
estimate_redir_addr(int * ac,char *** av)321d5a80549SGleb Smirnoff estimate_redir_addr(int *ac, char ***av)
322ead75a59SLuigi Rizzo {
323d6164b77SAlexander V. Chernikov size_t space = sizeof(struct nat44_cfg_redir);
324ce972082SGleb Smirnoff char *sep = **av;
325ce972082SGleb Smirnoff u_int c = 0;
326ead75a59SLuigi Rizzo
3276a7bb02dSLuigi Rizzo (void)ac; /* UNUSED */
328ce972082SGleb Smirnoff while ((sep = strchr(sep, ',')) != NULL) {
329ce972082SGleb Smirnoff c++;
330ce972082SGleb Smirnoff sep++;
331ead75a59SLuigi Rizzo }
332d5a80549SGleb Smirnoff
333ce972082SGleb Smirnoff if (c > 0)
334ce972082SGleb Smirnoff c++;
335ce972082SGleb Smirnoff
336d6164b77SAlexander V. Chernikov space += c * sizeof(struct nat44_cfg_spool);
337ce972082SGleb Smirnoff
338ead75a59SLuigi Rizzo return (space);
339ead75a59SLuigi Rizzo }
340ead75a59SLuigi Rizzo
341ead75a59SLuigi Rizzo static int
setup_redir_addr(char * buf,int * ac,char *** av)342d5a80549SGleb Smirnoff setup_redir_addr(char *buf, int *ac, char ***av)
343ead75a59SLuigi Rizzo {
344d6164b77SAlexander V. Chernikov struct nat44_cfg_redir *r;
345d5a80549SGleb Smirnoff char *sep;
346d5a80549SGleb Smirnoff size_t space;
347d5a80549SGleb Smirnoff
348d6164b77SAlexander V. Chernikov r = (struct nat44_cfg_redir *)buf;
349d5a80549SGleb Smirnoff r->mode = REDIR_ADDR;
350d6164b77SAlexander V. Chernikov /* Skip nat44_cfg_redir at beginning of buf. */
351d6164b77SAlexander V. Chernikov buf = &buf[sizeof(struct nat44_cfg_redir)];
352d6164b77SAlexander V. Chernikov space = sizeof(struct nat44_cfg_redir);
353d5a80549SGleb Smirnoff
354d5a80549SGleb Smirnoff /* Extract local address. */
3558d913bf8SGleb Smirnoff if (strchr(**av, ',') != NULL) {
356d6164b77SAlexander V. Chernikov struct nat44_cfg_spool *spool;
357d5a80549SGleb Smirnoff
358d5a80549SGleb Smirnoff /* Setup LSNAT server pool. */
359d5a80549SGleb Smirnoff r->laddr.s_addr = INADDR_NONE;
3608d913bf8SGleb Smirnoff sep = strtok(**av, ",");
361d5a80549SGleb Smirnoff while (sep != NULL) {
362d6164b77SAlexander V. Chernikov spool = (struct nat44_cfg_spool *)buf;
363d6164b77SAlexander V. Chernikov space += sizeof(struct nat44_cfg_spool);
364d5a80549SGleb Smirnoff StrToAddr(sep, &spool->addr);
365d5a80549SGleb Smirnoff spool->port = ~0;
366d5a80549SGleb Smirnoff r->spool_cnt++;
367d6164b77SAlexander V. Chernikov /* Point to the next possible nat44_cfg_spool. */
368d6164b77SAlexander V. Chernikov buf = &buf[sizeof(struct nat44_cfg_spool)];
369d5a80549SGleb Smirnoff sep = strtok(NULL, ",");
370d5a80549SGleb Smirnoff }
371d5a80549SGleb Smirnoff } else
372d5a80549SGleb Smirnoff StrToAddr(**av, &r->laddr);
373d5a80549SGleb Smirnoff (*av)++; (*ac)--;
374d5a80549SGleb Smirnoff
375d5a80549SGleb Smirnoff /* Extract public address. */
376d5a80549SGleb Smirnoff StrToAddr(**av, &r->paddr);
377d5a80549SGleb Smirnoff (*av)++; (*ac)--;
378d5a80549SGleb Smirnoff
379d5a80549SGleb Smirnoff return (space);
380d5a80549SGleb Smirnoff }
381d5a80549SGleb Smirnoff
382d5a80549SGleb Smirnoff static int
estimate_redir_port(int * ac,char *** av)383d5a80549SGleb Smirnoff estimate_redir_port(int *ac, char ***av)
384d5a80549SGleb Smirnoff {
385d6164b77SAlexander V. Chernikov size_t space = sizeof(struct nat44_cfg_redir);
386ce972082SGleb Smirnoff char *sep = **av;
387ce972082SGleb Smirnoff u_int c = 0;
388d5a80549SGleb Smirnoff
3896a7bb02dSLuigi Rizzo (void)ac; /* UNUSED */
390ce972082SGleb Smirnoff while ((sep = strchr(sep, ',')) != NULL) {
391ce972082SGleb Smirnoff c++;
392ce972082SGleb Smirnoff sep++;
393d5a80549SGleb Smirnoff }
394d5a80549SGleb Smirnoff
395ce972082SGleb Smirnoff if (c > 0)
396ce972082SGleb Smirnoff c++;
397ce972082SGleb Smirnoff
398d6164b77SAlexander V. Chernikov space += c * sizeof(struct nat44_cfg_spool);
399ce972082SGleb Smirnoff
400d5a80549SGleb Smirnoff return (space);
401d5a80549SGleb Smirnoff }
402d5a80549SGleb Smirnoff
403d5a80549SGleb Smirnoff static int
setup_redir_port(char * buf,int * ac,char *** av)404d5a80549SGleb Smirnoff setup_redir_port(char *buf, int *ac, char ***av)
405d5a80549SGleb Smirnoff {
406d6164b77SAlexander V. Chernikov struct nat44_cfg_redir *r;
407d5a80549SGleb Smirnoff char *sep, *protoName, *lsnat = NULL;
408d5a80549SGleb Smirnoff size_t space;
409ead75a59SLuigi Rizzo u_short numLocalPorts;
410ead75a59SLuigi Rizzo port_range portRange;
411ead75a59SLuigi Rizzo
412ead75a59SLuigi Rizzo numLocalPorts = 0;
413ead75a59SLuigi Rizzo
414d6164b77SAlexander V. Chernikov r = (struct nat44_cfg_redir *)buf;
415ead75a59SLuigi Rizzo r->mode = REDIR_PORT;
416d6164b77SAlexander V. Chernikov /* Skip nat44_cfg_redir at beginning of buf. */
417d6164b77SAlexander V. Chernikov buf = &buf[sizeof(struct nat44_cfg_redir)];
418d6164b77SAlexander V. Chernikov space = sizeof(struct nat44_cfg_redir);
419d5a80549SGleb Smirnoff
420ead75a59SLuigi Rizzo /*
421ead75a59SLuigi Rizzo * Extract protocol.
422ead75a59SLuigi Rizzo */
423d5a80549SGleb Smirnoff r->proto = StrToProto(**av);
424d5a80549SGleb Smirnoff protoName = **av;
425d5a80549SGleb Smirnoff (*av)++; (*ac)--;
426ead75a59SLuigi Rizzo
427ead75a59SLuigi Rizzo /*
428ead75a59SLuigi Rizzo * Extract local address.
429ead75a59SLuigi Rizzo */
4303dedcef7SKevin Lo if (strchr(**av, ',') != NULL) {
431ead75a59SLuigi Rizzo r->laddr.s_addr = INADDR_NONE;
432ead75a59SLuigi Rizzo r->lport = ~0;
433ead75a59SLuigi Rizzo numLocalPorts = 1;
434d5a80549SGleb Smirnoff lsnat = **av;
435ead75a59SLuigi Rizzo } else {
43637ce2656SPaolo Pisati /*
43737ce2656SPaolo Pisati * The sctp nat does not allow the port numbers to be mapped to
43837ce2656SPaolo Pisati * new port numbers. Therefore, no ports are to be specified
43937ce2656SPaolo Pisati * in the target port field.
44037ce2656SPaolo Pisati */
44137ce2656SPaolo Pisati if (r->proto == IPPROTO_SCTP) {
442d5a80549SGleb Smirnoff if (strchr(**av, ':'))
44337ce2656SPaolo Pisati errx(EX_DATAERR, "redirect_port:"
444d5a80549SGleb Smirnoff "port numbers do not change in sctp, so do "
445d5a80549SGleb Smirnoff "not specify them as part of the target");
44637ce2656SPaolo Pisati else
447d5a80549SGleb Smirnoff StrToAddr(**av, &r->laddr);
44837ce2656SPaolo Pisati } else {
449d5a80549SGleb Smirnoff if (StrToAddrAndPortRange(**av, &r->laddr, protoName,
450ead75a59SLuigi Rizzo &portRange) != 0)
451ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: "
452ead75a59SLuigi Rizzo "invalid local port range");
453ead75a59SLuigi Rizzo
454ead75a59SLuigi Rizzo r->lport = GETLOPORT(portRange);
455ead75a59SLuigi Rizzo numLocalPorts = GETNUMPORTS(portRange);
456ead75a59SLuigi Rizzo }
45737ce2656SPaolo Pisati }
458d5a80549SGleb Smirnoff (*av)++; (*ac)--;
459ead75a59SLuigi Rizzo
460ead75a59SLuigi Rizzo /*
461ead75a59SLuigi Rizzo * Extract public port and optionally address.
462ead75a59SLuigi Rizzo */
4633dedcef7SKevin Lo if (strchr(**av, ':') != NULL) {
464d5a80549SGleb Smirnoff if (StrToAddrAndPortRange(**av, &r->paddr, protoName,
465ead75a59SLuigi Rizzo &portRange) != 0)
466ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: "
467ead75a59SLuigi Rizzo "invalid public port range");
468ead75a59SLuigi Rizzo } else {
469ead75a59SLuigi Rizzo r->paddr.s_addr = INADDR_ANY;
470d5a80549SGleb Smirnoff if (StrToPortRange(**av, protoName, &portRange) != 0)
471ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: "
472ead75a59SLuigi Rizzo "invalid public port range");
473ead75a59SLuigi Rizzo }
474ead75a59SLuigi Rizzo
475ead75a59SLuigi Rizzo r->pport = GETLOPORT(portRange);
47637ce2656SPaolo Pisati if (r->proto == IPPROTO_SCTP) { /* so the logic below still works */
47737ce2656SPaolo Pisati numLocalPorts = GETNUMPORTS(portRange);
47837ce2656SPaolo Pisati r->lport = r->pport;
47937ce2656SPaolo Pisati }
480ead75a59SLuigi Rizzo r->pport_cnt = GETNUMPORTS(portRange);
481d5a80549SGleb Smirnoff (*av)++; (*ac)--;
482ead75a59SLuigi Rizzo
483ead75a59SLuigi Rizzo /*
484ead75a59SLuigi Rizzo * Extract remote address and optionally port.
485ead75a59SLuigi Rizzo */
486ead75a59SLuigi Rizzo /*
487ce972082SGleb Smirnoff * NB: isdigit(**av) => we've to check that next parameter is really an
488ead75a59SLuigi Rizzo * option for this redirect entry, else stop here processing arg[cv].
489ead75a59SLuigi Rizzo */
490ce972082SGleb Smirnoff if (*ac != 0 && isdigit(***av)) {
4913dedcef7SKevin Lo if (strchr(**av, ':') != NULL) {
492d5a80549SGleb Smirnoff if (StrToAddrAndPortRange(**av, &r->raddr, protoName,
493ead75a59SLuigi Rizzo &portRange) != 0)
494ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: "
495ead75a59SLuigi Rizzo "invalid remote port range");
496ead75a59SLuigi Rizzo } else {
497ead75a59SLuigi Rizzo SETLOPORT(portRange, 0);
498ead75a59SLuigi Rizzo SETNUMPORTS(portRange, 1);
499d5a80549SGleb Smirnoff StrToAddr(**av, &r->raddr);
500ead75a59SLuigi Rizzo }
501d5a80549SGleb Smirnoff (*av)++; (*ac)--;
502ead75a59SLuigi Rizzo } else {
503ead75a59SLuigi Rizzo SETLOPORT(portRange, 0);
504ead75a59SLuigi Rizzo SETNUMPORTS(portRange, 1);
505ead75a59SLuigi Rizzo r->raddr.s_addr = INADDR_ANY;
506ead75a59SLuigi Rizzo }
507ead75a59SLuigi Rizzo r->rport = GETLOPORT(portRange);
508ead75a59SLuigi Rizzo r->rport_cnt = GETNUMPORTS(portRange);
509ead75a59SLuigi Rizzo
510ead75a59SLuigi Rizzo /*
511ead75a59SLuigi Rizzo * Make sure port ranges match up, then add the redirect ports.
512ead75a59SLuigi Rizzo */
513ead75a59SLuigi Rizzo if (numLocalPorts != r->pport_cnt)
514ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: "
515ead75a59SLuigi Rizzo "port ranges must be equal in size");
516ead75a59SLuigi Rizzo
517ead75a59SLuigi Rizzo /* Remote port range is allowed to be '0' which means all ports. */
518ead75a59SLuigi Rizzo if (r->rport_cnt != numLocalPorts &&
519ead75a59SLuigi Rizzo (r->rport_cnt != 1 || r->rport != 0))
520ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port: remote port must"
521ead75a59SLuigi Rizzo "be 0 or equal to local port range in size");
522ead75a59SLuigi Rizzo
523d5a80549SGleb Smirnoff /* Setup LSNAT server pool. */
524d5a80549SGleb Smirnoff if (lsnat != NULL) {
525d6164b77SAlexander V. Chernikov struct nat44_cfg_spool *spool;
526d5a80549SGleb Smirnoff
527d5a80549SGleb Smirnoff sep = strtok(lsnat, ",");
528ead75a59SLuigi Rizzo while (sep != NULL) {
529d6164b77SAlexander V. Chernikov spool = (struct nat44_cfg_spool *)buf;
530d6164b77SAlexander V. Chernikov space += sizeof(struct nat44_cfg_spool);
53137ce2656SPaolo Pisati /*
532d5a80549SGleb Smirnoff * The sctp nat does not allow the port numbers to
533d5a80549SGleb Smirnoff * be mapped to new port numbers. Therefore, no ports
534d5a80549SGleb Smirnoff * are to be specified in the target port field.
53537ce2656SPaolo Pisati */
53637ce2656SPaolo Pisati if (r->proto == IPPROTO_SCTP) {
53737ce2656SPaolo Pisati if (strchr (sep, ':')) {
53837ce2656SPaolo Pisati errx(EX_DATAERR, "redirect_port:"
53937ce2656SPaolo Pisati "port numbers do not change in "
54037ce2656SPaolo Pisati "sctp, so do not specify them as "
54137ce2656SPaolo Pisati "part of the target");
54237ce2656SPaolo Pisati } else {
543d5a80549SGleb Smirnoff StrToAddr(sep, &spool->addr);
544d5a80549SGleb Smirnoff spool->port = r->pport;
54537ce2656SPaolo Pisati }
54637ce2656SPaolo Pisati } else {
547d5a80549SGleb Smirnoff if (StrToAddrAndPortRange(sep, &spool->addr,
54837ce2656SPaolo Pisati protoName, &portRange) != 0)
549ead75a59SLuigi Rizzo errx(EX_DATAERR, "redirect_port:"
550ead75a59SLuigi Rizzo "invalid local port range");
551ead75a59SLuigi Rizzo if (GETNUMPORTS(portRange) != 1)
55237ce2656SPaolo Pisati errx(EX_DATAERR, "redirect_port: "
55337ce2656SPaolo Pisati "local port must be single in "
55437ce2656SPaolo Pisati "this context");
555d5a80549SGleb Smirnoff spool->port = GETLOPORT(portRange);
55637ce2656SPaolo Pisati }
557ead75a59SLuigi Rizzo r->spool_cnt++;
558d6164b77SAlexander V. Chernikov /* Point to the next possible nat44_cfg_spool. */
559d6164b77SAlexander V. Chernikov buf = &buf[sizeof(struct nat44_cfg_spool)];
560ead75a59SLuigi Rizzo sep = strtok(NULL, ",");
561ead75a59SLuigi Rizzo }
562ead75a59SLuigi Rizzo }
563d5a80549SGleb Smirnoff
564ead75a59SLuigi Rizzo return (space);
565ead75a59SLuigi Rizzo }
566ead75a59SLuigi Rizzo
567ead75a59SLuigi Rizzo static int
setup_redir_proto(char * buf,int * ac,char *** av)568d5a80549SGleb Smirnoff setup_redir_proto(char *buf, int *ac, char ***av)
569ead75a59SLuigi Rizzo {
570d6164b77SAlexander V. Chernikov struct nat44_cfg_redir *r;
571d5a80549SGleb Smirnoff struct protoent *protoent;
572d5a80549SGleb Smirnoff size_t space;
573ead75a59SLuigi Rizzo
574d6164b77SAlexander V. Chernikov r = (struct nat44_cfg_redir *)buf;
575ead75a59SLuigi Rizzo r->mode = REDIR_PROTO;
576d6164b77SAlexander V. Chernikov /* Skip nat44_cfg_redir at beginning of buf. */
577d6164b77SAlexander V. Chernikov buf = &buf[sizeof(struct nat44_cfg_redir)];
578d6164b77SAlexander V. Chernikov space = sizeof(struct nat44_cfg_redir);
579d5a80549SGleb Smirnoff
580ead75a59SLuigi Rizzo /*
581ead75a59SLuigi Rizzo * Extract protocol.
582ead75a59SLuigi Rizzo */
583d5a80549SGleb Smirnoff protoent = getprotobyname(**av);
584ead75a59SLuigi Rizzo if (protoent == NULL)
585d5a80549SGleb Smirnoff errx(EX_DATAERR, "redirect_proto: unknown protocol %s", **av);
586ead75a59SLuigi Rizzo else
587ead75a59SLuigi Rizzo r->proto = protoent->p_proto;
588ead75a59SLuigi Rizzo
589d5a80549SGleb Smirnoff (*av)++; (*ac)--;
590ead75a59SLuigi Rizzo
591ead75a59SLuigi Rizzo /*
592ead75a59SLuigi Rizzo * Extract local address.
593ead75a59SLuigi Rizzo */
594d5a80549SGleb Smirnoff StrToAddr(**av, &r->laddr);
595ead75a59SLuigi Rizzo
596d5a80549SGleb Smirnoff (*av)++; (*ac)--;
597ead75a59SLuigi Rizzo
598ead75a59SLuigi Rizzo /*
599ead75a59SLuigi Rizzo * Extract optional public address.
600ead75a59SLuigi Rizzo */
601d5a80549SGleb Smirnoff if (*ac == 0) {
602ead75a59SLuigi Rizzo r->paddr.s_addr = INADDR_ANY;
603ead75a59SLuigi Rizzo r->raddr.s_addr = INADDR_ANY;
604ead75a59SLuigi Rizzo } else {
605ead75a59SLuigi Rizzo /* see above in setup_redir_port() */
606ce972082SGleb Smirnoff if (isdigit(***av)) {
607d5a80549SGleb Smirnoff StrToAddr(**av, &r->paddr);
608d5a80549SGleb Smirnoff (*av)++; (*ac)--;
609ead75a59SLuigi Rizzo
610ead75a59SLuigi Rizzo /*
611ead75a59SLuigi Rizzo * Extract optional remote address.
612ead75a59SLuigi Rizzo */
613ead75a59SLuigi Rizzo /* see above in setup_redir_port() */
614ce972082SGleb Smirnoff if (*ac != 0 && isdigit(***av)) {
615d5a80549SGleb Smirnoff StrToAddr(**av, &r->raddr);
616d5a80549SGleb Smirnoff (*av)++; (*ac)--;
617ead75a59SLuigi Rizzo }
618ead75a59SLuigi Rizzo }
619ead75a59SLuigi Rizzo }
620d5a80549SGleb Smirnoff
621ead75a59SLuigi Rizzo return (space);
622ead75a59SLuigi Rizzo }
623ead75a59SLuigi Rizzo
624ead75a59SLuigi Rizzo static void
nat_show_log(struct nat44_cfg_nat * n,void * arg __unused)62556707beeSMark Johnston nat_show_log(struct nat44_cfg_nat *n, void *arg __unused)
626ead75a59SLuigi Rizzo {
627d6164b77SAlexander V. Chernikov char *buf;
628d6164b77SAlexander V. Chernikov
629d6164b77SAlexander V. Chernikov buf = (char *)(n + 1);
630d6164b77SAlexander V. Chernikov if (buf[0] != '\0')
631d6164b77SAlexander V. Chernikov printf("nat %s: %s\n", n->name, buf);
632d6164b77SAlexander V. Chernikov }
633d6164b77SAlexander V. Chernikov
634d6164b77SAlexander V. Chernikov static void
nat_show_cfg(struct nat44_cfg_nat * n,void * arg __unused)63556707beeSMark Johnston nat_show_cfg(struct nat44_cfg_nat *n, void *arg __unused)
636d6164b77SAlexander V. Chernikov {
637d6164b77SAlexander V. Chernikov struct nat44_cfg_redir *t;
638d6164b77SAlexander V. Chernikov struct nat44_cfg_spool *s;
639d6164b77SAlexander V. Chernikov caddr_t buf;
640ead75a59SLuigi Rizzo struct protoent *p;
64156707beeSMark Johnston uint32_t cnt;
64256707beeSMark Johnston int i, off;
643ead75a59SLuigi Rizzo
644d6164b77SAlexander V. Chernikov buf = (caddr_t)n;
645ead75a59SLuigi Rizzo off = sizeof(*n);
646d6164b77SAlexander V. Chernikov printf("ipfw nat %s config", n->name);
647ead75a59SLuigi Rizzo if (strlen(n->if_name) != 0)
648ead75a59SLuigi Rizzo printf(" if %s", n->if_name);
649ead75a59SLuigi Rizzo else if (n->ip.s_addr != 0)
650ead75a59SLuigi Rizzo printf(" ip %s", inet_ntoa(n->ip));
651ead75a59SLuigi Rizzo while (n->mode != 0) {
652ead75a59SLuigi Rizzo if (n->mode & PKT_ALIAS_LOG) {
653ead75a59SLuigi Rizzo printf(" log");
654ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_LOG;
655ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_DENY_INCOMING) {
656ead75a59SLuigi Rizzo printf(" deny_in");
657ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_DENY_INCOMING;
658ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_SAME_PORTS) {
659ead75a59SLuigi Rizzo printf(" same_ports");
660ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_SAME_PORTS;
6611875bbfeSAndrey V. Elsukov } else if (n->mode & PKT_ALIAS_SKIP_GLOBAL) {
6621875bbfeSAndrey V. Elsukov printf(" skip_global");
6631875bbfeSAndrey V. Elsukov n->mode &= ~PKT_ALIAS_SKIP_GLOBAL;
664ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_UNREGISTERED_ONLY) {
665ead75a59SLuigi Rizzo printf(" unreg_only");
666ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_UNREGISTERED_ONLY;
66775b89337SAlexander V. Chernikov } else if (n->mode & PKT_ALIAS_UNREGISTERED_CGN) {
66875b89337SAlexander V. Chernikov printf(" unreg_cgn");
66975b89337SAlexander V. Chernikov n->mode &= ~PKT_ALIAS_UNREGISTERED_CGN;
670ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_RESET_ON_ADDR_CHANGE) {
671ead75a59SLuigi Rizzo printf(" reset");
672ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE;
673ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_REVERSE) {
674ead75a59SLuigi Rizzo printf(" reverse");
675ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_REVERSE;
676ead75a59SLuigi Rizzo } else if (n->mode & PKT_ALIAS_PROXY_ONLY) {
677ead75a59SLuigi Rizzo printf(" proxy_only");
678ead75a59SLuigi Rizzo n->mode &= ~PKT_ALIAS_PROXY_ONLY;
679ead75a59SLuigi Rizzo }
680ead75a59SLuigi Rizzo }
681ead75a59SLuigi Rizzo /* Print all the redirect's data configuration. */
682ead75a59SLuigi Rizzo for (cnt = 0; cnt < n->redir_cnt; cnt++) {
683d6164b77SAlexander V. Chernikov t = (struct nat44_cfg_redir *)&buf[off];
684d6164b77SAlexander V. Chernikov off += sizeof(struct nat44_cfg_redir);
685ead75a59SLuigi Rizzo switch (t->mode) {
686ead75a59SLuigi Rizzo case REDIR_ADDR:
687ead75a59SLuigi Rizzo printf(" redirect_addr");
688ead75a59SLuigi Rizzo if (t->spool_cnt == 0)
689ead75a59SLuigi Rizzo printf(" %s", inet_ntoa(t->laddr));
690ead75a59SLuigi Rizzo else
691ead75a59SLuigi Rizzo for (i = 0; i < t->spool_cnt; i++) {
692d6164b77SAlexander V. Chernikov s = (struct nat44_cfg_spool *)&buf[off];
693ead75a59SLuigi Rizzo if (i)
694ead75a59SLuigi Rizzo printf(",");
695ead75a59SLuigi Rizzo else
696ead75a59SLuigi Rizzo printf(" ");
697ead75a59SLuigi Rizzo printf("%s", inet_ntoa(s->addr));
698d6164b77SAlexander V. Chernikov off += sizeof(struct nat44_cfg_spool);
699ead75a59SLuigi Rizzo }
700ead75a59SLuigi Rizzo printf(" %s", inet_ntoa(t->paddr));
701ead75a59SLuigi Rizzo break;
702ead75a59SLuigi Rizzo case REDIR_PORT:
703ead75a59SLuigi Rizzo p = getprotobynumber(t->proto);
704ead75a59SLuigi Rizzo printf(" redirect_port %s ", p->p_name);
705ead75a59SLuigi Rizzo if (!t->spool_cnt) {
706ead75a59SLuigi Rizzo printf("%s:%u", inet_ntoa(t->laddr), t->lport);
707ead75a59SLuigi Rizzo if (t->pport_cnt > 1)
708ead75a59SLuigi Rizzo printf("-%u", t->lport +
709ead75a59SLuigi Rizzo t->pport_cnt - 1);
710ead75a59SLuigi Rizzo } else
711ead75a59SLuigi Rizzo for (i=0; i < t->spool_cnt; i++) {
712d6164b77SAlexander V. Chernikov s = (struct nat44_cfg_spool *)&buf[off];
713ead75a59SLuigi Rizzo if (i)
714ead75a59SLuigi Rizzo printf(",");
715ead75a59SLuigi Rizzo printf("%s:%u", inet_ntoa(s->addr),
716ead75a59SLuigi Rizzo s->port);
717d6164b77SAlexander V. Chernikov off += sizeof(struct nat44_cfg_spool);
718ead75a59SLuigi Rizzo }
719ead75a59SLuigi Rizzo
720ead75a59SLuigi Rizzo printf(" ");
721ead75a59SLuigi Rizzo if (t->paddr.s_addr)
722ead75a59SLuigi Rizzo printf("%s:", inet_ntoa(t->paddr));
723ead75a59SLuigi Rizzo printf("%u", t->pport);
724ead75a59SLuigi Rizzo if (!t->spool_cnt && t->pport_cnt > 1)
725ead75a59SLuigi Rizzo printf("-%u", t->pport + t->pport_cnt - 1);
726ead75a59SLuigi Rizzo
727ead75a59SLuigi Rizzo if (t->raddr.s_addr) {
728ead75a59SLuigi Rizzo printf(" %s", inet_ntoa(t->raddr));
729ead75a59SLuigi Rizzo if (t->rport) {
730ead75a59SLuigi Rizzo printf(":%u", t->rport);
731ead75a59SLuigi Rizzo if (!t->spool_cnt && t->rport_cnt > 1)
732ead75a59SLuigi Rizzo printf("-%u", t->rport +
733ead75a59SLuigi Rizzo t->rport_cnt - 1);
734ead75a59SLuigi Rizzo }
735ead75a59SLuigi Rizzo }
736ead75a59SLuigi Rizzo break;
737ead75a59SLuigi Rizzo case REDIR_PROTO:
738ead75a59SLuigi Rizzo p = getprotobynumber(t->proto);
739ead75a59SLuigi Rizzo printf(" redirect_proto %s %s", p->p_name,
740ead75a59SLuigi Rizzo inet_ntoa(t->laddr));
741ead75a59SLuigi Rizzo if (t->paddr.s_addr != 0) {
742ead75a59SLuigi Rizzo printf(" %s", inet_ntoa(t->paddr));
743ead75a59SLuigi Rizzo if (t->raddr.s_addr)
744ead75a59SLuigi Rizzo printf(" %s", inet_ntoa(t->raddr));
745ead75a59SLuigi Rizzo }
746ead75a59SLuigi Rizzo break;
747ead75a59SLuigi Rizzo default:
748ead75a59SLuigi Rizzo errx(EX_DATAERR, "unknown redir mode");
749ead75a59SLuigi Rizzo break;
750ead75a59SLuigi Rizzo }
751ead75a59SLuigi Rizzo }
752ead75a59SLuigi Rizzo printf("\n");
753ead75a59SLuigi Rizzo }
754ead75a59SLuigi Rizzo
755a08cdb6cSNeel Chauhan static int
nat_port_alias_parse(char * str,u_short * lpout,u_short * hpout)756a08cdb6cSNeel Chauhan nat_port_alias_parse(char *str, u_short *lpout, u_short *hpout) {
757a08cdb6cSNeel Chauhan long lp, hp;
758a08cdb6cSNeel Chauhan char *ptr;
759a08cdb6cSNeel Chauhan /* Lower port parsing */
760a08cdb6cSNeel Chauhan lp = (long) strtol(str, &ptr, 10);
761a08cdb6cSNeel Chauhan if (lp < 1024 || lp > 65535)
762a08cdb6cSNeel Chauhan return 0;
763a08cdb6cSNeel Chauhan if (!ptr || *ptr != '-')
764a08cdb6cSNeel Chauhan return 0;
765a08cdb6cSNeel Chauhan /* Upper port parsing */
766a08cdb6cSNeel Chauhan hp = (long) strtol(ptr, &ptr, 10);
767a08cdb6cSNeel Chauhan if (hp < 1024 || hp > 65535)
768a08cdb6cSNeel Chauhan return 0;
769a08cdb6cSNeel Chauhan if (ptr)
770a08cdb6cSNeel Chauhan return 0;
771a08cdb6cSNeel Chauhan
772a08cdb6cSNeel Chauhan *lpout = (u_short) lp;
773a08cdb6cSNeel Chauhan *hpout = (u_short) hp;
774a08cdb6cSNeel Chauhan return 1;
775a08cdb6cSNeel Chauhan }
776a08cdb6cSNeel Chauhan
777ead75a59SLuigi Rizzo void
ipfw_config_nat(int ac,char ** av)778ead75a59SLuigi Rizzo ipfw_config_nat(int ac, char **av)
779ead75a59SLuigi Rizzo {
780d6164b77SAlexander V. Chernikov ipfw_obj_header *oh;
781d6164b77SAlexander V. Chernikov struct nat44_cfg_nat *n; /* Nat instance configuration. */
782d5a80549SGleb Smirnoff int i, off, tok, ac1;
783a08cdb6cSNeel Chauhan u_short lp, hp;
784980cccebSAndrey V. Elsukov char *id, *buf, **av1, *end;
785d5a80549SGleb Smirnoff size_t len;
786ead75a59SLuigi Rizzo
7876ca60bebSGleb Smirnoff av++;
7886ca60bebSGleb Smirnoff ac--;
789ead75a59SLuigi Rizzo /* Nat id. */
790980cccebSAndrey V. Elsukov if (ac == 0)
791ead75a59SLuigi Rizzo errx(EX_DATAERR, "missing nat id");
792980cccebSAndrey V. Elsukov id = *av;
793980cccebSAndrey V. Elsukov i = (int)strtol(id, &end, 0);
794980cccebSAndrey V. Elsukov if (i <= 0 || *end != '\0')
795980cccebSAndrey V. Elsukov errx(EX_DATAERR, "illegal nat id: %s", id);
7966ca60bebSGleb Smirnoff av++;
7976ca60bebSGleb Smirnoff ac--;
798ead75a59SLuigi Rizzo if (ac == 0)
799ead75a59SLuigi Rizzo errx(EX_DATAERR, "missing option");
800ead75a59SLuigi Rizzo
801d6164b77SAlexander V. Chernikov len = sizeof(*oh) + sizeof(*n);
802d5a80549SGleb Smirnoff ac1 = ac;
803d5a80549SGleb Smirnoff av1 = av;
804d5a80549SGleb Smirnoff while (ac1 > 0) {
805d5a80549SGleb Smirnoff tok = match_token(nat_params, *av1);
8066ca60bebSGleb Smirnoff ac1--;
8076ca60bebSGleb Smirnoff av1++;
808d5a80549SGleb Smirnoff switch (tok) {
809d5a80549SGleb Smirnoff case TOK_IP:
810d5a80549SGleb Smirnoff case TOK_IF:
811a08cdb6cSNeel Chauhan case TOK_PORT_ALIAS:
8126ca60bebSGleb Smirnoff ac1--;
8136ca60bebSGleb Smirnoff av1++;
814d5a80549SGleb Smirnoff break;
815d5a80549SGleb Smirnoff case TOK_ALOG:
816d5a80549SGleb Smirnoff case TOK_DENY_INC:
817d5a80549SGleb Smirnoff case TOK_SAME_PORTS:
8181875bbfeSAndrey V. Elsukov case TOK_SKIP_GLOBAL:
819d5a80549SGleb Smirnoff case TOK_UNREG_ONLY:
82004777538SEugene Grosbein case TOK_UNREG_CGN:
821d5a80549SGleb Smirnoff case TOK_RESET_ADDR:
822d5a80549SGleb Smirnoff case TOK_ALIAS_REV:
823d5a80549SGleb Smirnoff case TOK_PROXY_ONLY:
824d5a80549SGleb Smirnoff break;
825d5a80549SGleb Smirnoff case TOK_REDIR_ADDR:
826d5a80549SGleb Smirnoff if (ac1 < 2)
827d5a80549SGleb Smirnoff errx(EX_DATAERR, "redirect_addr: "
828d5a80549SGleb Smirnoff "not enough arguments");
829d5a80549SGleb Smirnoff len += estimate_redir_addr(&ac1, &av1);
8306ca60bebSGleb Smirnoff av1 += 2;
8316ca60bebSGleb Smirnoff ac1 -= 2;
832d5a80549SGleb Smirnoff break;
833d5a80549SGleb Smirnoff case TOK_REDIR_PORT:
834d5a80549SGleb Smirnoff if (ac1 < 3)
835d5a80549SGleb Smirnoff errx(EX_DATAERR, "redirect_port: "
836d5a80549SGleb Smirnoff "not enough arguments");
8376ca60bebSGleb Smirnoff av1++;
8386ca60bebSGleb Smirnoff ac1--;
839d5a80549SGleb Smirnoff len += estimate_redir_port(&ac1, &av1);
8406ca60bebSGleb Smirnoff av1 += 2;
8416ca60bebSGleb Smirnoff ac1 -= 2;
842ce972082SGleb Smirnoff /* Skip optional remoteIP/port */
8430103912dSGleb Smirnoff if (ac1 != 0 && isdigit(**av1)) {
8446ca60bebSGleb Smirnoff av1++;
8456ca60bebSGleb Smirnoff ac1--;
8460103912dSGleb Smirnoff }
847d5a80549SGleb Smirnoff break;
848d5a80549SGleb Smirnoff case TOK_REDIR_PROTO:
849d5a80549SGleb Smirnoff if (ac1 < 2)
850d5a80549SGleb Smirnoff errx(EX_DATAERR, "redirect_proto: "
851d5a80549SGleb Smirnoff "not enough arguments");
852d6164b77SAlexander V. Chernikov len += sizeof(struct nat44_cfg_redir);
8536ca60bebSGleb Smirnoff av1 += 2;
8546ca60bebSGleb Smirnoff ac1 -= 2;
855ce972082SGleb Smirnoff /* Skip optional remoteIP/port */
8560103912dSGleb Smirnoff if (ac1 != 0 && isdigit(**av1)) {
8576ca60bebSGleb Smirnoff av1++;
8586ca60bebSGleb Smirnoff ac1--;
8590103912dSGleb Smirnoff }
8600103912dSGleb Smirnoff if (ac1 != 0 && isdigit(**av1)) {
8616ca60bebSGleb Smirnoff av1++;
8626ca60bebSGleb Smirnoff ac1--;
8630103912dSGleb Smirnoff }
864d5a80549SGleb Smirnoff break;
865d5a80549SGleb Smirnoff default:
866d5a80549SGleb Smirnoff errx(EX_DATAERR, "unrecognised option ``%s''", av1[-1]);
867d5a80549SGleb Smirnoff }
868d5a80549SGleb Smirnoff }
869d5a80549SGleb Smirnoff
870d5a80549SGleb Smirnoff if ((buf = malloc(len)) == NULL)
871d5a80549SGleb Smirnoff errx(EX_OSERR, "malloc failed");
872d5a80549SGleb Smirnoff
873d6164b77SAlexander V. Chernikov /* Offset in buf: save space for header at the beginning. */
874d6164b77SAlexander V. Chernikov off = sizeof(*oh) + sizeof(*n);
875d5a80549SGleb Smirnoff memset(buf, 0, len);
876d6164b77SAlexander V. Chernikov oh = (ipfw_obj_header *)buf;
877d6164b77SAlexander V. Chernikov n = (struct nat44_cfg_nat *)(oh + 1);
878d6164b77SAlexander V. Chernikov oh->ntlv.head.length = sizeof(oh->ntlv);
879d6164b77SAlexander V. Chernikov snprintf(oh->ntlv.name, sizeof(oh->ntlv.name), "%d", i);
880d6164b77SAlexander V. Chernikov snprintf(n->name, sizeof(n->name), "%d", i);
881d5a80549SGleb Smirnoff
882ead75a59SLuigi Rizzo while (ac > 0) {
883ead75a59SLuigi Rizzo tok = match_token(nat_params, *av);
8846ca60bebSGleb Smirnoff ac--;
8856ca60bebSGleb Smirnoff av++;
886ead75a59SLuigi Rizzo switch (tok) {
887ead75a59SLuigi Rizzo case TOK_IP:
888ead75a59SLuigi Rizzo if (ac == 0)
889ead75a59SLuigi Rizzo errx(EX_DATAERR, "missing option");
890ead75a59SLuigi Rizzo if (!inet_aton(av[0], &(n->ip)))
891ead75a59SLuigi Rizzo errx(EX_DATAERR, "bad ip address ``%s''",
892ead75a59SLuigi Rizzo av[0]);
8936ca60bebSGleb Smirnoff ac--;
8946ca60bebSGleb Smirnoff av++;
895ead75a59SLuigi Rizzo break;
896ead75a59SLuigi Rizzo case TOK_IF:
897ead75a59SLuigi Rizzo if (ac == 0)
898ead75a59SLuigi Rizzo errx(EX_DATAERR, "missing option");
899ead75a59SLuigi Rizzo set_addr_dynamic(av[0], n);
9006ca60bebSGleb Smirnoff ac--;
9016ca60bebSGleb Smirnoff av++;
902ead75a59SLuigi Rizzo break;
903ead75a59SLuigi Rizzo case TOK_ALOG:
904ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_LOG;
905ead75a59SLuigi Rizzo break;
906ead75a59SLuigi Rizzo case TOK_DENY_INC:
907ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_DENY_INCOMING;
908ead75a59SLuigi Rizzo break;
909ead75a59SLuigi Rizzo case TOK_SAME_PORTS:
910ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_SAME_PORTS;
911ead75a59SLuigi Rizzo break;
912ead75a59SLuigi Rizzo case TOK_UNREG_ONLY:
913ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_UNREGISTERED_ONLY;
914ead75a59SLuigi Rizzo break;
91504777538SEugene Grosbein case TOK_UNREG_CGN:
91604777538SEugene Grosbein n->mode |= PKT_ALIAS_UNREGISTERED_CGN;
91704777538SEugene Grosbein break;
9181875bbfeSAndrey V. Elsukov case TOK_SKIP_GLOBAL:
9191875bbfeSAndrey V. Elsukov n->mode |= PKT_ALIAS_SKIP_GLOBAL;
9201875bbfeSAndrey V. Elsukov break;
921ead75a59SLuigi Rizzo case TOK_RESET_ADDR:
922ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
923ead75a59SLuigi Rizzo break;
924ead75a59SLuigi Rizzo case TOK_ALIAS_REV:
925ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_REVERSE;
926ead75a59SLuigi Rizzo break;
927ead75a59SLuigi Rizzo case TOK_PROXY_ONLY:
928ead75a59SLuigi Rizzo n->mode |= PKT_ALIAS_PROXY_ONLY;
929ead75a59SLuigi Rizzo break;
930ead75a59SLuigi Rizzo /*
9315221106cSGleb Smirnoff * All the setup_redir_* functions work directly in
9325221106cSGleb Smirnoff * the final buffer, see above for details.
933ead75a59SLuigi Rizzo */
934ead75a59SLuigi Rizzo case TOK_REDIR_ADDR:
935ead75a59SLuigi Rizzo case TOK_REDIR_PORT:
936ead75a59SLuigi Rizzo case TOK_REDIR_PROTO:
937ead75a59SLuigi Rizzo switch (tok) {
938ead75a59SLuigi Rizzo case TOK_REDIR_ADDR:
939d5a80549SGleb Smirnoff i = setup_redir_addr(&buf[off], &ac, &av);
940ead75a59SLuigi Rizzo break;
941ead75a59SLuigi Rizzo case TOK_REDIR_PORT:
942d5a80549SGleb Smirnoff i = setup_redir_port(&buf[off], &ac, &av);
943ead75a59SLuigi Rizzo break;
944ead75a59SLuigi Rizzo case TOK_REDIR_PROTO:
945d5a80549SGleb Smirnoff i = setup_redir_proto(&buf[off], &ac, &av);
946ead75a59SLuigi Rizzo break;
947ead75a59SLuigi Rizzo }
948ead75a59SLuigi Rizzo n->redir_cnt++;
949ead75a59SLuigi Rizzo off += i;
950ead75a59SLuigi Rizzo break;
951a08cdb6cSNeel Chauhan case TOK_PORT_ALIAS:
952a08cdb6cSNeel Chauhan if (ac == 0)
953a08cdb6cSNeel Chauhan errx(EX_DATAERR, "missing option");
954a08cdb6cSNeel Chauhan if (!nat_port_alias_parse(av[0], &lp, &hp))
955a08cdb6cSNeel Chauhan errx(EX_DATAERR,
956a08cdb6cSNeel Chauhan "You need a range of port(s) from 1024 <= x < 65536");
957a08cdb6cSNeel Chauhan if (lp >= hp)
958a08cdb6cSNeel Chauhan errx(EX_DATAERR,
959a08cdb6cSNeel Chauhan "Upper port has to be greater than lower port");
960a08cdb6cSNeel Chauhan n->alias_port_lo = lp;
961a08cdb6cSNeel Chauhan n->alias_port_hi = hp;
962a08cdb6cSNeel Chauhan ac--;
963a08cdb6cSNeel Chauhan av++;
964a08cdb6cSNeel Chauhan break;
965ead75a59SLuigi Rizzo }
966ead75a59SLuigi Rizzo }
967a08cdb6cSNeel Chauhan if (n->mode & PKT_ALIAS_SAME_PORTS && n->alias_port_lo)
968a08cdb6cSNeel Chauhan errx(EX_DATAERR, "same_ports and port_range cannot both be selected");
969ead75a59SLuigi Rizzo
970d6164b77SAlexander V. Chernikov i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len);
971d6164b77SAlexander V. Chernikov if (i != 0)
972d6164b77SAlexander V. Chernikov err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG");
973ead75a59SLuigi Rizzo
97456707beeSMark Johnston if (!g_co.do_quiet) {
975ead75a59SLuigi Rizzo /* After every modification, we show the resultant rule. */
976ead75a59SLuigi Rizzo int _ac = 3;
977d7a32e24SLuigi Rizzo const char *_av[] = {"show", "config", id};
978d7a32e24SLuigi Rizzo ipfw_show_nat(_ac, (char **)(void *)_av);
979ead75a59SLuigi Rizzo }
980ead75a59SLuigi Rizzo }
981ead75a59SLuigi Rizzo
982db1102f2SAndrey V. Elsukov static void
nat_fill_ntlv(ipfw_obj_ntlv * ntlv,int i)983db1102f2SAndrey V. Elsukov nat_fill_ntlv(ipfw_obj_ntlv *ntlv, int i)
984db1102f2SAndrey V. Elsukov {
985db1102f2SAndrey V. Elsukov
986db1102f2SAndrey V. Elsukov ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
987db1102f2SAndrey V. Elsukov ntlv->head.length = sizeof(ipfw_obj_ntlv);
988db1102f2SAndrey V. Elsukov ntlv->idx = 1;
989db1102f2SAndrey V. Elsukov ntlv->set = 0; /* not yet */
990db1102f2SAndrey V. Elsukov snprintf(ntlv->name, sizeof(ntlv->name), "%d", i);
991db1102f2SAndrey V. Elsukov }
992db1102f2SAndrey V. Elsukov
993db1102f2SAndrey V. Elsukov int
ipfw_delete_nat(int i)994db1102f2SAndrey V. Elsukov ipfw_delete_nat(int i)
995db1102f2SAndrey V. Elsukov {
996db1102f2SAndrey V. Elsukov ipfw_obj_header oh;
997db1102f2SAndrey V. Elsukov int ret;
998db1102f2SAndrey V. Elsukov
999db1102f2SAndrey V. Elsukov memset(&oh, 0, sizeof(oh));
1000db1102f2SAndrey V. Elsukov nat_fill_ntlv(&oh.ntlv, i);
1001db1102f2SAndrey V. Elsukov ret = do_set3(IP_FW_NAT44_DESTROY, &oh.opheader, sizeof(oh));
1002db1102f2SAndrey V. Elsukov if (ret == -1) {
100356707beeSMark Johnston if (!g_co.do_quiet)
1004db1102f2SAndrey V. Elsukov warn("nat %u not available", i);
1005db1102f2SAndrey V. Elsukov return (EX_UNAVAILABLE);
1006db1102f2SAndrey V. Elsukov }
1007db1102f2SAndrey V. Elsukov return (EX_OK);
1008db1102f2SAndrey V. Elsukov }
1009db1102f2SAndrey V. Elsukov
1010d6164b77SAlexander V. Chernikov struct nat_list_arg {
1011d6164b77SAlexander V. Chernikov uint16_t cmd;
1012d6164b77SAlexander V. Chernikov int is_all;
1013d6164b77SAlexander V. Chernikov };
1014d6164b77SAlexander V. Chernikov
1015d6164b77SAlexander V. Chernikov static int
nat_show_data(struct nat44_cfg_nat * cfg,void * arg)1016d6164b77SAlexander V. Chernikov nat_show_data(struct nat44_cfg_nat *cfg, void *arg)
1017d6164b77SAlexander V. Chernikov {
1018d6164b77SAlexander V. Chernikov struct nat_list_arg *nla;
1019d6164b77SAlexander V. Chernikov ipfw_obj_header *oh;
1020d6164b77SAlexander V. Chernikov
1021d6164b77SAlexander V. Chernikov nla = (struct nat_list_arg *)arg;
1022d6164b77SAlexander V. Chernikov
1023d6164b77SAlexander V. Chernikov switch (nla->cmd) {
1024d6164b77SAlexander V. Chernikov case IP_FW_NAT44_XGETCONFIG:
1025d6164b77SAlexander V. Chernikov if (nat_get_cmd(cfg->name, nla->cmd, &oh) != 0) {
1026d6164b77SAlexander V. Chernikov warnx("Error getting nat instance %s info", cfg->name);
1027d6164b77SAlexander V. Chernikov break;
1028d6164b77SAlexander V. Chernikov }
1029d6164b77SAlexander V. Chernikov nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
1030d6164b77SAlexander V. Chernikov free(oh);
1031d6164b77SAlexander V. Chernikov break;
1032d6164b77SAlexander V. Chernikov case IP_FW_NAT44_XGETLOG:
1033d6164b77SAlexander V. Chernikov if (nat_get_cmd(cfg->name, nla->cmd, &oh) == 0) {
1034d6164b77SAlexander V. Chernikov nat_show_log((struct nat44_cfg_nat *)(oh + 1), NULL);
1035d6164b77SAlexander V. Chernikov free(oh);
1036d6164b77SAlexander V. Chernikov break;
1037d6164b77SAlexander V. Chernikov }
1038d6164b77SAlexander V. Chernikov /* Handle error */
1039d6164b77SAlexander V. Chernikov if (nla->is_all != 0 && errno == ENOENT)
1040d6164b77SAlexander V. Chernikov break;
1041d6164b77SAlexander V. Chernikov warn("Error getting nat instance %s info", cfg->name);
1042d6164b77SAlexander V. Chernikov break;
1043d6164b77SAlexander V. Chernikov }
1044d6164b77SAlexander V. Chernikov
1045d6164b77SAlexander V. Chernikov return (0);
1046d6164b77SAlexander V. Chernikov }
1047d6164b77SAlexander V. Chernikov
1048d6164b77SAlexander V. Chernikov /*
1049d6164b77SAlexander V. Chernikov * Compare nat names.
1050d6164b77SAlexander V. Chernikov * Honor number comparison.
1051d6164b77SAlexander V. Chernikov */
1052d6164b77SAlexander V. Chernikov static int
natname_cmp(const void * a,const void * b)1053d6164b77SAlexander V. Chernikov natname_cmp(const void *a, const void *b)
1054d6164b77SAlexander V. Chernikov {
105556707beeSMark Johnston const struct nat44_cfg_nat *ia, *ib;
1056d6164b77SAlexander V. Chernikov
105756707beeSMark Johnston ia = (const struct nat44_cfg_nat *)a;
105856707beeSMark Johnston ib = (const struct nat44_cfg_nat *)b;
1059d6164b77SAlexander V. Chernikov
1060d6164b77SAlexander V. Chernikov return (stringnum_cmp(ia->name, ib->name));
1061d6164b77SAlexander V. Chernikov }
1062d6164b77SAlexander V. Chernikov
1063d6164b77SAlexander V. Chernikov /*
1064d6164b77SAlexander V. Chernikov * Retrieves nat list from kernel,
1065d6164b77SAlexander V. Chernikov * optionally sorts it and calls requested function for each table.
1066d6164b77SAlexander V. Chernikov * Returns 0 on success.
1067d6164b77SAlexander V. Chernikov */
1068d6164b77SAlexander V. Chernikov static int
nat_foreach(nat_cb_t * f,void * arg,int sort)1069d6164b77SAlexander V. Chernikov nat_foreach(nat_cb_t *f, void *arg, int sort)
1070d6164b77SAlexander V. Chernikov {
1071d6164b77SAlexander V. Chernikov ipfw_obj_lheader *olh;
1072d6164b77SAlexander V. Chernikov struct nat44_cfg_nat *cfg;
1073d6164b77SAlexander V. Chernikov size_t sz;
107456707beeSMark Johnston uint32_t i;
1075d6164b77SAlexander V. Chernikov
1076d6164b77SAlexander V. Chernikov /* Start with reasonable default */
1077d6164b77SAlexander V. Chernikov sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat);
1078d6164b77SAlexander V. Chernikov
1079d6164b77SAlexander V. Chernikov for (;;) {
1080d6164b77SAlexander V. Chernikov if ((olh = calloc(1, sz)) == NULL)
1081d6164b77SAlexander V. Chernikov return (ENOMEM);
1082d6164b77SAlexander V. Chernikov
1083d6164b77SAlexander V. Chernikov olh->size = sz;
1084d6164b77SAlexander V. Chernikov if (do_get3(IP_FW_NAT44_LIST_NAT, &olh->opheader, &sz) != 0) {
1085d6164b77SAlexander V. Chernikov sz = olh->size;
10860577d6e0SAlexander V. Chernikov free(olh);
10870577d6e0SAlexander V. Chernikov if (errno == ENOMEM)
1088d6164b77SAlexander V. Chernikov continue;
1089d6164b77SAlexander V. Chernikov return (errno);
1090d6164b77SAlexander V. Chernikov }
1091d6164b77SAlexander V. Chernikov
1092d6164b77SAlexander V. Chernikov if (sort != 0)
1093d6164b77SAlexander V. Chernikov qsort(olh + 1, olh->count, olh->objsize, natname_cmp);
1094d6164b77SAlexander V. Chernikov
1095d6164b77SAlexander V. Chernikov cfg = (struct nat44_cfg_nat*)(olh + 1);
1096d6164b77SAlexander V. Chernikov for (i = 0; i < olh->count; i++) {
1097*f5e73306SJohn Baldwin (void)f(cfg, arg); /* Ignore errors for now */
1098d6164b77SAlexander V. Chernikov cfg = (struct nat44_cfg_nat *)((caddr_t)cfg +
1099d6164b77SAlexander V. Chernikov olh->objsize);
1100d6164b77SAlexander V. Chernikov }
1101d6164b77SAlexander V. Chernikov
1102d6164b77SAlexander V. Chernikov free(olh);
1103d6164b77SAlexander V. Chernikov break;
1104d6164b77SAlexander V. Chernikov }
1105d6164b77SAlexander V. Chernikov
1106d6164b77SAlexander V. Chernikov return (0);
1107d6164b77SAlexander V. Chernikov }
1108d6164b77SAlexander V. Chernikov
1109d6164b77SAlexander V. Chernikov static int
nat_get_cmd(char * name,uint16_t cmd,ipfw_obj_header ** ooh)1110d6164b77SAlexander V. Chernikov nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh)
1111d6164b77SAlexander V. Chernikov {
1112d6164b77SAlexander V. Chernikov ipfw_obj_header *oh;
1113d6164b77SAlexander V. Chernikov struct nat44_cfg_nat *cfg;
1114d6164b77SAlexander V. Chernikov size_t sz;
1115d6164b77SAlexander V. Chernikov
1116d6164b77SAlexander V. Chernikov /* Start with reasonable default */
1117d6164b77SAlexander V. Chernikov sz = sizeof(*oh) + sizeof(*cfg) + 128;
1118d6164b77SAlexander V. Chernikov
1119d6164b77SAlexander V. Chernikov for (;;) {
1120d6164b77SAlexander V. Chernikov if ((oh = calloc(1, sz)) == NULL)
1121d6164b77SAlexander V. Chernikov return (ENOMEM);
1122d6164b77SAlexander V. Chernikov cfg = (struct nat44_cfg_nat *)(oh + 1);
1123d6164b77SAlexander V. Chernikov oh->ntlv.head.length = sizeof(oh->ntlv);
1124d6164b77SAlexander V. Chernikov strlcpy(oh->ntlv.name, name, sizeof(oh->ntlv.name));
1125d6164b77SAlexander V. Chernikov strlcpy(cfg->name, name, sizeof(cfg->name));
1126d6164b77SAlexander V. Chernikov
1127d6164b77SAlexander V. Chernikov if (do_get3(cmd, &oh->opheader, &sz) != 0) {
1128d6164b77SAlexander V. Chernikov sz = cfg->size;
1129d6164b77SAlexander V. Chernikov free(oh);
1130d6164b77SAlexander V. Chernikov if (errno == ENOMEM)
1131d6164b77SAlexander V. Chernikov continue;
1132d6164b77SAlexander V. Chernikov return (errno);
1133d6164b77SAlexander V. Chernikov }
1134d6164b77SAlexander V. Chernikov
1135d6164b77SAlexander V. Chernikov *ooh = oh;
1136d6164b77SAlexander V. Chernikov break;
1137d6164b77SAlexander V. Chernikov }
1138d6164b77SAlexander V. Chernikov
1139d6164b77SAlexander V. Chernikov return (0);
1140d6164b77SAlexander V. Chernikov }
1141ead75a59SLuigi Rizzo
1142ead75a59SLuigi Rizzo void
ipfw_show_nat(int ac,char ** av)1143ead75a59SLuigi Rizzo ipfw_show_nat(int ac, char **av)
1144ead75a59SLuigi Rizzo {
1145d6164b77SAlexander V. Chernikov ipfw_obj_header *oh;
1146d6164b77SAlexander V. Chernikov char *name;
1147d6164b77SAlexander V. Chernikov int cmd;
1148d6164b77SAlexander V. Chernikov struct nat_list_arg nla;
1149ead75a59SLuigi Rizzo
11506ca60bebSGleb Smirnoff ac--;
11516ca60bebSGleb Smirnoff av++;
1152ead75a59SLuigi Rizzo
115356707beeSMark Johnston if (g_co.test_only)
1154ead75a59SLuigi Rizzo return;
1155ead75a59SLuigi Rizzo
1156ead75a59SLuigi Rizzo /* Parse parameters. */
1157d6164b77SAlexander V. Chernikov cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */
1158d6164b77SAlexander V. Chernikov name = NULL;
1159d6164b77SAlexander V. Chernikov for ( ; ac != 0; ac--, av++) {
1160ead75a59SLuigi Rizzo if (!strncmp(av[0], "config", strlen(av[0]))) {
1161d6164b77SAlexander V. Chernikov cmd = IP_FW_NAT44_XGETCONFIG;
1162ead75a59SLuigi Rizzo continue;
1163ead75a59SLuigi Rizzo }
1164d6164b77SAlexander V. Chernikov if (strcmp(av[0], "log") == 0) {
1165d6164b77SAlexander V. Chernikov cmd = IP_FW_NAT44_XGETLOG;
1166d6164b77SAlexander V. Chernikov continue;
1167d6164b77SAlexander V. Chernikov }
1168d6164b77SAlexander V. Chernikov if (name != NULL)
1169d6164b77SAlexander V. Chernikov err(EX_USAGE,"only one instance name may be specified");
1170d6164b77SAlexander V. Chernikov name = av[0];
1171ead75a59SLuigi Rizzo }
1172ead75a59SLuigi Rizzo
1173d6164b77SAlexander V. Chernikov if (cmd == 0)
1174d6164b77SAlexander V. Chernikov errx(EX_USAGE, "Please specify action. Available: config,log");
1175d6164b77SAlexander V. Chernikov
1176d6164b77SAlexander V. Chernikov if (name == NULL) {
1177d6164b77SAlexander V. Chernikov memset(&nla, 0, sizeof(nla));
1178d6164b77SAlexander V. Chernikov nla.cmd = cmd;
1179d6164b77SAlexander V. Chernikov nla.is_all = 1;
1180d6164b77SAlexander V. Chernikov nat_foreach(nat_show_data, &nla, 1);
1181ead75a59SLuigi Rizzo } else {
1182d6164b77SAlexander V. Chernikov if (nat_get_cmd(name, cmd, &oh) != 0)
1183d6164b77SAlexander V. Chernikov err(EX_OSERR, "Error getting nat %s instance info", name);
1184d6164b77SAlexander V. Chernikov nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
1185d6164b77SAlexander V. Chernikov free(oh);
1186ead75a59SLuigi Rizzo }
1187ead75a59SLuigi Rizzo }
1188d6164b77SAlexander V. Chernikov
1189