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