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");
287 if ((error = table_get_info(&oh, &i)) != 0)
288 err(EX_OSERR, "failed to request table info");
289 table_show_one(&i, NULL);
291 error = tables_foreach(table_show_one, NULL, 1);
293 err(EX_OSERR, "failed to request tables list");
298 table_lookup(&oh, ac, av);
304 table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
308 ntlv->head.type = IPFW_TLV_TBL_NAME;
309 ntlv->head.length = sizeof(ipfw_obj_ntlv);
312 strlcpy(ntlv->name, name, sizeof(ntlv->name));
316 table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
320 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
323 static struct _s_x tablenewcmds[] = {
324 { "type", TOK_TYPE },
325 { "valtype", TOK_VALTYPE },
326 { "algo", TOK_ALGO },
327 { "limit", TOK_LIMIT },
328 { "locked", TOK_LOCK },
332 static struct _s_x flowtypecmds[] = {
333 { "src-ip", IPFW_TFFLAG_SRCIP },
334 { "proto", IPFW_TFFLAG_PROTO },
335 { "src-port", IPFW_TFFLAG_SRCPORT },
336 { "dst-ip", IPFW_TFFLAG_DSTIP },
337 { "dst-port", IPFW_TFFLAG_DSTPORT },
342 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
344 uint32_t fset, fclear;
347 /* Parse type options */
349 case IPFW_TABLE_FLOW:
351 if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
353 "unable to parse flow option %s", e);
364 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
369 if ((tname = match_value(tabletypes, type)) == NULL)
372 l = snprintf(tbuf, size, "%s", tname);
377 case IPFW_TABLE_FLOW:
381 print_flags_buffer(tbuf, size, flowtypecmds, tflags);
390 * ipfw table NAME create [ type { addr | iface | number | flow } ]
394 table_create(ipfw_obj_header *oh, int ac, char *av[])
397 int error, tcmd, val;
398 uint32_t fset, fclear;
402 memset(&xi, 0, sizeof(xi));
405 tcmd = get_token(tablenewcmds, *av, "option");
410 NEED1("limit value required");
411 xi.limit = strtol(*av, NULL, 10);
415 NEED1("table type required");
416 /* Type may have suboptions after ':' */
417 if ((p = strchr(*av, ':')) != NULL)
419 val = match_token(tabletypes, *av);
421 concat_tokens(tbuf, sizeof(tbuf), tabletypes,
424 "Unknown tabletype: %s. Supported: %s",
429 error = table_parse_type(val, p, &xi.tflags);
432 "Unsupported suboptions: %s", p);
437 NEED1("table value type required");
439 val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
445 concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
446 errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
450 NEED1("table algorithm name required");
451 if (strlen(*av) > sizeof(xi.algoname))
452 errx(EX_USAGE, "algorithm name too long");
453 strlcpy(xi.algoname, *av, sizeof(xi.algoname));
457 xi.flags |= IPFW_TGFLAGS_LOCKED;
462 /* Set some defaults to preserve compatibility. */
463 if (xi.algoname[0] == '\0' && xi.type == 0)
464 xi.type = IPFW_TABLE_ADDR;
466 xi.vmask = IPFW_VTYPE_LEGACY;
468 if ((error = table_do_create(oh, &xi)) != 0)
469 err(EX_OSERR, "Table creation failed");
475 * Request: [ ipfw_obj_header ipfw_xtable_info ]
477 * Returns 0 on success.
480 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
482 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
485 memcpy(tbuf, oh, sizeof(*oh));
486 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
487 oh = (ipfw_obj_header *)tbuf;
489 error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
495 * Modifies existing table
497 * ipfw table NAME modify [ limit number ]
500 table_modify(ipfw_obj_header *oh, int ac, char *av[])
505 memset(&xi, 0, sizeof(xi));
508 tcmd = get_token(tablenewcmds, *av, "option");
513 NEED1("limit value required");
514 xi.limit = strtol(*av, NULL, 10);
515 xi.mflags |= IPFW_TMFLAGS_LIMIT;
519 errx(EX_USAGE, "cmd is not supported for modification");
523 if (table_do_modify(oh, &xi) != 0)
524 err(EX_OSERR, "Table modification failed");
528 * Modifies existing table.
530 * Request: [ ipfw_obj_header ipfw_xtable_info ]
532 * Returns 0 on success.
535 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
537 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
540 memcpy(tbuf, oh, sizeof(*oh));
541 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
542 oh = (ipfw_obj_header *)tbuf;
544 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
550 * Locks or unlocks given table
553 table_lock(ipfw_obj_header *oh, int lock)
557 memset(&xi, 0, sizeof(xi));
559 xi.mflags |= IPFW_TMFLAGS_LOCK;
560 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
562 if (table_do_modify(oh, &xi) != 0)
563 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
567 * Destroys given table specified by @oh->ntlv.
568 * Returns 0 on success.
571 table_destroy(ipfw_obj_header *oh)
574 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
581 table_destroy_one(ipfw_xtable_info *i, void *arg)
585 oh = (ipfw_obj_header *)arg;
586 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
587 if (table_destroy(oh) != 0) {
588 if (co.do_quiet == 0)
589 warn("failed to destroy table(%s) in set %u",
590 i->tablename, i->set);
597 * Flushes given table specified by @oh->ntlv.
598 * Returns 0 on success.
601 table_flush(ipfw_obj_header *oh)
604 if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
611 table_do_swap(ipfw_obj_header *oh, char *second)
613 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
616 memset(tbuf, 0, sizeof(tbuf));
617 memcpy(tbuf, oh, sizeof(*oh));
618 oh = (ipfw_obj_header *)tbuf;
619 table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
621 error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
627 * Swaps given table with @second one.
630 table_swap(ipfw_obj_header *oh, char *second)
633 if (table_check_name(second) != 0)
634 errx(EX_USAGE, "table name %s is invalid", second);
636 if (table_do_swap(oh, second) == 0)
641 errx(EX_USAGE, "Unable to swap table: check types");
643 errx(EX_USAGE, "Unable to swap table: check limits");
651 * Retrieves table in given table specified by @oh->ntlv.
653 * Returns 0 on success.
656 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
658 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
662 memset(tbuf, 0, sizeof(tbuf));
663 memcpy(tbuf, oh, sizeof(*oh));
664 oh = (ipfw_obj_header *)tbuf;
666 if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
669 if (sz < sizeof(tbuf))
672 *i = *(ipfw_xtable_info *)(oh + 1);
677 static struct _s_x tablealgoclass[] = {
678 { "hash", IPFW_TACLASS_HASH },
679 { "array", IPFW_TACLASS_ARRAY },
680 { "radix", IPFW_TACLASS_RADIX },
694 * Print global/per-AF table @i algorithm info.
697 table_show_tainfo(ipfw_xtable_info *i, struct ta_cldata *d,
698 const char *af, const char *taclass)
701 switch (d->taclass) {
702 case IPFW_TACLASS_HASH:
703 case IPFW_TACLASS_ARRAY:
704 printf(" %salgorithm %s info\n", af, taclass);
705 if (d->itemsize == d->itemsize6)
706 printf(" size: %u items: %u itemsize: %u\n",
707 d->size, d->count, d->itemsize);
709 printf(" size: %u items: %u "
710 "itemsize4: %u itemsize6: %u\n",
712 d->itemsize, d->itemsize6);
714 case IPFW_TACLASS_RADIX:
715 printf(" %salgorithm %s info\n", af, taclass);
716 if (d->itemsize == d->itemsize6)
717 printf(" items: %u itemsize: %u\n",
718 d->count, d->itemsize);
721 "itemsize4: %u itemsize6: %u\n",
722 d->count, d->itemsize, d->itemsize6);
725 printf(" algo class: %s\n", taclass);
730 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
733 if (vmask == IPFW_VTYPE_LEGACY) {
734 snprintf(buf, bufsize, "legacy");
738 memset(buf, 0, bufsize);
739 print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
743 * Prints table info struct @i in human-readable form.
746 table_show_info(ipfw_xtable_info *i, void *arg)
749 ipfw_ta_tinfo *tainfo;
752 char ttype[64], tvtype[64];
754 table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
755 table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
757 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
758 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
759 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
761 printf(" kindex: %d, type: %s\n", i->kidx, ttype);
762 printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
763 printf(" algorithm: %s\n", i->algoname);
764 printf(" items: %u, size: %u\n", i->count, i->size);
766 printf(" limit: %u\n", i->limit);
768 /* Print algo-specific info if requested & set */
772 if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
774 tainfo = &i->ta_info;
778 if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
780 if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
783 memset(&d, 0, sizeof(d));
784 d.taclass = tainfo->taclass4;
785 d.size = tainfo->size4;
786 d.count = tainfo->count4;
787 d.itemsize = tainfo->itemsize4;
788 if (afdata == 0 && afitem != 0)
789 d.itemsize6 = tainfo->itemsize6;
791 d.itemsize6 = d.itemsize;
792 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
796 table_show_tainfo(i, &d, "", vtype);
798 table_show_tainfo(i, &d, "IPv4 ", vtype);
799 memset(&d, 0, sizeof(d));
800 d.taclass = tainfo->taclass6;
801 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
803 d.size = tainfo->size6;
804 d.count = tainfo->count6;
805 d.itemsize = tainfo->itemsize6;
806 d.itemsize6 = d.itemsize;
807 table_show_tainfo(i, &d, "IPv6 ", vtype);
815 * Function wrappers which can be used either
816 * as is or as foreach function parameter.
820 table_show_one(ipfw_xtable_info *i, void *arg)
825 if ((error = table_do_get_list(i, &oh)) != 0) {
826 err(EX_OSERR, "Error requesting table %s list", i->tablename);
830 table_show_list(oh, 1);
837 table_flush_one(ipfw_xtable_info *i, void *arg)
841 oh = (ipfw_obj_header *)arg;
843 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
845 return (table_flush(oh));
849 table_do_modify_record(int cmd, ipfw_obj_header *oh,
850 ipfw_obj_tentry *tent, int count, int atomic)
853 ipfw_obj_tentry *tent_base;
855 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
859 sz = sizeof(*ctlv) + sizeof(*tent) * count;
861 memset(xbuf, 0, sizeof(xbuf));
864 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
868 memcpy(pbuf, oh, sizeof(*oh));
869 oh = (ipfw_obj_header *)pbuf;
870 oh->opheader.version = 1;
872 ctlv = (ipfw_obj_ctlv *)(oh + 1);
874 ctlv->head.length = sz;
876 ctlv->flags |= IPFW_CTF_ATOMIC;
879 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
880 tent = (ipfw_obj_tentry *)(ctlv + 1);
881 for (i = 0; i < count; i++, tent++) {
882 tent->head.length = sizeof(ipfw_obj_tentry);
887 error = do_get3(cmd, &oh->opheader, &sz);
890 tent = (ipfw_obj_tentry *)(ctlv + 1);
891 /* Copy result back to provided buffer */
892 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
901 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
902 int quiet, int update, int atomic)
904 ipfw_obj_tentry *ptent, tent, *tent_buf;
908 int cmd, count, error, i, ignored;
909 char *texterr, *etxt, *px;
912 errx(EX_USAGE, "address required");
915 cmd = IP_FW_TABLE_XADD;
916 texterr = "Adding record failed";
918 cmd = IP_FW_TABLE_XDEL;
919 texterr = "Deleting record failed";
923 * Calculate number of entries:
924 * Assume [key val] x N for add
928 count = (add != 0) ? ac / 2 + 1 : ac;
931 /* Adding single entry with/without value */
932 memset(&tent, 0, sizeof(tent));
936 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
938 "Unable to allocate memory for all entries");
942 memset(&xi, 0, sizeof(xi));
945 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
948 * Compatibility layer: auto-create table if not exists.
950 if (xi.tablename[0] == '\0') {
953 strlcpy(xi.tablename, oh->ntlv.name,
954 sizeof(xi.tablename));
956 warnx("DEPRECATED: inserting data into "
957 "non-existent table %s. (auto-created)",
959 table_do_create(oh, &xi);
962 oh->ntlv.type = type;
965 if (add != 0 && ac > 0) {
966 tentry_fill_value(oh, ptent, *av, type, vmask);
971 ptent->head.flags |= IPFW_TF_UPDATE;
977 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
980 * Compatibility stuff: do not yell on duplicate keys or
983 if (error == 0 || (error == EEXIST && add != 0) ||
984 (error == ENOENT && add == 0)) {
986 if (tent_buf != &tent)
992 /* Report results back */
994 for (i = 0; i < count; ptent++, i++) {
996 switch (ptent->result) {
1000 case IPFW_TR_DELETED:
1003 case IPFW_TR_UPDATED:
1014 case IPFW_TR_NOTFOUND:
1018 case IPFW_TR_EXISTS:
1022 case IPFW_TR_IGNORED:
1031 if (error != 0 && atomic != 0 && ignored == 0)
1032 printf("%s(reverted): ", px);
1036 table_show_entry(&xi, ptent);
1039 if (tent_buf != &tent)
1044 /* Get real OS error */
1047 /* Try to provide more human-readable error */
1050 etxt = "record already exists";
1056 etxt = "table not found";
1059 etxt = "record not found";
1062 etxt = "table is locked";
1065 etxt = strerror(error);
1068 errx(EX_OSERR, "%s: %s", texterr, etxt);
1072 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1073 ipfw_obj_tentry *xtent)
1075 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1076 ipfw_obj_tentry *tent;
1081 memcpy(xbuf, oh, sizeof(*oh));
1082 oh = (ipfw_obj_header *)xbuf;
1083 tent = (ipfw_obj_tentry *)(oh + 1);
1085 memset(tent, 0, sizeof(*tent));
1086 tent->head.length = sizeof(*tent);
1089 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1090 oh->ntlv.type = type;
1093 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1096 if (sz < sizeof(xbuf))
1105 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1107 ipfw_obj_tentry xtent;
1108 ipfw_xtable_info xi;
1113 errx(EX_USAGE, "address required");
1115 strlcpy(key, *av, sizeof(key));
1117 memset(&xi, 0, sizeof(xi));
1118 error = table_do_lookup(oh, key, &xi, &xtent);
1124 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1126 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1128 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1129 "\"lookup\" method", oh->ntlv.name);
1131 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1134 table_show_entry(&xi, &xtent);
1138 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1143 struct in6_addr *paddr, tmp;
1144 struct tflow_entry *tfe;
1145 uint32_t key, *pkey;
1147 struct protoent *pent;
1148 struct servent *sent;
1153 paddr = (struct in6_addr *)&tentry->k;
1156 case IPFW_TABLE_ADDR:
1157 /* Remove / if exists */
1158 if ((p = strchr(arg, '/')) != NULL) {
1163 if (inet_pton(AF_INET, arg, paddr) == 1) {
1164 if (p != NULL && mask > 32)
1165 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1168 masklen = p ? mask : 32;
1170 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1171 if (IN6_IS_ADDR_V4COMPAT(paddr))
1173 "Use IPv4 instead of v4-compatible");
1174 if (p != NULL && mask > 128)
1175 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1178 masklen = p ? mask : 128;
1182 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1183 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1186 type = IPFW_TABLE_ADDR;
1190 case IPFW_TABLE_INTERFACE:
1191 /* Assume interface name. Copy significant data only */
1192 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1193 memcpy(paddr, arg, mask);
1194 /* Set mask to exact match */
1195 masklen = 8 * IF_NAMESIZE;
1197 case IPFW_TABLE_NUMBER:
1198 /* Port or any other key */
1199 key = strtol(arg, &p, 10);
1201 errx(EX_DATAERR, "Invalid number: %s", arg);
1203 pkey = (uint32_t *)paddr;
1207 case IPFW_TABLE_FLOW:
1208 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1209 tfe = &tentry->k.flow;
1212 /* Handle <ipv4|ipv6> */
1213 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1214 if ((p = strchr(arg, ',')) != NULL)
1216 /* Determine family using temporary storage */
1217 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1218 if (af != 0 && af != AF_INET)
1220 "Inconsistent address family\n");
1222 memcpy(&tfe->a.a4.sip, &tmp, 4);
1223 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1224 if (af != 0 && af != AF_INET6)
1226 "Inconsistent address family\n");
1228 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1234 /* Handle <proto-num|proto-name> */
1235 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1237 errx(EX_DATAERR, "invalid key: proto missing");
1238 if ((p = strchr(arg, ',')) != NULL)
1241 key = strtol(arg, &pp, 10);
1243 if ((pent = getprotobyname(arg)) == NULL)
1244 errx(EX_DATAERR, "Unknown proto: %s",
1247 key = pent->p_proto;
1251 errx(EX_DATAERR, "Bad protocol number: %u",key);
1258 /* Handle <port-num|service-name> */
1259 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1261 errx(EX_DATAERR, "invalid key: src port missing");
1262 if ((p = strchr(arg, ',')) != NULL)
1265 port = htons(strtol(arg, &pp, 10));
1267 if ((sent = getservbyname(arg, NULL)) == NULL)
1268 errx(EX_DATAERR, "Unknown service: %s",
1270 port = sent->s_port;
1276 /* Handle <ipv4|ipv6>*/
1277 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1279 errx(EX_DATAERR, "invalid key: dst ip missing");
1280 if ((p = strchr(arg, ',')) != NULL)
1282 /* Determine family using temporary storage */
1283 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1284 if (af != 0 && af != AF_INET)
1286 "Inconsistent address family");
1288 memcpy(&tfe->a.a4.dip, &tmp, 4);
1289 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1290 if (af != 0 && af != AF_INET6)
1292 "Inconsistent address family");
1294 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1300 /* Handle <port-num|service-name> */
1301 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1303 errx(EX_DATAERR, "invalid key: dst port missing");
1304 if ((p = strchr(arg, ',')) != NULL)
1307 port = htons(strtol(arg, &pp, 10));
1309 if ((sent = getservbyname(arg, NULL)) == NULL)
1310 errx(EX_DATAERR, "Unknown service: %s",
1312 port = sent->s_port;
1323 errx(EX_DATAERR, "Unsupported table type: %d", type);
1326 tentry->subtype = af;
1327 tentry->masklen = masklen;
1331 * Tries to guess table key type.
1332 * This procedure is used in legacy table auto-create
1333 * code AND in `ipfw -n` ruleset checking.
1335 * Imported from old table_fill_xentry() parse code.
1338 guess_key_type(char *key, uint8_t *ptype)
1341 struct in6_addr addr;
1344 if (ishexnumber(*key) != 0 || *key == ':') {
1345 /* Remove / if exists */
1346 if ((p = strchr(key, '/')) != NULL)
1349 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1350 (inet_pton(AF_INET6, key, &addr) == 1)) {
1351 *ptype = IPFW_TABLE_CIDR;
1356 /* Port or any other key */
1357 /* Skip non-base 10 entries like 'fa1' */
1358 kv = strtol(key, &p, 10);
1360 *ptype = IPFW_TABLE_NUMBER;
1362 } else if ((p != key) && (*p == '.')) {
1364 * Warn on IPv4 address strings
1365 * which are "valid" for inet_aton() but not
1368 * Typical examples: '10.5' or '10.0.0.05'
1375 if (strchr(key, '.') == NULL) {
1376 *ptype = IPFW_TABLE_INTERFACE;
1380 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1383 *ptype = IPFW_TABLE_CIDR;
1388 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1389 int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1391 uint8_t type, tflags;
1399 if (xi->tablename[0] == '\0')
1400 error = table_get_info(oh, xi);
1405 if (co.test_only == 0) {
1408 tflags = xi->tflags;
1412 * We're running `ipfw -n`
1413 * Compatibility layer: try to guess key type
1416 if (guess_key_type(key, &type) != 0) {
1418 errx(EX_USAGE, "Cannot guess "
1419 "key '%s' type", key);
1421 vmask = IPFW_VTYPE_LEGACY;
1425 errx(EX_OSERR, "Error requesting table %s info",
1428 errx(EX_DATAERR, "Table %s does not exist",
1431 * Table does not exist
1432 * Compatibility layer: try to guess key type before failing.
1434 if (guess_key_type(key, &type) != 0) {
1436 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1437 "key '%s' type", oh->ntlv.name, key);
1440 vmask = IPFW_VTYPE_LEGACY;
1443 tentry_fill_key_type(key, tent, type, tflags);
1450 set_legacy_value(uint32_t val, ipfw_table_value *v)
1460 v->dscp = (uint8_t)val;
1465 tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *arg,
1466 uint8_t type, uint32_t vmask)
1468 struct addrinfo hints, *res;
1469 uint32_t a4, flag, val;
1470 ipfw_table_value *v;
1473 char *comma, *e, *etype, *n, *p;
1474 struct in_addr ipaddr;
1478 /* Compat layer: keep old behavior for legacy value types */
1479 if (vmask == IPFW_VTYPE_LEGACY) {
1480 /* Try to interpret as number first */
1481 val = strtoul(arg, &p, 0);
1483 set_legacy_value(val, v);
1486 if (inet_pton(AF_INET, arg, &val) == 1) {
1487 set_legacy_value(ntohl(val), v);
1491 if (lookup_host(arg, &ipaddr) == 0) {
1492 set_legacy_value(ntohl(ipaddr.s_addr), v);
1495 errx(EX_OSERR, "Unable to parse value %s", arg);
1499 * Shorthands: handle single value if vmask consists
1500 * of numbers only. e.g.:
1501 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1506 for (i = 1; i < (1 << 31); i *= 2) {
1507 if ((flag = (vmask & i)) == 0)
1511 if ((comma = strchr(n, ',')) != NULL)
1515 case IPFW_VTYPE_TAG:
1516 v->tag = strtol(n, &e, 10);
1520 case IPFW_VTYPE_PIPE:
1521 v->pipe = strtol(n, &e, 10);
1525 case IPFW_VTYPE_DIVERT:
1526 v->divert = strtol(n, &e, 10);
1530 case IPFW_VTYPE_SKIPTO:
1531 v->skipto = strtol(n, &e, 10);
1535 case IPFW_VTYPE_NETGRAPH:
1536 v->netgraph = strtol(n, &e, 10);
1540 case IPFW_VTYPE_FIB:
1541 v->fib = strtol(n, &e, 10);
1545 case IPFW_VTYPE_NAT:
1546 v->nat = strtol(n, &e, 10);
1550 case IPFW_VTYPE_LIMIT:
1551 v->limit = strtol(n, &e, 10);
1555 case IPFW_VTYPE_NH4:
1556 if (strchr(n, '.') != NULL &&
1557 inet_pton(AF_INET, n, &a4) == 1) {
1561 if (lookup_host(n, &ipaddr) == 0) {
1562 v->nh4 = ntohl(ipaddr.s_addr);
1567 case IPFW_VTYPE_DSCP:
1569 if ((dval = match_token(f_ipdscp, n)) != -1) {
1573 etype = "DSCP code";
1575 v->dscp = strtol(n, &e, 10);
1576 if (v->dscp > 63 || *e != '\0')
1577 etype = "DSCP value";
1580 case IPFW_VTYPE_NH6:
1581 if (strchr(n, ':') != NULL) {
1582 memset(&hints, 0, sizeof(hints));
1583 hints.ai_family = AF_INET6;
1584 hints.ai_flags = AI_NUMERICHOST;
1585 if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1586 v->nh6 = ((struct sockaddr_in6 *)
1587 res->ai_addr)->sin6_addr;
1588 v->zoneid = ((struct sockaddr_in6 *)
1589 res->ai_addr)->sin6_scope_id;
1599 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1604 if ((n = comma) != NULL)
1609 errx(EX_USAGE, "Not enough fields inside value");
1614 * Compare table names.
1615 * Honor number comparison.
1618 tablename_cmp(const void *a, const void *b)
1620 ipfw_xtable_info *ia, *ib;
1622 ia = (ipfw_xtable_info *)a;
1623 ib = (ipfw_xtable_info *)b;
1625 return (stringnum_cmp(ia->tablename, ib->tablename));
1629 * Retrieves table list from kernel,
1630 * optionally sorts it and calls requested function for each table.
1631 * Returns 0 on success.
1634 tables_foreach(table_cb_t *f, void *arg, int sort)
1636 ipfw_obj_lheader *olh;
1637 ipfw_xtable_info *info;
1641 /* Start with reasonable default */
1642 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1645 if ((olh = calloc(1, sz)) == NULL)
1649 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1652 if (errno != ENOMEM)
1658 qsort(olh + 1, olh->count, olh->objsize,
1661 info = (ipfw_xtable_info *)(olh + 1);
1662 for (i = 0; i < olh->count; i++) {
1663 if (co.use_set == 0 || info->set == co.use_set - 1)
1664 error = f(info, arg);
1665 info = (ipfw_xtable_info *)((caddr_t)info +
1676 * Retrieves all entries for given table @i in
1677 * eXtended format. Allocate buffer large enough
1678 * to store result. Called needs to free it later.
1680 * Returns 0 on success.
1683 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1685 ipfw_obj_header *oh;
1691 for (c = 0; c < 8; c++) {
1696 if ((oh = calloc(1, sz)) == NULL)
1698 table_fill_objheader(oh, i);
1699 oh->opheader.version = 1; /* Current version */
1700 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1705 if (errno != ENOMEM)
1714 * Shows all entries from @oh in human-readable format
1717 table_show_list(ipfw_obj_header *oh, int need_header)
1719 ipfw_obj_tentry *tent;
1721 ipfw_xtable_info *i;
1723 i = (ipfw_xtable_info *)(oh + 1);
1724 tent = (ipfw_obj_tentry *)(i + 1);
1727 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1731 table_show_entry(i, tent);
1732 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1738 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1739 uint32_t vmask, int print_ip)
1741 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1742 struct sockaddr_in6 sa6;
1743 uint32_t flag, i, l;
1750 * Some shorthands for printing values:
1751 * legacy assumes all values are equal, so keep the first one.
1753 if (vmask == IPFW_VTYPE_LEGACY) {
1754 if (print_ip != 0) {
1755 flag = htonl(v->tag);
1756 inet_ntop(AF_INET, &flag, buf, sz);
1758 snprintf(buf, sz, "%u", v->tag);
1762 for (i = 1; i < (1 << 31); i *= 2) {
1763 if ((flag = (vmask & i)) == 0)
1768 case IPFW_VTYPE_TAG:
1769 l = snprintf(buf, sz, "%u,", v->tag);
1771 case IPFW_VTYPE_PIPE:
1772 l = snprintf(buf, sz, "%u,", v->pipe);
1774 case IPFW_VTYPE_DIVERT:
1775 l = snprintf(buf, sz, "%d,", v->divert);
1777 case IPFW_VTYPE_SKIPTO:
1778 l = snprintf(buf, sz, "%d,", v->skipto);
1780 case IPFW_VTYPE_NETGRAPH:
1781 l = snprintf(buf, sz, "%u,", v->netgraph);
1783 case IPFW_VTYPE_FIB:
1784 l = snprintf(buf, sz, "%u,", v->fib);
1786 case IPFW_VTYPE_NAT:
1787 l = snprintf(buf, sz, "%u,", v->nat);
1789 case IPFW_VTYPE_LIMIT:
1790 l = snprintf(buf, sz, "%u,", v->limit);
1792 case IPFW_VTYPE_NH4:
1793 a4.s_addr = htonl(v->nh4);
1794 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1795 l = snprintf(buf, sz, "%s,", abuf);
1797 case IPFW_VTYPE_DSCP:
1798 l = snprintf(buf, sz, "%d,", v->dscp);
1800 case IPFW_VTYPE_NH6:
1801 sa6.sin6_family = AF_INET6;
1802 sa6.sin6_len = sizeof(sa6);
1803 sa6.sin6_addr = v->nh6;
1805 sa6.sin6_scope_id = v->zoneid;
1806 if (getnameinfo((const struct sockaddr *)&sa6,
1807 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1808 NI_NUMERICHOST) == 0)
1809 l = snprintf(buf, sz, "%s,", abuf);
1822 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1824 char *comma, tbuf[128], pval[128];
1826 struct tflow_entry *tfe;
1828 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1832 case IPFW_TABLE_ADDR:
1833 /* IPv4 or IPv6 prefixes */
1834 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1835 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1837 case IPFW_TABLE_INTERFACE:
1838 /* Interface names */
1839 printf("%s %s\n", tent->k.iface, pval);
1841 case IPFW_TABLE_NUMBER:
1843 printf("%u %s\n", tent->k.key, pval);
1845 case IPFW_TABLE_FLOW:
1847 tfe = &tent->k.flow;
1850 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1851 if (tfe->af == AF_INET)
1852 paddr = &tfe->a.a4.sip;
1854 paddr = &tfe->a.a6.sip6;
1856 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1857 printf("%s%s", comma, tbuf);
1861 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1862 printf("%s%d", comma, tfe->proto);
1866 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1867 printf("%s%d", comma, ntohs(tfe->sport));
1870 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1871 if (tfe->af == AF_INET)
1872 paddr = &tfe->a.a4.dip;
1874 paddr = &tfe->a.a6.dip6;
1876 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1877 printf("%s%s", comma, tbuf);
1881 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1882 printf("%s%d", comma, ntohs(tfe->dport));
1886 printf(" %s\n", pval);
1891 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1893 ipfw_obj_lheader req, *olh;
1896 memset(&req, 0, sizeof(req));
1899 if (do_get3(opcode, &req.opheader, &sz) != 0)
1900 if (errno != ENOMEM)
1904 if ((olh = calloc(1, sz)) == NULL)
1908 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1918 table_do_get_algolist(ipfw_obj_lheader **polh)
1921 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
1925 table_do_get_vlist(ipfw_obj_lheader **polh)
1928 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
1932 ipfw_list_ta(int ac, char *av[])
1934 ipfw_obj_lheader *olh;
1939 error = table_do_get_algolist(&olh);
1941 err(EX_OSERR, "Unable to request algorithm list");
1943 info = (ipfw_ta_info *)(olh + 1);
1944 for (i = 0; i < olh->count; i++) {
1945 if ((atype = match_value(tabletypes, info->type)) == NULL)
1947 printf("--- %s ---\n", info->algoname);
1948 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
1950 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
1957 /* Copy of current kernel table_value structure */
1958 struct _table_value {
1959 uint32_t tag; /* O_TAG/O_TAGGED */
1960 uint32_t pipe; /* O_PIPE/O_QUEUE */
1961 uint16_t divert; /* O_DIVERT/O_TEE */
1962 uint16_t skipto; /* skipto, CALLRET */
1963 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
1964 uint32_t fib; /* O_SETFIB */
1965 uint32_t nat; /* O_NAT */
1970 /* -- 32 bytes -- */
1971 struct in6_addr nh6;
1972 uint32_t limit; /* O_LIMIT */
1974 uint64_t refcnt; /* Number of references */
1978 compare_values(const void *_a, const void *_b)
1980 struct _table_value *a, *b;
1982 a = (struct _table_value *)_a;
1983 b = (struct _table_value *)_b;
1985 if (a->spare1 < b->spare1)
1987 else if (a->spare1 > b->spare1)
1994 ipfw_list_values(int ac, char *av[])
1996 ipfw_obj_lheader *olh;
1997 struct _table_value *v;
2002 error = table_do_get_vlist(&olh);
2004 err(EX_OSERR, "Unable to request value list");
2006 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2008 table_print_valheader(buf, sizeof(buf), vmask);
2009 printf("HEADER: %s\n", buf);
2010 v = (struct _table_value *)(olh + 1);
2011 qsort(v, olh->count, olh->objsize, compare_values);
2012 for (i = 0; i < olh->count; i++) {
2013 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2015 printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2016 v = (struct _table_value *)((caddr_t)v + olh->objsize);
2023 table_check_name(const char *tablename)
2026 if (ipfw_check_object_name(tablename) != 0)
2028 /* Restrict some 'special' names */
2029 if (strcmp(tablename, "all") == 0)