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 copy_satopfaddr(struct pf_addr *, struct sockaddr *);
70 void print_op (u_int8_t, const char *, const char *);
71 void print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int);
72 void print_ugid (u_int8_t, unsigned, unsigned, const char *, unsigned);
73 void print_flags (uint16_t);
74 void print_fromto(struct pf_rule_addr *, pf_osfp_t,
75 struct pf_rule_addr *, sa_family_t, u_int8_t, int, int);
76 int ifa_skip_if(const char *filter, struct node_host *p);
77
78 struct node_host *host_if(const char *, int);
79 struct node_host *host_ip(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 void
copy_satopfaddr(struct pf_addr * pfa,struct sockaddr * sa)233 copy_satopfaddr(struct pf_addr *pfa, struct sockaddr *sa)
234 {
235 if (sa->sa_family == AF_INET6)
236 pfa->v6 = ((struct sockaddr_in6 *)sa)->sin6_addr;
237 else if (sa->sa_family == AF_INET)
238 pfa->v4 = ((struct sockaddr_in *)sa)->sin_addr;
239 else
240 warnx("unhandled af %d", sa->sa_family);
241 }
242
243 const struct icmptypeent *
geticmptypebynumber(u_int8_t type,sa_family_t af)244 geticmptypebynumber(u_int8_t type, sa_family_t af)
245 {
246 unsigned int i;
247
248 if (af != AF_INET6) {
249 for (i=0; i < nitems(icmp_type); i++) {
250 if (type == icmp_type[i].type)
251 return (&icmp_type[i]);
252 }
253 } else {
254 for (i=0; i < nitems(icmp6_type); i++) {
255 if (type == icmp6_type[i].type)
256 return (&icmp6_type[i]);
257 }
258 }
259 return (NULL);
260 }
261
262 const struct icmptypeent *
geticmptypebyname(char * w,sa_family_t af)263 geticmptypebyname(char *w, sa_family_t af)
264 {
265 unsigned int i;
266
267 if (af != AF_INET6) {
268 for (i=0; i < nitems(icmp_type); i++) {
269 if (!strcmp(w, icmp_type[i].name))
270 return (&icmp_type[i]);
271 }
272 } else {
273 for (i=0; i < nitems(icmp6_type); i++) {
274 if (!strcmp(w, icmp6_type[i].name))
275 return (&icmp6_type[i]);
276 }
277 }
278 return (NULL);
279 }
280
281 const struct icmpcodeent *
geticmpcodebynumber(u_int8_t type,u_int8_t code,sa_family_t af)282 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af)
283 {
284 unsigned int i;
285
286 if (af != AF_INET6) {
287 for (i=0; i < nitems(icmp_code); i++) {
288 if (type == icmp_code[i].type &&
289 code == icmp_code[i].code)
290 return (&icmp_code[i]);
291 }
292 } else {
293 for (i=0; i < nitems(icmp6_code); i++) {
294 if (type == icmp6_code[i].type &&
295 code == icmp6_code[i].code)
296 return (&icmp6_code[i]);
297 }
298 }
299 return (NULL);
300 }
301
302 const struct icmpcodeent *
geticmpcodebyname(u_long type,char * w,sa_family_t af)303 geticmpcodebyname(u_long type, char *w, sa_family_t af)
304 {
305 unsigned int i;
306
307 if (af != AF_INET6) {
308 for (i=0; i < nitems(icmp_code); i++) {
309 if (type == icmp_code[i].type &&
310 !strcmp(w, icmp_code[i].name))
311 return (&icmp_code[i]);
312 }
313 } else {
314 for (i=0; i < nitems(icmp6_code); i++) {
315 if (type == icmp6_code[i].type &&
316 !strcmp(w, icmp6_code[i].name))
317 return (&icmp6_code[i]);
318 }
319 }
320 return (NULL);
321 }
322
323 void
print_op(u_int8_t op,const char * a1,const char * a2)324 print_op(u_int8_t op, const char *a1, const char *a2)
325 {
326 if (op == PF_OP_IRG)
327 printf(" %s >< %s", a1, a2);
328 else if (op == PF_OP_XRG)
329 printf(" %s <> %s", a1, a2);
330 else if (op == PF_OP_EQ)
331 printf(" = %s", a1);
332 else if (op == PF_OP_NE)
333 printf(" != %s", a1);
334 else if (op == PF_OP_LT)
335 printf(" < %s", a1);
336 else if (op == PF_OP_LE)
337 printf(" <= %s", a1);
338 else if (op == PF_OP_GT)
339 printf(" > %s", a1);
340 else if (op == PF_OP_GE)
341 printf(" >= %s", a1);
342 else if (op == PF_OP_RRG)
343 printf(" %s:%s", a1, a2);
344 }
345
346 void
print_port(u_int8_t op,u_int16_t p1,u_int16_t p2,const char * proto,int numeric)347 print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, const char *proto, int numeric)
348 {
349 char a1[6], a2[6];
350 struct servent *s;
351
352 if (!numeric)
353 s = getservbyport(p1, proto);
354 else
355 s = NULL;
356 p1 = ntohs(p1);
357 p2 = ntohs(p2);
358 snprintf(a1, sizeof(a1), "%u", p1);
359 snprintf(a2, sizeof(a2), "%u", p2);
360 printf(" port");
361 if (s != NULL && (op == PF_OP_EQ || op == PF_OP_NE))
362 print_op(op, s->s_name, a2);
363 else
364 print_op(op, a1, a2);
365 }
366
367 void
print_ugid(u_int8_t op,unsigned u1,unsigned u2,const char * t,unsigned umax)368 print_ugid(u_int8_t op, unsigned u1, unsigned u2, const char *t, unsigned umax)
369 {
370 char a1[11], a2[11];
371
372 snprintf(a1, sizeof(a1), "%u", u1);
373 snprintf(a2, sizeof(a2), "%u", u2);
374 printf(" %s", t);
375 if (u1 == umax && (op == PF_OP_EQ || op == PF_OP_NE))
376 print_op(op, "unknown", a2);
377 else
378 print_op(op, a1, a2);
379 }
380
381 void
print_flags(uint16_t f)382 print_flags(uint16_t f)
383 {
384 int i;
385
386 for (i = 0; tcpflags[i]; ++i)
387 if (f & (1 << i))
388 printf("%c", tcpflags[i]);
389 }
390
391 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)392 print_fromto(struct pf_rule_addr *src, pf_osfp_t osfp, struct pf_rule_addr *dst,
393 sa_family_t af, u_int8_t proto, int verbose, int numeric)
394 {
395 char buf[PF_OSFP_LEN*3];
396 if (src->addr.type == PF_ADDR_ADDRMASK &&
397 dst->addr.type == PF_ADDR_ADDRMASK &&
398 PF_AZERO(&src->addr.v.a.addr, AF_INET6) &&
399 PF_AZERO(&src->addr.v.a.mask, AF_INET6) &&
400 PF_AZERO(&dst->addr.v.a.addr, AF_INET6) &&
401 PF_AZERO(&dst->addr.v.a.mask, AF_INET6) &&
402 !src->neg && !dst->neg &&
403 !src->port_op && !dst->port_op &&
404 osfp == PF_OSFP_ANY)
405 printf(" all");
406 else {
407 printf(" from ");
408 if (src->neg)
409 printf("! ");
410 print_addr(&src->addr, af, verbose);
411 if (src->port_op)
412 print_port(src->port_op, src->port[0],
413 src->port[1],
414 proto == IPPROTO_TCP ? "tcp" : "udp",
415 numeric);
416 if (osfp != PF_OSFP_ANY)
417 printf(" os \"%s\"", pfctl_lookup_fingerprint(osfp, buf,
418 sizeof(buf)));
419
420 printf(" to ");
421 if (dst->neg)
422 printf("! ");
423 print_addr(&dst->addr, af, verbose);
424 if (dst->port_op)
425 print_port(dst->port_op, dst->port[0],
426 dst->port[1],
427 proto == IPPROTO_TCP ? "tcp" : "udp",
428 numeric);
429 }
430 }
431
432 void
print_pool(struct pfctl_pool * pool,u_int16_t p1,u_int16_t p2,sa_family_t af,int id)433 print_pool(struct pfctl_pool *pool, u_int16_t p1, u_int16_t p2,
434 sa_family_t af, int id)
435 {
436 struct pf_pooladdr *pooladdr;
437
438 if ((TAILQ_FIRST(&pool->list) != NULL) &&
439 TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL)
440 printf("{ ");
441 TAILQ_FOREACH(pooladdr, &pool->list, entries){
442 switch (id) {
443 case PF_NAT:
444 case PF_RDR:
445 case PF_BINAT:
446 print_addr(&pooladdr->addr, af, 0);
447 break;
448 case PF_PASS:
449 case PF_MATCH:
450 if (PF_AZERO(&pooladdr->addr.v.a.addr, af))
451 printf("%s", pooladdr->ifname);
452 else {
453 printf("(%s ", pooladdr->ifname);
454 print_addr(&pooladdr->addr, af, 0);
455 printf(")");
456 }
457 break;
458 default:
459 break;
460 }
461 if (TAILQ_NEXT(pooladdr, entries) != NULL)
462 printf(", ");
463 else if (TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL)
464 printf(" }");
465 }
466 switch (id) {
467 case PF_NAT:
468 if ((p1 != PF_NAT_PROXY_PORT_LOW ||
469 p2 != PF_NAT_PROXY_PORT_HIGH) && (p1 != 0 || p2 != 0)) {
470 if (p1 == p2)
471 printf(" port %u", p1);
472 else
473 printf(" port %u:%u", p1, p2);
474 }
475 break;
476 case PF_RDR:
477 if (p1) {
478 printf(" port %u", p1);
479 if (p2 && (p2 != p1))
480 printf(":%u", p2);
481 }
482 break;
483 default:
484 break;
485 }
486 switch (pool->opts & PF_POOL_TYPEMASK) {
487 case PF_POOL_NONE:
488 break;
489 case PF_POOL_BITMASK:
490 printf(" bitmask");
491 break;
492 case PF_POOL_RANDOM:
493 printf(" random");
494 break;
495 case PF_POOL_SRCHASH:
496 printf(" source-hash 0x%08x%08x%08x%08x",
497 pool->key.key32[0], pool->key.key32[1],
498 pool->key.key32[2], pool->key.key32[3]);
499 break;
500 case PF_POOL_ROUNDROBIN:
501 printf(" round-robin");
502 break;
503 }
504 if (pool->opts & PF_POOL_STICKYADDR)
505 printf(" sticky-address");
506 if (pool->opts & PF_POOL_ENDPI)
507 printf(" endpoint-independent");
508 if (id == PF_NAT && p1 == 0 && p2 == 0)
509 printf(" static-port");
510 if (pool->mape.offset > 0)
511 printf(" map-e-portset %u/%u/%u",
512 pool->mape.offset, pool->mape.psidlen, pool->mape.psid);
513 }
514
515 void
print_status(struct pfctl_status * s,struct pfctl_syncookies * cookies,int opts)516 print_status(struct pfctl_status *s, struct pfctl_syncookies *cookies, int opts)
517 {
518 struct pfctl_status_counter *c;
519 char statline[80], *running;
520 time_t runtime;
521 int i;
522 char buf[PF_MD5_DIGEST_LENGTH * 2 + 1];
523 static const char hex[] = "0123456789abcdef";
524
525 runtime = time(NULL) - s->since;
526 running = s->running ? "Enabled" : "Disabled";
527
528 if (s->since) {
529 unsigned int sec, min, hrs;
530 time_t day = runtime;
531
532 sec = day % 60;
533 day /= 60;
534 min = day % 60;
535 day /= 60;
536 hrs = day % 24;
537 day /= 24;
538 snprintf(statline, sizeof(statline),
539 "Status: %s for %lld days %.2u:%.2u:%.2u",
540 running, (long long)day, hrs, min, sec);
541 } else
542 snprintf(statline, sizeof(statline), "Status: %s", running);
543 printf("%-44s", statline);
544 switch (s->debug) {
545 case PF_DEBUG_NONE:
546 printf("%15s\n\n", "Debug: None");
547 break;
548 case PF_DEBUG_URGENT:
549 printf("%15s\n\n", "Debug: Urgent");
550 break;
551 case PF_DEBUG_MISC:
552 printf("%15s\n\n", "Debug: Misc");
553 break;
554 case PF_DEBUG_NOISY:
555 printf("%15s\n\n", "Debug: Loud");
556 break;
557 }
558
559 if (opts & PF_OPT_VERBOSE) {
560 printf("Hostid: 0x%08x\n", s->hostid);
561
562 for (i = 0; i < PF_MD5_DIGEST_LENGTH; i++) {
563 buf[i + i] = hex[s->pf_chksum[i] >> 4];
564 buf[i + i + 1] = hex[s->pf_chksum[i] & 0x0f];
565 }
566 buf[i + i] = '\0';
567 printf("Checksum: 0x%s\n\n", buf);
568 }
569
570 if (s->ifname[0] != 0) {
571 printf("Interface Stats for %-16s %5s %16s\n",
572 s->ifname, "IPv4", "IPv6");
573 printf(" %-25s %14llu %16llu\n", "Bytes In",
574 (unsigned long long)s->bcounters[0][0],
575 (unsigned long long)s->bcounters[1][0]);
576 printf(" %-25s %14llu %16llu\n", "Bytes Out",
577 (unsigned long long)s->bcounters[0][1],
578 (unsigned long long)s->bcounters[1][1]);
579 printf(" Packets In\n");
580 printf(" %-23s %14llu %16llu\n", "Passed",
581 (unsigned long long)s->pcounters[0][0][PF_PASS],
582 (unsigned long long)s->pcounters[1][0][PF_PASS]);
583 printf(" %-23s %14llu %16llu\n", "Blocked",
584 (unsigned long long)s->pcounters[0][0][PF_DROP],
585 (unsigned long long)s->pcounters[1][0][PF_DROP]);
586 printf(" Packets Out\n");
587 printf(" %-23s %14llu %16llu\n", "Passed",
588 (unsigned long long)s->pcounters[0][1][PF_PASS],
589 (unsigned long long)s->pcounters[1][1][PF_PASS]);
590 printf(" %-23s %14llu %16llu\n\n", "Blocked",
591 (unsigned long long)s->pcounters[0][1][PF_DROP],
592 (unsigned long long)s->pcounters[1][1][PF_DROP]);
593 }
594 printf("%-27s %14s %16s\n", "State Table", "Total", "Rate");
595 printf(" %-25s %14ju %14s\n", "current entries", s->states, "");
596 TAILQ_FOREACH(c, &s->fcounters, entry) {
597 printf(" %-25s %14ju ", c->name, c->counter);
598 if (runtime > 0)
599 printf("%14.1f/s\n",
600 (double)c->counter / (double)runtime);
601 else
602 printf("%14s\n", "");
603 }
604 if (opts & PF_OPT_VERBOSE) {
605 printf("Source Tracking Table\n");
606 printf(" %-25s %14ju %14s\n", "current entries",
607 s->src_nodes, "");
608 TAILQ_FOREACH(c, &s->scounters, entry) {
609 printf(" %-25s %14ju ", c->name, c->counter);
610 if (runtime > 0)
611 printf("%14.1f/s\n",
612 (double)c->counter / (double)runtime);
613 else
614 printf("%14s\n", "");
615 }
616 }
617 printf("Counters\n");
618 TAILQ_FOREACH(c, &s->counters, entry) {
619 printf(" %-25s %14ju ", c->name, c->counter);
620 if (runtime > 0)
621 printf("%14.1f/s\n",
622 (double)c->counter / (double)runtime);
623 else
624 printf("%14s\n", "");
625 }
626 if (opts & PF_OPT_VERBOSE) {
627 printf("Limit Counters\n");
628 TAILQ_FOREACH(c, &s->lcounters, entry) {
629 printf(" %-25s %14ju ", c->name, c->counter);
630 if (runtime > 0)
631 printf("%14.1f/s\n",
632 (double)c->counter / (double)runtime);
633 else
634 printf("%14s\n", "");
635 }
636
637 printf("Syncookies\n");
638 assert(cookies->mode <= PFCTL_SYNCOOKIES_ADAPTIVE);
639 printf(" %-25s %s\n", "mode",
640 PFCTL_SYNCOOKIES_MODE_NAMES[cookies->mode]);
641 printf(" %-25s %s\n", "active",
642 s->syncookies_active ? "active" : "inactive");
643 if (opts & PF_OPT_VERBOSE2) {
644 printf(" %-25s %d %%\n", "highwater", cookies->highwater);
645 printf(" %-25s %d %%\n", "lowwater", cookies->lowwater);
646 printf(" %-25s %d\n", "halfopen states", cookies->halfopen_states);
647 }
648 printf("Reassemble %24s %s\n",
649 s->reass & PF_REASS_ENABLED ? "yes" : "no",
650 s->reass & PF_REASS_NODF ? "no-df" : ""
651 );
652 }
653 }
654
655 void
print_running(struct pfctl_status * status)656 print_running(struct pfctl_status *status)
657 {
658 printf("%s\n", status->running ? "Enabled" : "Disabled");
659 }
660
661 void
print_src_node(struct pfctl_src_node * sn,int opts)662 print_src_node(struct pfctl_src_node *sn, int opts)
663 {
664 struct pf_addr_wrap aw;
665 uint64_t min, sec;
666 const char *sn_type_names[] = PF_SN_TYPE_NAMES;
667
668 memset(&aw, 0, sizeof(aw));
669 if (sn->af == AF_INET)
670 aw.v.a.mask.addr32[0] = 0xffffffff;
671 else
672 memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
673
674 aw.v.a.addr = sn->addr;
675 print_addr(&aw, sn->af, opts & PF_OPT_VERBOSE2);
676 printf(" -> ");
677 aw.v.a.addr = sn->raddr;
678 print_addr(&aw, sn->naf ? sn->naf : sn->af, opts & PF_OPT_VERBOSE2);
679 printf(" ( states %u, connections %u, rate %u.%u/%us )\n", sn->states,
680 sn->conn, sn->conn_rate.count / 1000,
681 (sn->conn_rate.count % 1000) / 100, sn->conn_rate.seconds);
682 if (opts & PF_OPT_VERBOSE) {
683 sec = sn->creation % 60;
684 sn->creation /= 60;
685 min = sn->creation % 60;
686 sn->creation /= 60;
687 printf(" age %.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64,
688 sn->creation, min, sec);
689 if (sn->states == 0) {
690 sec = sn->expire % 60;
691 sn->expire /= 60;
692 min = sn->expire % 60;
693 sn->expire /= 60;
694 printf(", expires in %.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64,
695 sn->expire, min, sec);
696 }
697 printf(", %" PRIu64 " pkts, %" PRIu64 " bytes",
698 sn->packets[0] + sn->packets[1],
699 sn->bytes[0] + sn->bytes[1]);
700 switch (sn->ruletype) {
701 case PF_NAT:
702 if (sn->rule != -1)
703 printf(", nat rule %u", sn->rule);
704 break;
705 case PF_RDR:
706 if (sn->rule != -1)
707 printf(", rdr rule %u", sn->rule);
708 break;
709 case PF_PASS:
710 case PF_MATCH:
711 if (sn->rule != -1)
712 printf(", filter rule %u", sn->rule);
713 break;
714 }
715 printf(", %s", sn_type_names[sn->type]);
716 printf("\n");
717 }
718 }
719
720 static void
print_eth_addr(const struct pfctl_eth_addr * a)721 print_eth_addr(const struct pfctl_eth_addr *a)
722 {
723 int i, masklen = ETHER_ADDR_LEN * 8;
724 bool seen_unset = false;
725
726 for (i = 0; i < ETHER_ADDR_LEN; i++) {
727 if (a->addr[i] != 0)
728 break;
729 }
730
731 /* Unset, so don't print anything. */
732 if (i == ETHER_ADDR_LEN)
733 return;
734
735 printf("%s%02x:%02x:%02x:%02x:%02x:%02x", a->neg ? "! " : "",
736 a->addr[0], a->addr[1], a->addr[2], a->addr[3], a->addr[4],
737 a->addr[5]);
738
739 for (i = 0; i < (ETHER_ADDR_LEN * 8); i++) {
740 bool isset = a->mask[i / 8] & (1 << i % 8);
741
742 if (! seen_unset) {
743 if (isset)
744 continue;
745 seen_unset = true;
746 masklen = i;
747 } else {
748 /* Not actually a continuous mask, so print the whole
749 * thing. */
750 if (isset)
751 break;
752 continue;
753 }
754 }
755
756 if (masklen == (ETHER_ADDR_LEN * 8))
757 return;
758
759 if (i == (ETHER_ADDR_LEN * 8)) {
760 printf("/%d", masklen);
761 return;
762 }
763
764 printf("&%02x:%02x:%02x:%02x:%02x:%02x",
765 a->mask[0], a->mask[1], a->mask[2], a->mask[3], a->mask[4],
766 a->mask[5]);
767 }
768
769 void
print_eth_rule(struct pfctl_eth_rule * r,const char * anchor_call,int rule_numbers)770 print_eth_rule(struct pfctl_eth_rule *r, const char *anchor_call,
771 int rule_numbers)
772 {
773 static const char *actiontypes[] = { "pass", "block", "", "", "", "",
774 "", "", "", "", "", "", "match" };
775
776 int i;
777
778 if (rule_numbers)
779 printf("@%u ", r->nr);
780
781 printf("ether ");
782 if (anchor_call[0]) {
783 if (anchor_call[0] == '_') {
784 printf("anchor");
785 } else
786 printf("anchor \"%s\"", anchor_call);
787 } else {
788 printf("%s", actiontypes[r->action]);
789 }
790 if (r->direction == PF_IN)
791 printf(" in");
792 else if (r->direction == PF_OUT)
793 printf(" out");
794
795 if (r->quick)
796 printf(" quick");
797 if (r->ifname[0]) {
798 if (r->ifnot)
799 printf(" on ! %s", r->ifname);
800 else
801 printf(" on %s", r->ifname);
802 }
803 if (r->bridge_to[0])
804 printf(" bridge-to %s", r->bridge_to);
805 if (r->proto)
806 printf(" proto 0x%04x", r->proto);
807
808 if (r->src.isset) {
809 printf(" from ");
810 print_eth_addr(&r->src);
811 }
812 if (r->dst.isset) {
813 printf(" to ");
814 print_eth_addr(&r->dst);
815 }
816 printf(" l3");
817 print_fromto(&r->ipsrc, PF_OSFP_ANY, &r->ipdst,
818 r->proto == ETHERTYPE_IP ? AF_INET : AF_INET6, 0,
819 0, 0);
820
821 i = 0;
822 while (r->label[i][0])
823 printf(" label \"%s\"", r->label[i++]);
824 if (r->ridentifier)
825 printf(" ridentifier %u", r->ridentifier);
826
827 if (r->qname[0])
828 printf(" queue %s", r->qname);
829 if (r->tagname[0])
830 printf(" tag %s", r->tagname);
831 if (r->match_tagname[0]) {
832 if (r->match_tag_not)
833 printf(" !");
834 printf(" tagged %s", r->match_tagname);
835 }
836 if (r->dnpipe)
837 printf(" %s %d",
838 r->dnflags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue",
839 r->dnpipe);
840 }
841
842 void
print_rule(struct pfctl_rule * r,const char * anchor_call,int verbose,int numeric)843 print_rule(struct pfctl_rule *r, const char *anchor_call, int verbose, int numeric)
844 {
845 static const char *actiontypes[] = { "pass", "block", "scrub",
846 "no scrub", "nat", "no nat", "binat", "no binat", "rdr", "no rdr",
847 "", "", "match"};
848 static const char *anchortypes[] = { "anchor", "anchor", "anchor",
849 "anchor", "nat-anchor", "nat-anchor", "binat-anchor",
850 "binat-anchor", "rdr-anchor", "rdr-anchor" };
851 int i, ropts;
852 char *p;
853
854 if (verbose)
855 printf("@%d ", r->nr);
856 if (r->action == PF_MATCH)
857 printf("match");
858 else if (r->action > PF_NORDR)
859 printf("action(%d)", r->action);
860 else if (anchor_call[0]) {
861 p = strrchr(anchor_call, '/');
862 if (p ? p[1] == '_' : anchor_call[0] == '_')
863 printf("%s", anchortypes[r->action]);
864 else
865 printf("%s \"%s\"", anchortypes[r->action],
866 anchor_call);
867 } else {
868 printf("%s", actiontypes[r->action]);
869 if (r->natpass)
870 printf(" pass");
871 }
872 if (r->action == PF_DROP) {
873 if (r->rule_flag & PFRULE_RETURN)
874 printf(" return");
875 else if (r->rule_flag & PFRULE_RETURNRST) {
876 if (!r->return_ttl)
877 printf(" return-rst");
878 else
879 printf(" return-rst(ttl %d)", r->return_ttl);
880 } else if (r->rule_flag & PFRULE_RETURNICMP) {
881 const struct icmpcodeent *ic, *ic6;
882
883 ic = geticmpcodebynumber(r->return_icmp >> 8,
884 r->return_icmp & 255, AF_INET);
885 ic6 = geticmpcodebynumber(r->return_icmp6 >> 8,
886 r->return_icmp6 & 255, AF_INET6);
887
888 switch (r->af) {
889 case AF_INET:
890 printf(" return-icmp");
891 if (ic == NULL)
892 printf("(%u)", r->return_icmp & 255);
893 else
894 printf("(%s)", ic->name);
895 break;
896 case AF_INET6:
897 printf(" return-icmp6");
898 if (ic6 == NULL)
899 printf("(%u)", r->return_icmp6 & 255);
900 else
901 printf("(%s)", ic6->name);
902 break;
903 default:
904 printf(" return-icmp");
905 if (ic == NULL)
906 printf("(%u, ", r->return_icmp & 255);
907 else
908 printf("(%s, ", ic->name);
909 if (ic6 == NULL)
910 printf("%u)", r->return_icmp6 & 255);
911 else
912 printf("%s)", ic6->name);
913 break;
914 }
915 } else
916 printf(" drop");
917 }
918 if (r->direction == PF_IN)
919 printf(" in");
920 else if (r->direction == PF_OUT)
921 printf(" out");
922 if (r->log) {
923 printf(" log");
924 if (r->log & ~PF_LOG || r->logif) {
925 int count = 0;
926
927 printf(" (");
928 if (r->log & PF_LOG_ALL)
929 printf("%sall", count++ ? ", " : "");
930 if (r->log & PF_LOG_MATCHES)
931 printf("%smatches", count++ ? ", " : "");
932 if (r->log & PF_LOG_SOCKET_LOOKUP)
933 printf("%suser", count++ ? ", " : "");
934 if (r->logif)
935 printf("%sto pflog%u", count++ ? ", " : "",
936 r->logif);
937 printf(")");
938 }
939 }
940 if (r->quick)
941 printf(" quick");
942 if (r->ifname[0]) {
943 if (r->ifnot)
944 printf(" on ! %s", r->ifname);
945 else
946 printf(" on %s", r->ifname);
947 }
948 if (r->rt) {
949 if (r->rt == PF_ROUTETO)
950 printf(" route-to");
951 else if (r->rt == PF_REPLYTO)
952 printf(" reply-to");
953 else if (r->rt == PF_DUPTO)
954 printf(" dup-to");
955 printf(" ");
956 print_pool(&r->rdr, 0, 0, r->af, PF_PASS);
957 print_pool(&r->route, 0, 0,
958 r->rule_flag & PFRULE_AFTO && r->rt != PF_REPLYTO ? r->naf : r->af,
959 PF_PASS);
960 }
961 if (r->af) {
962 if (r->af == AF_INET)
963 printf(" inet");
964 else
965 printf(" inet6");
966 }
967 if (r->proto) {
968 const char *protoname;
969
970 if ((protoname = pfctl_proto2name(r->proto)) != NULL)
971 printf(" proto %s", protoname);
972 else
973 printf(" proto %u", r->proto);
974 }
975 print_fromto(&r->src, r->os_fingerprint, &r->dst, r->af, r->proto,
976 verbose, numeric);
977 if (r->rcv_ifname[0])
978 printf(" %sreceived-on %s", r->rcvifnot ? "!" : "",
979 r->rcv_ifname);
980 if (r->uid.op)
981 print_ugid(r->uid.op, r->uid.uid[0], r->uid.uid[1], "user",
982 UID_MAX);
983 if (r->gid.op)
984 print_ugid(r->gid.op, r->gid.gid[0], r->gid.gid[1], "group",
985 GID_MAX);
986 if (r->flags || r->flagset) {
987 printf(" flags ");
988 print_flags(r->flags);
989 printf("/");
990 print_flags(r->flagset);
991 } else if ((r->action == PF_PASS || r->action == PF_MATCH) &&
992 (!r->proto || r->proto == IPPROTO_TCP) &&
993 !(r->rule_flag & PFRULE_FRAGMENT) &&
994 !anchor_call[0] && r->keep_state)
995 printf(" flags any");
996 if (r->type) {
997 const struct icmptypeent *it;
998
999 it = geticmptypebynumber(r->type-1, r->af);
1000 if (r->af != AF_INET6)
1001 printf(" icmp-type");
1002 else
1003 printf(" icmp6-type");
1004 if (it != NULL)
1005 printf(" %s", it->name);
1006 else
1007 printf(" %u", r->type-1);
1008 if (r->code) {
1009 const struct icmpcodeent *ic;
1010
1011 ic = geticmpcodebynumber(r->type-1, r->code-1, r->af);
1012 if (ic != NULL)
1013 printf(" code %s", ic->name);
1014 else
1015 printf(" code %u", r->code-1);
1016 }
1017 }
1018 if (r->tos)
1019 printf(" tos 0x%2.2x", r->tos);
1020 if (r->prio)
1021 printf(" prio %u", r->prio == PF_PRIO_ZERO ? 0 : r->prio);
1022 if (r->pktrate.limit)
1023 printf(" max-pkt-rate %u/%u", r->pktrate.limit,
1024 r->pktrate.seconds);
1025 if (r->max_pkt_size)
1026 printf( " max-pkt-size %u", r->max_pkt_size);
1027 if (r->scrub_flags & PFSTATE_SETMASK) {
1028 char *comma = "";
1029 printf(" set (");
1030 if (r->scrub_flags & PFSTATE_SETPRIO) {
1031 if (r->set_prio[0] == r->set_prio[1])
1032 printf("%s prio %u", comma, r->set_prio[0]);
1033 else
1034 printf("%s prio(%u, %u)", comma, r->set_prio[0],
1035 r->set_prio[1]);
1036 comma = ",";
1037 }
1038 if (r->scrub_flags & PFSTATE_SETTOS) {
1039 printf("%s tos 0x%2.2x", comma, r->set_tos);
1040 comma = ",";
1041 }
1042 printf(" )");
1043 }
1044 if (!r->keep_state && r->action == PF_PASS && !anchor_call[0])
1045 printf(" no state");
1046 else if (r->keep_state == PF_STATE_NORMAL)
1047 printf(" keep state");
1048 else if (r->keep_state == PF_STATE_MODULATE)
1049 printf(" modulate state");
1050 else if (r->keep_state == PF_STATE_SYNPROXY)
1051 printf(" synproxy state");
1052 if (r->prob) {
1053 char buf[20];
1054
1055 snprintf(buf, sizeof(buf), "%f", r->prob*100.0/(UINT_MAX+1.0));
1056 for (i = strlen(buf)-1; i > 0; i--) {
1057 if (buf[i] == '0')
1058 buf[i] = '\0';
1059 else {
1060 if (buf[i] == '.')
1061 buf[i] = '\0';
1062 break;
1063 }
1064 }
1065 printf(" probability %s%%", buf);
1066 }
1067 ropts = 0;
1068 if (r->max_states || r->max_src_nodes || r->max_src_states)
1069 ropts = 1;
1070 if (r->rule_flag & PFRULE_NOSYNC)
1071 ropts = 1;
1072 if (r->rule_flag & PFRULE_SRCTRACK)
1073 ropts = 1;
1074 if (r->rule_flag & PFRULE_IFBOUND)
1075 ropts = 1;
1076 if (r->rule_flag & PFRULE_STATESLOPPY)
1077 ropts = 1;
1078 if (r->rule_flag & PFRULE_PFLOW)
1079 ropts = 1;
1080 for (i = 0; !ropts && i < PFTM_MAX; ++i)
1081 if (r->timeout[i])
1082 ropts = 1;
1083 if (ropts) {
1084 printf(" (");
1085 if (r->max_states) {
1086 printf("max %u", r->max_states);
1087 ropts = 0;
1088 }
1089 if (r->rule_flag & PFRULE_NOSYNC) {
1090 if (!ropts)
1091 printf(", ");
1092 printf("no-sync");
1093 ropts = 0;
1094 }
1095 if (r->rule_flag & PFRULE_SRCTRACK) {
1096 if (!ropts)
1097 printf(", ");
1098 printf("source-track");
1099 if (r->rule_flag & PFRULE_RULESRCTRACK)
1100 printf(" rule");
1101 else
1102 printf(" global");
1103 ropts = 0;
1104 }
1105 if (r->max_src_states) {
1106 if (!ropts)
1107 printf(", ");
1108 printf("max-src-states %u", r->max_src_states);
1109 ropts = 0;
1110 }
1111 if (r->max_src_conn) {
1112 if (!ropts)
1113 printf(", ");
1114 printf("max-src-conn %u", r->max_src_conn);
1115 ropts = 0;
1116 }
1117 if (r->max_src_conn_rate.limit) {
1118 if (!ropts)
1119 printf(", ");
1120 printf("max-src-conn-rate %u/%u",
1121 r->max_src_conn_rate.limit,
1122 r->max_src_conn_rate.seconds);
1123 ropts = 0;
1124 }
1125 if (r->max_src_nodes) {
1126 if (!ropts)
1127 printf(", ");
1128 printf("max-src-nodes %u", r->max_src_nodes);
1129 ropts = 0;
1130 }
1131 if (r->overload_tblname[0]) {
1132 if (!ropts)
1133 printf(", ");
1134 printf("overload <%s>", r->overload_tblname);
1135 if (r->flush)
1136 printf(" flush");
1137 if (r->flush & PF_FLUSH_GLOBAL)
1138 printf(" global");
1139 }
1140 if (r->rule_flag & PFRULE_IFBOUND) {
1141 if (!ropts)
1142 printf(", ");
1143 printf("if-bound");
1144 ropts = 0;
1145 }
1146 if (r->rule_flag & PFRULE_STATESLOPPY) {
1147 if (!ropts)
1148 printf(", ");
1149 printf("sloppy");
1150 ropts = 0;
1151 }
1152 if (r->rule_flag & PFRULE_PFLOW) {
1153 if (!ropts)
1154 printf(", ");
1155 printf("pflow");
1156 ropts = 0;
1157 }
1158 for (i = 0; i < PFTM_MAX; ++i)
1159 if (r->timeout[i]) {
1160 int j;
1161
1162 if (!ropts)
1163 printf(", ");
1164 ropts = 0;
1165 for (j = 0; pf_timeouts[j].name != NULL;
1166 ++j)
1167 if (pf_timeouts[j].timeout == i)
1168 break;
1169 printf("%s %u", pf_timeouts[j].name == NULL ?
1170 "inv.timeout" : pf_timeouts[j].name,
1171 r->timeout[i]);
1172 }
1173 printf(")");
1174 }
1175 if (r->allow_opts)
1176 printf(" allow-opts");
1177 if (r->rule_flag & PFRULE_FRAGMENT)
1178 printf(" fragment");
1179 if (r->action == PF_SCRUB) {
1180 /* Scrub flags for old-style scrub. */
1181 if (r->rule_flag & PFRULE_NODF)
1182 printf(" no-df");
1183 if (r->rule_flag & PFRULE_RANDOMID)
1184 printf(" random-id");
1185 if (r->min_ttl)
1186 printf(" min-ttl %d", r->min_ttl);
1187 if (r->max_mss)
1188 printf(" max-mss %d", r->max_mss);
1189 if (r->rule_flag & PFRULE_SET_TOS)
1190 printf(" set-tos 0x%2.2x", r->set_tos);
1191 if (r->rule_flag & PFRULE_REASSEMBLE_TCP)
1192 printf(" reassemble tcp");
1193 /* The PFRULE_FRAGMENT_NOREASS is set on all rules by default! */
1194 printf(" fragment %sreassemble",
1195 r->rule_flag & PFRULE_FRAGMENT_NOREASS ? "no " : "");
1196 } else if (r->scrub_flags & PFSTATE_SCRUBMASK || r->min_ttl || r->max_mss) {
1197 /* Scrub actions on normal rules. */
1198 printf(" scrub(");
1199 if (r->scrub_flags & PFSTATE_NODF)
1200 printf(" no-df");
1201 if (r->scrub_flags & PFSTATE_RANDOMID)
1202 printf(" random-id");
1203 if (r->min_ttl)
1204 printf(" min-ttl %d", r->min_ttl);
1205 if (r->scrub_flags & PFSTATE_SETTOS)
1206 printf(" set-tos 0x%2.2x", r->set_tos);
1207 if (r->scrub_flags & PFSTATE_SCRUB_TCP)
1208 printf(" reassemble tcp");
1209 if (r->max_mss)
1210 printf(" max-mss %d", r->max_mss);
1211 printf(")");
1212 }
1213 i = 0;
1214 while (r->label[i][0])
1215 printf(" label \"%s\"", r->label[i++]);
1216 if (r->ridentifier)
1217 printf(" ridentifier %u", r->ridentifier);
1218 /* Only dnrpipe as we might do (0, 42) to only queue return traffic. */
1219 if (r->dnrpipe)
1220 printf(" %s(%d, %d)",
1221 r->free_flags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue",
1222 r->dnpipe, r->dnrpipe);
1223 else if (r->dnpipe)
1224 printf(" %s %d",
1225 r->free_flags & PFRULE_DN_IS_PIPE ? "dnpipe" : "dnqueue",
1226 r->dnpipe);
1227 if (r->qname[0] && r->pqname[0])
1228 printf(" queue(%s, %s)", r->qname, r->pqname);
1229 else if (r->qname[0])
1230 printf(" queue %s", r->qname);
1231 if (r->tagname[0])
1232 printf(" tag %s", r->tagname);
1233 if (r->match_tagname[0]) {
1234 if (r->match_tag_not)
1235 printf(" !");
1236 printf(" tagged %s", r->match_tagname);
1237 }
1238 if (r->rtableid != -1)
1239 printf(" rtable %u", r->rtableid);
1240 if (r->divert.port) {
1241 #ifdef __FreeBSD__
1242 printf(" divert-to %u", ntohs(r->divert.port));
1243 #else
1244 if (PF_AZERO(&r->divert.addr, r->af)) {
1245 printf(" divert-reply");
1246 } else {
1247 printf(" divert-to ");
1248 print_addr_str(r->af, &r->divert.addr);
1249 printf(" port %u", ntohs(r->divert.port));
1250 }
1251 #endif
1252 }
1253 if (anchor_call[0])
1254 return;
1255 if (r->action == PF_NAT || r->action == PF_BINAT || r->action == PF_RDR) {
1256 printf(" -> ");
1257 print_pool(&r->rdr, r->rdr.proxy_port[0],
1258 r->rdr.proxy_port[1], r->af, r->action);
1259 } else {
1260 if (!TAILQ_EMPTY(&r->nat.list)) {
1261 if (r->rule_flag & PFRULE_AFTO) {
1262 printf(" af-to %s from ", r->naf == AF_INET ? "inet" : "inet6");
1263 } else {
1264 printf(" nat-to ");
1265 }
1266 print_pool(&r->nat, r->nat.proxy_port[0],
1267 r->nat.proxy_port[1], r->naf ? r->naf : r->af,
1268 PF_NAT);
1269 }
1270 if (!TAILQ_EMPTY(&r->rdr.list)) {
1271 if (r->rule_flag & PFRULE_AFTO) {
1272 printf(" to ");
1273 } else {
1274 printf(" rdr-to ");
1275 }
1276 print_pool(&r->rdr, r->rdr.proxy_port[0],
1277 r->rdr.proxy_port[1], r->naf ? r->naf : r->af,
1278 PF_RDR);
1279 }
1280 }
1281 }
1282
1283 void
print_tabledef(const char * name,int flags,int addrs,struct node_tinithead * nodes)1284 print_tabledef(const char *name, int flags, int addrs,
1285 struct node_tinithead *nodes)
1286 {
1287 struct node_tinit *ti, *nti;
1288 struct node_host *h;
1289
1290 printf("table <%s>", name);
1291 if (flags & PFR_TFLAG_CONST)
1292 printf(" const");
1293 if (flags & PFR_TFLAG_PERSIST)
1294 printf(" persist");
1295 if (flags & PFR_TFLAG_COUNTERS)
1296 printf(" counters");
1297 SIMPLEQ_FOREACH(ti, nodes, entries) {
1298 if (ti->file) {
1299 printf(" file \"%s\"", ti->file);
1300 continue;
1301 }
1302 printf(" {");
1303 for (;;) {
1304 for (h = ti->host; h != NULL; h = h->next) {
1305 printf(h->not ? " !" : " ");
1306 print_addr(&h->addr, h->af, 0);
1307 }
1308 nti = SIMPLEQ_NEXT(ti, entries);
1309 if (nti != NULL && nti->file == NULL)
1310 ti = nti; /* merge lists */
1311 else
1312 break;
1313 }
1314 printf(" }");
1315 }
1316 if (addrs && SIMPLEQ_EMPTY(nodes))
1317 printf(" { }");
1318 printf("\n");
1319 }
1320
1321 int
parse_flags(char * s)1322 parse_flags(char *s)
1323 {
1324 char *p, *q;
1325 uint16_t f = 0;
1326
1327 for (p = s; *p; p++) {
1328 if ((q = strchr(tcpflags, *p)) == NULL)
1329 return -1;
1330 else
1331 f |= 1 << (q - tcpflags);
1332 }
1333 return (f ? f : TH_FLAGS);
1334 }
1335
1336 void
set_ipmask(struct node_host * h,int bb)1337 set_ipmask(struct node_host *h, int bb)
1338 {
1339 struct pf_addr *m, *n;
1340 int i, j = 0;
1341 uint8_t b;
1342
1343 m = &h->addr.v.a.mask;
1344 memset(m, 0, sizeof(*m));
1345
1346 if (bb == -1)
1347 b = h->af == AF_INET ? 32 : 128;
1348 else
1349 b = bb;
1350
1351 while (b >= 32) {
1352 m->addr32[j++] = 0xffffffff;
1353 b -= 32;
1354 }
1355 for (i = 31; i > 31-b; --i)
1356 m->addr32[j] |= (1 << i);
1357 if (b)
1358 m->addr32[j] = htonl(m->addr32[j]);
1359
1360 /* Mask off bits of the address that will never be used. */
1361 n = &h->addr.v.a.addr;
1362 if (h->addr.type == PF_ADDR_ADDRMASK)
1363 for (i = 0; i < 4; i++)
1364 n->addr32[i] = n->addr32[i] & m->addr32[i];
1365 }
1366
1367 int
check_netmask(struct node_host * h,sa_family_t af)1368 check_netmask(struct node_host *h, sa_family_t af)
1369 {
1370 struct node_host *n = NULL;
1371 struct pf_addr *m;
1372
1373 for (n = h; n != NULL; n = n->next) {
1374 if (h->addr.type == PF_ADDR_TABLE)
1375 continue;
1376 m = &h->addr.v.a.mask;
1377 /* netmasks > 32 bit are invalid on v4 */
1378 if (af == AF_INET &&
1379 (m->addr32[1] || m->addr32[2] || m->addr32[3])) {
1380 fprintf(stderr, "netmask %u invalid for IPv4 address\n",
1381 unmask(m));
1382 return (1);
1383 }
1384 }
1385 return (0);
1386 }
1387
1388 struct node_host *
gen_dynnode(struct node_host * h,sa_family_t af)1389 gen_dynnode(struct node_host *h, sa_family_t af)
1390 {
1391 struct node_host *n;
1392
1393 if (h->addr.type != PF_ADDR_DYNIFTL)
1394 return (NULL);
1395
1396 if ((n = calloc(1, sizeof(*n))) == NULL)
1397 return (NULL);
1398 bcopy(h, n, sizeof(*n));
1399 n->ifname = NULL;
1400 n->next = NULL;
1401 n->tail = NULL;
1402
1403 /* fix up netmask */
1404 if (af == AF_INET && unmask(&n->addr.v.a.mask) > 32)
1405 set_ipmask(n, 32);
1406
1407 return (n);
1408 }
1409
1410 /* interface lookup routines */
1411
1412 static struct node_host *iftab;
1413
1414 /*
1415 * Retrieve the list of groups this interface is a member of and make sure
1416 * each group is in the group map.
1417 */
1418 static void
ifa_add_groups_to_map(char * ifa_name)1419 ifa_add_groups_to_map(char *ifa_name)
1420 {
1421 int s, len;
1422 struct ifgroupreq ifgr;
1423 struct ifg_req *ifg;
1424
1425 s = get_query_socket();
1426
1427 /* Get size of group list for this interface */
1428 memset(&ifgr, 0, sizeof(ifgr));
1429 strlcpy(ifgr.ifgr_name, ifa_name, IFNAMSIZ);
1430 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1431 err(1, "SIOCGIFGROUP");
1432
1433 /* Retrieve group list for this interface */
1434 len = ifgr.ifgr_len;
1435 ifgr.ifgr_groups =
1436 (struct ifg_req *)calloc(len / sizeof(struct ifg_req),
1437 sizeof(struct ifg_req));
1438 if (ifgr.ifgr_groups == NULL)
1439 err(1, "calloc");
1440 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1441 err(1, "SIOCGIFGROUP");
1442
1443 ifg = ifgr.ifgr_groups;
1444 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1445 len -= sizeof(struct ifg_req);
1446 if (strcmp(ifg->ifgrq_group, "all")) {
1447 ENTRY item;
1448 ENTRY *ret_item;
1449 int *answer;
1450
1451 item.key = ifg->ifgrq_group;
1452 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0) {
1453 struct ifgroupreq ifgr2;
1454
1455 /* Don't know the answer yet */
1456 if ((answer = malloc(sizeof(int))) == NULL)
1457 err(1, "malloc");
1458
1459 bzero(&ifgr2, sizeof(ifgr2));
1460 strlcpy(ifgr2.ifgr_name, ifg->ifgrq_group,
1461 sizeof(ifgr2.ifgr_name));
1462 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr2) == 0)
1463 *answer = ifgr2.ifgr_len;
1464 else
1465 *answer = 0;
1466
1467 item.key = strdup(ifg->ifgrq_group);
1468 item.data = answer;
1469 if (hsearch_r(item, ENTER, &ret_item,
1470 &isgroup_map) == 0)
1471 err(1, "interface group query response"
1472 " map insert");
1473 }
1474 }
1475 }
1476 free(ifgr.ifgr_groups);
1477 }
1478
1479 void
ifa_load(void)1480 ifa_load(void)
1481 {
1482 struct ifaddrs *ifap, *ifa;
1483 struct node_host *n = NULL, *h = NULL;
1484
1485 if (getifaddrs(&ifap) < 0)
1486 err(1, "getifaddrs");
1487
1488 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1489 if (!(ifa->ifa_addr->sa_family == AF_INET ||
1490 ifa->ifa_addr->sa_family == AF_INET6 ||
1491 ifa->ifa_addr->sa_family == AF_LINK))
1492 continue;
1493 n = calloc(1, sizeof(struct node_host));
1494 if (n == NULL)
1495 err(1, "%s: calloc", __func__);
1496 n->af = ifa->ifa_addr->sa_family;
1497 n->ifa_flags = ifa->ifa_flags;
1498 #ifdef __KAME__
1499 if (n->af == AF_INET6 &&
1500 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
1501 ifa->ifa_addr)->sin6_addr) &&
1502 ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id ==
1503 0) {
1504 struct sockaddr_in6 *sin6;
1505
1506 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1507 sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 |
1508 sin6->sin6_addr.s6_addr[3];
1509 sin6->sin6_addr.s6_addr[2] = 0;
1510 sin6->sin6_addr.s6_addr[3] = 0;
1511 }
1512 #endif
1513 n->ifindex = 0;
1514 if (n->af == AF_LINK) {
1515 n->ifindex = ((struct sockaddr_dl *)
1516 ifa->ifa_addr)->sdl_index;
1517 ifa_add_groups_to_map(ifa->ifa_name);
1518 } else {
1519 copy_satopfaddr(&n->addr.v.a.addr, ifa->ifa_addr);
1520 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
1521 copy_satopfaddr(&n->addr.v.a.mask, ifa->ifa_netmask);
1522 if (ifa->ifa_broadaddr != NULL) {
1523 ifa->ifa_broadaddr->sa_family = ifa->ifa_addr->sa_family;
1524 copy_satopfaddr(&n->bcast, ifa->ifa_broadaddr);
1525 }
1526 if (ifa->ifa_dstaddr != NULL) {
1527 ifa->ifa_dstaddr->sa_family = ifa->ifa_addr->sa_family;
1528 copy_satopfaddr(&n->peer, ifa->ifa_dstaddr);
1529 }
1530 if (n->af == AF_INET6)
1531 n->ifindex = ((struct sockaddr_in6 *)
1532 ifa->ifa_addr) ->sin6_scope_id;
1533 }
1534 if ((n->ifname = strdup(ifa->ifa_name)) == NULL)
1535 err(1, "%s: strdup", __func__);
1536 n->next = NULL;
1537 n->tail = n;
1538 if (h == NULL)
1539 h = n;
1540 else {
1541 h->tail->next = n;
1542 h->tail = n;
1543 }
1544 }
1545
1546 iftab = h;
1547 freeifaddrs(ifap);
1548 }
1549
1550 static int
get_socket_domain(void)1551 get_socket_domain(void)
1552 {
1553 int sdom;
1554
1555 sdom = AF_UNSPEC;
1556 #ifdef WITH_INET6
1557 if (sdom == AF_UNSPEC && feature_present("inet6"))
1558 sdom = AF_INET6;
1559 #endif
1560 #ifdef WITH_INET
1561 if (sdom == AF_UNSPEC && feature_present("inet"))
1562 sdom = AF_INET;
1563 #endif
1564 if (sdom == AF_UNSPEC)
1565 sdom = AF_LINK;
1566
1567 return (sdom);
1568 }
1569
1570 int
get_query_socket(void)1571 get_query_socket(void)
1572 {
1573 static int s = -1;
1574
1575 if (s == -1) {
1576 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) == -1)
1577 err(1, "socket");
1578 }
1579
1580 return (s);
1581 }
1582
1583 /*
1584 * Returns the response len if the name is a group, otherwise returns 0.
1585 */
1586 static int
is_a_group(char * name)1587 is_a_group(char *name)
1588 {
1589 ENTRY item;
1590 ENTRY *ret_item;
1591
1592 item.key = name;
1593 if (hsearch_r(item, FIND, &ret_item, &isgroup_map) == 0)
1594 return (0);
1595
1596 return (*(int *)ret_item->data);
1597 }
1598
1599 unsigned int
ifa_nametoindex(const char * ifa_name)1600 ifa_nametoindex(const char *ifa_name)
1601 {
1602 struct node_host *p;
1603
1604 for (p = iftab; p; p = p->next) {
1605 if (p->af == AF_LINK && strcmp(p->ifname, ifa_name) == 0)
1606 return (p->ifindex);
1607 }
1608 errno = ENXIO;
1609 return (0);
1610 }
1611
1612 char *
ifa_indextoname(unsigned int ifindex,char * ifa_name)1613 ifa_indextoname(unsigned int ifindex, char *ifa_name)
1614 {
1615 struct node_host *p;
1616
1617 for (p = iftab; p; p = p->next) {
1618 if (p->af == AF_LINK && ifindex == p->ifindex) {
1619 strlcpy(ifa_name, p->ifname, IFNAMSIZ);
1620 return (ifa_name);
1621 }
1622 }
1623 errno = ENXIO;
1624 return (NULL);
1625 }
1626
1627 struct node_host *
ifa_exists(char * ifa_name)1628 ifa_exists(char *ifa_name)
1629 {
1630 struct node_host *n;
1631
1632 if (iftab == NULL)
1633 ifa_load();
1634
1635 /* check whether this is a group */
1636 if (is_a_group(ifa_name)) {
1637 /* fake a node_host */
1638 if ((n = calloc(1, sizeof(*n))) == NULL)
1639 err(1, "calloc");
1640 if ((n->ifname = strdup(ifa_name)) == NULL)
1641 err(1, "strdup");
1642 return (n);
1643 }
1644
1645 for (n = iftab; n; n = n->next) {
1646 if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ))
1647 return (n);
1648 }
1649
1650 return (NULL);
1651 }
1652
1653 struct node_host *
ifa_grouplookup(char * ifa_name,int flags)1654 ifa_grouplookup(char *ifa_name, int flags)
1655 {
1656 struct ifg_req *ifg;
1657 struct ifgroupreq ifgr;
1658 int s, len;
1659 struct node_host *n, *h = NULL;
1660
1661 s = get_query_socket();
1662 len = is_a_group(ifa_name);
1663 if (len == 0)
1664 return (NULL);
1665 bzero(&ifgr, sizeof(ifgr));
1666 strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name));
1667 ifgr.ifgr_len = len;
1668 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
1669 err(1, "calloc");
1670 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
1671 err(1, "SIOCGIFGMEMB");
1672
1673 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
1674 ifg++) {
1675 len -= sizeof(struct ifg_req);
1676 if ((n = ifa_lookup(ifg->ifgrq_member, flags)) == NULL)
1677 continue;
1678 if (h == NULL)
1679 h = n;
1680 else {
1681 h->tail->next = n;
1682 h->tail = n->tail;
1683 }
1684 }
1685 free(ifgr.ifgr_groups);
1686
1687 return (h);
1688 }
1689
1690 struct node_host *
ifa_lookup(char * ifa_name,int flags)1691 ifa_lookup(char *ifa_name, int flags)
1692 {
1693 struct node_host *p = NULL, *h = NULL, *n = NULL;
1694 int got4 = 0, got6 = 0;
1695 const char *last_if = NULL;
1696
1697 /* first load iftab and isgroup_map */
1698 if (iftab == NULL)
1699 ifa_load();
1700
1701 if ((h = ifa_grouplookup(ifa_name, flags)) != NULL)
1702 return (h);
1703
1704 if (!strncmp(ifa_name, "self", IFNAMSIZ))
1705 ifa_name = NULL;
1706
1707 for (p = iftab; p; p = p->next) {
1708 if (ifa_skip_if(ifa_name, p))
1709 continue;
1710 if ((flags & PFI_AFLAG_BROADCAST) && p->af != AF_INET)
1711 continue;
1712 if ((flags & PFI_AFLAG_BROADCAST) &&
1713 !(p->ifa_flags & IFF_BROADCAST))
1714 continue;
1715 if ((flags & PFI_AFLAG_BROADCAST) && p->bcast.v4.s_addr == 0)
1716 continue;
1717 if ((flags & PFI_AFLAG_PEER) &&
1718 !(p->ifa_flags & IFF_POINTOPOINT))
1719 continue;
1720 if ((flags & PFI_AFLAG_NETWORK) && p->ifindex > 0)
1721 continue;
1722 if (last_if == NULL || strcmp(last_if, p->ifname))
1723 got4 = got6 = 0;
1724 last_if = p->ifname;
1725 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET && got4)
1726 continue;
1727 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 &&
1728 IN6_IS_ADDR_LINKLOCAL(&p->addr.v.a.addr.v6))
1729 continue;
1730 if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && got6)
1731 continue;
1732 if (p->af == AF_INET)
1733 got4 = 1;
1734 else
1735 got6 = 1;
1736 n = calloc(1, sizeof(struct node_host));
1737 if (n == NULL)
1738 err(1, "%s: calloc", __func__);
1739 n->af = p->af;
1740 if (flags & PFI_AFLAG_BROADCAST)
1741 memcpy(&n->addr.v.a.addr, &p->bcast,
1742 sizeof(struct pf_addr));
1743 else if (flags & PFI_AFLAG_PEER)
1744 memcpy(&n->addr.v.a.addr, &p->peer,
1745 sizeof(struct pf_addr));
1746 else
1747 memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr,
1748 sizeof(struct pf_addr));
1749 if (flags & PFI_AFLAG_NETWORK)
1750 set_ipmask(n, unmask(&p->addr.v.a.mask));
1751 else
1752 set_ipmask(n, -1);
1753 n->ifindex = p->ifindex;
1754 n->ifname = strdup(p->ifname);
1755
1756 n->next = NULL;
1757 n->tail = n;
1758 if (h == NULL)
1759 h = n;
1760 else {
1761 h->tail->next = n;
1762 h->tail = n;
1763 }
1764 }
1765 return (h);
1766 }
1767
1768 int
ifa_skip_if(const char * filter,struct node_host * p)1769 ifa_skip_if(const char *filter, struct node_host *p)
1770 {
1771 int n;
1772
1773 if (p->af != AF_INET && p->af != AF_INET6)
1774 return (1);
1775 if (filter == NULL || !*filter)
1776 return (0);
1777 if (!strcmp(p->ifname, filter))
1778 return (0); /* exact match */
1779 n = strlen(filter);
1780 if (n < 1 || n >= IFNAMSIZ)
1781 return (1); /* sanity check */
1782 if (filter[n-1] >= '0' && filter[n-1] <= '9')
1783 return (1); /* only do exact match in that case */
1784 if (strncmp(p->ifname, filter, n))
1785 return (1); /* prefix doesn't match */
1786 return (p->ifname[n] < '0' || p->ifname[n] > '9');
1787 }
1788
1789
1790 struct node_host *
host(const char * s,int opts)1791 host(const char *s, int opts)
1792 {
1793 struct node_host *h = NULL;
1794 int mask = -1;
1795 char *p, *ps;
1796 const char *errstr;
1797
1798 if ((p = strrchr(s, '/')) != NULL) {
1799 mask = strtonum(p+1, 0, 128, &errstr);
1800 if (errstr) {
1801 fprintf(stderr, "netmask is %s: %s\n", errstr, p);
1802 goto error;
1803 }
1804 if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL)
1805 err(1, "%s: malloc", __func__);
1806 strlcpy(ps, s, strlen(s) - strlen(p) + 1);
1807 } else {
1808 if ((ps = strdup(s)) == NULL)
1809 err(1, "%s: strdup", __func__);
1810 }
1811
1812 if ((h = host_ip(ps, mask)) == NULL &&
1813 (h = host_if(ps, mask)) == NULL &&
1814 (h = host_dns(ps, mask, (opts & PF_OPT_NODNS))) == NULL) {
1815 fprintf(stderr, "no IP address found for %s\n", s);
1816 goto error;
1817 }
1818
1819 error:
1820 free(ps);
1821 return (h);
1822 }
1823
1824 struct node_host *
host_if(const char * s,int mask)1825 host_if(const char *s, int mask)
1826 {
1827 struct node_host *n, *h = NULL;
1828 char *p, *ps;
1829 int flags = 0;
1830
1831 if ((ps = strdup(s)) == NULL)
1832 err(1, "host_if: strdup");
1833 while ((p = strrchr(ps, ':')) != NULL) {
1834 if (!strcmp(p+1, "network"))
1835 flags |= PFI_AFLAG_NETWORK;
1836 else if (!strcmp(p+1, "broadcast"))
1837 flags |= PFI_AFLAG_BROADCAST;
1838 else if (!strcmp(p+1, "peer"))
1839 flags |= PFI_AFLAG_PEER;
1840 else if (!strcmp(p+1, "0"))
1841 flags |= PFI_AFLAG_NOALIAS;
1842 else
1843 goto error;
1844 *p = '\0';
1845 }
1846 if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { /* Yep! */
1847 fprintf(stderr, "illegal combination of interface modifiers\n");
1848 goto error;
1849 }
1850 if ((flags & (PFI_AFLAG_NETWORK|PFI_AFLAG_BROADCAST)) && mask > -1) {
1851 fprintf(stderr, "network or broadcast lookup, but "
1852 "extra netmask given\n");
1853 goto error;
1854 }
1855 if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) {
1856 /* interface with this name exists */
1857 h = ifa_lookup(ps, flags);
1858 if (mask > -1)
1859 for (n = h; n != NULL; n = n->next)
1860 set_ipmask(n, mask);
1861 }
1862
1863 error:
1864 free(ps);
1865 return (h);
1866 }
1867
1868 struct node_host *
host_ip(const char * s,int mask)1869 host_ip(const char *s, int mask)
1870 {
1871 struct addrinfo hints, *res;
1872 struct node_host *h = NULL;
1873
1874 h = calloc(1, sizeof(*h));
1875 if (h == NULL)
1876 err(1, "%s: calloc", __func__);
1877 if (mask != -1) {
1878 /* Try to parse 10/8 */
1879 h->af = AF_INET;
1880 if (inet_net_pton(AF_INET, s, &h->addr.v.a.addr.v4,
1881 sizeof(h->addr.v.a.addr.v4)) != -1)
1882 goto out;
1883 }
1884
1885 memset(&hints, 0, sizeof(hints));
1886 hints.ai_family = AF_UNSPEC;
1887 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
1888 hints.ai_flags = AI_NUMERICHOST;
1889 if (getaddrinfo(s, NULL, &hints, &res) == 0) {
1890 h->af = res->ai_family;
1891 copy_satopfaddr(&h->addr.v.a.addr, res->ai_addr);
1892 if (h->af == AF_INET6)
1893 h->ifindex =
1894 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
1895 freeaddrinfo(res);
1896 } else {
1897 free(h);
1898 return (NULL);
1899 }
1900 out:
1901 set_ipmask(h, mask);
1902 h->ifname = NULL;
1903 h->next = NULL;
1904 h->tail = h;
1905
1906 return (h);
1907 }
1908
1909 struct node_host *
host_dns(const char * s,int mask,int numeric)1910 host_dns(const char *s, int mask, int numeric)
1911 {
1912 struct addrinfo hints, *res0, *res;
1913 struct node_host *n, *h = NULL;
1914 int noalias = 0, got4 = 0, got6 = 0;
1915 char *p, *ps;
1916
1917 if ((ps = strdup(s)) == NULL)
1918 err(1, "host_dns: strdup");
1919 if ((p = strrchr(ps, ':')) != NULL && !strcmp(p, ":0")) {
1920 noalias = 1;
1921 *p = '\0';
1922 }
1923 memset(&hints, 0, sizeof(hints));
1924 hints.ai_family = PF_UNSPEC;
1925 hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1926 if (numeric)
1927 hints.ai_flags = AI_NUMERICHOST;
1928 if (getaddrinfo(ps, NULL, &hints, &res0) != 0)
1929 goto error;
1930
1931 for (res = res0; res; res = res->ai_next) {
1932 if (res->ai_family != AF_INET &&
1933 res->ai_family != AF_INET6)
1934 continue;
1935 if (noalias) {
1936 if (res->ai_family == AF_INET) {
1937 if (got4)
1938 continue;
1939 got4 = 1;
1940 } else {
1941 if (got6)
1942 continue;
1943 got6 = 1;
1944 }
1945 }
1946 n = calloc(1, sizeof(struct node_host));
1947 if (n == NULL)
1948 err(1, "host_dns: calloc");
1949 n->ifname = NULL;
1950 n->af = res->ai_family;
1951 copy_satopfaddr(&n->addr.v.a.addr, res->ai_addr);
1952 if (res->ai_family == AF_INET6)
1953 n->ifindex =
1954 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
1955 set_ipmask(n, mask);
1956 n->next = NULL;
1957 n->tail = n;
1958 if (h == NULL)
1959 h = n;
1960 else {
1961 h->tail->next = n;
1962 h->tail = n;
1963 }
1964 }
1965 freeaddrinfo(res0);
1966 error:
1967 free(ps);
1968
1969 return (h);
1970 }
1971
1972 /*
1973 * convert a hostname to a list of addresses and put them in the given buffer.
1974 * test:
1975 * if set to 1, only simple addresses are accepted (no netblock, no "!").
1976 */
1977 int
append_addr(struct pfr_buffer * b,char * s,int test,int opts)1978 append_addr(struct pfr_buffer *b, char *s, int test, int opts)
1979 {
1980 char *r;
1981 struct node_host *h, *n;
1982 int rv, not = 0;
1983
1984 for (r = s; *r == '!'; r++)
1985 not = !not;
1986 if ((n = host(r, opts)) == NULL) {
1987 errno = 0;
1988 return (-1);
1989 }
1990 rv = append_addr_host(b, n, test, not);
1991 do {
1992 h = n;
1993 n = n->next;
1994 free(h);
1995 } while (n != NULL);
1996 return (rv);
1997 }
1998
1999 /*
2000 * same as previous function, but with a pre-parsed input and the ability
2001 * to "negate" the result. Does not free the node_host list.
2002 * not:
2003 * setting it to 1 is equivalent to adding "!" in front of parameter s.
2004 */
2005 int
append_addr_host(struct pfr_buffer * b,struct node_host * n,int test,int not)2006 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not)
2007 {
2008 int bits;
2009 struct pfr_addr addr;
2010
2011 do {
2012 bzero(&addr, sizeof(addr));
2013 addr.pfra_not = n->not ^ not;
2014 addr.pfra_af = n->af;
2015 addr.pfra_net = unmask(&n->addr.v.a.mask);
2016 switch (n->af) {
2017 case AF_INET:
2018 addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0];
2019 bits = 32;
2020 break;
2021 case AF_INET6:
2022 memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6,
2023 sizeof(struct in6_addr));
2024 bits = 128;
2025 break;
2026 default:
2027 errno = EINVAL;
2028 return (-1);
2029 }
2030 if ((test && (not || addr.pfra_net != bits)) ||
2031 addr.pfra_net > bits) {
2032 errno = EINVAL;
2033 return (-1);
2034 }
2035 if (pfr_buf_add(b, &addr))
2036 return (-1);
2037 } while ((n = n->next) != NULL);
2038
2039 return (0);
2040 }
2041
2042 int
pfctl_add_trans(struct pfr_buffer * buf,int rs_num,const char * anchor)2043 pfctl_add_trans(struct pfr_buffer *buf, int rs_num, const char *anchor)
2044 {
2045 struct pfioc_trans_e trans;
2046
2047 bzero(&trans, sizeof(trans));
2048 trans.rs_num = rs_num;
2049 if (strlcpy(trans.anchor, anchor,
2050 sizeof(trans.anchor)) >= sizeof(trans.anchor))
2051 errx(1, "pfctl_add_trans: strlcpy");
2052
2053 return pfr_buf_add(buf, &trans);
2054 }
2055
2056 u_int32_t
pfctl_get_ticket(struct pfr_buffer * buf,int rs_num,const char * anchor)2057 pfctl_get_ticket(struct pfr_buffer *buf, int rs_num, const char *anchor)
2058 {
2059 struct pfioc_trans_e *p;
2060
2061 PFRB_FOREACH(p, buf)
2062 if (rs_num == p->rs_num && !strcmp(anchor, p->anchor))
2063 return (p->ticket);
2064 errx(1, "pfctl_get_ticket: assertion failed");
2065 }
2066
2067 int
pfctl_trans(int dev,struct pfr_buffer * buf,u_long cmd,int from)2068 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from)
2069 {
2070 struct pfioc_trans trans;
2071
2072 bzero(&trans, sizeof(trans));
2073 trans.size = buf->pfrb_size - from;
2074 trans.esize = sizeof(struct pfioc_trans_e);
2075 trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from;
2076 return ioctl(dev, cmd, &trans);
2077 }
2078