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