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