22 #include <sys/types.h> 23 #include <linux/netfilter/nfnetlink_log.h> 25 #include <netlink-private/netlink.h> 26 #include <netlink/attr.h> 27 #include <netlink/netfilter/nfnl.h> 28 #include <netlink/netfilter/log.h> 35 static int build_log_cmd_request(uint8_t family, uint16_t queuenum,
36 uint8_t command,
struct nl_msg **result)
39 struct nfulnl_msg_config_cmd cmd;
46 cmd.command = command;
47 if (
nla_put(msg, NFULA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
58 static int send_log_request(
struct nl_sock *sk,
struct nl_msg *msg)
67 return wait_for_ack(sk);
70 int nfnl_log_build_pf_bind(uint8_t pf,
struct nl_msg **result)
72 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_BIND, result);
75 int nfnl_log_pf_bind(
struct nl_sock *nlh, uint8_t pf)
80 if ((err = nfnl_log_build_pf_bind(pf, &msg)) < 0)
83 return send_log_request(nlh, msg);
86 int nfnl_log_build_pf_unbind(uint8_t pf,
struct nl_msg **result)
88 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_UNBIND, result);
91 int nfnl_log_pf_unbind(
struct nl_sock *nlh, uint8_t pf)
96 if ((err = nfnl_log_build_pf_unbind(pf, &msg)) < 0)
99 return send_log_request(nlh, msg);
102 static int nfnl_log_build_request(
const struct nfnl_log *log,
103 struct nl_msg **result)
107 if (!nfnl_log_test_group(log))
108 return -NLE_MISSING_ATTR;
111 0, nfnl_log_get_group(log));
118 if (nfnl_log_test_copy_mode(log)) {
119 struct nfulnl_msg_config_mode mode;
121 switch (nfnl_log_get_copy_mode(log)) {
122 case NFNL_LOG_COPY_NONE:
123 mode.copy_mode = NFULNL_COPY_NONE;
125 case NFNL_LOG_COPY_META:
126 mode.copy_mode = NFULNL_COPY_META;
128 case NFNL_LOG_COPY_PACKET:
129 mode.copy_mode = NFULNL_COPY_PACKET;
132 mode.copy_range = htonl(nfnl_log_get_copy_range(log));
135 if (
nla_put(msg, NFULA_CFG_MODE,
sizeof(mode), &mode) < 0)
136 goto nla_put_failure;
139 if (nfnl_log_test_flush_timeout(log) &&
141 htonl(nfnl_log_get_flush_timeout(log))) < 0)
142 goto nla_put_failure;
144 if (nfnl_log_test_alloc_size(log) &&
146 htonl(nfnl_log_get_alloc_size(log))) < 0)
147 goto nla_put_failure;
149 if (nfnl_log_test_queue_threshold(log) &&
151 htonl(nfnl_log_get_queue_threshold(log))) < 0)
152 goto nla_put_failure;
162 int nfnl_log_build_create_request(
const struct nfnl_log *log,
163 struct nl_msg **result)
165 struct nfulnl_msg_config_cmd cmd;
168 if ((err = nfnl_log_build_request(log, result)) < 0)
171 cmd.command = NFULNL_CFG_CMD_BIND;
173 if (
nla_put(*result, NFULA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
174 goto nla_put_failure;
183 int nfnl_log_create(
struct nl_sock *nlh,
const struct nfnl_log *log)
188 if ((err = nfnl_log_build_create_request(log, &msg)) < 0)
191 return send_log_request(nlh, msg);
194 int nfnl_log_build_change_request(
const struct nfnl_log *log,
195 struct nl_msg **result)
197 return nfnl_log_build_request(log, result);
200 int nfnl_log_change(
struct nl_sock *nlh,
const struct nfnl_log *log)
205 if ((err = nfnl_log_build_change_request(log, &msg)) < 0)
208 return send_log_request(nlh, msg);
211 int nfnl_log_build_delete_request(
const struct nfnl_log *log,
212 struct nl_msg **result)
214 if (!nfnl_log_test_group(log))
215 return -NLE_MISSING_ATTR;
217 return build_log_cmd_request(0, nfnl_log_get_group(log),
218 NFULNL_CFG_CMD_UNBIND, result);
221 int nfnl_log_delete(
struct nl_sock *nlh,
const struct nfnl_log *log)
226 if ((err = nfnl_log_build_delete_request(log, &msg)) < 0)
229 return send_log_request(nlh, msg);
234 static struct nl_cache_ops nfnl_log_ops = {
235 .co_name =
"netfilter/log",
236 .co_obj_ops = &log_obj_ops,
238 END_OF_MSGTYPES_LIST,
242 static void __init log_init(
void)
247 static void __exit log_exit(
void)
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
void nlmsg_free(struct nl_msg *msg)
Release a reference from an 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.
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.
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.