| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* (C) 2012-2013 by Pablo Neira Ayuso <[email protected]> |
| 4 |
* |
| 5 |
* This code has been sponsored by Sophos Astaro <http://www.sophos.com> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <string.h> |
| 11 |
#include <arpa/inet.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <linux/netfilter/nf_tables.h> |
| 14 |
#include <linux/netfilter/nf_log.h> |
| 15 |
|
| 16 |
#include "internal.h" |
| 17 |
#include <libmnl/libmnl.h> |
| 18 |
#include <libnftnl/expr.h> |
| 19 |
#include <libnftnl/rule.h> |
| 20 |
|
| 21 |
struct nftnl_expr_log { |
| 22 |
uint32_t snaplen; |
| 23 |
uint16_t group; |
| 24 |
uint16_t qthreshold; |
| 25 |
uint32_t level; |
| 26 |
uint32_t flags; |
| 27 |
const char *prefix; |
| 28 |
}; |
| 29 |
|
| 30 |
static int nftnl_expr_log_set(struct nftnl_expr *e, uint16_t type, |
| 31 |
const void *data, uint32_t data_len) |
| 32 |
{ |
| 33 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 34 |
|
| 35 |
switch(type) { |
| 36 |
case NFTNL_EXPR_LOG_PREFIX: |
| 37 |
if (log->flags & (1 << NFTNL_EXPR_LOG_PREFIX)) |
| 38 |
xfree(log->prefix); |
| 39 |
|
| 40 |
log->prefix = strdup(data); |
| 41 |
if (!log->prefix) |
| 42 |
return -1; |
| 43 |
break; |
| 44 |
case NFTNL_EXPR_LOG_GROUP: |
| 45 |
memcpy(&log->group, data, data_len); |
| 46 |
break; |
| 47 |
case NFTNL_EXPR_LOG_SNAPLEN: |
| 48 |
memcpy(&log->snaplen, data, data_len); |
| 49 |
break; |
| 50 |
case NFTNL_EXPR_LOG_QTHRESHOLD: |
| 51 |
memcpy(&log->qthreshold, data, data_len); |
| 52 |
break; |
| 53 |
case NFTNL_EXPR_LOG_LEVEL: |
| 54 |
memcpy(&log->level, data, data_len); |
| 55 |
break; |
| 56 |
case NFTNL_EXPR_LOG_FLAGS: |
| 57 |
memcpy(&log->flags, data, data_len); |
| 58 |
break; |
| 59 |
} |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static const void * |
| 64 |
nftnl_expr_log_get(const struct nftnl_expr *e, uint16_t type, |
| 65 |
uint32_t *data_len) |
| 66 |
{ |
| 67 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 68 |
|
| 69 |
switch(type) { |
| 70 |
case NFTNL_EXPR_LOG_PREFIX: |
| 71 |
*data_len = strlen(log->prefix)+1; |
| 72 |
return log->prefix; |
| 73 |
case NFTNL_EXPR_LOG_GROUP: |
| 74 |
*data_len = sizeof(log->group); |
| 75 |
return &log->group; |
| 76 |
case NFTNL_EXPR_LOG_SNAPLEN: |
| 77 |
*data_len = sizeof(log->snaplen); |
| 78 |
return &log->snaplen; |
| 79 |
case NFTNL_EXPR_LOG_QTHRESHOLD: |
| 80 |
*data_len = sizeof(log->qthreshold); |
| 81 |
return &log->qthreshold; |
| 82 |
case NFTNL_EXPR_LOG_LEVEL: |
| 83 |
*data_len = sizeof(log->level); |
| 84 |
return &log->level; |
| 85 |
case NFTNL_EXPR_LOG_FLAGS: |
| 86 |
*data_len = sizeof(log->flags); |
| 87 |
return &log->flags; |
| 88 |
} |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
static int nftnl_expr_log_cb(const struct nlattr *attr, void *data) |
| 93 |
{ |
| 94 |
const struct nlattr **tb = data; |
| 95 |
int type = mnl_attr_get_type(attr); |
| 96 |
|
| 97 |
if (mnl_attr_type_valid(attr, NFTA_LOG_MAX) < 0) |
| 98 |
return MNL_CB_OK; |
| 99 |
|
| 100 |
switch(type) { |
| 101 |
case NFTA_LOG_PREFIX: |
| 102 |
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 103 |
abi_breakage(); |
| 104 |
break; |
| 105 |
case NFTA_LOG_GROUP: |
| 106 |
case NFTA_LOG_QTHRESHOLD: |
| 107 |
if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) |
| 108 |
abi_breakage(); |
| 109 |
break; |
| 110 |
case NFTA_LOG_SNAPLEN: |
| 111 |
case NFTA_LOG_LEVEL: |
| 112 |
case NFTA_LOG_FLAGS: |
| 113 |
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 114 |
abi_breakage(); |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
tb[type] = attr; |
| 119 |
return MNL_CB_OK; |
| 120 |
} |
| 121 |
|
| 122 |
static void |
| 123 |
nftnl_expr_log_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 124 |
{ |
| 125 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 126 |
|
| 127 |
if (e->flags & (1 << NFTNL_EXPR_LOG_PREFIX)) |
| 128 |
mnl_attr_put_strz(nlh, NFTA_LOG_PREFIX, log->prefix); |
| 129 |
if (e->flags & (1 << NFTNL_EXPR_LOG_GROUP)) |
| 130 |
mnl_attr_put_u16(nlh, NFTA_LOG_GROUP, htons(log->group)); |
| 131 |
if (e->flags & (1 << NFTNL_EXPR_LOG_SNAPLEN)) |
| 132 |
mnl_attr_put_u32(nlh, NFTA_LOG_SNAPLEN, htonl(log->snaplen)); |
| 133 |
if (e->flags & (1 << NFTNL_EXPR_LOG_QTHRESHOLD)) |
| 134 |
mnl_attr_put_u16(nlh, NFTA_LOG_QTHRESHOLD, htons(log->qthreshold)); |
| 135 |
if (e->flags & (1 << NFTNL_EXPR_LOG_LEVEL)) |
| 136 |
mnl_attr_put_u32(nlh, NFTA_LOG_LEVEL, htonl(log->level)); |
| 137 |
if (e->flags & (1 << NFTNL_EXPR_LOG_FLAGS)) |
| 138 |
mnl_attr_put_u32(nlh, NFTA_LOG_FLAGS, htonl(log->flags)); |
| 139 |
} |
| 140 |
|
| 141 |
static int |
| 142 |
nftnl_expr_log_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 143 |
{ |
| 144 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 145 |
struct nlattr *tb[NFTA_LOG_MAX+1] = {}; |
| 146 |
|
| 147 |
if (mnl_attr_parse_nested(attr, nftnl_expr_log_cb, tb) < 0) |
| 148 |
return -1; |
| 149 |
|
| 150 |
if (nftnl_parse_str_attr(tb[NFTA_LOG_PREFIX], |
| 151 |
NFTNL_EXPR_LOG_PREFIX, |
| 152 |
&log->prefix, &e->flags) < 0) |
| 153 |
return -1; |
| 154 |
if (tb[NFTA_LOG_GROUP]) { |
| 155 |
log->group = ntohs(mnl_attr_get_u16(tb[NFTA_LOG_GROUP])); |
| 156 |
e->flags |= (1 << NFTNL_EXPR_LOG_GROUP); |
| 157 |
} |
| 158 |
if (tb[NFTA_LOG_SNAPLEN]) { |
| 159 |
log->snaplen = ntohl(mnl_attr_get_u32(tb[NFTA_LOG_SNAPLEN])); |
| 160 |
e->flags |= (1 << NFTNL_EXPR_LOG_SNAPLEN); |
| 161 |
} |
| 162 |
if (tb[NFTA_LOG_QTHRESHOLD]) { |
| 163 |
log->qthreshold = ntohs(mnl_attr_get_u16(tb[NFTA_LOG_QTHRESHOLD])); |
| 164 |
e->flags |= (1 << NFTNL_EXPR_LOG_QTHRESHOLD); |
| 165 |
} |
| 166 |
if (tb[NFTA_LOG_LEVEL]) { |
| 167 |
log->level = ntohl(mnl_attr_get_u32(tb[NFTA_LOG_LEVEL])); |
| 168 |
e->flags |= (1 << NFTNL_EXPR_LOG_LEVEL); |
| 169 |
} |
| 170 |
if (tb[NFTA_LOG_FLAGS]) { |
| 171 |
log->flags = ntohl(mnl_attr_get_u32(tb[NFTA_LOG_FLAGS])); |
| 172 |
e->flags |= (1 << NFTNL_EXPR_LOG_FLAGS); |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static int |
| 179 |
nftnl_expr_log_snprintf(char *buf, size_t remain, |
| 180 |
uint32_t flags, const struct nftnl_expr *e) |
| 181 |
{ |
| 182 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 183 |
int ret, offset = 0; |
| 184 |
|
| 185 |
if (e->flags & (1 << NFTNL_EXPR_LOG_PREFIX)) { |
| 186 |
ret = snprintf(buf, remain, "prefix %s ", log->prefix); |
| 187 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 188 |
} |
| 189 |
|
| 190 |
if (e->flags & (1 << NFTNL_EXPR_LOG_GROUP)) { |
| 191 |
ret = snprintf(buf + offset, remain, |
| 192 |
"group %u snaplen %u qthreshold %u ", |
| 193 |
log->group, log->snaplen, log->qthreshold); |
| 194 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 195 |
} else { |
| 196 |
if (e->flags & (1 << NFTNL_EXPR_LOG_LEVEL)) { |
| 197 |
ret = snprintf(buf + offset, remain, "level %u ", |
| 198 |
log->level); |
| 199 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 200 |
} |
| 201 |
if (e->flags & (1 << NFTNL_EXPR_LOG_FLAGS)) { |
| 202 |
if (log->flags & NF_LOG_TCPSEQ) { |
| 203 |
ret = snprintf(buf + offset, remain, "tcpseq "); |
| 204 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 205 |
} |
| 206 |
if (log->flags & NF_LOG_TCPOPT) { |
| 207 |
ret = snprintf(buf + offset, remain, "tcpopt "); |
| 208 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 209 |
} |
| 210 |
if (log->flags & NF_LOG_IPOPT) { |
| 211 |
ret = snprintf(buf + offset, remain, "ipopt "); |
| 212 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 213 |
} |
| 214 |
if (log->flags & NF_LOG_UID) { |
| 215 |
ret = snprintf(buf + offset, remain, "uid "); |
| 216 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 217 |
} |
| 218 |
if (log->flags & NF_LOG_MACDECODE) { |
| 219 |
ret = snprintf(buf + offset, remain, |
| 220 |
"macdecode "); |
| 221 |
SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return offset; |
| 227 |
} |
| 228 |
|
| 229 |
static void nftnl_expr_log_free(const struct nftnl_expr *e) |
| 230 |
{ |
| 231 |
struct nftnl_expr_log *log = nftnl_expr_data(e); |
| 232 |
|
| 233 |
xfree(log->prefix); |
| 234 |
} |
| 235 |
|
| 236 |
static struct attr_policy log_attr_policy[__NFTNL_EXPR_LOG_MAX] = { |
| 237 |
[NFTNL_EXPR_LOG_PREFIX] = { .maxlen = NF_LOG_PREFIXLEN }, |
| 238 |
[NFTNL_EXPR_LOG_GROUP] = { .maxlen = sizeof(uint16_t) }, |
| 239 |
[NFTNL_EXPR_LOG_SNAPLEN] = { .maxlen = sizeof(uint32_t) }, |
| 240 |
[NFTNL_EXPR_LOG_QTHRESHOLD] = { .maxlen = sizeof(uint16_t) }, |
| 241 |
[NFTNL_EXPR_LOG_LEVEL] = { .maxlen = sizeof(uint32_t) }, |
| 242 |
[NFTNL_EXPR_LOG_FLAGS] = { .maxlen = sizeof(uint32_t) }, |
| 243 |
}; |
| 244 |
|
| 245 |
struct expr_ops expr_ops_log = { |
| 246 |
.name = "log", |
| 247 |
.alloc_len = sizeof(struct nftnl_expr_log), |
| 248 |
.nftnl_max_attr = __NFTNL_EXPR_LOG_MAX - 1, |
| 249 |
.attr_policy = log_attr_policy, |
| 250 |
.free = nftnl_expr_log_free, |
| 251 |
.set = nftnl_expr_log_set, |
| 252 |
.get = nftnl_expr_log_get, |
| 253 |
.parse = nftnl_expr_log_parse, |
| 254 |
.build = nftnl_expr_log_build, |
| 255 |
.output = nftnl_expr_log_snprintf, |
| 256 |
}; |