xref: /freebsd/sbin/pfctl/pf_print_state.c (revision 3b3a8eb937bf8045231e8364bfd1b94cd4a95979)
1*3b3a8eb9SGleb Smirnoff /*	$OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david Exp $	*/
2*3b3a8eb9SGleb Smirnoff 
3*3b3a8eb9SGleb Smirnoff /*
4*3b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
5*3b3a8eb9SGleb Smirnoff  * All rights reserved.
6*3b3a8eb9SGleb Smirnoff  *
7*3b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8*3b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
9*3b3a8eb9SGleb Smirnoff  * are met:
10*3b3a8eb9SGleb Smirnoff  *
11*3b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
12*3b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
13*3b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
14*3b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
15*3b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
16*3b3a8eb9SGleb Smirnoff  *      with the distribution.
17*3b3a8eb9SGleb Smirnoff  *
18*3b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*3b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*3b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*3b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*3b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*3b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*3b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*3b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26*3b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*3b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28*3b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*3b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
30*3b3a8eb9SGleb Smirnoff  *
31*3b3a8eb9SGleb Smirnoff  */
32*3b3a8eb9SGleb Smirnoff 
33*3b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
34*3b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
35*3b3a8eb9SGleb Smirnoff 
36*3b3a8eb9SGleb Smirnoff #include <sys/types.h>
37*3b3a8eb9SGleb Smirnoff #include <sys/socket.h>
38*3b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
39*3b3a8eb9SGleb Smirnoff #include <sys/endian.h>
40*3b3a8eb9SGleb Smirnoff #define	betoh64	be64toh
41*3b3a8eb9SGleb Smirnoff #endif
42*3b3a8eb9SGleb Smirnoff #include <net/if.h>
43*3b3a8eb9SGleb Smirnoff #define TCPSTATES
44*3b3a8eb9SGleb Smirnoff #include <netinet/tcp_fsm.h>
45*3b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
46*3b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
47*3b3a8eb9SGleb Smirnoff #include <netdb.h>
48*3b3a8eb9SGleb Smirnoff 
49*3b3a8eb9SGleb Smirnoff #include <stdio.h>
50*3b3a8eb9SGleb Smirnoff #include <string.h>
51*3b3a8eb9SGleb Smirnoff 
52*3b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
53*3b3a8eb9SGleb Smirnoff #include "pfctl.h"
54*3b3a8eb9SGleb Smirnoff 
55*3b3a8eb9SGleb Smirnoff void	print_name(struct pf_addr *, sa_family_t);
56*3b3a8eb9SGleb Smirnoff 
57*3b3a8eb9SGleb Smirnoff void
58*3b3a8eb9SGleb Smirnoff print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose)
59*3b3a8eb9SGleb Smirnoff {
60*3b3a8eb9SGleb Smirnoff 	switch (addr->type) {
61*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_DYNIFTL:
62*3b3a8eb9SGleb Smirnoff 		printf("(%s", addr->v.ifname);
63*3b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_NETWORK)
64*3b3a8eb9SGleb Smirnoff 			printf(":network");
65*3b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_BROADCAST)
66*3b3a8eb9SGleb Smirnoff 			printf(":broadcast");
67*3b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_PEER)
68*3b3a8eb9SGleb Smirnoff 			printf(":peer");
69*3b3a8eb9SGleb Smirnoff 		if (addr->iflags & PFI_AFLAG_NOALIAS)
70*3b3a8eb9SGleb Smirnoff 			printf(":0");
71*3b3a8eb9SGleb Smirnoff 		if (verbose) {
72*3b3a8eb9SGleb Smirnoff 			if (addr->p.dyncnt <= 0)
73*3b3a8eb9SGleb Smirnoff 				printf(":*");
74*3b3a8eb9SGleb Smirnoff 			else
75*3b3a8eb9SGleb Smirnoff 				printf(":%d", addr->p.dyncnt);
76*3b3a8eb9SGleb Smirnoff 		}
77*3b3a8eb9SGleb Smirnoff 		printf(")");
78*3b3a8eb9SGleb Smirnoff 		break;
79*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_TABLE:
80*3b3a8eb9SGleb Smirnoff 		if (verbose)
81*3b3a8eb9SGleb Smirnoff 			if (addr->p.tblcnt == -1)
82*3b3a8eb9SGleb Smirnoff 				printf("<%s:*>", addr->v.tblname);
83*3b3a8eb9SGleb Smirnoff 			else
84*3b3a8eb9SGleb Smirnoff 				printf("<%s:%d>", addr->v.tblname,
85*3b3a8eb9SGleb Smirnoff 				    addr->p.tblcnt);
86*3b3a8eb9SGleb Smirnoff 		else
87*3b3a8eb9SGleb Smirnoff 			printf("<%s>", addr->v.tblname);
88*3b3a8eb9SGleb Smirnoff 		return;
89*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_RANGE: {
90*3b3a8eb9SGleb Smirnoff 		char buf[48];
91*3b3a8eb9SGleb Smirnoff 
92*3b3a8eb9SGleb Smirnoff 		if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL)
93*3b3a8eb9SGleb Smirnoff 			printf("?");
94*3b3a8eb9SGleb Smirnoff 		else
95*3b3a8eb9SGleb Smirnoff 			printf("%s", buf);
96*3b3a8eb9SGleb Smirnoff 		if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL)
97*3b3a8eb9SGleb Smirnoff 			printf(" - ?");
98*3b3a8eb9SGleb Smirnoff 		else
99*3b3a8eb9SGleb Smirnoff 			printf(" - %s", buf);
100*3b3a8eb9SGleb Smirnoff 		break;
101*3b3a8eb9SGleb Smirnoff 	}
102*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_ADDRMASK:
103*3b3a8eb9SGleb Smirnoff 		if (PF_AZERO(&addr->v.a.addr, AF_INET6) &&
104*3b3a8eb9SGleb Smirnoff 		    PF_AZERO(&addr->v.a.mask, AF_INET6))
105*3b3a8eb9SGleb Smirnoff 			printf("any");
106*3b3a8eb9SGleb Smirnoff 		else {
107*3b3a8eb9SGleb Smirnoff 			char buf[48];
108*3b3a8eb9SGleb Smirnoff 
109*3b3a8eb9SGleb Smirnoff 			if (inet_ntop(af, &addr->v.a.addr, buf,
110*3b3a8eb9SGleb Smirnoff 			    sizeof(buf)) == NULL)
111*3b3a8eb9SGleb Smirnoff 				printf("?");
112*3b3a8eb9SGleb Smirnoff 			else
113*3b3a8eb9SGleb Smirnoff 				printf("%s", buf);
114*3b3a8eb9SGleb Smirnoff 		}
115*3b3a8eb9SGleb Smirnoff 		break;
116*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_NOROUTE:
117*3b3a8eb9SGleb Smirnoff 		printf("no-route");
118*3b3a8eb9SGleb Smirnoff 		return;
119*3b3a8eb9SGleb Smirnoff 	case PF_ADDR_URPFFAILED:
120*3b3a8eb9SGleb Smirnoff 		printf("urpf-failed");
121*3b3a8eb9SGleb Smirnoff 		return;
122*3b3a8eb9SGleb Smirnoff 	default:
123*3b3a8eb9SGleb Smirnoff 		printf("?");
124*3b3a8eb9SGleb Smirnoff 		return;
125*3b3a8eb9SGleb Smirnoff 	}
126*3b3a8eb9SGleb Smirnoff 
127*3b3a8eb9SGleb Smirnoff 	/* mask if not _both_ address and mask are zero */
128*3b3a8eb9SGleb Smirnoff 	if (addr->type != PF_ADDR_RANGE &&
129*3b3a8eb9SGleb Smirnoff 	    !(PF_AZERO(&addr->v.a.addr, AF_INET6) &&
130*3b3a8eb9SGleb Smirnoff 	    PF_AZERO(&addr->v.a.mask, AF_INET6))) {
131*3b3a8eb9SGleb Smirnoff 		int bits = unmask(&addr->v.a.mask, af);
132*3b3a8eb9SGleb Smirnoff 
133*3b3a8eb9SGleb Smirnoff 		if (bits != (af == AF_INET ? 32 : 128))
134*3b3a8eb9SGleb Smirnoff 			printf("/%d", bits);
135*3b3a8eb9SGleb Smirnoff 	}
136*3b3a8eb9SGleb Smirnoff }
137*3b3a8eb9SGleb Smirnoff 
138*3b3a8eb9SGleb Smirnoff void
139*3b3a8eb9SGleb Smirnoff print_name(struct pf_addr *addr, sa_family_t af)
140*3b3a8eb9SGleb Smirnoff {
141*3b3a8eb9SGleb Smirnoff 	char host[NI_MAXHOST];
142*3b3a8eb9SGleb Smirnoff 
143*3b3a8eb9SGleb Smirnoff 	strlcpy(host, "?", sizeof(host));
144*3b3a8eb9SGleb Smirnoff 	switch (af) {
145*3b3a8eb9SGleb Smirnoff 	case AF_INET: {
146*3b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
147*3b3a8eb9SGleb Smirnoff 
148*3b3a8eb9SGleb Smirnoff 		memset(&sin, 0, sizeof(sin));
149*3b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
150*3b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
151*3b3a8eb9SGleb Smirnoff 		sin.sin_addr = addr->v4;
152*3b3a8eb9SGleb Smirnoff 		getnameinfo((struct sockaddr *)&sin, sin.sin_len,
153*3b3a8eb9SGleb Smirnoff 		    host, sizeof(host), NULL, 0, NI_NOFQDN);
154*3b3a8eb9SGleb Smirnoff 		break;
155*3b3a8eb9SGleb Smirnoff 	}
156*3b3a8eb9SGleb Smirnoff 	case AF_INET6: {
157*3b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
158*3b3a8eb9SGleb Smirnoff 
159*3b3a8eb9SGleb Smirnoff 		memset(&sin6, 0, sizeof(sin6));
160*3b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
161*3b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
162*3b3a8eb9SGleb Smirnoff 		sin6.sin6_addr = addr->v6;
163*3b3a8eb9SGleb Smirnoff 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
164*3b3a8eb9SGleb Smirnoff 		    host, sizeof(host), NULL, 0, NI_NOFQDN);
165*3b3a8eb9SGleb Smirnoff 		break;
166*3b3a8eb9SGleb Smirnoff 	}
167*3b3a8eb9SGleb Smirnoff 	}
168*3b3a8eb9SGleb Smirnoff 	printf("%s", host);
169*3b3a8eb9SGleb Smirnoff }
170*3b3a8eb9SGleb Smirnoff 
171*3b3a8eb9SGleb Smirnoff void
172*3b3a8eb9SGleb Smirnoff print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts)
173*3b3a8eb9SGleb Smirnoff {
174*3b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_USEDNS)
175*3b3a8eb9SGleb Smirnoff 		print_name(addr, af);
176*3b3a8eb9SGleb Smirnoff 	else {
177*3b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap aw;
178*3b3a8eb9SGleb Smirnoff 
179*3b3a8eb9SGleb Smirnoff 		memset(&aw, 0, sizeof(aw));
180*3b3a8eb9SGleb Smirnoff 		aw.v.a.addr = *addr;
181*3b3a8eb9SGleb Smirnoff 		if (af == AF_INET)
182*3b3a8eb9SGleb Smirnoff 			aw.v.a.mask.addr32[0] = 0xffffffff;
183*3b3a8eb9SGleb Smirnoff 		else {
184*3b3a8eb9SGleb Smirnoff 			memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
185*3b3a8eb9SGleb Smirnoff 			af = AF_INET6;
186*3b3a8eb9SGleb Smirnoff 		}
187*3b3a8eb9SGleb Smirnoff 		print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
188*3b3a8eb9SGleb Smirnoff 	}
189*3b3a8eb9SGleb Smirnoff 
190*3b3a8eb9SGleb Smirnoff 	if (port) {
191*3b3a8eb9SGleb Smirnoff 		if (af == AF_INET)
192*3b3a8eb9SGleb Smirnoff 			printf(":%u", ntohs(port));
193*3b3a8eb9SGleb Smirnoff 		else
194*3b3a8eb9SGleb Smirnoff 			printf("[%u]", ntohs(port));
195*3b3a8eb9SGleb Smirnoff 	}
196*3b3a8eb9SGleb Smirnoff }
197*3b3a8eb9SGleb Smirnoff 
198*3b3a8eb9SGleb Smirnoff void
199*3b3a8eb9SGleb Smirnoff print_seq(struct pfsync_state_peer *p)
200*3b3a8eb9SGleb Smirnoff {
201*3b3a8eb9SGleb Smirnoff 	if (p->seqdiff)
202*3b3a8eb9SGleb Smirnoff 		printf("[%u + %u](+%u)", ntohl(p->seqlo),
203*3b3a8eb9SGleb Smirnoff 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
204*3b3a8eb9SGleb Smirnoff 	else
205*3b3a8eb9SGleb Smirnoff 		printf("[%u + %u]", ntohl(p->seqlo),
206*3b3a8eb9SGleb Smirnoff 		    ntohl(p->seqhi) - ntohl(p->seqlo));
207*3b3a8eb9SGleb Smirnoff }
208*3b3a8eb9SGleb Smirnoff 
209*3b3a8eb9SGleb Smirnoff void
210*3b3a8eb9SGleb Smirnoff print_state(struct pfsync_state *s, int opts)
211*3b3a8eb9SGleb Smirnoff {
212*3b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer *src, *dst;
213*3b3a8eb9SGleb Smirnoff 	struct pfsync_state_key *sk, *nk;
214*3b3a8eb9SGleb Smirnoff 	struct protoent *p;
215*3b3a8eb9SGleb Smirnoff 	int min, sec;
216*3b3a8eb9SGleb Smirnoff 
217*3b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT) {
218*3b3a8eb9SGleb Smirnoff 		src = &s->src;
219*3b3a8eb9SGleb Smirnoff 		dst = &s->dst;
220*3b3a8eb9SGleb Smirnoff 		sk = &s->key[PF_SK_STACK];
221*3b3a8eb9SGleb Smirnoff 		nk = &s->key[PF_SK_WIRE];
222*3b3a8eb9SGleb Smirnoff 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
223*3b3a8eb9SGleb Smirnoff 			sk->port[0] = nk->port[0];
224*3b3a8eb9SGleb Smirnoff 	} else {
225*3b3a8eb9SGleb Smirnoff 		src = &s->dst;
226*3b3a8eb9SGleb Smirnoff 		dst = &s->src;
227*3b3a8eb9SGleb Smirnoff 		sk = &s->key[PF_SK_WIRE];
228*3b3a8eb9SGleb Smirnoff 		nk = &s->key[PF_SK_STACK];
229*3b3a8eb9SGleb Smirnoff 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
230*3b3a8eb9SGleb Smirnoff 			sk->port[1] = nk->port[1];
231*3b3a8eb9SGleb Smirnoff 	}
232*3b3a8eb9SGleb Smirnoff 	printf("%s ", s->ifname);
233*3b3a8eb9SGleb Smirnoff 	if ((p = getprotobynumber(s->proto)) != NULL)
234*3b3a8eb9SGleb Smirnoff 		printf("%s ", p->p_name);
235*3b3a8eb9SGleb Smirnoff 	else
236*3b3a8eb9SGleb Smirnoff 		printf("%u ", s->proto);
237*3b3a8eb9SGleb Smirnoff 
238*3b3a8eb9SGleb Smirnoff 	print_host(&nk->addr[1], nk->port[1], s->af, opts);
239*3b3a8eb9SGleb Smirnoff 	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
240*3b3a8eb9SGleb Smirnoff 	    nk->port[1] != sk->port[1]) {
241*3b3a8eb9SGleb Smirnoff 		printf(" (");
242*3b3a8eb9SGleb Smirnoff 		print_host(&sk->addr[1], sk->port[1], s->af, opts);
243*3b3a8eb9SGleb Smirnoff 		printf(")");
244*3b3a8eb9SGleb Smirnoff 	}
245*3b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT)
246*3b3a8eb9SGleb Smirnoff 		printf(" -> ");
247*3b3a8eb9SGleb Smirnoff 	else
248*3b3a8eb9SGleb Smirnoff 		printf(" <- ");
249*3b3a8eb9SGleb Smirnoff 	print_host(&nk->addr[0], nk->port[0], s->af, opts);
250*3b3a8eb9SGleb Smirnoff 	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
251*3b3a8eb9SGleb Smirnoff 	    nk->port[0] != sk->port[0]) {
252*3b3a8eb9SGleb Smirnoff 		printf(" (");
253*3b3a8eb9SGleb Smirnoff 		print_host(&sk->addr[0], sk->port[0], s->af, opts);
254*3b3a8eb9SGleb Smirnoff 		printf(")");
255*3b3a8eb9SGleb Smirnoff 	}
256*3b3a8eb9SGleb Smirnoff 
257*3b3a8eb9SGleb Smirnoff 	printf("    ");
258*3b3a8eb9SGleb Smirnoff 	if (s->proto == IPPROTO_TCP) {
259*3b3a8eb9SGleb Smirnoff 		if (src->state <= TCPS_TIME_WAIT &&
260*3b3a8eb9SGleb Smirnoff 		    dst->state <= TCPS_TIME_WAIT)
261*3b3a8eb9SGleb Smirnoff 			printf("   %s:%s\n", tcpstates[src->state],
262*3b3a8eb9SGleb Smirnoff 			    tcpstates[dst->state]);
263*3b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_SRC ||
264*3b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_SRC)
265*3b3a8eb9SGleb Smirnoff 			printf("   PROXY:SRC\n");
266*3b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_DST ||
267*3b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_DST)
268*3b3a8eb9SGleb Smirnoff 			printf("   PROXY:DST\n");
269*3b3a8eb9SGleb Smirnoff 		else
270*3b3a8eb9SGleb Smirnoff 			printf("   <BAD STATE LEVELS %u:%u>\n",
271*3b3a8eb9SGleb Smirnoff 			    src->state, dst->state);
272*3b3a8eb9SGleb Smirnoff 		if (opts & PF_OPT_VERBOSE) {
273*3b3a8eb9SGleb Smirnoff 			printf("   ");
274*3b3a8eb9SGleb Smirnoff 			print_seq(src);
275*3b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
276*3b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
277*3b3a8eb9SGleb Smirnoff 				    src->wscale & PF_WSCALE_MASK);
278*3b3a8eb9SGleb Smirnoff 			printf("  ");
279*3b3a8eb9SGleb Smirnoff 			print_seq(dst);
280*3b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
281*3b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
282*3b3a8eb9SGleb Smirnoff 				    dst->wscale & PF_WSCALE_MASK);
283*3b3a8eb9SGleb Smirnoff 			printf("\n");
284*3b3a8eb9SGleb Smirnoff 		}
285*3b3a8eb9SGleb Smirnoff 	} else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
286*3b3a8eb9SGleb Smirnoff 	    dst->state < PFUDPS_NSTATES) {
287*3b3a8eb9SGleb Smirnoff 		const char *states[] = PFUDPS_NAMES;
288*3b3a8eb9SGleb Smirnoff 
289*3b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
290*3b3a8eb9SGleb Smirnoff 	} else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
291*3b3a8eb9SGleb Smirnoff 	    dst->state < PFOTHERS_NSTATES) {
292*3b3a8eb9SGleb Smirnoff 		/* XXX ICMP doesn't really have state levels */
293*3b3a8eb9SGleb Smirnoff 		const char *states[] = PFOTHERS_NAMES;
294*3b3a8eb9SGleb Smirnoff 
295*3b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
296*3b3a8eb9SGleb Smirnoff 	} else {
297*3b3a8eb9SGleb Smirnoff 		printf("   %u:%u\n", src->state, dst->state);
298*3b3a8eb9SGleb Smirnoff 	}
299*3b3a8eb9SGleb Smirnoff 
300*3b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE) {
301*3b3a8eb9SGleb Smirnoff 		u_int64_t packets[2];
302*3b3a8eb9SGleb Smirnoff 		u_int64_t bytes[2];
303*3b3a8eb9SGleb Smirnoff 		u_int32_t creation = ntohl(s->creation);
304*3b3a8eb9SGleb Smirnoff 		u_int32_t expire = ntohl(s->expire);
305*3b3a8eb9SGleb Smirnoff 
306*3b3a8eb9SGleb Smirnoff 		sec = creation % 60;
307*3b3a8eb9SGleb Smirnoff 		creation /= 60;
308*3b3a8eb9SGleb Smirnoff 		min = creation % 60;
309*3b3a8eb9SGleb Smirnoff 		creation /= 60;
310*3b3a8eb9SGleb Smirnoff 		printf("   age %.2u:%.2u:%.2u", creation, min, sec);
311*3b3a8eb9SGleb Smirnoff 		sec = expire % 60;
312*3b3a8eb9SGleb Smirnoff 		expire /= 60;
313*3b3a8eb9SGleb Smirnoff 		min = expire % 60;
314*3b3a8eb9SGleb Smirnoff 		expire /= 60;
315*3b3a8eb9SGleb Smirnoff 		printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
316*3b3a8eb9SGleb Smirnoff 
317*3b3a8eb9SGleb Smirnoff 		bcopy(s->packets[0], &packets[0], sizeof(u_int64_t));
318*3b3a8eb9SGleb Smirnoff 		bcopy(s->packets[1], &packets[1], sizeof(u_int64_t));
319*3b3a8eb9SGleb Smirnoff 		bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t));
320*3b3a8eb9SGleb Smirnoff 		bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t));
321*3b3a8eb9SGleb Smirnoff 		printf(", %llu:%llu pkts, %llu:%llu bytes",
322*3b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
323*3b3a8eb9SGleb Smirnoff 		    (unsigned long long)betoh64(packets[0]),
324*3b3a8eb9SGleb Smirnoff 		    (unsigned long long)betoh64(packets[1]),
325*3b3a8eb9SGleb Smirnoff 		    (unsigned long long)betoh64(bytes[0]),
326*3b3a8eb9SGleb Smirnoff 		    (unsigned long long)betoh64(bytes[1]));
327*3b3a8eb9SGleb Smirnoff #else
328*3b3a8eb9SGleb Smirnoff 		    betoh64(packets[0]),
329*3b3a8eb9SGleb Smirnoff 		    betoh64(packets[1]),
330*3b3a8eb9SGleb Smirnoff 		    betoh64(bytes[0]),
331*3b3a8eb9SGleb Smirnoff 		    betoh64(bytes[1]));
332*3b3a8eb9SGleb Smirnoff #endif
333*3b3a8eb9SGleb Smirnoff 		if (ntohl(s->anchor) != -1)
334*3b3a8eb9SGleb Smirnoff 			printf(", anchor %u", ntohl(s->anchor));
335*3b3a8eb9SGleb Smirnoff 		if (ntohl(s->rule) != -1)
336*3b3a8eb9SGleb Smirnoff 			printf(", rule %u", ntohl(s->rule));
337*3b3a8eb9SGleb Smirnoff 		if (s->state_flags & PFSTATE_SLOPPY)
338*3b3a8eb9SGleb Smirnoff 			printf(", sloppy");
339*3b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_SRCNODE)
340*3b3a8eb9SGleb Smirnoff 			printf(", source-track");
341*3b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE)
342*3b3a8eb9SGleb Smirnoff 			printf(", sticky-address");
343*3b3a8eb9SGleb Smirnoff 		printf("\n");
344*3b3a8eb9SGleb Smirnoff 	}
345*3b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE2) {
346*3b3a8eb9SGleb Smirnoff 		u_int64_t id;
347*3b3a8eb9SGleb Smirnoff 
348*3b3a8eb9SGleb Smirnoff 		bcopy(&s->id, &id, sizeof(u_int64_t));
349*3b3a8eb9SGleb Smirnoff 		printf("   id: %016llx creatorid: %08x",
350*3b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
351*3b3a8eb9SGleb Smirnoff 		    (unsigned long long)betoh64(id), ntohl(s->creatorid));
352*3b3a8eb9SGleb Smirnoff #else
353*3b3a8eb9SGleb Smirnoff 		    betoh64(id), ntohl(s->creatorid));
354*3b3a8eb9SGleb Smirnoff #endif
355*3b3a8eb9SGleb Smirnoff 		printf("\n");
356*3b3a8eb9SGleb Smirnoff 	}
357*3b3a8eb9SGleb Smirnoff }
358*3b3a8eb9SGleb Smirnoff 
359*3b3a8eb9SGleb Smirnoff int
360*3b3a8eb9SGleb Smirnoff unmask(struct pf_addr *m, sa_family_t af)
361*3b3a8eb9SGleb Smirnoff {
362*3b3a8eb9SGleb Smirnoff 	int i = 31, j = 0, b = 0;
363*3b3a8eb9SGleb Smirnoff 	u_int32_t tmp;
364*3b3a8eb9SGleb Smirnoff 
365*3b3a8eb9SGleb Smirnoff 	while (j < 4 && m->addr32[j] == 0xffffffff) {
366*3b3a8eb9SGleb Smirnoff 		b += 32;
367*3b3a8eb9SGleb Smirnoff 		j++;
368*3b3a8eb9SGleb Smirnoff 	}
369*3b3a8eb9SGleb Smirnoff 	if (j < 4) {
370*3b3a8eb9SGleb Smirnoff 		tmp = ntohl(m->addr32[j]);
371*3b3a8eb9SGleb Smirnoff 		for (i = 31; tmp & (1 << i); --i)
372*3b3a8eb9SGleb Smirnoff 			b++;
373*3b3a8eb9SGleb Smirnoff 	}
374*3b3a8eb9SGleb Smirnoff 	return (b);
375*3b3a8eb9SGleb Smirnoff }
376