1 /*
2 * Copyright (c) 2014 Yandex LLC
3 * Copyright (c) 2014 Alexander V. Chernikov
4 *
5 * Redistribution and use in source forms, with and without modification,
6 * are permitted provided that this entire comment appears intact.
7 *
8 * Redistribution in binary form may occur without any restrictions.
9 * Obviously, it would be nice if you gave credit where credit is due
10 * but requiring it would be too onerous.
11 *
12 * This software is provided ``AS IS'' without any warranties of any kind.
13 *
14 * in-kernel ipfw tables support.
15 */
16
17
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <sys/sysctl.h>
22
23 #include <ctype.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <netdb.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sysexits.h>
31
32 #include <net/ethernet.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <netinet/ip_fw.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38
39 #include "ipfw2.h"
40
41 static void table_modify_record(ipfw_obj_header *oh, int ac, char *av[],
42 int add, int quiet, int update, int atomic);
43 static int table_flush(ipfw_obj_header *oh);
44 static int table_destroy(ipfw_obj_header *oh);
45 static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i);
46 static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i);
47 static int table_do_swap(ipfw_obj_header *oh, char *second);
48 static void table_create(ipfw_obj_header *oh, int ac, char *av[]);
49 static void table_modify(ipfw_obj_header *oh, int ac, char *av[]);
50 static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]);
51 static void table_lock(ipfw_obj_header *oh, int lock);
52 static int table_swap(ipfw_obj_header *oh, char *second);
53 static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i);
54 static int table_show_info(ipfw_xtable_info *i, void *arg);
55
56 static int table_destroy_one(ipfw_xtable_info *i, void *arg);
57 static int table_flush_one(ipfw_xtable_info *i, void *arg);
58 static int table_show_one(ipfw_xtable_info *i, void *arg);
59 static int table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh);
60 static void table_show_list(ipfw_obj_header *oh, int need_header);
61 static void table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent);
62
63 static void tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
64 char *key, int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi);
65 static void tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
66 char *arg, uint8_t type, uint32_t vmask);
67 static void table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
68 uint32_t vmask, int print_ip);
69
70 typedef int (table_cb_t)(ipfw_xtable_info *i, void *arg);
71 static int tables_foreach(table_cb_t *f, void *arg, int sort);
72
73 #ifndef s6_addr32
74 #define s6_addr32 __u6_addr.__u6_addr32
75 #endif
76
77 static struct _s_x tabletypes[] = {
78 { "addr", IPFW_TABLE_ADDR },
79 { "mac", IPFW_TABLE_MAC },
80 { "iface", IPFW_TABLE_INTERFACE },
81 { "number", IPFW_TABLE_NUMBER },
82 { "flow", IPFW_TABLE_FLOW },
83 { NULL, 0 }
84 };
85
86 /* Default algorithms for various table types */
87 static struct _s_x tablealgos[] = {
88 { "addr:radix", IPFW_TABLE_ADDR },
89 { "flow:hash", IPFW_TABLE_FLOW },
90 { "iface:array", IPFW_TABLE_INTERFACE },
91 { "number:array", IPFW_TABLE_NUMBER },
92 { NULL, 0 }
93 };
94
95 static struct _s_x tablevaltypes[] = {
96 { "skipto", IPFW_VTYPE_SKIPTO },
97 { "pipe", IPFW_VTYPE_PIPE },
98 { "fib", IPFW_VTYPE_FIB },
99 { "nat", IPFW_VTYPE_NAT },
100 { "dscp", IPFW_VTYPE_DSCP },
101 { "tag", IPFW_VTYPE_TAG },
102 { "divert", IPFW_VTYPE_DIVERT },
103 { "netgraph", IPFW_VTYPE_NETGRAPH },
104 { "limit", IPFW_VTYPE_LIMIT },
105 { "ipv4", IPFW_VTYPE_NH4 },
106 { "ipv6", IPFW_VTYPE_NH6 },
107 { NULL, 0 }
108 };
109
110 static struct _s_x tablecmds[] = {
111 { "add", TOK_ADD },
112 { "delete", TOK_DEL },
113 { "create", TOK_CREATE },
114 { "destroy", TOK_DESTROY },
115 { "flush", TOK_FLUSH },
116 { "modify", TOK_MODIFY },
117 { "swap", TOK_SWAP },
118 { "info", TOK_INFO },
119 { "detail", TOK_DETAIL },
120 { "list", TOK_LIST },
121 { "lookup", TOK_LOOKUP },
122 { "atomic", TOK_ATOMIC },
123 { "lock", TOK_LOCK },
124 { "unlock", TOK_UNLOCK },
125 { NULL, 0 }
126 };
127
128 static int
lookup_host(char * host,struct in_addr * ipaddr)129 lookup_host (char *host, struct in_addr *ipaddr)
130 {
131 struct hostent *he;
132
133 if (!inet_aton(host, ipaddr)) {
134 if ((he = gethostbyname(host)) == NULL)
135 return(-1);
136 *ipaddr = *(struct in_addr *)he->h_addr_list[0];
137 }
138 return(0);
139 }
140
141 /*
142 * This one handles all table-related commands
143 * ipfw table NAME create ...
144 * ipfw table NAME modify ...
145 * ipfw table {NAME | all} destroy
146 * ipfw table NAME swap NAME
147 * ipfw table NAME lock
148 * ipfw table NAME unlock
149 * ipfw table NAME add addr[/masklen] [value]
150 * ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
151 * ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
152 * ipfw table NAME lookup addr
153 * ipfw table {NAME | all} flush
154 * ipfw table {NAME | all} list
155 * ipfw table {NAME | all} info
156 * ipfw table {NAME | all} detail
157 */
158 void
ipfw_table_handler(int ac,char * av[])159 ipfw_table_handler(int ac, char *av[])
160 {
161 int do_add, is_all;
162 int atomic, error, tcmd;
163 ipfw_xtable_info i;
164 ipfw_obj_header oh;
165 char *tablename;
166 uint8_t set;
167 void *arg;
168
169 memset(&oh, 0, sizeof(oh));
170 is_all = 0;
171 if (g_co.use_set != 0)
172 set = g_co.use_set - 1;
173 else
174 set = 0;
175
176 ac--; av++;
177 NEED1("table needs name");
178 tablename = *av;
179
180 if (table_check_name(tablename) == 0) {
181 table_fill_ntlv(&oh.ntlv, *av, set, 1);
182 oh.idx = 1;
183 } else {
184 if (strcmp(tablename, "all") == 0)
185 is_all = 1;
186 else
187 errx(EX_USAGE, "table name %s is invalid", tablename);
188 }
189 ac--; av++;
190 NEED1("table needs command");
191
192 tcmd = get_token(tablecmds, *av, "table command");
193 /* Check if atomic operation was requested */
194 atomic = 0;
195 if (tcmd == TOK_ATOMIC) {
196 ac--; av++;
197 NEED1("atomic needs command");
198 tcmd = get_token(tablecmds, *av, "table command");
199 switch (tcmd) {
200 case TOK_ADD:
201 break;
202 default:
203 errx(EX_USAGE, "atomic is not compatible with %s", *av);
204 }
205 atomic = 1;
206 }
207
208 switch (tcmd) {
209 case TOK_LIST:
210 case TOK_INFO:
211 case TOK_DETAIL:
212 case TOK_FLUSH:
213 case TOK_DESTROY:
214 break;
215 default:
216 if (is_all != 0)
217 errx(EX_USAGE, "table name required");
218 }
219
220 switch (tcmd) {
221 case TOK_ADD:
222 case TOK_DEL:
223 do_add = **av == 'a';
224 ac--; av++;
225 table_modify_record(&oh, ac, av, do_add, g_co.do_quiet,
226 g_co.do_quiet, atomic);
227 break;
228 case TOK_CREATE:
229 ac--; av++;
230 table_create(&oh, ac, av);
231 break;
232 case TOK_MODIFY:
233 ac--; av++;
234 table_modify(&oh, ac, av);
235 break;
236 case TOK_DESTROY:
237 if (is_all == 0) {
238 if (table_destroy(&oh) == 0)
239 break;
240 if (errno != ESRCH)
241 err(EX_OSERR, "failed to destroy table %s",
242 tablename);
243 /* ESRCH isn't fatal, warn if not quiet mode */
244 if (g_co.do_quiet == 0)
245 warn("failed to destroy table %s", tablename);
246 } else {
247 error = tables_foreach(table_destroy_one, &oh, 1);
248 if (error != 0)
249 err(EX_OSERR,
250 "failed to destroy tables list");
251 }
252 break;
253 case TOK_FLUSH:
254 if (is_all == 0) {
255 if ((error = table_flush(&oh)) == 0)
256 break;
257 if (errno != ESRCH)
258 err(EX_OSERR, "failed to flush table %s info",
259 tablename);
260 /* ESRCH isn't fatal, warn if not quiet mode */
261 if (g_co.do_quiet == 0)
262 warn("failed to flush table %s info",
263 tablename);
264 } else {
265 error = tables_foreach(table_flush_one, &oh, 1);
266 if (error != 0)
267 err(EX_OSERR, "failed to flush tables list");
268 /* XXX: we ignore errors here */
269 }
270 break;
271 case TOK_SWAP:
272 ac--; av++;
273 NEED1("second table name required");
274 table_swap(&oh, *av);
275 break;
276 case TOK_LOCK:
277 case TOK_UNLOCK:
278 table_lock(&oh, (tcmd == TOK_LOCK));
279 break;
280 case TOK_DETAIL:
281 case TOK_INFO:
282 arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL;
283 if (is_all == 0) {
284 if ((error = table_get_info(&oh, &i)) != 0)
285 err(EX_OSERR, "failed to request table info");
286 table_show_info(&i, arg);
287 } else {
288 error = tables_foreach(table_show_info, arg, 1);
289 if (error != 0)
290 err(EX_OSERR, "failed to request tables list");
291 }
292 break;
293 case TOK_LIST:
294 arg = is_all ? (void*)1 : NULL;
295 if (is_all == 0) {
296 if ((error = table_get_info(&oh, &i)) != 0)
297 err(EX_OSERR, "failed to request table info");
298 table_show_one(&i, arg);
299 } else {
300 error = tables_foreach(table_show_one, arg, 1);
301 if (error != 0)
302 err(EX_OSERR, "failed to request tables list");
303 }
304 break;
305 case TOK_LOOKUP:
306 ac--; av++;
307 table_lookup(&oh, ac, av);
308 break;
309 }
310 }
311
312 void
table_fill_ntlv(ipfw_obj_ntlv * ntlv,const char * name,uint8_t set,uint16_t uidx)313 table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
314 uint16_t uidx)
315 {
316
317 ntlv->head.type = IPFW_TLV_TBL_NAME;
318 ntlv->head.length = sizeof(ipfw_obj_ntlv);
319 ntlv->idx = uidx;
320 ntlv->set = set;
321 strlcpy(ntlv->name, name, sizeof(ntlv->name));
322 }
323
324 static void
table_fill_objheader(ipfw_obj_header * oh,ipfw_xtable_info * i)325 table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
326 {
327
328 oh->idx = 1;
329 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
330 }
331
332 static struct _s_x tablenewcmds[] = {
333 { "type", TOK_TYPE },
334 { "valtype", TOK_VALTYPE },
335 { "algo", TOK_ALGO },
336 { "limit", TOK_LIMIT },
337 { "locked", TOK_LOCK },
338 { "missing", TOK_MISSING },
339 { "or-flush", TOK_ORFLUSH },
340 { NULL, 0 }
341 };
342
343 static struct _s_x flowtypecmds[] = {
344 { "src-ip", IPFW_TFFLAG_SRCIP },
345 { "proto", IPFW_TFFLAG_PROTO },
346 { "src-port", IPFW_TFFLAG_SRCPORT },
347 { "dst-ip", IPFW_TFFLAG_DSTIP },
348 { "dst-port", IPFW_TFFLAG_DSTPORT },
349 { NULL, 0 }
350 };
351
352 static int
table_parse_type(uint8_t ttype,char * p,uint8_t * tflags)353 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
354 {
355 uint32_t fset, fclear;
356 char *e;
357
358 /* Parse type options */
359 switch(ttype) {
360 case IPFW_TABLE_FLOW:
361 fset = fclear = 0;
362 if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
363 errx(EX_USAGE,
364 "unable to parse flow option %s", e);
365 *tflags = fset;
366 break;
367 default:
368 return (EX_USAGE);
369 }
370
371 return (0);
372 }
373
374 static void
table_print_type(char * tbuf,size_t size,uint8_t type,uint8_t tflags)375 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
376 {
377 const char *tname;
378 int l;
379
380 if ((tname = match_value(tabletypes, type)) == NULL)
381 tname = "unknown";
382
383 l = snprintf(tbuf, size, "%s", tname);
384 tbuf += l;
385 size -= l;
386
387 switch(type) {
388 case IPFW_TABLE_FLOW:
389 if (tflags != 0) {
390 *tbuf++ = ':';
391 l--;
392 print_flags_buffer(tbuf, size, flowtypecmds, tflags);
393 }
394 break;
395 }
396 }
397
398 /*
399 * Creates new table
400 *
401 * ipfw table NAME create [ type { addr | iface | number | flow } ]
402 * [ algo algoname ] [missing] [or-flush]
403 */
404 static void
table_create(ipfw_obj_header * oh,int ac,char * av[])405 table_create(ipfw_obj_header *oh, int ac, char *av[])
406 {
407 ipfw_xtable_info xi, xie;
408 int error, missing, orflush, tcmd, val;
409 uint32_t fset, fclear;
410 char *e, *p;
411 char tbuf[128];
412
413 missing = orflush = 0;
414 memset(&xi, 0, sizeof(xi));
415 while (ac > 0) {
416 tcmd = get_token(tablenewcmds, *av, "option");
417 ac--; av++;
418
419 switch (tcmd) {
420 case TOK_LIMIT:
421 NEED1("limit value required");
422 xi.limit = strtol(*av, NULL, 10);
423 ac--; av++;
424 break;
425 case TOK_TYPE:
426 NEED1("table type required");
427 /* Type may have suboptions after ':' */
428 if ((p = strchr(*av, ':')) != NULL)
429 *p++ = '\0';
430 val = match_token(tabletypes, *av);
431 if (val == -1) {
432 concat_tokens(tbuf, sizeof(tbuf), tabletypes,
433 ", ");
434 errx(EX_USAGE,
435 "Unknown tabletype: %s. Supported: %s",
436 *av, tbuf);
437 }
438 xi.type = val;
439 if (p != NULL) {
440 error = table_parse_type(val, p, &xi.tflags);
441 if (error != 0)
442 errx(EX_USAGE,
443 "Unsupported suboptions: %s", p);
444 }
445 ac--; av++;
446 break;
447 case TOK_VALTYPE:
448 NEED1("table value type required");
449 fset = fclear = 0;
450 val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
451 if (val != -1) {
452 xi.vmask = fset;
453 ac--; av++;
454 break;
455 }
456 concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
457 errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
458 e, tbuf);
459 break;
460 case TOK_ALGO:
461 NEED1("table algorithm name required");
462 if (strlen(*av) > sizeof(xi.algoname))
463 errx(EX_USAGE, "algorithm name too long");
464 strlcpy(xi.algoname, *av, sizeof(xi.algoname));
465 ac--; av++;
466 break;
467 case TOK_LOCK:
468 xi.flags |= IPFW_TGFLAGS_LOCKED;
469 break;
470 case TOK_ORFLUSH:
471 orflush = 1;
472 /* FALLTHROUGH */
473 case TOK_MISSING:
474 missing = 1;
475 break;
476 }
477 }
478
479 /* Set some defaults to preserve compatibility. */
480 if (xi.algoname[0] == '\0') {
481 const char *algo;
482
483 if (xi.type == 0)
484 xi.type = IPFW_TABLE_ADDR;
485 algo = match_value(tablealgos, xi.type);
486 if (algo != NULL)
487 strlcpy(xi.algoname, algo, sizeof(xi.algoname));
488 }
489 if (xi.vmask == 0)
490 xi.vmask = IPFW_VTYPE_LEGACY;
491
492 error = table_do_create(oh, &xi);
493
494 if (error == 0)
495 return;
496
497 if (errno != EEXIST || missing == 0)
498 err(EX_OSERR, "Table creation failed");
499
500 /* Check that existing table is the same we are trying to create */
501 if (table_get_info(oh, &xie) != 0)
502 err(EX_OSERR, "Existing table check failed");
503
504 if (xi.limit != xie.limit || xi.type != xie.type ||
505 xi.tflags != xie.tflags || xi.vmask != xie.vmask || (
506 xi.algoname[0] != '\0' && strcmp(xi.algoname,
507 xie.algoname) != 0) || xi.flags != xie.flags)
508 errx(EX_DATAERR, "The existing table is not compatible "
509 "with one you are creating.");
510
511 /* Flush existing table if instructed to do so */
512 if (orflush != 0 && table_flush(oh) != 0)
513 err(EX_OSERR, "Table flush on creation failed");
514 }
515
516 /*
517 * Creates new table
518 *
519 * Request: [ ipfw_obj_header ipfw_xtable_info ]
520 *
521 * Returns 0 on success.
522 */
523 static int
table_do_create(ipfw_obj_header * oh,ipfw_xtable_info * i)524 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
525 {
526 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
527 int error;
528
529 memcpy(tbuf, oh, sizeof(*oh));
530 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
531 oh = (ipfw_obj_header *)tbuf;
532
533 error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
534
535 return (error);
536 }
537
538 /*
539 * Modifies existing table
540 *
541 * ipfw table NAME modify [ limit number ]
542 */
543 static void
table_modify(ipfw_obj_header * oh,int ac,char * av[])544 table_modify(ipfw_obj_header *oh, int ac, char *av[])
545 {
546 ipfw_xtable_info xi;
547 int tcmd;
548
549 memset(&xi, 0, sizeof(xi));
550
551 while (ac > 0) {
552 tcmd = get_token(tablenewcmds, *av, "option");
553 ac--; av++;
554
555 switch (tcmd) {
556 case TOK_LIMIT:
557 NEED1("limit value required");
558 xi.limit = strtol(*av, NULL, 10);
559 xi.mflags |= IPFW_TMFLAGS_LIMIT;
560 ac--; av++;
561 break;
562 default:
563 errx(EX_USAGE, "cmd is not supported for modification");
564 }
565 }
566
567 if (table_do_modify(oh, &xi) != 0)
568 err(EX_OSERR, "Table modification failed");
569 }
570
571 /*
572 * Modifies existing table.
573 *
574 * Request: [ ipfw_obj_header ipfw_xtable_info ]
575 *
576 * Returns 0 on success.
577 */
578 static int
table_do_modify(ipfw_obj_header * oh,ipfw_xtable_info * i)579 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
580 {
581 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
582 int error;
583
584 memcpy(tbuf, oh, sizeof(*oh));
585 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
586 oh = (ipfw_obj_header *)tbuf;
587
588 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
589
590 return (error);
591 }
592
593 /*
594 * Locks or unlocks given table
595 */
596 static void
table_lock(ipfw_obj_header * oh,int lock)597 table_lock(ipfw_obj_header *oh, int lock)
598 {
599 ipfw_xtable_info xi;
600
601 memset(&xi, 0, sizeof(xi));
602
603 xi.mflags |= IPFW_TMFLAGS_LOCK;
604 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
605
606 if (table_do_modify(oh, &xi) != 0)
607 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
608 }
609
610 /*
611 * Destroys given table specified by @oh->ntlv.
612 * Returns 0 on success.
613 */
614 static int
table_destroy(ipfw_obj_header * oh)615 table_destroy(ipfw_obj_header *oh)
616 {
617
618 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
619 return (-1);
620
621 return (0);
622 }
623
624 static int
table_destroy_one(ipfw_xtable_info * i,void * arg)625 table_destroy_one(ipfw_xtable_info *i, void *arg)
626 {
627 ipfw_obj_header *oh;
628
629 oh = (ipfw_obj_header *)arg;
630 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
631 if (table_destroy(oh) != 0) {
632 if (g_co.do_quiet == 0)
633 warn("failed to destroy table(%s) in set %u",
634 i->tablename, i->set);
635 return (-1);
636 }
637 return (0);
638 }
639
640 /*
641 * Flushes given table specified by @oh->ntlv.
642 * Returns 0 on success.
643 */
644 static int
table_flush(ipfw_obj_header * oh)645 table_flush(ipfw_obj_header *oh)
646 {
647
648 if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
649 return (-1);
650
651 return (0);
652 }
653
654 static int
table_do_swap(ipfw_obj_header * oh,char * second)655 table_do_swap(ipfw_obj_header *oh, char *second)
656 {
657 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
658 int error;
659
660 memset(tbuf, 0, sizeof(tbuf));
661 memcpy(tbuf, oh, sizeof(*oh));
662 oh = (ipfw_obj_header *)tbuf;
663 table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
664
665 error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
666
667 return (error);
668 }
669
670 /*
671 * Swaps given table with @second one.
672 */
673 static int
table_swap(ipfw_obj_header * oh,char * second)674 table_swap(ipfw_obj_header *oh, char *second)
675 {
676
677 if (table_check_name(second) != 0)
678 errx(EX_USAGE, "table name %s is invalid", second);
679
680 if (table_do_swap(oh, second) == 0)
681 return (0);
682
683 switch (errno) {
684 case EINVAL:
685 errx(EX_USAGE, "Unable to swap table: check types");
686 case EFBIG:
687 errx(EX_USAGE, "Unable to swap table: check limits");
688 }
689
690 return (0);
691 }
692
693
694 /*
695 * Retrieves table in given table specified by @oh->ntlv.
696 * it inside @i.
697 * Returns 0 on success.
698 */
699 static int
table_get_info(ipfw_obj_header * oh,ipfw_xtable_info * i)700 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
701 {
702 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
703 size_t sz;
704
705 sz = sizeof(tbuf);
706 memset(tbuf, 0, sizeof(tbuf));
707 memcpy(tbuf, oh, sizeof(*oh));
708 oh = (ipfw_obj_header *)tbuf;
709
710 if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
711 return (errno);
712
713 if (sz < sizeof(tbuf))
714 return (EINVAL);
715
716 *i = *(ipfw_xtable_info *)(oh + 1);
717
718 return (0);
719 }
720
721 static struct _s_x tablealgoclass[] = {
722 { "hash", IPFW_TACLASS_HASH },
723 { "array", IPFW_TACLASS_ARRAY },
724 { "radix", IPFW_TACLASS_RADIX },
725 { NULL, 0 }
726 };
727
728 struct ta_cldata {
729 uint8_t taclass;
730 uint8_t spare4;
731 uint16_t itemsize;
732 uint16_t itemsize6;
733 uint32_t size;
734 uint32_t count;
735 };
736
737 /*
738 * Print global/per-AF table @i algorithm info.
739 */
740 static void
table_show_tainfo(ipfw_xtable_info * i __unused,struct ta_cldata * d,const char * af,const char * taclass)741 table_show_tainfo(ipfw_xtable_info *i __unused, struct ta_cldata *d,
742 const char *af, const char *taclass)
743 {
744
745 switch (d->taclass) {
746 case IPFW_TACLASS_HASH:
747 case IPFW_TACLASS_ARRAY:
748 printf(" %salgorithm %s info\n", af, taclass);
749 if (d->itemsize == d->itemsize6)
750 printf(" size: %u items: %u itemsize: %u\n",
751 d->size, d->count, d->itemsize);
752 else
753 printf(" size: %u items: %u "
754 "itemsize4: %u itemsize6: %u\n",
755 d->size, d->count,
756 d->itemsize, d->itemsize6);
757 break;
758 case IPFW_TACLASS_RADIX:
759 printf(" %salgorithm %s info\n", af, taclass);
760 if (d->itemsize == d->itemsize6)
761 printf(" items: %u itemsize: %u\n",
762 d->count, d->itemsize);
763 else
764 printf(" items: %u "
765 "itemsize4: %u itemsize6: %u\n",
766 d->count, d->itemsize, d->itemsize6);
767 break;
768 default:
769 printf(" algo class: %s\n", taclass);
770 }
771 }
772
773 static void
table_print_valheader(char * buf,size_t bufsize,uint32_t vmask)774 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
775 {
776
777 if (vmask == IPFW_VTYPE_LEGACY) {
778 snprintf(buf, bufsize, "legacy");
779 return;
780 }
781
782 memset(buf, 0, bufsize);
783 print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
784 }
785
786 /*
787 * Prints table info struct @i in human-readable form.
788 */
789 static int
table_show_info(ipfw_xtable_info * i,void * arg)790 table_show_info(ipfw_xtable_info *i, void *arg)
791 {
792 const char *vtype;
793 ipfw_ta_tinfo *tainfo;
794 int afdata, afitem;
795 struct ta_cldata d;
796 char ttype[64], tvtype[64];
797
798 table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
799 table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
800
801 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
802 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
803 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
804 else
805 printf(" kindex: %d, type: %s\n", i->kidx, ttype);
806 printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
807 printf(" algorithm: %s\n", i->algoname);
808 printf(" items: %u, size: %u\n", i->count, i->size);
809 if (i->limit > 0)
810 printf(" limit: %u\n", i->limit);
811
812 /* Print algo-specific info if requested & set */
813 if (arg == NULL)
814 return (0);
815
816 if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
817 return (0);
818 tainfo = &i->ta_info;
819
820 afdata = 0;
821 afitem = 0;
822 if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
823 afdata = 1;
824 if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
825 afitem = 1;
826
827 memset(&d, 0, sizeof(d));
828 d.taclass = tainfo->taclass4;
829 d.size = tainfo->size4;
830 d.count = tainfo->count4;
831 d.itemsize = tainfo->itemsize4;
832 if (afdata == 0 && afitem != 0)
833 d.itemsize6 = tainfo->itemsize6;
834 else
835 d.itemsize6 = d.itemsize;
836 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
837 vtype = "unknown";
838
839 if (afdata == 0) {
840 table_show_tainfo(i, &d, "", vtype);
841 } else {
842 table_show_tainfo(i, &d, "IPv4 ", vtype);
843 memset(&d, 0, sizeof(d));
844 d.taclass = tainfo->taclass6;
845 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
846 vtype = "unknown";
847 d.size = tainfo->size6;
848 d.count = tainfo->count6;
849 d.itemsize = tainfo->itemsize6;
850 d.itemsize6 = d.itemsize;
851 table_show_tainfo(i, &d, "IPv6 ", vtype);
852 }
853
854 return (0);
855 }
856
857
858 /*
859 * Function wrappers which can be used either
860 * as is or as foreach function parameter.
861 */
862
863 static int
table_show_one(ipfw_xtable_info * i,void * arg)864 table_show_one(ipfw_xtable_info *i, void *arg)
865 {
866 ipfw_obj_header *oh = NULL;
867 int error;
868 int is_all;
869
870 is_all = arg == NULL ? 0 : 1;
871
872 if ((error = table_do_get_list(i, &oh)) != 0) {
873 err(EX_OSERR, "Error requesting table %s list", i->tablename);
874 return (error);
875 }
876
877 table_show_list(oh, is_all);
878
879 free(oh);
880 return (0);
881 }
882
883 static int
table_flush_one(ipfw_xtable_info * i,void * arg)884 table_flush_one(ipfw_xtable_info *i, void *arg)
885 {
886 ipfw_obj_header *oh;
887
888 oh = (ipfw_obj_header *)arg;
889
890 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
891
892 return (table_flush(oh));
893 }
894
895 static int
table_do_modify_record(int cmd,ipfw_obj_header * oh,ipfw_obj_tentry * tent,int count,int atomic)896 table_do_modify_record(int cmd, ipfw_obj_header *oh,
897 ipfw_obj_tentry *tent, int count, int atomic)
898 {
899 ipfw_obj_ctlv *ctlv;
900 ipfw_obj_tentry *tent_base;
901 caddr_t pbuf;
902 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
903 int error, i;
904 size_t sz;
905
906 sz = sizeof(*ctlv) + sizeof(*tent) * count;
907 if (count == 1) {
908 memset(xbuf, 0, sizeof(xbuf));
909 pbuf = xbuf;
910 } else {
911 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
912 return (ENOMEM);
913 }
914
915 memcpy(pbuf, oh, sizeof(*oh));
916 oh = (ipfw_obj_header *)pbuf;
917 oh->opheader.version = 1;
918
919 ctlv = (ipfw_obj_ctlv *)(oh + 1);
920 ctlv->count = count;
921 ctlv->head.length = sz;
922 if (atomic != 0)
923 ctlv->flags |= IPFW_CTF_ATOMIC;
924
925 tent_base = tent;
926 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
927 tent = (ipfw_obj_tentry *)(ctlv + 1);
928 for (i = 0; i < count; i++, tent++) {
929 tent->head.length = sizeof(ipfw_obj_tentry);
930 tent->idx = oh->idx;
931 }
932
933 sz += sizeof(*oh);
934 error = do_get3(cmd, &oh->opheader, &sz);
935 if (error != 0)
936 error = errno;
937 tent = (ipfw_obj_tentry *)(ctlv + 1);
938 /* Copy result back to provided buffer */
939 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
940
941 if (pbuf != xbuf)
942 free(pbuf);
943
944 return (error);
945 }
946
947 static void
table_modify_record(ipfw_obj_header * oh,int ac,char * av[],int add,int quiet,int update,int atomic)948 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
949 int quiet, int update, int atomic)
950 {
951 ipfw_obj_tentry *ptent, tent, *tent_buf;
952 ipfw_xtable_info xi;
953 const char *etxt, *px, *texterr;
954 uint8_t type;
955 uint32_t vmask;
956 int cmd, count, error, i, ignored;
957
958 if (ac == 0)
959 errx(EX_USAGE, "address required");
960
961 if (add != 0) {
962 cmd = IP_FW_TABLE_XADD;
963 texterr = "Adding record failed";
964 } else {
965 cmd = IP_FW_TABLE_XDEL;
966 texterr = "Deleting record failed";
967 }
968
969 /*
970 * Calculate number of entries:
971 * Assume [key val] x N for add
972 * and
973 * key x N for delete
974 */
975 count = (add != 0) ? ac / 2 + 1 : ac;
976
977 if (count <= 1) {
978 /* Adding single entry with/without value */
979 memset(&tent, 0, sizeof(tent));
980 tent_buf = &tent;
981 } else {
982
983 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
984 errx(EX_OSERR,
985 "Unable to allocate memory for all entries");
986 }
987 ptent = tent_buf;
988
989 memset(&xi, 0, sizeof(xi));
990 count = 0;
991 while (ac > 0) {
992 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
993
994 /*
995 * Compatibility layer: auto-create table if not exists.
996 */
997 if (xi.tablename[0] == '\0') {
998 xi.type = type;
999 xi.vmask = vmask;
1000 strlcpy(xi.tablename, oh->ntlv.name,
1001 sizeof(xi.tablename));
1002 if (quiet == 0)
1003 warnx("DEPRECATED: inserting data into "
1004 "non-existent table %s. (auto-created)",
1005 xi.tablename);
1006 table_do_create(oh, &xi);
1007 }
1008
1009 oh->ntlv.type = type;
1010 ac--; av++;
1011
1012 if (add != 0 && ac > 0) {
1013 tentry_fill_value(oh, ptent, *av, type, vmask);
1014 ac--; av++;
1015 }
1016
1017 if (update != 0)
1018 ptent->head.flags |= IPFW_TF_UPDATE;
1019
1020 count++;
1021 ptent++;
1022 }
1023
1024 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
1025
1026 /*
1027 * Compatibility stuff: do not yell on duplicate keys or
1028 * failed deletions.
1029 */
1030 if (error == 0 || (error == EEXIST && add != 0) ||
1031 (error == ENOENT && add == 0)) {
1032 if (quiet != 0) {
1033 if (tent_buf != &tent)
1034 free(tent_buf);
1035 return;
1036 }
1037 }
1038
1039 /* Report results back */
1040 ptent = tent_buf;
1041 for (i = 0; i < count; ptent++, i++) {
1042 ignored = 0;
1043 switch (ptent->result) {
1044 case IPFW_TR_ADDED:
1045 px = "added";
1046 break;
1047 case IPFW_TR_DELETED:
1048 px = "deleted";
1049 break;
1050 case IPFW_TR_UPDATED:
1051 px = "updated";
1052 break;
1053 case IPFW_TR_LIMIT:
1054 px = "limit";
1055 ignored = 1;
1056 break;
1057 case IPFW_TR_ERROR:
1058 px = "error";
1059 ignored = 1;
1060 break;
1061 case IPFW_TR_NOTFOUND:
1062 px = "notfound";
1063 ignored = 1;
1064 break;
1065 case IPFW_TR_EXISTS:
1066 px = "exists";
1067 ignored = 1;
1068 break;
1069 case IPFW_TR_IGNORED:
1070 px = "ignored";
1071 ignored = 1;
1072 break;
1073 default:
1074 px = "unknown";
1075 ignored = 1;
1076 }
1077
1078 if (error != 0 && atomic != 0 && ignored == 0)
1079 printf("%s(reverted): ", px);
1080 else
1081 printf("%s: ", px);
1082
1083 table_show_entry(&xi, ptent);
1084 }
1085
1086 if (tent_buf != &tent)
1087 free(tent_buf);
1088
1089 if (error == 0)
1090 return;
1091 /* Get real OS error */
1092 error = errno;
1093
1094 /* Try to provide more human-readable error */
1095 switch (error) {
1096 case EEXIST:
1097 etxt = "record already exists";
1098 break;
1099 case EFBIG:
1100 etxt = "limit hit";
1101 break;
1102 case ESRCH:
1103 etxt = "table not found";
1104 break;
1105 case ENOENT:
1106 etxt = "record not found";
1107 break;
1108 case EACCES:
1109 etxt = "table is locked";
1110 break;
1111 default:
1112 etxt = strerror(error);
1113 }
1114
1115 errx(EX_OSERR, "%s: %s", texterr, etxt);
1116 }
1117
1118 static int
table_do_lookup(ipfw_obj_header * oh,char * key,ipfw_xtable_info * xi,ipfw_obj_tentry * xtent)1119 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1120 ipfw_obj_tentry *xtent)
1121 {
1122 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1123 ipfw_obj_tentry *tent;
1124 uint8_t type;
1125 uint32_t vmask;
1126 size_t sz;
1127
1128 memcpy(xbuf, oh, sizeof(*oh));
1129 oh = (ipfw_obj_header *)xbuf;
1130 tent = (ipfw_obj_tentry *)(oh + 1);
1131
1132 memset(tent, 0, sizeof(*tent));
1133 tent->head.length = sizeof(*tent);
1134 tent->idx = 1;
1135
1136 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1137 oh->ntlv.type = type;
1138
1139 sz = sizeof(xbuf);
1140 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1141 return (errno);
1142
1143 if (sz < sizeof(xbuf))
1144 return (EINVAL);
1145
1146 *xtent = *tent;
1147
1148 return (0);
1149 }
1150
1151 static void
table_lookup(ipfw_obj_header * oh,int ac,char * av[])1152 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1153 {
1154 ipfw_obj_tentry xtent;
1155 ipfw_xtable_info xi;
1156 char key[64];
1157 int error;
1158
1159 if (ac == 0)
1160 errx(EX_USAGE, "address required");
1161
1162 strlcpy(key, *av, sizeof(key));
1163
1164 memset(&xi, 0, sizeof(xi));
1165 error = table_do_lookup(oh, key, &xi, &xtent);
1166
1167 switch (error) {
1168 case 0:
1169 break;
1170 case ESRCH:
1171 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1172 case ENOENT:
1173 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1174 case ENOTSUP:
1175 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1176 "\"lookup\" method", oh->ntlv.name);
1177 default:
1178 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1179 }
1180
1181 table_show_entry(&xi, &xtent);
1182 }
1183
1184 static void
tentry_fill_key_type(char * arg,ipfw_obj_tentry * tentry,uint8_t type,uint8_t tflags)1185 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1186 uint8_t tflags)
1187 {
1188 char *p, *pp;
1189 int mask, af;
1190 struct in6_addr *paddr, tmp;
1191 struct ether_addr *mac;
1192 struct tflow_entry *tfe;
1193 uint32_t key, *pkey;
1194 uint16_t port;
1195 struct protoent *pent;
1196 struct servent *sent;
1197 int masklen;
1198
1199 mask = masklen = 0;
1200 af = 0;
1201 paddr = (struct in6_addr *)&tentry->k;
1202
1203 switch (type) {
1204 case IPFW_TABLE_ADDR:
1205 /* Remove / if exists */
1206 if ((p = strchr(arg, '/')) != NULL) {
1207 *p = '\0';
1208 mask = atoi(p + 1);
1209 }
1210
1211 if (inet_pton(AF_INET, arg, paddr) == 1) {
1212 if (p != NULL && mask > 32)
1213 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1214 p + 1);
1215
1216 masklen = p ? mask : 32;
1217 af = AF_INET;
1218 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1219 if (IN6_IS_ADDR_V4COMPAT(paddr))
1220 errx(EX_DATAERR,
1221 "Use IPv4 instead of v4-compatible");
1222 if (p != NULL && mask > 128)
1223 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1224 p + 1);
1225
1226 masklen = p ? mask : 128;
1227 af = AF_INET6;
1228 } else {
1229 /* Assume FQDN */
1230 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1231 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1232
1233 masklen = 32;
1234 type = IPFW_TABLE_ADDR;
1235 af = AF_INET;
1236 }
1237 break;
1238 case IPFW_TABLE_MAC:
1239 /* Remove / if exists */
1240 if ((p = strchr(arg, '/')) != NULL) {
1241 *p = '\0';
1242 mask = atoi(p + 1);
1243 }
1244
1245 if (p != NULL && mask > 8 * ETHER_ADDR_LEN)
1246 errx(EX_DATAERR, "bad MAC mask width: %s",
1247 p + 1);
1248
1249 if ((mac = ether_aton(arg)) == NULL)
1250 errx(EX_DATAERR, "Incorrect MAC address");
1251
1252 memcpy(tentry->k.mac, mac->octet, ETHER_ADDR_LEN);
1253 masklen = p ? mask : 8 * ETHER_ADDR_LEN;
1254 af = AF_LINK;
1255 break;
1256 case IPFW_TABLE_INTERFACE:
1257 /* Assume interface name. Copy significant data only */
1258 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1259 memcpy(paddr, arg, mask);
1260 /* Set mask to exact match */
1261 masklen = 8 * IF_NAMESIZE;
1262 break;
1263 case IPFW_TABLE_NUMBER:
1264 /* Port or any other key */
1265 key = strtol(arg, &p, 10);
1266 if (*p != '\0')
1267 errx(EX_DATAERR, "Invalid number: %s", arg);
1268
1269 pkey = (uint32_t *)paddr;
1270 *pkey = key;
1271 masklen = 32;
1272 break;
1273 case IPFW_TABLE_FLOW:
1274 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1275 tfe = &tentry->k.flow;
1276 af = 0;
1277
1278 /* Handle <ipv4|ipv6> */
1279 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1280 if ((p = strchr(arg, ',')) != NULL)
1281 *p++ = '\0';
1282 /* Determine family using temporary storage */
1283 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1284 if (af != 0 && af != AF_INET)
1285 errx(EX_DATAERR,
1286 "Inconsistent address family\n");
1287 af = AF_INET;
1288 memcpy(&tfe->a.a4.sip, &tmp, 4);
1289 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1290 if (af != 0 && af != AF_INET6)
1291 errx(EX_DATAERR,
1292 "Inconsistent address family\n");
1293 af = AF_INET6;
1294 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1295 }
1296
1297 arg = p;
1298 }
1299
1300 /* Handle <proto-num|proto-name> */
1301 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1302 if (arg == NULL)
1303 errx(EX_DATAERR, "invalid key: proto missing");
1304 if ((p = strchr(arg, ',')) != NULL)
1305 *p++ = '\0';
1306
1307 key = strtol(arg, &pp, 10);
1308 if (*pp != '\0') {
1309 if ((pent = getprotobyname(arg)) == NULL)
1310 errx(EX_DATAERR, "Unknown proto: %s",
1311 arg);
1312 else
1313 key = pent->p_proto;
1314 }
1315
1316 if (key > 255)
1317 errx(EX_DATAERR, "Bad protocol number: %u",key);
1318
1319 tfe->proto = key;
1320
1321 arg = p;
1322 }
1323
1324 /* Handle <port-num|service-name> */
1325 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1326 if (arg == NULL)
1327 errx(EX_DATAERR, "invalid key: src port missing");
1328 if ((p = strchr(arg, ',')) != NULL)
1329 *p++ = '\0';
1330
1331 port = htons(strtol(arg, &pp, 10));
1332 if (*pp != '\0') {
1333 if ((sent = getservbyname(arg, NULL)) == NULL)
1334 errx(EX_DATAERR, "Unknown service: %s",
1335 arg);
1336 port = sent->s_port;
1337 }
1338 tfe->sport = port;
1339 arg = p;
1340 }
1341
1342 /* Handle <ipv4|ipv6>*/
1343 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1344 if (arg == NULL)
1345 errx(EX_DATAERR, "invalid key: dst ip missing");
1346 if ((p = strchr(arg, ',')) != NULL)
1347 *p++ = '\0';
1348 /* Determine family using temporary storage */
1349 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1350 if (af != 0 && af != AF_INET)
1351 errx(EX_DATAERR,
1352 "Inconsistent address family");
1353 af = AF_INET;
1354 memcpy(&tfe->a.a4.dip, &tmp, 4);
1355 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1356 if (af != 0 && af != AF_INET6)
1357 errx(EX_DATAERR,
1358 "Inconsistent address family");
1359 af = AF_INET6;
1360 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1361 }
1362
1363 arg = p;
1364 }
1365
1366 /* Handle <port-num|service-name> */
1367 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1368 if (arg == NULL)
1369 errx(EX_DATAERR, "invalid key: dst port missing");
1370 if ((p = strchr(arg, ',')) != NULL)
1371 *p++ = '\0';
1372
1373 port = htons(strtol(arg, &pp, 10));
1374 if (*pp != '\0') {
1375 if ((sent = getservbyname(arg, NULL)) == NULL)
1376 errx(EX_DATAERR, "Unknown service: %s",
1377 arg);
1378 port = sent->s_port;
1379 }
1380 tfe->dport = port;
1381 arg = p;
1382 }
1383
1384 tfe->af = af;
1385
1386 break;
1387
1388 default:
1389 errx(EX_DATAERR, "Unsupported table type: %d", type);
1390 }
1391
1392 tentry->subtype = af;
1393 tentry->masklen = masklen;
1394 }
1395
1396 /*
1397 * Tries to guess table key type.
1398 * This procedure is used in legacy table auto-create
1399 * code AND in `ipfw -n` ruleset checking.
1400 *
1401 * Imported from old table_fill_xentry() parse code.
1402 */
1403 static int
guess_key_type(char * key,uint8_t * ptype)1404 guess_key_type(char *key, uint8_t *ptype)
1405 {
1406 char *p;
1407 struct in6_addr addr;
1408 uint32_t kv;
1409
1410 if (ishexnumber(*key) != 0 || *key == ':') {
1411 /* Remove / if exists */
1412 if ((p = strchr(key, '/')) != NULL)
1413 *p = '\0';
1414
1415 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1416 (inet_pton(AF_INET6, key, &addr) == 1)) {
1417 *ptype = IPFW_TABLE_CIDR;
1418 if (p != NULL)
1419 *p = '/';
1420 return (0);
1421 } else {
1422 /* Port or any other key */
1423 /* Skip non-base 10 entries like 'fa1' */
1424 kv = strtol(key, &p, 10);
1425 if (*p == '\0') {
1426 *ptype = IPFW_TABLE_NUMBER;
1427 return (0);
1428 } else if ((p != key) && (*p == '.')) {
1429 /*
1430 * Warn on IPv4 address strings
1431 * which are "valid" for inet_aton() but not
1432 * in inet_pton().
1433 *
1434 * Typical examples: '10.5' or '10.0.0.05'
1435 */
1436 return (1);
1437 }
1438 }
1439 }
1440
1441 if (strchr(key, '.') == NULL) {
1442 *ptype = IPFW_TABLE_INTERFACE;
1443 return (0);
1444 }
1445
1446 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1447 return (1);
1448
1449 *ptype = IPFW_TABLE_CIDR;
1450 return (0);
1451 }
1452
1453 static void
tentry_fill_key(ipfw_obj_header * oh,ipfw_obj_tentry * tent,char * key,int add,uint8_t * ptype,uint32_t * pvmask,ipfw_xtable_info * xi)1454 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1455 int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1456 {
1457 uint8_t type, tflags;
1458 uint32_t vmask;
1459 int error;
1460
1461 type = 0;
1462 tflags = 0;
1463 vmask = 0;
1464
1465 if (xi->tablename[0] == '\0')
1466 error = table_get_info(oh, xi);
1467 else
1468 error = 0;
1469
1470 if (error == 0) {
1471 if (g_co.test_only == 0) {
1472 /* Table found */
1473 type = xi->type;
1474 tflags = xi->tflags;
1475 vmask = xi->vmask;
1476 } else {
1477 /*
1478 * We're running `ipfw -n`
1479 * Compatibility layer: try to guess key type
1480 * before failing.
1481 */
1482 if (guess_key_type(key, &type) != 0) {
1483 /* Inknown key */
1484 errx(EX_USAGE, "Cannot guess "
1485 "key '%s' type", key);
1486 }
1487 vmask = IPFW_VTYPE_LEGACY;
1488 }
1489 } else {
1490 if (error != ESRCH)
1491 errx(EX_OSERR, "Error requesting table %s info",
1492 oh->ntlv.name);
1493 if (add == 0)
1494 errx(EX_DATAERR, "Table %s does not exist",
1495 oh->ntlv.name);
1496 /*
1497 * Table does not exist
1498 * Compatibility layer: try to guess key type before failing.
1499 */
1500 if (guess_key_type(key, &type) != 0) {
1501 /* Inknown key */
1502 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1503 "key '%s' type", oh->ntlv.name, key);
1504 }
1505
1506 vmask = IPFW_VTYPE_LEGACY;
1507 }
1508
1509 tentry_fill_key_type(key, tent, type, tflags);
1510
1511 *ptype = type;
1512 *pvmask = vmask;
1513 }
1514
1515 static void
set_legacy_value(uint32_t val,ipfw_table_value * v)1516 set_legacy_value(uint32_t val, ipfw_table_value *v)
1517 {
1518 v->tag = val;
1519 v->pipe = val;
1520 v->divert = val;
1521 v->skipto = val;
1522 v->netgraph = val;
1523 v->fib = val;
1524 v->nat = val;
1525 v->nh4 = val;
1526 v->dscp = (uint8_t)val;
1527 v->limit = val;
1528 }
1529
1530 static void
tentry_fill_value(ipfw_obj_header * oh __unused,ipfw_obj_tentry * tent,char * arg,uint8_t type __unused,uint32_t vmask)1531 tentry_fill_value(ipfw_obj_header *oh __unused, ipfw_obj_tentry *tent,
1532 char *arg, uint8_t type __unused, uint32_t vmask)
1533 {
1534 struct addrinfo hints, *res;
1535 struct in_addr ipaddr;
1536 const char *etype;
1537 char *comma, *e, *n, *p;
1538 uint32_t a4, flag, val;
1539 ipfw_table_value *v;
1540 uint32_t i;
1541 int dval;
1542
1543 v = &tent->v.value;
1544
1545 /* Compat layer: keep old behavior for legacy value types */
1546 if (vmask == IPFW_VTYPE_LEGACY) {
1547 /* Try to interpret as number first */
1548 val = strtoul(arg, &p, 0);
1549 if (*p == '\0') {
1550 set_legacy_value(val, v);
1551 return;
1552 }
1553 if (inet_pton(AF_INET, arg, &val) == 1) {
1554 set_legacy_value(ntohl(val), v);
1555 return;
1556 }
1557 /* Try hostname */
1558 if (lookup_host(arg, &ipaddr) == 0) {
1559 set_legacy_value(ntohl(ipaddr.s_addr), v);
1560 return;
1561 }
1562 errx(EX_OSERR, "Unable to parse value %s", arg);
1563 }
1564
1565 /*
1566 * Shorthands: handle single value if vmask consists
1567 * of numbers only. e.g.:
1568 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1569 */
1570
1571 n = arg;
1572 etype = NULL;
1573 for (i = 1; i < (1u << 31); i *= 2) {
1574 if ((flag = (vmask & i)) == 0)
1575 continue;
1576 vmask &= ~flag;
1577
1578 if ((comma = strchr(n, ',')) != NULL)
1579 *comma = '\0';
1580
1581 switch (flag) {
1582 case IPFW_VTYPE_TAG:
1583 v->tag = strtol(n, &e, 10);
1584 if (*e != '\0')
1585 etype = "tag";
1586 break;
1587 case IPFW_VTYPE_PIPE:
1588 v->pipe = strtol(n, &e, 10);
1589 if (*e != '\0')
1590 etype = "pipe";
1591 break;
1592 case IPFW_VTYPE_DIVERT:
1593 v->divert = strtol(n, &e, 10);
1594 if (*e != '\0')
1595 etype = "divert";
1596 break;
1597 case IPFW_VTYPE_SKIPTO:
1598 v->skipto = strtol(n, &e, 10);
1599 if (*e != '\0')
1600 etype = "skipto";
1601 break;
1602 case IPFW_VTYPE_NETGRAPH:
1603 v->netgraph = strtol(n, &e, 10);
1604 if (*e != '\0')
1605 etype = "netgraph";
1606 break;
1607 case IPFW_VTYPE_FIB:
1608 v->fib = strtol(n, &e, 10);
1609 if (*e != '\0')
1610 etype = "fib";
1611 break;
1612 case IPFW_VTYPE_NAT:
1613 v->nat = strtol(n, &e, 10);
1614 if (*e != '\0')
1615 etype = "nat";
1616 break;
1617 case IPFW_VTYPE_LIMIT:
1618 v->limit = strtol(n, &e, 10);
1619 if (*e != '\0')
1620 etype = "limit";
1621 break;
1622 case IPFW_VTYPE_NH4:
1623 if (strchr(n, '.') != NULL &&
1624 inet_pton(AF_INET, n, &a4) == 1) {
1625 v->nh4 = ntohl(a4);
1626 break;
1627 }
1628 if (lookup_host(n, &ipaddr) == 0) {
1629 v->nh4 = ntohl(ipaddr.s_addr);
1630 break;
1631 }
1632 etype = "ipv4";
1633 break;
1634 case IPFW_VTYPE_DSCP:
1635 if (isalpha(*n)) {
1636 if ((dval = match_token(f_ipdscp, n)) != -1) {
1637 v->dscp = dval;
1638 break;
1639 } else
1640 etype = "DSCP code";
1641 } else {
1642 v->dscp = strtol(n, &e, 10);
1643 if (v->dscp > 63 || *e != '\0')
1644 etype = "DSCP value";
1645 }
1646 break;
1647 case IPFW_VTYPE_NH6:
1648 if (strchr(n, ':') != NULL) {
1649 memset(&hints, 0, sizeof(hints));
1650 hints.ai_family = AF_INET6;
1651 hints.ai_flags = AI_NUMERICHOST;
1652 if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1653 v->nh6 = ((struct sockaddr_in6 *)
1654 res->ai_addr)->sin6_addr;
1655 v->zoneid = ((struct sockaddr_in6 *)
1656 res->ai_addr)->sin6_scope_id;
1657 freeaddrinfo(res);
1658 break;
1659 }
1660 }
1661 etype = "ipv6";
1662 break;
1663 }
1664
1665 if (etype != NULL)
1666 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1667
1668 if (comma != NULL)
1669 *comma++ = ',';
1670
1671 if ((n = comma) != NULL)
1672 continue;
1673
1674 /* End of input. */
1675 if (vmask != 0)
1676 errx(EX_USAGE, "Not enough fields inside value");
1677 }
1678 }
1679
1680 /*
1681 * Compare table names.
1682 * Honor number comparison.
1683 */
1684 static int
tablename_cmp(const void * a,const void * b)1685 tablename_cmp(const void *a, const void *b)
1686 {
1687 const ipfw_xtable_info *ia, *ib;
1688
1689 ia = (const ipfw_xtable_info *)a;
1690 ib = (const ipfw_xtable_info *)b;
1691
1692 return (stringnum_cmp(ia->tablename, ib->tablename));
1693 }
1694
1695 /*
1696 * Retrieves table list from kernel,
1697 * optionally sorts it and calls requested function for each table.
1698 * Returns 0 on success.
1699 */
1700 static int
tables_foreach(table_cb_t * f,void * arg,int sort)1701 tables_foreach(table_cb_t *f, void *arg, int sort)
1702 {
1703 ipfw_obj_lheader *olh;
1704 ipfw_xtable_info *info;
1705 size_t sz;
1706 uint32_t i;
1707 int error;
1708
1709 /* Start with reasonable default */
1710 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1711
1712 for (;;) {
1713 if ((olh = calloc(1, sz)) == NULL)
1714 return (ENOMEM);
1715
1716 olh->size = sz;
1717 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1718 sz = olh->size;
1719 free(olh);
1720 if (errno != ENOMEM)
1721 return (errno);
1722 continue;
1723 }
1724
1725 if (sort != 0)
1726 qsort(olh + 1, olh->count, olh->objsize,
1727 tablename_cmp);
1728
1729 info = (ipfw_xtable_info *)(olh + 1);
1730 for (i = 0; i < olh->count; i++) {
1731 if (g_co.use_set == 0 || info->set == g_co.use_set - 1)
1732 error = f(info, arg);
1733 info = (ipfw_xtable_info *)((caddr_t)info +
1734 olh->objsize);
1735 }
1736 free(olh);
1737 break;
1738 }
1739 return (0);
1740 }
1741
1742
1743 /*
1744 * Retrieves all entries for given table @i in
1745 * eXtended format. Allocate buffer large enough
1746 * to store result. Called needs to free it later.
1747 *
1748 * Returns 0 on success.
1749 */
1750 static int
table_do_get_list(ipfw_xtable_info * i,ipfw_obj_header ** poh)1751 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1752 {
1753 ipfw_obj_header *oh;
1754 size_t sz;
1755 int c;
1756
1757 sz = 0;
1758 oh = NULL;
1759 for (c = 0; c < 8; c++) {
1760 if (sz < i->size)
1761 sz = i->size + 44;
1762 if (oh != NULL)
1763 free(oh);
1764 if ((oh = calloc(1, sz)) == NULL)
1765 continue;
1766 table_fill_objheader(oh, i);
1767 oh->opheader.version = 1; /* Current version */
1768 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1769 *poh = oh;
1770 return (0);
1771 }
1772
1773 if (errno != ENOMEM)
1774 break;
1775 }
1776 free(oh);
1777
1778 return (errno);
1779 }
1780
1781 /*
1782 * Shows all entries from @oh in human-readable format
1783 */
1784 static void
table_show_list(ipfw_obj_header * oh,int need_header)1785 table_show_list(ipfw_obj_header *oh, int need_header)
1786 {
1787 ipfw_obj_tentry *tent;
1788 uint32_t count;
1789 ipfw_xtable_info *i;
1790
1791 i = (ipfw_xtable_info *)(oh + 1);
1792 tent = (ipfw_obj_tentry *)(i + 1);
1793
1794 if (need_header)
1795 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1796
1797 count = i->count;
1798 while (count > 0) {
1799 table_show_entry(i, tent);
1800 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1801 count--;
1802 }
1803 }
1804
1805 static void
table_show_value(char * buf,size_t bufsize,ipfw_table_value * v,uint32_t vmask,int print_ip)1806 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1807 uint32_t vmask, int print_ip)
1808 {
1809 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1810 struct sockaddr_in6 sa6;
1811 uint32_t flag, i, l;
1812 size_t sz;
1813 struct in_addr a4;
1814
1815 sz = bufsize;
1816
1817 /*
1818 * Some shorthands for printing values:
1819 * legacy assumes all values are equal, so keep the first one.
1820 */
1821 if (vmask == IPFW_VTYPE_LEGACY) {
1822 if (print_ip != 0) {
1823 flag = htonl(v->tag);
1824 inet_ntop(AF_INET, &flag, buf, sz);
1825 } else
1826 snprintf(buf, sz, "%u", v->tag);
1827 return;
1828 }
1829
1830 for (i = 1; i < (1u << 31); i *= 2) {
1831 if ((flag = (vmask & i)) == 0)
1832 continue;
1833 l = 0;
1834
1835 switch (flag) {
1836 case IPFW_VTYPE_TAG:
1837 l = snprintf(buf, sz, "%u,", v->tag);
1838 break;
1839 case IPFW_VTYPE_PIPE:
1840 l = snprintf(buf, sz, "%u,", v->pipe);
1841 break;
1842 case IPFW_VTYPE_DIVERT:
1843 l = snprintf(buf, sz, "%d,", v->divert);
1844 break;
1845 case IPFW_VTYPE_SKIPTO:
1846 l = snprintf(buf, sz, "%d,", v->skipto);
1847 break;
1848 case IPFW_VTYPE_NETGRAPH:
1849 l = snprintf(buf, sz, "%u,", v->netgraph);
1850 break;
1851 case IPFW_VTYPE_FIB:
1852 l = snprintf(buf, sz, "%u,", v->fib);
1853 break;
1854 case IPFW_VTYPE_NAT:
1855 l = snprintf(buf, sz, "%u,", v->nat);
1856 break;
1857 case IPFW_VTYPE_LIMIT:
1858 l = snprintf(buf, sz, "%u,", v->limit);
1859 break;
1860 case IPFW_VTYPE_NH4:
1861 a4.s_addr = htonl(v->nh4);
1862 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1863 l = snprintf(buf, sz, "%s,", abuf);
1864 break;
1865 case IPFW_VTYPE_DSCP:
1866 l = snprintf(buf, sz, "%d,", v->dscp);
1867 break;
1868 case IPFW_VTYPE_NH6:
1869 sa6.sin6_family = AF_INET6;
1870 sa6.sin6_len = sizeof(sa6);
1871 sa6.sin6_addr = v->nh6;
1872 sa6.sin6_port = 0;
1873 sa6.sin6_scope_id = v->zoneid;
1874 if (getnameinfo((const struct sockaddr *)&sa6,
1875 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1876 NI_NUMERICHOST) == 0)
1877 l = snprintf(buf, sz, "%s,", abuf);
1878 break;
1879 }
1880
1881 buf += l;
1882 sz -= l;
1883 }
1884
1885 if (sz != bufsize)
1886 *(buf - 1) = '\0';
1887 }
1888
1889 static void
table_show_entry(ipfw_xtable_info * i,ipfw_obj_tentry * tent)1890 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1891 {
1892 char tbuf[128], pval[128];
1893 const char *comma;
1894 const u_char *mac;
1895 void *paddr;
1896 struct tflow_entry *tfe;
1897
1898 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1899 g_co.do_value_as_ip);
1900
1901 switch (i->type) {
1902 case IPFW_TABLE_ADDR:
1903 /* IPv4 or IPv6 prefixes */
1904 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1905 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1906 break;
1907 case IPFW_TABLE_MAC:
1908 /* MAC prefixes */
1909 mac = tent->k.mac;
1910 printf("%02x:%02x:%02x:%02x:%02x:%02x/%u %s\n",
1911 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
1912 tent->masklen, pval);
1913 break;
1914 case IPFW_TABLE_INTERFACE:
1915 /* Interface names */
1916 printf("%s %s\n", tent->k.iface, pval);
1917 break;
1918 case IPFW_TABLE_NUMBER:
1919 /* numbers */
1920 printf("%u %s\n", tent->k.key, pval);
1921 break;
1922 case IPFW_TABLE_FLOW:
1923 /* flows */
1924 tfe = &tent->k.flow;
1925 comma = "";
1926
1927 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1928 if (tfe->af == AF_INET)
1929 paddr = &tfe->a.a4.sip;
1930 else
1931 paddr = &tfe->a.a6.sip6;
1932
1933 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1934 printf("%s%s", comma, tbuf);
1935 comma = ",";
1936 }
1937
1938 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1939 printf("%s%d", comma, tfe->proto);
1940 comma = ",";
1941 }
1942
1943 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1944 printf("%s%d", comma, ntohs(tfe->sport));
1945 comma = ",";
1946 }
1947 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1948 if (tfe->af == AF_INET)
1949 paddr = &tfe->a.a4.dip;
1950 else
1951 paddr = &tfe->a.a6.dip6;
1952
1953 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1954 printf("%s%s", comma, tbuf);
1955 comma = ",";
1956 }
1957
1958 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1959 printf("%s%d", comma, ntohs(tfe->dport));
1960 comma = ",";
1961 }
1962
1963 printf(" %s\n", pval);
1964 }
1965 }
1966
1967 static int
table_do_get_stdlist(uint16_t opcode,ipfw_obj_lheader ** polh)1968 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1969 {
1970 ipfw_obj_lheader req, *olh;
1971 size_t sz;
1972
1973 memset(&req, 0, sizeof(req));
1974 sz = sizeof(req);
1975
1976 if (do_get3(opcode, &req.opheader, &sz) != 0)
1977 if (errno != ENOMEM)
1978 return (errno);
1979
1980 sz = req.size;
1981 if ((olh = calloc(1, sz)) == NULL)
1982 return (ENOMEM);
1983
1984 olh->size = sz;
1985 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1986 free(olh);
1987 return (errno);
1988 }
1989
1990 *polh = olh;
1991 return (0);
1992 }
1993
1994 static int
table_do_get_algolist(ipfw_obj_lheader ** polh)1995 table_do_get_algolist(ipfw_obj_lheader **polh)
1996 {
1997
1998 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
1999 }
2000
2001 static int
table_do_get_vlist(ipfw_obj_lheader ** polh)2002 table_do_get_vlist(ipfw_obj_lheader **polh)
2003 {
2004
2005 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
2006 }
2007
2008 void
ipfw_list_ta(int ac __unused,char * av[]__unused)2009 ipfw_list_ta(int ac __unused, char *av[] __unused)
2010 {
2011 ipfw_obj_lheader *olh;
2012 ipfw_ta_info *info;
2013 const char *atype;
2014 uint32_t i;
2015 int error;
2016
2017 error = table_do_get_algolist(&olh);
2018 if (error != 0)
2019 err(EX_OSERR, "Unable to request algorithm list");
2020
2021 info = (ipfw_ta_info *)(olh + 1);
2022 for (i = 0; i < olh->count; i++) {
2023 if ((atype = match_value(tabletypes, info->type)) == NULL)
2024 atype = "unknown";
2025 printf("--- %s ---\n", info->algoname);
2026 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
2027
2028 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
2029 }
2030
2031 free(olh);
2032 }
2033
2034
2035 /* Copy of current kernel table_value structure */
2036 struct _table_value {
2037 uint32_t tag; /* O_TAG/O_TAGGED */
2038 uint32_t pipe; /* O_PIPE/O_QUEUE */
2039 uint16_t divert; /* O_DIVERT/O_TEE */
2040 uint16_t skipto; /* skipto, CALLRET */
2041 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
2042 uint32_t fib; /* O_SETFIB */
2043 uint32_t nat; /* O_NAT */
2044 uint32_t nh4;
2045 uint8_t dscp;
2046 uint8_t spare0;
2047 uint16_t spare1;
2048 /* -- 32 bytes -- */
2049 struct in6_addr nh6;
2050 uint32_t limit; /* O_LIMIT */
2051 uint32_t zoneid;
2052 uint64_t refcnt; /* Number of references */
2053 };
2054
2055 static int
compare_values(const void * _a,const void * _b)2056 compare_values(const void *_a, const void *_b)
2057 {
2058 const struct _table_value *a, *b;
2059
2060 a = (const struct _table_value *)_a;
2061 b = (const struct _table_value *)_b;
2062
2063 if (a->spare1 < b->spare1)
2064 return (-1);
2065 else if (a->spare1 > b->spare1)
2066 return (1);
2067
2068 return (0);
2069 }
2070
2071 void
ipfw_list_values(int ac __unused,char * av[]__unused)2072 ipfw_list_values(int ac __unused, char *av[] __unused)
2073 {
2074 char buf[128];
2075 ipfw_obj_lheader *olh;
2076 struct _table_value *v;
2077 uint32_t i, vmask;
2078 int error;
2079
2080 error = table_do_get_vlist(&olh);
2081 if (error != 0)
2082 err(EX_OSERR, "Unable to request value list");
2083
2084 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2085
2086 table_print_valheader(buf, sizeof(buf), vmask);
2087 printf("HEADER: %s\n", buf);
2088 v = (struct _table_value *)(olh + 1);
2089 qsort(v, olh->count, olh->objsize, compare_values);
2090 for (i = 0; i < olh->count; i++) {
2091 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2092 vmask, 0);
2093 printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2094 v = (struct _table_value *)((caddr_t)v + olh->objsize);
2095 }
2096
2097 free(olh);
2098 }
2099
2100 int
table_check_name(const char * tablename)2101 table_check_name(const char *tablename)
2102 {
2103
2104 if (ipfw_check_object_name(tablename) != 0)
2105 return (EINVAL);
2106 /* Restrict some 'special' names */
2107 if (strcmp(tablename, "all") == 0)
2108 return (EINVAL);
2109 return (0);
2110 }
2111
2112