2 * Copyright (c) 2014 Yandex LLC
3 * Copyright (c) 2014 Alexander V. Chernikov
5 * Redistribution and use in source forms, with and without modification,
6 * are permitted provided that this entire comment appears intact.
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.
12 * This software is provided ``AS IS'' without any warranties of any kind.
14 * in-kernel ipfw tables support.
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <sys/sysctl.h>
32 #include <net/ethernet.h>
34 #include <netinet/in.h>
35 #include <netinet/ip_fw.h>
36 #include <arpa/inet.h>
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);
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);
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);
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);
74 #define s6_addr32 __u6_addr.__u6_addr32
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 },
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 },
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 { "mark", IPFW_VTYPE_MARK },
111 static struct _s_x tablecmds[] = {
113 { "delete", TOK_DEL },
114 { "create", TOK_CREATE },
115 { "destroy", TOK_DESTROY },
116 { "flush", TOK_FLUSH },
117 { "modify", TOK_MODIFY },
118 { "swap", TOK_SWAP },
119 { "info", TOK_INFO },
120 { "detail", TOK_DETAIL },
121 { "list", TOK_LIST },
122 { "lookup", TOK_LOOKUP },
123 { "atomic", TOK_ATOMIC },
124 { "lock", TOK_LOCK },
125 { "unlock", TOK_UNLOCK },
130 lookup_host (char *host, struct in_addr *ipaddr)
134 if (!inet_aton(host, ipaddr)) {
135 if ((he = gethostbyname(host)) == NULL)
137 *ipaddr = *(struct in_addr *)he->h_addr_list[0];
143 * This one handles all table-related commands
144 * ipfw table NAME create ...
145 * ipfw table NAME modify ...
146 * ipfw table {NAME | all} destroy
147 * ipfw table NAME swap NAME
148 * ipfw table NAME lock
149 * ipfw table NAME unlock
150 * ipfw table NAME add addr[/masklen] [value]
151 * ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
152 * ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
153 * ipfw table NAME lookup addr
154 * ipfw table {NAME | all} flush
155 * ipfw table {NAME | all} list
156 * ipfw table {NAME | all} info
157 * ipfw table {NAME | all} detail
160 ipfw_table_handler(int ac, char *av[])
163 int atomic, error, tcmd;
170 memset(&oh, 0, sizeof(oh));
172 if (g_co.use_set != 0)
173 set = g_co.use_set - 1;
178 NEED1("table needs name");
181 if (table_check_name(tablename) == 0) {
182 table_fill_ntlv(&oh.ntlv, *av, set, 1);
185 if (strcmp(tablename, "all") == 0)
188 errx(EX_USAGE, "table name %s is invalid", tablename);
191 NEED1("table needs command");
193 tcmd = get_token(tablecmds, *av, "table command");
194 /* Check if atomic operation was requested */
196 if (tcmd == TOK_ATOMIC) {
198 NEED1("atomic needs command");
199 tcmd = get_token(tablecmds, *av, "table command");
204 errx(EX_USAGE, "atomic is not compatible with %s", *av);
218 errx(EX_USAGE, "table name required");
224 do_add = **av == 'a';
226 table_modify_record(&oh, ac, av, do_add, g_co.do_quiet,
227 g_co.do_quiet, atomic);
231 table_create(&oh, ac, av);
235 table_modify(&oh, ac, av);
239 if (table_destroy(&oh) == 0)
242 err(EX_OSERR, "failed to destroy table %s",
244 /* ESRCH isn't fatal, warn if not quiet mode */
245 if (g_co.do_quiet == 0)
246 warn("failed to destroy table %s", tablename);
248 error = tables_foreach(table_destroy_one, &oh, 1);
251 "failed to destroy tables list");
256 if ((error = table_flush(&oh)) == 0)
259 err(EX_OSERR, "failed to flush table %s info",
261 /* ESRCH isn't fatal, warn if not quiet mode */
262 if (g_co.do_quiet == 0)
263 warn("failed to flush table %s info",
266 error = tables_foreach(table_flush_one, &oh, 1);
268 err(EX_OSERR, "failed to flush tables list");
269 /* XXX: we ignore errors here */
274 NEED1("second table name required");
275 table_swap(&oh, *av);
279 table_lock(&oh, (tcmd == TOK_LOCK));
283 arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL;
285 if ((error = table_get_info(&oh, &i)) != 0)
286 err(EX_OSERR, "failed to request table info");
287 table_show_info(&i, arg);
289 error = tables_foreach(table_show_info, arg, 1);
291 err(EX_OSERR, "failed to request tables list");
295 arg = is_all ? (void*)1 : NULL;
297 if ((error = table_get_info(&oh, &i)) != 0)
298 err(EX_OSERR, "failed to request table info");
299 table_show_one(&i, arg);
301 error = tables_foreach(table_show_one, arg, 1);
303 err(EX_OSERR, "failed to request tables list");
308 table_lookup(&oh, ac, av);
314 table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
318 ntlv->head.type = IPFW_TLV_TBL_NAME;
319 ntlv->head.length = sizeof(ipfw_obj_ntlv);
322 strlcpy(ntlv->name, name, sizeof(ntlv->name));
326 table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
330 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
333 static struct _s_x tablenewcmds[] = {
334 { "type", TOK_TYPE },
335 { "valtype", TOK_VALTYPE },
336 { "algo", TOK_ALGO },
337 { "limit", TOK_LIMIT },
338 { "locked", TOK_LOCK },
339 { "missing", TOK_MISSING },
340 { "or-flush", TOK_ORFLUSH },
344 static struct _s_x flowtypecmds[] = {
345 { "src-ip", IPFW_TFFLAG_SRCIP },
346 { "proto", IPFW_TFFLAG_PROTO },
347 { "src-port", IPFW_TFFLAG_SRCPORT },
348 { "dst-ip", IPFW_TFFLAG_DSTIP },
349 { "dst-port", IPFW_TFFLAG_DSTPORT },
354 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
356 uint32_t fset, fclear;
359 /* Parse type options */
361 case IPFW_TABLE_FLOW:
363 if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
365 "unable to parse flow option %s", e);
376 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
381 if ((tname = match_value(tabletypes, type)) == NULL)
384 l = snprintf(tbuf, size, "%s", tname);
389 case IPFW_TABLE_FLOW:
393 print_flags_buffer(tbuf, size, flowtypecmds, tflags);
402 * ipfw table NAME create [ type { addr | iface | number | flow } ]
403 * [ algo algoname ] [missing] [or-flush]
406 table_create(ipfw_obj_header *oh, int ac, char *av[])
408 ipfw_xtable_info xi, xie;
409 int error, missing, orflush, tcmd, val;
410 uint32_t fset, fclear;
414 missing = orflush = 0;
415 memset(&xi, 0, sizeof(xi));
417 tcmd = get_token(tablenewcmds, *av, "option");
422 NEED1("limit value required");
423 xi.limit = strtol(*av, NULL, 10);
427 NEED1("table type required");
428 /* Type may have suboptions after ':' */
429 if ((p = strchr(*av, ':')) != NULL)
431 val = match_token(tabletypes, *av);
433 concat_tokens(tbuf, sizeof(tbuf), tabletypes,
436 "Unknown tabletype: %s. Supported: %s",
441 error = table_parse_type(val, p, &xi.tflags);
444 "Unsupported suboptions: %s", p);
449 NEED1("table value type required");
451 val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
457 concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
458 errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
462 NEED1("table algorithm name required");
463 if (strlen(*av) > sizeof(xi.algoname))
464 errx(EX_USAGE, "algorithm name too long");
465 strlcpy(xi.algoname, *av, sizeof(xi.algoname));
469 xi.flags |= IPFW_TGFLAGS_LOCKED;
480 /* Set some defaults to preserve compatibility. */
481 if (xi.algoname[0] == '\0') {
485 xi.type = IPFW_TABLE_ADDR;
486 algo = match_value(tablealgos, xi.type);
488 strlcpy(xi.algoname, algo, sizeof(xi.algoname));
491 xi.vmask = IPFW_VTYPE_LEGACY;
493 error = table_do_create(oh, &xi);
498 if (errno != EEXIST || missing == 0)
499 err(EX_OSERR, "Table creation failed");
501 /* Check that existing table is the same we are trying to create */
502 if (table_get_info(oh, &xie) != 0)
503 err(EX_OSERR, "Existing table check failed");
505 if (xi.limit != xie.limit || xi.type != xie.type ||
506 xi.tflags != xie.tflags || xi.vmask != xie.vmask || (
507 xi.algoname[0] != '\0' && strcmp(xi.algoname,
508 xie.algoname) != 0) || xi.flags != xie.flags)
509 errx(EX_DATAERR, "The existing table is not compatible "
510 "with one you are creating.");
512 /* Flush existing table if instructed to do so */
513 if (orflush != 0 && table_flush(oh) != 0)
514 err(EX_OSERR, "Table flush on creation failed");
520 * Request: [ ipfw_obj_header ipfw_xtable_info ]
522 * Returns 0 on success.
525 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
527 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
530 memcpy(tbuf, oh, sizeof(*oh));
531 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
532 oh = (ipfw_obj_header *)tbuf;
534 error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
540 * Modifies existing table
542 * ipfw table NAME modify [ limit number ]
545 table_modify(ipfw_obj_header *oh, int ac, char *av[])
550 memset(&xi, 0, sizeof(xi));
553 tcmd = get_token(tablenewcmds, *av, "option");
558 NEED1("limit value required");
559 xi.limit = strtol(*av, NULL, 10);
560 xi.mflags |= IPFW_TMFLAGS_LIMIT;
564 errx(EX_USAGE, "cmd is not supported for modification");
568 if (table_do_modify(oh, &xi) != 0)
569 err(EX_OSERR, "Table modification failed");
573 * Modifies existing table.
575 * Request: [ ipfw_obj_header ipfw_xtable_info ]
577 * Returns 0 on success.
580 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
582 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
585 memcpy(tbuf, oh, sizeof(*oh));
586 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
587 oh = (ipfw_obj_header *)tbuf;
589 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
595 * Locks or unlocks given table
598 table_lock(ipfw_obj_header *oh, int lock)
602 memset(&xi, 0, sizeof(xi));
604 xi.mflags |= IPFW_TMFLAGS_LOCK;
605 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
607 if (table_do_modify(oh, &xi) != 0)
608 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
612 * Destroys given table specified by @oh->ntlv.
613 * Returns 0 on success.
616 table_destroy(ipfw_obj_header *oh)
619 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
626 table_destroy_one(ipfw_xtable_info *i, void *arg)
630 oh = (ipfw_obj_header *)arg;
631 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
632 if (table_destroy(oh) != 0) {
633 if (g_co.do_quiet == 0)
634 warn("failed to destroy table(%s) in set %u",
635 i->tablename, i->set);
642 * Flushes given table specified by @oh->ntlv.
643 * Returns 0 on success.
646 table_flush(ipfw_obj_header *oh)
649 if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
656 table_do_swap(ipfw_obj_header *oh, char *second)
658 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
661 memset(tbuf, 0, sizeof(tbuf));
662 memcpy(tbuf, oh, sizeof(*oh));
663 oh = (ipfw_obj_header *)tbuf;
664 table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
666 error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
672 * Swaps given table with @second one.
675 table_swap(ipfw_obj_header *oh, char *second)
678 if (table_check_name(second) != 0)
679 errx(EX_USAGE, "table name %s is invalid", second);
681 if (table_do_swap(oh, second) == 0)
686 errx(EX_USAGE, "Unable to swap table: check types");
688 errx(EX_USAGE, "Unable to swap table: check limits");
696 * Retrieves table in given table specified by @oh->ntlv.
698 * Returns 0 on success.
701 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
703 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
707 memset(tbuf, 0, sizeof(tbuf));
708 memcpy(tbuf, oh, sizeof(*oh));
709 oh = (ipfw_obj_header *)tbuf;
711 if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
714 if (sz < sizeof(tbuf))
717 *i = *(ipfw_xtable_info *)(oh + 1);
722 static struct _s_x tablealgoclass[] = {
723 { "hash", IPFW_TACLASS_HASH },
724 { "array", IPFW_TACLASS_ARRAY },
725 { "radix", IPFW_TACLASS_RADIX },
739 * Print global/per-AF table @i algorithm info.
742 table_show_tainfo(ipfw_xtable_info *i __unused, struct ta_cldata *d,
743 const char *af, const char *taclass)
746 switch (d->taclass) {
747 case IPFW_TACLASS_HASH:
748 case IPFW_TACLASS_ARRAY:
749 printf(" %salgorithm %s info\n", af, taclass);
750 if (d->itemsize == d->itemsize6)
751 printf(" size: %u items: %u itemsize: %u\n",
752 d->size, d->count, d->itemsize);
754 printf(" size: %u items: %u "
755 "itemsize4: %u itemsize6: %u\n",
757 d->itemsize, d->itemsize6);
759 case IPFW_TACLASS_RADIX:
760 printf(" %salgorithm %s info\n", af, taclass);
761 if (d->itemsize == d->itemsize6)
762 printf(" items: %u itemsize: %u\n",
763 d->count, d->itemsize);
766 "itemsize4: %u itemsize6: %u\n",
767 d->count, d->itemsize, d->itemsize6);
770 printf(" algo class: %s\n", taclass);
775 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
778 if (vmask == IPFW_VTYPE_LEGACY) {
779 snprintf(buf, bufsize, "legacy");
783 memset(buf, 0, bufsize);
784 print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
788 * Prints table info struct @i in human-readable form.
791 table_show_info(ipfw_xtable_info *i, void *arg)
794 ipfw_ta_tinfo *tainfo;
797 char ttype[64], tvtype[64];
799 table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
800 table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
802 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
803 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
804 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
806 printf(" kindex: %d, type: %s\n", i->kidx, ttype);
807 printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
808 printf(" algorithm: %s\n", i->algoname);
809 printf(" items: %u, size: %u\n", i->count, i->size);
811 printf(" limit: %u\n", i->limit);
813 /* Print algo-specific info if requested & set */
817 if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
819 tainfo = &i->ta_info;
823 if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
825 if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
828 memset(&d, 0, sizeof(d));
829 d.taclass = tainfo->taclass4;
830 d.size = tainfo->size4;
831 d.count = tainfo->count4;
832 d.itemsize = tainfo->itemsize4;
833 if (afdata == 0 && afitem != 0)
834 d.itemsize6 = tainfo->itemsize6;
836 d.itemsize6 = d.itemsize;
837 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
841 table_show_tainfo(i, &d, "", vtype);
843 table_show_tainfo(i, &d, "IPv4 ", vtype);
844 memset(&d, 0, sizeof(d));
845 d.taclass = tainfo->taclass6;
846 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
848 d.size = tainfo->size6;
849 d.count = tainfo->count6;
850 d.itemsize = tainfo->itemsize6;
851 d.itemsize6 = d.itemsize;
852 table_show_tainfo(i, &d, "IPv6 ", vtype);
860 * Function wrappers which can be used either
861 * as is or as foreach function parameter.
865 table_show_one(ipfw_xtable_info *i, void *arg)
867 ipfw_obj_header *oh = NULL;
871 is_all = arg == NULL ? 0 : 1;
873 if ((error = table_do_get_list(i, &oh)) != 0) {
874 err(EX_OSERR, "Error requesting table %s list", i->tablename);
878 table_show_list(oh, is_all);
885 table_flush_one(ipfw_xtable_info *i, void *arg)
889 oh = (ipfw_obj_header *)arg;
891 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
893 return (table_flush(oh));
897 table_do_modify_record(int cmd, ipfw_obj_header *oh,
898 ipfw_obj_tentry *tent, int count, int atomic)
901 ipfw_obj_tentry *tent_base;
903 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
907 sz = sizeof(*ctlv) + sizeof(*tent) * count;
909 memset(xbuf, 0, sizeof(xbuf));
912 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
916 memcpy(pbuf, oh, sizeof(*oh));
917 oh = (ipfw_obj_header *)pbuf;
918 oh->opheader.version = 1; /* Current version */
920 ctlv = (ipfw_obj_ctlv *)(oh + 1);
922 ctlv->head.length = sz;
924 ctlv->flags |= IPFW_CTF_ATOMIC;
927 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
928 tent = (ipfw_obj_tentry *)(ctlv + 1);
929 for (i = 0; i < count; i++, tent++) {
930 tent->head.length = sizeof(ipfw_obj_tentry);
935 error = do_get3(cmd, &oh->opheader, &sz);
938 tent = (ipfw_obj_tentry *)(ctlv + 1);
939 /* Copy result back to provided buffer */
940 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
949 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
950 int quiet, int update, int atomic)
952 ipfw_obj_tentry *ptent, tent, *tent_buf;
954 const char *etxt, *px, *texterr;
957 int cmd, count, error, i, ignored;
960 errx(EX_USAGE, "address required");
963 cmd = IP_FW_TABLE_XADD;
964 texterr = "Adding record failed";
966 cmd = IP_FW_TABLE_XDEL;
967 texterr = "Deleting record failed";
971 * Calculate number of entries:
972 * Assume [key val] x N for add
976 count = (add != 0) ? ac / 2 + 1 : ac;
979 /* Adding single entry with/without value */
980 memset(&tent, 0, sizeof(tent));
984 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
986 "Unable to allocate memory for all entries");
990 memset(&xi, 0, sizeof(xi));
993 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
996 * Compatibility layer: auto-create table if not exists.
998 if (xi.tablename[0] == '\0') {
1001 strlcpy(xi.tablename, oh->ntlv.name,
1002 sizeof(xi.tablename));
1004 warnx("DEPRECATED: inserting data into "
1005 "non-existent table %s. (auto-created)",
1007 table_do_create(oh, &xi);
1010 oh->ntlv.type = type;
1013 if (add != 0 && ac > 0) {
1014 tentry_fill_value(oh, ptent, *av, type, vmask);
1019 ptent->head.flags |= IPFW_TF_UPDATE;
1025 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
1028 * Compatibility stuff: do not yell on duplicate keys or
1031 if (error == 0 || (error == EEXIST && add != 0) ||
1032 (error == ENOENT && add == 0)) {
1034 if (tent_buf != &tent)
1040 /* Report results back */
1042 for (i = 0; i < count; ptent++, i++) {
1044 switch (ptent->result) {
1048 case IPFW_TR_DELETED:
1051 case IPFW_TR_UPDATED:
1062 case IPFW_TR_NOTFOUND:
1066 case IPFW_TR_EXISTS:
1070 case IPFW_TR_IGNORED:
1079 if (error != 0 && atomic != 0 && ignored == 0)
1080 printf("%s(reverted): ", px);
1084 table_show_entry(&xi, ptent);
1087 if (tent_buf != &tent)
1092 /* Get real OS error */
1095 /* Try to provide more human-readable error */
1098 etxt = "record already exists";
1104 etxt = "table not found";
1107 etxt = "record not found";
1110 etxt = "table is locked";
1113 etxt = strerror(error);
1116 errx(EX_OSERR, "%s: %s", texterr, etxt);
1120 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1121 ipfw_obj_tentry *xtent)
1123 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1124 ipfw_obj_tentry *tent;
1129 memcpy(xbuf, oh, sizeof(*oh));
1130 oh = (ipfw_obj_header *)xbuf;
1131 tent = (ipfw_obj_tentry *)(oh + 1);
1133 memset(tent, 0, sizeof(*tent));
1134 tent->head.length = sizeof(*tent);
1137 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1138 oh->ntlv.type = type;
1141 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1144 if (sz < sizeof(xbuf))
1153 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1155 ipfw_obj_tentry xtent;
1156 ipfw_xtable_info xi;
1161 errx(EX_USAGE, "address required");
1163 strlcpy(key, *av, sizeof(key));
1165 memset(&xi, 0, sizeof(xi));
1166 error = table_do_lookup(oh, key, &xi, &xtent);
1172 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1174 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1176 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1177 "\"lookup\" method", oh->ntlv.name);
1179 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1182 table_show_entry(&xi, &xtent);
1186 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1191 struct in6_addr *paddr, tmp;
1192 struct ether_addr *mac;
1193 struct tflow_entry *tfe;
1194 uint32_t key, *pkey;
1196 struct protoent *pent;
1197 struct servent *sent;
1202 paddr = (struct in6_addr *)&tentry->k;
1205 case IPFW_TABLE_ADDR:
1206 /* Remove / if exists */
1207 if ((p = strchr(arg, '/')) != NULL) {
1212 if (inet_pton(AF_INET, arg, paddr) == 1) {
1213 if (p != NULL && mask > 32)
1214 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1217 masklen = p ? mask : 32;
1219 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1220 if (IN6_IS_ADDR_V4COMPAT(paddr))
1222 "Use IPv4 instead of v4-compatible");
1223 if (p != NULL && mask > 128)
1224 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1227 masklen = p ? mask : 128;
1231 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1232 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1235 type = IPFW_TABLE_ADDR;
1239 case IPFW_TABLE_MAC:
1240 /* Remove / if exists */
1241 if ((p = strchr(arg, '/')) != NULL) {
1246 if (p != NULL && mask > 8 * ETHER_ADDR_LEN)
1247 errx(EX_DATAERR, "bad MAC mask width: %s",
1250 if ((mac = ether_aton(arg)) == NULL)
1251 errx(EX_DATAERR, "Incorrect MAC address");
1253 memcpy(tentry->k.mac, mac->octet, ETHER_ADDR_LEN);
1254 masklen = p ? mask : 8 * ETHER_ADDR_LEN;
1257 case IPFW_TABLE_INTERFACE:
1258 /* Assume interface name. Copy significant data only */
1259 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1260 memcpy(paddr, arg, mask);
1261 /* Set mask to exact match */
1262 masklen = 8 * IF_NAMESIZE;
1264 case IPFW_TABLE_NUMBER:
1265 /* Port or any other key */
1266 key = strtol(arg, &p, 10);
1268 errx(EX_DATAERR, "Invalid number: %s", arg);
1270 pkey = (uint32_t *)paddr;
1274 case IPFW_TABLE_FLOW:
1275 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1276 tfe = &tentry->k.flow;
1279 /* Handle <ipv4|ipv6> */
1280 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1281 if ((p = strchr(arg, ',')) != NULL)
1283 /* Determine family using temporary storage */
1284 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1285 if (af != 0 && af != AF_INET)
1287 "Inconsistent address family\n");
1289 memcpy(&tfe->a.a4.sip, &tmp, 4);
1290 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1291 if (af != 0 && af != AF_INET6)
1293 "Inconsistent address family\n");
1295 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1301 /* Handle <proto-num|proto-name> */
1302 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1304 errx(EX_DATAERR, "invalid key: proto missing");
1305 if ((p = strchr(arg, ',')) != NULL)
1308 key = strtol(arg, &pp, 10);
1310 if ((pent = getprotobyname(arg)) == NULL)
1311 errx(EX_DATAERR, "Unknown proto: %s",
1314 key = pent->p_proto;
1318 errx(EX_DATAERR, "Bad protocol number: %u",key);
1325 /* Handle <port-num|service-name> */
1326 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1328 errx(EX_DATAERR, "invalid key: src port missing");
1329 if ((p = strchr(arg, ',')) != NULL)
1332 port = htons(strtol(arg, &pp, 10));
1334 if ((sent = getservbyname(arg, NULL)) == NULL)
1335 errx(EX_DATAERR, "Unknown service: %s",
1337 port = sent->s_port;
1343 /* Handle <ipv4|ipv6>*/
1344 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1346 errx(EX_DATAERR, "invalid key: dst ip missing");
1347 if ((p = strchr(arg, ',')) != NULL)
1349 /* Determine family using temporary storage */
1350 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1351 if (af != 0 && af != AF_INET)
1353 "Inconsistent address family");
1355 memcpy(&tfe->a.a4.dip, &tmp, 4);
1356 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1357 if (af != 0 && af != AF_INET6)
1359 "Inconsistent address family");
1361 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1367 /* Handle <port-num|service-name> */
1368 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1370 errx(EX_DATAERR, "invalid key: dst port missing");
1371 if ((p = strchr(arg, ',')) != NULL)
1374 port = htons(strtol(arg, &pp, 10));
1376 if ((sent = getservbyname(arg, NULL)) == NULL)
1377 errx(EX_DATAERR, "Unknown service: %s",
1379 port = sent->s_port;
1390 errx(EX_DATAERR, "Unsupported table type: %d", type);
1393 tentry->subtype = af;
1394 tentry->masklen = masklen;
1398 * Tries to guess table key type.
1399 * This procedure is used in legacy table auto-create
1400 * code AND in `ipfw -n` ruleset checking.
1402 * Imported from old table_fill_xentry() parse code.
1405 guess_key_type(char *key, uint8_t *ptype)
1408 struct in6_addr addr;
1410 if (ishexnumber(*key) != 0 || *key == ':') {
1411 /* Remove / if exists */
1412 if ((p = strchr(key, '/')) != NULL)
1415 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1416 (inet_pton(AF_INET6, key, &addr) == 1)) {
1417 *ptype = IPFW_TABLE_CIDR;
1422 /* Port or any other key */
1423 /* Skip non-base 10 entries like 'fa1' */
1424 (void)strtol(key, &p, 10);
1426 *ptype = IPFW_TABLE_NUMBER;
1428 } else if ((p != key) && (*p == '.')) {
1430 * Warn on IPv4 address strings
1431 * which are "valid" for inet_aton() but not
1434 * Typical examples: '10.5' or '10.0.0.05'
1441 if (strchr(key, '.') == NULL) {
1442 *ptype = IPFW_TABLE_INTERFACE;
1446 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1449 *ptype = IPFW_TABLE_CIDR;
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)
1457 uint8_t type, tflags;
1465 if (xi->tablename[0] == '\0')
1466 error = table_get_info(oh, xi);
1471 if (g_co.test_only == 0) {
1474 tflags = xi->tflags;
1478 * We're running `ipfw -n`
1479 * Compatibility layer: try to guess key type
1482 if (guess_key_type(key, &type) != 0) {
1484 errx(EX_USAGE, "Cannot guess "
1485 "key '%s' type", key);
1487 vmask = IPFW_VTYPE_LEGACY;
1491 errx(EX_OSERR, "Error requesting table %s info",
1494 errx(EX_DATAERR, "Table %s does not exist",
1497 * Table does not exist
1498 * Compatibility layer: try to guess key type before failing.
1500 if (guess_key_type(key, &type) != 0) {
1502 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1503 "key '%s' type", oh->ntlv.name, key);
1506 vmask = IPFW_VTYPE_LEGACY;
1509 tentry_fill_key_type(key, tent, type, tflags);
1516 set_legacy_value(uint32_t val, ipfw_table_value *v)
1526 v->dscp = (uint8_t)val;
1531 tentry_fill_value(ipfw_obj_header *oh __unused, ipfw_obj_tentry *tent,
1532 char *arg, uint8_t type __unused, uint32_t vmask)
1534 struct addrinfo hints, *res;
1535 struct in_addr ipaddr;
1537 char *comma, *e, *n, *p;
1538 uint32_t a4, flag, val;
1539 ipfw_table_value *v;
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);
1550 set_legacy_value(val, v);
1553 if (inet_pton(AF_INET, arg, &val) == 1) {
1554 set_legacy_value(ntohl(val), v);
1558 if (lookup_host(arg, &ipaddr) == 0) {
1559 set_legacy_value(ntohl(ipaddr.s_addr), v);
1562 errx(EX_OSERR, "Unable to parse value %s", arg);
1566 * Shorthands: handle single value if vmask consists
1567 * of numbers only. e.g.:
1568 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1573 for (i = 1; i < (1u << 31); i *= 2) {
1574 if ((flag = (vmask & i)) == 0)
1578 if ((comma = strchr(n, ',')) != NULL)
1582 case IPFW_VTYPE_TAG:
1583 v->tag = strtol(n, &e, 10);
1587 case IPFW_VTYPE_PIPE:
1588 v->pipe = strtol(n, &e, 10);
1592 case IPFW_VTYPE_DIVERT:
1593 v->divert = strtol(n, &e, 10);
1597 case IPFW_VTYPE_SKIPTO:
1598 v->skipto = strtol(n, &e, 10);
1602 case IPFW_VTYPE_NETGRAPH:
1603 v->netgraph = strtol(n, &e, 10);
1607 case IPFW_VTYPE_FIB:
1608 v->fib = strtol(n, &e, 10);
1612 case IPFW_VTYPE_NAT:
1613 v->nat = strtol(n, &e, 10);
1617 case IPFW_VTYPE_LIMIT:
1618 v->limit = strtol(n, &e, 10);
1622 case IPFW_VTYPE_NH4:
1623 if (strchr(n, '.') != NULL &&
1624 inet_pton(AF_INET, n, &a4) == 1) {
1628 if (lookup_host(n, &ipaddr) == 0) {
1629 v->nh4 = ntohl(ipaddr.s_addr);
1634 case IPFW_VTYPE_DSCP:
1636 if ((dval = match_token(f_ipdscp, n)) != -1) {
1640 etype = "DSCP code";
1642 v->dscp = strtol(n, &e, 10);
1643 if (v->dscp > 63 || *e != '\0')
1644 etype = "DSCP value";
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;
1663 case IPFW_VTYPE_MARK:
1664 v->mark = strtol(n, &e, 16);
1671 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1676 if ((n = comma) != NULL)
1681 errx(EX_USAGE, "Not enough fields inside value");
1686 * Compare table names.
1687 * Honor number comparison.
1690 tablename_cmp(const void *a, const void *b)
1692 const ipfw_xtable_info *ia, *ib;
1694 ia = (const ipfw_xtable_info *)a;
1695 ib = (const ipfw_xtable_info *)b;
1697 return (stringnum_cmp(ia->tablename, ib->tablename));
1701 * Retrieves table list from kernel,
1702 * optionally sorts it and calls requested function for each table.
1703 * Returns 0 on success.
1706 tables_foreach(table_cb_t *f, void *arg, int sort)
1708 ipfw_obj_lheader *olh;
1709 ipfw_xtable_info *info;
1713 /* Start with reasonable default */
1714 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1717 if ((olh = calloc(1, sz)) == NULL)
1721 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1724 if (errno != ENOMEM)
1730 qsort(olh + 1, olh->count, olh->objsize,
1733 info = (ipfw_xtable_info *)(olh + 1);
1734 for (i = 0; i < olh->count; i++) {
1735 if (g_co.use_set == 0 || info->set == g_co.use_set - 1)
1737 info = (ipfw_xtable_info *)((caddr_t)info +
1748 * Retrieves all entries for given table @i in
1749 * eXtended format. Allocate buffer large enough
1750 * to store result. Called needs to free it later.
1752 * Returns 0 on success.
1755 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1757 ipfw_obj_header *oh;
1763 for (c = 0; c < 8; c++) {
1768 if ((oh = calloc(1, sz)) == NULL)
1770 table_fill_objheader(oh, i);
1771 oh->opheader.version = 1; /* Current version */
1772 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1777 if (errno != ENOMEM)
1786 * Shows all entries from @oh in human-readable format
1789 table_show_list(ipfw_obj_header *oh, int need_header)
1791 ipfw_obj_tentry *tent;
1793 ipfw_xtable_info *i;
1795 i = (ipfw_xtable_info *)(oh + 1);
1796 tent = (ipfw_obj_tentry *)(i + 1);
1799 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1803 table_show_entry(i, tent);
1804 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1810 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1811 uint32_t vmask, int print_ip)
1813 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1814 struct sockaddr_in6 sa6;
1815 uint32_t flag, i, l;
1822 * Some shorthands for printing values:
1823 * legacy assumes all values are equal, so keep the first one.
1825 if (vmask == IPFW_VTYPE_LEGACY) {
1826 if (print_ip != 0) {
1827 flag = htonl(v->tag);
1828 inet_ntop(AF_INET, &flag, buf, sz);
1830 snprintf(buf, sz, "%u", v->tag);
1834 for (i = 1; i < (1u << 31); i *= 2) {
1835 if ((flag = (vmask & i)) == 0)
1840 case IPFW_VTYPE_TAG:
1841 l = snprintf(buf, sz, "%u,", v->tag);
1843 case IPFW_VTYPE_PIPE:
1844 l = snprintf(buf, sz, "%u,", v->pipe);
1846 case IPFW_VTYPE_DIVERT:
1847 l = snprintf(buf, sz, "%d,", v->divert);
1849 case IPFW_VTYPE_SKIPTO:
1850 l = snprintf(buf, sz, "%d,", v->skipto);
1852 case IPFW_VTYPE_NETGRAPH:
1853 l = snprintf(buf, sz, "%u,", v->netgraph);
1855 case IPFW_VTYPE_FIB:
1856 l = snprintf(buf, sz, "%u,", v->fib);
1858 case IPFW_VTYPE_NAT:
1859 l = snprintf(buf, sz, "%u,", v->nat);
1861 case IPFW_VTYPE_LIMIT:
1862 l = snprintf(buf, sz, "%u,", v->limit);
1864 case IPFW_VTYPE_NH4:
1865 a4.s_addr = htonl(v->nh4);
1866 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1867 l = snprintf(buf, sz, "%s,", abuf);
1869 case IPFW_VTYPE_DSCP:
1870 l = snprintf(buf, sz, "%d,", v->dscp);
1872 case IPFW_VTYPE_NH6:
1873 sa6.sin6_family = AF_INET6;
1874 sa6.sin6_len = sizeof(sa6);
1875 sa6.sin6_addr = v->nh6;
1877 sa6.sin6_scope_id = v->zoneid;
1878 if (getnameinfo((const struct sockaddr *)&sa6,
1879 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1880 NI_NUMERICHOST) == 0)
1881 l = snprintf(buf, sz, "%s,", abuf);
1883 case IPFW_VTYPE_MARK:
1884 l = snprintf(buf, sz, "%#x,", v->mark);
1897 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1899 char tbuf[128], pval[128];
1903 struct tflow_entry *tfe;
1905 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1906 g_co.do_value_as_ip);
1909 case IPFW_TABLE_ADDR:
1910 /* IPv4 or IPv6 prefixes */
1911 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1912 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1914 case IPFW_TABLE_MAC:
1917 printf("%02x:%02x:%02x:%02x:%02x:%02x/%u %s\n",
1918 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
1919 tent->masklen, pval);
1921 case IPFW_TABLE_INTERFACE:
1922 /* Interface names */
1923 printf("%s %s\n", tent->k.iface, pval);
1925 case IPFW_TABLE_NUMBER:
1927 printf("%u %s\n", tent->k.key, pval);
1929 case IPFW_TABLE_FLOW:
1931 tfe = &tent->k.flow;
1934 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1935 if (tfe->af == AF_INET)
1936 paddr = &tfe->a.a4.sip;
1938 paddr = &tfe->a.a6.sip6;
1940 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1941 printf("%s%s", comma, tbuf);
1945 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1946 printf("%s%d", comma, tfe->proto);
1950 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1951 printf("%s%d", comma, ntohs(tfe->sport));
1954 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1955 if (tfe->af == AF_INET)
1956 paddr = &tfe->a.a4.dip;
1958 paddr = &tfe->a.a6.dip6;
1960 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1961 printf("%s%s", comma, tbuf);
1965 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1966 printf("%s%d", comma, ntohs(tfe->dport));
1970 printf(" %s\n", pval);
1975 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1977 ipfw_obj_lheader req, *olh;
1980 memset(&req, 0, sizeof(req));
1983 if (do_get3(opcode, &req.opheader, &sz) != 0)
1984 if (errno != ENOMEM)
1988 if ((olh = calloc(1, sz)) == NULL)
1992 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
2002 table_do_get_algolist(ipfw_obj_lheader **polh)
2005 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
2009 table_do_get_vlist(ipfw_obj_lheader **polh)
2012 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
2016 ipfw_list_ta(int ac __unused, char *av[] __unused)
2018 ipfw_obj_lheader *olh;
2024 error = table_do_get_algolist(&olh);
2026 err(EX_OSERR, "Unable to request algorithm list");
2028 info = (ipfw_ta_info *)(olh + 1);
2029 for (i = 0; i < olh->count; i++) {
2030 if ((atype = match_value(tabletypes, info->type)) == NULL)
2032 printf("--- %s ---\n", info->algoname);
2033 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
2035 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
2043 compare_values(const void *_a, const void *_b)
2045 const ipfw_table_value *a, *b;
2047 a = (const ipfw_table_value *)_a;
2048 b = (const ipfw_table_value *)_b;
2050 if (a->kidx < b->kidx)
2052 else if (a->kidx > b->kidx)
2059 ipfw_list_values(int ac __unused, char *av[] __unused)
2062 ipfw_obj_lheader *olh;
2063 ipfw_table_value *v;
2067 error = table_do_get_vlist(&olh);
2069 err(EX_OSERR, "Unable to request value list");
2071 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2073 table_print_valheader(buf, sizeof(buf), vmask);
2074 printf("HEADER: %s\n", buf);
2075 v = (ipfw_table_value *)(olh + 1);
2076 qsort(v, olh->count, olh->objsize, compare_values);
2077 for (i = 0; i < olh->count; i++) {
2078 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2080 printf("[%u] refs=%lu %s\n", v->kidx, (u_long)v->refcnt, buf);
2081 v = (ipfw_table_value *)((caddr_t)v + olh->objsize);
2088 table_check_name(const char *tablename)
2091 if (ipfw_check_object_name(tablename) != 0)
2093 /* Restrict some 'special' names */
2094 if (strcmp(tablename, "all") == 0)