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_NETDEV] =
"netdev",
30 [NFPROTO_BRIDGE] =
"bridge",
31 [NFPROTO_IPV6] =
"ip6",
34 const char *nftnl_family2str(uint32_t family)
36 if (family >= NFPROTO_NUMPROTO || !nftnl_family_str[family])
39 return nftnl_family_str[family];
42 int 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 },
73 int nftnl_get_value(
enum nftnl_type type,
void *val,
void *out)
83 uval = *((uint64_t *)val);
84 if (uval > basetype[type].max) {
88 memcpy(out, &uval, basetype[type].len);
94 sval = *((int64_t *)val);
95 if (sval < basetype[type].min ||
96 sval > (int64_t)basetype[type].max) {
100 memcpy(out, &sval, basetype[type].len);
107 int nftnl_strtoi(
const char *
string,
int base,
void *out,
enum nftnl_type type)
119 uval = strtoll(
string, &endptr, base);
120 ret = nftnl_get_value(type, &uval, out);
126 sval = strtoull(
string, &endptr, base);
127 ret = nftnl_get_value(type, &sval, out);
142 const char *nftnl_verdict2str(uint32_t verdict)
172 int nftnl_str2verdict(
const char *verdict,
int *verdict_num)
174 if (strcmp(verdict,
"accept") == 0) {
175 *verdict_num = NF_ACCEPT;
177 }
else if (strcmp(verdict,
"drop") == 0) {
178 *verdict_num = NF_DROP;
180 }
else if (strcmp(verdict,
"return") == 0) {
181 *verdict_num = NFT_RETURN;
183 }
else if (strcmp(verdict,
"jump") == 0) {
184 *verdict_num = NFT_JUMP;
186 }
else if (strcmp(verdict,
"goto") == 0) {
187 *verdict_num = NFT_GOTO;
194 enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
196 if (flags & NFTNL_OF_EVENT_NEW)
197 return NFTNL_CMD_ADD;
198 else if (flags & NFTNL_OF_EVENT_DEL)
199 return NFTNL_CMD_DELETE;
201 return NFTNL_CMD_UNSPEC;
204 static const char *cmd2tag[NFTNL_CMD_MAX] = {
205 [NFTNL_CMD_ADD] = ADD,
206 [NFTNL_CMD_INSERT] = INSERT,
207 [NFTNL_CMD_DELETE] = DELETE,
208 [NFTNL_CMD_REPLACE] = REPLACE,
209 [NFTNL_CMD_FLUSH] = FLUSH,
212 const char *nftnl_cmd2tag(
enum nftnl_cmd_type cmd)
214 if (cmd >= NFTNL_CMD_MAX)
220 uint32_t nftnl_str2cmd(
const char *cmd)
222 if (strcmp(cmd, ADD) == 0)
223 return NFTNL_CMD_ADD;
224 else if (strcmp(cmd, INSERT) == 0)
225 return NFTNL_CMD_INSERT;
226 else if (strcmp(cmd, DELETE) == 0)
227 return NFTNL_CMD_DELETE;
228 else if (strcmp(cmd, REPLACE) == 0)
229 return NFTNL_CMD_REPLACE;
230 else if (strcmp(cmd, FLUSH) == 0)
231 return NFTNL_CMD_FLUSH;
233 return NFTNL_CMD_UNSPEC;
236 int nftnl_fprintf(FILE *fp,
const void *obj, uint32_t cmd, uint32_t type,
238 int (*snprintf_cb)(
char *buf,
size_t bufsiz,
const void *obj,
239 uint32_t cmd, uint32_t type,
242 char _buf[NFTNL_SNPRINTF_BUFSIZ];
244 size_t bufsiz =
sizeof(_buf);
247 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
251 if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
254 buf = malloc(bufsiz);
258 ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
263 ret = fprintf(fp,
"%s", buf);
272 void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
273 const char *filename,
int line)
275 fprintf(stderr,
"libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
276 attr, attr_max, filename, line);
280 void __nftnl_assert_fail(uint16_t attr,
const char *filename,
int line)
282 fprintf(stderr,
"libnftnl: attribute %d assertion failed in %s:%d\n",
283 attr, filename, line);
287 void __noreturn __abi_breakage(
const char *file,
int line,
const char *reason)
289 fprintf(stderr,
"nf_tables kernel ABI is broken, contact your vendor.\n"
290 "%s:%d reason: %s\n", file, line, reason);