Debian Codegraph @

src/utils.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/utils.c`/
2 /*
3 * (C) 2012-2013 by Pablo Neira Ayuso <[email protected]>
4 * (C) 2013 by Arturo Borrero Gonzalez <[email protected]>
5 */
6
7 #include <internal.h>cxx . . $ `<file>/include/internal.h`/
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <stdint.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <inttypes.h>
15
16 #include <libmnl/libmnl.h>
17
18 #include <libnftnl/common.h>cxx . . $ `<file>/include/libnftnl/common.h`/
19
20 #include <linux/if.h>
21 #include <linux/netfilter.h>cxx . . $ `<file>/include/linux/netfilter.h`/
22 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
23
24 static const char *const nftnl_family_strcxx . . $ nftnl_family_str.[NFPROTO_NUMPROTOcxx . . $ NFPROTO_NUMPROTO.] = {
25 [NFPROTO_INETcxx . . $ NFPROTO_INET.] = "inet",
26 [NFPROTO_IPV4cxx . . $ NFPROTO_IPV4.] = "ip",
27 [NFPROTO_ARPcxx . . $ NFPROTO_ARP.] = "arp",
28 [NFPROTO_NETDEVcxx . . $ NFPROTO_NETDEV.] = "netdev",
29 [NFPROTO_BRIDGEcxx . . $ NFPROTO_BRIDGE.] = "bridge",
30 [NFPROTO_IPV6cxx . . $ NFPROTO_IPV6.] = "ip6",
31 };
32
33 const char *nftnl_family2strcxx . . $ nftnl_family2str(81a3ae90da26a711).(uint32_tcxx . . $ uint32_t# family)
34 {
35 if (family >= NFPROTO_NUMPROTOcxx . . $ NFPROTO_NUMPROTO. || !nftnl_family_strcxx . . $ nftnl_family_str.[family])
36 return "unknown";
37
38 return nftnl_family_strcxx . . $ nftnl_family_str.[family];
39 }
40
41 const char *nftnl_verdict2strcxx . . $ nftnl_verdict2str(81a3ae90da26a711).(uint32_tcxx . . $ uint32_t# verdict)
42 {
43 switch (verdict) {
44 case NF_ACCEPTcxx . . $ `include/linux/netfilter.h:11:9`!:
45 return "accept";
46 case NF_DROPcxx . . $ `include/linux/netfilter.h:10:9`!:
47 return "drop";
48 case NF_STOLENcxx . . $ `include/linux/netfilter.h:12:9`!:
49 return "stolen";
50 case NF_QUEUEcxx . . $ `include/linux/netfilter.h:13:9`!:
51 return "queue";
52 case NF_REPEATcxx . . $ `include/linux/netfilter.h:14:9`!:
53 return "repeat";
54 case NF_STOPcxx . . $ `include/linux/netfilter.h:15:9`!:
55 return "stop";
56 case NFT_RETURNcxx . . $ NFT_RETURN.:
57 return "return";
58 case NFT_JUMPcxx . . $ NFT_JUMP.:
59 return "jump";
60 case NFT_GOTOcxx . . $ NFT_GOTO.:
61 return "goto";
62 case NFT_CONTINUEcxx . . $ NFT_CONTINUE.:
63 return "continue";
64 case NFT_BREAKcxx . . $ NFT_BREAK.:
65 return "break";
66 default:
67 return "unknown";
68 }
69 }
70
71 enum nftnl_cmd_typecxx . . $ nftnl_cmd_type# nftnl_flag2cmdcxx . . $ nftnl_flag2cmd(73b61a9477c235b4).(uint32_tcxx . . $ uint32_t# flags)
72 {
73 if (flags & NFTNL_OF_EVENT_NEWcxx . . $ NFTNL_OF_EVENT_NEW.)
74 return NFTNL_CMD_ADDcxx . . $ NFTNL_CMD_ADD.;
75 else if (flags & NFTNL_OF_EVENT_DELcxx . . $ NFTNL_OF_EVENT_DEL.)
76 return NFTNL_CMD_DELETEcxx . . $ NFTNL_CMD_DELETE.;
77
78 return NFTNL_CMD_UNSPECcxx . . $ NFTNL_CMD_UNSPEC.;
79 }
80
81 int nftnl_fprintfcxx . . $ nftnl_fprintf(959aea809fde9589).(FILEcxx . . $ FILE# *fp, const void *obj, uint32_tcxx . . $ uint32_t# cmd, uint32_tcxx . . $ uint32_t# type,
82 uint32_tcxx . . $ uint32_t# flags,
83 int (*snprintf_cb)(char *buf, size_t bufsiz, const void *obj,
84 uint32_tcxx . . $ uint32_t# cmd, uint32_tcxx . . $ uint32_t# type,
85 uint32_tcxx . . $ uint32_t# flags))
86 {
87 char _buf[NFTNL_SNPRINTF_BUFSIZcxx . . $ `include/common.h:7:9`!];
88 char *buf = _buf;
89 size_t bufsiz = sizeof(_buf);
90 int ret;
91
92 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
93 if (ret <= 0)
94 goto out;
95
96 if (ret >= NFTNL_SNPRINTF_BUFSIZcxx . . $ `include/common.h:7:9`!) {
97 bufsiz = ret + 1;
98
99 buf = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(bufsiz);
100 if (buf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
101 return -1;
102
103 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
104 if (ret <= 0)
105 goto out;
106 }
107
108 ret = fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "%s", buf);
109
110 out:
111 if (buf != _buf)
112 xfreecxx . . $ `include/utils.h:22:9`!(buf);
113
114 return ret;
115 }
116
117 void __nftnl_assert_attr_existscxx . . $ __nftnl_assert_attr_exists(62ad6cc440b3457b).(uint16_tcxx . . $ uint16_t# attr, uint16_tcxx . . $ uint16_t# attr_max,
118 const char *filename, int line)
119 {
120 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
121 attr, attr_max, filename, line);
122 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
123 }
124
125 void __nftnl_assert_failcxx . . $ __nftnl_assert_fail(c96d46ecfa8e00ae).(uint16_tcxx . . $ uint16_t# attr, const char *filename, int line)
126 {
127 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "libnftnl: attribute %d assertion failed in %s:%d\n",
128 attr, filename, line);
129 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
130 }
131
132 void __noreturncxx . . $ `include/utils.h:20:9`! __abi_breakagecxx . . $ __abi_breakage(1eb19c77f92b47d4).(const char *file, int line, const char *reason)
133 {
134 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "nf_tables kernel ABI is broken, contact your vendor.\n"
135 "%s:%d reason: %s\n", file, line, reason);
136 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!);
137 }
138
139 int nftnl_set_str_attrcxx . . $ nftnl_set_str_attr(fd565288bd370d98).(const char **dptr, uint32_tcxx . . $ uint32_t# *flags,
140 uint16_tcxx . . $ uint16_t# attr, const void *data, uint32_tcxx . . $ uint32_t# data_len)
141 {
142 if (*flags & (1 << attr))
143 xfreecxx . . $ `include/utils.h:22:9`!(*dptr);
144
145 *dptr = strndupcxx . . $ strndup(eabdfdef77619b3d).(data, data_len);
146 if (!*dptr)
147 return -1;
148
149 *flags |= (1 << attr);
150 return 0;
151 }
152
153 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! is_wildcard_strcxx . . $ is_wildcard_str(c5f7da30cbc0f4e9).(const char *str)
154 {
155 size_t len = strlencxx . . $ strlen(751346f8406fd082).(str);
156
157 if (len < 1 || str[len - 1] != '*')
158 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
159 if (len < 2 || str[len - 2] != '\\')
160 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
161 /* XXX: ignore backslash escaping for now */
162 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
163 }
164
165 void nftnl_attr_put_ifnamecxx . . $ nftnl_attr_put_ifname(96236743ed9497eb).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const char *ifname)
166 {
167 uint16_tcxx . . $ uint16_t# attr = NFTA_DEVICE_NAMEcxx . . $ NFTA_DEVICE_NAME.;
168 char pfx[IFNAMSIZcxx . . $ `<external>/5120534110947197618/if.h:31:9`!];
169
170 if (is_wildcard_strcxx . . $ is_wildcard_str(c5f7da30cbc0f4e9).(ifname)) {
171 snprintfcxx . . $ snprintf(ea502d73e74c754c).(pfx, IFNAMSIZcxx . . $ `<external>/5120534110947197618/if.h:31:9`!, "%s", ifname);
172 pfx[strlencxx . . $ strlen(751346f8406fd082).(pfx) - 1] = '\0';
173
174 attr = NFTA_DEVICE_PREFIXcxx . . $ NFTA_DEVICE_PREFIX.;
175 ifname = pfx;
176 }
177 mnl_attr_put_strzcxx . . $ mnl_attr_put_strz(eb76286d4bfc1a8d).(nlh, attr, ifname);
178 }
179
180 char *nftnl_attr_get_ifnamecxx . . $ nftnl_attr_get_ifname(5e785b67b24f52d1).(const struct nlattrcxx . . $ nlattr# *attr)
181 {
182 const char *dev = mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(attr);
183 char buf[IFNAMSIZcxx . . $ `<external>/5120534110947197618/if.h:31:9`!];
184
185 switch (mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr)) {
186 case NFTA_DEVICE_NAMEcxx . . $ NFTA_DEVICE_NAME.:
187 return strdupcxx . . $ strdup(a8b8e32347182212).(dev);
188 case NFTA_DEVICE_PREFIXcxx . . $ NFTA_DEVICE_PREFIX.:
189 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, IFNAMSIZcxx . . $ `<external>/5120534110947197618/if.h:31:9`!, "%s*", dev);
190 return strdupcxx . . $ strdup(a8b8e32347182212).(buf);
191 default:
192 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
193 }
194 }
195
196 int nftnl_parse_str_attrcxx . . $ nftnl_parse_str_attr(2002c8408bf98f7f).(const struct nlattrcxx . . $ nlattr# *tb, int attr,
197 const char **field, uint32_tcxx . . $ uint32_t# *flags)
198 {
199 if (!tb)
200 return 0;
201
202 if (*flags & (1 << attr))
203 xfreecxx . . $ `include/utils.h:22:9`!(*field);
204 *field = strdupcxx . . $ strdup(a8b8e32347182212).(mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(tb));
205 if (!*field)
206 return -1;
207 *flags |= (1 << attr);
208 return 0;
209 }