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