20#include <libnftnl/common.h>
22#include <linux/netfilter.h>
23#include <linux/netfilter/nf_tables.h>
25static const char *
const nftnl_family_str[NFPROTO_NUMPROTO] = {
26 [NFPROTO_INET] =
"inet",
27 [NFPROTO_IPV4] =
"ip",
28 [NFPROTO_ARP] =
"arp",
29 [NFPROTO_NETDEV] =
"netdev",
30 [NFPROTO_BRIDGE] =
"bridge",
31 [NFPROTO_IPV6] =
"ip6",
34const char *nftnl_family2str(uint32_t family)
36 if (family >= NFPROTO_NUMPROTO || !nftnl_family_str[family])
39 return nftnl_family_str[family];
42int nftnl_str2family(
const char *family)
46 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
47 if (nftnl_family_str[i] == NULL)
50 if (strcmp(nftnl_family_str[i], family) == 0)
63 [NFTNL_TYPE_U8] = { .len =
sizeof(uint8_t), .max = UINT8_MAX },
64 [NFTNL_TYPE_U16] = { .len =
sizeof(uint16_t), .max = UINT16_MAX },
65 [NFTNL_TYPE_U32] = { .len =
sizeof(uint32_t), .max = UINT32_MAX },
66 [NFTNL_TYPE_U64] = { .len =
sizeof(uint64_t), .max = UINT64_MAX },
67 [NFTNL_TYPE_S8] = { .len =
sizeof(int8_t), .min = INT8_MIN, .max = INT8_MAX },
68 [NFTNL_TYPE_S16] = { .len =
sizeof(int16_t), .min = INT16_MIN, .max = INT16_MAX },
69 [NFTNL_TYPE_S32] = { .len =
sizeof(int32_t), .min = INT32_MIN, .max = INT32_MAX },
70 [NFTNL_TYPE_S64] = { .len =
sizeof(int64_t), .min = INT64_MIN, .max = INT64_MAX },
73int nftnl_get_value(
enum nftnl_type type,
void *val,
void *out)
92 memcpy(&uval, val,
sizeof(uval));
93 if (uval > basetype[type].max) {
102 memcpy(&sval, val,
sizeof(sval));
103 if (sval < basetype[type].min ||
104 sval > (int64_t)basetype[type].max) {
118 valuep = &values.u16;
122 valuep = &values.u32;
133 valuep = &values.s16;
137 valuep = &values.s32;
143 memcpy(out, valuep, basetype[type].len);
147int nftnl_strtoi(
const char *
string,
int base,
void *out,
enum nftnl_type type)
159 uval = strtoll(
string, &endptr, base);
160 ret = nftnl_get_value(type, &uval, out);
166 sval = strtoull(
string, &endptr, base);
167 ret = nftnl_get_value(type, &sval, out);
182const char *nftnl_verdict2str(uint32_t verdict)
212int nftnl_str2verdict(
const char *verdict,
int *verdict_num)
214 if (strcmp(verdict,
"accept") == 0) {
215 *verdict_num = NF_ACCEPT;
217 }
else if (strcmp(verdict,
"drop") == 0) {
218 *verdict_num = NF_DROP;
220 }
else if (strcmp(verdict,
"return") == 0) {
221 *verdict_num = NFT_RETURN;
223 }
else if (strcmp(verdict,
"jump") == 0) {
224 *verdict_num = NFT_JUMP;
226 }
else if (strcmp(verdict,
"goto") == 0) {
227 *verdict_num = NFT_GOTO;
234enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
236 if (flags & NFTNL_OF_EVENT_NEW)
237 return NFTNL_CMD_ADD;
238 else if (flags & NFTNL_OF_EVENT_DEL)
239 return NFTNL_CMD_DELETE;
241 return NFTNL_CMD_UNSPEC;
244static const char *cmd2tag[NFTNL_CMD_MAX] = {
245 [NFTNL_CMD_ADD] =
"add",
246 [NFTNL_CMD_INSERT] =
"insert",
247 [NFTNL_CMD_DELETE] =
"delete",
248 [NFTNL_CMD_REPLACE] =
"replace",
249 [NFTNL_CMD_FLUSH] =
"flush",
252const char *nftnl_cmd2tag(
enum nftnl_cmd_type cmd)
254 if (cmd >= NFTNL_CMD_MAX)
260uint32_t nftnl_str2cmd(
const char *cmd)
262 if (strcmp(cmd,
"add") == 0)
263 return NFTNL_CMD_ADD;
264 else if (strcmp(cmd,
"insert") == 0)
265 return NFTNL_CMD_INSERT;
266 else if (strcmp(cmd,
"delete") == 0)
267 return NFTNL_CMD_DELETE;
268 else if (strcmp(cmd,
"replace") == 0)
269 return NFTNL_CMD_REPLACE;
270 else if (strcmp(cmd,
"flush") == 0)
271 return NFTNL_CMD_FLUSH;
273 return NFTNL_CMD_UNSPEC;
276int nftnl_fprintf(FILE *fp,
const void *obj, uint32_t cmd, uint32_t type,
278 int (*snprintf_cb)(
char *buf,
size_t bufsiz,
const void *obj,
279 uint32_t cmd, uint32_t type,
282 char _buf[NFTNL_SNPRINTF_BUFSIZ];
284 size_t bufsiz =
sizeof(_buf);
287 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
291 if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
294 buf = malloc(bufsiz);
298 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
303 ret = fprintf(fp,
"%s", buf);
312void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
313 const char *filename,
int line)
315 fprintf(stderr,
"libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
316 attr, attr_max, filename, line);
320void __nftnl_assert_fail(uint16_t attr,
const char *filename,
int line)
322 fprintf(stderr,
"libnftnl: attribute %d assertion failed in %s:%d\n",
323 attr, filename, line);
327void __noreturn __abi_breakage(
const char *file,
int line,
const char *reason)
329 fprintf(stderr,
"nf_tables kernel ABI is broken, contact your vendor.\n"
330 "%s:%d reason: %s\n", file, line, reason);