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