2 * lib/netfilter/log.c Netfilter Log
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11 * Copyright (c) 2007 Secure Computing Corporation
21 #include <sys/types.h>
22 #include <linux/netfilter/nfnetlink_log.h>
24 #include <netlink-local.h>
25 #include <netlink/attr.h>
26 #include <netlink/netfilter/nfnl.h>
27 #include <netlink/netfilter/log.h>
34 static int build_log_cmd_request(uint8_t family, uint16_t queuenum,
35 uint8_t command, struct nl_msg **result)
38 struct nfulnl_msg_config_cmd cmd;
40 msg = nfnlmsg_alloc_simple(NFNL_SUBSYS_ULOG, NFULNL_MSG_CONFIG, 0,
45 cmd.command = command;
46 if (nla_put(msg, NFULA_CFG_CMD, sizeof(cmd), &cmd) < 0)
57 static int send_log_request(struct nl_sock *sk, struct nl_msg *msg)
61 err = nl_send_auto_complete(sk, msg);
66 return wait_for_ack(sk);
69 int nfnl_log_build_pf_bind(uint8_t pf, struct nl_msg **result)
71 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_BIND, result);
74 int nfnl_log_pf_bind(struct nl_sock *nlh, uint8_t pf)
79 if ((err = nfnl_log_build_pf_bind(pf, &msg)) < 0)
82 return send_log_request(nlh, msg);
85 int nfnl_log_build_pf_unbind(uint8_t pf, struct nl_msg **result)
87 return build_log_cmd_request(pf, 0, NFULNL_CFG_CMD_PF_UNBIND, result);
90 int nfnl_log_pf_unbind(struct nl_sock *nlh, uint8_t pf)
95 if ((err = nfnl_log_build_pf_unbind(pf, &msg)) < 0)
98 return send_log_request(nlh, msg);
101 static int nfnl_log_build_request(const struct nfnl_log *log,
102 struct nl_msg **result)
106 if (!nfnl_log_test_group(log))
107 return -NLE_MISSING_ATTR;
109 msg = nfnlmsg_alloc_simple(NFNL_SUBSYS_ULOG, NFULNL_MSG_CONFIG, 0,
110 0, nfnl_log_get_group(log));
114 /* This sucks. The nfnetlink_log interface always expects both
115 * parameters to be present. Needs to be done properly.
117 if (nfnl_log_test_copy_mode(log)) {
118 struct nfulnl_msg_config_mode mode;
120 switch (nfnl_log_get_copy_mode(log)) {
121 case NFNL_LOG_COPY_NONE:
122 mode.copy_mode = NFULNL_COPY_NONE;
124 case NFNL_LOG_COPY_META:
125 mode.copy_mode = NFULNL_COPY_META;
127 case NFNL_LOG_COPY_PACKET:
128 mode.copy_mode = NFULNL_COPY_PACKET;
131 mode.copy_range = htonl(nfnl_log_get_copy_range(log));
134 if (nla_put(msg, NFULA_CFG_MODE, sizeof(mode), &mode) < 0)
135 goto nla_put_failure;
138 if (nfnl_log_test_flush_timeout(log) &&
139 nla_put_u32(msg, NFULA_CFG_TIMEOUT,
140 htonl(nfnl_log_get_flush_timeout(log))) < 0)
141 goto nla_put_failure;
143 if (nfnl_log_test_alloc_size(log) &&
144 nla_put_u32(msg, NFULA_CFG_NLBUFSIZ,
145 htonl(nfnl_log_get_alloc_size(log))) < 0)
146 goto nla_put_failure;
148 if (nfnl_log_test_queue_threshold(log) &&
149 nla_put_u32(msg, NFULA_CFG_QTHRESH,
150 htonl(nfnl_log_get_queue_threshold(log))) < 0)
151 goto nla_put_failure;
161 int nfnl_log_build_create_request(const struct nfnl_log *log,
162 struct nl_msg **result)
164 struct nfulnl_msg_config_cmd cmd;
167 if ((err = nfnl_log_build_request(log, result)) < 0)
170 cmd.command = NFULNL_CFG_CMD_BIND;
172 if (nla_put(*result, NFULA_CFG_CMD, sizeof(cmd), &cmd) < 0)
173 goto nla_put_failure;
182 int nfnl_log_create(struct nl_sock *nlh, const struct nfnl_log *log)
187 if ((err = nfnl_log_build_create_request(log, &msg)) < 0)
190 return send_log_request(nlh, msg);
193 int nfnl_log_build_change_request(const struct nfnl_log *log,
194 struct nl_msg **result)
196 return nfnl_log_build_request(log, result);
199 int nfnl_log_change(struct nl_sock *nlh, const struct nfnl_log *log)
204 if ((err = nfnl_log_build_change_request(log, &msg)) < 0)
207 return send_log_request(nlh, msg);
210 int nfnl_log_build_delete_request(const struct nfnl_log *log,
211 struct nl_msg **result)
213 if (!nfnl_log_test_group(log))
214 return -NLE_MISSING_ATTR;
216 return build_log_cmd_request(0, nfnl_log_get_group(log),
217 NFULNL_CFG_CMD_UNBIND, result);
220 int nfnl_log_delete(struct nl_sock *nlh, const struct nfnl_log *log)
225 if ((err = nfnl_log_build_delete_request(log, &msg)) < 0)
228 return send_log_request(nlh, msg);
233 static struct nl_cache_ops nfnl_log_ops = {
234 .co_name = "netfilter/log",
235 .co_obj_ops = &log_obj_ops,
237 END_OF_MSGTYPES_LIST,
241 static void __init log_init(void)
243 nl_cache_mngt_register(&nfnl_log_ops);
246 static void __exit log_exit(void)
248 nl_cache_mngt_unregister(&nfnl_log_ops);