nft.h 8.33 KB
Newer Older
1
2
3
4
5
#ifndef _NFT_H_
#define _NFT_H_

#include "xshared.h"
#include "nft-shared.h"
6
7
#include "nft-cache.h"
#include "nft-cmd.h"
8
9
#include <libiptc/linux_list.h>

10
enum nft_table_type {
11
	NFT_TABLE_MANGLE	= 0,
12
	NFT_TABLE_SECURITY,
13
14
	NFT_TABLE_RAW,
	NFT_TABLE_FILTER,
15
16
17
	NFT_TABLE_NAT,
};
#define NFT_TABLE_MAX	(NFT_TABLE_NAT + 1)
18
19
20
21
22
23
24
25
26
27

struct builtin_chain {
	const char *name;
	const char *type;
	uint32_t prio;
	uint32_t hook;
};

struct builtin_table {
	const char *name;
28
	enum nft_table_type type;
29
	struct builtin_chain chains[NF_INET_NUMHOOKS];
30
31
};

32
33
34
35
enum nft_cache_level {
	NFT_CL_TABLES,
	NFT_CL_CHAINS,
	NFT_CL_SETS,
36
37
	NFT_CL_RULES,
	NFT_CL_FAKE	/* must be last entry */
38
39
};

40
41
42
struct nft_cache {
	struct {
		struct nftnl_chain_list *chains;
43
		struct nftnl_set_list	*sets;
44
		bool			exists;
45
	} table[NFT_TABLE_MAX];
46
47
};

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
enum obj_update_type {
	NFT_COMPAT_TABLE_ADD,
	NFT_COMPAT_TABLE_FLUSH,
	NFT_COMPAT_CHAIN_ADD,
	NFT_COMPAT_CHAIN_USER_ADD,
	NFT_COMPAT_CHAIN_USER_DEL,
	NFT_COMPAT_CHAIN_USER_FLUSH,
	NFT_COMPAT_CHAIN_UPDATE,
	NFT_COMPAT_CHAIN_RENAME,
	NFT_COMPAT_CHAIN_ZERO,
	NFT_COMPAT_RULE_APPEND,
	NFT_COMPAT_RULE_INSERT,
	NFT_COMPAT_RULE_REPLACE,
	NFT_COMPAT_RULE_DELETE,
	NFT_COMPAT_RULE_FLUSH,
	NFT_COMPAT_SET_ADD,
	NFT_COMPAT_RULE_LIST,
	NFT_COMPAT_RULE_CHECK,
	NFT_COMPAT_CHAIN_RESTORE,
	NFT_COMPAT_RULE_SAVE,
	NFT_COMPAT_RULE_ZERO,
	NFT_COMPAT_BRIDGE_USER_CHAIN_UPDATE,
};

struct cache_chain {
	struct list_head head;
	char *name;
};

struct nft_cache_req {
	enum nft_cache_level	level;
	char			*table;
	bool			all_chains;
	struct list_head	chain_list;
};

84
85
86
struct nft_handle {
	int			family;
	struct mnl_socket	*nl;
87
88
	int			nlsndbuffsiz;
	int			nlrcvbuffsiz;
89
90
	uint32_t		portid;
	uint32_t		seq;
91
92
	uint32_t		nft_genid;
	uint32_t		rule_id;
93
94
	struct list_head	obj_list;
	int			obj_list_num;
95
96
	struct nftnl_batch	*batch;
	struct list_head	err_list;
97
	struct nft_family_ops	*ops;
98
99
100
101
	const struct builtin_table *tables;
	unsigned int		cache_index;
	struct nft_cache	__cache[2];
	struct nft_cache	*cache;
102
	struct nft_cache_req	cache_req;
103
	bool			restore;
104
	bool			noflush;
105
	int8_t			config_done;
106
107
	struct list_head	cmd_list;
	bool			cache_init;
108
109
110
111
112

	/* meta data, for error reporting */
	struct {
		unsigned int	lineno;
	} error;
113
114
};

115
116
117
extern const struct builtin_table xtables_ipv4[NFT_TABLE_MAX];
extern const struct builtin_table xtables_arp[NFT_TABLE_MAX];
extern const struct builtin_table xtables_bridge[NFT_TABLE_MAX];
118
119
120
121

int mnl_talk(struct nft_handle *h, struct nlmsghdr *nlh,
	     int (*cb)(const struct nlmsghdr *nlh, void *data),
	     void *data);
122
int nft_init(struct nft_handle *h, int family, const struct builtin_table *t);
123
void nft_fini(struct nft_handle *h);
124
int nft_restart(struct nft_handle *h);
125
126
127
128
129
130
131

/*
 * Operations with tables.
 */
struct nftnl_table;
struct nftnl_chain_list;

132
int nft_for_each_table(struct nft_handle *h, int (*func)(struct nft_handle *h, const char *tablename, void *data), void *data);
133
134
bool nft_table_find(struct nft_handle *h, const char *tablename);
int nft_table_purge_chains(struct nft_handle *h, const char *table, struct nftnl_chain_list *list);
135
int nft_table_flush(struct nft_handle *h, const char *table);
136
const struct builtin_table *nft_table_builtin_find(struct nft_handle *h, const char *table);
137
138
139
140
141
142
143

/*
 * Operations with chains.
 */
struct nftnl_chain;

