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