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