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