1 /* $OpenBSD: pfctl_parser.c,v 1.347 2022/11/09 23:00:00 sashan Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Daniel Hartmeier 5 * Copyright (c) 2002 - 2013 Henning Brauer <henning@openbsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer in the documentation and/or other materials provided 17 * with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 #include <sys/types.h> 35 #include <sys/ioctl.h> 36 #include <sys/socket.h> 37 #include <net/if_dl.h> 38 #include <net/if.h> 39 #include <netinet/in.h> 40 #include <netinet/ip.h> 41 #include <netinet/ip_icmp.h> 42 #include <netinet/icmp6.h> 43 #include <net/pfvar.h> 44 #include <arpa/inet.h> 45 46 #include <ctype.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <ifaddrs.h> 50 #include <limits.h> 51 #include <netdb.h> 52 #include <stdarg.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 #define SYSLOG_NAMES 60 #include <syslog.h> 61 62 #include "pfctl_parser.h" 63 #include "pfctl.h" 64 65 void print_op (u_int8_t, const char *, const char *); 66 void print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int); 67 void print_ugid (u_int8_t, id_t, id_t, const char *); 68 void print_flags (u_int8_t); 69 void print_fromto(struct pf_rule_addr *, pf_osfp_t, 70 struct pf_rule_addr *, u_int8_t, u_int8_t, int); 71 void print_bwspec(const char *index, struct pf_queue_bwspec *); 72 void print_scspec(const char *, struct pf_queue_scspec *); 73 int ifa_skip_if(const char *filter, struct node_host *p); 74 75 struct node_host *ifa_grouplookup(const char *, int); 76 struct node_host *host_if(const char *, int); 77 struct node_host *host_ip(const char *, int); 78 struct node_host *host_dns(const char *, int, int); 79 80 const char *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 { "listenrepv2", MLDV2_LISTENER_REPORT }, 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 { "beyond-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_BEYONDSCOPE }, 177 { "addr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR }, 178 { "port-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT }, 179 { "transit", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT }, 180 { "reassemb", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY }, 181 { "badhead", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER }, 182 { "nxthdr", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER }, 183 { "redironlink", ND_REDIRECT, ND_REDIRECT_ONLINK }, 184 { "redirrouter", ND_REDIRECT, ND_REDIRECT_ROUTER } 185 }; 186 187 const struct pf_timeout pf_timeouts[] = { 188 { "tcp.first", PFTM_TCP_FIRST_PACKET }, 189 { "tcp.opening", PFTM_TCP_OPENING }, 190 { "tcp.established", PFTM_TCP_ESTABLISHED }, 191 { "tcp.closing", PFTM_TCP_CLOSING }, 192 { "tcp.finwait", PFTM_TCP_FIN_WAIT }, 193 { "tcp.closed", PFTM_TCP_CLOSED }, 194 { "tcp.tsdiff", PFTM_TS_DIFF }, 195 { "udp.first", PFTM_UDP_FIRST_PACKET }, 196 { "udp.single", PFTM_UDP_SINGLE }, 197 { "udp.multiple", PFTM_UDP_MULTIPLE }, 198 { "icmp.first", PFTM_ICMP_FIRST_PACKET }, 199 { "icmp.error", PFTM_ICMP_ERROR_REPLY }, 200 { "other.first", PFTM_OTHER_FIRST_PACKET }, 201 { "other.single", PFTM_OTHER_SINGLE }, 202 { "other.multiple", PFTM_OTHER_MULTIPLE }, 203 { "frag", PFTM_FRAG }, 204 { "interval", PFTM_INTERVAL }, 205 { "adaptive.start", PFTM_ADAPTIVE_START }, 206 { "adaptive.end", PFTM_ADAPTIVE_END }, 207 { "src.track", PFTM_SRC_NODE }, 208 { NULL, 0 } 209 }; 210 211 enum { PF_POOL_ROUTE, PF_POOL_NAT, PF_POOL_RDR }; 212 213 void 214 copy_satopfaddr(struct pf_addr *pfa, struct sockaddr *sa) 215 { 216 if (sa->sa_family == AF_INET6) 217 pfa->v6 = ((struct sockaddr_in6 *)sa)->sin6_addr; 218 else if (sa->sa_family == AF_INET) 219 pfa->v4 = ((struct sockaddr_in *)sa)->sin_addr; 220 else 221 warnx("unhandled af %d", sa->sa_family); 222 } 223 224 const struct icmptypeent * 225 geticmptypebynumber(u_int8_t type, sa_family_t af) 226 { 227 unsigned int i; 228 229 if (af != AF_INET6) { 230 for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0])); 231 i++) { 232 if (type == icmp_type[i].type) 233 return (&icmp_type[i]); 234 } 235 } else { 236 for (i=0; i < (sizeof (icmp6_type) / 237 sizeof(icmp6_type[0])); i++) { 238 if (type == icmp6_type[i].type) 239 return (&icmp6_type[i]); 240 } 241 } 242 return (NULL); 243 } 244 245 const struct icmptypeent * 246 geticmptypebyname(char *w, sa_family_t af) 247 { 248 unsigned int i; 249 250 if (af != AF_INET6) { 251 for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0])); 252 i++) { 253 if (!strcmp(w, icmp_type[i].name)) 254 return (&icmp_type[i]); 255 } 256 } else { 257 for (i=0; i < (sizeof (icmp6_type) / 258 sizeof(icmp6_type[0])); i++) { 259 if (!strcmp(w, icmp6_type[i].name)) 260 return (&icmp6_type[i]); 261 } 262 } 263 return (NULL); 264 } 265 266 const struct icmpcodeent * 267 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af) 268 { 269 unsigned int i; 270 271 if (af != AF_INET6) { 272 for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0])); 273 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 < (sizeof (icmp6_code) / 280 sizeof(icmp6_code[0])); 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 < (sizeof (icmp_code) / sizeof(icmp_code[0])); 296 i++) { 297 if (type == icmp_code[i].type && 298 !strcmp(w, icmp_code[i].name)) 299 return (&icmp_code[i]); 300 } 301 } else { 302 for (i=0; i < (sizeof (icmp6_code) / 303 sizeof(icmp6_code[0])); i++) { 304 if (type == icmp6_code[i].type && 305 !strcmp(w, icmp6_code[i].name)) 306 return (&icmp6_code[i]); 307 } 308 } 309 return (NULL); 310 } 311 312 /* 313 * Decode a symbolic name to a numeric value. 314 * From syslogd. 315 */ 316 int 317 string_to_loglevel(const char *name) 318 { 319 CODE *c; 320 char *p, buf[40]; 321 322 if (isdigit((unsigned char)*name)) { 323 const char *errstr; 324 int val; 325 326 val = strtonum(name, 0, LOG_DEBUG, &errstr); 327 if (errstr) 328 return -1; 329 return val; 330 } 331 332 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { 333 if (isupper((unsigned char)*name)) 334 *p = tolower((unsigned char)*name); 335 else 336 *p = *name; 337 } 338 *p = '\0'; 339 for (c = prioritynames; c->c_name; c++) 340 if (!strcmp(buf, c->c_name) && c->c_val != INTERNAL_NOPRI) 341 return (c->c_val); 342 343 return (-1); 344 } 345 346 const char * 347 loglevel_to_string(int level) 348 { 349 CODE *c; 350 351 for (c = prioritynames; c->c_name; c++) 352 if (c->c_val == level) 353 return (c->c_name); 354 355 return ("unknown"); 356 } 357 358 void 359 print_op(u_int8_t op, const char *a1, const char *a2) 360 { 361 if (op == PF_OP_IRG) 362 printf(" %s >< %s", a1, a2); 363 else if (op == PF_OP_XRG) 364 printf(" %s <> %s", a1, a2); 365 else if (op == PF_OP_EQ) 366 printf(" = %s", a1); 367 else if (op == PF_OP_NE) 368 printf(" != %s", a1); 369 else if (op == PF_OP_LT) 370 printf(" < %s", a1); 371 else if (op == PF_OP_LE) 372 printf(" <= %s", a1); 373 else if (op == PF_OP_GT) 374 printf(" > %s", a1); 375 else if (op == PF_OP_GE) 376 printf(" >= %s", a1); 377 else if (op == PF_OP_RRG) 378 printf(" %s:%s", a1, a2); 379 } 380 381 void 382 print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, const char *proto, int opts) 383 { 384 char a1[6], a2[6]; 385 struct servent *s = NULL; 386 387 if (opts & PF_OPT_PORTNAMES) 388 s = getservbyport(p1, proto); 389 p1 = ntohs(p1); 390 p2 = ntohs(p2); 391 snprintf(a1, sizeof(a1), "%u", p1); 392 snprintf(a2, sizeof(a2), "%u", p2); 393 printf(" port"); 394 if (s != NULL && (op == PF_OP_EQ || op == PF_OP_NE)) 395 print_op(op, s->s_name, a2); 396 else 397 print_op(op, a1, a2); 398 } 399 400 void 401 print_ugid(u_int8_t op, id_t i1, id_t i2, const char *t) 402 { 403 char a1[11], a2[11]; 404 405 snprintf(a1, sizeof(a1), "%u", i1); 406 snprintf(a2, sizeof(a2), "%u", i2); 407 printf(" %s", t); 408 if (i1 == -1 && (op == PF_OP_EQ || op == PF_OP_NE)) 409 print_op(op, "unknown", a2); 410 else 411 print_op(op, a1, a2); 412 } 413 414 void 415 print_flags(u_int8_t f) 416 { 417 int i; 418 419 for (i = 0; tcpflags[i]; ++i) 420 if (f & (1 << i)) 421 printf("%c", tcpflags[i]); 422 } 423 424 void 425 print_fromto(struct pf_rule_addr *src, pf_osfp_t osfp, struct pf_rule_addr *dst, 426 sa_family_t af, u_int8_t proto, int opts) 427 { 428 char buf[PF_OSFP_LEN*3]; 429 int verbose = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG); 430 if (src->addr.type == PF_ADDR_ADDRMASK && 431 dst->addr.type == PF_ADDR_ADDRMASK && 432 PF_AZERO(&src->addr.v.a.addr, AF_INET6) && 433 PF_AZERO(&src->addr.v.a.mask, AF_INET6) && 434 PF_AZERO(&dst->addr.v.a.addr, AF_INET6) && 435 PF_AZERO(&dst->addr.v.a.mask, AF_INET6) && 436 !src->neg && !dst->neg && 437 !src->port_op && !dst->port_op && 438 osfp == PF_OSFP_ANY) 439 printf(" all"); 440 else { 441 printf(" from "); 442 if (src->neg) 443 printf("! "); 444 print_addr(&src->addr, af, verbose); 445 if (src->port_op) 446 print_port(src->port_op, src->port[0], 447 src->port[1], 448 proto == IPPROTO_TCP ? "tcp" : "udp", opts); 449 if (osfp != PF_OSFP_ANY) 450 printf(" os \"%s\"", pfctl_lookup_fingerprint(osfp, buf, 451 sizeof(buf))); 452 453 printf(" to "); 454 if (dst->neg) 455 printf("! "); 456 print_addr(&dst->addr, af, verbose); 457 if (dst->port_op) 458 print_port(dst->port_op, dst->port[0], 459 dst->port[1], 460 proto == IPPROTO_TCP ? "tcp" : "udp", opts); 461 } 462 } 463 464 void 465 print_pool(struct pf_pool *pool, u_int16_t p1, u_int16_t p2, 466 sa_family_t af, int id, int verbose) 467 { 468 if (pool->ifname[0]) { 469 if (!PF_AZERO(&pool->addr.v.a.addr, af)) { 470 print_addr(&pool->addr, af, verbose); 471 printf("@"); 472 } 473 printf("%s", pool->ifname); 474 } else 475 print_addr(&pool->addr, af, verbose); 476 switch (id) { 477 case PF_POOL_NAT: 478 if ((p1 != PF_NAT_PROXY_PORT_LOW || 479 p2 != PF_NAT_PROXY_PORT_HIGH) && (p1 != 0 || p2 != 0)) { 480 if (p1 == p2) 481 printf(" port %u", p1); 482 else 483 printf(" port %u:%u", p1, p2); 484 } 485 break; 486 case PF_POOL_RDR: 487 if (p1) { 488 printf(" port %u", p1); 489 if (p2 && (p2 != p1)) 490 printf(":%u", p2); 491 } 492 break; 493 default: 494 break; 495 } 496 switch (pool->opts & PF_POOL_TYPEMASK) { 497 case PF_POOL_NONE: 498 break; 499 case PF_POOL_BITMASK: 500 printf(" bitmask"); 501 break; 502 case PF_POOL_RANDOM: 503 printf(" random"); 504 break; 505 case PF_POOL_SRCHASH: 506 printf(" source-hash 0x%08x%08x%08x%08x", 507 pool->key.key32[0], pool->key.key32[1], 508 pool->key.key32[2], pool->key.key32[3]); 509 break; 510 case PF_POOL_ROUNDROBIN: 511 printf(" round-robin"); 512 break; 513 case PF_POOL_LEASTSTATES: 514 printf(" least-states"); 515 break; 516 } 517 if (pool->opts & PF_POOL_STICKYADDR) 518 printf(" sticky-address"); 519 if (id == PF_POOL_NAT && p1 == 0 && p2 == 0) 520 printf(" static-port"); 521 } 522 523 const char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES; 524 const char *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES; 525 const char *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES; 526 const char *pf_scounters[FCNT_MAX+1] = FCNT_NAMES; 527 528 void 529 print_status(struct pf_status *s, struct pfctl_watermarks *synflwats, int opts) 530 { 531 char statline[80], *running, *debug; 532 time_t runtime = 0; 533 struct timespec uptime; 534 int i; 535 char buf[PF_MD5_DIGEST_LENGTH * 2 + 1]; 536 static const char hex[] = "0123456789abcdef"; 537 538 if (!clock_gettime(CLOCK_BOOTTIME, &uptime)) 539 runtime = uptime.tv_sec - s->since; 540 running = s->running ? "Enabled" : "Disabled"; 541 542 if (runtime) { 543 unsigned int sec, min, hrs; 544 time_t day = runtime; 545 546 sec = day % 60; 547 day /= 60; 548 min = day % 60; 549 day /= 60; 550 hrs = day % 24; 551 day /= 24; 552 snprintf(statline, sizeof(statline), 553 "Status: %s for %lld days %.2u:%.2u:%.2u", 554 running, (long long)day, hrs, min, sec); 555 } else 556 snprintf(statline, sizeof(statline), "Status: %s", running); 557 printf("%-44s", statline); 558 if (asprintf(&debug, "Debug: %s", loglevel_to_string(s->debug)) != -1) { 559 printf("%15s\n\n", debug); 560 free(debug); 561 } 562 563 if (opts & PF_OPT_VERBOSE) { 564 printf("Hostid: 0x%08x\n", ntohl(s->hostid)); 565 566 for (i = 0; i < PF_MD5_DIGEST_LENGTH; i++) { 567 buf[i + i] = hex[s->pf_chksum[i] >> 4]; 568 buf[i + i + 1] = hex[s->pf_chksum[i] & 0x0f]; 569 } 570 buf[i + i] = '\0'; 571 printf("Checksum: 0x%s\n\n", buf); 572 } 573 574 if (s->ifname[0] != 0) { 575 printf("Interface Stats for %-16s %5s %16s\n", 576 s->ifname, "IPv4", "IPv6"); 577 printf(" %-25s %14llu %16llu\n", "Bytes In", 578 (unsigned long long)s->bcounters[0][0], 579 (unsigned long long)s->bcounters[1][0]); 580 printf(" %-25s %14llu %16llu\n", "Bytes Out", 581 (unsigned long long)s->bcounters[0][1], 582 (unsigned long long)s->bcounters[1][1]); 583 printf(" Packets In\n"); 584 printf(" %-23s %14llu %16llu\n", "Passed", 585 (unsigned long long)s->pcounters[0][0][PF_PASS], 586 (unsigned long long)s->pcounters[1][0][PF_PASS]); 587 printf(" %-23s %14llu %16llu\n", "Blocked", 588 (unsigned long long)s->pcounters[0][0][PF_DROP], 589 (unsigned long long)s->pcounters[1][0][PF_DROP]); 590 printf(" Packets Out\n"); 591 printf(" %-23s %14llu %16llu\n", "Passed", 592 (unsigned long long)s->pcounters[0][1][PF_PASS], 593 (unsigned long long)s->pcounters[1][1][PF_PASS]); 594 printf(" %-23s %14llu %16llu\n\n", "Blocked", 595 (unsigned long long)s->pcounters[0][1][PF_DROP], 596 (unsigned long long)s->pcounters[1][1][PF_DROP]); 597 } 598 printf("%-27s %14s %16s\n", "State Table", "Total", "Rate"); 599 printf(" %-25s %14u %14s\n", "current entries", s->states, ""); 600 printf(" %-25s %14u %14s\n", "half-open tcp", s->states_halfopen, ""); 601 for (i = 0; i < FCNT_MAX; i++) { 602 printf(" %-25s %14llu ", pf_fcounters[i], 603 (unsigned long long)s->fcounters[i]); 604 if (runtime > 0) 605 printf("%14.1f/s\n", 606 (double)s->fcounters[i] / (double)runtime); 607 else 608 printf("%14s\n", ""); 609 } 610 if (opts & PF_OPT_VERBOSE) { 611 printf("Source Tracking Table\n"); 612 printf(" %-25s %14u %14s\n", "current entries", 613 s->src_nodes, ""); 614 for (i = 0; i < SCNT_MAX; i++) { 615 printf(" %-25s %14lld ", pf_scounters[i], 616 s->scounters[i]); 617 if (runtime > 0) 618 printf("%14.1f/s\n", 619 (double)s->scounters[i] / (double)runtime); 620 else 621 printf("%14s\n", ""); 622 } 623 } 624 printf("Counters\n"); 625 for (i = 0; i < PFRES_MAX; i++) { 626 printf(" %-25s %14llu ", pf_reasons[i], 627 (unsigned long long)s->counters[i]); 628 if (runtime > 0) 629 printf("%14.1f/s\n", 630 (double)s->counters[i] / (double)runtime); 631 else 632 printf("%14s\n", ""); 633 } 634 if (opts & PF_OPT_VERBOSE) { 635 printf("Limit Counters\n"); 636 for (i = 0; i < LCNT_MAX; i++) { 637 printf(" %-25s %14lld ", pf_lcounters[i], 638 s->lcounters[i]); 639 if (runtime > 0) 640 printf("%14.1f/s\n", 641 (double)s->lcounters[i] / (double)runtime); 642 else 643 printf("%14s\n", ""); 644 } 645 } 646 if (opts & PF_OPT_VERBOSE) { 647 printf("Adaptive Syncookies Watermarks\n"); 648 printf(" %-25s %14d states\n", "start", synflwats->hi); 649 printf(" %-25s %14d states\n", "end", synflwats->lo); 650 } 651 } 652 653 void 654 print_src_node(struct pf_src_node *sn, int opts) 655 { 656 struct pf_addr_wrap aw; 657 int min, sec; 658 659 memset(&aw, 0, sizeof(aw)); 660 if (sn->af == AF_INET) 661 aw.v.a.mask.addr32[0] = 0xffffffff; 662 else 663 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask)); 664 665 aw.v.a.addr = sn->addr; 666 print_addr(&aw, sn->af, opts & PF_OPT_VERBOSE2); 667 668 if (!PF_AZERO(&sn->raddr, sn->af)) { 669 if (sn->type == PF_SN_NAT) 670 printf(" nat-to "); 671 else if (sn->type == PF_SN_RDR) 672 printf(" rdr-to "); 673 else if (sn->type == PF_SN_ROUTE) 674 printf(" route-to "); 675 else 676 printf(" ??? (%u) ", sn->type); 677 aw.v.a.addr = sn->raddr; 678 print_addr(&aw, sn->naf ? sn->naf : sn->af, 679 opts & PF_OPT_VERBOSE2); 680 } 681 682 printf(" ( states %u, connections %u, rate %u.%u/%us )\n", sn->states, 683 sn->conn, sn->conn_rate.count / 1000, 684 (sn->conn_rate.count % 1000) / 100, sn->conn_rate.seconds); 685 if (opts & PF_OPT_VERBOSE) { 686 sec = sn->creation % 60; 687 sn->creation /= 60; 688 min = sn->creation % 60; 689 sn->creation /= 60; 690 printf(" age %.2u:%.2u:%.2u", sn->creation, min, sec); 691 if (sn->states == 0) { 692 sec = sn->expire % 60; 693 sn->expire /= 60; 694 min = sn->expire % 60; 695 sn->expire /= 60; 696 printf(", expires in %.2u:%.2u:%.2u", 697 sn->expire, min, sec); 698 } 699 printf(", %llu pkts, %llu bytes", 700 sn->packets[0] + sn->packets[1], 701 sn->bytes[0] + sn->bytes[1]); 702 if (sn->rule.nr != -1) 703 printf(", rule %u", sn->rule.nr); 704 printf("\n"); 705 } 706 } 707 708 void 709 print_rule(struct pf_rule *r, const char *anchor_call, int opts) 710 { 711 static const char *actiontypes[] = { "pass", "block", "scrub", 712 "no scrub", "nat", "no nat", "binat", "no binat", "rdr", "no rdr", 713 "", "", "match"}; 714 static const char *anchortypes[] = { "anchor", "anchor", "anchor", 715 "anchor", "nat-anchor", "nat-anchor", "binat-anchor", 716 "binat-anchor", "rdr-anchor", "rdr-anchor" }; 717 int i, ropts; 718 int verbose = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG); 719 char *p; 720 721 if ((r->rule_flag & PFRULE_EXPIRED) && (!verbose)) 722 return; 723 724 if (verbose) 725 printf("@%d ", r->nr); 726 727 if (r->action > PF_MATCH) 728 printf("action(%d)", r->action); 729 else if (anchor_call[0]) { 730 p = strrchr(anchor_call, '/'); 731 if (p ? p[1] == '_' : anchor_call[0] == '_') 732 printf("%s", anchortypes[r->action]); 733 else 734 printf("%s \"%s\"", anchortypes[r->action], 735 anchor_call); 736 } else 737 printf("%s", actiontypes[r->action]); 738 if (r->action == PF_DROP) { 739 if (r->rule_flag & PFRULE_RETURN) 740 printf(" return"); 741 else if (r->rule_flag & PFRULE_RETURNRST) { 742 if (!r->return_ttl) 743 printf(" return-rst"); 744 else 745 printf(" return-rst(ttl %d)", r->return_ttl); 746 } else if (r->rule_flag & PFRULE_RETURNICMP) { 747 const struct icmpcodeent *ic, *ic6; 748 749 ic = geticmpcodebynumber(r->return_icmp >> 8, 750 r->return_icmp & 255, AF_INET); 751 ic6 = geticmpcodebynumber(r->return_icmp6 >> 8, 752 r->return_icmp6 & 255, AF_INET6); 753 754 switch (r->af) { 755 case AF_INET: 756 printf(" return-icmp"); 757 if (ic == NULL) 758 printf("(%u)", r->return_icmp & 255); 759 else 760 printf("(%s)", ic->name); 761 break; 762 case AF_INET6: 763 printf(" return-icmp6"); 764 if (ic6 == NULL) 765 printf("(%u)", r->return_icmp6 & 255); 766 else 767 printf("(%s)", ic6->name); 768 break; 769 default: 770 printf(" return-icmp"); 771 if (ic == NULL) 772 printf("(%u, ", r->return_icmp & 255); 773 else 774 printf("(%s, ", ic->name); 775 if (ic6 == NULL) 776 printf("%u)", r->return_icmp6 & 255); 777 else 778 printf("%s)", ic6->name); 779 break; 780 } 781 } else 782 printf(" drop"); 783 } 784 if (r->direction == PF_IN) 785 printf(" in"); 786 else if (r->direction == PF_OUT) 787 printf(" out"); 788 if (r->log) { 789 printf(" log"); 790 if (r->log & ~PF_LOG || r->logif) { 791 int count = 0; 792 793 printf(" ("); 794 if (r->log & PF_LOG_ALL) 795 printf("%sall", count++ ? ", " : ""); 796 if (r->log & PF_LOG_MATCHES) 797 printf("%smatches", count++ ? ", " : ""); 798 if (r->log & PF_LOG_USER) 799 printf("%suser", count++ ? ", " : ""); 800 if (r->logif) 801 printf("%sto pflog%u", count++ ? ", " : "", 802 r->logif); 803 printf(")"); 804 } 805 } 806 if (r->quick) 807 printf(" quick"); 808 if (r->ifname[0]) { 809 if (r->ifnot) 810 printf(" on ! %s", r->ifname); 811 else 812 printf(" on %s", r->ifname); 813 } 814 if (r->onrdomain >= 0) { 815 if (r->ifnot) 816 printf(" on ! rdomain %d", r->onrdomain); 817 else 818 printf(" on rdomain %d", r->onrdomain); 819 } 820 if (r->af) { 821 if (r->af == AF_INET) 822 printf(" inet"); 823 else 824 printf(" inet6"); 825 } 826 if (r->proto) { 827 struct protoent *p; 828 829 if ((p = getprotobynumber(r->proto)) != NULL) 830 printf(" proto %s", p->p_name); 831 else 832 printf(" proto %u", r->proto); 833 } 834 print_fromto(&r->src, r->os_fingerprint, &r->dst, r->af, r->proto, 835 opts); 836 if (r->rcv_ifname[0]) 837 printf(" %sreceived-on %s", r->rcvifnot ? "!" : "", 838 r->rcv_ifname); 839 if (r->uid.op) 840 print_ugid(r->uid.op, r->uid.uid[0], r->uid.uid[1], "user"); 841 if (r->gid.op) 842 print_ugid(r->gid.op, r->gid.gid[0], r->gid.gid[1], "group"); 843 if (r->flags || r->flagset) { 844 printf(" flags "); 845 print_flags(r->flags); 846 printf("/"); 847 print_flags(r->flagset); 848 } else if ((r->action == PF_PASS || r->action == PF_MATCH) && 849 (!r->proto || r->proto == IPPROTO_TCP) && 850 !(r->rule_flag & PFRULE_FRAGMENT) && 851 !anchor_call[0] && r->keep_state) 852 printf(" flags any"); 853 if (r->type) { 854 const struct icmptypeent *it; 855 856 it = geticmptypebynumber(r->type-1, r->af); 857 if (r->af != AF_INET6) 858 printf(" icmp-type"); 859 else 860 printf(" icmp6-type"); 861 if (it != NULL) 862 printf(" %s", it->name); 863 else 864 printf(" %u", r->type-1); 865 if (r->code) { 866 const struct icmpcodeent *ic; 867 868 ic = geticmpcodebynumber(r->type-1, r->code-1, r->af); 869 if (ic != NULL) 870 printf(" code %s", ic->name); 871 else 872 printf(" code %u", r->code-1); 873 } 874 } 875 if (r->tos) 876 printf(" tos 0x%2.2x", r->tos); 877 if (r->prio) 878 printf(" prio %u", r->prio == PF_PRIO_ZERO ? 0 : r->prio); 879 if (r->pktrate.limit) 880 printf(" max-pkt-rate %u/%u", r->pktrate.limit, 881 r->pktrate.seconds); 882 883 if (r->scrub_flags & PFSTATE_SETMASK || r->qname[0] || 884 r->rule_flag & PFRULE_SETDELAY) { 885 char *comma = ""; 886 printf(" set ("); 887 if (r->scrub_flags & PFSTATE_SETPRIO) { 888 if (r->set_prio[0] == r->set_prio[1]) 889 printf("%sprio %u", comma, r->set_prio[0]); 890 else 891 printf("%sprio(%u, %u)", comma, r->set_prio[0], 892 r->set_prio[1]); 893 comma = ", "; 894 } 895 if (r->qname[0]) { 896 if (r->pqname[0]) 897 printf("%squeue(%s, %s)", comma, r->qname, 898 r->pqname); 899 else 900 printf("%squeue %s", comma, r->qname); 901 comma = ", "; 902 } 903 if (r->scrub_flags & PFSTATE_SETTOS) { 904 printf("%stos 0x%2.2x", comma, r->set_tos); 905 comma = ", "; 906 } 907 if (r->rule_flag & PFRULE_SETDELAY) { 908 printf("%sdelay %u", comma, r->delay); 909 comma = ", "; 910 } 911 printf(")"); 912 } 913 914 ropts = 0; 915 if (r->max_states || r->max_src_nodes || r->max_src_states) 916 ropts = 1; 917 if (r->rule_flag & PFRULE_NOSYNC) 918 ropts = 1; 919 if (r->rule_flag & PFRULE_SRCTRACK) 920 ropts = 1; 921 if (r->rule_flag & PFRULE_IFBOUND) 922 ropts = 1; 923 if (r->rule_flag & PFRULE_STATESLOPPY) 924 ropts = 1; 925 if (r->rule_flag & PFRULE_PFLOW) 926 ropts = 1; 927 for (i = 0; !ropts && i < PFTM_MAX; ++i) 928 if (r->timeout[i]) 929 ropts = 1; 930 931 if (!r->keep_state && r->action == PF_PASS && !anchor_call[0]) 932 printf(" no state"); 933 else if (r->keep_state == PF_STATE_NORMAL && ropts) 934 printf(" keep state"); 935 else if (r->keep_state == PF_STATE_MODULATE) 936 printf(" modulate state"); 937 else if (r->keep_state == PF_STATE_SYNPROXY) 938 printf(" synproxy state"); 939 if (r->prob) { 940 char buf[20]; 941 942 snprintf(buf, sizeof(buf), "%f", r->prob*100.0/(UINT_MAX+1.0)); 943 for (i = strlen(buf)-1; i > 0; i--) { 944 if (buf[i] == '0') 945 buf[i] = '\0'; 946 else { 947 if (buf[i] == '.') 948 buf[i] = '\0'; 949 break; 950 } 951 } 952 printf(" probability %s%%", buf); 953 } 954 if (ropts) { 955 printf(" ("); 956 if (r->max_states) { 957 printf("max %u", r->max_states); 958 ropts = 0; 959 } 960 if (r->rule_flag & PFRULE_NOSYNC) { 961 if (!ropts) 962 printf(", "); 963 printf("no-sync"); 964 ropts = 0; 965 } 966 if (r->rule_flag & PFRULE_SRCTRACK) { 967 if (!ropts) 968 printf(", "); 969 printf("source-track"); 970 if (r->rule_flag & PFRULE_RULESRCTRACK) 971 printf(" rule"); 972 else 973 printf(" global"); 974 ropts = 0; 975 } 976 if (r->max_src_states) { 977 if (!ropts) 978 printf(", "); 979 printf("max-src-states %u", r->max_src_states); 980 ropts = 0; 981 } 982 if (r->max_src_conn) { 983 if (!ropts) 984 printf(", "); 985 printf("max-src-conn %u", r->max_src_conn); 986 ropts = 0; 987 } 988 if (r->max_src_conn_rate.limit) { 989 if (!ropts) 990 printf(", "); 991 printf("max-src-conn-rate %u/%u", 992 r->max_src_conn_rate.limit, 993 r->max_src_conn_rate.seconds); 994 ropts = 0; 995 } 996 if (r->max_src_nodes) { 997 if (!ropts) 998 printf(", "); 999 printf("max-src-nodes %u", r->max_src_nodes); 1000 ropts = 0; 1001 } 1002 if (r->overload_tblname[0]) { 1003 if (!ropts) 1004 printf(", "); 1005 printf("overload <%s>", r->overload_tblname); 1006 if (r->flush) 1007 printf(" flush"); 1008 if (r->flush & PF_FLUSH_GLOBAL) 1009 printf(" global"); 1010 } 1011 if (r->rule_flag & PFRULE_IFBOUND) { 1012 if (!ropts) 1013 printf(", "); 1014 printf("if-bound"); 1015 ropts = 0; 1016 } 1017 if (r->rule_flag & PFRULE_STATESLOPPY) { 1018 if (!ropts) 1019 printf(", "); 1020 printf("sloppy"); 1021 ropts = 0; 1022 } 1023 if (r->rule_flag & PFRULE_PFLOW) { 1024 if (!ropts) 1025 printf(", "); 1026 printf("pflow"); 1027 ropts = 0; 1028 } 1029 for (i = 0; i < PFTM_MAX; ++i) 1030 if (r->timeout[i]) { 1031 int j; 1032 1033 if (!ropts) 1034 printf(", "); 1035 ropts = 0; 1036 for (j = 0; pf_timeouts[j].name != NULL; 1037 ++j) 1038 if (pf_timeouts[j].timeout == i) 1039 break; 1040 printf("%s %u", pf_timeouts[j].name == NULL ? 1041 "inv.timeout" : pf_timeouts[j].name, 1042 r->timeout[i]); 1043 } 1044 printf(")"); 1045 } 1046 1047 if (r->rule_flag & PFRULE_FRAGMENT) 1048 printf(" fragment"); 1049 1050 if (r->scrub_flags & PFSTATE_SCRUBMASK || r->min_ttl || r->max_mss) { 1051 printf(" scrub ("); 1052 ropts = 1; 1053 if (r->scrub_flags & PFSTATE_NODF) { 1054 printf("no-df"); 1055 ropts = 0; 1056 } 1057 if (r->scrub_flags & PFSTATE_RANDOMID) { 1058 if (!ropts) 1059 printf(" "); 1060 printf("random-id"); 1061 ropts = 0; 1062 } 1063 if (r->min_ttl) { 1064 if (!ropts) 1065 printf(" "); 1066 printf("min-ttl %d", r->min_ttl); 1067 ropts = 0; 1068 } 1069 if (r->scrub_flags & PFSTATE_SCRUB_TCP) { 1070 if (!ropts) 1071 printf(" "); 1072 printf("reassemble tcp"); 1073 ropts = 0; 1074 } 1075 if (r->max_mss) { 1076 if (!ropts) 1077 printf(" "); 1078 printf("max-mss %d", r->max_mss); 1079 ropts = 0; 1080 } 1081 printf(")"); 1082 } 1083 1084 if (r->allow_opts) 1085 printf(" allow-opts"); 1086 if (r->label[0]) 1087 printf(" label \"%s\"", r->label); 1088 if (r->rule_flag & PFRULE_ONCE) 1089 printf(" once"); 1090 if (r->tagname[0]) 1091 printf(" tag %s", r->tagname); 1092 if (r->match_tagname[0]) { 1093 if (r->match_tag_not) 1094 printf(" !"); 1095 printf(" tagged %s", r->match_tagname); 1096 } 1097 if (r->rtableid != -1) 1098 printf(" rtable %u", r->rtableid); 1099 switch (r->divert.type) { 1100 case PF_DIVERT_NONE: 1101 break; 1102 case PF_DIVERT_TO: { 1103 printf(" divert-to "); 1104 print_addr_str(r->af, &r->divert.addr); 1105 printf(" port %u", ntohs(r->divert.port)); 1106 break; 1107 } 1108 case PF_DIVERT_REPLY: 1109 printf(" divert-reply"); 1110 break; 1111 case PF_DIVERT_PACKET: 1112 printf(" divert-packet port %u", ntohs(r->divert.port)); 1113 break; 1114 default: 1115 printf(" divert ???"); 1116 break; 1117 } 1118 1119 if (!anchor_call[0] && r->nat.addr.type != PF_ADDR_NONE && 1120 r->rule_flag & PFRULE_AFTO) { 1121 printf(" af-to %s from ", r->naf == AF_INET ? "inet" : "inet6"); 1122 print_pool(&r->nat, r->nat.proxy_port[0], 1123 r->nat.proxy_port[1], r->naf ? r->naf : r->af, 1124 PF_POOL_NAT, verbose); 1125 if (r->rdr.addr.type != PF_ADDR_NONE) { 1126 printf(" to "); 1127 print_pool(&r->rdr, r->rdr.proxy_port[0], 1128 r->rdr.proxy_port[1], r->naf ? r->naf : r->af, 1129 PF_POOL_RDR, verbose); 1130 } 1131 } else if (!anchor_call[0] && r->nat.addr.type != PF_ADDR_NONE) { 1132 printf (" nat-to "); 1133 print_pool(&r->nat, r->nat.proxy_port[0], 1134 r->nat.proxy_port[1], r->naf ? r->naf : r->af, 1135 PF_POOL_NAT, verbose); 1136 } else if (!anchor_call[0] && r->rdr.addr.type != PF_ADDR_NONE) { 1137 printf (" rdr-to "); 1138 print_pool(&r->rdr, r->rdr.proxy_port[0], 1139 r->rdr.proxy_port[1], r->af, PF_POOL_RDR, verbose); 1140 } 1141 if (r->rt) { 1142 if (r->rt == PF_ROUTETO) 1143 printf(" route-to"); 1144 else if (r->rt == PF_REPLYTO) 1145 printf(" reply-to"); 1146 else if (r->rt == PF_DUPTO) 1147 printf(" dup-to"); 1148 printf(" "); 1149 print_pool(&r->route, 0, 0, r->af, PF_POOL_ROUTE, verbose); 1150 } 1151 1152 if (r->rule_flag & PFRULE_EXPIRED) 1153 printf(" # expired"); 1154 } 1155 1156 void 1157 print_tabledef(const char *name, int flags, int addrs, 1158 struct node_tinithead *nodes) 1159 { 1160 struct node_tinit *ti, *nti; 1161 struct node_host *h; 1162 1163 printf("table <%s>", name); 1164 if (flags & PFR_TFLAG_CONST) 1165 printf(" const"); 1166 if (flags & PFR_TFLAG_PERSIST) 1167 printf(" persist"); 1168 if (flags & PFR_TFLAG_COUNTERS) 1169 printf(" counters"); 1170 SIMPLEQ_FOREACH(ti, nodes, entries) { 1171 if (ti->file) { 1172 printf(" file \"%s\"", ti->file); 1173 continue; 1174 } 1175 printf(" {"); 1176 for (;;) { 1177 for (h = ti->host; h != NULL; h = h->next) { 1178 printf(h->not ? " !" : " "); 1179 print_addr(&h->addr, h->af, 0); 1180 if (h->ifname) 1181 printf("@%s", h->ifname); 1182 } 1183 nti = SIMPLEQ_NEXT(ti, entries); 1184 if (nti != NULL && nti->file == NULL) 1185 ti = nti; /* merge lists */ 1186 else 1187 break; 1188 } 1189 printf(" }"); 1190 } 1191 if (addrs && SIMPLEQ_EMPTY(nodes)) 1192 printf(" { }"); 1193 printf("\n"); 1194 } 1195 1196 void 1197 print_bwspec(const char *prefix, struct pf_queue_bwspec *bw) 1198 { 1199 u_int rate; 1200 int i; 1201 static const char unit[] = " KMG"; 1202 1203 if (bw->percent) 1204 printf("%s%u%%", prefix, bw->percent); 1205 else if (bw->absolute) { 1206 rate = bw->absolute; 1207 for (i = 0; rate >= 1000 && i <= 3 && (rate % 1000 == 0); i++) 1208 rate /= 1000; 1209 printf("%s%u%c", prefix, rate, unit[i]); 1210 } 1211 } 1212 1213 void 1214 print_scspec(const char *prefix, struct pf_queue_scspec *sc) 1215 { 1216 print_bwspec(prefix, &sc->m2); 1217 if (sc->d) { 1218 printf(" burst "); 1219 print_bwspec("", &sc->m1); 1220 printf(" for %ums", sc->d); 1221 } 1222 } 1223 1224 void 1225 print_queuespec(struct pf_queuespec *q) 1226 { 1227 printf("queue %s", q->qname); 1228 if (q->parent[0]) 1229 printf(" parent %s", q->parent); 1230 else if (q->ifname[0]) 1231 printf(" on %s", q->ifname); 1232 if (q->flags & PFQS_FLOWQUEUE) { 1233 printf(" flows %u", q->flowqueue.flows); 1234 if (q->flowqueue.quantum > 0) 1235 printf(" quantum %u", q->flowqueue.quantum); 1236 if (q->flowqueue.interval > 0) 1237 printf(" interval %ums", 1238 q->flowqueue.interval / 1000000); 1239 if (q->flowqueue.target > 0) 1240 printf(" target %ums", 1241 q->flowqueue.target / 1000000); 1242 } 1243 if (q->linkshare.m1.absolute || q->linkshare.m2.absolute) { 1244 print_scspec(" bandwidth ", &q->linkshare); 1245 print_scspec(", min ", &q->realtime); 1246 print_scspec(", max ", &q->upperlimit); 1247 } 1248 if (q->flags & PFQS_DEFAULT) 1249 printf(" default"); 1250 if (q->qlimit) 1251 printf(" qlimit %u", q->qlimit); 1252 printf("\n"); 1253 } 1254 1255 int 1256 parse_flags(char *s) 1257 { 1258 char *p, *q; 1259 u_int8_t f = 0; 1260 1261 for (p = s; *p; p++) { 1262 if ((q = strchr(tcpflags, *p)) == NULL) 1263 return -1; 1264 else 1265 f |= 1 << (q - tcpflags); 1266 } 1267 return (f ? f : PF_TH_ALL); 1268 } 1269 1270 void 1271 set_ipmask(struct node_host *h, int bb) 1272 { 1273 struct pf_addr *m, *n; 1274 int i, j = 0; 1275 u_int8_t b; 1276 1277 m = &h->addr.v.a.mask; 1278 memset(m, 0, sizeof(*m)); 1279 1280 if (bb == -1) 1281 b = h->af == AF_INET ? 32 : 128; 1282 else 1283 b = bb; 1284 1285 while (b >= 32) { 1286 m->addr32[j++] = 0xffffffff; 1287 b -= 32; 1288 } 1289 for (i = 31; i > 31-b; --i) 1290 m->addr32[j] |= (1 << i); 1291 if (b) 1292 m->addr32[j] = htonl(m->addr32[j]); 1293 1294 /* Mask off bits of the address that will never be used. */ 1295 n = &h->addr.v.a.addr; 1296 if (h->addr.type == PF_ADDR_ADDRMASK) 1297 for (i = 0; i < 4; i++) 1298 n->addr32[i] = n->addr32[i] & m->addr32[i]; 1299 } 1300 1301 int 1302 check_netmask(struct node_host *h, sa_family_t af) 1303 { 1304 struct node_host *n = NULL; 1305 struct pf_addr *m; 1306 1307 for (n = h; n != NULL; n = n->next) { 1308 if (h->addr.type == PF_ADDR_TABLE) 1309 continue; 1310 m = &h->addr.v.a.mask; 1311 /* netmasks > 32 bit are invalid on v4 */ 1312 if (af == AF_INET && 1313 (m->addr32[1] || m->addr32[2] || m->addr32[3])) { 1314 fprintf(stderr, "netmask %u invalid for IPv4 address\n", 1315 unmask(m)); 1316 return (1); 1317 } 1318 } 1319 return (0); 1320 } 1321 1322 struct node_host * 1323 gen_dynnode(struct node_host *h, sa_family_t af) 1324 { 1325 struct node_host *n; 1326 1327 if (h->addr.type != PF_ADDR_DYNIFTL) 1328 return (NULL); 1329 1330 if ((n = calloc(1, sizeof(*n))) == NULL) 1331 return (NULL); 1332 bcopy(h, n, sizeof(*n)); 1333 n->ifname = NULL; 1334 n->next = NULL; 1335 n->tail = NULL; 1336 1337 /* fix up netmask */ 1338 if (af == AF_INET && unmask(&n->addr.v.a.mask) > 32) 1339 set_ipmask(n, 32); 1340 1341 return (n); 1342 } 1343 1344 /* interface lookup routines */ 1345 1346 struct node_host *iftab; 1347 1348 void 1349 ifa_load(void) 1350 { 1351 struct ifaddrs *ifap, *ifa; 1352 struct node_host *n = NULL, *h = NULL; 1353 1354 if (getifaddrs(&ifap) == -1) 1355 err(1, "getifaddrs"); 1356 1357 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1358 if (ifa->ifa_addr == NULL || 1359 !(ifa->ifa_addr->sa_family == AF_INET || 1360 ifa->ifa_addr->sa_family == AF_INET6 || 1361 ifa->ifa_addr->sa_family == AF_LINK)) 1362 continue; 1363 n = calloc(1, sizeof(struct node_host)); 1364 if (n == NULL) 1365 err(1, "%s: calloc", __func__); 1366 n->af = ifa->ifa_addr->sa_family; 1367 n->ifa_flags = ifa->ifa_flags; 1368 #ifdef __KAME__ 1369 if (n->af == AF_INET6 && 1370 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) 1371 ifa->ifa_addr)->sin6_addr) && 1372 ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id == 1373 0) { 1374 struct sockaddr_in6 *sin6; 1375 1376 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1377 sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 | 1378 sin6->sin6_addr.s6_addr[3]; 1379 sin6->sin6_addr.s6_addr[2] = 0; 1380 sin6->sin6_addr.s6_addr[3] = 0; 1381 } 1382 #endif 1383 n->ifindex = 0; 1384 if (n->af == AF_LINK) 1385 n->ifindex = ((struct sockaddr_dl *) 1386 ifa->ifa_addr)->sdl_index; 1387 else { 1388 copy_satopfaddr(&n->addr.v.a.addr, ifa->ifa_addr); 1389 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family; 1390 copy_satopfaddr(&n->addr.v.a.mask, ifa->ifa_netmask); 1391 if (ifa->ifa_broadaddr != NULL) { 1392 ifa->ifa_broadaddr->sa_family = ifa->ifa_addr->sa_family; 1393 copy_satopfaddr(&n->bcast, ifa->ifa_broadaddr); 1394 } 1395 if (ifa->ifa_dstaddr != NULL) { 1396 ifa->ifa_dstaddr->sa_family = ifa->ifa_addr->sa_family; 1397 copy_satopfaddr(&n->peer, ifa->ifa_dstaddr); 1398 } 1399 if (n->af == AF_INET6) 1400 n->ifindex = ((struct sockaddr_in6 *) 1401 ifa->ifa_addr)->sin6_scope_id; 1402 } 1403 if ((n->ifname = strdup(ifa->ifa_name)) == NULL) 1404 err(1, "%s: strdup", __func__); 1405 n->next = NULL; 1406 n->tail = n; 1407 if (h == NULL) 1408 h = n; 1409 else { 1410 h->tail->next = n; 1411 h->tail = n; 1412 } 1413 } 1414 1415 iftab = h; 1416 freeifaddrs(ifap); 1417 } 1418 1419 unsigned int 1420 ifa_nametoindex(const char *ifa_name) 1421 { 1422 struct node_host *p; 1423 1424 for (p = iftab; p; p = p->next) { 1425 if (p->af == AF_LINK && strcmp(p->ifname, ifa_name) == 0) 1426 return (p->ifindex); 1427 } 1428 errno = ENXIO; 1429 return (0); 1430 } 1431 1432 char * 1433 ifa_indextoname(unsigned int ifindex, char *ifa_name) 1434 { 1435 struct node_host *p; 1436 1437 for (p = iftab; p; p = p->next) { 1438 if (p->af == AF_LINK && ifindex == p->ifindex) { 1439 strlcpy(ifa_name, p->ifname, IFNAMSIZ); 1440 return (ifa_name); 1441 } 1442 } 1443 errno = ENXIO; 1444 return (NULL); 1445 } 1446 1447 struct node_host * 1448 ifa_exists(const char *ifa_name) 1449 { 1450 struct node_host *n; 1451 struct ifgroupreq ifgr; 1452 int s; 1453 1454 if (iftab == NULL) 1455 ifa_load(); 1456 1457 /* check whether this is a group */ 1458 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 1459 err(1, "socket"); 1460 bzero(&ifgr, sizeof(ifgr)); 1461 strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name)); 1462 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == 0) { 1463 /* fake a node_host */ 1464 if ((n = calloc(1, sizeof(*n))) == NULL) 1465 err(1, "calloc"); 1466 if ((n->ifname = strdup(ifa_name)) == NULL) 1467 err(1, "strdup"); 1468 close(s); 1469 return (n); 1470 } 1471 close(s); 1472 1473 for (n = iftab; n; n = n->next) { 1474 if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ)) 1475 return (n); 1476 } 1477 1478 return (NULL); 1479 } 1480 1481 struct node_host * 1482 ifa_grouplookup(const char *ifa_name, int flags) 1483 { 1484 struct ifg_req *ifg; 1485 struct ifgroupreq ifgr; 1486 int s, len; 1487 struct node_host *n, *h = NULL; 1488 1489 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 1490 err(1, "socket"); 1491 bzero(&ifgr, sizeof(ifgr)); 1492 strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name)); 1493 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) { 1494 close(s); 1495 return (NULL); 1496 } 1497 1498 len = ifgr.ifgr_len; 1499 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) 1500 err(1, "calloc"); 1501 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) 1502 err(1, "SIOCGIFGMEMB"); 1503 1504 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req); 1505 ifg++) { 1506 len -= sizeof(struct ifg_req); 1507 if ((n = ifa_lookup(ifg->ifgrq_member, flags)) == NULL) 1508 continue; 1509 if (h == NULL) 1510 h = n; 1511 else { 1512 h->tail->next = n; 1513 h->tail = n->tail; 1514 } 1515 } 1516 free(ifgr.ifgr_groups); 1517 close(s); 1518 1519 return (h); 1520 } 1521 1522 struct node_host * 1523 ifa_lookup(const char *ifa_name, int flags) 1524 { 1525 struct node_host *p = NULL, *h = NULL, *n = NULL; 1526 int got4 = 0, got6 = 0; 1527 const char *last_if = NULL; 1528 1529 if ((h = ifa_grouplookup(ifa_name, flags)) != NULL) 1530 return (h); 1531 1532 if (!strncmp(ifa_name, "self", IFNAMSIZ)) 1533 ifa_name = NULL; 1534 1535 if (iftab == NULL) 1536 ifa_load(); 1537 1538 for (p = iftab; p; p = p->next) { 1539 if (ifa_skip_if(ifa_name, p)) 1540 continue; 1541 if ((flags & PFI_AFLAG_BROADCAST) && p->af != AF_INET) 1542 continue; 1543 if ((flags & PFI_AFLAG_BROADCAST) && 1544 !(p->ifa_flags & IFF_BROADCAST)) 1545 continue; 1546 if ((flags & PFI_AFLAG_BROADCAST) && p->bcast.v4.s_addr == 0) 1547 continue; 1548 if ((flags & PFI_AFLAG_PEER) && 1549 !(p->ifa_flags & IFF_POINTOPOINT)) 1550 continue; 1551 if ((flags & PFI_AFLAG_NETWORK) && p->ifindex > 0) 1552 continue; 1553 if (last_if == NULL || strcmp(last_if, p->ifname)) 1554 got4 = got6 = 0; 1555 last_if = p->ifname; 1556 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET && got4) 1557 continue; 1558 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && got6) 1559 continue; 1560 if (p->af == AF_INET) 1561 got4 = 1; 1562 else 1563 got6 = 1; 1564 n = calloc(1, sizeof(struct node_host)); 1565 if (n == NULL) 1566 err(1, "%s: calloc", __func__); 1567 n->af = p->af; 1568 if (flags & PFI_AFLAG_BROADCAST) 1569 memcpy(&n->addr.v.a.addr, &p->bcast, 1570 sizeof(struct pf_addr)); 1571 else if (flags & PFI_AFLAG_PEER) 1572 memcpy(&n->addr.v.a.addr, &p->peer, 1573 sizeof(struct pf_addr)); 1574 else 1575 memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr, 1576 sizeof(struct pf_addr)); 1577 if (flags & PFI_AFLAG_NETWORK) 1578 set_ipmask(n, unmask(&p->addr.v.a.mask)); 1579 else 1580 set_ipmask(n, -1); 1581 n->ifindex = p->ifindex; 1582 1583 n->next = NULL; 1584 n->tail = n; 1585 if (h == NULL) 1586 h = n; 1587 else { 1588 h->tail->next = n; 1589 h->tail = n; 1590 } 1591 } 1592 return (h); 1593 } 1594 1595 int 1596 ifa_skip_if(const char *filter, struct node_host *p) 1597 { 1598 int n; 1599 1600 if (p->af != AF_INET && p->af != AF_INET6) 1601 return (1); 1602 if (filter == NULL || !*filter) 1603 return (0); 1604 if (!strcmp(p->ifname, filter)) 1605 return (0); /* exact match */ 1606 n = strlen(filter); 1607 if (n < 1 || n >= IFNAMSIZ) 1608 return (1); /* sanity check */ 1609 if (filter[n-1] >= '0' && filter[n-1] <= '9') 1610 return (1); /* only do exact match in that case */ 1611 if (strncmp(p->ifname, filter, n)) 1612 return (1); /* prefix doesn't match */ 1613 return (p->ifname[n] < '0' || p->ifname[n] > '9'); 1614 } 1615 1616 struct node_host * 1617 host(const char *s, int opts) 1618 { 1619 struct node_host *h = NULL, *n; 1620 int mask = -1; 1621 char *p, *ps; 1622 const char *errstr; 1623 1624 if ((ps = strdup(s)) == NULL) 1625 err(1, "%s: strdup", __func__); 1626 1627 if ((p = strchr(ps, '/')) != NULL) { 1628 mask = strtonum(p+1, 0, 128, &errstr); 1629 if (errstr) { 1630 fprintf(stderr, "netmask is %s: %s\n", errstr, p); 1631 goto error; 1632 } 1633 p[0] = '\0'; 1634 } 1635 1636 if ((h = host_if(ps, mask)) == NULL && 1637 (h = host_ip(ps, mask)) == NULL && 1638 (h = host_dns(ps, mask, (opts & PF_OPT_NODNS))) == NULL) { 1639 fprintf(stderr, "no IP address found for %s\n", s); 1640 goto error; 1641 } 1642 1643 for (n = h; n != NULL; n = n->next) { 1644 n->addr.type = PF_ADDR_ADDRMASK; 1645 n->weight = 0; 1646 } 1647 1648 error: 1649 free(ps); 1650 return (h); 1651 } 1652 1653 struct node_host * 1654 host_if(const char *s, int mask) 1655 { 1656 struct node_host *n, *h = NULL; 1657 char *p, *ps; 1658 int flags = 0; 1659 1660 if ((ps = strdup(s)) == NULL) 1661 err(1, "host_if: strdup"); 1662 while ((p = strrchr(ps, ':')) != NULL) { 1663 if (!strcmp(p+1, "network")) 1664 flags |= PFI_AFLAG_NETWORK; 1665 else if (!strcmp(p+1, "broadcast")) 1666 flags |= PFI_AFLAG_BROADCAST; 1667 else if (!strcmp(p+1, "peer")) 1668 flags |= PFI_AFLAG_PEER; 1669 else if (!strcmp(p+1, "0")) 1670 flags |= PFI_AFLAG_NOALIAS; 1671 else 1672 goto error; 1673 *p = '\0'; 1674 } 1675 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { /* Yep! */ 1676 fprintf(stderr, "illegal combination of interface modifiers\n"); 1677 goto error; 1678 } 1679 if ((flags & (PFI_AFLAG_NETWORK|PFI_AFLAG_BROADCAST)) && mask > -1) { 1680 fprintf(stderr, "network or broadcast lookup, but " 1681 "extra netmask given\n"); 1682 goto error; 1683 } 1684 if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) { 1685 /* interface with this name exists */ 1686 h = ifa_lookup(ps, flags); 1687 if (mask > -1) 1688 for (n = h; n != NULL; n = n->next) 1689 set_ipmask(n, mask); 1690 } 1691 1692 error: 1693 free(ps); 1694 return (h); 1695 } 1696 1697 struct node_host * 1698 host_ip(const char *s, int mask) 1699 { 1700 struct addrinfo hints, *res; 1701 struct node_host *h = NULL; 1702 1703 memset(&hints, 0, sizeof(hints)); 1704 hints.ai_family = AF_UNSPEC; 1705 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1706 hints.ai_flags = AI_NUMERICHOST; 1707 if (getaddrinfo(s, NULL, &hints, &res) == 0) { 1708 h = calloc(1, sizeof(*h)); 1709 if (h == NULL) 1710 err(1, "%s: calloc", __func__); 1711 h->af = res->ai_family; 1712 copy_satopfaddr(&h->addr.v.a.addr, res->ai_addr); 1713 if (h->af == AF_INET6) 1714 h->ifindex = 1715 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1716 freeaddrinfo(res); 1717 } else { /* ie. for 10/8 parsing */ 1718 if (mask == -1) 1719 return (NULL); 1720 h = calloc(1, sizeof(*h)); 1721 if (h == NULL) 1722 err(1, "%s: calloc", __func__); 1723 h->af = AF_INET; 1724 if (inet_net_pton(AF_INET, s, &h->addr.v.a.addr.v4, 1725 sizeof(h->addr.v.a.addr.v4)) == -1) { 1726 free(h); 1727 return (NULL); 1728 } 1729 } 1730 set_ipmask(h, mask); 1731 h->ifname = NULL; 1732 h->next = NULL; 1733 h->tail = h; 1734 1735 return (h); 1736 } 1737 1738 struct node_host * 1739 host_dns(const char *s, int mask, int numeric) 1740 { 1741 struct addrinfo hints, *res0, *res; 1742 struct node_host *n, *h = NULL; 1743 int noalias = 0, got4 = 0, got6 = 0; 1744 char *p, *ps; 1745 1746 if ((ps = strdup(s)) == NULL) 1747 err(1, "host_dns: strdup"); 1748 if ((p = strrchr(ps, ':')) != NULL && !strcmp(p, ":0")) { 1749 noalias = 1; 1750 *p = '\0'; 1751 } 1752 memset(&hints, 0, sizeof(hints)); 1753 hints.ai_family = PF_UNSPEC; 1754 hints.ai_socktype = SOCK_STREAM; /* DUMMY */ 1755 if (numeric) 1756 hints.ai_flags = AI_NUMERICHOST; 1757 if (getaddrinfo(ps, NULL, &hints, &res0) != 0) 1758 goto error; 1759 1760 for (res = res0; res; res = res->ai_next) { 1761 if (res->ai_family != AF_INET && 1762 res->ai_family != AF_INET6) 1763 continue; 1764 if (noalias) { 1765 if (res->ai_family == AF_INET) { 1766 if (got4) 1767 continue; 1768 got4 = 1; 1769 } else { 1770 if (got6) 1771 continue; 1772 got6 = 1; 1773 } 1774 } 1775 n = calloc(1, sizeof(struct node_host)); 1776 if (n == NULL) 1777 err(1, "host_dns: calloc"); 1778 n->ifname = NULL; 1779 n->af = res->ai_family; 1780 copy_satopfaddr(&n->addr.v.a.addr, res->ai_addr); 1781 if (res->ai_family == AF_INET6) 1782 n->ifindex = 1783 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1784 set_ipmask(n, mask); 1785 n->next = NULL; 1786 n->tail = n; 1787 if (h == NULL) 1788 h = n; 1789 else { 1790 h->tail->next = n; 1791 h->tail = n; 1792 } 1793 } 1794 freeaddrinfo(res0); 1795 error: 1796 free(ps); 1797 1798 return (h); 1799 } 1800 1801 /* 1802 * convert a hostname to a list of addresses and put them in the given buffer. 1803 * test: 1804 * if set to 1, only simple addresses are accepted (no netblock, no "!"). 1805 */ 1806 int 1807 append_addr(struct pfr_buffer *b, char *s, int test, int opts) 1808 { 1809 static int previous = 0; 1810 static int expect = 0; 1811 struct pfr_addr *a; 1812 struct node_host *h, *n; 1813 char *r; 1814 const char *errstr; 1815 int rv, not = 0, i = 0; 1816 u_int16_t weight; 1817 1818 /* skip weight if given */ 1819 if (strcmp(s, "weight") == 0) { 1820 expect = 1; 1821 return (1); /* expecting further call */ 1822 } 1823 1824 /* check if previous host is set */ 1825 if (expect) { 1826 /* parse and append load balancing weight */ 1827 weight = strtonum(s, 1, USHRT_MAX, &errstr); 1828 if (errstr) { 1829 fprintf(stderr, "failed to convert weight %s\n", s); 1830 return (-1); 1831 } 1832 if (previous != -1) { 1833 PFRB_FOREACH(a, b) { 1834 if (++i >= previous) { 1835 a->pfra_weight = weight; 1836 a->pfra_type = PFRKE_COST; 1837 } 1838 } 1839 } 1840 1841 expect = 0; 1842 return (0); 1843 } 1844 1845 for (r = s; *r == '!'; r++) 1846 not = !not; 1847 if ((n = host(r, opts)) == NULL) { 1848 errno = 0; 1849 return (-1); 1850 } 1851 rv = append_addr_host(b, n, test, not); 1852 previous = b->pfrb_size; 1853 do { 1854 h = n; 1855 n = n->next; 1856 free(h); 1857 } while (n != NULL); 1858 return (rv); 1859 } 1860 1861 /* 1862 * same as previous function, but with a pre-parsed input and the ability 1863 * to "negate" the result. Does not free the node_host list. 1864 * not: 1865 * setting it to 1 is equivalent to adding "!" in front of parameter s. 1866 */ 1867 int 1868 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not) 1869 { 1870 int bits; 1871 struct pfr_addr addr; 1872 1873 do { 1874 bzero(&addr, sizeof(addr)); 1875 addr.pfra_not = n->not ^ not; 1876 addr.pfra_af = n->af; 1877 addr.pfra_net = unmask(&n->addr.v.a.mask); 1878 if (n->ifname) { 1879 if (strlcpy(addr.pfra_ifname, n->ifname, 1880 sizeof(addr.pfra_ifname)) >= sizeof(addr.pfra_ifname)) 1881 errx(1, "append_addr_host: strlcpy"); 1882 addr.pfra_type = PFRKE_ROUTE; 1883 } 1884 if (n->weight > 0) { 1885 addr.pfra_weight = n->weight; 1886 addr.pfra_type = PFRKE_COST; 1887 } 1888 switch (n->af) { 1889 case AF_INET: 1890 addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0]; 1891 bits = 32; 1892 break; 1893 case AF_INET6: 1894 memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6, 1895 sizeof(struct in6_addr)); 1896 bits = 128; 1897 break; 1898 default: 1899 errno = EINVAL; 1900 return (-1); 1901 } 1902 if ((test && (not || addr.pfra_net != bits)) || 1903 addr.pfra_net > bits) { 1904 errno = EINVAL; 1905 return (-1); 1906 } 1907 if (pfr_buf_add(b, &addr)) 1908 return (-1); 1909 } while ((n = n->next) != NULL); 1910 1911 return (0); 1912 } 1913 1914 int 1915 pfctl_add_trans(struct pfr_buffer *buf, int type, const char *anchor) 1916 { 1917 struct pfioc_trans_e trans; 1918 1919 bzero(&trans, sizeof(trans)); 1920 trans.type = type; 1921 if (strlcpy(trans.anchor, anchor, 1922 sizeof(trans.anchor)) >= sizeof(trans.anchor)) 1923 errx(1, "pfctl_add_trans: strlcpy"); 1924 1925 return pfr_buf_add(buf, &trans); 1926 } 1927 1928 u_int32_t 1929 pfctl_get_ticket(struct pfr_buffer *buf, int type, const char *anchor) 1930 { 1931 struct pfioc_trans_e *p; 1932 1933 PFRB_FOREACH(p, buf) 1934 if (type == p->type && !strcmp(anchor, p->anchor)) 1935 return (p->ticket); 1936 errx(1, "pfctl_get_ticket: assertion failed"); 1937 } 1938 1939 int 1940 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from) 1941 { 1942 struct pfioc_trans trans; 1943 1944 bzero(&trans, sizeof(trans)); 1945 trans.size = buf->pfrb_size - from; 1946 trans.esize = sizeof(struct pfioc_trans_e); 1947 trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from; 1948 return ioctl(dev, cmd, &trans); 1949 } 1950