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