GCC Code Coverage Report


Directory: ./
File: src/payload.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 692 775 89.3%
Functions: 48 49 98.0%
Branches: 405 524 77.3%

Line Branch Exec Source
1 /*
2 * Payload expression and related functions.
3 *
4 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 */
12
13 #include <nft.h>
14
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <net/if_arp.h>
18 #include <arpa/inet.h>
19 #include <linux/netfilter.h>
20 #include <linux/if_ether.h>
21 #include <netinet/ip_icmp.h>
22 #include <netinet/icmp6.h>
23
24 #include <rule.h>
25 #include <expression.h>
26 #include <statement.h>
27 #include <payload.h>
28 #include <gmputil.h>
29 #include <utils.h>
30 #include <json.h>
31
32 10487 bool payload_is_known(const struct expr *expr)
33 {
34 const struct proto_hdr_template *tmpl;
35 const struct proto_desc *desc;
36
37 10487 desc = expr->payload.desc;
38 10487 tmpl = expr->payload.tmpl;
39
40
5/8
✓ Branch 0 (2→3) taken 10487 times.
✗ Branch 1 (2→7) not taken.
✓ Branch 2 (3→4) taken 10487 times.
✗ Branch 3 (3→7) not taken.
✓ Branch 4 (4→5) taken 9795 times.
✓ Branch 5 (4→7) taken 692 times.
✓ Branch 6 (5→6) taken 9795 times.
✗ Branch 7 (5→7) not taken.
10487 return desc && tmpl && desc != &proto_unknown &&
41 tmpl != &proto_unknown_template;
42 }
43
44 5443 static void payload_expr_print(const struct expr *expr, struct output_ctx *octx)
45 {
46 const struct proto_desc *desc;
47 const struct proto_hdr_template *tmpl;
48
49
2/2
✓ Branch 0 (2→3) taken 208 times.
✓ Branch 1 (2→5) taken 5235 times.
5443 if (expr->payload.inner_desc &&
50
2/2
✓ Branch 0 (3→4) taken 198 times.
✓ Branch 1 (3→5) taken 10 times.
208 expr->payload.inner_desc != expr->payload.desc)
51 198 nft_print(octx, "%s ", expr->payload.inner_desc->name);
52
53 5443 desc = expr->payload.desc;
54 5443 tmpl = expr->payload.tmpl;
55
2/2
✓ Branch 0 (6→7) taken 5359 times.
✓ Branch 1 (6→8) taken 84 times.
5443 if (payload_is_known(expr))
56 5359 nft_print(octx, "%s %s", desc->name, tmpl->token);
57 else
58 84 nft_print(octx, "@%s,%u,%u",
59 84 proto_base_tokens[expr->payload.base],
60 84 expr->payload.offset, expr->len);
61 5443 }
62
63 400 bool payload_expr_cmp(const struct expr *e1, const struct expr *e2)
64 {
65 800 return e1->payload.inner_desc == e2->payload.inner_desc &&
66
1/2
✓ Branch 0 (3→4) taken 400 times.
✗ Branch 1 (3→8) not taken.
400 e1->payload.desc == e2->payload.desc &&
67
1/2
✓ Branch 0 (4→5) taken 400 times.
✗ Branch 1 (4→8) not taken.
400 e1->payload.tmpl == e2->payload.tmpl &&
68
2/4
✓ Branch 0 (2→3) taken 400 times.
✗ Branch 1 (2→8) not taken.
✓ Branch 2 (5→6) taken 400 times.
✗ Branch 3 (5→8) not taken.
1200 e1->payload.base == e2->payload.base &&
69
1/2
✓ Branch 0 (6→7) taken 400 times.
✗ Branch 1 (6→8) not taken.
400 e1->payload.offset == e2->payload.offset;
70 }
71
72 13595 static void payload_expr_clone(struct expr *new, const struct expr *expr)
73 {
74 13595 new->payload.inner_desc = expr->payload.inner_desc;
75 13595 new->payload.desc = expr->payload.desc;
76 13595 new->payload.tmpl = expr->payload.tmpl;
77 13595 new->payload.base = expr->payload.base;
78 13595 new->payload.offset = expr->payload.offset;
79 13595 }
80
81 /**
82 * payload_expr_pctx_update - update protocol context based on payload match
83 *
84 * @ctx: protocol context
85 * @expr: relational payload expression
86 *
87 * Update protocol context for relational payload expressions.
88 */
89 4525 static void payload_expr_pctx_update(struct proto_ctx *ctx,
90 const struct location *loc,
91 const struct expr *left,
92 const struct expr *right)
93 {
94 const struct proto_desc *base, *desc;
95 4525 unsigned int proto = 0;
96
97 /* Export the data in the correct byte order */
98
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4525 times.
4525 assert(right->len / BITS_PER_BYTE <= sizeof(proto));
99
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 4525 times.
4525 mpz_export_data(constant_data_ptr(proto, right->len), right->value,
100 right->byteorder, right->len / BITS_PER_BYTE);
101
102 4525 base = ctx->protocol[left->payload.base].desc;
103 4525 desc = proto_find_upper(base, proto);
104
105
2/2
✓ Branch 0 (8→9) taken 1018 times.
✓ Branch 1 (8→19) taken 3507 times.
4525 if (!desc) {
106
2/2
✓ Branch 0 (9→10) taken 523 times.
✓ Branch 1 (9→14) taken 495 times.
1018 if (base == &proto_icmp) {
107 /* proto 0 is ECHOREPLY, just pretend its ECHO.
108 * Not doing this would need an additional marker
109 * bit to tell when icmp.type was set.
110 */
111
2/2
✓ Branch 0 (10→11) taken 375 times.
✓ Branch 1 (10→12) taken 148 times.
523 ctx->th_dep.icmp.type = proto ? proto : ICMP_ECHO;
112
2/2
✓ Branch 0 (14→15) taken 475 times.
✓ Branch 1 (14→18) taken 20 times.
495 } else if (base == &proto_icmp6) {
113
2/2
✓ Branch 0 (15→16) taken 41 times.
✓ Branch 1 (15→17) taken 434 times.
475 if (proto == ICMP6_ECHO_REPLY)
114 41 proto = ICMP6_ECHO_REQUEST;
115 475 ctx->th_dep.icmp.type = proto;
116 }
117 1018 return;
118 }
119
120
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 3507 times.
3507 assert(desc->base <= PROTO_BASE_MAX);
121
2/2
✓ Branch 0 (21→22) taken 2203 times.
✓ Branch 1 (21→28) taken 1304 times.
3507 if (desc->base == base->base) {
122
1/2
✓ Branch 0 (22→23) taken 2203 times.
✗ Branch 1 (22→28) not taken.
2203 if (!left->payload.is_raw) {
123
2/2
✓ Branch 0 (23→24) taken 2202 times.
✓ Branch 1 (23→28) taken 1 times.
2203 if (desc->base == PROTO_BASE_LL_HDR &&
124
1/2
✓ Branch 0 (24→25) taken 2202 times.
✗ Branch 1 (24→28) not taken.
2202 ctx->stacked_ll_count < PROTO_CTX_NUM_PROTOS) {
125
1/2
✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 2202 times.
2202 assert(base->length > 0);
126 2202 ctx->stacked_ll[ctx->stacked_ll_count] = base;
127 2202 ctx->stacked_ll_count++;
128 }
129 }
130 }
131 3507 proto_ctx_update(ctx, desc->base, loc, desc);
132 }
133
134 #define NFTNL_UDATA_SET_KEY_PAYLOAD_DESC 0
135 #define NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE 1
136 #define NFTNL_UDATA_SET_KEY_PAYLOAD_BASE 2
137 #define NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET 3
138 #define NFTNL_UDATA_SET_KEY_PAYLOAD_LEN 4
139 #define NFTNL_UDATA_SET_KEY_PAYLOAD_INNER_DESC 5
140 #define NFTNL_UDATA_SET_KEY_PAYLOAD_MAX 6
141
142 4090 static unsigned int expr_payload_type(const struct proto_desc *desc,
143 const struct proto_hdr_template *tmpl)
144 {
145
2/2
✓ Branch 0 (2→3) taken 49 times.
✓ Branch 1 (2→4) taken 4041 times.
4090 if (desc->id == PROTO_DESC_UNKNOWN)
146 49 return 0;
147
148 4041 return (unsigned int)(tmpl - &desc->templates[0]);
149 }
150
151 4090 static int payload_expr_build_udata(struct nftnl_udata_buf *udbuf,
152 const struct expr *expr)
153 {
154 4090 const struct proto_hdr_template *tmpl = expr->payload.tmpl;
155 4090 const struct proto_desc *desc = expr->payload.desc;
156 4090 unsigned int type = expr_payload_type(desc, tmpl);
157
158 4090 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_DESC, desc->id);
159 4090 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE, type);
160
161
2/2
✓ Branch 0 (5→6) taken 49 times.
✓ Branch 1 (5→8) taken 4041 times.
4090 if (desc->id == 0) {
162 49 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_BASE,
163 49 expr->payload.base);
164 49 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET,
165 49 expr->payload.offset);
166 }
167
2/2
✓ Branch 0 (8→9) taken 1203 times.
✓ Branch 1 (8→10) taken 2887 times.
4090 if (expr->dtype->type == TYPE_INTEGER)
168 1203 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_LEN, expr->len);
169
170
2/2
✓ Branch 0 (10→11) taken 100 times.
✓ Branch 1 (10→12) taken 3990 times.
4090 if (expr->payload.inner_desc) {
171 100 nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEY_PAYLOAD_INNER_DESC,
172 100 expr->payload.inner_desc->id);
173 }
174
175 4090 return 0;
176 }
177
178 6686 const struct proto_desc *find_proto_desc(const struct nftnl_udata *ud)
179 {
180 6686 return proto_find_desc(nftnl_udata_get_u32(ud));
181 }
182
183 15497 static int payload_parse_udata(const struct nftnl_udata *attr, void *data)
184 {
185 15497 const struct nftnl_udata **ud = data;
186 15497 uint8_t type = nftnl_udata_type(attr);
187 15497 uint8_t len = nftnl_udata_len(attr);
188
189
1/2
✓ Branch 0 (4→5) taken 15497 times.
✗ Branch 1 (4→8) not taken.
15497 switch (type) {
190 15497 case NFTNL_UDATA_SET_KEY_PAYLOAD_DESC:
191 case NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE:
192 case NFTNL_UDATA_SET_KEY_PAYLOAD_BASE:
193 case NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET:
194 case NFTNL_UDATA_SET_KEY_PAYLOAD_LEN:
195 case NFTNL_UDATA_SET_KEY_PAYLOAD_INNER_DESC:
196
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 15497 times.
15497 if (len != sizeof(uint32_t))
197 ✗ return -1;
198 15497 break;
199 ✗ default:
200 ✗ return 0;
201 }
202
203 15497 ud[type] = attr;
204 15497 return 0;
205 }
206
207 6404 static struct expr *payload_expr_parse_udata(const struct nftnl_udata *attr)
208 {
209 6404 const struct nftnl_udata *ud[NFTNL_UDATA_SET_KEY_PAYLOAD_MAX + 1] = {};
210 6404 unsigned int type, base, offset, len = 0;
211 const struct proto_desc *desc;
212 6404 bool is_raw = false;
213 struct expr *expr;
214 int err;
215
216 6404 err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr),
217 payload_parse_udata, ud);
218
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 6404 times.
6404 if (err < 0)
219 ✗ return NULL;
220
221
1/2
✓ Branch 0 (7→8) taken 6404 times.
✗ Branch 1 (7→9) not taken.
6404 if (!ud[NFTNL_UDATA_SET_KEY_PAYLOAD_DESC] ||
222
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 6404 times.
6404 !ud[NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE])
223 ✗ return NULL;
224
225 6404 desc = find_proto_desc(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_DESC]);
226
2/2
✓ Branch 0 (11→12) taken 50 times.
✓ Branch 1 (11→18) taken 6354 times.
6404 if (!desc) {
227
1/2
✓ Branch 0 (12→13) taken 50 times.
✗ Branch 1 (12→14) not taken.
50 if (!ud[NFTNL_UDATA_SET_KEY_PAYLOAD_BASE] ||
228
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 50 times.
50 !ud[NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET])
229 ✗ return NULL;
230
231 50 base = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_BASE]);
232 50 offset = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET]);
233 50 is_raw = true;
234 }
235
236 6404 type = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE]);
237
2/2
✓ Branch 0 (19→20) taken 2307 times.
✓ Branch 1 (19→21) taken 4097 times.
6404 if (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN])
238 2307 len = nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN]);
239
240 6404 expr = payload_expr_alloc(&internal_location, desc, type);
241
242
2/2
✓ Branch 0 (22→23) taken 2307 times.
✓ Branch 1 (22→24) taken 4097 times.
6404 if (len)
243 2307 expr->len = len;
244
245
2/2
✓ Branch 0 (24→25) taken 50 times.
✓ Branch 1 (24→27) taken 6354 times.
6404 if (is_raw) {
246 struct datatype *dtype;
247
248 50 expr->payload.base = base;
249 50 expr->payload.offset = offset;
250 50 expr->payload.is_raw = true;
251 50 expr->len = len;
252 50 dtype = datatype_clone(&xinteger_type);
253 50 dtype->size = len;
254 50 dtype->byteorder = BYTEORDER_BIG_ENDIAN;
255 50 __datatype_set(expr, dtype);
256 }
257
258
2/2
✓ Branch 0 (27→28) taken 282 times.
✓ Branch 1 (27→30) taken 6122 times.
6404 if (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_INNER_DESC]) {
259 282 desc = find_proto_desc(ud[NFTNL_UDATA_SET_KEY_PAYLOAD_INNER_DESC]);
260 282 expr->payload.inner_desc = desc;
261 }
262
263 6404 return expr;
264 }
265
266 const struct expr_ops payload_expr_ops = {
267 .type = EXPR_PAYLOAD,
268 .name = "payload",
269 .print = payload_expr_print,
270 .json = payload_expr_json,
271 .cmp = payload_expr_cmp,
272 .clone = payload_expr_clone,
273 .pctx_update = payload_expr_pctx_update,
274 .build_udata = payload_expr_build_udata,
275 .parse_udata = payload_expr_parse_udata,
276 };
277
278 /*
279 * We normally use 'meta l4proto' to fetch the last l4 header of the
280 * ipv6 extension header chain so we will also match
281 * tcp after a fragmentation header, for instance.
282 * For consistency we also use meta l4proto for ipv4.
283 *
284 * If user specifically asks for nexthdr x, don't add another (useless)
285 * meta dependency.
286 */
287 29492 static bool proto_key_is_protocol(const struct proto_desc *desc, unsigned int type)
288 {
289
2/2
✓ Branch 0 (2→3) taken 3517 times.
✓ Branch 1 (2→4) taken 25975 times.
29492 if (type == desc->protocol_key)
290 3517 return true;
291
292
4/4
✓ Branch 0 (4→5) taken 1958 times.
✓ Branch 1 (4→7) taken 24017 times.
✓ Branch 2 (5→6) taken 183 times.
✓ Branch 3 (5→7) taken 1775 times.
25975 if (desc == &proto_ip6 && type == IP6HDR_NEXTHDR)
293 183 return true;
294
4/4
✓ Branch 0 (7→8) taken 7071 times.
✓ Branch 1 (7→10) taken 18721 times.
✓ Branch 2 (8→9) taken 787 times.
✓ Branch 3 (8→10) taken 6284 times.
25792 if (desc == &proto_ip && type == IPHDR_PROTOCOL)
295 787 return true;
296
297 25005 return false;
298 }
299
300 40893 struct expr *payload_expr_alloc(const struct location *loc,
301 const struct proto_desc *desc,
302 unsigned int type)
303 {
304 const struct proto_hdr_template *tmpl;
305 enum proto_bases base;
306 struct expr *expr;
307 40893 unsigned int flags = 0;
308
309
2/2
✓ Branch 0 (2→3) taken 29492 times.
✓ Branch 1 (2→6) taken 11401 times.
40893 if (desc != NULL) {
310 29492 tmpl = &desc->templates[type];
311 29492 base = desc->base;
312
2/2
✓ Branch 0 (4→5) taken 4487 times.
✓ Branch 1 (4→7) taken 25005 times.
29492 if (proto_key_is_protocol(desc, type))
313 4487 flags = EXPR_F_PROTOCOL;
314 } else {
315 11401 tmpl = &proto_unknown_template;
316 11401 base = PROTO_BASE_INVALID;
317 11401 desc = &proto_unknown;
318 }
319
320 40893 expr = expr_alloc(loc, EXPR_PAYLOAD, tmpl->dtype,
321 40893 tmpl->byteorder, tmpl->len);
322 40893 expr->flags |= flags;
323
324 40893 expr->payload.desc = desc;
325 40893 expr->payload.tmpl = tmpl;
326 40893 expr->payload.base = base;
327 40893 expr->payload.offset = tmpl->offset;
328
329 40893 return expr;
330 }
331
332 11207 void payload_init_raw(struct expr *expr, enum proto_bases base,
333 unsigned int offset, unsigned int len)
334 {
335 enum th_hdr_fields thf;
336
337 11207 expr->payload.base = base;
338 11207 expr->payload.offset = offset;
339 11207 expr->len = len;
340 11207 expr->dtype = &xinteger_type;
341
342
2/2
✓ Branch 0 (2→3) taken 6640 times.
✓ Branch 1 (2→4) taken 4567 times.
11207 if (base != PROTO_BASE_TRANSPORT_HDR)
343 6640 return;
344
2/2
✓ Branch 0 (4→5) taken 1689 times.
✓ Branch 1 (4→6) taken 2878 times.
4567 if (len != 16)
345 1689 return;
346
347
3/3
✓ Branch 0 (6→7) taken 437 times.
✓ Branch 1 (6→8) taken 1818 times.
✓ Branch 2 (6→11) taken 623 times.
2878 switch (offset) {
348 437 case 0:
349 437 thf = THDR_SPORT;
350 /* fall through */
351 2255 case 16:
352
2/2
✓ Branch 0 (8→9) taken 1818 times.
✓ Branch 1 (8→10) taken 437 times.
2255 if (offset == 16)
353 1818 thf = THDR_DPORT;
354 2255 expr->payload.tmpl = &proto_th.templates[thf];
355 2255 expr->payload.desc = &proto_th;
356 2255 expr->dtype = &inet_service_type;
357 2255 break;
358 623 default:
359 623 break;
360 }
361 }
362
363 4722 unsigned int payload_hdr_field(const struct expr *expr)
364 {
365 4722 return expr->payload.tmpl - expr->payload.desc->templates;
366 }
367
368 231 static void payload_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
369 {
370 231 expr_print(stmt->payload.expr, octx);
371 231 nft_print(octx, " set ");
372 231 expr_print(stmt->payload.val, octx);
373 231 }
374
375 861 static void payload_stmt_destroy(struct stmt *stmt)
376 {
377 861 expr_free(stmt->payload.expr);
378 861 expr_free(stmt->payload.val);
379 861 }
380
381 const struct stmt_ops payload_stmt_ops = {
382 .type = STMT_PAYLOAD,
383 .name = "payload",
384 .print = payload_stmt_print,
385 .json = payload_stmt_json,
386 .destroy = payload_stmt_destroy,
387 };
388
389 861 struct stmt *payload_stmt_alloc(const struct location *loc,
390 struct expr *expr, struct expr *val)
391 {
392 struct stmt *stmt;
393
394 861 stmt = stmt_alloc(loc, &payload_stmt_ops);
395 861 stmt->payload.expr = expr;
396 861 stmt->payload.val = val;
397 861 return stmt;
398 }
399
400 8170 static int payload_add_dependency(struct eval_ctx *ctx,
401 const struct proto_desc *desc,
402 const struct proto_desc *upper,
403 const struct expr *expr,
404 struct stmt **res)
405 {
406 const struct proto_hdr_template *tmpl;
407 struct expr *dep, *left, *right;
408 struct proto_ctx *pctx;
409 struct stmt *stmt;
410 int protocol;
411
412 8170 protocol = proto_find_num(desc, upper);
413
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 8170 times.
8170 if (protocol < 0)
414 ✗ return expr_error(ctx->msgs, expr,
415 "conflicting protocols specified: %s vs. %s",
416 desc->name, upper->name);
417
418 8170 tmpl = &desc->templates[desc->protocol_key];
419
2/2
✓ Branch 0 (6→7) taken 8030 times.
✓ Branch 1 (6→8) taken 140 times.
8170 if (tmpl->meta_key)
420 8030 left = meta_expr_alloc(&expr->location, tmpl->meta_key);
421 else
422 140 left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
423
424 8170 right = constant_expr_alloc(&expr->location, tmpl->dtype,
425 8170 tmpl->dtype->byteorder, tmpl->len,
426 constant_data_ptr(protocol, tmpl->len));
427
428 8170 dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
429
430 8170 stmt = expr_stmt_alloc(&dep->location, dep);
431
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 8170 times.
8170 if (stmt_dependency_evaluate(ctx, stmt) < 0)
432 ✗ return -1;
433
434
2/2
✓ Branch 0 (15→16) taken 333 times.
✓ Branch 1 (15→19) taken 7837 times.
8170 if (ctx->inner_desc) {
435
2/2
✓ Branch 0 (16→17) taken 303 times.
✓ Branch 1 (16→18) taken 30 times.
333 if (tmpl->meta_key)
436 303 left->meta.inner_desc = ctx->inner_desc;
437 else
438 30 left->payload.inner_desc = ctx->inner_desc;
439 }
440
441 8170 pctx = eval_proto_ctx(ctx);
442 8170 relational_expr_pctx_update(pctx, dep);
443 8170 *res = stmt;
444 8170 return 0;
445 }
446
447 static const struct proto_desc *
448 ✗ payload_get_get_ll_hdr(const struct proto_ctx *pctx)
449 {
450 ✗ switch (pctx->family) {
451 ✗ case NFPROTO_INET:
452 ✗ return &proto_inet;
453 ✗ case NFPROTO_BRIDGE:
454 ✗ return &proto_eth;
455 ✗ case NFPROTO_NETDEV:
456 ✗ return &proto_netdev;
457 ✗ default:
458 ✗ break;
459 }
460
461 ✗ return NULL;
462 }
463
464 static const struct proto_desc *
465 2372 payload_gen_special_dependency(struct eval_ctx *ctx, const struct expr *expr)
466 {
467 2372 struct proto_ctx *pctx = eval_proto_ctx(ctx);
468
469
1/3
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2372 times.
✗ Branch 2 (3→25) not taken.
2372 switch (expr->payload.base) {
470 ✗ case PROTO_BASE_LL_HDR:
471 ✗ return payload_get_get_ll_hdr(pctx);
472 2372 case PROTO_BASE_TRANSPORT_HDR:
473
2/2
✓ Branch 0 (5→6) taken 2304 times.
✓ Branch 1 (5→8) taken 68 times.
2372 if (expr->payload.desc == &proto_icmp ||
474
2/2
✓ Branch 0 (6→7) taken 2250 times.
✓ Branch 1 (6→8) taken 54 times.
2304 expr->payload.desc == &proto_icmp6 ||
475
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→24) taken 2250 times.
2250 expr->payload.desc == &proto_igmp) {
476 const struct proto_desc *desc, *desc_upper;
477 struct stmt *nstmt;
478
479 122 desc = pctx->protocol[PROTO_BASE_LL_HDR].desc;
480
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→12) taken 122 times.
122 if (!desc) {
481 ✗ desc = payload_get_get_ll_hdr(pctx);
482 ✗ if (!desc)
483 ✗ break;
484 }
485
486 /* this tunnel protocol does not encapsulate an inner
487 * link layer, use proto_netdev which relies on
488 * NFT_META_PROTOCOL for dependencies.
489 */
490
2/2
✓ Branch 0 (12→13) taken 40 times.
✓ Branch 1 (12→15) taken 82 times.
122 if (expr->payload.inner_desc &&
491
2/2
✓ Branch 0 (13→14) taken 10 times.
✓ Branch 1 (13→15) taken 30 times.
40 !(expr->payload.inner_desc->inner.flags & NFT_INNER_LL))
492 10 desc = &proto_netdev;
493
494 122 desc_upper = &proto_ip6;
495
2/2
✓ Branch 0 (15→16) taken 54 times.
✓ Branch 1 (15→17) taken 68 times.
122 if (expr->payload.desc == &proto_icmp ||
496
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 54 times.
54 expr->payload.desc == &proto_igmp)
497 68 desc_upper = &proto_ip;
498
499
1/2
✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 122 times.
122 if (payload_add_dependency(ctx, desc, desc_upper,
500 expr, &nstmt) < 0)
501 122 return NULL;
502
503 122 list_add_tail(&nstmt->list, &ctx->stmt->list);
504 122 return desc_upper;
505 }
506 2250 return &proto_inet_service;
507 ✗ default:
508 ✗ break;
509 }
510 ✗ return NULL;
511 }
512
513 /**
514 * payload_gen_dependency - generate match expression on payload dependency
515 *
516 * @ctx: evaluation context
517 * @expr: payload expression
518 * @res: dependency expression
519 *
520 * Generate matches on protocol dependencies. There are two different kinds
521 * of dependencies:
522 *
523 * - A payload expression for a base above the hook base requires a match
524 * on the protocol value in the lower layer header.
525 *
526 * - A payload expression for a base below the hook base is invalid in the
527 * output path since the lower layer header does not exist when the packet
528 * is classified. In the input path a payload expressions for a base exactly
529 * one below the hook base is valid. In this case a match on the device type
530 * is required to verify that we're dealing with the expected protocol.
531 *
532 * Note: since it is unknown to userspace which hooks a chain is called from,
533 * it is not explicitly verified. The NFT_META_IIFTYPE match will only match
534 * in the input path though.
535 */
536 7716 int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
537 struct stmt **res)
538 {
539 const struct hook_proto_desc *h;
540 const struct proto_desc *desc;
541 struct proto_ctx *pctx;
542 struct stmt *stmt;
543 uint16_t type;
544
545 7716 pctx = eval_proto_ctx(ctx);
546 7716 h = &hook_proto_desc[pctx->family];
547
2/2
✓ Branch 0 (3→4) taken 68 times.
✓ Branch 1 (3→16) taken 7648 times.
7716 if (expr->payload.base < h->base) {
548
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→7) taken 68 times.
68 if (expr->payload.base < h->base - 1)
549 ✗ return expr_error(ctx->msgs, expr,
550 "payload base is invalid for this "
551 "family");
552
553
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 68 times.
68 if (proto_dev_type(expr->payload.desc, &type) < 0)
554 ✗ return expr_error(ctx->msgs, expr,
555 "protocol specification is invalid "
556 "for this family");
557
558 68 stmt = meta_stmt_meta_iiftype(&expr->location, type);
559
1/2
✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 68 times.
68 if (stmt_dependency_evaluate(ctx, stmt) < 0)
560 ✗ return -1;
561
562 68 *res = stmt;
563
564 68 return 0;
565 }
566
567 7648 desc = pctx->protocol[expr->payload.base - 1].desc;
568 /* Special case for mixed IPv4/IPv6 and bridge tables */
569
2/2
✓ Branch 0 (16→17) taken 2372 times.
✓ Branch 1 (16→18) taken 5276 times.
7648 if (desc == NULL)
570 2372 desc = payload_gen_special_dependency(ctx, expr);
571
572
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→21) taken 7648 times.
7648 if (desc == NULL)
573 ✗ return expr_error(ctx->msgs, expr,
574 "ambiguous payload specification: "
575 "no %s protocol specified",
576 proto_base_names[expr->payload.base - 1]);
577
578
4/4
✓ Branch 0 (21→22) taken 691 times.
✓ Branch 1 (21→26) taken 6957 times.
✓ Branch 2 (22→23) taken 497 times.
✓ Branch 3 (22→26) taken 194 times.
7648 if (pctx->family == NFPROTO_BRIDGE && desc == &proto_eth) {
579 /* prefer netdev proto, which adds dependencies based
580 * on skb->protocol.
581 *
582 * This has the advantage that we will also match
583 * vlan encapsulated traffic.
584 *
585 * eth_hdr(skb)->type would not match, as nft_payload
586 * will pretend vlan tag was not offloaded, i.e.
587 * type is ETH_P_8021Q in such a case, but skb->protocol
588 * would still match the l3 header type.
589 */
590
2/2
✓ Branch 0 (23→24) taken 15 times.
✓ Branch 1 (23→25) taken 482 times.
497 if (expr->payload.desc == &proto_ip ||
591
1/2
✓ Branch 0 (24→25) taken 15 times.
✗ Branch 1 (24→26) not taken.
15 expr->payload.desc == &proto_ip6)
592 497 desc = &proto_netdev;
593 }
594
595 7648 return payload_add_dependency(ctx, desc, expr->payload.desc, expr, res);
596 }
597
598 400 int exthdr_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
599 const struct proto_desc *dependency,
600 enum proto_bases pb, struct stmt **res)
601 {
602 const struct proto_desc *desc;
603 struct proto_ctx *pctx;
604
605 400 pctx = eval_proto_ctx(ctx);
606 400 desc = pctx->protocol[pb].desc;
607
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→10) taken 400 times.
400 if (desc == NULL) {
608 ✗ if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
609 ✗ switch (pctx->family) {
610 ✗ case NFPROTO_NETDEV:
611 case NFPROTO_BRIDGE:
612 case NFPROTO_INET:
613 ✗ desc = &proto_inet_service;
614 ✗ goto found;
615 ✗ default:
616 ✗ break;
617 }
618 }
619
620 ✗ return expr_error(ctx->msgs, expr,
621 "Cannot generate dependency: "
622 "no %s protocol specified",
623 proto_base_names[pb]);
624 }
625
626 400 found:
627 400 return payload_add_dependency(ctx, desc, dependency, expr, res);
628 }
629
630 /**
631 * payload_is_stacked - return whether a payload protocol match defines a stacked
632 * protocol on the same layer
633 *
634 * @desc: current protocol description on this layer
635 * @expr: payload match
636 */
637 4375 bool payload_is_stacked(const struct proto_desc *desc, const struct expr *expr)
638 {
639 const struct proto_desc *next;
640
641
1/2
✓ Branch 0 (2→3) taken 4375 times.
✗ Branch 1 (2→5) not taken.
4375 if (expr->left->etype != EXPR_PAYLOAD ||
642
2/2
✓ Branch 0 (3→4) taken 936 times.
✓ Branch 1 (3→5) taken 3439 times.
4375 !(expr->left->flags & EXPR_F_PROTOCOL) ||
643
2/2
✓ Branch 0 (4→5) taken 33 times.
✓ Branch 1 (4→6) taken 903 times.
936 expr->op != OP_EQ)
644 3472 return false;
645
646 903 next = proto_find_upper(desc, mpz_get_be16(expr->right->value));
647
4/4
✓ Branch 0 (8→9) taken 467 times.
✓ Branch 1 (8→11) taken 436 times.
✓ Branch 2 (9→10) taken 315 times.
✓ Branch 3 (9→11) taken 152 times.
903 return next && next->base == desc->base;
648 }
649
650 12721 void payload_dependency_reset(struct payload_dep_ctx *ctx)
651 {
652 12721 memset(ctx, 0, sizeof(*ctx));
653 12721 }
654
655 8376 static bool payload_dependency_store_icmp_type(struct payload_dep_ctx *ctx,
656 const struct stmt *stmt)
657 {
658 8376 struct expr *dep = stmt->expr;
659 const struct proto_desc *desc;
660 const struct expr *right;
661 uint8_t type;
662
663
2/2
✓ Branch 0 (2→3) taken 7473 times.
✓ Branch 1 (2→4) taken 903 times.
8376 if (dep->left->etype != EXPR_PAYLOAD)
664 7473 return false;
665
666 903 right = dep->right;
667
3/4
✓ Branch 0 (4→5) taken 903 times.
✗ Branch 1 (4→6) not taken.
✓ Branch 2 (5→6) taken 467 times.
✓ Branch 3 (5→7) taken 436 times.
903 if (right->etype != EXPR_VALUE || right->len != BITS_PER_BYTE)
668 467 return false;
669
670 436 desc = dep->left->payload.desc;
671
2/2
✓ Branch 0 (7→8) taken 169 times.
✓ Branch 1 (7→12) taken 267 times.
436 if (desc == &proto_icmp) {
672 169 type = mpz_get_uint8(right->value);
673
674
2/2
✓ Branch 0 (9→10) taken 49 times.
✓ Branch 1 (9→11) taken 120 times.
169 if (type == ICMP_ECHOREPLY)
675 49 type = ICMP_ECHO;
676
677 169 ctx->icmp_type = type;
678
679 169 return type == ICMP_ECHO;
680
2/2
✓ Branch 0 (12→13) taken 108 times.
✓ Branch 1 (12→19) taken 159 times.
267 } else if (desc == &proto_icmp6) {
681 108 type = mpz_get_uint8(right->value);
682
683 108 ctx->icmp_type = type;
684
4/4
✓ Branch 0 (14→15) taken 86 times.
✓ Branch 1 (14→16) taken 22 times.
✓ Branch 2 (15→16) taken 4 times.
✓ Branch 3 (15→17) taken 82 times.
108 return type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY;
685 }
686
687 159 return false;
688 }
689
690 /**
691 * payload_dependency_store - store a possibly redundant protocol match
692 *
693 * @ctx: payload dependency context
694 * @stmt: payload match
695 * @base: base of payload match
696 */
697 8376 void payload_dependency_store(struct payload_dep_ctx *ctx,
698 struct stmt *stmt, enum proto_bases base)
699 {
700 8376 bool ignore_dep = payload_dependency_store_icmp_type(ctx, stmt);
701
702
2/2
✓ Branch 0 (3→4) taken 135 times.
✓ Branch 1 (3→5) taken 8241 times.
8376 if (ignore_dep)
703 135 return;
704
705 8241 ctx->pdeps[base + 1] = stmt;
706 }
707
708 /**
709 * payload_dependency_exists - there is a payload dependency in place
710 * @ctx: payload dependency context
711 * @base: payload protocol base
712 *
713 * Check if we have seen a protocol key payload expression for this base, we can
714 * usually remove it if we can infer it from another payload expression in the
715 * upper base.
716 */
717 60544 bool payload_dependency_exists(const struct payload_dep_ctx *ctx,
718 enum proto_bases base)
719 {
720
2/2
✓ Branch 0 (2→3) taken 7648 times.
✓ Branch 1 (2→4) taken 52896 times.
60544 if (ctx->pdeps[base])
721 7648 return true;
722
723
2/2
✓ Branch 0 (4→5) taken 702 times.
✓ Branch 1 (4→7) taken 52194 times.
53598 return base == PROTO_BASE_TRANSPORT_HDR &&
724
2/2
✓ Branch 0 (5→6) taken 69 times.
✓ Branch 1 (5→7) taken 633 times.
702 ctx->pdeps[PROTO_BASE_INNER_HDR];
725 }
726
727 /**
728 * payload_dependency_get - return a payload dependency if available
729 * @ctx: payload dependency context
730 * @base: payload protocol base
731 *
732 * If we have seen a protocol key payload expression for this base, we return
733 * it.
734 */
735 7591 struct expr *payload_dependency_get(struct payload_dep_ctx *ctx,
736 enum proto_bases base)
737 {
738
2/2
✓ Branch 0 (2→3) taken 7451 times.
✓ Branch 1 (2→4) taken 140 times.
7591 if (ctx->pdeps[base])
739 7451 return ctx->pdeps[base]->expr;
740
741
2/2
✓ Branch 0 (4→5) taken 138 times.
✓ Branch 1 (4→7) taken 2 times.
140 if (base == PROTO_BASE_TRANSPORT_HDR &&
742
1/2
✓ Branch 0 (5→6) taken 138 times.
✗ Branch 1 (5→7) not taken.
138 ctx->pdeps[PROTO_BASE_INNER_HDR])
743 138 return ctx->pdeps[PROTO_BASE_INNER_HDR]->expr;
744
745 2 return NULL;
746 }
747
748 − static void __payload_dependency_release(struct payload_dep_ctx *ctx,
749 enum proto_bases base)
750 {
751 − list_del(&ctx->pdeps[base]->list);
752 − stmt_free(ctx->pdeps[base]);
753
754 − if (ctx->pdeps[base] == ctx->prev)
755 − ctx->prev = NULL;
756 − ctx->pdeps[base] = NULL;
757 − }
758
759 7642 void payload_dependency_release(struct payload_dep_ctx *ctx,
760 enum proto_bases base)
761 {
762
2/2
✓ Branch 0 (2→3) taken 7583 times.
✓ Branch 1 (2→4) taken 59 times.
7642 if (ctx->pdeps[base])
763 7583 __payload_dependency_release(ctx, base);
764
1/2
✓ Branch 0 (4→5) taken 59 times.
✗ Branch 1 (4→7) not taken.
59 else if (base == PROTO_BASE_TRANSPORT_HDR &&
765
1/2
✓ Branch 0 (5→6) taken 59 times.
✗ Branch 1 (5→7) not taken.
59 ctx->pdeps[PROTO_BASE_INNER_HDR])
766 59 __payload_dependency_release(ctx, PROTO_BASE_INNER_HDR);
767 7642 }
768
769 201 static uint8_t icmp_dep_to_type(enum icmp_hdr_field_type t)
770 {
771
6/11
✗ Branch 0 (2→3) not taken.
✗ Branch 1 (2→5) not taken.
✗ Branch 2 (2→6) not taken.
✓ Branch 3 (2→7) taken 49 times.
✓ Branch 4 (2→8) taken 48 times.
✓ Branch 5 (2→9) taken 47 times.
✓ Branch 6 (2→10) taken 24 times.
✓ Branch 7 (2→11) taken 18 times.
✓ Branch 8 (2→12) taken 15 times.
✗ Branch 9 (2→13) not taken.
✗ Branch 10 (2→14) not taken.
201 switch (t) {
772 ✗ case PROTO_ICMP_ANY:
773 ✗ BUG("Invalid map for simple dependency");
774 ✗ case PROTO_ICMP_ECHO: return ICMP_ECHO;
775 ✗ case PROTO_ICMP6_ECHO: return ICMP6_ECHO_REQUEST;
776 49 case PROTO_ICMP_MTU: return ICMP_DEST_UNREACH;
777 48 case PROTO_ICMP_ADDRESS: return ICMP_REDIRECT;
778 47 case PROTO_ICMP6_MTU: return ICMP6_PACKET_TOO_BIG;
779 24 case PROTO_ICMP6_MGMQ: return MLD_LISTENER_QUERY;
780 18 case PROTO_ICMP6_PPTR: return ICMP6_PARAM_PROB;
781 15 case PROTO_ICMP6_REDIRECT: return ND_REDIRECT;
782 ✗ case PROTO_ICMP6_ADDRESS: return ND_NEIGHBOR_SOLICIT;
783 }
784
785 ✗ BUG("Missing icmp type mapping");
786 }
787
788 162 static bool icmp_dep_type_match(enum icmp_hdr_field_type t, uint8_t type)
789 {
790
4/6
✓ Branch 0 (2→3) taken 24 times.
✓ Branch 1 (2→8) taken 32 times.
✓ Branch 2 (2→13) taken 25 times.
✓ Branch 3 (2→22) taken 81 times.
✗ Branch 4 (2→24) not taken.
✗ Branch 5 (2→25) not taken.
162 switch (t) {
791 24 case PROTO_ICMP_ECHO:
792
3/4
✓ Branch 0 (3→4) taken 22 times.
✓ Branch 1 (3→5) taken 2 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 22 times.
24 return type == ICMP_ECHO || type == ICMP_ECHOREPLY;
793 32 case PROTO_ICMP6_ECHO:
794
3/4
✓ Branch 0 (8→9) taken 8 times.
✓ Branch 1 (8→10) taken 24 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 8 times.
32 return type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY;
795 25 case PROTO_ICMP6_ADDRESS:
796
2/2
✓ Branch 0 (14→15) taken 13 times.
✓ Branch 1 (14→19) taken 6 times.
19 return type == ND_NEIGHBOR_SOLICIT ||
797
2/2
✓ Branch 0 (15→16) taken 10 times.
✓ Branch 1 (15→19) taken 3 times.
13 type == ND_NEIGHBOR_ADVERT ||
798
2/2
✓ Branch 0 (16→17) taken 2 times.
✓ Branch 1 (16→19) taken 8 times.
10 type == ND_REDIRECT ||
799
1/2
✓ Branch 0 (17→18) taken 2 times.
✗ Branch 1 (17→19) not taken.
2 type == MLD_LISTENER_QUERY ||
800
3/4
✓ Branch 0 (13→14) taken 19 times.
✓ Branch 1 (13→19) taken 6 times.
✗ Branch 2 (18→19) not taken.
✓ Branch 3 (18→20) taken 2 times.
44 type == MLD_LISTENER_REPORT ||
801 type == MLD_LISTENER_REDUCTION;
802 81 case PROTO_ICMP_ADDRESS:
803 case PROTO_ICMP_MTU:
804 case PROTO_ICMP6_MTU:
805 case PROTO_ICMP6_MGMQ:
806 case PROTO_ICMP6_PPTR:
807 case PROTO_ICMP6_REDIRECT:
808 81 return icmp_dep_to_type(t) == type;
809 ✗ case PROTO_ICMP_ANY:
810 ✗ return true;
811 }
812 ✗ BUG("Missing icmp type mapping");
813 }
814
815 69 static bool payload_may_dependency_kill_icmp(struct payload_dep_ctx *ctx, const struct expr *expr)
816 {
817 69 const struct expr *dep = payload_dependency_get(ctx, expr->payload.base);
818 enum icmp_hdr_field_type icmp_dep;
819
820 69 icmp_dep = expr->payload.tmpl->icmp_dep;
821
2/2
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 65 times.
69 if (icmp_dep == PROTO_ICMP_ANY)
822 4 return false;
823
824
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 65 times.
65 if (dep->left->payload.desc != expr->payload.desc)
825 ✗ return false;
826
827
1/2
✓ Branch 0 (7→8) taken 65 times.
✗ Branch 1 (7→10) not taken.
65 if (expr->payload.tmpl->icmp_dep == PROTO_ICMP_ECHO ||
828
1/2
✓ Branch 0 (8→9) taken 65 times.
✗ Branch 1 (8→10) not taken.
65 expr->payload.tmpl->icmp_dep == PROTO_ICMP6_ECHO ||
829
2/2
✓ Branch 0 (9→10) taken 6 times.
✓ Branch 1 (9→11) taken 59 times.
65 expr->payload.tmpl->icmp_dep == PROTO_ICMP6_ADDRESS)
830 6 return false;
831
832 59 return ctx->icmp_type == icmp_dep_to_type(icmp_dep);
833 }
834
835 275 static bool payload_may_dependency_kill_ll(struct payload_dep_ctx *ctx, const struct expr *expr)
836 {
837 275 const struct expr *dep = payload_dependency_get(ctx, expr->payload.base);
838
839 /* Never remove a 'vlan type 0x...' expression, they are never added
840 * implicitly
841 */
842
2/2
✓ Branch 0 (3→4) taken 14 times.
✓ Branch 1 (3→5) taken 261 times.
275 if (dep->left->payload.desc == &proto_vlan)
843 14 return false;
844
845 /* 'vlan id 2' implies 'ether type 8021Q'. If a different protocol is
846 * tested, this is not a redundant expression.
847 */
848
1/2
✓ Branch 0 (5→6) taken 261 times.
✗ Branch 1 (5→10) not taken.
261 if (dep->left->payload.desc == &proto_eth &&
849
2/4
✓ Branch 0 (6→7) taken 261 times.
✗ Branch 1 (6→10) not taken.
✓ Branch 2 (7→8) taken 261 times.
✗ Branch 3 (7→10) not taken.
261 dep->right->etype == EXPR_VALUE && dep->right->len == 16)
850 261 return mpz_get_uint16(dep->right->value) == ETH_P_8021Q;
851
852 ✗ return true;
853 }
854
855 7022 static bool payload_may_dependency_kill(struct payload_dep_ctx *ctx,
856 unsigned int family, struct expr *expr)
857 {
858 7022 struct expr *dep = payload_dependency_get(ctx, expr->payload.base);
859
860 /* Protocol key payload expression at network base such as 'ip6 nexthdr'
861 * need to be left in place since it implicitly restricts matching to
862 * IPv6 for the bridge, inet and netdev families.
863 */
864
2/2
✓ Branch 0 (3→4) taken 5021 times.
✓ Branch 1 (3→19) taken 2001 times.
7022 switch (family) {
865 5021 case NFPROTO_BRIDGE:
866 case NFPROTO_NETDEV:
867 case NFPROTO_INET:
868
2/2
✓ Branch 0 (4→5) taken 410 times.
✓ Branch 1 (4→10) taken 4611 times.
5021 if (dep->left->etype == EXPR_PAYLOAD &&
869
2/2
✓ Branch 0 (5→6) taken 24 times.
✓ Branch 1 (5→10) taken 386 times.
410 dep->left->payload.base == PROTO_BASE_NETWORK_HDR &&
870
2/2
✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→8) taken 20 times.
24 (dep->left->payload.desc == &proto_ip ||
871
1/2
✓ Branch 0 (7→8) taken 4 times.
✗ Branch 1 (7→10) not taken.
4 dep->left->payload.desc == &proto_ip6) &&
872
1/2
✓ Branch 0 (8→9) taken 24 times.
✗ Branch 1 (8→10) not taken.
24 expr->payload.base == PROTO_BASE_TRANSPORT_HDR)
873 24 return false;
874 /* Do not kill
875 * ether type vlan and vlan type ip and ip protocol icmp
876 * into
877 * ip protocol icmp
878 * as this lacks ether type vlan.
879 * More generally speaking, do not kill protocol type
880 * for stacked protocols if we only have protcol type matches.
881 */
882
3/4
✓ Branch 0 (10→11) taken 386 times.
✓ Branch 1 (10→18) taken 4611 times.
✓ Branch 2 (11→12) taken 386 times.
✗ Branch 3 (11→18) not taken.
4997 if (dep->left->etype == EXPR_PAYLOAD && dep->op == OP_EQ &&
883
2/2
✓ Branch 0 (12→13) taken 326 times.
✓ Branch 1 (12→18) taken 60 times.
386 expr->payload.base == dep->left->payload.base) {
884
2/2
✓ Branch 0 (13→14) taken 51 times.
✓ Branch 1 (13→15) taken 275 times.
326 if (expr->flags & EXPR_F_PROTOCOL)
885 51 return false;
886
887
1/2
✓ Branch 0 (15→16) taken 275 times.
✗ Branch 1 (15→18) not taken.
275 if (expr->payload.base == PROTO_BASE_LL_HDR)
888 275 return payload_may_dependency_kill_ll(ctx, expr);
889 }
890
891 4671 break;
892 }
893
894
2/2
✓ Branch 0 (19→20) taken 2579 times.
✓ Branch 1 (19→21) taken 4093 times.
6672 if (expr->payload.base != PROTO_BASE_TRANSPORT_HDR)
895 2579 return true;
896
897
2/2
✓ Branch 0 (21→22) taken 6 times.
✓ Branch 1 (21→23) taken 4087 times.
4093 if (expr->payload.desc == &proto_th) {
898 /* &proto_th could mean any of udp, tcp, dccp, ... so we
899 * cannot remove the dependency.
900 *
901 * Also prefer raw payload @th syntax, there is no
902 * 'source/destination port' protocol here.
903 */
904 6 expr->payload.desc = &proto_unknown;
905 6 expr->dtype = &xinteger_type;
906 6 return false;
907 }
908
909
2/2
✓ Branch 0 (23→24) taken 73 times.
✓ Branch 1 (23→25) taken 4014 times.
4087 if (dep->left->etype != EXPR_PAYLOAD ||
910
2/2
✓ Branch 0 (24→25) taken 4 times.
✓ Branch 1 (24→26) taken 69 times.
73 dep->left->payload.base != PROTO_BASE_TRANSPORT_HDR)
911 4018 return true;
912
913
2/2
✓ Branch 0 (26→27) taken 34 times.
✓ Branch 1 (26→29) taken 35 times.
69 if (dep->left->payload.desc == &proto_icmp)
914 34 return payload_may_dependency_kill_icmp(ctx, expr);
915
916
1/2
✓ Branch 0 (29→30) taken 35 times.
✗ Branch 1 (29→32) not taken.
35 if (dep->left->payload.desc == &proto_icmp6)
917 35 return payload_may_dependency_kill_icmp(ctx, expr);
918
919 ✗ return true;
920 }
921
922 /**
923 * payload_dependency_kill - kill a redundant payload dependency
924 *
925 * @ctx: payload dependency context
926 * @expr: higher layer payload expression
927 *
928 * Kill a redundant payload expression if a higher layer payload expression
929 * implies its existence. Skip this if the dependency is a network payload and
930 * we are in bridge, netdev and inet families.
931 */
932 11503 void payload_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr,
933 unsigned int family)
934 {
935
4/4
✓ Branch 0 (2→3) taken 10693 times.
✓ Branch 1 (2→8) taken 810 times.
✓ Branch 2 (4→5) taken 7022 times.
✓ Branch 3 (4→8) taken 3671 times.
22196 if (expr->payload.desc != &proto_unknown &&
936
2/2
✓ Branch 0 (6→7) taken 6895 times.
✓ Branch 1 (6→8) taken 127 times.
17715 payload_dependency_exists(ctx, expr->payload.base) &&
937 7022 payload_may_dependency_kill(ctx, family, expr))
938 6895 payload_dependency_release(ctx, expr->payload.base);
939 11503 }
940
941 1230 void exthdr_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr,
942 unsigned int family)
943 {
944
4/4
✓ Branch 0 (2→3) taken 247 times.
✓ Branch 1 (2→7) taken 663 times.
✓ Branch 2 (2→11) taken 3 times.
✓ Branch 3 (2→15) taken 317 times.
1230 switch (expr->exthdr.op) {
945 247 case NFT_EXTHDR_OP_TCPOPT:
946
2/2
✓ Branch 0 (4→5) taken 6 times.
✓ Branch 1 (4→6) taken 241 times.
247 if (payload_dependency_exists(ctx, PROTO_BASE_TRANSPORT_HDR))
947 6 payload_dependency_release(ctx, PROTO_BASE_TRANSPORT_HDR);
948 247 break;
949 663 case NFT_EXTHDR_OP_IPV6:
950
2/2
✓ Branch 0 (8→9) taken 367 times.
✓ Branch 1 (8→10) taken 296 times.
663 if (payload_dependency_exists(ctx, PROTO_BASE_NETWORK_HDR))
951 367 payload_dependency_release(ctx, PROTO_BASE_NETWORK_HDR);
952 663 break;
953 3 case NFT_EXTHDR_OP_IPV4:
954
1/2
✓ Branch 0 (12→13) taken 3 times.
✗ Branch 1 (12→14) not taken.
3 if (payload_dependency_exists(ctx, PROTO_BASE_NETWORK_HDR))
955 3 payload_dependency_release(ctx, PROTO_BASE_NETWORK_HDR);
956 3 break;
957 317 default:
958 317 break;
959 }
960 1230 }
961
962 12557 static const struct proto_desc *get_stacked_desc(const struct proto_ctx *ctx,
963 const struct proto_desc *top,
964 const struct expr *e,
965 unsigned int *skip)
966 {
967 12557 unsigned int i, total, payload_offset = e->payload.offset;
968
969
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 12557 times.
12557 assert(e->etype == EXPR_PAYLOAD);
970
971
2/2
✓ Branch 0 (4→5) taken 1770 times.
✓ Branch 1 (4→6) taken 10787 times.
12557 if (e->payload.base != PROTO_BASE_LL_HDR ||
972
2/2
✓ Branch 0 (5→6) taken 534 times.
✓ Branch 1 (5→7) taken 1236 times.
1770 payload_offset < top->length) {
973 11321 *skip = 0;
974 11321 return top;
975 }
976
977
2/2
✓ Branch 0 (11→8) taken 1266 times.
✓ Branch 1 (11→12) taken 1226 times.
2492 for (i = 0, total = 0; i < ctx->stacked_ll_count; i++) {
978 const struct proto_desc *stacked;
979
980 1266 stacked = ctx->stacked_ll[i];
981
2/2
✓ Branch 0 (8→9) taken 10 times.
✓ Branch 1 (8→10) taken 1256 times.
1266 if (payload_offset < stacked->length) {
982 10 *skip = total;
983 10 return stacked;
984 }
985
986 1256 payload_offset -= stacked->length;
987 1256 total += stacked->length;
988 }
989
990 1226 *skip = total;
991 1226 return top;
992 }
993
994 /**
995 * payload_expr_complete - fill in type information of a raw payload expr
996 *
997 * @expr: the payload expression
998 * @ctx: protocol context
999 *
1000 * Complete the type of a raw payload expression based on the context. If
1001 * insufficient information is available the expression remains unchanged.
1002 */
1003 7552 void payload_expr_complete(struct expr *expr, const struct proto_ctx *ctx)
1004 {
1005 7552 unsigned int payload_offset = expr->payload.offset;
1006 const struct proto_desc *desc;
1007 const struct proto_hdr_template *tmpl;
1008 unsigned int i, total;
1009
1010
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 7552 times.
7552 assert(expr->etype == EXPR_PAYLOAD);
1011
1012 7552 desc = ctx->protocol[expr->payload.base].desc;
1013
4/4
✓ Branch 0 (4→5) taken 7384 times.
✓ Branch 1 (4→6) taken 168 times.
✓ Branch 2 (5→6) taken 4 times.
✓ Branch 3 (5→7) taken 7380 times.
7552 if (desc == NULL || desc == &proto_inet)
1014 6624 return;
1015
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 7380 times.
7380 assert(desc->base == expr->payload.base);
1016
1017 7380 desc = get_stacked_desc(ctx, desc, expr, &total);
1018 7380 payload_offset -= total;
1019
1020
2/2
✓ Branch 0 (24→11) taken 68959 times.
✓ Branch 1 (24→25) taken 928 times.
69887 for (i = 0; i < array_size(desc->templates); i++) {
1021 68959 tmpl = &desc->templates[i];
1022
2/2
✓ Branch 0 (11→12) taken 15571 times.
✓ Branch 1 (11→13) taken 53388 times.
68959 if (tmpl->offset != payload_offset ||
1023
2/2
✓ Branch 0 (12→13) taken 9047 times.
✓ Branch 1 (12→14) taken 6524 times.
15571 tmpl->len != expr->len)
1024 62435 continue;
1025
1026
3/4
✓ Branch 0 (14→15) taken 46 times.
✓ Branch 1 (14→17) taken 6478 times.
✓ Branch 2 (15→16) taken 46 times.
✗ Branch 3 (15→17) not taken.
6524 if (tmpl->meta_key && i == 0)
1027 46 continue;
1028
1029
4/4
✓ Branch 0 (17→18) taken 96 times.
✓ Branch 1 (17→22) taken 6382 times.
✓ Branch 2 (18→19) taken 80 times.
✓ Branch 3 (18→22) taken 16 times.
6478 if (tmpl->icmp_dep && ctx->th_dep.icmp.type &&
1030
2/2
✓ Branch 0 (20→21) taken 26 times.
✓ Branch 1 (20→22) taken 54 times.
80 !icmp_dep_type_match(tmpl->icmp_dep,
1031 80 ctx->th_dep.icmp.type))
1032 26 continue;
1033
1034 6452 expr->dtype = tmpl->dtype;
1035 6452 expr->payload.desc = desc;
1036 6452 expr->byteorder = tmpl->byteorder;
1037 6452 expr->payload.tmpl = tmpl;
1038 6452 return;
1039 }
1040 }
1041
1042 2346 static unsigned int mask_to_offset(const struct expr *mask)
1043 {
1044
1/2
✓ Branch 0 (2→3) taken 2346 times.
✗ Branch 1 (2→4) not taken.
2346 return mask ? mpz_scan1(mask->value, 0) : 0;
1045 }
1046
1047 1219 static unsigned int mask_length(const struct expr *mask)
1048 {
1049 unsigned long off;
1050
1051 1219 off = mask_to_offset(mask);
1052
1053 1219 return mpz_scan0(mask->value, off + 1);
1054 }
1055
1056 /**
1057 * payload_expr_trim - trim payload expression according to mask
1058 *
1059 * @expr: the payload expression
1060 * @mask: mask to use when searching templates
1061 * @ctx: protocol context
1062 * @shift: shift adjustment to fix up RHS value
1063 *
1064 * Walk the template list and determine if a match can be found without
1065 * using the provided mask.
1066 *
1067 * If the mask has to be used, trim the payload expression length accordingly,
1068 * adjust the payload offset and return true to let the caller know that the
1069 * mask can be removed. This function also returns the shift for the right hand
1070 * constant side of the expression.
1071 */
1072 1059 bool payload_expr_trim(struct expr *expr, struct expr *mask,
1073 const struct proto_ctx *ctx, unsigned int *shift)
1074 {
1075 1059 unsigned int payload_offset = expr->payload.offset;
1076 1059 unsigned int mask_offset = mask_to_offset(mask);
1077 1059 unsigned int mask_len = mask_length(mask);
1078 const struct proto_hdr_template *tmpl;
1079 1059 unsigned int payload_len = expr->len;
1080 const struct proto_desc *desc;
1081 1059 unsigned int off, i, len = 0;
1082 unsigned int total;
1083
1084
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1059 times.
1059 assert(expr->etype == EXPR_PAYLOAD);
1085
1086 1059 desc = ctx->protocol[expr->payload.base].desc;
1087
2/2
✓ Branch 0 (6→7) taken 68 times.
✓ Branch 1 (6→8) taken 991 times.
1059 if (desc == NULL)
1088 68 return false;
1089
1090
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 991 times.
991 assert(desc->base == expr->payload.base);
1091
1092 991 desc = get_stacked_desc(ctx, desc, expr, &total);
1093 991 payload_offset -= total;
1094
1095 991 off = round_up(mask->len, BITS_PER_BYTE) - mask_len;
1096 991 payload_offset += off;
1097
1098
2/2
✓ Branch 0 (21→12) taken 5456 times.
✓ Branch 1 (21→22) taken 137 times.
5593 for (i = 1; i < array_size(desc->templates); i++) {
1099 5456 tmpl = &desc->templates[i];
1100
2/2
✓ Branch 0 (12→13) taken 4354 times.
✓ Branch 1 (12→14) taken 1102 times.
5456 if (tmpl->offset != payload_offset)
1101 4354 continue;
1102
1103
2/2
✓ Branch 0 (14→15) taken 20 times.
✓ Branch 1 (14→16) taken 1082 times.
1102 if (tmpl->len > payload_len)
1104 20 return false;
1105
1106 1082 payload_len -= tmpl->len;
1107 1082 payload_offset += tmpl->len;
1108 1082 len += tmpl->len;
1109
2/2
✓ Branch 0 (16→17) taken 2 times.
✓ Branch 1 (16→18) taken 1080 times.
1082 if (payload_len == 0)
1110 2 return false;
1111
1112
2/2
✓ Branch 0 (18→19) taken 832 times.
✓ Branch 1 (18→20) taken 248 times.
1080 if (mask_offset + len == mask_len) {
1113 832 expr->payload.offset += off;
1114 832 expr->len = len;
1115 832 *shift = mask_offset;
1116 832 return true;
1117 }
1118 }
1119
1120 137 return false;
1121 }
1122
1123 /**
1124 * payload_expr_trim_force - adjust payload len/offset according to mask
1125 *
1126 * @expr: the payload expression
1127 * @mask: mask to use when searching templates
1128 * @shift: shift adjustment to fix up RHS value
1129 *
1130 * Force-trim an unknown payload expression according to mask.
1131 *
1132 * This is only useful for unkown payload expressions that need
1133 * to be printed in raw syntax (@base,offset,length). The kernel
1134 * can only deal with byte-divisible offsets/length, e.g. @th,16,8.
1135 * In such case we might be able to get rid of the mask.
1136 * @base,offset,length & MASK OPERATOR VALUE then becomes
1137 * @base,offset,length VALUE, where at least one of offset increases
1138 * and length decreases.
1139 *
1140 * This function also returns the shift for the right hand
1141 * constant side of the expression.
1142 *
1143 * @return: true if @expr was adjusted and mask can be discarded.
1144 */
1145 160 bool payload_expr_trim_force(struct expr *expr, struct expr *mask, unsigned int *shift)
1146 {
1147 160 unsigned int payload_offset = expr->payload.offset;
1148 160 unsigned int mask_len = mask_length(mask);
1149 unsigned int off, real_len;
1150
1151
4/4
✓ Branch 0 (4→5) taken 80 times.
✓ Branch 1 (4→6) taken 80 times.
✓ Branch 2 (5→6) taken 12 times.
✓ Branch 3 (5→7) taken 68 times.
160 if (payload_is_known(expr) || expr->len <= mask_len)
1152 92 return false;
1153
1154 /* This provides the payload offset to use.
1155 * mask->len is the total length of the mask, e.g. 16.
1156 * mask_len holds the last bit number that will be zeroed,
1157 */
1158 68 off = round_up(mask->len, BITS_PER_BYTE) - mask_len;
1159 68 payload_offset += off;
1160
1161 /* kernel only allows offsets <= 255 */
1162
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 68 times.
68 if (round_up(payload_offset, BITS_PER_BYTE) > 255)
1163 ✗ return false;
1164
1165 68 real_len = mpz_popcount(mask->value);
1166
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 68 times.
68 if (real_len > expr->len)
1167 ✗ return false;
1168
1169 68 expr->payload.offset = payload_offset;
1170 68 expr->len = real_len;
1171
1172 68 *shift = mask_to_offset(mask);
1173 68 return true;
1174 }
1175
1176 /**
1177 * stmt_payload_expr_trim - adjust payload len/offset according to mask
1178 *
1179 * @stmt: the payload statement
1180 * @pctx: protocol context
1181 *
1182 * Infer offset to header field from mask, walk the template list to determine
1183 * if offset falls within a matching header field.
1184 *
1185 * Trim the payload expression length accordingly, adjust the payload offset
1186 * and return true if payload statement expressions has been updated.
1187 *
1188 * @return: true if @stmt was adjusted.
1189 */
1190 184 bool stmt_payload_expr_trim(struct stmt *stmt, const struct proto_ctx *pctx)
1191 {
1192 184 struct expr *expr = stmt->payload.val;
1193 const struct proto_hdr_template *tmpl;
1194 const struct proto_desc *desc;
1195 struct expr *payload, *mask;
1196 uint32_t offset, i, shift;
1197 unsigned int mask_offset;
1198 mpz_t bitmask, tmp, tmp2;
1199 unsigned long n;
1200
1201
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 184 times.
184 assert(stmt->type == STMT_PAYLOAD);
1202
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 184 times.
184 assert(expr->etype == EXPR_BINOP);
1203
1204 184 payload = expr->left;
1205 184 mask = expr->right;
1206
1207
1/2
✓ Branch 0 (6→7) taken 184 times.
✗ Branch 1 (6→8) not taken.
184 if (payload->etype != EXPR_PAYLOAD ||
1208
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 184 times.
184 mask->etype != EXPR_VALUE)
1209 ✗ return false;
1210
1211
2/2
✓ Branch 0 (10→11) taken 162 times.
✓ Branch 1 (10→13) taken 22 times.
184 if (payload_is_known(payload) ||
1212
2/2
✓ Branch 0 (11→12) taken 144 times.
✓ Branch 1 (11→13) taken 18 times.
162 !pctx->protocol[payload->payload.base].desc ||
1213
2/2
✓ Branch 0 (12→13) taken 4 times.
✓ Branch 1 (12→14) taken 140 times.
144 payload->len % (2 * BITS_PER_BYTE) != 0)
1214 44 return false;
1215
1216
2/3
✓ Branch 0 (14→15) taken 80 times.
✓ Branch 1 (14→18) taken 60 times.
✗ Branch 2 (14→21) not taken.
140 switch (expr->op) {
1217 80 case OP_AND:
1218 /* infer offset from first 0 in mask */
1219 80 n = mpz_scan0(mask->value, 0);
1220
1/2
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 80 times.
80 if (n == ULONG_MAX)
1221 ✗ return false;
1222
1223 80 mask_offset = payload->len - n;
1224 80 break;
1225 60 case OP_OR:
1226 case OP_XOR:
1227 /* infer offset from first 1 in mask */
1228 60 n = mpz_scan1(mask->value, 0);
1229
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 60 times.
60 if (n == ULONG_MAX)
1230 ✗ return false;
1231
1232 60 mask_offset = payload->len - n;
1233 60 break;
1234 ✗ default:
1235 ✗ return false;
1236 }
1237
1238 140 offset = payload->payload.offset + mask_offset;
1239
1240 140 desc = pctx->protocol[payload->payload.base].desc;
1241
1/2
✓ Branch 0 (68→23) taken 480 times.
✗ Branch 1 (68→69) not taken.
480 for (i = 1; i < array_size(desc->templates); i++) {
1242 480 tmpl = &desc->templates[i];
1243
1244
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 480 times.
480 if (tmpl->len == 0)
1245 ✗ return false;
1246
1247 /* Is this inferred offset within this header field? */
1248
2/2
✓ Branch 0 (25→26) taken 140 times.
✓ Branch 1 (25→67) taken 340 times.
480 if (tmpl->offset + tmpl->len >= offset) {
1249 /* Infer shift to reach this header field. */
1250
2/2
✓ Branch 0 (26→27) taken 40 times.
✓ Branch 1 (26→28) taken 100 times.
140 if ((tmpl->offset % (2 * BITS_PER_BYTE)) < 8) {
1251 40 shift = BITS_PER_BYTE - (tmpl->offset % BITS_PER_BYTE + tmpl->len);
1252 40 shift += BITS_PER_BYTE;
1253 } else {
1254 100 shift = (2 * BITS_PER_BYTE) - (tmpl->offset % (2 * BITS_PER_BYTE) + tmpl->len);
1255 }
1256
1257 /* Build bitmask to fetch this header field. */
1258 140 mpz_init2(bitmask, payload->len);
1259 140 mpz_bitmask(bitmask, tmpl->len);
1260
2/2
✓ Branch 0 (31→32) taken 80 times.
✓ Branch 1 (31→33) taken 60 times.
140 if (shift)
1261 80 mpz_lshift_ui(bitmask, shift);
1262
1263 /* Check if mask expression falls within this header
1264 * bitfield, if the mask expression is over this header
1265 * field, then skip this delinearization, this could be
1266 * a raw expression.
1267 */
1268
2/3
✓ Branch 0 (33→34) taken 80 times.
✓ Branch 1 (33→44) taken 60 times.
✗ Branch 2 (33→51) not taken.
140 switch (expr->op) {
1269 80 case OP_AND:
1270 /* Inverted bitmask to fetch untouched bits. */
1271 80 mpz_init_bitmask(tmp, payload->len);
1272 80 mpz_xor(tmp, bitmask, tmp);
1273
1274 /* Get untouched bits out of the header field. */
1275 80 mpz_init2(tmp2, payload->len);
1276 80 mpz_and(tmp2, mask->value, tmp);
1277
1278 /* Modified any bits out of the header field? */
1279
1/2
✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→43) taken 80 times.
80 if (mpz_cmp(tmp, tmp2) != 0) {
1280 ✗ mpz_clear(tmp);
1281 ✗ mpz_clear(tmp2);
1282 ✗ mpz_clear(bitmask);
1283 ✗ return false;
1284 }
1285 80 mpz_clear(tmp2);
1286 80 break;
1287 60 case OP_OR:
1288 case OP_XOR:
1289 60 mpz_init2(tmp, payload->len);
1290
1291 /* Get modified bits in header field. */
1292 60 mpz_and(tmp, mask->value, bitmask);
1293
1294 /* Modified any bits out of the header field? */
1295
1/2
✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→50) taken 60 times.
60 if (mpz_cmp(tmp, mask->value) != 0) {
1296 ✗ mpz_clear(tmp);
1297 ✗ mpz_clear(bitmask);
1298 ✗ return false;
1299 }
1300 60 break;
1301 ✗ default:
1302 ✗ assert(0);
1303 break;
1304 }
1305 140 mpz_clear(tmp);
1306
1307 /* Clear unrelated bits for this header field. Shrink
1308 * to "real size". Shift bits when needed.
1309 */
1310 140 mpz_and(mask->value, bitmask, mask->value);
1311 140 mpz_clear(bitmask);
1312
1313 140 mask->len -= (tmpl->offset - payload->payload.offset);
1314
2/2
✓ Branch 0 (57→58) taken 80 times.
✓ Branch 1 (57→59) taken 60 times.
140 if (shift) {
1315 80 mask->len -= shift;
1316 80 mpz_rshift_ui(mask->value, shift);
1317 }
1318 140 payload->payload.offset = tmpl->offset;
1319 140 payload->len = tmpl->len;
1320
1321 140 expr_free(stmt->payload.expr);
1322 140 stmt->payload.expr = expr_get(payload);
1323
1324
2/2
✓ Branch 0 (61→62) taken 80 times.
✓ Branch 1 (61→66) taken 60 times.
140 if (expr->op == OP_AND) {
1325 /* Reduce 'expr AND 0x0', otherwise listing
1326 * shows:
1327 *
1328 * ip dscp set ip dscp & 0x0
1329 *
1330 * instead of the more compact:
1331 *
1332 * ip dscp set 0x0
1333 */
1334
3/4
✓ Branch 0 (62→63) taken 80 times.
✗ Branch 1 (62→66) not taken.
✓ Branch 2 (63→64) taken 10 times.
✓ Branch 3 (63→66) taken 70 times.
80 if (mpz_cmp_ui(mask->value, 0) == 0) {
1335 10 expr = stmt->payload.val;
1336 10 stmt->payload.val = expr_get(mask);
1337 10 expr_free(expr);
1338 }
1339 }
1340 140 return true;
1341 }
1342 }
1343
1344 ✗ return false;
1345 }
1346
1347 /**
1348 * payload_expr_expand - expand raw merged adjacent payload expressions into its
1349 * original components
1350 *
1351 * @list: list to append expanded payload expressions to
1352 * @expr: the payload expression to expand
1353 * @ctx: protocol context
1354 *
1355 * Expand a merged adjacent payload expression into its original components
1356 * by splitting elements off the beginning matching a payload template.
1357 *
1358 * Note: this requires all payload templates to be specified in ascending
1359 * offset order.
1360 */
1361 4254 void payload_expr_expand(struct list_head *list, struct expr *expr,
1362 const struct proto_ctx *ctx)
1363 {
1364 4254 unsigned int payload_offset = expr->payload.offset;
1365 const struct proto_hdr_template *tmpl;
1366 const struct proto_desc *desc;
1367 unsigned int i, total;
1368 struct expr *new;
1369
1370
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4254 times.
4254 assert(expr->etype == EXPR_PAYLOAD);
1371
1372 4254 desc = ctx->protocol[expr->payload.base].desc;
1373
4/4
✓ Branch 0 (4→5) taken 4198 times.
✓ Branch 1 (4→6) taken 56 times.
✓ Branch 2 (5→6) taken 12 times.
✓ Branch 3 (5→7) taken 4186 times.
4254 if (desc == NULL || desc == &proto_unknown)
1374 68 goto raw;
1375
1376
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 4186 times.
4186 assert(desc->base == expr->payload.base);
1377
1378 4186 desc = get_stacked_desc(ctx, desc, expr, &total);
1379 4186 payload_offset -= total;
1380
1381
1/2
✓ Branch 0 (31→11) taken 17936 times.
✗ Branch 1 (31→32) not taken.
17936 for (i = 1; i < array_size(desc->templates); i++) {
1382 17936 tmpl = &desc->templates[i];
1383
1384
2/2
✓ Branch 0 (11→12) taken 6 times.
✓ Branch 1 (11→13) taken 17930 times.
17936 if (tmpl->len == 0)
1385 6 break;
1386
1387
2/2
✓ Branch 0 (13→14) taken 13603 times.
✓ Branch 1 (13→15) taken 4327 times.
17930 if (tmpl->offset != payload_offset)
1388 13603 continue;
1389
1390
4/4
✓ Branch 0 (15→16) taken 90 times.
✓ Branch 1 (15→20) taken 4237 times.
✓ Branch 2 (16→17) taken 69 times.
✓ Branch 3 (16→20) taken 21 times.
4327 if (tmpl->icmp_dep && ctx->th_dep.icmp.type &&
1391
2/2
✓ Branch 0 (18→19) taken 26 times.
✓ Branch 1 (18→20) taken 43 times.
69 !icmp_dep_type_match(tmpl->icmp_dep,
1392 69 ctx->th_dep.icmp.type))
1393 26 continue;
1394
1395
2/2
✓ Branch 0 (20→21) taken 4198 times.
✓ Branch 1 (20→25) taken 103 times.
4301 if (tmpl->len <= expr->len) {
1396 4198 new = payload_expr_alloc(&expr->location, desc, i);
1397 4198 list_add_tail(&new->list, list);
1398 4198 expr->len -= tmpl->len;
1399 4198 expr->payload.offset += tmpl->len;
1400 4198 payload_offset += tmpl->len;
1401
2/2
✓ Branch 0 (23→24) taken 4077 times.
✓ Branch 1 (23→30) taken 121 times.
4198 if (expr->len == 0)
1402 4180 return;
1403
1/2
✓ Branch 0 (25→26) taken 103 times.
✗ Branch 1 (25→29) not taken.
103 } else if (expr->len > 0) {
1404 103 new = payload_expr_alloc(&expr->location, desc, i);
1405 103 new->len = expr->len;
1406 103 list_add_tail(&new->list, list);
1407 103 return;
1408 } else
1409 ✗ break;
1410 }
1411 ✗ raw:
1412 74 new = payload_expr_alloc(&expr->location, NULL, 0);
1413 74 payload_init_raw(new, expr->payload.base, payload_offset,
1414 expr->len);
1415
1416
2/2
✓ Branch 0 (35→36) taken 20 times.
✓ Branch 1 (35→37) taken 54 times.
74 if (expr->payload.inner_desc)
1417 20 new->dtype = &integer_type;
1418
1419 74 list_add_tail(&new->list, list);
1420 }
1421
1422 931 static bool payload_is_adjacent(const struct expr *e1, const struct expr *e2)
1423 {
1424
2/2
✓ Branch 0 (2→3) taken 532 times.
✓ Branch 1 (2→5) taken 399 times.
931 if (e1->payload.base == e2->payload.base &&
1425
2/2
✓ Branch 0 (3→4) taken 378 times.
✓ Branch 1 (3→5) taken 154 times.
532 e1->payload.offset + e1->len == e2->payload.offset)
1426 378 return true;
1427 553 return false;
1428 }
1429
1430 /**
1431 * payload_can_merge - return whether two payload expressions can be merged
1432 *
1433 * @e1: first payload expression
1434 * @e2: second payload expression
1435 */
1436 928 bool payload_can_merge(const struct expr *e1, const struct expr *e2)
1437 {
1438 unsigned int total;
1439
1440
2/2
✓ Branch 0 (2→3) taken 141 times.
✓ Branch 1 (2→4) taken 787 times.
928 if (e1->payload.inner_desc != e2->payload.inner_desc)
1441 141 return false;
1442
1443
2/2
✓ Branch 0 (5→6) taken 553 times.
✓ Branch 1 (5→7) taken 234 times.
787 if (!payload_is_adjacent(e1, e2))
1444 553 return false;
1445
1446
2/4
✓ Branch 0 (7→8) taken 234 times.
✗ Branch 1 (7→11) not taken.
✓ Branch 2 (8→9) taken 234 times.
✗ Branch 3 (8→11) not taken.
234 if (e1->payload.offset % BITS_PER_BYTE || e1->len % BITS_PER_BYTE ||
1447
2/4
✓ Branch 0 (9→10) taken 234 times.
✗ Branch 1 (9→11) not taken.
✗ Branch 2 (10→11) not taken.
✓ Branch 3 (10→12) taken 234 times.
234 e2->payload.offset % BITS_PER_BYTE || e2->len % BITS_PER_BYTE)
1448 ✗ return false;
1449
1450 234 total = e1->len + e2->len;
1451
3/4
✓ Branch 0 (12→13) taken 234 times.
✗ Branch 1 (12→14) not taken.
✓ Branch 2 (13→14) taken 8 times.
✓ Branch 3 (13→15) taken 226 times.
234 if (total < e1->len || total > (NFT_REG_SIZE * BITS_PER_BYTE))
1452 8 return false;
1453
1454 /* could return true after this, the expressions are mergeable.
1455 *
1456 * However, there are some caveats.
1457 *
1458 * Loading anything <= sizeof(u32) with base >= network header
1459 * is fast, because its handled directly from eval loop in the
1460 * kernel.
1461 *
1462 * We thus restrict merging a bit more.
1463 */
1464
1465 /* can still be handled by fastpath after merge */
1466
2/2
✓ Branch 0 (15→16) taken 96 times.
✓ Branch 1 (15→17) taken 130 times.
226 if (total <= NFT_REG32_SIZE * BITS_PER_BYTE)
1467 96 return true;
1468
1469 /* Linklayer base is not handled in fastpath, merge */
1470
2/2
✓ Branch 0 (17→18) taken 33 times.
✓ Branch 1 (17→19) taken 97 times.
130 if (e1->payload.base == PROTO_BASE_LL_HDR)
1471 33 return true;
1472
1473 /* Also merge if at least one expression is already
1474 * above REG32 size, in this case merging is faster.
1475 */
1476
1/2
✓ Branch 0 (19→20) taken 97 times.
✗ Branch 1 (19→21) not taken.
97 if (e1->len > (NFT_REG32_SIZE * BITS_PER_BYTE) ||
1477
2/2
✓ Branch 0 (20→21) taken 15 times.
✓ Branch 1 (20→22) taken 82 times.
97 e2->len > (NFT_REG32_SIZE * BITS_PER_BYTE))
1478 15 return true;
1479
1480 82 return false;
1481 }
1482
1483 /**
1484 * payload_expr_join - join two adjacent payload expressions
1485 *
1486 * @e1: first payload expression
1487 * @e2: second payload expression
1488 */
1489 144 struct expr *payload_expr_join(const struct expr *e1, const struct expr *e2)
1490 {
1491 struct expr *expr;
1492
1493
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 144 times.
144 assert(payload_is_adjacent(e1, e2));
1494
1495 144 expr = payload_expr_alloc(&internal_location, NULL, 0);
1496 144 expr->payload.base = e1->payload.base;
1497 144 expr->payload.offset = e1->payload.offset;
1498 144 expr->len = e1->len + e2->len;
1499 144 expr->payload.inner_desc = e1->payload.inner_desc;
1500
1501 144 return expr;
1502 }
1503
1504 static struct stmt *
1505 − __payload_gen_icmp_simple_dependency(struct eval_ctx *ctx, const struct expr *expr,
1506 const struct datatype *icmp_type,
1507 const struct proto_desc *desc,
1508 uint8_t type)
1509 {
1510 struct expr *left, *right, *dep;
1511
1512 − left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
1513 − right = constant_expr_alloc(&expr->location, icmp_type,
1514 BYTEORDER_BIG_ENDIAN, BITS_PER_BYTE,
1515 constant_data_ptr(type, BITS_PER_BYTE));
1516
1517 − dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
1518 − return expr_stmt_alloc(&dep->location, dep);
1519 }
1520
1521 static struct stmt *
1522 − __payload_gen_icmp_echo_dependency(struct eval_ctx *ctx, const struct expr *expr,
1523 uint8_t echo, uint8_t reply,
1524 const struct datatype *icmp_type,
1525 const struct proto_desc *desc)
1526 {
1527 struct expr *left, *right, *dep, *set;
1528
1529 − left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
1530
1531 − set = set_expr_alloc(&expr->location, NULL);
1532
1533 − right = constant_expr_alloc(&expr->location, icmp_type,
1534 BYTEORDER_BIG_ENDIAN, BITS_PER_BYTE,
1535 constant_data_ptr(echo, BITS_PER_BYTE));
1536 − right = set_elem_expr_alloc(&expr->location, right);
1537 − set_expr_add(set, right);
1538
1539 − right = constant_expr_alloc(&expr->location, icmp_type,
1540 BYTEORDER_BIG_ENDIAN, BITS_PER_BYTE,
1541 constant_data_ptr(reply, BITS_PER_BYTE));
1542 − right = set_elem_expr_alloc(&expr->location, right);
1543 − set_expr_add(set, right);
1544
1545 − dep = relational_expr_alloc(&expr->location, OP_IMPLICIT, left, set);
1546 − return expr_stmt_alloc(&dep->location, dep);
1547 }
1548
1549 static struct stmt *
1550 − __payload_gen_icmp6_addr_dependency(struct eval_ctx *ctx, const struct expr *expr,
1551 const struct proto_desc *desc)
1552 {
1553 static const uint8_t icmp_addr_types[] = {
1554 MLD_LISTENER_QUERY,
1555 MLD_LISTENER_REPORT,
1556 MLD_LISTENER_REDUCTION,
1557 ND_NEIGHBOR_SOLICIT,
1558 ND_NEIGHBOR_ADVERT,
1559 ND_REDIRECT
1560 };
1561 struct expr *left, *right, *dep, *set;
1562 size_t i;
1563
1564 − left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
1565
1566 − set = set_expr_alloc(&expr->location, NULL);
1567
1568 − for (i = 0; i < array_size(icmp_addr_types); ++i) {
1569 − right = constant_expr_alloc(&expr->location, &icmp6_type_type,
1570 BYTEORDER_BIG_ENDIAN, BITS_PER_BYTE,
1571 − constant_data_ptr(icmp_addr_types[i],
1572 BITS_PER_BYTE));
1573 − right = set_elem_expr_alloc(&expr->location, right);
1574 − set_expr_add(set, right);
1575 }
1576
1577 − dep = relational_expr_alloc(&expr->location, OP_IMPLICIT, left, set);
1578 − return expr_stmt_alloc(&dep->location, dep);
1579 }
1580
1581 164 int payload_gen_icmp_dependency(struct eval_ctx *ctx, const struct expr *expr,
1582 struct stmt **res)
1583 {
1584 164 struct proto_ctx *pctx = eval_proto_ctx(ctx);
1585 const struct proto_hdr_template *tmpl;
1586 const struct proto_desc *desc;
1587 164 struct stmt *stmt = NULL;
1588 uint8_t type;
1589
1590
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 164 times.
164 assert(expr->etype == EXPR_PAYLOAD);
1591
1592 164 tmpl = expr->payload.tmpl;
1593 164 desc = expr->payload.desc;
1594
1595
5/7
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→8) taken 55 times.
✓ Branch 2 (5→13) taken 33 times.
✓ Branch 3 (5→20) taken 35 times.
✓ Branch 4 (5→27) taken 13 times.
✓ Branch 5 (5→34) taken 28 times.
✗ Branch 6 (5→41) not taken.
164 switch (tmpl->icmp_dep) {
1596 ✗ case PROTO_ICMP_ANY:
1597 ✗ BUG("No dependency needed");
1598 break;
1599 55 case PROTO_ICMP_ECHO:
1600 /* do not test ICMP_ECHOREPLY here: its 0 */
1601
2/2
✓ Branch 0 (8→9) taken 37 times.
✓ Branch 1 (8→10) taken 18 times.
55 if (pctx->th_dep.icmp.type == ICMP_ECHO)
1602 37 goto done;
1603
1604 18 type = ICMP_ECHO;
1605
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 18 times.
18 if (pctx->th_dep.icmp.type)
1606 ✗ goto bad_proto;
1607
1608 18 stmt = __payload_gen_icmp_echo_dependency(ctx, expr,
1609 ICMP_ECHO, ICMP_ECHOREPLY,
1610 &icmp_type_type,
1611 desc);
1612 18 break;
1613 33 case PROTO_ICMP_MTU:
1614 case PROTO_ICMP_ADDRESS:
1615 33 type = icmp_dep_to_type(tmpl->icmp_dep);
1616
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 33 times.
33 if (pctx->th_dep.icmp.type == type)
1617 ✗ goto done;
1618
2/2
✓ Branch 0 (16→17) taken 1 times.
✓ Branch 1 (16→18) taken 32 times.
33 if (pctx->th_dep.icmp.type)
1619 1 goto bad_proto;
1620 32 stmt = __payload_gen_icmp_simple_dependency(ctx, expr,
1621 &icmp_type_type,
1622 desc, type);
1623 32 break;
1624 35 case PROTO_ICMP6_ECHO:
1625
2/2
✓ Branch 0 (20→21) taken 21 times.
✓ Branch 1 (20→22) taken 14 times.
35 if (pctx->th_dep.icmp.type == ICMP6_ECHO_REQUEST ||
1626
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 21 times.
21 pctx->th_dep.icmp.type == ICMP6_ECHO_REPLY)
1627 14 goto done;
1628
1629 21 type = ICMP6_ECHO_REQUEST;
1630
1/2
✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 21 times.
21 if (pctx->th_dep.icmp.type)
1631 ✗ goto bad_proto;
1632
1633 21 stmt = __payload_gen_icmp_echo_dependency(ctx, expr,
1634 ICMP6_ECHO_REQUEST,
1635 ICMP6_ECHO_REPLY,
1636 &icmp6_type_type,
1637 desc);
1638 21 break;
1639 13 case PROTO_ICMP6_ADDRESS:
1640
2/2
✓ Branch 0 (28→29) taken 11 times.
✓ Branch 1 (28→30) taken 2 times.
13 if (icmp_dep_type_match(PROTO_ICMP6_ADDRESS,
1641 13 pctx->th_dep.icmp.type))
1642 11 goto done;
1643 2 type = ND_NEIGHBOR_SOLICIT;
1644
1/2
✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 2 times.
2 if (pctx->th_dep.icmp.type)
1645 ✗ goto bad_proto;
1646 2 stmt = __payload_gen_icmp6_addr_dependency(ctx, expr, desc);
1647 2 break;
1648 28 case PROTO_ICMP6_REDIRECT:
1649 case PROTO_ICMP6_MTU:
1650 case PROTO_ICMP6_MGMQ:
1651 case PROTO_ICMP6_PPTR:
1652 28 type = icmp_dep_to_type(tmpl->icmp_dep);
1653
2/2
✓ Branch 0 (35→36) taken 2 times.
✓ Branch 1 (35→37) taken 26 times.
28 if (pctx->th_dep.icmp.type == type)
1654 2 goto done;
1655
1/2
✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→39) taken 26 times.
26 if (pctx->th_dep.icmp.type)
1656 ✗ goto bad_proto;
1657 26 stmt = __payload_gen_icmp_simple_dependency(ctx, expr,
1658 &icmp6_type_type,
1659 desc, type);
1660 26 break;
1661 break;
1662 ✗ default:
1663 ✗ BUG("Unhandled icmp dependency code");
1664 }
1665
1666 99 pctx->th_dep.icmp.type = type;
1667
1668
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 99 times.
99 if (stmt_dependency_evaluate(ctx, stmt) < 0)
1669 ✗ return -1;
1670 99 done:
1671 163 *res = stmt;
1672 163 return 0;
1673
1674 1 bad_proto:
1675 1 return expr_error(ctx->msgs, expr, "incompatible icmp match: rule has %d, need %u",
1676 pctx->th_dep.icmp.type, type);
1677 }
1678
1679 160 int payload_gen_inner_dependency(struct eval_ctx *ctx, const struct expr *expr,
1680 struct stmt **res)
1681 {
1682 160 struct proto_ctx *pctx = eval_proto_ctx(ctx);
1683 const struct proto_hdr_template *tmpl;
1684 const struct proto_desc *desc, *inner_desc;
1685 struct expr *left, *right, *dep;
1686 160 struct stmt *stmt = NULL;
1687 int protocol;
1688
1689
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 160 times.
160 assert(expr->etype == EXPR_PAYLOAD);
1690
1691 160 inner_desc = expr->payload.inner_desc;
1692 160 desc = pctx->protocol[inner_desc->base - 1].desc;
1693
2/2
✓ Branch 0 (5→6) taken 96 times.
✓ Branch 1 (5→7) taken 64 times.
160 if (desc == NULL)
1694 96 desc = &proto_ip;
1695
1696 160 tmpl = &inner_desc->templates[0];
1697
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 160 times.
160 assert(tmpl);
1698
1699 160 protocol = proto_find_num(desc, inner_desc);
1700
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 160 times.
160 if (protocol < 0)
1701 ✗ return expr_error(ctx->msgs, expr,
1702 "conflicting protocols specified: %s vs. %s",
1703 desc->name, inner_desc->name);
1704
1705 160 left = meta_expr_alloc(&expr->location, tmpl->meta_key);
1706
1707 160 right = constant_expr_alloc(&expr->location, tmpl->dtype,
1708 160 tmpl->dtype->byteorder, tmpl->len,
1709 constant_data_ptr(protocol, tmpl->len));
1710
1711 160 dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
1712 160 stmt = expr_stmt_alloc(&dep->location, dep);
1713
1714 160 *res = stmt;
1715 160 return 0;
1716 }
1717