libnl 3.5.0

log_msg_obj.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * lib/netfilter/log_msg_obj.c Netfilter Log Object
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
11 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
12 * Copyright (c) 2007 Secure Computing Corporation
13 */
14
15 #include <netlink-private/netlink.h>
16 #include <netlink/netfilter/nfnl.h>
17 #include <netlink/netfilter/netfilter.h>
18 #include <netlink/netfilter/log_msg.h>
19
20 /** @cond SKIP */
21 #define LOG_MSG_ATTR_FAMILY (1UL << 0)
22 #define LOG_MSG_ATTR_HWPROTO (1UL << 1)
23 #define LOG_MSG_ATTR_HOOK (1UL << 2)
24 #define LOG_MSG_ATTR_MARK (1UL << 3)
25 #define LOG_MSG_ATTR_TIMESTAMP (1UL << 4)
26 #define LOG_MSG_ATTR_INDEV (1UL << 5)
27 #define LOG_MSG_ATTR_OUTDEV (1UL << 6)
28 #define LOG_MSG_ATTR_PHYSINDEV (1UL << 7)
29 #define LOG_MSG_ATTR_PHYSOUTDEV (1UL << 8)
30 #define LOG_MSG_ATTR_HWADDR (1UL << 9)
31 #define LOG_MSG_ATTR_PAYLOAD (1UL << 10)
32 #define LOG_MSG_ATTR_PREFIX (1UL << 11)
33 #define LOG_MSG_ATTR_UID (1UL << 12)
34 #define LOG_MSG_ATTR_GID (1UL << 13)
35 #define LOG_MSG_ATTR_SEQ (1UL << 14)
36 #define LOG_MSG_ATTR_SEQ_GLOBAL (1UL << 15)
37 /** @endcond */
38
39 static void log_msg_free_data(struct nl_object *c)
40 {
41 struct nfnl_log_msg *msg = (struct nfnl_log_msg *) c;
42
43 if (msg == NULL)
44 return;
45
46 free(msg->log_msg_payload);
47 free(msg->log_msg_prefix);
48 }
49
50 static int log_msg_clone(struct nl_object *_dst, struct nl_object *_src)
51 {
52 struct nfnl_log_msg *dst = (struct nfnl_log_msg *) _dst;
53 struct nfnl_log_msg *src = (struct nfnl_log_msg *) _src;
54 int err;
55
56 if (src->log_msg_payload) {
57 err = nfnl_log_msg_set_payload(dst, src->log_msg_payload,
58 src->log_msg_payload_len);
59 if (err < 0)
60 goto errout;
61 }
62
63 if (src->log_msg_prefix) {
64 err = nfnl_log_msg_set_prefix(dst, src->log_msg_prefix);
65 if (err < 0)
66 goto errout;
67 }
68
69 return 0;
70 errout:
71 return err;
72 }
73
74 static void log_msg_dump(struct nl_object *a, struct nl_dump_params *p)
75 {
76 struct nfnl_log_msg *msg = (struct nfnl_log_msg *) a;
77 struct nl_cache *link_cache;
78 char buf[64];
79
80 link_cache = nl_cache_mngt_require_safe("route/link");
81
82 nl_new_line(p);
83
84 if (msg->ce_mask & LOG_MSG_ATTR_PREFIX)
85 nl_dump(p, "%s", msg->log_msg_prefix);
86
87 if (msg->ce_mask & LOG_MSG_ATTR_INDEV) {
88 if (link_cache)
89 nl_dump(p, "IN=%s ",
90 rtnl_link_i2name(link_cache,
91 msg->log_msg_indev,
92 buf, sizeof(buf)));
93 else
94 nl_dump(p, "IN=%d ", msg->log_msg_indev);
95 }
96
97 if (msg->ce_mask & LOG_MSG_ATTR_PHYSINDEV) {
98 if (link_cache)
99 nl_dump(p, "PHYSIN=%s ",
100 rtnl_link_i2name(link_cache,
101 msg->log_msg_physindev,
102 buf, sizeof(buf)));
103 else
104 nl_dump(p, "IN=%d ", msg->log_msg_physindev);
105 }
106
107 if (msg->ce_mask & LOG_MSG_ATTR_OUTDEV) {
108 if (link_cache)
109 nl_dump(p, "OUT=%s ",
110 rtnl_link_i2name(link_cache,
111 msg->log_msg_outdev,
112 buf, sizeof(buf)));
113 else
114 nl_dump(p, "OUT=%d ", msg->log_msg_outdev);
115 }
116
117 if (msg->ce_mask & LOG_MSG_ATTR_PHYSOUTDEV) {
118 if (link_cache)
119 nl_dump(p, "PHYSOUT=%s ",
120 rtnl_link_i2name(link_cache,
121 msg->log_msg_physoutdev,
122 buf, sizeof(buf)));
123 else
124 nl_dump(p, "PHYSOUT=%d ", msg->log_msg_physoutdev);
125 }
126
127 if (msg->ce_mask & LOG_MSG_ATTR_HWADDR) {
128 int i;
129
130 nl_dump(p, "MAC");
131 for (i = 0; i < msg->log_msg_hwaddr_len; i++)
132 nl_dump(p, "%c%02x", i?':':'=', msg->log_msg_hwaddr[i]);
133 nl_dump(p, " ");
134 }
135
136 /* FIXME: parse the payload to get iptables LOG compatible format */
137
138 if (msg->ce_mask & LOG_MSG_ATTR_FAMILY)
139 nl_dump(p, "FAMILY=%s ",
140 nl_af2str(msg->log_msg_family, buf, sizeof(buf)));
141
142 if (msg->ce_mask & LOG_MSG_ATTR_HWPROTO)
143 nl_dump(p, "HWPROTO=%s ",
144 nl_ether_proto2str(ntohs(msg->log_msg_hwproto),
145 buf, sizeof(buf)));
146
147 if (msg->ce_mask & LOG_MSG_ATTR_HOOK)
148 nl_dump(p, "HOOK=%s ",
149 nfnl_inet_hook2str(msg->log_msg_hook,
150 buf, sizeof(buf)));
151
152 if (msg->ce_mask & LOG_MSG_ATTR_MARK)
153 nl_dump(p, "MARK=%u ", msg->log_msg_mark);
154
155 if (msg->ce_mask & LOG_MSG_ATTR_PAYLOAD)
156 nl_dump(p, "PAYLOADLEN=%d ", msg->log_msg_payload_len);
157
158 if (msg->ce_mask & LOG_MSG_ATTR_UID)
159 nl_dump(p, "UID=%u ", msg->log_msg_uid);
160
161 if (msg->ce_mask & LOG_MSG_ATTR_GID)
162 nl_dump(p, "GID=%u ", msg->log_msg_gid);
163
164 if (msg->ce_mask & LOG_MSG_ATTR_SEQ)
165 nl_dump(p, "SEQ=%d ", msg->log_msg_seq);
166
167 if (msg->ce_mask & LOG_MSG_ATTR_SEQ_GLOBAL)
168 nl_dump(p, "SEQGLOBAL=%d ", msg->log_msg_seq_global);
169
170 nl_dump(p, "\n");
171
172 if (link_cache)
173 nl_cache_put(link_cache);
174 }
175
176 /**
177 * @name Allocation/Freeing
178 * @{
179 */
180
181 struct nfnl_log_msg *nfnl_log_msg_alloc(void)
182 {
183 return (struct nfnl_log_msg *) nl_object_alloc(&log_msg_obj_ops);
184 }
185
186 void nfnl_log_msg_get(struct nfnl_log_msg *msg)
187 {
188 nl_object_get((struct nl_object *) msg);
189 }
190
191 void nfnl_log_msg_put(struct nfnl_log_msg *msg)
192 {
193 nl_object_put((struct nl_object *) msg);
194 }
195
196 /** @} */
197
198 /**
199 * @name Attributes
200 * @{
201 */
202
203 void nfnl_log_msg_set_family(struct nfnl_log_msg *msg, uint8_t family)
204 {
205 msg->log_msg_family = family;
206 msg->ce_mask |= LOG_MSG_ATTR_FAMILY;
207 }
208
209 uint8_t nfnl_log_msg_get_family(const struct nfnl_log_msg *msg)
210 {
211 if (msg->ce_mask & LOG_MSG_ATTR_FAMILY)
212 return msg->log_msg_family;
213 else
214 return AF_UNSPEC;
215 }
216
217 void nfnl_log_msg_set_hwproto(struct nfnl_log_msg *msg, uint16_t hwproto)
218 {
219 msg->log_msg_hwproto = hwproto;
220 msg->ce_mask |= LOG_MSG_ATTR_HWPROTO;
221 }
222
223 int nfnl_log_msg_test_hwproto(const struct nfnl_log_msg *msg)
224 {
225 return !!(msg->ce_mask & LOG_MSG_ATTR_HWPROTO);
226 }
227
228 uint16_t nfnl_log_msg_get_hwproto(const struct nfnl_log_msg *msg)
229 {
230 return msg->log_msg_hwproto;
231 }
232
233 void nfnl_log_msg_set_hook(struct nfnl_log_msg *msg, uint8_t hook)
234 {
235 msg->log_msg_hook = hook;
236 msg->ce_mask |= LOG_MSG_ATTR_HOOK;
237 }
238
239 int nfnl_log_msg_test_hook(const struct nfnl_log_msg *msg)
240 {
241 return !!(msg->ce_mask & LOG_MSG_ATTR_HOOK);
242 }
243
244 uint8_t nfnl_log_msg_get_hook(const struct nfnl_log_msg *msg)
245 {
246 return msg->log_msg_hook;
247 }
248
249 void nfnl_log_msg_set_mark(struct nfnl_log_msg *msg, uint32_t mark)
250 {
251 msg->log_msg_mark = mark;
252 msg->ce_mask |= LOG_MSG_ATTR_MARK;
253 }
254
255 int nfnl_log_msg_test_mark(const struct nfnl_log_msg *msg)
256 {
257 return !!(msg->ce_mask & LOG_MSG_ATTR_MARK);
258 }
259
260 uint32_t nfnl_log_msg_get_mark(const struct nfnl_log_msg *msg)
261 {
262 return msg->log_msg_mark;
263 }
264
265 void nfnl_log_msg_set_timestamp(struct nfnl_log_msg *msg, struct timeval *tv)
266 {
267 msg->log_msg_timestamp.tv_sec = tv->tv_sec;
268 msg->log_msg_timestamp.tv_usec = tv->tv_usec;
269 msg->ce_mask |= LOG_MSG_ATTR_TIMESTAMP;
270 }
271
272 const struct timeval *nfnl_log_msg_get_timestamp(const struct nfnl_log_msg *msg)
273 {
274 if (!(msg->ce_mask & LOG_MSG_ATTR_TIMESTAMP))
275 return NULL;
276 return &msg->log_msg_timestamp;
277 }
278
279 void nfnl_log_msg_set_indev(struct nfnl_log_msg *msg, uint32_t indev)
280 {
281 msg->log_msg_indev = indev;
282 msg->ce_mask |= LOG_MSG_ATTR_INDEV;
283 }
284
285 uint32_t nfnl_log_msg_get_indev(const struct nfnl_log_msg *msg)
286 {
287 return msg->log_msg_indev;
288 }
289
290 void nfnl_log_msg_set_outdev(struct nfnl_log_msg *msg, uint32_t outdev)
291 {
292 msg->log_msg_outdev = outdev;
293 msg->ce_mask |= LOG_MSG_ATTR_OUTDEV;
294 }
295
296 uint32_t nfnl_log_msg_get_outdev(const struct nfnl_log_msg *msg)
297 {
298 return msg->log_msg_outdev;
299 }
300
301 void nfnl_log_msg_set_physindev(struct nfnl_log_msg *msg, uint32_t physindev)
302 {
303 msg->log_msg_physindev = physindev;
304 msg->ce_mask |= LOG_MSG_ATTR_PHYSINDEV;
305 }
306
307 uint32_t nfnl_log_msg_get_physindev(const struct nfnl_log_msg *msg)
308 {
309 return msg->log_msg_physindev;
310 }
311
312 void nfnl_log_msg_set_physoutdev(struct nfnl_log_msg *msg, uint32_t physoutdev)
313 {
314 msg->log_msg_physoutdev = physoutdev;
315 msg->ce_mask |= LOG_MSG_ATTR_PHYSOUTDEV;
316 }
317
318 uint32_t nfnl_log_msg_get_physoutdev(const struct nfnl_log_msg *msg)
319 {
320 return msg->log_msg_physoutdev;
321 }
322
323 void nfnl_log_msg_set_hwaddr(struct nfnl_log_msg *msg, uint8_t *hwaddr, int len)
324 {
325 if (len > sizeof(msg->log_msg_hwaddr))
326 len = sizeof(msg->log_msg_hwaddr);
327 msg->log_msg_hwaddr_len = len;
328 memcpy(msg->log_msg_hwaddr, hwaddr, len);
329 msg->ce_mask |= LOG_MSG_ATTR_HWADDR;
330 }
331
332 const uint8_t *nfnl_log_msg_get_hwaddr(const struct nfnl_log_msg *msg, int *len)
333 {
334 if (!(msg->ce_mask & LOG_MSG_ATTR_HWADDR)) {
335 *len = 0;
336 return NULL;
337 }
338
339 *len = msg->log_msg_hwaddr_len;
340 return msg->log_msg_hwaddr;
341 }
342
343 int nfnl_log_msg_set_payload(struct nfnl_log_msg *msg, uint8_t *payload, int len)
344 {
345 free(msg->log_msg_payload);
346 msg->log_msg_payload = malloc(len);
347 if (!msg->log_msg_payload)
348 return -NLE_NOMEM;
349
350 memcpy(msg->log_msg_payload, payload, len);
351 msg->log_msg_payload_len = len;
352 msg->ce_mask |= LOG_MSG_ATTR_PAYLOAD;
353 return 0;
354 }
355
356 const void *nfnl_log_msg_get_payload(const struct nfnl_log_msg *msg, int *len)
357 {
358 if (!(msg->ce_mask & LOG_MSG_ATTR_PAYLOAD)) {
359 *len = 0;
360 return NULL;
361 }
362
363 *len = msg->log_msg_payload_len;
364 return msg->log_msg_payload;
365 }
366
367 int nfnl_log_msg_set_prefix(struct nfnl_log_msg *msg, void *prefix)
368 {
369 free(msg->log_msg_prefix);
370 msg->log_msg_prefix = strdup(prefix);
371 if (!msg->log_msg_prefix)
372 return -NLE_NOMEM;
373
374 msg->ce_mask |= LOG_MSG_ATTR_PREFIX;
375 return 0;
376 }
377
378 const char *nfnl_log_msg_get_prefix(const struct nfnl_log_msg *msg)
379 {
380 return msg->log_msg_prefix;
381 }
382
383 void nfnl_log_msg_set_uid(struct nfnl_log_msg *msg, uint32_t uid)
384 {
385 msg->log_msg_uid = uid;
386 msg->ce_mask |= LOG_MSG_ATTR_UID;
387 }
388
389 int nfnl_log_msg_test_uid(const struct nfnl_log_msg *msg)
390 {
391 return !!(msg->ce_mask & LOG_MSG_ATTR_UID);
392 }
393
394 uint32_t nfnl_log_msg_get_uid(const struct nfnl_log_msg *msg)
395 {
396 return msg->log_msg_uid;
397 }
398
399 void nfnl_log_msg_set_gid(struct nfnl_log_msg *msg, uint32_t gid)
400 {
401 msg->log_msg_gid = gid;
402 msg->ce_mask |= LOG_MSG_ATTR_GID;
403 }
404
405 int nfnl_log_msg_test_gid(const struct nfnl_log_msg *msg)
406 {
407 return !!(msg->ce_mask & LOG_MSG_ATTR_GID);
408 }
409
410 uint32_t nfnl_log_msg_get_gid(const struct nfnl_log_msg *msg)
411 {
412 return msg->log_msg_gid;
413 }
414
415
416 void nfnl_log_msg_set_seq(struct nfnl_log_msg *msg, uint32_t seq)
417 {
418 msg->log_msg_seq = seq;
419 msg->ce_mask |= LOG_MSG_ATTR_SEQ;
420 }
421
422 int nfnl_log_msg_test_seq(const struct nfnl_log_msg *msg)
423 {
424 return !!(msg->ce_mask & LOG_MSG_ATTR_SEQ);
425 }
426
427 uint32_t nfnl_log_msg_get_seq(const struct nfnl_log_msg *msg)
428 {
429 return msg->log_msg_seq;
430 }
431
432 void nfnl_log_msg_set_seq_global(struct nfnl_log_msg *msg, uint32_t seq_global)
433 {
434 msg->log_msg_seq_global = seq_global;
435 msg->ce_mask |= LOG_MSG_ATTR_SEQ_GLOBAL;
436 }
437
438 int nfnl_log_msg_test_seq_global(const struct nfnl_log_msg *msg)
439 {
440 return !!(msg->ce_mask & LOG_MSG_ATTR_SEQ_GLOBAL);
441 }
442
443 uint32_t nfnl_log_msg_get_seq_global(const struct nfnl_log_msg *msg)
444 {
445 return msg->log_msg_seq_global;
446 }
447
448 /** @} */
449
450 struct nl_object_ops log_msg_obj_ops = {
451 .oo_name = "netfilter/log_msg",
452 .oo_size = sizeof(struct nfnl_log_msg),
453 .oo_free_data = log_msg_free_data,
454 .oo_clone = log_msg_clone,
455 .oo_dump = {
456 [NL_DUMP_LINE] = log_msg_dump,
457 [NL_DUMP_DETAILS] = log_msg_dump,
458 [NL_DUMP_STATS] = log_msg_dump,
459 },
460 };
461
462 /** @} */
NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:22
nl_new_line
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
Definition: utils.c:913
nl_object_alloc
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:55
nl_cache_mngt_require_safe
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
Definition: cache_mngt.c:431
nl_object_get
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:205
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
nl_object_put
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:216
rtnl_link_i2name
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
Definition: link.c:1484
nl_dump_params
Dumping parameters.
Definition: types.h:33
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:962
NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:24