1/* $OpenBSD: pf_print_state.c,v 1.52 2008/08/12 16:40:18 david 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/cdefs.h>
|
35 36#include <sys/types.h> 37#include <sys/socket.h> 38#ifdef __FreeBSD__ 39#include <sys/endian.h> 40#define betoh64 be64toh 41#endif 42#include <net/if.h> 43#define TCPSTATES 44#include <netinet/tcp_fsm.h> 45#include <net/pfvar.h> 46#include <arpa/inet.h> 47#include <netdb.h> 48 49#include <stdio.h> 50#include <string.h> 51 52#include "pfctl_parser.h" 53#include "pfctl.h" 54 55void print_name(struct pf_addr *, sa_family_t); 56 57void 58print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose) 59{ 60 switch (addr->type) { 61 case PF_ADDR_DYNIFTL: 62 printf("(%s", addr->v.ifname); 63 if (addr->iflags & PFI_AFLAG_NETWORK) 64 printf(":network"); 65 if (addr->iflags & PFI_AFLAG_BROADCAST) 66 printf(":broadcast"); 67 if (addr->iflags & PFI_AFLAG_PEER) 68 printf(":peer"); 69 if (addr->iflags & PFI_AFLAG_NOALIAS) 70 printf(":0"); 71 if (verbose) { 72 if (addr->p.dyncnt <= 0) 73 printf(":*"); 74 else 75 printf(":%d", addr->p.dyncnt); 76 } 77 printf(")"); 78 break; 79 case PF_ADDR_TABLE: 80 if (verbose) 81 if (addr->p.tblcnt == -1) 82 printf("<%s:*>", addr->v.tblname); 83 else 84 printf("<%s:%d>", addr->v.tblname, 85 addr->p.tblcnt); 86 else 87 printf("<%s>", addr->v.tblname); 88 return; 89 case PF_ADDR_RANGE: { 90 char buf[48]; 91 92 if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL) 93 printf("?"); 94 else 95 printf("%s", buf); 96 if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL) 97 printf(" - ?"); 98 else 99 printf(" - %s", buf); 100 break; 101 } 102 case PF_ADDR_ADDRMASK: 103 if (PF_AZERO(&addr->v.a.addr, AF_INET6) && 104 PF_AZERO(&addr->v.a.mask, AF_INET6)) 105 printf("any"); 106 else { 107 char buf[48]; 108 109 if (inet_ntop(af, &addr->v.a.addr, buf, 110 sizeof(buf)) == NULL) 111 printf("?"); 112 else 113 printf("%s", buf); 114 } 115 break; 116 case PF_ADDR_NOROUTE: 117 printf("no-route"); 118 return; 119 case PF_ADDR_URPFFAILED: 120 printf("urpf-failed"); 121 return;
|
122 default: 123 printf("?"); 124 return; 125 } 126 127 /* mask if not _both_ address and mask are zero */ 128 if (addr->type != PF_ADDR_RANGE && 129 !(PF_AZERO(&addr->v.a.addr, AF_INET6) && 130 PF_AZERO(&addr->v.a.mask, AF_INET6))) { 131 int bits = unmask(&addr->v.a.mask, af); 132 133 if (bits != (af == AF_INET ? 32 : 128)) 134 printf("/%d", bits); 135 } 136} 137 138void 139print_name(struct pf_addr *addr, sa_family_t af) 140{ 141 char host[NI_MAXHOST]; 142 143 strlcpy(host, "?", sizeof(host)); 144 switch (af) { 145 case AF_INET: { 146 struct sockaddr_in sin; 147 148 memset(&sin, 0, sizeof(sin)); 149 sin.sin_len = sizeof(sin); 150 sin.sin_family = AF_INET; 151 sin.sin_addr = addr->v4; 152 getnameinfo((struct sockaddr *)&sin, sin.sin_len, 153 host, sizeof(host), NULL, 0, NI_NOFQDN); 154 break; 155 } 156 case AF_INET6: { 157 struct sockaddr_in6 sin6; 158 159 memset(&sin6, 0, sizeof(sin6)); 160 sin6.sin6_len = sizeof(sin6); 161 sin6.sin6_family = AF_INET6; 162 sin6.sin6_addr = addr->v6; 163 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 164 host, sizeof(host), NULL, 0, NI_NOFQDN); 165 break; 166 } 167 } 168 printf("%s", host); 169} 170 171void 172print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, int opts) 173{ 174 if (opts & PF_OPT_USEDNS) 175 print_name(addr, af); 176 else { 177 struct pf_addr_wrap aw; 178 179 memset(&aw, 0, sizeof(aw)); 180 aw.v.a.addr = *addr; 181 if (af == AF_INET) 182 aw.v.a.mask.addr32[0] = 0xffffffff; 183 else { 184 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask)); 185 af = AF_INET6; 186 } 187 print_addr(&aw, af, opts & PF_OPT_VERBOSE2); 188 } 189 190 if (port) { 191 if (af == AF_INET) 192 printf(":%u", ntohs(port)); 193 else 194 printf("[%u]", ntohs(port)); 195 } 196} 197 198void 199print_seq(struct pfsync_state_peer *p) 200{ 201 if (p->seqdiff) 202 printf("[%u + %u](+%u)", ntohl(p->seqlo), 203 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)); 204 else 205 printf("[%u + %u]", ntohl(p->seqlo), 206 ntohl(p->seqhi) - ntohl(p->seqlo)); 207} 208 209void 210print_state(struct pfsync_state *s, int opts) 211{ 212 struct pfsync_state_peer *src, *dst; 213 struct pfsync_state_key *sk, *nk; 214 struct protoent *p; 215 int min, sec; 216 217 if (s->direction == PF_OUT) { 218 src = &s->src; 219 dst = &s->dst; 220 sk = &s->key[PF_SK_STACK]; 221 nk = &s->key[PF_SK_WIRE]; 222 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6) 223 sk->port[0] = nk->port[0]; 224 } else { 225 src = &s->dst; 226 dst = &s->src; 227 sk = &s->key[PF_SK_WIRE]; 228 nk = &s->key[PF_SK_STACK]; 229 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6) 230 sk->port[1] = nk->port[1]; 231 } 232 printf("%s ", s->ifname); 233 if ((p = getprotobynumber(s->proto)) != NULL) 234 printf("%s ", p->p_name); 235 else 236 printf("%u ", s->proto); 237 238 print_host(&nk->addr[1], nk->port[1], s->af, opts); 239 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) || 240 nk->port[1] != sk->port[1]) { 241 printf(" ("); 242 print_host(&sk->addr[1], sk->port[1], s->af, opts); 243 printf(")"); 244 } 245 if (s->direction == PF_OUT) 246 printf(" -> "); 247 else 248 printf(" <- "); 249 print_host(&nk->addr[0], nk->port[0], s->af, opts); 250 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) || 251 nk->port[0] != sk->port[0]) { 252 printf(" ("); 253 print_host(&sk->addr[0], sk->port[0], s->af, opts); 254 printf(")"); 255 } 256 257 printf(" "); 258 if (s->proto == IPPROTO_TCP) { 259 if (src->state <= TCPS_TIME_WAIT && 260 dst->state <= TCPS_TIME_WAIT) 261 printf(" %s:%s\n", tcpstates[src->state], 262 tcpstates[dst->state]); 263 else if (src->state == PF_TCPS_PROXY_SRC || 264 dst->state == PF_TCPS_PROXY_SRC) 265 printf(" PROXY:SRC\n"); 266 else if (src->state == PF_TCPS_PROXY_DST || 267 dst->state == PF_TCPS_PROXY_DST) 268 printf(" PROXY:DST\n"); 269 else 270 printf(" <BAD STATE LEVELS %u:%u>\n", 271 src->state, dst->state); 272 if (opts & PF_OPT_VERBOSE) { 273 printf(" "); 274 print_seq(src); 275 if (src->wscale && dst->wscale) 276 printf(" wscale %u", 277 src->wscale & PF_WSCALE_MASK); 278 printf(" "); 279 print_seq(dst); 280 if (src->wscale && dst->wscale) 281 printf(" wscale %u", 282 dst->wscale & PF_WSCALE_MASK); 283 printf("\n"); 284 } 285 } else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES && 286 dst->state < PFUDPS_NSTATES) { 287 const char *states[] = PFUDPS_NAMES; 288 289 printf(" %s:%s\n", states[src->state], states[dst->state]); 290 } else if (s->proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES && 291 dst->state < PFOTHERS_NSTATES) { 292 /* XXX ICMP doesn't really have state levels */ 293 const char *states[] = PFOTHERS_NAMES; 294 295 printf(" %s:%s\n", states[src->state], states[dst->state]); 296 } else { 297 printf(" %u:%u\n", src->state, dst->state); 298 } 299 300 if (opts & PF_OPT_VERBOSE) { 301 u_int64_t packets[2]; 302 u_int64_t bytes[2]; 303 u_int32_t creation = ntohl(s->creation); 304 u_int32_t expire = ntohl(s->expire); 305 306 sec = creation % 60; 307 creation /= 60; 308 min = creation % 60; 309 creation /= 60; 310 printf(" age %.2u:%.2u:%.2u", creation, min, sec); 311 sec = expire % 60; 312 expire /= 60; 313 min = expire % 60; 314 expire /= 60; 315 printf(", expires in %.2u:%.2u:%.2u", expire, min, sec); 316 317 bcopy(s->packets[0], &packets[0], sizeof(u_int64_t)); 318 bcopy(s->packets[1], &packets[1], sizeof(u_int64_t)); 319 bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t)); 320 bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t)); 321 printf(", %llu:%llu pkts, %llu:%llu bytes", 322#ifdef __FreeBSD__ 323 (unsigned long long)betoh64(packets[0]), 324 (unsigned long long)betoh64(packets[1]), 325 (unsigned long long)betoh64(bytes[0]), 326 (unsigned long long)betoh64(bytes[1])); 327#else 328 betoh64(packets[0]), 329 betoh64(packets[1]), 330 betoh64(bytes[0]), 331 betoh64(bytes[1])); 332#endif 333 if (ntohl(s->anchor) != -1) 334 printf(", anchor %u", ntohl(s->anchor)); 335 if (ntohl(s->rule) != -1) 336 printf(", rule %u", ntohl(s->rule)); 337 if (s->state_flags & PFSTATE_SLOPPY) 338 printf(", sloppy");
|