1 /* $OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david Exp $ */
4 * Copyright (c) 2001 Daniel Hartmeier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <sys/endian.h>
40 #define betoh64 be64toh
44 #include <netinet/tcp_fsm.h>
45 #include <net/pfvar.h>
46 #include <arpa/inet.h>
52 #include "pfctl_parser.h"
55 void print_name(struct pf_addr *, sa_family_t);
58 print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose)
62 printf("(%s", addr->v.ifname);
63 if (addr->iflags & PFI_AFLAG_NETWORK)
65 if (addr->iflags & PFI_AFLAG_BROADCAST)
67 if (addr->iflags & PFI_AFLAG_PEER)
69 if (addr->iflags & PFI_AFLAG_NOALIAS)
72 if (addr->p.dyncnt <= 0)
75 printf(":%d", addr->p.dyncnt);
81 if (addr->p.tblcnt == -1)
82 printf("<%s:*>", addr->v.tblname);
84 printf("<%s:%d>", addr->v.tblname,
87 printf("<%s>", addr->v.tblname);
92 if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL)
96 if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL)
102 case PF_ADDR_ADDRMASK:
103 if (PF_AZERO(&addr->v.a.addr, AF_INET6) &&
104 PF_AZERO(&addr->v.a.mask, AF_INET6))
109 if (inet_ntop(af, &addr->v.a.addr, buf,
110 sizeof(buf)) == NULL)
116 case PF_ADDR_NOROUTE:
119 case PF_ADDR_URPFFAILED:
120 printf("urpf-failed");
122 case PF_ADDR_RTLABEL:
123 printf("route \"%s\"", addr->v.rtlabelname);
130 /* mask if not _both_ address and mask are zero */
131 if (addr->type != PF_ADDR_RANGE &&
132 !(PF_AZERO(&addr->v.a.addr, AF_INET6) &&
133 PF_AZERO(&addr->v.a.mask, AF_INET6))) {
134 int bits = unmask(&addr->v.a.mask, af);
136 if (bits != (af == AF_INET ? 32 : 128))
142 print_name(struct pf_addr *addr, sa_family_t af)
144 char host[NI_MAXHOST];
146 strlcpy(host, "?", sizeof(host));
149 struct sockaddr_in sin;
151 memset(&sin, 0, sizeof(sin));
152 sin.sin_len = sizeof(sin);
153 sin.sin_family = AF_INET;
154 sin.sin_addr = addr->v4;
155 getnameinfo((struct sockaddr *)&sin, sin.sin_len,
156 host, sizeof(host), NULL, 0, NI_NOFQDN);
160 struct sockaddr_in6 sin6;
162 memset(&sin6, 0, sizeof(sin6));
163 sin6.sin6_len = sizeof(sin6);
164 sin6.sin6_family = AF_INET6;
165 sin6.sin6_addr = addr->v6;
166 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
167 host, sizeof(host), NULL, 0, NI_NOFQDN);
175 print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts)
177 if (opts & PF_OPT_USEDNS)
178 print_name(addr, af);
180 struct pf_addr_wrap aw;
182 memset(&aw, 0, sizeof(aw));
185 aw.v.a.mask.addr32[0] = 0xffffffff;
187 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
190 print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
195 printf(":%u", ntohs(port));
197 printf("[%u]", ntohs(port));
202 print_seq(struct pfsync_state_peer *p)
205 printf("[%u + %u](+%u)", ntohl(p->seqlo),
206 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
208 printf("[%u + %u]", ntohl(p->seqlo),
209 ntohl(p->seqhi) - ntohl(p->seqlo));
213 print_state(struct pfsync_state *s, int opts)
215 struct pfsync_state_peer *src, *dst;
216 struct pfsync_state_key *sk, *nk;
220 if (s->direction == PF_OUT) {
223 sk = &s->key[PF_SK_STACK];
224 nk = &s->key[PF_SK_WIRE];
225 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
226 sk->port[0] = nk->port[0];
230 sk = &s->key[PF_SK_WIRE];
231 nk = &s->key[PF_SK_STACK];
232 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
233 sk->port[1] = nk->port[1];
235 printf("%s ", s->ifname);
236 if ((p = getprotobynumber(s->proto)) != NULL)
237 printf("%s ", p->p_name);
239 printf("%u ", s->proto);
241 print_host(&nk->addr[1], nk->port[1], s->af, opts);
242 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
243 nk->port[1] != sk->port[1]) {
245 print_host(&sk->addr[1], sk->port[1], s->af, opts);
248 if (s->direction == PF_OUT)
252 print_host(&nk->addr[0], nk->port[0], s->af, opts);
253 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
254 nk->port[0] != sk->port[0]) {
256 print_host(&sk->addr[0], sk->port[0], s->af, opts);
261 if (s->proto == IPPROTO_TCP) {
262 if (src->state <= TCPS_TIME_WAIT &&
263 dst->state <= TCPS_TIME_WAIT)
264 printf(" %s:%s\n", tcpstates[src->state],
265 tcpstates[dst->state]);
266 else if (src->state == PF_TCPS_PROXY_SRC ||
267 dst->state == PF_TCPS_PROXY_SRC)
268 printf(" PROXY:SRC\n");
269 else if (src->state == PF_TCPS_PROXY_DST ||
270 dst->state == PF_TCPS_PROXY_DST)
271 printf(" PROXY:DST\n");
273 printf(" <BAD STATE LEVELS %u:%u>\n",
274 src->state, dst->state);
275 if (opts & PF_OPT_VERBOSE) {
278 if (src->wscale && dst->wscale)
280 src->wscale & PF_WSCALE_MASK);
283 if (src->wscale && dst->wscale)
285 dst->wscale & PF_WSCALE_MASK);
288 } else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
289 dst->state < PFUDPS_NSTATES) {
290 const char *states[] = PFUDPS_NAMES;
292 printf(" %s:%s\n", states[src->state], states[dst->state]);
293 } else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
294 dst->state < PFOTHERS_NSTATES) {
295 /* XXX ICMP doesn't really have state levels */
296 const char *states[] = PFOTHERS_NAMES;
298 printf(" %s:%s\n", states[src->state], states[dst->state]);
300 printf(" %u:%u\n", src->state, dst->state);
303 if (opts & PF_OPT_VERBOSE) {
304 u_int64_t packets[2];
306 u_int32_t creation = ntohl(s->creation);
307 u_int32_t expire = ntohl(s->expire);
313 printf(" age %.2u:%.2u:%.2u", creation, min, sec);
318 printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
320 bcopy(s->packets[0], &packets[0], sizeof(u_int64_t));
321 bcopy(s->packets[1], &packets[1], sizeof(u_int64_t));
322 bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t));
323 bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t));
324 printf(", %llu:%llu pkts, %llu:%llu bytes",
326 (unsigned long long)betoh64(packets[0]),
327 (unsigned long long)betoh64(packets[1]),
328 (unsigned long long)betoh64(bytes[0]),
329 (unsigned long long)betoh64(bytes[1]));
336 if (ntohl(s->anchor) != -1)
337 printf(", anchor %u", ntohl(s->anchor));
338 if (ntohl(s->rule) != -1)
339 printf(", rule %u", ntohl(s->rule));
340 if (s->state_flags & PFSTATE_SLOPPY)
342 if (s->state_flags & PFSTATE_PFLOW)
344 if (s->sync_flags & PFSYNC_FLAG_SRCNODE)
345 printf(", source-track");
346 if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE)
347 printf(", sticky-address");
350 if (opts & PF_OPT_VERBOSE2) {
353 bcopy(&s->id, &id, sizeof(u_int64_t));
354 printf(" id: %016llx creatorid: %08x",
356 (unsigned long long)betoh64(id), ntohl(s->creatorid));
358 betoh64(id), ntohl(s->creatorid));
365 unmask(struct pf_addr *m, sa_family_t af)
367 int i = 31, j = 0, b = 0;
370 while (j < 4 && m->addr32[j] == 0xffffffff) {
375 tmp = ntohl(m->addr32[j]);
376 for (i = 31; tmp & (1 << i); --i)