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