2009-08-18 16:13:59 +00:00
|
|
|
/* $OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david Exp $ */
|
2004-02-28 16:52:45 +00:00
|
|
|
|
2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
2004-02-28 16:52:45 +00:00
|
|
|
* Copyright (c) 2001 Daniel Hartmeier
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-06-16 23:39:33 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2004-02-28 16:52:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2004-06-16 23:39:33 +00:00
|
|
|
#include <sys/endian.h>
|
2004-02-28 16:52:45 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#define TCPSTATES
|
|
|
|
#include <netinet/tcp_fsm.h>
|
|
|
|
#include <net/pfvar.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2012-09-29 16:42:01 +00:00
|
|
|
#include <stdint.h>
|
2004-02-28 16:52:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "pfctl_parser.h"
|
|
|
|
#include "pfctl.h"
|
|
|
|
|
|
|
|
void print_name(struct pf_addr *, sa_family_t);
|
|
|
|
|
|
|
|
void
|
|
|
|
print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose)
|
|
|
|
{
|
2004-06-16 23:26:00 +00:00
|
|
|
switch (addr->type) {
|
2004-02-28 16:52:45 +00:00
|
|
|
case PF_ADDR_DYNIFTL:
|
2004-06-16 23:26:00 +00:00
|
|
|
printf("(%s", addr->v.ifname);
|
|
|
|
if (addr->iflags & PFI_AFLAG_NETWORK)
|
|
|
|
printf(":network");
|
|
|
|
if (addr->iflags & PFI_AFLAG_BROADCAST)
|
|
|
|
printf(":broadcast");
|
|
|
|
if (addr->iflags & PFI_AFLAG_PEER)
|
|
|
|
printf(":peer");
|
|
|
|
if (addr->iflags & PFI_AFLAG_NOALIAS)
|
|
|
|
printf(":0");
|
|
|
|
if (verbose) {
|
|
|
|
if (addr->p.dyncnt <= 0)
|
|
|
|
printf(":*");
|
|
|
|
else
|
|
|
|
printf(":%d", addr->p.dyncnt);
|
|
|
|
}
|
|
|
|
printf(")");
|
2004-02-28 16:52:45 +00:00
|
|
|
break;
|
|
|
|
case PF_ADDR_TABLE:
|
|
|
|
if (verbose)
|
|
|
|
if (addr->p.tblcnt == -1)
|
|
|
|
printf("<%s:*>", addr->v.tblname);
|
|
|
|
else
|
|
|
|
printf("<%s:%d>", addr->v.tblname,
|
|
|
|
addr->p.tblcnt);
|
|
|
|
else
|
|
|
|
printf("<%s>", addr->v.tblname);
|
|
|
|
return;
|
2008-12-10 20:59:26 +00:00
|
|
|
case PF_ADDR_RANGE: {
|
|
|
|
char buf[48];
|
|
|
|
|
|
|
|
if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL)
|
|
|
|
printf("?");
|
|
|
|
else
|
|
|
|
printf("%s", buf);
|
|
|
|
if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL)
|
|
|
|
printf(" - ?");
|
|
|
|
else
|
|
|
|
printf(" - %s", buf);
|
|
|
|
break;
|
|
|
|
}
|
2004-02-28 16:52:45 +00:00
|
|
|
case PF_ADDR_ADDRMASK:
|
|
|
|
if (PF_AZERO(&addr->v.a.addr, AF_INET6) &&
|
|
|
|
PF_AZERO(&addr->v.a.mask, AF_INET6))
|
|
|
|
printf("any");
|
|
|
|
else {
|
|
|
|
char buf[48];
|
|
|
|
|
|
|
|
if (inet_ntop(af, &addr->v.a.addr, buf,
|
|
|
|
sizeof(buf)) == NULL)
|
|
|
|
printf("?");
|
|
|
|
else
|
|
|
|
printf("%s", buf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PF_ADDR_NOROUTE:
|
|
|
|
printf("no-route");
|
|
|
|
return;
|
2007-07-03 12:30:03 +00:00
|
|
|
case PF_ADDR_URPFFAILED:
|
|
|
|
printf("urpf-failed");
|
|
|
|
return;
|
2004-02-28 16:52:45 +00:00
|
|
|
default:
|
|
|
|
printf("?");
|
|
|
|
return;
|
|
|
|
}
|
2004-06-16 23:26:00 +00:00
|
|
|
|
|
|
|
/* mask if not _both_ address and mask are zero */
|
2008-12-10 20:59:26 +00:00
|
|
|
if (addr->type != PF_ADDR_RANGE &&
|
|
|
|
!(PF_AZERO(&addr->v.a.addr, AF_INET6) &&
|
2004-06-16 23:26:00 +00:00
|
|
|
PF_AZERO(&addr->v.a.mask, AF_INET6))) {
|
2004-02-28 16:52:45 +00:00
|
|
|
int bits = unmask(&addr->v.a.mask, af);
|
|
|
|
|
|
|
|
if (bits != (af == AF_INET ? 32 : 128))
|
|
|
|
printf("/%d", bits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_name(struct pf_addr *addr, sa_family_t af)
|
|
|
|
{
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
|
|
|
|
strlcpy(host, "?", sizeof(host));
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET: {
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr = addr->v4;
|
|
|
|
getnameinfo((struct sockaddr *)&sin, sin.sin_len,
|
|
|
|
host, sizeof(host), NULL, 0, NI_NOFQDN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
sin6.sin6_len = sizeof(sin6);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_addr = addr->v6;
|
|
|
|
getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
|
|
|
|
host, sizeof(host), NULL, 0, NI_NOFQDN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s", host);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-12-10 21:08:42 +00:00
|
|
|
print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts)
|
2004-02-28 16:52:45 +00:00
|
|
|
{
|
|
|
|
if (opts & PF_OPT_USEDNS)
|
2008-12-10 21:08:42 +00:00
|
|
|
print_name(addr, af);
|
2004-02-28 16:52:45 +00:00
|
|
|
else {
|
|
|
|
struct pf_addr_wrap aw;
|
|
|
|
|
|
|
|
memset(&aw, 0, sizeof(aw));
|
2008-12-10 21:08:42 +00:00
|
|
|
aw.v.a.addr = *addr;
|
2004-02-28 16:52:45 +00:00
|
|
|
if (af == AF_INET)
|
|
|
|
aw.v.a.mask.addr32[0] = 0xffffffff;
|
2004-06-16 23:26:00 +00:00
|
|
|
else {
|
2004-02-28 16:52:45 +00:00
|
|
|
memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
|
2004-06-16 23:26:00 +00:00
|
|
|
af = AF_INET6;
|
|
|
|
}
|
2004-02-28 16:52:45 +00:00
|
|
|
print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
|
|
|
|
}
|
|
|
|
|
2008-12-10 21:08:42 +00:00
|
|
|
if (port) {
|
2004-02-28 16:52:45 +00:00
|
|
|
if (af == AF_INET)
|
2008-12-10 21:08:42 +00:00
|
|
|
printf(":%u", ntohs(port));
|
2004-02-28 16:52:45 +00:00
|
|
|
else
|
2008-12-10 21:08:42 +00:00
|
|
|
printf("[%u]", ntohs(port));
|
2004-02-28 16:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-10 14:51:38 +00:00
|
|
|
print_seq(struct pfctl_state_peer *p)
|
2004-02-28 16:52:45 +00:00
|
|
|
{
|
|
|
|
if (p->seqdiff)
|
2021-05-10 14:51:38 +00:00
|
|
|
printf("[%u + %u](+%u)", p->seqlo,
|
|
|
|
p->seqhi - p->seqlo, p->seqdiff);
|
2004-02-28 16:52:45 +00:00
|
|
|
else
|
2021-05-10 14:51:38 +00:00
|
|
|
printf("[%u + %u]", p->seqlo,
|
|
|
|
p->seqhi - p->seqlo);
|
2004-02-28 16:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-10 14:51:38 +00:00
|
|
|
print_state(struct pfctl_state *s, int opts)
|
2004-02-28 16:52:45 +00:00
|
|
|
{
|
2021-05-10 14:51:38 +00:00
|
|
|
struct pfctl_state_peer *src, *dst;
|
|
|
|
struct pfctl_state_key *key, *sk, *nk;
|
2021-07-01 19:25:43 +00:00
|
|
|
const char *protoname;
|
2004-02-28 16:52:45 +00:00
|
|
|
int min, sec;
|
2021-05-10 14:51:38 +00:00
|
|
|
sa_family_t af;
|
|
|
|
uint8_t proto;
|
2016-01-30 22:03:14 +00:00
|
|
|
#ifndef __NO_STRICT_ALIGNMENT
|
2021-05-10 14:51:38 +00:00
|
|
|
struct pfctl_state_key aligned_key[2];
|
2016-01-30 22:03:14 +00:00
|
|
|
|
|
|
|
bcopy(&s->key, aligned_key, sizeof(aligned_key));
|
|
|
|
key = aligned_key;
|
|
|
|
#else
|
|
|
|
key = s->key;
|
|
|
|
#endif
|
2004-02-28 16:52:45 +00:00
|
|
|
|
2021-05-10 14:51:38 +00:00
|
|
|
af = s->key[PF_SK_WIRE].af;
|
|
|
|
proto = s->key[PF_SK_WIRE].proto;
|
|
|
|
|
2004-02-28 16:52:45 +00:00
|
|
|
if (s->direction == PF_OUT) {
|
|
|
|
src = &s->src;
|
|
|
|
dst = &s->dst;
|
2016-01-30 22:03:14 +00:00
|
|
|
sk = &key[PF_SK_STACK];
|
|
|
|
nk = &key[PF_SK_WIRE];
|
2021-05-10 14:51:38 +00:00
|
|
|
if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6)
|
2008-12-10 21:08:42 +00:00
|
|
|
sk->port[0] = nk->port[0];
|
2004-02-28 16:52:45 +00:00
|
|
|
} else {
|
|
|
|
src = &s->dst;
|
|
|
|
dst = &s->src;
|
2016-01-30 22:03:14 +00:00
|
|
|
sk = &key[PF_SK_WIRE];
|
|
|
|
nk = &key[PF_SK_STACK];
|
2021-05-10 14:51:38 +00:00
|
|
|
if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6)
|
2008-12-10 21:08:42 +00:00
|
|
|
sk->port[1] = nk->port[1];
|
2004-02-28 16:52:45 +00:00
|
|
|
}
|
2008-12-10 20:54:37 +00:00
|
|
|
printf("%s ", s->ifname);
|
2021-07-01 19:25:43 +00:00
|
|
|
if ((protoname = pfctl_proto2name(proto)) != NULL)
|
|
|
|
printf("%s ", protoname);
|
2004-02-28 16:52:45 +00:00
|
|
|
else
|
2021-05-10 14:51:38 +00:00
|
|
|
printf("%u ", proto);
|
2008-12-10 21:08:42 +00:00
|
|
|
|
2021-05-10 14:51:38 +00:00
|
|
|
print_host(&nk->addr[1], nk->port[1], af, opts);
|
|
|
|
if (PF_ANEQ(&nk->addr[1], &sk->addr[1], af) ||
|
2008-12-10 21:08:42 +00:00
|
|
|
nk->port[1] != sk->port[1]) {
|
|
|
|
printf(" (");
|
2021-05-10 14:51:38 +00:00
|
|
|
print_host(&sk->addr[1], sk->port[1], af, opts);
|
2008-12-10 21:08:42 +00:00
|
|
|
printf(")");
|
2004-02-28 16:52:45 +00:00
|
|
|
}
|
|
|
|
if (s->direction == PF_OUT)
|
|
|
|
printf(" -> ");
|
|
|
|
else
|
|
|
|
printf(" <- ");
|
2021-05-10 14:51:38 +00:00
|
|
|
print_host(&nk->addr[0], nk->port[0], af, opts);
|
|
|
|
if (PF_ANEQ(&nk->addr[0], &sk->addr[0], af) ||
|
2008-12-10 21:08:42 +00:00
|
|
|
nk->port[0] != sk->port[0]) {
|
|
|
|
printf(" (");
|
2021-05-10 14:51:38 +00:00
|
|
|
print_host(&sk->addr[0], sk->port[0], af, opts);
|
2008-12-10 21:08:42 +00:00
|
|
|
printf(")");
|
|
|
|
}
|
2004-02-28 16:52:45 +00:00
|
|
|
|
|
|
|
printf(" ");
|
2021-05-10 14:51:38 +00:00
|
|
|
if (proto == IPPROTO_TCP) {
|
2004-02-28 16:52:45 +00:00
|
|
|
if (src->state <= TCPS_TIME_WAIT &&
|
|
|
|
dst->state <= TCPS_TIME_WAIT)
|
|
|
|
printf(" %s:%s\n", tcpstates[src->state],
|
|
|
|
tcpstates[dst->state]);
|
|
|
|
else if (src->state == PF_TCPS_PROXY_SRC ||
|
|
|
|
dst->state == PF_TCPS_PROXY_SRC)
|
|
|
|
printf(" PROXY:SRC\n");
|
|
|
|
else if (src->state == PF_TCPS_PROXY_DST ||
|
|
|
|
dst->state == PF_TCPS_PROXY_DST)
|
|
|
|
printf(" PROXY:DST\n");
|
|
|
|
else
|
|
|
|
printf(" <BAD STATE LEVELS %u:%u>\n",
|
|
|
|
src->state, dst->state);
|
|
|
|
if (opts & PF_OPT_VERBOSE) {
|
|
|
|
printf(" ");
|
|
|
|
print_seq(src);
|
|
|
|
if (src->wscale && dst->wscale)
|
|
|
|
printf(" wscale %u",
|
|
|
|
src->wscale & PF_WSCALE_MASK);
|
|
|
|
printf(" ");
|
|
|
|
print_seq(dst);
|
|
|
|
if (src->wscale && dst->wscale)
|
|
|
|
printf(" wscale %u",
|
|
|
|
dst->wscale & PF_WSCALE_MASK);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2021-05-10 14:51:38 +00:00
|
|
|
} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
|
2004-02-28 16:52:45 +00:00
|
|
|
dst->state < PFUDPS_NSTATES) {
|
|
|
|
const char *states[] = PFUDPS_NAMES;
|
|
|
|
|
|
|
|
printf(" %s:%s\n", states[src->state], states[dst->state]);
|
2013-10-27 21:07:37 +00:00
|
|
|
#ifndef INET6
|
2021-05-10 14:51:38 +00:00
|
|
|
} else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
|
2004-02-28 16:52:45 +00:00
|
|
|
dst->state < PFOTHERS_NSTATES) {
|
2013-10-27 21:07:37 +00:00
|
|
|
#else
|
2021-05-10 14:51:38 +00:00
|
|
|
} else if (proto != IPPROTO_ICMP && proto != IPPROTO_ICMPV6 &&
|
2013-10-27 21:07:37 +00:00
|
|
|
src->state < PFOTHERS_NSTATES && dst->state < PFOTHERS_NSTATES) {
|
|
|
|
#endif
|
2004-02-28 16:52:45 +00:00
|
|
|
/* XXX ICMP doesn't really have state levels */
|
|
|
|
const char *states[] = PFOTHERS_NAMES;
|
|
|
|
|
|
|
|
printf(" %s:%s\n", states[src->state], states[dst->state]);
|
|
|
|
} else {
|
|
|
|
printf(" %u:%u\n", src->state, dst->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts & PF_OPT_VERBOSE) {
|
2021-05-10 14:51:38 +00:00
|
|
|
u_int32_t creation = s->creation;
|
|
|
|
u_int32_t expire = s->expire;
|
2008-12-10 21:08:42 +00:00
|
|
|
|
|
|
|
sec = creation % 60;
|
|
|
|
creation /= 60;
|
|
|
|
min = creation % 60;
|
|
|
|
creation /= 60;
|
|
|
|
printf(" age %.2u:%.2u:%.2u", creation, min, sec);
|
|
|
|
sec = expire % 60;
|
|
|
|
expire /= 60;
|
2009-08-18 16:13:59 +00:00
|
|
|
min = expire % 60;
|
2008-12-10 21:08:42 +00:00
|
|
|
expire /= 60;
|
|
|
|
printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
|
|
|
|
|
2012-09-29 16:42:01 +00:00
|
|
|
printf(", %ju:%ju pkts, %ju:%ju bytes",
|
2021-05-10 14:51:38 +00:00
|
|
|
s->packets[0],
|
|
|
|
s->packets[1],
|
|
|
|
s->bytes[0],
|
|
|
|
s->bytes[1]);
|
|
|
|
if (s->anchor != -1)
|
|
|
|
printf(", anchor %u", s->anchor);
|
|
|
|
if (s->rule != -1)
|
|
|
|
printf(", rule %u", s->rule);
|
2009-12-24 00:43:44 +00:00
|
|
|
if (s->state_flags & PFSTATE_SLOPPY)
|
|
|
|
printf(", sloppy");
|
2008-12-10 20:54:37 +00:00
|
|
|
if (s->sync_flags & PFSYNC_FLAG_SRCNODE)
|
2004-06-16 23:26:00 +00:00
|
|
|
printf(", source-track");
|
2008-12-10 20:54:37 +00:00
|
|
|
if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE)
|
2004-06-16 23:26:00 +00:00
|
|
|
printf(", sticky-address");
|
2004-02-28 16:52:45 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2004-06-16 23:26:00 +00:00
|
|
|
if (opts & PF_OPT_VERBOSE2) {
|
2008-12-10 21:08:42 +00:00
|
|
|
u_int64_t id;
|
|
|
|
|
|
|
|
bcopy(&s->id, &id, sizeof(u_int64_t));
|
2021-05-10 14:51:38 +00:00
|
|
|
printf(" id: %016jx creatorid: %08x", id, s->creatorid);
|
2021-05-12 11:24:57 +00:00
|
|
|
printf(" gateway: ");
|
2021-05-10 14:51:38 +00:00
|
|
|
print_host(&s->rt_addr, 0, af, opts);
|
2008-12-10 21:08:42 +00:00
|
|
|
printf("\n");
|
2021-05-12 11:24:57 +00:00
|
|
|
|
|
|
|
if (strcmp(s->ifname, s->orig_ifname) != 0)
|
|
|
|
printf(" origif: %s\n", s->orig_ifname);
|
2004-06-16 23:26:00 +00:00
|
|
|
}
|
2004-02-28 16:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
unmask(struct pf_addr *m, sa_family_t af)
|
|
|
|
{
|
|
|
|
int i = 31, j = 0, b = 0;
|
|
|
|
u_int32_t tmp;
|
|
|
|
|
|
|
|
while (j < 4 && m->addr32[j] == 0xffffffff) {
|
|
|
|
b += 32;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (j < 4) {
|
|
|
|
tmp = ntohl(m->addr32[j]);
|
|
|
|
for (i = 31; tmp & (1 << i); --i)
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
return (b);
|
|
|
|
}
|