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