xref: /freebsd/sbin/pfctl/pf_print_state.c (revision e044f67a66c82721b419949132fc48261ee39555)
13b3a8eb9SGleb Smirnoff /*	$OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david Exp $	*/
23b3a8eb9SGleb Smirnoff 
31de7b4b8SPedro F. Giffuni /*-
41de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
63b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
73b3a8eb9SGleb Smirnoff  * All rights reserved.
83b3a8eb9SGleb Smirnoff  *
93b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
103b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
113b3a8eb9SGleb Smirnoff  * are met:
123b3a8eb9SGleb Smirnoff  *
133b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
143b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
153b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
163b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
173b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
183b3a8eb9SGleb Smirnoff  *      with the distribution.
193b3a8eb9SGleb Smirnoff  *
203b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
213b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
243b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
273b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
283b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
303b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
313b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
323b3a8eb9SGleb Smirnoff  *
333b3a8eb9SGleb Smirnoff  */
343b3a8eb9SGleb Smirnoff 
353b3a8eb9SGleb Smirnoff #include <sys/types.h>
363b3a8eb9SGleb Smirnoff #include <sys/socket.h>
373b3a8eb9SGleb Smirnoff #include <sys/endian.h>
383b3a8eb9SGleb Smirnoff #include <net/if.h>
393b3a8eb9SGleb Smirnoff #define TCPSTATES
403b3a8eb9SGleb Smirnoff #include <netinet/tcp_fsm.h>
41010ee43fSKristof Provost #include <netinet/sctp.h>
423b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
433b3a8eb9SGleb Smirnoff #include <arpa/inet.h>
443b3a8eb9SGleb Smirnoff #include <netdb.h>
453b3a8eb9SGleb Smirnoff 
46a1ce87ecSGleb 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
print_addr(struct pf_addr_wrap * addr,sa_family_t af,int verbose)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 
131*e044f67aSKristof Provost 		if (bits < (af == AF_INET ? 32 : 128))
1323b3a8eb9SGleb Smirnoff 			printf("/%d", bits);
1333b3a8eb9SGleb Smirnoff 	}
1343b3a8eb9SGleb Smirnoff }
1353b3a8eb9SGleb Smirnoff 
1363b3a8eb9SGleb Smirnoff void
print_name(struct pf_addr * addr,sa_family_t af)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
print_host(struct pf_addr * addr,u_int16_t port,sa_family_t af,int opts)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
print_seq(struct pfctl_state_peer * p)197bc941291SKristof Provost print_seq(struct pfctl_state_peer *p)
1983b3a8eb9SGleb Smirnoff {
1993b3a8eb9SGleb Smirnoff 	if (p->seqdiff)
200bc941291SKristof Provost 		printf("[%u + %u](+%u)", p->seqlo,
201bc941291SKristof Provost 		    p->seqhi - p->seqlo, p->seqdiff);
2023b3a8eb9SGleb Smirnoff 	else
203bc941291SKristof Provost 		printf("[%u + %u]", p->seqlo,
204bc941291SKristof Provost 		    p->seqhi - p->seqlo);
2053b3a8eb9SGleb Smirnoff }
2063b3a8eb9SGleb Smirnoff 
207010ee43fSKristof Provost 
208010ee43fSKristof Provost static const char *
sctp_state_name(int state)209010ee43fSKristof Provost sctp_state_name(int state)
210010ee43fSKristof Provost {
211010ee43fSKristof Provost 	switch (state) {
212010ee43fSKristof Provost 	case SCTP_CLOSED:
213010ee43fSKristof Provost 		return ("CLOSED");
214010ee43fSKristof Provost 	case SCTP_BOUND:
215010ee43fSKristof Provost 		return ("BOUND");
216010ee43fSKristof Provost 	case SCTP_LISTEN:
217010ee43fSKristof Provost 		return ("LISTEN");
218010ee43fSKristof Provost 	case SCTP_COOKIE_WAIT:
219010ee43fSKristof Provost 		return ("COOKIE_WAIT");
220010ee43fSKristof Provost 	case SCTP_COOKIE_ECHOED:
221010ee43fSKristof Provost 		return ("COOKIE_ECHOED");
222010ee43fSKristof Provost 	case SCTP_ESTABLISHED:
223010ee43fSKristof Provost 		return ("ESTABLISHED");
224010ee43fSKristof Provost 	case SCTP_SHUTDOWN_SENT:
225010ee43fSKristof Provost 		return ("SHUTDOWN_SENT");
226010ee43fSKristof Provost 	case SCTP_SHUTDOWN_RECEIVED:
227010ee43fSKristof Provost 		return ("SHUTDOWN_RECEIVED");
228010ee43fSKristof Provost 	case SCTP_SHUTDOWN_ACK_SENT:
229010ee43fSKristof Provost 		return ("SHUTDOWN_ACK_SENT");
230010ee43fSKristof Provost 	case SCTP_SHUTDOWN_PENDING:
231010ee43fSKristof Provost 		return ("SHUTDOWN_PENDING");
232010ee43fSKristof Provost 	default:
233010ee43fSKristof Provost 		return ("?");
234010ee43fSKristof Provost 	}
235010ee43fSKristof Provost }
236010ee43fSKristof Provost 
2373b3a8eb9SGleb Smirnoff void
print_state(struct pfctl_state * s,int opts)238bc941291SKristof Provost print_state(struct pfctl_state *s, int opts)
2393b3a8eb9SGleb Smirnoff {
240bc941291SKristof Provost 	struct pfctl_state_peer *src, *dst;
241bc941291SKristof Provost 	struct pfctl_state_key *key, *sk, *nk;
242858937beSMateusz Guzik 	const char *protoname;
2433b3a8eb9SGleb Smirnoff 	int min, sec;
244bc941291SKristof Provost 	sa_family_t af;
245bc941291SKristof Provost 	uint8_t proto;
2461cac2626SIan Lepore #ifndef __NO_STRICT_ALIGNMENT
247bc941291SKristof Provost 	struct pfctl_state_key aligned_key[2];
2481cac2626SIan Lepore 
2491cac2626SIan Lepore 	bcopy(&s->key, aligned_key, sizeof(aligned_key));
2501cac2626SIan Lepore 	key = aligned_key;
2511cac2626SIan Lepore #else
2521cac2626SIan Lepore 	key = s->key;
2531cac2626SIan Lepore #endif
2543b3a8eb9SGleb Smirnoff 
255bc941291SKristof Provost 	af = s->key[PF_SK_WIRE].af;
256bc941291SKristof Provost 	proto = s->key[PF_SK_WIRE].proto;
257bc941291SKristof Provost 
2583b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT) {
2593b3a8eb9SGleb Smirnoff 		src = &s->src;
2603b3a8eb9SGleb Smirnoff 		dst = &s->dst;
2611cac2626SIan Lepore 		sk = &key[PF_SK_STACK];
2621cac2626SIan Lepore 		nk = &key[PF_SK_WIRE];
263bc941291SKristof Provost 		if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6)
2643b3a8eb9SGleb Smirnoff 			sk->port[0] = nk->port[0];
2653b3a8eb9SGleb Smirnoff 	} else {
2663b3a8eb9SGleb Smirnoff 		src = &s->dst;
2673b3a8eb9SGleb Smirnoff 		dst = &s->src;
2681cac2626SIan Lepore 		sk = &key[PF_SK_WIRE];
2691cac2626SIan Lepore 		nk = &key[PF_SK_STACK];
270bc941291SKristof Provost 		if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6)
2713b3a8eb9SGleb Smirnoff 			sk->port[1] = nk->port[1];
2723b3a8eb9SGleb Smirnoff 	}
2733b3a8eb9SGleb Smirnoff 	printf("%s ", s->ifname);
274858937beSMateusz Guzik 	if ((protoname = pfctl_proto2name(proto)) != NULL)
275858937beSMateusz Guzik 		printf("%s ", protoname);
2763b3a8eb9SGleb Smirnoff 	else
277bc941291SKristof Provost 		printf("%u ", proto);
2783b3a8eb9SGleb Smirnoff 
279bc941291SKristof Provost 	print_host(&nk->addr[1], nk->port[1], af, opts);
280bc941291SKristof Provost 	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], af) ||
2813b3a8eb9SGleb Smirnoff 	    nk->port[1] != sk->port[1]) {
2823b3a8eb9SGleb Smirnoff 		printf(" (");
283bc941291SKristof Provost 		print_host(&sk->addr[1], sk->port[1], af, opts);
2843b3a8eb9SGleb Smirnoff 		printf(")");
2853b3a8eb9SGleb Smirnoff 	}
2863b3a8eb9SGleb Smirnoff 	if (s->direction == PF_OUT)
2873b3a8eb9SGleb Smirnoff 		printf(" -> ");
2883b3a8eb9SGleb Smirnoff 	else
2893b3a8eb9SGleb Smirnoff 		printf(" <- ");
290bc941291SKristof Provost 	print_host(&nk->addr[0], nk->port[0], af, opts);
291bc941291SKristof Provost 	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], af) ||
2923b3a8eb9SGleb Smirnoff 	    nk->port[0] != sk->port[0]) {
2933b3a8eb9SGleb Smirnoff 		printf(" (");
294bc941291SKristof Provost 		print_host(&sk->addr[0], sk->port[0], af, opts);
2953b3a8eb9SGleb Smirnoff 		printf(")");
2963b3a8eb9SGleb Smirnoff 	}
2973b3a8eb9SGleb Smirnoff 
2983b3a8eb9SGleb Smirnoff 	printf("    ");
299bc941291SKristof Provost 	if (proto == IPPROTO_TCP) {
3003b3a8eb9SGleb Smirnoff 		if (src->state <= TCPS_TIME_WAIT &&
3013b3a8eb9SGleb Smirnoff 		    dst->state <= TCPS_TIME_WAIT)
3023b3a8eb9SGleb Smirnoff 			printf("   %s:%s\n", tcpstates[src->state],
3033b3a8eb9SGleb Smirnoff 			    tcpstates[dst->state]);
3043b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_SRC ||
3053b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_SRC)
3063b3a8eb9SGleb Smirnoff 			printf("   PROXY:SRC\n");
3073b3a8eb9SGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_DST ||
3083b3a8eb9SGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_DST)
3093b3a8eb9SGleb Smirnoff 			printf("   PROXY:DST\n");
3103b3a8eb9SGleb Smirnoff 		else
3113b3a8eb9SGleb Smirnoff 			printf("   <BAD STATE LEVELS %u:%u>\n",
3123b3a8eb9SGleb Smirnoff 			    src->state, dst->state);
3133b3a8eb9SGleb Smirnoff 		if (opts & PF_OPT_VERBOSE) {
3143b3a8eb9SGleb Smirnoff 			printf("   ");
3153b3a8eb9SGleb Smirnoff 			print_seq(src);
3163b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
3173b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
3183b3a8eb9SGleb Smirnoff 				    src->wscale & PF_WSCALE_MASK);
3193b3a8eb9SGleb Smirnoff 			printf("  ");
3203b3a8eb9SGleb Smirnoff 			print_seq(dst);
3213b3a8eb9SGleb Smirnoff 			if (src->wscale && dst->wscale)
3223b3a8eb9SGleb Smirnoff 				printf(" wscale %u",
3233b3a8eb9SGleb Smirnoff 				    dst->wscale & PF_WSCALE_MASK);
3243b3a8eb9SGleb Smirnoff 			printf("\n");
3253b3a8eb9SGleb Smirnoff 		}
326bc941291SKristof Provost 	} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
3273b3a8eb9SGleb Smirnoff 	    dst->state < PFUDPS_NSTATES) {
3283b3a8eb9SGleb Smirnoff 		const char *states[] = PFUDPS_NAMES;
3293b3a8eb9SGleb Smirnoff 
3303b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
331010ee43fSKristof Provost 	} else if (proto == IPPROTO_SCTP) {
332010ee43fSKristof Provost 		printf("   %s:%s\n", sctp_state_name(src->state),
333010ee43fSKristof Provost 		    sctp_state_name(dst->state));
334e0b95cb5SBaptiste Daroussin #ifndef INET6
335bc941291SKristof Provost 	} else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
3363b3a8eb9SGleb Smirnoff 	    dst->state < PFOTHERS_NSTATES) {
337e0b95cb5SBaptiste Daroussin #else
338bc941291SKristof Provost 	} else if (proto != IPPROTO_ICMP && proto != IPPROTO_ICMPV6 &&
339e0b95cb5SBaptiste Daroussin 	    src->state < PFOTHERS_NSTATES && dst->state < PFOTHERS_NSTATES) {
340e0b95cb5SBaptiste Daroussin #endif
3413b3a8eb9SGleb Smirnoff 		/* XXX ICMP doesn't really have state levels */
3423b3a8eb9SGleb Smirnoff 		const char *states[] = PFOTHERS_NAMES;
3433b3a8eb9SGleb Smirnoff 
3443b3a8eb9SGleb Smirnoff 		printf("   %s:%s\n", states[src->state], states[dst->state]);
3453b3a8eb9SGleb Smirnoff 	} else {
3463b3a8eb9SGleb Smirnoff 		printf("   %u:%u\n", src->state, dst->state);
3473b3a8eb9SGleb Smirnoff 	}
3483b3a8eb9SGleb Smirnoff 
3493b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE) {
350bc941291SKristof Provost 		u_int32_t creation = s->creation;
351bc941291SKristof Provost 		u_int32_t expire = s->expire;
3523b3a8eb9SGleb Smirnoff 
3533b3a8eb9SGleb Smirnoff 		sec = creation % 60;
3543b3a8eb9SGleb Smirnoff 		creation /= 60;
3553b3a8eb9SGleb Smirnoff 		min = creation % 60;
3563b3a8eb9SGleb Smirnoff 		creation /= 60;
3573b3a8eb9SGleb Smirnoff 		printf("   age %.2u:%.2u:%.2u", creation, min, sec);
3583b3a8eb9SGleb Smirnoff 		sec = expire % 60;
3593b3a8eb9SGleb Smirnoff 		expire /= 60;
3603b3a8eb9SGleb Smirnoff 		min = expire % 60;
3613b3a8eb9SGleb Smirnoff 		expire /= 60;
3623b3a8eb9SGleb Smirnoff 		printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
3633b3a8eb9SGleb Smirnoff 
364a1ce87ecSGleb Smirnoff 		printf(", %ju:%ju pkts, %ju:%ju bytes",
365bc941291SKristof Provost 		    s->packets[0],
366bc941291SKristof Provost 		    s->packets[1],
367bc941291SKristof Provost 		    s->bytes[0],
368bc941291SKristof Provost 		    s->bytes[1]);
369bc941291SKristof Provost 		if (s->anchor != -1)
370bc941291SKristof Provost 			printf(", anchor %u", s->anchor);
371bc941291SKristof Provost 		if (s->rule != -1)
372bc941291SKristof Provost 			printf(", rule %u", s->rule);
37339282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_ALLOWOPTS)
37439282ef3SKajetan Staszkiewicz 			printf(", allow-opts");
3753b3a8eb9SGleb Smirnoff 		if (s->state_flags & PFSTATE_SLOPPY)
3763b3a8eb9SGleb Smirnoff 			printf(", sloppy");
37739282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_NOSYNC)
37839282ef3SKajetan Staszkiewicz 			printf(", no-sync");
379baf9b6d0SKristof Provost 		if (s->state_flags & PFSTATE_PFLOW)
380baf9b6d0SKristof Provost 			printf(", pflow");
38139282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_ACK)
38239282ef3SKajetan Staszkiewicz 			printf(", psync-ack");
38339282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_NODF)
38439282ef3SKajetan Staszkiewicz 			printf(", no-df");
38539282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_SETTOS)
386c45d6b0eSKajetan Staszkiewicz 			printf(", set-tos 0x%2.2x", s->set_tos);
38739282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_RANDOMID)
38839282ef3SKajetan Staszkiewicz 			printf(", random-id");
38939282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_SCRUB_TCP)
390c45d6b0eSKajetan Staszkiewicz 			printf(", reassemble-tcp");
39139282ef3SKajetan Staszkiewicz 		if (s->state_flags & PFSTATE_SETPRIO)
392c45d6b0eSKajetan Staszkiewicz 			printf(", set-prio (0x%02x 0x%02x)",
393c45d6b0eSKajetan Staszkiewicz 			    s->set_prio[0], s->set_prio[1]);
394c45d6b0eSKajetan Staszkiewicz 		if (s->dnpipe || s->dnrpipe) {
395c45d6b0eSKajetan Staszkiewicz 			if (s->state_flags & PFSTATE_DN_IS_PIPE)
396c45d6b0eSKajetan Staszkiewicz 				printf(", dummynet pipe (%d %d)",
397c45d6b0eSKajetan Staszkiewicz 				s->dnpipe, s->dnrpipe);
398c45d6b0eSKajetan Staszkiewicz 			if (s->state_flags & PFSTATE_DN_IS_QUEUE)
399c45d6b0eSKajetan Staszkiewicz 				printf(", dummynet queue (%d %d)",
400c45d6b0eSKajetan Staszkiewicz 				s->dnpipe, s->dnrpipe);
401c45d6b0eSKajetan Staszkiewicz 		}
4023b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_SRCNODE)
4033b3a8eb9SGleb Smirnoff 			printf(", source-track");
4043b3a8eb9SGleb Smirnoff 		if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE)
4053b3a8eb9SGleb Smirnoff 			printf(", sticky-address");
406c45d6b0eSKajetan Staszkiewicz 		if (s->log)
407c45d6b0eSKajetan Staszkiewicz 			printf(", log");
408c45d6b0eSKajetan Staszkiewicz 		if (s->log & PF_LOG_ALL)
409c45d6b0eSKajetan Staszkiewicz 			printf(" (all)");
410c45d6b0eSKajetan Staszkiewicz 		if (s->min_ttl)
411c45d6b0eSKajetan Staszkiewicz 			printf(", min-ttl %d", s->min_ttl);
412c45d6b0eSKajetan Staszkiewicz 		if (s->max_mss)
413c45d6b0eSKajetan Staszkiewicz 			printf(", max-mss %d", s->max_mss);
4143b3a8eb9SGleb Smirnoff 		printf("\n");
4153b3a8eb9SGleb Smirnoff 	}
4163b3a8eb9SGleb Smirnoff 	if (opts & PF_OPT_VERBOSE2) {
4173b3a8eb9SGleb Smirnoff 		u_int64_t id;
4183b3a8eb9SGleb Smirnoff 
4193b3a8eb9SGleb Smirnoff 		bcopy(&s->id, &id, sizeof(u_int64_t));
420bc941291SKristof Provost 		printf("   id: %016jx creatorid: %08x", id, s->creatorid);
421c45d6b0eSKajetan Staszkiewicz 		if (s->rt) {
422c45d6b0eSKajetan Staszkiewicz 			switch (s->rt) {
423c45d6b0eSKajetan Staszkiewicz 				case PF_ROUTETO:
424c45d6b0eSKajetan Staszkiewicz 					printf(" route-to: ");
425c45d6b0eSKajetan Staszkiewicz 					break;
426c45d6b0eSKajetan Staszkiewicz 				case PF_DUPTO:
427c45d6b0eSKajetan Staszkiewicz 					printf(" dup-to: ");
428c45d6b0eSKajetan Staszkiewicz 					break;
429c45d6b0eSKajetan Staszkiewicz 				case PF_REPLYTO:
430c45d6b0eSKajetan Staszkiewicz 					printf(" reply-to: ");
431c45d6b0eSKajetan Staszkiewicz 					break;
432c45d6b0eSKajetan Staszkiewicz 				default:
433cc948296SKristof Provost 					printf(" gateway: ");
434c45d6b0eSKajetan Staszkiewicz 			}
435bc941291SKristof Provost 			print_host(&s->rt_addr, 0, af, opts);
436c45d6b0eSKajetan Staszkiewicz 			if (s->rt_ifname[0])
437c45d6b0eSKajetan Staszkiewicz 				printf("@%s", s->rt_ifname);
438c45d6b0eSKajetan Staszkiewicz 		}
439c45d6b0eSKajetan Staszkiewicz 		if (s->rtableid != -1)
440c45d6b0eSKajetan Staszkiewicz 			printf(" rtable: %d", s->rtableid);
4413b3a8eb9SGleb Smirnoff 		printf("\n");
442d0fdf2b2SKristof Provost 
443d0fdf2b2SKristof Provost 		if (strcmp(s->ifname, s->orig_ifname) != 0)
444d0fdf2b2SKristof Provost 			printf("   origif: %s\n", s->orig_ifname);
4453b3a8eb9SGleb Smirnoff 	}
4463b3a8eb9SGleb Smirnoff }
4473b3a8eb9SGleb Smirnoff 
4483b3a8eb9SGleb Smirnoff int
unmask(struct pf_addr * m,sa_family_t af)4493b3a8eb9SGleb Smirnoff unmask(struct pf_addr *m, sa_family_t af)
4503b3a8eb9SGleb Smirnoff {
4513b3a8eb9SGleb Smirnoff 	int i = 31, j = 0, b = 0;
4523b3a8eb9SGleb Smirnoff 	u_int32_t tmp;
4533b3a8eb9SGleb Smirnoff 
4543b3a8eb9SGleb Smirnoff 	while (j < 4 && m->addr32[j] == 0xffffffff) {
4553b3a8eb9SGleb Smirnoff 		b += 32;
4563b3a8eb9SGleb Smirnoff 		j++;
4573b3a8eb9SGleb Smirnoff 	}
4583b3a8eb9SGleb Smirnoff 	if (j < 4) {
4593b3a8eb9SGleb Smirnoff 		tmp = ntohl(m->addr32[j]);
4603b3a8eb9SGleb Smirnoff 		for (i = 31; tmp & (1 << i); --i)
4613b3a8eb9SGleb Smirnoff 			b++;
4623b3a8eb9SGleb Smirnoff 	}
4633b3a8eb9SGleb Smirnoff 	return (b);
4643b3a8eb9SGleb Smirnoff }
465