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