2 * Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
3 * Copyright (c) 2014 Yandex LLC
4 * Copyright (c) 2014 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Lookup table support for ipfw.
34 * This file contains handlers for all generic tables' operations:
35 * add/del/flush entries, list/dump tables etc..
37 * Table data modification is protected by both UH and runtime lock
38 * while reading configuration/data is protected by UH lock.
40 * Lookup algorithms for all table types are located in ip_fw_table_algo.c
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/rwlock.h>
51 #include <sys/rmlock.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/queue.h>
55 #include <net/if.h> /* ip_fw.h requires IFNAMSIZ */
57 #include <netinet/in.h>
58 #include <netinet/ip_var.h> /* struct ipfw_rule_ref */
59 #include <netinet/ip_fw.h>
61 #include <netpfil/ipfw/ip_fw_private.h>
62 #include <netpfil/ipfw/ip_fw_table.h>
65 * Table has the following `type` concepts:
67 * `no.type` represents lookup key type (addr, ifp, uid, etc..)
68 * vmask represents bitmask of table values which are present at the moment.
69 * Special IPFW_VTYPE_LEGACY ( (uint32_t)-1 ) represents old
70 * single-value-for-all approach.
73 struct named_object no;
74 uint8_t tflags; /* type flags */
75 uint8_t locked; /* 1 if locked from changes */
76 uint8_t linked; /* 1 if already linked */
77 uint8_t ochanged; /* used by set swapping */
78 uint8_t vshared; /* 1 if using shared value array */
80 uint32_t count; /* Number of records */
81 uint32_t limit; /* Max number of records */
82 uint32_t vmask; /* bitmask with supported values */
83 uint32_t ocount; /* used by set swapping */
84 uint64_t gencnt; /* generation count */
85 char tablename[64]; /* table name */
86 struct table_algo *ta; /* Callbacks for given algo */
87 void *astate; /* algorithm state */
88 struct table_info ti_copy; /* data to put to table_info */
89 struct namedobj_instance *vi;
92 static int find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
93 struct table_config **tc);
94 static struct table_config *find_table(struct namedobj_instance *ni,
96 static struct table_config *alloc_table_config(struct ip_fw_chain *ch,
97 struct tid_info *ti, struct table_algo *ta, char *adata, uint8_t tflags);
98 static void free_table_config(struct namedobj_instance *ni,
99 struct table_config *tc);
100 static int create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
101 char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int ref);
102 static void link_table(struct ip_fw_chain *ch, struct table_config *tc);
103 static void unlink_table(struct ip_fw_chain *ch, struct table_config *tc);
104 static int find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
105 struct tentry_info *tei, uint32_t count, int op, struct table_config **ptc);
108 static int export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
109 struct sockopt_data *sd);
110 static void export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
111 ipfw_xtable_info *i);
112 static int dump_table_tentry(void *e, void *arg);
113 static int dump_table_xentry(void *e, void *arg);
115 static int swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
118 static int check_table_name(const char *name);
119 static int check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
120 struct table_config *tc, struct table_info *ti, uint32_t count);
121 static int destroy_table(struct ip_fw_chain *ch, struct tid_info *ti);
123 static struct table_algo *find_table_algo(struct tables_config *tableconf,
124 struct tid_info *ti, char *name);
126 static void objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti);
127 static void ntlv_to_ti(struct _ipfw_obj_ntlv *ntlv, struct tid_info *ti);
129 #define CHAIN_TO_NI(chain) (CHAIN_TO_TCFG(chain)->namehash)
130 #define KIDX_TO_TI(ch, k) (&(((struct table_info *)(ch)->tablestate)[k]))
132 #define TA_BUF_SZ 128 /* On-stack buffer for add/delete state */
135 rollback_toperation_state(struct ip_fw_chain *ch, void *object)
137 struct tables_config *tcfg;
140 tcfg = CHAIN_TO_TCFG(ch);
141 TAILQ_FOREACH(os, &tcfg->state_list, next)
142 os->func(object, os);
146 add_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
148 struct tables_config *tcfg;
150 tcfg = CHAIN_TO_TCFG(ch);
151 TAILQ_INSERT_HEAD(&tcfg->state_list, &ts->opstate, next);
155 del_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
157 struct tables_config *tcfg;
159 tcfg = CHAIN_TO_TCFG(ch);
160 TAILQ_REMOVE(&tcfg->state_list, &ts->opstate, next);
164 tc_ref(struct table_config *tc)
171 tc_unref(struct table_config *tc)
177 static struct table_value *
178 get_table_value(struct ip_fw_chain *ch, struct table_config *tc, uint32_t kidx)
180 struct table_value *pval;
182 pval = (struct table_value *)ch->valuestate;
184 return (&pval[kidx]);
189 * Checks if we're able to insert/update entry @tei into table
191 * May alter @tei to indicate insertion error / insert
194 * Returns 0 if operation can be performed/
197 check_table_limit(struct table_config *tc, struct tentry_info *tei)
200 if (tc->limit == 0 || tc->count < tc->limit)
203 if ((tei->flags & TEI_FLAGS_UPDATE) == 0) {
204 /* Notify userland on error cause */
205 tei->flags |= TEI_FLAGS_LIMIT;
210 * We have UPDATE flag set.
211 * Permit updating record (if found),
212 * but restrict adding new one since we've
213 * already hit the limit.
215 tei->flags |= TEI_FLAGS_DONTADD;
221 * Convert algorithm callback return code into
222 * one of pre-defined states known by userland.
225 store_tei_result(struct tentry_info *tei, int op, int error, uint32_t num)
233 if (op == OP_ADD && num != 0)
234 flag = TEI_FLAGS_ADDED;
236 flag = TEI_FLAGS_DELETED;
239 flag = TEI_FLAGS_NOTFOUND;
242 flag = TEI_FLAGS_EXISTS;
245 flag = TEI_FLAGS_ERROR;
252 * Creates and references table with default parameters.
253 * Saves table config, algo and allocated kidx info @ptc, @pta and
254 * @pkidx if non-zero.
255 * Used for table auto-creation to support old binaries.
257 * Returns 0 on success.
260 create_table_compat(struct ip_fw_chain *ch, struct tid_info *ti,
266 memset(&xi, 0, sizeof(xi));
267 /* Set default value mask for legacy clients */
268 xi.vmask = IPFW_VTYPE_LEGACY;
270 error = create_table_internal(ch, ti, NULL, &xi, pkidx, 1);
278 * Find and reference existing table optionally
281 * Saves found table config into @ptc.
282 * Note function may drop/acquire UH_WLOCK.
283 * Returns 0 if table was found/created and referenced
284 * or non-zero return code.
287 find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
288 struct tentry_info *tei, uint32_t count, int op,
289 struct table_config **ptc)
291 struct namedobj_instance *ni;
292 struct table_config *tc;
296 IPFW_UH_WLOCK_ASSERT(ch);
298 ni = CHAIN_TO_NI(ch);
300 if ((tc = find_table(ni, ti)) != NULL) {
301 /* check table type */
302 if (tc->no.subtype != ti->type)
308 /* Try to exit early on limit hit */
309 if (op == OP_ADD && count == 1 &&
310 check_table_limit(tc, tei) != 0)
313 /* Reference and return */
322 /* Compatibility mode: create new table for old clients */
323 if ((tei->flags & TEI_FLAGS_COMPAT) == 0)
327 error = create_table_compat(ch, ti, &kidx);
333 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
334 KASSERT(tc != NULL, ("create_table_compat returned bad idx %d", kidx));
336 /* OK, now we've got referenced table. */
342 * Rolls back already @added to @tc entries using state array @ta_buf_m.
343 * Assume the following layout:
344 * 1) ADD state (ta_buf_m[0] ... t_buf_m[added - 1]) for handling update cases
345 * 2) DEL state (ta_buf_m[count[ ... t_buf_m[count + added - 1])
346 * for storing deleted state
349 rollback_added_entries(struct ip_fw_chain *ch, struct table_config *tc,
350 struct table_info *tinfo, struct tentry_info *tei, caddr_t ta_buf_m,
351 uint32_t count, uint32_t added)
353 struct table_algo *ta;
354 struct tentry_info *ptei;
360 IPFW_UH_WLOCK_ASSERT(ch);
363 ta_buf_sz = ta->ta_buf_size;
365 vv = v + count * ta_buf_sz;
366 for (i = 0; i < added; i++, v += ta_buf_sz, vv += ta_buf_sz) {
368 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 /* Sigle add/delete, use on-stack buffer */
409 memset(*ta_buf, 0, TA_BUF_SZ);
414 * Multiple adds/deletes, allocate larger buffer
416 * Note we need 2xcount buffer for add case:
417 * we have hold both ADD state
418 * and DELETE state (this may be needed
419 * if we need to rollback all changes)
421 sz = count * ta_buf_sz;
422 ta_buf_m = malloc((op == OP_ADD) ? sz * 2 : sz, M_TEMP,
427 for (i = 0; i < count; i++, v += ta_buf_sz) {
429 error = (op == OP_ADD) ?
430 ta->prepare_add(ch, ptei, v) : ta->prepare_del(ch, ptei, v);
433 * Some syntax error (incorrect mask, or address, or
434 * anything). Return error regardless of atomicity
446 * Flushes allocated state for each @count entries in @tei.
447 * Frees @ta_buf_m if differs from stack buffer @ta_buf.
450 flush_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
451 struct tentry_info *tei, uint32_t count, int rollback,
452 caddr_t ta_buf_m, caddr_t ta_buf)
455 struct tentry_info *ptei;
459 ta_buf_sz = ta->ta_buf_size;
461 /* Run cleaning callback anyway */
463 for (i = 0; i < count; i++, v += ta_buf_sz) {
465 ta->flush_entry(ch, ptei, v);
466 if (ptei->ptv != NULL) {
467 free(ptei->ptv, M_IPFW);
472 /* Clean up "deleted" state in case of rollback */
474 v = ta_buf_m + count * ta_buf_sz;
475 for (i = 0; i < count; i++, v += ta_buf_sz)
476 ta->flush_entry(ch, &tei[i], v);
479 if (ta_buf_m != ta_buf)
480 free(ta_buf_m, M_TEMP);
485 rollback_add_entry(void *object, struct op_state *_state)
487 struct ip_fw_chain *ch;
488 struct tableop_state *ts;
490 ts = (struct tableop_state *)_state;
492 if (ts->tc != object && ts->ch != object)
497 IPFW_UH_WLOCK_ASSERT(ch);
499 /* Call specifid unlockers */
500 rollback_table_values(ts);
502 /* Indicate we've called */
507 * Adds/updates one or more entries in table @ti.
509 * Function may drop/reacquire UH wlock multiple times due to
510 * items alloc, algorithm callbacks (check_space), value linkage
511 * (new values, value storage realloc), etc..
512 * Other processes like other adds (which may involve storage resize),
513 * table swaps (which changes table data and may change algo type),
514 * table modify (which may change value mask) may be executed
515 * simultaneously so we need to deal with it.
517 * The following approach was implemented:
518 * we have per-chain linked list, protected with UH lock.
519 * add_table_entry prepares special on-stack structure wthich is passed
520 * to its descendants. Users add this structure to this list before unlock.
521 * After performing needed operations and acquiring UH lock back, each user
522 * checks if structure has changed. If true, it rolls local state back and
523 * returns without error to the caller.
524 * add_table_entry() on its own checks if structure has changed and restarts
525 * its operation from the beginning (goto restart).
527 * Functions which are modifying fields of interest (currently
528 * resize_shared_value_storage() and swap_tables() )
529 * traverses given list while holding UH lock immediately before
530 * performing their operations calling function provided be list entry
531 * ( currently rollback_add_entry ) which performs rollback for all necessary
532 * state and sets appropriate values in structure indicating rollback
536 * Function references @ti first to ensure table won't
537 * disappear or change its type.
538 * After that, prepare_add callback is called for each @tei entry.
539 * Next, we try to add each entry under UH+WHLOCK
540 * using add() callback.
541 * Finally, we free all state by calling flush_entry callback
544 * Returns 0 on success.
547 add_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
548 struct tentry_info *tei, uint8_t flags, uint32_t count)
550 struct table_config *tc;
551 struct table_algo *ta;
553 int error, first_error, i, rollback;
554 uint32_t num, numadd;
555 struct tentry_info *ptei;
556 struct tableop_state ts;
557 char ta_buf[TA_BUF_SZ];
560 memset(&ts, 0, sizeof(ts));
565 * Find and reference existing table.
568 if (ts.modified != 0) {
570 flush_batch_buffer(ch, ta, tei, count, rollback,
572 memset(&ts, 0, sizeof(ts));
577 error = find_ref_table(ch, ti, tei, count, OP_ADD, &tc);
584 /* Fill in tablestate */
586 ts.opstate.func = rollback_add_entry;
588 ts.vshared = tc->vshared;
589 ts.vmask = tc->vmask;
594 add_toperation_state(ch, &ts);
597 /* Allocate memory and prepare record(s) */
598 /* Pass stack buffer by default */
600 error = prepare_batch_buffer(ch, ta, tei, count, OP_ADD, &ta_buf_m);
603 del_toperation_state(ch, &ts);
604 /* Drop reference we've used in first search */
607 /* Check prepare_batch_buffer() error */
612 * Check if table swap has happened.
613 * (so table algo might be changed).
614 * Restart operation to achieve consistent behavior.
616 if (ts.modified != 0)
620 * Link all values values to shared/per-table value array.
622 * May release/reacquire UH_WLOCK.
624 error = ipfw_link_table_values(ch, &ts);
627 if (ts.modified != 0)
631 * Ensure we are able to add all entries without additional
632 * memory allocations. May release/reacquire UH_WLOCK.
635 error = check_table_space(ch, &ts, tc, KIDX_TO_TI(ch, kidx), count);
638 if (ts.modified != 0)
641 /* We've got valid table in @tc. Let's try to add data */
650 for (i = 0; i < count; i++, v += ta->ta_buf_size) {
653 /* check limit before adding */
654 if ((error = check_table_limit(tc, ptei)) == 0) {
655 error = ta->add(tc->astate, KIDX_TO_TI(ch, kidx),
657 /* Set status flag to inform userland */
658 store_tei_result(ptei, OP_ADD, error, num);
661 /* Update number of records to ease limit checking */
667 if (first_error == 0)
671 * Some error have happened. Check our atomicity
672 * settings: continue if atomicity is not required,
673 * rollback changes otherwise.
675 if ((flags & IPFW_CTF_ATOMIC) == 0)
678 rollback_added_entries(ch, tc, KIDX_TO_TI(ch, kidx),
679 tei, ta_buf_m, count, i);
687 ipfw_garbage_table_values(ch, tc, tei, count, rollback);
689 /* Permit post-add algorithm grow/rehash. */
691 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
693 /* Return first error to user, if any */
699 flush_batch_buffer(ch, ta, tei, count, rollback, ta_buf_m, ta_buf);
705 * Deletes one or more entries in table @ti.
707 * Returns 0 on success.
710 del_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
711 struct tentry_info *tei, uint8_t flags, uint32_t count)
713 struct table_config *tc;
714 struct table_algo *ta;
715 struct tentry_info *ptei;
717 int error, first_error, i;
718 uint32_t num, numdel;
719 char ta_buf[TA_BUF_SZ];
723 * Find and reference existing table.
726 error = find_ref_table(ch, ti, tei, count, OP_DEL, &tc);
734 /* Allocate memory and prepare record(s) */
735 /* Pass stack buffer by default */
737 error = prepare_batch_buffer(ch, ta, tei, count, OP_DEL, &ta_buf_m);
743 /* Drop reference we've used in first search */
747 * Check if table algo is still the same.
748 * (changed ta may be the result of table swap).
762 for (i = 0; i < count; i++, v += ta->ta_buf_size) {
765 error = ta->del(tc->astate, KIDX_TO_TI(ch, kidx), ptei, v,
767 /* Save state for userland */
768 store_tei_result(ptei, OP_DEL, error, num);
769 if (error != 0 && first_error == 0)
776 /* Unlink non-used values */
777 ipfw_garbage_table_values(ch, tc, tei, count, 0);
780 /* Run post-del hook to permit shrinking */
781 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
786 /* Return first error to user, if any */
790 flush_batch_buffer(ch, ta, tei, count, 0, ta_buf_m, ta_buf);
796 * Ensure that table @tc has enough space to add @count entries without
797 * need for reallocation.
800 * 0) need_modify() (UH_WLOCK) - checks if @count items can be added w/o resize.
802 * 1) alloc_modify (no locks, M_WAITOK) - alloc new state based on @pflags.
803 * 2) prepare_modifyt (UH_WLOCK) - copy old data into new storage
804 * 3) modify (UH_WLOCK + WLOCK) - switch pointers
805 * 4) flush_modify (UH_WLOCK) - free state, if needed
807 * Returns 0 on success.
810 check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
811 struct table_config *tc, struct table_info *ti, uint32_t count)
813 struct table_algo *ta;
815 char ta_buf[TA_BUF_SZ];
818 IPFW_UH_WLOCK_ASSERT(ch);
822 if (ta->need_modify == NULL)
825 /* Acquire reference not to loose @tc between locks/unlocks */
829 * TODO: think about avoiding race between large add/large delete
830 * operation on algorithm which implements shrinking along with
835 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
840 /* We have to shrink/grow table */
842 add_toperation_state(ch, ts);
845 memset(&ta_buf, 0, sizeof(ta_buf));
846 error = ta->prepare_mod(ta_buf, &pflags);
850 del_toperation_state(ch, ts);
855 if (ts != NULL && ts->modified != 0) {
858 * Swap operation has happened
859 * so we're currently operating on other
860 * table data. Stop doing this.
862 ta->flush_mod(ta_buf);
866 /* Check if we still need to alter table */
867 ti = KIDX_TO_TI(ch, tc->no.kidx);
868 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
872 * Other thread has already performed resize.
873 * Flush our state and return.
875 ta->flush_mod(ta_buf);
879 error = ta->fill_mod(tc->astate, ti, ta_buf, &pflags);
881 /* Do actual modification */
883 ta->modify(tc->astate, ti, ta_buf, pflags);
887 /* Anyway, flush data and retry */
888 ta->flush_mod(ta_buf);
896 * Adds or deletes record in table.
898 * Request: [ ip_fw3_opheader ipfw_table_xentry ]
900 * Returns 0 on success
903 manage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
904 struct sockopt_data *sd)
906 ipfw_table_xentry *xent;
907 struct tentry_info tei;
909 struct table_value v;
910 int error, hdrlen, read;
912 hdrlen = offsetof(ipfw_table_xentry, k);
914 /* Check minimum header size */
915 if (sd->valsize < (sizeof(*op3) + hdrlen))
918 read = sizeof(ip_fw3_opheader);
920 /* Check if xentry len field is valid */
921 xent = (ipfw_table_xentry *)(op3 + 1);
922 if (xent->len < hdrlen || xent->len + read > sd->valsize)
925 memset(&tei, 0, sizeof(tei));
926 tei.paddr = &xent->k;
927 tei.masklen = xent->masklen;
928 ipfw_import_table_value_legacy(xent->value, &v);
930 /* Old requests compatibility */
931 tei.flags = TEI_FLAGS_COMPAT;
932 if (xent->type == IPFW_TABLE_ADDR) {
933 if (xent->len - hdrlen == sizeof(in_addr_t))
934 tei.subtype = AF_INET;
936 tei.subtype = AF_INET6;
939 memset(&ti, 0, sizeof(ti));
941 ti.type = xent->type;
943 error = (op3->opcode == IP_FW_TABLE_XADD) ?
944 add_table_entry(ch, &ti, &tei, 0, 1) :
945 del_table_entry(ch, &ti, &tei, 0, 1);
951 * Adds or deletes record in table.
952 * Data layout (v1)(current):
953 * Request: [ ipfw_obj_header
954 * ipfw_obj_ctlv(IPFW_TLV_TBLENT_LIST) [ ipfw_obj_tentry x N ]
957 * Returns 0 on success
960 manage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
961 struct sockopt_data *sd)
963 ipfw_obj_tentry *tent, *ptent;
966 struct tentry_info *ptei, tei, *tei_buf;
968 int error, i, kidx, read;
970 /* Check minimum header size */
971 if (sd->valsize < (sizeof(*oh) + sizeof(*ctlv)))
974 /* Check if passed data is too long */
975 if (sd->valsize != sd->kavail)
978 oh = (ipfw_obj_header *)sd->kbuf;
980 /* Basic length checks for TLVs */
981 if (oh->ntlv.head.length != sizeof(oh->ntlv))
986 ctlv = (ipfw_obj_ctlv *)(oh + 1);
987 if (ctlv->head.length + read != sd->valsize)
990 read += sizeof(*ctlv);
991 tent = (ipfw_obj_tentry *)(ctlv + 1);
992 if (ctlv->count * sizeof(*tent) + read != sd->valsize)
995 if (ctlv->count == 0)
999 * Mark entire buffer as "read".
1000 * This instructs sopt api write it back
1001 * after function return.
1003 ipfw_get_sopt_header(sd, sd->valsize);
1005 /* Perform basic checks for each entry */
1008 for (i = 0; i < ctlv->count; i++, ptent++) {
1009 if (ptent->head.length != sizeof(*ptent))
1011 if (ptent->idx != kidx)
1015 /* Convert data into kernel request objects */
1016 objheader_to_ti(oh, &ti);
1017 ti.type = oh->ntlv.type;
1020 /* Use on-stack buffer for single add/del */
1021 if (ctlv->count == 1) {
1022 memset(&tei, 0, sizeof(tei));
1025 tei_buf = malloc(ctlv->count * sizeof(tei), M_TEMP,
1030 for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1031 ptei->paddr = &ptent->k;
1032 ptei->subtype = ptent->subtype;
1033 ptei->masklen = ptent->masklen;
1034 if (ptent->head.flags & IPFW_TF_UPDATE)
1035 ptei->flags |= TEI_FLAGS_UPDATE;
1037 ipfw_import_table_value_v1(&ptent->v.value);
1038 ptei->pvalue = (struct table_value *)&ptent->v.value;
1041 error = (oh->opheader.opcode == IP_FW_TABLE_XADD) ?
1042 add_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count) :
1043 del_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count);
1045 /* Translate result back to userland */
1048 for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1049 if (ptei->flags & TEI_FLAGS_ADDED)
1050 ptent->result = IPFW_TR_ADDED;
1051 else if (ptei->flags & TEI_FLAGS_DELETED)
1052 ptent->result = IPFW_TR_DELETED;
1053 else if (ptei->flags & TEI_FLAGS_UPDATED)
1054 ptent->result = IPFW_TR_UPDATED;
1055 else if (ptei->flags & TEI_FLAGS_LIMIT)
1056 ptent->result = IPFW_TR_LIMIT;
1057 else if (ptei->flags & TEI_FLAGS_ERROR)
1058 ptent->result = IPFW_TR_ERROR;
1059 else if (ptei->flags & TEI_FLAGS_NOTFOUND)
1060 ptent->result = IPFW_TR_NOTFOUND;
1061 else if (ptei->flags & TEI_FLAGS_EXISTS)
1062 ptent->result = IPFW_TR_EXISTS;
1063 ipfw_export_table_value_v1(ptei->pvalue, &ptent->v.value);
1066 if (tei_buf != &tei)
1067 free(tei_buf, M_TEMP);
1073 * Looks up an entry in given table.
1074 * Data layout (v0)(current):
1075 * Request: [ ipfw_obj_header ipfw_obj_tentry ]
1076 * Reply: [ ipfw_obj_header ipfw_obj_tentry ]
1078 * Returns 0 on success
1081 find_table_entry(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1082 struct sockopt_data *sd)
1084 ipfw_obj_tentry *tent;
1085 ipfw_obj_header *oh;
1087 struct table_config *tc;
1088 struct table_algo *ta;
1089 struct table_info *kti;
1090 struct table_value *pval;
1091 struct namedobj_instance *ni;
1095 /* Check minimum header size */
1096 sz = sizeof(*oh) + sizeof(*tent);
1097 if (sd->valsize != sz)
1100 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1101 tent = (ipfw_obj_tentry *)(oh + 1);
1103 /* Basic length checks for TLVs */
1104 if (oh->ntlv.head.length != sizeof(oh->ntlv))
1107 objheader_to_ti(oh, &ti);
1108 ti.type = oh->ntlv.type;
1109 ti.uidx = tent->idx;
1112 ni = CHAIN_TO_NI(ch);
1115 * Find existing table and check its type .
1118 if ((tc = find_table(ni, &ti)) == NULL) {
1119 IPFW_UH_RUNLOCK(ch);
1123 /* check table type */
1124 if (tc->no.subtype != ti.type) {
1125 IPFW_UH_RUNLOCK(ch);
1129 kti = KIDX_TO_TI(ch, tc->no.kidx);
1132 if (ta->find_tentry == NULL)
1135 error = ta->find_tentry(tc->astate, kti, tent);
1137 pval = get_table_value(ch, tc, tent->v.kidx);
1138 ipfw_export_table_value_v1(pval, &tent->v.value);
1140 IPFW_UH_RUNLOCK(ch);
1146 * Flushes all entries or destroys given table.
1147 * Data layout (v0)(current):
1148 * Request: [ ipfw_obj_header ]
1150 * Returns 0 on success
1153 flush_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1154 struct sockopt_data *sd)
1157 struct _ipfw_obj_header *oh;
1160 if (sd->valsize != sizeof(*oh))
1163 oh = (struct _ipfw_obj_header *)op3;
1164 objheader_to_ti(oh, &ti);
1166 if (op3->opcode == IP_FW_TABLE_XDESTROY)
1167 error = destroy_table(ch, &ti);
1168 else if (op3->opcode == IP_FW_TABLE_XFLUSH)
1169 error = flush_table(ch, &ti);
1177 restart_flush(void *object, struct op_state *_state)
1179 struct tableop_state *ts;
1181 ts = (struct tableop_state *)_state;
1183 if (ts->tc != object)
1186 /* Indicate we've called */
1191 * Flushes given table.
1193 * Function create new table instance with the same
1194 * parameters, swaps it with old one and
1195 * flushes state without holding runtime WLOCK.
1197 * Returns 0 on success.
1200 flush_table(struct ip_fw_chain *ch, struct tid_info *ti)
1202 struct namedobj_instance *ni;
1203 struct table_config *tc;
1204 struct table_algo *ta;
1205 struct table_info ti_old, ti_new, *tablestate;
1206 void *astate_old, *astate_new;
1207 char algostate[64], *pstate;
1208 struct tableop_state ts;
1214 * Stage 1: save table algorithm.
1215 * Reference found table to ensure it won't disappear.
1218 ni = CHAIN_TO_NI(ch);
1219 if ((tc = find_table(ni, ti)) == NULL) {
1220 IPFW_UH_WUNLOCK(ch);
1225 memset(&ti_new, 0, sizeof(ti_new));
1227 /* Set up swap handler */
1228 memset(&ts, 0, sizeof(ts));
1229 ts.opstate.func = restart_flush;
1233 /* Do not flush readonly tables */
1234 if ((ta->flags & TA_FLAG_READONLY) != 0) {
1235 IPFW_UH_WUNLOCK(ch);
1238 /* Save startup algo parameters */
1239 if (ta->print_config != NULL) {
1240 ta->print_config(tc->astate, KIDX_TO_TI(ch, tc->no.kidx),
1241 algostate, sizeof(algostate));
1245 tflags = tc->tflags;
1247 add_toperation_state(ch, &ts);
1248 IPFW_UH_WUNLOCK(ch);
1251 * Stage 1.5: if this is not the first attempt, destroy previous state
1254 ta->destroy(astate_new, &ti_new);
1259 * Stage 2: allocate new table instance using same algo.
1261 memset(&ti_new, 0, sizeof(struct table_info));
1262 error = ta->init(ch, &astate_new, &ti_new, pstate, tflags);
1265 * Stage 3: swap old state pointers with newly-allocated ones.
1266 * Decrease refcount.
1270 del_toperation_state(ch, &ts);
1273 IPFW_UH_WUNLOCK(ch);
1278 * Restart operation if table swap has happened:
1279 * even if algo may be the same, algo init parameters
1280 * may change. Restart operation instead of doing
1283 if (ts.modified != 0) {
1284 /* Delay destroying data since we're holding UH lock */
1289 ni = CHAIN_TO_NI(ch);
1291 tablestate = (struct table_info *)ch->tablestate;
1294 ti_old = tablestate[kidx];
1295 tablestate[kidx] = ti_new;
1298 astate_old = tc->astate;
1299 tc->astate = astate_new;
1300 tc->ti_copy = ti_new;
1303 /* Notify algo on real @ti address */
1304 if (ta->change_ti != NULL)
1305 ta->change_ti(tc->astate, &tablestate[kidx]);
1308 * Stage 4: unref values.
1310 ipfw_unref_table_values(ch, tc, ta, astate_old, &ti_old);
1311 IPFW_UH_WUNLOCK(ch);
1314 * Stage 5: perform real flush/destroy.
1316 ta->destroy(astate_old, &ti_old);
1323 * Data layout (v0)(current):
1324 * Request: [ ipfw_obj_header ipfw_obj_ntlv ]
1326 * Returns 0 on success
1329 swap_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1330 struct sockopt_data *sd)
1333 struct _ipfw_obj_header *oh;
1334 struct tid_info ti_a, ti_b;
1336 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_obj_ntlv))
1339 oh = (struct _ipfw_obj_header *)op3;
1340 ntlv_to_ti(&oh->ntlv, &ti_a);
1341 ntlv_to_ti((ipfw_obj_ntlv *)(oh + 1), &ti_b);
1343 error = swap_tables(ch, &ti_a, &ti_b);
1349 * Swaps two tables of the same type/valtype.
1351 * Checks if tables are compatible and limits
1352 * permits swap, than actually perform swap.
1354 * Each table consists of 2 different parts:
1356 * @tc (with name, set, kidx) and rule bindings, which is "stable".
1360 * runtime data @ti (ch->tablestate)
1361 * runtime cache in @tc
1362 * algo-specific data (@tc->astate)
1369 * After that we call @ti change handler for each table.
1371 * Note that referencing @tc won't protect tc->ta from change.
1372 * XXX: Do we need to restrict swap between locked tables?
1373 * XXX: Do we need to exchange ftype?
1375 * Returns 0 on success.
1378 swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
1381 struct namedobj_instance *ni;
1382 struct table_config *tc_a, *tc_b;
1383 struct table_algo *ta;
1384 struct table_info ti, *tablestate;
1389 * Stage 1: find both tables and ensure they are of
1393 ni = CHAIN_TO_NI(ch);
1394 if ((tc_a = find_table(ni, a)) == NULL) {
1395 IPFW_UH_WUNLOCK(ch);
1398 if ((tc_b = find_table(ni, b)) == NULL) {
1399 IPFW_UH_WUNLOCK(ch);
1403 /* It is very easy to swap between the same table */
1405 IPFW_UH_WUNLOCK(ch);
1409 /* Check type and value are the same */
1410 if (tc_a->no.subtype!=tc_b->no.subtype || tc_a->tflags!=tc_b->tflags) {
1411 IPFW_UH_WUNLOCK(ch);
1415 /* Check limits before swap */
1416 if ((tc_a->limit != 0 && tc_b->count > tc_a->limit) ||
1417 (tc_b->limit != 0 && tc_a->count > tc_b->limit)) {
1418 IPFW_UH_WUNLOCK(ch);
1422 /* Check if one of the tables is readonly */
1423 if (((tc_a->ta->flags | tc_b->ta->flags) & TA_FLAG_READONLY) != 0) {
1424 IPFW_UH_WUNLOCK(ch);
1428 /* Notify we're going to swap */
1429 rollback_toperation_state(ch, tc_a);
1430 rollback_toperation_state(ch, tc_b);
1432 /* Everything is fine, prepare to swap */
1433 tablestate = (struct table_info *)ch->tablestate;
1434 ti = tablestate[tc_a->no.kidx];
1436 astate = tc_a->astate;
1437 count = tc_a->count;
1441 tablestate[tc_a->no.kidx] = tablestate[tc_b->no.kidx];
1442 tc_a->ta = tc_b->ta;
1443 tc_a->astate = tc_b->astate;
1444 tc_a->count = tc_b->count;
1446 tablestate[tc_b->no.kidx] = ti;
1448 tc_b->astate = astate;
1449 tc_b->count = count;
1452 /* Ensure tc.ti copies are in sync */
1453 tc_a->ti_copy = tablestate[tc_a->no.kidx];
1454 tc_b->ti_copy = tablestate[tc_b->no.kidx];
1456 /* Notify both tables on @ti change */
1457 if (tc_a->ta->change_ti != NULL)
1458 tc_a->ta->change_ti(tc_a->astate, &tablestate[tc_a->no.kidx]);
1459 if (tc_b->ta->change_ti != NULL)
1460 tc_b->ta->change_ti(tc_b->astate, &tablestate[tc_b->no.kidx]);
1462 IPFW_UH_WUNLOCK(ch);
1468 * Destroys table specified by @ti.
1469 * Data layout (v0)(current):
1470 * Request: [ ip_fw3_opheader ]
1472 * Returns 0 on success
1475 destroy_table(struct ip_fw_chain *ch, struct tid_info *ti)
1477 struct namedobj_instance *ni;
1478 struct table_config *tc;
1482 ni = CHAIN_TO_NI(ch);
1483 if ((tc = find_table(ni, ti)) == NULL) {
1484 IPFW_UH_WUNLOCK(ch);
1488 /* Do not permit destroying referenced tables */
1489 if (tc->no.refcnt > 0) {
1490 IPFW_UH_WUNLOCK(ch);
1495 unlink_table(ch, tc);
1498 /* Free obj index */
1499 if (ipfw_objhash_free_idx(ni, tc->no.kidx) != 0)
1500 printf("Error unlinking kidx %d from table %s\n",
1501 tc->no.kidx, tc->tablename);
1503 /* Unref values used in tables while holding UH lock */
1504 ipfw_unref_table_values(ch, tc, tc->ta, tc->astate, &tc->ti_copy);
1505 IPFW_UH_WUNLOCK(ch);
1507 free_table_config(ni, tc);
1513 roundup2p(uint32_t v)
1528 * Grow tables index.
1530 * Returns 0 on success.
1533 ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables)
1535 unsigned int ntables_old, tbl;
1536 struct namedobj_instance *ni;
1537 void *new_idx, *old_tablestate, *tablestate;
1538 struct table_info *ti;
1539 struct table_config *tc;
1542 /* Check new value for validity */
1545 if (ntables > IPFW_TABLES_MAX)
1546 ntables = IPFW_TABLES_MAX;
1547 /* Alight to nearest power of 2 */
1548 ntables = (unsigned int)roundup2p(ntables);
1550 /* Allocate new pointers */
1551 tablestate = malloc(ntables * sizeof(struct table_info),
1552 M_IPFW, M_WAITOK | M_ZERO);
1554 ipfw_objhash_bitmap_alloc(ntables, (void *)&new_idx, &new_blocks);
1558 tbl = (ntables >= V_fw_tables_max) ? V_fw_tables_max : ntables;
1559 ni = CHAIN_TO_NI(ch);
1561 /* Temporary restrict decreasing max_tables */
1562 if (ntables < V_fw_tables_max) {
1565 * FIXME: Check if we really can shrink
1567 IPFW_UH_WUNLOCK(ch);
1571 /* Copy table info/indices */
1572 memcpy(tablestate, ch->tablestate, sizeof(struct table_info) * tbl);
1573 ipfw_objhash_bitmap_merge(ni, &new_idx, &new_blocks);
1577 /* Change pointers */
1578 old_tablestate = ch->tablestate;
1579 ch->tablestate = tablestate;
1580 ipfw_objhash_bitmap_swap(ni, &new_idx, &new_blocks);
1582 ntables_old = V_fw_tables_max;
1583 V_fw_tables_max = ntables;
1587 /* Notify all consumers that their @ti pointer has changed */
1588 ti = (struct table_info *)ch->tablestate;
1589 for (i = 0; i < tbl; i++, ti++) {
1590 if (ti->lookup == NULL)
1592 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, i);
1593 if (tc == NULL || tc->ta->change_ti == NULL)
1596 tc->ta->change_ti(tc->astate, ti);
1599 IPFW_UH_WUNLOCK(ch);
1601 /* Free old pointers */
1602 free(old_tablestate, M_IPFW);
1603 ipfw_objhash_bitmap_free(new_idx, new_blocks);
1609 * Lookup table's named object by its @kidx.
1611 struct named_object *
1612 ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch, uint16_t kidx)
1615 return (ipfw_objhash_lookup_kidx(CHAIN_TO_NI(ch), kidx));
1619 * Take reference to table specified in @ntlv.
1620 * On success return its @kidx.
1623 ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx)
1626 struct table_config *tc;
1629 IPFW_UH_WLOCK_ASSERT(ch);
1631 ntlv_to_ti(ntlv, &ti);
1632 error = find_table_err(CHAIN_TO_NI(ch), &ti, &tc);
1640 *kidx = tc->no.kidx;
1646 ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx)
1649 struct namedobj_instance *ni;
1650 struct named_object *no;
1652 IPFW_UH_WLOCK_ASSERT(ch);
1653 ni = CHAIN_TO_NI(ch);
1654 no = ipfw_objhash_lookup_kidx(ni, kidx);
1655 KASSERT(no != NULL, ("Table with index %d not found", kidx));
1660 * Lookup an IP @addr in table @tbl.
1661 * Stores found value in @val.
1663 * Returns 1 if @addr was found.
1666 ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
1669 struct table_info *ti;
1671 ti = KIDX_TO_TI(ch, tbl);
1673 return (ti->lookup(ti, &addr, sizeof(in_addr_t), val));
1677 * Lookup an arbtrary key @paddr of legth @plen in table @tbl.
1678 * Stores found value in @val.
1680 * Returns 1 if key was found.
1683 ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
1684 void *paddr, uint32_t *val)
1686 struct table_info *ti;
1688 ti = KIDX_TO_TI(ch, tbl);
1690 return (ti->lookup(ti, paddr, plen, val));
1694 * Info/List/dump support for tables.
1699 * High-level 'get' cmds sysctl handlers
1703 * Lists all tables currently available in kernel.
1704 * Data layout (v0)(current):
1705 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
1706 * Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
1708 * Returns 0 on success
1711 list_tables(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1712 struct sockopt_data *sd)
1714 struct _ipfw_obj_lheader *olh;
1717 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
1720 if (sd->valsize < olh->size)
1724 error = export_tables(ch, olh, sd);
1725 IPFW_UH_RUNLOCK(ch);
1731 * Store table info to buffer provided by @sd.
1732 * Data layout (v0)(current):
1733 * Request: [ ipfw_obj_header ipfw_xtable_info(empty)]
1734 * Reply: [ ipfw_obj_header ipfw_xtable_info ]
1736 * Returns 0 on success.
1739 describe_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1740 struct sockopt_data *sd)
1742 struct _ipfw_obj_header *oh;
1743 struct table_config *tc;
1747 sz = sizeof(*oh) + sizeof(ipfw_xtable_info);
1748 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1752 objheader_to_ti(oh, &ti);
1755 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
1756 IPFW_UH_RUNLOCK(ch);
1760 export_table_info(ch, tc, (ipfw_xtable_info *)(oh + 1));
1761 IPFW_UH_RUNLOCK(ch);
1767 * Modifies existing table.
1768 * Data layout (v0)(current):
1769 * Request: [ ipfw_obj_header ipfw_xtable_info ]
1771 * Returns 0 on success
1774 modify_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1775 struct sockopt_data *sd)
1777 struct _ipfw_obj_header *oh;
1778 ipfw_xtable_info *i;
1781 struct namedobj_instance *ni;
1782 struct table_config *tc;
1784 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1787 oh = (struct _ipfw_obj_header *)sd->kbuf;
1788 i = (ipfw_xtable_info *)(oh + 1);
1791 * Verify user-supplied strings.
1792 * Check for null-terminated/zero-length strings/
1794 tname = oh->ntlv.name;
1795 if (check_table_name(tname) != 0)
1798 objheader_to_ti(oh, &ti);
1802 ni = CHAIN_TO_NI(ch);
1803 if ((tc = find_table(ni, &ti)) == NULL) {
1804 IPFW_UH_WUNLOCK(ch);
1808 /* Do not support any modifications for readonly tables */
1809 if ((tc->ta->flags & TA_FLAG_READONLY) != 0) {
1810 IPFW_UH_WUNLOCK(ch);
1814 if ((i->mflags & IPFW_TMFLAGS_LIMIT) != 0)
1815 tc->limit = i->limit;
1816 if ((i->mflags & IPFW_TMFLAGS_LOCK) != 0)
1817 tc->locked = ((i->flags & IPFW_TGFLAGS_LOCKED) != 0);
1818 IPFW_UH_WUNLOCK(ch);
1824 * Creates new table.
1825 * Data layout (v0)(current):
1826 * Request: [ ipfw_obj_header ipfw_xtable_info ]
1828 * Returns 0 on success
1831 create_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1832 struct sockopt_data *sd)
1834 struct _ipfw_obj_header *oh;
1835 ipfw_xtable_info *i;
1836 char *tname, *aname;
1838 struct namedobj_instance *ni;
1840 if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1843 oh = (struct _ipfw_obj_header *)sd->kbuf;
1844 i = (ipfw_xtable_info *)(oh + 1);
1847 * Verify user-supplied strings.
1848 * Check for null-terminated/zero-length strings/
1850 tname = oh->ntlv.name;
1851 aname = i->algoname;
1852 if (check_table_name(tname) != 0 ||
1853 strnlen(aname, sizeof(i->algoname)) == sizeof(i->algoname))
1856 if (aname[0] == '\0') {
1857 /* Use default algorithm */
1861 objheader_to_ti(oh, &ti);
1864 ni = CHAIN_TO_NI(ch);
1867 if (find_table(ni, &ti) != NULL) {
1868 IPFW_UH_RUNLOCK(ch);
1871 IPFW_UH_RUNLOCK(ch);
1873 return (create_table_internal(ch, &ti, aname, i, NULL, 0));
1877 * Creates new table based on @ti and @aname.
1879 * Assume @aname to be checked and valid.
1880 * Stores allocated table kidx inside @pkidx (if non-NULL).
1881 * Reference created table if @compat is non-zero.
1883 * Returns 0 on success.
1886 create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
1887 char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int compat)
1889 struct namedobj_instance *ni;
1890 struct table_config *tc, *tc_new, *tmp;
1891 struct table_algo *ta;
1894 ni = CHAIN_TO_NI(ch);
1896 ta = find_table_algo(CHAIN_TO_TCFG(ch), ti, aname);
1900 tc = alloc_table_config(ch, ti, ta, aname, i->tflags);
1904 tc->vmask = i->vmask;
1905 tc->limit = i->limit;
1906 if (ta->flags & TA_FLAG_READONLY)
1909 tc->locked = (i->flags & IPFW_TGFLAGS_LOCKED) != 0;
1913 /* Check if table has been already created */
1914 tc_new = find_table(ni, ti);
1915 if (tc_new != NULL) {
1918 * Compat: do not fail if we're
1919 * requesting to create existing table
1920 * which has the same type
1922 if (compat == 0 || tc_new->no.subtype != tc->no.subtype) {
1923 IPFW_UH_WUNLOCK(ch);
1924 free_table_config(ni, tc);
1928 /* Exchange tc and tc_new for proper refcounting & freeing */
1934 if (ipfw_objhash_alloc_idx(ni, &kidx) != 0) {
1935 IPFW_UH_WUNLOCK(ch);
1936 printf("Unable to allocate table index."
1937 " Consider increasing net.inet.ip.fw.tables_max");
1938 free_table_config(ni, tc);
1942 tc->no.etlv = IPFW_TLV_TBL_NAME;
1952 *pkidx = tc->no.kidx;
1954 IPFW_UH_WUNLOCK(ch);
1957 free_table_config(ni, tc_new);
1963 ntlv_to_ti(ipfw_obj_ntlv *ntlv, struct tid_info *ti)
1966 memset(ti, 0, sizeof(struct tid_info));
1967 ti->set = ntlv->set;
1968 ti->uidx = ntlv->idx;
1970 ti->tlen = ntlv->head.length;
1974 objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti)
1977 ntlv_to_ti(&oh->ntlv, ti);
1980 struct namedobj_instance *
1981 ipfw_get_table_objhash(struct ip_fw_chain *ch)
1984 return (CHAIN_TO_NI(ch));
1988 * Exports basic table info as name TLV.
1989 * Used inside dump_static_rules() to provide info
1990 * about all tables referenced by current ruleset.
1992 * Returns 0 on success.
1995 ipfw_export_table_ntlv(struct ip_fw_chain *ch, uint16_t kidx,
1996 struct sockopt_data *sd)
1998 struct namedobj_instance *ni;
1999 struct named_object *no;
2000 ipfw_obj_ntlv *ntlv;
2002 ni = CHAIN_TO_NI(ch);
2004 no = ipfw_objhash_lookup_kidx(ni, kidx);
2005 KASSERT(no != NULL, ("invalid table kidx passed"));
2007 ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
2011 ntlv->head.type = IPFW_TLV_TBL_NAME;
2012 ntlv->head.length = sizeof(*ntlv);
2013 ntlv->idx = no->kidx;
2014 strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
2020 struct ip_fw_chain *ch;
2021 struct table_info *ti;
2022 struct table_config *tc;
2023 struct sockopt_data *sd;
2028 ipfw_table_entry *ent;
2031 ipfw_obj_tentry tent;
2035 count_ext_entries(void *e, void *arg)
2037 struct dump_args *da;
2039 da = (struct dump_args *)arg;
2046 * Gets number of items from table either using
2047 * internal counter or calling algo callback for
2048 * externally-managed tables.
2050 * Returns number of records.
2053 table_get_count(struct ip_fw_chain *ch, struct table_config *tc)
2055 struct table_info *ti;
2056 struct table_algo *ta;
2057 struct dump_args da;
2059 ti = KIDX_TO_TI(ch, tc->no.kidx);
2062 /* Use internal counter for self-managed tables */
2063 if ((ta->flags & TA_FLAG_READONLY) == 0)
2066 /* Use callback to quickly get number of items */
2067 if ((ta->flags & TA_FLAG_EXTCOUNTER) != 0)
2068 return (ta->get_count(tc->astate, ti));
2070 /* Count number of iterms ourselves */
2071 memset(&da, 0, sizeof(da));
2072 ta->foreach(tc->astate, ti, count_ext_entries, &da);
2078 * Exports table @tc info into standard ipfw_xtable_info format.
2081 export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
2082 ipfw_xtable_info *i)
2084 struct table_info *ti;
2085 struct table_algo *ta;
2087 i->type = tc->no.subtype;
2088 i->tflags = tc->tflags;
2089 i->vmask = tc->vmask;
2090 i->set = tc->no.set;
2091 i->kidx = tc->no.kidx;
2092 i->refcnt = tc->no.refcnt;
2093 i->count = table_get_count(ch, tc);
2094 i->limit = tc->limit;
2095 i->flags |= (tc->locked != 0) ? IPFW_TGFLAGS_LOCKED : 0;
2096 i->size = i->count * sizeof(ipfw_obj_tentry);
2097 i->size += sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2098 strlcpy(i->tablename, tc->tablename, sizeof(i->tablename));
2099 ti = KIDX_TO_TI(ch, tc->no.kidx);
2101 if (ta->print_config != NULL) {
2102 /* Use algo function to print table config to string */
2103 ta->print_config(tc->astate, ti, i->algoname,
2104 sizeof(i->algoname));
2106 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2107 /* Dump algo-specific data, if possible */
2108 if (ta->dump_tinfo != NULL) {
2109 ta->dump_tinfo(tc->astate, ti, &i->ta_info);
2110 i->ta_info.flags |= IPFW_TATFLAGS_DATA;
2114 struct dump_table_args {
2115 struct ip_fw_chain *ch;
2116 struct sockopt_data *sd;
2120 export_table_internal(struct namedobj_instance *ni, struct named_object *no,
2123 ipfw_xtable_info *i;
2124 struct dump_table_args *dta;
2126 dta = (struct dump_table_args *)arg;
2128 i = (ipfw_xtable_info *)ipfw_get_sopt_space(dta->sd, sizeof(*i));
2129 KASSERT(i != NULL, ("previously checked buffer is not enough"));
2131 export_table_info(dta->ch, (struct table_config *)no, i);
2136 * Export all tables as ipfw_xtable_info structures to
2137 * storage provided by @sd.
2139 * If supplied buffer is too small, fills in required size
2140 * and returns ENOMEM.
2141 * Returns 0 on success.
2144 export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
2145 struct sockopt_data *sd)
2149 struct dump_table_args dta;
2151 count = ipfw_objhash_count(CHAIN_TO_NI(ch));
2152 size = count * sizeof(ipfw_xtable_info) + sizeof(ipfw_obj_lheader);
2154 /* Fill in header regadless of buffer size */
2156 olh->objsize = sizeof(ipfw_xtable_info);
2158 if (size > olh->size) {
2168 ipfw_objhash_foreach(CHAIN_TO_NI(ch), export_table_internal, &dta);
2174 * Dumps all table data
2175 * Data layout (v1)(current):
2176 * Request: [ ipfw_obj_header ], size = ipfw_xtable_info.size
2177 * Reply: [ ipfw_obj_header ipfw_xtable_info ipfw_obj_tentry x N ]
2179 * Returns 0 on success
2182 dump_table_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2183 struct sockopt_data *sd)
2185 struct _ipfw_obj_header *oh;
2186 ipfw_xtable_info *i;
2188 struct table_config *tc;
2189 struct table_algo *ta;
2190 struct dump_args da;
2193 sz = sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2194 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
2198 i = (ipfw_xtable_info *)(oh + 1);
2199 objheader_to_ti(oh, &ti);
2202 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2203 IPFW_UH_RUNLOCK(ch);
2206 export_table_info(ch, tc, i);
2208 if (sd->valsize < i->size) {
2211 * Submitted buffer size is not enough.
2212 * WE've already filled in @i structure with
2213 * relevant table info including size, so we
2214 * can return. Buffer will be flushed automatically.
2216 IPFW_UH_RUNLOCK(ch);
2221 * Do the actual dump in eXtended format
2223 memset(&da, 0, sizeof(da));
2225 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2231 ta->foreach(tc->astate, da.ti, dump_table_tentry, &da);
2232 IPFW_UH_RUNLOCK(ch);
2238 * Dumps all table data
2239 * Data layout (version 0)(legacy):
2240 * Request: [ ipfw_xtable ], size = IP_FW_TABLE_XGETSIZE()
2241 * Reply: [ ipfw_xtable ipfw_table_xentry x N ]
2243 * Returns 0 on success
2246 dump_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2247 struct sockopt_data *sd)
2251 struct table_config *tc;
2252 struct table_algo *ta;
2253 struct dump_args da;
2256 xtbl = (ipfw_xtable *)ipfw_get_sopt_header(sd, sizeof(ipfw_xtable));
2260 memset(&ti, 0, sizeof(ti));
2261 ti.uidx = xtbl->tbl;
2264 if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2265 IPFW_UH_RUNLOCK(ch);
2268 count = table_get_count(ch, tc);
2269 sz = count * sizeof(ipfw_table_xentry) + sizeof(ipfw_xtable);
2273 xtbl->type = tc->no.subtype;
2274 xtbl->tbl = ti.uidx;
2276 if (sd->valsize < sz) {
2279 * Submitted buffer size is not enough.
2280 * WE've already filled in @i structure with
2281 * relevant table info including size, so we
2282 * can return. Buffer will be flushed automatically.
2284 IPFW_UH_RUNLOCK(ch);
2288 /* Do the actual dump in eXtended format */
2289 memset(&da, 0, sizeof(da));
2291 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2297 ta->foreach(tc->astate, da.ti, dump_table_xentry, &da);
2298 IPFW_UH_RUNLOCK(ch);
2304 * Legacy function to retrieve number of items in table.
2307 get_table_size(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2308 struct sockopt_data *sd)
2315 sz = sizeof(*op3) + sizeof(uint32_t);
2316 op3 = (ip_fw3_opheader *)ipfw_get_sopt_header(sd, sz);
2320 tbl = (uint32_t *)(op3 + 1);
2321 memset(&ti, 0, sizeof(ti));
2324 error = ipfw_count_xtable(ch, &ti, tbl);
2325 IPFW_UH_RUNLOCK(ch);
2330 * Legacy IP_FW_TABLE_GETSIZE handler
2333 ipfw_count_table(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2335 struct table_config *tc;
2337 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2339 *cnt = table_get_count(ch, tc);
2344 * Legacy IP_FW_TABLE_XGETSIZE handler
2347 ipfw_count_xtable(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2349 struct table_config *tc;
2352 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL) {
2354 return (0); /* 'table all list' requires success */
2357 count = table_get_count(ch, tc);
2358 *cnt = count * sizeof(ipfw_table_xentry);
2360 *cnt += sizeof(ipfw_xtable);
2365 dump_table_entry(void *e, void *arg)
2367 struct dump_args *da;
2368 struct table_config *tc;
2369 struct table_algo *ta;
2370 ipfw_table_entry *ent;
2371 struct table_value *pval;
2374 da = (struct dump_args *)arg;
2379 /* Out of memory, returning */
2380 if (da->cnt == da->size)
2383 ent->tbl = da->uidx;
2386 error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2390 ent->addr = da->tent.k.addr.s_addr;
2391 ent->masklen = da->tent.masklen;
2392 pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2393 ent->value = ipfw_export_table_value_legacy(pval);
2399 * Dumps table in pre-8.1 legacy format.
2402 ipfw_dump_table_legacy(struct ip_fw_chain *ch, struct tid_info *ti,
2405 struct table_config *tc;
2406 struct table_algo *ta;
2407 struct dump_args da;
2411 if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2412 return (0); /* XXX: We should return ESRCH */
2416 /* This dump format supports IPv4 only */
2417 if (tc->no.subtype != IPFW_TABLE_ADDR)
2420 memset(&da, 0, sizeof(da));
2422 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2424 da.ent = &tbl->ent[0];
2425 da.size = tbl->size;
2428 ta->foreach(tc->astate, da.ti, dump_table_entry, &da);
2435 * Dumps table entry in eXtended format (v1)(current).
2438 dump_table_tentry(void *e, void *arg)
2440 struct dump_args *da;
2441 struct table_config *tc;
2442 struct table_algo *ta;
2443 struct table_value *pval;
2444 ipfw_obj_tentry *tent;
2447 da = (struct dump_args *)arg;
2452 tent = (ipfw_obj_tentry *)ipfw_get_sopt_space(da->sd, sizeof(*tent));
2453 /* Out of memory, returning */
2458 tent->head.length = sizeof(ipfw_obj_tentry);
2459 tent->idx = da->uidx;
2461 error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2465 pval = get_table_value(da->ch, da->tc, tent->v.kidx);
2466 ipfw_export_table_value_v1(pval, &tent->v.value);
2472 * Dumps table entry in eXtended format (v0).
2475 dump_table_xentry(void *e, void *arg)
2477 struct dump_args *da;
2478 struct table_config *tc;
2479 struct table_algo *ta;
2480 ipfw_table_xentry *xent;
2481 ipfw_obj_tentry *tent;
2482 struct table_value *pval;
2485 da = (struct dump_args *)arg;
2490 xent = (ipfw_table_xentry *)ipfw_get_sopt_space(da->sd, sizeof(*xent));
2491 /* Out of memory, returning */
2494 xent->len = sizeof(ipfw_table_xentry);
2495 xent->tbl = da->uidx;
2497 memset(&da->tent, 0, sizeof(da->tent));
2499 error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2503 /* Convert current format to previous one */
2504 xent->masklen = tent->masklen;
2505 pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2506 xent->value = ipfw_export_table_value_legacy(pval);
2507 /* Apply some hacks */
2508 if (tc->no.subtype == IPFW_TABLE_ADDR && tent->subtype == AF_INET) {
2509 xent->k.addr6.s6_addr32[3] = tent->k.addr.s_addr;
2510 xent->flags = IPFW_TCF_INET;
2512 memcpy(&xent->k, &tent->k, sizeof(xent->k));
2518 * Helper function to export table algo data
2519 * to tentry format before calling user function.
2521 * Returns 0 on success.
2524 prepare_table_tentry(void *e, void *arg)
2526 struct dump_args *da;
2527 struct table_config *tc;
2528 struct table_algo *ta;
2531 da = (struct dump_args *)arg;
2536 error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2540 da->f(&da->tent, da->farg);
2546 * Allow external consumers to read table entries in standard format.
2549 ipfw_foreach_table_tentry(struct ip_fw_chain *ch, uint16_t kidx,
2550 ta_foreach_f *f, void *arg)
2552 struct namedobj_instance *ni;
2553 struct table_config *tc;
2554 struct table_algo *ta;
2555 struct dump_args da;
2557 ni = CHAIN_TO_NI(ch);
2559 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
2565 memset(&da, 0, sizeof(da));
2567 da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2572 ta->foreach(tc->astate, da.ti, prepare_table_tentry, &da);
2582 * Finds algorithm by index, table type or supplied name.
2584 * Returns pointer to algo or NULL.
2586 static struct table_algo *
2587 find_table_algo(struct tables_config *tcfg, struct tid_info *ti, char *name)
2590 struct table_algo *ta;
2592 if (ti->type > IPFW_TABLE_MAXTYPE)
2595 /* Search by index */
2596 if (ti->atype != 0) {
2597 if (ti->atype > tcfg->algo_count)
2599 return (tcfg->algo[ti->atype]);
2603 /* Return default algorithm for given type if set */
2604 return (tcfg->def_algo[ti->type]);
2607 /* Search by name */
2608 /* TODO: better search */
2609 for (i = 1; i <= tcfg->algo_count; i++) {
2613 * One can supply additional algorithm
2614 * parameters so we compare only the first word
2616 * 'addr:chash hsize=32'
2620 l = strlen(ta->name);
2621 if (strncmp(name, ta->name, l) != 0)
2623 if (name[l] != '\0' && name[l] != ' ')
2625 /* Check if we're requesting proper table type */
2626 if (ti->type != 0 && ti->type != ta->type)
2635 * Register new table algo @ta.
2636 * Stores algo id inside @idx.
2638 * Returns 0 on success.
2641 ipfw_add_table_algo(struct ip_fw_chain *ch, struct table_algo *ta, size_t size,
2644 struct tables_config *tcfg;
2645 struct table_algo *ta_new;
2648 if (size > sizeof(struct table_algo))
2651 /* Check for the required on-stack size for add/del */
2652 sz = roundup2(ta->ta_buf_size, sizeof(void *));
2656 KASSERT(ta->type <= IPFW_TABLE_MAXTYPE,("Increase IPFW_TABLE_MAXTYPE"));
2658 /* Copy algorithm data to stable storage. */
2659 ta_new = malloc(sizeof(struct table_algo), M_IPFW, M_WAITOK | M_ZERO);
2660 memcpy(ta_new, ta, size);
2662 tcfg = CHAIN_TO_TCFG(ch);
2664 KASSERT(tcfg->algo_count < 255, ("Increase algo array size"));
2666 tcfg->algo[++tcfg->algo_count] = ta_new;
2667 ta_new->idx = tcfg->algo_count;
2669 /* Set algorithm as default one for given type */
2670 if ((ta_new->flags & TA_FLAG_DEFAULT) != 0 &&
2671 tcfg->def_algo[ta_new->type] == NULL)
2672 tcfg->def_algo[ta_new->type] = ta_new;
2680 * Unregisters table algo using @idx as id.
2681 * XXX: It is NOT safe to call this function in any place
2682 * other than ipfw instance destroy handler.
2685 ipfw_del_table_algo(struct ip_fw_chain *ch, int idx)
2687 struct tables_config *tcfg;
2688 struct table_algo *ta;
2690 tcfg = CHAIN_TO_TCFG(ch);
2692 KASSERT(idx <= tcfg->algo_count, ("algo idx %d out of range 1..%d",
2693 idx, tcfg->algo_count));
2695 ta = tcfg->algo[idx];
2696 KASSERT(ta != NULL, ("algo idx %d is NULL", idx));
2698 if (tcfg->def_algo[ta->type] == ta)
2699 tcfg->def_algo[ta->type] = NULL;
2705 * Lists all table algorithms currently available.
2706 * Data layout (v0)(current):
2707 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
2708 * Reply: [ ipfw_obj_lheader ipfw_ta_info x N ]
2710 * Returns 0 on success
2713 list_table_algo(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2714 struct sockopt_data *sd)
2716 struct _ipfw_obj_lheader *olh;
2717 struct tables_config *tcfg;
2719 struct table_algo *ta;
2720 uint32_t count, n, size;
2722 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
2725 if (sd->valsize < olh->size)
2729 tcfg = CHAIN_TO_TCFG(ch);
2730 count = tcfg->algo_count;
2731 size = count * sizeof(ipfw_ta_info) + sizeof(ipfw_obj_lheader);
2733 /* Fill in header regadless of buffer size */
2735 olh->objsize = sizeof(ipfw_ta_info);
2737 if (size > olh->size) {
2739 IPFW_UH_RUNLOCK(ch);
2744 for (n = 1; n <= count; n++) {
2745 i = (ipfw_ta_info *)ipfw_get_sopt_space(sd, sizeof(*i));
2746 KASSERT(i != NULL, ("previously checked buffer is not enough"));
2748 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2750 i->refcnt = ta->refcnt;
2753 IPFW_UH_RUNLOCK(ch);
2759 classify_srcdst(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2761 /* Basic IPv4/IPv6 or u32 lookups */
2763 /* Assume ADDR by default */
2764 *ptype = IPFW_TABLE_ADDR;
2767 if (F_LEN(cmd) > F_INSN_SIZE(ipfw_insn_u32)) {
2769 * generic lookup. The key must be
2770 * in 32bit big-endian format.
2772 v = ((ipfw_insn_u32 *)cmd)->d[1];
2781 *ptype = IPFW_TABLE_NUMBER;
2785 *ptype = IPFW_TABLE_NUMBER;
2789 *ptype = IPFW_TABLE_NUMBER;
2793 *ptype = IPFW_TABLE_NUMBER;
2802 classify_via(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2804 ipfw_insn_if *cmdif;
2806 /* Interface table, possibly */
2807 cmdif = (ipfw_insn_if *)cmd;
2808 if (cmdif->name[0] != '\1')
2811 *ptype = IPFW_TABLE_INTERFACE;
2812 *puidx = cmdif->p.kidx;
2818 classify_flow(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2822 *ptype = IPFW_TABLE_FLOW;
2828 update_arg1(ipfw_insn *cmd, uint16_t idx)
2835 update_via(ipfw_insn *cmd, uint16_t idx)
2837 ipfw_insn_if *cmdif;
2839 cmdif = (ipfw_insn_if *)cmd;
2840 cmdif->p.kidx = idx;
2844 table_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
2845 struct named_object **pno)
2847 struct table_config *tc;
2850 IPFW_UH_WLOCK_ASSERT(ch);
2852 error = find_table_err(CHAIN_TO_NI(ch), ti, &tc);
2860 /* XXX: sets-sets! */
2861 static struct named_object *
2862 table_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
2864 struct namedobj_instance *ni;
2865 struct table_config *tc;
2867 IPFW_UH_WLOCK_ASSERT(ch);
2868 ni = CHAIN_TO_NI(ch);
2869 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, idx);
2870 KASSERT(tc != NULL, ("Table with index %d not found", idx));
2876 table_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2877 enum ipfw_sets_cmd cmd)
2885 * Always return success, the real action and decision
2886 * should make table_manage_sets_all().
2892 * NOTE: we need to use ipfw_objhash_del/ipfw_objhash_add
2893 * if set number will be used in hash function. Currently
2894 * we can just use generic handler that replaces set value.
2896 if (V_fw_tables_sets == 0)
2901 * Return EOPNOTSUPP for COUNT_ONE when per-set sysctl is
2902 * disabled. This allow skip table's opcodes from additional
2903 * checks when specific rules moved to another set.
2905 if (V_fw_tables_sets == 0)
2906 return (EOPNOTSUPP);
2908 /* Use generic sets handler when per-set sysctl is enabled. */
2909 return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2910 set, new_set, cmd));
2914 * We register several opcode rewriters for lookup tables.
2915 * All tables opcodes have the same ETLV type, but different subtype.
2916 * To avoid invoking sets handler several times for XXX_ALL commands,
2917 * we use separate manage_sets handler. O_RECV has the lowest value,
2918 * so it should be called first.
2921 table_manage_sets_all(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2922 enum ipfw_sets_cmd cmd)
2929 * Return success for TEST_ALL, since nothing prevents
2930 * move rules from one set to another. All tables are
2931 * accessible from all sets when per-set tables sysctl
2935 if (V_fw_tables_sets == 0)
2939 return (table_manage_sets(ch, set, new_set, cmd));
2941 /* Use generic sets handler when per-set sysctl is enabled. */
2942 return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2943 set, new_set, cmd));
2946 static struct opcode_obj_rewrite opcodes[] = {
2948 .opcode = O_IP_SRC_LOOKUP,
2949 .etlv = IPFW_TLV_TBL_NAME,
2950 .classifier = classify_srcdst,
2951 .update = update_arg1,
2952 .find_byname = table_findbyname,
2953 .find_bykidx = table_findbykidx,
2954 .create_object = create_table_compat,
2955 .manage_sets = table_manage_sets,
2958 .opcode = O_IP_DST_LOOKUP,
2959 .etlv = IPFW_TLV_TBL_NAME,
2960 .classifier = classify_srcdst,
2961 .update = update_arg1,
2962 .find_byname = table_findbyname,
2963 .find_bykidx = table_findbykidx,
2964 .create_object = create_table_compat,
2965 .manage_sets = table_manage_sets,
2968 .opcode = O_IP_FLOW_LOOKUP,
2969 .etlv = IPFW_TLV_TBL_NAME,
2970 .classifier = classify_flow,
2971 .update = update_arg1,
2972 .find_byname = table_findbyname,
2973 .find_bykidx = table_findbykidx,
2974 .create_object = create_table_compat,
2975 .manage_sets = table_manage_sets,
2979 .etlv = IPFW_TLV_TBL_NAME,
2980 .classifier = classify_via,
2981 .update = update_via,
2982 .find_byname = table_findbyname,
2983 .find_bykidx = table_findbykidx,
2984 .create_object = create_table_compat,
2985 .manage_sets = table_manage_sets,
2989 .etlv = IPFW_TLV_TBL_NAME,
2990 .classifier = classify_via,
2991 .update = update_via,
2992 .find_byname = table_findbyname,
2993 .find_bykidx = table_findbykidx,
2994 .create_object = create_table_compat,
2995 .manage_sets = table_manage_sets_all,
2999 .etlv = IPFW_TLV_TBL_NAME,
3000 .classifier = classify_via,
3001 .update = update_via,
3002 .find_byname = table_findbyname,
3003 .find_bykidx = table_findbykidx,
3004 .create_object = create_table_compat,
3005 .manage_sets = table_manage_sets,
3010 test_sets_cb(struct namedobj_instance *ni __unused, struct named_object *no,
3014 /* Check that there aren't any tables in not default set */
3021 * Switch between "set 0" and "rule's set" table binding,
3022 * Check all ruleset bindings and permits changing
3023 * IFF each binding has both rule AND table in default set (set 0).
3025 * Returns 0 on success.
3028 ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int sets)
3030 struct opcode_obj_rewrite *rw;
3031 struct namedobj_instance *ni;
3032 struct named_object *no;
3041 if (V_fw_tables_sets == sets) {
3042 IPFW_UH_WUNLOCK(ch);
3045 ni = CHAIN_TO_NI(ch);
3048 * Prevent disabling sets support if we have some tables
3049 * in not default sets.
3051 if (ipfw_objhash_foreach_type(ni, test_sets_cb,
3052 NULL, IPFW_TLV_TBL_NAME) != 0) {
3053 IPFW_UH_WUNLOCK(ch);
3058 * Scan all rules and examine tables opcodes.
3060 for (i = 0; i < ch->n_rules; i++) {
3066 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
3067 cmdlen = F_LEN(cmd);
3068 /* Check only tables opcodes */
3069 for (kidx = 0, rw = opcodes;
3070 rw < opcodes + nitems(opcodes); rw++) {
3071 if (rw->opcode != cmd->opcode)
3073 if (rw->classifier(cmd, &kidx, &subtype) == 0)
3078 no = ipfw_objhash_lookup_kidx(ni, kidx);
3079 /* Check if both table object and rule has the set 0 */
3080 if (no->set != 0 || rule->set != 0) {
3081 IPFW_UH_WUNLOCK(ch);
3087 V_fw_tables_sets = sets;
3088 IPFW_UH_WUNLOCK(ch);
3093 * Checks table name for validity.
3094 * Enforce basic length checks, the rest
3095 * should be done in userland.
3097 * Returns 0 if name is considered valid.
3100 check_table_name(const char *name)
3104 * TODO: do some more complicated checks
3106 return (ipfw_check_object_name_generic(name));
3110 * Finds table config based on either legacy index
3112 * Note @ti structure contains unchecked data from userland.
3114 * Returns 0 in success and fills in @tc with found config
3117 find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
3118 struct table_config **tc)
3120 char *name, bname[16];
3121 struct named_object *no;
3122 ipfw_obj_ntlv *ntlv;
3125 if (ti->tlvs != NULL) {
3126 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3133 * Use set provided by @ti instead of @ntlv one.
3134 * This is needed due to different sets behavior
3135 * controlled by V_fw_tables_sets.
3137 set = (V_fw_tables_sets != 0) ? ti->set : 0;
3139 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3144 no = ipfw_objhash_lookup_name(ni, set, name);
3145 *tc = (struct table_config *)no;
3151 * Finds table config based on either legacy index
3153 * Note @ti structure contains unchecked data from userland.
3155 * Returns pointer to table_config or NULL.
3157 static struct table_config *
3158 find_table(struct namedobj_instance *ni, struct tid_info *ti)
3160 struct table_config *tc;
3162 if (find_table_err(ni, ti, &tc) != 0)
3169 * Allocate new table config structure using
3170 * specified @algo and @aname.
3172 * Returns pointer to config or NULL.
3174 static struct table_config *
3175 alloc_table_config(struct ip_fw_chain *ch, struct tid_info *ti,
3176 struct table_algo *ta, char *aname, uint8_t tflags)
3178 char *name, bname[16];
3179 struct table_config *tc;
3181 ipfw_obj_ntlv *ntlv;
3184 if (ti->tlvs != NULL) {
3185 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3192 /* Compat part: convert number to string representation */
3193 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3198 tc = malloc(sizeof(struct table_config), M_IPFW, M_WAITOK | M_ZERO);
3199 tc->no.name = tc->tablename;
3200 tc->no.subtype = ta->type;
3202 tc->tflags = tflags;
3204 strlcpy(tc->tablename, name, sizeof(tc->tablename));
3205 /* Set "shared" value type by default */
3208 /* Preallocate data structures for new tables */
3209 error = ta->init(ch, &tc->astate, &tc->ti_copy, aname, tflags);
3219 * Destroys table state and config.
3222 free_table_config(struct namedobj_instance *ni, struct table_config *tc)
3225 KASSERT(tc->linked == 0, ("free() on linked config"));
3226 /* UH lock MUST NOT be held */
3229 * We're using ta without any locking/referencing.
3230 * TODO: fix this if we're going to use unloadable algos.
3232 tc->ta->destroy(tc->astate, &tc->ti_copy);
3237 * Links @tc to @chain table named instance.
3238 * Sets appropriate type/states in @chain table info.
3241 link_table(struct ip_fw_chain *ch, struct table_config *tc)
3243 struct namedobj_instance *ni;
3244 struct table_info *ti;
3247 IPFW_UH_WLOCK_ASSERT(ch);
3248 IPFW_WLOCK_ASSERT(ch);
3250 ni = CHAIN_TO_NI(ch);
3253 ipfw_objhash_add(ni, &tc->no);
3255 ti = KIDX_TO_TI(ch, kidx);
3258 /* Notify algo on real @ti address */
3259 if (tc->ta->change_ti != NULL)
3260 tc->ta->change_ti(tc->astate, ti);
3267 * Unlinks @tc from @chain table named instance.
3268 * Zeroes states in @chain and stores them in @tc.
3271 unlink_table(struct ip_fw_chain *ch, struct table_config *tc)
3273 struct namedobj_instance *ni;
3274 struct table_info *ti;
3277 IPFW_UH_WLOCK_ASSERT(ch);
3278 IPFW_WLOCK_ASSERT(ch);
3280 ni = CHAIN_TO_NI(ch);
3283 /* Clear state. @ti copy is already saved inside @tc */
3284 ipfw_objhash_del(ni, &tc->no);
3285 ti = KIDX_TO_TI(ch, kidx);
3286 memset(ti, 0, sizeof(struct table_info));
3290 /* Notify algo on real @ti address */
3291 if (tc->ta->change_ti != NULL)
3292 tc->ta->change_ti(tc->astate, NULL);
3295 static struct ipfw_sopt_handler scodes[] = {
3296 { IP_FW_TABLE_XCREATE, 0, HDIR_SET, create_table },
3297 { IP_FW_TABLE_XDESTROY, 0, HDIR_SET, flush_table_v0 },
3298 { IP_FW_TABLE_XFLUSH, 0, HDIR_SET, flush_table_v0 },
3299 { IP_FW_TABLE_XMODIFY, 0, HDIR_BOTH, modify_table },
3300 { IP_FW_TABLE_XINFO, 0, HDIR_GET, describe_table },
3301 { IP_FW_TABLES_XLIST, 0, HDIR_GET, list_tables },
3302 { IP_FW_TABLE_XLIST, 0, HDIR_GET, dump_table_v0 },
3303 { IP_FW_TABLE_XLIST, 1, HDIR_GET, dump_table_v1 },
3304 { IP_FW_TABLE_XADD, 0, HDIR_BOTH, manage_table_ent_v0 },
3305 { IP_FW_TABLE_XADD, 1, HDIR_BOTH, manage_table_ent_v1 },
3306 { IP_FW_TABLE_XDEL, 0, HDIR_BOTH, manage_table_ent_v0 },
3307 { IP_FW_TABLE_XDEL, 1, HDIR_BOTH, manage_table_ent_v1 },
3308 { IP_FW_TABLE_XFIND, 0, HDIR_GET, find_table_entry },
3309 { IP_FW_TABLE_XSWAP, 0, HDIR_SET, swap_table },
3310 { IP_FW_TABLES_ALIST, 0, HDIR_GET, list_table_algo },
3311 { IP_FW_TABLE_XGETSIZE, 0, HDIR_GET, get_table_size },
3315 destroy_table_locked(struct namedobj_instance *ni, struct named_object *no,
3319 unlink_table((struct ip_fw_chain *)arg, (struct table_config *)no);
3320 if (ipfw_objhash_free_idx(ni, no->kidx) != 0)
3321 printf("Error unlinking kidx %d from table %s\n",
3322 no->kidx, no->name);
3323 free_table_config(ni, (struct table_config *)no);
3328 * Shuts tables module down.
3331 ipfw_destroy_tables(struct ip_fw_chain *ch, int last)
3334 IPFW_DEL_SOPT_HANDLER(last, scodes);
3335 IPFW_DEL_OBJ_REWRITER(last, opcodes);
3337 /* Remove all tables from working set */
3340 ipfw_objhash_foreach(CHAIN_TO_NI(ch), destroy_table_locked, ch);
3342 IPFW_UH_WUNLOCK(ch);
3344 /* Free pointers itself */
3345 free(ch->tablestate, M_IPFW);
3347 ipfw_table_value_destroy(ch, last);
3348 ipfw_table_algo_destroy(ch);
3350 ipfw_objhash_destroy(CHAIN_TO_NI(ch));
3351 free(CHAIN_TO_TCFG(ch), M_IPFW);
3355 * Starts tables module.
3358 ipfw_init_tables(struct ip_fw_chain *ch, int first)
3360 struct tables_config *tcfg;
3362 /* Allocate pointers */
3363 ch->tablestate = malloc(V_fw_tables_max * sizeof(struct table_info),
3364 M_IPFW, M_WAITOK | M_ZERO);
3366 tcfg = malloc(sizeof(struct tables_config), M_IPFW, M_WAITOK | M_ZERO);
3367 tcfg->namehash = ipfw_objhash_create(V_fw_tables_max);
3370 ipfw_table_value_init(ch, first);
3371 ipfw_table_algo_init(ch);
3373 IPFW_ADD_OBJ_REWRITER(first, opcodes);
3374 IPFW_ADD_SOPT_HANDLER(first, scodes);