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