165309e5cSBrian Somers /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
465309e5cSBrian Somers * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
565309e5cSBrian Somers * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
665309e5cSBrian Somers * Internet Initiative Japan, Inc (IIJ)
765309e5cSBrian Somers * All rights reserved.
8af57ed9fSAtsushi Murai *
965309e5cSBrian Somers * Redistribution and use in source and binary forms, with or without
1065309e5cSBrian Somers * modification, are permitted provided that the following conditions
1165309e5cSBrian Somers * are met:
1265309e5cSBrian Somers * 1. Redistributions of source code must retain the above copyright
1365309e5cSBrian Somers * notice, this list of conditions and the following disclaimer.
1465309e5cSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright
1565309e5cSBrian Somers * notice, this list of conditions and the following disclaimer in the
1665309e5cSBrian Somers * documentation and/or other materials provided with the distribution.
17af57ed9fSAtsushi Murai *
1865309e5cSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1965309e5cSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2065309e5cSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2165309e5cSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2265309e5cSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2365309e5cSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2465309e5cSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2565309e5cSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2665309e5cSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2765309e5cSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2865309e5cSBrian Somers * SUCH DAMAGE.
29af57ed9fSAtsushi Murai */
30af57ed9fSAtsushi Murai
31972a1bcfSBrian Somers #include <sys/param.h>
32af57ed9fSAtsushi Murai #include <netinet/in.h>
33f1884650SAtsushi Murai #include <netdb.h>
34eaa4df37SBrian Somers #include <netinet/in_systm.h>
35eaa4df37SBrian Somers #include <netinet/ip.h>
3630949fd4SBrian Somers #include <sys/socket.h>
371fa665f5SBrian Somers #include <sys/un.h>
3875240ed1SBrian Somers
396eafd353SBrian Somers #include <stdarg.h>
40af57ed9fSAtsushi Murai #include <stdio.h>
41af57ed9fSAtsushi Murai #include <stdlib.h>
42de59e178SBrian Somers #include <string.h>
4385b542cfSBrian Somers #include <termios.h>
4475240ed1SBrian Somers
455d9e6103SBrian Somers #include "layer.h"
46c9e11a11SBrian Somers #include "defs.h"
47af57ed9fSAtsushi Murai #include "command.h"
48927145beSBrian Somers #include "mbuf.h"
49927145beSBrian Somers #include "log.h"
5029e275ceSBrian Somers #include "iplist.h"
5129e275ceSBrian Somers #include "timer.h"
5229e275ceSBrian Somers #include "throughput.h"
53879ed6faSBrian Somers #include "lqr.h"
548c07a7b2SBrian Somers #include "hdlc.h"
557308ec68SBrian Somers #include "fsm.h"
563b0f8d2eSBrian Somers #include "lcp.h"
573b0f8d2eSBrian Somers #include "ccp.h"
583b0f8d2eSBrian Somers #include "link.h"
59eaa4df37SBrian Somers #include "slcompress.h"
6030949fd4SBrian Somers #include "ncpaddr.h"
61186d0be4SBrian Somers #include "ipcp.h"
6275240ed1SBrian Somers #include "filter.h"
6385b542cfSBrian Somers #include "descriptor.h"
6485b542cfSBrian Somers #include "prompt.h"
653b0f8d2eSBrian Somers #include "mp.h"
66972a1bcfSBrian Somers #ifndef NORADIUS
67972a1bcfSBrian Somers #include "radius.h"
68972a1bcfSBrian Somers #endif
6930949fd4SBrian Somers #include "ipv6cp.h"
7030949fd4SBrian Somers #include "ncp.h"
715828db6dSBrian Somers #include "bundle.h"
7275240ed1SBrian Somers
73057f1760SBrian Somers static unsigned filter_Nam2Op(const char *);
74af57ed9fSAtsushi Murai
75af57ed9fSAtsushi Murai static int
ParsePort(const char * service,const char * proto)7630949fd4SBrian Somers ParsePort(const char *service, const char *proto)
77f1884650SAtsushi Murai {
78f1884650SAtsushi Murai struct servent *servent;
7930949fd4SBrian Somers char *cp;
80f1884650SAtsushi Murai int port;
81f1884650SAtsushi Murai
8230949fd4SBrian Somers servent = getservbyname(service, proto);
8371866e62SMarcelo Araujo if (servent != NULL)
84cad7e742SBrian Somers return ntohs(servent->s_port);
85f1884650SAtsushi Murai
86f1884650SAtsushi Murai port = strtol(service, &cp, 0);
87f1884650SAtsushi Murai if (cp == service) {
88dd7e2610SBrian Somers log_Printf(LogWARN, "ParsePort: %s is not a port name or number.\n",
89927145beSBrian Somers service);
90cad7e742SBrian Somers return 0;
91f1884650SAtsushi Murai }
92cad7e742SBrian Somers return port;
93f1884650SAtsushi Murai }
94f1884650SAtsushi Murai
95af57ed9fSAtsushi Murai /*
96af57ed9fSAtsushi Murai * ICMP Syntax: src eq icmp_message_type
97af57ed9fSAtsushi Murai */
98af57ed9fSAtsushi Murai static int
ParseIcmp(int argc,char const * const * argv,struct filterent * tgt)99057f1760SBrian Somers ParseIcmp(int argc, char const *const *argv, struct filterent *tgt)
100af57ed9fSAtsushi Murai {
101af57ed9fSAtsushi Murai int type;
102af57ed9fSAtsushi Murai char *cp;
103af57ed9fSAtsushi Murai
104af57ed9fSAtsushi Murai switch (argc) {
105af57ed9fSAtsushi Murai case 0:
106af57ed9fSAtsushi Murai /* permit/deny all ICMP types */
10730949fd4SBrian Somers tgt->f_srcop = tgt->f_dstop = OP_NONE;
108af57ed9fSAtsushi Murai break;
1098390b576SBrian Somers
110af57ed9fSAtsushi Murai case 3:
11175240ed1SBrian Somers if (!strcmp(*argv, "src") && !strcmp(argv[1], "eq")) {
112af57ed9fSAtsushi Murai type = strtol(argv[2], &cp, 0);
113af57ed9fSAtsushi Murai if (cp == argv[2]) {
114dd7e2610SBrian Somers log_Printf(LogWARN, "ParseIcmp: type is expected.\n");
115cad7e742SBrian Somers return 0;
116af57ed9fSAtsushi Murai }
117cad7e742SBrian Somers tgt->f_srcop = OP_EQ;
118cad7e742SBrian Somers tgt->f_srcport = type;
11930949fd4SBrian Somers tgt->f_dstop = OP_NONE;
120af57ed9fSAtsushi Murai }
121af57ed9fSAtsushi Murai break;
1228390b576SBrian Somers
1238390b576SBrian Somers default:
124dd7e2610SBrian Somers log_Printf(LogWARN, "ParseIcmp: bad icmp syntax.\n");
125cad7e742SBrian Somers return 0;
126af57ed9fSAtsushi Murai }
127cad7e742SBrian Somers return 1;
128af57ed9fSAtsushi Murai }
129af57ed9fSAtsushi Murai
130af57ed9fSAtsushi Murai /*
131af57ed9fSAtsushi Murai * UDP Syntax: [src op port] [dst op port]
132af57ed9fSAtsushi Murai */
133af57ed9fSAtsushi Murai static int
ParseUdpOrTcp(int argc,char const * const * argv,const struct protoent * pe,struct filterent * tgt)13430949fd4SBrian Somers ParseUdpOrTcp(int argc, char const *const *argv, const struct protoent *pe,
1355ca5389aSBrian Somers struct filterent *tgt)
136af57ed9fSAtsushi Murai {
137cad7e742SBrian Somers tgt->f_srcop = tgt->f_dstop = OP_NONE;
138cad7e742SBrian Somers tgt->f_estab = tgt->f_syn = tgt->f_finrst = 0;
13901db51afSBrian Somers
14075240ed1SBrian Somers if (argc >= 3 && !strcmp(*argv, "src")) {
141cad7e742SBrian Somers tgt->f_srcop = filter_Nam2Op(argv[1]);
142cad7e742SBrian Somers if (tgt->f_srcop == OP_NONE) {
14330949fd4SBrian Somers log_Printf(LogWARN, "ParseUdpOrTcp: bad operator\n");
144cad7e742SBrian Somers return 0;
145af57ed9fSAtsushi Murai }
14630949fd4SBrian Somers if (pe == NULL)
14730949fd4SBrian Somers return 0;
14830949fd4SBrian Somers tgt->f_srcport = ParsePort(argv[2], pe->p_name);
149cad7e742SBrian Somers if (tgt->f_srcport == 0)
150cad7e742SBrian Somers return 0;
151944f7098SBrian Somers argc -= 3;
152944f7098SBrian Somers argv += 3;
153af57ed9fSAtsushi Murai }
154af57ed9fSAtsushi Murai
1551ae349f5Scvs2svn if (argc >= 3 && !strcmp(argv[0], "dst")) {
156cad7e742SBrian Somers tgt->f_dstop = filter_Nam2Op(argv[1]);
157cad7e742SBrian Somers if (tgt->f_dstop == OP_NONE) {
15830949fd4SBrian Somers log_Printf(LogWARN, "ParseUdpOrTcp: bad operator\n");
159cad7e742SBrian Somers return 0;
1601ae349f5Scvs2svn }
16130949fd4SBrian Somers if (pe == NULL)
16230949fd4SBrian Somers return 0;
16330949fd4SBrian Somers tgt->f_dstport = ParsePort(argv[2], pe->p_name);
164cad7e742SBrian Somers if (tgt->f_dstport == 0)
165cad7e742SBrian Somers return 0;
1661ae349f5Scvs2svn argc -= 3;
1671ae349f5Scvs2svn argv += 3;
1681ae349f5Scvs2svn }
16963f98b41SBrian Somers
17030949fd4SBrian Somers if (pe && pe->p_proto == IPPROTO_TCP) {
17163f98b41SBrian Somers for (; argc > 0; argc--, argv++)
17263f98b41SBrian Somers if (!strcmp(*argv, "estab"))
173cad7e742SBrian Somers tgt->f_estab = 1;
17463f98b41SBrian Somers else if (!strcmp(*argv, "syn"))
175cad7e742SBrian Somers tgt->f_syn = 1;
17663f98b41SBrian Somers else if (!strcmp(*argv, "finrst"))
177cad7e742SBrian Somers tgt->f_finrst = 1;
17863f98b41SBrian Somers else
17963f98b41SBrian Somers break;
180e43ebac1SBrian Somers }
18163f98b41SBrian Somers
18263f98b41SBrian Somers if (argc > 0) {
183dd7e2610SBrian Somers log_Printf(LogWARN, "ParseUdpOrTcp: bad src/dst port syntax: %s\n", *argv);
18463f98b41SBrian Somers return 0;
18563f98b41SBrian Somers }
18663f98b41SBrian Somers
18763f98b41SBrian Somers return 1;
1881ae349f5Scvs2svn }
189af57ed9fSAtsushi Murai
1902faae814SBrian Somers static int
ParseGeneric(int argc,struct filterent * tgt)191057f1760SBrian Somers ParseGeneric(int argc, struct filterent *tgt)
192cad7e742SBrian Somers {
1932faae814SBrian Somers /*
1942faae814SBrian Somers * Filter currently is a catch-all. Requests are either permitted or
1952faae814SBrian Somers * dropped.
1962faae814SBrian Somers */
1971f9e5fe5SBrian Somers if (argc != 0) {
19830949fd4SBrian Somers log_Printf(LogWARN, "ParseGeneric: Too many parameters\n");
1991f9e5fe5SBrian Somers return 0;
2001f9e5fe5SBrian Somers } else
20130949fd4SBrian Somers tgt->f_srcop = tgt->f_dstop = OP_NONE;
2021f9e5fe5SBrian Somers
2031f9e5fe5SBrian Somers return 1;
2041f9e5fe5SBrian Somers }
2051f9e5fe5SBrian Somers
2061d1fc017SBrian Somers static unsigned
addrtype(const char * addr)2071d1fc017SBrian Somers addrtype(const char *addr)
2081d1fc017SBrian Somers {
2091d1fc017SBrian Somers if (!strncasecmp(addr, "MYADDR", 6) && (addr[6] == '\0' || addr[6] == '/'))
2101d1fc017SBrian Somers return T_MYADDR;
21130949fd4SBrian Somers if (!strncasecmp(addr, "MYADDR6", 7) && (addr[7] == '\0' || addr[7] == '/'))
21230949fd4SBrian Somers return T_MYADDR6;
2131d1fc017SBrian Somers if (!strncasecmp(addr, "HISADDR", 7) && (addr[7] == '\0' || addr[7] == '/'))
2141d1fc017SBrian Somers return T_HISADDR;
21530949fd4SBrian Somers if (!strncasecmp(addr, "HISADDR6", 8) && (addr[8] == '\0' || addr[8] == '/'))
21630949fd4SBrian Somers return T_HISADDR6;
217d568d6c4SBrian Somers if (!strncasecmp(addr, "DNS0", 4) && (addr[4] == '\0' || addr[4] == '/'))
218d568d6c4SBrian Somers return T_DNS0;
219d568d6c4SBrian Somers if (!strncasecmp(addr, "DNS1", 4) && (addr[4] == '\0' || addr[4] == '/'))
220d568d6c4SBrian Somers return T_DNS1;
2211d1fc017SBrian Somers
2221d1fc017SBrian Somers return T_ADDR;
2231d1fc017SBrian Somers }
2241d1fc017SBrian Somers
2251d1fc017SBrian Somers static const char *
addrstr(struct ncprange * addr,unsigned type)22630949fd4SBrian Somers addrstr(struct ncprange *addr, unsigned type)
2271d1fc017SBrian Somers {
2281d1fc017SBrian Somers switch (type) {
2291d1fc017SBrian Somers case T_MYADDR:
2301d1fc017SBrian Somers return "MYADDR";
2311d1fc017SBrian Somers case T_HISADDR:
2321d1fc017SBrian Somers return "HISADDR";
233d568d6c4SBrian Somers case T_DNS0:
234d568d6c4SBrian Somers return "DNS0";
235d568d6c4SBrian Somers case T_DNS1:
236d568d6c4SBrian Somers return "DNS1";
2371d1fc017SBrian Somers }
23830949fd4SBrian Somers return ncprange_ntoa(addr);
239cad7e742SBrian Somers }
240cad7e742SBrian Somers
241af57ed9fSAtsushi Murai static int
filter_Parse(struct ncp * ncp,int argc,char const * const * argv,struct filterent * ofp)24230949fd4SBrian Somers filter_Parse(struct ncp *ncp, int argc, char const *const *argv,
2435828db6dSBrian Somers struct filterent *ofp)
244af57ed9fSAtsushi Murai {
24530949fd4SBrian Somers struct filterent fe;
24630949fd4SBrian Somers struct protoent *pe;
247af57ed9fSAtsushi Murai char *wp;
24830949fd4SBrian Somers int action, family, ruleno, val, width;
249af57ed9fSAtsushi Murai
250cad7e742SBrian Somers ruleno = strtol(*argv, &wp, 0);
251cad7e742SBrian Somers if (*argv == wp || ruleno >= MAXFILTERS) {
252dd7e2610SBrian Somers log_Printf(LogWARN, "Parse: invalid filter number.\n");
253cad7e742SBrian Somers return 0;
254af57ed9fSAtsushi Murai }
255cad7e742SBrian Somers if (ruleno < 0) {
256cad7e742SBrian Somers for (ruleno = 0; ruleno < MAXFILTERS; ruleno++) {
257cad7e742SBrian Somers ofp->f_action = A_NONE;
258af57ed9fSAtsushi Murai ofp++;
259af57ed9fSAtsushi Murai }
260dd7e2610SBrian Somers log_Printf(LogWARN, "Parse: filter cleared.\n");
261cad7e742SBrian Somers return 1;
262af57ed9fSAtsushi Murai }
263cad7e742SBrian Somers ofp += ruleno;
264af57ed9fSAtsushi Murai
265af57ed9fSAtsushi Murai if (--argc == 0) {
266dd7e2610SBrian Somers log_Printf(LogWARN, "Parse: missing action.\n");
267cad7e742SBrian Somers return 0;
268af57ed9fSAtsushi Murai }
269af57ed9fSAtsushi Murai argv++;
270af57ed9fSAtsushi Murai
27130949fd4SBrian Somers memset(&fe, '\0', sizeof fe);
272af57ed9fSAtsushi Murai
273cad7e742SBrian Somers val = strtol(*argv, &wp, 0);
274cad7e742SBrian Somers if (!*wp && val >= 0 && val < MAXFILTERS) {
275cad7e742SBrian Somers if (val <= ruleno) {
276cad7e742SBrian Somers log_Printf(LogWARN, "Parse: Can only jump forward from rule %d\n",
277cad7e742SBrian Somers ruleno);
278cad7e742SBrian Somers return 0;
279cad7e742SBrian Somers }
280cad7e742SBrian Somers action = val;
281cad7e742SBrian Somers } else if (!strcmp(*argv, "permit")) {
282af57ed9fSAtsushi Murai action = A_PERMIT;
28375240ed1SBrian Somers } else if (!strcmp(*argv, "deny")) {
284af57ed9fSAtsushi Murai action = A_DENY;
28575240ed1SBrian Somers } else if (!strcmp(*argv, "clear")) {
286cad7e742SBrian Somers ofp->f_action = A_NONE;
287cad7e742SBrian Somers return 1;
288af57ed9fSAtsushi Murai } else {
28930949fd4SBrian Somers log_Printf(LogWARN, "Parse: %s: bad action\n", *argv);
290cad7e742SBrian Somers return 0;
291af57ed9fSAtsushi Murai }
29230949fd4SBrian Somers fe.f_action = action;
293af57ed9fSAtsushi Murai
294944f7098SBrian Somers argc--;
295944f7098SBrian Somers argv++;
296af57ed9fSAtsushi Murai
297cad7e742SBrian Somers if (argc && argv[0][0] == '!' && !argv[0][1]) {
29830949fd4SBrian Somers fe.f_invert = 1;
299944f7098SBrian Somers argc--;
300944f7098SBrian Somers argv++;
301af57ed9fSAtsushi Murai }
302972a1bcfSBrian Somers
30330949fd4SBrian Somers ncprange_init(&fe.f_src);
30430949fd4SBrian Somers ncprange_init(&fe.f_dst);
30530949fd4SBrian Somers
30630949fd4SBrian Somers if (argc == 0)
30730949fd4SBrian Somers pe = NULL;
30830949fd4SBrian Somers else if ((pe = getprotobyname(*argv)) == NULL && strcmp(*argv, "all") != 0) {
30930949fd4SBrian Somers if (argc < 2) {
31030949fd4SBrian Somers log_Printf(LogWARN, "Parse: Protocol or address pair expected\n");
31130949fd4SBrian Somers return 0;
31230949fd4SBrian Somers } else if (strcasecmp(*argv, "any") == 0 ||
31330949fd4SBrian Somers ncprange_aton(&fe.f_src, ncp, *argv)) {
31430949fd4SBrian Somers family = ncprange_family(&fe.f_src);
31530949fd4SBrian Somers if (!ncprange_getwidth(&fe.f_src, &width))
31630949fd4SBrian Somers width = 0;
31730949fd4SBrian Somers if (width == 0)
31830949fd4SBrian Somers ncprange_init(&fe.f_src);
31930949fd4SBrian Somers fe.f_srctype = addrtype(*argv);
320944f7098SBrian Somers argc--;
321944f7098SBrian Somers argv++;
32230949fd4SBrian Somers
32330949fd4SBrian Somers if (strcasecmp(*argv, "any") == 0 ||
32430949fd4SBrian Somers ncprange_aton(&fe.f_dst, ncp, *argv)) {
32530949fd4SBrian Somers if (ncprange_family(&fe.f_dst) != AF_UNSPEC &&
32630949fd4SBrian Somers ncprange_family(&fe.f_src) != AF_UNSPEC &&
32730949fd4SBrian Somers family != ncprange_family(&fe.f_dst)) {
32830949fd4SBrian Somers log_Printf(LogWARN, "Parse: src and dst address families differ\n");
32930949fd4SBrian Somers return 0;
33030949fd4SBrian Somers }
33130949fd4SBrian Somers if (!ncprange_getwidth(&fe.f_dst, &width))
33230949fd4SBrian Somers width = 0;
33330949fd4SBrian Somers if (width == 0)
33430949fd4SBrian Somers ncprange_init(&fe.f_dst);
33530949fd4SBrian Somers fe.f_dsttype = addrtype(*argv);
336944f7098SBrian Somers argc--;
337944f7098SBrian Somers argv++;
33830949fd4SBrian Somers } else {
33930949fd4SBrian Somers log_Printf(LogWARN, "Parse: Protocol or address pair expected\n");
34030949fd4SBrian Somers return 0;
34130949fd4SBrian Somers }
34230949fd4SBrian Somers
343cad7e742SBrian Somers if (argc) {
34430949fd4SBrian Somers if ((pe = getprotobyname(*argv)) == NULL && strcmp(*argv, "all") != 0) {
34530949fd4SBrian Somers log_Printf(LogWARN, "Parse: %s: Protocol expected\n", *argv);
346cad7e742SBrian Somers return 0;
347cad7e742SBrian Somers } else {
348944f7098SBrian Somers argc--;
349944f7098SBrian Somers argv++;
350af57ed9fSAtsushi Murai }
351cad7e742SBrian Somers }
3529ed2f048SBrian Somers } else {
35330949fd4SBrian Somers log_Printf(LogWARN, "Parse: Protocol or address pair expected\n");
354cad7e742SBrian Somers return 0;
355af57ed9fSAtsushi Murai }
356af57ed9fSAtsushi Murai } else {
357944f7098SBrian Somers argc--;
358944f7098SBrian Somers argv++;
359af57ed9fSAtsushi Murai }
360af57ed9fSAtsushi Murai
36130949fd4SBrian Somers if (argc >= 2 && strcmp(*argv, "timeout") == 0) {
36230949fd4SBrian Somers fe.timeout = strtoul(argv[1], NULL, 10);
3630a4b6c5cSBrian Somers argc -= 2;
36430949fd4SBrian Somers argv += 2;
3650a4b6c5cSBrian Somers }
3660a4b6c5cSBrian Somers
367af57ed9fSAtsushi Murai val = 1;
36830949fd4SBrian Somers fe.f_proto = (pe == NULL) ? 0 : pe->p_proto;
369af57ed9fSAtsushi Murai
37030949fd4SBrian Somers switch (fe.f_proto) {
37130949fd4SBrian Somers case IPPROTO_TCP:
37230949fd4SBrian Somers case IPPROTO_UDP:
37330949fd4SBrian Somers case IPPROTO_IPIP:
37430949fd4SBrian Somers #ifndef NOINET6
37530949fd4SBrian Somers case IPPROTO_IPV6:
37662e85934SBrian Somers #endif
37730949fd4SBrian Somers val = ParseUdpOrTcp(argc, argv, pe, &fe);
37828149effSBrian Somers break;
37930949fd4SBrian Somers case IPPROTO_ICMP:
38030949fd4SBrian Somers #ifndef NOINET6
38130949fd4SBrian Somers case IPPROTO_ICMPV6:
38228149effSBrian Somers #endif
383057f1760SBrian Somers val = ParseIcmp(argc, argv, &fe);
38430949fd4SBrian Somers break;
38530949fd4SBrian Somers default:
386057f1760SBrian Somers val = ParseGeneric(argc, &fe);
38730949fd4SBrian Somers break;
388af57ed9fSAtsushi Murai }
389af57ed9fSAtsushi Murai
39030949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: Src: %s\n", ncprange_ntoa(&fe.f_src));
39130949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: Dst: %s\n", ncprange_ntoa(&fe.f_dst));
39230949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: Proto: %d\n", fe.f_proto);
393af57ed9fSAtsushi Murai
394dd7e2610SBrian Somers log_Printf(LogDEBUG, "Parse: src: %s (%d)\n",
39530949fd4SBrian Somers filter_Op2Nam(fe.f_srcop), fe.f_srcport);
396dd7e2610SBrian Somers log_Printf(LogDEBUG, "Parse: dst: %s (%d)\n",
39730949fd4SBrian Somers filter_Op2Nam(fe.f_dstop), fe.f_dstport);
39830949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: estab: %u\n", fe.f_estab);
39930949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: syn: %u\n", fe.f_syn);
40030949fd4SBrian Somers log_Printf(LogDEBUG, "Parse: finrst: %u\n", fe.f_finrst);
401af57ed9fSAtsushi Murai
402af57ed9fSAtsushi Murai if (val)
40330949fd4SBrian Somers *ofp = fe;
404cad7e742SBrian Somers
405cad7e742SBrian Somers return val;
406af57ed9fSAtsushi Murai }
407af57ed9fSAtsushi Murai
408af57ed9fSAtsushi Murai int
filter_Set(struct cmdargs const * arg)409dd7e2610SBrian Somers filter_Set(struct cmdargs const *arg)
410af57ed9fSAtsushi Murai {
4115ca5389aSBrian Somers struct filter *filter;
4121ae349f5Scvs2svn
41325092092SBrian Somers if (arg->argc < arg->argn+2)
4141ae349f5Scvs2svn return -1;
4151ae349f5Scvs2svn
41625092092SBrian Somers if (!strcmp(arg->argv[arg->argn], "in"))
4175ca5389aSBrian Somers filter = &arg->bundle->filter.in;
41825092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "out"))
4195ca5389aSBrian Somers filter = &arg->bundle->filter.out;
42025092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "dial"))
4215ca5389aSBrian Somers filter = &arg->bundle->filter.dial;
42225092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "alive"))
4235ca5389aSBrian Somers filter = &arg->bundle->filter.alive;
42463f98b41SBrian Somers else {
425dd7e2610SBrian Somers log_Printf(LogWARN, "filter_Set: %s: Invalid filter name.\n",
42663f98b41SBrian Somers arg->argv[arg->argn]);
427927145beSBrian Somers return -1;
428af57ed9fSAtsushi Murai }
429af57ed9fSAtsushi Murai
43030949fd4SBrian Somers filter_Parse(&arg->bundle->ncp, arg->argc - arg->argn - 1,
43125092092SBrian Somers arg->argv + arg->argn + 1, filter->rule);
432927145beSBrian Somers return 0;
433927145beSBrian Somers }
434af57ed9fSAtsushi Murai
4358390b576SBrian Somers const char *
filter_Action2Nam(unsigned act)436057f1760SBrian Somers filter_Action2Nam(unsigned act)
437af57ed9fSAtsushi Murai {
438182c898aSBrian Somers static const char * const actname[] = { " none ", "permit ", " deny " };
439cad7e742SBrian Somers static char buf[8];
440cad7e742SBrian Somers
441057f1760SBrian Somers if (act < MAXFILTERS) {
442804c097bSBrian Somers snprintf(buf, sizeof buf, "%6d ", act);
443cad7e742SBrian Somers return buf;
444cad7e742SBrian Somers } else if (act >= A_NONE && act < A_NONE + sizeof(actname)/sizeof(char *))
445cad7e742SBrian Somers return actname[act - A_NONE];
446cad7e742SBrian Somers else
447cad7e742SBrian Somers return "?????? ";
4488390b576SBrian Somers }
449af57ed9fSAtsushi Murai
450af57ed9fSAtsushi Murai static void
doShowFilter(struct filterent * fp,struct prompt * prompt)451b6217683SBrian Somers doShowFilter(struct filterent *fp, struct prompt *prompt)
452af57ed9fSAtsushi Murai {
45330949fd4SBrian Somers struct protoent *pe;
454af57ed9fSAtsushi Murai int n;
455af57ed9fSAtsushi Murai
456af57ed9fSAtsushi Murai for (n = 0; n < MAXFILTERS; n++, fp++) {
457cad7e742SBrian Somers if (fp->f_action != A_NONE) {
458cad7e742SBrian Somers prompt_Printf(prompt, " %2d %s", n, filter_Action2Nam(fp->f_action));
459cad7e742SBrian Somers prompt_Printf(prompt, "%c ", fp->f_invert ? '!' : ' ');
46030949fd4SBrian Somers
46130949fd4SBrian Somers if (ncprange_isset(&fp->f_src))
46230949fd4SBrian Somers prompt_Printf(prompt, "%s ", addrstr(&fp->f_src, fp->f_srctype));
46330949fd4SBrian Somers else
46430949fd4SBrian Somers prompt_Printf(prompt, "any ");
46530949fd4SBrian Somers
46630949fd4SBrian Somers if (ncprange_isset(&fp->f_dst))
46730949fd4SBrian Somers prompt_Printf(prompt, "%s ", addrstr(&fp->f_dst, fp->f_dsttype));
46830949fd4SBrian Somers else
46930949fd4SBrian Somers prompt_Printf(prompt, "any ");
47030949fd4SBrian Somers
471cad7e742SBrian Somers if (fp->f_proto) {
47230949fd4SBrian Somers if ((pe = getprotobynumber(fp->f_proto)) == NULL)
47330949fd4SBrian Somers prompt_Printf(prompt, "P:%d", fp->f_proto);
47430949fd4SBrian Somers else
47530949fd4SBrian Somers prompt_Printf(prompt, "%s", pe->p_name);
476af57ed9fSAtsushi Murai
477cad7e742SBrian Somers if (fp->f_srcop)
478cad7e742SBrian Somers prompt_Printf(prompt, " src %s %d", filter_Op2Nam(fp->f_srcop),
479cad7e742SBrian Somers fp->f_srcport);
480cad7e742SBrian Somers if (fp->f_dstop)
481cad7e742SBrian Somers prompt_Printf(prompt, " dst %s %d", filter_Op2Nam(fp->f_dstop),
482cad7e742SBrian Somers fp->f_dstport);
483cad7e742SBrian Somers if (fp->f_estab)
484b6217683SBrian Somers prompt_Printf(prompt, " estab");
485cad7e742SBrian Somers if (fp->f_syn)
48663f98b41SBrian Somers prompt_Printf(prompt, " syn");
487cad7e742SBrian Somers if (fp->f_finrst)
48863f98b41SBrian Somers prompt_Printf(prompt, " finrst");
48930949fd4SBrian Somers } else
49030949fd4SBrian Somers prompt_Printf(prompt, "all");
4910a4b6c5cSBrian Somers if (fp->timeout != 0)
4920a4b6c5cSBrian Somers prompt_Printf(prompt, " timeout %u", fp->timeout);
493b6217683SBrian Somers prompt_Printf(prompt, "\n");
494af57ed9fSAtsushi Murai }
495af57ed9fSAtsushi Murai }
496af57ed9fSAtsushi Murai }
497af57ed9fSAtsushi Murai
498af57ed9fSAtsushi Murai int
filter_Show(struct cmdargs const * arg)499dd7e2610SBrian Somers filter_Show(struct cmdargs const *arg)
500af57ed9fSAtsushi Murai {
50125092092SBrian Somers if (arg->argc > arg->argn+1)
5025ca5389aSBrian Somers return -1;
5035ca5389aSBrian Somers
50425092092SBrian Somers if (arg->argc == arg->argn+1) {
5055ca5389aSBrian Somers struct filter *filter;
5065ca5389aSBrian Somers
50725092092SBrian Somers if (!strcmp(arg->argv[arg->argn], "in"))
5085ca5389aSBrian Somers filter = &arg->bundle->filter.in;
50925092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "out"))
5105ca5389aSBrian Somers filter = &arg->bundle->filter.out;
51125092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "dial"))
5125ca5389aSBrian Somers filter = &arg->bundle->filter.dial;
51325092092SBrian Somers else if (!strcmp(arg->argv[arg->argn], "alive"))
5145ca5389aSBrian Somers filter = &arg->bundle->filter.alive;
5155ca5389aSBrian Somers else
5165ca5389aSBrian Somers return -1;
517b6217683SBrian Somers doShowFilter(filter->rule, arg->prompt);
5185ca5389aSBrian Somers } else {
5195ca5389aSBrian Somers struct filter *filter[4];
5205ca5389aSBrian Somers int f;
5215ca5389aSBrian Somers
5225ca5389aSBrian Somers filter[0] = &arg->bundle->filter.in;
5235ca5389aSBrian Somers filter[1] = &arg->bundle->filter.out;
5245ca5389aSBrian Somers filter[2] = &arg->bundle->filter.dial;
5255ca5389aSBrian Somers filter[3] = &arg->bundle->filter.alive;
5265ca5389aSBrian Somers for (f = 0; f < 4; f++) {
5275ca5389aSBrian Somers if (f)
528b6217683SBrian Somers prompt_Printf(arg->prompt, "\n");
529b6217683SBrian Somers prompt_Printf(arg->prompt, "%s:\n", filter[f]->name);
530b6217683SBrian Somers doShowFilter(filter[f]->rule, arg->prompt);
5315ca5389aSBrian Somers }
5321ae349f5Scvs2svn }
5331ae349f5Scvs2svn
534927145beSBrian Somers return 0;
535af57ed9fSAtsushi Murai }
536af57ed9fSAtsushi Murai
537182c898aSBrian Somers static const char * const opname[] = {"none", "eq", "gt", "lt"};
5388390b576SBrian Somers
5398390b576SBrian Somers const char *
filter_Op2Nam(unsigned op)540057f1760SBrian Somers filter_Op2Nam(unsigned op)
54153c9f6c0SAtsushi Murai {
5428390b576SBrian Somers if (op >= sizeof opname / sizeof opname[0])
5438390b576SBrian Somers return "unknown";
5448390b576SBrian Somers return opname[op];
5458390b576SBrian Somers
5468390b576SBrian Somers }
5478390b576SBrian Somers
548057f1760SBrian Somers static unsigned
filter_Nam2Op(const char * cp)5498390b576SBrian Somers filter_Nam2Op(const char *cp)
5508390b576SBrian Somers {
551057f1760SBrian Somers unsigned op;
5528390b576SBrian Somers
5538390b576SBrian Somers for (op = sizeof opname / sizeof opname[0] - 1; op; op--)
5548390b576SBrian Somers if (!strcasecmp(cp, opname[op]))
5558390b576SBrian Somers break;
5568390b576SBrian Somers
5578390b576SBrian Somers return op;
55853c9f6c0SAtsushi Murai }
5591d1fc017SBrian Somers
5601d1fc017SBrian Somers void
filter_AdjustAddr(struct filter * filter,struct ncpaddr * local,struct ncpaddr * remote,struct in_addr * dns)56130949fd4SBrian Somers filter_AdjustAddr(struct filter *filter, struct ncpaddr *local,
56230949fd4SBrian Somers struct ncpaddr *remote, struct in_addr *dns)
5631d1fc017SBrian Somers {
5641d1fc017SBrian Somers struct filterent *fp;
5651d1fc017SBrian Somers int n;
5661d1fc017SBrian Somers
5671d1fc017SBrian Somers for (fp = filter->rule, n = 0; n < MAXFILTERS; fp++, n++)
568cad7e742SBrian Somers if (fp->f_action != A_NONE) {
56930949fd4SBrian Somers if (local) {
57030949fd4SBrian Somers if (fp->f_srctype == T_MYADDR && ncpaddr_family(local) == AF_INET)
57130949fd4SBrian Somers ncprange_sethost(&fp->f_src, local);
57230949fd4SBrian Somers if (fp->f_dsttype == T_MYADDR && ncpaddr_family(local) == AF_INET)
57330949fd4SBrian Somers ncprange_sethost(&fp->f_dst, local);
57430949fd4SBrian Somers #ifndef NOINET6
57530949fd4SBrian Somers if (fp->f_srctype == T_MYADDR6 && ncpaddr_family(local) == AF_INET6)
57630949fd4SBrian Somers ncprange_sethost(&fp->f_src, local);
57730949fd4SBrian Somers if (fp->f_dsttype == T_MYADDR6 && ncpaddr_family(local) == AF_INET6)
57830949fd4SBrian Somers ncprange_sethost(&fp->f_dst, local);
57930949fd4SBrian Somers #endif
5801d1fc017SBrian Somers }
58130949fd4SBrian Somers if (remote) {
58230949fd4SBrian Somers if (fp->f_srctype == T_HISADDR && ncpaddr_family(remote) == AF_INET)
58330949fd4SBrian Somers ncprange_sethost(&fp->f_src, remote);
58430949fd4SBrian Somers if (fp->f_dsttype == T_HISADDR && ncpaddr_family(remote) == AF_INET)
58530949fd4SBrian Somers ncprange_sethost(&fp->f_dst, remote);
58630949fd4SBrian Somers #ifndef NOINET6
58730949fd4SBrian Somers if (fp->f_srctype == T_HISADDR6 && ncpaddr_family(remote) == AF_INET6)
58830949fd4SBrian Somers ncprange_sethost(&fp->f_src, remote);
58930949fd4SBrian Somers if (fp->f_dsttype == T_HISADDR6 && ncpaddr_family(remote) == AF_INET6)
59030949fd4SBrian Somers ncprange_sethost(&fp->f_dst, remote);
59130949fd4SBrian Somers #endif
5921d1fc017SBrian Somers }
593d568d6c4SBrian Somers if (dns) {
594d568d6c4SBrian Somers if (fp->f_srctype == T_DNS0)
59530949fd4SBrian Somers ncprange_setip4host(&fp->f_src, dns[0]);
596d568d6c4SBrian Somers if (fp->f_dsttype == T_DNS0)
59730949fd4SBrian Somers ncprange_setip4host(&fp->f_dst, dns[0]);
598d568d6c4SBrian Somers if (fp->f_srctype == T_DNS1)
59930949fd4SBrian Somers ncprange_setip4host(&fp->f_src, dns[1]);
600d568d6c4SBrian Somers if (fp->f_dsttype == T_DNS1)
60130949fd4SBrian Somers ncprange_setip4host(&fp->f_dst, dns[1]);
602d568d6c4SBrian Somers }
6031d1fc017SBrian Somers }
6041d1fc017SBrian Somers }
605