2 * lib/netfilter/log_obj.c Netfilter Log Object
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11 * Copyright (c) 2007 Secure Computing Corporation
14 #include <netlink-local.h>
15 #include <netlink/netfilter/nfnl.h>
16 #include <netlink/netfilter/log.h>
19 #define LOG_ATTR_FAMILY (1UL << 0)
20 #define LOG_ATTR_HWPROTO (1UL << 1)
21 #define LOG_ATTR_HOOK (1UL << 2)
22 #define LOG_ATTR_MARK (1UL << 3)
23 #define LOG_ATTR_TIMESTAMP (1UL << 4)
24 #define LOG_ATTR_INDEV (1UL << 5)
25 #define LOG_ATTR_OUTDEV (1UL << 6)
26 #define LOG_ATTR_PHYSINDEV (1UL << 7)
27 #define LOG_ATTR_PHYSOUTDEV (1UL << 8)
28 #define LOG_ATTR_HWADDR (1UL << 9)
29 #define LOG_ATTR_PAYLOAD (1UL << 10)
30 #define LOG_ATTR_PREFIX (1UL << 11)
31 #define LOG_ATTR_UID (1UL << 12)
32 #define LOG_ATTR_SEQ (1UL << 13)
33 #define LOG_ATTR_SEQ_GLOBAL (1UL << 14)
36 static void log_free_data(struct nl_object *c)
38 struct nfnl_log *log = (struct nfnl_log *) c;
43 free(log->log_payload);
44 free(log->log_prefix);
47 static int log_clone(struct nl_object *_dst, struct nl_object *_src)
49 struct nfnl_log *dst = (struct nfnl_log *) _dst;
50 struct nfnl_log *src = (struct nfnl_log *) _src;
53 if (src->log_payload) {
54 err = nfnl_log_set_payload(dst, src->log_payload,
55 src->log_payload_len);
60 if (src->log_prefix) {
61 err = nfnl_log_set_prefix(dst, src->log_prefix);
71 static int log_dump(struct nl_object *a, struct nl_dump_params *p)
73 struct nfnl_log *log = (struct nfnl_log *) a;
74 struct nl_cache *link_cache;
77 link_cache = nl_cache_mngt_require("route/link");
79 if (log->ce_mask & LOG_ATTR_PREFIX)
80 dp_dump(p, "%s", log->log_prefix);
82 if (log->ce_mask & LOG_ATTR_INDEV) {
85 rtnl_link_i2name(link_cache, log->log_indev,
88 dp_dump(p, "IN=%d ", log->log_indev);
91 if (log->ce_mask & LOG_ATTR_PHYSINDEV) {
93 dp_dump(p, "PHYSIN=%s ",
94 rtnl_link_i2name(link_cache, log->log_physindev,
97 dp_dump(p, "IN=%d ", log->log_physindev);
100 if (log->ce_mask & LOG_ATTR_OUTDEV) {
102 dp_dump(p, "OUT=%s ",
103 rtnl_link_i2name(link_cache, log->log_outdev,
106 dp_dump(p, "OUT=%d ", log->log_outdev);
109 if (log->ce_mask & LOG_ATTR_PHYSOUTDEV) {
111 dp_dump(p, "PHYSOUT=%s ",
112 rtnl_link_i2name(link_cache,log->log_physoutdev,
115 dp_dump(p, "PHYSOUT=%d ", log->log_physoutdev);
118 if (log->ce_mask & LOG_ATTR_HWADDR) {
122 for (i = 0; i < log->log_hwaddr_len; i++)
123 dp_dump(p, "%c%02x", i?':':'=', log->log_hwaddr[i]);
127 /* FIXME: parse the payload to get iptables LOG compatible format */
129 if (log->ce_mask & LOG_ATTR_FAMILY)
130 dp_dump(p, "FAMILY=%s ",
131 nl_af2str(log->log_family, buf, sizeof(buf)));
133 if (log->ce_mask & LOG_ATTR_HWPROTO)
134 dp_dump(p, "HWPROTO=%s ",
135 nl_ether_proto2str(ntohs(log->log_hwproto),
138 if (log->ce_mask & LOG_ATTR_HOOK)
139 dp_dump(p, "HOOK=%d ", log->log_hook);
141 if (log->ce_mask & LOG_ATTR_MARK)
142 dp_dump(p, "MARK=%d ", log->log_mark);
144 if (log->ce_mask & LOG_ATTR_PAYLOAD)
145 dp_dump(p, "PAYLOADLEN=%d ", log->log_payload_len);
147 if (log->ce_mask & LOG_ATTR_SEQ)
148 dp_dump(p, "SEQ=%d ", log->log_seq);
150 if (log->ce_mask & LOG_ATTR_SEQ_GLOBAL)
151 dp_dump(p, "SEQGLOBAL=%d ", log->log_seq_global);
159 * @name Allocation/Freeing
163 struct nfnl_log *nfnl_log_alloc(void)
165 return (struct nfnl_log *) nl_object_alloc(&log_obj_ops);
168 void nfnl_log_get(struct nfnl_log *log)
170 nl_object_get((struct nl_object *) log);
173 void nfnl_log_put(struct nfnl_log *log)
175 nl_object_put((struct nl_object *) log);
185 void nfnl_log_set_family(struct nfnl_log *log, uint8_t family)
187 log->log_family = family;
188 log->ce_mask |= LOG_ATTR_FAMILY;
191 uint8_t nfnl_log_get_family(const struct nfnl_log *log)
193 if (log->ce_mask & LOG_ATTR_FAMILY)
194 return log->log_family;
199 void nfnl_log_set_hwproto(struct nfnl_log *log, uint16_t hwproto)
201 log->log_hwproto = hwproto;
202 log->ce_mask |= LOG_ATTR_HWPROTO;
205 int nfnl_log_test_hwproto(const struct nfnl_log *log)
207 return !!(log->ce_mask & LOG_ATTR_HWPROTO);
210 uint16_t nfnl_log_get_hwproto(const struct nfnl_log *log)
212 return log->log_hwproto;
215 void nfnl_log_set_hook(struct nfnl_log *log, uint8_t hook)
217 log->log_hook = hook;
218 log->ce_mask |= LOG_ATTR_HOOK;
221 int nfnl_log_test_hook(const struct nfnl_log *log)
223 return !!(log->ce_mask & LOG_ATTR_HOOK);
226 uint8_t nfnl_log_get_hook(const struct nfnl_log *log)
228 return log->log_hook;
231 void nfnl_log_set_mark(struct nfnl_log *log, uint32_t mark)
233 log->log_mark = mark;
234 log->ce_mask |= LOG_ATTR_MARK;
237 int nfnl_log_test_mark(const struct nfnl_log *log)
239 return !!(log->ce_mask & LOG_ATTR_MARK);
242 uint32_t nfnl_log_get_mark(const struct nfnl_log *log)
244 return log->log_mark;
247 void nfnl_log_set_timestamp(struct nfnl_log *log, struct timeval *tv)
249 log->log_timestamp.tv_sec = tv->tv_sec;
250 log->log_timestamp.tv_usec = tv->tv_usec;
251 log->ce_mask |= LOG_ATTR_TIMESTAMP;
254 const struct timeval *nfnl_log_get_timestamp(const struct nfnl_log *log)
256 if (!(log->ce_mask & LOG_ATTR_TIMESTAMP))
258 return &log->log_timestamp;
261 void nfnl_log_set_indev(struct nfnl_log *log, uint32_t indev)
263 log->log_indev = indev;
264 log->ce_mask |= LOG_ATTR_INDEV;
267 uint32_t nfnl_log_get_indev(const struct nfnl_log *log)
269 return log->log_indev;
272 void nfnl_log_set_outdev(struct nfnl_log *log, uint32_t outdev)
274 log->log_outdev = outdev;
275 log->ce_mask |= LOG_ATTR_OUTDEV;
278 uint32_t nfnl_log_get_outdev(const struct nfnl_log *log)
280 return log->log_outdev;
283 void nfnl_log_set_physindev(struct nfnl_log *log, uint32_t physindev)
285 log->log_physindev = physindev;
286 log->ce_mask |= LOG_ATTR_PHYSINDEV;
289 uint32_t nfnl_log_get_physindev(const struct nfnl_log *log)
291 return log->log_physindev;
294 void nfnl_log_set_physoutdev(struct nfnl_log *log, uint32_t physoutdev)
296 log->log_physoutdev = physoutdev;
297 log->ce_mask |= LOG_ATTR_PHYSOUTDEV;
300 uint32_t nfnl_log_get_physoutdev(const struct nfnl_log *log)
302 return log->log_physoutdev;
305 void nfnl_log_set_hwaddr(struct nfnl_log *log, uint8_t *hwaddr, int len)
307 if (len > sizeof(log->log_hwaddr))
308 len = sizeof(log->log_hwaddr);
309 log->log_hwaddr_len = len;
310 memcpy(log->log_hwaddr, hwaddr, len);
311 log->ce_mask |= LOG_ATTR_HWADDR;
314 const uint8_t *nfnl_log_get_hwaddr(const struct nfnl_log *log, int *len)
316 if (!(log->ce_mask & LOG_ATTR_HWADDR)) {
321 *len = log->log_hwaddr_len;
322 return log->log_hwaddr;
325 int nfnl_log_set_payload(struct nfnl_log *log, uint8_t *payload, int len)
327 free(log->log_payload);
328 log->log_payload = malloc(len);
329 if (!log->log_payload)
330 return nl_errno(ENOMEM);
332 memcpy(log->log_payload, payload, len);
333 log->log_payload_len = len;
334 log->ce_mask |= LOG_ATTR_PAYLOAD;
338 const void *nfnl_log_get_payload(const struct nfnl_log *log, int *len)
340 if (!(log->ce_mask & LOG_ATTR_PAYLOAD)) {
345 *len = log->log_payload_len;
346 return log->log_payload;
349 int nfnl_log_set_prefix(struct nfnl_log *log, void *prefix)
351 free(log->log_prefix);
352 log->log_prefix = strdup(prefix);
353 if (!log->log_prefix)
354 return nl_errno(ENOMEM);
356 log->ce_mask |= LOG_ATTR_PREFIX;
360 const char *nfnl_log_get_prefix(const struct nfnl_log *log)
362 return log->log_prefix;
365 void nfnl_log_set_uid(struct nfnl_log *log, uint32_t uid)
368 log->ce_mask |= LOG_ATTR_UID;
371 int nfnl_log_test_uid(const struct nfnl_log *log)
373 return !!(log->ce_mask & LOG_ATTR_UID);
376 uint32_t nfnl_log_get_uid(const struct nfnl_log *log)
381 void nfnl_log_set_seq(struct nfnl_log *log, uint32_t seq)
384 log->ce_mask |= LOG_ATTR_SEQ;
387 int nfnl_log_test_seq(const struct nfnl_log *log)
389 return !!(log->ce_mask & LOG_ATTR_SEQ);
392 uint32_t nfnl_log_get_seq(const struct nfnl_log *log)
397 void nfnl_log_set_seq_global(struct nfnl_log *log, uint32_t seq_global)
399 log->log_seq_global = seq_global;
400 log->ce_mask |= LOG_ATTR_SEQ_GLOBAL;
403 int nfnl_log_test_seq_global(const struct nfnl_log *log)
405 return !!(log->ce_mask & LOG_ATTR_SEQ_GLOBAL);
408 uint32_t nfnl_log_get_seq_global(const struct nfnl_log *log)
410 return log->log_seq_global;
415 struct nl_object_ops log_obj_ops = {
416 .oo_name = "netfilter/log",
417 .oo_size = sizeof(struct nfnl_log),
418 .oo_free_data = log_free_data,
419 .oo_clone = log_clone,
420 .oo_dump[NL_DUMP_BRIEF] = log_dump,
421 .oo_dump[NL_DUMP_FULL] = log_dump,
422 .oo_dump[NL_DUMP_STATS] = log_dump,