1 /* $OpenBSD: pf_print_state.c,v 1.58 2010/11/12 13:14:41 claudio 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 48 void print_name(struct pf_addr *, sa_family_t); 49 50 void 51 print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose) 52 { 53 switch (addr->type) { 54 case PF_ADDR_DYNIFTL: 55 printf("(%s", addr->v.ifname); 56 if (addr->iflags & PFI_AFLAG_NETWORK) 57 printf(":network"); 58 if (addr->iflags & PFI_AFLAG_BROADCAST) 59 printf(":broadcast"); 60 if (addr->iflags & PFI_AFLAG_PEER) 61 printf(":peer"); 62 if (addr->iflags & PFI_AFLAG_NOALIAS) 63 printf(":0"); 64 if (verbose) { 65 if (addr->p.dyncnt <= 0) 66 printf(":*"); 67 else 68 printf(":%d", addr->p.dyncnt); 69 } 70 printf(")"); 71 break; 72 case PF_ADDR_TABLE: 73 if (verbose) 74 if (addr->p.tblcnt == -1) 75 printf("<%s:*>", addr->v.tblname); 76 else 77 printf("<%s:%d>", addr->v.tblname, 78 addr->p.tblcnt); 79 else 80 printf("<%s>", addr->v.tblname); 81 return; 82 case PF_ADDR_RANGE: { 83 char buf[48]; 84 85 if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL) 86 printf("?"); 87 else 88 printf("%s", buf); 89 if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL) 90 printf(" - ?"); 91 else 92 printf(" - %s", buf); 93 break; 94 } 95 case PF_ADDR_ADDRMASK: 96 if (PF_AZERO(&addr->v.a.addr, AF_INET6) && 97 PF_AZERO(&addr->v.a.mask, AF_INET6)) 98 printf("any"); 99 else { 100 char buf[48]; 101 102 if (inet_ntop(af, &addr->v.a.addr, buf, 103 sizeof(buf)) == NULL) 104 printf("?"); 105 else 106 printf("%s", buf); 107 } 108 break; 109 case PF_ADDR_NOROUTE: 110 printf("no-route"); 111 return; 112 case PF_ADDR_URPFFAILED: 113 printf("urpf-failed"); 114 return; 115 case PF_ADDR_RTLABEL: 116 printf("route \"%s\"", addr->v.rtlabelname); 117 return; 118 default: 119 printf("?"); 120 return; 121 } 122 123 /* mask if not _both_ address and mask are zero */ 124 if (addr->type != PF_ADDR_RANGE && 125 !(PF_AZERO(&addr->v.a.addr, AF_INET6) && 126 PF_AZERO(&addr->v.a.mask, AF_INET6))) { 127 int bits = unmask(&addr->v.a.mask, af); 128 129 if (bits < (af == AF_INET ? 32 : 128)) 130 printf("/%d", bits); 131 } 132 } 133 134 void 135 print_name(struct pf_addr *addr, sa_family_t af) 136 { 137 char host[NI_MAXHOST]; 138 139 strlcpy(host, "?", sizeof(host)); 140 switch (af) { 141 case AF_INET: { 142 struct sockaddr_in sin; 143 144 memset(&sin, 0, sizeof(sin)); 145 sin.sin_len = sizeof(sin); 146 sin.sin_family = AF_INET; 147 sin.sin_addr = addr->v4; 148 getnameinfo((struct sockaddr *)&sin, sin.sin_len, 149 host, sizeof(host), NULL, 0, NI_NOFQDN); 150 break; 151 } 152 case AF_INET6: { 153 struct sockaddr_in6 sin6; 154 155 memset(&sin6, 0, sizeof(sin6)); 156 sin6.sin6_len = sizeof(sin6); 157 sin6.sin6_family = AF_INET6; 158 sin6.sin6_addr = addr->v6; 159 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 160 host, sizeof(host), NULL, 0, NI_NOFQDN); 161 break; 162 } 163 } 164 printf("%s", host); 165 } 166 167 void 168 print_host(struct pf_addr *addr, u_int16_t port, sa_family_t af, u_int16_t rdom, 169 int opts) 170 { 171 if (rdom) 172 printf("(%u) ", ntohs(rdom)); 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 198 void 199 print_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 209 void 210 print_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, nk->rdomain, opts); 239 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) || 240 nk->port[1] != sk->port[1] || 241 nk->rdomain != sk->rdomain) { 242 printf(" ("); 243 print_host(&sk->addr[1], sk->port[1], s->af, sk->rdomain, opts); 244 printf(")"); 245 } 246 if (s->direction == PF_OUT) 247 printf(" -> "); 248 else 249 printf(" <- "); 250 print_host(&nk->addr[0], nk->port[0], s->af, nk->rdomain, opts); 251 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) || 252 nk->port[0] != sk->port[0] || 253 nk->rdomain != sk->rdomain) { 254 printf(" ("); 255 print_host(&sk->addr[0], sk->port[0], s->af, sk->rdomain, opts); 256 printf(")"); 257 } 258 259 printf(" "); 260 if (s->proto == IPPROTO_TCP) { 261 if (src->state <= TCPS_TIME_WAIT && 262 dst->state <= TCPS_TIME_WAIT) 263 printf(" %s:%s\n", tcpstates[src->state], 264 tcpstates[dst->state]); 265 else if (src->state == PF_TCPS_PROXY_SRC || 266 dst->state == PF_TCPS_PROXY_SRC) 267 printf(" PROXY:SRC\n"); 268 else if (src->state == PF_TCPS_PROXY_DST || 269 dst->state == PF_TCPS_PROXY_DST) 270 printf(" PROXY:DST\n"); 271 else 272 printf(" <BAD STATE LEVELS %u:%u>\n", 273 src->state, dst->state); 274 if (opts & PF_OPT_VERBOSE) { 275 printf(" "); 276 print_seq(src); 277 if (src->wscale && dst->wscale) 278 printf(" wscale %u", 279 src->wscale & PF_WSCALE_MASK); 280 printf(" "); 281 print_seq(dst); 282 if (src->wscale && dst->wscale) 283 printf(" wscale %u", 284 dst->wscale & PF_WSCALE_MASK); 285 printf("\n"); 286 } 287 } else if (s->proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES && 288 dst->state < PFUDPS_NSTATES) { 289 const char *states[] = PFUDPS_NAMES; 290 291 printf(" %s:%s\n", states[src->state], states[dst->state]); 292 } else if (s->proto != IPPROTO_ICMP && s->proto != IPPROTO_ICMPV6 && 293 src->state < PFOTHERS_NSTATES && dst->state < PFOTHERS_NSTATES) { 294 /* XXX ICMP doesn't really have state levels */ 295 const char *states[] = PFOTHERS_NAMES; 296 297 printf(" %s:%s\n", states[src->state], states[dst->state]); 298 } else { 299 printf(" %u:%u\n", src->state, dst->state); 300 } 301 302 if (opts & PF_OPT_VERBOSE) { 303 u_int64_t packets[2]; 304 u_int64_t bytes[2]; 305 u_int32_t creation = ntohl(s->creation); 306 u_int32_t expire = ntohl(s->expire); 307 308 sec = creation % 60; 309 creation /= 60; 310 min = creation % 60; 311 creation /= 60; 312 printf(" age %.2u:%.2u:%.2u", creation, min, sec); 313 sec = expire % 60; 314 expire /= 60; 315 min = expire % 60; 316 expire /= 60; 317 printf(", expires in %.2u:%.2u:%.2u", expire, min, sec); 318 319 bcopy(s->packets[0], &packets[0], sizeof(u_int64_t)); 320 bcopy(s->packets[1], &packets[1], sizeof(u_int64_t)); 321 bcopy(s->bytes[0], &bytes[0], sizeof(u_int64_t)); 322 bcopy(s->bytes[1], &bytes[1], sizeof(u_int64_t)); 323 printf(", %llu:%llu pkts, %llu:%llu bytes", 324 betoh64(packets[0]), 325 betoh64(packets[1]), 326 betoh64(bytes[0]), 327 betoh64(bytes[1])); 328 if (ntohl(s->anchor) != -1) 329 printf(", anchor %u", ntohl(s->anchor)); 330 if (ntohl(s->rule) != -1) 331 printf(", rule %u", ntohl(s->rule)); 332 if (s->state_flags & PFSTATE_SLOPPY) 333 printf(", sloppy"); 334 if (s->state_flags & PFSTATE_PFLOW) 335 printf(", pflow"); 336 if (s->sync_flags & PFSYNC_FLAG_SRCNODE) 337 printf(", source-track"); 338 if (s->sync_flags & PFSYNC_FLAG_NATSRCNODE) 339 printf(", sticky-address"); 340 printf("\n"); 341 } 342 if (opts & PF_OPT_VERBOSE2) { 343 u_int64_t id; 344 345 bcopy(&s->id, &id, sizeof(u_int64_t)); 346 printf(" id: %016llx creatorid: %08x", 347 betoh64(id), ntohl(s->creatorid)); 348 printf("\n"); 349 } 350 } 351 352 int 353 unmask(struct pf_addr *m, sa_family_t af) 354 { 355 int i = 31, j = 0, b = 0; 356 u_int32_t tmp; 357 358 while (j < 4 && m->addr32[j] == 0xffffffff) { 359 b += 32; 360 j++; 361 } 362 if (j < 4) { 363 tmp = ntohl(m->addr32[j]); 364 for (i = 31; tmp & (1 << i); --i) 365 b++; 366 } 367 return (b); 368 } 369