GCC Code Coverage Report


Directory: ./
File: src/nfnl_osf.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 183 228 80.3%
Functions: 4 4 100.0%
Branches: 72 115 62.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2005 Evgeniy Polyakov <johnpol@2ka.mxt.ru>
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 as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 *
19 * Based on iptables/utils/nfnl_osf.c.
20 */
21
22 #include <nft.h>
23
24 #include <sys/time.h>
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <time.h>
29
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
32
33 #include <linux/unistd.h>
34
35 #include <libmnl/libmnl.h>
36
37 #include <linux/netfilter/nfnetlink.h>
38 #include <linux/netfilter/nfnetlink_osf.h>
39 #include <mnl.h>
40 #include <osf.h>
41
42 #define OPTDEL ','
43 #define OSFPDEL ':'
44 #define MAXOPTSTRLEN 128
45
46 static struct nf_osf_opt IANA_opts[] = {
47 { .kind = 0, .length = 1,},
48 { .kind=1, .length=1,},
49 { .kind=2, .length=4,},
50 { .kind=3, .length=3,},
51 { .kind=4, .length=2,},
52 { .kind=5, .length=1,}, /* SACK length is not defined */
53 { .kind=6, .length=6,},
54 { .kind=7, .length=6,},
55 { .kind=8, .length=10,},
56 { .kind=9, .length=2,},
57 { .kind=10, .length=3,},
58 { .kind=11, .length=1,}, /* CC: Suppose 1 */
59 { .kind=12, .length=1,}, /* the same */
60 { .kind=13, .length=1,}, /* and here too */
61 { .kind=14, .length=3,},
62 { .kind=15, .length=1,}, /* TCP Alternate Checksum Data. Length is not defined */
63 { .kind=16, .length=1,},
64 { .kind=17, .length=1,},
65 { .kind=18, .length=3,},
66 { .kind=19, .length=18,},
67 { .kind=20, .length=1,},
68 { .kind=21, .length=1,},
69 { .kind=22, .length=1,},
70 { .kind=23, .length=1,},
71 { .kind=24, .length=1,},
72 { .kind=25, .length=1,},
73 { .kind=26, .length=1,},
74 };
75
76 188055 static char *nf_osf_strchr(char *ptr, char c)
77 {
78 char *tmp;
79
80 188055 tmp = strchr(ptr, c);
81
1/2
✓ Branch 0 (2→3) taken 188055 times.
✗ Branch 1 (2→4) not taken.
188055 if (tmp)
82 188055 *tmp = '\0';
83
84
3/4
✓ Branch 0 (6→7) taken 221067 times.
✗ Branch 1 (6→8) not taken.
✓ Branch 2 (7→5) taken 33012 times.
✓ Branch 3 (7→8) taken 188055 times.
221067 while (tmp && isspace(*(tmp + 1)))
85 33012 tmp++;
86
87 188055 return tmp;
88 }
89
90 15372 static void nf_osf_parse_opt(struct nf_osf_opt *opt, __u16 *optnum, char *obuf,
91 int olen)
92 {
93 int i, op;
94 char *ptr, wc;
95 unsigned long val;
96
97 15372 ptr = &obuf[0];
98 15372 i = 0;
99
4/6
✓ Branch 0 (55→56) taken 80451 times.
✗ Branch 1 (55→58) not taken.
✓ Branch 2 (56→57) taken 80451 times.
✗ Branch 3 (56→58) not taken.
✓ Branch 4 (57→3) taken 65079 times.
✓ Branch 5 (57→58) taken 15372 times.
80451 while (ptr != NULL && i < olen && *ptr != 0) {
100 65079 val = 0;
101 65079 wc = OSF_WSS_PLAIN;
102
6/7
✓ Branch 0 (3→4) taken 28035 times.
✓ Branch 1 (3→9) taken 8001 times.
✓ Branch 2 (3→14) taken 5985 times.
✓ Branch 3 (3→19) taken 7686 times.
✓ Branch 4 (3→32) taken 14805 times.
✗ Branch 5 (3→42) not taken.
✓ Branch 6 (3→47) taken 567 times.
65079 switch (obuf[i]) {
103 28035 case 'N':
104 28035 op = OSFOPT_NOP;
105 28035 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
106
1/2
✓ Branch 0 (5→6) taken 28035 times.
✗ Branch 1 (5→7) not taken.
28035 if (ptr) {
107 28035 *ptr = '\0';
108 28035 ptr++;
109 28035 i += (int)(ptr - &obuf[i]);
110 } else
111 ✗ i++;
112 28035 break;
113 8001 case 'S':
114 8001 op = OSFOPT_SACKP;
115 8001 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
116
1/2
✓ Branch 0 (10→11) taken 8001 times.
✗ Branch 1 (10→12) not taken.
8001 if (ptr) {
117 8001 *ptr = '\0';
118 8001 ptr++;
119 8001 i += (int)(ptr - &obuf[i]);
120 } else
121 ✗ i++;
122 8001 break;
123 5985 case 'T':
124 5985 op = OSFOPT_TS;
125 5985 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
126
1/2
✓ Branch 0 (15→16) taken 5985 times.
✗ Branch 1 (15→17) not taken.
5985 if (ptr) {
127 5985 *ptr = '\0';
128 5985 ptr++;
129 5985 i += (int)(ptr - &obuf[i]);
130 } else
131 ✗ i++;
132 5985 break;
133 7686 case 'W':
134 7686 op = OSFOPT_WSO;
135 7686 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
136
1/2
✓ Branch 0 (20→21) taken 7686 times.
✗ Branch 1 (20→30) not taken.
7686 if (ptr) {
137
2/4
✓ Branch 0 (21→22) taken 189 times.
✗ Branch 1 (21→23) not taken.
✗ Branch 2 (21→24) not taken.
✓ Branch 3 (21→25) taken 7497 times.
7686 switch (obuf[i + 1]) {
138 189 case '%':
139 189 wc = OSF_WSS_MODULO;
140 189 break;
141 ✗ case 'S':
142 ✗ wc = OSF_WSS_MSS;
143 ✗ break;
144 ✗ case 'T':
145 ✗ wc = OSF_WSS_MTU;
146 ✗ break;
147 7497 default:
148 7497 wc = OSF_WSS_PLAIN;
149 7497 break;
150 }
151
152 7686 *ptr = '\0';
153 7686 ptr++;
154
2/2
✓ Branch 0 (26→27) taken 189 times.
✓ Branch 1 (26→28) taken 7497 times.
7686 if (wc)
155 189 val = strtoul(&obuf[i + 2], NULL, 10);
156 else
157 7497 val = strtoul(&obuf[i + 1], NULL, 10);
158 7686 i += (int)(ptr - &obuf[i]);
159
160 } else
161 ✗ i++;
162 7686 break;
163 14805 case 'M':
164 14805 op = OSFOPT_MSS;
165 14805 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
166
1/2
✓ Branch 0 (33→34) taken 14805 times.
✗ Branch 1 (33→40) not taken.
14805 if (ptr) {
167
1/2
✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 14805 times.
14805 if (obuf[i + 1] == '%')
168 ✗ wc = OSF_WSS_MODULO;
169 14805 *ptr = '\0';
170 14805 ptr++;
171
1/2
✗ Branch 0 (36→37) not taken.
✓ Branch 1 (36→38) taken 14805 times.
14805 if (wc)
172 ✗ val = strtoul(&obuf[i + 2], NULL, 10);
173 else
174 14805 val = strtoul(&obuf[i + 1], NULL, 10);
175 14805 i += (int)(ptr - &obuf[i]);
176 } else
177 ✗ i++;
178 14805 break;
179 ✗ case 'E':
180 ✗ op = OSFOPT_EOL;
181 ✗ ptr = nf_osf_strchr(&obuf[i], OPTDEL);
182 ✗ if (ptr) {
183 ✗ *ptr = '\0';
184 ✗ ptr++;
185 ✗ i += (int)(ptr - &obuf[i]);
186 } else
187 ✗ i++;
188 ✗ break;
189 567 default:
190 567 op = OSFOPT_EMPTY;
191 567 ptr = nf_osf_strchr(&obuf[i], OPTDEL);
192
1/2
✓ Branch 0 (48→49) taken 567 times.
✗ Branch 1 (48→50) not taken.
567 if (ptr) {
193 567 ptr++;
194 567 i += (int)(ptr - &obuf[i]);
195 } else
196 ✗ i++;
197 567 break;
198 }
199
200
2/2
✓ Branch 0 (52→53) taken 64512 times.
✓ Branch 1 (52→54) taken 567 times.
65079 if (op != OSFOPT_EMPTY) {
201 64512 opt[*optnum].kind = IANA_opts[op].kind;
202 64512 opt[*optnum].length = IANA_opts[op].length;
203 64512 opt[*optnum].wc.wc = wc;
204 64512 opt[*optnum].wc.val = val;
205 64512 (*optnum)++;
206 }
207 }
208 15372 }
209
210 15372 static int osf_load_line(char *buffer, int len, int del,
211 struct netlink_ctx *ctx)
212 15372 {
213 15372 int i, cnt = 0;
214 char obuf[MAXOPTSTRLEN];
215 struct nf_osf_user_finger f;
216 char *pbeg, *pend;
217 struct nlmsghdr *nlh;
218 struct nfgenmsg *nfg;
219
1/2
✓ Branch 0 (3→4) taken 15372 times.
✗ Branch 1 (3→5) not taken.
15372 char buf[MNL_SOCKET_BUFFER_SIZE];
220
221 15372 memset(&f, 0, sizeof(struct nf_osf_user_finger));
222
223
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 15372 times.
15372 if (ctx->nft->debug_mask & NFT_DEBUG_MNL)
224 ✗ nft_print(&ctx->nft->output, "Loading '%s'.\n", buffer);
225
226
3/4
✓ Branch 0 (12→13) taken 927171 times.
✓ Branch 1 (12→14) taken 15372 times.
✓ Branch 2 (13→9) taken 927171 times.
✗ Branch 3 (13→14) not taken.
942543 for (i = 0; i < len && buffer[i] != '\0'; ++i) {
227
2/2
✓ Branch 0 (9→10) taken 122976 times.
✓ Branch 1 (9→11) taken 804195 times.
927171 if (buffer[i] == ':')
228 122976 cnt++;
229 }
230
231
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→18) taken 15372 times.
15372 if (cnt != 8) {
232 ✗ if (ctx->nft->debug_mask & NFT_DEBUG_MNL)
233 ✗ nft_print(&ctx->nft->output, "Wrong input line '%s': cnt: %d, must be 8, i: %d, must be %d.\n", buffer, cnt, i, len);
234 ✗ return -EINVAL;
235 }
236
237 15372 memset(obuf, 0, sizeof(obuf));
238
239 15372 pbeg = buffer;
240 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
241
1/2
✓ Branch 0 (19→20) taken 15372 times.
✗ Branch 1 (19→43) not taken.
15372 if (pend) {
242 15372 *pend = '\0';
243
2/2
✓ Branch 0 (20→21) taken 4914 times.
✓ Branch 1 (20→28) taken 10458 times.
15372 if (pbeg[0] == 'S') {
244 4914 f.wss.wc = OSF_WSS_MSS;
245
1/2
✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 4914 times.
4914 if (pbeg[1] == '%')
246 ✗ f.wss.val = strtoul(&pbeg[2], NULL, 10);
247
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 4914 times.
4914 else if (pbeg[1] == '*')
248 ✗ f.wss.val = 0;
249 else
250 4914 f.wss.val = strtoul(&pbeg[1], NULL, 10);
251
2/2
✓ Branch 0 (28→29) taken 441 times.
✓ Branch 1 (28→36) taken 10017 times.
10458 } else if (pbeg[0] == 'T') {
252 441 f.wss.wc = OSF_WSS_MTU;
253
1/2
✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→32) taken 441 times.
441 if (pbeg[1] == '%')
254 ✗ f.wss.val = strtoul(&pbeg[2], NULL, 10);
255
1/2
✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→34) taken 441 times.
441 else if (pbeg[1] == '*')
256 ✗ f.wss.val = 0;
257 else
258 441 f.wss.val = strtoul(&pbeg[1], NULL, 10);
259
2/2
✓ Branch 0 (36→37) taken 252 times.
✓ Branch 1 (36→39) taken 9765 times.
10017 } else if (pbeg[0] == '%') {
260 252 f.wss.wc = OSF_WSS_MODULO;
261 252 f.wss.val = strtoul(&pbeg[1], NULL, 10);
262
2/2
✓ Branch 0 (39→40) taken 9135 times.
✓ Branch 1 (39→42) taken 630 times.
9765 } else if (isdigit(pbeg[0])) {
263 9135 f.wss.wc = OSF_WSS_PLAIN;
264 9135 f.wss.val = strtoul(&pbeg[0], NULL, 10);
265 }
266
267 15372 pbeg = pend + 1;
268 }
269 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
270
1/2
✓ Branch 0 (44→45) taken 15372 times.
✗ Branch 1 (44→47) not taken.
15372 if (pend) {
271 15372 *pend = '\0';
272 15372 f.ttl = strtoul(pbeg, NULL, 10);
273 15372 pbeg = pend + 1;
274 }
275 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
276
1/2
✓ Branch 0 (48→49) taken 15372 times.
✗ Branch 1 (48→51) not taken.
15372 if (pend) {
277 15372 *pend = '\0';
278 15372 f.df = strtoul(pbeg, NULL, 10);
279 15372 pbeg = pend + 1;
280 }
281 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
282
1/2
✓ Branch 0 (52→53) taken 15372 times.
✗ Branch 1 (52→55) not taken.
15372 if (pend) {
283 15372 *pend = '\0';
284 15372 f.ss = strtoul(pbeg, NULL, 10);
285 15372 pbeg = pend + 1;
286 }
287
288 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
289
1/2
✓ Branch 0 (56→57) taken 15372 times.
✗ Branch 1 (56→58) not taken.
15372 if (pend) {
290 15372 *pend = '\0';
291 15372 i = sizeof(obuf);
292 15372 snprintf(obuf, i, "%.*s,", i - 2, pbeg);
293 15372 pbeg = pend + 1;
294 }
295
296 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
297
1/2
✓ Branch 0 (59→60) taken 15372 times.
✗ Branch 1 (59→64) not taken.
15372 if (pend) {
298 15372 *pend = '\0';
299 15372 i = sizeof(f.genre);
300
4/4
✓ Branch 0 (60→61) taken 14616 times.
✓ Branch 1 (60→62) taken 756 times.
✓ Branch 2 (61→62) taken 567 times.
✓ Branch 3 (61→63) taken 14049 times.
15372 if (pbeg[0] == '@' || pbeg[0] == '*')
301 1323 pbeg++;
302 15372 snprintf(f.genre, i, "%.*s", i - 1, pbeg);
303 15372 pbeg = pend + 1;
304 }
305
306 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
307
1/2
✓ Branch 0 (65→66) taken 15372 times.
✗ Branch 1 (65→67) not taken.
15372 if (pend) {
308 15372 *pend = '\0';
309 15372 i = sizeof(f.version);
310 15372 snprintf(f.version, i, "%.*s", i - 1, pbeg);
311 15372 pbeg = pend + 1;
312 }
313
314 15372 pend = nf_osf_strchr(pbeg, OSFPDEL);
315
1/2
✓ Branch 0 (68→69) taken 15372 times.
✗ Branch 1 (68→70) not taken.
15372 if (pend) {
316 15372 *pend = '\0';
317 15372 i = sizeof(f.subtype);
318 15372 snprintf(f.subtype, i, "%.*s", i - 1, pbeg);
319 15372 pbeg = pend + 1;
320 }
321
322 15372 nf_osf_parse_opt(f.opt, &f.opt_num, obuf, sizeof(obuf));
323
324 15372 memset(buf, 0, sizeof(buf));
325
326
1/2
✗ Branch 0 (71→72) not taken.
✓ Branch 1 (71→75) taken 15372 times.
15372 if (del) {
327 ✗ nlh = mnl_nlmsg_put_header(buf);
328 ✗ nlh->nlmsg_type = (NFNL_SUBSYS_OSF << 8) | OSF_MSG_REMOVE;
329 ✗ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
330 ✗ nlh->nlmsg_seq = ctx->seqnum;
331
332 ✗ nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
333 ✗ nfg->nfgen_family = AF_UNSPEC;
334 ✗ nfg->version = NFNETLINK_V0;
335 ✗ nfg->res_id = 0;
336 } else {
337 15372 nlh = mnl_nlmsg_put_header(buf);
338 15372 nlh->nlmsg_type = (NFNL_SUBSYS_OSF << 8) | OSF_MSG_ADD;
339 15372 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
340 15372 nlh->nlmsg_seq = ctx->seqnum;
341
342 15372 nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
343 15372 nfg->nfgen_family = AF_UNSPEC;
344 15372 nfg->version = NFNETLINK_V0;
345 15372 nfg->res_id = 0;
346
347 15372 mnl_attr_put(nlh, OSF_ATTR_FINGER, sizeof(struct nf_osf_user_finger), &f);
348 }
349
350 15372 return nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, 0, NULL);
351 }
352
353 #define OS_SIGNATURES DEFAULT_INCLUDE_PATH "/nftables/osf/pf.os"
354
355 63 int nfnl_osf_load_fingerprints(struct netlink_ctx *ctx, int del)
356 {
357 FILE *inf;
358 63 int err = 0;
359 char buf[1024];
360
361
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 63 times.
63 if (ctx->nft->debug_mask & NFT_DEBUG_MNL)
362 ✗ nft_print(&ctx->nft->output, "Opening OS signature file '%s'\n",
363 OS_SIGNATURES);
364
365 63 inf = fopen(OS_SIGNATURES, "r");
366
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 63 times.
63 if (!inf) {
367 ✗ if (ctx->nft->debug_mask & NFT_DEBUG_MNL)
368 ✗ nft_print(&ctx->nft->output, "Failed to open file '%s'\n",
369 OS_SIGNATURES);
370
371 ✗ return -1;
372 }
373
374
2/2
✓ Branch 0 (22→10) taken 44289 times.
✓ Branch 1 (22→23) taken 63 times.
44352 while (fgets(buf, sizeof(buf), inf)) {
375 int len;
376
377
5/6
✓ Branch 0 (10→11) taken 25389 times.
✓ Branch 1 (10→13) taken 18900 times.
✓ Branch 2 (11→12) taken 15372 times.
✓ Branch 3 (11→13) taken 10017 times.
✗ Branch 4 (12→13) not taken.
✓ Branch 5 (12→14) taken 15372 times.
44289 if (buf[0] == '#' || buf[0] == '\n' || buf[0] == '\r')
378 28917 continue;
379
380 15372 len = strlen(buf) - 1;
381
382
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 15372 times.
15372 if (len <= 0)
383 ✗ continue;
384
385 15372 buf[len] = '\0';
386
387 15372 err = osf_load_line(buf, len, del, ctx);
388
1/2
✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→19) taken 15372 times.
15372 if (err)
389 ✗ break;
390
391 15372 memset(buf, 0, sizeof(buf));
392 }
393
394 63 fclose(inf);
395 63 return err;
396 }
397