| 1 | |
| 2 | |
| 3 | * Copyright (c) 2008-2009 Patrick McHardy <[email protected]>
|
| 4 | * Copyright (c) 2012-2014 Pablo Neira Ayuso <[email protected]>
|
| 5 | *
|
| 6 | * Development of this code funded by Astaro AG (http://www.astaro.com/)
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/audit.h>
|
| 10 | #include <linux/kernel.h>
|
| 11 | #include <linux/init.h>
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/netlink.h>
|
| 14 | #include <linux/netfilter.h>
|
| 15 | #include <linux/netfilter/nf_tables.h>
|
| 16 | #include <net/ipv6.h>
|
| 17 | #include <net/ip.h>
|
| 18 | #include <net/netfilter/nf_tables.h>
|
| 19 | #include <net/netfilter/nf_log.h>
|
| 20 | #include <linux/netdevice.h>
|
| 21 |
|
| 22 | static const char *nft_log_null_prefix = "";
|
| 23 |
|
| 24 | struct nft_log {
|
| 25 | struct nf_loginfo loginfo;
|
| 26 | char *prefix;
|
| 27 | };
|
| 28 |
|
| 29 | static void nft_log_eval_audit(const struct nft_pktinfo *pkt)
|
| 30 | {
|
| 31 | struct sk_buff *skb = pkt->skb;
|
| 32 | struct audit_buffer *ab;
|
| 33 |
|
| 34 | if (!audit_enabled)
|
| 35 | return;
|
| 36 |
|
| 37 | ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
|
| 38 | if (!ab)
|
| 39 | return;
|
| 40 |
|
| 41 | audit_log_format(ab, "mark=%#x", skb->mark);
|
| 42 |
|
| 43 | audit_log_nf_skb(ab, skb, nft_pf(pkt));
|
| 44 |
|
| 45 | audit_log_end(ab);
|
| 46 | }
|
| 47 |
|
| 48 | static void nft_log_eval(const struct nft_expr *expr,
|
| 49 | struct nft_regs *regs,
|
| 50 | const struct nft_pktinfo *pkt)
|
| 51 | {
|
| 52 | const struct nft_log *priv = nft_expr_priv(expr);
|
| 53 |
|
| 54 | if (priv->loginfo.type == NF_LOG_TYPE_LOG &&
|
| 55 | priv->loginfo.u.log.level == NFT_LOGLEVEL_AUDIT) {
|
| 56 | nft_log_eval_audit(pkt);
|
| 57 | return;
|
| 58 | }
|
| 59 |
|
| 60 | nf_log_packet(nft_net(pkt), nft_pf(pkt), nft_hook(pkt), pkt->skb,
|
| 61 | nft_in(pkt), nft_out(pkt), &priv->loginfo, "%s",
|
| 62 | priv->prefix);
|
| 63 | }
|
| 64 |
|
| 65 | static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
|
| 66 | [NFTA_LOG_GROUP] = { .type = NLA_U16 },
|
| 67 | [NFTA_LOG_PREFIX] = { .type = NLA_STRING,
|
| 68 | .len = NF_LOG_PREFIXLEN - 1 },
|
| 69 | [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
|
| 70 | [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
|
| 71 | [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
|
| 72 | [NFTA_LOG_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NF_LOG_MASK),
|
| 73 | };
|
| 74 |
|
| 75 | static int nft_log_modprobe(struct net *net, enum nf_log_type t)
|
| 76 | {
|
| 77 | switch (t) {
|
| 78 | case NF_LOG_TYPE_LOG:
|
| 79 | return nft_request_module(net, "%s", "nf_log_syslog");
|
| 80 | case NF_LOG_TYPE_ULOG:
|
| 81 | return nft_request_module(net, "%s", "nfnetlink_log");
|
| 82 | case NF_LOG_TYPE_MAX:
|
| 83 | break;
|
| 84 | }
|
| 85 |
|
| 86 | return -ENOENT;
|
| 87 | }
|
| 88 |
|
| 89 | static int nft_log_init(const struct nft_ctx *ctx,
|
| 90 | const struct nft_expr *expr,
|
| 91 | const struct nlattr * const tb[])
|
| 92 | {
|
| 93 | struct nft_log *priv = nft_expr_priv(expr);
|
| 94 | struct nf_loginfo *li = &priv->loginfo;
|
| 95 | const struct nlattr *nla;
|
| 96 | int err;
|
| 97 |
|
| 98 | li->type = NF_LOG_TYPE_LOG;
|
| 99 | if (tb[NFTA_LOG_LEVEL] != NULL &&
|
| 100 | tb[NFTA_LOG_GROUP] != NULL)
|
| 101 | return -EINVAL;
|
| 102 | if (tb[NFTA_LOG_GROUP] != NULL) {
|
| 103 | li->type = NF_LOG_TYPE_ULOG;
|
| 104 | if (tb[NFTA_LOG_FLAGS] != NULL)
|
| 105 | return -EINVAL;
|
| 106 | }
|
| 107 |
|
| 108 | nla = tb[NFTA_LOG_PREFIX];
|
| 109 | if (nla != NULL) {
|
| 110 | priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL_ACCOUNT);
|
| 111 | if (priv->prefix == NULL)
|
| 112 | return -ENOMEM;
|
| 113 | nla_strscpy(priv->prefix, nla, nla_len(nla) + 1);
|
| 114 | } else {
|
| 115 | priv->prefix = (char *)nft_log_null_prefix;
|
| 116 | }
|
| 117 |
|
| 118 | switch (li->type) {
|
| 119 | case NF_LOG_TYPE_LOG:
|
| 120 | if (tb[NFTA_LOG_LEVEL] != NULL) {
|
| 121 | li->u.log.level =
|
| 122 | ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
|
| 123 | } else {
|
| 124 | li->u.log.level = NFT_LOGLEVEL_WARNING;
|
| 125 | }
|
| 126 | if (li->u.log.level > NFT_LOGLEVEL_AUDIT) {
|
| 127 | err = -EINVAL;
|
| 128 | goto err1;
|
| 129 | }
|
| 130 |
|
| 131 | if (tb[NFTA_LOG_FLAGS] != NULL) {
|
| 132 | li->u.log.logflags =
|
| 133 | ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
|
| 134 | if (li->u.log.logflags & ~NF_LOG_MASK) {
|
| 135 | err = -EINVAL;
|
| 136 | goto err1;
|
| 137 | }
|
| 138 | }
|
| 139 | break;
|
| 140 | case NF_LOG_TYPE_ULOG:
|
| 141 | li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
|
| 142 | if (tb[NFTA_LOG_SNAPLEN] != NULL) {
|
| 143 | li->u.ulog.flags |= NF_LOG_F_COPY_LEN;
|
| 144 | li->u.ulog.copy_len =
|
| 145 | ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
|
| 146 | }
|
| 147 | if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
|
| 148 | li->u.ulog.qthreshold =
|
| 149 | ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
|
| 150 | }
|
| 151 | break;
|
| 152 | }
|
| 153 |
|
| 154 | if (li->u.log.level == NFT_LOGLEVEL_AUDIT)
|
| 155 | return 0;
|
| 156 |
|
| 157 | err = nf_logger_find_get(ctx->family, li->type);
|
| 158 | if (err < 0) {
|
| 159 | if (nft_log_modprobe(ctx->net, li->type) == -EAGAIN)
|
| 160 | err = -EAGAIN;
|
| 161 |
|
| 162 | goto err1;
|
| 163 | }
|
| 164 |
|
| 165 | return 0;
|
| 166 |
|
| 167 | err1:
|
| 168 | if (priv->prefix != nft_log_null_prefix)
|
| 169 | kfree(priv->prefix);
|
| 170 | return err;
|
| 171 | }
|
| 172 |
|
| 173 | static void nft_log_destroy(const struct nft_ctx *ctx,
|
| 174 | const struct nft_expr *expr)
|
| 175 | {
|
| 176 | struct nft_log *priv = nft_expr_priv(expr);
|
| 177 | struct nf_loginfo *li = &priv->loginfo;
|
| 178 |
|
| 179 | if (priv->prefix != nft_log_null_prefix)
|
| 180 | kfree(priv->prefix);
|
| 181 |
|
| 182 | if (li->u.log.level == NFT_LOGLEVEL_AUDIT)
|
| 183 | return;
|
| 184 |
|
| 185 | nf_logger_put(ctx->family, li->type);
|
| 186 | }
|
| 187 |
|
| 188 | static int nft_log_dump(struct sk_buff *skb,
|
| 189 | const struct nft_expr *expr, bool reset)
|
| 190 | {
|
| 191 | const struct nft_log *priv = nft_expr_priv(expr);
|
| 192 | const struct nf_loginfo *li = &priv->loginfo;
|
| 193 |
|
| 194 | if (priv->prefix != nft_log_null_prefix)
|
| 195 | if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
|
| 196 | goto nla_put_failure;
|
| 197 | switch (li->type) {
|
| 198 | case NF_LOG_TYPE_LOG:
|
| 199 | if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
|
| 200 | goto nla_put_failure;
|
| 201 |
|
| 202 | if (li->u.log.logflags) {
|
| 203 | if (nla_put_be32(skb, NFTA_LOG_FLAGS,
|
| 204 | htonl(li->u.log.logflags)))
|
| 205 | goto nla_put_failure;
|
| 206 | }
|
| 207 | break;
|
| 208 | case NF_LOG_TYPE_ULOG:
|
| 209 | if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
|
| 210 | goto nla_put_failure;
|
| 211 |
|
| 212 | if (li->u.ulog.flags & NF_LOG_F_COPY_LEN) {
|
| 213 | if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
|
| 214 | htonl(li->u.ulog.copy_len)))
|
| 215 | goto nla_put_failure;
|
| 216 | }
|
| 217 | if (li->u.ulog.qthreshold) {
|
| 218 | if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
|
| 219 | htons(li->u.ulog.qthreshold)))
|
| 220 | goto nla_put_failure;
|
| 221 | }
|
| 222 | break;
|
| 223 | }
|
| 224 | return 0;
|
| 225 |
|
| 226 | nla_put_failure:
|
| 227 | return -1;
|
| 228 | }
|
| 229 |
|
| 230 | static struct nft_expr_type nft_log_type;
|
| 231 | static const struct nft_expr_ops nft_log_ops = {
|
| 232 | .type = &nft_log_type,
|
| 233 | .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
|
| 234 | .eval = nft_log_eval,
|
| 235 | .init = nft_log_init,
|
| 236 | .destroy = nft_log_destroy,
|
| 237 | .dump = nft_log_dump,
|
| 238 | };
|
| 239 |
|
| 240 | static struct nft_expr_type nft_log_type __read_mostly = {
|
| 241 | .name = "log",
|
| 242 | .ops = &nft_log_ops,
|
| 243 | .policy = nft_log_policy,
|
| 244 | .maxattr = NFTA_LOG_MAX,
|
| 245 | .owner = THIS_MODULE,
|
| 246 | };
|
| 247 |
|
| 248 | static int __init nft_log_module_init(void)
|
| 249 | {
|
| 250 | return nft_register_expr(&nft_log_type);
|
| 251 | }
|
| 252 |
|
| 253 | static void __exit nft_log_module_exit(void)
|
| 254 | {
|
| 255 | nft_unregister_expr(&nft_log_type);
|
| 256 | }
|
| 257 |
|
| 258 | module_init(nft_log_module_init);
|
| 259 | module_exit(nft_log_module_exit);
|
| 260 |
|
| 261 | MODULE_LICENSE("GPL");
|
| 262 | MODULE_AUTHOR("Patrick McHardy <[email protected]>");
|
| 263 | MODULE_ALIAS_NFT_EXPR("log");
|
| 264 | MODULE_DESCRIPTION("Netfilter nf_tables log module");
|