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 : (void*)0;
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)
825 int is_all = (int)arg;
827 if ((error = table_do_get_list(i, &oh)) != 0) {
828 err(EX_OSERR, "Error requesting table %s list", i->tablename);
832 table_show_list(oh, is_all);
839 table_flush_one(ipfw_xtable_info *i, void *arg)
843 oh = (ipfw_obj_header *)arg;
845 table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
847 return (table_flush(oh));
851 table_do_modify_record(int cmd, ipfw_obj_header *oh,
852 ipfw_obj_tentry *tent, int count, int atomic)
855 ipfw_obj_tentry *tent_base;
857 char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
861 sz = sizeof(*ctlv) + sizeof(*tent) * count;
863 memset(xbuf, 0, sizeof(xbuf));
866 if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
870 memcpy(pbuf, oh, sizeof(*oh));
871 oh = (ipfw_obj_header *)pbuf;
872 oh->opheader.version = 1;
874 ctlv = (ipfw_obj_ctlv *)(oh + 1);
876 ctlv->head.length = sz;
878 ctlv->flags |= IPFW_CTF_ATOMIC;
881 memcpy(ctlv + 1, tent, sizeof(*tent) * count);
882 tent = (ipfw_obj_tentry *)(ctlv + 1);
883 for (i = 0; i < count; i++, tent++) {
884 tent->head.length = sizeof(ipfw_obj_tentry);
889 error = do_get3(cmd, &oh->opheader, &sz);
892 tent = (ipfw_obj_tentry *)(ctlv + 1);
893 /* Copy result back to provided buffer */
894 memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
903 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
904 int quiet, int update, int atomic)
906 ipfw_obj_tentry *ptent, tent, *tent_buf;
910 int cmd, count, error, i, ignored;
911 char *texterr, *etxt, *px;
914 errx(EX_USAGE, "address required");
917 cmd = IP_FW_TABLE_XADD;
918 texterr = "Adding record failed";
920 cmd = IP_FW_TABLE_XDEL;
921 texterr = "Deleting record failed";
925 * Calculate number of entries:
926 * Assume [key val] x N for add
930 count = (add != 0) ? ac / 2 + 1 : ac;
933 /* Adding single entry with/without value */
934 memset(&tent, 0, sizeof(tent));
938 if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
940 "Unable to allocate memory for all entries");
944 memset(&xi, 0, sizeof(xi));
947 tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
950 * Compatibility layer: auto-create table if not exists.
952 if (xi.tablename[0] == '\0') {
955 strlcpy(xi.tablename, oh->ntlv.name,
956 sizeof(xi.tablename));
958 warnx("DEPRECATED: inserting data into "
959 "non-existent table %s. (auto-created)",
961 table_do_create(oh, &xi);
964 oh->ntlv.type = type;
967 if (add != 0 && ac > 0) {
968 tentry_fill_value(oh, ptent, *av, type, vmask);
973 ptent->head.flags |= IPFW_TF_UPDATE;
979 error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
982 * Compatibility stuff: do not yell on duplicate keys or
985 if (error == 0 || (error == EEXIST && add != 0) ||
986 (error == ENOENT && add == 0)) {
988 if (tent_buf != &tent)
994 /* Report results back */
996 for (i = 0; i < count; ptent++, i++) {
998 switch (ptent->result) {
1002 case IPFW_TR_DELETED:
1005 case IPFW_TR_UPDATED:
1016 case IPFW_TR_NOTFOUND:
1020 case IPFW_TR_EXISTS:
1024 case IPFW_TR_IGNORED:
1033 if (error != 0 && atomic != 0 && ignored == 0)
1034 printf("%s(reverted): ", px);
1038 table_show_entry(&xi, ptent);
1041 if (tent_buf != &tent)
1046 /* Get real OS error */
1049 /* Try to provide more human-readable error */
1052 etxt = "record already exists";
1058 etxt = "table not found";
1061 etxt = "record not found";
1064 etxt = "table is locked";
1067 etxt = strerror(error);
1070 errx(EX_OSERR, "%s: %s", texterr, etxt);
1074 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1075 ipfw_obj_tentry *xtent)
1077 char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1078 ipfw_obj_tentry *tent;
1083 memcpy(xbuf, oh, sizeof(*oh));
1084 oh = (ipfw_obj_header *)xbuf;
1085 tent = (ipfw_obj_tentry *)(oh + 1);
1087 memset(tent, 0, sizeof(*tent));
1088 tent->head.length = sizeof(*tent);
1091 tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1092 oh->ntlv.type = type;
1095 if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1098 if (sz < sizeof(xbuf))
1107 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1109 ipfw_obj_tentry xtent;
1110 ipfw_xtable_info xi;
1115 errx(EX_USAGE, "address required");
1117 strlcpy(key, *av, sizeof(key));
1119 memset(&xi, 0, sizeof(xi));
1120 error = table_do_lookup(oh, key, &xi, &xtent);
1126 errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1128 errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1130 errx(EX_UNAVAILABLE, "Table %s algo does not support "
1131 "\"lookup\" method", oh->ntlv.name);
1133 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1136 table_show_entry(&xi, &xtent);
1140 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1145 struct in6_addr *paddr, tmp;
1146 struct tflow_entry *tfe;
1147 uint32_t key, *pkey;
1149 struct protoent *pent;
1150 struct servent *sent;
1155 paddr = (struct in6_addr *)&tentry->k;
1158 case IPFW_TABLE_ADDR:
1159 /* Remove / if exists */
1160 if ((p = strchr(arg, '/')) != NULL) {
1165 if (inet_pton(AF_INET, arg, paddr) == 1) {
1166 if (p != NULL && mask > 32)
1167 errx(EX_DATAERR, "bad IPv4 mask width: %s",
1170 masklen = p ? mask : 32;
1172 } else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1173 if (IN6_IS_ADDR_V4COMPAT(paddr))
1175 "Use IPv4 instead of v4-compatible");
1176 if (p != NULL && mask > 128)
1177 errx(EX_DATAERR, "bad IPv6 mask width: %s",
1180 masklen = p ? mask : 128;
1184 if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1185 errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1188 type = IPFW_TABLE_ADDR;
1192 case IPFW_TABLE_INTERFACE:
1193 /* Assume interface name. Copy significant data only */
1194 mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1195 memcpy(paddr, arg, mask);
1196 /* Set mask to exact match */
1197 masklen = 8 * IF_NAMESIZE;
1199 case IPFW_TABLE_NUMBER:
1200 /* Port or any other key */
1201 key = strtol(arg, &p, 10);
1203 errx(EX_DATAERR, "Invalid number: %s", arg);
1205 pkey = (uint32_t *)paddr;
1209 case IPFW_TABLE_FLOW:
1210 /* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1211 tfe = &tentry->k.flow;
1214 /* Handle <ipv4|ipv6> */
1215 if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1216 if ((p = strchr(arg, ',')) != NULL)
1218 /* Determine family using temporary storage */
1219 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1220 if (af != 0 && af != AF_INET)
1222 "Inconsistent address family\n");
1224 memcpy(&tfe->a.a4.sip, &tmp, 4);
1225 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1226 if (af != 0 && af != AF_INET6)
1228 "Inconsistent address family\n");
1230 memcpy(&tfe->a.a6.sip6, &tmp, 16);
1236 /* Handle <proto-num|proto-name> */
1237 if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1239 errx(EX_DATAERR, "invalid key: proto missing");
1240 if ((p = strchr(arg, ',')) != NULL)
1243 key = strtol(arg, &pp, 10);
1245 if ((pent = getprotobyname(arg)) == NULL)
1246 errx(EX_DATAERR, "Unknown proto: %s",
1249 key = pent->p_proto;
1253 errx(EX_DATAERR, "Bad protocol number: %u",key);
1260 /* Handle <port-num|service-name> */
1261 if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1263 errx(EX_DATAERR, "invalid key: src port missing");
1264 if ((p = strchr(arg, ',')) != NULL)
1267 port = htons(strtol(arg, &pp, 10));
1269 if ((sent = getservbyname(arg, NULL)) == NULL)
1270 errx(EX_DATAERR, "Unknown service: %s",
1272 port = sent->s_port;
1278 /* Handle <ipv4|ipv6>*/
1279 if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1281 errx(EX_DATAERR, "invalid key: dst ip missing");
1282 if ((p = strchr(arg, ',')) != NULL)
1284 /* Determine family using temporary storage */
1285 if (inet_pton(AF_INET, arg, &tmp) == 1) {
1286 if (af != 0 && af != AF_INET)
1288 "Inconsistent address family");
1290 memcpy(&tfe->a.a4.dip, &tmp, 4);
1291 } else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1292 if (af != 0 && af != AF_INET6)
1294 "Inconsistent address family");
1296 memcpy(&tfe->a.a6.dip6, &tmp, 16);
1302 /* Handle <port-num|service-name> */
1303 if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1305 errx(EX_DATAERR, "invalid key: dst port missing");
1306 if ((p = strchr(arg, ',')) != NULL)
1309 port = htons(strtol(arg, &pp, 10));
1311 if ((sent = getservbyname(arg, NULL)) == NULL)
1312 errx(EX_DATAERR, "Unknown service: %s",
1314 port = sent->s_port;
1325 errx(EX_DATAERR, "Unsupported table type: %d", type);
1328 tentry->subtype = af;
1329 tentry->masklen = masklen;
1333 * Tries to guess table key type.
1334 * This procedure is used in legacy table auto-create
1335 * code AND in `ipfw -n` ruleset checking.
1337 * Imported from old table_fill_xentry() parse code.
1340 guess_key_type(char *key, uint8_t *ptype)
1343 struct in6_addr addr;
1346 if (ishexnumber(*key) != 0 || *key == ':') {
1347 /* Remove / if exists */
1348 if ((p = strchr(key, '/')) != NULL)
1351 if ((inet_pton(AF_INET, key, &addr) == 1) ||
1352 (inet_pton(AF_INET6, key, &addr) == 1)) {
1353 *ptype = IPFW_TABLE_CIDR;
1358 /* Port or any other key */
1359 /* Skip non-base 10 entries like 'fa1' */
1360 kv = strtol(key, &p, 10);
1362 *ptype = IPFW_TABLE_NUMBER;
1364 } else if ((p != key) && (*p == '.')) {
1366 * Warn on IPv4 address strings
1367 * which are "valid" for inet_aton() but not
1370 * Typical examples: '10.5' or '10.0.0.05'
1377 if (strchr(key, '.') == NULL) {
1378 *ptype = IPFW_TABLE_INTERFACE;
1382 if (lookup_host(key, (struct in_addr *)&addr) != 0)
1385 *ptype = IPFW_TABLE_CIDR;
1390 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1391 int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1393 uint8_t type, tflags;
1401 if (xi->tablename[0] == '\0')
1402 error = table_get_info(oh, xi);
1407 if (co.test_only == 0) {
1410 tflags = xi->tflags;
1414 * We're running `ipfw -n`
1415 * Compatibility layer: try to guess key type
1418 if (guess_key_type(key, &type) != 0) {
1420 errx(EX_USAGE, "Cannot guess "
1421 "key '%s' type", key);
1423 vmask = IPFW_VTYPE_LEGACY;
1427 errx(EX_OSERR, "Error requesting table %s info",
1430 errx(EX_DATAERR, "Table %s does not exist",
1433 * Table does not exist
1434 * Compatibility layer: try to guess key type before failing.
1436 if (guess_key_type(key, &type) != 0) {
1438 errx(EX_USAGE, "Table %s does not exist, cannot guess "
1439 "key '%s' type", oh->ntlv.name, key);
1442 vmask = IPFW_VTYPE_LEGACY;
1445 tentry_fill_key_type(key, tent, type, tflags);
1452 set_legacy_value(uint32_t val, ipfw_table_value *v)
1462 v->dscp = (uint8_t)val;
1467 tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *arg,
1468 uint8_t type, uint32_t vmask)
1470 struct addrinfo hints, *res;
1471 uint32_t a4, flag, val;
1472 ipfw_table_value *v;
1475 char *comma, *e, *etype, *n, *p;
1476 struct in_addr ipaddr;
1480 /* Compat layer: keep old behavior for legacy value types */
1481 if (vmask == IPFW_VTYPE_LEGACY) {
1482 /* Try to interpret as number first */
1483 val = strtoul(arg, &p, 0);
1485 set_legacy_value(val, v);
1488 if (inet_pton(AF_INET, arg, &val) == 1) {
1489 set_legacy_value(ntohl(val), v);
1493 if (lookup_host(arg, &ipaddr) == 0) {
1494 set_legacy_value(ntohl(ipaddr.s_addr), v);
1497 errx(EX_OSERR, "Unable to parse value %s", arg);
1501 * Shorthands: handle single value if vmask consists
1502 * of numbers only. e.g.:
1503 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1508 for (i = 1; i < (1 << 31); i *= 2) {
1509 if ((flag = (vmask & i)) == 0)
1513 if ((comma = strchr(n, ',')) != NULL)
1517 case IPFW_VTYPE_TAG:
1518 v->tag = strtol(n, &e, 10);
1522 case IPFW_VTYPE_PIPE:
1523 v->pipe = strtol(n, &e, 10);
1527 case IPFW_VTYPE_DIVERT:
1528 v->divert = strtol(n, &e, 10);
1532 case IPFW_VTYPE_SKIPTO:
1533 v->skipto = strtol(n, &e, 10);
1537 case IPFW_VTYPE_NETGRAPH:
1538 v->netgraph = strtol(n, &e, 10);
1542 case IPFW_VTYPE_FIB:
1543 v->fib = strtol(n, &e, 10);
1547 case IPFW_VTYPE_NAT:
1548 v->nat = strtol(n, &e, 10);
1552 case IPFW_VTYPE_LIMIT:
1553 v->limit = strtol(n, &e, 10);
1557 case IPFW_VTYPE_NH4:
1558 if (strchr(n, '.') != NULL &&
1559 inet_pton(AF_INET, n, &a4) == 1) {
1563 if (lookup_host(n, &ipaddr) == 0) {
1564 v->nh4 = ntohl(ipaddr.s_addr);
1569 case IPFW_VTYPE_DSCP:
1571 if ((dval = match_token(f_ipdscp, n)) != -1) {
1575 etype = "DSCP code";
1577 v->dscp = strtol(n, &e, 10);
1578 if (v->dscp > 63 || *e != '\0')
1579 etype = "DSCP value";
1582 case IPFW_VTYPE_NH6:
1583 if (strchr(n, ':') != NULL) {
1584 memset(&hints, 0, sizeof(hints));
1585 hints.ai_family = AF_INET6;
1586 hints.ai_flags = AI_NUMERICHOST;
1587 if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1588 v->nh6 = ((struct sockaddr_in6 *)
1589 res->ai_addr)->sin6_addr;
1590 v->zoneid = ((struct sockaddr_in6 *)
1591 res->ai_addr)->sin6_scope_id;
1601 errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1606 if ((n = comma) != NULL)
1611 errx(EX_USAGE, "Not enough fields inside value");
1616 * Compare table names.
1617 * Honor number comparison.
1620 tablename_cmp(const void *a, const void *b)
1622 ipfw_xtable_info *ia, *ib;
1624 ia = (ipfw_xtable_info *)a;
1625 ib = (ipfw_xtable_info *)b;
1627 return (stringnum_cmp(ia->tablename, ib->tablename));
1631 * Retrieves table list from kernel,
1632 * optionally sorts it and calls requested function for each table.
1633 * Returns 0 on success.
1636 tables_foreach(table_cb_t *f, void *arg, int sort)
1638 ipfw_obj_lheader *olh;
1639 ipfw_xtable_info *info;
1643 /* Start with reasonable default */
1644 sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1647 if ((olh = calloc(1, sz)) == NULL)
1651 if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1654 if (errno != ENOMEM)
1660 qsort(olh + 1, olh->count, olh->objsize,
1663 info = (ipfw_xtable_info *)(olh + 1);
1664 for (i = 0; i < olh->count; i++) {
1665 if (co.use_set == 0 || info->set == co.use_set - 1)
1666 error = f(info, arg);
1667 info = (ipfw_xtable_info *)((caddr_t)info +
1678 * Retrieves all entries for given table @i in
1679 * eXtended format. Allocate buffer large enough
1680 * to store result. Called needs to free it later.
1682 * Returns 0 on success.
1685 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1687 ipfw_obj_header *oh;
1693 for (c = 0; c < 8; c++) {
1698 if ((oh = calloc(1, sz)) == NULL)
1700 table_fill_objheader(oh, i);
1701 oh->opheader.version = 1; /* Current version */
1702 if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1707 if (errno != ENOMEM)
1716 * Shows all entries from @oh in human-readable format
1719 table_show_list(ipfw_obj_header *oh, int need_header)
1721 ipfw_obj_tentry *tent;
1723 ipfw_xtable_info *i;
1725 i = (ipfw_xtable_info *)(oh + 1);
1726 tent = (ipfw_obj_tentry *)(i + 1);
1729 printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1733 table_show_entry(i, tent);
1734 tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1740 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1741 uint32_t vmask, int print_ip)
1743 char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1744 struct sockaddr_in6 sa6;
1745 uint32_t flag, i, l;
1752 * Some shorthands for printing values:
1753 * legacy assumes all values are equal, so keep the first one.
1755 if (vmask == IPFW_VTYPE_LEGACY) {
1756 if (print_ip != 0) {
1757 flag = htonl(v->tag);
1758 inet_ntop(AF_INET, &flag, buf, sz);
1760 snprintf(buf, sz, "%u", v->tag);
1764 for (i = 1; i < (1 << 31); i *= 2) {
1765 if ((flag = (vmask & i)) == 0)
1770 case IPFW_VTYPE_TAG:
1771 l = snprintf(buf, sz, "%u,", v->tag);
1773 case IPFW_VTYPE_PIPE:
1774 l = snprintf(buf, sz, "%u,", v->pipe);
1776 case IPFW_VTYPE_DIVERT:
1777 l = snprintf(buf, sz, "%d,", v->divert);
1779 case IPFW_VTYPE_SKIPTO:
1780 l = snprintf(buf, sz, "%d,", v->skipto);
1782 case IPFW_VTYPE_NETGRAPH:
1783 l = snprintf(buf, sz, "%u,", v->netgraph);
1785 case IPFW_VTYPE_FIB:
1786 l = snprintf(buf, sz, "%u,", v->fib);
1788 case IPFW_VTYPE_NAT:
1789 l = snprintf(buf, sz, "%u,", v->nat);
1791 case IPFW_VTYPE_LIMIT:
1792 l = snprintf(buf, sz, "%u,", v->limit);
1794 case IPFW_VTYPE_NH4:
1795 a4.s_addr = htonl(v->nh4);
1796 inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1797 l = snprintf(buf, sz, "%s,", abuf);
1799 case IPFW_VTYPE_DSCP:
1800 l = snprintf(buf, sz, "%d,", v->dscp);
1802 case IPFW_VTYPE_NH6:
1803 sa6.sin6_family = AF_INET6;
1804 sa6.sin6_len = sizeof(sa6);
1805 sa6.sin6_addr = v->nh6;
1807 sa6.sin6_scope_id = v->zoneid;
1808 if (getnameinfo((const struct sockaddr *)&sa6,
1809 sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1810 NI_NUMERICHOST) == 0)
1811 l = snprintf(buf, sz, "%s,", abuf);
1824 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1826 char *comma, tbuf[128], pval[128];
1828 struct tflow_entry *tfe;
1830 table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1834 case IPFW_TABLE_ADDR:
1835 /* IPv4 or IPv6 prefixes */
1836 inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1837 printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1839 case IPFW_TABLE_INTERFACE:
1840 /* Interface names */
1841 printf("%s %s\n", tent->k.iface, pval);
1843 case IPFW_TABLE_NUMBER:
1845 printf("%u %s\n", tent->k.key, pval);
1847 case IPFW_TABLE_FLOW:
1849 tfe = &tent->k.flow;
1852 if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1853 if (tfe->af == AF_INET)
1854 paddr = &tfe->a.a4.sip;
1856 paddr = &tfe->a.a6.sip6;
1858 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1859 printf("%s%s", comma, tbuf);
1863 if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1864 printf("%s%d", comma, tfe->proto);
1868 if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1869 printf("%s%d", comma, ntohs(tfe->sport));
1872 if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1873 if (tfe->af == AF_INET)
1874 paddr = &tfe->a.a4.dip;
1876 paddr = &tfe->a.a6.dip6;
1878 inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1879 printf("%s%s", comma, tbuf);
1883 if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1884 printf("%s%d", comma, ntohs(tfe->dport));
1888 printf(" %s\n", pval);
1893 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1895 ipfw_obj_lheader req, *olh;
1898 memset(&req, 0, sizeof(req));
1901 if (do_get3(opcode, &req.opheader, &sz) != 0)
1902 if (errno != ENOMEM)
1906 if ((olh = calloc(1, sz)) == NULL)
1910 if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1920 table_do_get_algolist(ipfw_obj_lheader **polh)
1923 return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
1927 table_do_get_vlist(ipfw_obj_lheader **polh)
1930 return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
1934 ipfw_list_ta(int ac, char *av[])
1936 ipfw_obj_lheader *olh;
1941 error = table_do_get_algolist(&olh);
1943 err(EX_OSERR, "Unable to request algorithm list");
1945 info = (ipfw_ta_info *)(olh + 1);
1946 for (i = 0; i < olh->count; i++) {
1947 if ((atype = match_value(tabletypes, info->type)) == NULL)
1949 printf("--- %s ---\n", info->algoname);
1950 printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
1952 info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
1959 /* Copy of current kernel table_value structure */
1960 struct _table_value {
1961 uint32_t tag; /* O_TAG/O_TAGGED */
1962 uint32_t pipe; /* O_PIPE/O_QUEUE */
1963 uint16_t divert; /* O_DIVERT/O_TEE */
1964 uint16_t skipto; /* skipto, CALLRET */
1965 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
1966 uint32_t fib; /* O_SETFIB */
1967 uint32_t nat; /* O_NAT */
1972 /* -- 32 bytes -- */
1973 struct in6_addr nh6;
1974 uint32_t limit; /* O_LIMIT */
1976 uint64_t refcnt; /* Number of references */
1980 compare_values(const void *_a, const void *_b)
1982 struct _table_value *a, *b;
1984 a = (struct _table_value *)_a;
1985 b = (struct _table_value *)_b;
1987 if (a->spare1 < b->spare1)
1989 else if (a->spare1 > b->spare1)
1996 ipfw_list_values(int ac, char *av[])
1998 ipfw_obj_lheader *olh;
1999 struct _table_value *v;
2004 error = table_do_get_vlist(&olh);
2006 err(EX_OSERR, "Unable to request value list");
2008 vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2010 table_print_valheader(buf, sizeof(buf), vmask);
2011 printf("HEADER: %s\n", buf);
2012 v = (struct _table_value *)(olh + 1);
2013 qsort(v, olh->count, olh->objsize, compare_values);
2014 for (i = 0; i < olh->count; i++) {
2015 table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2017 printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2018 v = (struct _table_value *)((caddr_t)v + olh->objsize);
2025 table_check_name(const char *tablename)
2028 if (ipfw_check_object_name(tablename) != 0)
2030 /* Restrict some 'special' names */
2031 if (strcmp(tablename, "all") == 0)