16 #include <arpa/inet.h>
20 #include <libnftnl/common.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/nf_tables.h>
25 static const char *
const nftnl_family_str[NFPROTO_NUMPROTO] = {
26 [NFPROTO_INET] =
"inet",
27 [NFPROTO_IPV4] =
"ip",
28 [NFPROTO_ARP] =
"arp",
29 [NFPROTO_BRIDGE] =
"bridge",
30 [NFPROTO_IPV6] =
"ip6",
33 const char *nftnl_family2str(uint32_t family)
35 if (nftnl_family_str[family] == NULL)
38 return nftnl_family_str[family];
41 int nftnl_str2family(
const char *family)
45 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
46 if (nftnl_family_str[i] == NULL)
49 if (strcmp(nftnl_family_str[i], family) == 0)
62 [NFTNL_TYPE_U8] = { .len =
sizeof(uint8_t), .max = UINT8_MAX },
63 [NFTNL_TYPE_U16] = { .len =
sizeof(uint16_t), .max = UINT16_MAX },
64 [NFTNL_TYPE_U32] = { .len =
sizeof(uint32_t), .max = UINT32_MAX },
65 [NFTNL_TYPE_U64] = { .len =
sizeof(uint64_t), .max = UINT64_MAX },
66 [NFTNL_TYPE_S8] = { .len =
sizeof(int8_t), .min = INT8_MIN, .max = INT8_MAX },
67 [NFTNL_TYPE_S16] = { .len =
sizeof(int16_t), .min = INT16_MIN, .max = INT16_MAX },
68 [NFTNL_TYPE_S32] = { .len =
sizeof(int32_t), .min = INT32_MIN, .max = INT32_MAX },
69 [NFTNL_TYPE_S64] = { .len =
sizeof(int64_t), .min = INT64_MIN, .max = INT64_MAX },
72 int nftnl_get_value(
enum nftnl_type type,
void *val,
void *out)
82 uval = *((uint64_t *)val);
83 if (uval > basetype[type].max) {
87 memcpy(out, &uval, basetype[type].len);
93 sval = *((int64_t *)val);
94 if (sval < basetype[type].min ||
95 sval > (int64_t)basetype[type].max) {
99 memcpy(out, &sval, basetype[type].len);
106 int nftnl_strtoi(
const char *
string,
int base,
void *out,
enum nftnl_type type)
118 uval = strtoll(
string, &endptr, base);
119 ret = nftnl_get_value(type, &uval, out);
125 sval = strtoull(
string, &endptr, base);
126 ret = nftnl_get_value(type, &sval, out);
141 const char *nftnl_verdict2str(uint32_t verdict)
159 int nftnl_str2verdict(
const char *verdict,
int *verdict_num)
161 if (strcmp(verdict,
"accept") == 0) {
162 *verdict_num = NF_ACCEPT;
164 }
else if (strcmp(verdict,
"drop") == 0) {
165 *verdict_num = NF_DROP;
167 }
else if (strcmp(verdict,
"return") == 0) {
168 *verdict_num = NFT_RETURN;
170 }
else if (strcmp(verdict,
"jump") == 0) {
171 *verdict_num = NFT_JUMP;
173 }
else if (strcmp(verdict,
"goto") == 0) {
174 *verdict_num = NFT_GOTO;
181 enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
183 if (flags & NFTNL_OF_EVENT_NEW)
184 return NFTNL_CMD_ADD;
185 else if (flags & NFTNL_OF_EVENT_DEL)
186 return NFTNL_CMD_DELETE;
188 return NFTNL_CMD_UNSPEC;
191 const char *cmd2tag[NFTNL_CMD_MAX] = {
192 [NFTNL_CMD_ADD] = ADD,
193 [NFTNL_CMD_INSERT] = INSERT,
194 [NFTNL_CMD_DELETE] = DELETE,
195 [NFTNL_CMD_REPLACE] = REPLACE,
196 [NFTNL_CMD_FLUSH] = FLUSH,
199 const char *nftnl_cmd2tag(
enum nftnl_cmd_type cmd)
201 if (cmd >= NFTNL_CMD_MAX)
207 uint32_t nftnl_str2cmd(
const char *cmd)
209 if (strcmp(cmd, ADD) == 0)
210 return NFTNL_CMD_ADD;
211 else if (strcmp(cmd, INSERT) == 0)
212 return NFTNL_CMD_INSERT;
213 else if (strcmp(cmd, DELETE) == 0)
214 return NFTNL_CMD_DELETE;
215 else if (strcmp(cmd, REPLACE) == 0)
216 return NFTNL_CMD_REPLACE;
217 else if (strcmp(cmd, FLUSH) == 0)
218 return NFTNL_CMD_FLUSH;
220 return NFTNL_CMD_UNSPEC;
223 int nftnl_fprintf(FILE *fp,
void *obj, uint32_t cmd, uint32_t type, uint32_t flags,
224 int (*snprintf_cb)(
char *buf,
size_t bufsiz,
void *obj,
225 uint32_t cmd, uint32_t type, uint32_t flags))
227 char _buf[NFTNL_SNPRINTF_BUFSIZ];
229 size_t bufsiz =
sizeof(_buf);
232 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
236 if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
239 buf = malloc(bufsiz);
243 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
248 ret = fprintf(fp,
"%s", buf);
257 void __nftnl_assert_fail(uint16_t attr,
const char *filename,
int line)
259 fprintf(stderr,
"libnftnl: attribute %d assertion failed in %s:%d\n",
260 attr, filename, line);
264 void __noreturn __abi_breakage(
const char *file,
int line,
const char *reason)
266 fprintf(stderr,
"nf_tables kernel ABI is broken, contact your vendor.\n"
267 "%s:%d reason: %s\n", file, line, reason);