14#include <linux/netfilter/nfnetlink_queue.h>
16#include <netlink-private/netlink.h>
17#include <netlink/attr.h>
18#include <netlink/netfilter/nfnl.h>
19#include <netlink/netfilter/queue.h>
21struct nl_sock *nfnl_queue_socket_alloc(
void)
31static int send_queue_request(
struct nl_sock *sk,
struct nl_msg *msg)
40 return wait_for_ack(sk);
48static int build_queue_cmd_request(uint8_t family, uint16_t queuenum,
49 uint8_t command,
struct nl_msg **result)
52 struct nfqnl_msg_config_cmd cmd;
59 cmd.pf = htons(family);
61 cmd.command = command;
62 if (
nla_put(msg, NFQA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
73int nfnl_queue_build_pf_bind(uint8_t pf,
struct nl_msg **result)
75 return build_queue_cmd_request(pf, 0, NFQNL_CFG_CMD_PF_BIND, result);
78int nfnl_queue_pf_bind(
struct nl_sock *nlh, uint8_t pf)
83 if ((err = nfnl_queue_build_pf_bind(pf, &msg)) < 0)
86 return send_queue_request(nlh, msg);
89int nfnl_queue_build_pf_unbind(uint8_t pf,
struct nl_msg **result)
91 return build_queue_cmd_request(pf, 0, NFQNL_CFG_CMD_PF_UNBIND, result);
94int nfnl_queue_pf_unbind(
struct nl_sock *nlh, uint8_t pf)
99 if ((err = nfnl_queue_build_pf_unbind(pf, &msg)) < 0)
102 return send_queue_request(nlh, msg);
105static int nfnl_queue_build_request(
const struct nfnl_queue *queue,
106 struct nl_msg **result)
110 if (!nfnl_queue_test_group(queue))
111 return -NLE_MISSING_ATTR;
114 0, nfnl_queue_get_group(queue));
118 if (nfnl_queue_test_maxlen(queue) &&
120 htonl(nfnl_queue_get_maxlen(queue))) < 0)
121 goto nla_put_failure;
126 if (nfnl_queue_test_copy_mode(queue)) {
127 struct nfqnl_msg_config_params params;
129 switch (nfnl_queue_get_copy_mode(queue)) {
130 case NFNL_QUEUE_COPY_NONE:
131 params.copy_mode = NFQNL_COPY_NONE;
133 case NFNL_QUEUE_COPY_META:
134 params.copy_mode = NFQNL_COPY_META;
136 case NFNL_QUEUE_COPY_PACKET:
137 params.copy_mode = NFQNL_COPY_PACKET;
140 params.copy_range = htonl(nfnl_queue_get_copy_range(queue));
142 if (
nla_put(msg, NFQA_CFG_PARAMS,
sizeof(params), ¶ms) < 0)
143 goto nla_put_failure;
154int nfnl_queue_build_create_request(
const struct nfnl_queue *queue,
155 struct nl_msg **result)
157 struct nfqnl_msg_config_cmd cmd;
160 if ((err = nfnl_queue_build_request(queue, result)) < 0)
165 cmd.command = NFQNL_CFG_CMD_BIND;
167 NLA_PUT(*result, NFQA_CFG_CMD,
sizeof(cmd), &cmd);
176int nfnl_queue_create(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
181 if ((err = nfnl_queue_build_create_request(queue, &msg)) < 0)
184 return send_queue_request(nlh, msg);
187int nfnl_queue_build_change_request(
const struct nfnl_queue *queue,
188 struct nl_msg **result)
190 return nfnl_queue_build_request(queue, result);
193int nfnl_queue_change(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
198 if ((err = nfnl_queue_build_change_request(queue, &msg)) < 0)
201 return send_queue_request(nlh, msg);
204int nfnl_queue_build_delete_request(
const struct nfnl_queue *queue,
205 struct nl_msg **result)
207 if (!nfnl_queue_test_group(queue))
208 return -NLE_MISSING_ATTR;
210 return build_queue_cmd_request(0, nfnl_queue_get_group(queue),
211 NFQNL_CFG_CMD_UNBIND, result);
214int nfnl_queue_delete(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
219 if ((err = nfnl_queue_build_delete_request(queue, &msg)) < 0)
222 return send_queue_request(nlh, msg);
227static struct nl_cache_ops nfnl_queue_ops = {
228 .co_name =
"netfilter/queue",
229 .co_obj_ops = &queue_obj_ops,
231 END_OF_MSGTYPES_LIST,
235static void __init nfnl_queue_init(
void)
240static void __exit nfnl_queue_exit(
void)
int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
Add 32 bit integer attribute to netlink message.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific 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)
void nl_socket_disable_auto_ack(struct nl_sock *sk)
Disable automatic request for ACK.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.