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