2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/usr.sbin/ppp/filter.c,v 1.39.2.7 2002/09/01 02:12:26 brian Exp $
31 #include <sys/param.h>
32 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
52 #include "throughput.h"
59 #include "slcompress.h"
63 #include "descriptor.h"
73 static unsigned filter_Nam2Op(const char *);
76 ParsePort(const char *service
, const char *proto
)
78 struct servent
*servent
;
82 servent
= getservbyname(service
, proto
);
84 return ntohs(servent
->s_port
);
86 port
= strtol(service
, &cp
, 0);
88 log_Printf(LogWARN
, "ParsePort: %s is not a port name or number.\n",
96 * ICMP Syntax: src eq icmp_message_type
99 ParseIcmp(int argc
, char const *const *argv
, struct filterent
*tgt
)
106 /* permit/deny all ICMP types */
107 tgt
->f_srcop
= tgt
->f_dstop
= OP_NONE
;
111 if (!strcmp(*argv
, "src") && !strcmp(argv
[1], "eq")) {
112 type
= strtol(argv
[2], &cp
, 0);
114 log_Printf(LogWARN
, "ParseIcmp: type is expected.\n");
117 tgt
->f_srcop
= OP_EQ
;
118 tgt
->f_srcport
= type
;
119 tgt
->f_dstop
= OP_NONE
;
124 log_Printf(LogWARN
, "ParseIcmp: bad icmp syntax.\n");
131 * UDP Syntax: [src op port] [dst op port]
134 ParseUdpOrTcp(int argc
, char const *const *argv
, const struct protoent
*pe
,
135 struct filterent
*tgt
)
137 tgt
->f_srcop
= tgt
->f_dstop
= OP_NONE
;
138 tgt
->f_estab
= tgt
->f_syn
= tgt
->f_finrst
= 0;
140 if (argc
>= 3 && !strcmp(*argv
, "src")) {
141 tgt
->f_srcop
= filter_Nam2Op(argv
[1]);
142 if (tgt
->f_srcop
== OP_NONE
) {
143 log_Printf(LogWARN
, "ParseUdpOrTcp: bad operator\n");
148 tgt
->f_srcport
= ParsePort(argv
[2], pe
->p_name
);
149 if (tgt
->f_srcport
== 0)
155 if (argc
>= 3 && !strcmp(argv
[0], "dst")) {
156 tgt
->f_dstop
= filter_Nam2Op(argv
[1]);
157 if (tgt
->f_dstop
== OP_NONE
) {
158 log_Printf(LogWARN
, "ParseUdpOrTcp: bad operator\n");
163 tgt
->f_dstport
= ParsePort(argv
[2], pe
->p_name
);
164 if (tgt
->f_dstport
== 0)
170 if (pe
&& pe
->p_proto
== IPPROTO_TCP
) {
171 for (; argc
> 0; argc
--, argv
++)
172 if (!strcmp(*argv
, "estab"))
174 else if (!strcmp(*argv
, "syn"))
176 else if (!strcmp(*argv
, "finrst"))
183 log_Printf(LogWARN
, "ParseUdpOrTcp: bad src/dst port syntax: %s\n", *argv
);
191 ParseGeneric(int argc
, struct filterent
*tgt
)
194 * Filter currently is a catch-all. Requests are either permitted or
198 log_Printf(LogWARN
, "ParseGeneric: Too many parameters\n");
201 tgt
->f_srcop
= tgt
->f_dstop
= OP_NONE
;
207 addrtype(const char *addr
)
209 if (!strncasecmp(addr
, "MYADDR", 6) && (addr
[6] == '\0' || addr
[6] == '/'))
211 if (!strncasecmp(addr
, "MYADDR6", 7) && (addr
[7] == '\0' || addr
[7] == '/'))
213 if (!strncasecmp(addr
, "HISADDR", 7) && (addr
[7] == '\0' || addr
[7] == '/'))
215 if (!strncasecmp(addr
, "HISADDR6", 8) && (addr
[8] == '\0' || addr
[8] == '/'))
217 if (!strncasecmp(addr
, "DNS0", 4) && (addr
[4] == '\0' || addr
[4] == '/'))
219 if (!strncasecmp(addr
, "DNS1", 4) && (addr
[4] == '\0' || addr
[4] == '/'))
226 addrstr(struct ncprange
*addr
, unsigned type
)
238 return ncprange_ntoa(addr
);
242 filter_Parse(struct ncp
*ncp
, int argc
, char const *const *argv
,
243 struct filterent
*ofp
)
248 int action
, family
, ruleno
, val
, width
;
250 ruleno
= strtol(*argv
, &wp
, 0);
251 if (*argv
== wp
|| ruleno
>= MAXFILTERS
) {
252 log_Printf(LogWARN
, "Parse: invalid filter number.\n");
256 for (ruleno
= 0; ruleno
< MAXFILTERS
; ruleno
++) {
257 ofp
->f_action
= A_NONE
;
260 log_Printf(LogWARN
, "Parse: filter cleared.\n");
266 log_Printf(LogWARN
, "Parse: missing action.\n");
271 memset(&fe
, '\0', sizeof fe
);
273 val
= strtol(*argv
, &wp
, 0);
274 if (!*wp
&& val
>= 0 && val
< MAXFILTERS
) {
276 log_Printf(LogWARN
, "Parse: Can only jump forward from rule %d\n",
281 } else if (!strcmp(*argv
, "permit")) {
283 } else if (!strcmp(*argv
, "deny")) {
285 } else if (!strcmp(*argv
, "clear")) {
286 ofp
->f_action
= A_NONE
;
289 log_Printf(LogWARN
, "Parse: %s: bad action\n", *argv
);
292 fe
.f_action
= action
;
297 if (argc
&& argv
[0][0] == '!' && !argv
[0][1]) {
303 ncprange_init(&fe
.f_src
);
304 ncprange_init(&fe
.f_dst
);
308 else if ((pe
= getprotobyname(*argv
)) == NULL
&& strcmp(*argv
, "all") != 0) {
310 log_Printf(LogWARN
, "Parse: Protocol or address pair expected\n");
312 } else if (strcasecmp(*argv
, "any") == 0 ||
313 ncprange_aton(&fe
.f_src
, ncp
, *argv
)) {
314 family
= ncprange_family(&fe
.f_src
);
315 if (!ncprange_getwidth(&fe
.f_src
, &width
))
318 ncprange_init(&fe
.f_src
);
319 fe
.f_srctype
= addrtype(*argv
);
323 if (strcasecmp(*argv
, "any") == 0 ||
324 ncprange_aton(&fe
.f_dst
, ncp
, *argv
)) {
325 if (ncprange_family(&fe
.f_dst
) != AF_UNSPEC
&&
326 ncprange_family(&fe
.f_src
) != AF_UNSPEC
&&
327 family
!= ncprange_family(&fe
.f_dst
)) {
328 log_Printf(LogWARN
, "Parse: src and dst address families differ\n");
331 if (!ncprange_getwidth(&fe
.f_dst
, &width
))
334 ncprange_init(&fe
.f_dst
);
335 fe
.f_dsttype
= addrtype(*argv
);
339 log_Printf(LogWARN
, "Parse: Protocol or address pair expected\n");
344 if ((pe
= getprotobyname(*argv
)) == NULL
&& strcmp(*argv
, "all") != 0) {
345 log_Printf(LogWARN
, "Parse: %s: Protocol expected\n", *argv
);
353 log_Printf(LogWARN
, "Parse: Protocol or address pair expected\n");
361 if (argc
>= 2 && strcmp(*argv
, "timeout") == 0) {
362 fe
.timeout
= strtoul(argv
[1], NULL
, 10);
368 fe
.f_proto
= (pe
== NULL
) ? 0 : pe
->p_proto
;
370 switch (fe
.f_proto
) {
377 val
= ParseUdpOrTcp(argc
, argv
, pe
, &fe
);
383 val
= ParseIcmp(argc
, argv
, &fe
);
386 val
= ParseGeneric(argc
, &fe
);
390 log_Printf(LogDEBUG
, "Parse: Src: %s\n", ncprange_ntoa(&fe
.f_src
));
391 log_Printf(LogDEBUG
, "Parse: Dst: %s\n", ncprange_ntoa(&fe
.f_dst
));
392 log_Printf(LogDEBUG
, "Parse: Proto: %d\n", fe
.f_proto
);
394 log_Printf(LogDEBUG
, "Parse: src: %s (%d)\n",
395 filter_Op2Nam(fe
.f_srcop
), fe
.f_srcport
);
396 log_Printf(LogDEBUG
, "Parse: dst: %s (%d)\n",
397 filter_Op2Nam(fe
.f_dstop
), fe
.f_dstport
);
398 log_Printf(LogDEBUG
, "Parse: estab: %u\n", fe
.f_estab
);
399 log_Printf(LogDEBUG
, "Parse: syn: %u\n", fe
.f_syn
);
400 log_Printf(LogDEBUG
, "Parse: finrst: %u\n", fe
.f_finrst
);
409 filter_Set(struct cmdargs
const *arg
)
411 struct filter
*filter
;
413 if (arg
->argc
< arg
->argn
+2)
416 if (!strcmp(arg
->argv
[arg
->argn
], "in"))
417 filter
= &arg
->bundle
->filter
.in
;
418 else if (!strcmp(arg
->argv
[arg
->argn
], "out"))
419 filter
= &arg
->bundle
->filter
.out
;
420 else if (!strcmp(arg
->argv
[arg
->argn
], "dial"))
421 filter
= &arg
->bundle
->filter
.dial
;
422 else if (!strcmp(arg
->argv
[arg
->argn
], "alive"))
423 filter
= &arg
->bundle
->filter
.alive
;
425 log_Printf(LogWARN
, "filter_Set: %s: Invalid filter name.\n",
426 arg
->argv
[arg
->argn
]);
430 filter_Parse(&arg
->bundle
->ncp
, arg
->argc
- arg
->argn
- 1,
431 arg
->argv
+ arg
->argn
+ 1, filter
->rule
);
436 filter_Action2Nam(unsigned act
)
438 static const char * const actname
[] = { " none ", "permit ", " deny " };
441 if (act
< MAXFILTERS
) {
442 snprintf(buf
, sizeof buf
, "%6d ", act
);
444 } else if (act
>= A_NONE
&& act
< A_NONE
+ sizeof(actname
)/sizeof(char *))
445 return actname
[act
- A_NONE
];
451 doShowFilter(struct filterent
*fp
, struct prompt
*prompt
)
456 for (n
= 0; n
< MAXFILTERS
; n
++, fp
++) {
457 if (fp
->f_action
!= A_NONE
) {
458 prompt_Printf(prompt
, " %2d %s", n
, filter_Action2Nam(fp
->f_action
));
459 prompt_Printf(prompt
, "%c ", fp
->f_invert
? '!' : ' ');
461 if (ncprange_isset(&fp
->f_src
))
462 prompt_Printf(prompt
, "%s ", addrstr(&fp
->f_src
, fp
->f_srctype
));
464 prompt_Printf(prompt
, "any ");
466 if (ncprange_isset(&fp
->f_dst
))
467 prompt_Printf(prompt
, "%s ", addrstr(&fp
->f_dst
, fp
->f_dsttype
));
469 prompt_Printf(prompt
, "any ");
472 if ((pe
= getprotobynumber(fp
->f_proto
)) == NULL
)
473 prompt_Printf(prompt
, "P:%d", fp
->f_proto
);
475 prompt_Printf(prompt
, "%s", pe
->p_name
);
478 prompt_Printf(prompt
, " src %s %d", filter_Op2Nam(fp
->f_srcop
),
481 prompt_Printf(prompt
, " dst %s %d", filter_Op2Nam(fp
->f_dstop
),
484 prompt_Printf(prompt
, " estab");
486 prompt_Printf(prompt
, " syn");
488 prompt_Printf(prompt
, " finrst");
490 prompt_Printf(prompt
, "all");
491 if (fp
->timeout
!= 0)
492 prompt_Printf(prompt
, " timeout %u", fp
->timeout
);
493 prompt_Printf(prompt
, "\n");
499 filter_Show(struct cmdargs
const *arg
)
501 if (arg
->argc
> arg
->argn
+1)
504 if (arg
->argc
== arg
->argn
+1) {
505 struct filter
*filter
;
507 if (!strcmp(arg
->argv
[arg
->argn
], "in"))
508 filter
= &arg
->bundle
->filter
.in
;
509 else if (!strcmp(arg
->argv
[arg
->argn
], "out"))
510 filter
= &arg
->bundle
->filter
.out
;
511 else if (!strcmp(arg
->argv
[arg
->argn
], "dial"))
512 filter
= &arg
->bundle
->filter
.dial
;
513 else if (!strcmp(arg
->argv
[arg
->argn
], "alive"))
514 filter
= &arg
->bundle
->filter
.alive
;
517 doShowFilter(filter
->rule
, arg
->prompt
);
519 struct filter
*filter
[4];
522 filter
[0] = &arg
->bundle
->filter
.in
;
523 filter
[1] = &arg
->bundle
->filter
.out
;
524 filter
[2] = &arg
->bundle
->filter
.dial
;
525 filter
[3] = &arg
->bundle
->filter
.alive
;
526 for (f
= 0; f
< 4; f
++) {
528 prompt_Printf(arg
->prompt
, "\n");
529 prompt_Printf(arg
->prompt
, "%s:\n", filter
[f
]->name
);
530 doShowFilter(filter
[f
]->rule
, arg
->prompt
);
537 static const char * const opname
[] = {"none", "eq", "gt", "lt"};
540 filter_Op2Nam(unsigned op
)
542 if (op
>= sizeof opname
/ sizeof opname
[0])
549 filter_Nam2Op(const char *cp
)
553 for (op
= sizeof opname
/ sizeof opname
[0] - 1; op
; op
--)
554 if (!strcasecmp(cp
, opname
[op
]))
561 filter_AdjustAddr(struct filter
*filter
, struct ncpaddr
*local
,
562 struct ncpaddr
*remote
, struct in_addr
*dns
)
564 struct filterent
*fp
;
567 for (fp
= filter
->rule
, n
= 0; n
< MAXFILTERS
; fp
++, n
++)
568 if (fp
->f_action
!= A_NONE
) {
570 if (fp
->f_srctype
== T_MYADDR
&& ncpaddr_family(local
) == AF_INET
)
571 ncprange_sethost(&fp
->f_src
, local
);
572 if (fp
->f_dsttype
== T_MYADDR
&& ncpaddr_family(local
) == AF_INET
)
573 ncprange_sethost(&fp
->f_dst
, local
);
575 if (fp
->f_srctype
== T_MYADDR6
&& ncpaddr_family(local
) == AF_INET6
)
576 ncprange_sethost(&fp
->f_src
, local
);
577 if (fp
->f_dsttype
== T_MYADDR6
&& ncpaddr_family(local
) == AF_INET6
)
578 ncprange_sethost(&fp
->f_dst
, local
);
582 if (fp
->f_srctype
== T_HISADDR
&& ncpaddr_family(remote
) == AF_INET
)
583 ncprange_sethost(&fp
->f_src
, remote
);
584 if (fp
->f_dsttype
== T_HISADDR
&& ncpaddr_family(remote
) == AF_INET
)
585 ncprange_sethost(&fp
->f_dst
, remote
);
587 if (fp
->f_srctype
== T_HISADDR6
&& ncpaddr_family(remote
) == AF_INET6
)
588 ncprange_sethost(&fp
->f_src
, remote
);
589 if (fp
->f_dsttype
== T_HISADDR6
&& ncpaddr_family(remote
) == AF_INET6
)
590 ncprange_sethost(&fp
->f_dst
, remote
);
594 if (fp
->f_srctype
== T_DNS0
)
595 ncprange_setip4host(&fp
->f_src
, dns
[0]);
596 if (fp
->f_dsttype
== T_DNS0
)
597 ncprange_setip4host(&fp
->f_dst
, dns
[0]);
598 if (fp
->f_srctype
== T_DNS1
)
599 ncprange_setip4host(&fp
->f_src
, dns
[1]);
600 if (fp
->f_dsttype
== T_DNS1
)
601 ncprange_setip4host(&fp
->f_dst
, dns
[1]);