Skip to content

Commit e500924

Browse files
committed
netfilter: nf_tables: add stateful objects
This patch augments nf_tables to support stateful objects. This new infrastructure allows you to create, dump and delete stateful objects, that are identified by a user-defined name. This patch adds the generic infrastructure, follow up patches add support for two stateful objects: counters and quotas. This patch provides a native infrastructure for nf_tables to replace nfacct, the extended accounting infrastructure for iptables. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 3bf3276 commit e500924

File tree

3 files changed

+624
-0
lines changed

3 files changed

+624
-0
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
875875
* @list: used internally
876876
* @chains: chains in the table
877877
* @sets: sets in the table
878+
* @objects: stateful objects in the table
878879
* @hgenerator: handle generator state
879880
* @use: number of chain references to this table
880881
* @flags: table flag (see enum nft_table_flags)
@@ -885,6 +886,7 @@ struct nft_table {
885886
struct list_head list;
886887
struct list_head chains;
887888
struct list_head sets;
889+
struct list_head objects;
888890
u64 hgenerator;
889891
u32 use;
890892
u16 flags:14,
@@ -934,6 +936,73 @@ void nft_unregister_expr(struct nft_expr_type *);
934936
int nft_verdict_dump(struct sk_buff *skb, int type,
935937
const struct nft_verdict *v);
936938

939+
/**
940+
* struct nft_object - nf_tables stateful object
941+
*
942+
* @list: table stateful object list node
943+
* @type: pointer to object type
944+
* @data: pointer to object data
945+
* @name: name of this stateful object
946+
* @genmask: generation mask
947+
* @use: number of references to this stateful object
948+
* @data: object data, layout depends on type
949+
*/
950+
struct nft_object {
951+
struct list_head list;
952+
char name[NFT_OBJ_MAXNAMELEN];
953+
u32 genmask:2,
954+
use:30;
955+
/* runtime data below here */
956+
const struct nft_object_type *type ____cacheline_aligned;
957+
unsigned char data[]
958+
__attribute__((aligned(__alignof__(u64))));
959+
};
960+
961+
static inline void *nft_obj_data(const struct nft_object *obj)
962+
{
963+
return (void *)obj->data;
964+
}
965+
966+
#define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
967+
968+
struct nft_object *nf_tables_obj_lookup(const struct nft_table *table,
969+
const struct nlattr *nla, u32 objtype,
970+
u8 genmask);
971+
972+
/**
973+
* struct nft_object_type - stateful object type
974+
*
975+
* @eval: stateful object evaluation function
976+
* @list: list node in list of object types
977+
* @type: stateful object numeric type
978+
* @size: stateful object size
979+
* @owner: module owner
980+
* @maxattr: maximum netlink attribute
981+
* @policy: netlink attribute policy
982+
* @init: initialize object from netlink attributes
983+
* @destroy: release existing stateful object
984+
* @dump: netlink dump stateful object
985+
*/
986+
struct nft_object_type {
987+
void (*eval)(struct nft_object *obj,
988+
struct nft_regs *regs,
989+
const struct nft_pktinfo *pkt);
990+
struct list_head list;
991+
u32 type;
992+
unsigned int size;
993+
unsigned int maxattr;
994+
struct module *owner;
995+
const struct nla_policy *policy;
996+
int (*init)(const struct nlattr * const tb[],
997+
struct nft_object *obj);
998+
void (*destroy)(struct nft_object *obj);
999+
int (*dump)(struct sk_buff *skb,
1000+
const struct nft_object *obj);
1001+
};
1002+
1003+
int nft_register_obj(struct nft_object_type *obj_type);
1004+
void nft_unregister_obj(struct nft_object_type *obj_type);
1005+
9371006
/**
9381007
* struct nft_traceinfo - nft tracing information and state
9391008
*
@@ -981,6 +1050,9 @@ void nft_trace_notify(struct nft_traceinfo *info);
9811050
#define MODULE_ALIAS_NFT_SET() \
9821051
MODULE_ALIAS("nft-set")
9831052

1053+
#define MODULE_ALIAS_NFT_OBJ(type) \
1054+
MODULE_ALIAS("nft-obj-" __stringify(type))
1055+
9841056
/*
9851057
* The gencursor defines two generations, the currently active and the
9861058
* next one. Objects contain a bitmask of 2 bits specifying the generations
@@ -1157,4 +1229,11 @@ struct nft_trans_elem {
11571229
#define nft_trans_elem(trans) \
11581230
(((struct nft_trans_elem *)trans->data)->elem)
11591231

1232+
struct nft_trans_obj {
1233+
struct nft_object *obj;
1234+
};
1235+
1236+
#define nft_trans_obj(trans) \
1237+
(((struct nft_trans_obj *)trans->data)->obj)
1238+
11601239
#endif /* _NET_NF_TABLES_H */

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define NFT_TABLE_MAXNAMELEN 32
55
#define NFT_CHAIN_MAXNAMELEN 32
66
#define NFT_SET_MAXNAMELEN 32
7+
#define NFT_OBJ_MAXNAMELEN 32
78
#define NFT_USERDATA_MAXLEN 256
89

910
/**
@@ -85,6 +86,9 @@ enum nft_verdicts {
8586
* @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
8687
* @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
8788
* @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
89+
* @NFT_MSG_NEWOBJ: create a stateful object (enum nft_obj_attributes)
90+
* @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes)
91+
* @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes)
8892
*/
8993
enum nf_tables_msg_types {
9094
NFT_MSG_NEWTABLE,
@@ -105,6 +109,9 @@ enum nf_tables_msg_types {
105109
NFT_MSG_NEWGEN,
106110
NFT_MSG_GETGEN,
107111
NFT_MSG_TRACE,
112+
NFT_MSG_NEWOBJ,
113+
NFT_MSG_GETOBJ,
114+
NFT_MSG_DELOBJ,
108115
NFT_MSG_MAX,
109116
};
110117

@@ -1178,6 +1185,28 @@ enum nft_fib_flags {
11781185
NFTA_FIB_F_OIF = 1 << 4, /* restrict to oif */
11791186
};
11801187

1188+
#define NFT_OBJECT_UNSPEC 0
1189+
1190+
/**
1191+
* enum nft_object_attributes - nf_tables stateful object netlink attributes
1192+
*
1193+
* @NFTA_OBJ_TABLE: name of the table containing the expression (NLA_STRING)
1194+
* @NFTA_OBJ_NAME: name of this expression type (NLA_STRING)
1195+
* @NFTA_OBJ_TYPE: stateful object type (NLA_U32)
1196+
* @NFTA_OBJ_DATA: stateful object data (NLA_NESTED)
1197+
* @NFTA_OBJ_USE: number of references to this expression (NLA_U32)
1198+
*/
1199+
enum nft_object_attributes {
1200+
NFTA_OBJ_UNSPEC,
1201+
NFTA_OBJ_TABLE,
1202+
NFTA_OBJ_NAME,
1203+
NFTA_OBJ_TYPE,
1204+
NFTA_OBJ_DATA,
1205+
NFTA_OBJ_USE,
1206+
__NFTA_OBJ_MAX
1207+
};
1208+
#define NFTA_OBJ_MAX (__NFTA_OBJ_MAX - 1)
1209+
11811210
/**
11821211
* enum nft_trace_attributes - nf_tables trace netlink attributes
11831212
*

0 commit comments

Comments
 (0)