GCC Code Coverage Report


Directory: ./
File: src/socket.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 15 44 34.1%
Functions: 3 7 42.9%
Branches: 1 14 7.1%

Line Branch Exec Source
1 /*
2 * Socket expression/statement related definition and types.
3 *
4 * Copyright (c) 2018 Máté Eckl <ecklm94@gmail.com>
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 (or any
8 * later) as published by the Free Software Foundation.
9 */
10
11 #include <nft.h>
12
13 #include <nftables.h>
14 #include <expression.h>
15 #include <socket.h>
16 #include <json.h>
17
18 const struct socket_template socket_templates[] = {
19 [NFT_SOCKET_TRANSPARENT] = {
20 .token = "transparent",
21 .dtype = &integer_type,
22 .len = BITS_PER_BYTE,
23 .byteorder = BYTEORDER_HOST_ENDIAN,
24 },
25 [NFT_SOCKET_MARK] = {
26 .token = "mark",
27 .dtype = &mark_type,
28 .len = 4 * BITS_PER_BYTE,
29 .byteorder = BYTEORDER_HOST_ENDIAN,
30 },
31 [NFT_SOCKET_WILDCARD] = {
32 .token = "wildcard",
33 .dtype = &integer_type,
34 .len = BITS_PER_BYTE,
35 .byteorder = BYTEORDER_HOST_ENDIAN,
36 },
37 [NFT_SOCKET_CGROUPV2] = {
38 .token = "cgroupv2",
39 .dtype = &cgroupv2_type,
40 .len = 8 * BITS_PER_BYTE,
41 .byteorder = BYTEORDER_HOST_ENDIAN,
42 },
43 };
44
45 15 static void socket_expr_print(const struct expr *expr, struct output_ctx *octx)
46 {
47 15 nft_print(octx, "socket %s", socket_templates[expr->socket.key].token);
48
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 15 times.
15 if (expr->socket.key == NFT_SOCKET_CGROUPV2)
49 ✗ nft_print(octx, " level %u", expr->socket.level);
50 15 }
51
52 ✗ static bool socket_expr_cmp(const struct expr *e1, const struct expr *e2)
53 {
54 ✗ return e1->socket.key == e2->socket.key &&
55 ✗ e1->socket.level == e2->socket.level;
56 }
57
58 30 static void socket_expr_clone(struct expr *new, const struct expr *expr)
59 {
60 30 new->socket.key = expr->socket.key;
61 30 new->socket.level = expr->socket.level;
62 30 }
63
64 #define NFTNL_UDATA_SOCKET_KEY 0
65 #define NFTNL_UDATA_SOCKET_MAX 1
66
67 ✗ static int socket_expr_build_udata(struct nftnl_udata_buf *udbuf,
68 const struct expr *expr)
69 {
70 ✗ nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SOCKET_KEY, expr->socket.key);
71
72 ✗ return 0;
73 }
74
75 ✗ static int socket_parse_udata(const struct nftnl_udata *attr, void *data)
76 {
77 ✗ const struct nftnl_udata **ud = data;
78 ✗ uint8_t type = nftnl_udata_type(attr);
79 ✗ uint8_t len = nftnl_udata_len(attr);
80
81 ✗ switch (type) {
82 ✗ case NFTNL_UDATA_SOCKET_KEY:
83 ✗ if (len != sizeof(uint32_t))
84 ✗ return -1;
85 ✗ break;
86 ✗ default:
87 ✗ return 0;
88 }
89
90 ✗ ud[type] = attr;
91 ✗ return 0;
92 }
93
94 ✗ static struct expr *socket_expr_parse_udata(const struct nftnl_udata *attr)
95 {
96 ✗ const struct nftnl_udata *ud[NFTNL_UDATA_SOCKET_MAX + 1] = {};
97 uint32_t key;
98 int err;
99
100 ✗ err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr),
101 socket_parse_udata, ud);
102 ✗ if (err < 0)
103 ✗ return NULL;
104
105 ✗ if (!ud[NFTNL_UDATA_SOCKET_KEY])
106 ✗ return NULL;
107
108 ✗ key = nftnl_udata_get_u32(ud[NFTNL_UDATA_SOCKET_KEY]);
109
110 ✗ return socket_expr_alloc(&internal_location, key, 0);
111 }
112
113 const struct expr_ops socket_expr_ops = {
114 .type = EXPR_SOCKET,
115 .name = "socket",
116 .print = socket_expr_print,
117 .json = socket_expr_json,
118 .cmp = socket_expr_cmp,
119 .clone = socket_expr_clone,
120 .build_udata = socket_expr_build_udata,
121 .parse_udata = socket_expr_parse_udata,
122 };
123
124 67 struct expr *socket_expr_alloc(const struct location *loc,
125 enum nft_socket_keys key, uint32_t level)
126 {
127 67 const struct socket_template *tmpl = &socket_templates[key];
128 struct expr *expr;
129
130 67 expr = expr_alloc(loc, EXPR_SOCKET, tmpl->dtype,
131 67 tmpl->byteorder, tmpl->len);
132 67 expr->socket.key = key;
133 67 expr->socket.level = level;
134
135 67 return expr;
136 }
137