ip_fw_table.c revision 300021
1200590Sluigi/*-
2200673Sru * Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
3272840Smelifaro * Copyright (c) 2014 Yandex LLC
4272840Smelifaro * Copyright (c) 2014 Alexander V. Chernikov
5200590Sluigi *
6200590Sluigi * Redistribution and use in source and binary forms, with or without
7200590Sluigi * modification, are permitted provided that the following conditions
8200590Sluigi * are met:
9200590Sluigi * 1. Redistributions of source code must retain the above copyright
10200590Sluigi *    notice, this list of conditions and the following disclaimer.
11200590Sluigi * 2. Redistributions in binary form must reproduce the above copyright
12200590Sluigi *    notice, this list of conditions and the following disclaimer in the
13200590Sluigi *    documentation and/or other materials provided with the distribution.
14200590Sluigi *
15200590Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16200590Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17200590Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18200590Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19200590Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20200590Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21200590Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22200590Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23200590Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24200590Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25200590Sluigi * SUCH DAMAGE.
26200590Sluigi */
27200590Sluigi
28200590Sluigi#include <sys/cdefs.h>
29200590Sluigi__FBSDID("$FreeBSD: head/sys/netpfil/ipfw/ip_fw_table.c 300021 2016-05-17 07:47:23Z ae $");
30200590Sluigi
31200590Sluigi/*
32272840Smelifaro * Lookup table support for ipfw.
33200601Sluigi *
34272840Smelifaro * This file contains handlers for all generic tables' operations:
35272840Smelifaro * add/del/flush entries, list/dump tables etc..
36200838Sluigi *
37272840Smelifaro * Table data modification is protected by both UH and runtime lock
38272840Smelifaro * while reading configuration/data is protected by UH lock.
39272840Smelifaro *
40272840Smelifaro * Lookup algorithms for all table types are located in ip_fw_table_algo.c
41200590Sluigi */
42200590Sluigi
43225518Sjhb#include "opt_ipfw.h"
44200590Sluigi
45200590Sluigi#include <sys/param.h>
46200590Sluigi#include <sys/systm.h>
47200590Sluigi#include <sys/malloc.h>
48200590Sluigi#include <sys/kernel.h>
49200590Sluigi#include <sys/lock.h>
50200590Sluigi#include <sys/rwlock.h>
51272840Smelifaro#include <sys/rmlock.h>
52200590Sluigi#include <sys/socket.h>
53272840Smelifaro#include <sys/socketvar.h>
54240494Sglebius#include <sys/queue.h>
55200590Sluigi#include <net/if.h>	/* ip_fw.h requires IFNAMSIZ */
56200590Sluigi
57200590Sluigi#include <netinet/in.h>
58201732Sluigi#include <netinet/ip_var.h>	/* struct ipfw_rule_ref */
59200590Sluigi#include <netinet/ip_fw.h>
60200590Sluigi
61240494Sglebius#include <netpfil/ipfw/ip_fw_private.h>
62272840Smelifaro#include <netpfil/ipfw/ip_fw_table.h>
63240494Sglebius
64272840Smelifaro /*
65272840Smelifaro * Table has the following `type` concepts:
66272840Smelifaro *
67272840Smelifaro * `no.type` represents lookup key type (addr, ifp, uid, etc..)
68272840Smelifaro * vmask represents bitmask of table values which are present at the moment.
69272840Smelifaro * Special IPFW_VTYPE_LEGACY ( (uint32_t)-1 ) represents old
70272840Smelifaro * single-value-for-all approach.
71272840Smelifaro */
72272840Smelifarostruct table_config {
73272840Smelifaro	struct named_object	no;
74272840Smelifaro	uint8_t		tflags;		/* type flags */
75272840Smelifaro	uint8_t		locked;		/* 1 if locked from changes */
76272840Smelifaro	uint8_t		linked;		/* 1 if already linked */
77272840Smelifaro	uint8_t		ochanged;	/* used by set swapping */
78272840Smelifaro	uint8_t		vshared;	/* 1 if using shared value array */
79272840Smelifaro	uint8_t		spare[3];
80272840Smelifaro	uint32_t	count;		/* Number of records */
81272840Smelifaro	uint32_t	limit;		/* Max number of records */
82272840Smelifaro	uint32_t	vmask;		/* bitmask with supported values */
83272840Smelifaro	uint32_t	ocount;		/* used by set swapping */
84272840Smelifaro	uint64_t	gencnt;		/* generation count */
85272840Smelifaro	char		tablename[64];	/* table name */
86272840Smelifaro	struct table_algo	*ta;	/* Callbacks for given algo */
87272840Smelifaro	void		*astate;	/* algorithm state */
88272840Smelifaro	struct table_info	ti_copy;	/* data to put to table_info */
89272840Smelifaro	struct namedobj_instance	*vi;
90272840Smelifaro};
91200590Sluigi
92282070Smelifarostatic int find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
93282070Smelifaro    struct table_config **tc);
94272840Smelifarostatic struct table_config *find_table(struct namedobj_instance *ni,
95272840Smelifaro    struct tid_info *ti);
96272840Smelifarostatic struct table_config *alloc_table_config(struct ip_fw_chain *ch,
97272840Smelifaro    struct tid_info *ti, struct table_algo *ta, char *adata, uint8_t tflags);
98272840Smelifarostatic void free_table_config(struct namedobj_instance *ni,
99272840Smelifaro    struct table_config *tc);
100272840Smelifarostatic int create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
101272840Smelifaro    char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int ref);
102272840Smelifarostatic void link_table(struct ip_fw_chain *ch, struct table_config *tc);
103272840Smelifarostatic void unlink_table(struct ip_fw_chain *ch, struct table_config *tc);
104272840Smelifarostatic int find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
105272840Smelifaro    struct tentry_info *tei, uint32_t count, int op, struct table_config **ptc);
106272840Smelifaro#define	OP_ADD	1
107272840Smelifaro#define	OP_DEL	0
108272840Smelifarostatic int export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
109272840Smelifaro    struct sockopt_data *sd);
110272840Smelifarostatic void export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
111272840Smelifaro    ipfw_xtable_info *i);
112272840Smelifarostatic int dump_table_tentry(void *e, void *arg);
113272840Smelifarostatic int dump_table_xentry(void *e, void *arg);
114200590Sluigi
115272840Smelifarostatic int swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
116272840Smelifaro    struct tid_info *b);
117200590Sluigi
118290332Saestatic int check_table_name(const char *name);
119272840Smelifarostatic int check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
120272840Smelifaro    struct table_config *tc, struct table_info *ti, uint32_t count);
121272840Smelifarostatic int destroy_table(struct ip_fw_chain *ch, struct tid_info *ti);
122232865Smelifaro
123272840Smelifarostatic struct table_algo *find_table_algo(struct tables_config *tableconf,
124272840Smelifaro    struct tid_info *ti, char *name);
125232865Smelifaro
126272840Smelifarostatic void objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti);
127272840Smelifarostatic void ntlv_to_ti(struct _ipfw_obj_ntlv *ntlv, struct tid_info *ti);
128272840Smelifaro
129272840Smelifaro#define	CHAIN_TO_NI(chain)	(CHAIN_TO_TCFG(chain)->namehash)
130272840Smelifaro#define	KIDX_TO_TI(ch, k)	(&(((struct table_info *)(ch)->tablestate)[k]))
131272840Smelifaro
132272840Smelifaro#define	TA_BUF_SZ	128	/* On-stack buffer for add/delete state */
133272840Smelifaro
134272840Smelifarovoid
135272840Smelifarorollback_toperation_state(struct ip_fw_chain *ch, void *object)
136272840Smelifaro{
137272840Smelifaro	struct tables_config *tcfg;
138272840Smelifaro	struct op_state *os;
139272840Smelifaro
140272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
141272840Smelifaro	TAILQ_FOREACH(os, &tcfg->state_list, next)
142272840Smelifaro		os->func(object, os);
143272840Smelifaro}
144272840Smelifaro
145272840Smelifarovoid
146272840Smelifaroadd_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
147272840Smelifaro{
148272840Smelifaro	struct tables_config *tcfg;
149272840Smelifaro
150272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
151272840Smelifaro	TAILQ_INSERT_HEAD(&tcfg->state_list, &ts->opstate, next);
152272840Smelifaro}
153272840Smelifaro
154272840Smelifarovoid
155272840Smelifarodel_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
156272840Smelifaro{
157272840Smelifaro	struct tables_config *tcfg;
158272840Smelifaro
159272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
160272840Smelifaro	TAILQ_REMOVE(&tcfg->state_list, &ts->opstate, next);
161272840Smelifaro}
162272840Smelifaro
163272840Smelifarovoid
164272840Smelifarotc_ref(struct table_config *tc)
165272840Smelifaro{
166272840Smelifaro
167272840Smelifaro	tc->no.refcnt++;
168272840Smelifaro}
169272840Smelifaro
170272840Smelifarovoid
171272840Smelifarotc_unref(struct table_config *tc)
172272840Smelifaro{
173272840Smelifaro
174272840Smelifaro	tc->no.refcnt--;
175272840Smelifaro}
176272840Smelifaro
177272840Smelifarostatic struct table_value *
178272840Smelifaroget_table_value(struct ip_fw_chain *ch, struct table_config *tc, uint32_t kidx)
179272840Smelifaro{
180272840Smelifaro	struct table_value *pval;
181272840Smelifaro
182272840Smelifaro	pval = (struct table_value *)ch->valuestate;
183272840Smelifaro
184272840Smelifaro	return (&pval[kidx]);
185272840Smelifaro}
186272840Smelifaro
187272840Smelifaro
188201120Sluigi/*
189272840Smelifaro * Checks if we're able to insert/update entry @tei into table
190272840Smelifaro * w.r.t @tc limits.
191272840Smelifaro * May alter @tei to indicate insertion error / insert
192272840Smelifaro * options.
193272840Smelifaro *
194272840Smelifaro * Returns 0 if operation can be performed/
195201120Sluigi */
196272840Smelifarostatic int
197272840Smelifarocheck_table_limit(struct table_config *tc, struct tentry_info *tei)
198272840Smelifaro{
199272840Smelifaro
200272840Smelifaro	if (tc->limit == 0 || tc->count < tc->limit)
201272840Smelifaro		return (0);
202272840Smelifaro
203272840Smelifaro	if ((tei->flags & TEI_FLAGS_UPDATE) == 0) {
204272840Smelifaro		/* Notify userland on error cause */
205272840Smelifaro		tei->flags |= TEI_FLAGS_LIMIT;
206272840Smelifaro		return (EFBIG);
207272840Smelifaro	}
208272840Smelifaro
209272840Smelifaro	/*
210272840Smelifaro	 * We have UPDATE flag set.
211272840Smelifaro	 * Permit updating record (if found),
212272840Smelifaro	 * but restrict adding new one since we've
213272840Smelifaro	 * already hit the limit.
214272840Smelifaro	 */
215272840Smelifaro	tei->flags |= TEI_FLAGS_DONTADD;
216272840Smelifaro
217272840Smelifaro	return (0);
218272840Smelifaro}
219272840Smelifaro
220232865Smelifaro/*
221272840Smelifaro * Convert algorithm callback return code into
222272840Smelifaro * one of pre-defined states known by userland.
223232865Smelifaro */
224272840Smelifarostatic void
225272840Smelifarostore_tei_result(struct tentry_info *tei, int op, int error, uint32_t num)
226272840Smelifaro{
227272840Smelifaro	int flag;
228201120Sluigi
229272840Smelifaro	flag = 0;
230232865Smelifaro
231272840Smelifaro	switch (error) {
232272840Smelifaro	case 0:
233272840Smelifaro		if (op == OP_ADD && num != 0)
234272840Smelifaro			flag = TEI_FLAGS_ADDED;
235272840Smelifaro		if (op == OP_DEL)
236272840Smelifaro			flag = TEI_FLAGS_DELETED;
237272840Smelifaro		break;
238272840Smelifaro	case ENOENT:
239272840Smelifaro		flag = TEI_FLAGS_NOTFOUND;
240272840Smelifaro		break;
241272840Smelifaro	case EEXIST:
242272840Smelifaro		flag = TEI_FLAGS_EXISTS;
243272840Smelifaro		break;
244272840Smelifaro	default:
245272840Smelifaro		flag = TEI_FLAGS_ERROR;
246272840Smelifaro	}
247232865Smelifaro
248272840Smelifaro	tei->flags |= flag;
249272840Smelifaro}
250272840Smelifaro
251272840Smelifaro/*
252272840Smelifaro * Creates and references table with default parameters.
253272840Smelifaro * Saves table config, algo and allocated kidx info @ptc, @pta and
254272840Smelifaro * @pkidx if non-zero.
255272840Smelifaro * Used for table auto-creation to support old binaries.
256272840Smelifaro *
257272840Smelifaro * Returns 0 on success.
258272840Smelifaro */
259272840Smelifarostatic int
260272840Smelifarocreate_table_compat(struct ip_fw_chain *ch, struct tid_info *ti,
261272840Smelifaro    uint16_t *pkidx)
262232865Smelifaro{
263272840Smelifaro	ipfw_xtable_info xi;
264272840Smelifaro	int error;
265232865Smelifaro
266272840Smelifaro	memset(&xi, 0, sizeof(xi));
267272840Smelifaro	/* Set default value mask for legacy clients */
268272840Smelifaro	xi.vmask = IPFW_VTYPE_LEGACY;
269272840Smelifaro
270272840Smelifaro	error = create_table_internal(ch, ti, NULL, &xi, pkidx, 1);
271272840Smelifaro	if (error != 0)
272272840Smelifaro		return (error);
273272840Smelifaro
274272840Smelifaro	return (0);
275232865Smelifaro}
276232865Smelifaro
277272840Smelifaro/*
278272840Smelifaro * Find and reference existing table optionally
279272840Smelifaro * creating new one.
280272840Smelifaro *
281272840Smelifaro * Saves found table config into @ptc.
282272840Smelifaro * Note function may drop/acquire UH_WLOCK.
283272840Smelifaro * Returns 0 if table was found/created and referenced
284272840Smelifaro * or non-zero return code.
285272840Smelifaro */
286272840Smelifarostatic int
287272840Smelifarofind_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
288272840Smelifaro    struct tentry_info *tei, uint32_t count, int op,
289272840Smelifaro    struct table_config **ptc)
290200590Sluigi{
291272840Smelifaro	struct namedobj_instance *ni;
292272840Smelifaro	struct table_config *tc;
293272840Smelifaro	uint16_t kidx;
294272840Smelifaro	int error;
295200590Sluigi
296272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
297232865Smelifaro
298272840Smelifaro	ni = CHAIN_TO_NI(ch);
299272840Smelifaro	tc = NULL;
300272840Smelifaro	if ((tc = find_table(ni, ti)) != NULL) {
301272840Smelifaro		/* check table type */
302282070Smelifaro		if (tc->no.subtype != ti->type)
303232865Smelifaro			return (EINVAL);
304272840Smelifaro
305272840Smelifaro		if (tc->locked != 0)
306272840Smelifaro			return (EACCES);
307272840Smelifaro
308272840Smelifaro		/* Try to exit early on limit hit */
309272840Smelifaro		if (op == OP_ADD && count == 1 &&
310272840Smelifaro		    check_table_limit(tc, tei) != 0)
311272840Smelifaro			return (EFBIG);
312272840Smelifaro
313272840Smelifaro		/* Reference and return */
314272840Smelifaro		tc->no.refcnt++;
315272840Smelifaro		*ptc = tc;
316272840Smelifaro		return (0);
317272840Smelifaro	}
318272840Smelifaro
319272840Smelifaro	if (op == OP_DEL)
320272840Smelifaro		return (ESRCH);
321272840Smelifaro
322298995Spfg	/* Compatibility mode: create new table for old clients */
323272840Smelifaro	if ((tei->flags & TEI_FLAGS_COMPAT) == 0)
324272840Smelifaro		return (ESRCH);
325272840Smelifaro
326272840Smelifaro	IPFW_UH_WUNLOCK(ch);
327272840Smelifaro	error = create_table_compat(ch, ti, &kidx);
328272840Smelifaro	IPFW_UH_WLOCK(ch);
329272840Smelifaro
330272840Smelifaro	if (error != 0)
331272840Smelifaro		return (error);
332272840Smelifaro
333272840Smelifaro	tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
334272840Smelifaro	KASSERT(tc != NULL, ("create_table_compat returned bad idx %d", kidx));
335272840Smelifaro
336272840Smelifaro	/* OK, now we've got referenced table. */
337272840Smelifaro	*ptc = tc;
338272840Smelifaro	return (0);
339272840Smelifaro}
340272840Smelifaro
341272840Smelifaro/*
342272840Smelifaro * Rolls back already @added to @tc entries using state array @ta_buf_m.
343272840Smelifaro * Assume the following layout:
344272840Smelifaro * 1) ADD state (ta_buf_m[0] ... t_buf_m[added - 1]) for handling update cases
345272840Smelifaro * 2) DEL state (ta_buf_m[count[ ... t_buf_m[count + added - 1])
346272840Smelifaro *   for storing deleted state
347272840Smelifaro */
348272840Smelifarostatic void
349272840Smelifarorollback_added_entries(struct ip_fw_chain *ch, struct table_config *tc,
350272840Smelifaro    struct table_info *tinfo, struct tentry_info *tei, caddr_t ta_buf_m,
351272840Smelifaro    uint32_t count, uint32_t added)
352272840Smelifaro{
353272840Smelifaro	struct table_algo *ta;
354272840Smelifaro	struct tentry_info *ptei;
355272840Smelifaro	caddr_t v, vv;
356272840Smelifaro	size_t ta_buf_sz;
357272840Smelifaro	int error, i;
358272840Smelifaro	uint32_t num;
359272840Smelifaro
360272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
361272840Smelifaro
362272840Smelifaro	ta = tc->ta;
363272840Smelifaro	ta_buf_sz = ta->ta_buf_size;
364272840Smelifaro	v = ta_buf_m;
365272840Smelifaro	vv = v + count * ta_buf_sz;
366272840Smelifaro	for (i = 0; i < added; i++, v += ta_buf_sz, vv += ta_buf_sz) {
367272840Smelifaro		ptei = &tei[i];
368272840Smelifaro		if ((ptei->flags & TEI_FLAGS_UPDATED) != 0) {
369272840Smelifaro
370272840Smelifaro			/*
371272840Smelifaro			 * We have old value stored by previous
372272840Smelifaro			 * call in @ptei->value. Do add once again
373272840Smelifaro			 * to restore it.
374272840Smelifaro			 */
375272840Smelifaro			error = ta->add(tc->astate, tinfo, ptei, v, &num);
376272840Smelifaro			KASSERT(error == 0, ("rollback UPDATE fail"));
377272840Smelifaro			KASSERT(num == 0, ("rollback UPDATE fail2"));
378272840Smelifaro			continue;
379232865Smelifaro		}
380272840Smelifaro
381272840Smelifaro		error = ta->prepare_del(ch, ptei, vv);
382272840Smelifaro		KASSERT(error == 0, ("pre-rollback INSERT failed"));
383272840Smelifaro		error = ta->del(tc->astate, tinfo, ptei, vv, &num);
384272840Smelifaro		KASSERT(error == 0, ("rollback INSERT failed"));
385272840Smelifaro		tc->count -= num;
386272840Smelifaro	}
387272840Smelifaro}
388272840Smelifaro
389272840Smelifaro/*
390272840Smelifaro * Prepares add/del state for all @count entries in @tei.
391272840Smelifaro * Uses either stack buffer (@ta_buf) or allocates a new one.
392272840Smelifaro * Stores pointer to allocated buffer back to @ta_buf.
393272840Smelifaro *
394272840Smelifaro * Returns 0 on success.
395272840Smelifaro */
396272840Smelifarostatic int
397272840Smelifaroprepare_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
398272840Smelifaro    struct tentry_info *tei, uint32_t count, int op, caddr_t *ta_buf)
399272840Smelifaro{
400272840Smelifaro	caddr_t ta_buf_m, v;
401272840Smelifaro	size_t ta_buf_sz, sz;
402272840Smelifaro	struct tentry_info *ptei;
403272840Smelifaro	int error, i;
404272840Smelifaro
405272840Smelifaro	error = 0;
406272840Smelifaro	ta_buf_sz = ta->ta_buf_size;
407272840Smelifaro	if (count == 1) {
408272840Smelifaro		/* Sigle add/delete, use on-stack buffer */
409272840Smelifaro		memset(*ta_buf, 0, TA_BUF_SZ);
410272840Smelifaro		ta_buf_m = *ta_buf;
411272840Smelifaro	} else {
412272840Smelifaro
413272840Smelifaro		/*
414272840Smelifaro		 * Multiple adds/deletes, allocate larger buffer
415272840Smelifaro		 *
416272840Smelifaro		 * Note we need 2xcount buffer for add case:
417272840Smelifaro		 * we have hold both ADD state
418272840Smelifaro		 * and DELETE state (this may be needed
419272840Smelifaro		 * if we need to rollback all changes)
420272840Smelifaro		 */
421272840Smelifaro		sz = count * ta_buf_sz;
422272840Smelifaro		ta_buf_m = malloc((op == OP_ADD) ? sz * 2 : sz, M_TEMP,
423272840Smelifaro		    M_WAITOK | M_ZERO);
424272840Smelifaro	}
425272840Smelifaro
426272840Smelifaro	v = ta_buf_m;
427272840Smelifaro	for (i = 0; i < count; i++, v += ta_buf_sz) {
428272840Smelifaro		ptei = &tei[i];
429272840Smelifaro		error = (op == OP_ADD) ?
430272840Smelifaro		    ta->prepare_add(ch, ptei, v) : ta->prepare_del(ch, ptei, v);
431272840Smelifaro
432272840Smelifaro		/*
433272840Smelifaro		 * Some syntax error (incorrect mask, or address, or
434272840Smelifaro		 * anything). Return error regardless of atomicity
435272840Smelifaro		 * settings.
436272840Smelifaro		 */
437272840Smelifaro		if (error != 0)
438272840Smelifaro			break;
439272840Smelifaro	}
440272840Smelifaro
441272840Smelifaro	*ta_buf = ta_buf_m;
442272840Smelifaro	return (error);
443272840Smelifaro}
444272840Smelifaro
445272840Smelifaro/*
446272840Smelifaro * Flushes allocated state for each @count entries in @tei.
447272840Smelifaro * Frees @ta_buf_m if differs from stack buffer @ta_buf.
448272840Smelifaro */
449272840Smelifarostatic void
450272840Smelifaroflush_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
451272840Smelifaro    struct tentry_info *tei, uint32_t count, int rollback,
452272840Smelifaro    caddr_t ta_buf_m, caddr_t ta_buf)
453272840Smelifaro{
454272840Smelifaro	caddr_t v;
455272840Smelifaro	struct tentry_info *ptei;
456272840Smelifaro	size_t ta_buf_sz;
457272840Smelifaro	int i;
458272840Smelifaro
459272840Smelifaro	ta_buf_sz = ta->ta_buf_size;
460272840Smelifaro
461272840Smelifaro	/* Run cleaning callback anyway */
462272840Smelifaro	v = ta_buf_m;
463272840Smelifaro	for (i = 0; i < count; i++, v += ta_buf_sz) {
464272840Smelifaro		ptei = &tei[i];
465272840Smelifaro		ta->flush_entry(ch, ptei, v);
466272840Smelifaro		if (ptei->ptv != NULL) {
467272840Smelifaro			free(ptei->ptv, M_IPFW);
468272840Smelifaro			ptei->ptv = NULL;
469272840Smelifaro		}
470272840Smelifaro	}
471272840Smelifaro
472272840Smelifaro	/* Clean up "deleted" state in case of rollback */
473272840Smelifaro	if (rollback != 0) {
474272840Smelifaro		v = ta_buf_m + count * ta_buf_sz;
475272840Smelifaro		for (i = 0; i < count; i++, v += ta_buf_sz)
476272840Smelifaro			ta->flush_entry(ch, &tei[i], v);
477272840Smelifaro	}
478272840Smelifaro
479272840Smelifaro	if (ta_buf_m != ta_buf)
480272840Smelifaro		free(ta_buf_m, M_TEMP);
481272840Smelifaro}
482272840Smelifaro
483272840Smelifaro
484272840Smelifarostatic void
485272840Smelifarorollback_add_entry(void *object, struct op_state *_state)
486272840Smelifaro{
487272840Smelifaro	struct ip_fw_chain *ch;
488272840Smelifaro	struct tableop_state *ts;
489272840Smelifaro
490272840Smelifaro	ts = (struct tableop_state *)_state;
491272840Smelifaro
492272840Smelifaro	if (ts->tc != object && ts->ch != object)
493272840Smelifaro		return;
494272840Smelifaro
495272840Smelifaro	ch = ts->ch;
496272840Smelifaro
497272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
498272840Smelifaro
499272840Smelifaro	/* Call specifid unlockers */
500272840Smelifaro	rollback_table_values(ts);
501272840Smelifaro
502272840Smelifaro	/* Indicate we've called */
503272840Smelifaro	ts->modified = 1;
504272840Smelifaro}
505272840Smelifaro
506272840Smelifaro/*
507272840Smelifaro * Adds/updates one or more entries in table @ti.
508272840Smelifaro *
509272840Smelifaro * Function may drop/reacquire UH wlock multiple times due to
510272840Smelifaro * items alloc, algorithm callbacks (check_space), value linkage
511272840Smelifaro * (new values, value storage realloc), etc..
512272840Smelifaro * Other processes like other adds (which may involve storage resize),
513272840Smelifaro * table swaps (which changes table data and may change algo type),
514272840Smelifaro * table modify (which may change value mask) may be executed
515272840Smelifaro * simultaneously so we need to deal with it.
516272840Smelifaro *
517272840Smelifaro * The following approach was implemented:
518272840Smelifaro * we have per-chain linked list, protected with UH lock.
519272840Smelifaro * add_table_entry prepares special on-stack structure wthich is passed
520272840Smelifaro * to its descendants. Users add this structure to this list before unlock.
521272840Smelifaro * After performing needed operations and acquiring UH lock back, each user
522272840Smelifaro * checks if structure has changed. If true, it rolls local state back and
523272840Smelifaro * returns without error to the caller.
524272840Smelifaro * add_table_entry() on its own checks if structure has changed and restarts
525272840Smelifaro * its operation from the beginning (goto restart).
526272840Smelifaro *
527272840Smelifaro * Functions which are modifying fields of interest (currently
528272840Smelifaro *   resize_shared_value_storage() and swap_tables() )
529272840Smelifaro * traverses given list while holding UH lock immediately before
530272840Smelifaro * performing their operations calling function provided be list entry
531272840Smelifaro * ( currently rollback_add_entry  ) which performs rollback for all necessary
532272840Smelifaro * state and sets appropriate values in structure indicating rollback
533272840Smelifaro * has happened.
534272840Smelifaro *
535272840Smelifaro * Algo interaction:
536272840Smelifaro * Function references @ti first to ensure table won't
537272840Smelifaro * disappear or change its type.
538272840Smelifaro * After that, prepare_add callback is called for each @tei entry.
539272840Smelifaro * Next, we try to add each entry under UH+WHLOCK
540272840Smelifaro * using add() callback.
541272840Smelifaro * Finally, we free all state by calling flush_entry callback
542272840Smelifaro * for each @tei.
543272840Smelifaro *
544272840Smelifaro * Returns 0 on success.
545272840Smelifaro */
546272840Smelifaroint
547272840Smelifaroadd_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
548272840Smelifaro    struct tentry_info *tei, uint8_t flags, uint32_t count)
549272840Smelifaro{
550272840Smelifaro	struct table_config *tc;
551272840Smelifaro	struct table_algo *ta;
552272840Smelifaro	uint16_t kidx;
553272840Smelifaro	int error, first_error, i, rollback;
554272840Smelifaro	uint32_t num, numadd;
555272840Smelifaro	struct tentry_info *ptei;
556272840Smelifaro	struct tableop_state ts;
557272840Smelifaro	char ta_buf[TA_BUF_SZ];
558272840Smelifaro	caddr_t ta_buf_m, v;
559272840Smelifaro
560272840Smelifaro	memset(&ts, 0, sizeof(ts));
561272840Smelifaro	ta = NULL;
562272840Smelifaro	IPFW_UH_WLOCK(ch);
563272840Smelifaro
564272840Smelifaro	/*
565272840Smelifaro	 * Find and reference existing table.
566272840Smelifaro	 */
567272840Smelifarorestart:
568272840Smelifaro	if (ts.modified != 0) {
569272840Smelifaro		IPFW_UH_WUNLOCK(ch);
570272840Smelifaro		flush_batch_buffer(ch, ta, tei, count, rollback,
571272840Smelifaro		    ta_buf_m, ta_buf);
572272840Smelifaro		memset(&ts, 0, sizeof(ts));
573272840Smelifaro		ta = NULL;
574272840Smelifaro		IPFW_UH_WLOCK(ch);
575272840Smelifaro	}
576272840Smelifaro
577272840Smelifaro	error = find_ref_table(ch, ti, tei, count, OP_ADD, &tc);
578272840Smelifaro	if (error != 0) {
579272840Smelifaro		IPFW_UH_WUNLOCK(ch);
580272840Smelifaro		return (error);
581272840Smelifaro	}
582272840Smelifaro	ta = tc->ta;
583272840Smelifaro
584272840Smelifaro	/* Fill in tablestate */
585272840Smelifaro	ts.ch = ch;
586272840Smelifaro	ts.opstate.func = rollback_add_entry;
587272840Smelifaro	ts.tc = tc;
588272840Smelifaro	ts.vshared = tc->vshared;
589272840Smelifaro	ts.vmask = tc->vmask;
590272840Smelifaro	ts.ta = ta;
591272840Smelifaro	ts.tei = tei;
592272840Smelifaro	ts.count = count;
593272840Smelifaro	rollback = 0;
594272840Smelifaro	add_toperation_state(ch, &ts);
595272840Smelifaro	IPFW_UH_WUNLOCK(ch);
596272840Smelifaro
597272840Smelifaro	/* Allocate memory and prepare record(s) */
598272840Smelifaro	/* Pass stack buffer by default */
599272840Smelifaro	ta_buf_m = ta_buf;
600272840Smelifaro	error = prepare_batch_buffer(ch, ta, tei, count, OP_ADD, &ta_buf_m);
601272840Smelifaro
602272840Smelifaro	IPFW_UH_WLOCK(ch);
603282521Smelifaro	del_toperation_state(ch, &ts);
604272840Smelifaro	/* Drop reference we've used in first search */
605272840Smelifaro	tc->no.refcnt--;
606272840Smelifaro
607282521Smelifaro	/* Check prepare_batch_buffer() error */
608282521Smelifaro	if (error != 0)
609282521Smelifaro		goto cleanup;
610282521Smelifaro
611272840Smelifaro	/*
612272840Smelifaro	 * Check if table swap has happened.
613272840Smelifaro	 * (so table algo might be changed).
614272840Smelifaro	 * Restart operation to achieve consistent behavior.
615272840Smelifaro	 */
616272840Smelifaro	if (ts.modified != 0)
617272840Smelifaro		goto restart;
618272840Smelifaro
619272840Smelifaro	/*
620272840Smelifaro	 * Link all values values to shared/per-table value array.
621272840Smelifaro	 *
622272840Smelifaro	 * May release/reacquire UH_WLOCK.
623272840Smelifaro	 */
624272840Smelifaro	error = ipfw_link_table_values(ch, &ts);
625272840Smelifaro	if (error != 0)
626272840Smelifaro		goto cleanup;
627272840Smelifaro	if (ts.modified != 0)
628272840Smelifaro		goto restart;
629272840Smelifaro
630272840Smelifaro	/*
631272840Smelifaro	 * Ensure we are able to add all entries without additional
632272840Smelifaro	 * memory allocations. May release/reacquire UH_WLOCK.
633272840Smelifaro	 */
634272840Smelifaro	kidx = tc->no.kidx;
635272840Smelifaro	error = check_table_space(ch, &ts, tc, KIDX_TO_TI(ch, kidx), count);
636272840Smelifaro	if (error != 0)
637272840Smelifaro		goto cleanup;
638272840Smelifaro	if (ts.modified != 0)
639272840Smelifaro		goto restart;
640272840Smelifaro
641272840Smelifaro	/* We've got valid table in @tc. Let's try to add data */
642272840Smelifaro	kidx = tc->no.kidx;
643272840Smelifaro	ta = tc->ta;
644272840Smelifaro	numadd = 0;
645272840Smelifaro	first_error = 0;
646272840Smelifaro
647272840Smelifaro	IPFW_WLOCK(ch);
648272840Smelifaro
649272840Smelifaro	v = ta_buf_m;
650272840Smelifaro	for (i = 0; i < count; i++, v += ta->ta_buf_size) {
651272840Smelifaro		ptei = &tei[i];
652272840Smelifaro		num = 0;
653272840Smelifaro		/* check limit before adding */
654272840Smelifaro		if ((error = check_table_limit(tc, ptei)) == 0) {
655272840Smelifaro			error = ta->add(tc->astate, KIDX_TO_TI(ch, kidx),
656272840Smelifaro			    ptei, v, &num);
657272840Smelifaro			/* Set status flag to inform userland */
658272840Smelifaro			store_tei_result(ptei, OP_ADD, error, num);
659272840Smelifaro		}
660272840Smelifaro		if (error == 0) {
661272840Smelifaro			/* Update number of records to ease limit checking */
662272840Smelifaro			tc->count += num;
663272840Smelifaro			numadd += num;
664272840Smelifaro			continue;
665272840Smelifaro		}
666272840Smelifaro
667272840Smelifaro		if (first_error == 0)
668272840Smelifaro			first_error = error;
669272840Smelifaro
670272840Smelifaro		/*
671272840Smelifaro		 * Some error have happened. Check our atomicity
672272840Smelifaro		 * settings: continue if atomicity is not required,
673272840Smelifaro		 * rollback changes otherwise.
674272840Smelifaro		 */
675272840Smelifaro		if ((flags & IPFW_CTF_ATOMIC) == 0)
676272840Smelifaro			continue;
677272840Smelifaro
678272840Smelifaro		rollback_added_entries(ch, tc, KIDX_TO_TI(ch, kidx),
679272840Smelifaro		    tei, ta_buf_m, count, i);
680272840Smelifaro
681272840Smelifaro		rollback = 1;
682232865Smelifaro		break;
683272840Smelifaro	}
684272840Smelifaro
685272840Smelifaro	IPFW_WUNLOCK(ch);
686272840Smelifaro
687272840Smelifaro	ipfw_garbage_table_values(ch, tc, tei, count, rollback);
688272840Smelifaro
689272840Smelifaro	/* Permit post-add algorithm grow/rehash. */
690272840Smelifaro	if (numadd != 0)
691272840Smelifaro		check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
692272840Smelifaro
693272840Smelifaro	/* Return first error to user, if any */
694272840Smelifaro	error = first_error;
695272840Smelifaro
696272840Smelifarocleanup:
697272840Smelifaro	IPFW_UH_WUNLOCK(ch);
698272840Smelifaro
699272840Smelifaro	flush_batch_buffer(ch, ta, tei, count, rollback, ta_buf_m, ta_buf);
700232865Smelifaro
701272840Smelifaro	return (error);
702272840Smelifaro}
703232865Smelifaro
704272840Smelifaro/*
705272840Smelifaro * Deletes one or more entries in table @ti.
706272840Smelifaro *
707272840Smelifaro * Returns 0 on success.
708272840Smelifaro */
709272840Smelifaroint
710272840Smelifarodel_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
711272840Smelifaro    struct tentry_info *tei, uint8_t flags, uint32_t count)
712272840Smelifaro{
713272840Smelifaro	struct table_config *tc;
714272840Smelifaro	struct table_algo *ta;
715272840Smelifaro	struct tentry_info *ptei;
716272840Smelifaro	uint16_t kidx;
717272840Smelifaro	int error, first_error, i;
718272840Smelifaro	uint32_t num, numdel;
719272840Smelifaro	char ta_buf[TA_BUF_SZ];
720272840Smelifaro	caddr_t ta_buf_m, v;
721232865Smelifaro
722272840Smelifaro	/*
723272840Smelifaro	 * Find and reference existing table.
724272840Smelifaro	 */
725272840Smelifaro	IPFW_UH_WLOCK(ch);
726272840Smelifaro	error = find_ref_table(ch, ti, tei, count, OP_DEL, &tc);
727272840Smelifaro	if (error != 0) {
728272840Smelifaro		IPFW_UH_WUNLOCK(ch);
729272840Smelifaro		return (error);
730272840Smelifaro	}
731272840Smelifaro	ta = tc->ta;
732272840Smelifaro	IPFW_UH_WUNLOCK(ch);
733232865Smelifaro
734272840Smelifaro	/* Allocate memory and prepare record(s) */
735272840Smelifaro	/* Pass stack buffer by default */
736272840Smelifaro	ta_buf_m = ta_buf;
737272840Smelifaro	error = prepare_batch_buffer(ch, ta, tei, count, OP_DEL, &ta_buf_m);
738272840Smelifaro	if (error != 0)
739272840Smelifaro		goto cleanup;
740272840Smelifaro
741272840Smelifaro	IPFW_UH_WLOCK(ch);
742272840Smelifaro
743272840Smelifaro	/* Drop reference we've used in first search */
744272840Smelifaro	tc->no.refcnt--;
745272840Smelifaro
746272840Smelifaro	/*
747272840Smelifaro	 * Check if table algo is still the same.
748272840Smelifaro	 * (changed ta may be the result of table swap).
749272840Smelifaro	 */
750272840Smelifaro	if (ta != tc->ta) {
751272840Smelifaro		IPFW_UH_WUNLOCK(ch);
752272840Smelifaro		error = EINVAL;
753272840Smelifaro		goto cleanup;
754232865Smelifaro	}
755232865Smelifaro
756272840Smelifaro	kidx = tc->no.kidx;
757272840Smelifaro	numdel = 0;
758272840Smelifaro	first_error = 0;
759272840Smelifaro
760200590Sluigi	IPFW_WLOCK(ch);
761272840Smelifaro	v = ta_buf_m;
762272840Smelifaro	for (i = 0; i < count; i++, v += ta->ta_buf_size) {
763272840Smelifaro		ptei = &tei[i];
764272840Smelifaro		num = 0;
765272840Smelifaro		error = ta->del(tc->astate, KIDX_TO_TI(ch, kidx), ptei, v,
766272840Smelifaro		    &num);
767272840Smelifaro		/* Save state for userland */
768272840Smelifaro		store_tei_result(ptei, OP_DEL, error, num);
769272840Smelifaro		if (error != 0 && first_error == 0)
770272840Smelifaro			first_error = error;
771272840Smelifaro		tc->count -= num;
772272840Smelifaro		numdel += num;
773272840Smelifaro	}
774272840Smelifaro	IPFW_WUNLOCK(ch);
775232865Smelifaro
776272840Smelifaro	/* Unlink non-used values */
777272840Smelifaro	ipfw_garbage_table_values(ch, tc, tei, count, 0);
778272840Smelifaro
779272840Smelifaro	if (numdel != 0) {
780272840Smelifaro		/* Run post-del hook to permit shrinking */
781272840Smelifaro		check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
782232865Smelifaro	}
783232865Smelifaro
784272840Smelifaro	IPFW_UH_WUNLOCK(ch);
785272840Smelifaro
786272840Smelifaro	/* Return first error to user, if any */
787272840Smelifaro	error = first_error;
788272840Smelifaro
789272840Smelifarocleanup:
790272840Smelifaro	flush_batch_buffer(ch, ta, tei, count, 0, ta_buf_m, ta_buf);
791272840Smelifaro
792272840Smelifaro	return (error);
793272840Smelifaro}
794272840Smelifaro
795272840Smelifaro/*
796272840Smelifaro * Ensure that table @tc has enough space to add @count entries without
797272840Smelifaro * need for reallocation.
798272840Smelifaro *
799272840Smelifaro * Callbacks order:
800272840Smelifaro * 0) need_modify() (UH_WLOCK) - checks if @count items can be added w/o resize.
801272840Smelifaro *
802272840Smelifaro * 1) alloc_modify (no locks, M_WAITOK) - alloc new state based on @pflags.
803272840Smelifaro * 2) prepare_modifyt (UH_WLOCK) - copy old data into new storage
804272840Smelifaro * 3) modify (UH_WLOCK + WLOCK) - switch pointers
805272840Smelifaro * 4) flush_modify (UH_WLOCK) - free state, if needed
806272840Smelifaro *
807272840Smelifaro * Returns 0 on success.
808272840Smelifaro */
809272840Smelifarostatic int
810272840Smelifarocheck_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
811272840Smelifaro    struct table_config *tc, struct table_info *ti, uint32_t count)
812272840Smelifaro{
813272840Smelifaro	struct table_algo *ta;
814272840Smelifaro	uint64_t pflags;
815272840Smelifaro	char ta_buf[TA_BUF_SZ];
816272840Smelifaro	int error;
817272840Smelifaro
818272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
819272840Smelifaro
820272840Smelifaro	error = 0;
821272840Smelifaro	ta = tc->ta;
822272840Smelifaro	if (ta->need_modify == NULL)
823272840Smelifaro		return (0);
824272840Smelifaro
825272840Smelifaro	/* Acquire reference not to loose @tc between locks/unlocks */
826272840Smelifaro	tc->no.refcnt++;
827272840Smelifaro
828272840Smelifaro	/*
829272840Smelifaro	 * TODO: think about avoiding race between large add/large delete
830272840Smelifaro	 * operation on algorithm which implements shrinking along with
831272840Smelifaro	 * growing.
832272840Smelifaro	 */
833272840Smelifaro	while (true) {
834272840Smelifaro		pflags = 0;
835272840Smelifaro		if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
836272840Smelifaro			error = 0;
837272840Smelifaro			break;
838232865Smelifaro		}
839232865Smelifaro
840272840Smelifaro		/* We have to shrink/grow table */
841272840Smelifaro		if (ts != NULL)
842272840Smelifaro			add_toperation_state(ch, ts);
843272840Smelifaro		IPFW_UH_WUNLOCK(ch);
844272840Smelifaro
845272840Smelifaro		memset(&ta_buf, 0, sizeof(ta_buf));
846272840Smelifaro		error = ta->prepare_mod(ta_buf, &pflags);
847272840Smelifaro
848272840Smelifaro		IPFW_UH_WLOCK(ch);
849272840Smelifaro		if (ts != NULL)
850272840Smelifaro			del_toperation_state(ch, ts);
851272840Smelifaro
852272840Smelifaro		if (error != 0)
853272840Smelifaro			break;
854272840Smelifaro
855272840Smelifaro		if (ts != NULL && ts->modified != 0) {
856272840Smelifaro
857272840Smelifaro			/*
858272840Smelifaro			 * Swap operation has happened
859272840Smelifaro			 * so we're currently operating on other
860272840Smelifaro			 * table data. Stop doing this.
861232865Smelifaro			 */
862272840Smelifaro			ta->flush_mod(ta_buf);
863272840Smelifaro			break;
864232865Smelifaro		}
865272840Smelifaro
866272840Smelifaro		/* Check if we still need to alter table */
867272840Smelifaro		ti = KIDX_TO_TI(ch, tc->no.kidx);
868272840Smelifaro		if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
869272840Smelifaro			IPFW_UH_WUNLOCK(ch);
870272840Smelifaro
871272840Smelifaro			/*
872272840Smelifaro			 * Other thread has already performed resize.
873272840Smelifaro			 * Flush our state and return.
874272840Smelifaro			 */
875272840Smelifaro			ta->flush_mod(ta_buf);
876272840Smelifaro			break;
877272840Smelifaro		}
878272840Smelifaro
879272840Smelifaro		error = ta->fill_mod(tc->astate, ti, ta_buf, &pflags);
880272840Smelifaro		if (error == 0) {
881272840Smelifaro			/* Do actual modification */
882272840Smelifaro			IPFW_WLOCK(ch);
883272840Smelifaro			ta->modify(tc->astate, ti, ta_buf, pflags);
884272840Smelifaro			IPFW_WUNLOCK(ch);
885272840Smelifaro		}
886272840Smelifaro
887272840Smelifaro		/* Anyway, flush data and retry */
888272840Smelifaro		ta->flush_mod(ta_buf);
889232865Smelifaro	}
890232865Smelifaro
891272840Smelifaro	tc->no.refcnt--;
892272840Smelifaro	return (error);
893272840Smelifaro}
894232865Smelifaro
895272840Smelifaro/*
896272840Smelifaro * Adds or deletes record in table.
897272840Smelifaro * Data layout (v0):
898272840Smelifaro * Request: [ ip_fw3_opheader ipfw_table_xentry ]
899272840Smelifaro *
900272840Smelifaro * Returns 0 on success
901272840Smelifaro */
902272840Smelifarostatic int
903272840Smelifaromanage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
904272840Smelifaro    struct sockopt_data *sd)
905272840Smelifaro{
906272840Smelifaro	ipfw_table_xentry *xent;
907272840Smelifaro	struct tentry_info tei;
908272840Smelifaro	struct tid_info ti;
909272840Smelifaro	struct table_value v;
910272840Smelifaro	int error, hdrlen, read;
911272840Smelifaro
912272840Smelifaro	hdrlen = offsetof(ipfw_table_xentry, k);
913272840Smelifaro
914272840Smelifaro	/* Check minimum header size */
915272840Smelifaro	if (sd->valsize < (sizeof(*op3) + hdrlen))
916272840Smelifaro		return (EINVAL);
917272840Smelifaro
918272840Smelifaro	read = sizeof(ip_fw3_opheader);
919272840Smelifaro
920272840Smelifaro	/* Check if xentry len field is valid */
921272840Smelifaro	xent = (ipfw_table_xentry *)(op3 + 1);
922272840Smelifaro	if (xent->len < hdrlen || xent->len + read > sd->valsize)
923272840Smelifaro		return (EINVAL);
924272840Smelifaro
925272840Smelifaro	memset(&tei, 0, sizeof(tei));
926272840Smelifaro	tei.paddr = &xent->k;
927272840Smelifaro	tei.masklen = xent->masklen;
928272840Smelifaro	ipfw_import_table_value_legacy(xent->value, &v);
929272840Smelifaro	tei.pvalue = &v;
930298995Spfg	/* Old requests compatibility */
931272840Smelifaro	tei.flags = TEI_FLAGS_COMPAT;
932272840Smelifaro	if (xent->type == IPFW_TABLE_ADDR) {
933272840Smelifaro		if (xent->len - hdrlen == sizeof(in_addr_t))
934272840Smelifaro			tei.subtype = AF_INET;
935272840Smelifaro		else
936272840Smelifaro			tei.subtype = AF_INET6;
937200590Sluigi	}
938272840Smelifaro
939272840Smelifaro	memset(&ti, 0, sizeof(ti));
940272840Smelifaro	ti.uidx = xent->tbl;
941272840Smelifaro	ti.type = xent->type;
942272840Smelifaro
943272840Smelifaro	error = (op3->opcode == IP_FW_TABLE_XADD) ?
944272840Smelifaro	    add_table_entry(ch, &ti, &tei, 0, 1) :
945272840Smelifaro	    del_table_entry(ch, &ti, &tei, 0, 1);
946272840Smelifaro
947272840Smelifaro	return (error);
948200590Sluigi}
949200590Sluigi
950272840Smelifaro/*
951272840Smelifaro * Adds or deletes record in table.
952272840Smelifaro * Data layout (v1)(current):
953272840Smelifaro * Request: [ ipfw_obj_header
954272840Smelifaro *   ipfw_obj_ctlv(IPFW_TLV_TBLENT_LIST) [ ipfw_obj_tentry x N ]
955272840Smelifaro * ]
956272840Smelifaro *
957272840Smelifaro * Returns 0 on success
958272840Smelifaro */
959272840Smelifarostatic int
960272840Smelifaromanage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
961272840Smelifaro    struct sockopt_data *sd)
962200590Sluigi{
963272840Smelifaro	ipfw_obj_tentry *tent, *ptent;
964272840Smelifaro	ipfw_obj_ctlv *ctlv;
965272840Smelifaro	ipfw_obj_header *oh;
966272840Smelifaro	struct tentry_info *ptei, tei, *tei_buf;
967272840Smelifaro	struct tid_info ti;
968272840Smelifaro	int error, i, kidx, read;
969200590Sluigi
970272840Smelifaro	/* Check minimum header size */
971272840Smelifaro	if (sd->valsize < (sizeof(*oh) + sizeof(*ctlv)))
972200590Sluigi		return (EINVAL);
973232865Smelifaro
974272840Smelifaro	/* Check if passed data is too long */
975272840Smelifaro	if (sd->valsize != sd->kavail)
976272840Smelifaro		return (EINVAL);
977232865Smelifaro
978272840Smelifaro	oh = (ipfw_obj_header *)sd->kbuf;
979272840Smelifaro
980272840Smelifaro	/* Basic length checks for TLVs */
981272840Smelifaro	if (oh->ntlv.head.length != sizeof(oh->ntlv))
982272840Smelifaro		return (EINVAL);
983272840Smelifaro
984272840Smelifaro	read = sizeof(*oh);
985272840Smelifaro
986272840Smelifaro	ctlv = (ipfw_obj_ctlv *)(oh + 1);
987272840Smelifaro	if (ctlv->head.length + read != sd->valsize)
988272840Smelifaro		return (EINVAL);
989272840Smelifaro
990272840Smelifaro	read += sizeof(*ctlv);
991272840Smelifaro	tent = (ipfw_obj_tentry *)(ctlv + 1);
992272840Smelifaro	if (ctlv->count * sizeof(*tent) + read != sd->valsize)
993272840Smelifaro		return (EINVAL);
994272840Smelifaro
995272840Smelifaro	if (ctlv->count == 0)
996272840Smelifaro		return (0);
997272840Smelifaro
998272840Smelifaro	/*
999272840Smelifaro	 * Mark entire buffer as "read".
1000272840Smelifaro	 * This instructs sopt api write it back
1001272840Smelifaro	 * after function return.
1002272840Smelifaro	 */
1003272840Smelifaro	ipfw_get_sopt_header(sd, sd->valsize);
1004272840Smelifaro
1005272840Smelifaro	/* Perform basic checks for each entry */
1006272840Smelifaro	ptent = tent;
1007272840Smelifaro	kidx = tent->idx;
1008272840Smelifaro	for (i = 0; i < ctlv->count; i++, ptent++) {
1009272840Smelifaro		if (ptent->head.length != sizeof(*ptent))
1010232865Smelifaro			return (EINVAL);
1011272840Smelifaro		if (ptent->idx != kidx)
1012272840Smelifaro			return (ENOTSUP);
1013272840Smelifaro	}
1014232865Smelifaro
1015272840Smelifaro	/* Convert data into kernel request objects */
1016272840Smelifaro	objheader_to_ti(oh, &ti);
1017272840Smelifaro	ti.type = oh->ntlv.type;
1018272840Smelifaro	ti.uidx = kidx;
1019232865Smelifaro
1020272840Smelifaro	/* Use on-stack buffer for single add/del */
1021272840Smelifaro	if (ctlv->count == 1) {
1022272840Smelifaro		memset(&tei, 0, sizeof(tei));
1023272840Smelifaro		tei_buf = &tei;
1024272840Smelifaro	} else
1025272840Smelifaro		tei_buf = malloc(ctlv->count * sizeof(tei), M_TEMP,
1026272840Smelifaro		    M_WAITOK | M_ZERO);
1027238265Smelifaro
1028272840Smelifaro	ptei = tei_buf;
1029272840Smelifaro	ptent = tent;
1030272840Smelifaro	for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1031272840Smelifaro		ptei->paddr = &ptent->k;
1032272840Smelifaro		ptei->subtype = ptent->subtype;
1033272840Smelifaro		ptei->masklen = ptent->masklen;
1034272840Smelifaro		if (ptent->head.flags & IPFW_TF_UPDATE)
1035272840Smelifaro			ptei->flags |= TEI_FLAGS_UPDATE;
1036232865Smelifaro
1037272840Smelifaro		ipfw_import_table_value_v1(&ptent->v.value);
1038272840Smelifaro		ptei->pvalue = (struct table_value *)&ptent->v.value;
1039272840Smelifaro	}
1040232865Smelifaro
1041272840Smelifaro	error = (oh->opheader.opcode == IP_FW_TABLE_XADD) ?
1042272840Smelifaro	    add_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count) :
1043272840Smelifaro	    del_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count);
1044272840Smelifaro
1045272840Smelifaro	/* Translate result back to userland */
1046272840Smelifaro	ptei = tei_buf;
1047272840Smelifaro	ptent = tent;
1048272840Smelifaro	for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1049272840Smelifaro		if (ptei->flags & TEI_FLAGS_ADDED)
1050272840Smelifaro			ptent->result = IPFW_TR_ADDED;
1051272840Smelifaro		else if (ptei->flags & TEI_FLAGS_DELETED)
1052272840Smelifaro			ptent->result = IPFW_TR_DELETED;
1053272840Smelifaro		else if (ptei->flags & TEI_FLAGS_UPDATED)
1054272840Smelifaro			ptent->result = IPFW_TR_UPDATED;
1055272840Smelifaro		else if (ptei->flags & TEI_FLAGS_LIMIT)
1056272840Smelifaro			ptent->result = IPFW_TR_LIMIT;
1057272840Smelifaro		else if (ptei->flags & TEI_FLAGS_ERROR)
1058272840Smelifaro			ptent->result = IPFW_TR_ERROR;
1059272840Smelifaro		else if (ptei->flags & TEI_FLAGS_NOTFOUND)
1060272840Smelifaro			ptent->result = IPFW_TR_NOTFOUND;
1061272840Smelifaro		else if (ptei->flags & TEI_FLAGS_EXISTS)
1062272840Smelifaro			ptent->result = IPFW_TR_EXISTS;
1063272840Smelifaro		ipfw_export_table_value_v1(ptei->pvalue, &ptent->v.value);
1064232865Smelifaro	}
1065232865Smelifaro
1066272840Smelifaro	if (tei_buf != &tei)
1067272840Smelifaro		free(tei_buf, M_TEMP);
1068272840Smelifaro
1069272840Smelifaro	return (error);
1070272840Smelifaro}
1071272840Smelifaro
1072272840Smelifaro/*
1073272840Smelifaro * Looks up an entry in given table.
1074272840Smelifaro * Data layout (v0)(current):
1075272840Smelifaro * Request: [ ipfw_obj_header ipfw_obj_tentry ]
1076272840Smelifaro * Reply: [ ipfw_obj_header ipfw_obj_tentry ]
1077272840Smelifaro *
1078272840Smelifaro * Returns 0 on success
1079272840Smelifaro */
1080272840Smelifarostatic int
1081272840Smelifarofind_table_entry(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1082272840Smelifaro    struct sockopt_data *sd)
1083272840Smelifaro{
1084272840Smelifaro	ipfw_obj_tentry *tent;
1085272840Smelifaro	ipfw_obj_header *oh;
1086272840Smelifaro	struct tid_info ti;
1087272840Smelifaro	struct table_config *tc;
1088272840Smelifaro	struct table_algo *ta;
1089272840Smelifaro	struct table_info *kti;
1090272840Smelifaro	struct namedobj_instance *ni;
1091272840Smelifaro	int error;
1092272840Smelifaro	size_t sz;
1093272840Smelifaro
1094272840Smelifaro	/* Check minimum header size */
1095272840Smelifaro	sz = sizeof(*oh) + sizeof(*tent);
1096272840Smelifaro	if (sd->valsize != sz)
1097272840Smelifaro		return (EINVAL);
1098272840Smelifaro
1099272840Smelifaro	oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1100272840Smelifaro	tent = (ipfw_obj_tentry *)(oh + 1);
1101272840Smelifaro
1102272840Smelifaro	/* Basic length checks for TLVs */
1103272840Smelifaro	if (oh->ntlv.head.length != sizeof(oh->ntlv))
1104272840Smelifaro		return (EINVAL);
1105272840Smelifaro
1106272840Smelifaro	objheader_to_ti(oh, &ti);
1107272840Smelifaro	ti.type = oh->ntlv.type;
1108272840Smelifaro	ti.uidx = tent->idx;
1109272840Smelifaro
1110272840Smelifaro	IPFW_UH_RLOCK(ch);
1111272840Smelifaro	ni = CHAIN_TO_NI(ch);
1112272840Smelifaro
1113272840Smelifaro	/*
1114272840Smelifaro	 * Find existing table and check its type .
1115272840Smelifaro	 */
1116272840Smelifaro	ta = NULL;
1117272840Smelifaro	if ((tc = find_table(ni, &ti)) == NULL) {
1118272840Smelifaro		IPFW_UH_RUNLOCK(ch);
1119200590Sluigi		return (ESRCH);
1120200590Sluigi	}
1121232865Smelifaro
1122272840Smelifaro	/* check table type */
1123282070Smelifaro	if (tc->no.subtype != ti.type) {
1124272840Smelifaro		IPFW_UH_RUNLOCK(ch);
1125232865Smelifaro		return (EINVAL);
1126232865Smelifaro	}
1127232865Smelifaro
1128272840Smelifaro	kti = KIDX_TO_TI(ch, tc->no.kidx);
1129272840Smelifaro	ta = tc->ta;
1130232865Smelifaro
1131272840Smelifaro	if (ta->find_tentry == NULL)
1132272840Smelifaro		return (ENOTSUP);
1133232865Smelifaro
1134272840Smelifaro	error = ta->find_tentry(tc->astate, kti, tent);
1135272840Smelifaro
1136272840Smelifaro	IPFW_UH_RUNLOCK(ch);
1137272840Smelifaro
1138272840Smelifaro	return (error);
1139200590Sluigi}
1140200590Sluigi
1141272840Smelifaro/*
1142272840Smelifaro * Flushes all entries or destroys given table.
1143272840Smelifaro * Data layout (v0)(current):
1144272840Smelifaro * Request: [ ipfw_obj_header ]
1145272840Smelifaro *
1146272840Smelifaro * Returns 0 on success
1147272840Smelifaro */
1148200590Sluigistatic int
1149272840Smelifaroflush_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1150272840Smelifaro    struct sockopt_data *sd)
1151200590Sluigi{
1152272840Smelifaro	int error;
1153272840Smelifaro	struct _ipfw_obj_header *oh;
1154272840Smelifaro	struct tid_info ti;
1155200590Sluigi
1156272840Smelifaro	if (sd->valsize != sizeof(*oh))
1157272840Smelifaro		return (EINVAL);
1158272840Smelifaro
1159272840Smelifaro	oh = (struct _ipfw_obj_header *)op3;
1160272840Smelifaro	objheader_to_ti(oh, &ti);
1161272840Smelifaro
1162272840Smelifaro	if (op3->opcode == IP_FW_TABLE_XDESTROY)
1163272840Smelifaro		error = destroy_table(ch, &ti);
1164272840Smelifaro	else if (op3->opcode == IP_FW_TABLE_XFLUSH)
1165272840Smelifaro		error = flush_table(ch, &ti);
1166272840Smelifaro	else
1167272840Smelifaro		return (ENOTSUP);
1168272840Smelifaro
1169272840Smelifaro	return (error);
1170200590Sluigi}
1171200590Sluigi
1172272840Smelifarostatic void
1173272840Smelifarorestart_flush(void *object, struct op_state *_state)
1174272840Smelifaro{
1175272840Smelifaro	struct tableop_state *ts;
1176272840Smelifaro
1177272840Smelifaro	ts = (struct tableop_state *)_state;
1178272840Smelifaro
1179272840Smelifaro	if (ts->tc != object)
1180272840Smelifaro		return;
1181272840Smelifaro
1182272840Smelifaro	/* Indicate we've called */
1183272840Smelifaro	ts->modified = 1;
1184272840Smelifaro}
1185272840Smelifaro
1186272840Smelifaro/*
1187272840Smelifaro * Flushes given table.
1188272840Smelifaro *
1189272840Smelifaro * Function create new table instance with the same
1190272840Smelifaro * parameters, swaps it with old one and
1191272840Smelifaro * flushes state without holding runtime WLOCK.
1192272840Smelifaro *
1193272840Smelifaro * Returns 0 on success.
1194272840Smelifaro */
1195200590Sluigiint
1196272840Smelifaroflush_table(struct ip_fw_chain *ch, struct tid_info *ti)
1197200590Sluigi{
1198272840Smelifaro	struct namedobj_instance *ni;
1199272840Smelifaro	struct table_config *tc;
1200272840Smelifaro	struct table_algo *ta;
1201272840Smelifaro	struct table_info ti_old, ti_new, *tablestate;
1202272840Smelifaro	void *astate_old, *astate_new;
1203272840Smelifaro	char algostate[64], *pstate;
1204272840Smelifaro	struct tableop_state ts;
1205278259Smelifaro	int error, need_gc;
1206272840Smelifaro	uint16_t kidx;
1207272840Smelifaro	uint8_t tflags;
1208200590Sluigi
1209272840Smelifaro	/*
1210298995Spfg	 * Stage 1: save table algorithm.
1211272840Smelifaro	 * Reference found table to ensure it won't disappear.
1212272840Smelifaro	 */
1213272840Smelifaro	IPFW_UH_WLOCK(ch);
1214272840Smelifaro	ni = CHAIN_TO_NI(ch);
1215272840Smelifaro	if ((tc = find_table(ni, ti)) == NULL) {
1216272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1217272840Smelifaro		return (ESRCH);
1218272840Smelifaro	}
1219278259Smelifaro	need_gc = 0;
1220278259Smelifaro	astate_new = NULL;
1221278259Smelifaro	memset(&ti_new, 0, sizeof(ti_new));
1222272840Smelifarorestart:
1223272840Smelifaro	/* Set up swap handler */
1224272840Smelifaro	memset(&ts, 0, sizeof(ts));
1225272840Smelifaro	ts.opstate.func = restart_flush;
1226272840Smelifaro	ts.tc = tc;
1227200590Sluigi
1228272840Smelifaro	ta = tc->ta;
1229272840Smelifaro	/* Do not flush readonly tables */
1230272840Smelifaro	if ((ta->flags & TA_FLAG_READONLY) != 0) {
1231272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1232272840Smelifaro		return (EACCES);
1233272840Smelifaro	}
1234272840Smelifaro	/* Save startup algo parameters */
1235272840Smelifaro	if (ta->print_config != NULL) {
1236272840Smelifaro		ta->print_config(tc->astate, KIDX_TO_TI(ch, tc->no.kidx),
1237272840Smelifaro		    algostate, sizeof(algostate));
1238272840Smelifaro		pstate = algostate;
1239272840Smelifaro	} else
1240272840Smelifaro		pstate = NULL;
1241272840Smelifaro	tflags = tc->tflags;
1242272840Smelifaro	tc->no.refcnt++;
1243272840Smelifaro	add_toperation_state(ch, &ts);
1244272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1245272840Smelifaro
1246232865Smelifaro	/*
1247278259Smelifaro	 * Stage 1.5: if this is not the first attempt, destroy previous state
1248278259Smelifaro	 */
1249278259Smelifaro	if (need_gc != 0) {
1250278259Smelifaro		ta->destroy(astate_new, &ti_new);
1251278259Smelifaro		need_gc = 0;
1252278259Smelifaro	}
1253278259Smelifaro
1254278259Smelifaro	/*
1255272840Smelifaro	 * Stage 2: allocate new table instance using same algo.
1256232865Smelifaro	 */
1257272840Smelifaro	memset(&ti_new, 0, sizeof(struct table_info));
1258272840Smelifaro	error = ta->init(ch, &astate_new, &ti_new, pstate, tflags);
1259232865Smelifaro
1260272840Smelifaro	/*
1261272840Smelifaro	 * Stage 3: swap old state pointers with newly-allocated ones.
1262272840Smelifaro	 * Decrease refcount.
1263272840Smelifaro	 */
1264272840Smelifaro	IPFW_UH_WLOCK(ch);
1265272840Smelifaro	tc->no.refcnt--;
1266272840Smelifaro	del_toperation_state(ch, &ts);
1267232865Smelifaro
1268272840Smelifaro	if (error != 0) {
1269272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1270272840Smelifaro		return (error);
1271232865Smelifaro	}
1272232865Smelifaro
1273272840Smelifaro	/*
1274272840Smelifaro	 * Restart operation if table swap has happened:
1275272840Smelifaro	 * even if algo may be the same, algo init parameters
1276272840Smelifaro	 * may change. Restart operation instead of doing
1277272840Smelifaro	 * complex checks.
1278272840Smelifaro	 */
1279272840Smelifaro	if (ts.modified != 0) {
1280278259Smelifaro		/* Delay destroying data since we're holding UH lock */
1281278259Smelifaro		need_gc = 1;
1282272840Smelifaro		goto restart;
1283232865Smelifaro	}
1284232865Smelifaro
1285272840Smelifaro	ni = CHAIN_TO_NI(ch);
1286272840Smelifaro	kidx = tc->no.kidx;
1287272840Smelifaro	tablestate = (struct table_info *)ch->tablestate;
1288272840Smelifaro
1289272840Smelifaro	IPFW_WLOCK(ch);
1290272840Smelifaro	ti_old = tablestate[kidx];
1291272840Smelifaro	tablestate[kidx] = ti_new;
1292272840Smelifaro	IPFW_WUNLOCK(ch);
1293272840Smelifaro
1294272840Smelifaro	astate_old = tc->astate;
1295272840Smelifaro	tc->astate = astate_new;
1296272840Smelifaro	tc->ti_copy = ti_new;
1297272840Smelifaro	tc->count = 0;
1298272840Smelifaro
1299272840Smelifaro	/* Notify algo on real @ti address */
1300272840Smelifaro	if (ta->change_ti != NULL)
1301272840Smelifaro		ta->change_ti(tc->astate, &tablestate[kidx]);
1302272840Smelifaro
1303272840Smelifaro	/*
1304272840Smelifaro	 * Stage 4: unref values.
1305272840Smelifaro	 */
1306272840Smelifaro	ipfw_unref_table_values(ch, tc, ta, astate_old, &ti_old);
1307272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1308272840Smelifaro
1309272840Smelifaro	/*
1310272840Smelifaro	 * Stage 5: perform real flush/destroy.
1311272840Smelifaro	 */
1312272840Smelifaro	ta->destroy(astate_old, &ti_old);
1313272840Smelifaro
1314200590Sluigi	return (0);
1315200590Sluigi}
1316200590Sluigi
1317272840Smelifaro/*
1318272840Smelifaro * Swaps two tables.
1319272840Smelifaro * Data layout (v0)(current):
1320272840Smelifaro * Request: [ ipfw_obj_header ipfw_obj_ntlv ]
1321272840Smelifaro *
1322272840Smelifaro * Returns 0 on success
1323272840Smelifaro */
1324272840Smelifarostatic int
1325272840Smelifaroswap_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1326272840Smelifaro    struct sockopt_data *sd)
1327200590Sluigi{
1328272840Smelifaro	int error;
1329272840Smelifaro	struct _ipfw_obj_header *oh;
1330272840Smelifaro	struct tid_info ti_a, ti_b;
1331200590Sluigi
1332272840Smelifaro	if (sd->valsize != sizeof(*oh) + sizeof(ipfw_obj_ntlv))
1333272840Smelifaro		return (EINVAL);
1334200590Sluigi
1335272840Smelifaro	oh = (struct _ipfw_obj_header *)op3;
1336272840Smelifaro	ntlv_to_ti(&oh->ntlv, &ti_a);
1337272840Smelifaro	ntlv_to_ti((ipfw_obj_ntlv *)(oh + 1), &ti_b);
1338272840Smelifaro
1339272840Smelifaro	error = swap_tables(ch, &ti_a, &ti_b);
1340272840Smelifaro
1341272840Smelifaro	return (error);
1342200590Sluigi}
1343200590Sluigi
1344272840Smelifaro/*
1345272840Smelifaro * Swaps two tables of the same type/valtype.
1346272840Smelifaro *
1347272840Smelifaro * Checks if tables are compatible and limits
1348272840Smelifaro * permits swap, than actually perform swap.
1349272840Smelifaro *
1350272840Smelifaro * Each table consists of 2 different parts:
1351272840Smelifaro * config:
1352272840Smelifaro *   @tc (with name, set, kidx) and rule bindings, which is "stable".
1353272840Smelifaro *   number of items
1354272840Smelifaro *   table algo
1355272840Smelifaro * runtime:
1356272840Smelifaro *   runtime data @ti (ch->tablestate)
1357272840Smelifaro *   runtime cache in @tc
1358272840Smelifaro *   algo-specific data (@tc->astate)
1359272840Smelifaro *
1360272840Smelifaro * So we switch:
1361272840Smelifaro *  all runtime data
1362272840Smelifaro *   number of items
1363272840Smelifaro *   table algo
1364272840Smelifaro *
1365272840Smelifaro * After that we call @ti change handler for each table.
1366272840Smelifaro *
1367272840Smelifaro * Note that referencing @tc won't protect tc->ta from change.
1368272840Smelifaro * XXX: Do we need to restrict swap between locked tables?
1369272840Smelifaro * XXX: Do we need to exchange ftype?
1370272840Smelifaro *
1371272840Smelifaro * Returns 0 on success.
1372272840Smelifaro */
1373272840Smelifarostatic int
1374272840Smelifaroswap_tables(struct ip_fw_chain *ch, struct tid_info *a,
1375272840Smelifaro    struct tid_info *b)
1376232865Smelifaro{
1377272840Smelifaro	struct namedobj_instance *ni;
1378272840Smelifaro	struct table_config *tc_a, *tc_b;
1379272840Smelifaro	struct table_algo *ta;
1380272840Smelifaro	struct table_info ti, *tablestate;
1381272840Smelifaro	void *astate;
1382272840Smelifaro	uint32_t count;
1383272840Smelifaro
1384272840Smelifaro	/*
1385272840Smelifaro	 * Stage 1: find both tables and ensure they are of
1386272840Smelifaro	 * the same type.
1387272840Smelifaro	 */
1388272840Smelifaro	IPFW_UH_WLOCK(ch);
1389272840Smelifaro	ni = CHAIN_TO_NI(ch);
1390272840Smelifaro	if ((tc_a = find_table(ni, a)) == NULL) {
1391272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1392272840Smelifaro		return (ESRCH);
1393272840Smelifaro	}
1394272840Smelifaro	if ((tc_b = find_table(ni, b)) == NULL) {
1395272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1396272840Smelifaro		return (ESRCH);
1397272840Smelifaro	}
1398272840Smelifaro
1399272840Smelifaro	/* It is very easy to swap between the same table */
1400272840Smelifaro	if (tc_a == tc_b) {
1401272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1402272840Smelifaro		return (0);
1403272840Smelifaro	}
1404272840Smelifaro
1405272840Smelifaro	/* Check type and value are the same */
1406282070Smelifaro	if (tc_a->no.subtype!=tc_b->no.subtype || tc_a->tflags!=tc_b->tflags) {
1407272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1408272840Smelifaro		return (EINVAL);
1409272840Smelifaro	}
1410272840Smelifaro
1411272840Smelifaro	/* Check limits before swap */
1412272840Smelifaro	if ((tc_a->limit != 0 && tc_b->count > tc_a->limit) ||
1413272840Smelifaro	    (tc_b->limit != 0 && tc_a->count > tc_b->limit)) {
1414272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1415272840Smelifaro		return (EFBIG);
1416272840Smelifaro	}
1417272840Smelifaro
1418272840Smelifaro	/* Check if one of the tables is readonly */
1419272840Smelifaro	if (((tc_a->ta->flags | tc_b->ta->flags) & TA_FLAG_READONLY) != 0) {
1420272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1421272840Smelifaro		return (EACCES);
1422272840Smelifaro	}
1423272840Smelifaro
1424272840Smelifaro	/* Notify we're going to swap */
1425272840Smelifaro	rollback_toperation_state(ch, tc_a);
1426272840Smelifaro	rollback_toperation_state(ch, tc_b);
1427272840Smelifaro
1428272840Smelifaro	/* Everything is fine, prepare to swap */
1429272840Smelifaro	tablestate = (struct table_info *)ch->tablestate;
1430272840Smelifaro	ti = tablestate[tc_a->no.kidx];
1431272840Smelifaro	ta = tc_a->ta;
1432272840Smelifaro	astate = tc_a->astate;
1433272840Smelifaro	count = tc_a->count;
1434272840Smelifaro
1435272840Smelifaro	IPFW_WLOCK(ch);
1436272840Smelifaro	/* a <- b */
1437272840Smelifaro	tablestate[tc_a->no.kidx] = tablestate[tc_b->no.kidx];
1438272840Smelifaro	tc_a->ta = tc_b->ta;
1439272840Smelifaro	tc_a->astate = tc_b->astate;
1440272840Smelifaro	tc_a->count = tc_b->count;
1441272840Smelifaro	/* b <- a */
1442272840Smelifaro	tablestate[tc_b->no.kidx] = ti;
1443272840Smelifaro	tc_b->ta = ta;
1444272840Smelifaro	tc_b->astate = astate;
1445272840Smelifaro	tc_b->count = count;
1446272840Smelifaro	IPFW_WUNLOCK(ch);
1447272840Smelifaro
1448272840Smelifaro	/* Ensure tc.ti copies are in sync */
1449272840Smelifaro	tc_a->ti_copy = tablestate[tc_a->no.kidx];
1450272840Smelifaro	tc_b->ti_copy = tablestate[tc_b->no.kidx];
1451272840Smelifaro
1452272840Smelifaro	/* Notify both tables on @ti change */
1453272840Smelifaro	if (tc_a->ta->change_ti != NULL)
1454272840Smelifaro		tc_a->ta->change_ti(tc_a->astate, &tablestate[tc_a->no.kidx]);
1455272840Smelifaro	if (tc_b->ta->change_ti != NULL)
1456272840Smelifaro		tc_b->ta->change_ti(tc_b->astate, &tablestate[tc_b->no.kidx]);
1457272840Smelifaro
1458272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1459272840Smelifaro
1460200590Sluigi	return (0);
1461200590Sluigi}
1462200590Sluigi
1463272840Smelifaro/*
1464272840Smelifaro * Destroys table specified by @ti.
1465272840Smelifaro * Data layout (v0)(current):
1466272840Smelifaro * Request: [ ip_fw3_opheader ]
1467272840Smelifaro *
1468272840Smelifaro * Returns 0 on success
1469272840Smelifaro */
1470272840Smelifarostatic int
1471272840Smelifarodestroy_table(struct ip_fw_chain *ch, struct tid_info *ti)
1472272840Smelifaro{
1473272840Smelifaro	struct namedobj_instance *ni;
1474272840Smelifaro	struct table_config *tc;
1475272840Smelifaro
1476272840Smelifaro	IPFW_UH_WLOCK(ch);
1477272840Smelifaro
1478272840Smelifaro	ni = CHAIN_TO_NI(ch);
1479272840Smelifaro	if ((tc = find_table(ni, ti)) == NULL) {
1480272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1481272840Smelifaro		return (ESRCH);
1482272840Smelifaro	}
1483272840Smelifaro
1484272840Smelifaro	/* Do not permit destroying referenced tables */
1485272840Smelifaro	if (tc->no.refcnt > 0) {
1486272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1487272840Smelifaro		return (EBUSY);
1488272840Smelifaro	}
1489272840Smelifaro
1490272840Smelifaro	IPFW_WLOCK(ch);
1491272840Smelifaro	unlink_table(ch, tc);
1492272840Smelifaro	IPFW_WUNLOCK(ch);
1493272840Smelifaro
1494272840Smelifaro	/* Free obj index */
1495272840Smelifaro	if (ipfw_objhash_free_idx(ni, tc->no.kidx) != 0)
1496272840Smelifaro		printf("Error unlinking kidx %d from table %s\n",
1497272840Smelifaro		    tc->no.kidx, tc->tablename);
1498272840Smelifaro
1499272840Smelifaro	/* Unref values used in tables while holding UH lock */
1500272840Smelifaro	ipfw_unref_table_values(ch, tc, tc->ta, tc->astate, &tc->ti_copy);
1501272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1502272840Smelifaro
1503272840Smelifaro	free_table_config(ni, tc);
1504272840Smelifaro
1505272840Smelifaro	return (0);
1506272840Smelifaro}
1507272840Smelifaro
1508273274Smelifarostatic uint32_t
1509273274Smelifaroroundup2p(uint32_t v)
1510273274Smelifaro{
1511273274Smelifaro
1512273274Smelifaro	v--;
1513273274Smelifaro	v |= v >> 1;
1514273274Smelifaro	v |= v >> 2;
1515273274Smelifaro	v |= v >> 4;
1516273274Smelifaro	v |= v >> 8;
1517273274Smelifaro	v |= v >> 16;
1518273274Smelifaro	v++;
1519273274Smelifaro
1520273274Smelifaro	return (v);
1521273274Smelifaro}
1522273274Smelifaro
1523272840Smelifaro/*
1524272840Smelifaro * Grow tables index.
1525272840Smelifaro *
1526272840Smelifaro * Returns 0 on success.
1527272840Smelifaro */
1528200590Sluigiint
1529233478Smelifaroipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables)
1530233478Smelifaro{
1531233478Smelifaro	unsigned int ntables_old, tbl;
1532272840Smelifaro	struct namedobj_instance *ni;
1533272840Smelifaro	void *new_idx, *old_tablestate, *tablestate;
1534272840Smelifaro	struct table_info *ti;
1535272840Smelifaro	struct table_config *tc;
1536272840Smelifaro	int i, new_blocks;
1537233478Smelifaro
1538233478Smelifaro	/* Check new value for validity */
1539273274Smelifaro	if (ntables == 0)
1540273274Smelifaro		return (EINVAL);
1541233478Smelifaro	if (ntables > IPFW_TABLES_MAX)
1542233478Smelifaro		ntables = IPFW_TABLES_MAX;
1543273274Smelifaro	/* Alight to nearest power of 2 */
1544273274Smelifaro	ntables = (unsigned int)roundup2p(ntables);
1545233478Smelifaro
1546233478Smelifaro	/* Allocate new pointers */
1547272840Smelifaro	tablestate = malloc(ntables * sizeof(struct table_info),
1548272840Smelifaro	    M_IPFW, M_WAITOK | M_ZERO);
1549233478Smelifaro
1550272840Smelifaro	ipfw_objhash_bitmap_alloc(ntables, (void *)&new_idx, &new_blocks);
1551233478Smelifaro
1552272840Smelifaro	IPFW_UH_WLOCK(ch);
1553272840Smelifaro
1554233478Smelifaro	tbl = (ntables >= V_fw_tables_max) ? V_fw_tables_max : ntables;
1555272840Smelifaro	ni = CHAIN_TO_NI(ch);
1556233478Smelifaro
1557272840Smelifaro	/* Temporary restrict decreasing max_tables */
1558272840Smelifaro	if (ntables < V_fw_tables_max) {
1559233478Smelifaro
1560272840Smelifaro		/*
1561272840Smelifaro		 * FIXME: Check if we really can shrink
1562272840Smelifaro		 */
1563272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1564272840Smelifaro		return (EINVAL);
1565272840Smelifaro	}
1566233478Smelifaro
1567272840Smelifaro	/* Copy table info/indices */
1568272840Smelifaro	memcpy(tablestate, ch->tablestate, sizeof(struct table_info) * tbl);
1569272840Smelifaro	ipfw_objhash_bitmap_merge(ni, &new_idx, &new_blocks);
1570272840Smelifaro
1571272840Smelifaro	IPFW_WLOCK(ch);
1572272840Smelifaro
1573272840Smelifaro	/* Change pointers */
1574272840Smelifaro	old_tablestate = ch->tablestate;
1575272840Smelifaro	ch->tablestate = tablestate;
1576272840Smelifaro	ipfw_objhash_bitmap_swap(ni, &new_idx, &new_blocks);
1577272840Smelifaro
1578233478Smelifaro	ntables_old = V_fw_tables_max;
1579233478Smelifaro	V_fw_tables_max = ntables;
1580233478Smelifaro
1581233478Smelifaro	IPFW_WUNLOCK(ch);
1582233478Smelifaro
1583272840Smelifaro	/* Notify all consumers that their @ti pointer has changed */
1584272840Smelifaro	ti = (struct table_info *)ch->tablestate;
1585272840Smelifaro	for (i = 0; i < tbl; i++, ti++) {
1586272840Smelifaro		if (ti->lookup == NULL)
1587272840Smelifaro			continue;
1588272840Smelifaro		tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, i);
1589272840Smelifaro		if (tc == NULL || tc->ta->change_ti == NULL)
1590272840Smelifaro			continue;
1591272840Smelifaro
1592272840Smelifaro		tc->ta->change_ti(tc->astate, ti);
1593272840Smelifaro	}
1594272840Smelifaro
1595272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1596272840Smelifaro
1597272840Smelifaro	/* Free old pointers */
1598272840Smelifaro	free(old_tablestate, M_IPFW);
1599272840Smelifaro	ipfw_objhash_bitmap_free(new_idx, new_blocks);
1600272840Smelifaro
1601272840Smelifaro	return (0);
1602272840Smelifaro}
1603272840Smelifaro
1604272840Smelifaro/*
1605272840Smelifaro * Lookup an IP @addr in table @tbl.
1606272840Smelifaro * Stores found value in @val.
1607272840Smelifaro *
1608272840Smelifaro * Returns 1 if @addr was found.
1609272840Smelifaro */
1610233478Smelifaroint
1611200590Sluigiipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
1612200590Sluigi    uint32_t *val)
1613200590Sluigi{
1614272840Smelifaro	struct table_info *ti;
1615200590Sluigi
1616272840Smelifaro	ti = KIDX_TO_TI(ch, tbl);
1617272840Smelifaro
1618272840Smelifaro	return (ti->lookup(ti, &addr, sizeof(in_addr_t), val));
1619272840Smelifaro}
1620272840Smelifaro
1621272840Smelifaro/*
1622272840Smelifaro * Lookup an arbtrary key @paddr of legth @plen in table @tbl.
1623272840Smelifaro * Stores found value in @val.
1624272840Smelifaro *
1625272840Smelifaro * Returns 1 if key was found.
1626272840Smelifaro */
1627272840Smelifaroint
1628272840Smelifaroipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
1629272840Smelifaro    void *paddr, uint32_t *val)
1630272840Smelifaro{
1631272840Smelifaro	struct table_info *ti;
1632272840Smelifaro
1633272840Smelifaro	ti = KIDX_TO_TI(ch, tbl);
1634272840Smelifaro
1635272840Smelifaro	return (ti->lookup(ti, paddr, plen, val));
1636272840Smelifaro}
1637272840Smelifaro
1638272840Smelifaro/*
1639272840Smelifaro * Info/List/dump support for tables.
1640272840Smelifaro *
1641272840Smelifaro */
1642272840Smelifaro
1643272840Smelifaro/*
1644272840Smelifaro * High-level 'get' cmds sysctl handlers
1645272840Smelifaro */
1646272840Smelifaro
1647272840Smelifaro/*
1648272840Smelifaro * Lists all tables currently available in kernel.
1649272840Smelifaro * Data layout (v0)(current):
1650272840Smelifaro * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
1651272840Smelifaro * Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
1652272840Smelifaro *
1653272840Smelifaro * Returns 0 on success
1654272840Smelifaro */
1655272840Smelifarostatic int
1656272840Smelifarolist_tables(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1657272840Smelifaro    struct sockopt_data *sd)
1658272840Smelifaro{
1659272840Smelifaro	struct _ipfw_obj_lheader *olh;
1660272840Smelifaro	int error;
1661272840Smelifaro
1662272840Smelifaro	olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
1663272840Smelifaro	if (olh == NULL)
1664272840Smelifaro		return (EINVAL);
1665272840Smelifaro	if (sd->valsize < olh->size)
1666272840Smelifaro		return (EINVAL);
1667272840Smelifaro
1668272840Smelifaro	IPFW_UH_RLOCK(ch);
1669272840Smelifaro	error = export_tables(ch, olh, sd);
1670272840Smelifaro	IPFW_UH_RUNLOCK(ch);
1671272840Smelifaro
1672272840Smelifaro	return (error);
1673272840Smelifaro}
1674272840Smelifaro
1675272840Smelifaro/*
1676272840Smelifaro * Store table info to buffer provided by @sd.
1677272840Smelifaro * Data layout (v0)(current):
1678272840Smelifaro * Request: [ ipfw_obj_header ipfw_xtable_info(empty)]
1679272840Smelifaro * Reply: [ ipfw_obj_header ipfw_xtable_info ]
1680272840Smelifaro *
1681272840Smelifaro * Returns 0 on success.
1682272840Smelifaro */
1683272840Smelifarostatic int
1684272840Smelifarodescribe_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1685272840Smelifaro    struct sockopt_data *sd)
1686272840Smelifaro{
1687272840Smelifaro	struct _ipfw_obj_header *oh;
1688272840Smelifaro	struct table_config *tc;
1689272840Smelifaro	struct tid_info ti;
1690272840Smelifaro	size_t sz;
1691272840Smelifaro
1692272840Smelifaro	sz = sizeof(*oh) + sizeof(ipfw_xtable_info);
1693272840Smelifaro	oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1694272840Smelifaro	if (oh == NULL)
1695272840Smelifaro		return (EINVAL);
1696272840Smelifaro
1697272840Smelifaro	objheader_to_ti(oh, &ti);
1698272840Smelifaro
1699272840Smelifaro	IPFW_UH_RLOCK(ch);
1700272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
1701272840Smelifaro		IPFW_UH_RUNLOCK(ch);
1702272840Smelifaro		return (ESRCH);
1703200590Sluigi	}
1704272840Smelifaro
1705272840Smelifaro	export_table_info(ch, tc, (ipfw_xtable_info *)(oh + 1));
1706272840Smelifaro	IPFW_UH_RUNLOCK(ch);
1707272840Smelifaro
1708200590Sluigi	return (0);
1709200590Sluigi}
1710200590Sluigi
1711272840Smelifaro/*
1712272840Smelifaro * Modifies existing table.
1713272840Smelifaro * Data layout (v0)(current):
1714272840Smelifaro * Request: [ ipfw_obj_header ipfw_xtable_info ]
1715272840Smelifaro *
1716272840Smelifaro * Returns 0 on success
1717272840Smelifaro */
1718272840Smelifarostatic int
1719272840Smelifaromodify_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1720272840Smelifaro    struct sockopt_data *sd)
1721232865Smelifaro{
1722272840Smelifaro	struct _ipfw_obj_header *oh;
1723272840Smelifaro	ipfw_xtable_info *i;
1724272840Smelifaro	char *tname;
1725272840Smelifaro	struct tid_info ti;
1726272840Smelifaro	struct namedobj_instance *ni;
1727272840Smelifaro	struct table_config *tc;
1728232865Smelifaro
1729272840Smelifaro	if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1730272840Smelifaro		return (EINVAL);
1731232865Smelifaro
1732272840Smelifaro	oh = (struct _ipfw_obj_header *)sd->kbuf;
1733272840Smelifaro	i = (ipfw_xtable_info *)(oh + 1);
1734232865Smelifaro
1735272840Smelifaro	/*
1736272840Smelifaro	 * Verify user-supplied strings.
1737272840Smelifaro	 * Check for null-terminated/zero-length strings/
1738272840Smelifaro	 */
1739272840Smelifaro	tname = oh->ntlv.name;
1740290332Sae	if (check_table_name(tname) != 0)
1741272840Smelifaro		return (EINVAL);
1742232865Smelifaro
1743272840Smelifaro	objheader_to_ti(oh, &ti);
1744272840Smelifaro	ti.type = i->type;
1745272840Smelifaro
1746272840Smelifaro	IPFW_UH_WLOCK(ch);
1747272840Smelifaro	ni = CHAIN_TO_NI(ch);
1748272840Smelifaro	if ((tc = find_table(ni, &ti)) == NULL) {
1749272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1750272840Smelifaro		return (ESRCH);
1751232865Smelifaro	}
1752232865Smelifaro
1753272840Smelifaro	/* Do not support any modifications for readonly tables */
1754272840Smelifaro	if ((tc->ta->flags & TA_FLAG_READONLY) != 0) {
1755272840Smelifaro		IPFW_UH_WUNLOCK(ch);
1756272840Smelifaro		return (EACCES);
1757232865Smelifaro	}
1758272840Smelifaro
1759272840Smelifaro	if ((i->mflags & IPFW_TMFLAGS_LIMIT) != 0)
1760272840Smelifaro		tc->limit = i->limit;
1761272840Smelifaro	if ((i->mflags & IPFW_TMFLAGS_LOCK) != 0)
1762272840Smelifaro		tc->locked = ((i->flags & IPFW_TGFLAGS_LOCKED) != 0);
1763272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1764272840Smelifaro
1765232865Smelifaro	return (0);
1766232865Smelifaro}
1767232865Smelifaro
1768272840Smelifaro/*
1769272840Smelifaro * Creates new table.
1770272840Smelifaro * Data layout (v0)(current):
1771272840Smelifaro * Request: [ ipfw_obj_header ipfw_xtable_info ]
1772272840Smelifaro *
1773272840Smelifaro * Returns 0 on success
1774272840Smelifaro */
1775200590Sluigistatic int
1776272840Smelifarocreate_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1777272840Smelifaro    struct sockopt_data *sd)
1778200590Sluigi{
1779272840Smelifaro	struct _ipfw_obj_header *oh;
1780272840Smelifaro	ipfw_xtable_info *i;
1781272840Smelifaro	char *tname, *aname;
1782272840Smelifaro	struct tid_info ti;
1783272840Smelifaro	struct namedobj_instance *ni;
1784200590Sluigi
1785272840Smelifaro	if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1786272840Smelifaro		return (EINVAL);
1787272840Smelifaro
1788272840Smelifaro	oh = (struct _ipfw_obj_header *)sd->kbuf;
1789272840Smelifaro	i = (ipfw_xtable_info *)(oh + 1);
1790272840Smelifaro
1791272840Smelifaro	/*
1792272840Smelifaro	 * Verify user-supplied strings.
1793272840Smelifaro	 * Check for null-terminated/zero-length strings/
1794272840Smelifaro	 */
1795272840Smelifaro	tname = oh->ntlv.name;
1796272840Smelifaro	aname = i->algoname;
1797290332Sae	if (check_table_name(tname) != 0 ||
1798272840Smelifaro	    strnlen(aname, sizeof(i->algoname)) == sizeof(i->algoname))
1799272840Smelifaro		return (EINVAL);
1800272840Smelifaro
1801272840Smelifaro	if (aname[0] == '\0') {
1802272840Smelifaro		/* Use default algorithm */
1803272840Smelifaro		aname = NULL;
1804272840Smelifaro	}
1805272840Smelifaro
1806272840Smelifaro	objheader_to_ti(oh, &ti);
1807272840Smelifaro	ti.type = i->type;
1808272840Smelifaro
1809272840Smelifaro	ni = CHAIN_TO_NI(ch);
1810272840Smelifaro
1811272840Smelifaro	IPFW_UH_RLOCK(ch);
1812274087Smelifaro	if (find_table(ni, &ti) != NULL) {
1813272840Smelifaro		IPFW_UH_RUNLOCK(ch);
1814272840Smelifaro		return (EEXIST);
1815272840Smelifaro	}
1816272840Smelifaro	IPFW_UH_RUNLOCK(ch);
1817272840Smelifaro
1818272840Smelifaro	return (create_table_internal(ch, &ti, aname, i, NULL, 0));
1819272840Smelifaro}
1820272840Smelifaro
1821272840Smelifaro/*
1822272840Smelifaro * Creates new table based on @ti and @aname.
1823272840Smelifaro *
1824272840Smelifaro * Assume @aname to be checked and valid.
1825272840Smelifaro * Stores allocated table kidx inside @pkidx (if non-NULL).
1826272840Smelifaro * Reference created table if @compat is non-zero.
1827272840Smelifaro *
1828272840Smelifaro * Returns 0 on success.
1829272840Smelifaro */
1830272840Smelifarostatic int
1831272840Smelifarocreate_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
1832272840Smelifaro    char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int compat)
1833272840Smelifaro{
1834272840Smelifaro	struct namedobj_instance *ni;
1835272840Smelifaro	struct table_config *tc, *tc_new, *tmp;
1836272840Smelifaro	struct table_algo *ta;
1837272840Smelifaro	uint16_t kidx;
1838272840Smelifaro
1839272840Smelifaro	ni = CHAIN_TO_NI(ch);
1840272840Smelifaro
1841272840Smelifaro	ta = find_table_algo(CHAIN_TO_TCFG(ch), ti, aname);
1842272840Smelifaro	if (ta == NULL)
1843272840Smelifaro		return (ENOTSUP);
1844272840Smelifaro
1845272840Smelifaro	tc = alloc_table_config(ch, ti, ta, aname, i->tflags);
1846272840Smelifaro	if (tc == NULL)
1847272840Smelifaro		return (ENOMEM);
1848272840Smelifaro
1849272840Smelifaro	tc->vmask = i->vmask;
1850272840Smelifaro	tc->limit = i->limit;
1851272840Smelifaro	if (ta->flags & TA_FLAG_READONLY)
1852272840Smelifaro		tc->locked = 1;
1853272840Smelifaro	else
1854272840Smelifaro		tc->locked = (i->flags & IPFW_TGFLAGS_LOCKED) != 0;
1855272840Smelifaro
1856272840Smelifaro	IPFW_UH_WLOCK(ch);
1857272840Smelifaro
1858272840Smelifaro	/* Check if table has been already created */
1859272840Smelifaro	tc_new = find_table(ni, ti);
1860272840Smelifaro	if (tc_new != NULL) {
1861272840Smelifaro
1862272840Smelifaro		/*
1863272840Smelifaro		 * Compat: do not fail if we're
1864272840Smelifaro		 * requesting to create existing table
1865272840Smelifaro		 * which has the same type
1866272840Smelifaro		 */
1867282070Smelifaro		if (compat == 0 || tc_new->no.subtype != tc->no.subtype) {
1868272840Smelifaro			IPFW_UH_WUNLOCK(ch);
1869272840Smelifaro			free_table_config(ni, tc);
1870272840Smelifaro			return (EEXIST);
1871272840Smelifaro		}
1872272840Smelifaro
1873272840Smelifaro		/* Exchange tc and tc_new for proper refcounting & freeing */
1874272840Smelifaro		tmp = tc;
1875272840Smelifaro		tc = tc_new;
1876272840Smelifaro		tc_new = tmp;
1877272840Smelifaro	} else {
1878272840Smelifaro		/* New table */
1879272840Smelifaro		if (ipfw_objhash_alloc_idx(ni, &kidx) != 0) {
1880272840Smelifaro			IPFW_UH_WUNLOCK(ch);
1881272840Smelifaro			printf("Unable to allocate table index."
1882272840Smelifaro			    " Consider increasing net.inet.ip.fw.tables_max");
1883272840Smelifaro			free_table_config(ni, tc);
1884272840Smelifaro			return (EBUSY);
1885272840Smelifaro		}
1886272840Smelifaro		tc->no.kidx = kidx;
1887282070Smelifaro		tc->no.etlv = IPFW_TLV_TBL_NAME;
1888272840Smelifaro
1889272840Smelifaro		IPFW_WLOCK(ch);
1890272840Smelifaro		link_table(ch, tc);
1891272840Smelifaro		IPFW_WUNLOCK(ch);
1892272840Smelifaro	}
1893272840Smelifaro
1894272840Smelifaro	if (compat != 0)
1895272840Smelifaro		tc->no.refcnt++;
1896272840Smelifaro	if (pkidx != NULL)
1897272840Smelifaro		*pkidx = tc->no.kidx;
1898272840Smelifaro
1899272840Smelifaro	IPFW_UH_WUNLOCK(ch);
1900272840Smelifaro
1901272840Smelifaro	if (tc_new != NULL)
1902272840Smelifaro		free_table_config(ni, tc_new);
1903272840Smelifaro
1904200590Sluigi	return (0);
1905200590Sluigi}
1906200590Sluigi
1907272840Smelifarostatic void
1908272840Smelifarontlv_to_ti(ipfw_obj_ntlv *ntlv, struct tid_info *ti)
1909272840Smelifaro{
1910272840Smelifaro
1911272840Smelifaro	memset(ti, 0, sizeof(struct tid_info));
1912272840Smelifaro	ti->set = ntlv->set;
1913272840Smelifaro	ti->uidx = ntlv->idx;
1914272840Smelifaro	ti->tlvs = ntlv;
1915272840Smelifaro	ti->tlen = ntlv->head.length;
1916272840Smelifaro}
1917272840Smelifaro
1918272840Smelifarostatic void
1919272840Smelifaroobjheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti)
1920272840Smelifaro{
1921272840Smelifaro
1922272840Smelifaro	ntlv_to_ti(&oh->ntlv, ti);
1923272840Smelifaro}
1924272840Smelifaro
1925282070Smelifarostruct namedobj_instance *
1926282070Smelifaroipfw_get_table_objhash(struct ip_fw_chain *ch)
1927282070Smelifaro{
1928282070Smelifaro
1929282070Smelifaro	return (CHAIN_TO_NI(ch));
1930282070Smelifaro}
1931282070Smelifaro
1932272840Smelifaro/*
1933272840Smelifaro * Exports basic table info as name TLV.
1934272840Smelifaro * Used inside dump_static_rules() to provide info
1935272840Smelifaro * about all tables referenced by current ruleset.
1936272840Smelifaro *
1937272840Smelifaro * Returns 0 on success.
1938272840Smelifaro */
1939200590Sluigiint
1940272840Smelifaroipfw_export_table_ntlv(struct ip_fw_chain *ch, uint16_t kidx,
1941272840Smelifaro    struct sockopt_data *sd)
1942200590Sluigi{
1943272840Smelifaro	struct namedobj_instance *ni;
1944272840Smelifaro	struct named_object *no;
1945272840Smelifaro	ipfw_obj_ntlv *ntlv;
1946200590Sluigi
1947272840Smelifaro	ni = CHAIN_TO_NI(ch);
1948272840Smelifaro
1949272840Smelifaro	no = ipfw_objhash_lookup_kidx(ni, kidx);
1950272840Smelifaro	KASSERT(no != NULL, ("invalid table kidx passed"));
1951272840Smelifaro
1952272840Smelifaro	ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
1953272840Smelifaro	if (ntlv == NULL)
1954272840Smelifaro		return (ENOMEM);
1955272840Smelifaro
1956272840Smelifaro	ntlv->head.type = IPFW_TLV_TBL_NAME;
1957272840Smelifaro	ntlv->head.length = sizeof(*ntlv);
1958272840Smelifaro	ntlv->idx = no->kidx;
1959272840Smelifaro	strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
1960272840Smelifaro
1961272840Smelifaro	return (0);
1962272840Smelifaro}
1963272840Smelifaro
1964272840Smelifarostruct dump_args {
1965272840Smelifaro	struct ip_fw_chain *ch;
1966272840Smelifaro	struct table_info *ti;
1967272840Smelifaro	struct table_config *tc;
1968272840Smelifaro	struct sockopt_data *sd;
1969272840Smelifaro	uint32_t cnt;
1970272840Smelifaro	uint16_t uidx;
1971272840Smelifaro	int error;
1972272840Smelifaro	uint32_t size;
1973272840Smelifaro	ipfw_table_entry *ent;
1974272840Smelifaro	ta_foreach_f *f;
1975272840Smelifaro	void *farg;
1976272840Smelifaro	ipfw_obj_tentry tent;
1977272840Smelifaro};
1978272840Smelifaro
1979272840Smelifarostatic int
1980272840Smelifarocount_ext_entries(void *e, void *arg)
1981272840Smelifaro{
1982272840Smelifaro	struct dump_args *da;
1983272840Smelifaro
1984272840Smelifaro	da = (struct dump_args *)arg;
1985272840Smelifaro	da->cnt++;
1986272840Smelifaro
1987272840Smelifaro	return (0);
1988272840Smelifaro}
1989272840Smelifaro
1990272840Smelifaro/*
1991272840Smelifaro * Gets number of items from table either using
1992272840Smelifaro * internal counter or calling algo callback for
1993272840Smelifaro * externally-managed tables.
1994272840Smelifaro *
1995272840Smelifaro * Returns number of records.
1996272840Smelifaro */
1997272840Smelifarostatic uint32_t
1998272840Smelifarotable_get_count(struct ip_fw_chain *ch, struct table_config *tc)
1999272840Smelifaro{
2000272840Smelifaro	struct table_info *ti;
2001272840Smelifaro	struct table_algo *ta;
2002272840Smelifaro	struct dump_args da;
2003272840Smelifaro
2004272840Smelifaro	ti = KIDX_TO_TI(ch, tc->no.kidx);
2005272840Smelifaro	ta = tc->ta;
2006272840Smelifaro
2007272840Smelifaro	/* Use internal counter for self-managed tables */
2008272840Smelifaro	if ((ta->flags & TA_FLAG_READONLY) == 0)
2009272840Smelifaro		return (tc->count);
2010272840Smelifaro
2011272840Smelifaro	/* Use callback to quickly get number of items */
2012272840Smelifaro	if ((ta->flags & TA_FLAG_EXTCOUNTER) != 0)
2013272840Smelifaro		return (ta->get_count(tc->astate, ti));
2014272840Smelifaro
2015272840Smelifaro	/* Count number of iterms ourselves */
2016272840Smelifaro	memset(&da, 0, sizeof(da));
2017272840Smelifaro	ta->foreach(tc->astate, ti, count_ext_entries, &da);
2018272840Smelifaro
2019272840Smelifaro	return (da.cnt);
2020272840Smelifaro}
2021272840Smelifaro
2022272840Smelifaro/*
2023272840Smelifaro * Exports table @tc info into standard ipfw_xtable_info format.
2024272840Smelifaro */
2025272840Smelifarostatic void
2026272840Smelifaroexport_table_info(struct ip_fw_chain *ch, struct table_config *tc,
2027272840Smelifaro    ipfw_xtable_info *i)
2028272840Smelifaro{
2029272840Smelifaro	struct table_info *ti;
2030272840Smelifaro	struct table_algo *ta;
2031272840Smelifaro
2032282070Smelifaro	i->type = tc->no.subtype;
2033272840Smelifaro	i->tflags = tc->tflags;
2034272840Smelifaro	i->vmask = tc->vmask;
2035272840Smelifaro	i->set = tc->no.set;
2036272840Smelifaro	i->kidx = tc->no.kidx;
2037272840Smelifaro	i->refcnt = tc->no.refcnt;
2038272840Smelifaro	i->count = table_get_count(ch, tc);
2039272840Smelifaro	i->limit = tc->limit;
2040272840Smelifaro	i->flags |= (tc->locked != 0) ? IPFW_TGFLAGS_LOCKED : 0;
2041293625Smelifaro	i->size = i->count * sizeof(ipfw_obj_tentry);
2042272840Smelifaro	i->size += sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2043272840Smelifaro	strlcpy(i->tablename, tc->tablename, sizeof(i->tablename));
2044272840Smelifaro	ti = KIDX_TO_TI(ch, tc->no.kidx);
2045272840Smelifaro	ta = tc->ta;
2046272840Smelifaro	if (ta->print_config != NULL) {
2047272840Smelifaro		/* Use algo function to print table config to string */
2048272840Smelifaro		ta->print_config(tc->astate, ti, i->algoname,
2049272840Smelifaro		    sizeof(i->algoname));
2050272840Smelifaro	} else
2051272840Smelifaro		strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2052272840Smelifaro	/* Dump algo-specific data, if possible */
2053272840Smelifaro	if (ta->dump_tinfo != NULL) {
2054272840Smelifaro		ta->dump_tinfo(tc->astate, ti, &i->ta_info);
2055272840Smelifaro		i->ta_info.flags |= IPFW_TATFLAGS_DATA;
2056272840Smelifaro	}
2057272840Smelifaro}
2058272840Smelifaro
2059272840Smelifarostruct dump_table_args {
2060272840Smelifaro	struct ip_fw_chain *ch;
2061272840Smelifaro	struct sockopt_data *sd;
2062272840Smelifaro};
2063272840Smelifaro
2064299152Saestatic int
2065272840Smelifaroexport_table_internal(struct namedobj_instance *ni, struct named_object *no,
2066272840Smelifaro    void *arg)
2067272840Smelifaro{
2068272840Smelifaro	ipfw_xtable_info *i;
2069272840Smelifaro	struct dump_table_args *dta;
2070272840Smelifaro
2071272840Smelifaro	dta = (struct dump_table_args *)arg;
2072272840Smelifaro
2073272840Smelifaro	i = (ipfw_xtable_info *)ipfw_get_sopt_space(dta->sd, sizeof(*i));
2074298048Spfg	KASSERT(i != NULL, ("previously checked buffer is not enough"));
2075272840Smelifaro
2076272840Smelifaro	export_table_info(dta->ch, (struct table_config *)no, i);
2077299152Sae	return (0);
2078272840Smelifaro}
2079272840Smelifaro
2080272840Smelifaro/*
2081272840Smelifaro * Export all tables as ipfw_xtable_info structures to
2082272840Smelifaro * storage provided by @sd.
2083272840Smelifaro *
2084272840Smelifaro * If supplied buffer is too small, fills in required size
2085272840Smelifaro * and returns ENOMEM.
2086272840Smelifaro * Returns 0 on success.
2087272840Smelifaro */
2088272840Smelifarostatic int
2089272840Smelifaroexport_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
2090272840Smelifaro    struct sockopt_data *sd)
2091272840Smelifaro{
2092272840Smelifaro	uint32_t size;
2093272840Smelifaro	uint32_t count;
2094272840Smelifaro	struct dump_table_args dta;
2095272840Smelifaro
2096272840Smelifaro	count = ipfw_objhash_count(CHAIN_TO_NI(ch));
2097272840Smelifaro	size = count * sizeof(ipfw_xtable_info) + sizeof(ipfw_obj_lheader);
2098272840Smelifaro
2099272840Smelifaro	/* Fill in header regadless of buffer size */
2100272840Smelifaro	olh->count = count;
2101272840Smelifaro	olh->objsize = sizeof(ipfw_xtable_info);
2102272840Smelifaro
2103272840Smelifaro	if (size > olh->size) {
2104272840Smelifaro		olh->size = size;
2105272840Smelifaro		return (ENOMEM);
2106272840Smelifaro	}
2107272840Smelifaro
2108272840Smelifaro	olh->size = size;
2109272840Smelifaro
2110272840Smelifaro	dta.ch = ch;
2111272840Smelifaro	dta.sd = sd;
2112272840Smelifaro
2113272840Smelifaro	ipfw_objhash_foreach(CHAIN_TO_NI(ch), export_table_internal, &dta);
2114272840Smelifaro
2115272840Smelifaro	return (0);
2116272840Smelifaro}
2117272840Smelifaro
2118272840Smelifaro/*
2119272840Smelifaro * Dumps all table data
2120272840Smelifaro * Data layout (v1)(current):
2121272840Smelifaro * Request: [ ipfw_obj_header ], size = ipfw_xtable_info.size
2122272840Smelifaro * Reply: [ ipfw_obj_header ipfw_xtable_info ipfw_obj_tentry x N ]
2123272840Smelifaro *
2124272840Smelifaro * Returns 0 on success
2125272840Smelifaro */
2126272840Smelifarostatic int
2127272840Smelifarodump_table_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2128272840Smelifaro    struct sockopt_data *sd)
2129272840Smelifaro{
2130272840Smelifaro	struct _ipfw_obj_header *oh;
2131272840Smelifaro	ipfw_xtable_info *i;
2132272840Smelifaro	struct tid_info ti;
2133272840Smelifaro	struct table_config *tc;
2134272840Smelifaro	struct table_algo *ta;
2135272840Smelifaro	struct dump_args da;
2136272840Smelifaro	uint32_t sz;
2137272840Smelifaro
2138272840Smelifaro	sz = sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2139272840Smelifaro	oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
2140272840Smelifaro	if (oh == NULL)
2141200590Sluigi		return (EINVAL);
2142272840Smelifaro
2143272840Smelifaro	i = (ipfw_xtable_info *)(oh + 1);
2144272840Smelifaro	objheader_to_ti(oh, &ti);
2145272840Smelifaro
2146272840Smelifaro	IPFW_UH_RLOCK(ch);
2147272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2148272840Smelifaro		IPFW_UH_RUNLOCK(ch);
2149272840Smelifaro		return (ESRCH);
2150272840Smelifaro	}
2151272840Smelifaro	export_table_info(ch, tc, i);
2152272840Smelifaro
2153272840Smelifaro	if (sd->valsize < i->size) {
2154272840Smelifaro
2155272840Smelifaro		/*
2156272840Smelifaro		 * Submitted buffer size is not enough.
2157272840Smelifaro		 * WE've already filled in @i structure with
2158272840Smelifaro		 * relevant table info including size, so we
2159272840Smelifaro		 * can return. Buffer will be flushed automatically.
2160272840Smelifaro		 */
2161272840Smelifaro		IPFW_UH_RUNLOCK(ch);
2162272840Smelifaro		return (ENOMEM);
2163272840Smelifaro	}
2164272840Smelifaro
2165272840Smelifaro	/*
2166272840Smelifaro	 * Do the actual dump in eXtended format
2167272840Smelifaro	 */
2168272840Smelifaro	memset(&da, 0, sizeof(da));
2169272840Smelifaro	da.ch = ch;
2170272840Smelifaro	da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2171272840Smelifaro	da.tc = tc;
2172272840Smelifaro	da.sd = sd;
2173272840Smelifaro
2174272840Smelifaro	ta = tc->ta;
2175272840Smelifaro
2176272840Smelifaro	ta->foreach(tc->astate, da.ti, dump_table_tentry, &da);
2177272840Smelifaro	IPFW_UH_RUNLOCK(ch);
2178272840Smelifaro
2179272840Smelifaro	return (da.error);
2180272840Smelifaro}
2181272840Smelifaro
2182272840Smelifaro/*
2183272840Smelifaro * Dumps all table data
2184272840Smelifaro * Data layout (version 0)(legacy):
2185272840Smelifaro * Request: [ ipfw_xtable ], size = IP_FW_TABLE_XGETSIZE()
2186272840Smelifaro * Reply: [ ipfw_xtable ipfw_table_xentry x N ]
2187272840Smelifaro *
2188272840Smelifaro * Returns 0 on success
2189272840Smelifaro */
2190272840Smelifarostatic int
2191272840Smelifarodump_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2192272840Smelifaro    struct sockopt_data *sd)
2193272840Smelifaro{
2194272840Smelifaro	ipfw_xtable *xtbl;
2195272840Smelifaro	struct tid_info ti;
2196272840Smelifaro	struct table_config *tc;
2197272840Smelifaro	struct table_algo *ta;
2198272840Smelifaro	struct dump_args da;
2199272840Smelifaro	size_t sz, count;
2200272840Smelifaro
2201272840Smelifaro	xtbl = (ipfw_xtable *)ipfw_get_sopt_header(sd, sizeof(ipfw_xtable));
2202272840Smelifaro	if (xtbl == NULL)
2203272840Smelifaro		return (EINVAL);
2204272840Smelifaro
2205272840Smelifaro	memset(&ti, 0, sizeof(ti));
2206272840Smelifaro	ti.uidx = xtbl->tbl;
2207272840Smelifaro
2208272840Smelifaro	IPFW_UH_RLOCK(ch);
2209272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2210272840Smelifaro		IPFW_UH_RUNLOCK(ch);
2211232865Smelifaro		return (0);
2212272840Smelifaro	}
2213272840Smelifaro	count = table_get_count(ch, tc);
2214272840Smelifaro	sz = count * sizeof(ipfw_table_xentry) + sizeof(ipfw_xtable);
2215272840Smelifaro
2216272840Smelifaro	xtbl->cnt = count;
2217272840Smelifaro	xtbl->size = sz;
2218282070Smelifaro	xtbl->type = tc->no.subtype;
2219272840Smelifaro	xtbl->tbl = ti.uidx;
2220272840Smelifaro
2221272840Smelifaro	if (sd->valsize < sz) {
2222272840Smelifaro
2223272840Smelifaro		/*
2224272840Smelifaro		 * Submitted buffer size is not enough.
2225272840Smelifaro		 * WE've already filled in @i structure with
2226272840Smelifaro		 * relevant table info including size, so we
2227272840Smelifaro		 * can return. Buffer will be flushed automatically.
2228272840Smelifaro		 */
2229272840Smelifaro		IPFW_UH_RUNLOCK(ch);
2230272840Smelifaro		return (ENOMEM);
2231272840Smelifaro	}
2232272840Smelifaro
2233272840Smelifaro	/* Do the actual dump in eXtended format */
2234272840Smelifaro	memset(&da, 0, sizeof(da));
2235272840Smelifaro	da.ch = ch;
2236272840Smelifaro	da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2237272840Smelifaro	da.tc = tc;
2238272840Smelifaro	da.sd = sd;
2239272840Smelifaro
2240272840Smelifaro	ta = tc->ta;
2241272840Smelifaro
2242272840Smelifaro	ta->foreach(tc->astate, da.ti, dump_table_xentry, &da);
2243272840Smelifaro	IPFW_UH_RUNLOCK(ch);
2244272840Smelifaro
2245200590Sluigi	return (0);
2246200590Sluigi}
2247200590Sluigi
2248272840Smelifaro/*
2249272840Smelifaro * Legacy function to retrieve number of items in table.
2250272840Smelifaro */
2251200590Sluigistatic int
2252272840Smelifaroget_table_size(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2253272840Smelifaro    struct sockopt_data *sd)
2254200590Sluigi{
2255272840Smelifaro	uint32_t *tbl;
2256272840Smelifaro	struct tid_info ti;
2257272840Smelifaro	size_t sz;
2258272840Smelifaro	int error;
2259200590Sluigi
2260272840Smelifaro	sz = sizeof(*op3) + sizeof(uint32_t);
2261272840Smelifaro	op3 = (ip_fw3_opheader *)ipfw_get_sopt_header(sd, sz);
2262272840Smelifaro	if (op3 == NULL)
2263272840Smelifaro		return (EINVAL);
2264272840Smelifaro
2265272840Smelifaro	tbl = (uint32_t *)(op3 + 1);
2266272840Smelifaro	memset(&ti, 0, sizeof(ti));
2267272840Smelifaro	ti.uidx = *tbl;
2268272840Smelifaro	IPFW_UH_RLOCK(ch);
2269272840Smelifaro	error = ipfw_count_xtable(ch, &ti, tbl);
2270272840Smelifaro	IPFW_UH_RUNLOCK(ch);
2271272840Smelifaro	return (error);
2272272840Smelifaro}
2273272840Smelifaro
2274272840Smelifaro/*
2275272840Smelifaro * Legacy IP_FW_TABLE_GETSIZE handler
2276272840Smelifaro */
2277272840Smelifaroint
2278272840Smelifaroipfw_count_table(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2279272840Smelifaro{
2280272840Smelifaro	struct table_config *tc;
2281272840Smelifaro
2282272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2283272840Smelifaro		return (ESRCH);
2284272840Smelifaro	*cnt = table_get_count(ch, tc);
2285200590Sluigi	return (0);
2286200590Sluigi}
2287200590Sluigi
2288272840Smelifaro/*
2289272840Smelifaro * Legacy IP_FW_TABLE_XGETSIZE handler
2290272840Smelifaro */
2291200590Sluigiint
2292272840Smelifaroipfw_count_xtable(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2293200590Sluigi{
2294272840Smelifaro	struct table_config *tc;
2295272840Smelifaro	uint32_t count;
2296200590Sluigi
2297272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL) {
2298272840Smelifaro		*cnt = 0;
2299272840Smelifaro		return (0); /* 'table all list' requires success */
2300272840Smelifaro	}
2301272840Smelifaro
2302272840Smelifaro	count = table_get_count(ch, tc);
2303272840Smelifaro	*cnt = count * sizeof(ipfw_table_xentry);
2304272840Smelifaro	if (count > 0)
2305272840Smelifaro		*cnt += sizeof(ipfw_xtable);
2306200590Sluigi	return (0);
2307200590Sluigi}
2308232865Smelifaro
2309232865Smelifarostatic int
2310272840Smelifarodump_table_entry(void *e, void *arg)
2311232865Smelifaro{
2312272840Smelifaro	struct dump_args *da;
2313272840Smelifaro	struct table_config *tc;
2314272840Smelifaro	struct table_algo *ta;
2315272840Smelifaro	ipfw_table_entry *ent;
2316272840Smelifaro	struct table_value *pval;
2317272840Smelifaro	int error;
2318232865Smelifaro
2319272840Smelifaro	da = (struct dump_args *)arg;
2320272840Smelifaro
2321272840Smelifaro	tc = da->tc;
2322272840Smelifaro	ta = tc->ta;
2323272840Smelifaro
2324272840Smelifaro	/* Out of memory, returning */
2325272840Smelifaro	if (da->cnt == da->size)
2326272840Smelifaro		return (1);
2327272840Smelifaro	ent = da->ent++;
2328272840Smelifaro	ent->tbl = da->uidx;
2329272840Smelifaro	da->cnt++;
2330272840Smelifaro
2331272840Smelifaro	error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2332272840Smelifaro	if (error != 0)
2333272840Smelifaro		return (error);
2334272840Smelifaro
2335272840Smelifaro	ent->addr = da->tent.k.addr.s_addr;
2336272840Smelifaro	ent->masklen = da->tent.masklen;
2337272840Smelifaro	pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2338272840Smelifaro	ent->value = ipfw_export_table_value_legacy(pval);
2339272840Smelifaro
2340232865Smelifaro	return (0);
2341232865Smelifaro}
2342232865Smelifaro
2343272840Smelifaro/*
2344272840Smelifaro * Dumps table in pre-8.1 legacy format.
2345272840Smelifaro */
2346232865Smelifaroint
2347272840Smelifaroipfw_dump_table_legacy(struct ip_fw_chain *ch, struct tid_info *ti,
2348272840Smelifaro    ipfw_table *tbl)
2349232865Smelifaro{
2350272840Smelifaro	struct table_config *tc;
2351272840Smelifaro	struct table_algo *ta;
2352272840Smelifaro	struct dump_args da;
2353232865Smelifaro
2354272840Smelifaro	tbl->cnt = 0;
2355272840Smelifaro
2356272840Smelifaro	if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2357272840Smelifaro		return (0);	/* XXX: We should return ESRCH */
2358272840Smelifaro
2359272840Smelifaro	ta = tc->ta;
2360272840Smelifaro
2361272840Smelifaro	/* This dump format supports IPv4 only */
2362282070Smelifaro	if (tc->no.subtype != IPFW_TABLE_ADDR)
2363272840Smelifaro		return (0);
2364272840Smelifaro
2365272840Smelifaro	memset(&da, 0, sizeof(da));
2366272840Smelifaro	da.ch = ch;
2367272840Smelifaro	da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2368272840Smelifaro	da.tc = tc;
2369272840Smelifaro	da.ent = &tbl->ent[0];
2370272840Smelifaro	da.size = tbl->size;
2371272840Smelifaro
2372272840Smelifaro	tbl->cnt = 0;
2373272840Smelifaro	ta->foreach(tc->astate, da.ti, dump_table_entry, &da);
2374272840Smelifaro	tbl->cnt = da.cnt;
2375272840Smelifaro
2376232865Smelifaro	return (0);
2377232865Smelifaro}
2378232865Smelifaro
2379272840Smelifaro/*
2380272840Smelifaro * Dumps table entry in eXtended format (v1)(current).
2381272840Smelifaro */
2382232865Smelifarostatic int
2383272840Smelifarodump_table_tentry(void *e, void *arg)
2384232865Smelifaro{
2385272840Smelifaro	struct dump_args *da;
2386272840Smelifaro	struct table_config *tc;
2387272840Smelifaro	struct table_algo *ta;
2388272840Smelifaro	struct table_value *pval;
2389272840Smelifaro	ipfw_obj_tentry *tent;
2390272840Smelifaro	int error;
2391232865Smelifaro
2392272840Smelifaro	da = (struct dump_args *)arg;
2393272840Smelifaro
2394272840Smelifaro	tc = da->tc;
2395272840Smelifaro	ta = tc->ta;
2396272840Smelifaro
2397272840Smelifaro	tent = (ipfw_obj_tentry *)ipfw_get_sopt_space(da->sd, sizeof(*tent));
2398232865Smelifaro	/* Out of memory, returning */
2399272840Smelifaro	if (tent == NULL) {
2400272840Smelifaro		da->error = ENOMEM;
2401232865Smelifaro		return (1);
2402272840Smelifaro	}
2403272840Smelifaro	tent->head.length = sizeof(ipfw_obj_tentry);
2404272840Smelifaro	tent->idx = da->uidx;
2405272840Smelifaro
2406272840Smelifaro	error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2407272840Smelifaro	if (error != 0)
2408272840Smelifaro		return (error);
2409272840Smelifaro
2410272840Smelifaro	pval = get_table_value(da->ch, da->tc, tent->v.kidx);
2411272840Smelifaro	ipfw_export_table_value_v1(pval, &tent->v.value);
2412272840Smelifaro
2413232865Smelifaro	return (0);
2414232865Smelifaro}
2415232865Smelifaro
2416272840Smelifaro/*
2417272840Smelifaro * Dumps table entry in eXtended format (v0).
2418272840Smelifaro */
2419232865Smelifarostatic int
2420272840Smelifarodump_table_xentry(void *e, void *arg)
2421232865Smelifaro{
2422272840Smelifaro	struct dump_args *da;
2423272840Smelifaro	struct table_config *tc;
2424272840Smelifaro	struct table_algo *ta;
2425232865Smelifaro	ipfw_table_xentry *xent;
2426272840Smelifaro	ipfw_obj_tentry *tent;
2427272840Smelifaro	struct table_value *pval;
2428272840Smelifaro	int error;
2429272840Smelifaro
2430272840Smelifaro	da = (struct dump_args *)arg;
2431272840Smelifaro
2432272840Smelifaro	tc = da->tc;
2433272840Smelifaro	ta = tc->ta;
2434272840Smelifaro
2435272840Smelifaro	xent = (ipfw_table_xentry *)ipfw_get_sopt_space(da->sd, sizeof(*xent));
2436232865Smelifaro	/* Out of memory, returning */
2437272840Smelifaro	if (xent == NULL)
2438232865Smelifaro		return (1);
2439232865Smelifaro	xent->len = sizeof(ipfw_table_xentry);
2440272840Smelifaro	xent->tbl = da->uidx;
2441232865Smelifaro
2442272840Smelifaro	memset(&da->tent, 0, sizeof(da->tent));
2443272840Smelifaro	tent = &da->tent;
2444272840Smelifaro	error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2445272840Smelifaro	if (error != 0)
2446272840Smelifaro		return (error);
2447272840Smelifaro
2448272840Smelifaro	/* Convert current format to previous one */
2449272840Smelifaro	xent->masklen = tent->masklen;
2450272840Smelifaro	pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2451272840Smelifaro	xent->value = ipfw_export_table_value_legacy(pval);
2452272840Smelifaro	/* Apply some hacks */
2453282070Smelifaro	if (tc->no.subtype == IPFW_TABLE_ADDR && tent->subtype == AF_INET) {
2454272840Smelifaro		xent->k.addr6.s6_addr32[3] = tent->k.addr.s_addr;
2455272840Smelifaro		xent->flags = IPFW_TCF_INET;
2456272840Smelifaro	} else
2457272840Smelifaro		memcpy(&xent->k, &tent->k, sizeof(xent->k));
2458272840Smelifaro
2459272840Smelifaro	return (0);
2460272840Smelifaro}
2461272840Smelifaro
2462272840Smelifaro/*
2463272840Smelifaro * Helper function to export table algo data
2464272840Smelifaro * to tentry format before calling user function.
2465272840Smelifaro *
2466272840Smelifaro * Returns 0 on success.
2467272840Smelifaro */
2468272840Smelifarostatic int
2469272840Smelifaroprepare_table_tentry(void *e, void *arg)
2470272840Smelifaro{
2471272840Smelifaro	struct dump_args *da;
2472272840Smelifaro	struct table_config *tc;
2473272840Smelifaro	struct table_algo *ta;
2474272840Smelifaro	int error;
2475272840Smelifaro
2476272840Smelifaro	da = (struct dump_args *)arg;
2477272840Smelifaro
2478272840Smelifaro	tc = da->tc;
2479272840Smelifaro	ta = tc->ta;
2480272840Smelifaro
2481272840Smelifaro	error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2482272840Smelifaro	if (error != 0)
2483272840Smelifaro		return (error);
2484272840Smelifaro
2485272840Smelifaro	da->f(&da->tent, da->farg);
2486272840Smelifaro
2487272840Smelifaro	return (0);
2488272840Smelifaro}
2489272840Smelifaro
2490272840Smelifaro/*
2491272840Smelifaro * Allow external consumers to read table entries in standard format.
2492272840Smelifaro */
2493272840Smelifaroint
2494272840Smelifaroipfw_foreach_table_tentry(struct ip_fw_chain *ch, uint16_t kidx,
2495272840Smelifaro    ta_foreach_f *f, void *arg)
2496272840Smelifaro{
2497272840Smelifaro	struct namedobj_instance *ni;
2498272840Smelifaro	struct table_config *tc;
2499272840Smelifaro	struct table_algo *ta;
2500272840Smelifaro	struct dump_args da;
2501272840Smelifaro
2502272840Smelifaro	ni = CHAIN_TO_NI(ch);
2503272840Smelifaro
2504272840Smelifaro	tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
2505272840Smelifaro	if (tc == NULL)
2506272840Smelifaro		return (ESRCH);
2507272840Smelifaro
2508272840Smelifaro	ta = tc->ta;
2509272840Smelifaro
2510272840Smelifaro	memset(&da, 0, sizeof(da));
2511272840Smelifaro	da.ch = ch;
2512272840Smelifaro	da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2513272840Smelifaro	da.tc = tc;
2514272840Smelifaro	da.f = f;
2515272840Smelifaro	da.farg = arg;
2516272840Smelifaro
2517272840Smelifaro	ta->foreach(tc->astate, da.ti, prepare_table_tentry, &da);
2518272840Smelifaro
2519272840Smelifaro	return (0);
2520272840Smelifaro}
2521272840Smelifaro
2522272840Smelifaro/*
2523272840Smelifaro * Table algorithms
2524272840Smelifaro */
2525272840Smelifaro
2526272840Smelifaro/*
2527298995Spfg * Finds algorithm by index, table type or supplied name.
2528272840Smelifaro *
2529272840Smelifaro * Returns pointer to algo or NULL.
2530272840Smelifaro */
2531272840Smelifarostatic struct table_algo *
2532272840Smelifarofind_table_algo(struct tables_config *tcfg, struct tid_info *ti, char *name)
2533272840Smelifaro{
2534272840Smelifaro	int i, l;
2535272840Smelifaro	struct table_algo *ta;
2536272840Smelifaro
2537272840Smelifaro	if (ti->type > IPFW_TABLE_MAXTYPE)
2538272840Smelifaro		return (NULL);
2539272840Smelifaro
2540272840Smelifaro	/* Search by index */
2541272840Smelifaro	if (ti->atype != 0) {
2542272840Smelifaro		if (ti->atype > tcfg->algo_count)
2543272840Smelifaro			return (NULL);
2544272840Smelifaro		return (tcfg->algo[ti->atype]);
2545272840Smelifaro	}
2546272840Smelifaro
2547272840Smelifaro	if (name == NULL) {
2548272840Smelifaro		/* Return default algorithm for given type if set */
2549272840Smelifaro		return (tcfg->def_algo[ti->type]);
2550272840Smelifaro	}
2551272840Smelifaro
2552272840Smelifaro	/* Search by name */
2553272840Smelifaro	/* TODO: better search */
2554272840Smelifaro	for (i = 1; i <= tcfg->algo_count; i++) {
2555272840Smelifaro		ta = tcfg->algo[i];
2556272840Smelifaro
2557272840Smelifaro		/*
2558272840Smelifaro		 * One can supply additional algorithm
2559272840Smelifaro		 * parameters so we compare only the first word
2560272840Smelifaro		 * of supplied name:
2561272840Smelifaro		 * 'addr:chash hsize=32'
2562272840Smelifaro		 * '^^^^^^^^^'
2563272840Smelifaro		 *
2564272840Smelifaro		 */
2565272840Smelifaro		l = strlen(ta->name);
2566272840Smelifaro		if (strncmp(name, ta->name, l) != 0)
2567272840Smelifaro			continue;
2568272840Smelifaro		if (name[l] != '\0' && name[l] != ' ')
2569272840Smelifaro			continue;
2570272840Smelifaro		/* Check if we're requesting proper table type */
2571272840Smelifaro		if (ti->type != 0 && ti->type != ta->type)
2572272840Smelifaro			return (NULL);
2573272840Smelifaro		return (ta);
2574272840Smelifaro	}
2575272840Smelifaro
2576272840Smelifaro	return (NULL);
2577272840Smelifaro}
2578272840Smelifaro
2579272840Smelifaro/*
2580272840Smelifaro * Register new table algo @ta.
2581272840Smelifaro * Stores algo id inside @idx.
2582272840Smelifaro *
2583272840Smelifaro * Returns 0 on success.
2584272840Smelifaro */
2585272840Smelifaroint
2586272840Smelifaroipfw_add_table_algo(struct ip_fw_chain *ch, struct table_algo *ta, size_t size,
2587272840Smelifaro    int *idx)
2588272840Smelifaro{
2589272840Smelifaro	struct tables_config *tcfg;
2590272840Smelifaro	struct table_algo *ta_new;
2591272840Smelifaro	size_t sz;
2592272840Smelifaro
2593272840Smelifaro	if (size > sizeof(struct table_algo))
2594272840Smelifaro		return (EINVAL);
2595272840Smelifaro
2596272840Smelifaro	/* Check for the required on-stack size for add/del */
2597272840Smelifaro	sz = roundup2(ta->ta_buf_size, sizeof(void *));
2598272840Smelifaro	if (sz > TA_BUF_SZ)
2599272840Smelifaro		return (EINVAL);
2600272840Smelifaro
2601272840Smelifaro	KASSERT(ta->type <= IPFW_TABLE_MAXTYPE,("Increase IPFW_TABLE_MAXTYPE"));
2602272840Smelifaro
2603272840Smelifaro	/* Copy algorithm data to stable storage. */
2604272840Smelifaro	ta_new = malloc(sizeof(struct table_algo), M_IPFW, M_WAITOK | M_ZERO);
2605272840Smelifaro	memcpy(ta_new, ta, size);
2606272840Smelifaro
2607272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
2608272840Smelifaro
2609272840Smelifaro	KASSERT(tcfg->algo_count < 255, ("Increase algo array size"));
2610272840Smelifaro
2611272840Smelifaro	tcfg->algo[++tcfg->algo_count] = ta_new;
2612272840Smelifaro	ta_new->idx = tcfg->algo_count;
2613272840Smelifaro
2614272840Smelifaro	/* Set algorithm as default one for given type */
2615272840Smelifaro	if ((ta_new->flags & TA_FLAG_DEFAULT) != 0 &&
2616272840Smelifaro	    tcfg->def_algo[ta_new->type] == NULL)
2617272840Smelifaro		tcfg->def_algo[ta_new->type] = ta_new;
2618272840Smelifaro
2619272840Smelifaro	*idx = ta_new->idx;
2620232865Smelifaro
2621272840Smelifaro	return (0);
2622272840Smelifaro}
2623272840Smelifaro
2624272840Smelifaro/*
2625272840Smelifaro * Unregisters table algo using @idx as id.
2626272840Smelifaro * XXX: It is NOT safe to call this function in any place
2627272840Smelifaro * other than ipfw instance destroy handler.
2628272840Smelifaro */
2629272840Smelifarovoid
2630272840Smelifaroipfw_del_table_algo(struct ip_fw_chain *ch, int idx)
2631272840Smelifaro{
2632272840Smelifaro	struct tables_config *tcfg;
2633272840Smelifaro	struct table_algo *ta;
2634272840Smelifaro
2635272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
2636272840Smelifaro
2637272840Smelifaro	KASSERT(idx <= tcfg->algo_count, ("algo idx %d out of range 1..%d",
2638272840Smelifaro	    idx, tcfg->algo_count));
2639272840Smelifaro
2640272840Smelifaro	ta = tcfg->algo[idx];
2641272840Smelifaro	KASSERT(ta != NULL, ("algo idx %d is NULL", idx));
2642272840Smelifaro
2643272840Smelifaro	if (tcfg->def_algo[ta->type] == ta)
2644272840Smelifaro		tcfg->def_algo[ta->type] = NULL;
2645272840Smelifaro
2646272840Smelifaro	free(ta, M_IPFW);
2647272840Smelifaro}
2648272840Smelifaro
2649272840Smelifaro/*
2650272840Smelifaro * Lists all table algorithms currently available.
2651272840Smelifaro * Data layout (v0)(current):
2652272840Smelifaro * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
2653272840Smelifaro * Reply: [ ipfw_obj_lheader ipfw_ta_info x N ]
2654272840Smelifaro *
2655272840Smelifaro * Returns 0 on success
2656272840Smelifaro */
2657272840Smelifarostatic int
2658272840Smelifarolist_table_algo(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2659272840Smelifaro    struct sockopt_data *sd)
2660272840Smelifaro{
2661272840Smelifaro	struct _ipfw_obj_lheader *olh;
2662272840Smelifaro	struct tables_config *tcfg;
2663272840Smelifaro	ipfw_ta_info *i;
2664272840Smelifaro	struct table_algo *ta;
2665272840Smelifaro	uint32_t count, n, size;
2666272840Smelifaro
2667272840Smelifaro	olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
2668272840Smelifaro	if (olh == NULL)
2669272840Smelifaro		return (EINVAL);
2670272840Smelifaro	if (sd->valsize < olh->size)
2671272840Smelifaro		return (EINVAL);
2672272840Smelifaro
2673272840Smelifaro	IPFW_UH_RLOCK(ch);
2674272840Smelifaro	tcfg = CHAIN_TO_TCFG(ch);
2675272840Smelifaro	count = tcfg->algo_count;
2676272840Smelifaro	size = count * sizeof(ipfw_ta_info) + sizeof(ipfw_obj_lheader);
2677272840Smelifaro
2678272840Smelifaro	/* Fill in header regadless of buffer size */
2679272840Smelifaro	olh->count = count;
2680272840Smelifaro	olh->objsize = sizeof(ipfw_ta_info);
2681272840Smelifaro
2682272840Smelifaro	if (size > olh->size) {
2683272840Smelifaro		olh->size = size;
2684272840Smelifaro		IPFW_UH_RUNLOCK(ch);
2685272840Smelifaro		return (ENOMEM);
2686232865Smelifaro	}
2687272840Smelifaro	olh->size = size;
2688232865Smelifaro
2689272840Smelifaro	for (n = 1; n <= count; n++) {
2690272840Smelifaro		i = (ipfw_ta_info *)ipfw_get_sopt_space(sd, sizeof(*i));
2691298048Spfg		KASSERT(i != NULL, ("previously checked buffer is not enough"));
2692272840Smelifaro		ta = tcfg->algo[n];
2693272840Smelifaro		strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2694272840Smelifaro		i->type = ta->type;
2695272840Smelifaro		i->refcnt = ta->refcnt;
2696272840Smelifaro	}
2697272840Smelifaro
2698272840Smelifaro	IPFW_UH_RUNLOCK(ch);
2699272840Smelifaro
2700232865Smelifaro	return (0);
2701232865Smelifaro}
2702232865Smelifaro
2703282070Smelifarostatic int
2704282070Smelifaroclassify_srcdst(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2705282070Smelifaro{
2706282070Smelifaro	/* Basic IPv4/IPv6 or u32 lookups */
2707282070Smelifaro	*puidx = cmd->arg1;
2708282070Smelifaro	/* Assume ADDR by default */
2709282070Smelifaro	*ptype = IPFW_TABLE_ADDR;
2710282070Smelifaro	int v;
2711282070Smelifaro
2712282070Smelifaro	if (F_LEN(cmd) > F_INSN_SIZE(ipfw_insn_u32)) {
2713282070Smelifaro		/*
2714282070Smelifaro		 * generic lookup. The key must be
2715282070Smelifaro		 * in 32bit big-endian format.
2716282070Smelifaro		 */
2717282070Smelifaro		v = ((ipfw_insn_u32 *)cmd)->d[1];
2718282070Smelifaro		switch (v) {
2719282070Smelifaro		case 0:
2720282070Smelifaro		case 1:
2721282070Smelifaro			/* IPv4 src/dst */
2722282070Smelifaro			break;
2723282070Smelifaro		case 2:
2724282070Smelifaro		case 3:
2725282070Smelifaro			/* src/dst port */
2726282070Smelifaro			*ptype = IPFW_TABLE_NUMBER;
2727282070Smelifaro			break;
2728282070Smelifaro		case 4:
2729282070Smelifaro			/* uid/gid */
2730282070Smelifaro			*ptype = IPFW_TABLE_NUMBER;
2731282070Smelifaro			break;
2732282070Smelifaro		case 5:
2733282070Smelifaro			/* jid */
2734282070Smelifaro			*ptype = IPFW_TABLE_NUMBER;
2735282070Smelifaro			break;
2736282070Smelifaro		case 6:
2737282070Smelifaro			/* dscp */
2738282070Smelifaro			*ptype = IPFW_TABLE_NUMBER;
2739282070Smelifaro			break;
2740282070Smelifaro		}
2741282070Smelifaro	}
2742272840Smelifaro
2743282070Smelifaro	return (0);
2744282070Smelifaro}
2745282070Smelifaro
2746272840Smelifarostatic int
2747282070Smelifaroclassify_via(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2748272840Smelifaro{
2749272840Smelifaro	ipfw_insn_if *cmdif;
2750272840Smelifaro
2751282070Smelifaro	/* Interface table, possibly */
2752282070Smelifaro	cmdif = (ipfw_insn_if *)cmd;
2753282070Smelifaro	if (cmdif->name[0] != '\1')
2754282070Smelifaro		return (1);
2755272840Smelifaro
2756282070Smelifaro	*ptype = IPFW_TABLE_INTERFACE;
2757282070Smelifaro	*puidx = cmdif->p.kidx;
2758272840Smelifaro
2759282070Smelifaro	return (0);
2760282070Smelifaro}
2761272840Smelifaro
2762282070Smelifarostatic int
2763282070Smelifaroclassify_flow(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2764282070Smelifaro{
2765282070Smelifaro
2766282070Smelifaro	*puidx = cmd->arg1;
2767282070Smelifaro	*ptype = IPFW_TABLE_FLOW;
2768282070Smelifaro
2769282070Smelifaro	return (0);
2770272840Smelifaro}
2771272840Smelifaro
2772272840Smelifarostatic void
2773282070Smelifaroupdate_arg1(ipfw_insn *cmd, uint16_t idx)
2774272840Smelifaro{
2775282070Smelifaro
2776282070Smelifaro	cmd->arg1 = idx;
2777282070Smelifaro}
2778282070Smelifaro
2779282070Smelifarostatic void
2780282070Smelifaroupdate_via(ipfw_insn *cmd, uint16_t idx)
2781282070Smelifaro{
2782272840Smelifaro	ipfw_insn_if *cmdif;
2783272840Smelifaro
2784282070Smelifaro	cmdif = (ipfw_insn_if *)cmd;
2785282070Smelifaro	cmdif->p.kidx = idx;
2786272840Smelifaro}
2787272840Smelifaro
2788282070Smelifarostatic int
2789282070Smelifarotable_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
2790282070Smelifaro    struct named_object **pno)
2791282070Smelifaro{
2792282070Smelifaro	struct table_config *tc;
2793282070Smelifaro	int error;
2794282070Smelifaro
2795282070Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
2796282070Smelifaro
2797282070Smelifaro	error = find_table_err(CHAIN_TO_NI(ch), ti, &tc);
2798282070Smelifaro	if (error != 0)
2799282070Smelifaro		return (error);
2800282070Smelifaro
2801282070Smelifaro	*pno = &tc->no;
2802282070Smelifaro	return (0);
2803282070Smelifaro}
2804282070Smelifaro
2805282070Smelifaro/* XXX: sets-sets! */
2806282070Smelifarostatic struct named_object *
2807282070Smelifarotable_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
2808282070Smelifaro{
2809282070Smelifaro	struct namedobj_instance *ni;
2810282070Smelifaro	struct table_config *tc;
2811282070Smelifaro
2812282070Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
2813282070Smelifaro	ni = CHAIN_TO_NI(ch);
2814282070Smelifaro	tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, idx);
2815282070Smelifaro	KASSERT(tc != NULL, ("Table with index %d not found", idx));
2816282070Smelifaro
2817282070Smelifaro	return (&tc->no);
2818282070Smelifaro}
2819282070Smelifaro
2820300021Saestatic int
2821300021Saetable_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2822300021Sae    enum ipfw_sets_cmd cmd)
2823300021Sae{
2824300021Sae
2825300021Sae	switch (cmd) {
2826300021Sae	case SWAP_ALL:
2827300021Sae	case TEST_ALL:
2828300021Sae		/*
2829300021Sae		 * Return success for TEST_ALL, since nothing prevents
2830300021Sae		 * move rules from one set to another. All tables are
2831300021Sae		 * accessible from all sets when per-set tables sysctl
2832300021Sae		 * is disabled.
2833300021Sae		 */
2834300021Sae	case MOVE_ALL:
2835300021Sae	case TEST_ONE:
2836300021Sae	case MOVE_ONE:
2837300021Sae		/*
2838300021Sae		 * NOTE: we need to use ipfw_objhash_del/ipfw_objhash_add
2839300021Sae		 * if set number will be used in hash function. Currently
2840300021Sae		 * we can just use generic handler that replaces set value.
2841300021Sae		 */
2842300021Sae		if (V_fw_tables_sets == 0)
2843300021Sae			return (0);
2844300021Sae		break;
2845300021Sae	case COUNT_ONE:
2846300021Sae		/*
2847300021Sae		 * Return EOPNOTSUPP for COUNT_ONE when per-set sysctl is
2848300021Sae		 * disabled. This allow skip table's opcodes from additional
2849300021Sae		 * checks when specific rules moved to another set.
2850300021Sae		 */
2851300021Sae		if (V_fw_tables_sets == 0)
2852300021Sae			return (EOPNOTSUPP);
2853300021Sae	}
2854300021Sae	/* Use generic sets handler when per-set sysctl is enabled. */
2855300021Sae	return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2856300021Sae	    set, new_set, cmd));
2857300021Sae}
2858300021Sae
2859282070Smelifarostatic struct opcode_obj_rewrite opcodes[] = {
2860282070Smelifaro	{
2861300021Sae		.opcode = O_IP_SRC_LOOKUP,
2862300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2863300021Sae		.classifier = classify_srcdst,
2864300021Sae		.update = update_arg1,
2865300021Sae		.find_byname = table_findbyname,
2866300021Sae		.find_bykidx = table_findbykidx,
2867300021Sae		.create_object = create_table_compat,
2868300021Sae		.manage_sets = table_manage_sets,
2869282070Smelifaro	},
2870282070Smelifaro	{
2871300021Sae		.opcode = O_IP_DST_LOOKUP,
2872300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2873300021Sae		.classifier = classify_srcdst,
2874300021Sae		.update = update_arg1,
2875300021Sae		.find_byname = table_findbyname,
2876300021Sae		.find_bykidx = table_findbykidx,
2877300021Sae		.create_object = create_table_compat,
2878300021Sae		.manage_sets = table_manage_sets,
2879282070Smelifaro	},
2880282070Smelifaro	{
2881300021Sae		.opcode = O_IP_FLOW_LOOKUP,
2882300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2883300021Sae		.classifier = classify_flow,
2884300021Sae		.update = update_arg1,
2885300021Sae		.find_byname = table_findbyname,
2886300021Sae		.find_bykidx = table_findbykidx,
2887300021Sae		.create_object = create_table_compat,
2888300021Sae		.manage_sets = table_manage_sets,
2889282070Smelifaro	},
2890282070Smelifaro	{
2891300021Sae		.opcode = O_XMIT,
2892300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2893300021Sae		.classifier = classify_via,
2894300021Sae		.update = update_via,
2895300021Sae		.find_byname = table_findbyname,
2896300021Sae		.find_bykidx = table_findbykidx,
2897300021Sae		.create_object = create_table_compat,
2898300021Sae		.manage_sets = table_manage_sets,
2899282070Smelifaro	},
2900282070Smelifaro	{
2901300021Sae		.opcode = O_RECV,
2902300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2903300021Sae		.classifier = classify_via,
2904300021Sae		.update = update_via,
2905300021Sae		.find_byname = table_findbyname,
2906300021Sae		.find_bykidx = table_findbykidx,
2907300021Sae		.create_object = create_table_compat,
2908300021Sae		.manage_sets = table_manage_sets,
2909282070Smelifaro	},
2910282070Smelifaro	{
2911300021Sae		.opcode = O_VIA,
2912300021Sae		.etlv = IPFW_TLV_TBL_NAME,
2913300021Sae		.classifier = classify_via,
2914300021Sae		.update = update_via,
2915300021Sae		.find_byname = table_findbyname,
2916300021Sae		.find_bykidx = table_findbykidx,
2917300021Sae		.create_object = create_table_compat,
2918300021Sae		.manage_sets = table_manage_sets,
2919282070Smelifaro	},
2920282070Smelifaro};
2921282070Smelifaro
2922300021Saestatic int
2923300021Saetest_sets_cb(struct namedobj_instance *ni __unused, struct named_object *no,
2924300021Sae    void *arg __unused)
2925300021Sae{
2926282070Smelifaro
2927300021Sae	/* Check that there aren't any tables in not default set */
2928300021Sae	if (no->set != 0)
2929300021Sae		return (EBUSY);
2930300021Sae	return (0);
2931300021Sae}
2932300021Sae
2933272840Smelifaro/*
2934300021Sae * Switch between "set 0" and "rule's set" table binding,
2935300021Sae * Check all ruleset bindings and permits changing
2936300021Sae * IFF each binding has both rule AND table in default set (set 0).
2937300021Sae *
2938300021Sae * Returns 0 on success.
2939300021Sae */
2940300021Saeint
2941300021Saeipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int sets)
2942300021Sae{
2943300021Sae	struct opcode_obj_rewrite *rw;
2944300021Sae	struct namedobj_instance *ni;
2945300021Sae	struct named_object *no;
2946300021Sae	struct ip_fw *rule;
2947300021Sae	ipfw_insn *cmd;
2948300021Sae	int cmdlen, i, l;
2949300021Sae	uint16_t kidx;
2950300021Sae	uint8_t subtype;
2951300021Sae
2952300021Sae	IPFW_UH_WLOCK(ch);
2953300021Sae
2954300021Sae	if (V_fw_tables_sets == sets) {
2955300021Sae		IPFW_UH_WUNLOCK(ch);
2956300021Sae		return (0);
2957300021Sae	}
2958300021Sae	ni = CHAIN_TO_NI(ch);
2959300021Sae	if (sets == 0) {
2960300021Sae		/*
2961300021Sae		 * Prevent disabling sets support if we have some tables
2962300021Sae		 * in not default sets.
2963300021Sae		 */
2964300021Sae		if (ipfw_objhash_foreach_type(ni, test_sets_cb,
2965300021Sae		    NULL, IPFW_TLV_TBL_NAME) != 0) {
2966300021Sae			IPFW_UH_WUNLOCK(ch);
2967300021Sae			return (EBUSY);
2968300021Sae		}
2969300021Sae	}
2970300021Sae	/*
2971300021Sae	 * Scan all rules and examine tables opcodes.
2972300021Sae	 */
2973300021Sae	for (i = 0; i < ch->n_rules; i++) {
2974300021Sae		rule = ch->map[i];
2975300021Sae
2976300021Sae		l = rule->cmd_len;
2977300021Sae		cmd = rule->cmd;
2978300021Sae		cmdlen = 0;
2979300021Sae		for ( ;	l > 0 ; l -= cmdlen, cmd += cmdlen) {
2980300021Sae			cmdlen = F_LEN(cmd);
2981300021Sae			/* Check only tables opcodes */
2982300021Sae			for (kidx = 0, rw = opcodes;
2983300021Sae			    rw < opcodes + nitems(opcodes); rw++) {
2984300021Sae				if (rw->opcode != cmd->opcode)
2985300021Sae					continue;
2986300021Sae				if (rw->classifier(cmd, &kidx, &subtype) == 0)
2987300021Sae					break;
2988300021Sae			}
2989300021Sae			if (kidx == 0)
2990300021Sae				continue;
2991300021Sae			no = ipfw_objhash_lookup_kidx(ni, kidx);
2992300021Sae			/* Check if both table object and rule has the set 0 */
2993300021Sae			if (no->set != 0 || rule->set != 0) {
2994300021Sae				IPFW_UH_WUNLOCK(ch);
2995300021Sae				return (EBUSY);
2996300021Sae			}
2997300021Sae
2998300021Sae		}
2999300021Sae	}
3000300021Sae	V_fw_tables_sets = sets;
3001300021Sae	IPFW_UH_WUNLOCK(ch);
3002300021Sae	return (0);
3003300021Sae}
3004300021Sae
3005300021Sae/*
3006272840Smelifaro * Checks table name for validity.
3007272840Smelifaro * Enforce basic length checks, the rest
3008272840Smelifaro * should be done in userland.
3009272840Smelifaro *
3010272840Smelifaro * Returns 0 if name is considered valid.
3011272840Smelifaro */
3012290332Saestatic int
3013290332Saecheck_table_name(const char *name)
3014232865Smelifaro{
3015232865Smelifaro
3016272840Smelifaro	/*
3017272840Smelifaro	 * TODO: do some more complicated checks
3018272840Smelifaro	 */
3019290332Sae	return (ipfw_check_object_name_generic(name));
3020232865Smelifaro}
3021232865Smelifaro
3022272840Smelifaro/*
3023272840Smelifaro * Finds table config based on either legacy index
3024272840Smelifaro * or name in ntlv.
3025272840Smelifaro * Note @ti structure contains unchecked data from userland.
3026272840Smelifaro *
3027282070Smelifaro * Returns 0 in success and fills in @tc with found config
3028272840Smelifaro */
3029282070Smelifarostatic int
3030282070Smelifarofind_table_err(struct namedobj_instance *ni, struct tid_info *ti,
3031282070Smelifaro    struct table_config **tc)
3032272840Smelifaro{
3033272840Smelifaro	char *name, bname[16];
3034272840Smelifaro	struct named_object *no;
3035272840Smelifaro	ipfw_obj_ntlv *ntlv;
3036272840Smelifaro	uint32_t set;
3037272840Smelifaro
3038272840Smelifaro	if (ti->tlvs != NULL) {
3039299136Sae		ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3040299136Sae		    IPFW_TLV_TBL_NAME);
3041272840Smelifaro		if (ntlv == NULL)
3042282070Smelifaro			return (EINVAL);
3043272840Smelifaro		name = ntlv->name;
3044272840Smelifaro
3045272840Smelifaro		/*
3046272840Smelifaro		 * Use set provided by @ti instead of @ntlv one.
3047272840Smelifaro		 * This is needed due to different sets behavior
3048272840Smelifaro		 * controlled by V_fw_tables_sets.
3049272840Smelifaro		 */
3050300021Sae		set = (V_fw_tables_sets != 0) ? ti->set : 0;
3051272840Smelifaro	} else {
3052272840Smelifaro		snprintf(bname, sizeof(bname), "%d", ti->uidx);
3053272840Smelifaro		name = bname;
3054272840Smelifaro		set = 0;
3055272840Smelifaro	}
3056272840Smelifaro
3057272840Smelifaro	no = ipfw_objhash_lookup_name(ni, set, name);
3058282070Smelifaro	*tc = (struct table_config *)no;
3059272840Smelifaro
3060282070Smelifaro	return (0);
3061272840Smelifaro}
3062272840Smelifaro
3063272840Smelifaro/*
3064282070Smelifaro * Finds table config based on either legacy index
3065282070Smelifaro * or name in ntlv.
3066282070Smelifaro * Note @ti structure contains unchecked data from userland.
3067282070Smelifaro *
3068282070Smelifaro * Returns pointer to table_config or NULL.
3069282070Smelifaro */
3070282070Smelifarostatic struct table_config *
3071282070Smelifarofind_table(struct namedobj_instance *ni, struct tid_info *ti)
3072282070Smelifaro{
3073282070Smelifaro	struct table_config *tc;
3074282070Smelifaro
3075282070Smelifaro	if (find_table_err(ni, ti, &tc) != 0)
3076282070Smelifaro		return (NULL);
3077282070Smelifaro
3078282070Smelifaro	return (tc);
3079282070Smelifaro}
3080282070Smelifaro
3081282070Smelifaro/*
3082272840Smelifaro * Allocate new table config structure using
3083272840Smelifaro * specified @algo and @aname.
3084272840Smelifaro *
3085272840Smelifaro * Returns pointer to config or NULL.
3086272840Smelifaro */
3087272840Smelifarostatic struct table_config *
3088272840Smelifaroalloc_table_config(struct ip_fw_chain *ch, struct tid_info *ti,
3089272840Smelifaro    struct table_algo *ta, char *aname, uint8_t tflags)
3090272840Smelifaro{
3091272840Smelifaro	char *name, bname[16];
3092272840Smelifaro	struct table_config *tc;
3093272840Smelifaro	int error;
3094272840Smelifaro	ipfw_obj_ntlv *ntlv;
3095272840Smelifaro	uint32_t set;
3096272840Smelifaro
3097272840Smelifaro	if (ti->tlvs != NULL) {
3098299136Sae		ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3099299136Sae		    IPFW_TLV_TBL_NAME);
3100272840Smelifaro		if (ntlv == NULL)
3101272840Smelifaro			return (NULL);
3102272840Smelifaro		name = ntlv->name;
3103272840Smelifaro		set = ntlv->set;
3104272840Smelifaro	} else {
3105282070Smelifaro		/* Compat part: convert number to string representation */
3106272840Smelifaro		snprintf(bname, sizeof(bname), "%d", ti->uidx);
3107272840Smelifaro		name = bname;
3108272840Smelifaro		set = 0;
3109272840Smelifaro	}
3110272840Smelifaro
3111272840Smelifaro	tc = malloc(sizeof(struct table_config), M_IPFW, M_WAITOK | M_ZERO);
3112272840Smelifaro	tc->no.name = tc->tablename;
3113282070Smelifaro	tc->no.subtype = ta->type;
3114272840Smelifaro	tc->no.set = set;
3115272840Smelifaro	tc->tflags = tflags;
3116272840Smelifaro	tc->ta = ta;
3117272840Smelifaro	strlcpy(tc->tablename, name, sizeof(tc->tablename));
3118272840Smelifaro	/* Set "shared" value type by default */
3119272840Smelifaro	tc->vshared = 1;
3120272840Smelifaro
3121272840Smelifaro	/* Preallocate data structures for new tables */
3122272840Smelifaro	error = ta->init(ch, &tc->astate, &tc->ti_copy, aname, tflags);
3123272840Smelifaro	if (error != 0) {
3124272840Smelifaro		free(tc, M_IPFW);
3125272840Smelifaro		return (NULL);
3126272840Smelifaro	}
3127272840Smelifaro
3128272840Smelifaro	return (tc);
3129272840Smelifaro}
3130272840Smelifaro
3131272840Smelifaro/*
3132272840Smelifaro * Destroys table state and config.
3133272840Smelifaro */
3134272840Smelifarostatic void
3135272840Smelifarofree_table_config(struct namedobj_instance *ni, struct table_config *tc)
3136272840Smelifaro{
3137272840Smelifaro
3138272840Smelifaro	KASSERT(tc->linked == 0, ("free() on linked config"));
3139278259Smelifaro	/* UH lock MUST NOT be held */
3140272840Smelifaro
3141272840Smelifaro	/*
3142272840Smelifaro	 * We're using ta without any locking/referencing.
3143272840Smelifaro	 * TODO: fix this if we're going to use unloadable algos.
3144272840Smelifaro	 */
3145272840Smelifaro	tc->ta->destroy(tc->astate, &tc->ti_copy);
3146272840Smelifaro	free(tc, M_IPFW);
3147272840Smelifaro}
3148272840Smelifaro
3149272840Smelifaro/*
3150272840Smelifaro * Links @tc to @chain table named instance.
3151272840Smelifaro * Sets appropriate type/states in @chain table info.
3152272840Smelifaro */
3153272840Smelifarostatic void
3154272840Smelifarolink_table(struct ip_fw_chain *ch, struct table_config *tc)
3155272840Smelifaro{
3156272840Smelifaro	struct namedobj_instance *ni;
3157272840Smelifaro	struct table_info *ti;
3158272840Smelifaro	uint16_t kidx;
3159272840Smelifaro
3160272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
3161272840Smelifaro	IPFW_WLOCK_ASSERT(ch);
3162272840Smelifaro
3163272840Smelifaro	ni = CHAIN_TO_NI(ch);
3164272840Smelifaro	kidx = tc->no.kidx;
3165272840Smelifaro
3166272840Smelifaro	ipfw_objhash_add(ni, &tc->no);
3167272840Smelifaro
3168272840Smelifaro	ti = KIDX_TO_TI(ch, kidx);
3169272840Smelifaro	*ti = tc->ti_copy;
3170272840Smelifaro
3171272840Smelifaro	/* Notify algo on real @ti address */
3172272840Smelifaro	if (tc->ta->change_ti != NULL)
3173272840Smelifaro		tc->ta->change_ti(tc->astate, ti);
3174272840Smelifaro
3175272840Smelifaro	tc->linked = 1;
3176272840Smelifaro	tc->ta->refcnt++;
3177272840Smelifaro}
3178272840Smelifaro
3179272840Smelifaro/*
3180272840Smelifaro * Unlinks @tc from @chain table named instance.
3181272840Smelifaro * Zeroes states in @chain and stores them in @tc.
3182272840Smelifaro */
3183272840Smelifarostatic void
3184272840Smelifarounlink_table(struct ip_fw_chain *ch, struct table_config *tc)
3185272840Smelifaro{
3186272840Smelifaro	struct namedobj_instance *ni;
3187272840Smelifaro	struct table_info *ti;
3188272840Smelifaro	uint16_t kidx;
3189272840Smelifaro
3190272840Smelifaro	IPFW_UH_WLOCK_ASSERT(ch);
3191272840Smelifaro	IPFW_WLOCK_ASSERT(ch);
3192272840Smelifaro
3193272840Smelifaro	ni = CHAIN_TO_NI(ch);
3194272840Smelifaro	kidx = tc->no.kidx;
3195272840Smelifaro
3196272840Smelifaro	/* Clear state. @ti copy is already saved inside @tc */
3197272840Smelifaro	ipfw_objhash_del(ni, &tc->no);
3198272840Smelifaro	ti = KIDX_TO_TI(ch, kidx);
3199272840Smelifaro	memset(ti, 0, sizeof(struct table_info));
3200272840Smelifaro	tc->linked = 0;
3201272840Smelifaro	tc->ta->refcnt--;
3202272840Smelifaro
3203272840Smelifaro	/* Notify algo on real @ti address */
3204272840Smelifaro	if (tc->ta->change_ti != NULL)
3205272840Smelifaro		tc->ta->change_ti(tc->astate, NULL);
3206272840Smelifaro}
3207272840Smelifaro
3208272840Smelifarostatic struct ipfw_sopt_handler	scodes[] = {
3209272840Smelifaro	{ IP_FW_TABLE_XCREATE,	0,	HDIR_SET,	create_table },
3210272840Smelifaro	{ IP_FW_TABLE_XDESTROY,	0,	HDIR_SET,	flush_table_v0 },
3211272840Smelifaro	{ IP_FW_TABLE_XFLUSH,	0,	HDIR_SET,	flush_table_v0 },
3212272840Smelifaro	{ IP_FW_TABLE_XMODIFY,	0,	HDIR_BOTH,	modify_table },
3213272840Smelifaro	{ IP_FW_TABLE_XINFO,	0,	HDIR_GET,	describe_table },
3214272840Smelifaro	{ IP_FW_TABLES_XLIST,	0,	HDIR_GET,	list_tables },
3215272840Smelifaro	{ IP_FW_TABLE_XLIST,	0,	HDIR_GET,	dump_table_v0 },
3216272840Smelifaro	{ IP_FW_TABLE_XLIST,	1,	HDIR_GET,	dump_table_v1 },
3217272840Smelifaro	{ IP_FW_TABLE_XADD,	0,	HDIR_BOTH,	manage_table_ent_v0 },
3218272840Smelifaro	{ IP_FW_TABLE_XADD,	1,	HDIR_BOTH,	manage_table_ent_v1 },
3219272840Smelifaro	{ IP_FW_TABLE_XDEL,	0,	HDIR_BOTH,	manage_table_ent_v0 },
3220272840Smelifaro	{ IP_FW_TABLE_XDEL,	1,	HDIR_BOTH,	manage_table_ent_v1 },
3221272840Smelifaro	{ IP_FW_TABLE_XFIND,	0,	HDIR_GET,	find_table_entry },
3222272840Smelifaro	{ IP_FW_TABLE_XSWAP,	0,	HDIR_SET,	swap_table },
3223272840Smelifaro	{ IP_FW_TABLES_ALIST,	0,	HDIR_GET,	list_table_algo },
3224272840Smelifaro	{ IP_FW_TABLE_XGETSIZE,	0,	HDIR_GET,	get_table_size },
3225272840Smelifaro};
3226272840Smelifaro
3227299152Saestatic int
3228272840Smelifarodestroy_table_locked(struct namedobj_instance *ni, struct named_object *no,
3229272840Smelifaro    void *arg)
3230272840Smelifaro{
3231272840Smelifaro
3232272840Smelifaro	unlink_table((struct ip_fw_chain *)arg, (struct table_config *)no);
3233272840Smelifaro	if (ipfw_objhash_free_idx(ni, no->kidx) != 0)
3234272840Smelifaro		printf("Error unlinking kidx %d from table %s\n",
3235272840Smelifaro		    no->kidx, no->name);
3236272840Smelifaro	free_table_config(ni, (struct table_config *)no);
3237299152Sae	return (0);
3238272840Smelifaro}
3239272840Smelifaro
3240272840Smelifaro/*
3241272840Smelifaro * Shuts tables module down.
3242272840Smelifaro */
3243272840Smelifarovoid
3244272840Smelifaroipfw_destroy_tables(struct ip_fw_chain *ch, int last)
3245272840Smelifaro{
3246272840Smelifaro
3247272840Smelifaro	IPFW_DEL_SOPT_HANDLER(last, scodes);
3248282070Smelifaro	IPFW_DEL_OBJ_REWRITER(last, opcodes);
3249272840Smelifaro
3250272840Smelifaro	/* Remove all tables from working set */
3251272840Smelifaro	IPFW_UH_WLOCK(ch);
3252272840Smelifaro	IPFW_WLOCK(ch);
3253272840Smelifaro	ipfw_objhash_foreach(CHAIN_TO_NI(ch), destroy_table_locked, ch);
3254272840Smelifaro	IPFW_WUNLOCK(ch);
3255272840Smelifaro	IPFW_UH_WUNLOCK(ch);
3256272840Smelifaro
3257272840Smelifaro	/* Free pointers itself */
3258272840Smelifaro	free(ch->tablestate, M_IPFW);
3259272840Smelifaro
3260272840Smelifaro	ipfw_table_value_destroy(ch, last);
3261272840Smelifaro	ipfw_table_algo_destroy(ch);
3262272840Smelifaro
3263272840Smelifaro	ipfw_objhash_destroy(CHAIN_TO_NI(ch));
3264272840Smelifaro	free(CHAIN_TO_TCFG(ch), M_IPFW);
3265272840Smelifaro}
3266272840Smelifaro
3267272840Smelifaro/*
3268272840Smelifaro * Starts tables module.
3269272840Smelifaro */
3270272840Smelifaroint
3271272840Smelifaroipfw_init_tables(struct ip_fw_chain *ch, int first)
3272272840Smelifaro{
3273272840Smelifaro	struct tables_config *tcfg;
3274272840Smelifaro
3275272840Smelifaro	/* Allocate pointers */
3276272840Smelifaro	ch->tablestate = malloc(V_fw_tables_max * sizeof(struct table_info),
3277272840Smelifaro	    M_IPFW, M_WAITOK | M_ZERO);
3278272840Smelifaro
3279272840Smelifaro	tcfg = malloc(sizeof(struct tables_config), M_IPFW, M_WAITOK | M_ZERO);
3280272840Smelifaro	tcfg->namehash = ipfw_objhash_create(V_fw_tables_max);
3281272840Smelifaro	ch->tblcfg = tcfg;
3282272840Smelifaro
3283272840Smelifaro	ipfw_table_value_init(ch, first);
3284272840Smelifaro	ipfw_table_algo_init(ch);
3285272840Smelifaro
3286282070Smelifaro	IPFW_ADD_OBJ_REWRITER(first, opcodes);
3287272840Smelifaro	IPFW_ADD_SOPT_HANDLER(first, scodes);
3288272840Smelifaro	return (0);
3289272840Smelifaro}
3290272840Smelifaro
3291272840Smelifaro
3292272840Smelifaro
3293