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