2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
5 * Copyright (c) 2014 Yandex LLC
6 * Copyright (c) 2014 Alexander V. Chernikov
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Lookup table support for ipfw.
36 * This file contains handlers for all generic tables' operations:
37 * add/del/flush entries, list/dump tables etc..
39 * Table data modification is protected by both UH and runtime lock
40 * while reading configuration/data is protected by UH lock.
42 * Lookup algorithms for all table types are located in ip_fw_table_algo.c
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
52 #include <sys/rwlock.h>
53 #include <sys/rmlock.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/queue.h>
57 #include <net/if.h> /* ip_fw.h requires IFNAMSIZ */
59 #include <netinet/in.h>
60 #include <netinet/ip_var.h> /* struct ipfw_rule_ref */
61 #include <netinet/ip_fw.h>
63 #include <netpfil/ipfw/ip_fw_private.h>
64 #include <netpfil/ipfw/ip_fw_table.h>
67 * Table has the following `type` concepts:
69 * `no.type` represents lookup key type (addr, ifp, uid, etc..)
70 * vmask represents bitmask of table values which are present at the moment.
71 * Special IPFW_VTYPE_LEGACY ( (uint32_t)-1 ) represents old
72 * single-value-for-all approach.
75 struct named_object no;
76 uint8_t tflags; /* type flags */
77 uint8_t locked; /* 1 if locked from changes */
78 uint8_t linked; /* 1 if already linked */
79 uint8_t ochanged; /* used by set swapping */
80 uint8_t vshared; /* 1 if using shared value array */
82 uint32_t count; /* Number of records */
83 uint32_t limit; /* Max number of records */
84 uint32_t vmask; /* bitmask with supported values */
85 uint32_t ocount; /* used by set swapping */
86 uint64_t gencnt; /* generation count */
87 char tablename[64]; /* table name */
88 struct table_algo *ta; /* Callbacks for given algo */
89 void *astate; /* algorithm state */
90 struct table_info ti_copy; /* data to put to table_info */
91 struct namedobj_instance *vi;
94 static int find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
95 struct table_config **tc);
96 static struct table_config *find_table(struct namedobj_instance *ni,
98 static struct table_config *alloc_table_config(struct ip_fw_chain *ch,
99 struct tid_info *ti, struct table_algo *ta, char *adata, uint8_t tflags);
100 static void free_table_config(struct namedobj_instance *ni,
101 struct table_config *tc);
102 static int create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
103 char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int ref);
104 static void link_table(struct ip_fw_chain *ch, struct table_config *tc);
105 static void unlink_table(struct ip_fw_chain *ch, struct table_config *tc);
106 static int find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
107 struct tentry_info *tei, uint32_t count, int op, struct table_config **ptc);
110 static int export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
111 struct sockopt_data *sd);
112 static void export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
113 ipfw_xtable_info *i);
114 static int dump_table_tentry(void *e, void *arg);
115 static int dump_table_xentry(void *e, void *arg);
117 static int swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
120 static int check_table_name(const char *name);
121 static int check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
122 struct table_config *tc, struct table_info *ti, uint32_t count);
123 static int destroy_table(struct ip_fw_chain *ch, struct tid_info *ti);
125 static struct table_algo *find_table_algo(struct tables_config *tableconf,
126 struct tid_info *ti, char *name);
128 static void objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti);
129 static void ntlv_to_ti(struct _ipfw_obj_ntlv *ntlv, struct tid_info *ti);
131 #define CHAIN_TO_NI(chain) (CHAIN_TO_TCFG(chain)->namehash)
132 #define KIDX_TO_TI(ch, k) (&(((struct table_info *)(ch)->tablestate)[k]))
134 #define TA_BUF_SZ 128 /* On-stack buffer for add/delete state */
137 rollback_toperation_state(struct ip_fw_chain *ch, void *object)
139 struct tables_config *tcfg;
142 tcfg = CHAIN_TO_TCFG(ch);
143 TAILQ_FOREACH(os, &tcfg->state_list, next)
144 os->func(object, os);
148 add_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
150 struct tables_config *tcfg;
152 tcfg = CHAIN_TO_TCFG(ch);
153 TAILQ_INSERT_HEAD(&tcfg->state_list, &ts->opstate, next);
157 del_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
159 struct tables_config *tcfg;
161 tcfg = CHAIN_TO_TCFG(ch);
162 TAILQ_REMOVE(&tcfg->state_list, &ts->opstate, next);
166 tc_ref(struct table_config *tc)
173 tc_unref(struct table_config *tc)
179 static struct table_value *
180 get_table_value(struct ip_fw_chain *ch, struct table_config *tc, uint32_t kidx)
182 struct table_value *pval;
184 pval = (struct table_value *)ch->valuestate;
186 return (&pval[kidx]);
190 * Checks if we're able to insert/update entry @tei into table
192 * May alter @tei to indicate insertion error / insert
195 * Returns 0 if operation can be performed/
198 check_table_limit(struct table_config *tc, struct tentry_info *tei)
201 if (tc->limit == 0 || tc->count < tc->limit)
204 if ((tei->flags & TEI_FLAGS_UPDATE) == 0) {
205 /* Notify userland on error cause */
206 tei->flags |= TEI_FLAGS_LIMIT;
211 * We have UPDATE flag set.
212 * Permit updating record (if found),
213 * but restrict adding new one since we've
214 * already hit the limit.
216 tei->flags |= TEI_FLAGS_DONTADD;
222 * Convert algorithm callback return code into
223 * one of pre-defined states known by userland.
226 store_tei_result(struct tentry_info *tei, int op, int error, uint32_t num)
234 if (op == OP_ADD && num != 0)
235 flag = TEI_FLAGS_ADDED;
237 flag = TEI_FLAGS_DELETED;
240 flag = TEI_FLAGS_NOTFOUND;
243 flag = TEI_FLAGS_EXISTS;
246 flag = TEI_FLAGS_ERROR;
253 * Creates and references table with default parameters.
254 * Saves table config, algo and allocated kidx info @ptc, @pta and
255 * @pkidx if non-zero.
256 * Used for table auto-creation to support old binaries.
258 * Returns 0 on success.
261 create_table_compat(struct ip_fw_chain *ch, struct tid_info *ti,
267 memset(&xi, 0, sizeof(xi));
268 /* Set default value mask for legacy clients */
269 xi.vmask = IPFW_VTYPE_LEGACY;
271 error = create_table_internal(ch, ti, NULL, &xi, pkidx, 1);
279 * Find and reference existing table optionally
282 * Saves found table config into @ptc.
283 * Note function may drop/acquire UH_WLOCK.
284 * Returns 0 if table was found/created and referenced
285 * or non-zero return code.
288 find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
289 struct tentry_info *tei, uint32_t count, int op,
290 struct table_config **ptc)
292 struct namedobj_instance *ni;
293 struct table_config *tc;
297 IPFW_UH_WLOCK_ASSERT(ch);
299 ni = CHAIN_TO_NI(ch);
301 if ((tc = find_table(ni, ti)) != NULL) {
302 /* check table type */
303 if (tc->no.subtype != ti->type)
309 /* Try to exit early on limit hit */
310 if (op == OP_ADD && count == 1 &&
311 check_table_limit(tc, tei) != 0)
314 /* Reference and return */
323 /* Compatibility mode: create new table for old clients */
324 if ((tei->flags & TEI_FLAGS_COMPAT) == 0)
328 error = create_table_compat(ch, ti, &kidx);
334 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
335 KASSERT(tc != NULL, ("create_table_compat returned bad idx %d", kidx));
337 /* OK, now we've got referenced table. */
343 * Rolls back already @added to @tc entries using state array @ta_buf_m.
344 * Assume the following layout:
345 * 1) ADD state (ta_buf_m[0] ... t_buf_m[added - 1]) for handling update cases
346 * 2) DEL state (ta_buf_m[count[ ... t_buf_m[count + added - 1])
347 * for storing deleted state
350 rollback_added_entries(struct ip_fw_chain *ch, struct table_config *tc,
351 struct table_info *tinfo, struct tentry_info *tei, caddr_t ta_buf_m,
352 uint32_t count, uint32_t added)
354 struct table_algo *ta;
355 struct tentry_info *ptei;
361 IPFW_UH_WLOCK_ASSERT(ch);
364 ta_buf_sz = ta->ta_buf_size;
366 vv = v + count * ta_buf_sz;
367 for (i = 0; i < added; i++, v += ta_buf_sz, vv += ta_buf_sz) {
369 if ((ptei->flags & TEI_FLAGS_UPDATED) != 0) {
371 * We have old value stored by previous
372 * call in @ptei->value. Do add once again
375 error = ta->add(tc->astate, tinfo, ptei, v, &num);
376 KASSERT(error == 0, ("rollback UPDATE fail"));
377 KASSERT(num == 0, ("rollback UPDATE fail2"));
381 error = ta->prepare_del(ch, ptei, vv);
382 KASSERT(error == 0, ("pre-rollback INSERT failed"));
383 error = ta->del(tc->astate, tinfo, ptei, vv, &num);
384 KASSERT(error == 0, ("rollback INSERT failed"));
390 * Prepares add/del state for all @count entries in @tei.
391 * Uses either stack buffer (@ta_buf) or allocates a new one.
392 * Stores pointer to allocated buffer back to @ta_buf.
394 * Returns 0 on success.
397 prepare_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
398 struct tentry_info *tei, uint32_t count, int op, caddr_t *ta_buf)
401 size_t ta_buf_sz, sz;
402 struct tentry_info *ptei;
406 ta_buf_sz = ta->ta_buf_size;
408 /* Single add/delete, use on-stack buffer */
409 memset(*ta_buf, 0, TA_BUF_SZ);
413 * Multiple adds/deletes, allocate larger buffer
415 * Note we need 2xcount buffer for add case:
416 * we have hold both ADD state
417 * and DELETE state (this may be needed
418 * if we need to rollback all changes)
420 sz = count * ta_buf_sz;
421 ta_buf_m = malloc((op == OP_ADD) ? sz * 2 : sz, M_TEMP,
426 for (i = 0; i < count; i++, v += ta_buf_sz) {
428 error = (op == OP_ADD) ?
429 ta->prepare_add(ch, ptei, v) : ta->prepare_del(ch, ptei, v);
432 * Some syntax error (incorrect mask, or address, or
433 * anything). Return error regardless of atomicity
445 * Flushes allocated state for each @count entries in @tei.
446 * Frees @ta_buf_m if differs from stack buffer @ta_buf.
449 flush_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
450 struct tentry_info *tei, uint32_t count, int rollback,
451 caddr_t ta_buf_m, caddr_t ta_buf)
454 struct tentry_info *ptei;
458 ta_buf_sz = ta->ta_buf_size;
460 /* Run cleaning callback anyway */
462 for (i = 0; i < count; i++, v += ta_buf_sz) {
464 ta->flush_entry(ch, ptei, v);
465 if (ptei->ptv != NULL) {
466 free(ptei->ptv, M_IPFW);
471 /* Clean up "deleted" state in case of rollback */
473 v = ta_buf_m + count * ta_buf_sz;
474 for (i = 0; i < count; i++, v += ta_buf_sz)
475 ta->flush_entry(ch, &tei[i], v);
478 if (ta_buf_m != ta_buf)
479 free(ta_buf_m, M_TEMP);
483 rollback_add_entry(void *object, struct op_state *_state)
485 struct ip_fw_chain *ch;
486 struct tableop_state *ts;
488 ts = (struct tableop_state *)_state;
490 if (ts->tc != object && ts->ch != object)
495 IPFW_UH_WLOCK_ASSERT(ch);
497 /* Call specifid unlockers */
498 rollback_table_values(ts);
500 /* Indicate we've called */
505 * Adds/updates one or more entries in table @ti.
507 * Function may drop/reacquire UH wlock multiple times due to
508 * items alloc, algorithm callbacks (check_space), value linkage
509 * (new values, value storage realloc), etc..
510 * Other processes like other adds (which may involve storage resize),
511 * table swaps (which changes table data and may change algo type),
512 * table modify (which may change value mask) may be executed
513 * simultaneously so we need to deal with it.
515 * The following approach was implemented:
516 * we have per-chain linked list, protected with UH lock.
517 * add_table_entry prepares special on-stack structure wthich is passed
518 * to its descendants. Users add this structure to this list before unlock.
519 * After performing needed operations and acquiring UH lock back, each user
520 * checks if structure has changed. If true, it rolls local state back and
521 * returns without error to the caller.
522 * add_table_entry() on its own checks if structure has changed and restarts
523 * its operation from the beginning (goto restart).
525 * Functions which are modifying fields of interest (currently
526 * resize_shared_value_storage() and swap_tables() )
527 * traverses given list while holding UH lock immediately before
528 * performing their operations calling function provided be list entry
529 * ( currently rollback_add_entry ) which performs rollback for all necessary
530 * state and sets appropriate values in structure indicating rollback
534 * Function references @ti first to ensure table won't
535 * disappear or change its type.
536 * After that, prepare_add callback is called for each @tei entry.
537 * Next, we try to add each entry under UH+WHLOCK
538 * using add() callback.
539 * Finally, we free all state by calling flush_entry callback
542 * Returns 0 on success.
545 add_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
546 struct tentry_info *tei, uint8_t flags, uint32_t count)
548 struct table_config *tc;
549 struct table_algo *ta;
551 int error, first_error, i, rollback;
552 uint32_t num, numadd;
553 struct tentry_info *ptei;
554 struct tableop_state ts;
555 char ta_buf[TA_BUF_SZ];
558 memset(&ts, 0, sizeof(ts));
563 * Find and reference existing table.
566 if (ts.modified != 0) {
568 flush_batch_buffer(ch, ta, tei, count, rollback,
570 memset(&ts, 0, sizeof(ts));
575 error = find_ref_table(ch, ti, tei, count, OP_ADD, &tc);
582 /* Fill in tablestate */
584 ts.opstate.func = rollback_add_entry;
586 ts.vshared = tc->vshared;
587 ts.vmask = tc->vmask;
592 add_toperation_state(ch, &ts);
595 /* Allocate memory and prepare record(s) */
596 /* Pass stack buffer by default */
598 error = prepare_batch_buffer(ch, ta, tei, count, OP_ADD, &ta_buf_m);
601 del_toperation_state(ch, &ts);
602 /* Drop reference we've used in first search */
605 /* Check prepare_batch_buffer() error */
610 * Check if table swap has happened.
611 * (so table algo might be changed).
612 * Restart operation to achieve consistent behavior.
614 if (ts.modified != 0)
618 * Link all values values to shared/per-table value array.
620 * May release/reacquire UH_WLOCK.
622 error = ipfw_link_table_values(ch, &ts, flags);
625 if (ts.modified != 0)
629 * Ensure we are able to add all entries without additional
630 * memory allocations. May release/reacquire UH_WLOCK.
633 error = check_table_space(ch, &ts, tc, KIDX_TO_TI(ch, kidx), count);
636 if (ts.modified != 0)
639 /* We've got valid table in @tc. Let's try to add data */
648 for (i = 0; i < count; i++, v += ta->ta_buf_size) {
651 /* check limit before adding */
652 if ((error = check_table_limit(tc, ptei)) == 0) {
654 * It should be safe to insert a record w/o
655 * a properly-linked value if atomicity is
658 * If the added item does not have a valid value
659 * index, it would get rejected by ta->add().
661 error = ta->add(tc->astate, KIDX_TO_TI(ch, kidx),
663 /* Set status flag to inform userland */
664 store_tei_result(ptei, OP_ADD, error, num);
667 /* Update number of records to ease limit checking */
673 if (first_error == 0)
677 * Some error have happened. Check our atomicity
678 * settings: continue if atomicity is not required,
679 * rollback changes otherwise.
681 if ((flags & IPFW_CTF_ATOMIC) == 0)
684 rollback_added_entries(ch, tc, KIDX_TO_TI(ch, kidx),
685 tei, ta_buf_m, count, i);
693 ipfw_garbage_table_values(ch, tc, tei, count, rollback);
695 /* Permit post-add algorithm grow/rehash. */
697 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
699 /* Return first error to user, if any */
705 flush_batch_buffer(ch, ta, tei, count, rollback, ta_buf_m, ta_buf);
711 * Deletes one or more entries in table @ti.
713 * Returns 0 on success.
716 del_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
717 struct tentry_info *tei, uint8_t flags, uint32_t count)
719 struct table_config *tc;
720 struct table_algo *ta;
721 struct tentry_info *ptei;
723 int error, first_error, i;
724 uint32_t num, numdel;
725 char ta_buf[TA_BUF_SZ];
729 * Find and reference existing table.
732 error = find_ref_table(ch, ti, tei, count, OP_DEL, &tc);
740 /* Allocate memory and prepare record(s) */
741 /* Pass stack buffer by default */
743 error = prepare_batch_buffer(ch, ta, tei, count, OP_DEL, &ta_buf_m);
749 /* Drop reference we've used in first search */
753 * Check if table algo is still the same.
754 * (changed ta may be the result of table swap).
768 for (i = 0; i < count; i++, v += ta->ta_buf_size) {
771 error = ta->del(tc->astate, KIDX_TO_TI(ch, kidx), ptei, v,
773 /* Save state for userland */
774 store_tei_result(ptei, OP_DEL, error, num);
775 if (error != 0 && first_error == 0)
782 /* Unlink non-used values */
783 ipfw_garbage_table_values(ch, tc, tei, count, 0);
786 /* Run post-del hook to permit shrinking */
787 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
792 /* Return first error to user, if any */
796 flush_batch_buffer(ch, ta, tei, count, 0, ta_buf_m, ta_buf);
802 * Ensure that table @tc has enough space to add @count entries without
803 * need for reallocation.
806 * 0) need_modify() (UH_WLOCK) - checks if @count items can be added w/o resize.
808 * 1) alloc_modify (no locks, M_WAITOK) - alloc new state based on @pflags.
809 * 2) prepare_modifyt (UH_WLOCK) - copy old data into new storage
810 * 3) modify (UH_WLOCK + WLOCK) - switch pointers
811 * 4) flush_modify (UH_WLOCK) - free state, if needed
813 * Returns 0 on success.
816 check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
817 struct table_config *tc, struct table_info *ti, uint32_t count)
819 struct table_algo *ta;
821 char ta_buf[TA_BUF_SZ];
824 IPFW_UH_WLOCK_ASSERT(ch);
828 if (ta->need_modify == NULL)
831 /* Acquire reference not to loose @tc between locks/unlocks */
835 * TODO: think about avoiding race between large add/large delete
836 * operation on algorithm which implements shrinking along with
841 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
846 /* We have to shrink/grow table */
848 add_toperation_state(ch, ts);
851 memset(&ta_buf, 0, sizeof(ta_buf));
852 error = ta->prepare_mod(ta_buf, &pflags);
856 del_toperation_state(ch, ts);
861 if (ts != NULL && ts->modified != 0) {
863 * Swap operation has happened
864 * so we're currently operating on other
865 * table data. Stop doing this.
867 ta->flush_mod(ta_buf);
871 /* Check if we still need to alter table */
872 ti = KIDX_TO_TI(ch, tc->no.kidx);
873 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
877 * Other thread has already performed resize.
878 * Flush our state and return.
880 ta->flush_mod(ta_buf);
884 error = ta->fill_mod(tc->astate, ti, ta_buf, &pflags);
886 /* Do actual modification */
888 ta->modify(tc->astate, ti, ta_buf, pflags);
892 /* Anyway, flush data and retry */
893 ta->flush_mod(ta_buf);
901 * Adds or deletes record in table.
903 * Request: [ ip_fw3_opheader ipfw_table_xentry ]
905 * Returns 0 on success
908 manage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
909 struct sockopt_data *sd)
911 ipfw_table_xentry *xent;
912 struct tentry_info tei;
914 struct table_value v;
915 int error, hdrlen, read;
917 hdrlen = offsetof(ipfw_table_xentry, k);
919 /* Check minimum header size */
920 if (sd->valsize < (sizeof(*op3) + hdrlen))
923 read = sizeof(ip_fw3_opheader);
925 /* Check if xentry len field is valid */
926 xent = (ipfw_table_xentry *)(op3 + 1);
927 if (xent->len < hdrlen || xent->len + read > sd->valsize)
930 memset(&tei, 0, sizeof(tei));
931 tei.paddr = &xent->k;
932 tei.masklen = xent->masklen;
933 ipfw_import_table_value_legacy(xent->value, &v);
935 /* Old requests compatibility */
936 tei.flags = TEI_FLAGS_COMPAT;
937 if (xent->type == IPFW_TABLE_ADDR) {
938 if (xent->len - hdrlen == sizeof(in_addr_t))
939 tei.subtype = AF_INET;
941 tei.subtype = AF_INET6;
944 memset(&ti, 0, sizeof(ti));
946 ti.type = xent->type;
948 error = (op3->opcode == IP_FW_TABLE_XADD) ?
949 add_table_entry(ch, &ti, &tei, 0, 1) :
950 del_table_entry(ch, &ti, &tei, 0, 1);
956 * Adds or deletes record in table.
957 * Data layout (v1)(current):
958 * Request: [ ipfw_obj_header
959 * ipfw_obj_ctlv(IPFW_TLV_TBLENT_LIST) [ ipfw_obj_tentry x N ]
962 * Returns 0 on success
965 manage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
966 struct sockopt_data *sd)
968 ipfw_obj_tentry *tent, *ptent;
971 struct tentry_info *ptei, tei, *tei_buf;
973 int error, i, kidx, read;
975 /* Check minimum header size */
976 if (sd->valsize < (sizeof(*oh) + sizeof(*ctlv)))
979 /* Check if passed data is too long */
980 if (sd->valsize != sd->kavail)
983 oh = (ipfw_obj_header *)sd->kbuf;
985 /* Basic length checks for TLVs */
986 if (oh->ntlv.head.length != sizeof(oh->ntlv))
991 ctlv = (ipfw_obj_ctlv *)(oh + 1);
992 if (ctlv->head.length + read != sd->valsize)
995 read += sizeof(*ctlv);
996 tent = (ipfw_obj_tentry *)(ctlv + 1);
997 if (ctlv->count * sizeof(*tent) + read != sd->valsize)
1000 if (ctlv->count == 0)
1004 * Mark entire buffer as "read".
1005 * This instructs sopt api write it back
1006 * after function return.
1008 ipfw_get_sopt_header(sd, sd->valsize);
1010 /* Perform basic checks for each entry */
1013 for (i = 0; i < ctlv->count; i++, ptent++) {
1014 if (ptent->head.length != sizeof(*ptent))
1016 if (ptent->idx != kidx)
1020 /* Convert data into kernel request objects */
1021 objheader_to_ti(oh, &ti);
1022 ti.type = oh->ntlv.type;
1025 /* Use on-stack buffer for single add/del */
1026 if (ctlv->count == 1) {
1027 memset(&tei, 0, sizeof(tei));
1030 tei_buf = malloc(ctlv->count * sizeof(tei), M_TEMP,
1035 for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1036 ptei->paddr = &ptent->k;
1037 ptei->subtype = ptent->subtype;
1038 ptei->masklen = ptent->masklen;
1039 if (ptent->head.flags & IPFW_TF_UPDATE)
1040 ptei->flags |= TEI_FLAGS_UPDATE;
1042 ipfw_import_table_value_v1(&ptent->v.value);
1043 ptei->pvalue = (struct table_value *)&ptent->v.value;
1046 error = (oh->opheader.opcode == IP_FW_TABLE_XADD) ?
1047 add_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count) :
1048 del_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count);
1050 /* Translate result back to userland */
1053 for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1054 if (ptei->flags & TEI_FLAGS_ADDED)
1055 ptent->result = IPFW_TR_ADDED;
1056 else if (ptei->flags & TEI_FLAGS_DELETED)
1057 ptent->result = IPFW_TR_DELETED;
1058 else if (ptei->flags & TEI_FLAGS_UPDATED)
1059 ptent->result = IPFW_TR_UPDATED;
1060 else if (ptei->flags & TEI_FLAGS_LIMIT)
1061 ptent->result = IPFW_TR_LIMIT;
1062 else if (ptei->flags & TEI_FLAGS_ERROR)
1063 ptent->result = IPFW_TR_ERROR;
1064 else if (ptei->flags & TEI_FLAGS_NOTFOUND)
1065 ptent->result = IPFW_TR_NOTFOUND;
1066 else if (ptei->flags & TEI_FLAGS_EXISTS)
1067 ptent->result = IPFW_TR_EXISTS;
1068 ipfw_export_table_value_v1(ptei->pvalue, &ptent->v.value);
1071 if (tei_buf != &tei)
1072 free(tei_buf, M_TEMP);
1078 * Looks up an entry in given table.
1079 * Data layout (v0)(current):
1080 * Request: [ ipfw_obj_header ipfw_obj_tentry ]
1081 * Reply: [ ipfw_obj_header ipfw_obj_tentry ]
1083 * Returns 0 on success
1086 find_table_entry(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1087 struct sockopt_data *sd)
1089 ipfw_obj_tentry *tent;
1090 ipfw_obj_header *oh;
1092 struct table_config *tc;
1093 struct table_algo *ta;
1094 struct table_info *kti;
1095 struct table_value *pval;
1096 struct namedobj_instance *ni;
1100 /* Check minimum header size */
1101 sz = sizeof(*oh) + sizeof(*tent);
1102 if (sd->valsize != sz)
1105 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1106 tent = (ipfw_obj_tentry *)(oh + 1);
1108 /* Basic length checks for TLVs */
1109 if (oh->ntlv.head.length != sizeof(oh->ntlv))
1112 objheader_to_ti(oh, &ti);
1113 ti.type = oh->ntlv.type;
1114 ti.uidx = tent->idx;
1117 ni = CHAIN_TO_NI(ch);
1120 * Find existing table and check its type .
1123 if ((tc = find_table(ni, &ti)) == NULL) {
1124 IPFW_UH_RUNLOCK(ch);
1128 /* check table type */
1129 if (tc->no.subtype != ti.type) {
1130 IPFW_UH_RUNLOCK(ch);
1134 kti = KIDX_TO_TI(ch, tc->no.kidx);
1137 if (ta->find_tentry == NULL)
1140 error = ta->find_tentry(tc->astate, kti, tent);
1142 pval = get_table_value(ch, tc, tent->v.kidx);
1143 ipfw_export_table_value_v1(pval, &tent->v.value);
1145 IPFW_UH_RUNLOCK(ch);
1151 * Flushes all entries or destroys given table.
1152 * Data layout (v0)(current):
1153 * Request: [ ipfw_obj_header ]
1155 * Returns 0 on success
1158 flush_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1159 struct sockopt_data *sd)
1162 struct _ipfw_obj_header *oh;
1165 if (sd->valsize != sizeof(*oh))
1168 oh = (struct _ipfw_obj_header *)op3;
1169 objheader_to_ti(oh, &ti);
1171 if (op3->opcode == IP_FW_TABLE_XDESTROY)
1172 error = destroy_table(ch, &ti);
1173 else if (op3->opcode == IP_FW_TABLE_XFLUSH)
1174 error = flush_table(ch, &ti);
1182 restart_flush(void *object, struct op_state *_state)
1184 struct tableop_state *ts;
1186 ts = (struct tableop_state *)_state;
1188 if (ts->tc != object)
1191 /* Indicate we've called */
1196 * Flushes given table.
1198 * Function create new table instance with the same
1199 * parameters, swaps it with old one and
1200 * flushes state without holding runtime WLOCK.
1202 * Returns 0 on success.
1205 flush_table(struct ip_fw_chain *ch, struct tid_info *ti)
1207 struct namedobj_instance *ni;
1208 struct table_config *tc;
1209 struct table_algo *ta;
1210 struct table_info ti_old, ti_new, *tablestate;
1211 void *astate_old, *astate_new;
1212 char algostate[64], *pstate;
1213 struct tableop_state ts;
1219 * Stage 1: save table algorithm.
1220 * Reference found table to ensure it won't disappear.
1223 ni = CHAIN_TO_NI(ch);
1224 if ((tc = find_table(ni, ti)) == NULL) {
1225 IPFW_UH_WUNLOCK(ch);
1230 memset(&ti_new, 0, sizeof(ti_new));
1232 /* Set up swap handler */
1233 memset(&ts, 0, sizeof(ts));
1234 ts.opstate.func = restart_flush;
1238 /* Do not flush readonly tables */
1239 if ((ta->flags & TA_FLAG_READONLY) != 0) {
1240 IPFW_UH_WUNLOCK(ch);
1243 /* Save startup algo parameters */
1244 if (ta->print_config != NULL) {
1245 ta->print_config(tc->astate, KIDX_TO_TI(ch, tc->no.kidx),
1246 algostate, sizeof(algostate));
1250 tflags = tc->tflags;
1252 add_toperation_state(ch, &ts);
1253 IPFW_UH_WUNLOCK(ch);
1256 * Stage 1.5: if this is not the first attempt, destroy previous state
1259 ta->destroy(astate_new, &ti_new);
1264 * Stage 2: allocate new table instance using same algo.
1266 memset(&ti_new, 0, sizeof(struct table_info));
1267 error = ta->init(ch, &astate_new, &ti_new, pstate, tflags);
1270 * Stage 3: swap old state pointers with newly-allocated ones.
1271 * Decrease refcount.
1275 del_toperation_state(ch, &ts);
1278 IPFW_UH_WUNLOCK(ch);
1283 * Restart operation if table swap has happened:
1284 * even if algo may be the same, algo init parameters
1285 * may change. Restart operation instead of doing
1288 if (ts.modified != 0) {
1289 /* Delay destroying data since we're holding UH lock */
1294 ni = CHAIN_TO_NI(ch);
1296 tablestate = (struct table_info *)ch->tablestate;
1299 ti_old = tablestate[kidx];
1300 tablestate[kidx] = ti_new;
1303 astate_old = tc->astate;
1304 tc->astate = astate_new;
1305 tc->ti_copy = ti_new;
1308 /* Notify algo on real @ti address */
1309 if (ta->change_ti != NULL)
1310 ta->change_ti(tc->astate, &tablestate[kidx]);
1313 * Stage 4: unref values.
1315 ipfw_unref_table_values(ch, tc, ta, astate_old, &ti_old);
1316 IPFW_UH_WUNLOCK(ch);
1319 * Stage 5: perform real flush/destroy.
1321 ta->destroy(astate_old, &ti_old);
1328 * Data layout (v0)(current):
1329 * Request: [ ipfw_obj_header ipfw_obj_ntlv ]
1331 * Returns 0 on success
1334 swap_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1335 struct sockopt_data *sd)
1338 struct _ipfw_obj_header *oh;
1339 struct tid_info ti_a, ti_b;
1341 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_obj_ntlv))
1344 oh = (struct _ipfw_obj_header *)op3;
1345 ntlv_to_ti(&oh->ntlv, &ti_a);
1346 ntlv_to_ti((ipfw_obj_ntlv *)(oh + 1), &ti_b);
1348 error = swap_tables(ch, &ti_a, &ti_b);
1354 * Swaps two tables of the same type/valtype.
1356 * Checks if tables are compatible and limits
1357 * permits swap, than actually perform swap.
1359 * Each table consists of 2 different parts:
1361 * @tc (with name, set, kidx) and rule bindings, which is "stable".
1365 * runtime data @ti (ch->tablestate)
1366 * runtime cache in @tc
1367 * algo-specific data (@tc->astate)
1374 * After that we call @ti change handler for each table.
1376 * Note that referencing @tc won't protect tc->ta from change.
1377 * XXX: Do we need to restrict swap between locked tables?
1378 * XXX: Do we need to exchange ftype?
1380 * Returns 0 on success.
1383 swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
1386 struct namedobj_instance *ni;
1387 struct table_config *tc_a, *tc_b;
1388 struct table_algo *ta;
1389 struct table_info ti, *tablestate;
1394 * Stage 1: find both tables and ensure they are of
1398 ni = CHAIN_TO_NI(ch);
1399 if ((tc_a = find_table(ni, a)) == NULL) {
1400 IPFW_UH_WUNLOCK(ch);
1403 if ((tc_b = find_table(ni, b)) == NULL) {
1404 IPFW_UH_WUNLOCK(ch);
1408 /* It is very easy to swap between the same table */
1410 IPFW_UH_WUNLOCK(ch);
1414 /* Check type and value are the same */
1415 if (tc_a->no.subtype!=tc_b->no.subtype || tc_a->tflags!=tc_b->tflags) {
1416 IPFW_UH_WUNLOCK(ch);
1420 /* Check limits before swap */
1421 if ((tc_a->limit != 0 && tc_b->count > tc_a->limit) ||
1422 (tc_b->limit != 0 && tc_a->count > tc_b->limit)) {
1423 IPFW_UH_WUNLOCK(ch);
1427 /* Check if one of the tables is readonly */
1428 if (((tc_a->ta->flags | tc_b->ta->flags) & TA_FLAG_READONLY) != 0) {
1429 IPFW_UH_WUNLOCK(ch);
1433 /* Notify we're going to swap */
1434 rollback_toperation_state(ch, tc_a);
1435 rollback_toperation_state(ch, tc_b);
1437 /* Everything is fine, prepare to swap */
1438 tablestate = (struct table_info *)ch->tablestate;
1439 ti = tablestate[tc_a->no.kidx];
1441 astate = tc_a->astate;
1442 count = tc_a->count;
1446 tablestate[tc_a->no.kidx] = tablestate[tc_b->no.kidx];
1447 tc_a->ta = tc_b->ta;
1448 tc_a->astate = tc_b->astate;
1449 tc_a->count = tc_b->count;
1451 tablestate[tc_b->no.kidx] = ti;
1453 tc_b->astate = astate;
1454 tc_b->count = count;
1457 /* Ensure tc.ti copies are in sync */
1458 tc_a->ti_copy = tablestate[tc_a->no.kidx];
1459 tc_b->ti_copy = tablestate[tc_b->no.kidx];
1461 /* Notify both tables on @ti change */
1462 if (tc_a->ta->change_ti != NULL)
1463 tc_a->ta->change_ti(tc_a->astate, &tablestate[tc_a->no.kidx]);
1464 if (tc_b->ta->change_ti != NULL)
1465 tc_b->ta->change_ti(tc_b->astate, &tablestate[tc_b->no.kidx]);
1467 IPFW_UH_WUNLOCK(ch);
1473 * Destroys table specified by @ti.
1474 * Data layout (v0)(current):
1475 * Request: [ ip_fw3_opheader ]
1477 * Returns 0 on success
1480 destroy_table(struct ip_fw_chain *ch, struct tid_info *ti)
1482 struct namedobj_instance *ni;
1483 struct table_config *tc;
1487 ni = CHAIN_TO_NI(ch);
1488 if ((tc = find_table(ni, ti)) == NULL) {
1489 IPFW_UH_WUNLOCK(ch);
1493 /* Do not permit destroying referenced tables */
1494 if (tc->no.refcnt > 0) {
1495 IPFW_UH_WUNLOCK(ch);
1500 unlink_table(ch, tc);
1503 /* Free obj index */
1504 if (ipfw_objhash_free_idx(ni, tc->no.kidx) != 0)
1505 printf("Error unlinking kidx %d from table %s\n",
1506 tc->no.kidx, tc->tablename);
1508 /* Unref values used in tables while holding UH lock */
1509 ipfw_unref_table_values(ch, tc, tc->ta, tc->astate, &tc->ti_copy);
1510 IPFW_UH_WUNLOCK(ch);
1512 free_table_config(ni, tc);
1518 roundup2p(uint32_t v)
1533 * Grow tables index.
1535 * Returns 0 on success.
1538 ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables)
1540 unsigned int ntables_old, tbl;
1541 struct namedobj_instance *ni;
1542 void *new_idx, *old_tablestate, *tablestate;
1543 struct table_info *ti;
1544 struct table_config *tc;
1547 /* Check new value for validity */
1550 if (ntables > IPFW_TABLES_MAX)
1551 ntables = IPFW_TABLES_MAX;
1552 /* Alight to nearest power of 2 */
1553 ntables = (unsigned int)roundup2p(ntables);
1555 /* Allocate new pointers */
1556 tablestate = malloc(ntables * sizeof(struct table_info),
1557 M_IPFW, M_WAITOK | M_ZERO);
1559 ipfw_objhash_bitmap_alloc(ntables, (void *)&new_idx, &new_blocks);
1563 tbl = (ntables >= V_fw_tables_max) ? V_fw_tables_max : ntables;
1564 ni = CHAIN_TO_NI(ch);
1566 /* Temporary restrict decreasing max_tables */
1567 if (ntables < V_fw_tables_max) {
1569 * FIXME: Check if we really can shrink
1571 IPFW_UH_WUNLOCK(ch);
1575 /* Copy table info/indices */
1576 memcpy(tablestate, ch->tablestate, sizeof(struct table_info) * tbl);
1577 ipfw_objhash_bitmap_merge(ni, &new_idx, &new_blocks);
1581 /* Change pointers */
1582 old_tablestate = ch->tablestate;
1583 ch->tablestate = tablestate;
1584 ipfw_objhash_bitmap_swap(ni, &new_idx, &new_blocks);
1586 ntables_old = V_fw_tables_max;
1587 V_fw_tables_max = ntables;
1591 /* Notify all consumers that their @ti pointer has changed */
1592 ti = (struct table_info *)ch->tablestate;
1593 for (i = 0; i < tbl; i++, ti++) {
1594 if (ti->lookup == NULL)
1596 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, i);
1597 if (tc == NULL || tc->ta->change_ti == NULL)
1600 tc->ta->change_ti(tc->astate, ti);
1603 IPFW_UH_WUNLOCK(ch);
1605 /* Free old pointers */
1606 free(old_tablestate, M_IPFW);
1607 ipfw_objhash_bitmap_free(new_idx, new_blocks);
1613 * Lookup table's named object by its @kidx.
1615 struct named_object *
1616 ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch, uint16_t kidx)
1619 return (ipfw_objhash_lookup_kidx(CHAIN_TO_NI(ch), kidx));
1623 * Take reference to table specified in @ntlv.
1624 * On success return its @kidx.
1627 ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx)
1630 struct table_config *tc;
1633 IPFW_UH_WLOCK_ASSERT(ch);
1635 ntlv_to_ti(ntlv, &ti);
1636 error = find_table_err(CHAIN_TO_NI(ch), &ti, &tc);
1644 *kidx = tc->no.kidx;
1650 ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx)
1653 struct namedobj_instance *ni;
1654 struct named_object *no;
1656 IPFW_UH_WLOCK_ASSERT(ch);
1657 ni = CHAIN_TO_NI(ch);
1658 no = ipfw_objhash_lookup_kidx(ni, kidx);
1659 KASSERT(no != NULL, ("Table with index %d not found", kidx));
1664 * Lookup an arbitrary key @paddr of length @plen in table @tbl.
1665 * Stores found value in @val.
1667 * Returns 1 if key was found.
1670 ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
1671 void *paddr, uint32_t *val)
1673 struct table_info *ti;
1675 ti = KIDX_TO_TI(ch, tbl);
1677 return (ti->lookup(ti, paddr, plen, val));
1681 * Info/List/dump support for tables.
1686 * High-level 'get' cmds sysctl handlers
1690 * Lists all tables currently available in kernel.
1691 * Data layout (v0)(current):
1692 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
1693 * Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
1695 * Returns 0 on success
1698 list_tables(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1699 struct sockopt_data *sd)
1701 struct _ipfw_obj_lheader *olh;
1704 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
1707 if (sd->valsize < olh->size)
1711 error = export_tables(ch, olh, sd);
1712 IPFW_UH_RUNLOCK(ch);
1718 * Store table info to buffer provided by @sd.
1719 * Data layout (v0)(current):
1720 * Request: [ ipfw_obj_header ipfw_xtable_info(empty)]
1721 * Reply: [ ipfw_obj_header ipfw_xtable_info ]
1723 * Returns 0 on success.
1726 describe_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1727 struct sockopt_data *sd)
1729 struct _ipfw_obj_header *oh;
1730 struct table_config *tc;
1734 sz = sizeof(*oh) + sizeof(ipfw_xtable_info);
1735 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1739 objheader_to_ti(oh, &ti);
1742 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
1743 IPFW_UH_RUNLOCK(ch);
1747 export_table_info(ch, tc, (ipfw_xtable_info *)(oh + 1));
1748 IPFW_UH_RUNLOCK(ch);
1754 * Modifies existing table.
1755 * Data layout (v0)(current):
1756 * Request: [ ipfw_obj_header ipfw_xtable_info ]
1758 * Returns 0 on success
1761 modify_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1762 struct sockopt_data *sd)
1764 struct _ipfw_obj_header *oh;
1765 ipfw_xtable_info *i;
1768 struct namedobj_instance *ni;
1769 struct table_config *tc;
1771 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1774 oh = (struct _ipfw_obj_header *)sd->kbuf;
1775 i = (ipfw_xtable_info *)(oh + 1);
1778 * Verify user-supplied strings.
1779 * Check for null-terminated/zero-length strings/
1781 tname = oh->ntlv.name;
1782 if (check_table_name(tname) != 0)
1785 objheader_to_ti(oh, &ti);
1789 ni = CHAIN_TO_NI(ch);
1790 if ((tc = find_table(ni, &ti)) == NULL) {
1791 IPFW_UH_WUNLOCK(ch);
1795 /* Do not support any modifications for readonly tables */
1796 if ((tc->ta->flags & TA_FLAG_READONLY) != 0) {
1797 IPFW_UH_WUNLOCK(ch);
1801 if ((i->mflags & IPFW_TMFLAGS_LIMIT) != 0)
1802 tc->limit = i->limit;
1803 if ((i->mflags & IPFW_TMFLAGS_LOCK) != 0)
1804 tc->locked = ((i->flags & IPFW_TGFLAGS_LOCKED) != 0);
1805 IPFW_UH_WUNLOCK(ch);
1811 * Creates new table.
1812 * Data layout (v0)(current):
1813 * Request: [ ipfw_obj_header ipfw_xtable_info ]
1815 * Returns 0 on success
1818 create_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1819 struct sockopt_data *sd)
1821 struct _ipfw_obj_header *oh;
1822 ipfw_xtable_info *i;
1823 char *tname, *aname;
1825 struct namedobj_instance *ni;
1827 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1830 oh = (struct _ipfw_obj_header *)sd->kbuf;
1831 i = (ipfw_xtable_info *)(oh + 1);
1834 * Verify user-supplied strings.
1835 * Check for null-terminated/zero-length strings/
1837 tname = oh->ntlv.name;
1838 aname = i->algoname;
1839 if (check_table_name(tname) != 0 ||
1840 strnlen(aname, sizeof(i->algoname)) == sizeof(i->algoname))
1843 if (aname[0] == '\0') {
1844 /* Use default algorithm */
1848 objheader_to_ti(oh, &ti);
1851 ni = CHAIN_TO_NI(ch);
1854 if (find_table(ni, &ti) != NULL) {
1855 IPFW_UH_RUNLOCK(ch);
1858 IPFW_UH_RUNLOCK(ch);
1860 return (create_table_internal(ch, &ti, aname, i, NULL, 0));
1864 * Creates new table based on @ti and @aname.
1866 * Assume @aname to be checked and valid.
1867 * Stores allocated table kidx inside @pkidx (if non-NULL).
1868 * Reference created table if @compat is non-zero.
1870 * Returns 0 on success.
1873 create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
1874 char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int compat)
1876 struct namedobj_instance *ni;
1877 struct table_config *tc, *tc_new, *tmp;
1878 struct table_algo *ta;
1881 ni = CHAIN_TO_NI(ch);
1883 ta = find_table_algo(CHAIN_TO_TCFG(ch), ti, aname);
1887 tc = alloc_table_config(ch, ti, ta, aname, i->tflags);
1891 tc->vmask = i->vmask;
1892 tc->limit = i->limit;
1893 if (ta->flags & TA_FLAG_READONLY)
1896 tc->locked = (i->flags & IPFW_TGFLAGS_LOCKED) != 0;
1900 /* Check if table has been already created */
1901 tc_new = find_table(ni, ti);
1902 if (tc_new != NULL) {
1904 * Compat: do not fail if we're
1905 * requesting to create existing table
1906 * which has the same type
1908 if (compat == 0 || tc_new->no.subtype != tc->no.subtype) {
1909 IPFW_UH_WUNLOCK(ch);
1910 free_table_config(ni, tc);
1914 /* Exchange tc and tc_new for proper refcounting & freeing */
1920 if (ipfw_objhash_alloc_idx(ni, &kidx) != 0) {
1921 IPFW_UH_WUNLOCK(ch);
1922 printf("Unable to allocate table index."
1923 " Consider increasing net.inet.ip.fw.tables_max");
1924 free_table_config(ni, tc);
1928 tc->no.etlv = IPFW_TLV_TBL_NAME;
1936 *pkidx = tc->no.kidx;
1938 IPFW_UH_WUNLOCK(ch);
1941 free_table_config(ni, tc_new);
1947 ntlv_to_ti(ipfw_obj_ntlv *ntlv, struct tid_info *ti)
1950 memset(ti, 0, sizeof(struct tid_info));
1951 ti->set = ntlv->set;
1952 ti->uidx = ntlv->idx;
1954 ti->tlen = ntlv->head.length;
1958 objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti)
1961 ntlv_to_ti(&oh->ntlv, ti);
1964 struct namedobj_instance *
1965 ipfw_get_table_objhash(struct ip_fw_chain *ch)
1968 return (CHAIN_TO_NI(ch));
1972 * Exports basic table info as name TLV.
1973 * Used inside dump_static_rules() to provide info
1974 * about all tables referenced by current ruleset.
1976 * Returns 0 on success.
1979 ipfw_export_table_ntlv(struct ip_fw_chain *ch, uint16_t kidx,
1980 struct sockopt_data *sd)
1982 struct namedobj_instance *ni;
1983 struct named_object *no;
1984 ipfw_obj_ntlv *ntlv;
1986 ni = CHAIN_TO_NI(ch);
1988 no = ipfw_objhash_lookup_kidx(ni, kidx);
1989 KASSERT(no != NULL, ("invalid table kidx passed"));
1991 ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
1995 ntlv->head.type = IPFW_TLV_TBL_NAME;
1996 ntlv->head.length = sizeof(*ntlv);
1997 ntlv->idx = no->kidx;
1998 strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
2004 struct ip_fw_chain *ch;
2005 struct table_info *ti;
2006 struct table_config *tc;
2007 struct sockopt_data *sd;
2012 ipfw_table_entry *ent;
2015 ipfw_obj_tentry tent;
2019 count_ext_entries(void *e, void *arg)
2021 struct dump_args *da;
2023 da = (struct dump_args *)arg;
2030 * Gets number of items from table either using
2031 * internal counter or calling algo callback for
2032 * externally-managed tables.
2034 * Returns number of records.
2037 table_get_count(struct ip_fw_chain *ch, struct table_config *tc)
2039 struct table_info *ti;
2040 struct table_algo *ta;
2041 struct dump_args da;
2043 ti = KIDX_TO_TI(ch, tc->no.kidx);
2046 /* Use internal counter for self-managed tables */
2047 if ((ta->flags & TA_FLAG_READONLY) == 0)
2050 /* Use callback to quickly get number of items */
2051 if ((ta->flags & TA_FLAG_EXTCOUNTER) != 0)
2052 return (ta->get_count(tc->astate, ti));
2054 /* Count number of iterms ourselves */
2055 memset(&da, 0, sizeof(da));
2056 ta->foreach(tc->astate, ti, count_ext_entries, &da);
2062 * Exports table @tc info into standard ipfw_xtable_info format.
2065 export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
2066 ipfw_xtable_info *i)
2068 struct table_info *ti;
2069 struct table_algo *ta;
2071 i->type = tc->no.subtype;
2072 i->tflags = tc->tflags;
2073 i->vmask = tc->vmask;
2074 i->set = tc->no.set;
2075 i->kidx = tc->no.kidx;
2076 i->refcnt = tc->no.refcnt;
2077 i->count = table_get_count(ch, tc);
2078 i->limit = tc->limit;
2079 i->flags |= (tc->locked != 0) ? IPFW_TGFLAGS_LOCKED : 0;
2080 i->size = i->count * sizeof(ipfw_obj_tentry);
2081 i->size += sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2082 strlcpy(i->tablename, tc->tablename, sizeof(i->tablename));
2083 ti = KIDX_TO_TI(ch, tc->no.kidx);
2085 if (ta->print_config != NULL) {
2086 /* Use algo function to print table config to string */
2087 ta->print_config(tc->astate, ti, i->algoname,
2088 sizeof(i->algoname));
2090 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2091 /* Dump algo-specific data, if possible */
2092 if (ta->dump_tinfo != NULL) {
2093 ta->dump_tinfo(tc->astate, ti, &i->ta_info);
2094 i->ta_info.flags |= IPFW_TATFLAGS_DATA;
2098 struct dump_table_args {
2099 struct ip_fw_chain *ch;
2100 struct sockopt_data *sd;
2104 export_table_internal(struct namedobj_instance *ni, struct named_object *no,
2107 ipfw_xtable_info *i;
2108 struct dump_table_args *dta;
2110 dta = (struct dump_table_args *)arg;
2112 i = (ipfw_xtable_info *)ipfw_get_sopt_space(dta->sd, sizeof(*i));
2113 KASSERT(i != NULL, ("previously checked buffer is not enough"));
2115 export_table_info(dta->ch, (struct table_config *)no, i);
2120 * Export all tables as ipfw_xtable_info structures to
2121 * storage provided by @sd.
2123 * If supplied buffer is too small, fills in required size
2124 * and returns ENOMEM.
2125 * Returns 0 on success.
2128 export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
2129 struct sockopt_data *sd)
2133 struct dump_table_args dta;
2135 count = ipfw_objhash_count(CHAIN_TO_NI(ch));
2136 size = count * sizeof(ipfw_xtable_info) + sizeof(ipfw_obj_lheader);
2138 /* Fill in header regadless of buffer size */
2140 olh->objsize = sizeof(ipfw_xtable_info);
2142 if (size > olh->size) {
2152 ipfw_objhash_foreach(CHAIN_TO_NI(ch), export_table_internal, &dta);
2158 * Dumps all table data
2159 * Data layout (v1)(current):
2160 * Request: [ ipfw_obj_header ], size = ipfw_xtable_info.size
2161 * Reply: [ ipfw_obj_header ipfw_xtable_info ipfw_obj_tentry x N ]
2163 * Returns 0 on success
2166 dump_table_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2167 struct sockopt_data *sd)
2169 struct _ipfw_obj_header *oh;
2170 ipfw_xtable_info *i;
2172 struct table_config *tc;
2173 struct table_algo *ta;
2174 struct dump_args da;
2177 sz = sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2178 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
2182 i = (ipfw_xtable_info *)(oh + 1);
2183 objheader_to_ti(oh, &ti);
2186 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2187 IPFW_UH_RUNLOCK(ch);
2190 export_table_info(ch, tc, i);
2192 if (sd->valsize < i->size) {
2194 * Submitted buffer size is not enough.
2195 * WE've already filled in @i structure with
2196 * relevant table info including size, so we
2197 * can return. Buffer will be flushed automatically.
2199 IPFW_UH_RUNLOCK(ch);
2204 * Do the actual dump in eXtended format
2206 memset(&da, 0, sizeof(da));
2208 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2214 ta->foreach(tc->astate, da.ti, dump_table_tentry, &da);
2215 IPFW_UH_RUNLOCK(ch);
2221 * Dumps all table data
2222 * Data layout (version 0)(legacy):
2223 * Request: [ ipfw_xtable ], size = IP_FW_TABLE_XGETSIZE()
2224 * Reply: [ ipfw_xtable ipfw_table_xentry x N ]
2226 * Returns 0 on success
2229 dump_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2230 struct sockopt_data *sd)
2234 struct table_config *tc;
2235 struct table_algo *ta;
2236 struct dump_args da;
2239 xtbl = (ipfw_xtable *)ipfw_get_sopt_header(sd, sizeof(ipfw_xtable));
2243 memset(&ti, 0, sizeof(ti));
2244 ti.uidx = xtbl->tbl;
2247 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2248 IPFW_UH_RUNLOCK(ch);
2251 count = table_get_count(ch, tc);
2252 sz = count * sizeof(ipfw_table_xentry) + sizeof(ipfw_xtable);
2256 xtbl->type = tc->no.subtype;
2257 xtbl->tbl = ti.uidx;
2259 if (sd->valsize < sz) {
2261 * Submitted buffer size is not enough.
2262 * WE've already filled in @i structure with
2263 * relevant table info including size, so we
2264 * can return. Buffer will be flushed automatically.
2266 IPFW_UH_RUNLOCK(ch);
2270 /* Do the actual dump in eXtended format */
2271 memset(&da, 0, sizeof(da));
2273 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2279 ta->foreach(tc->astate, da.ti, dump_table_xentry, &da);
2280 IPFW_UH_RUNLOCK(ch);
2286 * Legacy function to retrieve number of items in table.
2289 get_table_size(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2290 struct sockopt_data *sd)
2297 sz = sizeof(*op3) + sizeof(uint32_t);
2298 op3 = (ip_fw3_opheader *)ipfw_get_sopt_header(sd, sz);
2302 tbl = (uint32_t *)(op3 + 1);
2303 memset(&ti, 0, sizeof(ti));
2306 error = ipfw_count_xtable(ch, &ti, tbl);
2307 IPFW_UH_RUNLOCK(ch);
2312 * Legacy IP_FW_TABLE_GETSIZE handler
2315 ipfw_count_table(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2317 struct table_config *tc;
2319 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2321 *cnt = table_get_count(ch, tc);
2326 * Legacy IP_FW_TABLE_XGETSIZE handler
2329 ipfw_count_xtable(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2331 struct table_config *tc;
2334 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL) {
2336 return (0); /* 'table all list' requires success */
2339 count = table_get_count(ch, tc);
2340 *cnt = count * sizeof(ipfw_table_xentry);
2342 *cnt += sizeof(ipfw_xtable);
2347 dump_table_entry(void *e, void *arg)
2349 struct dump_args *da;
2350 struct table_config *tc;
2351 struct table_algo *ta;
2352 ipfw_table_entry *ent;
2353 struct table_value *pval;
2356 da = (struct dump_args *)arg;
2361 /* Out of memory, returning */
2362 if (da->cnt == da->size)
2365 ent->tbl = da->uidx;
2368 error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2372 ent->addr = da->tent.k.addr.s_addr;
2373 ent->masklen = da->tent.masklen;
2374 pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2375 ent->value = ipfw_export_table_value_legacy(pval);
2381 * Dumps table in pre-8.1 legacy format.
2384 ipfw_dump_table_legacy(struct ip_fw_chain *ch, struct tid_info *ti,
2387 struct table_config *tc;
2388 struct table_algo *ta;
2389 struct dump_args da;
2393 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2394 return (0); /* XXX: We should return ESRCH */
2398 /* This dump format supports IPv4 only */
2399 if (tc->no.subtype != IPFW_TABLE_ADDR)
2402 memset(&da, 0, sizeof(da));
2404 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2406 da.ent = &tbl->ent[0];
2407 da.size = tbl->size;
2410 ta->foreach(tc->astate, da.ti, dump_table_entry, &da);
2417 * Dumps table entry in eXtended format (v1)(current).
2420 dump_table_tentry(void *e, void *arg)
2422 struct dump_args *da;
2423 struct table_config *tc;
2424 struct table_algo *ta;
2425 struct table_value *pval;
2426 ipfw_obj_tentry *tent;
2429 da = (struct dump_args *)arg;
2434 tent = (ipfw_obj_tentry *)ipfw_get_sopt_space(da->sd, sizeof(*tent));
2435 /* Out of memory, returning */
2440 tent->head.length = sizeof(ipfw_obj_tentry);
2441 tent->idx = da->uidx;
2443 error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2447 pval = get_table_value(da->ch, da->tc, tent->v.kidx);
2448 ipfw_export_table_value_v1(pval, &tent->v.value);
2454 * Dumps table entry in eXtended format (v0).
2457 dump_table_xentry(void *e, void *arg)
2459 struct dump_args *da;
2460 struct table_config *tc;
2461 struct table_algo *ta;
2462 ipfw_table_xentry *xent;
2463 ipfw_obj_tentry *tent;
2464 struct table_value *pval;
2467 da = (struct dump_args *)arg;
2472 xent = (ipfw_table_xentry *)ipfw_get_sopt_space(da->sd, sizeof(*xent));
2473 /* Out of memory, returning */
2476 xent->len = sizeof(ipfw_table_xentry);
2477 xent->tbl = da->uidx;
2479 memset(&da->tent, 0, sizeof(da->tent));
2481 error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2485 /* Convert current format to previous one */
2486 xent->masklen = tent->masklen;
2487 pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2488 xent->value = ipfw_export_table_value_legacy(pval);
2489 /* Apply some hacks */
2490 if (tc->no.subtype == IPFW_TABLE_ADDR && tent->subtype == AF_INET) {
2491 xent->k.addr6.s6_addr32[3] = tent->k.addr.s_addr;
2492 xent->flags = IPFW_TCF_INET;
2494 memcpy(&xent->k, &tent->k, sizeof(xent->k));
2500 * Helper function to export table algo data
2501 * to tentry format before calling user function.
2503 * Returns 0 on success.
2506 prepare_table_tentry(void *e, void *arg)
2508 struct dump_args *da;
2509 struct table_config *tc;
2510 struct table_algo *ta;
2513 da = (struct dump_args *)arg;
2518 error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2522 da->f(&da->tent, da->farg);
2528 * Allow external consumers to read table entries in standard format.
2531 ipfw_foreach_table_tentry(struct ip_fw_chain *ch, uint16_t kidx,
2532 ta_foreach_f *f, void *arg)
2534 struct namedobj_instance *ni;
2535 struct table_config *tc;
2536 struct table_algo *ta;
2537 struct dump_args da;
2539 ni = CHAIN_TO_NI(ch);
2541 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
2547 memset(&da, 0, sizeof(da));
2549 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2554 ta->foreach(tc->astate, da.ti, prepare_table_tentry, &da);
2564 * Finds algorithm by index, table type or supplied name.
2566 * Returns pointer to algo or NULL.
2568 static struct table_algo *
2569 find_table_algo(struct tables_config *tcfg, struct tid_info *ti, char *name)
2572 struct table_algo *ta;
2574 if (ti->type > IPFW_TABLE_MAXTYPE)
2577 /* Search by index */
2578 if (ti->atype != 0) {
2579 if (ti->atype > tcfg->algo_count)
2581 return (tcfg->algo[ti->atype]);
2585 /* Return default algorithm for given type if set */
2586 return (tcfg->def_algo[ti->type]);
2589 /* Search by name */
2590 /* TODO: better search */
2591 for (i = 1; i <= tcfg->algo_count; i++) {
2595 * One can supply additional algorithm
2596 * parameters so we compare only the first word
2598 * 'addr:chash hsize=32'
2602 l = strlen(ta->name);
2603 if (strncmp(name, ta->name, l) != 0)
2605 if (name[l] != '\0' && name[l] != ' ')
2607 /* Check if we're requesting proper table type */
2608 if (ti->type != 0 && ti->type != ta->type)
2617 * Register new table algo @ta.
2618 * Stores algo id inside @idx.
2620 * Returns 0 on success.
2623 ipfw_add_table_algo(struct ip_fw_chain *ch, struct table_algo *ta, size_t size,
2626 struct tables_config *tcfg;
2627 struct table_algo *ta_new;
2630 if (size > sizeof(struct table_algo))
2633 /* Check for the required on-stack size for add/del */
2634 sz = roundup2(ta->ta_buf_size, sizeof(void *));
2638 KASSERT(ta->type <= IPFW_TABLE_MAXTYPE,("Increase IPFW_TABLE_MAXTYPE"));
2640 /* Copy algorithm data to stable storage. */
2641 ta_new = malloc(sizeof(struct table_algo), M_IPFW, M_WAITOK | M_ZERO);
2642 memcpy(ta_new, ta, size);
2644 tcfg = CHAIN_TO_TCFG(ch);
2646 KASSERT(tcfg->algo_count < 255, ("Increase algo array size"));
2648 tcfg->algo[++tcfg->algo_count] = ta_new;
2649 ta_new->idx = tcfg->algo_count;
2651 /* Set algorithm as default one for given type */
2652 if ((ta_new->flags & TA_FLAG_DEFAULT) != 0 &&
2653 tcfg->def_algo[ta_new->type] == NULL)
2654 tcfg->def_algo[ta_new->type] = ta_new;
2662 * Unregisters table algo using @idx as id.
2663 * XXX: It is NOT safe to call this function in any place
2664 * other than ipfw instance destroy handler.
2667 ipfw_del_table_algo(struct ip_fw_chain *ch, int idx)
2669 struct tables_config *tcfg;
2670 struct table_algo *ta;
2672 tcfg = CHAIN_TO_TCFG(ch);
2674 KASSERT(idx <= tcfg->algo_count, ("algo idx %d out of range 1..%d",
2675 idx, tcfg->algo_count));
2677 ta = tcfg->algo[idx];
2678 KASSERT(ta != NULL, ("algo idx %d is NULL", idx));
2680 if (tcfg->def_algo[ta->type] == ta)
2681 tcfg->def_algo[ta->type] = NULL;
2687 * Lists all table algorithms currently available.
2688 * Data layout (v0)(current):
2689 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
2690 * Reply: [ ipfw_obj_lheader ipfw_ta_info x N ]
2692 * Returns 0 on success
2695 list_table_algo(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2696 struct sockopt_data *sd)
2698 struct _ipfw_obj_lheader *olh;
2699 struct tables_config *tcfg;
2701 struct table_algo *ta;
2702 uint32_t count, n, size;
2704 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
2707 if (sd->valsize < olh->size)
2711 tcfg = CHAIN_TO_TCFG(ch);
2712 count = tcfg->algo_count;
2713 size = count * sizeof(ipfw_ta_info) + sizeof(ipfw_obj_lheader);
2715 /* Fill in header regadless of buffer size */
2717 olh->objsize = sizeof(ipfw_ta_info);
2719 if (size > olh->size) {
2721 IPFW_UH_RUNLOCK(ch);
2726 for (n = 1; n <= count; n++) {
2727 i = (ipfw_ta_info *)ipfw_get_sopt_space(sd, sizeof(*i));
2728 KASSERT(i != NULL, ("previously checked buffer is not enough"));
2730 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2732 i->refcnt = ta->refcnt;
2735 IPFW_UH_RUNLOCK(ch);
2741 classify_srcdst(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2743 /* Basic IPv4/IPv6 or u32 lookups */
2745 /* Assume ADDR by default */
2746 *ptype = IPFW_TABLE_ADDR;
2749 if (F_LEN(cmd) > F_INSN_SIZE(ipfw_insn_u32)) {
2751 * generic lookup. The key must be
2752 * in 32bit big-endian format.
2754 v = ((ipfw_insn_u32 *)cmd)->d[1];
2763 *ptype = IPFW_TABLE_NUMBER;
2767 *ptype = IPFW_TABLE_NUMBER;
2771 *ptype = IPFW_TABLE_NUMBER;
2775 *ptype = IPFW_TABLE_NUMBER;
2784 classify_via(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2786 ipfw_insn_if *cmdif;
2788 /* Interface table, possibly */
2789 cmdif = (ipfw_insn_if *)cmd;
2790 if (cmdif->name[0] != '\1')
2793 *ptype = IPFW_TABLE_INTERFACE;
2794 *puidx = cmdif->p.kidx;
2800 classify_flow(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2804 *ptype = IPFW_TABLE_FLOW;
2810 update_arg1(ipfw_insn *cmd, uint16_t idx)
2817 update_via(ipfw_insn *cmd, uint16_t idx)
2819 ipfw_insn_if *cmdif;
2821 cmdif = (ipfw_insn_if *)cmd;
2822 cmdif->p.kidx = idx;
2826 table_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
2827 struct named_object **pno)
2829 struct table_config *tc;
2832 IPFW_UH_WLOCK_ASSERT(ch);
2834 error = find_table_err(CHAIN_TO_NI(ch), ti, &tc);
2842 /* XXX: sets-sets! */
2843 static struct named_object *
2844 table_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
2846 struct namedobj_instance *ni;
2847 struct table_config *tc;
2849 IPFW_UH_WLOCK_ASSERT(ch);
2850 ni = CHAIN_TO_NI(ch);
2851 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, idx);
2852 KASSERT(tc != NULL, ("Table with index %d not found", idx));
2858 table_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2859 enum ipfw_sets_cmd cmd)
2867 * Always return success, the real action and decision
2868 * should make table_manage_sets_all().
2874 * NOTE: we need to use ipfw_objhash_del/ipfw_objhash_add
2875 * if set number will be used in hash function. Currently
2876 * we can just use generic handler that replaces set value.
2878 if (V_fw_tables_sets == 0)
2883 * Return EOPNOTSUPP for COUNT_ONE when per-set sysctl is
2884 * disabled. This allow skip table's opcodes from additional
2885 * checks when specific rules moved to another set.
2887 if (V_fw_tables_sets == 0)
2888 return (EOPNOTSUPP);
2890 /* Use generic sets handler when per-set sysctl is enabled. */
2891 return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2892 set, new_set, cmd));
2896 * We register several opcode rewriters for lookup tables.
2897 * All tables opcodes have the same ETLV type, but different subtype.
2898 * To avoid invoking sets handler several times for XXX_ALL commands,
2899 * we use separate manage_sets handler. O_RECV has the lowest value,
2900 * so it should be called first.
2903 table_manage_sets_all(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2904 enum ipfw_sets_cmd cmd)
2911 * Return success for TEST_ALL, since nothing prevents
2912 * move rules from one set to another. All tables are
2913 * accessible from all sets when per-set tables sysctl
2917 if (V_fw_tables_sets == 0)
2921 return (table_manage_sets(ch, set, new_set, cmd));
2923 /* Use generic sets handler when per-set sysctl is enabled. */
2924 return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2925 set, new_set, cmd));
2928 static struct opcode_obj_rewrite opcodes[] = {
2930 .opcode = O_IP_SRC_LOOKUP,
2931 .etlv = IPFW_TLV_TBL_NAME,
2932 .classifier = classify_srcdst,
2933 .update = update_arg1,
2934 .find_byname = table_findbyname,
2935 .find_bykidx = table_findbykidx,
2936 .create_object = create_table_compat,
2937 .manage_sets = table_manage_sets,
2940 .opcode = O_IP_DST_LOOKUP,
2941 .etlv = IPFW_TLV_TBL_NAME,
2942 .classifier = classify_srcdst,
2943 .update = update_arg1,
2944 .find_byname = table_findbyname,
2945 .find_bykidx = table_findbykidx,
2946 .create_object = create_table_compat,
2947 .manage_sets = table_manage_sets,
2950 .opcode = O_IP_FLOW_LOOKUP,
2951 .etlv = IPFW_TLV_TBL_NAME,
2952 .classifier = classify_flow,
2953 .update = update_arg1,
2954 .find_byname = table_findbyname,
2955 .find_bykidx = table_findbykidx,
2956 .create_object = create_table_compat,
2957 .manage_sets = table_manage_sets,
2961 .etlv = IPFW_TLV_TBL_NAME,
2962 .classifier = classify_via,
2963 .update = update_via,
2964 .find_byname = table_findbyname,
2965 .find_bykidx = table_findbykidx,
2966 .create_object = create_table_compat,
2967 .manage_sets = table_manage_sets,
2971 .etlv = IPFW_TLV_TBL_NAME,
2972 .classifier = classify_via,
2973 .update = update_via,
2974 .find_byname = table_findbyname,
2975 .find_bykidx = table_findbykidx,
2976 .create_object = create_table_compat,
2977 .manage_sets = table_manage_sets_all,
2981 .etlv = IPFW_TLV_TBL_NAME,
2982 .classifier = classify_via,
2983 .update = update_via,
2984 .find_byname = table_findbyname,
2985 .find_bykidx = table_findbykidx,
2986 .create_object = create_table_compat,
2987 .manage_sets = table_manage_sets,
2992 test_sets_cb(struct namedobj_instance *ni __unused, struct named_object *no,
2996 /* Check that there aren't any tables in not default set */
3003 * Switch between "set 0" and "rule's set" table binding,
3004 * Check all ruleset bindings and permits changing
3005 * IFF each binding has both rule AND table in default set (set 0).
3007 * Returns 0 on success.
3010 ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int sets)
3012 struct opcode_obj_rewrite *rw;
3013 struct namedobj_instance *ni;
3014 struct named_object *no;
3023 if (V_fw_tables_sets == sets) {
3024 IPFW_UH_WUNLOCK(ch);
3027 ni = CHAIN_TO_NI(ch);
3030 * Prevent disabling sets support if we have some tables
3031 * in not default sets.
3033 if (ipfw_objhash_foreach_type(ni, test_sets_cb,
3034 NULL, IPFW_TLV_TBL_NAME) != 0) {
3035 IPFW_UH_WUNLOCK(ch);
3040 * Scan all rules and examine tables opcodes.
3042 for (i = 0; i < ch->n_rules; i++) {
3048 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
3049 cmdlen = F_LEN(cmd);
3050 /* Check only tables opcodes */
3051 for (kidx = 0, rw = opcodes;
3052 rw < opcodes + nitems(opcodes); rw++) {
3053 if (rw->opcode != cmd->opcode)
3055 if (rw->classifier(cmd, &kidx, &subtype) == 0)
3060 no = ipfw_objhash_lookup_kidx(ni, kidx);
3061 /* Check if both table object and rule has the set 0 */
3062 if (no->set != 0 || rule->set != 0) {
3063 IPFW_UH_WUNLOCK(ch);
3068 V_fw_tables_sets = sets;
3069 IPFW_UH_WUNLOCK(ch);
3074 * Checks table name for validity.
3075 * Enforce basic length checks, the rest
3076 * should be done in userland.
3078 * Returns 0 if name is considered valid.
3081 check_table_name(const char *name)
3085 * TODO: do some more complicated checks
3087 return (ipfw_check_object_name_generic(name));
3091 * Finds table config based on either legacy index
3093 * Note @ti structure contains unchecked data from userland.
3095 * Returns 0 in success and fills in @tc with found config
3098 find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
3099 struct table_config **tc)
3101 char *name, bname[16];
3102 struct named_object *no;
3103 ipfw_obj_ntlv *ntlv;
3106 if (ti->tlvs != NULL) {
3107 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3114 * Use set provided by @ti instead of @ntlv one.
3115 * This is needed due to different sets behavior
3116 * controlled by V_fw_tables_sets.
3118 set = (V_fw_tables_sets != 0) ? ti->set : 0;
3120 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3125 no = ipfw_objhash_lookup_name(ni, set, name);
3126 *tc = (struct table_config *)no;
3132 * Finds table config based on either legacy index
3134 * Note @ti structure contains unchecked data from userland.
3136 * Returns pointer to table_config or NULL.
3138 static struct table_config *
3139 find_table(struct namedobj_instance *ni, struct tid_info *ti)
3141 struct table_config *tc;
3143 if (find_table_err(ni, ti, &tc) != 0)
3150 * Allocate new table config structure using
3151 * specified @algo and @aname.
3153 * Returns pointer to config or NULL.
3155 static struct table_config *
3156 alloc_table_config(struct ip_fw_chain *ch, struct tid_info *ti,
3157 struct table_algo *ta, char *aname, uint8_t tflags)
3159 char *name, bname[16];
3160 struct table_config *tc;
3162 ipfw_obj_ntlv *ntlv;
3165 if (ti->tlvs != NULL) {
3166 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3171 set = (V_fw_tables_sets == 0) ? 0 : ntlv->set;
3173 /* Compat part: convert number to string representation */
3174 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3179 tc = malloc(sizeof(struct table_config), M_IPFW, M_WAITOK | M_ZERO);
3180 tc->no.name = tc->tablename;
3181 tc->no.subtype = ta->type;
3183 tc->tflags = tflags;
3185 strlcpy(tc->tablename, name, sizeof(tc->tablename));
3186 /* Set "shared" value type by default */
3189 /* Preallocate data structures for new tables */
3190 error = ta->init(ch, &tc->astate, &tc->ti_copy, aname, tflags);
3200 * Destroys table state and config.
3203 free_table_config(struct namedobj_instance *ni, struct table_config *tc)
3206 KASSERT(tc->linked == 0, ("free() on linked config"));
3207 /* UH lock MUST NOT be held */
3210 * We're using ta without any locking/referencing.
3211 * TODO: fix this if we're going to use unloadable algos.
3213 tc->ta->destroy(tc->astate, &tc->ti_copy);
3218 * Links @tc to @chain table named instance.
3219 * Sets appropriate type/states in @chain table info.
3222 link_table(struct ip_fw_chain *ch, struct table_config *tc)
3224 struct namedobj_instance *ni;
3225 struct table_info *ti;
3228 IPFW_UH_WLOCK_ASSERT(ch);
3230 ni = CHAIN_TO_NI(ch);
3233 ipfw_objhash_add(ni, &tc->no);
3235 ti = KIDX_TO_TI(ch, kidx);
3238 /* Notify algo on real @ti address */
3239 if (tc->ta->change_ti != NULL)
3240 tc->ta->change_ti(tc->astate, ti);
3247 * Unlinks @tc from @chain table named instance.
3248 * Zeroes states in @chain and stores them in @tc.
3251 unlink_table(struct ip_fw_chain *ch, struct table_config *tc)
3253 struct namedobj_instance *ni;
3254 struct table_info *ti;
3257 IPFW_UH_WLOCK_ASSERT(ch);
3258 IPFW_WLOCK_ASSERT(ch);
3260 ni = CHAIN_TO_NI(ch);
3263 /* Clear state. @ti copy is already saved inside @tc */
3264 ipfw_objhash_del(ni, &tc->no);
3265 ti = KIDX_TO_TI(ch, kidx);
3266 memset(ti, 0, sizeof(struct table_info));
3270 /* Notify algo on real @ti address */
3271 if (tc->ta->change_ti != NULL)
3272 tc->ta->change_ti(tc->astate, NULL);
3275 static struct ipfw_sopt_handler scodes[] = {
3276 { IP_FW_TABLE_XCREATE, 0, HDIR_SET, create_table },
3277 { IP_FW_TABLE_XDESTROY, 0, HDIR_SET, flush_table_v0 },
3278 { IP_FW_TABLE_XFLUSH, 0, HDIR_SET, flush_table_v0 },
3279 { IP_FW_TABLE_XMODIFY, 0, HDIR_BOTH, modify_table },
3280 { IP_FW_TABLE_XINFO, 0, HDIR_GET, describe_table },
3281 { IP_FW_TABLES_XLIST, 0, HDIR_GET, list_tables },
3282 { IP_FW_TABLE_XLIST, 0, HDIR_GET, dump_table_v0 },
3283 { IP_FW_TABLE_XLIST, 1, HDIR_GET, dump_table_v1 },
3284 { IP_FW_TABLE_XADD, 0, HDIR_BOTH, manage_table_ent_v0 },
3285 { IP_FW_TABLE_XADD, 1, HDIR_BOTH, manage_table_ent_v1 },
3286 { IP_FW_TABLE_XDEL, 0, HDIR_BOTH, manage_table_ent_v0 },
3287 { IP_FW_TABLE_XDEL, 1, HDIR_BOTH, manage_table_ent_v1 },
3288 { IP_FW_TABLE_XFIND, 0, HDIR_GET, find_table_entry },
3289 { IP_FW_TABLE_XSWAP, 0, HDIR_SET, swap_table },
3290 { IP_FW_TABLES_ALIST, 0, HDIR_GET, list_table_algo },
3291 { IP_FW_TABLE_XGETSIZE, 0, HDIR_GET, get_table_size },
3295 destroy_table_locked(struct namedobj_instance *ni, struct named_object *no,
3299 unlink_table((struct ip_fw_chain *)arg, (struct table_config *)no);
3300 if (ipfw_objhash_free_idx(ni, no->kidx) != 0)
3301 printf("Error unlinking kidx %d from table %s\n",
3302 no->kidx, no->name);
3303 free_table_config(ni, (struct table_config *)no);
3308 * Shuts tables module down.
3311 ipfw_destroy_tables(struct ip_fw_chain *ch, int last)
3314 IPFW_DEL_SOPT_HANDLER(last, scodes);
3315 IPFW_DEL_OBJ_REWRITER(last, opcodes);
3317 /* Remove all tables from working set */
3320 ipfw_objhash_foreach(CHAIN_TO_NI(ch), destroy_table_locked, ch);
3322 IPFW_UH_WUNLOCK(ch);
3324 /* Free pointers itself */
3325 free(ch->tablestate, M_IPFW);
3327 ipfw_table_value_destroy(ch, last);
3328 ipfw_table_algo_destroy(ch);
3330 ipfw_objhash_destroy(CHAIN_TO_NI(ch));
3331 free(CHAIN_TO_TCFG(ch), M_IPFW);
3335 * Starts tables module.
3338 ipfw_init_tables(struct ip_fw_chain *ch, int first)
3340 struct tables_config *tcfg;
3342 /* Allocate pointers */
3343 ch->tablestate = malloc(V_fw_tables_max * sizeof(struct table_info),
3344 M_IPFW, M_WAITOK | M_ZERO);
3346 tcfg = malloc(sizeof(struct tables_config), M_IPFW, M_WAITOK | M_ZERO);
3347 tcfg->namehash = ipfw_objhash_create(V_fw_tables_max);
3350 ipfw_table_value_init(ch, first);
3351 ipfw_table_algo_init(ch);
3353 IPFW_ADD_OBJ_REWRITER(first, opcodes);
3354 IPFW_ADD_SOPT_HANDLER(first, scodes);