14#include "nl-default.h"
18#include <linux/netfilter/nfnetlink_queue.h>
20#include <netlink/attr.h>
21#include <netlink/netfilter/nfnl.h>
22#include <netlink/netfilter/queue_msg.h>
24#include "nl-netfilter.h"
25#include "nl-priv-dynamic-core/nl-core.h"
26#include "nl-priv-dynamic-core/cache-api.h"
28static struct nl_cache_ops nfnl_queue_msg_ops;
30static struct nla_policy queue_policy[NFQA_MAX+1] = {
32 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
34 [NFQA_VERDICT_HDR] = {
35 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
37 [NFQA_MARK] = { .type =
NLA_U32 },
39 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
41 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
42 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
43 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
44 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
46 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
50int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
54 struct nlattr *tb[NFQA_MAX+1];
58 msg = nfnl_queue_msg_alloc();
62 msg->ce_msgtype = nlh->nlmsg_type;
64 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
72 attr = tb[NFQA_PACKET_HDR];
74 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
76 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
78 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
79 nfnl_queue_msg_set_hook(msg, hdr->hook);
84 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
86 attr = tb[NFQA_TIMESTAMP];
88 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
91 tv.tv_sec = ntohll(timestamp->sec);
92 tv.tv_usec = ntohll(timestamp->usec);
93 nfnl_queue_msg_set_timestamp(msg, &tv);
96 attr = tb[NFQA_IFINDEX_INDEV];
98 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
100 attr = tb[NFQA_IFINDEX_OUTDEV];
102 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
104 attr = tb[NFQA_IFINDEX_PHYSINDEV];
106 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
108 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
110 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
112 attr = tb[NFQA_HWADDR];
114 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
116 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
117 ntohs(hw->hw_addrlen));
120 attr = tb[NFQA_PAYLOAD];
122 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
132 nfnl_queue_msg_put(msg);
136static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
137 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
142 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
145 err = pp->pp_cb((
struct nl_object *) msg, pp);
146 nfnl_queue_msg_put(msg);
152static struct nl_msg *
156 struct nl_msg *nlmsg;
157 struct nfqnl_msg_verdict_hdr verdict;
160 nfnl_queue_msg_get_family(msg),
161 nfnl_queue_msg_get_group(msg));
165 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
166 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
167 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
168 goto nla_put_failure;
170 if (nfnl_queue_msg_test_mark(msg) &&
172 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
173 goto nla_put_failure;
185 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
189nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
191 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
200int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
203 struct nl_msg *nlmsg;
206 nlmsg = nfnl_queue_msg_build_verdict(msg);
214 return wait_for_ack(nlh);
223int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
226 struct nl_msg *nlmsg;
229 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
237 return wait_for_ack(nlh);
248int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
250 const void *payload_data,
unsigned payload_len)
252 struct nl_msg *nlmsg;
257 nlmsg = nfnl_queue_msg_build_verdict(msg);
261 memset(iov, 0,
sizeof(iov));
263 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
264 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
266 nla.nla_type = NFQA_PAYLOAD;
267 nla.nla_len = payload_len +
sizeof(nla);
268 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
270 iov[1].iov_base = (
void *) &nla;
271 iov[1].iov_len =
sizeof(nla);
273 iov[2].iov_base = (
void *) payload_data;
274 iov[2].iov_len = NLA_ALIGN(payload_len);
282 return wait_for_ack(nlh);
285#define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
286static struct nl_cache_ops nfnl_queue_msg_ops = {
287 .co_name =
"netfilter/queue_msg",
288 .co_hdrsize = NFNL_HDRLEN,
290 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
291 END_OF_MSGTYPES_LIST,
293 .co_protocol = NETLINK_NETFILTER,
294 .co_msg_parser = queue_msg_parser,
295 .co_obj_ops = &queue_msg_obj_ops,
298static void _nl_init nfnl_msg_queue_init(
void)
303static void _nl_exit nfnl_queue_msg_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
Add 32 bit integer attribute to netlink message.
int nla_len(const struct nlattr *nla)
Return length of the payload .
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.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
uint16_t nfnlmsg_res_id(struct nlmsghdr *nlh)
Get netfilter resource id from message.
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from 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_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
Transmit Netlink message (taking IO vector)
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
Finalize Netlink message.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.