1 /* $OpenBSD: pfctl_table.c,v 1.67 2008/06/10 20:55:02 mcbride Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2002 Cedric Berger
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <net/if.h>
42 #include <net/pfvar.h>
43
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <netdb.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53
54 #include "pfctl_parser.h"
55 #include "pfctl.h"
56
57 extern void usage(void);
58 static void print_table(const struct pfr_table *, int, int);
59 static int print_tstats(const struct pfr_tstats *, int);
60 static int load_addr(struct pfr_buffer *, int, char *[], char *, int, int);
61 static void print_addrx(struct pfr_addr *, struct pfr_addr *, int);
62 static int nonzero_astats(struct pfr_astats *);
63 static void print_astats(struct pfr_astats *, int);
64 static void xprintf(int, const char *, ...);
65 static void print_iface(struct pfi_kif *, int);
66
67 static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
68 { "In/Block:", "In/Pass:", "In/XPass:" },
69 { "Out/Block:", "Out/Pass:", "Out/XPass:" }
70 };
71
72 static const char *istats_text[2][2][2] = {
73 { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
74 { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
75 };
76
77 #define RETTEST(fct) do { \
78 int _ret; \
79 if ((!(opts & PF_OPT_NOACTION) || \
80 (opts & PF_OPT_DUMMYACTION)) && \
81 (_ret = (fct) != 0)) { \
82 if ((opts & PF_OPT_RECURSE) == 0) \
83 warnx("%s", pf_strerror(_ret)); \
84 goto _error; \
85 } \
86 } while (0)
87
88 #define RVTEST(fct) do { \
89 if ((!(opts & PF_OPT_NOACTION) || \
90 (opts & PF_OPT_DUMMYACTION)) && \
91 (fct)) { \
92 if ((opts & PF_OPT_RECURSE) == 0) \
93 warnx("%s", pf_strerror(errno)); \
94 goto _error; \
95 } \
96 } while (0)
97
98 #define CREATE_TABLE do { \
99 int _ret; \
100 warn_duplicate_tables(table.pfrt_name, \
101 table.pfrt_anchor); \
102 table.pfrt_flags |= PFR_TFLAG_PERSIST; \
103 if ((!(opts & PF_OPT_NOACTION) || \
104 (opts & PF_OPT_DUMMYACTION)) && \
105 (_ret = pfr_add_table(&table, &nadd, flags)) && \
106 (_ret != EPERM)) { \
107 warnx("%s", pf_strerror(_ret)); \
108 goto _error; \
109 } \
110 if (nadd) { \
111 xprintf(opts, "%d table created", nadd); \
112 if (opts & PF_OPT_NOACTION) \
113 return (0); \
114 } \
115 table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \
116 } while(0)
117
118 int
pfctl_do_clear_tables(const char * anchor,int opts)119 pfctl_do_clear_tables(const char *anchor, int opts)
120 {
121 int rv;
122
123 if ((rv = pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts)) == -1) {
124 if ((opts & PF_OPT_IGNFAIL) == 0)
125 exit(1);
126 }
127
128 return (rv);
129 }
130
131 void
pfctl_show_tables(const char * anchor,int opts)132 pfctl_show_tables(const char *anchor, int opts)
133 {
134 if (pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts))
135 exit(1);
136 }
137
138 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)139 pfctl_table(int argc, char *argv[], char *tname, const char *command,
140 char *file, const char *anchor, int opts)
141 {
142 struct pfr_table table;
143 struct pfr_buffer b, b2;
144 struct pfr_addr *a, *a2;
145 int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
146 int rv = 0, flags = 0, nmatch = 0;
147 void *p;
148
149 if (command == NULL)
150 usage();
151 if (opts & PF_OPT_NOACTION)
152 flags |= PFR_FLAG_DUMMY;
153
154 bzero(&b, sizeof(b));
155 bzero(&b2, sizeof(b2));
156 bzero(&table, sizeof(table));
157 if (tname != NULL) {
158 if (strlen(tname) >= PF_TABLE_NAME_SIZE)
159 usage();
160 if (strlcpy(table.pfrt_name, tname,
161 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
162 errx(1, "pfctl_table: strlcpy");
163 }
164 if (strlcpy(table.pfrt_anchor, anchor,
165 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
166 errx(1, "pfctl_table: strlcpy");
167
168 if (!strcmp(command, "-F")) {
169 if (argc || file != NULL)
170 usage();
171 RETTEST(pfctl_clear_tables(pfh, &table, &ndel, flags));
172 xprintf(opts, "%d tables deleted", ndel);
173 } else if (!strcmp(command, "-s")) {
174 b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
175 PFRB_TSTATS : PFRB_TABLES;
176 if (argc || file != NULL)
177 usage();
178
179 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
180 pfctl_print_title("TABLES:");
181
182 if (opts & PF_OPT_VERBOSE2) {
183 uintptr_t arg = opts & PF_OPT_DEBUG;
184 pfctl_get_tstats(pfh, &table,
185 (pfctl_get_tstats_fn)print_tstats, (void *)arg);
186 } else {
187 for (;;) {
188 pfr_buf_grow(&b, b.pfrb_size);
189 b.pfrb_size = b.pfrb_msize;
190 RVTEST(pfr_get_tables(&table,
191 b.pfrb_caddr, &b.pfrb_size, flags));
192 if (b.pfrb_size <= b.pfrb_msize)
193 break;
194 }
195
196 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
197 pfctl_print_title("TABLES:");
198
199 PFRB_FOREACH(p, &b)
200 print_table(p, opts & PF_OPT_VERBOSE,
201 opts & PF_OPT_DEBUG);
202 }
203 } else if (!strcmp(command, "kill")) {
204 if (argc || file != NULL)
205 usage();
206 RETTEST(pfr_del_table(&table, &ndel, flags));
207 xprintf(opts, "%d table deleted", ndel);
208 } else if (!strcmp(command, "flush")) {
209 if (argc || file != NULL)
210 usage();
211 RETTEST(pfr_clr_addrs(&table, &ndel, flags));
212 xprintf(opts, "%d addresses deleted", ndel);
213 } else if (!strcmp(command, "add")) {
214 b.pfrb_type = PFRB_ADDRS;
215 if (load_addr(&b, argc, argv, file, 0, opts))
216 goto _error;
217 CREATE_TABLE;
218 if (opts & PF_OPT_VERBOSE)
219 flags |= PFR_FLAG_FEEDBACK;
220 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
221 &nadd, flags));
222 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
223 if (opts & PF_OPT_VERBOSE)
224 PFRB_FOREACH(a, &b)
225 if ((opts & PF_OPT_VERBOSE2) ||
226 a->pfra_fback != PFR_FB_NONE)
227 print_addrx(a, NULL,
228 opts & PF_OPT_USEDNS);
229 } else if (!strcmp(command, "delete")) {
230 b.pfrb_type = PFRB_ADDRS;
231 if (load_addr(&b, argc, argv, file, 0, opts))
232 goto _error;
233 if (opts & PF_OPT_VERBOSE)
234 flags |= PFR_FLAG_FEEDBACK;
235 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
236 &ndel, flags));
237 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
238 if (opts & PF_OPT_VERBOSE)
239 PFRB_FOREACH(a, &b)
240 if ((opts & PF_OPT_VERBOSE2) ||
241 a->pfra_fback != PFR_FB_NONE)
242 print_addrx(a, NULL,
243 opts & PF_OPT_USEDNS);
244 } else if (!strcmp(command, "replace")) {
245 b.pfrb_type = PFRB_ADDRS;
246 if (load_addr(&b, argc, argv, file, 0, opts))
247 goto _error;
248 CREATE_TABLE;
249 if (opts & PF_OPT_VERBOSE)
250 flags |= PFR_FLAG_FEEDBACK;
251 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
252 &nadd, &ndel, &nchange, flags));
253 if (nadd)
254 xprintf(opts, "%d addresses added", nadd);
255 if (ndel)
256 xprintf(opts, "%d addresses deleted", ndel);
257 if (nchange)
258 xprintf(opts, "%d addresses changed", nchange);
259 if (!nadd && !ndel && !nchange)
260 xprintf(opts, "no changes");
261 if (opts & PF_OPT_VERBOSE)
262 PFRB_FOREACH(a, &b)
263 if ((opts & PF_OPT_VERBOSE2) ||
264 a->pfra_fback != PFR_FB_NONE)
265 print_addrx(a, NULL,
266 opts & PF_OPT_USEDNS);
267 } else if (!strcmp(command, "expire")) {
268 const char *errstr;
269 u_int lifetime;
270
271 b.pfrb_type = PFRB_ASTATS;
272 b2.pfrb_type = PFRB_ADDRS;
273 if (argc != 1 || file != NULL)
274 usage();
275 lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
276 if (errstr)
277 errx(1, "expiry time: %s", errstr);
278 for (;;) {
279 pfr_buf_grow(&b, b.pfrb_size);
280 b.pfrb_size = b.pfrb_msize;
281 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
282 &b.pfrb_size, flags));
283 if (b.pfrb_size <= b.pfrb_msize)
284 break;
285 }
286 PFRB_FOREACH(p, &b) {
287 ((struct pfr_astats *)p)->pfras_a.pfra_fback = PFR_FB_NONE;
288 if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
289 lifetime)
290 if (pfr_buf_add(&b2,
291 &((struct pfr_astats *)p)->pfras_a))
292 err(1, "duplicate buffer");
293 }
294
295 if (opts & PF_OPT_VERBOSE)
296 flags |= PFR_FLAG_FEEDBACK;
297 RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
298 &ndel, flags));
299 xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
300 if (opts & PF_OPT_VERBOSE)
301 PFRB_FOREACH(a, &b2)
302 if ((opts & PF_OPT_VERBOSE2) ||
303 a->pfra_fback != PFR_FB_NONE)
304 print_addrx(a, NULL,
305 opts & PF_OPT_USEDNS);
306 } else if (!strcmp(command, "reset")) {
307 struct pfr_astats *as;
308
309 b.pfrb_type = PFRB_ASTATS;
310 b2.pfrb_type = PFRB_ADDRS;
311 if (argc || file != NULL)
312 usage();
313 do {
314 pfr_buf_grow(&b, b.pfrb_size);
315 b.pfrb_size = b.pfrb_msize;
316 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
317 &b.pfrb_size, flags));
318 } while (b.pfrb_size > b.pfrb_msize);
319 PFRB_FOREACH(as, &b) {
320 as->pfras_a.pfra_fback = 0;
321 if (nonzero_astats(as))
322 if (pfr_buf_add(&b2, &as->pfras_a))
323 err(1, "duplicate buffer");
324 }
325
326 if (opts & PF_OPT_VERBOSE)
327 flags |= PFR_FLAG_FEEDBACK;
328 RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size,
329 &nzero, flags));
330 xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size);
331 if (opts & PF_OPT_VERBOSE)
332 PFRB_FOREACH(a, &b2)
333 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
334 print_addrx(a, NULL,
335 opts & PF_OPT_USEDNS);
336 } else if (!strcmp(command, "show")) {
337 b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
338 PFRB_ASTATS : PFRB_ADDRS;
339 if (argc || file != NULL)
340 usage();
341 for (;;) {
342 pfr_buf_grow(&b, b.pfrb_size);
343 b.pfrb_size = b.pfrb_msize;
344 if (opts & PF_OPT_VERBOSE)
345 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
346 &b.pfrb_size, flags));
347 else
348 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
349 &b.pfrb_size, flags));
350 if (b.pfrb_size <= b.pfrb_msize)
351 break;
352 }
353 PFRB_FOREACH(p, &b)
354 if (opts & PF_OPT_VERBOSE)
355 print_astats(p, opts & PF_OPT_USEDNS);
356 else
357 print_addrx(p, NULL, opts & PF_OPT_USEDNS);
358 } else if (!strcmp(command, "test")) {
359 b.pfrb_type = PFRB_ADDRS;
360 b2.pfrb_type = PFRB_ADDRS;
361
362 if (load_addr(&b, argc, argv, file, 1, opts))
363 goto _error;
364 if (opts & PF_OPT_VERBOSE2) {
365 flags |= PFR_FLAG_REPLACE;
366 PFRB_FOREACH(a, &b)
367 if (pfr_buf_add(&b2, a))
368 err(1, "duplicate buffer");
369 }
370 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
371 &nmatch, flags));
372 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
373 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
374 PFRB_FOREACH(a, &b)
375 if (a->pfra_fback == PFR_FB_MATCH)
376 print_addrx(a, NULL,
377 opts & PF_OPT_USEDNS);
378 if (opts & PF_OPT_VERBOSE2) {
379 a2 = NULL;
380 PFRB_FOREACH(a, &b) {
381 a2 = pfr_buf_next(&b2, a2);
382 print_addrx(a2, a, opts & PF_OPT_USEDNS);
383 }
384 }
385 if (nmatch < b.pfrb_size)
386 rv = 2;
387 } else if (!strcmp(command, "zero") && (argc || file != NULL)) {
388 b.pfrb_type = PFRB_ADDRS;
389 if (load_addr(&b, argc, argv, file, 0, opts))
390 goto _error;
391 if (opts & PF_OPT_VERBOSE)
392 flags |= PFR_FLAG_FEEDBACK;
393 RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
394 &nzero, flags));
395 xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
396 if (opts & PF_OPT_VERBOSE)
397 PFRB_FOREACH(a, &b)
398 if (opts & PF_OPT_VERBOSE2 ||
399 a->pfra_fback != PFR_FB_NONE)
400 print_addrx(a, NULL,
401 opts & PF_OPT_USEDNS);
402 } else if (!strcmp(command, "zero")) {
403 flags |= PFR_FLAG_ADDRSTOO;
404 RETTEST(pfctl_clear_tstats(pfh, &table, &nzero, flags));
405 xprintf(opts, "%d table/stats cleared", nzero);
406 } else
407 warnx("pfctl_table: unknown command '%s'", command);
408 goto _cleanup;
409
410 _error:
411 rv = -1;
412 _cleanup:
413 pfr_buf_clear(&b);
414 pfr_buf_clear(&b2);
415 return (rv);
416 }
417
418 void
print_table(const struct pfr_table * ta,int verbose,int debug)419 print_table(const struct pfr_table *ta, int verbose, int debug)
420 {
421 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
422 return;
423 if (verbose)
424 printf("%c%c%c%c%c%c%c\t",
425 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
426 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
427 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
428 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
429 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
430 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
431 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-');
432
433 printf("%s", ta->pfrt_name);
434 if (ta->pfrt_anchor[0] != '\0')
435 printf("@%s", ta->pfrt_anchor);
436
437 printf("\n");
438 }
439
440 int
print_tstats(const struct pfr_tstats * ts,int debug)441 print_tstats(const struct pfr_tstats *ts, int debug)
442 {
443 time_t time = ts->pfrts_tzero;
444 int dir, op;
445 char *ct;
446
447 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
448 return (0);
449 ct = ctime(&time);
450 print_table(&ts->pfrts_t, 1, debug);
451 printf("\tAddresses: %d\n", ts->pfrts_cnt);
452 if (ct)
453 printf("\tCleared: %s", ct);
454 else
455 printf("\tCleared: %lld\n", (long long)time);
456 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
457 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
458 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
459 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
460 (unsigned long long)ts->pfrts_nomatch,
461 (unsigned long long)ts->pfrts_match);
462 for (dir = 0; dir < PFR_DIR_MAX; dir++)
463 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
464 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
465 stats_text[dir][op],
466 (unsigned long long)ts->pfrts_packets[dir][op],
467 (unsigned long long)ts->pfrts_bytes[dir][op]);
468
469 return (0);
470 }
471
472 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork,int opts)473 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
474 int nonetwork, int opts)
475 {
476 while (argc--)
477 if (append_addr(b, *argv++, nonetwork, opts)) {
478 if (errno)
479 warn("cannot decode %s", argv[-1]);
480 return (-1);
481 }
482 if (pfr_buf_load(b, file, nonetwork, append_addr, opts)) {
483 warn("cannot load %s", file);
484 return (-1);
485 }
486 return (0);
487 }
488
489 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)490 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
491 {
492 char ch, buf[256] = "{error}";
493 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
494 unsigned int fback, hostnet;
495
496 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
497 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
498 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
499 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
500 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
501 if (ad->pfra_net < hostnet)
502 printf("/%d", ad->pfra_net);
503 if (rad != NULL && fback != PFR_FB_NONE) {
504 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
505 errx(1, "print_addrx: strlcpy");
506 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
507 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
508 if (rad->pfra_net < hostnet)
509 printf("/%d", rad->pfra_net);
510 }
511 if (rad != NULL && fback == PFR_FB_NONE)
512 printf("\t nomatch");
513 if (dns && ad->pfra_net == hostnet) {
514 char host[NI_MAXHOST];
515 struct sockaddr_storage ss;
516
517 strlcpy(host, "?", sizeof(host));
518 bzero(&ss, sizeof(ss));
519 ss.ss_family = ad->pfra_af;
520 if (ss.ss_family == AF_INET) {
521 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
522
523 sin->sin_len = sizeof(*sin);
524 sin->sin_addr = ad->pfra_ip4addr;
525 } else {
526 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
527
528 sin6->sin6_len = sizeof(*sin6);
529 sin6->sin6_addr = ad->pfra_ip6addr;
530 }
531 if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
532 sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
533 printf("\t(%s)", host);
534 }
535 printf("\n");
536 }
537
538 int
nonzero_astats(struct pfr_astats * as)539 nonzero_astats(struct pfr_astats *as)
540 {
541 uint64_t s = 0;
542
543 for (int dir = 0; dir < PFR_DIR_MAX; dir++)
544 for (int op = 0; op < PFR_OP_ADDR_MAX; op++)
545 s |= as->pfras_packets[dir][op] |
546 as->pfras_bytes[dir][op];
547
548 return (!!s);
549 }
550
551 void
print_astats(struct pfr_astats * as,int dns)552 print_astats(struct pfr_astats *as, int dns)
553 {
554 time_t time = as->pfras_tzero;
555 int dir, op;
556 char *ct;
557
558 ct = ctime(&time);
559 print_addrx(&as->pfras_a, NULL, dns);
560 if (ct)
561 printf("\tCleared: %s", ct);
562 else
563 printf("\tCleared: %lld\n", (long long)time);
564 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
565 return;
566 for (dir = 0; dir < PFR_DIR_MAX; dir++)
567 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
568 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
569 stats_text[dir][op],
570 (unsigned long long)as->pfras_packets[dir][op],
571 (unsigned long long)as->pfras_bytes[dir][op]);
572 }
573
574 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket,struct pfr_uktable * ukt)575 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
576 struct pfr_buffer *ab, u_int32_t ticket, struct pfr_uktable *ukt)
577 {
578 struct pfr_table tbl_buf;
579 struct pfr_table *tbl;
580
581 if (ukt == NULL) {
582 bzero(&tbl_buf, sizeof(tbl_buf));
583 tbl = &tbl_buf;
584 } else {
585 if (ab->pfrb_size != 0) {
586 /*
587 * copy IP addresses which come with table from
588 * temporal buffer to buffer attached to table.
589 */
590 ukt->pfrukt_addrs = *ab;
591 ab->pfrb_size = 0;
592 ab->pfrb_msize = 0;
593 ab->pfrb_caddr = NULL;
594 } else
595 memset(&ukt->pfrukt_addrs, 0,
596 sizeof(struct pfr_buffer));
597
598 tbl = &ukt->pfrukt_t;
599 }
600
601 if (strlcpy(tbl->pfrt_name, name, sizeof(tbl->pfrt_name)) >=
602 sizeof(tbl->pfrt_name) ||
603 strlcpy(tbl->pfrt_anchor, anchor, sizeof(tbl->pfrt_anchor)) >=
604 sizeof(tbl->pfrt_anchor))
605 errx(1, "%s: strlcpy", __func__);
606 tbl->pfrt_flags = flags;
607 DBGPRINT("%s %s@%s [%x]\n", __func__, tbl->pfrt_name, tbl->pfrt_anchor,
608 tbl->pfrt_flags);
609
610 /*
611 * non-root anchors processed by parse.y are loaded to kernel later.
612 * Here we load tables, which are either created for root anchor
613 * or by 'pfctl -t ... -T ...' command.
614 */
615 if (ukt != NULL)
616 return (0);
617
618 return (pfr_ina_define(tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, NULL,
619 ticket, addrs ? PFR_FLAG_ADDRSTOO : 0));
620 }
621
622 void
warn_duplicate_tables(const char * tablename,const char * anchorname)623 warn_duplicate_tables(const char *tablename, const char *anchorname)
624 {
625 struct pfr_buffer b;
626 struct pfr_table *t;
627
628 bzero(&b, sizeof(b));
629 b.pfrb_type = PFRB_TABLES;
630 for (;;) {
631 pfr_buf_grow(&b, b.pfrb_size);
632 b.pfrb_size = b.pfrb_msize;
633 if (pfr_get_tables(NULL, b.pfrb_caddr,
634 &b.pfrb_size, PFR_FLAG_ALLRSETS))
635 err(1, "pfr_get_tables");
636 if (b.pfrb_size <= b.pfrb_msize)
637 break;
638 }
639 PFRB_FOREACH(t, &b) {
640 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
641 continue;
642 if (!strcmp(anchorname, t->pfrt_anchor))
643 continue;
644 if (!strcmp(tablename, t->pfrt_name))
645 warnx("warning: table <%s> already defined"
646 " in anchor \"%s\"", tablename,
647 t->pfrt_anchor[0] ? t->pfrt_anchor : "/");
648 }
649 pfr_buf_clear(&b);
650 }
651
652 void
xprintf(int opts,const char * fmt,...)653 xprintf(int opts, const char *fmt, ...)
654 {
655 va_list args;
656
657 if (opts & PF_OPT_QUIET)
658 return;
659
660 va_start(args, fmt);
661 vfprintf(stderr, fmt, args);
662 va_end(args);
663
664 if (opts & PF_OPT_DUMMYACTION)
665 fprintf(stderr, " (dummy).\n");
666 else if (opts & PF_OPT_NOACTION)
667 fprintf(stderr, " (syntax only).\n");
668 else
669 fprintf(stderr, ".\n");
670 }
671
672
673 /* interface stuff */
674
675 void
pfctl_show_ifaces(const char * filter,int opts)676 pfctl_show_ifaces(const char *filter, int opts)
677 {
678 struct pfr_buffer b;
679 struct pfi_kif *p;
680
681 bzero(&b, sizeof(b));
682 b.pfrb_type = PFRB_IFACES;
683 for (;;) {
684 pfr_buf_grow(&b, b.pfrb_size);
685 b.pfrb_size = b.pfrb_msize;
686 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size))
687 errx(1, "%s", pf_strerror(errno));
688 if (b.pfrb_size <= b.pfrb_msize)
689 break;
690 }
691 if (opts & PF_OPT_SHOWALL)
692 pfctl_print_title("INTERFACES:");
693 PFRB_FOREACH(p, &b)
694 print_iface(p, opts);
695 }
696
697 void
print_iface(struct pfi_kif * p,int opts)698 print_iface(struct pfi_kif *p, int opts)
699 {
700 time_t tzero = p->pfik_tzero;
701 int i, af, dir, act;
702 char *ct;
703
704 printf("%s", p->pfik_name);
705 if (opts & PF_OPT_VERBOSE) {
706 if (p->pfik_flags & PFI_IFLAG_SKIP)
707 printf(" (skip)");
708 }
709 printf("\n");
710
711 if (!(opts & PF_OPT_VERBOSE2))
712 return;
713 ct = ctime(&tzero);
714 if (ct)
715 printf("\tCleared: %s", ct);
716 else
717 printf("\tCleared: %lld\n", (long long)tzero);
718 printf("\tReferences: %-18d\n", p->pfik_rulerefs);
719 for (i = 0; i < 8; i++) {
720 af = (i>>2) & 1;
721 dir = (i>>1) &1;
722 act = i & 1;
723 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
724 istats_text[af][dir][act],
725 (unsigned long long)p->pfik_packets[af][dir][act],
726 (unsigned long long)p->pfik_bytes[af][dir][act]);
727 }
728 }
729