xref: /openbsd-src/usr.sbin/tcpdump/pf_print_state.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: pf_print_state.c,v 1.7 2009/02/16 00:31:25 dlg 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 #include "addrtoname.h"
48 
49 void	print_name(struct pf_addr *, sa_family_t);
50 
51 void
52 print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose)
53 {
54 	switch (addr->type) {
55 	case PF_ADDR_DYNIFTL:
56 		printf("(%s", addr->v.ifname);
57 		if (addr->iflags & PFI_AFLAG_NETWORK)
58 			printf(":network");
59 		if (addr->iflags & PFI_AFLAG_BROADCAST)
60 			printf(":broadcast");
61 		if (addr->iflags & PFI_AFLAG_PEER)
62 			printf(":peer");
63 		if (addr->iflags & PFI_AFLAG_NOALIAS)
64 			printf(":0");
65 		if (verbose) {
66 			if (addr->p.dyncnt <= 0)
67 				printf(":*");
68 			else
69 				printf(":%d", addr->p.dyncnt);
70 		}
71 		printf(")");
72 		break;
73 	case PF_ADDR_TABLE:
74 		if (verbose)
75 			if (addr->p.tblcnt == -1)
76 				printf("<%s:*>", addr->v.tblname);
77 			else
78 				printf("<%s:%d>", addr->v.tblname,
79 				    addr->p.tblcnt);
80 		else
81 			printf("<%s>", addr->v.tblname);
82 		return;
83 	case PF_ADDR_ADDRMASK:
84 		if (PF_AZERO(&addr->v.a.addr, AF_INET6) &&
85 		    PF_AZERO(&addr->v.a.mask, AF_INET6))
86 			printf("any");
87 		else {
88 			char buf[48];
89 
90 			if (inet_ntop(af, &addr->v.a.addr, buf,
91 			    sizeof(buf)) == NULL)
92 				printf("?");
93 			else
94 				printf("%s", buf);
95 		}
96 		break;
97 	case PF_ADDR_NOROUTE:
98 		printf("no-route");
99 		return;
100 	default:
101 		printf("?");
102 		return;
103 	}
104 	if (! PF_AZERO(&addr->v.a.mask, af)) {
105 		int bits = unmask(&addr->v.a.mask, af);
106 
107 		if (bits != (af == AF_INET ? 32 : 128))
108 			printf("/%d", bits);
109 	}
110 }
111 
112 void
113 print_name(struct pf_addr *addr, sa_family_t af)
114 {
115 	char *host;
116 
117 	switch (af) {
118 	case AF_INET:
119 		host = getname((char *)&addr->v4);
120 		break;
121 	case AF_INET6:
122 		host = getname6((char *)&addr->v6);
123 		break;
124 	default:
125 		host = "?";
126 		break;
127 	}
128 	printf("%s", host);
129 }
130 
131 void
132 print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts)
133 {
134 	if (opts & PF_OPT_USEDNS)
135 		print_name(addr, af);
136 	else {
137 		struct pf_addr_wrap aw;
138 
139 		memset(&aw, 0, sizeof(aw));
140 		aw.v.a.addr = *addr;
141 		if (af == AF_INET)
142 			aw.v.a.mask.addr32[0] = 0xffffffff;
143 		else {
144 			memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
145 			af = AF_INET6;
146 		}
147 		print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
148 	}
149 
150 	if (port) {
151 		if (af == AF_INET)
152 			printf(":%u", ntohs(port));
153 		else
154 			printf("[%u]", ntohs(port));
155 	}
156 }
157 
158 void
159 print_seq(struct pfsync_state_peer *p)
160 {
161 	if (p->seqdiff)
162 		printf("[%u + %u](+%u)", ntohl(p->seqlo),
163 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
164 	else
165 		printf("[%u + %u]", ntohl(p->seqlo),
166 		    ntohl(p->seqhi) - ntohl(p->seqlo));
167 }
168 
169 void
170 print_state(struct pfsync_state *s, int opts)
171 {
172 	struct pfsync_state_peer *src, *dst;
173 	struct pfsync_state_key *sk, *nk;
174 	int min, sec;
175 
176 	if (s->direction == PF_OUT) {
177 		src = &s->src;
178 		dst = &s->dst;
179 		sk = &s->key[PF_SK_STACK];
180 		nk = &s->key[PF_SK_WIRE];
181 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
182 			sk->port[0] = nk->port[0];
183 	} else {
184 		src = &s->dst;
185 		dst = &s->src;
186 		sk = &s->key[PF_SK_WIRE];
187 		nk = &s->key[PF_SK_STACK];
188 		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
189 			sk->port[1] = nk->port[1];
190 	}
191 	printf("%s ", s->ifname);
192 	printf("%s ", ipproto_string(s->proto));
193 
194 	print_host(&nk->addr[1], nk->port[1], s->af, opts);
195 	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
196 	    nk->port[1] != sk->port[1]) {
197 		printf(" (");
198 		print_host(&sk->addr[1], sk->port[1], s->af, opts);
199 		printf(")");
200 	}
201 	if (s->direction == PF_OUT)
202 		printf(" -> ");
203 	else
204 		printf(" <- ");
205 	print_host(&nk->addr[0], nk->port[0], s->af, opts);
206 	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
207 	    nk->port[0] != sk->port[0]) {
208 		printf(" (");
209 		print_host(&sk->addr[0], sk->port[0], s->af, opts);
210 		printf(")");
211 	}
212 
213 	printf("    ");
214 	if (s->proto == IPPROTO_TCP) {
215 		if (src->state <= TCPS_TIME_WAIT &&
216 		    dst->state <= TCPS_TIME_WAIT)
217 			printf("\n   %s:%s", tcpstates[src->state],
218 			    tcpstates[dst->state]);
219 		else if (src->state == PF_TCPS_PROXY_SRC ||
220 		    dst->state == PF_TCPS_PROXY_SRC)
221 			printf("\n   PROXY:SRC");
222 		else if (src->state == PF_TCPS_PROXY_DST ||
223 		    dst->state == PF_TCPS_PROXY_DST)
224 			printf("\n   PROXY:DST");
225 		else
226 			printf("\n   <BAD STATE LEVELS %u:%u>",
227 			    src->state, dst->state);
228 		if (opts & PF_OPT_VERBOSE) {
229 			printf("\n   ");
230 			print_seq(src);
231 			if (src->wscale && dst->wscale)
232 				printf(" wscale %u",
233 				    src->wscale & PF_WSCALE_MASK);
234 			printf("  ");
235 			print_seq(dst);
236 			if (src->wscale && dst->wscale)
237 				printf(" wscale %u",
238 				    dst->wscale & PF_WSCALE_MASK);
239 		}
240 	} else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
241 	    dst->state < PFUDPS_NSTATES) {
242 		const char *states[] = PFUDPS_NAMES;
243 
244 		printf("   %s:%s", states[src->state], states[dst->state]);
245 	} else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
246 	    dst->state < PFOTHERS_NSTATES) {
247 		/* XXX ICMP doesn't really have state levels */
248 		const char *states[] = PFOTHERS_NAMES;
249 
250 		printf("   %s:%s", states[src->state], states[dst->state]);
251 	} else {
252 		printf("   %u:%u", src->state, dst->state);
253 	}
254 
255 	if (opts & PF_OPT_VERBOSE) {
256 		u_int64_t packets[2];
257 		u_int64_t bytes[2];
258 		u_int32_t creation = ntohl(s->creation);
259 		u_int32_t expire = ntohl(s->expire);
260 
261 		sec = creation % 60;
262 		creation /= 60;
263 		min = creation % 60;
264 		creation /= 60;
265 		printf("\n   age %.2u:%.2u:%.2u", creation, min, sec);
266 		sec = expire % 60;
267 		expire /= 60;
268 		min = expire % 60;
269 		expire /= 60;
270 		printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
271 
272 		bcopy(s->packets[0], &packets[0], sizeof(u_int64_t));
273 		bcopy(s->packets[1], &packets[1], sizeof(u_int64_t));
274 		bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t));
275 		bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t));
276 		printf(", %llu:%llu pkts, %llu:%llu bytes",
277 		    betoh64(packets[0]),
278 		    betoh64(packets[1]),
279 		    betoh64(bytes[0]),
280 		    betoh64(bytes[1]));
281 		if (s->anchor != -1)
282 			printf(", anchor %u", ntohl(s->anchor));
283 		if (s->rule != -1)
284 			printf(", rule %u", ntohl(s->rule));
285 	}
286 	if (opts & PF_OPT_VERBOSE2) {
287 		u_int64_t id;
288 
289 		bcopy(&s->id, &id, sizeof(u_int64_t));
290 		printf("\n   id: %016llx creatorid: %08x",
291 		    betoh64(id), ntohl(s->creatorid));
292 	}
293 }
294 
295 int
296 unmask(struct pf_addr *m, sa_family_t af)
297 {
298 	int i = 31, j = 0, b = 0;
299 	u_int32_t tmp;
300 
301 	while (j < 4 && m->addr32[j] == 0xffffffff) {
302 		b += 32;
303 		j++;
304 	}
305 	if (j < 4) {
306 		tmp = ntohl(m->addr32[j]);
307 		for (i = 31; tmp & (1 << i); --i)
308 			b++;
309 	}
310 	return (b);
311 }
312