libnftnl 1.2.7
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 }
34 return 0;
35}
36
37static const void *nftnl_obj_secmark_get(const struct nftnl_obj *e,
38 uint16_t type, uint32_t *data_len)
39{
40 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
41
42 switch (type) {
43 case NFTNL_OBJ_SECMARK_CTX:
44 *data_len = strlen(secmark->ctx);
45 return secmark->ctx;
46 }
47 return NULL;
48}
49
50static int nftnl_obj_secmark_cb(const struct nlattr *attr, void *data)
51{
52 const struct nftnl_obj_secmark *secmark = NULL;
53 int type = mnl_attr_get_type(attr);
54 const struct nlattr **tb = data;
55
56 if (mnl_attr_type_valid(attr, NFTA_SECMARK_MAX) < 0)
57 return MNL_CB_OK;
58
59 switch(type) {
60 case NFTA_SECMARK_CTX:
61 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
62 abi_breakage();
63 if (mnl_attr_get_payload_len(attr) >= sizeof(secmark->ctx))
64 abi_breakage();
65 break;
66 }
67
68 tb[type] = attr;
69 return MNL_CB_OK;
70}
71
72static void
73nftnl_obj_secmark_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
74{
75 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
76
77 if (e->flags & (1 << NFTNL_OBJ_SECMARK_CTX))
78 mnl_attr_put_str(nlh, NFTA_SECMARK_CTX, secmark->ctx);
79}
80
81static int
82nftnl_obj_secmark_parse(struct nftnl_obj *e, struct nlattr *attr)
83{
84 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
85 struct nlattr *tb[NFTA_SECMARK_MAX + 1] = {};
86
87 if (mnl_attr_parse_nested(attr, nftnl_obj_secmark_cb, tb) < 0)
88 return -1;
89
90 if (tb[NFTA_SECMARK_CTX]) {
91 snprintf(secmark->ctx, sizeof(secmark->ctx), "%s",
92 mnl_attr_get_str(tb[NFTA_SECMARK_CTX]));
93 e->flags |= (1 << NFTNL_OBJ_SECMARK_CTX);
94 }
95
96 return 0;
97}
98
99static int nftnl_obj_secmark_snprintf(char *buf, size_t len,
100 uint32_t flags,
101 const struct nftnl_obj *e)
102{
103 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
104
105 return snprintf(buf, len, "context %s ", secmark->ctx);
106}
107
108static struct attr_policy obj_secmark_attr_policy[__NFTNL_OBJ_SECMARK_MAX] = {
109 [NFTNL_OBJ_SECMARK_CTX] = { .maxlen = NFT_SECMARK_CTX_MAXLEN },
110};
111
112struct obj_ops obj_ops_secmark = {
113 .name = "secmark",
114 .type = NFT_OBJECT_SECMARK,
115 .alloc_len = sizeof(struct nftnl_obj_secmark),
116 .nftnl_max_attr = __NFTNL_OBJ_SECMARK_MAX - 1,
117 .attr_policy = obj_secmark_attr_policy,
118 .set = nftnl_obj_secmark_set,
119 .get = nftnl_obj_secmark_get,
120 .parse = nftnl_obj_secmark_parse,
121 .build = nftnl_obj_secmark_build,
122 .output = nftnl_obj_secmark_snprintf,
123};