122ce4affSfengbojiang /*-
2127dd473Swhl739 * Copyright (c) 2002-2003 Luigi Rizzo
3127dd473Swhl739 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4127dd473Swhl739 * Copyright (c) 1994 Ugen J.S.Antsilevich
5127dd473Swhl739 *
6127dd473Swhl739 * Idea and grammar partially left from:
7127dd473Swhl739 * Copyright (c) 1993 Daniel Boulet
8127dd473Swhl739 *
9127dd473Swhl739 * Redistribution and use in source forms, with and without modification,
10127dd473Swhl739 * are permitted provided that this entire comment appears intact.
11127dd473Swhl739 *
12127dd473Swhl739 * Redistribution in binary form may occur without any restrictions.
13127dd473Swhl739 * Obviously, it would be nice if you gave credit where credit is due
14127dd473Swhl739 * but requiring it would be too onerous.
15127dd473Swhl739 *
16127dd473Swhl739 * This software is provided ``AS IS'' without any warranties of any kind.
17127dd473Swhl739 *
18127dd473Swhl739 * NEW command line interface for IP firewall facility
19127dd473Swhl739 *
20127dd473Swhl739 * $FreeBSD$
21127dd473Swhl739 *
22127dd473Swhl739 * In-kernel nat support
23127dd473Swhl739 */
24127dd473Swhl739
25127dd473Swhl739 #include <sys/types.h>
26127dd473Swhl739 #include <sys/socket.h>
27127dd473Swhl739 #include <sys/sysctl.h>
28127dd473Swhl739
29127dd473Swhl739 #include "ipfw2.h"
30127dd473Swhl739
31127dd473Swhl739 #include <ctype.h>
32127dd473Swhl739 #include <err.h>
33127dd473Swhl739 #include <errno.h>
34127dd473Swhl739 #include <netdb.h>
35127dd473Swhl739 #include <stdio.h>
36127dd473Swhl739 #include <stdlib.h>
37127dd473Swhl739 #include <string.h>
38127dd473Swhl739 #include <sysexits.h>
39127dd473Swhl739
40127dd473Swhl739 #include <net/if.h>
41127dd473Swhl739 #include <net/if_dl.h>
42127dd473Swhl739 #include <net/route.h> /* def. of struct route */
43127dd473Swhl739 #include <netinet/in.h>
44127dd473Swhl739 #include <netinet/ip_fw.h>
45127dd473Swhl739 #include <arpa/inet.h>
46127dd473Swhl739 #include <alias.h>
47127dd473Swhl739
48*d4a07e70Sfengbojiang #ifdef FSTACK
49*d4a07e70Sfengbojiang #ifndef __unused
50*d4a07e70Sfengbojiang #define __unused __attribute__((__unused__))
51*d4a07e70Sfengbojiang #endif
52*d4a07e70Sfengbojiang #endif
53*d4a07e70Sfengbojiang
54127dd473Swhl739 typedef int (nat_cb_t)(struct nat44_cfg_nat *cfg, void *arg);
55127dd473Swhl739 static void nat_show_cfg(struct nat44_cfg_nat *n, void *arg);
56127dd473Swhl739 static void nat_show_log(struct nat44_cfg_nat *n, void *arg);
57127dd473Swhl739 static int nat_show_data(struct nat44_cfg_nat *cfg, void *arg);
58127dd473Swhl739 static int natname_cmp(const void *a, const void *b);
59127dd473Swhl739 static int nat_foreach(nat_cb_t *f, void *arg, int sort);
60127dd473Swhl739 static int nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh);
61127dd473Swhl739
62127dd473Swhl739 static struct _s_x nat_params[] = {
63127dd473Swhl739 { "ip", TOK_IP },
64127dd473Swhl739 { "if", TOK_IF },
65127dd473Swhl739 { "log", TOK_ALOG },
66127dd473Swhl739 { "deny_in", TOK_DENY_INC },
67127dd473Swhl739 { "same_ports", TOK_SAME_PORTS },
68127dd473Swhl739 { "unreg_only", TOK_UNREG_ONLY },
6922ce4affSfengbojiang { "unreg_cgn", TOK_UNREG_CGN },
70127dd473Swhl739 { "skip_global", TOK_SKIP_GLOBAL },
71127dd473Swhl739 { "reset", TOK_RESET_ADDR },
72127dd473Swhl739 { "reverse", TOK_ALIAS_REV },
73127dd473Swhl739 { "proxy_only", TOK_PROXY_ONLY },
74127dd473Swhl739 { "redirect_addr", TOK_REDIR_ADDR },
75127dd473Swhl739 { "redirect_port", TOK_REDIR_PORT },
76127dd473Swhl739 { "redirect_proto", TOK_REDIR_PROTO },
77127dd473Swhl739 { NULL, 0 } /* terminator */
78127dd473Swhl739 };
79127dd473Swhl739
80127dd473Swhl739
81127dd473Swhl739 /*
82127dd473Swhl739 * Search for interface with name "ifn", and fill n accordingly:
83127dd473Swhl739 *
84127dd473Swhl739 * n->ip ip address of interface "ifn"
85127dd473Swhl739 * n->if_name copy of interface name "ifn"
86127dd473Swhl739 */
87127dd473Swhl739 static void
set_addr_dynamic(const char * ifn,struct nat44_cfg_nat * n)88127dd473Swhl739 set_addr_dynamic(const char *ifn, struct nat44_cfg_nat *n)
89127dd473Swhl739 {
90127dd473Swhl739 size_t needed;
91127dd473Swhl739 int mib[6];
92127dd473Swhl739 char *buf, *lim, *next;
93127dd473Swhl739 struct if_msghdr *ifm;
94127dd473Swhl739 struct ifa_msghdr *ifam;
95127dd473Swhl739 struct sockaddr_dl *sdl;
96127dd473Swhl739 struct sockaddr_in *sin;
97127dd473Swhl739 int ifIndex;
98127dd473Swhl739
99127dd473Swhl739 mib[0] = CTL_NET;
100127dd473Swhl739 mib[1] = PF_ROUTE;
101127dd473Swhl739 mib[2] = 0;
102127dd473Swhl739 mib[3] = AF_INET;
103127dd473Swhl739 mib[4] = NET_RT_IFLIST;
104127dd473Swhl739 mib[5] = 0;
105127dd473Swhl739 /*
106127dd473Swhl739 * Get interface data.
107127dd473Swhl739 */
108127dd473Swhl739 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
109127dd473Swhl739 err(1, "iflist-sysctl-estimate");
110127dd473Swhl739 buf = safe_calloc(1, needed);
111127dd473Swhl739 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
112127dd473Swhl739 err(1, "iflist-sysctl-get");
113127dd473Swhl739 lim = buf + needed;
114127dd473Swhl739 /*
115127dd473Swhl739 * Loop through interfaces until one with
116127dd473Swhl739 * given name is found. This is done to
117127dd473Swhl739 * find correct interface index for routing
118127dd473Swhl739 * message processing.
119127dd473Swhl739 */
120127dd473Swhl739 ifIndex = 0;
121127dd473Swhl739 next = buf;
122127dd473Swhl739 while (next < lim) {
123127dd473Swhl739 ifm = (struct if_msghdr *)next;
124127dd473Swhl739 next += ifm->ifm_msglen;
125127dd473Swhl739 if (ifm->ifm_version != RTM_VERSION) {
12622ce4affSfengbojiang if (g_co.verbose)
127127dd473Swhl739 warnx("routing message version %d "
128127dd473Swhl739 "not understood", ifm->ifm_version);
129127dd473Swhl739 continue;
130127dd473Swhl739 }
131127dd473Swhl739 if (ifm->ifm_type == RTM_IFINFO) {
132127dd473Swhl739 sdl = (struct sockaddr_dl *)(ifm + 1);
133127dd473Swhl739 if (strlen(ifn) == sdl->sdl_nlen &&
134127dd473Swhl739 strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) {
135127dd473Swhl739 ifIndex = ifm->ifm_index;
136127dd473Swhl739 break;
137127dd473Swhl739 }
138127dd473Swhl739 }
139127dd473Swhl739 }
140127dd473Swhl739 if (!ifIndex)
141127dd473Swhl739 errx(1, "unknown interface name %s", ifn);
142127dd473Swhl739 /*
143127dd473Swhl739 * Get interface address.
144127dd473Swhl739 */
145127dd473Swhl739 sin = NULL;
146127dd473Swhl739 while (next < lim) {
147127dd473Swhl739 ifam = (struct ifa_msghdr *)next;
148127dd473Swhl739 next += ifam->ifam_msglen;
149127dd473Swhl739 if (ifam->ifam_version != RTM_VERSION) {
15022ce4affSfengbojiang if (g_co.verbose)
151127dd473Swhl739 warnx("routing message version %d "
152127dd473Swhl739 "not understood", ifam->ifam_version);
153127dd473Swhl739 continue;
154127dd473Swhl739 }
155127dd473Swhl739 if (ifam->ifam_type != RTM_NEWADDR)
156127dd473Swhl739 break;
157127dd473Swhl739 if (ifam->ifam_addrs & RTA_IFA) {
158127dd473Swhl739 int i;
159127dd473Swhl739 char *cp = (char *)(ifam + 1);
160127dd473Swhl739
161127dd473Swhl739 for (i = 1; i < RTA_IFA; i <<= 1) {
162127dd473Swhl739 if (ifam->ifam_addrs & i)
163127dd473Swhl739 cp += SA_SIZE((struct sockaddr *)cp);
164127dd473Swhl739 }
165127dd473Swhl739 if (((struct sockaddr *)cp)->sa_family == AF_INET) {
166127dd473Swhl739 sin = (struct sockaddr_in *)cp;
167127dd473Swhl739 break;
168127dd473Swhl739 }
169127dd473Swhl739 }
170127dd473Swhl739 }
171127dd473Swhl739 if (sin == NULL)
172127dd473Swhl739 n->ip.s_addr = htonl(INADDR_ANY);
173127dd473Swhl739 else
174127dd473Swhl739 n->ip = sin->sin_addr;
175127dd473Swhl739 strncpy(n->if_name, ifn, IF_NAMESIZE);
176127dd473Swhl739
177127dd473Swhl739 free(buf);
178127dd473Swhl739 }
179127dd473Swhl739
180127dd473Swhl739 /*
181127dd473Swhl739 * XXX - The following functions, macros and definitions come from natd.c:
182127dd473Swhl739 * it would be better to move them outside natd.c, in a file
183127dd473Swhl739 * (redirect_support.[ch]?) shared by ipfw and natd, but for now i can live
184127dd473Swhl739 * with it.
185127dd473Swhl739 */
186127dd473Swhl739
187127dd473Swhl739 /*
188127dd473Swhl739 * Definition of a port range, and macros to deal with values.
189127dd473Swhl739 * FORMAT: HI 16-bits == first port in range, 0 == all ports.
190127dd473Swhl739 * LO 16-bits == number of ports in range
191127dd473Swhl739 * NOTES: - Port values are not stored in network byte order.
192127dd473Swhl739 */
193127dd473Swhl739
194127dd473Swhl739 #define port_range u_long
195127dd473Swhl739
196127dd473Swhl739 #define GETLOPORT(x) ((x) >> 0x10)
197127dd473Swhl739 #define GETNUMPORTS(x) ((x) & 0x0000ffff)
198127dd473Swhl739 #define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x)))
199127dd473Swhl739
200127dd473Swhl739 /* Set y to be the low-port value in port_range variable x. */
201127dd473Swhl739 #define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10))
202127dd473Swhl739
203127dd473Swhl739 /* Set y to be the number of ports in port_range variable x. */
204127dd473Swhl739 #define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y))
205127dd473Swhl739
206127dd473Swhl739 static void
StrToAddr(const char * str,struct in_addr * addr)207127dd473Swhl739 StrToAddr (const char* str, struct in_addr* addr)
208127dd473Swhl739 {
209127dd473Swhl739 struct hostent* hp;
210127dd473Swhl739
211127dd473Swhl739 if (inet_aton (str, addr))
212127dd473Swhl739 return;
213*d4a07e70Sfengbojiang #ifdef FSTACK
214*d4a07e70Sfengbojiang else
215*d4a07e70Sfengbojiang errx (1, "invalid addr %d", addr->s_addr);
216*d4a07e70Sfengbojiang #else
217127dd473Swhl739
218127dd473Swhl739 hp = gethostbyname (str);
219127dd473Swhl739 if (!hp)
220127dd473Swhl739 errx (1, "unknown host %s", str);
221127dd473Swhl739
222127dd473Swhl739 memcpy (addr, hp->h_addr, sizeof (struct in_addr));
223*d4a07e70Sfengbojiang #endif
224127dd473Swhl739 }
225127dd473Swhl739
226127dd473Swhl739 static int
StrToPortRange(const char * str,const char * proto,port_range * portRange)227127dd473Swhl739 StrToPortRange (const char* str, const char* proto, port_range *portRange)
228127dd473Swhl739 {
229127dd473Swhl739 char* sep;
230127dd473Swhl739 struct servent* sp;
231127dd473Swhl739 char* end;
232127dd473Swhl739 u_short loPort;
233127dd473Swhl739 u_short hiPort;
234127dd473Swhl739
235127dd473Swhl739 /* First see if this is a service, return corresponding port if so. */
236127dd473Swhl739 sp = getservbyname (str,proto);
237127dd473Swhl739 if (sp) {
238127dd473Swhl739 SETLOPORT(*portRange, ntohs(sp->s_port));
239127dd473Swhl739 SETNUMPORTS(*portRange, 1);
240127dd473Swhl739 return 0;
241127dd473Swhl739 }
242127dd473Swhl739
243127dd473Swhl739 /* Not a service, see if it's a single port or port range. */
244127dd473Swhl739 sep = strchr (str, '-');
245127dd473Swhl739 if (sep == NULL) {
246127dd473Swhl739 SETLOPORT(*portRange, strtol(str, &end, 10));
247127dd473Swhl739 if (end != str) {
248127dd473Swhl739 /* Single port. */
249127dd473Swhl739 SETNUMPORTS(*portRange, 1);
250127dd473Swhl739 return 0;
251127dd473Swhl739 }
252127dd473Swhl739
253127dd473Swhl739 /* Error in port range field. */
254127dd473Swhl739 errx (EX_DATAERR, "%s/%s: unknown service", str, proto);
255127dd473Swhl739 }
256127dd473Swhl739
257127dd473Swhl739 /* Port range, get the values and sanity check. */
258127dd473Swhl739 sscanf (str, "%hu-%hu", &loPort, &hiPort);
259127dd473Swhl739 SETLOPORT(*portRange, loPort);
260127dd473Swhl739 SETNUMPORTS(*portRange, 0); /* Error by default */
261127dd473Swhl739 if (loPort <= hiPort)
262127dd473Swhl739 SETNUMPORTS(*portRange, hiPort - loPort + 1);
263127dd473Swhl739
264127dd473Swhl739 if (GETNUMPORTS(*portRange) == 0)
265127dd473Swhl739 errx (EX_DATAERR, "invalid port range %s", str);
266127dd473Swhl739
267127dd473Swhl739 return 0;
268127dd473Swhl739 }
269127dd473Swhl739
270127dd473Swhl739 static int
StrToProto(const char * str)271127dd473Swhl739 StrToProto (const char* str)
272127dd473Swhl739 {
273127dd473Swhl739 if (!strcmp (str, "tcp"))
274127dd473Swhl739 return IPPROTO_TCP;
275127dd473Swhl739
276127dd473Swhl739 if (!strcmp (str, "udp"))
277127dd473Swhl739 return IPPROTO_UDP;
278127dd473Swhl739
279127dd473Swhl739 if (!strcmp (str, "sctp"))
280127dd473Swhl739 return IPPROTO_SCTP;
281127dd473Swhl739 errx (EX_DATAERR, "unknown protocol %s. Expected sctp, tcp or udp", str);
282127dd473Swhl739 }
283127dd473Swhl739
284127dd473Swhl739 static int
StrToAddrAndPortRange(const char * str,struct in_addr * addr,char * proto,port_range * portRange)285127dd473Swhl739 StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto,
286127dd473Swhl739 port_range *portRange)
287127dd473Swhl739 {
288127dd473Swhl739 char* ptr;
289127dd473Swhl739
290127dd473Swhl739 ptr = strchr (str, ':');
291127dd473Swhl739 if (!ptr)
292127dd473Swhl739 errx (EX_DATAERR, "%s is missing port number", str);
293127dd473Swhl739
294127dd473Swhl739 *ptr = '\0';
295127dd473Swhl739 ++ptr;
296127dd473Swhl739
297127dd473Swhl739 StrToAddr (str, addr);
298127dd473Swhl739 return StrToPortRange (ptr, proto, portRange);
299127dd473Swhl739 }
300127dd473Swhl739
301127dd473Swhl739 /* End of stuff taken from natd.c. */
302127dd473Swhl739
303127dd473Swhl739 /*
304127dd473Swhl739 * The next 3 functions add support for the addr, port and proto redirect and
305127dd473Swhl739 * their logic is loosely based on SetupAddressRedirect(), SetupPortRedirect()
306127dd473Swhl739 * and SetupProtoRedirect() from natd.c.
307127dd473Swhl739 *
308127dd473Swhl739 * Every setup_* function fills at least one redirect entry
309127dd473Swhl739 * (struct nat44_cfg_redir) and zero or more server pool entry
310127dd473Swhl739 * (struct nat44_cfg_spool) in buf.
311127dd473Swhl739 *
312127dd473Swhl739 * The format of data in buf is:
313127dd473Swhl739 *
314127dd473Swhl739 * nat44_cfg_nat nat44_cfg_redir nat44_cfg_spool ...... nat44_cfg_spool
315127dd473Swhl739 *
316127dd473Swhl739 * ------------------------------------- ------------
317127dd473Swhl739 * | | .....X ..... | | | | .....
318127dd473Swhl739 * ------------------------------------- ...... ------------
319127dd473Swhl739 * ^
320127dd473Swhl739 * spool_cnt n=0 ...... n=(X-1)
321127dd473Swhl739 *
322127dd473Swhl739 * len points to the amount of available space in buf
323127dd473Swhl739 * space counts the memory consumed by every function
324127dd473Swhl739 *
325127dd473Swhl739 * XXX - Every function get all the argv params so it
326127dd473Swhl739 * has to check, in optional parameters, that the next
327127dd473Swhl739 * args is a valid option for the redir entry and not
328127dd473Swhl739 * another token. Only redir_port and redir_proto are
329127dd473Swhl739 * affected by this.
330127dd473Swhl739 */
331127dd473Swhl739
332127dd473Swhl739 static int
estimate_redir_addr(int * ac,char *** av)333127dd473Swhl739 estimate_redir_addr(int *ac, char ***av)
334127dd473Swhl739 {
335127dd473Swhl739 size_t space = sizeof(struct nat44_cfg_redir);
336127dd473Swhl739 char *sep = **av;
337127dd473Swhl739 u_int c = 0;
338127dd473Swhl739
339127dd473Swhl739 (void)ac; /* UNUSED */
340127dd473Swhl739 while ((sep = strchr(sep, ',')) != NULL) {
341127dd473Swhl739 c++;
342127dd473Swhl739 sep++;
343127dd473Swhl739 }
344127dd473Swhl739
345127dd473Swhl739 if (c > 0)
346127dd473Swhl739 c++;
347127dd473Swhl739
348127dd473Swhl739 space += c * sizeof(struct nat44_cfg_spool);
349127dd473Swhl739
350127dd473Swhl739 return (space);
351127dd473Swhl739 }
352127dd473Swhl739
353127dd473Swhl739 static int
setup_redir_addr(char * buf,int * ac,char *** av)354127dd473Swhl739 setup_redir_addr(char *buf, int *ac, char ***av)
355127dd473Swhl739 {
356127dd473Swhl739 struct nat44_cfg_redir *r;
357127dd473Swhl739 char *sep;
358127dd473Swhl739 size_t space;
359127dd473Swhl739
360127dd473Swhl739 r = (struct nat44_cfg_redir *)buf;
361127dd473Swhl739 r->mode = REDIR_ADDR;
362127dd473Swhl739 /* Skip nat44_cfg_redir at beginning of buf. */
363127dd473Swhl739 buf = &buf[sizeof(struct nat44_cfg_redir)];
364127dd473Swhl739 space = sizeof(struct nat44_cfg_redir);
365127dd473Swhl739
366127dd473Swhl739 /* Extract local address. */
367127dd473Swhl739 if (strchr(**av, ',') != NULL) {
368127dd473Swhl739 struct nat44_cfg_spool *spool;
369127dd473Swhl739
370127dd473Swhl739 /* Setup LSNAT server pool. */
371127dd473Swhl739 r->laddr.s_addr = INADDR_NONE;
372127dd473Swhl739 sep = strtok(**av, ",");
373127dd473Swhl739 while (sep != NULL) {
374127dd473Swhl739 spool = (struct nat44_cfg_spool *)buf;
375127dd473Swhl739 space += sizeof(struct nat44_cfg_spool);
376127dd473Swhl739 StrToAddr(sep, &spool->addr);
377127dd473Swhl739 spool->port = ~0;
378127dd473Swhl739 r->spool_cnt++;
379127dd473Swhl739 /* Point to the next possible nat44_cfg_spool. */
380127dd473Swhl739 buf = &buf[sizeof(struct nat44_cfg_spool)];
381127dd473Swhl739 sep = strtok(NULL, ",");
382127dd473Swhl739 }
383127dd473Swhl739 } else
384127dd473Swhl739 StrToAddr(**av, &r->laddr);
385127dd473Swhl739 (*av)++; (*ac)--;
386127dd473Swhl739
387127dd473Swhl739 /* Extract public address. */
388127dd473Swhl739 StrToAddr(**av, &r->paddr);
389127dd473Swhl739 (*av)++; (*ac)--;
390127dd473Swhl739
391127dd473Swhl739 return (space);
392127dd473Swhl739 }
393127dd473Swhl739
394127dd473Swhl739 static int
estimate_redir_port(int * ac,char *** av)395127dd473Swhl739 estimate_redir_port(int *ac, char ***av)
396127dd473Swhl739 {
397127dd473Swhl739 size_t space = sizeof(struct nat44_cfg_redir);
398127dd473Swhl739 char *sep = **av;
399127dd473Swhl739 u_int c = 0;
400127dd473Swhl739
401127dd473Swhl739 (void)ac; /* UNUSED */
402127dd473Swhl739 while ((sep = strchr(sep, ',')) != NULL) {
403127dd473Swhl739 c++;
404127dd473Swhl739 sep++;
405127dd473Swhl739 }
406127dd473Swhl739
407127dd473Swhl739 if (c > 0)
408127dd473Swhl739 c++;
409127dd473Swhl739
410127dd473Swhl739 space += c * sizeof(struct nat44_cfg_spool);
411127dd473Swhl739
412127dd473Swhl739 return (space);
413127dd473Swhl739 }
414127dd473Swhl739
415127dd473Swhl739 static int
setup_redir_port(char * buf,int * ac,char *** av)416127dd473Swhl739 setup_redir_port(char *buf, int *ac, char ***av)
417127dd473Swhl739 {
418127dd473Swhl739 struct nat44_cfg_redir *r;
419127dd473Swhl739 char *sep, *protoName, *lsnat = NULL;
420127dd473Swhl739 size_t space;
421127dd473Swhl739 u_short numLocalPorts;
422127dd473Swhl739 port_range portRange;
423127dd473Swhl739
424127dd473Swhl739 numLocalPorts = 0;
425127dd473Swhl739
426127dd473Swhl739 r = (struct nat44_cfg_redir *)buf;
427127dd473Swhl739 r->mode = REDIR_PORT;
428127dd473Swhl739 /* Skip nat44_cfg_redir at beginning of buf. */
429127dd473Swhl739 buf = &buf[sizeof(struct nat44_cfg_redir)];
430127dd473Swhl739 space = sizeof(struct nat44_cfg_redir);
431127dd473Swhl739
432127dd473Swhl739 /*
433127dd473Swhl739 * Extract protocol.
434127dd473Swhl739 */
435127dd473Swhl739 r->proto = StrToProto(**av);
436127dd473Swhl739 protoName = **av;
437127dd473Swhl739 (*av)++; (*ac)--;
438127dd473Swhl739
439127dd473Swhl739 /*
440127dd473Swhl739 * Extract local address.
441127dd473Swhl739 */
442127dd473Swhl739 if (strchr(**av, ',') != NULL) {
443127dd473Swhl739 r->laddr.s_addr = INADDR_NONE;
444127dd473Swhl739 r->lport = ~0;
445127dd473Swhl739 numLocalPorts = 1;
446127dd473Swhl739 lsnat = **av;
447127dd473Swhl739 } else {
448127dd473Swhl739 /*
449127dd473Swhl739 * The sctp nat does not allow the port numbers to be mapped to
450127dd473Swhl739 * new port numbers. Therefore, no ports are to be specified
451127dd473Swhl739 * in the target port field.
452127dd473Swhl739 */
453127dd473Swhl739 if (r->proto == IPPROTO_SCTP) {
454127dd473Swhl739 if (strchr(**av, ':'))
455127dd473Swhl739 errx(EX_DATAERR, "redirect_port:"
456127dd473Swhl739 "port numbers do not change in sctp, so do "
457127dd473Swhl739 "not specify them as part of the target");
458127dd473Swhl739 else
459127dd473Swhl739 StrToAddr(**av, &r->laddr);
460127dd473Swhl739 } else {
461127dd473Swhl739 if (StrToAddrAndPortRange(**av, &r->laddr, protoName,
462127dd473Swhl739 &portRange) != 0)
463127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
464127dd473Swhl739 "invalid local port range");
465127dd473Swhl739
466127dd473Swhl739 r->lport = GETLOPORT(portRange);
467127dd473Swhl739 numLocalPorts = GETNUMPORTS(portRange);
468127dd473Swhl739 }
469127dd473Swhl739 }
470127dd473Swhl739 (*av)++; (*ac)--;
471127dd473Swhl739
472127dd473Swhl739 /*
473127dd473Swhl739 * Extract public port and optionally address.
474127dd473Swhl739 */
475127dd473Swhl739 if (strchr(**av, ':') != NULL) {
476127dd473Swhl739 if (StrToAddrAndPortRange(**av, &r->paddr, protoName,
477127dd473Swhl739 &portRange) != 0)
478127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
479127dd473Swhl739 "invalid public port range");
480127dd473Swhl739 } else {
481127dd473Swhl739 r->paddr.s_addr = INADDR_ANY;
482127dd473Swhl739 if (StrToPortRange(**av, protoName, &portRange) != 0)
483127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
484127dd473Swhl739 "invalid public port range");
485127dd473Swhl739 }
486127dd473Swhl739
487127dd473Swhl739 r->pport = GETLOPORT(portRange);
488127dd473Swhl739 if (r->proto == IPPROTO_SCTP) { /* so the logic below still works */
489127dd473Swhl739 numLocalPorts = GETNUMPORTS(portRange);
490127dd473Swhl739 r->lport = r->pport;
491127dd473Swhl739 }
492127dd473Swhl739 r->pport_cnt = GETNUMPORTS(portRange);
493127dd473Swhl739 (*av)++; (*ac)--;
494127dd473Swhl739
495127dd473Swhl739 /*
496127dd473Swhl739 * Extract remote address and optionally port.
497127dd473Swhl739 */
498127dd473Swhl739 /*
499127dd473Swhl739 * NB: isdigit(**av) => we've to check that next parameter is really an
500127dd473Swhl739 * option for this redirect entry, else stop here processing arg[cv].
501127dd473Swhl739 */
502127dd473Swhl739 if (*ac != 0 && isdigit(***av)) {
503127dd473Swhl739 if (strchr(**av, ':') != NULL) {
504127dd473Swhl739 if (StrToAddrAndPortRange(**av, &r->raddr, protoName,
505127dd473Swhl739 &portRange) != 0)
506127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
507127dd473Swhl739 "invalid remote port range");
508127dd473Swhl739 } else {
509127dd473Swhl739 SETLOPORT(portRange, 0);
510127dd473Swhl739 SETNUMPORTS(portRange, 1);
511127dd473Swhl739 StrToAddr(**av, &r->raddr);
512127dd473Swhl739 }
513127dd473Swhl739 (*av)++; (*ac)--;
514127dd473Swhl739 } else {
515127dd473Swhl739 SETLOPORT(portRange, 0);
516127dd473Swhl739 SETNUMPORTS(portRange, 1);
517127dd473Swhl739 r->raddr.s_addr = INADDR_ANY;
518127dd473Swhl739 }
519127dd473Swhl739 r->rport = GETLOPORT(portRange);
520127dd473Swhl739 r->rport_cnt = GETNUMPORTS(portRange);
521127dd473Swhl739
522127dd473Swhl739 /*
523127dd473Swhl739 * Make sure port ranges match up, then add the redirect ports.
524127dd473Swhl739 */
525127dd473Swhl739 if (numLocalPorts != r->pport_cnt)
526127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
527127dd473Swhl739 "port ranges must be equal in size");
528127dd473Swhl739
529127dd473Swhl739 /* Remote port range is allowed to be '0' which means all ports. */
530127dd473Swhl739 if (r->rport_cnt != numLocalPorts &&
531127dd473Swhl739 (r->rport_cnt != 1 || r->rport != 0))
532127dd473Swhl739 errx(EX_DATAERR, "redirect_port: remote port must"
533127dd473Swhl739 "be 0 or equal to local port range in size");
534127dd473Swhl739
535127dd473Swhl739 /* Setup LSNAT server pool. */
536127dd473Swhl739 if (lsnat != NULL) {
537127dd473Swhl739 struct nat44_cfg_spool *spool;
538127dd473Swhl739
539127dd473Swhl739 sep = strtok(lsnat, ",");
540127dd473Swhl739 while (sep != NULL) {
541127dd473Swhl739 spool = (struct nat44_cfg_spool *)buf;
542127dd473Swhl739 space += sizeof(struct nat44_cfg_spool);
543127dd473Swhl739 /*
544127dd473Swhl739 * The sctp nat does not allow the port numbers to
545127dd473Swhl739 * be mapped to new port numbers. Therefore, no ports
546127dd473Swhl739 * are to be specified in the target port field.
547127dd473Swhl739 */
548127dd473Swhl739 if (r->proto == IPPROTO_SCTP) {
549127dd473Swhl739 if (strchr (sep, ':')) {
550127dd473Swhl739 errx(EX_DATAERR, "redirect_port:"
551127dd473Swhl739 "port numbers do not change in "
552127dd473Swhl739 "sctp, so do not specify them as "
553127dd473Swhl739 "part of the target");
554127dd473Swhl739 } else {
555127dd473Swhl739 StrToAddr(sep, &spool->addr);
556127dd473Swhl739 spool->port = r->pport;
557127dd473Swhl739 }
558127dd473Swhl739 } else {
559127dd473Swhl739 if (StrToAddrAndPortRange(sep, &spool->addr,
560127dd473Swhl739 protoName, &portRange) != 0)
561127dd473Swhl739 errx(EX_DATAERR, "redirect_port:"
562127dd473Swhl739 "invalid local port range");
563127dd473Swhl739 if (GETNUMPORTS(portRange) != 1)
564127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
565127dd473Swhl739 "local port must be single in "
566127dd473Swhl739 "this context");
567127dd473Swhl739 spool->port = GETLOPORT(portRange);
568127dd473Swhl739 }
569127dd473Swhl739 r->spool_cnt++;
570127dd473Swhl739 /* Point to the next possible nat44_cfg_spool. */
571127dd473Swhl739 buf = &buf[sizeof(struct nat44_cfg_spool)];
572127dd473Swhl739 sep = strtok(NULL, ",");
573127dd473Swhl739 }
574127dd473Swhl739 }
575127dd473Swhl739
576127dd473Swhl739 return (space);
577127dd473Swhl739 }
578127dd473Swhl739
579127dd473Swhl739 static int
setup_redir_proto(char * buf,int * ac,char *** av)580127dd473Swhl739 setup_redir_proto(char *buf, int *ac, char ***av)
581127dd473Swhl739 {
582127dd473Swhl739 struct nat44_cfg_redir *r;
583127dd473Swhl739 struct protoent *protoent;
584127dd473Swhl739 size_t space;
585127dd473Swhl739
586127dd473Swhl739 r = (struct nat44_cfg_redir *)buf;
587127dd473Swhl739 r->mode = REDIR_PROTO;
588127dd473Swhl739 /* Skip nat44_cfg_redir at beginning of buf. */
589127dd473Swhl739 buf = &buf[sizeof(struct nat44_cfg_redir)];
590127dd473Swhl739 space = sizeof(struct nat44_cfg_redir);
591127dd473Swhl739
592127dd473Swhl739 /*
593127dd473Swhl739 * Extract protocol.
594127dd473Swhl739 */
595127dd473Swhl739 protoent = getprotobyname(**av);
596127dd473Swhl739 if (protoent == NULL)
597127dd473Swhl739 errx(EX_DATAERR, "redirect_proto: unknown protocol %s", **av);
598127dd473Swhl739 else
599127dd473Swhl739 r->proto = protoent->p_proto;
600127dd473Swhl739
601127dd473Swhl739 (*av)++; (*ac)--;
602127dd473Swhl739
603127dd473Swhl739 /*
604127dd473Swhl739 * Extract local address.
605127dd473Swhl739 */
606127dd473Swhl739 StrToAddr(**av, &r->laddr);
607127dd473Swhl739
608127dd473Swhl739 (*av)++; (*ac)--;
609127dd473Swhl739
610127dd473Swhl739 /*
611127dd473Swhl739 * Extract optional public address.
612127dd473Swhl739 */
613127dd473Swhl739 if (*ac == 0) {
614127dd473Swhl739 r->paddr.s_addr = INADDR_ANY;
615127dd473Swhl739 r->raddr.s_addr = INADDR_ANY;
616127dd473Swhl739 } else {
617127dd473Swhl739 /* see above in setup_redir_port() */
618127dd473Swhl739 if (isdigit(***av)) {
619127dd473Swhl739 StrToAddr(**av, &r->paddr);
620127dd473Swhl739 (*av)++; (*ac)--;
621127dd473Swhl739
622127dd473Swhl739 /*
623127dd473Swhl739 * Extract optional remote address.
624127dd473Swhl739 */
625127dd473Swhl739 /* see above in setup_redir_port() */
626127dd473Swhl739 if (*ac != 0 && isdigit(***av)) {
627127dd473Swhl739 StrToAddr(**av, &r->raddr);
628127dd473Swhl739 (*av)++; (*ac)--;
629127dd473Swhl739 }
630127dd473Swhl739 }
631127dd473Swhl739 }
632127dd473Swhl739
633127dd473Swhl739 return (space);
634127dd473Swhl739 }
635127dd473Swhl739
636127dd473Swhl739 static void
nat_show_log(struct nat44_cfg_nat * n,void * arg __unused)63722ce4affSfengbojiang nat_show_log(struct nat44_cfg_nat *n, void *arg __unused)
638127dd473Swhl739 {
639127dd473Swhl739 char *buf;
640127dd473Swhl739
641127dd473Swhl739 buf = (char *)(n + 1);
642127dd473Swhl739 if (buf[0] != '\0')
643127dd473Swhl739 printf("nat %s: %s\n", n->name, buf);
644127dd473Swhl739 }
645127dd473Swhl739
646127dd473Swhl739 static void
nat_show_cfg(struct nat44_cfg_nat * n,void * arg __unused)64722ce4affSfengbojiang nat_show_cfg(struct nat44_cfg_nat *n, void *arg __unused)
648127dd473Swhl739 {
649127dd473Swhl739 struct nat44_cfg_redir *t;
650127dd473Swhl739 struct nat44_cfg_spool *s;
651127dd473Swhl739 caddr_t buf;
652127dd473Swhl739 struct protoent *p;
65322ce4affSfengbojiang uint32_t cnt;
65422ce4affSfengbojiang int i, off;
655127dd473Swhl739
656127dd473Swhl739 buf = (caddr_t)n;
657127dd473Swhl739 off = sizeof(*n);
658127dd473Swhl739 printf("ipfw nat %s config", n->name);
659127dd473Swhl739 if (strlen(n->if_name) != 0)
660127dd473Swhl739 printf(" if %s", n->if_name);
661127dd473Swhl739 else if (n->ip.s_addr != 0)
662127dd473Swhl739 printf(" ip %s", inet_ntoa(n->ip));
663127dd473Swhl739 while (n->mode != 0) {
664127dd473Swhl739 if (n->mode & PKT_ALIAS_LOG) {
665127dd473Swhl739 printf(" log");
666127dd473Swhl739 n->mode &= ~PKT_ALIAS_LOG;
667127dd473Swhl739 } else if (n->mode & PKT_ALIAS_DENY_INCOMING) {
668127dd473Swhl739 printf(" deny_in");
669127dd473Swhl739 n->mode &= ~PKT_ALIAS_DENY_INCOMING;
670127dd473Swhl739 } else if (n->mode & PKT_ALIAS_SAME_PORTS) {
671127dd473Swhl739 printf(" same_ports");
672127dd473Swhl739 n->mode &= ~PKT_ALIAS_SAME_PORTS;
673127dd473Swhl739 } else if (n->mode & PKT_ALIAS_SKIP_GLOBAL) {
674127dd473Swhl739 printf(" skip_global");
675127dd473Swhl739 n->mode &= ~PKT_ALIAS_SKIP_GLOBAL;
676127dd473Swhl739 } else if (n->mode & PKT_ALIAS_UNREGISTERED_ONLY) {
677127dd473Swhl739 printf(" unreg_only");
678127dd473Swhl739 n->mode &= ~PKT_ALIAS_UNREGISTERED_ONLY;
67922ce4affSfengbojiang } else if (n->mode & PKT_ALIAS_UNREGISTERED_CGN) {
68022ce4affSfengbojiang printf(" unreg_cgn");
68122ce4affSfengbojiang n->mode &= ~PKT_ALIAS_UNREGISTERED_CGN;
682127dd473Swhl739 } else if (n->mode & PKT_ALIAS_RESET_ON_ADDR_CHANGE) {
683127dd473Swhl739 printf(" reset");
684127dd473Swhl739 n->mode &= ~PKT_ALIAS_RESET_ON_ADDR_CHANGE;
685127dd473Swhl739 } else if (n->mode & PKT_ALIAS_REVERSE) {
686127dd473Swhl739 printf(" reverse");
687127dd473Swhl739 n->mode &= ~PKT_ALIAS_REVERSE;
688127dd473Swhl739 } else if (n->mode & PKT_ALIAS_PROXY_ONLY) {
689127dd473Swhl739 printf(" proxy_only");
690127dd473Swhl739 n->mode &= ~PKT_ALIAS_PROXY_ONLY;
691127dd473Swhl739 }
692127dd473Swhl739 }
693127dd473Swhl739 /* Print all the redirect's data configuration. */
694127dd473Swhl739 for (cnt = 0; cnt < n->redir_cnt; cnt++) {
695127dd473Swhl739 t = (struct nat44_cfg_redir *)&buf[off];
696127dd473Swhl739 off += sizeof(struct nat44_cfg_redir);
697127dd473Swhl739 switch (t->mode) {
698127dd473Swhl739 case REDIR_ADDR:
699127dd473Swhl739 printf(" redirect_addr");
700127dd473Swhl739 if (t->spool_cnt == 0)
701127dd473Swhl739 printf(" %s", inet_ntoa(t->laddr));
702127dd473Swhl739 else
703127dd473Swhl739 for (i = 0; i < t->spool_cnt; i++) {
704127dd473Swhl739 s = (struct nat44_cfg_spool *)&buf[off];
705127dd473Swhl739 if (i)
706127dd473Swhl739 printf(",");
707127dd473Swhl739 else
708127dd473Swhl739 printf(" ");
709127dd473Swhl739 printf("%s", inet_ntoa(s->addr));
710127dd473Swhl739 off += sizeof(struct nat44_cfg_spool);
711127dd473Swhl739 }
712127dd473Swhl739 printf(" %s", inet_ntoa(t->paddr));
713127dd473Swhl739 break;
714127dd473Swhl739 case REDIR_PORT:
715127dd473Swhl739 p = getprotobynumber(t->proto);
716127dd473Swhl739 printf(" redirect_port %s ", p->p_name);
717127dd473Swhl739 if (!t->spool_cnt) {
718127dd473Swhl739 printf("%s:%u", inet_ntoa(t->laddr), t->lport);
719127dd473Swhl739 if (t->pport_cnt > 1)
720127dd473Swhl739 printf("-%u", t->lport +
721127dd473Swhl739 t->pport_cnt - 1);
722127dd473Swhl739 } else
723127dd473Swhl739 for (i=0; i < t->spool_cnt; i++) {
724127dd473Swhl739 s = (struct nat44_cfg_spool *)&buf[off];
725127dd473Swhl739 if (i)
726127dd473Swhl739 printf(",");
727127dd473Swhl739 printf("%s:%u", inet_ntoa(s->addr),
728127dd473Swhl739 s->port);
729127dd473Swhl739 off += sizeof(struct nat44_cfg_spool);
730127dd473Swhl739 }
731127dd473Swhl739
732127dd473Swhl739 printf(" ");
733127dd473Swhl739 if (t->paddr.s_addr)
734127dd473Swhl739 printf("%s:", inet_ntoa(t->paddr));
735127dd473Swhl739 printf("%u", t->pport);
736127dd473Swhl739 if (!t->spool_cnt && t->pport_cnt > 1)
737127dd473Swhl739 printf("-%u", t->pport + t->pport_cnt - 1);
738127dd473Swhl739
739127dd473Swhl739 if (t->raddr.s_addr) {
740127dd473Swhl739 printf(" %s", inet_ntoa(t->raddr));
741127dd473Swhl739 if (t->rport) {
742127dd473Swhl739 printf(":%u", t->rport);
743127dd473Swhl739 if (!t->spool_cnt && t->rport_cnt > 1)
744127dd473Swhl739 printf("-%u", t->rport +
745127dd473Swhl739 t->rport_cnt - 1);
746127dd473Swhl739 }
747127dd473Swhl739 }
748127dd473Swhl739 break;
749127dd473Swhl739 case REDIR_PROTO:
750127dd473Swhl739 p = getprotobynumber(t->proto);
751127dd473Swhl739 printf(" redirect_proto %s %s", p->p_name,
752127dd473Swhl739 inet_ntoa(t->laddr));
753127dd473Swhl739 if (t->paddr.s_addr != 0) {
754127dd473Swhl739 printf(" %s", inet_ntoa(t->paddr));
755127dd473Swhl739 if (t->raddr.s_addr)
756127dd473Swhl739 printf(" %s", inet_ntoa(t->raddr));
757127dd473Swhl739 }
758127dd473Swhl739 break;
759127dd473Swhl739 default:
760127dd473Swhl739 errx(EX_DATAERR, "unknown redir mode");
761127dd473Swhl739 break;
762127dd473Swhl739 }
763127dd473Swhl739 }
764127dd473Swhl739 printf("\n");
765127dd473Swhl739 }
766127dd473Swhl739
767127dd473Swhl739 void
ipfw_config_nat(int ac,char ** av)768127dd473Swhl739 ipfw_config_nat(int ac, char **av)
769127dd473Swhl739 {
770127dd473Swhl739 ipfw_obj_header *oh;
771127dd473Swhl739 struct nat44_cfg_nat *n; /* Nat instance configuration. */
772127dd473Swhl739 int i, off, tok, ac1;
773127dd473Swhl739 char *id, *buf, **av1, *end;
774127dd473Swhl739 size_t len;
775127dd473Swhl739
776127dd473Swhl739 av++;
777127dd473Swhl739 ac--;
778127dd473Swhl739 /* Nat id. */
779127dd473Swhl739 if (ac == 0)
780127dd473Swhl739 errx(EX_DATAERR, "missing nat id");
781127dd473Swhl739 id = *av;
782127dd473Swhl739 i = (int)strtol(id, &end, 0);
783127dd473Swhl739 if (i <= 0 || *end != '\0')
784127dd473Swhl739 errx(EX_DATAERR, "illegal nat id: %s", id);
785127dd473Swhl739 av++;
786127dd473Swhl739 ac--;
787127dd473Swhl739 if (ac == 0)
788127dd473Swhl739 errx(EX_DATAERR, "missing option");
789127dd473Swhl739
790127dd473Swhl739 len = sizeof(*oh) + sizeof(*n);
791127dd473Swhl739 ac1 = ac;
792127dd473Swhl739 av1 = av;
793127dd473Swhl739 while (ac1 > 0) {
794127dd473Swhl739 tok = match_token(nat_params, *av1);
795127dd473Swhl739 ac1--;
796127dd473Swhl739 av1++;
797127dd473Swhl739 switch (tok) {
798127dd473Swhl739 case TOK_IP:
799127dd473Swhl739 case TOK_IF:
800127dd473Swhl739 ac1--;
801127dd473Swhl739 av1++;
802127dd473Swhl739 break;
803127dd473Swhl739 case TOK_ALOG:
804127dd473Swhl739 case TOK_DENY_INC:
805127dd473Swhl739 case TOK_SAME_PORTS:
806127dd473Swhl739 case TOK_SKIP_GLOBAL:
807127dd473Swhl739 case TOK_UNREG_ONLY:
80822ce4affSfengbojiang case TOK_UNREG_CGN:
809127dd473Swhl739 case TOK_RESET_ADDR:
810127dd473Swhl739 case TOK_ALIAS_REV:
811127dd473Swhl739 case TOK_PROXY_ONLY:
812127dd473Swhl739 break;
813127dd473Swhl739 case TOK_REDIR_ADDR:
814127dd473Swhl739 if (ac1 < 2)
815127dd473Swhl739 errx(EX_DATAERR, "redirect_addr: "
816127dd473Swhl739 "not enough arguments");
817127dd473Swhl739 len += estimate_redir_addr(&ac1, &av1);
818127dd473Swhl739 av1 += 2;
819127dd473Swhl739 ac1 -= 2;
820127dd473Swhl739 break;
821127dd473Swhl739 case TOK_REDIR_PORT:
822127dd473Swhl739 if (ac1 < 3)
823127dd473Swhl739 errx(EX_DATAERR, "redirect_port: "
824127dd473Swhl739 "not enough arguments");
825127dd473Swhl739 av1++;
826127dd473Swhl739 ac1--;
827127dd473Swhl739 len += estimate_redir_port(&ac1, &av1);
828127dd473Swhl739 av1 += 2;
829127dd473Swhl739 ac1 -= 2;
830127dd473Swhl739 /* Skip optional remoteIP/port */
831127dd473Swhl739 if (ac1 != 0 && isdigit(**av1)) {
832127dd473Swhl739 av1++;
833127dd473Swhl739 ac1--;
834127dd473Swhl739 }
835127dd473Swhl739 break;
836127dd473Swhl739 case TOK_REDIR_PROTO:
837127dd473Swhl739 if (ac1 < 2)
838127dd473Swhl739 errx(EX_DATAERR, "redirect_proto: "
839127dd473Swhl739 "not enough arguments");
840127dd473Swhl739 len += sizeof(struct nat44_cfg_redir);
841127dd473Swhl739 av1 += 2;
842127dd473Swhl739 ac1 -= 2;
843127dd473Swhl739 /* Skip optional remoteIP/port */
844127dd473Swhl739 if (ac1 != 0 && isdigit(**av1)) {
845127dd473Swhl739 av1++;
846127dd473Swhl739 ac1--;
847127dd473Swhl739 }
848127dd473Swhl739 if (ac1 != 0 && isdigit(**av1)) {
849127dd473Swhl739 av1++;
850127dd473Swhl739 ac1--;
851127dd473Swhl739 }
852127dd473Swhl739 break;
853127dd473Swhl739 default:
854127dd473Swhl739 errx(EX_DATAERR, "unrecognised option ``%s''", av1[-1]);
855127dd473Swhl739 }
856127dd473Swhl739 }
857127dd473Swhl739
858127dd473Swhl739 if ((buf = malloc(len)) == NULL)
859127dd473Swhl739 errx(EX_OSERR, "malloc failed");
860127dd473Swhl739
861127dd473Swhl739 /* Offset in buf: save space for header at the beginning. */
862127dd473Swhl739 off = sizeof(*oh) + sizeof(*n);
863127dd473Swhl739 memset(buf, 0, len);
864127dd473Swhl739 oh = (ipfw_obj_header *)buf;
865127dd473Swhl739 n = (struct nat44_cfg_nat *)(oh + 1);
866127dd473Swhl739 oh->ntlv.head.length = sizeof(oh->ntlv);
867127dd473Swhl739 snprintf(oh->ntlv.name, sizeof(oh->ntlv.name), "%d", i);
868127dd473Swhl739 snprintf(n->name, sizeof(n->name), "%d", i);
869127dd473Swhl739
870127dd473Swhl739 while (ac > 0) {
871127dd473Swhl739 tok = match_token(nat_params, *av);
872127dd473Swhl739 ac--;
873127dd473Swhl739 av++;
874127dd473Swhl739 switch (tok) {
875127dd473Swhl739 case TOK_IP:
876127dd473Swhl739 if (ac == 0)
877127dd473Swhl739 errx(EX_DATAERR, "missing option");
878127dd473Swhl739 if (!inet_aton(av[0], &(n->ip)))
879127dd473Swhl739 errx(EX_DATAERR, "bad ip address ``%s''",
880127dd473Swhl739 av[0]);
881127dd473Swhl739 ac--;
882127dd473Swhl739 av++;
883127dd473Swhl739 break;
884127dd473Swhl739 case TOK_IF:
885127dd473Swhl739 if (ac == 0)
886127dd473Swhl739 errx(EX_DATAERR, "missing option");
887127dd473Swhl739 set_addr_dynamic(av[0], n);
888127dd473Swhl739 ac--;
889127dd473Swhl739 av++;
890127dd473Swhl739 break;
891127dd473Swhl739 case TOK_ALOG:
892127dd473Swhl739 n->mode |= PKT_ALIAS_LOG;
893127dd473Swhl739 break;
894127dd473Swhl739 case TOK_DENY_INC:
895127dd473Swhl739 n->mode |= PKT_ALIAS_DENY_INCOMING;
896127dd473Swhl739 break;
897127dd473Swhl739 case TOK_SAME_PORTS:
898127dd473Swhl739 n->mode |= PKT_ALIAS_SAME_PORTS;
899127dd473Swhl739 break;
900127dd473Swhl739 case TOK_UNREG_ONLY:
901127dd473Swhl739 n->mode |= PKT_ALIAS_UNREGISTERED_ONLY;
902127dd473Swhl739 break;
90322ce4affSfengbojiang case TOK_UNREG_CGN:
90422ce4affSfengbojiang n->mode |= PKT_ALIAS_UNREGISTERED_CGN;
90522ce4affSfengbojiang break;
906127dd473Swhl739 case TOK_SKIP_GLOBAL:
907127dd473Swhl739 n->mode |= PKT_ALIAS_SKIP_GLOBAL;
908127dd473Swhl739 break;
909127dd473Swhl739 case TOK_RESET_ADDR:
910127dd473Swhl739 n->mode |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
911127dd473Swhl739 break;
912127dd473Swhl739 case TOK_ALIAS_REV:
913127dd473Swhl739 n->mode |= PKT_ALIAS_REVERSE;
914127dd473Swhl739 break;
915127dd473Swhl739 case TOK_PROXY_ONLY:
916127dd473Swhl739 n->mode |= PKT_ALIAS_PROXY_ONLY;
917127dd473Swhl739 break;
918127dd473Swhl739 /*
919127dd473Swhl739 * All the setup_redir_* functions work directly in
920127dd473Swhl739 * the final buffer, see above for details.
921127dd473Swhl739 */
922127dd473Swhl739 case TOK_REDIR_ADDR:
923127dd473Swhl739 case TOK_REDIR_PORT:
924127dd473Swhl739 case TOK_REDIR_PROTO:
925127dd473Swhl739 switch (tok) {
926127dd473Swhl739 case TOK_REDIR_ADDR:
927127dd473Swhl739 i = setup_redir_addr(&buf[off], &ac, &av);
928127dd473Swhl739 break;
929127dd473Swhl739 case TOK_REDIR_PORT:
930127dd473Swhl739 i = setup_redir_port(&buf[off], &ac, &av);
931127dd473Swhl739 break;
932127dd473Swhl739 case TOK_REDIR_PROTO:
933127dd473Swhl739 i = setup_redir_proto(&buf[off], &ac, &av);
934127dd473Swhl739 break;
935127dd473Swhl739 }
936127dd473Swhl739 n->redir_cnt++;
937127dd473Swhl739 off += i;
938127dd473Swhl739 break;
939127dd473Swhl739 }
940127dd473Swhl739 }
941127dd473Swhl739
942127dd473Swhl739 i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len);
943127dd473Swhl739 if (i != 0)
944127dd473Swhl739 err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG");
945127dd473Swhl739
94622ce4affSfengbojiang if (!g_co.do_quiet) {
947127dd473Swhl739 /* After every modification, we show the resultant rule. */
948127dd473Swhl739 int _ac = 3;
949127dd473Swhl739 const char *_av[] = {"show", "config", id};
950127dd473Swhl739 ipfw_show_nat(_ac, (char **)(void *)_av);
951127dd473Swhl739 }
952127dd473Swhl739 }
953127dd473Swhl739
95422ce4affSfengbojiang static void
nat_fill_ntlv(ipfw_obj_ntlv * ntlv,int i)95522ce4affSfengbojiang nat_fill_ntlv(ipfw_obj_ntlv *ntlv, int i)
95622ce4affSfengbojiang {
95722ce4affSfengbojiang
95822ce4affSfengbojiang ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
95922ce4affSfengbojiang ntlv->head.length = sizeof(ipfw_obj_ntlv);
96022ce4affSfengbojiang ntlv->idx = 1;
96122ce4affSfengbojiang ntlv->set = 0; /* not yet */
96222ce4affSfengbojiang snprintf(ntlv->name, sizeof(ntlv->name), "%d", i);
96322ce4affSfengbojiang }
96422ce4affSfengbojiang
96522ce4affSfengbojiang int
ipfw_delete_nat(int i)96622ce4affSfengbojiang ipfw_delete_nat(int i)
96722ce4affSfengbojiang {
96822ce4affSfengbojiang ipfw_obj_header oh;
96922ce4affSfengbojiang int ret;
97022ce4affSfengbojiang
97122ce4affSfengbojiang memset(&oh, 0, sizeof(oh));
97222ce4affSfengbojiang nat_fill_ntlv(&oh.ntlv, i);
97322ce4affSfengbojiang ret = do_set3(IP_FW_NAT44_DESTROY, &oh.opheader, sizeof(oh));
97422ce4affSfengbojiang if (ret == -1) {
97522ce4affSfengbojiang if (!g_co.do_quiet)
97622ce4affSfengbojiang warn("nat %u not available", i);
97722ce4affSfengbojiang return (EX_UNAVAILABLE);
97822ce4affSfengbojiang }
97922ce4affSfengbojiang return (EX_OK);
98022ce4affSfengbojiang }
98122ce4affSfengbojiang
982127dd473Swhl739 struct nat_list_arg {
983127dd473Swhl739 uint16_t cmd;
984127dd473Swhl739 int is_all;
985127dd473Swhl739 };
986127dd473Swhl739
987127dd473Swhl739 static int
nat_show_data(struct nat44_cfg_nat * cfg,void * arg)988127dd473Swhl739 nat_show_data(struct nat44_cfg_nat *cfg, void *arg)
989127dd473Swhl739 {
990127dd473Swhl739 struct nat_list_arg *nla;
991127dd473Swhl739 ipfw_obj_header *oh;
992127dd473Swhl739
993127dd473Swhl739 nla = (struct nat_list_arg *)arg;
994127dd473Swhl739
995127dd473Swhl739 switch (nla->cmd) {
996127dd473Swhl739 case IP_FW_NAT44_XGETCONFIG:
997127dd473Swhl739 if (nat_get_cmd(cfg->name, nla->cmd, &oh) != 0) {
998127dd473Swhl739 warnx("Error getting nat instance %s info", cfg->name);
999127dd473Swhl739 break;
1000127dd473Swhl739 }
1001127dd473Swhl739 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
1002127dd473Swhl739 free(oh);
1003127dd473Swhl739 break;
1004127dd473Swhl739 case IP_FW_NAT44_XGETLOG:
1005127dd473Swhl739 if (nat_get_cmd(cfg->name, nla->cmd, &oh) == 0) {
1006127dd473Swhl739 nat_show_log((struct nat44_cfg_nat *)(oh + 1), NULL);
1007127dd473Swhl739 free(oh);
1008127dd473Swhl739 break;
1009127dd473Swhl739 }
1010127dd473Swhl739 /* Handle error */
1011127dd473Swhl739 if (nla->is_all != 0 && errno == ENOENT)
1012127dd473Swhl739 break;
1013127dd473Swhl739 warn("Error getting nat instance %s info", cfg->name);
1014127dd473Swhl739 break;
1015127dd473Swhl739 }
1016127dd473Swhl739
1017127dd473Swhl739 return (0);
1018127dd473Swhl739 }
1019127dd473Swhl739
1020127dd473Swhl739 /*
1021127dd473Swhl739 * Compare nat names.
1022127dd473Swhl739 * Honor number comparison.
1023127dd473Swhl739 */
1024127dd473Swhl739 static int
natname_cmp(const void * a,const void * b)1025127dd473Swhl739 natname_cmp(const void *a, const void *b)
1026127dd473Swhl739 {
102722ce4affSfengbojiang const struct nat44_cfg_nat *ia, *ib;
1028127dd473Swhl739
102922ce4affSfengbojiang ia = (const struct nat44_cfg_nat *)a;
103022ce4affSfengbojiang ib = (const struct nat44_cfg_nat *)b;
1031127dd473Swhl739
1032127dd473Swhl739 return (stringnum_cmp(ia->name, ib->name));
1033127dd473Swhl739 }
1034127dd473Swhl739
1035127dd473Swhl739 /*
1036127dd473Swhl739 * Retrieves nat list from kernel,
1037127dd473Swhl739 * optionally sorts it and calls requested function for each table.
1038127dd473Swhl739 * Returns 0 on success.
1039127dd473Swhl739 */
1040127dd473Swhl739 static int
nat_foreach(nat_cb_t * f,void * arg,int sort)1041127dd473Swhl739 nat_foreach(nat_cb_t *f, void *arg, int sort)
1042127dd473Swhl739 {
1043127dd473Swhl739 ipfw_obj_lheader *olh;
1044127dd473Swhl739 struct nat44_cfg_nat *cfg;
1045127dd473Swhl739 size_t sz;
104622ce4affSfengbojiang uint32_t i;
104722ce4affSfengbojiang int error;
1048127dd473Swhl739
1049127dd473Swhl739 /* Start with reasonable default */
1050127dd473Swhl739 sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat);
1051127dd473Swhl739
1052127dd473Swhl739 for (;;) {
1053127dd473Swhl739 if ((olh = calloc(1, sz)) == NULL)
1054127dd473Swhl739 return (ENOMEM);
1055127dd473Swhl739
1056127dd473Swhl739 olh->size = sz;
1057127dd473Swhl739 if (do_get3(IP_FW_NAT44_LIST_NAT, &olh->opheader, &sz) != 0) {
1058127dd473Swhl739 sz = olh->size;
1059127dd473Swhl739 free(olh);
1060127dd473Swhl739 if (errno == ENOMEM)
1061127dd473Swhl739 continue;
1062127dd473Swhl739 return (errno);
1063127dd473Swhl739 }
1064127dd473Swhl739
1065127dd473Swhl739 if (sort != 0)
1066127dd473Swhl739 qsort(olh + 1, olh->count, olh->objsize, natname_cmp);
1067127dd473Swhl739
1068127dd473Swhl739 cfg = (struct nat44_cfg_nat*)(olh + 1);
1069127dd473Swhl739 for (i = 0; i < olh->count; i++) {
1070127dd473Swhl739 error = f(cfg, arg); /* Ignore errors for now */
1071127dd473Swhl739 cfg = (struct nat44_cfg_nat *)((caddr_t)cfg +
1072127dd473Swhl739 olh->objsize);
1073127dd473Swhl739 }
1074127dd473Swhl739
1075127dd473Swhl739 free(olh);
1076127dd473Swhl739 break;
1077127dd473Swhl739 }
1078127dd473Swhl739
1079127dd473Swhl739 return (0);
1080127dd473Swhl739 }
1081127dd473Swhl739
1082127dd473Swhl739 static int
nat_get_cmd(char * name,uint16_t cmd,ipfw_obj_header ** ooh)1083127dd473Swhl739 nat_get_cmd(char *name, uint16_t cmd, ipfw_obj_header **ooh)
1084127dd473Swhl739 {
1085127dd473Swhl739 ipfw_obj_header *oh;
1086127dd473Swhl739 struct nat44_cfg_nat *cfg;
1087127dd473Swhl739 size_t sz;
1088127dd473Swhl739
1089127dd473Swhl739 /* Start with reasonable default */
1090127dd473Swhl739 sz = sizeof(*oh) + sizeof(*cfg) + 128;
1091127dd473Swhl739
1092127dd473Swhl739 for (;;) {
1093127dd473Swhl739 if ((oh = calloc(1, sz)) == NULL)
1094127dd473Swhl739 return (ENOMEM);
1095127dd473Swhl739 cfg = (struct nat44_cfg_nat *)(oh + 1);
1096127dd473Swhl739 oh->ntlv.head.length = sizeof(oh->ntlv);
1097127dd473Swhl739 strlcpy(oh->ntlv.name, name, sizeof(oh->ntlv.name));
1098127dd473Swhl739 strlcpy(cfg->name, name, sizeof(cfg->name));
1099127dd473Swhl739
1100127dd473Swhl739 if (do_get3(cmd, &oh->opheader, &sz) != 0) {
1101127dd473Swhl739 sz = cfg->size;
1102127dd473Swhl739 free(oh);
1103127dd473Swhl739 if (errno == ENOMEM)
1104127dd473Swhl739 continue;
1105127dd473Swhl739 return (errno);
1106127dd473Swhl739 }
1107127dd473Swhl739
1108127dd473Swhl739 *ooh = oh;
1109127dd473Swhl739 break;
1110127dd473Swhl739 }
1111127dd473Swhl739
1112127dd473Swhl739 return (0);
1113127dd473Swhl739 }
1114127dd473Swhl739
1115127dd473Swhl739 void
ipfw_show_nat(int ac,char ** av)1116127dd473Swhl739 ipfw_show_nat(int ac, char **av)
1117127dd473Swhl739 {
1118127dd473Swhl739 ipfw_obj_header *oh;
1119127dd473Swhl739 char *name;
1120127dd473Swhl739 int cmd;
1121127dd473Swhl739 struct nat_list_arg nla;
1122127dd473Swhl739
1123127dd473Swhl739 ac--;
1124127dd473Swhl739 av++;
1125127dd473Swhl739
112622ce4affSfengbojiang if (g_co.test_only)
1127127dd473Swhl739 return;
1128127dd473Swhl739
1129127dd473Swhl739 /* Parse parameters. */
1130127dd473Swhl739 cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */
1131127dd473Swhl739 name = NULL;
1132127dd473Swhl739 for ( ; ac != 0; ac--, av++) {
1133127dd473Swhl739 if (!strncmp(av[0], "config", strlen(av[0]))) {
1134127dd473Swhl739 cmd = IP_FW_NAT44_XGETCONFIG;
1135127dd473Swhl739 continue;
1136127dd473Swhl739 }
1137127dd473Swhl739 if (strcmp(av[0], "log") == 0) {
1138127dd473Swhl739 cmd = IP_FW_NAT44_XGETLOG;
1139127dd473Swhl739 continue;
1140127dd473Swhl739 }
1141127dd473Swhl739 if (name != NULL)
1142127dd473Swhl739 err(EX_USAGE,"only one instance name may be specified");
1143127dd473Swhl739 name = av[0];
1144127dd473Swhl739 }
1145127dd473Swhl739
1146127dd473Swhl739 if (cmd == 0)
1147127dd473Swhl739 errx(EX_USAGE, "Please specify action. Available: config,log");
1148127dd473Swhl739
1149127dd473Swhl739 if (name == NULL) {
1150127dd473Swhl739 memset(&nla, 0, sizeof(nla));
1151127dd473Swhl739 nla.cmd = cmd;
1152127dd473Swhl739 nla.is_all = 1;
1153127dd473Swhl739 nat_foreach(nat_show_data, &nla, 1);
1154127dd473Swhl739 } else {
1155127dd473Swhl739 if (nat_get_cmd(name, cmd, &oh) != 0)
1156127dd473Swhl739 err(EX_OSERR, "Error getting nat %s instance info", name);
1157127dd473Swhl739 nat_show_cfg((struct nat44_cfg_nat *)(oh + 1), NULL);
1158127dd473Swhl739 free(oh);
1159127dd473Swhl739 }
1160127dd473Swhl739 }
1161127dd473Swhl739
1162