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.
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <sys/sysctl.h>
35 #include <netinet/in.h>
36 #include <netinet/ip_fw.h>
37 #include <arpa/inet.h>
42 static void table_modify_record(ipfw_obj_header *oh, int ac, char *av[],
43 int add, int quiet, int update, int atomic);
44 static int table_flush(ipfw_obj_header *oh);
45 static int table_destroy(ipfw_obj_header *oh);
46 static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i);
47 static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i);
48 static int table_do_swap(ipfw_obj_header *oh, char *second);
49 static void table_create(ipfw_obj_header *oh, int ac, char *av[]);
50 static void table_modify(ipfw_obj_header *oh, int ac, char *av[]);
51 static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]);
52 static void table_lock(ipfw_obj_header *oh, int lock);
53 static int table_swap(ipfw_obj_header *oh, char *second);
54 static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i);
55 static int table_show_info(ipfw_xtable_info *i, void *arg);
57 static int table_destroy_one(ipfw_xtable_info *i, void *arg);
58 static int table_flush_one(ipfw_xtable_info *i, void *arg);
59 static int table_show_one(ipfw_xtable_info *i, void *arg);
60 static int table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh);
61 static void table_show_list(ipfw_obj_header *oh, int need_header);
62 static void table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent);
64 static void tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
65 char *key, int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi);
66 static void tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
67 char *arg, uint8_t type, uint32_t vmask);
68 static void table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
69 uint32_t vmask, int print_ip);
71 typedef int (table_cb_t)(ipfw_xtable_info *i, void *arg);
72 static int tables_foreach(table_cb_t *f, void *arg, int sort);
75 #define s6_addr32 __u6_addr.__u6_addr32
78 static struct _s_x tabletypes[] = {
79 { "addr", IPFW_TABLE_ADDR },
80 { "iface", IPFW_TABLE_INTERFACE },
81 { "number", IPFW_TABLE_NUMBER },
82 { "flow", IPFW_TABLE_FLOW },
86 static struct _s_x tablevaltypes[] = {
87 { "skipto", IPFW_VTYPE_SKIPTO },
88 { "pipe", IPFW_VTYPE_PIPE },
89 { "fib", IPFW_VTYPE_FIB },
90 { "nat", IPFW_VTYPE_NAT },
91 { "dscp", IPFW_VTYPE_DSCP },
92 { "tag", IPFW_VTYPE_TAG },
93 { "divert", IPFW_VTYPE_DIVERT },
94 { "netgraph", IPFW_VTYPE_NETGRAPH },
95 { "limit", IPFW_VTYPE_LIMIT },
96 { "ipv4", IPFW_VTYPE_NH4 },
97 { "ipv6", IPFW_VTYPE_NH6 },
101 static struct _s_x tablecmds[] = {
103 { "delete", TOK_DEL },
104 { "create", TOK_CREATE },
105 { "destroy", TOK_DESTROY },
106 { "flush", TOK_FLUSH },
107 { "modify", TOK_MODIFY },
108 { "swap", TOK_SWAP },
109 { "info", TOK_INFO },
110 { "detail", TOK_DETAIL },
111 { "list", TOK_LIST },
112 { "lookup", TOK_LOOKUP },
113 { "atomic", TOK_ATOMIC },
114 { "lock", TOK_LOCK },
115 { "unlock", TOK_UNLOCK },
120 lookup_host (char *host, struct in_addr *ipaddr)
124 if (!inet_aton(host, ipaddr)) {
125 if ((he = gethostbyname(host)) == NULL)
127 *ipaddr = *(struct in_addr *)he->h_addr_list[0];
133 * This one handles all table-related commands
134 * ipfw table NAME create ...
135 * ipfw table NAME modify ...
136 * ipfw table {NAME | all} destroy
137 * ipfw table NAME swap NAME
138 * ipfw table NAME lock
139 * ipfw table NAME unlock
140 * ipfw table NAME add addr[/masklen] [value]
141 * ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
142 * ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
143 * ipfw table NAME lookup addr
144 * ipfw table {NAME | all} flush
145 * ipfw table {NAME | all} list
146 * ipfw table {NAME | all} info
147 * ipfw table {NAME | all} detail
150 ipfw_table_handler(int ac, char *av[])
153 int atomic, error, tcmd;
160 memset(&oh, 0, sizeof(oh));
163 set = co.use_set - 1;
168 NEED1("table needs name");
171 if (table_check_name(tablename) == 0) {
172 table_fill_ntlv(&oh.ntlv, *av, set, 1);
175 if (strcmp(tablename, "all") == 0)
178 errx(EX_USAGE, "table name %s is invalid", tablename);
181 NEED1("table needs command");
183 tcmd = get_token(tablecmds, *av, "table command");
184 /* Check if atomic operation was requested */
186 if (tcmd == TOK_ATOMIC) {
188 NEED1("atomic needs command");
189 tcmd = get_token(tablecmds, *av, "table command");
194 errx(EX_USAGE, "atomic is not compatible with %s", *av);
208 errx(EX_USAGE, "table name required");
214 do_add = **av == 'a';
216 table_modify_record(&oh, ac, av, do_add, co.do_quiet,
217 co.do_quiet, atomic);
221 table_create(&oh, ac, av);
225 table_modify(&oh, ac, av);
229 if (table_destroy(&oh) == 0)
232 err(EX_OSERR, "failed to destroy table %s",
234 /* ESRCH isn't fatal, warn if not quiet mode */
235 if (co.do_quiet == 0)
236 warn("failed to destroy table %s", tablename);
238 error = tables_foreach(table_destroy_one, &oh, 1);
241 "failed to destroy tables list");
246 if ((error = table_flush(&oh)) == 0)
249 err(EX_OSERR, "failed to flush table %s info",
251 /* ESRCH isn't fatal, warn if not quiet mode */
252 if (co.do_quiet == 0)
253 warn("failed to flush table %s info",
256 error = tables_foreach(table_flush_one, &oh, 1);
258 err(EX_OSERR, "failed to flush tables list");
259 /* XXX: we ignore errors here */
264 NEED1("second table name required");
265 table_swap(&oh, *av);
269 table_lock(&oh, (tcmd == TOK_LOCK));
273 arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL;
275 if ((error = table_get_info(&oh, &i)) != 0)
276 err(EX_OSERR, "failed to request table info");
277 table_show_info(&i, arg);
279 error = tables_foreach(table_show_info, arg, 1);
281 err(EX_OSERR, "failed to request tables list");
285 arg = is_all ? (void*)1 : NULL;
288 if ((error = table_get_info(&oh, &i)) != 0)
289 err(EX_OSERR, "failed to request table info");
290 table_show_one(&i, arg);
292 error = tables_foreach(table_show_one, arg, 1);
294 err(EX_OSERR, "failed to request tables list");
299 table_lookup(&oh, ac, av);
305 table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
309 ntlv->head.type = IPFW_TLV_TBL_NAME;
310 ntlv->head.length = sizeof(ipfw_obj_ntlv);
313 strlcpy(ntlv->name, name, sizeof(ntlv->name));
317 table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
321 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
324 static struct _s_x tablenewcmds[] = {
325 { "type", TOK_TYPE },
326 { "valtype", TOK_VALTYPE },
327 { "algo", TOK_ALGO },
328 { "limit", TOK_LIMIT },
329 { "locked", TOK_LOCK },
330 { "missing", TOK_MISSING },
331 { "or-flush", TOK_ORFLUSH },
335 static struct _s_x flowtypecmds[] = {
336 { "src-ip", IPFW_TFFLAG_SRCIP },
337 { "proto", IPFW_TFFLAG_PROTO },
338 { "src-port", IPFW_TFFLAG_SRCPORT },
339 { "dst-ip", IPFW_TFFLAG_DSTIP },
340 { "dst-port", IPFW_TFFLAG_DSTPORT },
345 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
347 uint32_t fset, fclear;
350 /* Parse type options */
352 case IPFW_TABLE_FLOW:
354 if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
356 "unable to parse flow option %s", e);
367 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
372 if ((tname = match_value(tabletypes, type)) == NULL)
375 l = snprintf(tbuf, size, "%s", tname);
380 case IPFW_TABLE_FLOW:
384 print_flags_buffer(tbuf, size, flowtypecmds, tflags);
393 * ipfw table NAME create [ type { addr | iface | number | flow } ]
394 * [ algo algoname ] [missing] [or-flush]
397 table_create(ipfw_obj_header *oh, int ac, char *av[])
399 ipfw_xtable_info xi, xie;
400 int error, missing, orflush, tcmd, val;
401 uint32_t fset, fclear;
405 missing = orflush = 0;
406 memset(&xi, 0, sizeof(xi));
408 tcmd = get_token(tablenewcmds, *av, "option");
413 NEED1("limit value required");
414 xi.limit = strtol(*av, NULL, 10);
418 NEED1("table type required");
419 /* Type may have suboptions after ':' */
420 if ((p = strchr(*av, ':')) != NULL)
422 val = match_token(tabletypes, *av);
424 concat_tokens(tbuf, sizeof(tbuf), tabletypes,
427 "Unknown tabletype: %s. Supported: %s",
432 error = table_parse_type(val, p, &xi.tflags);
435 "Unsupported suboptions: %s", p);
440 NEED1("table value type required");
442 val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
448 concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
449 errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
453 NEED1("table algorithm name required");
454 if (strlen(*av) > sizeof(xi.algoname))
455 errx(EX_USAGE, "algorithm name too long");
456 strlcpy(xi.algoname, *av, sizeof(xi.algoname));
460 xi.flags |= IPFW_TGFLAGS_LOCKED;
471 /* Set some defaults to preserve compatibility. */
472 if (xi.algoname[0] == '\0' && xi.type == 0)
473 xi.type = IPFW_TABLE_ADDR;
475 xi.vmask = IPFW_VTYPE_LEGACY;
477 error = table_do_create(oh, &xi);
482 if (errno != EEXIST || missing == 0)
483 err(EX_OSERR, "Table creation failed");
485 /* Check that existing table is the same we are trying to create */
486 if (table_get_info(oh, &xie) != 0)
487 err(EX_OSERR, "Existing table check failed");
489 if (xi.limit != xie.limit || xi.type != xie.type ||
490 xi.tflags != xie.tflags || xi.vmask != xie.vmask || (
491 xi.algoname[0] != '\0' && strcmp(xi.algoname,
492 xie.algoname) != 0) || xi.flags != xie.flags)
493 errx(EX_DATAERR, "The existing table is not compatible "
494 "with one you are creating.");
496 /* Flush existing table if instructed to do so */
497 if (orflush != 0 && table_flush(oh) != 0)
498 err(EX_OSERR, "Table flush on creation failed");
504 * Request: [ ipfw_obj_header ipfw_xtable_info ]
506 * Returns 0 on success.
509 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
511 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
514 memcpy(tbuf, oh, sizeof(*oh));
515 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
516 oh = (ipfw_obj_header *)tbuf;
518 error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
524 * Modifies existing table
526 * ipfw table NAME modify [ limit number ]
529 table_modify(ipfw_obj_header *oh, int ac, char *av[])
534 memset(&xi, 0, sizeof(xi));
537 tcmd = get_token(tablenewcmds, *av, "option");
542 NEED1("limit value required");
543 xi.limit = strtol(*av, NULL, 10);
544 xi.mflags |= IPFW_TMFLAGS_LIMIT;
548 errx(EX_USAGE, "cmd is not supported for modification");
552 if (table_do_modify(oh, &xi) != 0)
553 err(EX_OSERR, "Table modification failed");
557 * Modifies existing table.
559 * Request: [ ipfw_obj_header ipfw_xtable_info ]
561 * Returns 0 on success.
564 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
566 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
569 memcpy(tbuf, oh, sizeof(*oh));
570 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
571 oh = (ipfw_obj_header *)tbuf;
573 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
579 * Locks or unlocks given table
582 table_lock(ipfw_obj_header *oh, int lock)
586 memset(&xi, 0, sizeof(xi));
588 xi.mflags |= IPFW_TMFLAGS_LOCK;
589 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
591 if (table_do_modify(oh, &xi) != 0)
592 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
596 * Destroys given table specified by @oh->ntlv.
597 * Returns 0 on success.
600 table_destroy(ipfw_obj_header *oh)
603 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
610 table_destroy_one(ipfw_xtable_info *i, void *arg)
614 oh = (ipfw_obj_header *)arg;
615 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
616 if (table_destroy(oh) != 0) {
617 if (co.do_quiet == 0)
618 warn("failed to destroy table(%s) in set %u",
619 i->tablename, i->set);
626 * Flushes given table specified by @oh->ntlv.
627 * Returns 0 on success.
630 table_flush(ipfw_obj_header *oh)
633 if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
640 table_do_swap(ipfw_obj_header *oh, char *second)
642 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
645 memset(tbuf, 0, sizeof(tbuf));
646 memcpy(tbuf, oh, sizeof(*oh));
647 oh = (ipfw_obj_header *)tbuf;
648 table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
650 error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
656 * Swaps given table with @second one.
659 table_swap(ipfw_obj_header *oh, char *second)
662 if (table_check_name(second) != 0)
663 errx(EX_USAGE, "table name %s is invalid", second);
665 if (table_do_swap(oh, second) == 0)
670 errx(EX_USAGE, "Unable to swap table: check types");
672 errx(EX_USAGE, "Unable to swap table: check limits");
680 * Retrieves table in given table specified by @oh->ntlv.
682 * Returns 0 on success.
685 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
687 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
691 memset(tbuf, 0, sizeof(tbuf));
692 memcpy(tbuf, oh, sizeof(*oh));
693 oh = (ipfw_obj_header *)tbuf;
695 if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
698 if (sz < sizeof(tbuf))
701 *i = *(ipfw_xtable_info *)(oh + 1);
706 static struct _s_x tablealgoclass[] = {
707 { "hash", IPFW_TACLASS_HASH },
708 { "array", IPFW_TACLASS_ARRAY },
709 { "radix", IPFW_TACLASS_RADIX },
723 * Print global/per-AF table @i algorithm info.
726 table_show_tainfo(ipfw_xtable_info *i, struct ta_cldata *d,
727 const char *af, const char *taclass)
730 switch (d->taclass) {
731 case IPFW_TACLASS_HASH:
732 case IPFW_TACLASS_ARRAY:
733 printf(" %salgorithm %s info\n", af, taclass);
734 if (d->itemsize == d->itemsize6)
735 printf(" size: %u items: %u itemsize: %u\n",
736 d->size, d->count, d->itemsize);
738 printf(" size: %u items: %u "
739 "itemsize4: %u itemsize6: %u\n",
741 d->itemsize, d->itemsize6);
743 case IPFW_TACLASS_RADIX:
744 printf(" %salgorithm %s info\n", af, taclass);
745 if (d->itemsize == d->itemsize6)
746 printf(" items: %u itemsize: %u\n",
747 d->count, d->itemsize);
750 "itemsize4: %u itemsize6: %u\n",
751 d->count, d->itemsize, d->itemsize6);
754 printf(" algo class: %s\n", taclass);
759 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
762 if (vmask == IPFW_VTYPE_LEGACY) {
763 snprintf(buf, bufsize, "legacy");
767 memset(buf, 0, bufsize);
768 print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
772 * Prints table info struct @i in human-readable form.
775 table_show_info(ipfw_xtable_info *i, void *arg)
778 ipfw_ta_tinfo *tainfo;
781 char ttype[64], tvtype[64];
783 table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
784 table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
786 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
787 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
788 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
790 printf(" kindex: %d, type: %s\n", i->kidx, ttype);
791 printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
792 printf(" algorithm: %s\n", i->algoname);
793 printf(" items: %u, size: %u\n", i->count, i->size);
795 printf(" limit: %u\n", i->limit);
797 /* Print algo-specific info if requested & set */
801 if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
803 tainfo = &i->ta_info;
807 if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
809 if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
812 memset(&d, 0, sizeof(d));
813 d.taclass = tainfo->taclass4;
814 d.size = tainfo->size4;
815 d.count = tainfo->count4;
816 d.itemsize = tainfo->itemsize4;
817 if (afdata == 0 && afitem != 0)
818 d.itemsize6 = tainfo->itemsize6;
820 d.itemsize6 = d.itemsize;
821 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
825 table_show_tainfo(i, &d, "", vtype);
827 table_show_tainfo(i, &d, "IPv4 ", vtype);
828 memset(&d, 0, sizeof(d));
829 d.taclass = tainfo->taclass6;
830 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
832 d.size = tainfo->size6;
833 d.count = tainfo->count6;
834 d.itemsize = tainfo->itemsize6;
835 d.itemsize6 = d.itemsize;
836 table_show_tainfo(i, &d, "IPv6 ", vtype);
844 * Function wrappers which can be used either
845 * as is or as foreach function parameter.
849 table_show_one(ipfw_xtable_info *i, void *arg)
855 is_all = arg == NULL ? 0 : 1;
857 if ((error = table_do_get_list(i, &oh)) != 0) {
858 err(EX_OSERR, "Error requesting table %s list", i->tablename);
862 table_show_list(oh, is_all);
869 table_flush_one(ipfw_xtable_info *i, void *arg)
873 oh = (ipfw_obj_header *)arg;
875 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
877 return (table_flush(oh));
881 table_do_modify_record(int cmd, ipfw_obj_header *oh,
882 ipfw_obj_tentry *tent, int count, int atomic)
885 ipfw_obj_tentry *tent_base;
887 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
891 sz = sizeof(*ctlv) + sizeof(*tent) * count;
893 memset(xbuf, 0, sizeof(xbuf));
896 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
900 memcpy(pbuf, oh, sizeof(*oh));
901 oh = (ipfw_obj_header *)pbuf;
902 oh->opheader.version = 1;
904 ctlv = (ipfw_obj_ctlv *)(oh + 1);
906 ctlv->head.length = sz;
908 ctlv->flags |= IPFW_CTF_ATOMIC;
911 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
912 tent = (ipfw_obj_tentry *)(ctlv + 1);
913 for (i = 0; i < count; i++, tent++) {
914 tent->head.length = sizeof(ipfw_obj_tentry);
919 error = do_get3(cmd, &oh->opheader, &sz);
922 tent = (ipfw_obj_tentry *)(ctlv + 1);
923 /* Copy result back to provided buffer */
924 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
933 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
934 int quiet, int update, int atomic)
936 ipfw_obj_tentry *ptent, tent, *tent_buf;
940 int cmd, count, error, i, ignored;
941 char *texterr, *etxt, *px;
944 errx(EX_USAGE, "address required");
947 cmd = IP_FW_TABLE_XADD;
948 texterr = "Adding record failed";
950 cmd = IP_FW_TABLE_XDEL;
951 texterr = "Deleting record failed";
955 * Calculate number of entries:
956 * Assume [key val] x N for add
960 count = (add != 0) ? ac / 2 + 1 : ac;
963 /* Adding single entry with/without value */
964 memset(&tent, 0, sizeof(tent));
968 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
970 "Unable to allocate memory for all entries");
974 memset(&xi, 0, sizeof(xi));
977 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
980 * Compatibility layer: auto-create table if not exists.
982 if (xi.tablename[0] == '\0') {
985 strlcpy(xi.tablename, oh->ntlv.name,
986 sizeof(xi.tablename));
988 warnx("DEPRECATED: inserting data into "
989 "non-existent table %s. (auto-created)",
991 table_do_create(oh, &xi);
994 oh->ntlv.type = type;
997 if (add != 0 && ac > 0) {
998 tentry_fill_value(oh, ptent, *av, type, vmask);
1003 ptent->head.flags |= IPFW_TF_UPDATE;
1009 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
1012 * Compatibility stuff: do not yell on duplicate keys or
1015 if (error == 0 || (error == EEXIST && add != 0) ||
1016 (error == ENOENT && add == 0)) {
1018 if (tent_buf != &tent)
1024 /* Report results back */
1026 for (i = 0; i < count; ptent++, i++) {
1028 switch (ptent->result) {
1032 case IPFW_TR_DELETED:
1035 case IPFW_TR_UPDATED:
1046 case IPFW_TR_NOTFOUND:
1050 case IPFW_TR_EXISTS:
1054 case IPFW_TR_IGNORED:
1063 if (error != 0 && atomic != 0 && ignored == 0)
1064 printf("%s(reverted): ", px);
1068 table_show_entry(&xi, ptent);
1071 if (tent_buf != &tent)
1076 /* Get real OS error */
1079 /* Try to provide more human-readable error */
1082 etxt = "record already exists";
1088 etxt = "table not found";
1091 etxt = "record not found";
1094 etxt = "table is locked";
1097 etxt = strerror(error);
1100 errx(EX_OSERR, "%s: %s", texterr, etxt);
1104 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1105 ipfw_obj_tentry *xtent)
1107 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1108 ipfw_obj_tentry *tent;
1113 memcpy(xbuf, oh, sizeof(*oh));
1114 oh = (ipfw_obj_header *)xbuf;
1115 tent = (ipfw_obj_tentry *)(oh + 1);
1117 memset(tent, 0, sizeof(*tent));
1118 tent->head.length = sizeof(*tent);
1121 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1122 oh->ntlv.type = type;
1125 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1128 if (sz < sizeof(xbuf))
1137 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1139 ipfw_obj_tentry xtent;
1140 ipfw_xtable_info xi;
1145 errx(EX_USAGE, "address required");
1147 strlcpy(key, *av, sizeof(key));
1149 memset(&xi, 0, sizeof(xi));
1150 error = table_do_lookup(oh, key, &xi, &xtent);
1156 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1158 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1160 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1161 "\"lookup\" method", oh->ntlv.name);
1163 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1166 table_show_entry(&xi, &xtent);
1170 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1175 struct in6_addr *paddr, tmp;
1176 struct tflow_entry *tfe;
1177 uint32_t key, *pkey;
1179 struct protoent *pent;
1180 struct servent *sent;
1185 paddr = (struct in6_addr *)&tentry->k;
1188 case IPFW_TABLE_ADDR:
1189 /* Remove / if exists */
1190 if ((p = strchr(arg, '/')) != NULL) {
1195 if (inet_pton(AF_INET, arg, paddr) == 1) {
1196 if (p != NULL && mask > 32)
1197 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1200 masklen = p ? mask : 32;
1202 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1203 if (IN6_IS_ADDR_V4COMPAT(paddr))
1205 "Use IPv4 instead of v4-compatible");
1206 if (p != NULL && mask > 128)
1207 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1210 masklen = p ? mask : 128;
1214 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1215 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1218 type = IPFW_TABLE_ADDR;
1222 case IPFW_TABLE_INTERFACE:
1223 /* Assume interface name. Copy significant data only */
1224 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1225 memcpy(paddr, arg, mask);
1226 /* Set mask to exact match */
1227 masklen = 8 * IF_NAMESIZE;
1229 case IPFW_TABLE_NUMBER:
1230 /* Port or any other key */
1231 key = strtol(arg, &p, 10);
1233 errx(EX_DATAERR, "Invalid number: %s", arg);
1235 pkey = (uint32_t *)paddr;
1239 case IPFW_TABLE_FLOW:
1240 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1241 tfe = &tentry->k.flow;
1244 /* Handle <ipv4|ipv6> */
1245 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1246 if ((p = strchr(arg, ',')) != NULL)
1248 /* Determine family using temporary storage */
1249 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1250 if (af != 0 && af != AF_INET)
1252 "Inconsistent address family\n");
1254 memcpy(&tfe->a.a4.sip, &tmp, 4);
1255 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1256 if (af != 0 && af != AF_INET6)
1258 "Inconsistent address family\n");
1260 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1266 /* Handle <proto-num|proto-name> */
1267 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1269 errx(EX_DATAERR, "invalid key: proto missing");
1270 if ((p = strchr(arg, ',')) != NULL)
1273 key = strtol(arg, &pp, 10);
1275 if ((pent = getprotobyname(arg)) == NULL)
1276 errx(EX_DATAERR, "Unknown proto: %s",
1279 key = pent->p_proto;
1283 errx(EX_DATAERR, "Bad protocol number: %u",key);
1290 /* Handle <port-num|service-name> */
1291 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1293 errx(EX_DATAERR, "invalid key: src port missing");
1294 if ((p = strchr(arg, ',')) != NULL)
1297 port = htons(strtol(arg, &pp, 10));
1299 if ((sent = getservbyname(arg, NULL)) == NULL)
1300 errx(EX_DATAERR, "Unknown service: %s",
1302 port = sent->s_port;
1308 /* Handle <ipv4|ipv6>*/
1309 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1311 errx(EX_DATAERR, "invalid key: dst ip missing");
1312 if ((p = strchr(arg, ',')) != NULL)
1314 /* Determine family using temporary storage */
1315 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1316 if (af != 0 && af != AF_INET)
1318 "Inconsistent address family");
1320 memcpy(&tfe->a.a4.dip, &tmp, 4);
1321 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1322 if (af != 0 && af != AF_INET6)
1324 "Inconsistent address family");
1326 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1332 /* Handle <port-num|service-name> */
1333 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1335 errx(EX_DATAERR, "invalid key: dst port missing");
1336 if ((p = strchr(arg, ',')) != NULL)
1339 port = htons(strtol(arg, &pp, 10));
1341 if ((sent = getservbyname(arg, NULL)) == NULL)
1342 errx(EX_DATAERR, "Unknown service: %s",
1344 port = sent->s_port;
1355 errx(EX_DATAERR, "Unsupported table type: %d", type);
1358 tentry->subtype = af;
1359 tentry->masklen = masklen;
1363 * Tries to guess table key type.
1364 * This procedure is used in legacy table auto-create
1365 * code AND in `ipfw -n` ruleset checking.
1367 * Imported from old table_fill_xentry() parse code.
1370 guess_key_type(char *key, uint8_t *ptype)
1373 struct in6_addr addr;
1376 if (ishexnumber(*key) != 0 || *key == ':') {
1377 /* Remove / if exists */
1378 if ((p = strchr(key, '/')) != NULL)
1381 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1382 (inet_pton(AF_INET6, key, &addr) == 1)) {
1383 *ptype = IPFW_TABLE_CIDR;
1388 /* Port or any other key */
1389 /* Skip non-base 10 entries like 'fa1' */
1390 kv = strtol(key, &p, 10);
1392 *ptype = IPFW_TABLE_NUMBER;
1394 } else if ((p != key) && (*p == '.')) {
1396 * Warn on IPv4 address strings
1397 * which are "valid" for inet_aton() but not
1400 * Typical examples: '10.5' or '10.0.0.05'
1407 if (strchr(key, '.') == NULL) {
1408 *ptype = IPFW_TABLE_INTERFACE;
1412 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1415 *ptype = IPFW_TABLE_CIDR;
1420 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1421 int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1423 uint8_t type, tflags;
1431 if (xi->tablename[0] == '\0')
1432 error = table_get_info(oh, xi);
1437 if (co.test_only == 0) {
1440 tflags = xi->tflags;
1444 * We're running `ipfw -n`
1445 * Compatibility layer: try to guess key type
1448 if (guess_key_type(key, &type) != 0) {
1450 errx(EX_USAGE, "Cannot guess "
1451 "key '%s' type", key);
1453 vmask = IPFW_VTYPE_LEGACY;
1457 errx(EX_OSERR, "Error requesting table %s info",
1460 errx(EX_DATAERR, "Table %s does not exist",
1463 * Table does not exist
1464 * Compatibility layer: try to guess key type before failing.
1466 if (guess_key_type(key, &type) != 0) {
1468 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1469 "key '%s' type", oh->ntlv.name, key);
1472 vmask = IPFW_VTYPE_LEGACY;
1475 tentry_fill_key_type(key, tent, type, tflags);
1482 set_legacy_value(uint32_t val, ipfw_table_value *v)
1492 v->dscp = (uint8_t)val;
1497 tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *arg,
1498 uint8_t type, uint32_t vmask)
1500 struct addrinfo hints, *res;
1501 uint32_t a4, flag, val;
1502 ipfw_table_value *v;
1505 char *comma, *e, *etype, *n, *p;
1506 struct in_addr ipaddr;
1510 /* Compat layer: keep old behavior for legacy value types */
1511 if (vmask == IPFW_VTYPE_LEGACY) {
1512 /* Try to interpret as number first */
1513 val = strtoul(arg, &p, 0);
1515 set_legacy_value(val, v);
1518 if (inet_pton(AF_INET, arg, &val) == 1) {
1519 set_legacy_value(ntohl(val), v);
1523 if (lookup_host(arg, &ipaddr) == 0) {
1524 set_legacy_value(ntohl(ipaddr.s_addr), v);
1527 errx(EX_OSERR, "Unable to parse value %s", arg);
1531 * Shorthands: handle single value if vmask consists
1532 * of numbers only. e.g.:
1533 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1538 for (i = 1; i < (1 << 31); i *= 2) {
1539 if ((flag = (vmask & i)) == 0)
1543 if ((comma = strchr(n, ',')) != NULL)
1547 case IPFW_VTYPE_TAG:
1548 v->tag = strtol(n, &e, 10);
1552 case IPFW_VTYPE_PIPE:
1553 v->pipe = strtol(n, &e, 10);
1557 case IPFW_VTYPE_DIVERT:
1558 v->divert = strtol(n, &e, 10);
1562 case IPFW_VTYPE_SKIPTO:
1563 v->skipto = strtol(n, &e, 10);
1567 case IPFW_VTYPE_NETGRAPH:
1568 v->netgraph = strtol(n, &e, 10);
1572 case IPFW_VTYPE_FIB:
1573 v->fib = strtol(n, &e, 10);
1577 case IPFW_VTYPE_NAT:
1578 v->nat = strtol(n, &e, 10);
1582 case IPFW_VTYPE_LIMIT:
1583 v->limit = strtol(n, &e, 10);
1587 case IPFW_VTYPE_NH4:
1588 if (strchr(n, '.') != NULL &&
1589 inet_pton(AF_INET, n, &a4) == 1) {
1593 if (lookup_host(n, &ipaddr) == 0) {
1594 v->nh4 = ntohl(ipaddr.s_addr);
1599 case IPFW_VTYPE_DSCP:
1601 if ((dval = match_token(f_ipdscp, n)) != -1) {
1605 etype = "DSCP code";
1607 v->dscp = strtol(n, &e, 10);
1608 if (v->dscp > 63 || *e != '\0')
1609 etype = "DSCP value";
1612 case IPFW_VTYPE_NH6:
1613 if (strchr(n, ':') != NULL) {
1614 memset(&hints, 0, sizeof(hints));
1615 hints.ai_family = AF_INET6;
1616 hints.ai_flags = AI_NUMERICHOST;
1617 if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1618 v->nh6 = ((struct sockaddr_in6 *)
1619 res->ai_addr)->sin6_addr;
1620 v->zoneid = ((struct sockaddr_in6 *)
1621 res->ai_addr)->sin6_scope_id;
1631 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1636 if ((n = comma) != NULL)
1641 errx(EX_USAGE, "Not enough fields inside value");
1646 * Compare table names.
1647 * Honor number comparison.
1650 tablename_cmp(const void *a, const void *b)
1652 ipfw_xtable_info *ia, *ib;
1654 ia = (ipfw_xtable_info *)a;
1655 ib = (ipfw_xtable_info *)b;
1657 return (stringnum_cmp(ia->tablename, ib->tablename));
1661 * Retrieves table list from kernel,
1662 * optionally sorts it and calls requested function for each table.
1663 * Returns 0 on success.
1666 tables_foreach(table_cb_t *f, void *arg, int sort)
1668 ipfw_obj_lheader *olh;
1669 ipfw_xtable_info *info;
1673 /* Start with reasonable default */
1674 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1677 if ((olh = calloc(1, sz)) == NULL)
1681 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1684 if (errno != ENOMEM)
1690 qsort(olh + 1, olh->count, olh->objsize,
1693 info = (ipfw_xtable_info *)(olh + 1);
1694 for (i = 0; i < olh->count; i++) {
1695 if (co.use_set == 0 || info->set == co.use_set - 1)
1696 error = f(info, arg);
1697 info = (ipfw_xtable_info *)((caddr_t)info +
1708 * Retrieves all entries for given table @i in
1709 * eXtended format. Allocate buffer large enough
1710 * to store result. Called needs to free it later.
1712 * Returns 0 on success.
1715 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1717 ipfw_obj_header *oh;
1723 for (c = 0; c < 8; c++) {
1728 if ((oh = calloc(1, sz)) == NULL)
1730 table_fill_objheader(oh, i);
1731 oh->opheader.version = 1; /* Current version */
1732 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1737 if (errno != ENOMEM)
1746 * Shows all entries from @oh in human-readable format
1749 table_show_list(ipfw_obj_header *oh, int need_header)
1751 ipfw_obj_tentry *tent;
1753 ipfw_xtable_info *i;
1755 i = (ipfw_xtable_info *)(oh + 1);
1756 tent = (ipfw_obj_tentry *)(i + 1);
1759 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1763 table_show_entry(i, tent);
1764 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1770 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1771 uint32_t vmask, int print_ip)
1773 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1774 struct sockaddr_in6 sa6;
1775 uint32_t flag, i, l;
1782 * Some shorthands for printing values:
1783 * legacy assumes all values are equal, so keep the first one.
1785 if (vmask == IPFW_VTYPE_LEGACY) {
1786 if (print_ip != 0) {
1787 flag = htonl(v->tag);
1788 inet_ntop(AF_INET, &flag, buf, sz);
1790 snprintf(buf, sz, "%u", v->tag);
1794 for (i = 1; i < (1 << 31); i *= 2) {
1795 if ((flag = (vmask & i)) == 0)
1800 case IPFW_VTYPE_TAG:
1801 l = snprintf(buf, sz, "%u,", v->tag);
1803 case IPFW_VTYPE_PIPE:
1804 l = snprintf(buf, sz, "%u,", v->pipe);
1806 case IPFW_VTYPE_DIVERT:
1807 l = snprintf(buf, sz, "%d,", v->divert);
1809 case IPFW_VTYPE_SKIPTO:
1810 l = snprintf(buf, sz, "%d,", v->skipto);
1812 case IPFW_VTYPE_NETGRAPH:
1813 l = snprintf(buf, sz, "%u,", v->netgraph);
1815 case IPFW_VTYPE_FIB:
1816 l = snprintf(buf, sz, "%u,", v->fib);
1818 case IPFW_VTYPE_NAT:
1819 l = snprintf(buf, sz, "%u,", v->nat);
1821 case IPFW_VTYPE_LIMIT:
1822 l = snprintf(buf, sz, "%u,", v->limit);
1824 case IPFW_VTYPE_NH4:
1825 a4.s_addr = htonl(v->nh4);
1826 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1827 l = snprintf(buf, sz, "%s,", abuf);
1829 case IPFW_VTYPE_DSCP:
1830 l = snprintf(buf, sz, "%d,", v->dscp);
1832 case IPFW_VTYPE_NH6:
1833 sa6.sin6_family = AF_INET6;
1834 sa6.sin6_len = sizeof(sa6);
1835 sa6.sin6_addr = v->nh6;
1837 sa6.sin6_scope_id = v->zoneid;
1838 if (getnameinfo((const struct sockaddr *)&sa6,
1839 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1840 NI_NUMERICHOST) == 0)
1841 l = snprintf(buf, sz, "%s,", abuf);
1854 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1856 char *comma, tbuf[128], pval[128];
1858 struct tflow_entry *tfe;
1860 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1864 case IPFW_TABLE_ADDR:
1865 /* IPv4 or IPv6 prefixes */
1866 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1867 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1869 case IPFW_TABLE_INTERFACE:
1870 /* Interface names */
1871 printf("%s %s\n", tent->k.iface, pval);
1873 case IPFW_TABLE_NUMBER:
1875 printf("%u %s\n", tent->k.key, pval);
1877 case IPFW_TABLE_FLOW:
1879 tfe = &tent->k.flow;
1882 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1883 if (tfe->af == AF_INET)
1884 paddr = &tfe->a.a4.sip;
1886 paddr = &tfe->a.a6.sip6;
1888 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1889 printf("%s%s", comma, tbuf);
1893 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1894 printf("%s%d", comma, tfe->proto);
1898 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1899 printf("%s%d", comma, ntohs(tfe->sport));
1902 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1903 if (tfe->af == AF_INET)
1904 paddr = &tfe->a.a4.dip;
1906 paddr = &tfe->a.a6.dip6;
1908 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1909 printf("%s%s", comma, tbuf);
1913 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1914 printf("%s%d", comma, ntohs(tfe->dport));
1918 printf(" %s\n", pval);
1923 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1925 ipfw_obj_lheader req, *olh;
1928 memset(&req, 0, sizeof(req));
1931 if (do_get3(opcode, &req.opheader, &sz) != 0)
1932 if (errno != ENOMEM)
1936 if ((olh = calloc(1, sz)) == NULL)
1940 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1950 table_do_get_algolist(ipfw_obj_lheader **polh)
1953 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
1957 table_do_get_vlist(ipfw_obj_lheader **polh)
1960 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
1964 ipfw_list_ta(int ac, char *av[])
1966 ipfw_obj_lheader *olh;
1971 error = table_do_get_algolist(&olh);
1973 err(EX_OSERR, "Unable to request algorithm list");
1975 info = (ipfw_ta_info *)(olh + 1);
1976 for (i = 0; i < olh->count; i++) {
1977 if ((atype = match_value(tabletypes, info->type)) == NULL)
1979 printf("--- %s ---\n", info->algoname);
1980 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
1982 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
1989 /* Copy of current kernel table_value structure */
1990 struct _table_value {
1991 uint32_t tag; /* O_TAG/O_TAGGED */
1992 uint32_t pipe; /* O_PIPE/O_QUEUE */
1993 uint16_t divert; /* O_DIVERT/O_TEE */
1994 uint16_t skipto; /* skipto, CALLRET */
1995 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
1996 uint32_t fib; /* O_SETFIB */
1997 uint32_t nat; /* O_NAT */
2002 /* -- 32 bytes -- */
2003 struct in6_addr nh6;
2004 uint32_t limit; /* O_LIMIT */
2006 uint64_t refcnt; /* Number of references */
2010 compare_values(const void *_a, const void *_b)
2012 struct _table_value *a, *b;
2014 a = (struct _table_value *)_a;
2015 b = (struct _table_value *)_b;
2017 if (a->spare1 < b->spare1)
2019 else if (a->spare1 > b->spare1)
2026 ipfw_list_values(int ac, char *av[])
2028 ipfw_obj_lheader *olh;
2029 struct _table_value *v;
2034 error = table_do_get_vlist(&olh);
2036 err(EX_OSERR, "Unable to request value list");
2038 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2040 table_print_valheader(buf, sizeof(buf), vmask);
2041 printf("HEADER: %s\n", buf);
2042 v = (struct _table_value *)(olh + 1);
2043 qsort(v, olh->count, olh->objsize, compare_values);
2044 for (i = 0; i < olh->count; i++) {
2045 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2047 printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2048 v = (struct _table_value *)((caddr_t)v + olh->objsize);
2055 table_check_name(const char *tablename)
2058 if (ipfw_check_object_name(tablename) != 0)
2060 /* Restrict some 'special' names */
2061 if (strcmp(tablename, "all") == 0)