libnftnl 1.2.6
secmark.c
1/*
2 * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <stdio.h>
11#include <stdint.h>
12#include <arpa/inet.h>
13#include <errno.h>
14#include <inttypes.h>
15
16#include <linux/netfilter/nf_tables.h>
17
18#include "internal.h"
19#include <libmnl/libmnl.h>
20#include <libnftnl/object.h>
21
22#include "obj.h"
23
24static int nftnl_obj_secmark_set(struct nftnl_obj *e, uint16_t type,
25 const void *data, uint32_t data_len)
26{
27 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
28
29 switch (type) {
30 case NFTNL_OBJ_SECMARK_CTX:
31 snprintf(secmark->ctx, sizeof(secmark->ctx), "%s", (const char *)data);
32 break;
33 default:
34 return -1;
35 }
36 return 0;
37}
38
39static const void *nftnl_obj_secmark_get(const struct nftnl_obj *e,
40 uint16_t type, uint32_t *data_len)
41{
42 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
43
44 switch (type) {
45 case NFTNL_OBJ_SECMARK_CTX:
46 *data_len = strlen(secmark->ctx);
47 return secmark->ctx;
48 }
49 return NULL;
50}
51
52static int nftnl_obj_secmark_cb(const struct nlattr *attr, void *data)
53{
54 const struct nftnl_obj_secmark *secmark = NULL;
55 int type = mnl_attr_get_type(attr);
56 const struct nlattr **tb = data;
57
58 if (mnl_attr_type_valid(attr, NFTA_SECMARK_MAX) < 0)
59 return MNL_CB_OK;
60
61 switch(type) {
62 case NFTA_SECMARK_CTX:
63 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
64 abi_breakage();
65 if (mnl_attr_get_payload_len(attr) >= sizeof(secmark->ctx))
66 abi_breakage();
67 break;
68 }
69
70 tb[type] = attr;
71 return MNL_CB_OK;
72}
73
74static void
75nftnl_obj_secmark_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
76{
77 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
78
79 if (e->flags & (1 << NFTNL_OBJ_SECMARK_CTX))
80 mnl_attr_put_str(nlh, NFTA_SECMARK_CTX, secmark->ctx);
81}
82
83static int
84nftnl_obj_secmark_parse(struct nftnl_obj *e, struct nlattr *attr)
85{
86 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
87 struct nlattr *tb[NFTA_SECMARK_MAX + 1] = {};
88
89 if (mnl_attr_parse_nested(attr, nftnl_obj_secmark_cb, tb) < 0)
90 return -1;
91
92 if (tb[NFTA_SECMARK_CTX]) {
93 snprintf(secmark->ctx, sizeof(secmark->ctx), "%s",
94 mnl_attr_get_str(tb[NFTA_SECMARK_CTX]));
95 e->flags |= (1 << NFTNL_OBJ_SECMARK_CTX);
96 }
97
98 return 0;
99}
100
101static int nftnl_obj_secmark_snprintf(char *buf, size_t len,
102 uint32_t flags,
103 const struct nftnl_obj *e)
104{
105 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
106
107 return snprintf(buf, len, "context %s ", secmark->ctx);
108}
109
110struct obj_ops obj_ops_secmark = {
111 .name = "secmark",
112 .type = NFT_OBJECT_SECMARK,
113 .alloc_len = sizeof(struct nftnl_obj_secmark),
114 .max_attr = NFTA_SECMARK_MAX,
115 .set = nftnl_obj_secmark_set,
116 .get = nftnl_obj_secmark_get,
117 .parse = nftnl_obj_secmark_parse,
118 .build = nftnl_obj_secmark_build,
119 .output = nftnl_obj_secmark_snprintf,
120};