GCC Code Coverage Report


Directory: ./
File: include/datatype.h
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 5 5 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 #ifndef NFTABLES_DATATYPE_H
2 #define NFTABLES_DATATYPE_H
3
4 #include <json.h>
5
6 /**
7 * enum datatypes
8 *
9 * @TYPE_INVALID: uninitialized
10 * @TYPE_VERDICT: nftables verdict
11 * @TYPE_NFPROTO: netfilter protocol (integer subtype)
12 * @TYPE_BITMASK: bitmask
13 * @TYPE_INTEGER: integer
14 * @TYPE_STRING: string
15 * @TYPE_LLADDR: link layer address (integer subtype)
16 * @TYPE_IPADDR: IPv4 address (integer subtype)
17 * @TYPE_IP6ADDR: IPv6 address (integer subtype)
18 * @TYPE_ETHERADDR: Ethernet address (lladdr subtype)
19 * @TYPE_ETHERTYPE: EtherType (integer subtype)
20 * @TYPE_ARPOP: ARP operation (integer subtype)
21 * @TYPE_INET_PROTOCOL: internet protocol (integer subtype)
22 * @TYPE_INET_SERVICE: internet service (integer subtype)
23 * @TYPE_ICMP_TYPE: ICMP type codes (integer subtype)
24 * @TYPE_TCP_FLAG: TCP flag (bitmask subtype)
25 * @TYPE_DCCP_PKTTYPE: DCCP packet type (integer subtype)
26 * @TYPE_MH_TYPE: Mobility Header type (integer subtype)
27 * @TYPE_TIME: relative time
28 * @TYPE_MARK: packet mark (integer subtype)
29 * @TYPE_IFINDEX: interface index (integer subtype)
30 * @TYPE_ARPHRD: interface type (integer subtype)
31 * @TYPE_REALM: routing realm (integer subtype)
32 * @TYPE_CLASSID: TC classid (integer subtype)
33 * @TYPE_UID: user ID (integer subtype)
34 * @TYPE_GID: group ID (integer subtype)
35 * @TYPE_CT_STATE: conntrack state (bitmask subtype)
36 * @TYPE_CT_DIR: conntrack direction
37 * @TYPE_CT_STATUS: conntrack status (bitmask subtype)
38 * @TYPE_ICMP6_TYPE: ICMPv6 type codes (integer subtype)
39 * @TYPE_CT_LABEL: Conntrack Label (bitmask subtype)
40 * @TYPE_PKTTYPE: packet type (integer subtype)
41 * @TYPE_ICMP_CODE: icmp code (integer subtype)
42 * @TYPE_ICMPV6_CODE: icmpv6 code (integer subtype)
43 * @TYPE_ICMPX_CODE: icmpx code (integer subtype)
44 * @TYPE_DEVGROUP: devgroup code (integer subtype)
45 * @TYPE_DSCP: Differentiated Services Code Point (integer subtype)
46 * @TYPE_IFNAME: interface name (string subtype)
47 * @TYPE_IGMP: IGMP type (integer subtype)
48 * @TYPE_TIME_DATA Date type (integer subtype)
49 * @TYPE_TIME_HOUR Hour type (integer subtype)
50 * @TYPE_TIME_DAY Day type (integer subtype)
51 * @TYPE_CGROUPV2 cgroups v2 (integer subtype)
52 */
53 enum datatypes {
54 TYPE_INVALID,
55 TYPE_VERDICT,
56 TYPE_NFPROTO,
57 TYPE_BITMASK,
58 TYPE_INTEGER,
59 TYPE_STRING,
60 TYPE_LLADDR,
61 TYPE_IPADDR,
62 TYPE_IP6ADDR,
63 TYPE_ETHERADDR,
64 TYPE_ETHERTYPE,
65 TYPE_ARPOP,
66 TYPE_INET_PROTOCOL,
67 TYPE_INET_SERVICE,
68 TYPE_ICMP_TYPE,
69 TYPE_TCP_FLAG,
70 TYPE_DCCP_PKTTYPE,
71 TYPE_MH_TYPE,
72 TYPE_TIME,
73 TYPE_MARK,
74 TYPE_IFINDEX,
75 TYPE_ARPHRD,
76 TYPE_REALM,
77 TYPE_CLASSID,
78 TYPE_UID,
79 TYPE_GID,
80 TYPE_CT_STATE,
81 TYPE_CT_DIR,
82 TYPE_CT_STATUS,
83 TYPE_ICMP6_TYPE,
84 TYPE_CT_LABEL,
85 TYPE_PKTTYPE,
86 TYPE_ICMP_CODE,
87 TYPE_ICMPV6_CODE,
88 TYPE_ICMPX_CODE,
89 TYPE_DEVGROUP,
90 TYPE_DSCP,
91 TYPE_ECN,
92 TYPE_FIB_ADDR,
93 TYPE_BOOLEAN,
94 TYPE_CT_EVENTBIT,
95 TYPE_IFNAME,
96 TYPE_IGMP_TYPE,
97 TYPE_TIME_DATE,
98 TYPE_TIME_HOUR,
99 TYPE_TIME_DAY,
100 TYPE_CGROUPV2,
101 __TYPE_MAX
102 };
103 #define TYPE_MAX (__TYPE_MAX - 1)
104
105 #define TYPE_BITS 6
106 #define TYPE_MASK ((1 << TYPE_BITS) - 1)
107
108 /**
109 * enum byteorder
110 *
111 * @BYTEORDER_INVALID: uninitialized/unknown
112 * @BYTEORDER_HOST_ENDIAN: host endian
113 * @BYTEORDER_BIG_ENDIAN: big endian
114 */
115 enum byteorder {
116 BYTEORDER_INVALID,
117 BYTEORDER_HOST_ENDIAN,
118 BYTEORDER_BIG_ENDIAN,
119 };
120
121 struct expr;
122
123 struct parse_ctx;
124 /**
125 * struct datatype
126 *
127 * @type: numeric identifier
128 * @byteorder: byteorder of type (non-basetypes only)
129 * @size: type size (fixed sized non-basetypes only)
130 * @subtypes: number of subtypes (concat type)
131 * @name: type name
132 * @desc: type description
133 * @basetype: basetype for subtypes, determines type compatibility
134 * @basefmt: format string for basetype
135 * @print: function to print a constant of this type
136 * @parse: function to parse a symbol and return an expression
137 * @sym_tbl: symbol table for this type
138 * @refcnt: reference counter (only for dynamically allocated, see .alloc)
139 */
140 struct datatype {
141 uint32_t type;
142 enum byteorder byteorder:8;
143 uint32_t alloc:1,
144 is_typeof:1;
145 unsigned int size;
146 unsigned int subtypes;
147 const char *name;
148 const char *desc;
149 const struct datatype *basetype;
150 const char *basefmt;
151 void (*print)(const struct expr *expr,
152 struct output_ctx *octx);
153 json_t *(*json)(const struct expr *expr,
154 struct output_ctx *octx);
155 struct error_record *(*parse)(struct parse_ctx *ctx,
156 const struct expr *sym,
157 struct expr **res);
158 struct error_record *(*err)(const struct expr *sym);
159 void (*describe)(struct output_ctx *octx);
160 const struct symbol_table *sym_tbl;
161 unsigned int refcnt;
162 };
163
164 extern const struct datatype *datatype_lookup(enum datatypes type);
165 extern const struct datatype *datatype_lookup_byname(const char *name);
166 extern const struct datatype *datatype_get(const struct datatype *dtype);
167 extern void datatype_set(struct expr *expr, const struct datatype *dtype);
168 extern void __datatype_set(struct expr *expr, const struct datatype *dtype);
169 extern void datatype_free(const struct datatype *dtype);
170 struct datatype *datatype_clone(const struct datatype *orig_dtype);
171 bool datatype_prefix_notation(const struct datatype *dtype);
172
173 struct parse_ctx {
174 struct symbol_tables *tbl;
175 const struct input_ctx *input;
176 };
177
178 extern struct error_record *symbol_parse(struct parse_ctx *ctx,
179 const struct expr *sym,
180 struct expr **res);
181 extern void datatype_print(const struct expr *expr, struct output_ctx *octx);
182
183 1945261 static inline bool datatype_equal(const struct datatype *d1,
184 const struct datatype *d2)
185 {
186 1945261 return d1->type == d2->type;
187 }
188
189 static inline const struct datatype *
190 20532 datatype_basetype(const struct datatype *dtype)
191 {
192
2/2
✓ Branch 0 (2→3) taken 20342 times.
✓ Branch 1 (2→4) taken 190 times.
20532 return dtype->basetype ? dtype->basetype : dtype;
193 }
194
195 /**
196 * struct symbolic_constant - symbol <-> constant mapping
197 *
198 * @identifier: symbol
199 * @value: symbolic value
200 */
201 struct symbolic_constant {
202 const char *identifier;
203 uint64_t value;
204 };
205
206 #define SYMBOL(id, v) { .identifier = (id), .value = (v) }
207 #define SYMBOL_LIST_END (struct symbolic_constant) { }
208
209 /**
210 * enum base - indicate how to display symbol table values
211 *
212 * @BASE_HEXADECIMAL: hexadecimal
213 * @BASE_DECIMAL: decimal
214 */
215 enum base {
216 BASE_HEXADECIMAL,
217 BASE_DECIMAL,
218 };
219
220 /**
221 * struct symbol_table - type construction from symbolic values
222 *
223 * @base: base of symbols representation
224 * @symbols: the symbols
225 */
226 struct symbol_table {
227 enum base base;
228 struct symbolic_constant symbols[];
229 };
230
231 extern struct error_record *symbolic_constant_parse(struct parse_ctx *ctx,
232 const struct expr *sym,
233 const struct symbol_table *tbl,
234 struct expr **res);
235 extern void symbolic_constant_print(const struct symbol_table *tbl,
236 const struct expr *expr, bool quotes,
237 struct output_ctx *octx);
238 extern void symbol_table_print(const struct symbol_table *tbl,
239 const struct datatype *dtype,
240 enum byteorder byteorder,
241 struct output_ctx *octx);
242
243 extern struct symbol_table *rt_symbol_table_init(const char *filename);
244 extern void rt_symbol_table_free(const struct symbol_table *tbl);
245 extern void rt_symbol_table_describe(struct output_ctx *octx, const char *name,
246 const struct symbol_table *tbl,
247 const struct datatype *type);
248
249 extern const struct datatype invalid_type;
250 extern const struct datatype verdict_type;
251 extern const struct datatype nfproto_type;
252 extern const struct datatype bitmask_type;
253 extern const struct datatype integer_type;
254 extern const struct datatype string_type;
255 extern const struct datatype lladdr_type;
256 extern const struct datatype ipaddr_type;
257 extern const struct datatype ip6addr_type;
258 extern const struct datatype etheraddr_type;
259 extern const struct datatype ethertype_type;
260 extern const struct datatype arphrd_type;
261 extern const struct datatype inet_protocol_type;
262 extern const struct datatype inet_service_type;
263 extern const struct datatype mark_type;
264 extern const struct datatype icmp_type_type;
265 extern const struct datatype icmp_code_type;
266 extern const struct datatype icmpv6_code_type;
267 extern const struct datatype icmpx_code_type;
268 extern const struct datatype igmp_type_type;
269 extern const struct datatype time_type;
270 extern const struct datatype boolean_type;
271 extern const struct datatype priority_type;
272 extern const struct datatype policy_type;
273 extern const struct datatype cgroupv2_type;
274 extern const struct datatype queue_type;
275
276 /* private datatypes for reject statement. */
277 extern const struct datatype reject_icmp_code_type;
278 extern const struct datatype reject_icmpv6_code_type;
279 extern const struct datatype reject_icmpx_code_type;
280
281 /* TYPE_INTEGER aliases: */
282 extern const struct datatype xinteger_type;
283 extern const struct datatype mptcpopt_subtype;
284
285 void inet_service_type_print(const struct expr *expr, struct output_ctx *octx);
286
287 extern const struct datatype *concat_type_alloc(uint32_t type);
288
289 175866 static inline uint32_t concat_subtype_add(uint32_t type, uint32_t subtype)
290 {
291 175866 return type << TYPE_BITS | subtype;
292 }
293
294 463206 static inline uint32_t concat_subtype_id(uint32_t type, unsigned int n)
295 {
296 463206 return (type >> TYPE_BITS * n) & TYPE_MASK;
297 }
298
299 static inline const struct datatype *
300 265419 concat_subtype_lookup(uint32_t type, unsigned int n)
301 {
302 265419 return datatype_lookup(concat_subtype_id(type, n));
303 }
304
305 extern const struct datatype *
306 set_datatype_alloc(const struct datatype *orig_dtype, enum byteorder byteorder);
307
308 extern void time_print(uint64_t msec, struct output_ctx *octx);
309 extern struct error_record *time_parse(const struct location *loc,
310 const char *c, uint64_t *res);
311
312 struct limit_rate {
313 uint64_t rate, unit;
314 };
315
316 extern void expr_chain_export(const struct expr *e, char *chain);
317
318 #endif /* NFTABLES_DATATYPE_H */
319