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