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