GCC Code Coverage Report


Directory: ./
File: src/sctp_chunk.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 41 45 91.1%
Functions: 3 3 100.0%
Branches: 19 28 67.9%

Line Branch Exec Source
1 /*
2 * Copyright Red Hat
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 (or any
6 * later) as published by the Free Software Foundation.
7 */
8
9 #include <nft.h>
10
11 #include <exthdr.h>
12 #include <sctp_chunk.h>
13
14
15 #define PHT(__token, __offset, __len) \
16 PROTO_HDR_TEMPLATE(__token, &integer_type, BYTEORDER_BIG_ENDIAN, \
17 __offset, __len)
18
19 static const struct exthdr_desc sctp_chunk_data = {
20 .name = "data",
21 .type = SCTP_CHUNK_TYPE_DATA,
22 .templates = {
23 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
24 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
25 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
26 [SCTP_CHUNK_DATA_TSN] = PHT("tsn", 32, 32),
27 [SCTP_CHUNK_DATA_STREAM] = PHT("stream", 64, 16),
28 [SCTP_CHUNK_DATA_SSN] = PHT("ssn", 80, 16),
29 [SCTP_CHUNK_DATA_PPID] = PHT("ppid", 96, 32),
30 },
31 };
32
33 static const struct exthdr_desc sctp_chunk_init = {
34 .name = "init",
35 .type = SCTP_CHUNK_TYPE_INIT,
36 .templates = {
37 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
38 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
39 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
40 [SCTP_CHUNK_INIT_TAG] = PHT("init-tag", 32, 32),
41 [SCTP_CHUNK_INIT_RWND] = PHT("a-rwnd", 64, 32),
42 [SCTP_CHUNK_INIT_OSTREAMS] = PHT("num-outbound-streams", 96, 16),
43 [SCTP_CHUNK_INIT_ISTREAMS] = PHT("num-inbound-streams", 112, 16),
44 [SCTP_CHUNK_INIT_TSN] = PHT("initial-tsn", 128, 32),
45 },
46 };
47
48 static const struct exthdr_desc sctp_chunk_init_ack = {
49 .name = "init-ack",
50 .type = SCTP_CHUNK_TYPE_INIT_ACK,
51 .templates = {
52 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
53 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
54 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
55 [SCTP_CHUNK_INIT_TAG] = PHT("init-tag", 32, 32),
56 [SCTP_CHUNK_INIT_RWND] = PHT("a-rwnd", 64, 32),
57 [SCTP_CHUNK_INIT_OSTREAMS] = PHT("num-outbound-streams", 96, 16),
58 [SCTP_CHUNK_INIT_ISTREAMS] = PHT("num-inbound-streams", 112, 16),
59 [SCTP_CHUNK_INIT_TSN] = PHT("initial-tsn", 128, 32),
60 },
61 };
62
63 static const struct exthdr_desc sctp_chunk_sack = {
64 .name = "sack",
65 .type = SCTP_CHUNK_TYPE_SACK,
66 .templates = {
67 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
68 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
69 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
70 [SCTP_CHUNK_SACK_CTSN_ACK] = PHT("cum-tsn-ack", 32, 32),
71 [SCTP_CHUNK_SACK_RWND] = PHT("a-rwnd", 64, 32),
72 [SCTP_CHUNK_SACK_GACK_BLOCKS] = PHT("num-gap-ack-blocks", 96, 16),
73 [SCTP_CHUNK_SACK_DUP_TSNS] = PHT("num-dup-tsns", 112, 16),
74 },
75 };
76
77 static const struct exthdr_desc sctp_chunk_shutdown = {
78 .name = "shutdown",
79 .type = SCTP_CHUNK_TYPE_SHUTDOWN,
80 .templates = {
81 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
82 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
83 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
84 [SCTP_CHUNK_SHUTDOWN_CTSN_ACK] = PHT("cum-tsn-ack", 32, 32),
85 },
86 };
87
88 static const struct exthdr_desc sctp_chunk_ecne = {
89 .name = "ecne",
90 .type = SCTP_CHUNK_TYPE_ECNE,
91 .templates = {
92 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
93 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
94 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
95 [SCTP_CHUNK_ECNE_CWR_MIN_TSN] = PHT("lowest-tsn", 32, 32),
96 },
97 };
98
99 static const struct exthdr_desc sctp_chunk_cwr = {
100 .name = "cwr",
101 .type = SCTP_CHUNK_TYPE_CWR,
102 .templates = {
103 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
104 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
105 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
106 [SCTP_CHUNK_ECNE_CWR_MIN_TSN] = PHT("lowest-tsn", 32, 32),
107 },
108 };
109
110 static const struct exthdr_desc sctp_chunk_asconf_ack = {
111 .name = "asconf-ack",
112 .type = SCTP_CHUNK_TYPE_ASCONF_ACK,
113 .templates = {
114 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
115 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
116 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
117 [SCTP_CHUNK_ASCONF_SEQNO] = PHT("seqno", 32, 32),
118 },
119 };
120
121 static const struct exthdr_desc sctp_chunk_forward_tsn = {
122 .name = "forward-tsn",
123 .type = SCTP_CHUNK_TYPE_FORWARD_TSN,
124 .templates = {
125 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
126 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
127 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
128 [SCTP_CHUNK_FORWARD_TSN_NCTSN] = PHT("new-cum-tsn", 32, 32),
129 },
130 };
131
132 static const struct exthdr_desc sctp_chunk_asconf = {
133 .name = "asconf",
134 .type = SCTP_CHUNK_TYPE_ASCONF,
135 .templates = {
136 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8),
137 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8),
138 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),
139 [SCTP_CHUNK_ASCONF_SEQNO] = PHT("seqno", 32, 32),
140 },
141 };
142
143 #define SCTP_CHUNK_DESC_GENERATOR(descname, hname, desctype) \
144 static const struct exthdr_desc sctp_chunk_##descname = { \
145 .name = #hname, \
146 .type = SCTP_CHUNK_TYPE_##desctype, \
147 .templates = { \
148 [SCTP_CHUNK_COMMON_TYPE] = PHT("type", 0, 8), \
149 [SCTP_CHUNK_COMMON_FLAGS] = PHT("flags", 8, 8), \
150 [SCTP_CHUNK_COMMON_LENGTH] = PHT("length", 16, 16),\
151 }, \
152 };
153
154 SCTP_CHUNK_DESC_GENERATOR(heartbeat, heartbeat, HEARTBEAT)
155 SCTP_CHUNK_DESC_GENERATOR(heartbeat_ack, heartbeat-ack, HEARTBEAT_ACK)
156 SCTP_CHUNK_DESC_GENERATOR(abort, abort, ABORT)
157 SCTP_CHUNK_DESC_GENERATOR(shutdown_ack, shutdown-ack, SHUTDOWN_ACK)
158 SCTP_CHUNK_DESC_GENERATOR(error, error, ERROR)
159 SCTP_CHUNK_DESC_GENERATOR(cookie_echo, cookie-echo, COOKIE_ECHO)
160 SCTP_CHUNK_DESC_GENERATOR(cookie_ack, cookie-ack, COOKIE_ACK)
161 SCTP_CHUNK_DESC_GENERATOR(shutdown_complete, shutdown-complete, SHUTDOWN_COMPLETE)
162
163 #undef SCTP_CHUNK_DESC_GENERATOR
164
165 static const struct exthdr_desc *sctp_chunk_protocols[] = {
166 [SCTP_CHUNK_TYPE_DATA] = &sctp_chunk_data,
167 [SCTP_CHUNK_TYPE_INIT] = &sctp_chunk_init,
168 [SCTP_CHUNK_TYPE_INIT_ACK] = &sctp_chunk_init_ack,
169 [SCTP_CHUNK_TYPE_SACK] = &sctp_chunk_sack,
170 [SCTP_CHUNK_TYPE_HEARTBEAT] = &sctp_chunk_heartbeat,
171 [SCTP_CHUNK_TYPE_HEARTBEAT_ACK] = &sctp_chunk_heartbeat_ack,
172 [SCTP_CHUNK_TYPE_ABORT] = &sctp_chunk_abort,
173 [SCTP_CHUNK_TYPE_SHUTDOWN] = &sctp_chunk_shutdown,
174 [SCTP_CHUNK_TYPE_SHUTDOWN_ACK] = &sctp_chunk_shutdown_ack,
175 [SCTP_CHUNK_TYPE_ERROR] = &sctp_chunk_error,
176 [SCTP_CHUNK_TYPE_COOKIE_ECHO] = &sctp_chunk_cookie_echo,
177 [SCTP_CHUNK_TYPE_COOKIE_ACK] = &sctp_chunk_cookie_ack,
178 [SCTP_CHUNK_TYPE_ECNE] = &sctp_chunk_ecne,
179 [SCTP_CHUNK_TYPE_CWR] = &sctp_chunk_cwr,
180 [SCTP_CHUNK_TYPE_SHUTDOWN_COMPLETE] = &sctp_chunk_shutdown_complete,
181 [SCTP_CHUNK_TYPE_ASCONF_ACK] = &sctp_chunk_asconf_ack,
182 [SCTP_CHUNK_TYPE_FORWARD_TSN] = &sctp_chunk_forward_tsn,
183 [SCTP_CHUNK_TYPE_ASCONF] = &sctp_chunk_asconf,
184 };
185
186 159 const struct exthdr_desc *sctp_chunk_protocol_find(const char *name)
187 {
188 unsigned int i;
189
190
1/2
✓ Branch 0 (7→3) taken 6023 times.
✗ Branch 1 (7→8) not taken.
6023 for (i = 0; i < array_size(sctp_chunk_protocols); i++) {
191
2/2
✓ Branch 0 (3→4) taken 1373 times.
✓ Branch 1 (3→6) taken 4650 times.
6023 if (sctp_chunk_protocols[i] &&
192
2/2
✓ Branch 0 (4→5) taken 159 times.
✓ Branch 1 (4→6) taken 1214 times.
1373 !strcmp(sctp_chunk_protocols[i]->name, name))
193 159 return sctp_chunk_protocols[i];
194 }
195 ✗ return NULL;
196 }
197
198 322 struct expr *sctp_chunk_expr_alloc(const struct location *loc,
199 unsigned int type, unsigned int field)
200 {
201 const struct proto_hdr_template *tmpl;
202 322 const struct exthdr_desc *desc = NULL;
203 struct expr *expr;
204
205
1/2
✓ Branch 0 (2→3) taken 322 times.
✗ Branch 1 (2→4) not taken.
322 if (type < array_size(sctp_chunk_protocols))
206 322 desc = sctp_chunk_protocols[type];
207
208
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 322 times.
322 if (!desc)
209 ✗ return NULL;
210
211 322 tmpl = &desc->templates[field];
212
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 322 times.
322 if (!tmpl)
213 ✗ return NULL;
214
215 322 expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
216 322 BYTEORDER_BIG_ENDIAN, tmpl->len);
217 322 expr->exthdr.desc = desc;
218 322 expr->exthdr.tmpl = tmpl;
219 322 expr->exthdr.op = NFT_EXTHDR_OP_SCTP;
220 322 expr->exthdr.raw_type = desc->type;
221 322 expr->exthdr.offset = tmpl->offset;
222
223 322 return expr;
224 }
225
226 317 void sctp_chunk_init_raw(struct expr *expr, uint8_t type, unsigned int off,
227 unsigned int len, uint32_t flags)
228 {
229 const struct proto_hdr_template *tmpl;
230 unsigned int i;
231
232
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 317 times.
317 assert(expr->etype == EXPR_EXTHDR);
233
234 317 expr->len = len;
235 317 expr->exthdr.flags = flags;
236 317 expr->exthdr.offset = off;
237 317 expr->exthdr.op = NFT_EXTHDR_OP_SCTP;
238
239
2/2
✓ Branch 0 (4→5) taken 180 times.
✓ Branch 1 (4→6) taken 137 times.
317 if (flags & NFT_EXTHDR_F_PRESENT)
240 180 datatype_set(expr, &boolean_type);
241 else
242 137 datatype_set(expr, &integer_type);
243
244
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 317 times.
317 if (type >= array_size(sctp_chunk_protocols))
245 ✗ return;
246
247 317 expr->exthdr.desc = sctp_chunk_protocols[type];
248 317 expr->exthdr.flags = flags;
249
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 317 times.
317 assert(expr->exthdr.desc != NULL);
250
251
1/2
✓ Branch 0 (19→12) taken 757 times.
✗ Branch 1 (19→20) not taken.
757 for (i = 0; i < array_size(expr->exthdr.desc->templates); ++i) {
252 757 tmpl = &expr->exthdr.desc->templates[i];
253
3/4
✓ Branch 0 (12→13) taken 317 times.
✓ Branch 1 (12→14) taken 440 times.
✗ Branch 2 (13→14) not taken.
✓ Branch 3 (13→15) taken 317 times.
757 if (tmpl->offset != off || tmpl->len != len)
254 440 continue;
255
256
2/2
✓ Branch 0 (15→16) taken 137 times.
✓ Branch 1 (15→17) taken 180 times.
317 if ((flags & NFT_EXTHDR_F_PRESENT) == 0)
257 137 datatype_set(expr, tmpl->dtype);
258
259 317 expr->exthdr.tmpl = tmpl;
260 317 break;
261 }
262 }
263