15#include <linux/netfilter/nfnetlink_queue.h>
17#include <netlink-private/netlink.h>
18#include <netlink/attr.h>
19#include <netlink/netfilter/nfnl.h>
20#include <netlink/netfilter/queue_msg.h>
21#include <netlink-private/utils.h>
23static struct nl_cache_ops nfnl_queue_msg_ops;
25static struct nla_policy queue_policy[NFQA_MAX+1] = {
27 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
29 [NFQA_VERDICT_HDR] = {
30 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
32 [NFQA_MARK] = { .type =
NLA_U32 },
34 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
36 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
37 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
38 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
39 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
41 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
45int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
46 struct nfnl_queue_msg **result)
48 struct nfnl_queue_msg *msg;
49 struct nlattr *tb[NFQA_MAX+1];
53 msg = nfnl_queue_msg_alloc();
57 msg->ce_msgtype = nlh->nlmsg_type;
59 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
67 attr = tb[NFQA_PACKET_HDR];
69 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
71 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
73 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
74 nfnl_queue_msg_set_hook(msg, hdr->hook);
79 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
81 attr = tb[NFQA_TIMESTAMP];
83 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
86 tv.tv_sec = ntohll(timestamp->sec);
87 tv.tv_usec = ntohll(timestamp->usec);
88 nfnl_queue_msg_set_timestamp(msg, &tv);
91 attr = tb[NFQA_IFINDEX_INDEV];
93 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
95 attr = tb[NFQA_IFINDEX_OUTDEV];
97 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
99 attr = tb[NFQA_IFINDEX_PHYSINDEV];
101 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
103 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
105 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
107 attr = tb[NFQA_HWADDR];
109 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
111 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
112 ntohs(hw->hw_addrlen));
115 attr = tb[NFQA_PAYLOAD];
117 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
127 nfnl_queue_msg_put(msg);
131static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
132 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
134 struct nfnl_queue_msg *msg;
137 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
140 err = pp->pp_cb((
struct nl_object *) msg, pp);
141 nfnl_queue_msg_put(msg);
147static struct nl_msg *
148__nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg,
151 struct nl_msg *nlmsg;
152 struct nfqnl_msg_verdict_hdr verdict;
155 nfnl_queue_msg_get_family(msg),
156 nfnl_queue_msg_get_group(msg));
160 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
161 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
162 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
163 goto nla_put_failure;
165 if (nfnl_queue_msg_test_mark(msg) &&
167 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
168 goto nla_put_failure;
178nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
180 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
184nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
186 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
195int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
196 const struct nfnl_queue_msg *msg)
198 struct nl_msg *nlmsg;
201 nlmsg = nfnl_queue_msg_build_verdict(msg);
209 return wait_for_ack(nlh);
218int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
219 const struct nfnl_queue_msg *msg)
221 struct nl_msg *nlmsg;
224 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
232 return wait_for_ack(nlh);
243int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
244 const struct nfnl_queue_msg *msg,
245 const void *payload_data,
unsigned payload_len)
247 struct nl_msg *nlmsg;
252 nlmsg = nfnl_queue_msg_build_verdict(msg);
256 memset(iov, 0,
sizeof(iov));
258 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
259 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
261 nla.nla_type = NFQA_PAYLOAD;
262 nla.nla_len = payload_len +
sizeof(nla);
263 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
265 iov[1].iov_base = (
void *) &nla;
266 iov[1].iov_len =
sizeof(nla);
268 iov[2].iov_base = (
void *) payload_data;
269 iov[2].iov_len = NLA_ALIGN(payload_len);
277 return wait_for_ack(nlh);
280#define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
281static struct nl_cache_ops nfnl_queue_msg_ops = {
282 .co_name =
"netfilter/queue_msg",
283 .co_hdrsize = NFNL_HDRLEN,
285 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
286 END_OF_MSGTYPES_LIST,
288 .co_protocol = NETLINK_NETFILTER,
289 .co_msg_parser = queue_msg_parser,
290 .co_obj_ops = &queue_msg_obj_ops,
293static void __init nfnl_msg_queue_init(
void)
298static void __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.