Skip to content

Commit 3b49e2e

Browse files
committed
netfilter: nf_tables: add flow table netlink frontend
This patch introduces a netlink control plane to create, delete and dump flow tables. Flow tables are identified by name, this name is used from rules to refer to an specific flow table. Flow tables use the rhashtable class and a generic garbage collector to remove expired entries. This also adds the infrastructure to add different flow table types, so we can add one for each layer 3 protocol family. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 9096401 commit 3b49e2e

File tree

4 files changed

+870
-1
lines changed

4 files changed

+870
-1
lines changed

include/net/netfilter/nf_flow_table.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _NF_FLOW_TABLE_H
2+
#define _NF_FLOW_TABLE_H
3+
4+
#include <linux/rhashtable.h>
5+
6+
struct nf_flowtable;
7+
8+
struct nf_flowtable_type {
9+
struct list_head list;
10+
int family;
11+
void (*gc)(struct work_struct *work);
12+
const struct rhashtable_params *params;
13+
nf_hookfn *hook;
14+
struct module *owner;
15+
};
16+
17+
struct nf_flowtable {
18+
struct rhashtable rhashtable;
19+
const struct nf_flowtable_type *type;
20+
struct delayed_work gc_work;
21+
};
22+
23+
#endif /* _FLOW_OFFLOAD_H */

include/net/netfilter/nf_tables.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/netfilter/x_tables.h>
1010
#include <linux/netfilter/nf_tables.h>
1111
#include <linux/u64_stats_sync.h>
12+
#include <net/netfilter/nf_flow_table.h>
1213
#include <net/netlink.h>
1314

