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