1 /* $OpenBSD: pfctl_table.c,v 1.91 2024/11/20 13:57:29 kirill 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") && (argc || file != NULL)) {
350 b.pfrb_type = PFRB_ADDRS;
351 if (load_addr(&b, argc, argv, file, 0, opts))
352 goto _error;
353 if (opts & PF_OPT_VERBOSE)
354 flags |= PFR_FLAG_FEEDBACK;
355 RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
356 &nzero, flags));
357 xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
358 if (opts & PF_OPT_VERBOSE)
359 PFRB_FOREACH(a, &b)
360 if (opts & PF_OPT_VERBOSE2 ||
361 a->pfra_fback != PFR_FB_NONE)
362 print_addrx(a, NULL,
363 opts & PF_OPT_USEDNS);
364 } else if (!strcmp(command, "zero")) {
365 flags |= PFR_FLAG_ADDRSTOO;
366 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
367 xprintf(opts, "%d table/stats cleared", nzero);
368 } else
369 warnx("pfctl_table: unknown command '%s'", command);
370 goto _cleanup;
371
372 _error:
373 rv = -1;
374 _cleanup:
375 pfr_buf_clear(&b);
376 pfr_buf_clear(&b2);
377 return (rv);
378 }
379
380 void
print_table(struct pfr_table * ta,int verbose,int debug)381 print_table(struct pfr_table *ta, int verbose, int debug)
382 {
383 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
384 return;
385 if (verbose)
386 printf("%c%c%c%c%c%c%c\t",
387 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
388 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
389 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
390 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
391 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
392 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
393 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-');
394
395 printf("%s", ta->pfrt_name);
396 if (ta->pfrt_anchor[0] != '\0')
397 printf("@%s", ta->pfrt_anchor);
398
399 printf("\n");
400 }
401
402 void
print_tstats(struct pfr_tstats * ts,int debug)403 print_tstats(struct pfr_tstats *ts, int debug)
404 {
405 time_t time = ts->pfrts_tzero;
406 int dir, op;
407 char *ct;
408
409 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
410 return;
411 ct = ctime(&time);
412 print_table(&ts->pfrts_t, 1, debug);
413 printf("\tAddresses: %d\n", ts->pfrts_cnt);
414 if (ct)
415 printf("\tCleared: %s", ct);
416 else
417 printf("\tCleared: %lld\n", time);
418 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
419 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
420 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
421 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
422 (unsigned long long)ts->pfrts_nomatch,
423 (unsigned long long)ts->pfrts_match);
424 for (dir = 0; dir < PFR_DIR_MAX; dir++)
425 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
426 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
427 stats_text[dir][op],
428 (unsigned long long)ts->pfrts_packets[dir][op],
429 (unsigned long long)ts->pfrts_bytes[dir][op]);
430 }
431
432 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork,int opts)433 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
434 int nonetwork, int opts)
435 {
436 int ev = 0;
437 while (argc--)
438 if ((ev = append_addr(b, *argv++, nonetwork, opts)) == -1) {
439 if (errno)
440 warn("cannot decode %s", argv[-1]);
441 return (-1);
442 }
443 if (ev == 1) { /* expected further append_addr call */
444 warnx("failed to decode %s", argv[-1]);
445 return (-1);
446 }
447 if (pfr_buf_load(b, file, nonetwork, opts)) {
448 warn("cannot load %s", file);
449 return (-1);
450 }
451 return (0);
452 }
453
454 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)455 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
456 {
457 char ch, buf[256] = "{error}";
458 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
459 unsigned int fback, hostnet;
460
461 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
462 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
463 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
464 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
465 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
466 if (ad->pfra_net < hostnet)
467 printf("/%d", ad->pfra_net);
468 if (rad != NULL && fback != PFR_FB_NONE) {
469 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
470 errx(1, "print_addrx: strlcpy");
471 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
472 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
473 if (rad->pfra_net < hostnet)
474 printf("/%d", rad->pfra_net);
475 }
476 if (rad != NULL && fback == PFR_FB_NONE)
477 printf("\t nomatch");
478 if (dns && ad->pfra_net == hostnet) {
479 char host[NI_MAXHOST];
480 struct sockaddr_storage ss;
481
482 strlcpy(host, "?", sizeof(host));
483 bzero(&ss, sizeof(ss));
484 ss.ss_family = ad->pfra_af;
485 if (ss.ss_family == AF_INET) {
486 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
487
488 sin->sin_len = sizeof(*sin);
489 sin->sin_addr = ad->pfra_ip4addr;
490 } else {
491 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
492
493 sin6->sin6_len = sizeof(*sin6);
494 sin6->sin6_addr = ad->pfra_ip6addr;
495 }
496 if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
497 sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
498 printf("\t(%s)", host);
499 }
500 if (ad->pfra_ifname[0] != '\0')
501 printf("@%s", ad->pfra_ifname);
502 printf("\n");
503 }
504
505 void
print_astats(struct pfr_astats * as,int dns)506 print_astats(struct pfr_astats *as, int dns)
507 {
508 time_t time = as->pfras_tzero;
509 int dir, op;
510 char *ct;
511
512 ct = ctime(&time);
513 print_addrx(&as->pfras_a, NULL, dns);
514 if (ct)
515 printf("\tCleared: %s", ctime(&time));
516 else
517 printf("\tCleared: %lld\n", time);
518 if (as->pfras_a.pfra_states)
519 printf("\tActive States: %d\n", as->pfras_a.pfra_states);
520 if (as->pfras_a.pfra_type == PFRKE_COST)
521 printf("\tWeight: %d\n", as->pfras_a.pfra_weight);
522 if (as->pfras_a.pfra_ifname[0])
523 printf("\tInterface: %s\n", as->pfras_a.pfra_ifname);
524 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
525 return;
526 for (dir = 0; dir < PFR_DIR_MAX; dir++)
527 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
528 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
529 stats_text[dir][op],
530 (unsigned long long)as->pfras_packets[dir][op],
531 (unsigned long long)as->pfras_bytes[dir][op]);
532 }
533
534 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)535 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
536 struct pfr_buffer *ab, u_int32_t ticket, struct pfr_uktable *ukt)
537 {
538 struct pfr_table tbl_buf;
539 struct pfr_table *tbl;
540
541 if (ukt == NULL) {
542 bzero(&tbl_buf, sizeof(tbl_buf));
543 tbl = &tbl_buf;
544 } else {
545 if (ab->pfrb_size != 0) {
546 /*
547 * copy IP addresses which come with table from
548 * temporal buffer to buffer attached to table.
549 */
550 ukt->pfrukt_addrs = *ab;
551 ab->pfrb_size = 0;
552 ab->pfrb_msize = 0;
553 ab->pfrb_caddr = NULL;
554 } else
555 memset(&ukt->pfrukt_addrs, 0,
556 sizeof(struct pfr_buffer));
557
558 tbl = &ukt->pfrukt_t;
559 }
560
561 if (strlcpy(tbl->pfrt_name, name, sizeof(tbl->pfrt_name)) >=
562 sizeof(tbl->pfrt_name) || strlcpy(tbl->pfrt_anchor, anchor,
563 sizeof(tbl->pfrt_anchor)) >= sizeof(tbl->pfrt_anchor))
564 errx(1, "%s: strlcpy", __func__);
565 tbl->pfrt_flags = flags;
566 DBGPRINT("%s %s@%s [%x]\n", __func__, tbl->pfrt_name,
567 tbl->pfrt_anchor, tbl->pfrt_flags);
568
569 /*
570 * non-root anchors processed by parse.y are loaded to kernel later.
571 * Here we load tables, which are either created for root anchor
572 * or by 'pfctl -t ... -T ...' command.
573 */
574 if (ukt != NULL)
575 return (0);
576
577 return pfr_ina_define(tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
578 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
579 }
580
581 void
warn_duplicate_tables(const char * tablename,const char * anchorname)582 warn_duplicate_tables(const char *tablename, const char *anchorname)
583 {
584 struct pfr_buffer b;
585 struct pfr_table *t;
586
587 bzero(&b, sizeof(b));
588 b.pfrb_type = PFRB_TABLES;
589 for (;;) {
590 pfr_buf_grow(&b, b.pfrb_size);
591 b.pfrb_size = b.pfrb_msize;
592 if (pfr_get_tables(NULL, b.pfrb_caddr,
593 &b.pfrb_size, PFR_FLAG_ALLRSETS))
594 err(1, "pfr_get_tables");
595 if (b.pfrb_size <= b.pfrb_msize)
596 break;
597 }
598 PFRB_FOREACH(t, &b) {
599 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
600 continue;
601 if (!strcmp(anchorname, t->pfrt_anchor))
602 continue;
603 if (!strcmp(tablename, t->pfrt_name))
604 warnx("warning: table <%s> already defined"
605 " in anchor \"%s\"", tablename,
606 t->pfrt_anchor[0] ? t->pfrt_anchor : "/");
607 }
608 pfr_buf_clear(&b);
609 }
610
611 void
xprintf(int opts,const char * fmt,...)612 xprintf(int opts, const char *fmt, ...)
613 {
614 va_list args;
615
616 if (opts & PF_OPT_QUIET)
617 return;
618
619 va_start(args, fmt);
620 vfprintf(stderr, fmt, args);
621 va_end(args);
622
623 if (opts & PF_OPT_DUMMYACTION)
624 fprintf(stderr, " (dummy).\n");
625 else if (opts & PF_OPT_NOACTION)
626 fprintf(stderr, " (syntax only).\n");
627 else
628 fprintf(stderr, ".\n");
629 }
630
631
632 /* interface stuff */
633
634 void
pfctl_show_ifaces(const char * filter,int opts)635 pfctl_show_ifaces(const char *filter, int opts)
636 {
637 struct pfr_buffer b;
638 struct pfi_kif *p;
639
640 bzero(&b, sizeof(b));
641 b.pfrb_type = PFRB_IFACES;
642 for (;;) {
643 pfr_buf_grow(&b, 0);
644 b.pfrb_size = b.pfrb_msize;
645 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size))
646 errx(1, "%s", pf_strerror(errno));
647 if (b.pfrb_size < b.pfrb_msize)
648 break;
649 }
650 if (opts & PF_OPT_SHOWALL)
651 pfctl_print_title("INTERFACES:");
652 PFRB_FOREACH(p, &b)
653 print_iface(p, opts);
654 }
655
656 void
print_iface(struct pfi_kif * p,int opts)657 print_iface(struct pfi_kif *p, int opts)
658 {
659 time_t tzero = p->pfik_tzero;
660 int i, af, dir, act;
661 char *ct;
662
663 printf("%s", p->pfik_name);
664 if (opts & PF_OPT_VERBOSE) {
665 if (p->pfik_flags & PFI_IFLAG_SKIP)
666 printf(" (skip)");
667 }
668 printf("\n");
669
670 if (!(opts & PF_OPT_VERBOSE2))
671 return;
672
673 ct = ctime(&tzero);
674 if (ct)
675 printf("\tCleared: %s", ct);
676 else
677 printf("\tCleared: %lld\n", tzero);
678 printf("\tReferences: [ States: %-18d Rules: %-18d ]\n",
679 p->pfik_states, p->pfik_rules);
680 for (i = 0; i < 8; i++) {
681 af = (i>>2) & 1;
682 dir = (i>>1) &1;
683 act = i & 1;
684 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
685 istats_text[af][dir][act],
686 (unsigned long long)p->pfik_packets[af][dir][act],
687 (unsigned long long)p->pfik_bytes[af][dir][act]);
688 }
689 }
690