int nft_chain_set(struct nft_handle *h, const char *table, const char *chain, const char *policy, const struct xt_counters *counters);
144
int nft_chain_save(struct nftnl_chain *c, void *data);
145
int nft_chain_user_add(struct nft_handle *h, const char *chain, const char *table);
146
int nft_chain_user_del(struct nft_handle *h, const char *chain, const char *table, bool verbose);
147
int nft_chain_restore(struct nft_handle *h, const char *chain, const char *table);
148
int nft_chain_user_rename(struct nft_handle *h, const char *chain, const char *table, const char *newname);
149
int nft_chain_zero_counters(struct nft_handle *h, const char *chain, const char *table, bool verbose);
150
const struct builtin_chain *nft_chain_builtin_find(const struct builtin_table *t, const char *chain);
151
bool nft_chain_exists(struct nft_handle *h, const char *table, const char *chain);
152
153
154
void nft_bridge_chain_postprocess(struct nft_handle *h,
				  struct nftnl_chain *c);

155

156
157
158
159
160
161
/*
 * Operations with sets.
 */
struct nftnl_set *nft_set_batch_lookup_byid(struct nft_handle *h,
					    uint32_t set_id);

162
163
164
165
166
/*
 * Operations with rule-set.
 */
struct nftnl_rule;

167
168
169
170
171
struct nftnl_rule *nft_rule_new(struct nft_handle *h, const char *chain, const char *table, void *data);
int nft_rule_append(struct nft_handle *h, const char *chain, const char *table, struct nftnl_rule *r, struct nftnl_rule *ref, bool verbose);
int nft_rule_insert(struct nft_handle *h, const char *chain, const char *table, struct nftnl_rule *r, int rulenum, bool verbose);
int nft_rule_check(struct nft_handle *h, const char *chain, const char *table, struct nftnl_rule *r, bool verbose);
int nft_rule_delete(struct nft_handle *h, const char *chain, const char *table, struct nftnl_rule *r, bool verbose);
172
int nft_rule_delete_num(struct nft_handle *h, const char *chain, const char *table, int rulenum, bool verbose);
173
int nft_rule_replace(struct nft_handle *h, const char *chain, const char *table, struct nftnl_rule *r, int rulenum, bool verbose);
174
175
int nft_rule_list(struct nft_handle *h, const char *chain, const char *table, int rulenum, unsigned int format);
int nft_rule_list_save(struct nft_handle *h, const char *chain, const char *table, int rulenum, int counters);
176
177
int nft_rule_save(struct nft_handle *h, const char *table, unsigned int format);
int nft_rule_flush(struct nft_handle *h, const char *chain, const char *table, bool verbose);
178
179
180
181
182
183
184
int nft_rule_zero_counters(struct nft_handle *h, const char *chain, const char *table, int rulenum);

/*
 * Operations used in userspace tools
 */
int add_counters(struct nftnl_rule *r, uint64_t packets, uint64_t bytes);
int add_verdict(struct nftnl_rule *r, int verdict);
185
int add_match(struct nft_handle *h, struct nftnl_rule *r, struct xt_entry_match *m);
186
187
188
int add_target(struct nftnl_rule *r, struct xt_entry_target *t);
int add_jumpto(struct nftnl_rule *r, const char *name, int verdict);
int add_action(struct nftnl_rule *r, struct iptables_command_state *cs, bool goto_set);
189
char *get_comment(const void *data, uint32_t data_len);
190
191
192
193
194
195

enum nft_rule_print {
	NFT_RULE_APPEND,
	NFT_RULE_DEL,
};

196
197
void nft_rule_print_save(struct nft_handle *h, const struct nftnl_rule *r,
			 enum nft_rule_print type, unsigned int format);
198
199
200
201
202
203
204

uint32_t nft_invflags2cmp(uint32_t invflags, uint32_t flag);

/*
 * global commit and abort
 */
int nft_commit(struct nft_handle *h);
205
int nft_bridge_commit(struct nft_handle *h);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
int nft_abort(struct nft_handle *h);

/*
 * revision compatibility.
 */
int nft_compatible_revision(const char *name, uint8_t rev, int opt);

/*
 * Error reporting.
 */
const char *nft_strerror(int err);

/* For xtables.c */
int do_commandx(struct nft_handle *h, int argc, char *argv[], char **table, bool restore);
/* For xtables-arptables.c */
221
222
int nft_init_arp(struct nft_handle *h, const char *pname);
int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table, bool restore);
223
/* For xtables-eb.c */
224
int nft_init_eb(struct nft_handle *h, const char *pname);
225
void nft_fini_eb(struct nft_handle *h);
226
227
int ebt_get_current_chain(const char *chain);
int do_commandeb(struct nft_handle *h, int argc, char *argv[], char **table, bool restore);
228

229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Translation from iptables to nft
 */
struct xt_buf;

bool xlate_find_match(const struct iptables_command_state *cs, const char *p_name);
int xlate_matches(const struct iptables_command_state *cs, struct xt_xlate *xl);
int xlate_action(const struct iptables_command_state *cs, bool goto_set,
		 struct xt_xlate *xl);
void xlate_ifname(struct xt_xlate *xl, const char *nftmeta, const char *ifname,
		  bool invert);

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * ARP
 */

struct arpt_entry;

int nft_arp_rule_append(struct nft_handle *h, const char *chain,
			const char *table, struct arpt_entry *fw,
			bool verbose);
int nft_arp_rule_insert(struct nft_handle *h, const char *chain,
			const char *table, struct arpt_entry *fw,
			int rulenum, bool verbose);

void nft_rule_to_arpt_entry(struct nftnl_rule *r, struct arpt_entry *fw);

256
257
258
259
bool nft_is_table_compatible(struct nft_handle *h,
			     const char *table, const char *chain);
void nft_assert_table_compatible(struct nft_handle *h,
				 const char *table, const char *chain);
260

261
262
263
int ebt_set_user_chain_policy(struct nft_handle *h, const char *table,
			      const char *chain, const char *policy);

264
#endif