1 /* $OpenBSD: pfctl_parser.c,v 1.181 2003/11/14 15:32:33 henning Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Daniel Hartmeier 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33 #include <sys/types.h> 34 #include <sys/ioctl.h> 35 #include <sys/socket.h> 36 #include <net/if.h> 37 #include <netinet/in.h> 38 #include <netinet/in_systm.h> 39 #include <netinet/ip.h> 40 #include <netinet/ip_icmp.h> 41 #include <netinet/icmp6.h> 42 #include <net/pfvar.h> 43 #include <arpa/inet.h> 44 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <ctype.h> 49 #include <netdb.h> 50 #include <stdarg.h> 51 #include <errno.h> 52 #include <err.h> 53 #include <ifaddrs.h> 54 55 #include "pfctl_parser.h" 56 #include "pfctl.h" 57 58 void print_op (u_int8_t, const char *, const char *); 59 void print_port (u_int8_t, u_int16_t, u_int16_t, const char *); 60 void print_ugid (u_int8_t, unsigned, unsigned, const char *, unsigned); 61 void print_flags (u_int8_t); 62 void print_fromto(struct pf_rule_addr *, pf_osfp_t, 63 struct pf_rule_addr *, u_int8_t, u_int8_t, int); 64 65 struct node_host *host_if(const char *, int); 66 struct node_host *host_v4(const char *, int); 67 struct node_host *host_v6(const char *, int); 68 struct node_host *host_dns(const char *, int, int); 69 70 const char *tcpflags = "FSRPAUEW"; 71 72 static const struct icmptypeent icmp_type[] = { 73 { "echoreq", ICMP_ECHO }, 74 { "echorep", ICMP_ECHOREPLY }, 75 { "unreach", ICMP_UNREACH }, 76 { "squench", ICMP_SOURCEQUENCH }, 77 { "redir", ICMP_REDIRECT }, 78 { "althost", ICMP_ALTHOSTADDR }, 79 { "routeradv", ICMP_ROUTERADVERT }, 80 { "routersol", ICMP_ROUTERSOLICIT }, 81 { "timex", ICMP_TIMXCEED }, 82 { "paramprob", ICMP_PARAMPROB }, 83 { "timereq", ICMP_TSTAMP }, 84 { "timerep", ICMP_TSTAMPREPLY }, 85 { "inforeq", ICMP_IREQ }, 86 { "inforep", ICMP_IREQREPLY }, 87 { "maskreq", ICMP_MASKREQ }, 88 { "maskrep", ICMP_MASKREPLY }, 89 { "trace", ICMP_TRACEROUTE }, 90 { "dataconv", ICMP_DATACONVERR }, 91 { "mobredir", ICMP_MOBILE_REDIRECT }, 92 { "ipv6-where", ICMP_IPV6_WHEREAREYOU }, 93 { "ipv6-here", ICMP_IPV6_IAMHERE }, 94 { "mobregreq", ICMP_MOBILE_REGREQUEST }, 95 { "mobregrep", ICMP_MOBILE_REGREPLY }, 96 { "skip", ICMP_SKIP }, 97 { "photuris", ICMP_PHOTURIS } 98 }; 99 100 static const struct icmptypeent icmp6_type[] = { 101 { "unreach", ICMP6_DST_UNREACH }, 102 { "toobig", ICMP6_PACKET_TOO_BIG }, 103 { "timex", ICMP6_TIME_EXCEEDED }, 104 { "paramprob", ICMP6_PARAM_PROB }, 105 { "echoreq", ICMP6_ECHO_REQUEST }, 106 { "echorep", ICMP6_ECHO_REPLY }, 107 { "groupqry", ICMP6_MEMBERSHIP_QUERY }, 108 { "listqry", MLD_LISTENER_QUERY }, 109 { "grouprep", ICMP6_MEMBERSHIP_REPORT }, 110 { "listenrep", MLD_LISTENER_REPORT }, 111 { "groupterm", ICMP6_MEMBERSHIP_REDUCTION }, 112 { "listendone", MLD_LISTENER_DONE }, 113 { "routersol", ND_ROUTER_SOLICIT }, 114 { "routeradv", ND_ROUTER_ADVERT }, 115 { "neighbrsol", ND_NEIGHBOR_SOLICIT }, 116 { "neighbradv", ND_NEIGHBOR_ADVERT }, 117 { "redir", ND_REDIRECT }, 118 { "routrrenum", ICMP6_ROUTER_RENUMBERING }, 119 { "wrureq", ICMP6_WRUREQUEST }, 120 { "wrurep", ICMP6_WRUREPLY }, 121 { "fqdnreq", ICMP6_FQDN_QUERY }, 122 { "fqdnrep", ICMP6_FQDN_REPLY }, 123 { "niqry", ICMP6_NI_QUERY }, 124 { "nirep", ICMP6_NI_REPLY }, 125 { "mtraceresp", MLD_MTRACE_RESP }, 126 { "mtrace", MLD_MTRACE } 127 }; 128 129 static const struct icmpcodeent icmp_code[] = { 130 { "net-unr", ICMP_UNREACH, ICMP_UNREACH_NET }, 131 { "host-unr", ICMP_UNREACH, ICMP_UNREACH_HOST }, 132 { "proto-unr", ICMP_UNREACH, ICMP_UNREACH_PROTOCOL }, 133 { "port-unr", ICMP_UNREACH, ICMP_UNREACH_PORT }, 134 { "needfrag", ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG }, 135 { "srcfail", ICMP_UNREACH, ICMP_UNREACH_SRCFAIL }, 136 { "net-unk", ICMP_UNREACH, ICMP_UNREACH_NET_UNKNOWN }, 137 { "host-unk", ICMP_UNREACH, ICMP_UNREACH_HOST_UNKNOWN }, 138 { "isolate", ICMP_UNREACH, ICMP_UNREACH_ISOLATED }, 139 { "net-prohib", ICMP_UNREACH, ICMP_UNREACH_NET_PROHIB }, 140 { "host-prohib", ICMP_UNREACH, ICMP_UNREACH_HOST_PROHIB }, 141 { "net-tos", ICMP_UNREACH, ICMP_UNREACH_TOSNET }, 142 { "host-tos", ICMP_UNREACH, ICMP_UNREACH_TOSHOST }, 143 { "filter-prohib", ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB }, 144 { "host-preced", ICMP_UNREACH, ICMP_UNREACH_HOST_PRECEDENCE }, 145 { "cutoff-preced", ICMP_UNREACH, ICMP_UNREACH_PRECEDENCE_CUTOFF }, 146 { "redir-net", ICMP_REDIRECT, ICMP_REDIRECT_NET }, 147 { "redir-host", ICMP_REDIRECT, ICMP_REDIRECT_HOST }, 148 { "redir-tos-net", ICMP_REDIRECT, ICMP_REDIRECT_TOSNET }, 149 { "redir-tos-host", ICMP_REDIRECT, ICMP_REDIRECT_TOSHOST }, 150 { "normal-adv", ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NORMAL }, 151 { "common-adv", ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NOROUTE_COMMON }, 152 { "transit", ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS }, 153 { "reassemb", ICMP_TIMXCEED, ICMP_TIMXCEED_REASS }, 154 { "badhead", ICMP_PARAMPROB, ICMP_PARAMPROB_ERRATPTR }, 155 { "optmiss", ICMP_PARAMPROB, ICMP_PARAMPROB_OPTABSENT }, 156 { "badlen", ICMP_PARAMPROB, ICMP_PARAMPROB_LENGTH }, 157 { "unknown-ind", ICMP_PHOTURIS, ICMP_PHOTURIS_UNKNOWN_INDEX }, 158 { "auth-fail", ICMP_PHOTURIS, ICMP_PHOTURIS_AUTH_FAILED }, 159 { "decrypt-fail", ICMP_PHOTURIS, ICMP_PHOTURIS_DECRYPT_FAILED } 160 }; 161 162 static const struct icmpcodeent icmp6_code[] = { 163 { "admin-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN }, 164 { "noroute-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE }, 165 { "notnbr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOTNEIGHBOR }, 166 { "beyond-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_BEYONDSCOPE }, 167 { "addr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR }, 168 { "port-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT }, 169 { "transit", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT }, 170 { "reassemb", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY }, 171 { "badhead", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER }, 172 { "nxthdr", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER }, 173 { "redironlink", ND_REDIRECT, ND_REDIRECT_ONLINK }, 174 { "redirrouter", ND_REDIRECT, ND_REDIRECT_ROUTER } 175 }; 176 177 const struct pf_timeout pf_timeouts[] = { 178 { "tcp.first", PFTM_TCP_FIRST_PACKET }, 179 { "tcp.opening", PFTM_TCP_OPENING }, 180 { "tcp.established", PFTM_TCP_ESTABLISHED }, 181 { "tcp.closing", PFTM_TCP_CLOSING }, 182 { "tcp.finwait", PFTM_TCP_FIN_WAIT }, 183 { "tcp.closed", PFTM_TCP_CLOSED }, 184 { "udp.first", PFTM_UDP_FIRST_PACKET }, 185 { "udp.single", PFTM_UDP_SINGLE }, 186 { "udp.multiple", PFTM_UDP_MULTIPLE }, 187 { "icmp.first", PFTM_ICMP_FIRST_PACKET }, 188 { "icmp.error", PFTM_ICMP_ERROR_REPLY }, 189 { "other.first", PFTM_OTHER_FIRST_PACKET }, 190 { "other.single", PFTM_OTHER_SINGLE }, 191 { "other.multiple", PFTM_OTHER_MULTIPLE }, 192 { "frag", PFTM_FRAG }, 193 { "interval", PFTM_INTERVAL }, 194 { "adaptive.start", PFTM_ADAPTIVE_START }, 195 { "adaptive.end", PFTM_ADAPTIVE_END }, 196 { NULL, 0 } 197 }; 198 199 const struct icmptypeent * 200 geticmptypebynumber(u_int8_t type, sa_family_t af) 201 { 202 unsigned int i; 203 204 if (af != AF_INET6) { 205 for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0])); 206 i++) { 207 if (type == icmp_type[i].type) 208 return (&icmp_type[i]); 209 } 210 } else { 211 for (i=0; i < (sizeof (icmp6_type) / 212 sizeof(icmp6_type[0])); i++) { 213 if (type == icmp6_type[i].type) 214 return (&icmp6_type[i]); 215 } 216 } 217 return (NULL); 218 } 219 220 const struct icmptypeent * 221 geticmptypebyname(char *w, sa_family_t af) 222 { 223 unsigned int i; 224 225 if (af != AF_INET6) { 226 for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0])); 227 i++) { 228 if (!strcmp(w, icmp_type[i].name)) 229 return (&icmp_type[i]); 230 } 231 } else { 232 for (i=0; i < (sizeof (icmp6_type) / 233 sizeof(icmp6_type[0])); i++) { 234 if (!strcmp(w, icmp6_type[i].name)) 235 return (&icmp6_type[i]); 236 } 237 } 238 return (NULL); 239 } 240 241 const struct icmpcodeent * 242 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af) 243 { 244 unsigned int i; 245 246 if (af != AF_INET6) { 247 for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0])); 248 i++) { 249 if (type == icmp_code[i].type && 250 code == icmp_code[i].code) 251 return (&icmp_code[i]); 252 } 253 } else { 254 for (i=0; i < (sizeof (icmp6_code) / 255 sizeof(icmp6_code[0])); i++) { 256 if (type == icmp6_code[i].type && 257 code == icmp6_code[i].code) 258 return (&icmp6_code[i]); 259 } 260 } 261 return (NULL); 262 } 263 264 const struct icmpcodeent * 265 geticmpcodebyname(u_long type, char *w, sa_family_t af) 266 { 267 unsigned int i; 268 269 if (af != AF_INET6) { 270 for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0])); 271 i++) { 272 if (type == icmp_code[i].type && 273 !strcmp(w, icmp_code[i].name)) 274 return (&icmp_code[i]); 275 } 276 } else { 277 for (i=0; i < (sizeof (icmp6_code) / 278 sizeof(icmp6_code[0])); i++) { 279 if (type == icmp6_code[i].type && 280 !strcmp(w, icmp6_code[i].name)) 281 return (&icmp6_code[i]); 282 } 283 } 284 return (NULL); 285 } 286 287 void 288 print_op(u_int8_t op, const char *a1, const char *a2) 289 { 290 if (op == PF_OP_IRG) 291 printf(" %s >< %s", a1, a2); 292 else if (op == PF_OP_XRG) 293 printf(" %s <> %s", a1, a2); 294 else if (op == PF_OP_EQ) 295 printf(" = %s", a1); 296 else if (op == PF_OP_NE) 297 printf(" != %s", a1); 298 else if (op == PF_OP_LT) 299 printf(" < %s", a1); 300 else if (op == PF_OP_LE) 301 printf(" <= %s", a1); 302 else if (op == PF_OP_GT) 303 printf(" > %s", a1); 304 else if (op == PF_OP_GE) 305 printf(" >= %s", a1); 306 else if (op == PF_OP_RRG) 307 printf(" %s:%s", a1, a2); 308 } 309 310 void 311 print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, const char *proto) 312 { 313 char a1[6], a2[6]; 314 struct servent *s; 315 316 s = getservbyport(p1, proto); 317 p1 = ntohs(p1); 318 p2 = ntohs(p2); 319 snprintf(a1, sizeof(a1), "%u", p1); 320 snprintf(a2, sizeof(a2), "%u", p2); 321 printf(" port"); 322 if (s != NULL && (op == PF_OP_EQ || op == PF_OP_NE)) 323 print_op(op, s->s_name, a2); 324 else 325 print_op(op, a1, a2); 326 } 327 328 void 329 print_ugid(u_int8_t op, unsigned u1, unsigned u2, const char *t, unsigned umax) 330 { 331 char a1[11], a2[11]; 332 333 snprintf(a1, sizeof(a1), "%u", u1); 334 snprintf(a2, sizeof(a2), "%u", u2); 335 printf(" %s", t); 336 if (u1 == umax && (op == PF_OP_EQ || op == PF_OP_NE)) 337 print_op(op, "unknown", a2); 338 else 339 print_op(op, a1, a2); 340 } 341 342 void 343 print_flags(u_int8_t f) 344 { 345 int i; 346 347 for (i = 0; tcpflags[i]; ++i) 348 if (f & (1 << i)) 349 printf("%c", tcpflags[i]); 350 } 351 352 void 353 print_fromto(struct pf_rule_addr *src, pf_osfp_t osfp, struct pf_rule_addr *dst, 354 sa_family_t af, u_int8_t proto, int verbose) 355 { 356 char buf[PF_OSFP_LEN*3]; 357 if (src->addr.type == PF_ADDR_ADDRMASK && 358 dst->addr.type == PF_ADDR_ADDRMASK && 359 PF_AZERO(&src->addr.v.a.addr, AF_INET6) && 360 PF_AZERO(&src->addr.v.a.mask, AF_INET6) && 361 PF_AZERO(&dst->addr.v.a.addr, AF_INET6) && 362 PF_AZERO(&dst->addr.v.a.mask, AF_INET6) && 363 !src->not && !dst->not && 364 !src->port_op && !dst->port_op && 365 osfp == PF_OSFP_ANY) 366 printf(" all"); 367 else { 368 printf(" from "); 369 if (src->not) 370 printf("! "); 371 print_addr(&src->addr, af, verbose); 372 if (src->port_op) 373 print_port(src->port_op, src->port[0], 374 src->port[1], 375 proto == IPPROTO_TCP ? "tcp" : "udp"); 376 if (osfp != PF_OSFP_ANY) 377 printf(" os \"%s\"", pfctl_lookup_fingerprint(osfp, buf, 378 sizeof(buf))); 379 380 printf(" to "); 381 if (dst->not) 382 printf("! "); 383 print_addr(&dst->addr, af, verbose); 384 if (dst->port_op) 385 print_port(dst->port_op, dst->port[0], 386 dst->port[1], 387 proto == IPPROTO_TCP ? "tcp" : "udp"); 388 } 389 } 390 391 void 392 print_pool(struct pf_pool *pool, u_int16_t p1, u_int16_t p2, 393 sa_family_t af, int id) 394 { 395 struct pf_pooladdr *pooladdr; 396 397 if ((TAILQ_FIRST(&pool->list) != NULL) && 398 TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL) 399 printf("{ "); 400 TAILQ_FOREACH(pooladdr, &pool->list, entries){ 401 switch (id) { 402 case PF_NAT: 403 case PF_RDR: 404 case PF_BINAT: 405 print_addr(&pooladdr->addr, af, 0); 406 break; 407 case PF_PASS: 408 if (PF_AZERO(&pooladdr->addr.v.a.addr, af)) 409 printf("%s", pooladdr->ifname); 410 else { 411 printf("(%s ", pooladdr->ifname); 412 print_addr(&pooladdr->addr, af, 0); 413 printf(")"); 414 } 415 break; 416 default: 417 break; 418 } 419 if (TAILQ_NEXT(pooladdr, entries) != NULL) 420 printf(", "); 421 else if (TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL) 422 printf(" }"); 423 } 424 switch (id) { 425 case PF_NAT: 426 if ((p1 != PF_NAT_PROXY_PORT_LOW || 427 p2 != PF_NAT_PROXY_PORT_HIGH) && (p1 != 0 || p2 != 0)) { 428 if (p1 == p2) 429 printf(" port %u", p1); 430 else 431 printf(" port %u:%u", p1, p2); 432 } 433 break; 434 case PF_RDR: 435 if (p1) { 436 printf(" port %u", p1); 437 if (p2 && (p2 != p1)) 438 printf(":%u", p2); 439 } 440 break; 441 default: 442 break; 443 } 444 switch (pool->opts & PF_POOL_TYPEMASK) { 445 case PF_POOL_NONE: 446 break; 447 case PF_POOL_BITMASK: 448 printf(" bitmask"); 449 break; 450 case PF_POOL_RANDOM: 451 printf(" random"); 452 break; 453 case PF_POOL_SRCHASH: 454 printf(" source-hash 0x%08x%08x%08x%08x", 455 pool->key.key32[0], pool->key.key32[1], 456 pool->key.key32[2], pool->key.key32[3]); 457 break; 458 case PF_POOL_ROUNDROBIN: 459 printf(" round-robin"); 460 break; 461 } 462 if (id == PF_NAT && p1 == 0 && p2 == 0) 463 printf(" static-port"); 464 } 465 466 const char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES; 467 const char *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES; 468 469 void 470 print_status(struct pf_status *s) 471 { 472 char statline[80]; 473 time_t runtime; 474 int i; 475 476 runtime = time(NULL) - s->since; 477 478 if (s->running) { 479 unsigned sec, min, hrs, day = runtime; 480 481 sec = day % 60; 482 day /= 60; 483 min = day % 60; 484 day /= 60; 485 hrs = day % 24; 486 day /= 24; 487 snprintf(statline, sizeof(statline), 488 "Status: Enabled for %u days %.2u:%.2u:%.2u", 489 day, hrs, min, sec); 490 } else 491 snprintf(statline, sizeof(statline), "Status: Disabled"); 492 printf("%-44s", statline); 493 switch (s->debug) { 494 case PF_DEBUG_NONE: 495 printf("%15s\n\n", "Debug: None"); 496 break; 497 case PF_DEBUG_URGENT: 498 printf("%15s\n\n", "Debug: Urgent"); 499 break; 500 case PF_DEBUG_MISC: 501 printf("%15s\n\n", "Debug: Misc"); 502 break; 503 case PF_DEBUG_NOISY: 504 printf("%15s\n\n", "Debug: Loud"); 505 break; 506 } 507 if (s->ifname[0] != 0) { 508 printf("Interface Stats for %-16s %5s %16s\n", 509 s->ifname, "IPv4", "IPv6"); 510 printf(" %-25s %14llu %16llu\n", "Bytes In", 511 s->bcounters[0][0], s->bcounters[1][0]); 512 printf(" %-25s %14llu %16llu\n", "Bytes Out", 513 s->bcounters[0][1], s->bcounters[1][1]); 514 printf(" Packets In\n"); 515 printf(" %-23s %14llu %16llu\n", "Passed", 516 s->pcounters[0][0][PF_PASS], 517 s->pcounters[1][0][PF_PASS]); 518 printf(" %-23s %14llu %16llu\n", "Blocked", 519 s->pcounters[0][0][PF_DROP], 520 s->pcounters[1][0][PF_DROP]); 521 printf(" Packets Out\n"); 522 printf(" %-23s %14llu %16llu\n", "Passed", 523 s->pcounters[0][1][PF_PASS], 524 s->pcounters[1][1][PF_PASS]); 525 printf(" %-23s %14llu %16llu\n\n", "Blocked", 526 s->pcounters[0][1][PF_DROP], 527 s->pcounters[1][1][PF_DROP]); 528 } 529 printf("%-27s %14s %16s\n", "State Table", "Total", "Rate"); 530 printf(" %-25s %14u %14s\n", "current entries", s->states, ""); 531 for (i = 0; i < FCNT_MAX; i++) { 532 printf(" %-25s %14llu", pf_fcounters[i], 533 (unsigned long long)s->fcounters[i]); 534 if (runtime > 0) 535 printf("%14.1f/s\n", 536 (double)s->fcounters[i] / (double)runtime); 537 else 538 printf("%14s\n", ""); 539 } 540 printf("Counters\n"); 541 for (i = 0; i < PFRES_MAX; i++) { 542 printf(" %-25s %14llu ", pf_reasons[i], 543 (unsigned long long)s->counters[i]); 544 if (runtime > 0) 545 printf("%14.1f/s\n", 546 (double)s->counters[i] / (double)runtime); 547 else 548 printf("%14s\n", ""); 549 } 550 } 551 552 void 553 print_rule(struct pf_rule *r, int verbose) 554 { 555 static const char *actiontypes[] = { "pass", "block", "scrub", "nat", 556 "no nat", "binat", "no binat", "rdr", "no rdr" }; 557 static const char *anchortypes[] = { "anchor", "anchor", "anchor", 558 "nat-anchor", "nat-anchor", "binat-anchor", "binat-anchor", 559 "rdr-anchor", "rdr-anchor" }; 560 int i, opts; 561 562 if (verbose) 563 printf("@%d ", r->nr); 564 if (r->action > PF_NORDR) 565 printf("action(%d)", r->action); 566 else if (r->anchorname[0]) 567 printf("%s %s", anchortypes[r->action], r->anchorname); 568 else { 569 printf("%s", actiontypes[r->action]); 570 if (r->natpass) 571 printf(" pass"); 572 } 573 if (r->action == PF_DROP) { 574 if (r->rule_flag & PFRULE_RETURN) 575 printf(" return"); 576 else if (r->rule_flag & PFRULE_RETURNRST) { 577 if (!r->return_ttl) 578 printf(" return-rst"); 579 else 580 printf(" return-rst(ttl %d)", r->return_ttl); 581 } else if (r->rule_flag & PFRULE_RETURNICMP) { 582 const struct icmpcodeent *ic, *ic6; 583 584 ic = geticmpcodebynumber(r->return_icmp >> 8, 585 r->return_icmp & 255, AF_INET); 586 ic6 = geticmpcodebynumber(r->return_icmp6 >> 8, 587 r->return_icmp6 & 255, AF_INET6); 588 589 switch(r->af) { 590 case AF_INET: 591 printf(" return-icmp"); 592 if (ic == NULL) 593 printf("(%u)", r->return_icmp & 255); 594 else 595 printf("(%s)", ic->name); 596 break; 597 case AF_INET6: 598 printf(" return-icmp6"); 599 if (ic6 == NULL) 600 printf("(%u)", r->return_icmp6 & 255); 601 else 602 printf("(%s)", ic6->name); 603 break; 604 default: 605 printf(" return-icmp"); 606 if (ic == NULL) 607 printf("(%u, ", r->return_icmp & 255); 608 else 609 printf("(%s, ", ic->name); 610 if (ic6 == NULL) 611 printf("%u)", r->return_icmp6 & 255); 612 else 613 printf("%s)", ic6->name); 614 break; 615 } 616 } else 617 printf(" drop"); 618 } 619 if (r->direction == PF_IN) 620 printf(" in"); 621 else if (r->direction == PF_OUT) 622 printf(" out"); 623 if (r->log == 1) 624 printf(" log"); 625 else if (r->log == 2) 626 printf(" log-all"); 627 if (r->quick) 628 printf(" quick"); 629 if (r->ifname[0]) { 630 if (r->ifnot) 631 printf(" on ! %s", r->ifname); 632 else 633 printf(" on %s", r->ifname); 634 } 635 if (r->rt) { 636 if (r->rt == PF_ROUTETO) 637 printf(" route-to"); 638 else if (r->rt == PF_REPLYTO) 639 printf(" reply-to"); 640 else if (r->rt == PF_DUPTO) 641 printf(" dup-to"); 642 else if (r->rt == PF_FASTROUTE) 643 printf(" fastroute"); 644 if (r->rt != PF_FASTROUTE) { 645 printf(" "); 646 print_pool(&r->rpool, 0, 0, r->af, PF_PASS); 647 } 648 } 649 if (r->af) { 650 if (r->af == AF_INET) 651 printf(" inet"); 652 else 653 printf(" inet6"); 654 } 655 if (r->proto) { 656 struct protoent *p; 657 658 if ((p = getprotobynumber(r->proto)) != NULL) 659 printf(" proto %s", p->p_name); 660 else 661 printf(" proto %u", r->proto); 662 } 663 print_fromto(&r->src, r->os_fingerprint, &r->dst, r->af, r->proto, 664 verbose); 665 if (r->uid.op) 666 print_ugid(r->uid.op, r->uid.uid[0], r->uid.uid[1], "user", 667 UID_MAX); 668 if (r->gid.op) 669 print_ugid(r->gid.op, r->gid.gid[0], r->gid.gid[1], "group", 670 GID_MAX); 671 if (r->flags || r->flagset) { 672 printf(" flags "); 673 print_flags(r->flags); 674 printf("/"); 675 print_flags(r->flagset); 676 } 677 if (r->type) { 678 const struct icmptypeent *it; 679 680 it = geticmptypebynumber(r->type-1, r->af); 681 if (r->af != AF_INET6) 682 printf(" icmp-type"); 683 else 684 printf(" icmp6-type"); 685 if (it != NULL) 686 printf(" %s", it->name); 687 else 688 printf(" %u", r->type-1); 689 if (r->code) { 690 const struct icmpcodeent *ic; 691 692 ic = geticmpcodebynumber(r->type-1, r->code-1, r->af); 693 if (ic != NULL) 694 printf(" code %s", ic->name); 695 else 696 printf(" code %u", r->code-1); 697 } 698 } 699 if (r->tos) 700 printf(" tos 0x%2.2x", r->tos); 701 if (r->keep_state == PF_STATE_NORMAL) 702 printf(" keep state"); 703 else if (r->keep_state == PF_STATE_MODULATE) 704 printf(" modulate state"); 705 else if (r->keep_state == PF_STATE_SYNPROXY) 706 printf(" synproxy state"); 707 opts = 0; 708 if (r->max_states) 709 opts = 1; 710 if (r->rule_flag & PFRULE_NOSYNC) 711 opts = 1; 712 for (i = 0; !opts && i < PFTM_MAX; ++i) 713 if (r->timeout[i]) 714 opts = 1; 715 if (opts) { 716 printf(" ("); 717 if (r->max_states) { 718 printf("max %u", r->max_states); 719 opts = 0; 720 } 721 if (r->rule_flag & PFRULE_NOSYNC) { 722 if (!opts) 723 printf(", "); 724 printf("no-sync"); 725 opts = 0; 726 } 727 for (i = 0; i < PFTM_MAX; ++i) 728 if (r->timeout[i]) { 729 if (!opts) 730 printf(", "); 731 opts = 0; 732 printf("%s %u", pf_timeouts[i].name, 733 r->timeout[i]); 734 } 735 printf(")"); 736 } 737 if (r->rule_flag & PFRULE_FRAGMENT) 738 printf(" fragment"); 739 if (r->rule_flag & PFRULE_NODF) 740 printf(" no-df"); 741 if (r->rule_flag & PFRULE_RANDOMID) 742 printf(" random-id"); 743 if (r->min_ttl) 744 printf(" min-ttl %d", r->min_ttl); 745 if (r->max_mss) 746 printf(" max-mss %d", r->max_mss); 747 if (r->allow_opts) 748 printf(" allow-opts"); 749 if (r->action == PF_SCRUB) { 750 if (r->rule_flag & PFRULE_REASSEMBLE_TCP) 751 printf(" reassemble tcp"); 752 753 if (r->rule_flag & PFRULE_FRAGDROP) 754 printf(" fragment drop-ovl"); 755 else if (r->rule_flag & PFRULE_FRAGCROP) 756 printf(" fragment crop"); 757 else 758 printf(" fragment reassemble"); 759 } 760 if (r->label[0]) 761 printf(" label \"%s\"", r->label); 762 if (r->qname[0] && r->pqname[0]) 763 printf(" queue(%s, %s)", r->qname, r->pqname); 764 else if (r->qname[0]) 765 printf(" queue %s", r->qname); 766 if (r->tagname[0]) 767 printf(" tag %s", r->tagname); 768 if (r->match_tagname[0]) { 769 if (r->match_tag_not) 770 printf(" !"); 771 printf(" tagged %s", r->match_tagname); 772 } 773 if (!r->anchorname[0] && (r->action == PF_NAT || 774 r->action == PF_BINAT || r->action == PF_RDR)) { 775 printf(" -> "); 776 print_pool(&r->rpool, r->rpool.proxy_port[0], 777 r->rpool.proxy_port[1], r->af, r->action); 778 } 779 printf("\n"); 780 } 781 782 void 783 print_tabledef(const char *name, int flags, int addrs, 784 struct node_tinithead *nodes) 785 { 786 struct node_tinit *ti, *nti; 787 struct node_host *h; 788 789 printf("table <%s>", name); 790 if (flags & PFR_TFLAG_CONST) 791 printf(" const"); 792 if (flags & PFR_TFLAG_PERSIST) 793 printf(" persist"); 794 SIMPLEQ_FOREACH(ti, nodes, entries) { 795 if (ti->file) { 796 printf(" file \"%s\"", ti->file); 797 continue; 798 } 799 printf(" {"); 800 for (;;) { 801 for (h = ti->host; h != NULL; h = h->next) { 802 printf(h->not ? " !" : " "); 803 print_addr(&h->addr, h->af, 0); 804 } 805 nti = SIMPLEQ_NEXT(ti, entries); 806 if (nti != NULL && nti->file == NULL) 807 ti = nti; /* merge lists */ 808 else 809 break; 810 } 811 printf(" }"); 812 } 813 if (addrs && SIMPLEQ_EMPTY(nodes)) 814 printf(" { }"); 815 printf("\n"); 816 } 817 818 int 819 parse_flags(char *s) 820 { 821 char *p, *q; 822 u_int8_t f = 0; 823 824 for (p = s; *p; p++) { 825 if ((q = strchr(tcpflags, *p)) == NULL) 826 return -1; 827 else 828 f |= 1 << (q - tcpflags); 829 } 830 return (f ? f : PF_TH_ALL); 831 } 832 833 void 834 set_ipmask(struct node_host *h, u_int8_t b) 835 { 836 struct pf_addr *m, *n; 837 int i, j = 0; 838 839 m = &h->addr.v.a.mask; 840 841 for (i = 0; i < 4; i++) 842 m->addr32[i] = 0; 843 844 while (b >= 32) { 845 m->addr32[j++] = 0xffffffff; 846 b -= 32; 847 } 848 for (i = 31; i > 31-b; --i) 849 m->addr32[j] |= (1 << i); 850 if (b) 851 m->addr32[j] = htonl(m->addr32[j]); 852 853 /* Mask off bits of the address that will never be used. */ 854 n = &h->addr.v.a.addr; 855 if (h->addr.type == PF_ADDR_ADDRMASK) 856 for (i = 0; i < 4; i++) 857 n->addr32[i] = n->addr32[i] & m->addr32[i]; 858 } 859 860 int 861 check_netmask(struct node_host *h, sa_family_t af) 862 { 863 struct node_host *n = NULL; 864 struct pf_addr *m; 865 866 for (n = h; n != NULL; n = n->next) { 867 if (h->addr.type == PF_ADDR_TABLE) 868 continue; 869 m = &h->addr.v.a.mask; 870 /* fix up netmask for dynaddr */ 871 if (af == AF_INET && h->addr.type == PF_ADDR_DYNIFTL && 872 unmask(m, AF_INET6) > 32) 873 set_ipmask(n, 32); 874 /* netmasks > 32 bit are invalid on v4 */ 875 if (af == AF_INET && 876 (m->addr32[1] || m->addr32[2] || m->addr32[3])) { 877 fprintf(stderr, "netmask %u invalid for IPv4 address\n", 878 unmask(m, AF_INET6)); 879 return (1); 880 } 881 } 882 return (0); 883 } 884 885 /* interface lookup routines */ 886 887 struct node_host *iftab; 888 889 void 890 ifa_load(void) 891 { 892 struct ifaddrs *ifap, *ifa; 893 struct node_host *n = NULL, *h = NULL; 894 895 if (getifaddrs(&ifap) < 0) 896 err(1, "getifaddrs"); 897 898 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 899 if (!(ifa->ifa_addr->sa_family == AF_INET || 900 ifa->ifa_addr->sa_family == AF_INET6 || 901 ifa->ifa_addr->sa_family == AF_LINK)) 902 continue; 903 n = calloc(1, sizeof(struct node_host)); 904 if (n == NULL) 905 err(1, "address: calloc"); 906 n->af = ifa->ifa_addr->sa_family; 907 n->ifa_flags = ifa->ifa_flags; 908 #ifdef __KAME__ 909 if (n->af == AF_INET6 && 910 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) 911 ifa->ifa_addr)->sin6_addr) && 912 ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id == 0) { 913 struct sockaddr_in6 *sin6; 914 915 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 916 sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 | 917 sin6->sin6_addr.s6_addr[3]; 918 sin6->sin6_addr.s6_addr[2] = 0; 919 sin6->sin6_addr.s6_addr[3] = 0; 920 } 921 #endif 922 n->ifindex = 0; 923 if (n->af == AF_INET) { 924 memcpy(&n->addr.v.a.addr, &((struct sockaddr_in *) 925 ifa->ifa_addr)->sin_addr.s_addr, 926 sizeof(struct in_addr)); 927 memcpy(&n->addr.v.a.mask, &((struct sockaddr_in *) 928 ifa->ifa_netmask)->sin_addr.s_addr, 929 sizeof(struct in_addr)); 930 if (ifa->ifa_broadaddr != NULL) 931 memcpy(&n->bcast, &((struct sockaddr_in *) 932 ifa->ifa_broadaddr)->sin_addr.s_addr, 933 sizeof(struct in_addr)); 934 } else if (n->af == AF_INET6) { 935 memcpy(&n->addr.v.a.addr, &((struct sockaddr_in6 *) 936 ifa->ifa_addr)->sin6_addr.s6_addr, 937 sizeof(struct in6_addr)); 938 memcpy(&n->addr.v.a.mask, &((struct sockaddr_in6 *) 939 ifa->ifa_netmask)->sin6_addr.s6_addr, 940 sizeof(struct in6_addr)); 941 if (ifa->ifa_broadaddr != NULL) 942 memcpy(&n->bcast, &((struct sockaddr_in6 *) 943 ifa->ifa_broadaddr)->sin6_addr.s6_addr, 944 sizeof(struct in6_addr)); 945 n->ifindex = ((struct sockaddr_in6 *) 946 ifa->ifa_addr)->sin6_scope_id; 947 } 948 if ((n->ifname = strdup(ifa->ifa_name)) == NULL) 949 err(1, "ifa_load: strdup"); 950 n->next = NULL; 951 n->tail = n; 952 if (h == NULL) 953 h = n; 954 else { 955 h->tail->next = n; 956 h->tail = n; 957 } 958 } 959 iftab = h; 960 freeifaddrs(ifap); 961 } 962 963 struct node_host * 964 ifa_exists(const char *ifa_name) 965 { 966 struct node_host *n; 967 968 if (iftab == NULL) 969 ifa_load(); 970 971 for (n = iftab; n; n = n->next) { 972 if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ)) 973 return (n); 974 } 975 return (NULL); 976 } 977 978 struct node_host * 979 ifa_lookup(const char *ifa_name, enum pfctl_iflookup_mode mode) 980 { 981 struct node_host *p = NULL, *h = NULL, *n = NULL; 982 int return_all = 0; 983 984 if (!strncmp(ifa_name, "self", IFNAMSIZ)) 985 return_all = 1; 986 987 if (iftab == NULL) 988 ifa_load(); 989 990 for (p = iftab; p; p = p->next) { 991 if (!((p->af == AF_INET || p->af == AF_INET6) && 992 (!strncmp(p->ifname, ifa_name, IFNAMSIZ) || return_all))) 993 continue; 994 if (mode == PFCTL_IFLOOKUP_BCAST && p->af != AF_INET) 995 continue; 996 if (mode == PFCTL_IFLOOKUP_NET && p->ifindex > 0) 997 continue; 998 n = calloc(1, sizeof(struct node_host)); 999 if (n == NULL) 1000 err(1, "address: calloc"); 1001 n->af = p->af; 1002 if (mode == PFCTL_IFLOOKUP_BCAST) 1003 memcpy(&n->addr.v.a.addr, &p->bcast, 1004 sizeof(struct pf_addr)); 1005 else 1006 memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr, 1007 sizeof(struct pf_addr)); 1008 if (mode == PFCTL_IFLOOKUP_NET) 1009 set_ipmask(n, unmask(&p->addr.v.a.mask, n->af)); 1010 else { 1011 if (n->af == AF_INET) { 1012 if (p->ifa_flags & IFF_LOOPBACK && 1013 p->ifa_flags & IFF_LINK1) 1014 memcpy(&n->addr.v.a.mask, 1015 &p->addr.v.a.mask, 1016 sizeof(struct pf_addr)); 1017 else 1018 set_ipmask(n, 32); 1019 } else 1020 set_ipmask(n, 128); 1021 } 1022 n->ifindex = p->ifindex; 1023 1024 n->next = NULL; 1025 n->tail = n; 1026 if (h == NULL) 1027 h = n; 1028 else { 1029 h->tail->next = n; 1030 h->tail = n; 1031 } 1032 } 1033 if (h == NULL && mode == PFCTL_IFLOOKUP_HOST) { 1034 fprintf(stderr, "no IP address found for %s\n", ifa_name); 1035 } 1036 return (h); 1037 } 1038 1039 struct node_host * 1040 host(const char *s) 1041 { 1042 struct node_host *h = NULL; 1043 int mask, v4mask, v6mask, cont = 1; 1044 char *p, *q, *ps; 1045 1046 if ((p = strrchr(s, '/')) != NULL) { 1047 mask = strtol(p+1, &q, 0); 1048 if (!q || *q || mask > 128 || q == (p+1)) { 1049 fprintf(stderr, "invalid netmask\n"); 1050 return (NULL); 1051 } 1052 if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL) 1053 err(1, "host: malloc"); 1054 strlcpy(ps, s, strlen(s) - strlen(p) + 1); 1055 v4mask = v6mask = mask; 1056 } else { 1057 if ((ps = strdup(s)) == NULL) 1058 err(1, "host: strdup"); 1059 v4mask = 32; 1060 v6mask = 128; 1061 mask = -1; 1062 } 1063 1064 /* interface with this name exists? */ 1065 if (cont && (h = host_if(ps, mask)) != NULL) 1066 cont = 0; 1067 1068 /* IPv4 address? */ 1069 if (cont && (h = host_v4(s, mask)) != NULL) 1070 cont = 0; 1071 1072 /* IPv6 address? */ 1073 if (cont && (h = host_v6(ps, v6mask)) != NULL) 1074 cont = 0; 1075 1076 /* dns lookup */ 1077 if (cont && (h = host_dns(ps, v4mask, v6mask)) != NULL) 1078 cont = 0; 1079 free(ps); 1080 1081 if (h == NULL || cont == 1) { 1082 fprintf(stderr, "no IP address found for %s\n", s); 1083 return (NULL); 1084 } 1085 return (h); 1086 } 1087 1088 struct node_host * 1089 host_if(const char *s, int mask) 1090 { 1091 struct node_host *n, *h = NULL; 1092 char *p, *ps; 1093 int mode = PFCTL_IFLOOKUP_HOST; 1094 1095 if ((p = strrchr(s, ':')) != NULL && 1096 (!strcmp(p+1, "network") || !strcmp(p+1, "broadcast"))) { 1097 if (!strcmp(p+1, "network")) 1098 mode = PFCTL_IFLOOKUP_NET; 1099 if (!strcmp(p+1, "broadcast")) 1100 mode = PFCTL_IFLOOKUP_BCAST; 1101 if (mask > -1) { 1102 fprintf(stderr, "network or broadcast lookup, but " 1103 "extra netmask given\n"); 1104 return (NULL); 1105 } 1106 if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL) 1107 err(1, "host: malloc"); 1108 strlcpy(ps, s, strlen(s) - strlen(p) + 1); 1109 } else 1110 if ((ps = strdup(s)) == NULL) 1111 err(1, "host_if: strdup"); 1112 1113 if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) { 1114 /* interface with this name exists */ 1115 h = ifa_lookup(ps, mode); 1116 for (n = h; n != NULL && mask > -1; n = n->next) 1117 set_ipmask(n, mask); 1118 } 1119 1120 free(ps); 1121 return (h); 1122 } 1123 1124 struct node_host * 1125 host_v4(const char *s, int mask) 1126 { 1127 struct node_host *h = NULL; 1128 struct in_addr ina; 1129 int bits; 1130 1131 memset(&ina, 0, sizeof(struct in_addr)); 1132 if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) > -1) { 1133 h = calloc(1, sizeof(struct node_host)); 1134 if (h == NULL) 1135 err(1, "address: calloc"); 1136 h->ifname = NULL; 1137 h->af = AF_INET; 1138 h->addr.v.a.addr.addr32[0] = ina.s_addr; 1139 set_ipmask(h, bits); 1140 h->next = NULL; 1141 h->tail = h; 1142 } 1143 1144 return (h); 1145 } 1146 1147 struct node_host * 1148 host_v6(const char *s, int mask) 1149 { 1150 struct addrinfo hints, *res; 1151 struct node_host *h = NULL; 1152 1153 memset(&hints, 0, sizeof(hints)); 1154 hints.ai_family = AF_INET6; 1155 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1156 hints.ai_flags = AI_NUMERICHOST; 1157 if (getaddrinfo(s, "0", &hints, &res) == 0) { 1158 h = calloc(1, sizeof(struct node_host)); 1159 if (h == NULL) 1160 err(1, "address: calloc"); 1161 h->ifname = NULL; 1162 h->af = AF_INET6; 1163 memcpy(&h->addr.v.a.addr, 1164 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 1165 sizeof(h->addr.v.a.addr)); 1166 h->ifindex = 1167 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 1168 set_ipmask(h, mask); 1169 freeaddrinfo(res); 1170 h->next = NULL; 1171 h->tail = h; 1172 } 1173 1174 return (h); 1175 } 1176 1177 struct node_host * 1178 host_dns(const char *s, int v4mask, int v6mask) 1179 { 1180 struct addrinfo hints, *res0, *res; 1181 struct node_host *n, *h = NULL; 1182 int error; 1183 1184 memset(&hints, 0, sizeof(hints)); 1185 hints.ai_family = PF_UNSPEC; 1186 hints.ai_socktype = SOCK_STREAM; /* DUMMY */ 1187 error = getaddrinfo(s, NULL, &hints, &res0); 1188 if (error) 1189 return (h); 1190 1191 for (res = res0; res; res = res->ai_next) { 1192 if (res->ai_family != AF_INET && 1193 res->ai_family != AF_INET6) 1194 continue; 1195 n = calloc(1, sizeof(struct node_host)); 1196 if (n == NULL) 1197 err(1, "host_dns: calloc"); 1198 n->ifname = NULL; 1199 n->af = res->ai_family; 1200 if (res->ai_family == AF_INET) { 1201 memcpy(&n->addr.v.a.addr, 1202 &((struct sockaddr_in *) 1203 res->ai_addr)->sin_addr.s_addr, 1204 sizeof(struct in_addr)); 1205 set_ipmask(n, v4mask); 1206 } else { 1207 memcpy(&n->addr.v.a.addr, 1208 &((struct sockaddr_in6 *) 1209 res->ai_addr)->sin6_addr.s6_addr, 1210 sizeof(struct in6_addr)); 1211 n->ifindex = 1212 ((struct sockaddr_in6 *) 1213 res->ai_addr)->sin6_scope_id; 1214 set_ipmask(n, v6mask); 1215 } 1216 n->next = NULL; 1217 n->tail = n; 1218 if (h == NULL) 1219 h = n; 1220 else { 1221 h->tail->next = n; 1222 h->tail = n; 1223 } 1224 } 1225 freeaddrinfo(res0); 1226 1227 return (h); 1228 } 1229 1230 /* 1231 * convert a hostname to a list of addresses and put them in the given buffer. 1232 * test: 1233 * if set to 1, only simple addresses are accepted (no netblock, no "!"). 1234 */ 1235 int 1236 append_addr(struct pfr_buffer *b, char *s, int test) 1237 { 1238 char *r; 1239 struct node_host *h, *n; 1240 int rv, not = 0; 1241 1242 for (r = s; *r == '!'; r++) 1243 not = !not; 1244 if ((n = host(r)) == NULL) { 1245 errno = 0; 1246 return (-1); 1247 } 1248 rv = append_addr_host(b, n, test, not); 1249 do { 1250 h = n; 1251 n = n->next; 1252 free(h); 1253 } while (n != NULL); 1254 return (rv); 1255 } 1256 1257 /* 1258 * same as previous function, but with a pre-parsed input and the ability 1259 * to "negate" the result. Does not free the node_host list. 1260 * not: 1261 * setting it to 1 is equivalent to adding "!" in front of parameter s. 1262 */ 1263 int 1264 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not) 1265 { 1266 int bits; 1267 struct pfr_addr addr; 1268 1269 do { 1270 bzero(&addr, sizeof(addr)); 1271 addr.pfra_not = n->not ^ not; 1272 addr.pfra_af = n->af; 1273 addr.pfra_net = unmask(&n->addr.v.a.mask, n->af); 1274 switch (n->af) { 1275 case AF_INET: 1276 addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0]; 1277 bits = 32; 1278 break; 1279 case AF_INET6: 1280 memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6, 1281 sizeof(struct in6_addr)); 1282 bits = 128; 1283 break; 1284 default: 1285 errno = EINVAL; 1286 return (-1); 1287 } 1288 if ((test && (not || addr.pfra_net != bits)) || 1289 addr.pfra_net > bits) { 1290 errno = EINVAL; 1291 return (-1); 1292 } 1293 if (pfr_buf_add(b, &addr)) 1294 return (-1); 1295 } while ((n = n->next) != NULL); 1296 1297 return (0); 1298 } 1299 1300 int 1301 pfctl_add_trans(struct pfr_buffer *buf, int rs_num, const char *anchor, 1302 const char *ruleset) 1303 { 1304 struct pfioc_trans_e trans; 1305 1306 bzero(&trans, sizeof(trans)); 1307 trans.rs_num = rs_num; 1308 if (strlcpy(trans.anchor, anchor, 1309 sizeof(trans.anchor)) >= sizeof(trans.anchor) || 1310 strlcpy(trans.ruleset, ruleset, 1311 sizeof(trans.ruleset)) >= sizeof(trans.ruleset)) 1312 errx(1, "pfctl_add_trans: strlcpy"); 1313 1314 return pfr_buf_add(buf, &trans); 1315 } 1316 1317 u_int32_t 1318 pfctl_get_ticket(struct pfr_buffer *buf, int rs_num, const char *anchor, 1319 const char *ruleset) 1320 { 1321 struct pfioc_trans_e *p; 1322 1323 PFRB_FOREACH(p, buf) 1324 if (rs_num == p->rs_num && !strcmp(anchor, p->anchor) && 1325 !strcmp(ruleset, p->ruleset)) 1326 return (p->ticket); 1327 errx(1, "pfr_get_ticket: assertion failed"); 1328 } 1329 1330 int 1331 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from) 1332 { 1333 struct pfioc_trans trans; 1334 1335 bzero(&trans, sizeof(trans)); 1336 trans.size = buf->pfrb_size - from; 1337 trans.esize = sizeof(struct pfioc_trans_e); 1338 trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from; 1339 return ioctl(dev, cmd, &trans); 1340 } 1341