pf_print_state.c revision 1.2 1 /* $NetBSD: pf_print_state.c,v 1.2 2004/06/22 15:16:30 itojun Exp $ */
2 /* $OpenBSD: pf_print_state.c,v 1.39 2004/02/10 17:48:08 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 default:
104 printf("?");
105 return;
106 }
107
108 /* mask if not _both_ address and mask are zero */
109 if (!(PF_AZERO(&addr->v.a.addr, AF_INET6) &&
110 PF_AZERO(&addr->v.a.mask, AF_INET6))) {
111 int bits = unmask(&addr->v.a.mask, af);
112
113 if (bits != (af == AF_INET ? 32 : 128))
114 printf("/%d", bits);
115 }
116 }
117
118 void
119 print_name(struct pf_addr *addr, sa_family_t af)
120 {
121 char host[NI_MAXHOST];
122
123 strlcpy(host, "?", sizeof(host));
124 switch (af) {
125 case AF_INET: {
126 struct sockaddr_in sin;
127
128 memset(&sin, 0, sizeof(sin));
129 sin.sin_len = sizeof(sin);
130 sin.sin_family = AF_INET;
131 sin.sin_addr = addr->v4;
132 getnameinfo((struct sockaddr *)&sin, sin.sin_len,
133 host, sizeof(host), NULL, 0, NI_NOFQDN);
134 break;
135 }
136 case AF_INET6: {
137 struct sockaddr_in6 sin6;
138
139 memset(&sin6, 0, sizeof(sin6));
140 sin6.sin6_len = sizeof(sin6);
141 sin6.sin6_family = AF_INET6;
142 sin6.sin6_addr = addr->v6;
143 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
144 host, sizeof(host), NULL, 0, NI_NOFQDN);
145 break;
146 }
147 }
148 printf("%s", host);
149 }
150
151 void
152 print_host(struct pf_state_host *h, sa_family_t af, int opts)
153 {
154 u_int16_t p = ntohs(h->port);
155
156 if (opts & PF_OPT_USEDNS)
157 print_name(&h->addr, af);
158 else {
159 struct pf_addr_wrap aw;
160
161 memset(&aw, 0, sizeof(aw));
162 aw.v.a.addr = h->addr;
163 if (af == AF_INET)
164 aw.v.a.mask.addr32[0] = 0xffffffff;
165 else {
166 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
167 af = AF_INET6;
168 }
169 print_addr(&aw, af, opts & PF_OPT_VERBOSE2);
170 }
171
172 if (p) {
173 if (af == AF_INET)
174 printf(":%u", p);
175 else
176 printf("[%u]", p);
177 }
178 }
179
180 void
181 print_seq(struct pf_state_peer *p)
182 {
183 if (p->seqdiff)
184 printf("[%u + %u](+%u)", p->seqlo, p->seqhi - p->seqlo,
185 p->seqdiff);
186 else
187 printf("[%u + %u]", p->seqlo, p->seqhi - p->seqlo);
188 }
189
190 void
191 print_state(struct pf_state *s, int opts)
192 {
193 struct pf_state_peer *src, *dst;
194 struct protoent *p;
195 int min, sec;
196
197 if (s->direction == PF_OUT) {
198 src = &s->src;
199 dst = &s->dst;
200 } else {
201 src = &s->dst;
202 dst = &s->src;
203 }
204 printf("%s ", s->u.ifname);
205 if ((p = getprotobynumber(s->proto)) != NULL)
206 printf("%s ", p->p_name);
207 else
208 printf("%u ", s->proto);
209 if (PF_ANEQ(&s->lan.addr, &s->gwy.addr, s->af) ||
210 (s->lan.port != s->gwy.port)) {
211 print_host(&s->lan, s->af, opts);
212 if (s->direction == PF_OUT)
213 printf(" -> ");
214 else
215 printf(" <- ");
216 }
217 print_host(&s->gwy, s->af, opts);
218 if (s->direction == PF_OUT)
219 printf(" -> ");
220 else
221 printf(" <- ");
222 print_host(&s->ext, s->af, opts);
223
224 printf(" ");
225 if (s->proto == IPPROTO_TCP) {
226 if (src->state <= TCPS_TIME_WAIT &&
227 dst->state <= TCPS_TIME_WAIT)
228 printf(" %s:%s\n", tcpstates[src->state],
229 tcpstates[dst->state]);
230 else if (src->state == PF_TCPS_PROXY_SRC ||
231 dst->state == PF_TCPS_PROXY_SRC)
232 printf(" PROXY:SRC\n");
233 else if (src->state == PF_TCPS_PROXY_DST ||
234 dst->state == PF_TCPS_PROXY_DST)
235 printf(" PROXY:DST\n");
236 else
237 printf(" <BAD STATE LEVELS %u:%u>\n",
238 src->state, dst->state);
239 if (opts & PF_OPT_VERBOSE) {
240 printf(" ");
241 print_seq(src);
242 if (src->wscale && dst->wscale)
243 printf(" wscale %u",
244 src->wscale & PF_WSCALE_MASK);
245 printf(" ");
246 print_seq(dst);
247 if (src->wscale && dst->wscale)
248 printf(" wscale %u",
249 dst->wscale & PF_WSCALE_MASK);
250 printf("\n");
251 }
252 } else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
253 dst->state < PFUDPS_NSTATES) {
254 const char *states[] = PFUDPS_NAMES;
255
256 printf(" %s:%s\n", states[src->state], states[dst->state]);
257 } else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
258 dst->state < PFOTHERS_NSTATES) {
259 /* XXX ICMP doesn't really have state levels */
260 const char *states[] = PFOTHERS_NAMES;
261
262 printf(" %s:%s\n", states[src->state], states[dst->state]);
263 } else {
264 printf(" %u:%u\n", src->state, dst->state);
265 }
266
267 if (opts & PF_OPT_VERBOSE) {
268 sec = s->creation % 60;
269 s->creation /= 60;
270 min = s->creation % 60;
271 s->creation /= 60;
272 printf(" age %.2u:%.2u:%.2u", s->creation, min, sec);
273 sec = s->expire % 60;
274 s->expire /= 60;
275 min = s->expire % 60;
276 s->expire /= 60;
277 printf(", expires in %.2u:%.2u:%.2u", s->expire, min, sec);
278 printf(", %u:%u pkts, %u:%u bytes",
279 s->packets[0], s->packets[1], s->bytes[0], s->bytes[1]);
280 if (s->anchor.nr != -1)
281 printf(", anchor %u", s->anchor.nr);
282 if (s->rule.nr != -1)
283 printf(", rule %u", s->rule.nr);
284 if (s->src_node != NULL)
285 printf(", source-track");
286 if (s->nat_src_node != NULL)
287 printf(", sticky-address");
288 printf("\n");
289 }
290 if (opts & PF_OPT_VERBOSE2) {
291 #ifdef __OpenBSD__
292 printf(" id: %016llx creatorid: %08x\n",
293 betoh64(s->id), ntohl(s->creatorid));
294 #else
295 printf(" id: %016llx creatorid: %08x\n",
296 (unsigned long long)be64toh(s->id), ntohl(s->creatorid));
297 #endif
298 }
299 }
300
301 int
302 unmask(struct pf_addr *m, sa_family_t af)
303 {
304 int i = 31, j = 0, b = 0;
305 u_int32_t tmp;
306
307 while (j < 4 && m->addr32[j] == 0xffffffff) {
308 b += 32;
309 j++;
310 }
311 if (j < 4) {
312 tmp = ntohl(m->addr32[j]);
313 for (i = 31; tmp & (1 << i); --i)
314 b++;
315 }
316 return (b);
317 }
318