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 /* fix up netmask for dynaddr */ 1316 if (af == AF_INET && h->addr.type == PF_ADDR_DYNIFTL && 1317 unmask(m, AF_INET6) > 32) 1318 set_ipmask(n, 32); 1319 /* netmasks > 32 bit are invalid on v4 */ 1320 if (af == AF_INET && 1321 (m->addr32[1] || m->addr32[2] || m->addr32[3])) { 1322 fprintf(stderr, "netmask %u invalid for IPv4 address\n", 1323 unmask(m, AF_INET6)); 1324 return (1); 1325 } 1326 } 1327 return (0); 1328 } 1329 1330 /* interface lookup routines */ 1331 1332 static struct node_host *iftab; 1333 1334 /* 1335 * Retrieve the list of groups this interface is a member of and make sure 1336 * each group is in the group map. 1337 */ 1338 static void 1339 ifa_add_groups_to_map(char *ifa_name) 1340 { 1341 int s, len; 1342 struct ifgroupreq ifgr; 1343 struct ifg_req *ifg; 1344 1345 s = get_query_socket(); 1346 1347 /* Get size of group list for this interface */ 1348 memset(&ifgr, 0, sizeof(ifgr)); 1349 strlcpy(ifgr.ifgr_name, ifa_name, IFNAMSIZ); 1350 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) 1351 err(1, "SIOCGIFGROUP"); 1352 1353 /* Retrieve group list for this interface */ 1354 len = ifgr.ifgr_len; 1355 ifgr.ifgr_groups = 1356 (struct ifg_req *)calloc(len / sizeof(struct ifg_req), 1357 sizeof(struct ifg_req)); 1358 if (ifgr.ifgr_groups == NULL) 1359 err(1, "calloc"); 1360 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) 1361 err(1, "SIOCGIFGROUP"); 1362 1363 ifg = ifgr.ifgr_groups; 1364 for (; ifg && len >= sizeof(struct ifg_req); ifg++) { 1365 len -= sizeof(struct ifg_req); 1366 if (strcmp(ifg->ifgrq_group, "all")) { 1367 ENTRY item; 1368 ENTRY *ret_item; 1369 int *answer; 1370 1371 item.key = ifg->ifgrq_group; 1372 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0) { 1373 struct ifgroupreq ifgr2; 1374 1375 /* Don't know the answer yet */ 1376 if ((answer = malloc(sizeof(int))) == NULL) 1377 err(1, "malloc"); 1378 1379 bzero(&ifgr2, sizeof(ifgr2)); 1380 strlcpy(ifgr2.ifgr_name, ifg->ifgrq_group, 1381 sizeof(ifgr2.ifgr_name)); 1382 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr2) == 0) 1383 *answer = ifgr2.ifgr_len; 1384 else 1385 *answer = 0; 1386 1387 item.key = strdup(ifg->ifgrq_group); 1388 item.data = answer; 1389 if (hsearch_r(item, ENTER, &ret_item, 1390 &isgroup_map) == 0) 1391 err(1, "interface group query response" 1392 " map insert"); 1393 } 1394 } 1395 } 1396 free(ifgr.ifgr_groups); 1397 } 1398 1399 void 1400 ifa_load(void) 1401 { 1402 struct ifaddrs *ifap, *ifa; 1403 struct node_host *n = NULL, *h = NULL; 1404 1405 if (getifaddrs(&ifap) < 0) 1406 err(1, "getifaddrs"); 1407 1408 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1409 if (!(ifa->ifa_addr->sa_family == AF_INET || 1410 ifa->ifa_addr->sa_family == AF_INET6 || 1411 ifa->ifa_addr->sa_family == AF_LINK)) 1412 continue; 1413 n = calloc(1, sizeof(struct node_host)); 1414 if (n == NULL) 1415 err(1, "address: calloc"); 1416 n->af = ifa->ifa_addr->sa_family; 1417 n->ifa_flags = ifa->ifa_flags; 1418 #ifdef __KAME__ 1419 if (n->af == AF_INET6 && 1420 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) 1421 ifa->ifa_addr)->sin6_addr) && 1422 ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id == 1423 0) { 1424 struct sockaddr_in6 *sin6; 1425 1426 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1427 sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 | 1428 sin6->sin6_addr.s6_addr[3]; 1429 sin6->sin6_addr.s6_addr[2] = 0; 1430 sin6->sin6_addr.s6_addr[3] = 0; 1431 } 1432 #endif 1433 n->ifindex = 0; 1434 if (n->af == AF_INET) { 1435 memcpy(&n->addr.v.a.addr, &((struct sockaddr_in *) 1436 ifa->ifa_addr)->sin_addr.s_addr, 1437 sizeof(struct in_addr)); 1438 memcpy(&n->addr.v.a.mask, &((struct sockaddr_in *) 1439 ifa->ifa_netmask)->sin_addr.s_addr, 1440 sizeof(struct in_addr)); 1441 if (ifa->ifa_broadaddr != NULL) 1442 memcpy(&n->bcast, &((struct sockaddr_in *) 1443 ifa->ifa_broadaddr)->sin_addr.s_addr, 1444 sizeof(struct in_addr)); 1445 if (ifa->ifa_dstaddr != NULL) 1446 memcpy(&n->peer, &((struct sockaddr_in *) 1447 ifa->ifa_dstaddr)->sin_addr.s_addr, 1448 sizeof(struct in_addr)); 1449 } else if (n->af == AF_INET6) { 1450 memcpy(&n->addr.v.a.addr, &((struct sockaddr_in6 *) 1451 ifa->ifa_addr)->sin6_addr.s6_addr, 1452 sizeof(struct in6_addr)); 1453 memcpy(&n->addr.v.a.mask, &((struct sockaddr_in6 *) 1454 ifa->ifa_netmask)->sin6_addr.s6_addr, 1455 sizeof(struct in6_addr)); 1456 if (ifa->ifa_broadaddr != NULL) 1457 memcpy(&n->bcast, &((struct sockaddr_in6 *) 1458 ifa->ifa_broadaddr)->sin6_addr.s6_addr, 1459 sizeof(struct in6_addr)); 1460 if (ifa->ifa_dstaddr != NULL) 1461 memcpy(&n->peer, &((struct sockaddr_in6 *) 1462 ifa->ifa_dstaddr)->sin6_addr.s6_addr, 1463 sizeof(struct in6_addr)); 1464 n->ifindex = ((struct sockaddr_in6 *) 1465 ifa->ifa_addr)->sin6_scope_id; 1466 } else if (n->af == AF_LINK) { 1467 ifa_add_groups_to_map(ifa->ifa_name); 1468 } 1469 if ((n->ifname = strdup(ifa->ifa_name)) == NULL) 1470 err(1, "ifa_load: strdup"); 1471 n->next = NULL; 1472 n->tail = n; 1473 if (h == NULL) 1474 h = n; 1475 else { 1476 h->tail->next = n; 1477 h->tail = n; 1478 } 1479 } 1480 1481 iftab = h; 1482 freeifaddrs(ifap); 1483 } 1484 1485 static int 1486 get_socket_domain(void) 1487 { 1488 int sdom; 1489 1490 sdom = AF_UNSPEC; 1491 #ifdef WITH_INET6 1492 if (sdom == AF_UNSPEC && feature_present("inet6")) 1493 sdom = AF_INET6; 1494 #endif 1495 #ifdef WITH_INET 1496 if (sdom == AF_UNSPEC && feature_present("inet")) 1497 sdom = AF_INET; 1498 #endif 1499 if (sdom == AF_UNSPEC) 1500 sdom = AF_LINK; 1501 1502 return (sdom); 1503 } 1504 1505 int 1506 get_query_socket(void) 1507 { 1508 static int s = -1; 1509 1510 if (s == -1) { 1511 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) == -1) 1512 err(1, "socket"); 1513 } 1514 1515 return (s); 1516 } 1517 1518 /* 1519 * Returns the response len if the name is a group, otherwise returns 0. 1520 */ 1521 static int 1522 is_a_group(char *name) 1523 { 1524 ENTRY item; 1525 ENTRY *ret_item; 1526 1527 item.key = name; 1528 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0) 1529 return (0); 1530 1531 return (*(int *)ret_item->data); 1532 } 1533 1534 struct node_host * 1535 ifa_exists(char *ifa_name) 1536 { 1537 struct node_host *n; 1538 1539 if (iftab == NULL) 1540 ifa_load(); 1541 1542 /* check whether this is a group */ 1543 if (is_a_group(ifa_name)) { 1544 /* fake a node_host */ 1545 if ((n = calloc(1, sizeof(*n))) == NULL) 1546 err(1, "calloc"); 1547 if ((n->ifname = strdup(ifa_name)) == NULL) 1548 err(1, "strdup"); 1549 return (n); 1550 } 1551 1552 for (n = iftab; n; n = n->next) { 1553 if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ)) 1554 return (n); 1555 } 1556 1557 return (NULL); 1558 } 1559 1560 struct node_host * 1561 ifa_grouplookup(char *ifa_name, int flags) 1562 { 1563 struct ifg_req *ifg; 1564 struct ifgroupreq ifgr; 1565 int s, len; 1566 struct node_host *n, *h = NULL; 1567 1568 s = get_query_socket(); 1569 len = is_a_group(ifa_name); 1570 if (len == 0) 1571 return (NULL); 1572 bzero(&ifgr, sizeof(ifgr)); 1573 strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name)); 1574 ifgr.ifgr_len = len; 1575 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) 1576 err(1, "calloc"); 1577 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) 1578 err(1, "SIOCGIFGMEMB"); 1579 1580 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req); 1581 ifg++) { 1582 len -= sizeof(struct ifg_req); 1583 if ((n = ifa_lookup(ifg->ifgrq_member, flags)) == NULL) 1584 continue; 1585 if (h == NULL) 1586 h = n; 1587 else { 1588 h->tail->next = n; 1589 h->tail = n->tail; 1590 } 1591 } 1592 free(ifgr.ifgr_groups); 1593 1594 return (h); 1595 } 1596 1597 struct node_host * 1598 ifa_lookup(char *ifa_name, int flags) 1599 { 1600 struct node_host *p = NULL, *h = NULL, *n = NULL; 1601 int got4 = 0, got6 = 0; 1602 const char *last_if = NULL; 1603 1604 /* first load iftab and isgroup_map */ 1605 if (iftab == NULL) 1606 ifa_load(); 1607 1608 if ((h = ifa_grouplookup(ifa_name, flags)) != NULL) 1609 return (h); 1610 1611 if (!strncmp(ifa_name, "self", IFNAMSIZ)) 1612 ifa_name = NULL; 1613 1614 for (p = iftab; p; p = p->next) { 1615 if (ifa_skip_if(ifa_name, p)) 1616 continue; 1617 if ((flags & PFI_AFLAG_BROADCAST) && p->af != AF_INET) 1618 continue; 1619 if ((flags & PFI_AFLAG_BROADCAST) && 1620 !(p->ifa_flags & IFF_BROADCAST)) 1621 continue; 1622 if ((flags & PFI_AFLAG_PEER) && 1623 !(p->ifa_flags & IFF_POINTOPOINT)) 1624 continue; 1625 if ((flags & PFI_AFLAG_NETWORK) && p->ifindex > 0) 1626 continue; 1627 if (last_if == NULL || strcmp(last_if, p->ifname)) 1628 got4 = got6 = 0; 1629 last_if = p->ifname; 1630 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET && got4) 1631 continue; 1632 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && 1633 IN6_IS_ADDR_LINKLOCAL(&p->addr.v.a.addr.v6)) 1634 continue; 1635 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && got6) 1636 continue; 1637 if (p->af == AF_INET) 1638 got4 = 1; 1639 else 1640 got6 = 1; 1641 n = calloc(1, sizeof(struct node_host)); 1642 if (n == NULL) 1643 err(1, "address: calloc"); 1644 n->af = p->af; 1645 if (flags & PFI_AFLAG_BROADCAST) 1646 memcpy(&n->addr.v.a.addr, &p->bcast, 1647 sizeof(struct pf_addr)); 1648 else if (flags & PFI_AFLAG_PEER) 1649 memcpy(&n->addr.v.a.addr, &p->peer, 1650 sizeof(struct pf_addr)); 1651 else 1652 memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr, 1653 sizeof(struct pf_addr)); 1654 if (flags & PFI_AFLAG_NETWORK) 1655 set_ipmask(n, unmask(&p->addr.v.a.mask, n->af)); 1656 else { 1657 if (n->af == AF_INET) { 1658 if (p->ifa_flags & IFF_LOOPBACK && 1659 p->ifa_flags & IFF_LINK1) 1660 memcpy(&n->addr.v.a.mask, 1661 &p->addr.v.a.mask, 1662 sizeof(struct pf_addr)); 1663 else 1664 set_ipmask(n, 32); 1665 } else 1666 set_ipmask(n, 128); 1667 } 1668 n->ifindex = p->ifindex; 1669 n->ifname = strdup(p->ifname); 1670 1671 n->next = NULL; 1672 n->tail = n; 1673 if (h == NULL) 1674 h = n; 1675 else { 1676 h->tail->next = n; 1677 h->tail = n; 1678 } 1679 } 1680 return (h); 1681 } 1682 1683 int 1684 ifa_skip_if(const char *filter, struct node_host *p) 1685 { 1686 int n; 1687 1688 if (p->af != AF_INET && p->af != AF_INET6) 1689 return (1); 1690 if (filter == NULL || !*filter) 1691 return (0); 1692 if (!strcmp(p->ifname, filter)) 1693 return (0); /* exact match */ 1694 n = strlen(filter); 1695 if (n < 1 || n >= IFNAMSIZ) 1696 return (1); /* sanity check */ 1697 if (filter[n-1] >= '0' && filter[n-1] <= '9') 1698 return (1); /* only do exact match in that case */ 1699 if (strncmp(p->ifname, filter, n)) 1700 return (1); /* prefix doesn't match */ 1701 return (p->ifname[n] < '0' || p->ifname[n] > '9'); 1702 } 1703 1704 1705 struct node_host * 1706 host(const char *s) 1707 { 1708 struct node_host *h = NULL; 1709 int mask, v4mask, v6mask, cont = 1; 1710 char *p, *q, *ps; 1711 1712 if ((p = strrchr(s, '/')) != NULL) { 1713 mask = strtol(p+1, &q, 0); 1714 if (!q || *q || mask > 128 || q == (p+1)) { 1715 fprintf(stderr, "invalid netmask '%s'\n", p); 1716 return (NULL); 1717 } 1718 if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL) 1719 err(1, "host: malloc"); 1720 strlcpy(ps, s, strlen(s) - strlen(p) + 1); 1721 v4mask = v6mask = mask; 1722 } else { 1723 if ((ps = strdup(s)) == NULL) 1724 err(1, "host: strdup"); 1725 v4mask = 32; 1726 v6mask = 128; 1727 mask = -1; 1728 } 1729 1730 /* IPv4 address? */ 1731 if (cont && (h = host_v4(s, mask)) != NULL) 1732 cont = 0; 1733 1734 /* IPv6 address? */ 1735 if (cont && (h = host_v6(ps, v6mask)) != NULL) 1736 cont = 0; 1737 1738 /* interface with this name exists? */ 1739 /* expensive with thousands of interfaces - prioritze IPv4/6 check */ 1740 if (cont && (h = host_if(ps, mask, &cont)) != NULL) 1741 cont = 0; 1742 1743 /* dns lookup */ 1744 if (cont && (h = host_dns(ps, v4mask, v6mask)) != NULL) 1745 cont = 0; 1746 free(ps); 1747 1748 if (h == NULL || cont == 1) { 1749 fprintf(stderr, "no IP address found for %s\n", s); 1750 return (NULL); 1751 } 1752 return (h); 1753 } 1754 1755 struct node_host * 1756 host_if(const char *s, int mask, int *cont) 1757 { 1758 struct node_host *n, *h = NULL; 1759 char *p, *ps; 1760 int flags = 0; 1761 1762 if ((ps = strdup(s)) == NULL) 1763 err(1, "host_if: strdup"); 1764 while ((p = strrchr(ps, ':')) != NULL) { 1765 if (!strcmp(p+1, "network")) 1766 flags |= PFI_AFLAG_NETWORK; 1767 else if (!strcmp(p+1, "broadcast")) 1768 flags |= PFI_AFLAG_BROADCAST; 1769 else if (!strcmp(p+1, "peer")) 1770 flags |= PFI_AFLAG_PEER; 1771 else if (!strcmp(p+1, "0")) 1772 flags |= PFI_AFLAG_NOALIAS; 1773 else { 1774 free(ps); 1775 return (NULL); 1776 } 1777 *p = '\0'; 1778 *cont = 0; 1779 } 1780 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { /* Yep! */ 1781 fprintf(stderr, "illegal combination of interface modifiers\n"); 1782 free(ps); 1783 return (NULL); 1784 } 1785 if ((flags & (PFI_AFLAG_NETWORK|PFI_AFLAG_BROADCAST)) && mask > -1) { 1786 fprintf(stderr, "network or broadcast lookup, but " 1787 "extra netmask given\n"); 1788 free(ps); 1789 return (NULL); 1790 } 1791 if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) { 1792 /* interface with this name exists */ 1793 h = ifa_lookup(ps, flags); 1794 for (n = h; n != NULL && mask > -1; n = n->next) 1795 set_ipmask(n, mask); 1796 } 1797 1798 free(ps); 1799 return (h); 1800 } 1801 1802 struct node_host * 1803 host_v4(const char *s, int mask) 1804 { 1805 struct node_host *h = NULL; 1806 struct in_addr ina; 1807 int bits = 32; 1808 1809 memset(&ina, 0, sizeof(struct in_addr)); 1810 if (strrchr(s, '/') != NULL) { 1811 if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) == -1) 1812 return (NULL); 1813 } else { 1814 if (inet_pton(AF_INET, s, &ina) != 1) 1815 return (NULL); 1816 } 1817 1818 h = calloc(1, sizeof(struct node_host)); 1819 if (h == NULL) 1820 err(1, "address: calloc"); 1821 h->ifname = NULL; 1822 h->af = AF_INET; 1823 h->addr.v.a.addr.addr32[0] = ina.s_addr; 1824 set_ipmask(h, bits); 1825 h->next = NULL; 1826 h->tail = h; 1827 1828 return (h); 1829 } 1830 1831 struct node_host * 1832 host_v6(const char *s, int mask) 1833 { 1834 struct addrinfo hints, *res; 1835 struct node_host *h = NULL; 1836 1837 memset(&hints, 0, sizeof(hints)); 1838 hints.ai_family = AF_INET6; 1839 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1840 hints.ai_flags = AI_NUMERICHOST; 1841 if (getaddrinfo(s, "0", &hints, &res) == 0) { 1842 h = calloc(1, sizeof(struct node_host)); 1843 if (h == NULL) 1844 err(1, "address: calloc"); 1845 h->ifname = NULL; 1846 h->af = AF_INET6; 1847 memcpy(&h->addr.v.a.addr, 1848 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 1849 sizeof(h->addr.v.a.addr)); 1850 h->ifindex = 1851 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1852 set_ipmask(h, mask); 1853 freeaddrinfo(res); 1854 h->next = NULL; 1855 h->tail = h; 1856 } 1857 1858 return (h); 1859 } 1860 1861 struct node_host * 1862 host_dns(const char *s, int v4mask, int v6mask) 1863 { 1864 struct addrinfo hints, *res0, *res; 1865 struct node_host *n, *h = NULL; 1866 int error, noalias = 0; 1867 int got4 = 0, got6 = 0; 1868 char *p, *ps; 1869 1870 if ((ps = strdup(s)) == NULL) 1871 err(1, "host_dns: strdup"); 1872 if ((p = strrchr(ps, ':')) != NULL && !strcmp(p, ":0")) { 1873 noalias = 1; 1874 *p = '\0'; 1875 } 1876 memset(&hints, 0, sizeof(hints)); 1877 hints.ai_family = PF_UNSPEC; 1878 hints.ai_socktype = SOCK_STREAM; /* DUMMY */ 1879 error = getaddrinfo(ps, NULL, &hints, &res0); 1880 if (error) { 1881 free(ps); 1882 return (h); 1883 } 1884 1885 for (res = res0; res; res = res->ai_next) { 1886 if (res->ai_family != AF_INET && 1887 res->ai_family != AF_INET6) 1888 continue; 1889 if (noalias) { 1890 if (res->ai_family == AF_INET) { 1891 if (got4) 1892 continue; 1893 got4 = 1; 1894 } else { 1895 if (got6) 1896 continue; 1897 got6 = 1; 1898 } 1899 } 1900 n = calloc(1, sizeof(struct node_host)); 1901 if (n == NULL) 1902 err(1, "host_dns: calloc"); 1903 n->ifname = NULL; 1904 n->af = res->ai_family; 1905 if (res->ai_family == AF_INET) { 1906 memcpy(&n->addr.v.a.addr, 1907 &((struct sockaddr_in *) 1908 res->ai_addr)->sin_addr.s_addr, 1909 sizeof(struct in_addr)); 1910 set_ipmask(n, v4mask); 1911 } else { 1912 memcpy(&n->addr.v.a.addr, 1913 &((struct sockaddr_in6 *) 1914 res->ai_addr)->sin6_addr.s6_addr, 1915 sizeof(struct in6_addr)); 1916 n->ifindex = 1917 ((struct sockaddr_in6 *) 1918 res->ai_addr)->sin6_scope_id; 1919 set_ipmask(n, v6mask); 1920 } 1921 n->next = NULL; 1922 n->tail = n; 1923 if (h == NULL) 1924 h = n; 1925 else { 1926 h->tail->next = n; 1927 h->tail = n; 1928 } 1929 } 1930 freeaddrinfo(res0); 1931 free(ps); 1932 1933 return (h); 1934 } 1935 1936 /* 1937 * convert a hostname to a list of addresses and put them in the given buffer. 1938 * test: 1939 * if set to 1, only simple addresses are accepted (no netblock, no "!"). 1940 */ 1941 int 1942 append_addr(struct pfr_buffer *b, char *s, int test) 1943 { 1944 char *r; 1945 struct node_host *h, *n; 1946 int rv, not = 0; 1947 1948 for (r = s; *r == '!'; r++) 1949 not = !not; 1950 if ((n = host(r)) == NULL) { 1951 errno = 0; 1952 return (-1); 1953 } 1954 rv = append_addr_host(b, n, test, not); 1955 do { 1956 h = n; 1957 n = n->next; 1958 free(h); 1959 } while (n != NULL); 1960 return (rv); 1961 } 1962 1963 /* 1964 * same as previous function, but with a pre-parsed input and the ability 1965 * to "negate" the result. Does not free the node_host list. 1966 * not: 1967 * setting it to 1 is equivalent to adding "!" in front of parameter s. 1968 */ 1969 int 1970 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not) 1971 { 1972 int bits; 1973 struct pfr_addr addr; 1974 1975 do { 1976 bzero(&addr, sizeof(addr)); 1977 addr.pfra_not = n->not ^ not; 1978 addr.pfra_af = n->af; 1979 addr.pfra_net = unmask(&n->addr.v.a.mask, n->af); 1980 switch (n->af) { 1981 case AF_INET: 1982 addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0]; 1983 bits = 32; 1984 break; 1985 case AF_INET6: 1986 memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6, 1987 sizeof(struct in6_addr)); 1988 bits = 128; 1989 break; 1990 default: 1991 errno = EINVAL; 1992 return (-1); 1993 } 1994 if ((test && (not || addr.pfra_net != bits)) || 1995 addr.pfra_net > bits) { 1996 errno = EINVAL; 1997 return (-1); 1998 } 1999 if (pfr_buf_add(b, &addr)) 2000 return (-1); 2001 } while ((n = n->next) != NULL); 2002 2003 return (0); 2004 } 2005 2006 int 2007 pfctl_add_trans(struct pfr_buffer *buf, int rs_num, const char *anchor) 2008 { 2009 struct pfioc_trans_e trans; 2010 2011 bzero(&trans, sizeof(trans)); 2012 trans.rs_num = rs_num; 2013 if (strlcpy(trans.anchor, anchor, 2014 sizeof(trans.anchor)) >= sizeof(trans.anchor)) 2015 errx(1, "pfctl_add_trans: strlcpy"); 2016 2017 return pfr_buf_add(buf, &trans); 2018 } 2019 2020 u_int32_t 2021 pfctl_get_ticket(struct pfr_buffer *buf, int rs_num, const char *anchor) 2022 { 2023 struct pfioc_trans_e *p; 2024 2025 PFRB_FOREACH(p, buf) 2026 if (rs_num == p->rs_num && !strcmp(anchor, p->anchor)) 2027 return (p->ticket); 2028 errx(1, "pfctl_get_ticket: assertion failed"); 2029 } 2030 2031 int 2032 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from) 2033 { 2034 struct pfioc_trans trans; 2035 2036 bzero(&trans, sizeof(trans)); 2037 trans.size = buf->pfrb_size - from; 2038 trans.esize = sizeof(struct pfioc_trans_e); 2039 trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from; 2040 return ioctl(dev, cmd, &trans); 2041 } 2042