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  */
7 
8 #include <netlink-private/netlink.h>
9 #include <netlink/netfilter/nfnl.h>
10 #include <netlink/netfilter/netfilter.h>
11 #include <netlink/netfilter/log_msg.h>
12 
13 /** @cond SKIP */
14 #define LOG_MSG_ATTR_FAMILY		(1UL << 0)
15 #define LOG_MSG_ATTR_HWPROTO		(1UL << 1)
16 #define LOG_MSG_ATTR_HOOK		(1UL << 2)
17 #define LOG_MSG_ATTR_MARK		(1UL << 3)
18 #define LOG_MSG_ATTR_TIMESTAMP		(1UL << 4)
19 #define LOG_MSG_ATTR_INDEV		(1UL << 5)
20 #define LOG_MSG_ATTR_OUTDEV		(1UL << 6)
21 #define LOG_MSG_ATTR_PHYSINDEV		(1UL << 7)
22 #define LOG_MSG_ATTR_PHYSOUTDEV		(1UL << 8)
23 #define LOG_MSG_ATTR_HWADDR		(1UL << 9)
24 #define LOG_MSG_ATTR_PAYLOAD		(1UL << 10)
25 #define LOG_MSG_ATTR_PREFIX		(1UL << 11)
26 #define LOG_MSG_ATTR_UID		(1UL << 12)
27 #define LOG_MSG_ATTR_GID		(1UL << 13)
28 #define LOG_MSG_ATTR_SEQ		(1UL << 14)
29 #define LOG_MSG_ATTR_SEQ_GLOBAL		(1UL << 15)
30 #define LOG_MSG_ATTR_HWTYPE		(1UL << 16)
31 #define LOG_MSG_ATTR_HWLEN		(1UL << 17)
32 #define LOG_MSG_ATTR_HWHEADER		(1UL << 18)
33 #define LOG_MSG_ATTR_VLAN_PROTO		(1UL << 19)
34 #define LOG_MSG_ATTR_VLAN_TAG		(1UL << 20)
35 #define LOG_MSG_ATTR_CT_INFO		(1UL << 21)
36 #define LOG_MSG_ATTR_CT			(1UL << 22)
37 /** @endcond */
38 
log_msg_free_data(struct nl_object * c)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 	free(msg->log_msg_hwheader);
49 	if (msg->log_msg_ct)
50 		nfnl_ct_put(msg->log_msg_ct);
51 }
52 
log_msg_clone(struct nl_object * _dst,struct nl_object * _src)53 static int log_msg_clone(struct nl_object *_dst, struct nl_object *_src)
54 {
55 	struct nfnl_log_msg *dst = (struct nfnl_log_msg *) _dst;
56 	struct nfnl_log_msg *src = (struct nfnl_log_msg *) _src;
57 	int err;
58 
59 	dst->log_msg_payload = NULL;
60 	dst->log_msg_payload_len = 0;
61 	dst->log_msg_prefix = NULL;
62 	dst->log_msg_hwheader = NULL;
63 	dst->log_msg_hwheader_len = 0;
64 	dst->log_msg_ct = NULL;
65 
66 	if (src->log_msg_payload) {
67 		err = nfnl_log_msg_set_payload(dst, src->log_msg_payload,
68 		                               src->log_msg_payload_len);
69 		if (err < 0)
70 			return err;
71 	}
72 
73 	if (src->log_msg_prefix) {
74 		err = nfnl_log_msg_set_prefix(dst, src->log_msg_prefix);
75 		if (err < 0)
76 			return err;
77 	}
78 
79 	if (src->log_msg_hwheader) {
80 		err = nfnl_log_msg_set_hwheader(dst, src->log_msg_hwheader,
81 		                                src->log_msg_hwheader_len);
82 		if (err < 0)
83 			return err;
84 	}
85 
86 	if (src->log_msg_ct) {
87 		dst->log_msg_ct = (struct nfnl_ct *) nl_object_clone((struct nl_object *) src->log_msg_ct);
88 		if (!dst->log_msg_ct) {
89 			return -NLE_NOMEM;
90 		}
91 	}
92 
93 	return 0;
94 }
95 
log_msg_dump(struct nl_object * a,struct nl_dump_params * p)96 static void log_msg_dump(struct nl_object *a, struct nl_dump_params *p)
97 {
98 	struct nfnl_log_msg *msg = (struct nfnl_log_msg *) a;
99 	struct nl_cache *link_cache;
100 	char buf[64];
101 
102 	link_cache = nl_cache_mngt_require_safe("route/link");
103 
104 	nl_new_line(p);
105 
106 	if (msg->ce_mask & LOG_MSG_ATTR_PREFIX)
107 		nl_dump(p, "%s", msg->log_msg_prefix);
108 
109 	if (msg->ce_mask & LOG_MSG_ATTR_INDEV) {
110 		if (link_cache)
111 			nl_dump(p, "IN=%s ",
112 				rtnl_link_i2name(link_cache,
113 						 msg->log_msg_indev,
114 						 buf, sizeof(buf)));
115 		else
116 			nl_dump(p, "IN=%d ", msg->log_msg_indev);
117 	}
118 
119 	if (msg->ce_mask & LOG_MSG_ATTR_PHYSINDEV) {
120 		if (link_cache)
121 			nl_dump(p, "PHYSIN=%s ",
122 				rtnl_link_i2name(link_cache,
123 						 msg->log_msg_physindev,
124 						 buf, sizeof(buf)));
125 		else
126 			nl_dump(p, "PHYSIN=%d ", msg->log_msg_physindev);
127 	}
128 
129 	if (msg->ce_mask & LOG_MSG_ATTR_OUTDEV) {
130 		if (link_cache)
131 			nl_dump(p, "OUT=%s ",
132 				rtnl_link_i2name(link_cache,
133 						 msg->log_msg_outdev,
134 						 buf, sizeof(buf)));
135 		else
136 			nl_dump(p, "OUT=%d ", msg->log_msg_outdev);
137 	}
138 
139 	if (msg->ce_mask & LOG_MSG_ATTR_PHYSOUTDEV) {
140 		if (link_cache)
141 			nl_dump(p, "PHYSOUT=%s ",
142 				rtnl_link_i2name(link_cache,
143 						 msg->log_msg_physoutdev,
144 						 buf, sizeof(buf)));
145 		else
146 			nl_dump(p, "PHYSOUT=%d ", msg->log_msg_physoutdev);
147 	}
148 
149 	if (msg->ce_mask & LOG_MSG_ATTR_HWADDR) {
150 		int i;
151 
152 		nl_dump(p, "MAC");
153 		for (i = 0; i < msg->log_msg_hwaddr_len; i++)
154 			nl_dump(p, "%c%02x", i?':':'=', msg->log_msg_hwaddr[i]);
155 		nl_dump(p, " ");
156 	}
157 
158 	/* FIXME: parse the payload to get iptables LOG compatible format */
159 
160 	if (msg->ce_mask & LOG_MSG_ATTR_FAMILY)
161 		nl_dump(p, "FAMILY=%s ",
162 			nl_af2str(msg->log_msg_family, buf, sizeof(buf)));
163 
164 	if (msg->ce_mask & LOG_MSG_ATTR_HWPROTO)
165 		nl_dump(p, "HWPROTO=%s ",
166 			nl_ether_proto2str(ntohs(msg->log_msg_hwproto),
167 					   buf, sizeof(buf)));
168 
169 	if (msg->ce_mask & LOG_MSG_ATTR_HOOK)
170 		nl_dump(p, "HOOK=%s ",
171 			nfnl_inet_hook2str(msg->log_msg_hook,
172 					   buf, sizeof(buf)));
173 
174 	if (msg->ce_mask & LOG_MSG_ATTR_MARK)
175 		nl_dump(p, "MARK=%u ", msg->log_msg_mark);
176 
177 	if (msg->ce_mask & LOG_MSG_ATTR_PAYLOAD)
178 		nl_dump(p, "PAYLOADLEN=%d ", msg->log_msg_payload_len);
179 
180 	if (msg->ce_mask & LOG_MSG_ATTR_UID)
181 		nl_dump(p, "UID=%u ", msg->log_msg_uid);
182 
183 	if (msg->ce_mask & LOG_MSG_ATTR_GID)
184 		nl_dump(p, "GID=%u ", msg->log_msg_gid);
185 
186 	if (msg->ce_mask & LOG_MSG_ATTR_SEQ)
187 		nl_dump(p, "SEQ=%d ", msg->log_msg_seq);
188 
189 	if (msg->ce_mask & LOG_MSG_ATTR_SEQ_GLOBAL)
190 		nl_dump(p, "SEQGLOBAL=%d ", msg->log_msg_seq_global);
191 
192 	if (msg->ce_mask & LOG_MSG_ATTR_HWTYPE)
193 		nl_dump(p, "HWTYPE=%u ", msg->log_msg_hwtype);
194 
195 	if (msg->ce_mask & LOG_MSG_ATTR_HWLEN)
196 		nl_dump(p, "HWLEN=%u ", msg->log_msg_hwlen);
197 
198 	if (msg->ce_mask & LOG_MSG_ATTR_HWHEADER) {
199 		int i;
200 
201 		nl_dump(p, "HWHEADER");
202 		for (i = 0; i < msg->log_msg_hwheader_len; i++)
203 			nl_dump(p, "%c%02x", i?':':'=', ((uint8_t*) msg->log_msg_hwheader) [i]);
204 		nl_dump(p, " ");
205 	}
206 
207 	if (msg->ce_mask & LOG_MSG_ATTR_VLAN_TAG)
208 		nl_dump(p, "VLAN=%d CFI=%d PRIO=%d",
209 		        (int) nfnl_log_msg_get_vlan_id(msg),
210 		        (int) nfnl_log_msg_get_vlan_cfi(msg),
211 		        (int) nfnl_log_msg_get_vlan_prio(msg));
212 
213 	if (msg->ce_mask & LOG_MSG_ATTR_CT_INFO)
214 		nl_dump(p, "CTINFO=%u ", msg->log_msg_ct_info);
215 
216 	nl_dump(p, "\n");
217 
218 	if (msg->ce_mask & LOG_MSG_ATTR_CT)
219 		ct_obj_ops.oo_dump[NL_DUMP_LINE]((struct nl_object *)msg->log_msg_ct, p);
220 
221 	if (link_cache)
222 		nl_cache_put(link_cache);
223 }
224 
225 /**
226  * @name Allocation/Freeing
227  * @{
228  */
229 
nfnl_log_msg_alloc(void)230 struct nfnl_log_msg *nfnl_log_msg_alloc(void)
231 {
232 	return (struct nfnl_log_msg *) nl_object_alloc(&log_msg_obj_ops);
233 }
234 
nfnl_log_msg_get(struct nfnl_log_msg * msg)235 void nfnl_log_msg_get(struct nfnl_log_msg *msg)
236 {
237 	nl_object_get((struct nl_object *) msg);
238 }
239 
nfnl_log_msg_put(struct nfnl_log_msg * msg)240 void nfnl_log_msg_put(struct nfnl_log_msg *msg)
241 {
242 	nl_object_put((struct nl_object *) msg);
243 }
244 
245 /** @} */
246 
247 /**
248  * @name Attributes
249  * @{
250  */
251 
nfnl_log_msg_set_family(struct nfnl_log_msg * msg,uint8_t family)252 void nfnl_log_msg_set_family(struct nfnl_log_msg *msg, uint8_t family)
253 {
254 	msg->log_msg_family = family;
255 	msg->ce_mask |= LOG_MSG_ATTR_FAMILY;
256 }
257 
nfnl_log_msg_get_family(const struct nfnl_log_msg * msg)258 uint8_t nfnl_log_msg_get_family(const struct nfnl_log_msg *msg)
259 {
260 	if (msg->ce_mask & LOG_MSG_ATTR_FAMILY)
261 		return msg->log_msg_family;
262 	else
263 		return AF_UNSPEC;
264 }
265 
nfnl_log_msg_set_hwproto(struct nfnl_log_msg * msg,uint16_t hwproto)266 void nfnl_log_msg_set_hwproto(struct nfnl_log_msg *msg, uint16_t hwproto)
267 {
268 	msg->log_msg_hwproto = hwproto;
269 	msg->ce_mask |= LOG_MSG_ATTR_HWPROTO;
270 }
271 
nfnl_log_msg_test_hwproto(const struct nfnl_log_msg * msg)272 int nfnl_log_msg_test_hwproto(const struct nfnl_log_msg *msg)
273 {
274 	return !!(msg->ce_mask & LOG_MSG_ATTR_HWPROTO);
275 }
276 
nfnl_log_msg_get_hwproto(const struct nfnl_log_msg * msg)277 uint16_t nfnl_log_msg_get_hwproto(const struct nfnl_log_msg *msg)
278 {
279 	return msg->log_msg_hwproto;
280 }
281 
nfnl_log_msg_set_hook(struct nfnl_log_msg * msg,uint8_t hook)282 void nfnl_log_msg_set_hook(struct nfnl_log_msg *msg, uint8_t hook)
283 {
284 	msg->log_msg_hook = hook;
285 	msg->ce_mask |= LOG_MSG_ATTR_HOOK;
286 }
287 
nfnl_log_msg_test_hook(const struct nfnl_log_msg * msg)288 int nfnl_log_msg_test_hook(const struct nfnl_log_msg *msg)
289 {
290 	return !!(msg->ce_mask & LOG_MSG_ATTR_HOOK);
291 }
292 
nfnl_log_msg_get_hook(const struct nfnl_log_msg * msg)293 uint8_t nfnl_log_msg_get_hook(const struct nfnl_log_msg *msg)
294 {
295 	return msg->log_msg_hook;
296 }
297 
nfnl_log_msg_set_mark(struct nfnl_log_msg * msg,uint32_t mark)298 void nfnl_log_msg_set_mark(struct nfnl_log_msg *msg, uint32_t mark)
299 {
300 	msg->log_msg_mark = mark;
301 	msg->ce_mask |= LOG_MSG_ATTR_MARK;
302 }
303 
nfnl_log_msg_test_mark(const struct nfnl_log_msg * msg)304 int nfnl_log_msg_test_mark(const struct nfnl_log_msg *msg)
305 {
306 	return !!(msg->ce_mask & LOG_MSG_ATTR_MARK);
307 }
308 
nfnl_log_msg_get_mark(const struct nfnl_log_msg * msg)309 uint32_t nfnl_log_msg_get_mark(const struct nfnl_log_msg *msg)
310 {
311 	return msg->log_msg_mark;
312 }
313 
nfnl_log_msg_set_timestamp(struct nfnl_log_msg * msg,struct timeval * tv)314 void nfnl_log_msg_set_timestamp(struct nfnl_log_msg *msg, struct timeval *tv)
315 {
316 	msg->log_msg_timestamp.tv_sec = tv->tv_sec;
317 	msg->log_msg_timestamp.tv_usec = tv->tv_usec;
318 	msg->ce_mask |= LOG_MSG_ATTR_TIMESTAMP;
319 }
320 
nfnl_log_msg_get_timestamp(const struct nfnl_log_msg * msg)321 const struct timeval *nfnl_log_msg_get_timestamp(const struct nfnl_log_msg *msg)
322 {
323 	if (!(msg->ce_mask & LOG_MSG_ATTR_TIMESTAMP))
324 		return NULL;
325 	return &msg->log_msg_timestamp;
326 }
327 
nfnl_log_msg_set_indev(struct nfnl_log_msg * msg,uint32_t indev)328 void nfnl_log_msg_set_indev(struct nfnl_log_msg *msg, uint32_t indev)
329 {
330 	msg->log_msg_indev = indev;
331 	msg->ce_mask |= LOG_MSG_ATTR_INDEV;
332 }
333 
nfnl_log_msg_get_indev(const struct nfnl_log_msg * msg)334 uint32_t nfnl_log_msg_get_indev(const struct nfnl_log_msg *msg)
335 {
336 	return msg->log_msg_indev;
337 }
338 
nfnl_log_msg_set_outdev(struct nfnl_log_msg * msg,uint32_t outdev)339 void nfnl_log_msg_set_outdev(struct nfnl_log_msg *msg, uint32_t outdev)
340 {
341 	msg->log_msg_outdev = outdev;
342 	msg->ce_mask |= LOG_MSG_ATTR_OUTDEV;
343 }
344 
nfnl_log_msg_get_outdev(const struct nfnl_log_msg * msg)345 uint32_t nfnl_log_msg_get_outdev(const struct nfnl_log_msg *msg)
346 {
347 	return msg->log_msg_outdev;
348 }
349 
nfnl_log_msg_set_physindev(struct nfnl_log_msg * msg,uint32_t physindev)350 void nfnl_log_msg_set_physindev(struct nfnl_log_msg *msg, uint32_t physindev)
351 {
352 	msg->log_msg_physindev = physindev;
353 	msg->ce_mask |= LOG_MSG_ATTR_PHYSINDEV;
354 }
355 
nfnl_log_msg_get_physindev(const struct nfnl_log_msg * msg)356 uint32_t nfnl_log_msg_get_physindev(const struct nfnl_log_msg *msg)
357 {
358 	return msg->log_msg_physindev;
359 }
360 
nfnl_log_msg_set_physoutdev(struct nfnl_log_msg * msg,uint32_t physoutdev)361 void nfnl_log_msg_set_physoutdev(struct nfnl_log_msg *msg, uint32_t physoutdev)
362 {
363 	msg->log_msg_physoutdev = physoutdev;
364 	msg->ce_mask |= LOG_MSG_ATTR_PHYSOUTDEV;
365 }
366 
nfnl_log_msg_get_physoutdev(const struct nfnl_log_msg * msg)367 uint32_t nfnl_log_msg_get_physoutdev(const struct nfnl_log_msg *msg)
368 {
369 	return msg->log_msg_physoutdev;
370 }
371 
nfnl_log_msg_set_hwaddr(struct nfnl_log_msg * msg,uint8_t * hwaddr,int len)372 void nfnl_log_msg_set_hwaddr(struct nfnl_log_msg *msg, uint8_t *hwaddr, int len)
373 {
374 	if (len > sizeof(msg->log_msg_hwaddr))
375 		len = sizeof(msg->log_msg_hwaddr);
376 	msg->log_msg_hwaddr_len = len;
377 	memcpy(msg->log_msg_hwaddr, hwaddr, len);
378 	msg->ce_mask |= LOG_MSG_ATTR_HWADDR;
379 }
380 
nfnl_log_msg_get_hwaddr(const struct nfnl_log_msg * msg,int * len)381 const uint8_t *nfnl_log_msg_get_hwaddr(const struct nfnl_log_msg *msg, int *len)
382 {
383 	if (!(msg->ce_mask & LOG_MSG_ATTR_HWADDR)) {
384 		*len = 0;
385 		return NULL;
386 	}
387 
388 	*len = msg->log_msg_hwaddr_len;
389 	return msg->log_msg_hwaddr;
390 }
391 
nfnl_log_msg_set_payload(struct nfnl_log_msg * msg,uint8_t * payload,int len)392 int nfnl_log_msg_set_payload(struct nfnl_log_msg *msg, uint8_t *payload, int len)
393 {
394 	uint8_t *p = NULL;
395 
396 	if (len < 0)
397 		return -NLE_INVAL;
398 
399 	p = _nl_memdup(payload, len);
400 	if (!p && len > 0)
401 		return -NLE_NOMEM;
402 
403 	free(msg->log_msg_payload);
404 	msg->log_msg_payload = p;
405 	msg->log_msg_payload_len = len;
406 	if (len > 0)
407 		msg->ce_mask |= LOG_MSG_ATTR_PAYLOAD;
408 	else
409 		msg->ce_mask &= ~LOG_MSG_ATTR_PAYLOAD;
410 	return 0;
411 }
412 
nfnl_log_msg_get_payload(const struct nfnl_log_msg * msg,int * len)413 const void *nfnl_log_msg_get_payload(const struct nfnl_log_msg *msg, int *len)
414 {
415 	if (!(msg->ce_mask & LOG_MSG_ATTR_PAYLOAD)) {
416 		*len = 0;
417 		return NULL;
418 	}
419 
420 	*len = msg->log_msg_payload_len;
421 	return msg->log_msg_payload;
422 }
423 
nfnl_log_msg_set_prefix(struct nfnl_log_msg * msg,void * prefix)424 int nfnl_log_msg_set_prefix(struct nfnl_log_msg *msg, void *prefix)
425 {
426 	char *p = NULL;
427 
428 	if (prefix) {
429 		p = strdup(prefix);
430 		if (!p)
431 			return -NLE_NOMEM;
432 	}
433 
434 	free(msg->log_msg_prefix);
435 	msg->log_msg_prefix = p;
436 
437 	if (p)
438 		msg->ce_mask |= LOG_MSG_ATTR_PREFIX;
439 	else
440 		msg->ce_mask &= ~LOG_MSG_ATTR_PREFIX;
441 	return 0;
442 }
443 
nfnl_log_msg_get_prefix(const struct nfnl_log_msg * msg)444 const char *nfnl_log_msg_get_prefix(const struct nfnl_log_msg *msg)
445 {
446 	return msg->log_msg_prefix;
447 }
448 
nfnl_log_msg_set_uid(struct nfnl_log_msg * msg,uint32_t uid)449 void nfnl_log_msg_set_uid(struct nfnl_log_msg *msg, uint32_t uid)
450 {
451 	msg->log_msg_uid = uid;
452 	msg->ce_mask |= LOG_MSG_ATTR_UID;
453 }
454 
nfnl_log_msg_test_uid(const struct nfnl_log_msg * msg)455 int nfnl_log_msg_test_uid(const struct nfnl_log_msg *msg)
456 {
457 	return !!(msg->ce_mask & LOG_MSG_ATTR_UID);
458 }
459 
nfnl_log_msg_get_uid(const struct nfnl_log_msg * msg)460 uint32_t nfnl_log_msg_get_uid(const struct nfnl_log_msg *msg)
461 {
462 	return msg->log_msg_uid;
463 }
464 
nfnl_log_msg_set_gid(struct nfnl_log_msg * msg,uint32_t gid)465 void nfnl_log_msg_set_gid(struct nfnl_log_msg *msg, uint32_t gid)
466 {
467 	msg->log_msg_gid = gid;
468 	msg->ce_mask |= LOG_MSG_ATTR_GID;
469 }
470 
nfnl_log_msg_test_gid(const struct nfnl_log_msg * msg)471 int nfnl_log_msg_test_gid(const struct nfnl_log_msg *msg)
472 {
473 	return !!(msg->ce_mask & LOG_MSG_ATTR_GID);
474 }
475 
nfnl_log_msg_get_gid(const struct nfnl_log_msg * msg)476 uint32_t nfnl_log_msg_get_gid(const struct nfnl_log_msg *msg)
477 {
478 	return msg->log_msg_gid;
479 }
480 
481 
nfnl_log_msg_set_seq(struct nfnl_log_msg * msg,uint32_t seq)482 void nfnl_log_msg_set_seq(struct nfnl_log_msg *msg, uint32_t seq)
483 {
484 	msg->log_msg_seq = seq;
485 	msg->ce_mask |= LOG_MSG_ATTR_SEQ;
486 }
487 
nfnl_log_msg_test_seq(const struct nfnl_log_msg * msg)488 int nfnl_log_msg_test_seq(const struct nfnl_log_msg *msg)
489 {
490 	return !!(msg->ce_mask & LOG_MSG_ATTR_SEQ);
491 }
492 
nfnl_log_msg_get_seq(const struct nfnl_log_msg * msg)493 uint32_t nfnl_log_msg_get_seq(const struct nfnl_log_msg *msg)
494 {
495 	return msg->log_msg_seq;
496 }
497 
nfnl_log_msg_set_seq_global(struct nfnl_log_msg * msg,uint32_t seq_global)498 void nfnl_log_msg_set_seq_global(struct nfnl_log_msg *msg, uint32_t seq_global)
499 {
500 	msg->log_msg_seq_global = seq_global;
501 	msg->ce_mask |= LOG_MSG_ATTR_SEQ_GLOBAL;
502 }
503 
nfnl_log_msg_test_seq_global(const struct nfnl_log_msg * msg)504 int nfnl_log_msg_test_seq_global(const struct nfnl_log_msg *msg)
505 {
506 	return !!(msg->ce_mask & LOG_MSG_ATTR_SEQ_GLOBAL);
507 }
508 
nfnl_log_msg_get_seq_global(const struct nfnl_log_msg * msg)509 uint32_t nfnl_log_msg_get_seq_global(const struct nfnl_log_msg *msg)
510 {
511 	return msg->log_msg_seq_global;
512 }
513 
nfnl_log_msg_set_hwtype(struct nfnl_log_msg * msg,uint16_t hwtype)514 void nfnl_log_msg_set_hwtype(struct nfnl_log_msg *msg, uint16_t hwtype)
515 {
516 	msg->log_msg_hwtype = hwtype;
517 	msg->ce_mask |= LOG_MSG_ATTR_HWTYPE;
518 }
519 
nfnl_log_msg_test_hwtype(const struct nfnl_log_msg * msg)520 int nfnl_log_msg_test_hwtype(const struct nfnl_log_msg *msg)
521 {
522 	return !!(msg->ce_mask & LOG_MSG_ATTR_HWTYPE);
523 }
524 
nfnl_log_msg_get_hwtype(const struct nfnl_log_msg * msg)525 uint16_t nfnl_log_msg_get_hwtype(const struct nfnl_log_msg *msg)
526 {
527 	return msg->log_msg_hwtype;
528 }
529 
nfnl_log_msg_set_hwlen(struct nfnl_log_msg * msg,uint16_t hwlen)530 void nfnl_log_msg_set_hwlen(struct nfnl_log_msg *msg, uint16_t hwlen)
531 {
532 	msg->log_msg_hwlen = hwlen;
533 	msg->ce_mask |= LOG_MSG_ATTR_HWLEN;
534 }
535 
nfnl_log_msg_test_hwlen(const struct nfnl_log_msg * msg)536 int nfnl_log_msg_test_hwlen(const struct nfnl_log_msg *msg)
537 {
538 	return !!(msg->ce_mask & LOG_MSG_ATTR_HWLEN);
539 }
540 
nfnl_log_msg_get_hwlen(const struct nfnl_log_msg * msg)541 uint16_t nfnl_log_msg_get_hwlen(const struct nfnl_log_msg *msg)
542 {
543 	return msg->log_msg_hwlen;
544 }
545 
nfnl_log_msg_set_hwheader(struct nfnl_log_msg * msg,void * data,int len)546 int nfnl_log_msg_set_hwheader(struct nfnl_log_msg *msg, void *data, int len)
547 {
548 	void *p = NULL;
549 
550 	if (len < 0)
551 		return -NLE_INVAL;
552 
553 	p = _nl_memdup(data, len);
554 	if (!p && len > 0)
555 		return -NLE_NOMEM;
556 
557 	free(msg->log_msg_hwheader);
558 	msg->log_msg_hwheader = p;
559 	msg->log_msg_hwheader_len = len;
560 	if (len > 0)
561 		msg->ce_mask |= LOG_MSG_ATTR_HWHEADER;
562 	else
563 		msg->ce_mask &= ~LOG_MSG_ATTR_HWHEADER;
564 	return 0;
565 }
566 
nfnl_log_msg_test_hwheader(const struct nfnl_log_msg * msg)567 int nfnl_log_msg_test_hwheader(const struct nfnl_log_msg *msg)
568 {
569 	return !!(msg->ce_mask & LOG_MSG_ATTR_HWHEADER);
570 }
571 
nfnl_log_msg_get_hwheader(const struct nfnl_log_msg * msg,int * len)572 const void *nfnl_log_msg_get_hwheader(const struct nfnl_log_msg *msg, int *len)
573 {
574 	if (!(msg->ce_mask & LOG_MSG_ATTR_HWHEADER)) {
575 		*len = 0;
576 		return NULL;
577 	}
578 
579 	*len = msg->log_msg_hwheader_len;
580 	return msg->log_msg_hwheader;
581 }
582 
nfnl_log_msg_set_vlan_proto(struct nfnl_log_msg * msg,uint16_t vlan_proto)583 void nfnl_log_msg_set_vlan_proto(struct nfnl_log_msg *msg, uint16_t vlan_proto)
584 {
585 	msg->log_msg_vlan_proto = vlan_proto;
586 	msg->ce_mask |= LOG_MSG_ATTR_VLAN_PROTO;
587 }
588 
nfnl_log_msg_test_vlan_proto(const struct nfnl_log_msg * msg)589 int nfnl_log_msg_test_vlan_proto(const struct nfnl_log_msg *msg)
590 {
591 	return !!(msg->ce_mask & LOG_MSG_ATTR_VLAN_PROTO);
592 }
593 
nfnl_log_msg_get_vlan_proto(const struct nfnl_log_msg * msg)594 uint16_t nfnl_log_msg_get_vlan_proto(const struct nfnl_log_msg *msg)
595 {
596 	return msg->log_msg_vlan_proto;
597 }
598 
nfnl_log_msg_set_vlan_tag(struct nfnl_log_msg * msg,uint16_t vlan_tag)599 void nfnl_log_msg_set_vlan_tag(struct nfnl_log_msg *msg, uint16_t vlan_tag)
600 {
601 	msg->log_msg_vlan_tag = vlan_tag;
602 	msg->ce_mask |= LOG_MSG_ATTR_VLAN_TAG;
603 }
604 
nfnl_log_msg_test_vlan_tag(const struct nfnl_log_msg * msg)605 int nfnl_log_msg_test_vlan_tag(const struct nfnl_log_msg *msg)
606 {
607 	return !!(msg->ce_mask & LOG_MSG_ATTR_VLAN_TAG);
608 }
609 
nfnl_log_msg_get_vlan_tag(const struct nfnl_log_msg * msg)610 uint16_t nfnl_log_msg_get_vlan_tag(const struct nfnl_log_msg *msg)
611 {
612 	return msg->log_msg_vlan_tag;
613 }
614 
nfnl_log_msg_get_vlan_id(const struct nfnl_log_msg * msg)615 uint16_t nfnl_log_msg_get_vlan_id(const struct nfnl_log_msg *msg)
616 {
617 	return msg->log_msg_vlan_tag & 0x0fff;
618 }
619 
nfnl_log_msg_get_vlan_cfi(const struct nfnl_log_msg * msg)620 uint16_t nfnl_log_msg_get_vlan_cfi(const struct nfnl_log_msg *msg)
621 {
622 	return !!(msg->log_msg_vlan_tag & 0x1000);
623 }
624 
nfnl_log_msg_get_vlan_prio(const struct nfnl_log_msg * msg)625 uint16_t nfnl_log_msg_get_vlan_prio(const struct nfnl_log_msg *msg)
626 {
627 	return (msg->log_msg_vlan_tag & 0xe000 ) >> 13;
628 }
629 
nfnl_log_msg_set_ct_info(struct nfnl_log_msg * msg,uint32_t ct_info)630 void nfnl_log_msg_set_ct_info(struct nfnl_log_msg *msg, uint32_t ct_info)
631 {
632 	msg->log_msg_ct_info = ct_info;
633 	msg->ce_mask |= LOG_MSG_ATTR_CT_INFO;
634 }
635 
nfnl_log_msg_test_ct_info(const struct nfnl_log_msg * msg)636 int nfnl_log_msg_test_ct_info(const struct nfnl_log_msg *msg)
637 {
638 	return !!(msg->ce_mask & LOG_MSG_ATTR_CT_INFO);
639 }
640 
nfnl_log_msg_get_ct_info(const struct nfnl_log_msg * msg)641 uint32_t nfnl_log_msg_get_ct_info(const struct nfnl_log_msg *msg)
642 {
643 	return msg->log_msg_ct_info;
644 }
645 
nfnl_log_msg_set_ct(struct nfnl_log_msg * msg,struct nfnl_ct * ct)646 void nfnl_log_msg_set_ct(struct nfnl_log_msg *msg, struct nfnl_ct *ct)
647 {
648 	msg->log_msg_ct = (struct nfnl_ct *) nl_object_clone((struct nl_object *)ct);
649 	msg->ce_mask |= LOG_MSG_ATTR_CT;
650 }
651 
nfnl_log_msg_test_ct(const struct nfnl_log_msg * msg)652 int nfnl_log_msg_test_ct(const struct nfnl_log_msg *msg)
653 {
654 	return !!(msg->ce_mask & LOG_MSG_ATTR_CT);
655 }
656 
nfnl_log_msg_get_ct(const struct nfnl_log_msg * msg)657 struct nfnl_ct *nfnl_log_msg_get_ct(const struct nfnl_log_msg *msg)
658 {
659 	return msg->log_msg_ct;
660 }
661 
662 /** @} */
663 
664 struct nl_object_ops log_msg_obj_ops = {
665 	.oo_name		= "netfilter/log_msg",
666 	.oo_size		= sizeof(struct nfnl_log_msg),
667 	.oo_free_data		= log_msg_free_data,
668 	.oo_clone		= log_msg_clone,
669 	.oo_dump = {
670 	    [NL_DUMP_LINE]	= log_msg_dump,
671 	    [NL_DUMP_DETAILS]	= log_msg_dump,
672 	    [NL_DUMP_STATS]	= log_msg_dump,
673 	},
674 };
675 
676 /** @} */
677