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