1415
#define NFT_JUMP_STACK_SIZE 16
@@ -943,6 +944,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
943944
* @chains: chains in the table
944945
* @sets: sets in the table
945946
* @objects: stateful objects in the table
947+
* @flowtables: flow tables in the table
946948
* @hgenerator: handle generator state
947949
* @use: number of chain references to this table
948950
* @flags: table flag (see enum nft_table_flags)
@@ -954,6 +956,7 @@ struct nft_table {
954956
struct list_head chains;
955957
struct list_head sets;
956958
struct list_head objects;
959+
struct list_head flowtables;
957960
u64 hgenerator;
958961
u32 use;
959962
u16 flags:14,
@@ -1084,6 +1087,44 @@ struct nft_object_ops {
10841087
int nft_register_obj(struct nft_object_type *obj_type);
10851088
void nft_unregister_obj(struct nft_object_type *obj_type);
10861089

1090+
/**
1091+
* struct nft_flowtable - nf_tables flow table
1092+
*
1093+
* @list: flow table list node in table list
1094+
* @table: the table the flow table is contained in
1095+
* @name: name of this flow table
1096+
* @hooknum: hook number
1097+
* @priority: hook priority
1098+
* @ops_len: number of hooks in array
1099+
* @genmask: generation mask
1100+
* @use: number of references to this flow table
1101+
* @data: rhashtable and garbage collector
1102+
* @ops: array of hooks
1103+
*/
1104+
struct nft_flowtable {
1105+
struct list_head list;
1106+
struct nft_table *table;
1107+
char *name;
1108+
int hooknum;
1109+
int priority;
1110+
int ops_len;
1111+
u32 genmask:2,
1112+
use:30;
1113+
/* runtime data below here */
1114+
struct nf_hook_ops *ops ____cacheline_aligned;
1115+
struct nf_flowtable data;
1116+
};
1117+
1118+
struct nft_flowtable *nf_tables_flowtable_lookup(const struct nft_table *table,
1119+
const struct nlattr *nla,
1120+
u8 genmask);
1121+
void nft_flow_table_iterate(struct net *net,
1122+
void (*iter)(struct nf_flowtable *flowtable, void *data),
1123+
void *data);
1124+
1125+
void nft_register_flowtable_type(struct nf_flowtable_type *type);
1126+
void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1127+
10871128
/**
10881129
* struct nft_traceinfo - nft tracing information and state
10891130
*
@@ -1317,4 +1358,11 @@ struct nft_trans_obj {
13171358
#define nft_trans_obj(trans) \
13181359
(((struct nft_trans_obj *)trans->data)->obj)
13191360

1361+
struct nft_trans_flowtable {
1362+
struct nft_flowtable *flowtable;
1363+
};
1364+
1365+
#define nft_trans_flowtable(trans) \
1366+
(((struct nft_trans_flowtable *)trans->data)->flowtable)
1367+
13201368
#endif /* _NET_NF_TABLES_H */

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ enum nft_verdicts {
9292
* @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes)
9393
* @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes)
9494
* @NFT_MSG_GETOBJ_RESET: get and reset a stateful object (enum nft_obj_attributes)
95+
* @NFT_MSG_NEWFLOWTABLE: add new flow table (enum nft_flowtable_attributes)
96+
* @NFT_MSG_GETFLOWTABLE: get flow table (enum nft_flowtable_attributes)
97+
* @NFT_MSG_DELFLOWTABLE: delete flow table (enum nft_flowtable_attributes)
9598
*/
9699
enum nf_tables_msg_types {
97100
NFT_MSG_NEWTABLE,
@@ -116,6 +119,9 @@ enum nf_tables_msg_types {
116119
NFT_MSG_GETOBJ,
117120
NFT_MSG_DELOBJ,
118121
NFT_MSG_GETOBJ_RESET,
122+
NFT_MSG_NEWFLOWTABLE,
123+
NFT_MSG_GETFLOWTABLE,
124+
NFT_MSG_DELFLOWTABLE,
119125
NFT_MSG_MAX,
120126
};
121127

@@ -1309,6 +1315,53 @@ enum nft_object_attributes {
13091315
};
13101316
#define NFTA_OBJ_MAX (__NFTA_OBJ_MAX - 1)
13111317

1318+
/**
1319+
* enum nft_flowtable_attributes - nf_tables flow table netlink attributes
1320+
*
1321+
* @NFTA_FLOWTABLE_TABLE: name of the table containing the expression (NLA_STRING)
1322+
* @NFTA_FLOWTABLE_NAME: name of this flow table (NLA_STRING)
1323+
* @NFTA_FLOWTABLE_HOOK: netfilter hook configuration(NLA_U32)
1324+
* @NFTA_FLOWTABLE_USE: number of references to this flow table (NLA_U32)
1325+
*/
1326+
enum nft_flowtable_attributes {
1327+
NFTA_FLOWTABLE_UNSPEC,
1328+
NFTA_FLOWTABLE_TABLE,
1329+
NFTA_FLOWTABLE_NAME,
1330+
NFTA_FLOWTABLE_HOOK,
1331+
NFTA_FLOWTABLE_USE,
1332+
__NFTA_FLOWTABLE_MAX
1333+
};
1334+
#define NFTA_FLOWTABLE_MAX (__NFTA_FLOWTABLE_MAX - 1)
1335+
1336+
/**
1337+
* enum nft_flowtable_hook_attributes - nf_tables flow table hook netlink attributes
1338+
*
1339+
* @NFTA_FLOWTABLE_HOOK_NUM: netfilter hook number (NLA_U32)
1340+
* @NFTA_FLOWTABLE_HOOK_PRIORITY: netfilter hook priority (NLA_U32)
1341+
* @NFTA_FLOWTABLE_HOOK_DEVS: input devices this flow table is bound to (NLA_NESTED)
1342+
*/
1343+
enum nft_flowtable_hook_attributes {
1344+
NFTA_FLOWTABLE_HOOK_UNSPEC,
1345+
NFTA_FLOWTABLE_HOOK_NUM,
1346+
NFTA_FLOWTABLE_HOOK_PRIORITY,
1347+
NFTA_FLOWTABLE_HOOK_DEVS,
1348+
__NFTA_FLOWTABLE_HOOK_MAX
1349+
};
1350+
#define NFTA_FLOWTABLE_HOOK_MAX (__NFTA_FLOWTABLE_HOOK_MAX - 1)
1351+
1352+
/**
1353+
* enum nft_device_attributes - nf_tables device netlink attributes
1354+
*
1355+
* @NFTA_DEVICE_NAME: name of this device (NLA_STRING)
1356+
*/
1357+
enum nft_devices_attributes {
1358+
NFTA_DEVICE_UNSPEC,
1359+
NFTA_DEVICE_NAME,
1360+
__NFTA_DEVICE_MAX
1361+
};
1362+
#define NFTA_DEVICE_MAX (__NFTA_DEVICE_MAX - 1)
1363+
1364+
13121365
/**
13131366
* enum nft_trace_attributes - nf_tables trace netlink attributes
13141367
*

0 commit comments

Comments
 (0)