20 #include <sys/types.h> 21 #include <linux/netfilter/nfnetlink_queue.h> 23 #include <netlink-private/netlink.h> 24 #include <netlink/attr.h> 25 #include <netlink/netfilter/nfnl.h> 26 #include <netlink/netfilter/queue.h> 28 struct nl_sock *nfnl_queue_socket_alloc(
void)
38 static int send_queue_request(
struct nl_sock *sk,
struct nl_msg *msg)
47 return wait_for_ack(sk);
55 static int build_queue_cmd_request(uint8_t family, uint16_t queuenum,
56 uint8_t command,
struct nl_msg **result)
59 struct nfqnl_msg_config_cmd cmd;
66 cmd.pf = htons(family);
68 cmd.command = command;
69 if (
nla_put(msg, NFQA_CFG_CMD,
sizeof(cmd), &cmd) < 0)
80 int nfnl_queue_build_pf_bind(uint8_t pf,
struct nl_msg **result)
82 return build_queue_cmd_request(pf, 0, NFQNL_CFG_CMD_PF_BIND, result);
85 int nfnl_queue_pf_bind(
struct nl_sock *nlh, uint8_t pf)
90 if ((err = nfnl_queue_build_pf_bind(pf, &msg)) < 0)
93 return send_queue_request(nlh, msg);
96 int nfnl_queue_build_pf_unbind(uint8_t pf,
struct nl_msg **result)
98 return build_queue_cmd_request(pf, 0, NFQNL_CFG_CMD_PF_UNBIND, result);
101 int nfnl_queue_pf_unbind(
struct nl_sock *nlh, uint8_t pf)
106 if ((err = nfnl_queue_build_pf_unbind(pf, &msg)) < 0)
109 return send_queue_request(nlh, msg);
112 static int nfnl_queue_build_request(
const struct nfnl_queue *queue,
113 struct nl_msg **result)
117 if (!nfnl_queue_test_group(queue))
118 return -NLE_MISSING_ATTR;
121 0, nfnl_queue_get_group(queue));
125 if (nfnl_queue_test_maxlen(queue) &&
127 htonl(nfnl_queue_get_maxlen(queue))) < 0)
128 goto nla_put_failure;
133 if (nfnl_queue_test_copy_mode(queue)) {
134 struct nfqnl_msg_config_params params;
136 switch (nfnl_queue_get_copy_mode(queue)) {
137 case NFNL_QUEUE_COPY_NONE:
138 params.copy_mode = NFQNL_COPY_NONE;
140 case NFNL_QUEUE_COPY_META:
141 params.copy_mode = NFQNL_COPY_META;
143 case NFNL_QUEUE_COPY_PACKET:
144 params.copy_mode = NFQNL_COPY_PACKET;
147 params.copy_range = htonl(nfnl_queue_get_copy_range(queue));
149 if (
nla_put(msg, NFQA_CFG_PARAMS,
sizeof(params), ¶ms) < 0)
150 goto nla_put_failure;
161 int nfnl_queue_build_create_request(
const struct nfnl_queue *queue,
162 struct nl_msg **result)
164 struct nfqnl_msg_config_cmd cmd;
167 if ((err = nfnl_queue_build_request(queue, result)) < 0)
172 cmd.command = NFQNL_CFG_CMD_BIND;
174 NLA_PUT(*result, NFQA_CFG_CMD,
sizeof(cmd), &cmd);
183 int nfnl_queue_create(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
188 if ((err = nfnl_queue_build_create_request(queue, &msg)) < 0)
191 return send_queue_request(nlh, msg);
194 int nfnl_queue_build_change_request(
const struct nfnl_queue *queue,
195 struct nl_msg **result)
197 return nfnl_queue_build_request(queue, result);
200 int nfnl_queue_change(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
205 if ((err = nfnl_queue_build_change_request(queue, &msg)) < 0)
208 return send_queue_request(nlh, msg);
211 int nfnl_queue_build_delete_request(
const struct nfnl_queue *queue,
212 struct nl_msg **result)
214 if (!nfnl_queue_test_group(queue))
215 return -NLE_MISSING_ATTR;
217 return build_queue_cmd_request(0, nfnl_queue_get_group(queue),
218 NFQNL_CFG_CMD_UNBIND, result);
221 int nfnl_queue_delete(
struct nl_sock *nlh,
const struct nfnl_queue *queue)
226 if ((err = nfnl_queue_build_delete_request(queue, &msg)) < 0)
229 return send_queue_request(nlh, msg);
234 static struct nl_cache_ops nfnl_queue_ops = {
235 .co_name =
"netfilter/queue",
236 .co_obj_ops = &queue_obj_ops,
238 END_OF_MSGTYPES_LIST,
242 static void __init nfnl_queue_init(
void)
247 static void __exit nfnl_queue_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.
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.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific 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.
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.