xref: /freebsd/sbin/pfctl/pf_print_state.c (revision a1ce87eced929a5d7c5fbb439ef267acf878d6b8)
13b3a8eb9SGleb Smirnoff /*	$OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david Exp $	*/
23b3a8eb9SGleb Smirnoff 
33b3a8eb9SGleb Smirnoff /*
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
53b3a8eb9SGleb Smirnoff  * All rights reserved.
63b3a8eb9SGleb Smirnoff  *
73b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
83b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
93b3a8eb9SGleb Smirnoff  * are met:
103b3a8eb9SGleb Smirnoff  *
113b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
123b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
133b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
143b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
153b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
163b3a8eb9SGleb Smirnoff  *      with the distribution.
173b3a8eb9SGleb Smirnoff  *
183b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
213b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
223b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
233b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
243b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
253b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
263b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
283b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
303b3a8eb9SGleb Smirnoff  *
313b3a8eb9SGleb Smirnoff  */
323b3a8eb9SGleb Smirnoff 
333b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
343b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
353b3a8eb9SGleb Smirnoff 
363b3a8eb9SGleb Smirnoff #include <sys/types.h>
373b3a8eb9SGleb Smirnoff #include <sys/socket.h>
383b3a8eb9SGleb Smirnoff #include <sys/endian.h>
393b3a8eb9SGleb Smirnoff #include <net/if.h>
403b3a8eb9SGleb Smirnoff #define TCPSTATES
413b3a8eb9SGleb Smirnoff #include <netinet/tcp_fsm.h>
423b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
433b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
443b3a8eb9SGleb Smirnoff #include <netdb.h>
453b3a8eb9SGleb Smirnoff 
46*a1ce87ecSGleb Smirnoff #include <stdint.h>
473b3a8eb9SGleb Smirnoff #include <stdio.h>
483b3a8eb9SGleb Smirnoff #include <string.h>
493b3a8eb9SGleb Smirnoff 
503b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
513b3a8eb9SGleb Smirnoff #include "pfctl.h"
523b3a8eb9SGleb Smirnoff 
533b3a8eb9SGleb Smirnoff void	print_name(struct pf_addr *, sa_family_t);
543b3a8eb9SGleb Smirnoff 
553b3a8eb9SGleb Smirnoff void
563b3a8eb9SGleb Smirnoff print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose)
573b3a8eb9SGleb Smirnoff {
583b3a8eb9SGleb Smirnoff 	switch (addr->type) {
593b3a8eb9SGleb Smirnoff 	case PF_ADDR_DYNIFTL:
603b3a8eb9SGleb Smirnoff 		printf("(%s", addr->v.ifname);
613b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_NETWORK)
623b3a8eb9SGleb Smirnoff 			printf(":network");
633b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_BROADCAST)
643b3a8eb9SGleb Smirnoff 			printf(":broadcast");
653b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_PEER)
663b3a8eb9SGleb Smirnoff 			printf(":peer");
673b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_NOALIAS)
683b3a8eb9SGleb Smirnoff 			printf(":0");
693b3a8eb9SGleb Smirnoff 		if (verbose) {
703b3a8eb9SGleb Smirnoff 			if (addr->p.dyncnt <= 0)
713b3a8eb9SGleb Smirnoff 				printf(":*");
723b3a8eb9SGleb Smirnoff 			else
733b3a8eb9SGleb Smirnoff 				printf(":%d", addr->p.dyncnt);
743b3a8eb9SGleb Smirnoff 		}
753b3a8eb9SGleb Smirnoff 		printf(")");
763b3a8eb9SGleb Smirnoff 		break;
773b3a8eb9SGleb Smirnoff 	case PF_ADDR_TABLE:
783b3a8eb9SGleb Smirnoff 		if (verbose)
793b3a8eb9SGleb Smirnoff 			if (addr->p.tblcnt == -1)
803b3a8eb9SGleb Smirnoff 				printf("<%s:*>", addr->v.tblname);
813b3a8eb9SGleb Smirnoff 			else
823b3a8eb9SGleb Smirnoff 				printf("<%s:%d>", addr->v.tblname,
833b3a8eb9SGleb Smirnoff 				    addr->p.tblcnt);
843b3a8eb9SGleb Smirnoff 		else
853b3a8eb9SGleb Smirnoff 			printf("<%s>", addr->v.tblname);
863b3a8eb9SGleb Smirnoff 		return;
873b3a8eb9SGleb Smirnoff 	case PF_ADDR_RANGE: {
883b3a8eb9SGleb Smirnoff 		char buf[48];
893b3a8eb9SGleb Smirnoff 
903b3a8eb9SGleb Smirnoff 		if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL)
913b3a8eb9SGleb Smirnoff 			printf("?");
923b3a8eb9SGleb Smirnoff 		else
933b3a8eb9SGleb Smirnoff 			printf("%s", buf);
943b3a8eb9SGleb Smirnoff 		if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL)
953b3a8eb9SGleb Smirnoff 			printf(" - ?");
963b3a8eb9SGleb Smirnoff 		else
973b3a8eb9SGleb Smirnoff 			printf(" - %s", buf);
983b3a8eb9SGleb Smirnoff 		break;
993b3a8eb9SGleb Smirnoff 	}
1003b3a8eb9SGleb Smirnoff 	case PF_ADDR_ADDRMASK:
1013b3a8eb9SGleb Smirnoff 		if (PF_AZERO(&addr->v.a.addr, AF_INET6) &&
1023b3a8eb9SGleb Smirnoff 		    PF_AZERO(&addr->v.a.mask, AF_INET6))
1033b3a8eb9SGleb Smirnoff 			printf("any");
1043b3a8eb9SGleb Smirnoff 		else {
1053b3a8eb9SGleb Smirnoff 			char buf[48];
1063b3a8eb9SGleb Smirnoff 
1073b3a8eb9SGleb Smirnoff 			if (inet_ntop(af, &addr->v.a.addr, buf,
1083b3a8eb9SGleb Smirnoff 			    sizeof(buf)) == NULL)
1093b3a8eb9SGleb Smirnoff 				printf("?");
1103b3a8eb9SGleb Smirnoff 			else
1113b3a8eb9SGleb Smirnoff 				printf("%s", buf);
1123b3a8eb9SGleb Smirnoff 		}
1133b3a8eb9SGleb Smirnoff 		break;
1143b3a8eb9SGleb Smirnoff 	case PF_ADDR_NOROUTE:
1153b3a8eb9SGleb Smirnoff 		printf("no-route");
1163b3a8eb9SGleb Smirnoff 		return;
1173b3a8eb9SGleb Smirnoff 	case PF_ADDR_URPFFAILED:
1183b3a8eb9SGleb Smirnoff 		printf("urpf-failed");
1193b3a8eb9SGleb Smirnoff 		return;
1203b3a8eb9SGleb Smirnoff 	default:
1213b3a8eb9SGleb Smirnoff 		printf("?");
1223b3a8eb9SGleb Smirnoff 		return;
1233b3a8eb9SGleb Smirnoff 	}
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff 	/* mask if not _both_ address and mask are zero */
1263b3a8eb9SGleb Smirnoff 	if (addr->type != PF_ADDR_RANGE &&
1273b3a8eb9SGleb Smirnoff 	    !(PF_AZERO(&addr->v.a.addr, AF_INET6) &&
1283b3a8eb9SGleb Smirnoff 	    PF_AZERO(&addr->v.a.mask, AF_INET6))) {
1293b3a8eb9SGleb Smirnoff 		int bits = unmask(&addr->v.a.mask, af);
1303b3a8eb9SGleb Smirnoff 
1313b3a8eb9SGleb Smirnoff 		if (bits != (af == AF_INET ? 32 : 128))
1323b3a8eb9SGleb Smirnoff 			printf("/%d", bits);
1333b3a8eb9SGleb Smirnoff 	}
1343b3a8eb9SGleb Smirnoff }
1353b3a8eb9SGleb Smirnoff 
1363b3a8eb9SGleb Smirnoff void
1373b3a8eb9SGleb Smirnoff print_name(struct pf_addr *addr, sa_family_t af)
1383b3a8eb9SGleb Smirnoff {
1393b3a8eb9SGleb Smirnoff 	char host[NI_MAXHOST];
1403b3a8eb9SGleb Smirnoff 
1413b3a8eb9SGleb Smirnoff 	strlcpy(host, "?", sizeof(host));
1423b3a8eb9SGleb Smirnoff 	switch (af) {
1433b3a8eb9SGleb Smirnoff 	case AF_INET: {
1443b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
1453b3a8eb9SGleb Smirnoff 
1463b3a8eb9SGleb Smirnoff 		memset(&sin, 0, sizeof(sin));
1473b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
1483b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
1493b3a8eb9SGleb Smirnoff 		sin.sin_addr = addr->v4;
1503b3a8eb9SGleb Smirnoff 		getnameinfo((struct sockaddr *)&sin, sin.sin_len,
1513b3a8eb9SGleb Smirnoff 		    host, sizeof(host), NULL, 0, NI_NOFQDN);
1523b3a8eb9SGleb Smirnoff 		break;
1533b3a8eb9SGleb Smirnoff 	}
1543b3a8eb9SGleb Smirnoff 	case AF_INET6: {
1553b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
1563b3a8eb9SGleb Smirnoff 
1573b3a8eb9SGleb Smirnoff 		memset(&sin6, 0, sizeof(sin6));
1583b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
1593b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
1603b3a8eb9SGleb Smirnoff 		sin6.sin6_addr = addr->v6;
1613b3a8eb9SGleb Smirnoff 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1623b3a8eb9SGleb Smirnoff 		    host, sizeof(host), NULL, 0, NI_NOFQDN);
1633b3a8eb9SGleb Smirnoff 		break;
1643b3a8eb9SGleb Smirnoff 	}
1653b3a8eb9SGleb Smirnoff 	}
1663b3a8eb9SGleb Smirnoff 	printf("%s", host);
1673b3a8eb9SGleb Smirnoff }
1683b3a8eb9SGleb Smirnoff 
1693b3a8eb9SGleb Smirnoff void
1703b3a8eb9SGleb Smirnoff print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts)
1713b3a8eb9SGleb Smirnoff {
1723b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_USEDNS)
1733b3a8eb9SGleb Smirnoff 		print_name(addr, af);
1743b3a8eb9SGleb Smirnoff 	else {
1753b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap aw;
1763b3a8eb9SGleb Smirnoff 
1773b3a8eb9SGleb Smirnoff 		memset(&aw, 0, sizeof(aw));
1783b3a8eb9SGleb Smirnoff 		aw.v.a.addr = *addr;
1793b3a8eb9SGleb Smirnoff 		if (af == AF_INET)
1803b3a8eb9SGleb Smirnoff 			aw.v.a.mask.addr32[0] = 0xffffffff;
1813b3a8eb9SGleb Smirnoff 		else {
1823b3a8eb9SGleb Smirnoff 			memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
1833b3a8eb9SGleb Smirnoff 			af = AF_INET6;
1843b3a8eb9SGleb Smirnoff 		}
1853b3a8eb9SGleb Smirnoff 		print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
1863b3a8eb9SGleb Smirnoff 	}
1873b3a8eb9SGleb Smirnoff 
1883b3a8eb9SGleb Smirnoff 	if (port) {
1893b3a8eb9SGleb Smirnoff 		if (af == AF_INET)
1903b3a8eb9SGleb Smirnoff 			printf(":%u", ntohs(port));
1913b3a8eb9SGleb Smirnoff 		else
1923b3a8eb9SGleb Smirnoff 			printf("[%u]", ntohs(port));
1933b3a8eb9SGleb Smirnoff 	}
1943b3a8eb9SGleb Smirnoff }
1953b3a8eb9SGleb Smirnoff 
1963b3a8eb9SGleb Smirnoff void
1973b3a8eb9SGleb Smirnoff print_seq(struct pfsync_state_peer *p)
1983b3a8eb9SGleb Smirnoff {
1993b3a8eb9SGleb Smirnoff 	if (p->seqdiff)
2003b3a8eb9SGleb Smirnoff 		printf("[%u + %u](+%u)", ntohl(p->seqlo),
2013b3a8eb9SGleb Smirnoff 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
2023b3a8eb9SGleb Smirnoff 	else
2033b3a8eb9SGleb Smirnoff 		printf("[%u + %u]", ntohl(p->seqlo),
2043b3a8eb9SGleb Smirnoff 		    ntohl(p->seqhi) - ntohl(p->seqlo));
2053b3a8eb9SGleb Smirnoff }
2063b3a8eb9SGleb Smirnoff 
2073b3a8eb9SGleb Smirnoff void
2083b3a8eb9SGleb Smirnoff print_state(struct pfsync_state *s, int opts)
2093b3a8eb9SGleb Smirnoff {
2103b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer *src, *dst;
2113b3a8eb9SGleb Smirnoff 	struct pfsync_state_key *sk, *nk;
2123b3a8eb9SGleb Smirnoff 	struct protoent *p;
2133b3a8eb9SGleb Smirnoff 	int min, sec;
2143b3a8eb9SGleb Smirnoff 
2153b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT) {
2163b3a8eb9SGleb Smirnoff 		src = &s->src;
2173b3a8eb9SGleb Smirnoff 		dst = &s->dst;
2183b3a8eb9SGleb Smirnoff 		sk = &s->key[PF_SK_STACK];
2193b3a8eb9SGleb Smirnoff 		nk = &s->key[PF_SK_WIRE];
2203b3a8eb9SGleb Smirnoff 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
2213b3a8eb9SGleb Smirnoff 			sk->port[0] = nk->port[0];
2223b3a8eb9SGleb Smirnoff 	} else {
2233b3a8eb9SGleb Smirnoff 		src = &s->dst;
2243b3a8eb9SGleb Smirnoff 		dst = &s->src;
2253b3a8eb9SGleb Smirnoff 		sk = &s->key[PF_SK_WIRE];
2263b3a8eb9SGleb Smirnoff 		nk = &s->key[PF_SK_STACK];
2273b3a8eb9SGleb Smirnoff 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
2283b3a8eb9SGleb Smirnoff 			sk->port[1] = nk->port[1];
2293b3a8eb9SGleb Smirnoff 	}
2303b3a8eb9SGleb Smirnoff 	printf("%s ", s->ifname);
2313b3a8eb9SGleb Smirnoff 	if ((p = getprotobynumber(s->proto)) != NULL)
2323b3a8eb9SGleb Smirnoff 		printf("%s ", p->p_name);
2333b3a8eb9SGleb Smirnoff 	else
2343b3a8eb9SGleb Smirnoff 		printf("%u ", s->proto);
2353b3a8eb9SGleb Smirnoff 
2363b3a8eb9SGleb Smirnoff 	print_host(&nk->addr[1], nk->port[1], s->af, opts);
2373b3a8eb9SGleb Smirnoff 	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
2383b3a8eb9SGleb Smirnoff 	    nk->port[1] != sk->port[1]) {
2393b3a8eb9SGleb Smirnoff 		printf(" (");
2403b3a8eb9SGleb Smirnoff 		print_host(&sk->addr[1], sk->port[1], s->af, opts);
2413b3a8eb9SGleb Smirnoff 		printf(")");
2423b3a8eb9SGleb Smirnoff 	}
2433b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT)
2443b3a8eb9SGleb Smirnoff 		printf(" -> ");
2453b3a8eb9SGleb Smirnoff 	else
2463b3a8eb9SGleb Smirnoff 		printf(" <- ");
2473b3a8eb9SGleb Smirnoff 	print_host(&nk->addr[0], nk->port[0], s->af, opts);
2483b3a8eb9SGleb Smirnoff 	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
2493b3a8eb9SGleb Smirnoff 	    nk->port[0] != sk->port[0]) {
2503b3a8eb9SGleb Smirnoff 		printf(" (");
2513b3a8eb9SGleb Smirnoff 		print_host(&sk->addr[0], sk->port[0], s->af, opts);
2523b3a8eb9SGleb Smirnoff 		printf(")");
2533b3a8eb9SGleb Smirnoff 	}
2543b3a8eb9SGleb Smirnoff 
2553b3a8eb9SGleb Smirnoff 	printf("    ");
2563b3a8eb9SGleb Smirnoff 	if (s->proto == IPPROTO_TCP) {
2573b3a8eb9SGleb Smirnoff 		if (src->state <= TCPS_TIME_WAIT &&
2583b3a8eb9SGleb Smirnoff 		    dst->state <= TCPS_TIME_WAIT)
2593b3a8eb9SGleb Smirnoff 			printf("   %s:%s\n", tcpstates[src->state],
2603b3a8eb9SGleb Smirnoff 			    tcpstates[dst->state]);
2613b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_SRC ||
2623b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_SRC)
2633b3a8eb9SGleb Smirnoff 			printf("   PROXY:SRC\n");
2643b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_DST ||
2653b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_DST)
2663b3a8eb9SGleb Smirnoff 			printf("   PROXY:DST\n");
2673b3a8eb9SGleb Smirnoff 		else
2683b3a8eb9SGleb Smirnoff 			printf("   <BAD STATE LEVELS %u:%u>\n",
2693b3a8eb9SGleb Smirnoff 			    src->state, dst->state);
2703b3a8eb9SGleb Smirnoff 		if (opts & PF_OPT_VERBOSE) {
2713b3a8eb9SGleb Smirnoff 			printf("   ");
2723b3a8eb9SGleb Smirnoff 			print_seq(src);
2733b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
2743b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
2753b3a8eb9SGleb Smirnoff 				    src->wscale & PF_WSCALE_MASK);
2763b3a8eb9SGleb Smirnoff 			printf("  ");
2773b3a8eb9SGleb Smirnoff 			print_seq(dst);
2783b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
2793b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
2803b3a8eb9SGleb Smirnoff 				    dst->wscale & PF_WSCALE_MASK);
2813b3a8eb9SGleb Smirnoff 			printf("\n");
2823b3a8eb9SGleb Smirnoff 		}
2833b3a8eb9SGleb Smirnoff 	} else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
2843b3a8eb9SGleb Smirnoff 	    dst->state < PFUDPS_NSTATES) {
2853b3a8eb9SGleb Smirnoff 		const char *states[] = PFUDPS_NAMES;
2863b3a8eb9SGleb Smirnoff 
2873b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
2883b3a8eb9SGleb Smirnoff 	} else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
2893b3a8eb9SGleb Smirnoff 	    dst->state < PFOTHERS_NSTATES) {
2903b3a8eb9SGleb Smirnoff 		/* XXX ICMP doesn't really have state levels */
2913b3a8eb9SGleb Smirnoff 		const char *states[] = PFOTHERS_NAMES;
2923b3a8eb9SGleb Smirnoff 
2933b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
2943b3a8eb9SGleb Smirnoff 	} else {
2953b3a8eb9SGleb Smirnoff 		printf("   %u:%u\n", src->state, dst->state);
2963b3a8eb9SGleb Smirnoff 	}
2973b3a8eb9SGleb Smirnoff 
2983b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE) {
2993b3a8eb9SGleb Smirnoff 		u_int64_t packets[2];
3003b3a8eb9SGleb Smirnoff 		u_int64_t bytes[2];
3013b3a8eb9SGleb Smirnoff 		u_int32_t creation = ntohl(s->creation);
3023b3a8eb9SGleb Smirnoff 		u_int32_t expire = ntohl(s->expire);
3033b3a8eb9SGleb Smirnoff 
3043b3a8eb9SGleb Smirnoff 		sec = creation % 60;
3053b3a8eb9SGleb Smirnoff 		creation /= 60;
3063b3a8eb9SGleb Smirnoff 		min = creation % 60;
3073b3a8eb9SGleb Smirnoff 		creation /= 60;
3083b3a8eb9SGleb Smirnoff 		printf("   age %.2u:%.2u:%.2u", creation, min, sec);
3093b3a8eb9SGleb Smirnoff 		sec = expire % 60;
3103b3a8eb9SGleb Smirnoff 		expire /= 60;
3113b3a8eb9SGleb Smirnoff 		min = expire % 60;
3123b3a8eb9SGleb Smirnoff 		expire /= 60;
3133b3a8eb9SGleb Smirnoff 		printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
3143b3a8eb9SGleb Smirnoff 
3153b3a8eb9SGleb Smirnoff 		bcopy(s->packets[0], &packets[0], sizeof(u_int64_t));
3163b3a8eb9SGleb Smirnoff 		bcopy(s->packets[1], &packets[1], sizeof(u_int64_t));
3173b3a8eb9SGleb Smirnoff 		bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t));
3183b3a8eb9SGleb Smirnoff 		bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t));
319*a1ce87ecSGleb Smirnoff 		printf(", %ju:%ju pkts, %ju:%ju bytes",
320*a1ce87ecSGleb Smirnoff 		    (uintmax_t )be64toh(packets[0]),
321*a1ce87ecSGleb Smirnoff 		    (uintmax_t )be64toh(packets[1]),
322*a1ce87ecSGleb Smirnoff 		    (uintmax_t )be64toh(bytes[0]),
323*a1ce87ecSGleb Smirnoff 		    (uintmax_t )be64toh(bytes[1]));
3243b3a8eb9SGleb Smirnoff 		if (ntohl(s->anchor) != -1)
3253b3a8eb9SGleb Smirnoff 			printf(", anchor %u", ntohl(s->anchor));
3263b3a8eb9SGleb Smirnoff 		if (ntohl(s->rule) != -1)
3273b3a8eb9SGleb Smirnoff 			printf(", rule %u", ntohl(s->rule));
3283b3a8eb9SGleb Smirnoff 		if (s->state_flags & PFSTATE_SLOPPY)
3293b3a8eb9SGleb Smirnoff 			printf(", sloppy");
3303b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_SRCNODE)
3313b3a8eb9SGleb Smirnoff 			printf(", source-track");
3323b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE)
3333b3a8eb9SGleb Smirnoff 			printf(", sticky-address");
3343b3a8eb9SGleb Smirnoff 		printf("\n");
3353b3a8eb9SGleb Smirnoff 	}
3363b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE2) {
3373b3a8eb9SGleb Smirnoff 		u_int64_t id;
3383b3a8eb9SGleb Smirnoff 
3393b3a8eb9SGleb Smirnoff 		bcopy(&s->id, &id, sizeof(u_int64_t));
340*a1ce87ecSGleb Smirnoff 		printf("   id: %016jx creatorid: %08x",
341*a1ce87ecSGleb Smirnoff 		    (uintmax_t )be64toh(id), ntohl(s->creatorid));
3423b3a8eb9SGleb Smirnoff 		printf("\n");
3433b3a8eb9SGleb Smirnoff 	}
3443b3a8eb9SGleb Smirnoff }
3453b3a8eb9SGleb Smirnoff 
3463b3a8eb9SGleb Smirnoff int
3473b3a8eb9SGleb Smirnoff unmask(struct pf_addr *m, sa_family_t af)
3483b3a8eb9SGleb Smirnoff {
3493b3a8eb9SGleb Smirnoff 	int i = 31, j = 0, b = 0;
3503b3a8eb9SGleb Smirnoff 	u_int32_t tmp;
3513b3a8eb9SGleb Smirnoff 
3523b3a8eb9SGleb Smirnoff 	while (j < 4 && m->addr32[j] == 0xffffffff) {
3533b3a8eb9SGleb Smirnoff 		b += 32;
3543b3a8eb9SGleb Smirnoff 		j++;
3553b3a8eb9SGleb Smirnoff 	}
3563b3a8eb9SGleb Smirnoff 	if (j < 4) {
3573b3a8eb9SGleb Smirnoff 		tmp = ntohl(m->addr32[j]);
3583b3a8eb9SGleb Smirnoff 		for (i = 31; tmp & (1 << i); --i)
3593b3a8eb9SGleb Smirnoff 			b++;
3603b3a8eb9SGleb Smirnoff 	}
3613b3a8eb9SGleb Smirnoff 	return (b);
3623b3a8eb9SGleb Smirnoff }
363