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