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