libnftnl  1.2.9
last.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * (C) 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/expr.h>
17 #include <libnftnl/rule.h>
18 
20  uint64_t msecs;
21  uint32_t set;
22 };
23 
24 static int nftnl_expr_last_set(struct nftnl_expr *e, uint16_t type,
25  const void *data, uint32_t data_len)
26 {
27  struct nftnl_expr_last *last = nftnl_expr_data(e);
28 
29  switch (type) {
30  case NFTNL_EXPR_LAST_MSECS:
31  memcpy(&last->msecs, data, data_len);
32  break;
33  case NFTNL_EXPR_LAST_SET:
34  memcpy(&last->set, data, data_len);
35  break;
36  }
37  return 0;
38 }
39 
40 static const void *nftnl_expr_last_get(const struct nftnl_expr *e,
41  uint16_t type, uint32_t *data_len)
42 {
43  struct nftnl_expr_last *last = nftnl_expr_data(e);
44 
45  switch (type) {
46  case NFTNL_EXPR_LAST_MSECS:
47  *data_len = sizeof(last->msecs);
48  return &last->msecs;
49  case NFTNL_EXPR_LAST_SET:
50  *data_len = sizeof(last->set);
51  return &last->set;
52  }
53  return NULL;
54 }
55 
56 static int nftnl_expr_last_cb(const struct nlattr *attr, void *data)
57 {
58  int type = mnl_attr_get_type(attr);
59  const struct nlattr **tb = data;
60 
61  if (mnl_attr_type_valid(attr, NFTA_LAST_MAX) < 0)
62  return MNL_CB_OK;
63 
64  switch(type) {
65  case NFTA_LAST_MSECS:
66  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
67  abi_breakage();
68  break;
69  case NFTA_LAST_SET:
70  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
71  abi_breakage();
72  break;
73  }
74 
75  tb[type] = attr;
76  return MNL_CB_OK;
77 }
78 
79 static void
80 nftnl_expr_last_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
81 {
82  struct nftnl_expr_last *last = nftnl_expr_data(e);
83 
84  if (e->flags & (1 << NFTNL_EXPR_LAST_MSECS))
85  mnl_attr_put_u64(nlh, NFTA_LAST_MSECS, htobe64(last->msecs));
86  if (e->flags & (1 << NFTNL_EXPR_LAST_SET))
87  mnl_attr_put_u32(nlh, NFTA_LAST_SET, htonl(last->set));
88 }
89 
90 static int
91 nftnl_expr_last_parse(struct nftnl_expr *e, struct nlattr *attr)
92 {
93  struct nftnl_expr_last *last = nftnl_expr_data(e);
94  struct nlattr *tb[NFTA_LAST_MAX + 1] = {};
95 
96  if (mnl_attr_parse_nested(attr, nftnl_expr_last_cb, tb) < 0)
97  return -1;
98 
99  if (tb[NFTA_LAST_MSECS]) {
100  last->msecs = be64toh(mnl_attr_get_u64(tb[NFTA_LAST_MSECS]));
101  e->flags |= (1 << NFTNL_EXPR_LAST_MSECS);
102  }
103  if (tb[NFTA_LAST_SET]) {
104  last->set = ntohl(mnl_attr_get_u32(tb[NFTA_LAST_SET]));
105  e->flags |= (1 << NFTNL_EXPR_LAST_SET);
106  }
107 
108  return 0;
109 }
110 
111 static int nftnl_expr_last_snprintf(char *buf, size_t len,
112  uint32_t flags,
113  const struct nftnl_expr *e)
114 {
115  struct nftnl_expr_last *last = nftnl_expr_data(e);
116 
117  if (!last->set)
118  return snprintf(buf, len, "never ");
119 
120  return snprintf(buf, len, "%"PRIu64" ", last->msecs);
121 }
122 
123 static struct attr_policy last_attr_policy[__NFTNL_EXPR_LAST_MAX] = {
124  [NFTNL_EXPR_LAST_MSECS] = { .maxlen = sizeof(uint64_t) },
125  [NFTNL_EXPR_LAST_SET] = { .maxlen = sizeof(uint32_t) },
126 };
127 
128 struct expr_ops expr_ops_last = {
129  .name = "last",
130  .alloc_len = sizeof(struct nftnl_expr_last),
131  .nftnl_max_attr = __NFTNL_EXPR_LAST_MAX - 1,
132  .attr_policy = last_attr_policy,
133  .set = nftnl_expr_last_set,
134  .get = nftnl_expr_last_get,
135  .parse = nftnl_expr_last_parse,
136  .build = nftnl_expr_last_build,
137  .output = nftnl_expr_last_snprintf,
138 };