libnl 3.7.0
log_obj.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
4 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
5 * Copyright (c) 2007 Secure Computing Corporation
6 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
7 */
8
9#include <netlink-private/netlink.h>
10#include <netlink/netfilter/nfnl.h>
11#include <netlink/netfilter/log.h>
12
13/** @cond SKIP */
14#define LOG_ATTR_GROUP (1UL << 0)
15#define LOG_ATTR_COPY_MODE (1UL << 1)
16#define LOG_ATTR_COPY_RANGE (1UL << 3)
17#define LOG_ATTR_FLUSH_TIMEOUT (1UL << 4)
18#define LOG_ATTR_ALLOC_SIZE (1UL << 5)
19#define LOG_ATTR_QUEUE_THRESHOLD (1UL << 6)
20
21/** @endcond */
22
23static void nfnl_log_dump(struct nl_object *a, struct nl_dump_params *p)
24{
25 struct nfnl_log *log = (struct nfnl_log *) a;
26 char buf[64];
27
28 nl_new_line(p);
29
30 if (log->ce_mask & LOG_ATTR_GROUP)
31 nl_dump(p, "group=%u ", log->log_group);
32
33 if (log->ce_mask & LOG_ATTR_COPY_MODE)
34 nl_dump(p, "copy_mode=%s ",
35 nfnl_log_copy_mode2str(log->log_copy_mode,
36 buf, sizeof(buf)));
37
38 if (log->ce_mask & LOG_ATTR_COPY_RANGE)
39 nl_dump(p, "copy_range=%u ", log->log_copy_range);
40
41 if (log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT)
42 nl_dump(p, "flush_timeout=%u ", log->log_flush_timeout);
43
44 if (log->ce_mask & LOG_ATTR_ALLOC_SIZE)
45 nl_dump(p, "alloc_size=%u ", log->log_alloc_size);
46
47 if (log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD)
48 nl_dump(p, "queue_threshold=%u ", log->log_queue_threshold);
49
50 nl_dump(p, "\n");
51}
52
53static const struct trans_tbl copy_modes[] = {
54 __ADD(NFNL_LOG_COPY_NONE, none),
55 __ADD(NFNL_LOG_COPY_META, meta),
56 __ADD(NFNL_LOG_COPY_PACKET, packet),
57};
58
59char *nfnl_log_copy_mode2str(enum nfnl_log_copy_mode copy_mode, char *buf,
60 size_t len)
61{
62 return __type2str(copy_mode, buf, len, copy_modes,
63 ARRAY_SIZE(copy_modes));
64}
65
66int nfnl_log_str2copy_mode(const char *name)
67{
68 return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
69}
70
71/**
72 * @name Allocation/Freeing
73 * @{
74 */
75
76struct nfnl_log *nfnl_log_alloc(void)
77{
78 return (struct nfnl_log *) nl_object_alloc(&log_obj_ops);
79}
80
81void nfnl_log_get(struct nfnl_log *log)
82{
83 nl_object_get((struct nl_object *) log);
84}
85
86void nfnl_log_put(struct nfnl_log *log)
87{
88 nl_object_put((struct nl_object *) log);
89}
90
91/** @} */
92
93/**
94 * @name Attributes
95 * @{
96 */
97
98void nfnl_log_set_group(struct nfnl_log *log, uint16_t group)
99{
100 log->log_group = group;
101 log->ce_mask |= LOG_ATTR_GROUP;
102}
103
104int nfnl_log_test_group(const struct nfnl_log *log)
105{
106 return !!(log->ce_mask & LOG_ATTR_GROUP);
107}
108
109uint16_t nfnl_log_get_group(const struct nfnl_log *log)
110{
111 return log->log_group;
112}
113
114void nfnl_log_set_copy_mode(struct nfnl_log *log, enum nfnl_log_copy_mode mode)
115{
116 log->log_copy_mode = mode;
117 log->ce_mask |= LOG_ATTR_COPY_MODE;
118}
119
120int nfnl_log_test_copy_mode(const struct nfnl_log *log)
121{
122 return !!(log->ce_mask & LOG_ATTR_COPY_MODE);
123}
124
125enum nfnl_log_copy_mode nfnl_log_get_copy_mode(const struct nfnl_log *log)
126{
127 return log->log_copy_mode;
128}
129
130void nfnl_log_set_copy_range(struct nfnl_log *log, uint32_t copy_range)
131{
132 log->log_copy_range = copy_range;
133 log->ce_mask |= LOG_ATTR_COPY_RANGE;
134}
135
136int nfnl_log_test_copy_range(const struct nfnl_log *log)
137{
138 return !!(log->ce_mask & LOG_ATTR_COPY_RANGE);
139}
140
141uint32_t nfnl_log_get_copy_range(const struct nfnl_log *log)
142{
143 return log->log_copy_range;
144}
145
146void nfnl_log_set_flush_timeout(struct nfnl_log *log, uint32_t timeout)
147{
148 log->log_flush_timeout = timeout;
149 log->ce_mask |= LOG_ATTR_FLUSH_TIMEOUT;
150}
151
152int nfnl_log_test_flush_timeout(const struct nfnl_log *log)
153{
154 return !!(log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT);
155}
156
157uint32_t nfnl_log_get_flush_timeout(const struct nfnl_log *log)
158{
159 return log->log_flush_timeout;
160}
161
162void nfnl_log_set_alloc_size(struct nfnl_log *log, uint32_t alloc_size)
163{
164 log->log_alloc_size = alloc_size;
165 log->ce_mask |= LOG_ATTR_ALLOC_SIZE;
166}
167
168int nfnl_log_test_alloc_size(const struct nfnl_log *log)
169{
170 return !!(log->ce_mask & LOG_ATTR_ALLOC_SIZE);
171}
172
173uint32_t nfnl_log_get_alloc_size(const struct nfnl_log *log)
174{
175 return log->log_alloc_size;
176}
177
178void nfnl_log_set_queue_threshold(struct nfnl_log *log, uint32_t threshold)
179{
180 log->log_queue_threshold = threshold;
181 log->ce_mask |= LOG_ATTR_QUEUE_THRESHOLD;
182}
183
184int nfnl_log_test_queue_threshold(const struct nfnl_log *log)
185{
186 return !!(log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD);
187}
188
189uint32_t nfnl_log_get_queue_threshold(const struct nfnl_log *log)
190{
191 return log->log_queue_threshold;
192}
193
194/* We don't actually use the flags for anything yet since the
195 * nfnetlog_log interface truly sucks - it only contains the
196 * flag value, but not mask, so we would have to make assumptions
197 * about the supported flags.
198 */
199void nfnl_log_set_flags(struct nfnl_log *log, unsigned int flags)
200{
201 log->log_flags |= flags;
202 log->log_flag_mask |= flags;
203}
204
205void nfnl_log_unset_flags(struct nfnl_log *log, unsigned int flags)
206{
207 log->log_flags &= ~flags;
208 log->log_flag_mask |= flags;
209}
210
211unsigned int nfnl_log_get_flags(const struct nfnl_log *log)
212{
213 return log->log_flags;
214}
215
216static const struct trans_tbl log_flags[] = {
217 __ADD(NFNL_LOG_FLAG_SEQ, seq),
218 __ADD(NFNL_LOG_FLAG_SEQ_GLOBAL, seq_global),
219 __ADD(NFNL_LOG_FLAG_CONNTRACK, conntrack),
220};
221
222char *nfnl_log_flags2str(unsigned int flags, char *buf, size_t len)
223{
224 return __flags2str(flags, buf, len, log_flags, ARRAY_SIZE(log_flags));
225}
226
227unsigned int nfnl_log_str2flags(const char *name)
228{
229 return __str2flags(name, log_flags, ARRAY_SIZE(log_flags));
230}
231
232static uint64_t nfnl_log_compare(struct nl_object *_a, struct nl_object *_b,
233 uint64_t attrs, int flags)
234{
235 struct nfnl_log *a = (struct nfnl_log *) _a;
236 struct nfnl_log *b = (struct nfnl_log *) _b;
237 uint64_t diff = 0;
238
239#define NFNL_LOG_DIFF(ATTR, EXPR) \
240 ATTR_DIFF(attrs, LOG_ATTR_##ATTR, a, b, EXPR)
241#define NFNL_LOG_DIFF_VAL(ATTR, FIELD) \
242 NFNL_LOG_DIFF(ATTR, a->FIELD != b->FIELD)
243
244 diff |= NFNL_LOG_DIFF_VAL(GROUP, log_group);
245 diff |= NFNL_LOG_DIFF_VAL(COPY_MODE, log_copy_mode);
246 diff |= NFNL_LOG_DIFF_VAL(COPY_RANGE, log_copy_range);
247 diff |= NFNL_LOG_DIFF_VAL(FLUSH_TIMEOUT, log_flush_timeout);
248 diff |= NFNL_LOG_DIFF_VAL(ALLOC_SIZE, log_alloc_size);
249 diff |= NFNL_LOG_DIFF_VAL(QUEUE_THRESHOLD, log_queue_threshold);
250
251#undef NFNL_LOG_DIFF
252#undef NFNL_LOG_DIFF_VAL
253
254 return diff;
255}
256
257static const struct trans_tbl nfnl_log_attrs[] = {
258 __ADD(LOG_ATTR_GROUP, group),
259 __ADD(LOG_ATTR_COPY_MODE, copy_mode),
260 __ADD(LOG_ATTR_COPY_RANGE, copy_range),
261 __ADD(LOG_ATTR_FLUSH_TIMEOUT, flush_timeout),
262 __ADD(LOG_ATTR_ALLOC_SIZE, alloc_size),
263 __ADD(LOG_ATTR_QUEUE_THRESHOLD, queue_threshold),
264};
265
266static char *nfnl_log_attrs2str(int attrs, char *buf, size_t len)
267{
268 return __flags2str(attrs, buf, len, nfnl_log_attrs,
269 ARRAY_SIZE(nfnl_log_attrs));
270}
271
272/** @} */
273
274struct nl_object_ops log_obj_ops = {
275 .oo_name = "netfilter/log",
276 .oo_size = sizeof(struct nfnl_log),
277 .oo_dump = {
278 [NL_DUMP_LINE] = nfnl_log_dump,
279 [NL_DUMP_DETAILS] = nfnl_log_dump,
280 [NL_DUMP_STATS] = nfnl_log_dump,
281 },
282 .oo_compare = nfnl_log_compare,
283 .oo_attrs2str = nfnl_log_attrs2str,
284 .oo_id_attrs = LOG_ATTR_GROUP,
285};
286
287/** @} */
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:214
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:203
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:48
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
Definition: utils.c:906
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28