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 },
333 static struct _s_x flowtypecmds[] = {
334 { "src-ip", IPFW_TFFLAG_SRCIP },
335 { "proto", IPFW_TFFLAG_PROTO },
336 { "src-port", IPFW_TFFLAG_SRCPORT },
337 { "dst-ip", IPFW_TFFLAG_DSTIP },
338 { "dst-port", IPFW_TFFLAG_DSTPORT },
343 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
345 uint32_t fset, fclear;
348 /* Parse type options */
350 case IPFW_TABLE_FLOW:
352 if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
354 "unable to parse flow option %s", e);
365 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
370 if ((tname = match_value(tabletypes, type)) == NULL)
373 l = snprintf(tbuf, size, "%s", tname);
378 case IPFW_TABLE_FLOW:
382 print_flags_buffer(tbuf, size, flowtypecmds, tflags);
391 * ipfw table NAME create [ type { addr | iface | number | flow } ]
395 table_create(ipfw_obj_header *oh, int ac, char *av[])
398 int error, tcmd, val;
399 uint32_t fset, fclear;
403 memset(&xi, 0, sizeof(xi));
406 tcmd = get_token(tablenewcmds, *av, "option");
411 NEED1("limit value required");
412 xi.limit = strtol(*av, NULL, 10);
416 NEED1("table type required");
417 /* Type may have suboptions after ':' */
418 if ((p = strchr(*av, ':')) != NULL)
420 val = match_token(tabletypes, *av);
422 concat_tokens(tbuf, sizeof(tbuf), tabletypes,
425 "Unknown tabletype: %s. Supported: %s",
430 error = table_parse_type(val, p, &xi.tflags);
433 "Unsupported suboptions: %s", p);
438 NEED1("table value type required");
440 val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
446 concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
447 errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
451 NEED1("table algorithm name required");
452 if (strlen(*av) > sizeof(xi.algoname))
453 errx(EX_USAGE, "algorithm name too long");
454 strlcpy(xi.algoname, *av, sizeof(xi.algoname));
458 xi.flags |= IPFW_TGFLAGS_LOCKED;
463 /* Set some defaults to preserve compatibility. */
464 if (xi.algoname[0] == '\0' && xi.type == 0)
465 xi.type = IPFW_TABLE_ADDR;
467 xi.vmask = IPFW_VTYPE_LEGACY;
469 if ((error = table_do_create(oh, &xi)) != 0)
470 err(EX_OSERR, "Table creation failed");
476 * Request: [ ipfw_obj_header ipfw_xtable_info ]
478 * Returns 0 on success.
481 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
483 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
486 memcpy(tbuf, oh, sizeof(*oh));
487 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
488 oh = (ipfw_obj_header *)tbuf;
490 error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
496 * Modifies existing table
498 * ipfw table NAME modify [ limit number ]
501 table_modify(ipfw_obj_header *oh, int ac, char *av[])
506 memset(&xi, 0, sizeof(xi));
509 tcmd = get_token(tablenewcmds, *av, "option");
514 NEED1("limit value required");
515 xi.limit = strtol(*av, NULL, 10);
516 xi.mflags |= IPFW_TMFLAGS_LIMIT;
520 errx(EX_USAGE, "cmd is not supported for modification");
524 if (table_do_modify(oh, &xi) != 0)
525 err(EX_OSERR, "Table modification failed");
529 * Modifies existing table.
531 * Request: [ ipfw_obj_header ipfw_xtable_info ]
533 * Returns 0 on success.
536 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
538 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
541 memcpy(tbuf, oh, sizeof(*oh));
542 memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
543 oh = (ipfw_obj_header *)tbuf;
545 error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
551 * Locks or unlocks given table
554 table_lock(ipfw_obj_header *oh, int lock)
558 memset(&xi, 0, sizeof(xi));
560 xi.mflags |= IPFW_TMFLAGS_LOCK;
561 xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
563 if (table_do_modify(oh, &xi) != 0)
564 err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
568 * Destroys given table specified by @oh->ntlv.
569 * Returns 0 on success.
572 table_destroy(ipfw_obj_header *oh)
575 if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
582 table_destroy_one(ipfw_xtable_info *i, void *arg)
586 oh = (ipfw_obj_header *)arg;
587 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
588 if (table_destroy(oh) != 0) {
589 if (co.do_quiet == 0)
590 warn("failed to destroy table(%s) in set %u",
591 i->tablename, i->set);
598 * Flushes given table specified by @oh->ntlv.
599 * Returns 0 on success.
602 table_flush(ipfw_obj_header *oh)
605 if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
612 table_do_swap(ipfw_obj_header *oh, char *second)
614 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
617 memset(tbuf, 0, sizeof(tbuf));
618 memcpy(tbuf, oh, sizeof(*oh));
619 oh = (ipfw_obj_header *)tbuf;
620 table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
622 error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
628 * Swaps given table with @second one.
631 table_swap(ipfw_obj_header *oh, char *second)
634 if (table_check_name(second) != 0)
635 errx(EX_USAGE, "table name %s is invalid", second);
637 if (table_do_swap(oh, second) == 0)
642 errx(EX_USAGE, "Unable to swap table: check types");
644 errx(EX_USAGE, "Unable to swap table: check limits");
652 * Retrieves table in given table specified by @oh->ntlv.
654 * Returns 0 on success.
657 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
659 char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
663 memset(tbuf, 0, sizeof(tbuf));
664 memcpy(tbuf, oh, sizeof(*oh));
665 oh = (ipfw_obj_header *)tbuf;
667 if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
670 if (sz < sizeof(tbuf))
673 *i = *(ipfw_xtable_info *)(oh + 1);
678 static struct _s_x tablealgoclass[] = {
679 { "hash", IPFW_TACLASS_HASH },
680 { "array", IPFW_TACLASS_ARRAY },
681 { "radix", IPFW_TACLASS_RADIX },
695 * Print global/per-AF table @i algorithm info.
698 table_show_tainfo(ipfw_xtable_info *i, struct ta_cldata *d,
699 const char *af, const char *taclass)
702 switch (d->taclass) {
703 case IPFW_TACLASS_HASH:
704 case IPFW_TACLASS_ARRAY:
705 printf(" %salgorithm %s info\n", af, taclass);
706 if (d->itemsize == d->itemsize6)
707 printf(" size: %u items: %u itemsize: %u\n",
708 d->size, d->count, d->itemsize);
710 printf(" size: %u items: %u "
711 "itemsize4: %u itemsize6: %u\n",
713 d->itemsize, d->itemsize6);
715 case IPFW_TACLASS_RADIX:
716 printf(" %salgorithm %s info\n", af, taclass);
717 if (d->itemsize == d->itemsize6)
718 printf(" items: %u itemsize: %u\n",
719 d->count, d->itemsize);
722 "itemsize4: %u itemsize6: %u\n",
723 d->count, d->itemsize, d->itemsize6);
726 printf(" algo class: %s\n", taclass);
731 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
734 if (vmask == IPFW_VTYPE_LEGACY) {
735 snprintf(buf, bufsize, "legacy");
739 memset(buf, 0, bufsize);
740 print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
744 * Prints table info struct @i in human-readable form.
747 table_show_info(ipfw_xtable_info *i, void *arg)
750 ipfw_ta_tinfo *tainfo;
753 char ttype[64], tvtype[64];
755 table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
756 table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
758 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
759 if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
760 printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
762 printf(" kindex: %d, type: %s\n", i->kidx, ttype);
763 printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
764 printf(" algorithm: %s\n", i->algoname);
765 printf(" items: %u, size: %u\n", i->count, i->size);
767 printf(" limit: %u\n", i->limit);
769 /* Print algo-specific info if requested & set */
773 if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
775 tainfo = &i->ta_info;
779 if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
781 if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
784 memset(&d, 0, sizeof(d));
785 d.taclass = tainfo->taclass4;
786 d.size = tainfo->size4;
787 d.count = tainfo->count4;
788 d.itemsize = tainfo->itemsize4;
789 if (afdata == 0 && afitem != 0)
790 d.itemsize6 = tainfo->itemsize6;
792 d.itemsize6 = d.itemsize;
793 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
797 table_show_tainfo(i, &d, "", vtype);
799 table_show_tainfo(i, &d, "IPv4 ", vtype);
800 memset(&d, 0, sizeof(d));
801 d.taclass = tainfo->taclass6;
802 if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
804 d.size = tainfo->size6;
805 d.count = tainfo->count6;
806 d.itemsize = tainfo->itemsize6;
807 d.itemsize6 = d.itemsize;
808 table_show_tainfo(i, &d, "IPv6 ", vtype);
816 * Function wrappers which can be used either
817 * as is or as foreach function parameter.
821 table_show_one(ipfw_xtable_info *i, void *arg)
827 is_all = arg == NULL ? 0 : 1;
829 if ((error = table_do_get_list(i, &oh)) != 0) {
830 err(EX_OSERR, "Error requesting table %s list", i->tablename);
834 table_show_list(oh, is_all);
841 table_flush_one(ipfw_xtable_info *i, void *arg)
845 oh = (ipfw_obj_header *)arg;
847 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
849 return (table_flush(oh));
853 table_do_modify_record(int cmd, ipfw_obj_header *oh,
854 ipfw_obj_tentry *tent, int count, int atomic)
857 ipfw_obj_tentry *tent_base;
859 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
863 sz = sizeof(*ctlv) + sizeof(*tent) * count;
865 memset(xbuf, 0, sizeof(xbuf));
868 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
872 memcpy(pbuf, oh, sizeof(*oh));
873 oh = (ipfw_obj_header *)pbuf;
874 oh->opheader.version = 1;
876 ctlv = (ipfw_obj_ctlv *)(oh + 1);
878 ctlv->head.length = sz;
880 ctlv->flags |= IPFW_CTF_ATOMIC;
883 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
884 tent = (ipfw_obj_tentry *)(ctlv + 1);
885 for (i = 0; i < count; i++, tent++) {
886 tent->head.length = sizeof(ipfw_obj_tentry);
891 error = do_get3(cmd, &oh->opheader, &sz);
894 tent = (ipfw_obj_tentry *)(ctlv + 1);
895 /* Copy result back to provided buffer */
896 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
905 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
906 int quiet, int update, int atomic)
908 ipfw_obj_tentry *ptent, tent, *tent_buf;
912 int cmd, count, error, i, ignored;
913 char *texterr, *etxt, *px;
916 errx(EX_USAGE, "address required");
919 cmd = IP_FW_TABLE_XADD;
920 texterr = "Adding record failed";
922 cmd = IP_FW_TABLE_XDEL;
923 texterr = "Deleting record failed";
927 * Calculate number of entries:
928 * Assume [key val] x N for add
932 count = (add != 0) ? ac / 2 + 1 : ac;
935 /* Adding single entry with/without value */
936 memset(&tent, 0, sizeof(tent));
940 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
942 "Unable to allocate memory for all entries");
946 memset(&xi, 0, sizeof(xi));
949 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
952 * Compatibility layer: auto-create table if not exists.
954 if (xi.tablename[0] == '\0') {
957 strlcpy(xi.tablename, oh->ntlv.name,
958 sizeof(xi.tablename));
960 warnx("DEPRECATED: inserting data into "
961 "non-existent table %s. (auto-created)",
963 table_do_create(oh, &xi);
966 oh->ntlv.type = type;
969 if (add != 0 && ac > 0) {
970 tentry_fill_value(oh, ptent, *av, type, vmask);
975 ptent->head.flags |= IPFW_TF_UPDATE;
981 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
984 * Compatibility stuff: do not yell on duplicate keys or
987 if (error == 0 || (error == EEXIST && add != 0) ||
988 (error == ENOENT && add == 0)) {
990 if (tent_buf != &tent)
996 /* Report results back */
998 for (i = 0; i < count; ptent++, i++) {
1000 switch (ptent->result) {
1004 case IPFW_TR_DELETED:
1007 case IPFW_TR_UPDATED:
1018 case IPFW_TR_NOTFOUND:
1022 case IPFW_TR_EXISTS:
1026 case IPFW_TR_IGNORED:
1035 if (error != 0 && atomic != 0 && ignored == 0)
1036 printf("%s(reverted): ", px);
1040 table_show_entry(&xi, ptent);
1043 if (tent_buf != &tent)
1048 /* Get real OS error */
1051 /* Try to provide more human-readable error */
1054 etxt = "record already exists";
1060 etxt = "table not found";
1063 etxt = "record not found";
1066 etxt = "table is locked";
1069 etxt = strerror(error);
1072 errx(EX_OSERR, "%s: %s", texterr, etxt);
1076 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1077 ipfw_obj_tentry *xtent)
1079 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1080 ipfw_obj_tentry *tent;
1085 memcpy(xbuf, oh, sizeof(*oh));
1086 oh = (ipfw_obj_header *)xbuf;
1087 tent = (ipfw_obj_tentry *)(oh + 1);
1089 memset(tent, 0, sizeof(*tent));
1090 tent->head.length = sizeof(*tent);
1093 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1094 oh->ntlv.type = type;
1097 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1100 if (sz < sizeof(xbuf))
1109 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1111 ipfw_obj_tentry xtent;
1112 ipfw_xtable_info xi;
1117 errx(EX_USAGE, "address required");
1119 strlcpy(key, *av, sizeof(key));
1121 memset(&xi, 0, sizeof(xi));
1122 error = table_do_lookup(oh, key, &xi, &xtent);
1128 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1130 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1132 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1133 "\"lookup\" method", oh->ntlv.name);
1135 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1138 table_show_entry(&xi, &xtent);
1142 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1147 struct in6_addr *paddr, tmp;
1148 struct tflow_entry *tfe;
1149 uint32_t key, *pkey;
1151 struct protoent *pent;
1152 struct servent *sent;
1157 paddr = (struct in6_addr *)&tentry->k;
1160 case IPFW_TABLE_ADDR:
1161 /* Remove / if exists */
1162 if ((p = strchr(arg, '/')) != NULL) {
1167 if (inet_pton(AF_INET, arg, paddr) == 1) {
1168 if (p != NULL && mask > 32)
1169 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1172 masklen = p ? mask : 32;
1174 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1175 if (IN6_IS_ADDR_V4COMPAT(paddr))
1177 "Use IPv4 instead of v4-compatible");
1178 if (p != NULL && mask > 128)
1179 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1182 masklen = p ? mask : 128;
1186 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1187 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1190 type = IPFW_TABLE_ADDR;
1194 case IPFW_TABLE_INTERFACE:
1195 /* Assume interface name. Copy significant data only */
1196 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1197 memcpy(paddr, arg, mask);
1198 /* Set mask to exact match */
1199 masklen = 8 * IF_NAMESIZE;
1201 case IPFW_TABLE_NUMBER:
1202 /* Port or any other key */
1203 key = strtol(arg, &p, 10);
1205 errx(EX_DATAERR, "Invalid number: %s", arg);
1207 pkey = (uint32_t *)paddr;
1211 case IPFW_TABLE_FLOW:
1212 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1213 tfe = &tentry->k.flow;
1216 /* Handle <ipv4|ipv6> */
1217 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1218 if ((p = strchr(arg, ',')) != NULL)
1220 /* Determine family using temporary storage */
1221 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1222 if (af != 0 && af != AF_INET)
1224 "Inconsistent address family\n");
1226 memcpy(&tfe->a.a4.sip, &tmp, 4);
1227 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1228 if (af != 0 && af != AF_INET6)
1230 "Inconsistent address family\n");
1232 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1238 /* Handle <proto-num|proto-name> */
1239 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1241 errx(EX_DATAERR, "invalid key: proto missing");
1242 if ((p = strchr(arg, ',')) != NULL)
1245 key = strtol(arg, &pp, 10);
1247 if ((pent = getprotobyname(arg)) == NULL)
1248 errx(EX_DATAERR, "Unknown proto: %s",
1251 key = pent->p_proto;
1255 errx(EX_DATAERR, "Bad protocol number: %u",key);
1262 /* Handle <port-num|service-name> */
1263 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1265 errx(EX_DATAERR, "invalid key: src port missing");
1266 if ((p = strchr(arg, ',')) != NULL)
1269 port = htons(strtol(arg, &pp, 10));
1271 if ((sent = getservbyname(arg, NULL)) == NULL)
1272 errx(EX_DATAERR, "Unknown service: %s",
1274 port = sent->s_port;
1280 /* Handle <ipv4|ipv6>*/
1281 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1283 errx(EX_DATAERR, "invalid key: dst ip missing");
1284 if ((p = strchr(arg, ',')) != NULL)
1286 /* Determine family using temporary storage */
1287 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1288 if (af != 0 && af != AF_INET)
1290 "Inconsistent address family");
1292 memcpy(&tfe->a.a4.dip, &tmp, 4);
1293 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1294 if (af != 0 && af != AF_INET6)
1296 "Inconsistent address family");
1298 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1304 /* Handle <port-num|service-name> */
1305 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1307 errx(EX_DATAERR, "invalid key: dst port missing");
1308 if ((p = strchr(arg, ',')) != NULL)
1311 port = htons(strtol(arg, &pp, 10));
1313 if ((sent = getservbyname(arg, NULL)) == NULL)
1314 errx(EX_DATAERR, "Unknown service: %s",
1316 port = sent->s_port;
1327 errx(EX_DATAERR, "Unsupported table type: %d", type);
1330 tentry->subtype = af;
1331 tentry->masklen = masklen;
1335 * Tries to guess table key type.
1336 * This procedure is used in legacy table auto-create
1337 * code AND in `ipfw -n` ruleset checking.
1339 * Imported from old table_fill_xentry() parse code.
1342 guess_key_type(char *key, uint8_t *ptype)
1345 struct in6_addr addr;
1348 if (ishexnumber(*key) != 0 || *key == ':') {
1349 /* Remove / if exists */
1350 if ((p = strchr(key, '/')) != NULL)
1353 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1354 (inet_pton(AF_INET6, key, &addr) == 1)) {
1355 *ptype = IPFW_TABLE_CIDR;
1360 /* Port or any other key */
1361 /* Skip non-base 10 entries like 'fa1' */
1362 kv = strtol(key, &p, 10);
1364 *ptype = IPFW_TABLE_NUMBER;
1366 } else if ((p != key) && (*p == '.')) {
1368 * Warn on IPv4 address strings
1369 * which are "valid" for inet_aton() but not
1372 * Typical examples: '10.5' or '10.0.0.05'
1379 if (strchr(key, '.') == NULL) {
1380 *ptype = IPFW_TABLE_INTERFACE;
1384 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1387 *ptype = IPFW_TABLE_CIDR;
1392 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1393 int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1395 uint8_t type, tflags;
1403 if (xi->tablename[0] == '\0')
1404 error = table_get_info(oh, xi);
1409 if (co.test_only == 0) {
1412 tflags = xi->tflags;
1416 * We're running `ipfw -n`
1417 * Compatibility layer: try to guess key type
1420 if (guess_key_type(key, &type) != 0) {
1422 errx(EX_USAGE, "Cannot guess "
1423 "key '%s' type", key);
1425 vmask = IPFW_VTYPE_LEGACY;
1429 errx(EX_OSERR, "Error requesting table %s info",
1432 errx(EX_DATAERR, "Table %s does not exist",
1435 * Table does not exist
1436 * Compatibility layer: try to guess key type before failing.
1438 if (guess_key_type(key, &type) != 0) {
1440 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1441 "key '%s' type", oh->ntlv.name, key);
1444 vmask = IPFW_VTYPE_LEGACY;
1447 tentry_fill_key_type(key, tent, type, tflags);
1454 set_legacy_value(uint32_t val, ipfw_table_value *v)
1464 v->dscp = (uint8_t)val;
1469 tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *arg,
1470 uint8_t type, uint32_t vmask)
1472 struct addrinfo hints, *res;
1473 uint32_t a4, flag, val;
1474 ipfw_table_value *v;
1477 char *comma, *e, *etype, *n, *p;
1478 struct in_addr ipaddr;
1482 /* Compat layer: keep old behavior for legacy value types */
1483 if (vmask == IPFW_VTYPE_LEGACY) {
1484 /* Try to interpret as number first */
1485 val = strtoul(arg, &p, 0);
1487 set_legacy_value(val, v);
1490 if (inet_pton(AF_INET, arg, &val) == 1) {
1491 set_legacy_value(ntohl(val), v);
1495 if (lookup_host(arg, &ipaddr) == 0) {
1496 set_legacy_value(ntohl(ipaddr.s_addr), v);
1499 errx(EX_OSERR, "Unable to parse value %s", arg);
1503 * Shorthands: handle single value if vmask consists
1504 * of numbers only. e.g.:
1505 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1510 for (i = 1; i < (1 << 31); i *= 2) {
1511 if ((flag = (vmask & i)) == 0)
1515 if ((comma = strchr(n, ',')) != NULL)
1519 case IPFW_VTYPE_TAG:
1520 v->tag = strtol(n, &e, 10);
1524 case IPFW_VTYPE_PIPE:
1525 v->pipe = strtol(n, &e, 10);
1529 case IPFW_VTYPE_DIVERT:
1530 v->divert = strtol(n, &e, 10);
1534 case IPFW_VTYPE_SKIPTO:
1535 v->skipto = strtol(n, &e, 10);
1539 case IPFW_VTYPE_NETGRAPH:
1540 v->netgraph = strtol(n, &e, 10);
1544 case IPFW_VTYPE_FIB:
1545 v->fib = strtol(n, &e, 10);
1549 case IPFW_VTYPE_NAT:
1550 v->nat = strtol(n, &e, 10);
1554 case IPFW_VTYPE_LIMIT:
1555 v->limit = strtol(n, &e, 10);
1559 case IPFW_VTYPE_NH4:
1560 if (strchr(n, '.') != NULL &&
1561 inet_pton(AF_INET, n, &a4) == 1) {
1565 if (lookup_host(n, &ipaddr) == 0) {
1566 v->nh4 = ntohl(ipaddr.s_addr);
1571 case IPFW_VTYPE_DSCP:
1573 if ((dval = match_token(f_ipdscp, n)) != -1) {
1577 etype = "DSCP code";
1579 v->dscp = strtol(n, &e, 10);
1580 if (v->dscp > 63 || *e != '\0')
1581 etype = "DSCP value";
1584 case IPFW_VTYPE_NH6:
1585 if (strchr(n, ':') != NULL) {
1586 memset(&hints, 0, sizeof(hints));
1587 hints.ai_family = AF_INET6;
1588 hints.ai_flags = AI_NUMERICHOST;
1589 if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1590 v->nh6 = ((struct sockaddr_in6 *)
1591 res->ai_addr)->sin6_addr;
1592 v->zoneid = ((struct sockaddr_in6 *)
1593 res->ai_addr)->sin6_scope_id;
1603 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1608 if ((n = comma) != NULL)
1613 errx(EX_USAGE, "Not enough fields inside value");
1618 * Compare table names.
1619 * Honor number comparison.
1622 tablename_cmp(const void *a, const void *b)
1624 ipfw_xtable_info *ia, *ib;
1626 ia = (ipfw_xtable_info *)a;
1627 ib = (ipfw_xtable_info *)b;
1629 return (stringnum_cmp(ia->tablename, ib->tablename));
1633 * Retrieves table list from kernel,
1634 * optionally sorts it and calls requested function for each table.
1635 * Returns 0 on success.
1638 tables_foreach(table_cb_t *f, void *arg, int sort)
1640 ipfw_obj_lheader *olh;
1641 ipfw_xtable_info *info;
1645 /* Start with reasonable default */
1646 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1649 if ((olh = calloc(1, sz)) == NULL)
1653 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1656 if (errno != ENOMEM)
1662 qsort(olh + 1, olh->count, olh->objsize,
1665 info = (ipfw_xtable_info *)(olh + 1);
1666 for (i = 0; i < olh->count; i++) {
1667 if (co.use_set == 0 || info->set == co.use_set - 1)
1668 error = f(info, arg);
1669 info = (ipfw_xtable_info *)((caddr_t)info +
1680 * Retrieves all entries for given table @i in
1681 * eXtended format. Allocate buffer large enough
1682 * to store result. Called needs to free it later.
1684 * Returns 0 on success.
1687 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1689 ipfw_obj_header *oh;
1695 for (c = 0; c < 8; c++) {
1700 if ((oh = calloc(1, sz)) == NULL)
1702 table_fill_objheader(oh, i);
1703 oh->opheader.version = 1; /* Current version */
1704 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1709 if (errno != ENOMEM)
1718 * Shows all entries from @oh in human-readable format
1721 table_show_list(ipfw_obj_header *oh, int need_header)
1723 ipfw_obj_tentry *tent;
1725 ipfw_xtable_info *i;
1727 i = (ipfw_xtable_info *)(oh + 1);
1728 tent = (ipfw_obj_tentry *)(i + 1);
1731 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1735 table_show_entry(i, tent);
1736 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1742 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1743 uint32_t vmask, int print_ip)
1745 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1746 struct sockaddr_in6 sa6;
1747 uint32_t flag, i, l;
1754 * Some shorthands for printing values:
1755 * legacy assumes all values are equal, so keep the first one.
1757 if (vmask == IPFW_VTYPE_LEGACY) {
1758 if (print_ip != 0) {
1759 flag = htonl(v->tag);
1760 inet_ntop(AF_INET, &flag, buf, sz);
1762 snprintf(buf, sz, "%u", v->tag);
1766 for (i = 1; i < (1 << 31); i *= 2) {
1767 if ((flag = (vmask & i)) == 0)
1772 case IPFW_VTYPE_TAG:
1773 l = snprintf(buf, sz, "%u,", v->tag);
1775 case IPFW_VTYPE_PIPE:
1776 l = snprintf(buf, sz, "%u,", v->pipe);
1778 case IPFW_VTYPE_DIVERT:
1779 l = snprintf(buf, sz, "%d,", v->divert);
1781 case IPFW_VTYPE_SKIPTO:
1782 l = snprintf(buf, sz, "%d,", v->skipto);
1784 case IPFW_VTYPE_NETGRAPH:
1785 l = snprintf(buf, sz, "%u,", v->netgraph);
1787 case IPFW_VTYPE_FIB:
1788 l = snprintf(buf, sz, "%u,", v->fib);
1790 case IPFW_VTYPE_NAT:
1791 l = snprintf(buf, sz, "%u,", v->nat);
1793 case IPFW_VTYPE_LIMIT:
1794 l = snprintf(buf, sz, "%u,", v->limit);
1796 case IPFW_VTYPE_NH4:
1797 a4.s_addr = htonl(v->nh4);
1798 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1799 l = snprintf(buf, sz, "%s,", abuf);
1801 case IPFW_VTYPE_DSCP:
1802 l = snprintf(buf, sz, "%d,", v->dscp);
1804 case IPFW_VTYPE_NH6:
1805 sa6.sin6_family = AF_INET6;
1806 sa6.sin6_len = sizeof(sa6);
1807 sa6.sin6_addr = v->nh6;
1809 sa6.sin6_scope_id = v->zoneid;
1810 if (getnameinfo((const struct sockaddr *)&sa6,
1811 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1812 NI_NUMERICHOST) == 0)
1813 l = snprintf(buf, sz, "%s,", abuf);
1826 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1828 char *comma, tbuf[128], pval[128];
1830 struct tflow_entry *tfe;
1832 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1836 case IPFW_TABLE_ADDR:
1837 /* IPv4 or IPv6 prefixes */
1838 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1839 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1841 case IPFW_TABLE_INTERFACE:
1842 /* Interface names */
1843 printf("%s %s\n", tent->k.iface, pval);
1845 case IPFW_TABLE_NUMBER:
1847 printf("%u %s\n", tent->k.key, pval);
1849 case IPFW_TABLE_FLOW:
1851 tfe = &tent->k.flow;
1854 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1855 if (tfe->af == AF_INET)
1856 paddr = &tfe->a.a4.sip;
1858 paddr = &tfe->a.a6.sip6;
1860 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1861 printf("%s%s", comma, tbuf);
1865 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1866 printf("%s%d", comma, tfe->proto);
1870 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1871 printf("%s%d", comma, ntohs(tfe->sport));
1874 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1875 if (tfe->af == AF_INET)
1876 paddr = &tfe->a.a4.dip;
1878 paddr = &tfe->a.a6.dip6;
1880 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1881 printf("%s%s", comma, tbuf);
1885 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1886 printf("%s%d", comma, ntohs(tfe->dport));
1890 printf(" %s\n", pval);
1895 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1897 ipfw_obj_lheader req, *olh;
1900 memset(&req, 0, sizeof(req));
1903 if (do_get3(opcode, &req.opheader, &sz) != 0)
1904 if (errno != ENOMEM)
1908 if ((olh = calloc(1, sz)) == NULL)
1912 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1922 table_do_get_algolist(ipfw_obj_lheader **polh)
1925 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
1929 table_do_get_vlist(ipfw_obj_lheader **polh)
1932 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
1936 ipfw_list_ta(int ac, char *av[])
1938 ipfw_obj_lheader *olh;
1943 error = table_do_get_algolist(&olh);
1945 err(EX_OSERR, "Unable to request algorithm list");
1947 info = (ipfw_ta_info *)(olh + 1);
1948 for (i = 0; i < olh->count; i++) {
1949 if ((atype = match_value(tabletypes, info->type)) == NULL)
1951 printf("--- %s ---\n", info->algoname);
1952 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
1954 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
1961 /* Copy of current kernel table_value structure */
1962 struct _table_value {
1963 uint32_t tag; /* O_TAG/O_TAGGED */
1964 uint32_t pipe; /* O_PIPE/O_QUEUE */
1965 uint16_t divert; /* O_DIVERT/O_TEE */
1966 uint16_t skipto; /* skipto, CALLRET */
1967 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
1968 uint32_t fib; /* O_SETFIB */
1969 uint32_t nat; /* O_NAT */
1974 /* -- 32 bytes -- */
1975 struct in6_addr nh6;
1976 uint32_t limit; /* O_LIMIT */
1978 uint64_t refcnt; /* Number of references */
1982 compare_values(const void *_a, const void *_b)
1984 struct _table_value *a, *b;
1986 a = (struct _table_value *)_a;
1987 b = (struct _table_value *)_b;
1989 if (a->spare1 < b->spare1)
1991 else if (a->spare1 > b->spare1)
1998 ipfw_list_values(int ac, char *av[])
2000 ipfw_obj_lheader *olh;
2001 struct _table_value *v;
2006 error = table_do_get_vlist(&olh);
2008 err(EX_OSERR, "Unable to request value list");
2010 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2012 table_print_valheader(buf, sizeof(buf), vmask);
2013 printf("HEADER: %s\n", buf);
2014 v = (struct _table_value *)(olh + 1);
2015 qsort(v, olh->count, olh->objsize, compare_values);
2016 for (i = 0; i < olh->count; i++) {
2017 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2019 printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2020 v = (struct _table_value *)((caddr_t)v + olh->objsize);
2027 table_check_name(const char *tablename)
2030 if (ipfw_check_object_name(tablename) != 0)
2032 /* Restrict some 'special' names */
2033 if (strcmp(tablename, "all") == 0)