2009-12-15 21:24:12 +00:00
|
|
|
/*-
|
2009-12-18 17:22:21 +00:00
|
|
|
* Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
|
2009-12-15 21:24:12 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
/*
|
2014-06-28 23:20:24 +00:00
|
|
|
* Lookup table support for ipfw.
|
2009-12-16 10:48:40 +00:00
|
|
|
*
|
2014-07-03 22:25:59 +00:00
|
|
|
* This file contains handlers for all generic tables' operations:
|
2014-06-28 23:20:24 +00:00
|
|
|
* add/del/flush entries, list/dump tables etc..
|
2009-12-22 13:53:34 +00:00
|
|
|
*
|
2014-06-28 23:20:24 +00:00
|
|
|
* Table data modification is protected by both UH and runtimg lock
|
|
|
|
* while reading configuration/data is protected by UH lock.
|
|
|
|
*
|
|
|
|
* Lookup algorithms for all table types are located in ip_fw_table_algo.c
|
2009-12-15 21:24:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_ipfw.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/rwlock.h>
|
|
|
|
#include <sys/socket.h>
|
2014-06-14 22:47:25 +00:00
|
|
|
#include <sys/socketvar.h>
|
2012-09-14 11:51:49 +00:00
|
|
|
#include <sys/queue.h>
|
2009-12-15 21:24:12 +00:00
|
|
|
#include <net/if.h> /* ip_fw.h requires IFNAMSIZ */
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2010-01-07 10:08:05 +00:00
|
|
|
#include <netinet/ip_var.h> /* struct ipfw_rule_ref */
|
2009-12-15 21:24:12 +00:00
|
|
|
#include <netinet/ip_fw.h>
|
2012-09-14 11:51:49 +00:00
|
|
|
|
|
|
|
#include <netpfil/ipfw/ip_fw_private.h>
|
2014-06-14 11:13:02 +00:00
|
|
|
#include <netpfil/ipfw/ip_fw_table.h>
|
2009-12-15 21:24:12 +00:00
|
|
|
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Table has the following `type` concepts:
|
|
|
|
*
|
2014-06-14 10:58:39 +00:00
|
|
|
* `no.type` represents lookup key type (cidr, ifp, uid, etc..)
|
|
|
|
* `ta->atype` represents exact lookup algorithm.
|
2014-06-12 09:59:11 +00:00
|
|
|
* For example, we can use more efficient search schemes if we plan
|
|
|
|
* to use some specific table for storing host-routes only.
|
2014-06-16 13:05:07 +00:00
|
|
|
* `ftype` (at the moment )is pure userland field helping to properly
|
|
|
|
* format value data e.g. "value is IPv4 nexthop" or "value is DSCP"
|
|
|
|
* or "value is port".
|
2014-06-12 09:59:11 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct table_config {
|
|
|
|
struct named_object no;
|
2014-07-03 22:25:59 +00:00
|
|
|
uint8_t vtype; /* format table type */
|
2014-06-12 09:59:11 +00:00
|
|
|
uint8_t linked; /* 1 if already linked */
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
uint8_t tflags; /* type flags */
|
2014-08-07 21:37:31 +00:00
|
|
|
uint8_t ochanged; /* used by set swapping */
|
2014-06-12 09:59:11 +00:00
|
|
|
uint32_t count; /* Number of records */
|
2014-08-01 15:17:46 +00:00
|
|
|
uint32_t limit; /* Max number of records */
|
2014-08-07 21:37:31 +00:00
|
|
|
uint32_t ocount; /* used by set swapping */
|
|
|
|
uint64_t gencnt; /* generation count */
|
2014-06-12 09:59:11 +00:00
|
|
|
char tablename[64]; /* table name */
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_algo *ta; /* Callbacks for given algo */
|
|
|
|
void *astate; /* algorithm state */
|
|
|
|
struct table_info ti; /* data to put to table_info */
|
2014-06-12 09:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tables_config {
|
|
|
|
struct namedobj_instance *namehash;
|
2014-06-14 10:58:39 +00:00
|
|
|
int algo_count;
|
|
|
|
struct table_algo *algo[256];
|
2014-08-01 07:35:17 +00:00
|
|
|
struct table_algo *def_algo[IPFW_TABLE_MAXTYPE + 1];
|
2014-06-12 09:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct table_config *find_table(struct namedobj_instance *ni,
|
|
|
|
struct tid_info *ti);
|
2014-07-28 19:01:25 +00:00
|
|
|
static struct table_config *alloc_table_config(struct ip_fw_chain *ch,
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
struct tid_info *ti, struct table_algo *ta, char *adata, uint8_t tflags,
|
|
|
|
uint8_t vtype);
|
2014-06-12 09:59:11 +00:00
|
|
|
static void free_table_config(struct namedobj_instance *ni,
|
|
|
|
struct table_config *tc);
|
2014-07-26 13:37:25 +00:00
|
|
|
static int create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
|
2014-08-01 15:17:46 +00:00
|
|
|
char *aname, ipfw_xtable_info *i);
|
2014-06-12 09:59:11 +00:00
|
|
|
static void link_table(struct ip_fw_chain *chain, struct table_config *tc);
|
|
|
|
static void unlink_table(struct ip_fw_chain *chain, struct table_config *tc);
|
|
|
|
static void free_table_state(void **state, void **xstate, uint8_t type);
|
2014-06-27 10:07:00 +00:00
|
|
|
static int export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
|
|
|
|
struct sockopt_data *sd);
|
2014-07-03 22:25:59 +00:00
|
|
|
static void export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
|
|
|
|
ipfw_xtable_info *i);
|
2014-07-06 18:16:04 +00:00
|
|
|
static int dump_table_tentry(void *e, void *arg);
|
2014-06-14 22:47:25 +00:00
|
|
|
static int dump_table_xentry(void *e, void *arg);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
static int ipfw_dump_table_v0(struct ip_fw_chain *ch, struct sockopt_data *sd);
|
|
|
|
static int ipfw_dump_table_v1(struct ip_fw_chain *ch, struct sockopt_data *sd);
|
2014-07-26 13:37:25 +00:00
|
|
|
static int ipfw_manage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
2014-07-03 22:25:59 +00:00
|
|
|
struct sockopt_data *sd);
|
2014-07-26 13:37:25 +00:00
|
|
|
static int ipfw_manage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
2014-07-03 22:25:59 +00:00
|
|
|
struct sockopt_data *sd);
|
2014-08-07 21:37:31 +00:00
|
|
|
static int swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
|
2014-08-03 21:37:12 +00:00
|
|
|
struct tid_info *b);
|
2014-07-03 22:25:59 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
static int check_table_space(struct ip_fw_chain *ch, struct table_config *tc,
|
|
|
|
struct table_info *ti, uint32_t count);
|
2014-07-03 22:25:59 +00:00
|
|
|
static int destroy_table(struct ip_fw_chain *ch, struct tid_info *ti);
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
static struct table_algo *find_table_algo(struct tables_config *tableconf,
|
2014-06-16 13:05:07 +00:00
|
|
|
struct tid_info *ti, char *name);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-08-03 21:37:12 +00:00
|
|
|
static void objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti);
|
|
|
|
static void ntlv_to_ti(struct _ipfw_obj_ntlv *ntlv, struct tid_info *ti);
|
2014-08-07 21:37:31 +00:00
|
|
|
static int classify_table_opcode(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype);
|
2014-08-03 21:37:12 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
#define CHAIN_TO_TCFG(chain) ((struct tables_config *)(chain)->tblcfg)
|
|
|
|
#define CHAIN_TO_NI(chain) (CHAIN_TO_TCFG(chain)->namehash)
|
2014-06-14 10:58:39 +00:00
|
|
|
#define KIDX_TO_TI(ch, k) (&(((struct table_info *)(ch)->tablestate)[k]))
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
#define TA_BUF_SZ 128 /* On-stack buffer for add/delete state */
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2009-12-15 21:24:12 +00:00
|
|
|
int
|
2014-07-04 07:02:11 +00:00
|
|
|
add_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
|
2014-08-02 17:18:47 +00:00
|
|
|
struct tentry_info *tei, uint32_t count)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
2014-07-26 13:37:25 +00:00
|
|
|
struct table_config *tc;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_algo *ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobj_instance *ni;
|
|
|
|
uint16_t kidx;
|
2014-06-14 10:58:39 +00:00
|
|
|
int error;
|
2014-07-29 08:00:13 +00:00
|
|
|
uint32_t num;
|
2014-08-02 17:18:47 +00:00
|
|
|
ipfw_xtable_info *xi;
|
|
|
|
char ta_buf[TA_BUF_SZ];
|
2009-12-15 21:24:12 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/*
|
|
|
|
* Find and reference existing table.
|
|
|
|
*/
|
|
|
|
ta = NULL;
|
|
|
|
if ((tc = find_table(ni, ti)) != NULL) {
|
|
|
|
/* check table type */
|
|
|
|
if (tc->no.type != ti->type) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-12 14:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:17:46 +00:00
|
|
|
/* Try to exit early on limit hit */
|
|
|
|
if (tc->limit != 0 && tc->count == tc->limit &&
|
|
|
|
(tei->flags & TEI_FLAGS_UPDATE) == 0) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Reference and unlock */
|
|
|
|
tc->no.refcnt++;
|
|
|
|
ta = tc->ta;
|
2012-03-12 14:07:57 +00:00
|
|
|
}
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
if (tc == NULL) {
|
2014-07-26 13:37:25 +00:00
|
|
|
/* Compability mode: create new table for old clients */
|
|
|
|
if ((tei->flags & TEI_FLAGS_COMPAT) == 0)
|
|
|
|
return (ESRCH);
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
xi = malloc(sizeof(ipfw_xtable_info), M_TEMP, M_WAITOK|M_ZERO);
|
|
|
|
xi->vtype = IPFW_VTYPE_U32;
|
2014-08-01 15:17:46 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
error = create_table_internal(ch, ti, NULL, xi);
|
|
|
|
free(xi, M_TEMP);
|
2014-07-26 13:37:25 +00:00
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* Let's try to find & reference another time */
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
if ((tc = find_table(ni, ti)) == NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tc->no.type != ti->type) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reference and unlock */
|
|
|
|
tc->no.refcnt++;
|
|
|
|
ta = tc->ta;
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Prepare record (allocate memory) */
|
|
|
|
memset(&ta_buf, 0, sizeof(ta_buf));
|
2014-07-28 19:01:25 +00:00
|
|
|
error = ta->prepare_add(ch, tei, &ta_buf);
|
2014-07-26 13:37:25 +00:00
|
|
|
if (error != 0)
|
2014-06-14 10:58:39 +00:00
|
|
|
return (error);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/*
|
|
|
|
* Ensure we are able to add all entries without additional
|
|
|
|
* memory allocations. May release/reacquire UH_WLOCK.
|
|
|
|
*/
|
|
|
|
kidx = tc->no.kidx;
|
|
|
|
error = check_table_space(ch, tc, KIDX_TO_TI(ch, kidx), count);
|
|
|
|
if (error != 0) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
ta->flush_entry(ch, tei, &ta_buf);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
2014-07-26 13:37:25 +00:00
|
|
|
/* Drop reference we've used in first search */
|
|
|
|
tc->no.refcnt--;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-08-01 15:17:46 +00:00
|
|
|
/* Check limit before adding */
|
|
|
|
if (tc->limit != 0 && tc->count == tc->limit) {
|
|
|
|
if ((tei->flags & TEI_FLAGS_UPDATE) == 0) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2014-08-02 17:18:47 +00:00
|
|
|
ta->flush_entry(ch, tei, &ta_buf);
|
2014-08-01 15:17:46 +00:00
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have UPDATE flag set.
|
|
|
|
* Permit updating record (if found),
|
|
|
|
* but restrict adding new one since we've
|
|
|
|
* already hit the limit.
|
|
|
|
*/
|
|
|
|
tei->flags |= TEI_FLAGS_DONTADD;
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* We've got valid table in @tc. Let's add data */
|
2014-06-12 09:59:11 +00:00
|
|
|
kidx = tc->no.kidx;
|
2014-06-14 10:58:39 +00:00
|
|
|
ta = tc->ta;
|
2014-07-29 08:00:13 +00:00
|
|
|
num = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
IPFW_WLOCK(ch);
|
2014-08-02 17:18:47 +00:00
|
|
|
error = ta->add(tc->astate, KIDX_TO_TI(ch, kidx), tei, &ta_buf, &num);
|
2009-12-15 21:24:12 +00:00
|
|
|
IPFW_WUNLOCK(ch);
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
/* Update number of records. */
|
2014-08-02 17:18:47 +00:00
|
|
|
if (error == 0) {
|
2014-07-29 08:00:13 +00:00
|
|
|
tc->count += num;
|
2014-08-02 17:18:47 +00:00
|
|
|
/* Permit post-add algorithm grow/rehash. */
|
|
|
|
error = check_table_space(ch, tc, KIDX_TO_TI(ch, kidx), 0);
|
|
|
|
}
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
/* Run cleaning callback anyway */
|
2014-07-28 19:01:25 +00:00
|
|
|
ta->flush_entry(ch, tei, &ta_buf);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
return (error);
|
2009-12-15 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-07-04 07:02:11 +00:00
|
|
|
del_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
|
2014-08-02 17:18:47 +00:00
|
|
|
struct tentry_info *tei, uint32_t count)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
2014-06-12 09:59:11 +00:00
|
|
|
struct table_config *tc;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_algo *ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobj_instance *ni;
|
|
|
|
uint16_t kidx;
|
2014-06-14 10:58:39 +00:00
|
|
|
int error;
|
2014-07-29 08:00:13 +00:00
|
|
|
uint32_t num;
|
2014-08-02 17:18:47 +00:00
|
|
|
char ta_buf[TA_BUF_SZ];
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
if ((tc = find_table(ni, ti)) == NULL) {
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2009-12-15 21:24:12 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
if (tc->no.type != ti->type) {
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-12 14:07:57 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ta = tc->ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/*
|
|
|
|
* Give a chance for algorithm to shrink.
|
|
|
|
* May release/reacquire UH_WLOCK.
|
|
|
|
*/
|
|
|
|
kidx = tc->no.kidx;
|
|
|
|
error = check_table_space(ch, tc, KIDX_TO_TI(ch, kidx), 0);
|
|
|
|
if (error != 0) {
|
2014-07-26 13:37:25 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2014-08-02 17:18:47 +00:00
|
|
|
ta->flush_entry(ch, tei, &ta_buf);
|
|
|
|
return (error);
|
2014-07-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We assume ta_buf size is enough for storing
|
2014-08-02 17:18:47 +00:00
|
|
|
* prepare_del() key, so we're running under UH_WLOCK here.
|
2014-07-26 13:37:25 +00:00
|
|
|
*/
|
2014-06-14 10:58:39 +00:00
|
|
|
memset(&ta_buf, 0, sizeof(ta_buf));
|
2014-07-28 19:01:25 +00:00
|
|
|
if ((error = ta->prepare_del(ch, tei, &ta_buf)) != 0) {
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (error);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
kidx = tc->no.kidx;
|
2014-07-29 08:00:13 +00:00
|
|
|
num = 0;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_WLOCK(ch);
|
2014-08-02 17:18:47 +00:00
|
|
|
error = ta->del(tc->astate, KIDX_TO_TI(ch, kidx), tei, &ta_buf, &num);
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_WUNLOCK(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
if (error == 0) {
|
2014-07-29 08:00:13 +00:00
|
|
|
tc->count -= num;
|
2014-08-02 17:18:47 +00:00
|
|
|
/* Run post-del hook to permit shrinking */
|
|
|
|
error = check_table_space(ch, tc, KIDX_TO_TI(ch, kidx), 0);
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2009-12-15 21:24:12 +00:00
|
|
|
|
2014-07-28 19:01:25 +00:00
|
|
|
ta->flush_entry(ch, tei, &ta_buf);
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-07-26 13:37:25 +00:00
|
|
|
/*
|
2014-08-02 17:18:47 +00:00
|
|
|
* Ensure that table @tc has enough space to add @count entries without
|
|
|
|
* need for reallocation.
|
2014-07-26 13:37:25 +00:00
|
|
|
*
|
|
|
|
* Callbacks order:
|
2014-08-02 17:18:47 +00:00
|
|
|
* 0) has_space() (UH_WLOCK) - checks if @count items can be added w/o resize.
|
|
|
|
*
|
2014-07-26 13:37:25 +00:00
|
|
|
* 1) alloc_modify (no locks, M_WAITOK) - alloc new state based on @pflags.
|
|
|
|
* 2) prepare_modifyt (UH_WLOCK) - copy old data into new storage
|
|
|
|
* 3) modify (UH_WLOCK + WLOCK) - switch pointers
|
2014-08-02 17:18:47 +00:00
|
|
|
* 4) flush_modify (UH_WLOCK) - free state, if needed
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
2014-07-26 13:37:25 +00:00
|
|
|
*/
|
|
|
|
static int
|
2014-08-02 17:18:47 +00:00
|
|
|
check_table_space(struct ip_fw_chain *ch, struct table_config *tc,
|
|
|
|
struct table_info *ti, uint32_t count)
|
2014-07-26 13:37:25 +00:00
|
|
|
{
|
2014-08-02 17:18:47 +00:00
|
|
|
struct table_algo *ta;
|
|
|
|
uint64_t pflags;
|
|
|
|
char ta_buf[TA_BUF_SZ];
|
2014-07-26 13:37:25 +00:00
|
|
|
int error;
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
IPFW_UH_WLOCK_ASSERT(ch);
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
error = 0;
|
|
|
|
ta = tc->ta;
|
|
|
|
/* Acquire reference not to loose @tc between locks/unlocks */
|
|
|
|
tc->no.refcnt++;
|
2014-07-26 13:37:25 +00:00
|
|
|
|
|
|
|
/*
|
2014-08-02 17:18:47 +00:00
|
|
|
* TODO: think about avoiding race between large add/large delete
|
|
|
|
* operation on algorithm which implements shrinking along with
|
|
|
|
* growing.
|
2014-07-26 13:37:25 +00:00
|
|
|
*/
|
2014-08-02 17:18:47 +00:00
|
|
|
while (true) {
|
|
|
|
pflags = 0;
|
|
|
|
if (ta->has_space(tc->astate, ti, count, &pflags) != 0) {
|
|
|
|
tc->no.refcnt--;
|
|
|
|
return (0);
|
|
|
|
}
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/* We have to shrink/grow table */
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
memset(&ta_buf, 0, sizeof(ta_buf));
|
|
|
|
|
|
|
|
if ((error = ta->prepare_mod(ta_buf, &pflags)) != 0) {
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
break;
|
|
|
|
}
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/* Check if we still need to alter table */
|
|
|
|
ti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
if (ta->has_space(tc->astate, ti, count, &pflags) != 0) {
|
2014-07-26 13:37:25 +00:00
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/*
|
|
|
|
* Other threads has already performed resize.
|
|
|
|
* Flush our state and return/
|
|
|
|
*/
|
|
|
|
ta->flush_mod(ta_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = ta->fill_mod(tc->astate, ti, ta_buf, &pflags);
|
|
|
|
if (error == 0) {
|
|
|
|
/* Do actual modification */
|
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
ta->modify(tc->astate, ti, ta_buf, pflags);
|
|
|
|
IPFW_WUNLOCK(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Anyway, flush data and retry */
|
|
|
|
ta->flush_mod(ta_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->no.refcnt--;
|
2014-07-26 13:37:25 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
int
|
2014-07-26 13:37:25 +00:00
|
|
|
ipfw_manage_table_ent(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
2014-07-03 22:25:59 +00:00
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch (op3->version) {
|
|
|
|
case 0:
|
2014-07-26 13:37:25 +00:00
|
|
|
error = ipfw_manage_table_ent_v0(ch, op3, sd);
|
2014-07-03 22:25:59 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-07-26 13:37:25 +00:00
|
|
|
error = ipfw_manage_table_ent_v1(ch, op3, sd);
|
2014-07-03 22:25:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
2009-12-15 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
2014-07-03 22:25:59 +00:00
|
|
|
* Adds or deletes record in table.
|
|
|
|
* Data layout (v0):
|
|
|
|
* Request: [ ip_fw3_opheader ipfw_table_xentry ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
2014-06-12 09:59:11 +00:00
|
|
|
*/
|
2014-07-03 22:25:59 +00:00
|
|
|
static int
|
2014-07-26 13:37:25 +00:00
|
|
|
ipfw_manage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
2014-07-03 22:25:59 +00:00
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
ipfw_table_xentry *xent;
|
|
|
|
struct tentry_info tei;
|
|
|
|
struct tid_info ti;
|
|
|
|
int error, hdrlen, read;
|
|
|
|
|
|
|
|
hdrlen = offsetof(ipfw_table_xentry, k);
|
|
|
|
|
|
|
|
/* Check minimum header size */
|
|
|
|
if (sd->valsize < (sizeof(*op3) + hdrlen))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
read = sizeof(ip_fw3_opheader);
|
|
|
|
|
|
|
|
/* Check if xentry len field is valid */
|
|
|
|
xent = (ipfw_table_xentry *)(op3 + 1);
|
|
|
|
if (xent->len < hdrlen || xent->len + read > sd->valsize)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
memset(&tei, 0, sizeof(tei));
|
|
|
|
tei.paddr = &xent->k;
|
|
|
|
tei.masklen = xent->masklen;
|
|
|
|
tei.value = xent->value;
|
|
|
|
/* Old requests compability */
|
2014-07-26 13:37:25 +00:00
|
|
|
tei.flags = TEI_FLAGS_COMPAT;
|
2014-07-03 22:25:59 +00:00
|
|
|
if (xent->type == IPFW_TABLE_CIDR) {
|
|
|
|
if (xent->len - hdrlen == sizeof(in_addr_t))
|
|
|
|
tei.subtype = AF_INET;
|
|
|
|
else
|
|
|
|
tei.subtype = AF_INET6;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ti, 0, sizeof(ti));
|
|
|
|
ti.uidx = xent->tbl;
|
|
|
|
ti.type = xent->type;
|
|
|
|
|
|
|
|
error = (op3->opcode == IP_FW_TABLE_XADD) ?
|
2014-08-02 17:18:47 +00:00
|
|
|
add_table_entry(ch, &ti, &tei, 1) :
|
|
|
|
del_table_entry(ch, &ti, &tei, 1);
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds or deletes record in table.
|
|
|
|
* Data layout (v1)(current):
|
2014-07-26 13:37:25 +00:00
|
|
|
* Request: [ ipfw_obj_header
|
|
|
|
* ipfw_obj_ctlv(IPFW_TLV_TBLENT_LIST) [ ipfw_obj_tentry x N ]
|
|
|
|
* ]
|
2014-07-03 22:25:59 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-26 13:37:25 +00:00
|
|
|
ipfw_manage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
2014-07-03 22:25:59 +00:00
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
ipfw_obj_tentry *tent;
|
2014-07-26 13:37:25 +00:00
|
|
|
ipfw_obj_ctlv *ctlv;
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_obj_header *oh;
|
|
|
|
struct tentry_info tei;
|
|
|
|
struct tid_info ti;
|
|
|
|
int error, read;
|
|
|
|
|
|
|
|
/* Check minimum header size */
|
2014-07-26 13:37:25 +00:00
|
|
|
if (sd->valsize < (sizeof(*oh) + sizeof(*ctlv)))
|
2014-07-03 22:25:59 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Check if passed data is too long */
|
|
|
|
if (sd->valsize != sd->kavail)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
oh = (ipfw_obj_header *)sd->kbuf;
|
|
|
|
|
|
|
|
/* Basic length checks for TLVs */
|
|
|
|
if (oh->ntlv.head.length != sizeof(oh->ntlv))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
read = sizeof(*oh);
|
|
|
|
|
2014-07-26 13:37:25 +00:00
|
|
|
ctlv = (ipfw_obj_ctlv *)(oh + 1);
|
|
|
|
if (ctlv->head.length + read != sd->valsize)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: permit adding multiple entries for given table
|
|
|
|
* at once
|
|
|
|
*/
|
|
|
|
if (ctlv->count != 1)
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
read += sizeof(*ctlv);
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
/* Assume tentry may grow to support larger keys */
|
2014-07-26 13:37:25 +00:00
|
|
|
tent = (ipfw_obj_tentry *)(ctlv + 1);
|
2014-07-03 22:25:59 +00:00
|
|
|
if (tent->head.length < sizeof(*tent) ||
|
|
|
|
tent->head.length + read > sd->valsize)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
/* Convert data into kernel request objects */
|
2014-07-03 22:25:59 +00:00
|
|
|
memset(&tei, 0, sizeof(tei));
|
|
|
|
tei.paddr = &tent->k;
|
|
|
|
tei.subtype = tent->subtype;
|
|
|
|
tei.masklen = tent->masklen;
|
2014-07-06 18:16:04 +00:00
|
|
|
if (tent->head.flags & IPFW_TF_UPDATE)
|
2014-07-03 22:25:59 +00:00
|
|
|
tei.flags |= TEI_FLAGS_UPDATE;
|
|
|
|
tei.value = tent->value;
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
objheader_to_ti(oh, &ti);
|
2014-07-03 22:25:59 +00:00
|
|
|
ti.type = oh->ntlv.type;
|
2014-07-06 18:16:04 +00:00
|
|
|
ti.uidx = tent->idx;
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
error = (oh->opheader.opcode == IP_FW_TABLE_XADD) ?
|
2014-08-02 17:18:47 +00:00
|
|
|
add_table_entry(ch, &ti, &tei, 1) :
|
|
|
|
del_table_entry(ch, &ti, &tei, 1);
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
/*
|
|
|
|
* Looks up an entry in given table.
|
|
|
|
* Data layout (v0)(current):
|
|
|
|
* Request: [ ipfw_obj_header ipfw_obj_tentry ]
|
|
|
|
* Reply: [ ipfw_obj_header ipfw_obj_tentry ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_find_table_entry(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
ipfw_obj_tentry *tent;
|
|
|
|
ipfw_obj_header *oh;
|
|
|
|
struct tid_info ti;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
struct table_info *kti;
|
|
|
|
struct namedobj_instance *ni;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
int error;
|
2014-07-06 18:16:04 +00:00
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
/* Check minimum header size */
|
|
|
|
sz = sizeof(*oh) + sizeof(*tent);
|
|
|
|
if (sd->valsize != sz)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
|
|
|
|
tent = (ipfw_obj_tentry *)(oh + 1);
|
|
|
|
|
|
|
|
/* Basic length checks for TLVs */
|
|
|
|
if (oh->ntlv.head.length != sizeof(oh->ntlv))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
objheader_to_ti(oh, &ti);
|
|
|
|
ti.type = oh->ntlv.type;
|
|
|
|
ti.uidx = tent->idx;
|
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find existing table and check its type .
|
|
|
|
*/
|
|
|
|
ta = NULL;
|
|
|
|
if ((tc = find_table(ni, &ti)) == NULL) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check table type */
|
|
|
|
if (tc->no.type != ti.type) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
kti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
ta = tc->ta;
|
|
|
|
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
if (ta->find_tentry == NULL)
|
|
|
|
return (ENOTSUP);
|
|
|
|
|
|
|
|
error = ta->find_tentry(tc->astate, kti, tent);
|
2014-07-06 18:16:04 +00:00
|
|
|
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-08-03 21:37:12 +00:00
|
|
|
/*
|
|
|
|
* Flushes all entries or destroys given table.
|
|
|
|
* Data layout (v0)(current):
|
|
|
|
* Request: [ ipfw_obj_header ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
2009-12-15 21:24:12 +00:00
|
|
|
int
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_flush_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct _ipfw_obj_header *oh;
|
|
|
|
struct tid_info ti;
|
|
|
|
|
|
|
|
if (sd->valsize != sizeof(*oh))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
oh = (struct _ipfw_obj_header *)op3;
|
|
|
|
objheader_to_ti(oh, &ti);
|
|
|
|
|
2014-07-04 07:02:11 +00:00
|
|
|
if (op3->opcode == IP_FW_TABLE_XDESTROY)
|
2014-07-03 22:25:59 +00:00
|
|
|
error = destroy_table(ch, &ti);
|
2014-07-04 07:02:11 +00:00
|
|
|
else if (op3->opcode == IP_FW_TABLE_XFLUSH)
|
2014-07-03 22:25:59 +00:00
|
|
|
error = flush_table(ch, &ti);
|
|
|
|
else
|
|
|
|
return (ENOTSUP);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-07-04 07:02:11 +00:00
|
|
|
int
|
2014-07-03 22:25:59 +00:00
|
|
|
flush_table(struct ip_fw_chain *ch, struct tid_info *ti)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct table_config *tc;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_algo *ta;
|
|
|
|
struct table_info ti_old, ti_new, *tablestate;
|
|
|
|
void *astate_old, *astate_new;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
char algostate[64], *pstate;
|
2014-06-12 09:59:11 +00:00
|
|
|
int error;
|
|
|
|
uint16_t kidx;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
uint8_t tflags;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2012-03-12 14:07:57 +00:00
|
|
|
/*
|
2014-06-14 10:58:39 +00:00
|
|
|
* Stage 1: save table algoritm.
|
2014-06-12 09:59:11 +00:00
|
|
|
* Reference found table to ensure it won't disappear.
|
2012-03-12 14:07:57 +00:00
|
|
|
*/
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
if ((tc = find_table(ni, ti)) == NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
2014-06-14 10:58:39 +00:00
|
|
|
ta = tc->ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
tc->no.refcnt++;
|
2014-07-30 09:17:40 +00:00
|
|
|
/* Save statup algo parameters */
|
|
|
|
if (ta->print_config != NULL) {
|
|
|
|
ta->print_config(tc->astate, KIDX_TO_TI(ch, tc->no.kidx),
|
|
|
|
algostate, sizeof(algostate));
|
|
|
|
pstate = algostate;
|
|
|
|
} else
|
|
|
|
pstate = NULL;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
tflags = tc->tflags;
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
|
|
|
/*
|
2014-06-14 10:58:39 +00:00
|
|
|
* Stage 2: allocate new table instance using same algo.
|
2014-06-12 09:59:11 +00:00
|
|
|
*/
|
2014-06-14 10:58:39 +00:00
|
|
|
memset(&ti_new, 0, sizeof(struct table_info));
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
if ((error = ta->init(ch, &astate_new, &ti_new, pstate, tflags)) != 0) {
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
tc->no.refcnt--;
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (error);
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Stage 3: swap old state pointers with newly-allocated ones.
|
|
|
|
* Decrease refcount.
|
|
|
|
*/
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
kidx = tc->no.kidx;
|
2014-06-14 10:58:39 +00:00
|
|
|
tablestate = (struct table_info *)ch->tablestate;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
ti_old = tablestate[kidx];
|
|
|
|
tablestate[kidx] = ti_new;
|
|
|
|
IPFW_WUNLOCK(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
astate_old = tc->astate;
|
|
|
|
tc->astate = astate_new;
|
|
|
|
tc->ti = ti_new;
|
|
|
|
tc->count = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
tc->no.refcnt--;
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Stage 4: perform real flush.
|
|
|
|
*/
|
2014-06-14 10:58:39 +00:00
|
|
|
ta->destroy(astate_old, &ti_old);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-03 21:37:12 +00:00
|
|
|
/*
|
|
|
|
* Swaps two tables.
|
|
|
|
* Data layout (v0)(current):
|
|
|
|
* Request: [ ipfw_obj_header ipfw_obj_ntlv ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_swap_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct _ipfw_obj_header *oh;
|
|
|
|
struct tid_info ti_a, ti_b;
|
|
|
|
|
|
|
|
if (sd->valsize != sizeof(*oh) + sizeof(ipfw_obj_ntlv))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
oh = (struct _ipfw_obj_header *)op3;
|
|
|
|
ntlv_to_ti(&oh->ntlv, &ti_a);
|
|
|
|
ntlv_to_ti((ipfw_obj_ntlv *)(oh + 1), &ti_b);
|
|
|
|
|
2014-08-07 21:37:31 +00:00
|
|
|
error = swap_tables(ch, &ti_a, &ti_b);
|
2014-08-03 21:37:12 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-08-07 21:37:31 +00:00
|
|
|
swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
|
2014-08-03 21:37:12 +00:00
|
|
|
struct tid_info *b)
|
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct table_config *tc_a, *tc_b;
|
|
|
|
struct table_algo *ta;
|
|
|
|
struct table_info ti, *tablestate;
|
|
|
|
void *astate;
|
|
|
|
uint32_t count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stage 1: find both tables and ensure they are of
|
|
|
|
* the same type and algo.
|
|
|
|
*/
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
if ((tc_a = find_table(ni, a)) == NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
if ((tc_b = find_table(ni, b)) == NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is very easy to swap between the same table */
|
|
|
|
if (tc_a == tc_b) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check type and value are the same */
|
|
|
|
if (tc_a->no.type != tc_b->no.type || tc_a->tflags != tc_b->tflags ||
|
|
|
|
tc_a->vtype != tc_b->vtype) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check limits before swap */
|
|
|
|
if ((tc_a->limit != 0 && tc_b->count > tc_a->limit) ||
|
|
|
|
(tc_b->limit != 0 && tc_a->count > tc_b->limit)) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything is fine, prepare to swap */
|
|
|
|
tablestate = (struct table_info *)ch->tablestate;
|
|
|
|
ti = tablestate[tc_a->no.kidx];
|
|
|
|
ta = tc_a->ta;
|
|
|
|
astate = tc_a->astate;
|
|
|
|
count = tc_a->count;
|
|
|
|
|
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
/* a <- b */
|
|
|
|
tablestate[tc_a->no.kidx] = tablestate[tc_b->no.kidx];
|
|
|
|
tc_a->ta = tc_b->ta;
|
|
|
|
tc_a->astate = tc_b->astate;
|
|
|
|
tc_a->count = tc_b->count;
|
|
|
|
/* b <- a */
|
|
|
|
tablestate[tc_b->no.kidx] = ti;
|
|
|
|
tc_b->ta = ta;
|
|
|
|
tc_b->astate = astate;
|
|
|
|
tc_b->count = count;
|
|
|
|
IPFW_WUNLOCK(ch);
|
|
|
|
|
|
|
|
/* Ensure tc.ti copies are in sync */
|
|
|
|
tc_a->ti = tablestate[tc_a->no.kidx];
|
|
|
|
tc_b->ti = tablestate[tc_b->no.kidx];
|
|
|
|
|
|
|
|
/* Notify both tables on @ti change */
|
|
|
|
if (tc_a->ta->change_ti != NULL)
|
|
|
|
tc_a->ta->change_ti(tc_a->astate, &tablestate[tc_a->no.kidx]);
|
|
|
|
if (tc_b->ta->change_ti != NULL)
|
|
|
|
tc_b->ta->change_ti(tc_b->astate, &tablestate[tc_b->no.kidx]);
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
2014-06-14 10:58:39 +00:00
|
|
|
* Destroys table specified by @ti.
|
2014-07-03 22:25:59 +00:00
|
|
|
* Data layout (v0)(current):
|
|
|
|
* Request: [ ip_fw3_opheader ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
2014-06-12 09:59:11 +00:00
|
|
|
*/
|
2014-07-03 22:25:59 +00:00
|
|
|
static int
|
|
|
|
destroy_table(struct ip_fw_chain *ch, struct tid_info *ti)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct table_config *tc;
|
|
|
|
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
if ((tc = find_table(ni, ti)) == NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
2012-03-12 14:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Do not permit destroying referenced tables */
|
|
|
|
if (tc->no.refcnt > 0) {
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EBUSY);
|
2012-03-12 14:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
unlink_table(ch, tc);
|
|
|
|
IPFW_WUNLOCK(ch);
|
|
|
|
|
|
|
|
/* Free obj index */
|
2014-07-03 22:25:59 +00:00
|
|
|
if (ipfw_objhash_free_idx(ni, tc->no.kidx) != 0)
|
2014-06-12 09:59:11 +00:00
|
|
|
printf("Error unlinking kidx %d from table %s\n",
|
|
|
|
tc->no.kidx, tc->tablename);
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
|
|
|
free_table_config(ni, tc);
|
|
|
|
|
2009-12-15 21:24:12 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
static void
|
|
|
|
destroy_table_locked(struct namedobj_instance *ni, struct named_object *no,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
unlink_table((struct ip_fw_chain *)arg, (struct table_config *)no);
|
2014-07-03 22:25:59 +00:00
|
|
|
if (ipfw_objhash_free_idx(ni, no->kidx) != 0)
|
2014-06-12 09:59:11 +00:00
|
|
|
printf("Error unlinking kidx %d from table %s\n",
|
|
|
|
no->kidx, no->name);
|
|
|
|
free_table_config(ni, (struct table_config *)no);
|
|
|
|
}
|
|
|
|
|
2009-12-15 21:24:12 +00:00
|
|
|
void
|
2010-03-21 15:54:07 +00:00
|
|
|
ipfw_destroy_tables(struct ip_fw_chain *ch)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/* Remove all tables from working set */
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
ipfw_objhash_foreach(CHAIN_TO_NI(ch), destroy_table_locked, ch);
|
|
|
|
IPFW_WUNLOCK(ch);
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-12 14:07:57 +00:00
|
|
|
|
|
|
|
/* Free pointers itself */
|
2014-06-14 10:58:39 +00:00
|
|
|
free(ch->tablestate, M_IPFW);
|
|
|
|
|
|
|
|
ipfw_table_algo_destroy(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
ipfw_objhash_destroy(CHAIN_TO_NI(ch));
|
|
|
|
free(CHAIN_TO_TCFG(ch), M_IPFW);
|
2009-12-15 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ipfw_init_tables(struct ip_fw_chain *ch)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-12 09:59:11 +00:00
|
|
|
struct tables_config *tcfg;
|
|
|
|
|
2012-03-12 14:07:57 +00:00
|
|
|
/* Allocate pointers */
|
2014-06-14 10:58:39 +00:00
|
|
|
ch->tablestate = malloc(V_fw_tables_max * sizeof(struct table_info),
|
|
|
|
M_IPFW, M_WAITOK | M_ZERO);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
tcfg = malloc(sizeof(struct tables_config), M_IPFW, M_WAITOK | M_ZERO);
|
|
|
|
tcfg->namehash = ipfw_objhash_create(V_fw_tables_max);
|
|
|
|
ch->tblcfg = tcfg;
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ipfw_table_algo_init(ch);
|
|
|
|
|
2009-12-15 21:24:12 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:37:59 +00:00
|
|
|
int
|
|
|
|
ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables)
|
|
|
|
{
|
|
|
|
unsigned int ntables_old, tbl;
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobj_instance *ni;
|
2014-06-14 10:58:39 +00:00
|
|
|
void *new_idx, *old_tablestate, *tablestate;
|
2014-07-28 19:01:25 +00:00
|
|
|
struct table_info *ti;
|
|
|
|
struct table_config *tc;
|
|
|
|
int i, new_blocks;
|
2012-03-25 20:37:59 +00:00
|
|
|
|
|
|
|
/* Check new value for validity */
|
|
|
|
if (ntables > IPFW_TABLES_MAX)
|
|
|
|
ntables = IPFW_TABLES_MAX;
|
|
|
|
|
|
|
|
/* Allocate new pointers */
|
2014-06-14 10:58:39 +00:00
|
|
|
tablestate = malloc(ntables * sizeof(struct table_info),
|
|
|
|
M_IPFW, M_WAITOK | M_ZERO);
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
ipfw_objhash_bitmap_alloc(ntables, (void *)&new_idx, &new_blocks);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
|
|
|
tbl = (ntables >= V_fw_tables_max) ? V_fw_tables_max : ntables;
|
2014-06-12 09:59:11 +00:00
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Temporary restrict decreasing max_tables */
|
|
|
|
if (ntables < V_fw_tables_max) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: Check if we really can shrink
|
|
|
|
*/
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2012-03-25 20:37:59 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Copy table info/indices */
|
|
|
|
memcpy(tablestate, ch->tablestate, sizeof(struct table_info) * tbl);
|
|
|
|
ipfw_objhash_bitmap_merge(ni, &new_idx, &new_blocks);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
|
|
|
|
/* Change pointers */
|
|
|
|
old_tablestate = ch->tablestate;
|
|
|
|
ch->tablestate = tablestate;
|
|
|
|
ipfw_objhash_bitmap_swap(ni, &new_idx, &new_blocks);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
|
|
|
ntables_old = V_fw_tables_max;
|
|
|
|
V_fw_tables_max = ntables;
|
|
|
|
|
|
|
|
IPFW_WUNLOCK(ch);
|
2014-07-28 19:01:25 +00:00
|
|
|
|
|
|
|
/* Notify all consumers that their @ti pointer has changed */
|
|
|
|
ti = (struct table_info *)ch->tablestate;
|
|
|
|
for (i = 0; i < tbl; i++, ti++) {
|
|
|
|
if (ti->lookup == NULL)
|
|
|
|
continue;
|
|
|
|
tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, i);
|
|
|
|
if (tc == NULL || tc->ta->change_ti == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tc->ta->change_ti(tc->astate, ti);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
|
|
|
/* Free old pointers */
|
2014-06-14 10:58:39 +00:00
|
|
|
free(old_tablestate, M_IPFW);
|
2014-06-12 09:59:11 +00:00
|
|
|
ipfw_objhash_bitmap_free(new_idx, new_blocks);
|
2012-03-25 20:37:59 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-07 21:37:31 +00:00
|
|
|
/*
|
|
|
|
* Switch between "set 0" and "rule set" table binding,
|
|
|
|
* Check all ruleset bindings and permits changing
|
|
|
|
* IFF each binding has both rule AND table in default set (set 0).
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int sets)
|
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct named_object *no;
|
|
|
|
struct ip_fw *rule;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
int cmdlen, i, l;
|
|
|
|
uint16_t kidx;
|
|
|
|
uint8_t type;
|
|
|
|
|
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
|
|
|
|
if (V_fw_tables_sets == sets) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
|
|
|
for (i = 0; i < ch->n_rules; i++) {
|
|
|
|
rule = ch->map[i];
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
|
|
|
|
|
|
|
if (no->set != 0 || rule->set != 0) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
V_fw_tables_sets = sets;
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-12-15 21:24:12 +00:00
|
|
|
int
|
|
|
|
ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
|
|
|
|
uint32_t *val)
|
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_info *ti;
|
2009-12-15 21:24:12 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ti = &(((struct table_info *)ch->tablestate)[tbl]);
|
|
|
|
|
|
|
|
return (ti->lookup(ti, &addr, sizeof(in_addr_t), val));
|
2009-12-15 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 14:07:57 +00:00
|
|
|
int
|
2014-06-14 10:58:39 +00:00
|
|
|
ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
|
|
|
|
void *paddr, uint32_t *val)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_info *ti;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ti = &(((struct table_info *)ch->tablestate)[tbl]);
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
return (ti->lookup(ti, paddr, plen, val));
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/*
|
|
|
|
* Info/List/dump support for tables.
|
|
|
|
*
|
|
|
|
*/
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
/*
|
2014-06-15 13:40:27 +00:00
|
|
|
* High-level 'get' cmds sysctl handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lists all tables currently available in kernel.
|
2014-07-03 22:25:59 +00:00
|
|
|
* Data layout (v0)(current):
|
2014-06-15 13:40:27 +00:00
|
|
|
* Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
|
|
|
|
* Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
2014-06-14 22:47:25 +00:00
|
|
|
int
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_list_tables(struct ip_fw_chain *ch, struct sockopt_data *sd)
|
2014-06-14 22:47:25 +00:00
|
|
|
{
|
|
|
|
struct _ipfw_obj_lheader *olh;
|
|
|
|
int error;
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
|
|
|
|
if (olh == NULL)
|
2014-06-15 13:40:27 +00:00
|
|
|
return (EINVAL);
|
2014-07-28 19:01:25 +00:00
|
|
|
if (sd->valsize < olh->size)
|
|
|
|
return (EINVAL);
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
IPFW_UH_RLOCK(ch);
|
2014-06-27 10:07:00 +00:00
|
|
|
error = export_tables(ch, olh, sd);
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
2014-06-14 22:47:25 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
return (error);
|
2014-06-14 22:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:40:27 +00:00
|
|
|
/*
|
2014-06-27 10:07:00 +00:00
|
|
|
* Store table info to buffer provided by @sd.
|
2014-07-03 22:25:59 +00:00
|
|
|
* Data layout (v0)(current):
|
2014-06-15 13:40:27 +00:00
|
|
|
* Request: [ ipfw_obj_header ipfw_xtable_info(empty)]
|
|
|
|
* Reply: [ ipfw_obj_header ipfw_xtable_info ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_describe_table(struct ip_fw_chain *ch, struct sockopt_data *sd)
|
2014-06-15 13:40:27 +00:00
|
|
|
{
|
|
|
|
struct _ipfw_obj_header *oh;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct tid_info ti;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
sz = sizeof(*oh) + sizeof(ipfw_xtable_info);
|
2014-06-27 10:07:00 +00:00
|
|
|
oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
|
|
|
|
if (oh == NULL)
|
2014-06-15 13:40:27 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
objheader_to_ti(oh, &ti);
|
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
export_table_info(ch, tc, (ipfw_xtable_info *)(oh + 1));
|
2014-06-15 13:40:27 +00:00
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
return (0);
|
2014-06-15 13:40:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
struct dump_args {
|
|
|
|
struct table_info *ti;
|
|
|
|
struct table_config *tc;
|
2014-06-27 10:07:00 +00:00
|
|
|
struct sockopt_data *sd;
|
2014-06-14 22:47:25 +00:00
|
|
|
uint32_t cnt;
|
|
|
|
uint16_t uidx;
|
2014-07-06 18:16:04 +00:00
|
|
|
int error;
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_table_entry *ent;
|
|
|
|
uint32_t size;
|
2014-07-06 18:16:04 +00:00
|
|
|
ipfw_obj_tentry tent;
|
2014-06-14 22:47:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_dump_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
|
|
|
struct sockopt_data *sd)
|
2014-06-15 13:40:27 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch (op3->version) {
|
|
|
|
case 0:
|
2014-06-27 10:07:00 +00:00
|
|
|
error = ipfw_dump_table_v0(ch, sd);
|
2014-06-15 13:40:27 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-06-27 10:07:00 +00:00
|
|
|
error = ipfw_dump_table_v1(ch, sd);
|
2014-06-15 13:40:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dumps all table data
|
2014-07-03 22:25:59 +00:00
|
|
|
* Data layout (v1)(current):
|
2014-06-27 10:07:00 +00:00
|
|
|
* Request: [ ipfw_obj_header ], size = ipfw_xtable_info.size
|
2014-07-06 18:16:04 +00:00
|
|
|
* Reply: [ ipfw_obj_header ipfw_xtable_info ipfw_obj_tentry x N ]
|
2014-06-15 13:40:27 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
static int
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_dump_table_v1(struct ip_fw_chain *ch, struct sockopt_data *sd)
|
2014-06-14 22:47:25 +00:00
|
|
|
{
|
|
|
|
struct _ipfw_obj_header *oh;
|
|
|
|
ipfw_xtable_info *i;
|
|
|
|
struct tid_info ti;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
struct dump_args da;
|
2014-06-15 13:40:27 +00:00
|
|
|
uint32_t sz;
|
2014-06-14 22:47:25 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
sz = sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
|
|
|
|
oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
|
|
|
|
if (oh == NULL)
|
2014-06-15 13:40:27 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
i = (ipfw_xtable_info *)(oh + 1);
|
2014-06-15 13:40:27 +00:00
|
|
|
objheader_to_ti(oh, &ti);
|
2014-06-14 22:47:25 +00:00
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
2014-07-03 22:25:59 +00:00
|
|
|
export_table_info(ch, tc, i);
|
2014-06-14 22:47:25 +00:00
|
|
|
sz = tc->count;
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
if (sd->valsize < sz + tc->count * sizeof(ipfw_obj_tentry)) {
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
/*
|
|
|
|
* Submitted buffer size is not enough.
|
|
|
|
* WE've already filled in @i structure with
|
|
|
|
* relevant table info including size, so we
|
|
|
|
* can return. Buffer will be flushed automatically.
|
|
|
|
*/
|
2014-06-14 22:47:25 +00:00
|
|
|
IPFW_UH_RUNLOCK(ch);
|
2014-06-27 10:07:00 +00:00
|
|
|
return (ENOMEM);
|
2014-06-14 22:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the actual dump in eXtended format
|
|
|
|
*/
|
2014-06-15 13:40:27 +00:00
|
|
|
memset(&da, 0, sizeof(da));
|
2014-06-14 22:47:25 +00:00
|
|
|
da.ti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
da.tc = tc;
|
2014-06-27 10:07:00 +00:00
|
|
|
da.sd = sd;
|
2014-06-14 22:47:25 +00:00
|
|
|
|
|
|
|
ta = tc->ta;
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
ta->foreach(tc->astate, da.ti, dump_table_tentry, &da);
|
2014-06-14 22:47:25 +00:00
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
return (da.error);
|
2014-06-14 22:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:40:27 +00:00
|
|
|
/*
|
|
|
|
* Dumps all table data
|
2014-06-27 10:07:00 +00:00
|
|
|
* Data layout (version 0)(legacy):
|
2014-06-15 13:40:27 +00:00
|
|
|
* Request: [ ipfw_xtable ], size = IP_FW_TABLE_XGETSIZE()
|
|
|
|
* Reply: [ ipfw_xtable ipfw_table_xentry x N ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
static int
|
2014-06-27 10:07:00 +00:00
|
|
|
ipfw_dump_table_v0(struct ip_fw_chain *ch, struct sockopt_data *sd)
|
2014-06-15 13:40:27 +00:00
|
|
|
{
|
|
|
|
ipfw_xtable *xtbl;
|
|
|
|
struct tid_info ti;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
struct dump_args da;
|
|
|
|
size_t sz;
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
xtbl = (ipfw_xtable *)ipfw_get_sopt_header(sd, sizeof(ipfw_xtable));
|
|
|
|
if (xtbl == NULL)
|
2014-06-15 13:40:27 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
memset(&ti, 0, sizeof(ti));
|
|
|
|
ti.uidx = xtbl->tbl;
|
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (0);
|
|
|
|
}
|
2014-06-27 10:07:00 +00:00
|
|
|
sz = tc->count * sizeof(ipfw_table_xentry) + sizeof(ipfw_xtable);
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
xtbl->cnt = tc->count;
|
|
|
|
xtbl->size = sz;
|
|
|
|
xtbl->type = tc->no.type;
|
|
|
|
xtbl->tbl = ti.uidx;
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
if (sd->valsize < sz) {
|
2014-06-15 13:40:27 +00:00
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
/*
|
|
|
|
* Submitted buffer size is not enough.
|
|
|
|
* WE've already filled in @i structure with
|
|
|
|
* relevant table info including size, so we
|
|
|
|
* can return. Buffer will be flushed automatically.
|
|
|
|
*/
|
2014-06-15 13:40:27 +00:00
|
|
|
IPFW_UH_RUNLOCK(ch);
|
2014-06-27 10:07:00 +00:00
|
|
|
return (ENOMEM);
|
2014-06-15 13:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the actual dump in eXtended format */
|
|
|
|
memset(&da, 0, sizeof(da));
|
|
|
|
da.ti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
da.tc = tc;
|
2014-06-27 10:07:00 +00:00
|
|
|
da.sd = sd;
|
|
|
|
|
2014-06-15 13:40:27 +00:00
|
|
|
ta = tc->ta;
|
|
|
|
|
|
|
|
ta->foreach(tc->astate, da.ti, dump_table_xentry, &da);
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
return (0);
|
2014-06-15 13:40:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/*
|
|
|
|
* Creates new table.
|
2014-07-03 22:25:59 +00:00
|
|
|
* Data layout (v0)(current):
|
2014-06-16 13:05:07 +00:00
|
|
|
* Request: [ ipfw_obj_header ipfw_xtable_info ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
int
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_create_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
|
|
|
|
struct sockopt_data *sd)
|
2014-06-16 13:05:07 +00:00
|
|
|
{
|
|
|
|
struct _ipfw_obj_header *oh;
|
|
|
|
ipfw_xtable_info *i;
|
|
|
|
char *tname, *aname;
|
|
|
|
struct tid_info ti;
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct table_config *tc;
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
|
2014-06-16 13:05:07 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
oh = (struct _ipfw_obj_header *)sd->kbuf;
|
2014-06-16 13:05:07 +00:00
|
|
|
i = (ipfw_xtable_info *)(oh + 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify user-supplied strings.
|
2014-06-27 10:07:00 +00:00
|
|
|
* Check for null-terminated/zero-length strings/
|
2014-06-16 13:05:07 +00:00
|
|
|
*/
|
2014-07-03 22:25:59 +00:00
|
|
|
tname = oh->ntlv.name;
|
2014-06-16 13:05:07 +00:00
|
|
|
aname = i->algoname;
|
2014-07-03 22:25:59 +00:00
|
|
|
if (ipfw_check_table_name(tname) != 0 ||
|
2014-06-16 13:05:07 +00:00
|
|
|
strnlen(aname, sizeof(i->algoname)) == sizeof(i->algoname))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (aname[0] == '\0') {
|
|
|
|
/* Use default algorithm */
|
|
|
|
aname = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
objheader_to_ti(oh, &ti);
|
2014-07-03 22:25:59 +00:00
|
|
|
ti.type = i->type;
|
2014-06-16 13:05:07 +00:00
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
if ((tc = find_table(ni, &ti)) != NULL) {
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (EEXIST);
|
|
|
|
}
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
2014-08-01 15:17:46 +00:00
|
|
|
return (create_table_internal(ch, &ti, aname, i));
|
2014-07-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates new table based on @ti and @aname.
|
|
|
|
*
|
|
|
|
* Relies on table name checking inside find_name_tlv()
|
|
|
|
* Assume @aname to be checked and valid.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
|
2014-08-01 15:17:46 +00:00
|
|
|
char *aname, ipfw_xtable_info *i)
|
2014-07-26 13:37:25 +00:00
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
uint16_t kidx;
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
|
|
|
ta = find_table_algo(CHAIN_TO_TCFG(ch), ti, aname);
|
2014-06-16 13:05:07 +00:00
|
|
|
if (ta == NULL)
|
|
|
|
return (ENOTSUP);
|
2014-08-01 15:17:46 +00:00
|
|
|
|
|
|
|
tc = alloc_table_config(ch, ti, ta, aname, i->tflags, i->vtype);
|
|
|
|
if (tc == NULL)
|
2014-06-16 13:05:07 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
|
2014-08-01 15:17:46 +00:00
|
|
|
tc->limit = i->limit;
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
2014-07-26 13:37:25 +00:00
|
|
|
|
|
|
|
/* Check if table has been already created */
|
|
|
|
if (find_table(ni, ti) != NULL) {
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
free_table_config(ni, tc);
|
|
|
|
return (EEXIST);
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
if (ipfw_objhash_alloc_idx(ni, &kidx) != 0) {
|
2014-06-16 13:05:07 +00:00
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2014-07-26 13:37:25 +00:00
|
|
|
printf("Unable to allocate table index."
|
|
|
|
" Consider increasing net.inet.ip.fw.tables_max");
|
2014-06-16 13:05:07 +00:00
|
|
|
free_table_config(ni, tc);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->no.kidx = kidx;
|
|
|
|
|
|
|
|
IPFW_WLOCK(ch);
|
|
|
|
link_table(ch, tc);
|
|
|
|
IPFW_WUNLOCK(ch);
|
|
|
|
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-03 21:37:12 +00:00
|
|
|
static void
|
|
|
|
ntlv_to_ti(ipfw_obj_ntlv *ntlv, struct tid_info *ti)
|
2014-06-15 13:40:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
memset(ti, 0, sizeof(struct tid_info));
|
2014-08-03 21:37:12 +00:00
|
|
|
ti->set = ntlv->set;
|
|
|
|
ti->uidx = ntlv->idx;
|
|
|
|
ti->tlvs = ntlv;
|
|
|
|
ti->tlen = ntlv->head.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti)
|
|
|
|
{
|
|
|
|
|
|
|
|
ntlv_to_ti(&oh->ntlv, ti);
|
2014-06-15 13:40:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 23:20:24 +00:00
|
|
|
int
|
|
|
|
ipfw_export_table_ntlv(struct ip_fw_chain *ch, uint16_t kidx,
|
|
|
|
struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct named_object *no;
|
|
|
|
ipfw_obj_ntlv *ntlv;
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
2014-06-28 23:20:24 +00:00
|
|
|
KASSERT(no != NULL, ("invalid table kidx passed"));
|
|
|
|
|
|
|
|
ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
|
|
|
|
if (ntlv == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
ntlv->head.type = IPFW_TLV_TBL_NAME;
|
|
|
|
ntlv->head.length = sizeof(*ntlv);
|
|
|
|
ntlv->idx = no->kidx;
|
|
|
|
strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
static void
|
2014-07-03 22:25:59 +00:00
|
|
|
export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
|
|
|
|
ipfw_xtable_info *i)
|
2014-06-14 10:58:39 +00:00
|
|
|
{
|
2014-07-03 22:25:59 +00:00
|
|
|
struct table_info *ti;
|
2014-08-03 12:19:45 +00:00
|
|
|
struct table_algo *ta;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
i->type = tc->no.type;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
i->tflags = tc->tflags;
|
2014-07-03 22:25:59 +00:00
|
|
|
i->vtype = tc->vtype;
|
2014-06-14 10:58:39 +00:00
|
|
|
i->set = tc->no.set;
|
|
|
|
i->kidx = tc->no.kidx;
|
|
|
|
i->refcnt = tc->no.refcnt;
|
|
|
|
i->count = tc->count;
|
2014-08-01 15:17:46 +00:00
|
|
|
i->limit = tc->limit;
|
2014-07-06 18:16:04 +00:00
|
|
|
i->size = tc->count * sizeof(ipfw_obj_tentry);
|
2014-06-14 22:47:25 +00:00
|
|
|
i->size += sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
|
2014-06-14 10:58:39 +00:00
|
|
|
strlcpy(i->tablename, tc->tablename, sizeof(i->tablename));
|
2014-08-03 12:19:45 +00:00
|
|
|
ti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
ta = tc->ta;
|
|
|
|
if (ta->print_config != NULL) {
|
2014-07-03 22:25:59 +00:00
|
|
|
/* Use algo function to print table config to string */
|
2014-08-03 12:19:45 +00:00
|
|
|
ta->print_config(tc->astate, ti, i->algoname,
|
2014-07-03 22:25:59 +00:00
|
|
|
sizeof(i->algoname));
|
|
|
|
} else
|
2014-08-03 12:19:45 +00:00
|
|
|
strlcpy(i->algoname, ta->name, sizeof(i->algoname));
|
|
|
|
/* Dump algo-specific data, if possible */
|
|
|
|
if (ta->dump_tinfo != NULL) {
|
|
|
|
ta->dump_tinfo(tc->astate, ti, &i->ta_info);
|
|
|
|
i->ta_info.flags |= IPFW_TATFLAGS_DATA;
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
struct dump_table_args {
|
|
|
|
struct ip_fw_chain *ch;
|
|
|
|
struct sockopt_data *sd;
|
|
|
|
};
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
static void
|
|
|
|
export_table_internal(struct namedobj_instance *ni, struct named_object *no,
|
|
|
|
void *arg)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
ipfw_xtable_info *i;
|
2014-07-03 22:25:59 +00:00
|
|
|
struct dump_table_args *dta;
|
2009-12-15 21:24:12 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
dta = (struct dump_table_args *)arg;
|
|
|
|
|
|
|
|
i = (ipfw_xtable_info *)ipfw_get_sopt_space(dta->sd, sizeof(*i));
|
2014-07-28 19:01:25 +00:00
|
|
|
KASSERT(i != 0, ("previously checked buffer is not enough"));
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
export_table_info(dta->ch, (struct table_config *)no, i);
|
2009-12-15 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
/*
|
|
|
|
* Export all tables as ipfw_xtable_info structures to
|
2014-06-27 10:07:00 +00:00
|
|
|
* storage provided by @sd.
|
2014-08-08 06:36:26 +00:00
|
|
|
*
|
|
|
|
* If supplied buffer is too small, fills in required size
|
|
|
|
* and returns ENOMEM.
|
2014-06-14 22:47:25 +00:00
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
static int
|
2014-06-27 10:07:00 +00:00
|
|
|
export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
|
|
|
|
struct sockopt_data *sd)
|
2009-12-15 21:24:12 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
uint32_t size;
|
|
|
|
uint32_t count;
|
2014-07-03 22:25:59 +00:00
|
|
|
struct dump_table_args dta;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
count = ipfw_objhash_count(CHAIN_TO_NI(ch));
|
|
|
|
size = count * sizeof(ipfw_xtable_info) + sizeof(ipfw_obj_lheader);
|
2014-06-27 10:07:00 +00:00
|
|
|
|
|
|
|
/* Fill in header regadless of buffer size */
|
|
|
|
olh->count = count;
|
|
|
|
olh->objsize = sizeof(ipfw_xtable_info);
|
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
if (size > olh->size) {
|
|
|
|
olh->size = size;
|
2014-06-14 10:58:39 +00:00
|
|
|
return (ENOMEM);
|
2014-06-14 22:47:25 +00:00
|
|
|
}
|
2014-07-28 19:01:25 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
olh->size = size;
|
2014-06-27 10:07:00 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
dta.ch = ch;
|
|
|
|
dta.sd = sd;
|
|
|
|
|
|
|
|
ipfw_objhash_foreach(CHAIN_TO_NI(ch), export_table_internal, &dta);
|
2009-12-15 21:24:12 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
/*
|
|
|
|
* Legacy IP_FW_TABLE_GETSIZE handler
|
|
|
|
*/
|
2014-06-14 10:58:39 +00:00
|
|
|
int
|
|
|
|
ipfw_count_table(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_config *tc;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
*cnt = tc->count;
|
2012-03-12 14:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
/*
|
|
|
|
* Legacy IP_FW_TABLE_XGETSIZE handler
|
|
|
|
*/
|
2012-03-12 14:07:57 +00:00
|
|
|
int
|
2014-06-12 09:59:11 +00:00
|
|
|
ipfw_count_xtable(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-12 09:59:11 +00:00
|
|
|
struct table_config *tc;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-06-15 13:40:27 +00:00
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL) {
|
|
|
|
*cnt = 0;
|
2014-06-14 10:58:39 +00:00
|
|
|
return (0); /* 'table all list' requires success */
|
2014-06-15 13:40:27 +00:00
|
|
|
}
|
2014-06-14 10:58:39 +00:00
|
|
|
*cnt = tc->count * sizeof(ipfw_table_xentry);
|
|
|
|
if (tc->count > 0)
|
|
|
|
*cnt += sizeof(ipfw_xtable);
|
2012-03-12 14:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-06-14 10:58:39 +00:00
|
|
|
dump_table_entry(void *e, void *arg)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
struct dump_args *da;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
ipfw_table_entry *ent;
|
2014-07-06 18:16:04 +00:00
|
|
|
int error;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
da = (struct dump_args *)arg;
|
|
|
|
|
|
|
|
tc = da->tc;
|
|
|
|
ta = tc->ta;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
|
|
|
/* Out of memory, returning */
|
2014-06-14 22:47:25 +00:00
|
|
|
if (da->cnt == da->size)
|
2012-03-12 14:07:57 +00:00
|
|
|
return (1);
|
2014-06-14 22:47:25 +00:00
|
|
|
ent = da->ent++;
|
|
|
|
ent->tbl = da->uidx;
|
|
|
|
da->cnt++;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
ent->addr = da->tent.k.addr.s_addr;
|
|
|
|
ent->masklen = da->tent.masklen;
|
|
|
|
ent->value = da->tent.value;
|
|
|
|
|
|
|
|
return (0);
|
2014-06-14 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 22:47:25 +00:00
|
|
|
/*
|
|
|
|
* Dumps table in pre-8.1 legacy format.
|
|
|
|
*/
|
2014-06-14 10:58:39 +00:00
|
|
|
int
|
2014-06-14 22:47:25 +00:00
|
|
|
ipfw_dump_table_legacy(struct ip_fw_chain *ch, struct tid_info *ti,
|
|
|
|
ipfw_table *tbl)
|
2014-06-14 10:58:39 +00:00
|
|
|
{
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
struct dump_args da;
|
|
|
|
|
|
|
|
tbl->cnt = 0;
|
|
|
|
|
|
|
|
if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
|
|
|
|
return (0); /* XXX: We should return ESRCH */
|
|
|
|
|
|
|
|
ta = tc->ta;
|
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
/* This dump format supports IPv4 only */
|
|
|
|
if (tc->no.type != IPFW_TABLE_CIDR)
|
|
|
|
return (0);
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-06-15 13:40:27 +00:00
|
|
|
memset(&da, 0, sizeof(da));
|
2014-06-14 10:58:39 +00:00
|
|
|
da.ti = KIDX_TO_TI(ch, tc->no.kidx);
|
|
|
|
da.tc = tc;
|
2014-06-14 22:47:25 +00:00
|
|
|
da.ent = &tbl->ent[0];
|
|
|
|
da.size = tbl->size;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
tbl->cnt = 0;
|
|
|
|
ta->foreach(tc->astate, da.ti, dump_table_entry, &da);
|
2014-06-14 22:47:25 +00:00
|
|
|
tbl->cnt = da.cnt;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2012-03-12 14:07:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/*
|
2014-07-06 18:16:04 +00:00
|
|
|
* Dumps table entry in eXtended format (v1)(current).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dump_table_tentry(void *e, void *arg)
|
|
|
|
{
|
|
|
|
struct dump_args *da;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
|
|
|
ipfw_obj_tentry *tent;
|
|
|
|
|
|
|
|
da = (struct dump_args *)arg;
|
|
|
|
|
|
|
|
tc = da->tc;
|
|
|
|
ta = tc->ta;
|
|
|
|
|
|
|
|
tent = (ipfw_obj_tentry *)ipfw_get_sopt_space(da->sd, sizeof(*tent));
|
|
|
|
/* Out of memory, returning */
|
|
|
|
if (tent == NULL) {
|
|
|
|
da->error = ENOMEM;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
tent->head.length = sizeof(ipfw_obj_tentry);
|
|
|
|
tent->idx = da->uidx;
|
|
|
|
|
|
|
|
return (ta->dump_tentry(tc->astate, da->ti, e, tent));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dumps table entry in eXtended format (v0).
|
2014-06-16 13:05:07 +00:00
|
|
|
*/
|
2012-03-12 14:07:57 +00:00
|
|
|
static int
|
2014-06-14 10:58:39 +00:00
|
|
|
dump_table_xentry(void *e, void *arg)
|
2012-03-12 14:07:57 +00:00
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
struct dump_args *da;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct table_algo *ta;
|
2012-03-12 14:07:57 +00:00
|
|
|
ipfw_table_xentry *xent;
|
2014-07-06 18:16:04 +00:00
|
|
|
ipfw_obj_tentry *tent;
|
|
|
|
int error;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
da = (struct dump_args *)arg;
|
|
|
|
|
|
|
|
tc = da->tc;
|
|
|
|
ta = tc->ta;
|
|
|
|
|
2014-06-27 10:07:00 +00:00
|
|
|
xent = (ipfw_table_xentry *)ipfw_get_sopt_space(da->sd, sizeof(*xent));
|
2012-03-12 14:07:57 +00:00
|
|
|
/* Out of memory, returning */
|
2014-06-27 10:07:00 +00:00
|
|
|
if (xent == NULL)
|
2012-03-12 14:07:57 +00:00
|
|
|
return (1);
|
|
|
|
xent->len = sizeof(ipfw_table_xentry);
|
2014-06-14 22:47:25 +00:00
|
|
|
xent->tbl = da->uidx;
|
2012-03-12 14:07:57 +00:00
|
|
|
|
2014-07-06 18:16:04 +00:00
|
|
|
memset(&da->tent, 0, sizeof(da->tent));
|
|
|
|
tent = &da->tent;
|
|
|
|
error = ta->dump_tentry(tc->astate, da->ti, e, tent);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* Convert current format to previous one */
|
|
|
|
xent->masklen = tent->masklen;
|
|
|
|
xent->value = tent->value;
|
|
|
|
/* Apply some hacks */
|
|
|
|
if (tc->no.type == IPFW_TABLE_CIDR && tent->subtype == AF_INET) {
|
|
|
|
xent->k.addr6.s6_addr32[3] = tent->k.addr.s_addr;
|
|
|
|
xent->flags = IPFW_TCF_INET;
|
|
|
|
} else
|
|
|
|
memcpy(&xent->k, &tent->k, sizeof(xent->k));
|
|
|
|
|
|
|
|
return (0);
|
2012-03-12 14:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/*
|
|
|
|
* Table algorithms
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-06-16 13:05:07 +00:00
|
|
|
* Finds algoritm by index, table type or supplied name
|
2014-06-14 10:58:39 +00:00
|
|
|
*/
|
|
|
|
static struct table_algo *
|
2014-06-16 13:05:07 +00:00
|
|
|
find_table_algo(struct tables_config *tcfg, struct tid_info *ti, char *name)
|
2014-06-14 10:58:39 +00:00
|
|
|
{
|
2014-06-16 13:05:07 +00:00
|
|
|
int i, l;
|
|
|
|
struct table_algo *ta;
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-08-01 07:35:17 +00:00
|
|
|
if (ti->type > IPFW_TABLE_MAXTYPE)
|
|
|
|
return (NULL);
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Search by index */
|
|
|
|
if (ti->atype != 0) {
|
|
|
|
if (ti->atype > tcfg->algo_count)
|
|
|
|
return (NULL);
|
|
|
|
return (tcfg->algo[ti->atype]);
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/* Search by name if supplied */
|
|
|
|
if (name != NULL) {
|
|
|
|
/* TODO: better search */
|
|
|
|
for (i = 1; i <= tcfg->algo_count; i++) {
|
|
|
|
ta = tcfg->algo[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* One can supply additional algorithm
|
|
|
|
* parameters so we compare only the first word
|
|
|
|
* of supplied name:
|
|
|
|
* 'hash_cidr hsize=32'
|
|
|
|
* '^^^^^^^^^'
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
l = strlen(ta->name);
|
|
|
|
if (strncmp(name, ta->name, l) == 0) {
|
|
|
|
if (name[l] == '\0' || name[l] == ' ')
|
|
|
|
return (ta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-08-01 07:35:17 +00:00
|
|
|
/* Return default algorithm for given type if set */
|
|
|
|
return (tcfg->def_algo[ti->type]);
|
2014-06-14 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/*
|
|
|
|
* Register new table algo @ta.
|
|
|
|
* Stores algo id iside @idx.<F2>
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
2014-07-29 21:38:06 +00:00
|
|
|
int
|
|
|
|
ipfw_add_table_algo(struct ip_fw_chain *ch, struct table_algo *ta, size_t size,
|
|
|
|
int *idx)
|
2014-06-14 10:58:39 +00:00
|
|
|
{
|
|
|
|
struct tables_config *tcfg;
|
2014-07-29 21:38:06 +00:00
|
|
|
struct table_algo *ta_new;
|
2014-08-02 17:18:47 +00:00
|
|
|
size_t sz;
|
2014-07-29 21:38:06 +00:00
|
|
|
|
|
|
|
if (size > sizeof(struct table_algo))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/* Check for the required on-stack size for add/del */
|
|
|
|
sz = roundup2(ta->ta_buf_size, sizeof(void *));
|
|
|
|
if (sz > TA_BUF_SZ)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2014-08-01 07:35:17 +00:00
|
|
|
KASSERT(ta->type >= IPFW_TABLE_MAXTYPE,("Increase IPFW_TABLE_MAXTYPE"));
|
|
|
|
|
2014-07-29 21:38:06 +00:00
|
|
|
ta_new = malloc(sizeof(struct table_algo), M_IPFW, M_WAITOK | M_ZERO);
|
|
|
|
memcpy(ta_new, ta, size);
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
tcfg = CHAIN_TO_TCFG(ch);
|
|
|
|
|
|
|
|
KASSERT(tcfg->algo_count < 255, ("Increase algo array size"));
|
|
|
|
|
2014-07-29 21:38:06 +00:00
|
|
|
tcfg->algo[++tcfg->algo_count] = ta_new;
|
|
|
|
ta_new->idx = tcfg->algo_count;
|
|
|
|
|
2014-08-01 07:35:17 +00:00
|
|
|
/* Set algorithm as default one for given type */
|
|
|
|
if ((ta_new->flags & TA_FLAG_DEFAULT) != 0 &&
|
|
|
|
tcfg->def_algo[ta_new->type] == NULL)
|
|
|
|
tcfg->def_algo[ta_new->type] = ta_new;
|
|
|
|
|
2014-07-29 21:38:06 +00:00
|
|
|
*idx = ta_new->idx;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
/*
|
|
|
|
* Unregisters table algo using @idx as id.
|
|
|
|
*/
|
2014-07-29 21:38:06 +00:00
|
|
|
void
|
|
|
|
ipfw_del_table_algo(struct ip_fw_chain *ch, int idx)
|
|
|
|
{
|
|
|
|
struct tables_config *tcfg;
|
|
|
|
struct table_algo *ta;
|
|
|
|
|
|
|
|
tcfg = CHAIN_TO_TCFG(ch);
|
|
|
|
|
2014-08-02 17:18:47 +00:00
|
|
|
KASSERT(idx <= tcfg->algo_count, ("algo idx %d out of range 1..%d",
|
|
|
|
idx, tcfg->algo_count));
|
2014-07-29 21:38:06 +00:00
|
|
|
|
|
|
|
ta = tcfg->algo[idx];
|
|
|
|
KASSERT(ta != NULL, ("algo idx %d is NULL", idx));
|
2014-08-01 07:35:17 +00:00
|
|
|
|
|
|
|
if (tcfg->def_algo[ta->type] == ta)
|
|
|
|
tcfg->def_algo[ta->type] = NULL;
|
|
|
|
|
2014-07-29 21:38:06 +00:00
|
|
|
free(ta, M_IPFW);
|
2014-06-14 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:44:26 +00:00
|
|
|
/*
|
|
|
|
* Lists all table algorithms currently available.
|
|
|
|
* Data layout (v0)(current):
|
|
|
|
* Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
|
|
|
|
* Reply: [ ipfw_obj_lheader ipfw_ta_info x N ]
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_list_table_algo(struct ip_fw_chain *ch, struct sockopt_data *sd)
|
|
|
|
{
|
|
|
|
struct _ipfw_obj_lheader *olh;
|
|
|
|
struct tables_config *tcfg;
|
|
|
|
ipfw_ta_info *i;
|
|
|
|
struct table_algo *ta;
|
|
|
|
uint32_t count, n, size;
|
|
|
|
|
|
|
|
olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
|
|
|
|
if (olh == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
if (sd->valsize < olh->size)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
IPFW_UH_RLOCK(ch);
|
|
|
|
tcfg = CHAIN_TO_TCFG(ch);
|
|
|
|
count = tcfg->algo_count;
|
|
|
|
size = count * sizeof(ipfw_ta_info) + sizeof(ipfw_obj_lheader);
|
|
|
|
|
|
|
|
/* Fill in header regadless of buffer size */
|
|
|
|
olh->count = count;
|
|
|
|
olh->objsize = sizeof(ipfw_ta_info);
|
|
|
|
|
|
|
|
if (size > olh->size) {
|
|
|
|
olh->size = size;
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
olh->size = size;
|
|
|
|
|
|
|
|
for (n = 1; n <= count; n++) {
|
|
|
|
i = (ipfw_ta_info *)ipfw_get_sopt_space(sd, sizeof(*i));
|
|
|
|
KASSERT(i != 0, ("previously checked buffer is not enough"));
|
|
|
|
ta = tcfg->algo[n];
|
|
|
|
strlcpy(i->algoname, ta->name, sizeof(i->algoname));
|
|
|
|
i->type = ta->type;
|
|
|
|
i->refcnt = ta->refcnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPFW_UH_RUNLOCK(ch);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Tables rewriting code
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine table number and lookup type for @cmd.
|
|
|
|
* Fill @tbl and @type with appropriate values.
|
|
|
|
* Returns 0 for relevant opcodes, 1 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
classify_table_opcode(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
|
|
|
|
{
|
|
|
|
ipfw_insn_if *cmdif;
|
|
|
|
int skip;
|
|
|
|
uint16_t v;
|
|
|
|
|
|
|
|
skip = 1;
|
|
|
|
|
|
|
|
switch (cmd->opcode) {
|
|
|
|
case O_IP_SRC_LOOKUP:
|
|
|
|
case O_IP_DST_LOOKUP:
|
|
|
|
/* Basic IPv4/IPv6 or u32 lookups */
|
|
|
|
*puidx = cmd->arg1;
|
|
|
|
/* Assume CIDR by default */
|
|
|
|
*ptype = IPFW_TABLE_CIDR;
|
|
|
|
skip = 0;
|
|
|
|
|
|
|
|
if (F_LEN(cmd) > F_INSN_SIZE(ipfw_insn_u32)) {
|
|
|
|
/*
|
|
|
|
* generic lookup. The key must be
|
|
|
|
* in 32bit big-endian format.
|
|
|
|
*/
|
|
|
|
v = ((ipfw_insn_u32 *)cmd)->d[1];
|
|
|
|
switch (v) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
/* IPv4 src/dst */
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
/* src/dst port */
|
2014-07-30 14:52:26 +00:00
|
|
|
*ptype = IPFW_TABLE_NUMBER;
|
2014-06-12 09:59:11 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* uid/gid */
|
2014-07-30 14:52:26 +00:00
|
|
|
*ptype = IPFW_TABLE_NUMBER;
|
|
|
|
break;
|
2014-06-12 09:59:11 +00:00
|
|
|
case 5:
|
|
|
|
/* jid */
|
2014-07-30 14:52:26 +00:00
|
|
|
*ptype = IPFW_TABLE_NUMBER;
|
|
|
|
break;
|
2014-06-12 09:59:11 +00:00
|
|
|
case 6:
|
|
|
|
/* dscp */
|
2014-07-30 14:52:26 +00:00
|
|
|
*ptype = IPFW_TABLE_NUMBER;
|
2014-06-12 09:59:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case O_XMIT:
|
|
|
|
case O_RECV:
|
|
|
|
case O_VIA:
|
|
|
|
/* Interface table, possibly */
|
|
|
|
cmdif = (ipfw_insn_if *)cmd;
|
|
|
|
if (cmdif->name[0] != '\1')
|
|
|
|
break;
|
|
|
|
|
|
|
|
*ptype = IPFW_TABLE_INTERFACE;
|
|
|
|
*puidx = cmdif->p.glob;
|
|
|
|
skip = 0;
|
|
|
|
break;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
case O_IP_FLOW_LOOKUP:
|
|
|
|
*puidx = cmd->arg1;
|
|
|
|
*ptype = IPFW_TABLE_FLOW;
|
|
|
|
skip = 0;
|
|
|
|
break;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets new table value for given opcode.
|
|
|
|
* Assume the same opcodes as classify_table_opcode()
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
update_table_opcode(ipfw_insn *cmd, uint16_t idx)
|
|
|
|
{
|
|
|
|
ipfw_insn_if *cmdif;
|
|
|
|
|
|
|
|
switch (cmd->opcode) {
|
|
|
|
case O_IP_SRC_LOOKUP:
|
|
|
|
case O_IP_DST_LOOKUP:
|
|
|
|
/* Basic IPv4/IPv6 or u32 lookups */
|
|
|
|
cmd->arg1 = idx;
|
|
|
|
break;
|
|
|
|
case O_XMIT:
|
|
|
|
case O_RECV:
|
|
|
|
case O_VIA:
|
|
|
|
/* Interface table, possibly */
|
|
|
|
cmdif = (ipfw_insn_if *)cmd;
|
|
|
|
cmdif->p.glob = idx;
|
|
|
|
break;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
case O_IP_FLOW_LOOKUP:
|
|
|
|
cmd->arg1 = idx;
|
|
|
|
break;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
/*
|
|
|
|
* Checks table name for validity.
|
|
|
|
* Enforce basic length checks, the rest
|
|
|
|
* should be done in userland.
|
|
|
|
*
|
|
|
|
* Returns 0 if name is considered valid.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_check_table_name(char *name)
|
|
|
|
{
|
|
|
|
int nsize;
|
|
|
|
ipfw_obj_ntlv *ntlv = NULL;
|
|
|
|
|
|
|
|
nsize = sizeof(ntlv->name);
|
|
|
|
|
|
|
|
if (strnlen(name, nsize) == nsize)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (name[0] == '\0')
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: do some more complicated checks
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find tablename TLV by @uid.
|
|
|
|
* Check @tlvs for valid data inside.
|
|
|
|
*
|
|
|
|
* Returns pointer to found TLV or NULL.
|
|
|
|
*/
|
|
|
|
static ipfw_obj_ntlv *
|
2014-06-12 09:59:11 +00:00
|
|
|
find_name_tlv(void *tlvs, int len, uint16_t uidx)
|
|
|
|
{
|
2014-06-14 10:58:39 +00:00
|
|
|
ipfw_obj_ntlv *ntlv;
|
2014-06-12 09:59:11 +00:00
|
|
|
uintptr_t pa, pe;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
pa = (uintptr_t)tlvs;
|
|
|
|
pe = pa + len;
|
|
|
|
l = 0;
|
|
|
|
for (; pa < pe; pa += l) {
|
2014-06-14 10:58:39 +00:00
|
|
|
ntlv = (ipfw_obj_ntlv *)pa;
|
2014-06-12 09:59:11 +00:00
|
|
|
l = ntlv->head.length;
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
if (l != sizeof(*ntlv))
|
|
|
|
return (NULL);
|
|
|
|
|
2014-06-28 23:20:24 +00:00
|
|
|
if (ntlv->head.type != IPFW_TLV_TBL_NAME)
|
2014-06-12 09:59:11 +00:00
|
|
|
continue;
|
2014-07-03 22:25:59 +00:00
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
if (ntlv->idx != uidx)
|
|
|
|
continue;
|
2014-07-03 22:25:59 +00:00
|
|
|
|
|
|
|
if (ipfw_check_table_name(ntlv->name) != 0)
|
|
|
|
return (NULL);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
return (ntlv);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
/*
|
|
|
|
* Finds table config based on either legacy index
|
|
|
|
* or name in ntlv.
|
|
|
|
* Note @ti structure contains unchecked data from userland.
|
|
|
|
*
|
|
|
|
* Returns pointer to table_config or NULL.
|
|
|
|
*/
|
2014-06-12 09:59:11 +00:00
|
|
|
static struct table_config *
|
|
|
|
find_table(struct namedobj_instance *ni, struct tid_info *ti)
|
|
|
|
{
|
|
|
|
char *name, bname[16];
|
|
|
|
struct named_object *no;
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_obj_ntlv *ntlv;
|
|
|
|
uint32_t set;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
if (ti->tlvs != NULL) {
|
2014-07-03 22:25:59 +00:00
|
|
|
ntlv = find_name_tlv(ti->tlvs, ti->tlen, ti->uidx);
|
|
|
|
if (ntlv == NULL)
|
2014-06-12 09:59:11 +00:00
|
|
|
return (NULL);
|
2014-07-03 22:25:59 +00:00
|
|
|
name = ntlv->name;
|
|
|
|
set = ntlv->set;
|
2014-06-12 09:59:11 +00:00
|
|
|
} else {
|
|
|
|
snprintf(bname, sizeof(bname), "%d", ti->uidx);
|
|
|
|
name = bname;
|
2014-07-03 22:25:59 +00:00
|
|
|
set = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
no = ipfw_objhash_lookup_name(ni, set, name);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
return ((struct table_config *)no);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct table_config *
|
2014-07-28 19:01:25 +00:00
|
|
|
alloc_table_config(struct ip_fw_chain *ch, struct tid_info *ti,
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
struct table_algo *ta, char *aname, uint8_t tflags, uint8_t vtype)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
|
|
|
char *name, bname[16];
|
|
|
|
struct table_config *tc;
|
|
|
|
int error;
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_obj_ntlv *ntlv;
|
|
|
|
uint32_t set;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
if (ti->tlvs != NULL) {
|
2014-07-03 22:25:59 +00:00
|
|
|
ntlv = find_name_tlv(ti->tlvs, ti->tlen, ti->uidx);
|
|
|
|
if (ntlv == NULL)
|
2014-06-12 09:59:11 +00:00
|
|
|
return (NULL);
|
2014-07-03 22:25:59 +00:00
|
|
|
name = ntlv->name;
|
|
|
|
set = ntlv->set;
|
2014-06-12 09:59:11 +00:00
|
|
|
} else {
|
|
|
|
snprintf(bname, sizeof(bname), "%d", ti->uidx);
|
|
|
|
name = bname;
|
2014-07-03 22:25:59 +00:00
|
|
|
set = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tc = malloc(sizeof(struct table_config), M_IPFW, M_WAITOK | M_ZERO);
|
|
|
|
tc->no.name = tc->tablename;
|
|
|
|
tc->no.type = ti->type;
|
2014-07-03 22:25:59 +00:00
|
|
|
tc->no.set = set;
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
tc->tflags = tflags;
|
2014-06-14 10:58:39 +00:00
|
|
|
tc->ta = ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
strlcpy(tc->tablename, name, sizeof(tc->tablename));
|
2014-07-03 22:25:59 +00:00
|
|
|
/* Set default value type to u32 for compability reasons */
|
2014-07-26 13:37:25 +00:00
|
|
|
if (vtype == 0)
|
|
|
|
tc->vtype = IPFW_VTYPE_U32;
|
|
|
|
else
|
|
|
|
tc->vtype = vtype;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
if (ti->tlvs == NULL) {
|
|
|
|
tc->no.compat = 1;
|
|
|
|
tc->no.uidx = ti->uidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Preallocate data structures for new tables */
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
error = ta->init(ch, &tc->astate, &tc->ti, aname, tflags);
|
2014-06-12 09:59:11 +00:00
|
|
|
if (error != 0) {
|
|
|
|
free(tc, M_IPFW);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_table_config(struct namedobj_instance *ni, struct table_config *tc)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (tc->linked == 0)
|
2014-07-28 19:01:25 +00:00
|
|
|
tc->ta->destroy(tc->astate, &tc->ti);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
free(tc, M_IPFW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Links @tc to @chain table named instance.
|
|
|
|
* Sets appropriate type/states in @chain table info.
|
|
|
|
*/
|
|
|
|
static void
|
2014-06-14 10:58:39 +00:00
|
|
|
link_table(struct ip_fw_chain *ch, struct table_config *tc)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_info *ti;
|
2014-06-12 09:59:11 +00:00
|
|
|
uint16_t kidx;
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK_ASSERT(ch);
|
|
|
|
IPFW_WLOCK_ASSERT(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ni = CHAIN_TO_NI(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
kidx = tc->no.kidx;
|
|
|
|
|
|
|
|
ipfw_objhash_add(ni, &tc->no);
|
2014-06-14 10:58:39 +00:00
|
|
|
|
|
|
|
ti = KIDX_TO_TI(ch, kidx);
|
|
|
|
*ti = tc->ti;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-07-28 19:01:25 +00:00
|
|
|
/* Notify algo on real @ti address */
|
|
|
|
if (tc->ta->change_ti != NULL)
|
|
|
|
tc->ta->change_ti(tc->astate, ti);
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
tc->linked = 1;
|
2014-07-29 22:44:26 +00:00
|
|
|
tc->ta->refcnt++;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlinks @tc from @chain table named instance.
|
|
|
|
* Zeroes states in @chain and stores them in @tc.
|
|
|
|
*/
|
|
|
|
static void
|
2014-06-14 10:58:39 +00:00
|
|
|
unlink_table(struct ip_fw_chain *ch, struct table_config *tc)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
|
|
|
struct namedobj_instance *ni;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_info *ti;
|
2014-06-12 09:59:11 +00:00
|
|
|
uint16_t kidx;
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK_ASSERT(ch);
|
|
|
|
IPFW_WLOCK_ASSERT(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
ni = CHAIN_TO_NI(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
kidx = tc->no.kidx;
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Clear state. @ti copy is already saved inside @tc */
|
2014-06-12 09:59:11 +00:00
|
|
|
ipfw_objhash_del(ni, &tc->no);
|
2014-06-14 10:58:39 +00:00
|
|
|
ti = KIDX_TO_TI(ch, kidx);
|
|
|
|
memset(ti, 0, sizeof(struct table_info));
|
2014-06-12 09:59:11 +00:00
|
|
|
tc->linked = 0;
|
2014-07-29 22:44:26 +00:00
|
|
|
tc->ta->refcnt--;
|
2014-07-28 19:01:25 +00:00
|
|
|
|
|
|
|
/* Notify algo on real @ti address */
|
|
|
|
if (tc->ta->change_ti != NULL)
|
|
|
|
tc->ta->change_ti(tc->astate, NULL);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds named object by @uidx number.
|
|
|
|
* Refs found object, allocate new index for non-existing object.
|
2014-06-16 13:05:07 +00:00
|
|
|
* Fills in @oib with userland/kernel indexes.
|
|
|
|
* First free oidx pointer is saved back in @oib.
|
2014-06-12 09:59:11 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
|
|
|
static int
|
2014-06-16 13:05:07 +00:00
|
|
|
bind_table_rule(struct ip_fw_chain *ch, struct ip_fw *rule,
|
|
|
|
struct rule_check_info *ci, struct obj_idx **oib, struct tid_info *ti)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
|
|
|
struct table_config *tc;
|
2014-06-16 13:05:07 +00:00
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct named_object *no;
|
|
|
|
int error, l, cmdlen;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
struct obj_idx *pidx, *p;
|
|
|
|
|
|
|
|
pidx = *oib;
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
error = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
IPFW_UH_WLOCK(ch);
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
2014-06-14 10:58:39 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
if (classify_table_opcode(cmd, &ti->uidx, &ti->type) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pidx->uidx = ti->uidx;
|
|
|
|
pidx->type = ti->type;
|
|
|
|
|
|
|
|
if ((tc = find_table(ni, ti)) != NULL) {
|
|
|
|
if (tc->no.type != ti->type) {
|
|
|
|
/* Incompatible types */
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reference found table and save kidx */
|
|
|
|
tc->no.refcnt++;
|
|
|
|
pidx->kidx = tc->no.kidx;
|
|
|
|
pidx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Table not found. Allocate new index and save for later */
|
2014-07-03 22:25:59 +00:00
|
|
|
if (ipfw_objhash_alloc_idx(ni, &pidx->kidx) != 0) {
|
|
|
|
printf("Unable to allocate table %s index in set %u."
|
2014-06-12 09:59:11 +00:00
|
|
|
" Consider increasing net.inet.ip.fw.tables_max",
|
2014-07-03 22:25:59 +00:00
|
|
|
"", ti->set);
|
2014-06-16 13:05:07 +00:00
|
|
|
error = EBUSY;
|
|
|
|
break;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ci->new_tables++;
|
2014-06-16 13:05:07 +00:00
|
|
|
pidx->new = 1;
|
|
|
|
pidx++;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
if (error != 0) {
|
|
|
|
/* Unref everything we have already done */
|
|
|
|
for (p = *oib; p < pidx; p++) {
|
|
|
|
if (p->new != 0) {
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_objhash_free_idx(ni, p->kidx);
|
2014-06-16 13:05:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/* Find & unref by existing idx */
|
2014-07-03 22:25:59 +00:00
|
|
|
no = ipfw_objhash_lookup_kidx(ni, p->kidx);
|
2014-06-16 13:05:07 +00:00
|
|
|
KASSERT(no != NULL, ("Ref'd table %d disappeared",
|
|
|
|
p->kidx));
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
no->refcnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IPFW_UH_WUNLOCK(ch);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
*oib = pidx;
|
|
|
|
|
|
|
|
return (error);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 21:37:31 +00:00
|
|
|
struct swap_table_args {
|
|
|
|
int set;
|
|
|
|
int new_set;
|
|
|
|
int mv;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change set for each matching table.
|
|
|
|
*
|
|
|
|
* Ensure we dispatch each table once by setting/checking ochange
|
|
|
|
* fields.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
swap_table_set(struct namedobj_instance *ni, struct named_object *no,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct table_config *tc;
|
|
|
|
struct swap_table_args *sta;
|
|
|
|
|
|
|
|
tc = (struct table_config *)no;
|
|
|
|
sta = (struct swap_table_args *)arg;
|
|
|
|
|
|
|
|
if (no->set != sta->set && (no->set != sta->new_set || sta->mv != 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tc->ochanged != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tc->ochanged = 1;
|
|
|
|
ipfw_objhash_del(ni, no);
|
|
|
|
if (no->set == sta->set)
|
|
|
|
no->set = sta->new_set;
|
|
|
|
else
|
|
|
|
no->set = sta->set;
|
|
|
|
ipfw_objhash_add(ni, no);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleans up ochange field for all tables.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clean_table_set_data(struct namedobj_instance *ni, struct named_object *no,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct table_config *tc;
|
|
|
|
struct swap_table_args *sta;
|
|
|
|
|
|
|
|
tc = (struct table_config *)no;
|
|
|
|
sta = (struct swap_table_args *)arg;
|
|
|
|
|
|
|
|
tc->ochanged = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swaps tables within two sets.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ipfw_swap_tables_sets(struct ip_fw_chain *ch, uint32_t set,
|
|
|
|
uint32_t new_set, int mv)
|
|
|
|
{
|
|
|
|
struct swap_table_args sta;
|
|
|
|
|
|
|
|
IPFW_UH_WLOCK_ASSERT(ch);
|
|
|
|
|
|
|
|
sta.set = set;
|
|
|
|
sta.new_set = new_set;
|
|
|
|
sta.mv = mv;
|
|
|
|
|
|
|
|
ipfw_objhash_foreach(CHAIN_TO_NI(ch), swap_table_set, &sta);
|
|
|
|
ipfw_objhash_foreach(CHAIN_TO_NI(ch), clean_table_set_data, &sta);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move all tables which are reference by rules in @rr to set @new_set.
|
|
|
|
* Makes sure that all relevant tables are referenced ONLLY by given rules.
|
|
|
|
*
|
|
|
|
* Retuns 0 on success,
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_move_tables_sets(struct ip_fw_chain *ch, ipfw_range_tlv *rt,
|
|
|
|
uint32_t new_set)
|
|
|
|
{
|
|
|
|
struct ip_fw *rule;
|
|
|
|
struct table_config *tc;
|
|
|
|
struct named_object *no;
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
int bad, i, l, cmdlen;
|
|
|
|
uint16_t kidx;
|
|
|
|
uint8_t type;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
|
|
|
|
IPFW_UH_WLOCK_ASSERT(ch);
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(ch);
|
|
|
|
|
|
|
|
/* Stage 1: count number of references by given rules */
|
|
|
|
for (i = 0; i < ch->n_rules - 1; i++) {
|
|
|
|
rule = ch->map[i];
|
|
|
|
if (ipfw_match_range(rule, rt) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
|
|
|
KASSERT(no != NULL,
|
|
|
|
("objhash lookup failed on index %d", kidx));
|
|
|
|
tc = (struct table_config *)no;
|
|
|
|
tc->ocount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stage 2: verify "ownership" */
|
|
|
|
bad = 0;
|
|
|
|
for (i = 0; i < ch->n_rules - 1; i++) {
|
|
|
|
rule = ch->map[i];
|
|
|
|
if (ipfw_match_range(rule, rt) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
|
|
|
KASSERT(no != NULL,
|
|
|
|
("objhash lookup failed on index %d", kidx));
|
|
|
|
tc = (struct table_config *)no;
|
|
|
|
if (tc->no.refcnt != tc->ocount) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of references differ:
|
|
|
|
* Other rule(s) are holding reference to given
|
|
|
|
* table, so it is not possible to change its set.
|
|
|
|
*
|
|
|
|
* Note that refcnt may account
|
|
|
|
* references to some going-to-be-added rules.
|
|
|
|
* Since we don't know their numbers (and event
|
|
|
|
* if they will be added) it is perfectly OK
|
|
|
|
* to return error here.
|
|
|
|
*/
|
|
|
|
bad = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bad != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stage 3: change set or cleanup */
|
|
|
|
for (i = 0; i < ch->n_rules - 1; i++) {
|
|
|
|
rule = ch->map[i];
|
|
|
|
if (ipfw_match_range(rule, rt) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
|
|
|
KASSERT(no != NULL,
|
|
|
|
("objhash lookup failed on index %d", kidx));
|
|
|
|
tc = (struct table_config *)no;
|
|
|
|
|
|
|
|
tc->ocount = 0;
|
|
|
|
if (bad != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Actually change set. */
|
|
|
|
ipfw_objhash_del(ni, no);
|
|
|
|
no->set = new_set;
|
|
|
|
ipfw_objhash_add(ni, no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bad);
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Compatibility function for old ipfw(8) binaries.
|
|
|
|
* Rewrites table kernel indices with userland ones.
|
|
|
|
* Works for \d+ talbes only (e.g. for tables, converted
|
|
|
|
* from old numbered system calls).
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Raises error on any other tables.
|
|
|
|
*/
|
|
|
|
int
|
2014-07-08 23:11:15 +00:00
|
|
|
ipfw_rewrite_table_kidx(struct ip_fw_chain *chain, struct ip_fw_rule0 *rule)
|
2014-06-12 09:59:11 +00:00
|
|
|
{
|
2014-07-04 07:02:11 +00:00
|
|
|
int cmdlen, error, l;
|
2014-06-12 09:59:11 +00:00
|
|
|
ipfw_insn *cmd;
|
2014-07-04 07:02:11 +00:00
|
|
|
uint16_t kidx, uidx;
|
2014-06-12 09:59:11 +00:00
|
|
|
uint8_t type;
|
|
|
|
struct named_object *no;
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(chain);
|
2014-07-04 07:02:11 +00:00
|
|
|
error = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
if ((no = ipfw_objhash_lookup_kidx(ni, kidx)) == NULL)
|
2014-06-12 09:59:11 +00:00
|
|
|
return (1);
|
|
|
|
|
2014-07-04 07:02:11 +00:00
|
|
|
uidx = no->uidx;
|
|
|
|
if (no->compat == 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are called via legacy opcode.
|
|
|
|
* Save error and show table as fake number
|
|
|
|
* not to make ipfw(8) hang.
|
|
|
|
*/
|
|
|
|
uidx = 65535;
|
|
|
|
error = 2;
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-07-04 07:02:11 +00:00
|
|
|
update_table_opcode(cmd, uidx);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 07:02:11 +00:00
|
|
|
return (error);
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 23:20:24 +00:00
|
|
|
/*
|
|
|
|
* Sets every table kidx in @bmask which is used in rule @rule.
|
|
|
|
*
|
|
|
|
* Returns number of newly-referenced tables.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_mark_table_kidx(struct ip_fw_chain *chain, struct ip_fw *rule,
|
|
|
|
uint32_t *bmask)
|
|
|
|
{
|
|
|
|
int cmdlen, l, count;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
uint16_t kidx;
|
|
|
|
uint8_t type;
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
count = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((bmask[kidx / 32] & (1 << (kidx % 32))) == 0)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
bmask[kidx / 32] |= 1 << (kidx % 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks is opcode is referencing table of appropriate type.
|
|
|
|
* Adds reference count for found table if true.
|
|
|
|
* Rewrites user-supplied opcode values with kernel ones.
|
|
|
|
*
|
|
|
|
* Returns 0 on success and appropriate error code otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ipfw_rewrite_table_uidx(struct ip_fw_chain *chain,
|
|
|
|
struct rule_check_info *ci)
|
|
|
|
{
|
|
|
|
int cmdlen, error, ftype, l;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
uint16_t uidx;
|
|
|
|
uint8_t type;
|
|
|
|
struct table_config *tc;
|
2014-06-14 10:58:39 +00:00
|
|
|
struct table_algo *ta;
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct named_object *no, *no_n, *no_tmp;
|
2014-06-16 13:05:07 +00:00
|
|
|
struct obj_idx *p, *pidx_first, *pidx_last;
|
2014-06-12 09:59:11 +00:00
|
|
|
struct namedobjects_head nh;
|
|
|
|
struct tid_info ti;
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(chain);
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/* Prepare queue to store configs */
|
|
|
|
TAILQ_INIT(&nh);
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Prepare an array for storing opcode indices.
|
|
|
|
* Use stack allocation by default.
|
|
|
|
*/
|
|
|
|
if (ci->table_opcodes <= (sizeof(ci->obuf)/sizeof(ci->obuf[0]))) {
|
|
|
|
/* Stack */
|
2014-06-16 13:05:07 +00:00
|
|
|
pidx_first = ci->obuf;
|
2014-06-12 09:59:11 +00:00
|
|
|
} else
|
2014-06-16 13:05:07 +00:00
|
|
|
pidx_first = malloc(ci->table_opcodes * sizeof(struct obj_idx),
|
2014-06-12 09:59:11 +00:00
|
|
|
M_IPFW, M_WAITOK | M_ZERO);
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
pidx_last = pidx_first;
|
2014-06-12 09:59:11 +00:00
|
|
|
error = 0;
|
|
|
|
|
|
|
|
type = 0;
|
|
|
|
ftype = 0;
|
|
|
|
|
|
|
|
memset(&ti, 0, sizeof(ti));
|
2014-07-04 07:02:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use default set for looking up tables (old way) or
|
|
|
|
* use set rule is assigned to (new way).
|
|
|
|
*/
|
|
|
|
ti.set = (V_fw_tables_sets != 0) ? ci->krule->set : 0;
|
2014-06-29 22:35:47 +00:00
|
|
|
if (ci->ctlv != NULL) {
|
|
|
|
ti.tlvs = (void *)(ci->ctlv + 1);
|
|
|
|
ti.tlen = ci->ctlv->head.length - sizeof(ipfw_obj_ctlv);
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
/*
|
2014-06-16 13:05:07 +00:00
|
|
|
* Stage 1: reference existing tables, determine number
|
|
|
|
* of tables we need to allocate and allocate indexes for each.
|
2014-06-12 09:59:11 +00:00
|
|
|
*/
|
2014-06-16 13:05:07 +00:00
|
|
|
error = bind_table_rule(chain, ci->krule, ci, &pidx_last, &ti);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
if (error != 0) {
|
2014-06-16 13:05:07 +00:00
|
|
|
if (pidx_first != ci->obuf)
|
|
|
|
free(pidx_first, M_IPFW);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stage 2: allocate table configs for every non-existent table
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ci->new_tables > 0) {
|
2014-06-16 13:05:07 +00:00
|
|
|
for (p = pidx_first; p < pidx_last; p++) {
|
2014-06-12 09:59:11 +00:00
|
|
|
if (p->new == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ti.uidx = p->uidx;
|
|
|
|
ti.type = p->type;
|
2014-06-14 10:58:39 +00:00
|
|
|
ti.atype = 0;
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
ta = find_table_algo(CHAIN_TO_TCFG(chain), &ti, NULL);
|
2014-06-14 10:58:39 +00:00
|
|
|
if (ta == NULL) {
|
|
|
|
error = ENOTSUP;
|
|
|
|
goto free;
|
|
|
|
}
|
* Add new "flow" table type to support N=1..5-tuple lookups
* Add "flow:hash" algorithm
Kernel changes:
* Add O_IP_FLOW_LOOKUP opcode to support "flow" lookups
* Add IPFW_TABLE_FLOW table type
* Add "struct tflow_entry" as strage for 6-tuple flows
* Add "flow:hash" algorithm. Basically it is auto-growing chained hash table.
Additionally, we store mask of fields we need to compare in each instance/
* Increase ipfw_obj_tentry size by adding struct tflow_entry
* Add per-algorithm stat (ifpw_ta_tinfo) to ipfw_xtable_info
* Increase algoname length: 32 -> 64 (algo options passed there as string)
* Assume every table type can be customized by flags, use u8 to store "tflags" field.
* Simplify ipfw_find_table_entry() by providing @tentry directly to algo callback.
* Fix bug in cidr:chash resize procedure.
Userland changes:
* add "flow table(NAME)" syntax to support n-tuple checking tables.
* make fill_flags() separate function to ease working with _s_x arrays
* change "table info" output to reflect longer "type" fields
Syntax:
ipfw table fl2 create type flow:[src-ip][,proto][,src-port][,dst-ip][dst-port] [algo flow:hash]
Examples:
0:02 [2] zfscurr0# ipfw table fl2 create type flow:src-ip,proto,dst-port algo flow:hash
0:02 [2] zfscurr0# ipfw table fl2 info
+++ table(fl2), set(0) +++
kindex: 0, type: flow:src-ip,proto,dst-port
valtype: number, references: 0
algorithm: flow:hash
items: 0, size: 280
0:02 [2] zfscurr0# ipfw table fl2 add 2a02:6b8::333,tcp,443 45000
0:02 [2] zfscurr0# ipfw table fl2 add 10.0.0.92,tcp,80 22000
0:02 [2] zfscurr0# ipfw table fl2 list
+++ table(fl2), set(0) +++
2a02:6b8::333,6,443 45000
10.0.0.92,6,80 22000
0:02 [2] zfscurr0# ipfw add 200 count tcp from me to 78.46.89.105 80 flow 'table(fl2)'
00200 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
0:03 [2] zfscurr0# ipfw show
00200 0 0 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 617 59416 allow ip from any to any
0:03 [2] zfscurr0# telnet -s 10.0.0.92 78.46.89.105 80
Trying 78.46.89.105...
..
0:04 [2] zfscurr0# ipfw show
00200 5 272 count tcp from me to 78.46.89.105 dst-port 80 flow table(fl2)
65535 682 66733 allow ip from any to any
2014-07-31 20:08:19 +00:00
|
|
|
tc = alloc_table_config(chain, &ti, ta, NULL, 0,
|
2014-07-28 19:01:25 +00:00
|
|
|
IPFW_VTYPE_U32);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
if (tc == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->no.kidx = p->kidx;
|
|
|
|
tc->no.refcnt = 1;
|
|
|
|
|
|
|
|
/* Add to list */
|
|
|
|
TAILQ_INSERT_TAIL(&nh, &tc->no, nn_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stage 2.1: Check if we're going to create 2 tables
|
|
|
|
* with the same name, but different table types.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(no, &nh, nn_next) {
|
|
|
|
TAILQ_FOREACH(no_tmp, &nh, nn_next) {
|
2014-06-16 13:05:07 +00:00
|
|
|
if (ipfw_objhash_same_name(ni, no, no_tmp) == 0)
|
2014-06-12 09:59:11 +00:00
|
|
|
continue;
|
|
|
|
if (no->type != no_tmp->type) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 10:58:39 +00:00
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WLOCK(chain);
|
|
|
|
|
|
|
|
if (ci->new_tables > 0) {
|
2014-06-12 09:59:11 +00:00
|
|
|
/*
|
|
|
|
* Stage 3: link & reference new table configs
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 3.1: Check if some tables we need to create have been
|
|
|
|
* already created with different table type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
TAILQ_FOREACH_SAFE(no, &nh, nn_next, no_tmp) {
|
|
|
|
no_n = ipfw_objhash_lookup_name(ni, no->set, no->name);
|
|
|
|
if (no_n == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (no_n->type != no->type) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != 0) {
|
|
|
|
/*
|
|
|
|
* Someone has allocated table with different table type.
|
|
|
|
* We have to rollback everything.
|
|
|
|
*/
|
|
|
|
IPFW_UH_WUNLOCK(chain);
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-14 10:58:39 +00:00
|
|
|
* Attach new tables.
|
|
|
|
* We need to set table pointers for each new table,
|
2014-06-12 09:59:11 +00:00
|
|
|
* so we have to acquire main WLOCK.
|
|
|
|
*/
|
|
|
|
IPFW_WLOCK(chain);
|
|
|
|
TAILQ_FOREACH_SAFE(no, &nh, nn_next, no_tmp) {
|
|
|
|
no_n = ipfw_objhash_lookup_name(ni, no->set, no->name);
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
if (no_n == NULL) {
|
|
|
|
/* New table. Attach to runtime hash */
|
|
|
|
TAILQ_REMOVE(&nh, no, nn_next);
|
|
|
|
link_table(chain, (struct table_config *)no);
|
2014-06-12 09:59:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
/*
|
|
|
|
* Newly-allocated table with the same type.
|
|
|
|
* Reference it and update out @pidx array
|
|
|
|
* rewrite info.
|
|
|
|
*/
|
|
|
|
no_n->refcnt++;
|
|
|
|
/* Keep oib array in sync: update kidx */
|
|
|
|
for (p = pidx_first; p < pidx_last; p++) {
|
|
|
|
if (p->kidx != no->kidx)
|
|
|
|
continue;
|
|
|
|
/* Update kidx */
|
|
|
|
p->kidx = no_n->kidx;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
IPFW_WUNLOCK(chain);
|
2014-06-14 10:58:39 +00:00
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
/* Perform rule rewrite */
|
|
|
|
l = ci->krule->cmd_len;
|
|
|
|
cmd = ci->krule->cmd;
|
|
|
|
cmdlen = 0;
|
2014-06-16 13:05:07 +00:00
|
|
|
p = pidx_first;
|
2014-06-14 10:58:39 +00:00
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
if (classify_table_opcode(cmd, &uidx, &type) != 0)
|
|
|
|
continue;
|
2014-06-16 13:05:07 +00:00
|
|
|
update_table_opcode(cmd, p->kidx);
|
|
|
|
p++;
|
2014-06-12 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 10:58:39 +00:00
|
|
|
IPFW_UH_WUNLOCK(chain);
|
|
|
|
|
2014-06-12 09:59:11 +00:00
|
|
|
error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stage 4: free resources
|
|
|
|
*/
|
|
|
|
free:
|
2014-06-16 13:05:07 +00:00
|
|
|
if (!TAILQ_EMPTY(&nh)) {
|
|
|
|
/* Free indexes first */
|
|
|
|
IPFW_UH_WLOCK(chain);
|
|
|
|
TAILQ_FOREACH_SAFE(no, &nh, nn_next, no_tmp) {
|
2014-07-03 22:25:59 +00:00
|
|
|
ipfw_objhash_free_idx(ni, no->kidx);
|
2014-06-16 13:05:07 +00:00
|
|
|
}
|
|
|
|
IPFW_UH_WUNLOCK(chain);
|
|
|
|
/* Free configs */
|
|
|
|
TAILQ_FOREACH_SAFE(no, &nh, nn_next, no_tmp)
|
|
|
|
free_table_config(ni, tc);
|
|
|
|
}
|
2014-06-12 09:59:11 +00:00
|
|
|
|
2014-06-16 13:05:07 +00:00
|
|
|
if (pidx_first != ci->obuf)
|
|
|
|
free(pidx_first, M_IPFW);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove references from every table used in @rule.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ipfw_unbind_table_rule(struct ip_fw_chain *chain, struct ip_fw *rule)
|
|
|
|
{
|
|
|
|
int cmdlen, l;
|
|
|
|
ipfw_insn *cmd;
|
|
|
|
struct namedobj_instance *ni;
|
|
|
|
struct named_object *no;
|
|
|
|
uint16_t kidx;
|
|
|
|
uint8_t type;
|
|
|
|
|
|
|
|
ni = CHAIN_TO_NI(chain);
|
|
|
|
|
|
|
|
l = rule->cmd_len;
|
|
|
|
cmd = rule->cmd;
|
|
|
|
cmdlen = 0;
|
|
|
|
for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
|
|
|
|
cmdlen = F_LEN(cmd);
|
|
|
|
|
|
|
|
if (classify_table_opcode(cmd, &kidx, &type) != 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-03 22:25:59 +00:00
|
|
|
no = ipfw_objhash_lookup_kidx(ni, kidx);
|
2014-06-12 09:59:11 +00:00
|
|
|
|
|
|
|
KASSERT(no != NULL, ("table id %d not found", kidx));
|
|
|
|
KASSERT(no->type == type, ("wrong type %d (%d) for table id %d",
|
|
|
|
no->type, type, kidx));
|
|
|
|
KASSERT(no->refcnt > 0, ("refcount for table %d is %d",
|
|
|
|
kidx, no->refcnt));
|
|
|
|
|
|
|
|
no->refcnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes table bindings for every rule in rule chain @head.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ipfw_unbind_table_list(struct ip_fw_chain *chain, struct ip_fw *head)
|
|
|
|
{
|
|
|
|
struct ip_fw *rule;
|
|
|
|
|
|
|
|
while ((rule = head) != NULL) {
|
|
|
|
head = head->x_next;
|
|
|
|
ipfw_unbind_table_rule(chain, rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-16 10:48:40 +00:00
|
|
|
/* end of file */
|