16#include <linux/netfilter/nfnetlink_log.h>
18#include <netlink-private/netlink.h>
19#include <netlink/attr.h>
20#include <netlink/netfilter/nfnl.h>
21#include <netlink/netfilter/log.h>
28static int build_log_cmd_request(uint8_t family, uint16_t queuenum,
29 uint8_t command,
struct nl_msg **result)
32 struct nfulnl_msg_config_cmd cmd;
39 cmd.command = command;
40 if (
nla_put(msg, NFULA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
51static int send_log_request(
struct nl_sock *sk,
struct nl_msg *msg)
60 return wait_for_ack(sk);
63int nfnl_log_build_pf_bind(uint8_t pf,
struct nl_msg **result)
65 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_BIND, result);
68int nfnl_log_pf_bind(
struct nl_sock *nlh, uint8_t pf)
73 if ((err = nfnl_log_build_pf_bind(pf, &msg)) < 0)
76 return send_log_request(nlh, msg);
79int nfnl_log_build_pf_unbind(uint8_t pf,
struct nl_msg **result)
81 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_UNBIND, result);
84int nfnl_log_pf_unbind(
struct nl_sock *nlh, uint8_t pf)
89 if ((err = nfnl_log_build_pf_unbind(pf, &msg)) < 0)
92 return send_log_request(nlh, msg);
95static int nfnl_log_build_request(
const struct nfnl_log *log,
96 struct nl_msg **result)
100 if (!nfnl_log_test_group(log))
101 return -NLE_MISSING_ATTR;
104 0, nfnl_log_get_group(log));
111 if (nfnl_log_test_copy_mode(log)) {
112 struct nfulnl_msg_config_mode mode;
114 switch (nfnl_log_get_copy_mode(log)) {
115 case NFNL_LOG_COPY_NONE:
116 mode.copy_mode = NFULNL_COPY_NONE;
118 case NFNL_LOG_COPY_META:
119 mode.copy_mode = NFULNL_COPY_META;
121 case NFNL_LOG_COPY_PACKET:
122 mode.copy_mode = NFULNL_COPY_PACKET;
125 mode.copy_range = htonl(nfnl_log_get_copy_range(log));
128 if (
nla_put(msg, NFULA_CFG_MODE,
sizeof(mode), &mode) < 0)
129 goto nla_put_failure;
132 if (nfnl_log_test_flush_timeout(log) &&
134 htonl(nfnl_log_get_flush_timeout(log))) < 0)
135 goto nla_put_failure;
137 if (nfnl_log_test_alloc_size(log) &&
139 htonl(nfnl_log_get_alloc_size(log))) < 0)
140 goto nla_put_failure;
142 if (nfnl_log_test_queue_threshold(log) &&
144 htonl(nfnl_log_get_queue_threshold(log))) < 0)
145 goto nla_put_failure;
147 if (nfnl_log_get_flags(log) &&
149 htons(nfnl_log_get_flags(log))) < 0)
150 goto nla_put_failure;
160int nfnl_log_build_create_request(
const struct nfnl_log *log,
161 struct nl_msg **result)
163 struct nfulnl_msg_config_cmd cmd;
166 if ((err = nfnl_log_build_request(log, result)) < 0)
169 cmd.command = NFULNL_CFG_CMD_BIND;
171 if (
nla_put(*result, NFULA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
172 goto nla_put_failure;
181int nfnl_log_create(
struct nl_sock *nlh,
const struct nfnl_log *log)
186 if ((err = nfnl_log_build_create_request(log, &msg)) < 0)
189 return send_log_request(nlh, msg);
192int nfnl_log_build_change_request(
const struct nfnl_log *log,
193 struct nl_msg **result)
195 return nfnl_log_build_request(log, result);
198int nfnl_log_change(
struct nl_sock *nlh,
const struct nfnl_log *log)
203 if ((err = nfnl_log_build_change_request(log, &msg)) < 0)
206 return send_log_request(nlh, msg);
209int nfnl_log_build_delete_request(
const struct nfnl_log *log,
210 struct nl_msg **result)
212 if (!nfnl_log_test_group(log))
213 return -NLE_MISSING_ATTR;
215 return build_log_cmd_request(0, nfnl_log_get_group(log),
216 NFULNL_CFG_CMD_UNBIND, result);
219int nfnl_log_delete(
struct nl_sock *nlh,
const struct nfnl_log *log)
224 if ((err = nfnl_log_build_delete_request(log, &msg)) < 0)
227 return send_log_request(nlh, msg);
232static struct nl_cache_ops nfnl_log_ops = {
233 .co_name =
"netfilter/log",
234 .co_obj_ops = &log_obj_ops,
236 END_OF_MSGTYPES_LIST,
240static void __init log_init(
void)
245static void __exit log_exit(
void)
int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
Add 16 bit integer attribute to netlink message.
int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
Add 32 bit integer attribute to netlink message.
int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
Add a unspecific attribute to netlink message.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
struct nl_msg * nfnlmsg_alloc_simple(uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
Allocate a new netfilter netlink message.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)