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