GCC Code Coverage Report


Directory: ./
File: src/ipopt.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 33 44 75.0%
Functions: 2 3 66.7%
Branches: 11 24 45.8%

Line Branch Exec Source
1 #include <nft.h>
2
3
4 #include <netinet/in.h>
5 #include <netinet/ip.h>
6 #include <netinet/ip6.h>
7 #include <netinet/tcp.h>
8
9 #include <utils.h>
10 #include <headers.h>
11 #include <expression.h>
12 #include <ipopt.h>
13
14 static const struct proto_hdr_template ipopt_unknown_template =
15 PROTO_HDR_TEMPLATE("unknown", &invalid_type, BYTEORDER_INVALID, 0, 0);
16
17 #define PHT(__token, __offset, __len) \
18 PROTO_HDR_TEMPLATE(__token, &integer_type, BYTEORDER_BIG_ENDIAN, \
19 __offset, __len)
20 static const struct exthdr_desc ipopt_lsrr = {
21 .name = "lsrr",
22 .type = IPOPT_LSRR,
23 .templates = {
24 [IPOPT_FIELD_TYPE] = PHT("type", 0, 8),
25 [IPOPT_FIELD_LENGTH] = PHT("length", 8, 8),
26 [IPOPT_FIELD_PTR] = PHT("ptr", 16, 8),
27 [IPOPT_FIELD_ADDR_0] = PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
28 },
29 };
30
31 static const struct exthdr_desc ipopt_rr = {
32 .name = "rr",
33 .type = IPOPT_RR,
34 .templates = {
35 [IPOPT_FIELD_TYPE] = PHT("type", 0, 8),
36 [IPOPT_FIELD_LENGTH] = PHT("length", 8, 8),
37 [IPOPT_FIELD_PTR] = PHT("ptr", 16, 8),
38 [IPOPT_FIELD_ADDR_0] = PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
39 },
40 };
41
42 static const struct exthdr_desc ipopt_ssrr = {
43 .name = "ssrr",
44 .type = IPOPT_SSRR,
45 .templates = {
46 [IPOPT_FIELD_TYPE] = PHT("type", 0, 8),
47 [IPOPT_FIELD_LENGTH] = PHT("length", 8, 8),
48 [IPOPT_FIELD_PTR] = PHT("ptr", 16, 8),
49 [IPOPT_FIELD_ADDR_0] = PROTO_HDR_TEMPLATE("addr", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24, 32),
50 },
51 };
52
53 static const struct exthdr_desc ipopt_ra = {
54 .name = "ra",
55 .type = IPOPT_RA,
56 .templates = {
57 [IPOPT_FIELD_TYPE] = PHT("type", 0, 8),
58 [IPOPT_FIELD_LENGTH] = PHT("length", 8, 8),
59 [IPOPT_FIELD_VALUE] = PHT("value", 16, 16),
60 },
61 };
62
63 const struct exthdr_desc *ipopt_protocols[UINT8_MAX] = {
64 [IPOPT_LSRR] = &ipopt_lsrr,
65 [IPOPT_RR] = &ipopt_rr,
66 [IPOPT_SSRR] = &ipopt_ssrr,
67 [IPOPT_RA] = &ipopt_ra,
68 };
69
70 14 struct expr *ipopt_expr_alloc(const struct location *loc, uint8_t type,
71 uint8_t field)
72 {
73 const struct proto_hdr_template *tmpl;
74 const struct exthdr_desc *desc;
75 struct expr *expr;
76
77 14 desc = ipopt_protocols[type];
78 14 tmpl = &desc->templates[field];
79
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 14 times.
14 if (!tmpl)
80 ✗ return NULL;
81
82
2/2
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 13 times.
14 if (!tmpl->len)
83 1 return NULL;
84
85 13 expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
86 13 BYTEORDER_BIG_ENDIAN, tmpl->len);
87 13 expr->exthdr.desc = desc;
88 13 expr->exthdr.tmpl = tmpl;
89 13 expr->exthdr.op = NFT_EXTHDR_OP_IPV4;
90 13 expr->exthdr.offset = tmpl->offset;
91 13 expr->exthdr.raw_type = desc->type;
92
93 13 return expr;
94 }
95
96 3 void ipopt_init_raw(struct expr *expr, uint8_t type, unsigned int offset,
97 unsigned int len, uint32_t flags, bool set_unknown)
98 {
99 const struct proto_hdr_template *tmpl;
100 unsigned int i;
101
102
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 3 times.
3 assert(expr->etype == EXPR_EXTHDR);
103
104 3 expr->len = len;
105 3 expr->exthdr.flags = flags;
106 3 expr->exthdr.offset = offset;
107
108
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 3 times.
3 assert(type < array_size(ipopt_protocols));
109 3 expr->exthdr.desc = ipopt_protocols[type];
110 3 expr->exthdr.flags = flags;
111
112
1/2
✓ Branch 0 (15→7) taken 12 times.
✗ Branch 1 (15→16) not taken.
12 for (i = 0; i < array_size(expr->exthdr.desc->templates); ++i) {
113 12 tmpl = &expr->exthdr.desc->templates[i];
114
115 /* Make sure that it's the right template based on offset and len */
116
3/4
✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→9) taken 9 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 3 times.
12 if (tmpl->offset != offset || tmpl->len != len)
117 9 continue;
118
119
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 3 times.
3 if (flags & NFT_EXTHDR_F_PRESENT)
120 ✗ expr->dtype = &boolean_type;
121 else
122 3 expr->dtype = tmpl->dtype;
123 3 expr->exthdr.tmpl = tmpl;
124 3 expr->exthdr.op = NFT_EXTHDR_OP_IPV4;
125 3 break;
126 }
127
1/4
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 3 times.
✗ Branch 2 (17→18) not taken.
✗ Branch 3 (17→19) not taken.
3 if (i == array_size(expr->exthdr.desc->templates) && set_unknown) {
128 ✗ expr->exthdr.tmpl = &ipopt_unknown_template;
129 ✗ expr->exthdr.op = NFT_EXTHDR_OP_IPV4;
130 }
131 3 }
132
133 ✗ bool ipopt_find_template(struct expr *expr, unsigned int offset,
134 unsigned int len)
135 {
136 ✗ if (expr->exthdr.tmpl != &ipopt_unknown_template)
137 ✗ return false;
138
139 ✗ ipopt_init_raw(expr, expr->exthdr.desc->type, offset, len, 0, false);
140
141 ✗ if (expr->exthdr.tmpl == &ipopt_unknown_template)
142 ✗ return false;
143
144 ✗ return true;
145 }
146