1 /* $OpenBSD: pfctl_parser.c,v 1.240 2008/06/10 20:55:02 mcbride Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2001 Daniel Hartmeier 7 * Copyright (c) 2002,2003 Henning Brauer 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * - Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * - Redistributions in binary form must reproduce the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer in the documentation and/or other materials provided 19 * with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/types.h> 37 #include <sys/ioctl.h> 38 #include <sys/socket.h> 39 #include <sys/param.h> 40 #include <sys/proc.h> 41 #include <net/if_dl.h> 42 #include <net/if.h> 43 #include <netinet/in.h> 44 #include <netinet/in_systm.h> 45 #include <netinet/ip.h> 46 #include <netinet/ip_icmp.h> 47 #include <netinet/icmp6.h> 48 #include <netinet/tcp.h> 49 #include <net/pfvar.h> 50 #include <arpa/inet.h> 51 52 #include <assert.h> 53 #include <search.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <ctype.h> 58 #include <netdb.h> 59 #include <stdarg.h> 60 #include <errno.h> 61 #include <err.h> 62 #include <ifaddrs.h> 63 #include <inttypes.h> 64 #include <unistd.h> 65 66 #include "pfctl_parser.h" 67 #include "pfctl.h" 68 69 void print_op (u_int8_t, const char *, const char *); 70 void print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int); 71 void print_ugid (u_int8_t, id_t, id_t, const char *); 72 void print_flags (uint16_t); 73 void print_fromto(struct pf_rule_addr *, pf_osfp_t, 74 struct pf_rule_addr *, sa_family_t, u_int8_t, int, int); 75 int ifa_skip_if(const char *filter, struct node_host *p); 76 77 struct node_host *host_if(const char *, int); 78 struct node_host *host_ip(const char *, int); 79 struct node_host *host_dns(const char *, int, int); 80 81 const char * const tcpflags = "FSRPAUEWe"; 82 83 static const struct icmptypeent icmp_type[] = { 84 { "echoreq", ICMP_ECHO }, 85 { "echorep", ICMP_ECHOREPLY }, 86 { "unreach", ICMP_UNREACH }, 87 { "squench", ICMP_SOURCEQUENCH }, 88 { "redir", ICMP_REDIRECT }, 89 { "althost", ICMP_ALTHOSTADDR }, 90 { "routeradv", ICMP_ROUTERADVERT }, 91 { "routersol", ICMP_ROUTERSOLICIT }, 92 { "timex", ICMP_TIMXCEED }, 93 { "paramprob", ICMP_PARAMPROB }, 94 { "timereq", ICMP_TSTAMP }, 95 { "timerep", ICMP_TSTAMPREPLY }, 96 { "inforeq", ICMP_IREQ }, 97 { "inforep", ICMP_IREQREPLY }, 98 { "maskreq", ICMP_MASKREQ }, 99 { "maskrep", ICMP_MASKREPLY }, 100 { "trace", ICMP_TRACEROUTE }, 101 { "dataconv", ICMP_DATACONVERR }, 102 { "mobredir", ICMP_MOBILE_REDIRECT }, 103 { "ipv6-where", ICMP_IPV6_WHEREAREYOU }, 104 { "ipv6-here", ICMP_IPV6_IAMHERE }, 105 { "mobregreq", ICMP_MOBILE_REGREQUEST }, 106 { "mobregrep", ICMP_MOBILE_REGREPLY }, 107 { "skip", ICMP_SKIP }, 108 { "photuris", ICMP_PHOTURIS } 109 }; 110 111 static const struct icmptypeent icmp6_type[] = { 112 { "unreach", ICMP6_DST_UNREACH }, 113 { "toobig", ICMP6_PACKET_TOO_BIG }, 114 { "timex", ICMP6_TIME_EXCEEDED }, 115 { "paramprob", ICMP6_PARAM_PROB }, 116 { "echoreq", ICMP6_ECHO_REQUEST }, 117 { "echorep", ICMP6_ECHO_REPLY }, 118 { "groupqry", ICMP6_MEMBERSHIP_QUERY }, 119 { "listqry", MLD_LISTENER_QUERY }, 120 { "grouprep", ICMP6_MEMBERSHIP_REPORT }, 121 { "listenrep", MLD_LISTENER_REPORT }, 122 { "groupterm", ICMP6_MEMBERSHIP_REDUCTION }, 123 { "listendone", MLD_LISTENER_DONE }, 124 { "routersol", ND_ROUTER_SOLICIT }, 125 { "routeradv", ND_ROUTER_ADVERT }, 126 { "neighbrsol", ND_NEIGHBOR_SOLICIT }, 127 { "neighbradv", ND_NEIGHBOR_ADVERT }, 128 { "redir", ND_REDIRECT }, 129 { "routrrenum", ICMP6_ROUTER_RENUMBERING }, 130 { "wrureq", ICMP6_WRUREQUEST }, 131 { "wrurep", ICMP6_WRUREPLY }, 132 { "fqdnreq", ICMP6_FQDN_QUERY }, 133 { "fqdnrep", ICMP6_FQDN_REPLY }, 134 { "niqry", ICMP6_NI_QUERY }, 135 { "nirep", ICMP6_NI_REPLY }, 136 { "mtraceresp", MLD_MTRACE_RESP }, 137 { "mtrace", MLD_MTRACE }, 138 { "listenrepv2", MLDV2_LISTENER_REPORT }, 139 }; 140 141 static const struct icmpcodeent icmp_code[] = { 142 { "net-unr", ICMP_UNREACH, ICMP_UNREACH_NET }, 143 { "host-unr", ICMP_UNREACH, ICMP_UNREACH_HOST }, 144 { "proto-unr", ICMP_UNREACH, ICMP_UNREACH_PROTOCOL }, 145 { "port-unr", ICMP_UNREACH, ICMP_UNREACH_PORT }, 146 { "needfrag", ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG }, 147 { "srcfail", ICMP_UNREACH, ICMP_UNREACH_SRCFAIL }, 148 { "net-unk", ICMP_UNREACH, ICMP_UNREACH_NET_UNKNOWN }, 149 { "host-unk", ICMP_UNREACH, ICMP_UNREACH_HOST_UNKNOWN }, 150 { "isolate", ICMP_UNREACH, ICMP_UNREACH_ISOLATED }, 151 { "net-prohib", ICMP_UNREACH, ICMP_UNREACH_NET_PROHIB }, 152 { "host-prohib", ICMP_UNREACH, ICMP_UNREACH_HOST_PROHIB }, 153 { "net-tos", ICMP_UNREACH, ICMP_UNREACH_TOSNET }, 154 { "host-tos", ICMP_UNREACH, ICMP_UNREACH_TOSHOST }, 155 { "filter-prohib", ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB }, 156 { "host-preced", ICMP_UNREACH, ICMP_UNREACH_HOST_PRECEDENCE }, 157 { "cutoff-preced", ICMP_UNREACH, ICMP_UNREACH_PRECEDENCE_CUTOFF }, 158 { "redir-net", ICMP_REDIRECT, ICMP_REDIRECT_NET }, 159 { "redir-host", ICMP_REDIRECT, ICMP_REDIRECT_HOST }, 160 { "redir-tos-net", ICMP_REDIRECT, ICMP_REDIRECT_TOSNET }, 161 { "redir-tos-host", ICMP_REDIRECT, ICMP_REDIRECT_TOSHOST }, 162 { "normal-adv", ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NORMAL }, 163 { "common-adv", ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NOROUTE_COMMON }, 164 { "transit", ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS }, 165 { "reassemb", ICMP_TIMXCEED, ICMP_TIMXCEED_REASS }, 166 { "badhead", ICMP_PARAMPROB, ICMP_PARAMPROB_ERRATPTR }, 167 { "optmiss", ICMP_PARAMPROB, ICMP_PARAMPROB_OPTABSENT }, 168 { "badlen", ICMP_PARAMPROB, ICMP_PARAMPROB_LENGTH }, 169 { "unknown-ind", ICMP_PHOTURIS, ICMP_PHOTURIS_UNKNOWN_INDEX }, 170 { "auth-fail", ICMP_PHOTURIS, ICMP_PHOTURIS_AUTH_FAILED }, 171 { "decrypt-fail", ICMP_PHOTURIS, ICMP_PHOTURIS_DECRYPT_FAILED } 172 }; 173 174 static const struct icmpcodeent icmp6_code[] = { 175 { "admin-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN }, 176 { "noroute-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE }, 177 { "notnbr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOTNEIGHBOR }, 178 { "beyond-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_BEYONDSCOPE }, 179 { "addr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR }, 180 { "port-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT }, 181 { "transit", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT }, 182 { "reassemb", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY }, 183 { "badhead", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER }, 184 { "nxthdr", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER }, 185 { "redironlink", ND_REDIRECT, ND_REDIRECT_ONLINK }, 186 { "redirrouter", ND_REDIRECT, ND_REDIRECT_ROUTER } 187 }; 188 189 const struct pf_timeout pf_timeouts[] = { 190 { "tcp.first", PFTM_TCP_FIRST_PACKET }, 191 { "tcp.opening", PFTM_TCP_OPENING }, 192 { "tcp.established", PFTM_TCP_ESTABLISHED }, 193 { "tcp.closing", PFTM_TCP_CLOSING }, 194 { "tcp.finwait", PFTM_TCP_FIN_WAIT }, 195 { "tcp.closed", PFTM_TCP_CLOSED }, 196 { "tcp.tsdiff", PFTM_TS_DIFF }, 197 { "sctp.first", PFTM_SCTP_FIRST_PACKET }, 198 { "sctp.opening", PFTM_SCTP_OPENING }, 199 { "sctp.established", PFTM_SCTP_ESTABLISHED }, 200 { "sctp.closing", PFTM_SCTP_CLOSING }, 201 { "sctp.closed", PFTM_SCTP_CLOSED }, 202 { "udp.first", PFTM_UDP_FIRST_PACKET }, 203 { "udp.single", PFTM_UDP_SINGLE }, 204 { "udp.multiple", PFTM_UDP_MULTIPLE }, 205 { "icmp.first", PFTM_ICMP_FIRST_PACKET }, 206 { "icmp.error", PFTM_ICMP_ERROR_REPLY }, 207 { "other.first", PFTM_OTHER_FIRST_PACKET }, 208 { "other.single", PFTM_OTHER_SINGLE }, 209 { "other.multiple", PFTM_OTHER_MULTIPLE }, 210 { "frag", PFTM_FRAG }, 211 { "interval", PFTM_INTERVAL }, 212 { "adaptive.start", PFTM_ADAPTIVE_START }, 213 { "adaptive.end", PFTM_ADAPTIVE_END }, 214 { "src.track", PFTM_SRC_NODE }, 215 { NULL, 0 } 216 }; 217 218 static struct hsearch_data isgroup_map; 219 220 static __attribute__((constructor)) void 221 pfctl_parser_init(void) 222 { 223 /* 224 * As hdestroy() will never be called on these tables, it will be 225 * safe to use references into the stored data as keys. 226 */ 227 if (hcreate_r(0, &isgroup_map) == 0) 228 err(1, "Failed to create interface group query response map"); 229 } 230 231 void 232 copy_satopfaddr(struct pf_addr *pfa, struct sockaddr *sa) 233 { 234 if (sa->sa_family == AF_INET6) 235 pfa->v6 = ((struct sockaddr_in6 *)sa)->sin6_addr; 236 else if (sa->sa_family == AF_INET) 237 pfa->v4 = ((struct sockaddr_in *)sa)->sin_addr; 238 else 239 warnx("unhandled af %d", sa->sa_family); 240 } 241 242 const struct icmptypeent * 243 geticmptypebynumber(u_int8_t type, sa_family_t af) 244 { 245 unsigned int i; 246 247 if (af != AF_INET6) { 248 for (i=0; i < nitems(icmp_type); i++) { 249 if (type == icmp_type[i].type) 250 return (&icmp_type[i]); 251 } 252 } else { 253 for (i=0; i < nitems(icmp6_type); i++) { 254 if (type == icmp6_type[i].type) 255 return (&icmp6_type[i]); 256 } 257 } 258 return (NULL); 259 } 260 261 const struct icmptypeent * 262 geticmptypebyname(char *w, sa_family_t af) 263 { 264 unsigned int i; 265 266 if (af != AF_INET6) { 267 for (i=0; i < nitems(icmp_type); i++) { 268 if (!strcmp(w, icmp_type[i].name)) 269 return (&icmp_type[i]); 270 } 271 } else { 272 for (i=0; i < nitems(icmp6_type); i++) { 273 if (!strcmp(w, icmp6_type[i].name)) 274 return (&icmp6_type[i]); 275 } 276 } 277 return (NULL); 278 } 279 280 const struct icmpcodeent * 281 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af) 282 { 283 unsigned int i; 284 285 if (af != AF_INET6) { 286 for (i=0; i < nitems(icmp_code); i++) { 287 if (type == icmp_code[i].type && 288 code == icmp_code[i].code) 289 return (&icmp_code[i]); 290 } 291 } else { 292 for (i=0; i < nitems(icmp6_code); i++) { 293 if (type == icmp6_code[i].type && 294 code == icmp6_code[i].code) 295 return (&icmp6_code[i]); 296 } 297 } 298 return (NULL); 299 } 300 301 const struct icmpcodeent * 302 geticmpcodebyname(u_long type, char *w, sa_family_t af) 303 { 304 unsigned int i; 305 306 if (af != AF_INET6) { 307 for (i=0; i < nitems(icmp_code); i++) { 308 if (type == icmp_code[i].type && 309 !strcmp(w, icmp_code[i].name)) 310 return (&icmp_code[i]); 311 } 312 } else { 313 for (i=0; i < nitems(icmp6_code); i++) { 314 if (type == icmp6_code[i].type && 315 !strcmp(w, icmp6_code[i].name)) 316 return (&icmp6_code[i]); 317 } 318 } 319 return (NULL); 320 } 321 322 void 323 print_op(u_int8_t op, const char *a1, const char *a2) 324 { 325 if (op == PF_OP_IRG) 326 printf(" %s >< %s", a1, a2); 327 else if (op == PF_OP_XRG) 328 printf(" %s <> %s", a1, a2); 329 else if (op == PF_OP_EQ) 330 printf(" = %s", a1); 331 else if (op == PF_OP_NE) 332 printf(" != %s", a1); 333 else if (op == PF_OP_LT) 334 printf(" < %s", a1); 335 else if (op == PF_OP_LE) 336 printf(" <= %s", a1); 337 else if (op == PF_OP_GT) 338 printf(" > %s", a1); 339 else if (op == PF_OP_GE) 340 printf(" >= %s", a1); 341 else if (op == PF_OP_RRG) 342 printf(" %s:%s", a1, a2); 343 } 344 345 void 346 print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, const char *proto, int numeric) 347 { 348 char a1[6], a2[6]; 349 struct servent *s; 350 351 if (!numeric) 352 s = getservbyport(p1, proto); 353 else 354 s = NULL; 355 p1 = ntohs(p1); 356 p2 = ntohs(p2); 357 snprintf(a1, sizeof(a1), "%u", p1); 358 snprintf(a2, sizeof(a2), "%u", p2); 359 printf(" port"); 360 if (s != NULL && (op == PF_OP_EQ || op == PF_OP_NE)) 361 print_op(op, s->s_name, a2); 362 else 363 print_op(op, a1, a2); 364 } 365 366 void 367 print_ugid(u_int8_t op, id_t i1, id_t i2, const char *t) 368 { 369 char a1[11], a2[11]; 370 371 snprintf(a1, sizeof(a1), "%ju", (uintmax_t)i1); 372 snprintf(a2, sizeof(a2), "%ju", (uintmax_t)i2); 373 printf(" %s", t); 374 if (i1 == -1 && (op == PF_OP_EQ || op == PF_OP_NE)) 375 print_op(op, "unknown", a2); 376 else 377 print_op(op, a1, a2); 378 } 379 380 void 381 print_flags(uint16_t f) 382 { 383 int i; 384 385 for (i = 0; tcpflags[i]; ++i) 386 if (f & (1 << i)) 387 printf("%c", tcpflags[i]); 388 } 389 390 void 391 print_fromto(struct pf_rule_addr *src, pf_osfp_t osfp, struct pf_rule_addr *dst, 392 sa_family_t af, u_int8_t proto, int verbose, int numeric) 393 { 394 char buf[PF_OSFP_LEN*3]; 395 if (src->addr.type == PF_ADDR_ADDRMASK && 396 dst->addr.type == PF_ADDR_ADDRMASK && 397 PF_AZERO(&src->addr.v.a.addr, AF_INET6) && 398 PF_AZERO(&src->addr.v.a.mask, AF_INET6) && 399 PF_AZERO(&dst->addr.v.a.addr, AF_INET6) && 400 PF_AZERO(&dst->addr.v.a.mask, AF_INET6) && 401 !src->neg && !dst->neg && 402 !src->port_op && !dst->port_op && 403 osfp == PF_OSFP_ANY) 404 printf(" all"); 405 else { 406 printf(" from "); 407 if (src->neg) 408 printf("! "); 409 print_addr(&src->addr, af, verbose); 410 if (src->port_op) 411 print_port(src->port_op, src->port[0], 412 src->port[1], 413 proto == IPPROTO_TCP ? "tcp" : "udp", 414 numeric); 415 if (osfp != PF_OSFP_ANY) 416 printf(" os \"%s\"", pfctl_lookup_fingerprint(osfp, buf, 417 sizeof(buf))); 418 419 printf(" to "); 420 if (dst->neg) 421 printf("! "); 422 print_addr(&dst->addr, af, verbose); 423 if (dst->port_op) 424 print_port(dst->port_op, dst->port[0], 425 dst->port[1], 426 proto == IPPROTO_TCP ? "tcp" : "udp", 427 numeric); 428 } 429 } 430 431 void 432 print_pool(struct pfctl_pool *pool, u_int16_t p1, u_int16_t p2, 433 sa_family_t af, int id) 434 { 435 struct pf_pooladdr *pooladdr; 436 437 if ((TAILQ_FIRST(&pool->list) != NULL) && 438 TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL) 439 printf("{ "); 440 TAILQ_FOREACH(pooladdr, &pool->list, entries){ 441 switch (id) { 442 case PF_NAT: 443 case PF_RDR: 444 case PF_BINAT: 445 print_addr(&pooladdr->addr, af, 0); 446 break; 447 case PF_PASS: 448 case PF_MATCH: 449 if (PF_AZERO(&pooladdr->addr.v.a.addr, af)) 450 printf("%s", pooladdr->ifname); 451 else { 452 printf("(%s ", pooladdr->ifname); 453 print_addr(&pooladdr->addr, af, 0); 454 printf(")"); 455 } 456 break; 457 default: 458 break; 459 } 460 if (TAILQ_NEXT(pooladdr, entries) != NULL) 461 printf(", "); 462 else if (TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL) 463 printf(" }"); 464 } 465 switch (id) { 466 case PF_NAT: 467 if ((p1 != PF_NAT_PROXY_PORT_LOW || 468 p2 != PF_NAT_PROXY_PORT_HIGH) && (p1 != 0 || p2 != 0)) { 469 if (p1 == p2) 470 printf(" port %u", p1); 471 else 472 printf(" port %u:%u", p1, p2); 473 } 474 break; 475 case PF_RDR: 476 if (p1) { 477 printf(" port %u", p1); 478 if (p2 && (p2 != p1)) 479 printf(":%u", p2); 480 } 481 break; 482 default: 483 break; 484 } 485 switch (pool->opts & PF_POOL_TYPEMASK) { 486 case PF_POOL_NONE: 487 break; 488 case PF_POOL_BITMASK: 489 printf(" bitmask"); 490 break; 491 case PF_POOL_RANDOM: 492 printf(" random"); 493 break; 494 case PF_POOL_SRCHASH: 495 printf(" source-hash 0x%08x%08x%08x%08x", 496 pool->key.key32[0], pool->key.key32[1], 497 pool->key.key32[2], pool->key.key32[3]); 498 break; 499 case PF_POOL_ROUNDROBIN: 500 printf(" round-robin"); 501 break; 502 } 503 if (pool->opts & PF_POOL_STICKYADDR) 504 printf(" sticky-address"); 505 if (pool->opts & PF_POOL_ENDPI) 506 printf(" endpoint-independent"); 507 if (id == PF_NAT && p1 == 0 && p2 == 0) 508 printf(" static-port"); 509 if (pool->mape.offset > 0) 510 printf(" map-e-portset %u/%u/%u", 511 pool->mape.offset, pool->mape.psidlen, pool->mape.psid); 512 } 513 514 void 515 print_status(struct pfctl_status *s, struct pfctl_syncookies *cookies, int opts) 516 { 517 struct pfctl_status_counter *c; 518 char statline[80], *running; 519 time_t runtime; 520 int i; 521 char buf[PF_MD5_DIGEST_LENGTH * 2 + 1]; 522 static const char hex[] = "0123456789abcdef"; 523 524 runtime = time(NULL) - s->since; 525 running = s->running ? "Enabled" : "Disabled"; 526 527 if (s->since) { 528 unsigned int sec, min, hrs; 529 time_t day = runtime; 530 531 sec = day % 60; 532 day /= 60; 533 min = day % 60; 534 day /= 60; 535 hrs = day % 24; 536 day /= 24; 537 snprintf(statline, sizeof(statline), 538 "Status: %s for %lld days %.2u:%.2u:%.2u", 539 running, (long long)day, hrs, min, sec); 540 } else 541 snprintf(statline, sizeof(statline), "Status: %s", running); 542 printf("%-44s", statline); 543 switch (s->debug) { 544 case PF_DEBUG_NONE: 545 printf("%15s\n\n", "Debug: None"); 546 break; 547 case PF_DEBUG_URGENT: 548 printf("%15s\n\n", "Debug: Urgent"); 549 break; 550 case PF_DEBUG_MISC: 551 printf("%15s\n\n", "Debug: Misc"); 552 break; 553 case PF_DEBUG_NOISY: 554 printf("%15s\n\n", "Debug: Loud"); 555 break; 556 } 557 558 if (opts & PF_OPT_VERBOSE) { 559 printf("Hostid: 0x%08x\n", s->hostid); 560 561 for (i = 0; i < PF_MD5_DIGEST_LENGTH; i++) { 562 buf[i + i] = hex[s->pf_chksum[i] >> 4]; 563 buf[i + i + 1] = hex[s->pf_chksum[i] & 0x0f]; 564 } 565 buf[i + i] = '\0'; 566 printf("Checksum: 0x%s\n\n", buf); 567 } 568 569 if (s->ifname[0] != 0) { 570 printf("Interface Stats for %-16s %5s %16s\n", 571 s->ifname, "IPv4", "IPv6"); 572 printf(" %-25s %14llu %16llu\n", "Bytes In", 573 (unsigned long long)s->bcounters[0][0], 574 (unsigned long long)s->bcounters[1][0]); 575 printf(" %-25s %14llu %16llu\n", "Bytes Out", 576 (unsigned long long)s->bcounters[0][1], 577 (unsigned long long)s->bcounters[1][1]); 578 printf(" Packets In\n"); 579 printf(" %-23s %14llu %16llu\n", "Passed", 580 (unsigned long long)s->pcounters[0][0][PF_PASS], 581 (unsigned long long)s->pcounters[1][0][PF_PASS]); 582 printf(" %-23s %14llu %16llu\n", "Blocked", 583 (unsigned long long)s->pcounters[0][0][PF_DROP], 584 (unsigned long long)s->pcounters[1][0][PF_DROP]); 585 printf(" Packets Out\n"); 586 printf(" %-23s %14llu %16llu\n", "Passed", 587 (unsigned long long)s->pcounters[0][1][PF_PASS], 588 (unsigned long long)s->pcounters[1][1][PF_PASS]); 589 printf(" %-23s %14llu %16llu\n\n", "Blocked", 590 (unsigned long long)s->pcounters[0][1][PF_DROP], 591 (unsigned long long)s->pcounters[1][1][PF_DROP]); 592 } 593 printf("%-27s %14s %16s\n", "State Table", "Total", "Rate"); 594 printf(" %-25s %14ju %14s\n", "current entries", s->states, ""); 595 TAILQ_FOREACH(c, &s->fcounters, entry) { 596 printf(" %-25s %14ju ", c->name, c->counter); 597 if (runtime > 0) 598 printf("%14.1f/s\n", 599 (double)c->counter / (double)runtime); 600 else 601 printf("%14s\n", ""); 602 } 603 if (opts & PF_OPT_VERBOSE) { 604 printf("Source Tracking Table\n"); 605 printf(" %-25s %14ju %14s\n", "current entries", 606 s->src_nodes, ""); 607 TAILQ_FOREACH(c, &s->scounters, entry) { 608 printf(" %-25s %14ju ", c->name, c->counter); 609 if (runtime > 0) 610 printf("%14.1f/s\n", 611 (double)c->counter / (double)runtime); 612 else 613 printf("%14s\n", ""); 614 } 615 } 616 printf("Counters\n"); 617 TAILQ_FOREACH(c, &s->counters, entry) { 618 printf(" %-25s %14ju ", c->name, c->counter); 619 if (runtime > 0) 620 printf("%14.1f/s\n", 621 (double)c->counter / (double)runtime); 622 else 623 printf("%14s\n", ""); 624 } 625 if (opts & PF_OPT_VERBOSE) { 626 printf("Limit Counters\n"); 627 TAILQ_FOREACH(c, &s->lcounters, entry) { 628 printf(" %-25s %14ju ", c->name, c->counter); 629 if (runtime > 0) 630 printf("%14.1f/s\n", 631 (double)c->counter / (double)runtime); 632 else 633 printf("%14s\n", ""); 634 } 635 636 printf("Syncookies\n"); 637 assert(cookies->mode <= PFCTL_SYNCOOKIES_ADAPTIVE); 638 printf(" %-25s %s\n", "mode", 639 PFCTL_SYNCOOKIES_MODE_NAMES[cookies->mode]); 640 printf(" %-25s %s\n", "active", 641 s->syncookies_active ? "active" : "inactive"); 642 if (opts & PF_OPT_VERBOSE2) { 643 printf(" %-25s %d %%\n", "highwater", cookies->highwater); 644 printf(" %-25s %d %%\n", "lowwater", cookies->lowwater); 645 printf(" %-25s %d\n", "halfopen states", cookies->halfopen_states); 646 } 647 printf("Reassemble %24s %s\n", 648 s->reass & PF_REASS_ENABLED ? "yes" : "no", 649 s->reass & PF_REASS_NODF ? "no-df" : "" 650 ); 651 } 652 } 653 654 void 655 print_running(struct pfctl_status *status) 656 { 657 printf("%s\n", status->running ? "Enabled" : "Disabled"); 658 } 659 660 void 661 print_src_node(struct pfctl_src_node *sn, int opts) 662 { 663 struct pf_addr_wrap aw; 664 uint64_t min, sec; 665 const char *sn_type_names[] = PF_SN_TYPE_NAMES; 666 667 memset(&aw, 0, sizeof(aw)); 668 if (sn->af == AF_INET) 669 aw.v.a.mask.addr32[0] = 0xffffffff; 670 else 671 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask)); 672 673 aw.v.a.addr = sn->addr; 674 print_addr(&aw, sn->af, opts & PF_OPT_VERBOSE2); 675 printf(" -> "); 676 aw.v.a.addr = sn->raddr; 677 print_addr(&aw, sn->naf ? sn->naf : sn->af, opts & PF_OPT_VERBOSE2); 678 printf(" ( states %u, connections %u, rate %u.%u/%us )\n", sn->states, 679 sn->conn, sn->conn_rate.count / 1000, 680 (sn->conn_rate.count % 1000) / 100, sn->conn_rate.seconds); 681 if (opts & PF_OPT_VERBOSE) { 682 sec = sn->creation % 60; 683 sn->creation /= 60; 684 min = sn->creation % 60; 685 sn->creation /= 60; 686 printf(" age %.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64, 687 sn->creation, min, sec); 688 if (sn->states == 0) { 689 sec = sn->expire % 60; 690 sn->expire /= 60; 691 min = sn->expire % 60; 692 sn->expire /= 60; 693 printf(", expires in %.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64, 694 sn->expire, min, sec); 695 } 696 printf(", %" PRIu64 " pkts, %" PRIu64 " bytes", 697 sn->packets[0] + sn->packets[1], 698 sn->bytes[0] + sn->bytes[1]); 699 switch (sn->ruletype) { 700 case PF_NAT: 701 if (sn->rule != -1) 702 printf(", nat rule %u", sn->rule); 703 break; 704 case PF_RDR: 705 if (sn->rule != -1) 706 printf(", rdr rule %u", sn->rule); 707 break; 708 case PF_PASS: 709 case PF_MATCH: 710 if (sn->rule != -1) 711 printf(", filter rule %u", sn->rule); 712 break; 713 } 714 printf(", %s", sn_type_names[sn->type]); 715 printf("\n"); 716 } 717 } 718 719 static void 720 print_eth_addr(const struct pfctl_eth_addr *a) 721 { 722 int i, masklen = ETHER_ADDR_LEN * 8; 723 bool seen_unset = false; 724 725 for (i = 0; i < ETHER_ADDR_LEN; i++) { 726 if (a->addr[i] != 0) 727 break; 728 } 729 730 /* Unset, so don't print anything. */ 731 if (i == ETHER_ADDR_LEN) 732 return; 733 734 printf("%s%02x:%02x:%02x:%02x:%02x:%02x", a->neg ? "! " : "", 735 a->addr[0], a->addr[1], a->addr[2], a->addr[3], a->addr[4], 736 a->addr[5]); 737 738 for (i = 0; i < (ETHER_ADDR_LEN * 8); i++) { 739 bool isset = a->mask[i / 8] & (1 << i % 8); 740 741 if (! seen_unset) { 742 if (isset) 743 continue; 744 seen_unset = true; 745 masklen = i; 746 } else { 747 /* Not actually a continuous mask, so print the whole 748 * thing. */ 749 if (isset) 750 break; 751 continue; 752 } 753 } 754 755 if (masklen == (ETHER_ADDR_LEN * 8)) 756 return; 757 758 if (i == (ETHER_ADDR_LEN * 8)) { 759 printf("/%d", masklen); 760 return; 761 } 762 763 printf("&%02x:%02x:%02x:%02x:%02x:%02x", 764 a->mask[0], a->mask[1], a->mask[2], a->mask[3], a->mask[4], 765 a->mask[5]); 766 } 767 768 void 769 print_eth_rule(struct pfctl_eth_rule *r, const char *anchor_call, 770 int rule_numbers) 771 { 772 static const char *actiontypes[] = { "pass", "block", "", "", "", "", 773 "", "", "", "", "", "", "match" }; 774 775 int i; 776 777 if (rule_numbers) 778 printf("@%u ", r->nr); 779 780 printf("ether "); 781 if (anchor_call[0]) { 782 if (anchor_call[0] == '_') { 783 printf("anchor"); 784 } else 785 printf("anchor \"%s\"", anchor_call); 786 } else { 787 printf("%s", actiontypes[r->action]); 788 } 789 if (r->direction == PF_IN) 790 printf(" in"); 791 else if (r->direction == PF_OUT) 792 printf(" out"); 793 794 if (r->quick) 795 printf(" quick"); 796 if (r->ifname[0]) { 797 if (r->ifnot) 798 printf(" on ! %s", r->ifname); 799 else 800 printf(" on %s", r->ifname); 801 } 802 if (r->bridge_to[0]) 803 printf(" bridge-to %s", r->bridge_to); 804 if (r->proto) 805 printf(" proto 0x%04x", r->proto); 806 807 if (r->src.isset) { 808 printf(" from "); 809 print_eth_addr(&r->src); 810 } 811 if (r->dst.isset) { 812 printf(" to "); 813 print_eth_addr(&r->dst); 814 } 815 printf(" l3"); 816 print_fromto(&r->ipsrc, PF_OSFP_ANY, &r->ipdst, 817 r->proto == ETHERTYPE_IP ? AF_INET : AF_INET6, 0, 818 0, 0); 819 820 i = 0; 821 while (r->label[i][0]) 822 printf(" label \"%s\"", r->label[i++]); 823 if (r->ridentifier) 824 printf(" ridentifier %u", r->ridentifier); 825 826 if (r->qname[0]) 827 printf(" queue %s", r->qname); 828 if (r->tagname[0]) 829 printf(" tag %s", r->tagname); 830 if (r->match_tagname[0]) { 831 if (r->match_tag_not) 832 printf(" !"); 833 printf(" tagged %s", r->match_tagname); 834 } 835 if (r->dnpipe) 836 printf(" %s %d", 837 r->dnflags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue", 838 r->dnpipe); 839 } 840 841 void 842 print_rule(struct pfctl_rule *r, const char *anchor_call, int verbose, int numeric) 843 { 844 static const char *actiontypes[] = { "pass", "block", "scrub", 845 "no scrub", "nat", "no nat", "binat", "no binat", "rdr", "no rdr", 846 "", "", "match"}; 847 static const char *anchortypes[] = { "anchor", "anchor", "anchor", 848 "anchor", "nat-anchor", "nat-anchor", "binat-anchor", 849 "binat-anchor", "rdr-anchor", "rdr-anchor" }; 850 int i, ropts; 851 char *p; 852 853 if (verbose) 854 printf("@%d ", r->nr); 855 if (r->action == PF_MATCH) 856 printf("match"); 857 else if (r->action > PF_NORDR) 858 printf("action(%d)", r->action); 859 else if (anchor_call[0]) { 860 p = strrchr(anchor_call, '/'); 861 if (p ? p[1] == '_' : anchor_call[0] == '_') 862 printf("%s", anchortypes[r->action]); 863 else 864 printf("%s \"%s\"", anchortypes[r->action], 865 anchor_call); 866 } else { 867 printf("%s", actiontypes[r->action]); 868 if (r->natpass) 869 printf(" pass"); 870 } 871 if (r->action == PF_DROP) { 872 if (r->rule_flag & PFRULE_RETURN) 873 printf(" return"); 874 else if (r->rule_flag & PFRULE_RETURNRST) { 875 if (!r->return_ttl) 876 printf(" return-rst"); 877 else 878 printf(" return-rst(ttl %d)", r->return_ttl); 879 } else if (r->rule_flag & PFRULE_RETURNICMP) { 880 const struct icmpcodeent *ic, *ic6; 881 882 ic = geticmpcodebynumber(r->return_icmp >> 8, 883 r->return_icmp & 255, AF_INET); 884 ic6 = geticmpcodebynumber(r->return_icmp6 >> 8, 885 r->return_icmp6 & 255, AF_INET6); 886 887 switch (r->af) { 888 case AF_INET: 889 printf(" return-icmp"); 890 if (ic == NULL) 891 printf("(%u)", r->return_icmp & 255); 892 else 893 printf("(%s)", ic->name); 894 break; 895 case AF_INET6: 896 printf(" return-icmp6"); 897 if (ic6 == NULL) 898 printf("(%u)", r->return_icmp6 & 255); 899 else 900 printf("(%s)", ic6->name); 901 break; 902 default: 903 printf(" return-icmp"); 904 if (ic == NULL) 905 printf("(%u, ", r->return_icmp & 255); 906 else 907 printf("(%s, ", ic->name); 908 if (ic6 == NULL) 909 printf("%u)", r->return_icmp6 & 255); 910 else 911 printf("%s)", ic6->name); 912 break; 913 } 914 } else 915 printf(" drop"); 916 } 917 if (r->direction == PF_IN) 918 printf(" in"); 919 else if (r->direction == PF_OUT) 920 printf(" out"); 921 if (r->log) { 922 printf(" log"); 923 if (r->log & ~PF_LOG || r->logif) { 924 int count = 0; 925 926 printf(" ("); 927 if (r->log & PF_LOG_ALL) 928 printf("%sall", count++ ? ", " : ""); 929 if (r->log & PF_LOG_MATCHES) 930 printf("%smatches", count++ ? ", " : ""); 931 if (r->log & PF_LOG_USER) 932 printf("%suser", count++ ? ", " : ""); 933 if (r->logif) 934 printf("%sto pflog%u", count++ ? ", " : "", 935 r->logif); 936 printf(")"); 937 } 938 } 939 if (r->quick) 940 printf(" quick"); 941 if (r->ifname[0]) { 942 if (r->ifnot) 943 printf(" on ! %s", r->ifname); 944 else 945 printf(" on %s", r->ifname); 946 } 947 if (r->rt) { 948 if (r->rt == PF_ROUTETO) 949 printf(" route-to"); 950 else if (r->rt == PF_REPLYTO) 951 printf(" reply-to"); 952 else if (r->rt == PF_DUPTO) 953 printf(" dup-to"); 954 printf(" "); 955 print_pool(&r->rdr, 0, 0, r->af, PF_PASS); 956 print_pool(&r->route, 0, 0, 957 r->rule_flag & PFRULE_AFTO && r->rt != PF_REPLYTO ? r->naf : r->af, 958 PF_PASS); 959 } 960 if (r->af) { 961 if (r->af == AF_INET) 962 printf(" inet"); 963 else 964 printf(" inet6"); 965 } 966 if (r->proto) { 967 const char *protoname; 968 969 if ((protoname = pfctl_proto2name(r->proto)) != NULL) 970 printf(" proto %s", protoname); 971 else 972 printf(" proto %u", r->proto); 973 } 974 print_fromto(&r->src, r->os_fingerprint, &r->dst, r->af, r->proto, 975 verbose, numeric); 976 if (r->rcv_ifname[0]) 977 printf(" %sreceived-on %s", r->rcvifnot ? "!" : "", 978 r->rcv_ifname); 979 if (r->uid.op) 980 print_ugid(r->uid.op, r->uid.uid[0], r->uid.uid[1], "user"); 981 if (r->gid.op) 982 print_ugid(r->gid.op, r->gid.gid[0], r->gid.gid[1], "group"); 983 if (r->flags || r->flagset) { 984 printf(" flags "); 985 print_flags(r->flags); 986 printf("/"); 987 print_flags(r->flagset); 988 } else if ((r->action == PF_PASS || r->action == PF_MATCH) && 989 (!r->proto || r->proto == IPPROTO_TCP) && 990 !(r->rule_flag & PFRULE_FRAGMENT) && 991 !anchor_call[0] && r->keep_state) 992 printf(" flags any"); 993 if (r->type) { 994 const struct icmptypeent *it; 995 996 it = geticmptypebynumber(r->type-1, r->af); 997 if (r->af != AF_INET6) 998 printf(" icmp-type"); 999 else 1000 printf(" icmp6-type"); 1001 if (it != NULL) 1002 printf(" %s", it->name); 1003 else 1004 printf(" %u", r->type-1); 1005 if (r->code) { 1006 const struct icmpcodeent *ic; 1007 1008 ic = geticmpcodebynumber(r->type-1, r->code-1, r->af); 1009 if (ic != NULL) 1010 printf(" code %s", ic->name); 1011 else 1012 printf(" code %u", r->code-1); 1013 } 1014 } 1015 if (r->tos) 1016 printf(" tos 0x%2.2x", r->tos); 1017 if (r->prio) 1018 printf(" prio %u", r->prio == PF_PRIO_ZERO ? 0 : r->prio); 1019 if (r->pktrate.limit) 1020 printf(" max-pkt-rate %u/%u", r->pktrate.limit, 1021 r->pktrate.seconds); 1022 if (r->max_pkt_size) 1023 printf( " max-pkt-size %u", r->max_pkt_size); 1024 if (r->scrub_flags & PFSTATE_SETMASK) { 1025 char *comma = ""; 1026 printf(" set ("); 1027 if (r->scrub_flags & PFSTATE_SETPRIO) { 1028 if (r->set_prio[0] == r->set_prio[1]) 1029 printf("%s prio %u", comma, r->set_prio[0]); 1030 else 1031 printf("%s prio(%u, %u)", comma, r->set_prio[0], 1032 r->set_prio[1]); 1033 comma = ","; 1034 } 1035 if (r->scrub_flags & PFSTATE_SETTOS) { 1036 printf("%s tos 0x%2.2x", comma, r->set_tos); 1037 comma = ","; 1038 } 1039 printf(" )"); 1040 } 1041 if (!r->keep_state && r->action == PF_PASS && !anchor_call[0]) 1042 printf(" no state"); 1043 else if (r->keep_state == PF_STATE_NORMAL) 1044 printf(" keep state"); 1045 else if (r->keep_state == PF_STATE_MODULATE) 1046 printf(" modulate state"); 1047 else if (r->keep_state == PF_STATE_SYNPROXY) 1048 printf(" synproxy state"); 1049 if (r->prob) { 1050 char buf[20]; 1051 1052 snprintf(buf, sizeof(buf), "%f", r->prob*100.0/(UINT_MAX+1.0)); 1053 for (i = strlen(buf)-1; i > 0; i--) { 1054 if (buf[i] == '0') 1055 buf[i] = '\0'; 1056 else { 1057 if (buf[i] == '.') 1058 buf[i] = '\0'; 1059 break; 1060 } 1061 } 1062 printf(" probability %s%%", buf); 1063 } 1064 ropts = 0; 1065 if (r->max_states || r->max_src_nodes || r->max_src_states) 1066 ropts = 1; 1067 if (r->rule_flag & PFRULE_NOSYNC) 1068 ropts = 1; 1069 if (r->rule_flag & PFRULE_SRCTRACK) 1070 ropts = 1; 1071 if (r->rule_flag & PFRULE_IFBOUND) 1072 ropts = 1; 1073 if (r->rule_flag & PFRULE_STATESLOPPY) 1074 ropts = 1; 1075 if (r->rule_flag & PFRULE_PFLOW) 1076 ropts = 1; 1077 for (i = 0; !ropts && i < PFTM_MAX; ++i) 1078 if (r->timeout[i]) 1079 ropts = 1; 1080 if (ropts) { 1081 printf(" ("); 1082 if (r->max_states) { 1083 printf("max %u", r->max_states); 1084 ropts = 0; 1085 } 1086 if (r->rule_flag & PFRULE_NOSYNC) { 1087 if (!ropts) 1088 printf(", "); 1089 printf("no-sync"); 1090 ropts = 0; 1091 } 1092 if (r->rule_flag & PFRULE_SRCTRACK) { 1093 if (!ropts) 1094 printf(", "); 1095 printf("source-track"); 1096 if (r->rule_flag & PFRULE_RULESRCTRACK) 1097 printf(" rule"); 1098 else 1099 printf(" global"); 1100 ropts = 0; 1101 } 1102 if (r->max_src_states) { 1103 if (!ropts) 1104 printf(", "); 1105 printf("max-src-states %u", r->max_src_states); 1106 ropts = 0; 1107 } 1108 if (r->max_src_conn) { 1109 if (!ropts) 1110 printf(", "); 1111 printf("max-src-conn %u", r->max_src_conn); 1112 ropts = 0; 1113 } 1114 if (r->max_src_conn_rate.limit) { 1115 if (!ropts) 1116 printf(", "); 1117 printf("max-src-conn-rate %u/%u", 1118 r->max_src_conn_rate.limit, 1119 r->max_src_conn_rate.seconds); 1120 ropts = 0; 1121 } 1122 if (r->max_src_nodes) { 1123 if (!ropts) 1124 printf(", "); 1125 printf("max-src-nodes %u", r->max_src_nodes); 1126 ropts = 0; 1127 } 1128 if (r->overload_tblname[0]) { 1129 if (!ropts) 1130 printf(", "); 1131 printf("overload <%s>", r->overload_tblname); 1132 if (r->flush) 1133 printf(" flush"); 1134 if (r->flush & PF_FLUSH_GLOBAL) 1135 printf(" global"); 1136 } 1137 if (r->rule_flag & PFRULE_IFBOUND) { 1138 if (!ropts) 1139 printf(", "); 1140 printf("if-bound"); 1141 ropts = 0; 1142 } 1143 if (r->rule_flag & PFRULE_STATESLOPPY) { 1144 if (!ropts) 1145 printf(", "); 1146 printf("sloppy"); 1147 ropts = 0; 1148 } 1149 if (r->rule_flag & PFRULE_PFLOW) { 1150 if (!ropts) 1151 printf(", "); 1152 printf("pflow"); 1153 ropts = 0; 1154 } 1155 for (i = 0; i < PFTM_MAX; ++i) 1156 if (r->timeout[i]) { 1157 int j; 1158 1159 if (!ropts) 1160 printf(", "); 1161 ropts = 0; 1162 for (j = 0; pf_timeouts[j].name != NULL; 1163 ++j) 1164 if (pf_timeouts[j].timeout == i) 1165 break; 1166 printf("%s %u", pf_timeouts[j].name == NULL ? 1167 "inv.timeout" : pf_timeouts[j].name, 1168 r->timeout[i]); 1169 } 1170 printf(")"); 1171 } 1172 if (r->allow_opts) 1173 printf(" allow-opts"); 1174 if (r->rule_flag & PFRULE_FRAGMENT) 1175 printf(" fragment"); 1176 if (r->action == PF_SCRUB) { 1177 /* Scrub flags for old-style scrub. */ 1178 if (r->rule_flag & PFRULE_NODF) 1179 printf(" no-df"); 1180 if (r->rule_flag & PFRULE_RANDOMID) 1181 printf(" random-id"); 1182 if (r->min_ttl) 1183 printf(" min-ttl %d", r->min_ttl); 1184 if (r->max_mss) 1185 printf(" max-mss %d", r->max_mss); 1186 if (r->rule_flag & PFRULE_SET_TOS) 1187 printf(" set-tos 0x%2.2x", r->set_tos); 1188 if (r->rule_flag & PFRULE_REASSEMBLE_TCP) 1189 printf(" reassemble tcp"); 1190 /* The PFRULE_FRAGMENT_NOREASS is set on all rules by default! */ 1191 printf(" fragment %sreassemble", 1192 r->rule_flag & PFRULE_FRAGMENT_NOREASS ? "no " : ""); 1193 } else if (r->scrub_flags & PFSTATE_SCRUBMASK || r->min_ttl || r->max_mss) { 1194 /* Scrub actions on normal rules. */ 1195 printf(" scrub("); 1196 if (r->scrub_flags & PFSTATE_NODF) 1197 printf(" no-df"); 1198 if (r->scrub_flags & PFSTATE_RANDOMID) 1199 printf(" random-id"); 1200 if (r->min_ttl) 1201 printf(" min-ttl %d", r->min_ttl); 1202 if (r->scrub_flags & PFSTATE_SETTOS) 1203 printf(" set-tos 0x%2.2x", r->set_tos); 1204 if (r->scrub_flags & PFSTATE_SCRUB_TCP) 1205 printf(" reassemble tcp"); 1206 if (r->max_mss) 1207 printf(" max-mss %d", r->max_mss); 1208 printf(")"); 1209 } 1210 i = 0; 1211 while (r->label[i][0]) 1212 printf(" label \"%s\"", r->label[i++]); 1213 if (r->ridentifier) 1214 printf(" ridentifier %u", r->ridentifier); 1215 /* Only dnrpipe as we might do (0, 42) to only queue return traffic. */ 1216 if (r->dnrpipe) 1217 printf(" %s(%d, %d)", 1218 r->free_flags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue", 1219 r->dnpipe, r->dnrpipe); 1220 else if (r->dnpipe) 1221 printf(" %s %d", 1222 r->free_flags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue", 1223 r->dnpipe); 1224 if (r->qname[0] && r->pqname[0]) 1225 printf(" queue(%s, %s)", r->qname, r->pqname); 1226 else if (r->qname[0]) 1227 printf(" queue %s", r->qname); 1228 if (r->tagname[0]) 1229 printf(" tag %s", r->tagname); 1230 if (r->match_tagname[0]) { 1231 if (r->match_tag_not) 1232 printf(" !"); 1233 printf(" tagged %s", r->match_tagname); 1234 } 1235 if (r->rtableid != -1) 1236 printf(" rtable %u", r->rtableid); 1237 if (r->divert.port) { 1238 #ifdef __FreeBSD__ 1239 printf(" divert-to %u", ntohs(r->divert.port)); 1240 #else 1241 if (PF_AZERO(&r->divert.addr, r->af)) { 1242 printf(" divert-reply"); 1243 } else { 1244 printf(" divert-to "); 1245 print_addr_str(r->af, &r->divert.addr); 1246 printf(" port %u", ntohs(r->divert.port)); 1247 } 1248 #endif 1249 } 1250 if (anchor_call[0]) 1251 return; 1252 if (r->action == PF_NAT || r->action == PF_BINAT || r->action == PF_RDR) { 1253 printf(" -> "); 1254 print_pool(&r->rdr, r->rdr.proxy_port[0], 1255 r->rdr.proxy_port[1], r->af, r->action); 1256 } else { 1257 if (!TAILQ_EMPTY(&r->nat.list)) { 1258 if (r->rule_flag & PFRULE_AFTO) { 1259 printf(" af-to %s from ", r->naf == AF_INET ? "inet" : "inet6"); 1260 } else { 1261 printf(" nat-to "); 1262 } 1263 print_pool(&r->nat, r->nat.proxy_port[0], 1264 r->nat.proxy_port[1], r->naf ? r->naf : r->af, 1265 PF_NAT); 1266 } 1267 if (!TAILQ_EMPTY(&r->rdr.list)) { 1268 if (r->rule_flag & PFRULE_AFTO) { 1269 printf(" to "); 1270 } else { 1271 printf(" rdr-to "); 1272 } 1273 print_pool(&r->rdr, r->rdr.proxy_port[0], 1274 r->rdr.proxy_port[1], r->naf ? r->naf : r->af, 1275 PF_RDR); 1276 } 1277 } 1278 } 1279 1280 void 1281 print_tabledef(const char *name, int flags, int addrs, 1282 struct node_tinithead *nodes) 1283 { 1284 struct node_tinit *ti, *nti; 1285 struct node_host *h; 1286 1287 printf("table <%s>", name); 1288 if (flags & PFR_TFLAG_CONST) 1289 printf(" const"); 1290 if (flags & PFR_TFLAG_PERSIST) 1291 printf(" persist"); 1292 if (flags & PFR_TFLAG_COUNTERS) 1293 printf(" counters"); 1294 SIMPLEQ_FOREACH(ti, nodes, entries) { 1295 if (ti->file) { 1296 printf(" file \"%s\"", ti->file); 1297 continue; 1298 } 1299 printf(" {"); 1300 for (;;) { 1301 for (h = ti->host; h != NULL; h = h->next) { 1302 printf(h->not ? " !" : " "); 1303 print_addr(&h->addr, h->af, 0); 1304 } 1305 nti = SIMPLEQ_NEXT(ti, entries); 1306 if (nti != NULL && nti->file == NULL) 1307 ti = nti; /* merge lists */ 1308 else 1309 break; 1310 } 1311 printf(" }"); 1312 } 1313 if (addrs && SIMPLEQ_EMPTY(nodes)) 1314 printf(" { }"); 1315 printf("\n"); 1316 } 1317 1318 int 1319 parse_flags(char *s) 1320 { 1321 char *p, *q; 1322 uint16_t f = 0; 1323 1324 for (p = s; *p; p++) { 1325 if ((q = strchr(tcpflags, *p)) == NULL) 1326 return -1; 1327 else 1328 f |= 1 << (q - tcpflags); 1329 } 1330 return (f ? f : TH_FLAGS); 1331 } 1332 1333 void 1334 set_ipmask(struct node_host *h, int bb) 1335 { 1336 struct pf_addr *m, *n; 1337 int i, j = 0; 1338 uint8_t b; 1339 1340 m = &h->addr.v.a.mask; 1341 memset(m, 0, sizeof(*m)); 1342 1343 if (bb == -1) 1344 b = h->af == AF_INET ? 32 : 128; 1345 else 1346 b = bb; 1347 1348 while (b >= 32) { 1349 m->addr32[j++] = 0xffffffff; 1350 b -= 32; 1351 } 1352 for (i = 31; i > 31-b; --i) 1353 m->addr32[j] |= (1 << i); 1354 if (b) 1355 m->addr32[j] = htonl(m->addr32[j]); 1356 1357 /* Mask off bits of the address that will never be used. */ 1358 n = &h->addr.v.a.addr; 1359 if (h->addr.type == PF_ADDR_ADDRMASK) 1360 for (i = 0; i < 4; i++) 1361 n->addr32[i] = n->addr32[i] & m->addr32[i]; 1362 } 1363 1364 int 1365 check_netmask(struct node_host *h, sa_family_t af) 1366 { 1367 struct node_host *n = NULL; 1368 struct pf_addr *m; 1369 1370 for (n = h; n != NULL; n = n->next) { 1371 if (h->addr.type == PF_ADDR_TABLE) 1372 continue; 1373 m = &h->addr.v.a.mask; 1374 /* netmasks > 32 bit are invalid on v4 */ 1375 if (af == AF_INET && 1376 (m->addr32[1] || m->addr32[2] || m->addr32[3])) { 1377 fprintf(stderr, "netmask %u invalid for IPv4 address\n", 1378 unmask(m)); 1379 return (1); 1380 } 1381 } 1382 return (0); 1383 } 1384 1385 struct node_host * 1386 gen_dynnode(struct node_host *h, sa_family_t af) 1387 { 1388 struct node_host *n; 1389 1390 if (h->addr.type != PF_ADDR_DYNIFTL) 1391 return (NULL); 1392 1393 if ((n = calloc(1, sizeof(*n))) == NULL) 1394 return (NULL); 1395 bcopy(h, n, sizeof(*n)); 1396 n->ifname = NULL; 1397 n->next = NULL; 1398 n->tail = NULL; 1399 1400 /* fix up netmask */ 1401 if (af == AF_INET && unmask(&n->addr.v.a.mask) > 32) 1402 set_ipmask(n, 32); 1403 1404 return (n); 1405 } 1406 1407 /* interface lookup routines */ 1408 1409 static struct node_host *iftab; 1410 1411 /* 1412 * Retrieve the list of groups this interface is a member of and make sure 1413 * each group is in the group map. 1414 */ 1415 static void 1416 ifa_add_groups_to_map(char *ifa_name) 1417 { 1418 int s, len; 1419 struct ifgroupreq ifgr; 1420 struct ifg_req *ifg; 1421 1422 s = get_query_socket(); 1423 1424 /* Get size of group list for this interface */ 1425 memset(&ifgr, 0, sizeof(ifgr)); 1426 strlcpy(ifgr.ifgr_name, ifa_name, IFNAMSIZ); 1427 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) 1428 err(1, "SIOCGIFGROUP"); 1429 1430 /* Retrieve group list for this interface */ 1431 len = ifgr.ifgr_len; 1432 ifgr.ifgr_groups = 1433 (struct ifg_req *)calloc(len / sizeof(struct ifg_req), 1434 sizeof(struct ifg_req)); 1435 if (ifgr.ifgr_groups == NULL) 1436 err(1, "calloc"); 1437 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) 1438 err(1, "SIOCGIFGROUP"); 1439 1440 ifg = ifgr.ifgr_groups; 1441 for (; ifg && len >= sizeof(struct ifg_req); ifg++) { 1442 len -= sizeof(struct ifg_req); 1443 if (strcmp(ifg->ifgrq_group, "all")) { 1444 ENTRY item; 1445 ENTRY *ret_item; 1446 int *answer; 1447 1448 item.key = ifg->ifgrq_group; 1449 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0) { 1450 struct ifgroupreq ifgr2; 1451 1452 /* Don't know the answer yet */ 1453 if ((answer = malloc(sizeof(int))) == NULL) 1454 err(1, "malloc"); 1455 1456 bzero(&ifgr2, sizeof(ifgr2)); 1457 strlcpy(ifgr2.ifgr_name, ifg->ifgrq_group, 1458 sizeof(ifgr2.ifgr_name)); 1459 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr2) == 0) 1460 *answer = ifgr2.ifgr_len; 1461 else 1462 *answer = 0; 1463 1464 item.key = strdup(ifg->ifgrq_group); 1465 item.data = answer; 1466 if (hsearch_r(item, ENTER, &ret_item, 1467 &isgroup_map) == 0) 1468 err(1, "interface group query response" 1469 " map insert"); 1470 } 1471 } 1472 } 1473 free(ifgr.ifgr_groups); 1474 } 1475 1476 void 1477 ifa_load(void) 1478 { 1479 struct ifaddrs *ifap, *ifa; 1480 struct node_host *n = NULL, *h = NULL; 1481 1482 if (getifaddrs(&ifap) < 0) 1483 err(1, "getifaddrs"); 1484 1485 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1486 if (ifa->ifa_addr == NULL || 1487 !(ifa->ifa_addr->sa_family == AF_INET || 1488 ifa->ifa_addr->sa_family == AF_INET6 || 1489 ifa->ifa_addr->sa_family == AF_LINK)) 1490 continue; 1491 n = calloc(1, sizeof(struct node_host)); 1492 if (n == NULL) 1493 err(1, "%s: calloc", __func__); 1494 n->af = ifa->ifa_addr->sa_family; 1495 n->ifa_flags = ifa->ifa_flags; 1496 #ifdef __KAME__ 1497 if (n->af == AF_INET6 && 1498 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) 1499 ifa->ifa_addr)->sin6_addr) && 1500 ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id == 1501 0) { 1502 struct sockaddr_in6 *sin6; 1503 1504 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1505 sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 | 1506 sin6->sin6_addr.s6_addr[3]; 1507 sin6->sin6_addr.s6_addr[2] = 0; 1508 sin6->sin6_addr.s6_addr[3] = 0; 1509 } 1510 #endif 1511 n->ifindex = 0; 1512 if (n->af == AF_LINK) { 1513 n->ifindex = ((struct sockaddr_dl *) 1514 ifa->ifa_addr)->sdl_index; 1515 ifa_add_groups_to_map(ifa->ifa_name); 1516 } else { 1517 copy_satopfaddr(&n->addr.v.a.addr, ifa->ifa_addr); 1518 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family; 1519 copy_satopfaddr(&n->addr.v.a.mask, ifa->ifa_netmask); 1520 if (ifa->ifa_broadaddr != NULL) { 1521 ifa->ifa_broadaddr->sa_family = ifa->ifa_addr->sa_family; 1522 copy_satopfaddr(&n->bcast, ifa->ifa_broadaddr); 1523 } 1524 if (ifa->ifa_dstaddr != NULL) { 1525 ifa->ifa_dstaddr->sa_family = ifa->ifa_addr->sa_family; 1526 copy_satopfaddr(&n->peer, ifa->ifa_dstaddr); 1527 } 1528 if (n->af == AF_INET6) 1529 n->ifindex = ((struct sockaddr_in6 *) 1530 ifa->ifa_addr) ->sin6_scope_id; 1531 } 1532 if ((n->ifname = strdup(ifa->ifa_name)) == NULL) 1533 err(1, "%s: strdup", __func__); 1534 n->next = NULL; 1535 n->tail = n; 1536 if (h == NULL) 1537 h = n; 1538 else { 1539 h->tail->next = n; 1540 h->tail = n; 1541 } 1542 } 1543 1544 iftab = h; 1545 freeifaddrs(ifap); 1546 } 1547 1548 static int 1549 get_socket_domain(void) 1550 { 1551 int sdom; 1552 1553 sdom = AF_UNSPEC; 1554 #ifdef WITH_INET6 1555 if (sdom == AF_UNSPEC && feature_present("inet6")) 1556 sdom = AF_INET6; 1557 #endif 1558 #ifdef WITH_INET 1559 if (sdom == AF_UNSPEC && feature_present("inet")) 1560 sdom = AF_INET; 1561 #endif 1562 if (sdom == AF_UNSPEC) 1563 sdom = AF_LINK; 1564 1565 return (sdom); 1566 } 1567 1568 int 1569 get_query_socket(void) 1570 { 1571 static int s = -1; 1572 1573 if (s == -1) { 1574 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) == -1) 1575 err(1, "socket"); 1576 } 1577 1578 return (s); 1579 } 1580 1581 /* 1582 * Returns the response len if the name is a group, otherwise returns 0. 1583 */ 1584 static int 1585 is_a_group(char *name) 1586 { 1587 ENTRY item; 1588 ENTRY *ret_item; 1589 1590 item.key = name; 1591 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0) 1592 return (0); 1593 1594 return (*(int *)ret_item->data); 1595 } 1596 1597 unsigned int 1598 ifa_nametoindex(const char *ifa_name) 1599 { 1600 struct node_host *p; 1601 1602 for (p = iftab; p; p = p->next) { 1603 if (p->af == AF_LINK && strcmp(p->ifname, ifa_name) == 0) 1604 return (p->ifindex); 1605 } 1606 errno = ENXIO; 1607 return (0); 1608 } 1609 1610 char * 1611 ifa_indextoname(unsigned int ifindex, char *ifa_name) 1612 { 1613 struct node_host *p; 1614 1615 for (p = iftab; p; p = p->next) { 1616 if (p->af == AF_LINK && ifindex == p->ifindex) { 1617 strlcpy(ifa_name, p->ifname, IFNAMSIZ); 1618 return (ifa_name); 1619 } 1620 } 1621 errno = ENXIO; 1622 return (NULL); 1623 } 1624 1625 struct node_host * 1626 ifa_exists(char *ifa_name) 1627 { 1628 struct node_host *n; 1629 1630 if (iftab == NULL) 1631 ifa_load(); 1632 1633 /* check whether this is a group */ 1634 if (is_a_group(ifa_name)) { 1635 /* fake a node_host */ 1636 if ((n = calloc(1, sizeof(*n))) == NULL) 1637 err(1, "calloc"); 1638 if ((n->ifname = strdup(ifa_name)) == NULL) 1639 err(1, "strdup"); 1640 return (n); 1641 } 1642 1643 for (n = iftab; n; n = n->next) { 1644 if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ)) 1645 return (n); 1646 } 1647 1648 return (NULL); 1649 } 1650 1651 struct node_host * 1652 ifa_grouplookup(char *ifa_name, int flags) 1653 { 1654 struct ifg_req *ifg; 1655 struct ifgroupreq ifgr; 1656 int s, len; 1657 struct node_host *n, *h = NULL; 1658 1659 s = get_query_socket(); 1660 len = is_a_group(ifa_name); 1661 if (len == 0) 1662 return (NULL); 1663 bzero(&ifgr, sizeof(ifgr)); 1664 strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name)); 1665 ifgr.ifgr_len = len; 1666 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) 1667 err(1, "calloc"); 1668 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) 1669 err(1, "SIOCGIFGMEMB"); 1670 1671 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req); 1672 ifg++) { 1673 len -= sizeof(struct ifg_req); 1674 if ((n = ifa_lookup(ifg->ifgrq_member, flags)) == NULL) 1675 continue; 1676 if (h == NULL) 1677 h = n; 1678 else { 1679 h->tail->next = n; 1680 h->tail = n->tail; 1681 } 1682 } 1683 free(ifgr.ifgr_groups); 1684 1685 return (h); 1686 } 1687 1688 struct node_host * 1689 ifa_lookup(char *ifa_name, int flags) 1690 { 1691 struct node_host *p = NULL, *h = NULL, *n = NULL; 1692 int got4 = 0, got6 = 0; 1693 const char *last_if = NULL; 1694 1695 /* first load iftab and isgroup_map */ 1696 if (iftab == NULL) 1697 ifa_load(); 1698 1699 if ((h = ifa_grouplookup(ifa_name, flags)) != NULL) 1700 return (h); 1701 1702 if (!strncmp(ifa_name, "self", IFNAMSIZ)) 1703 ifa_name = NULL; 1704 1705 for (p = iftab; p; p = p->next) { 1706 if (ifa_skip_if(ifa_name, p)) 1707 continue; 1708 if ((flags & PFI_AFLAG_BROADCAST) && p->af != AF_INET) 1709 continue; 1710 if ((flags & PFI_AFLAG_BROADCAST) && 1711 !(p->ifa_flags & IFF_BROADCAST)) 1712 continue; 1713 if ((flags & PFI_AFLAG_BROADCAST) && p->bcast.v4.s_addr == 0) 1714 continue; 1715 if ((flags & PFI_AFLAG_PEER) && 1716 !(p->ifa_flags & IFF_POINTOPOINT)) 1717 continue; 1718 if ((flags & PFI_AFLAG_NETWORK) && p->ifindex > 0) 1719 continue; 1720 if (last_if == NULL || strcmp(last_if, p->ifname)) 1721 got4 = got6 = 0; 1722 last_if = p->ifname; 1723 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET && got4) 1724 continue; 1725 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && 1726 IN6_IS_ADDR_LINKLOCAL(&p->addr.v.a.addr.v6)) 1727 continue; 1728 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && got6) 1729 continue; 1730 if (p->af == AF_INET) 1731 got4 = 1; 1732 else 1733 got6 = 1; 1734 n = calloc(1, sizeof(struct node_host)); 1735 if (n == NULL) 1736 err(1, "%s: calloc", __func__); 1737 n->af = p->af; 1738 if (flags & PFI_AFLAG_BROADCAST) 1739 memcpy(&n->addr.v.a.addr, &p->bcast, 1740 sizeof(struct pf_addr)); 1741 else if (flags & PFI_AFLAG_PEER) 1742 memcpy(&n->addr.v.a.addr, &p->peer, 1743 sizeof(struct pf_addr)); 1744 else 1745 memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr, 1746 sizeof(struct pf_addr)); 1747 if (flags & PFI_AFLAG_NETWORK) 1748 set_ipmask(n, unmask(&p->addr.v.a.mask)); 1749 else 1750 set_ipmask(n, -1); 1751 n->ifindex = p->ifindex; 1752 n->ifname = strdup(p->ifname); 1753 1754 n->next = NULL; 1755 n->tail = n; 1756 if (h == NULL) 1757 h = n; 1758 else { 1759 h->tail->next = n; 1760 h->tail = n; 1761 } 1762 } 1763 return (h); 1764 } 1765 1766 int 1767 ifa_skip_if(const char *filter, struct node_host *p) 1768 { 1769 int n; 1770 1771 if (p->af != AF_INET && p->af != AF_INET6) 1772 return (1); 1773 if (filter == NULL || !*filter) 1774 return (0); 1775 if (!strcmp(p->ifname, filter)) 1776 return (0); /* exact match */ 1777 n = strlen(filter); 1778 if (n < 1 || n >= IFNAMSIZ) 1779 return (1); /* sanity check */ 1780 if (filter[n-1] >= '0' && filter[n-1] <= '9') 1781 return (1); /* only do exact match in that case */ 1782 if (strncmp(p->ifname, filter, n)) 1783 return (1); /* prefix doesn't match */ 1784 return (p->ifname[n] < '0' || p->ifname[n] > '9'); 1785 } 1786 1787 1788 struct node_host * 1789 host(const char *s, int opts) 1790 { 1791 struct node_host *h = NULL; 1792 int mask = -1; 1793 char *p, *ps; 1794 const char *errstr; 1795 1796 if ((p = strchr(s, '/')) != NULL) { 1797 mask = strtonum(p+1, 0, 128, &errstr); 1798 if (errstr) { 1799 fprintf(stderr, "netmask is %s: %s\n", errstr, p); 1800 goto error; 1801 } 1802 if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL) 1803 err(1, "%s: malloc", __func__); 1804 strlcpy(ps, s, strlen(s) - strlen(p) + 1); 1805 } else { 1806 if ((ps = strdup(s)) == NULL) 1807 err(1, "%s: strdup", __func__); 1808 } 1809 1810 if ((h = host_ip(ps, mask)) == NULL && 1811 (h = host_if(ps, mask)) == NULL && 1812 (h = host_dns(ps, mask, (opts & PF_OPT_NODNS))) == NULL) { 1813 fprintf(stderr, "no IP address found for %s\n", s); 1814 goto error; 1815 } 1816 1817 error: 1818 free(ps); 1819 return (h); 1820 } 1821 1822 struct node_host * 1823 host_if(const char *s, int mask) 1824 { 1825 struct node_host *n, *h = NULL; 1826 char *p, *ps; 1827 int flags = 0; 1828 1829 if ((ps = strdup(s)) == NULL) 1830 err(1, "host_if: strdup"); 1831 while ((p = strrchr(ps, ':')) != NULL) { 1832 if (!strcmp(p+1, "network")) 1833 flags |= PFI_AFLAG_NETWORK; 1834 else if (!strcmp(p+1, "broadcast")) 1835 flags |= PFI_AFLAG_BROADCAST; 1836 else if (!strcmp(p+1, "peer")) 1837 flags |= PFI_AFLAG_PEER; 1838 else if (!strcmp(p+1, "0")) 1839 flags |= PFI_AFLAG_NOALIAS; 1840 else 1841 goto error; 1842 *p = '\0'; 1843 } 1844 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { /* Yep! */ 1845 fprintf(stderr, "illegal combination of interface modifiers\n"); 1846 goto error; 1847 } 1848 if ((flags & (PFI_AFLAG_NETWORK|PFI_AFLAG_BROADCAST)) && mask > -1) { 1849 fprintf(stderr, "network or broadcast lookup, but " 1850 "extra netmask given\n"); 1851 goto error; 1852 } 1853 if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) { 1854 /* interface with this name exists */ 1855 h = ifa_lookup(ps, flags); 1856 if (mask > -1) 1857 for (n = h; n != NULL; n = n->next) 1858 set_ipmask(n, mask); 1859 } 1860 1861 error: 1862 free(ps); 1863 return (h); 1864 } 1865 1866 struct node_host * 1867 host_ip(const char *s, int mask) 1868 { 1869 struct addrinfo hints, *res; 1870 struct node_host *h = NULL; 1871 1872 h = calloc(1, sizeof(*h)); 1873 if (h == NULL) 1874 err(1, "%s: calloc", __func__); 1875 if (mask != -1) { 1876 /* Try to parse 10/8 */ 1877 h->af = AF_INET; 1878 if (inet_net_pton(AF_INET, s, &h->addr.v.a.addr.v4, 1879 sizeof(h->addr.v.a.addr.v4)) != -1) 1880 goto out; 1881 } 1882 1883 memset(&hints, 0, sizeof(hints)); 1884 hints.ai_family = AF_UNSPEC; 1885 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1886 hints.ai_flags = AI_NUMERICHOST; 1887 if (getaddrinfo(s, NULL, &hints, &res) == 0) { 1888 h->af = res->ai_family; 1889 copy_satopfaddr(&h->addr.v.a.addr, res->ai_addr); 1890 if (h->af == AF_INET6) 1891 h->ifindex = 1892 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1893 freeaddrinfo(res); 1894 } else { 1895 free(h); 1896 return (NULL); 1897 } 1898 out: 1899 set_ipmask(h, mask); 1900 h->ifname = NULL; 1901 h->next = NULL; 1902 h->tail = h; 1903 1904 return (h); 1905 } 1906 1907 struct node_host * 1908 host_dns(const char *s, int mask, int numeric) 1909 { 1910 struct addrinfo hints, *res0, *res; 1911 struct node_host *n, *h = NULL; 1912 int noalias = 0, got4 = 0, got6 = 0; 1913 char *p, *ps; 1914 1915 if ((ps = strdup(s)) == NULL) 1916 err(1, "host_dns: strdup"); 1917 if ((p = strrchr(ps, ':')) != NULL && !strcmp(p, ":0")) { 1918 noalias = 1; 1919 *p = '\0'; 1920 } 1921 memset(&hints, 0, sizeof(hints)); 1922 hints.ai_family = PF_UNSPEC; 1923 hints.ai_socktype = SOCK_STREAM; /* DUMMY */ 1924 if (numeric) 1925 hints.ai_flags = AI_NUMERICHOST; 1926 if (getaddrinfo(ps, NULL, &hints, &res0) != 0) 1927 goto error; 1928 1929 for (res = res0; res; res = res->ai_next) { 1930 if (res->ai_family != AF_INET && 1931 res->ai_family != AF_INET6) 1932 continue; 1933 if (noalias) { 1934 if (res->ai_family == AF_INET) { 1935 if (got4) 1936 continue; 1937 got4 = 1; 1938 } else { 1939 if (got6) 1940 continue; 1941 got6 = 1; 1942 } 1943 } 1944 n = calloc(1, sizeof(struct node_host)); 1945 if (n == NULL) 1946 err(1, "host_dns: calloc"); 1947 n->ifname = NULL; 1948 n->af = res->ai_family; 1949 copy_satopfaddr(&n->addr.v.a.addr, res->ai_addr); 1950 if (res->ai_family == AF_INET6) 1951 n->ifindex = 1952 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1953 set_ipmask(n, mask); 1954 n->next = NULL; 1955 n->tail = n; 1956 if (h == NULL) 1957 h = n; 1958 else { 1959 h->tail->next = n; 1960 h->tail = n; 1961 } 1962 } 1963 freeaddrinfo(res0); 1964 error: 1965 free(ps); 1966 1967 return (h); 1968 } 1969 1970 /* 1971 * convert a hostname to a list of addresses and put them in the given buffer. 1972 * test: 1973 * if set to 1, only simple addresses are accepted (no netblock, no "!"). 1974 */ 1975 int 1976 append_addr(struct pfr_buffer *b, char *s, int test, int opts) 1977 { 1978 char *r; 1979 struct node_host *h, *n; 1980 int rv, not = 0; 1981 1982 for (r = s; *r == '!'; r++) 1983 not = !not; 1984 if ((n = host(r, opts)) == NULL) { 1985 errno = 0; 1986 return (-1); 1987 } 1988 rv = append_addr_host(b, n, test, not); 1989 do { 1990 h = n; 1991 n = n->next; 1992 free(h); 1993 } while (n != NULL); 1994 return (rv); 1995 } 1996 1997 /* 1998 * same as previous function, but with a pre-parsed input and the ability 1999 * to "negate" the result. Does not free the node_host list. 2000 * not: 2001 * setting it to 1 is equivalent to adding "!" in front of parameter s. 2002 */ 2003 int 2004 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not) 2005 { 2006 int bits; 2007 struct pfr_addr addr; 2008 2009 do { 2010 bzero(&addr, sizeof(addr)); 2011 addr.pfra_not = n->not ^ not; 2012 addr.pfra_af = n->af; 2013 addr.pfra_net = unmask(&n->addr.v.a.mask); 2014 switch (n->af) { 2015 case AF_INET: 2016 addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0]; 2017 bits = 32; 2018 break; 2019 case AF_INET6: 2020 memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6, 2021 sizeof(struct in6_addr)); 2022 bits = 128; 2023 break; 2024 default: 2025 errno = EINVAL; 2026 return (-1); 2027 } 2028 if ((test && (not || addr.pfra_net != bits)) || 2029 addr.pfra_net > bits) { 2030 errno = EINVAL; 2031 return (-1); 2032 } 2033 if (pfr_buf_add(b, &addr)) 2034 return (-1); 2035 } while ((n = n->next) != NULL); 2036 2037 return (0); 2038 } 2039 2040 int 2041 pfctl_add_trans(struct pfr_buffer *buf, int rs_num, const char *anchor) 2042 { 2043 struct pfioc_trans_e trans; 2044 2045 bzero(&trans, sizeof(trans)); 2046 trans.rs_num = rs_num; 2047 if (strlcpy(trans.anchor, anchor, 2048 sizeof(trans.anchor)) >= sizeof(trans.anchor)) 2049 errx(1, "pfctl_add_trans: strlcpy"); 2050 2051 return pfr_buf_add(buf, &trans); 2052 } 2053 2054 u_int32_t 2055 pfctl_get_ticket(struct pfr_buffer *buf, int rs_num, const char *anchor) 2056 { 2057 struct pfioc_trans_e *p; 2058 2059 PFRB_FOREACH(p, buf) 2060 if (rs_num == p->rs_num && !strcmp(anchor, p->anchor)) 2061 return (p->ticket); 2062 errx(1, "pfctl_get_ticket: assertion failed"); 2063 } 2064 2065 int 2066 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from) 2067 { 2068 struct pfioc_trans trans; 2069 2070 bzero(&trans, sizeof(trans)); 2071 trans.size = buf->pfrb_size - from; 2072 trans.esize = sizeof(struct pfioc_trans_e); 2073 trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from; 2074 return ioctl(dev, cmd, &trans); 2075 } 2076