GCC Code Coverage Report


Directory: ./
File: include/netlink.h
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 6 6 100.0%
Functions: 3 3 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef NFTABLES_NETLINK_H
2 #define NFTABLES_NETLINK_H
3
4 #include <libnftnl/table.h>
5 #include <libnftnl/chain.h>
6 #include <libnftnl/rule.h>
7 #include <libnftnl/expr.h>
8 #include <libnftnl/set.h>
9 #include <libnftnl/object.h>
10 #include <libnftnl/flowtable.h>
11
12 #include <linux/netlink.h>
13 #include <linux/netfilter/nf_tables.h>
14
15 #include <rule.h>
16
17 #define MAX_REGS (1 + NFT_REG32_15 - NFT_REG32_00)
18
19 #ifndef NETLINK_EXT_ACK
20 #define NETLINK_EXT_ACK 11
21
22 enum nlmsgerr_attrs {
23 NLMSGERR_ATTR_UNUSED,
24 NLMSGERR_ATTR_MSG,
25 NLMSGERR_ATTR_OFFS,
26 NLMSGERR_ATTR_COOKIE,
27
28 __NLMSGERR_ATTR_MAX,
29 NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
30 };
31 #define NLM_F_CAPPED 0x100 /* request was capped */
32 #define NLM_F_ACK_TLVS 0x200 /* extended ACK TVLs were included */
33 #endif
34
35 struct netlink_parse_ctx {
36 struct list_head *msgs;
37 struct table *table;
38 struct rule *rule;
39 struct stmt *stmt;
40 struct expr *registers[MAX_REGS + 1];
41 unsigned int debug_mask;
42 struct netlink_ctx *nlctx;
43 bool inner;
44 uint8_t inner_reg;
45 };
46
47
48 #define RULE_PP_IN_CONCATENATION (1 << 0)
49 #define RULE_PP_IN_SET_ELEM (1 << 1)
50
51 #define RULE_PP_REMOVE_OP_AND (RULE_PP_IN_CONCATENATION | \
52 RULE_PP_IN_SET_ELEM)
53
54 struct dl_proto_ctx {
55 struct proto_ctx pctx;
56 struct payload_dep_ctx pdctx;
57 };
58
59 struct rule_pp_ctx {
60 struct dl_proto_ctx _dl[2];
61 struct dl_proto_ctx *dl;
62 struct stmt *stmt;
63 unsigned int flags;
64 struct set *set;
65 };
66
67 extern const struct input_descriptor indesc_netlink;
68 extern const struct location netlink_location;
69
70 /**
71 * struct netlink_ctx
72 *
73 * @nft: nftables context
74 * @msgs: message queue
75 * @list: list of parsed rules/chains/tables
76 * @set: current set
77 * @data: pointer to pass data to callback
78 * @seqnum: sequence number
79 */
80 struct netlink_ctx {
81 struct nft_ctx *nft;
82 struct list_head *msgs;
83 struct list_head list;
84 struct set *set;
85 const void *data;
86 uint32_t seqnum;
87 struct nftnl_batch *batch;
88 int maybe_emsgsize;
89 };
90
91 extern struct nftnl_expr *alloc_nft_expr(const char *name);
92 extern void alloc_setelem_cache(const struct expr *set, struct nftnl_set *nls);
93 struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
94 const struct expr *expr);
95
96 extern struct nftnl_table *netlink_table_alloc(const struct nlmsghdr *nlh);
97 extern struct nftnl_chain *netlink_chain_alloc(const struct nlmsghdr *nlh);
98 extern struct nftnl_set *netlink_set_alloc(const struct nlmsghdr *nlh);
99 extern struct nftnl_obj *netlink_obj_alloc(const struct nlmsghdr *nlh);
100 extern struct nftnl_flowtable *netlink_flowtable_alloc(const struct nlmsghdr *nlh);
101 extern struct nftnl_rule *netlink_rule_alloc(const struct nlmsghdr *nlh);
102
103 struct nft_data_linearize {
104 uint32_t len;
105 uint32_t value[NFT_REG32_COUNT];
106 char chain[NFT_CHAIN_MAXNAMELEN];
107 uint32_t chain_id;
108 int verdict;
109 };
110
111 struct nft_data_delinearize {
112 uint32_t len;
113 const uint32_t *value;
114 const char *chain;
115 int verdict;
116 };
117
118 945818 static inline unsigned int netlink_register_space(unsigned int size)
119 {
120 945818 return div_round_up(size, NFT_REG32_SIZE * BITS_PER_BYTE);
121 }
122
123 712640 static inline unsigned int netlink_padded_len(unsigned int size)
124 {
125 712640 return netlink_register_space(size) * NFT_REG32_SIZE * BITS_PER_BYTE;
126 }
127
128 60337 static inline unsigned int netlink_padding_len(unsigned int size)
129 {
130 60337 return netlink_padded_len(size) - size;
131 }
132
133 extern void netlink_gen_data(const struct expr *expr,
134 struct nft_data_linearize *data);
135 extern void netlink_gen_raw_data(const mpz_t value, enum byteorder byteorder,
136 unsigned int len,
137 struct nft_data_linearize *data);
138 extern struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt);
139
140 extern struct expr *netlink_alloc_value(const struct location *loc,
141 const struct nft_data_delinearize *nld);
142 extern struct expr *netlink_alloc_data(const struct location *loc,
143 const struct nft_data_delinearize *nld,
144 enum nft_registers dreg);
145
146 struct netlink_linearize_ctx;
147 extern void netlink_linearize_rule(struct netlink_ctx *ctx,
148 const struct rule *rule,
149 struct netlink_linearize_ctx *lctx);
150 extern struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx,
151 struct nftnl_rule *r);
152
153 extern int netlink_list_chains(struct netlink_ctx *ctx, const struct handle *h);
154 extern struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,
155 const struct nftnl_chain *nlc);
156
157 extern int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h,
158 const struct nft_cache_filter *filter);
159 extern struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
160 const struct nftnl_table *nlt);
161
162 extern struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
163 const struct nftnl_set *nls);
164
165 extern struct stmt *netlink_parse_set_expr(const struct set *set,
166 const struct nft_cache *cache,
167 const struct nftnl_expr *nle);
168
169 extern int netlink_list_setelems(struct netlink_ctx *ctx,
170 const struct handle *h, struct set *set,
171 bool reset);
172 extern int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,
173 const struct location *loc, struct set *cache_set,
174 struct set *set, struct expr *init, bool reset);
175 extern int netlink_delinearize_setelem(struct netlink_ctx *ctx,
176 struct nftnl_set_elem *nlse,
177 struct set *set);
178
179 extern int netlink_list_objs(struct netlink_ctx *ctx, const struct handle *h);
180 extern struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
181 struct nftnl_obj *nlo);
182
183 extern int netlink_list_flowtables(struct netlink_ctx *ctx,
184 const struct handle *h);
185 extern struct flowtable *netlink_delinearize_flowtable(struct netlink_ctx *ctx,
186 struct nftnl_flowtable *nlo);
187
188 extern void netlink_dump_chain(const struct nftnl_chain *nlc,
189 struct netlink_ctx *ctx);
190 extern void netlink_dump_rule(const struct nftnl_rule *nlr,
191 struct netlink_ctx *ctx);
192 extern void netlink_dump_expr(const struct nftnl_expr *nle,
193 FILE *fp, unsigned int debug_mask);
194 extern void netlink_dump_set(const struct nftnl_set *nls,
195 struct netlink_ctx *ctx);
196 extern void netlink_dump_obj(struct nftnl_obj *nlo, struct netlink_ctx *ctx);
197 extern void netlink_dump_flowtable(struct nftnl_flowtable *flo, struct netlink_ctx *ctx);
198
199 #define netlink_abi_error() \
200 __netlink_abi_error(__FILE__, __LINE__, strerror(errno));
201 extern void __noreturn __netlink_abi_error(const char *file, int line, const char *reason);
202 extern int netlink_io_error(struct netlink_ctx *ctx,
203 const struct location *loc, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
204 #define netlink_init_error() \
205 __netlink_init_error(__FILE__, __LINE__, strerror(errno));
206 extern void __noreturn __netlink_init_error(const char *file, int line, const char *reason);
207
208 struct netlink_mon_handler {
209 uint32_t monitor_flags;
210 uint32_t format;
211 struct netlink_ctx *ctx;
212 const struct location *loc;
213 unsigned int debug_mask;
214 struct nft_cache *cache;
215 };
216
217 extern int netlink_monitor(struct netlink_mon_handler *monhandler,
218 struct mnl_socket *nf_sock);
219 struct netlink_cb_data {
220 struct netlink_ctx *nl_ctx;
221 struct list_head *err_list;
222 };
223 int netlink_echo_callback(const struct nlmsghdr *nlh, void *data);
224
225 struct ruleset_parse {
226 struct netlink_ctx *nl_ctx;
227 struct cmd *cmd;
228 };
229
230 enum nft_data_types dtype_map_to_kernel(const struct datatype *dtype);
231
232 void netlink_linearize_init(struct netlink_linearize_ctx *lctx,
233 struct nftnl_rule *nlr);
234 void netlink_linearize_fini(struct netlink_linearize_ctx *lctx);
235
236 struct netlink_linearize_ctx {
237 struct nftnl_rule *nlr;
238 unsigned int reg_low;
239 struct list_head *expr_loc_htable;
240 };
241
242 #define NFT_EXPR_LOC_HSIZE 128
243
244 struct nft_expr_loc {
245 struct list_head hlist;
246 const struct nftnl_expr *nle;
247 const struct location *loc;
248 };
249
250 struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
251 struct netlink_linearize_ctx *ctx);
252
253 struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);
254
255 #endif /* NFTABLES_NETLINK_H */
256