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