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_msg.h>
29 static struct nl_cache_ops nfnl_queue_msg_ops;
31 #if __BYTE_ORDER == __BIG_ENDIAN
32 static uint64_t ntohll(uint64_t x)
36 #elif __BYTE_ORDER == __LITTLE_ENDIAN
37 static uint64_t ntohll(uint64_t x)
43 static struct nla_policy queue_policy[NFQA_MAX+1] = {
45 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
47 [NFQA_VERDICT_HDR] = {
48 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
50 [NFQA_MARK] = { .type =
NLA_U32 },
52 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
54 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
55 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
56 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
57 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
59 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
63 int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
64 struct nfnl_queue_msg **result)
66 struct nfnl_queue_msg *msg;
67 struct nlattr *tb[NFQA_MAX+1];
71 msg = nfnl_queue_msg_alloc();
75 msg->ce_msgtype = nlh->nlmsg_type;
77 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
85 attr = tb[NFQA_PACKET_HDR];
87 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
89 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
91 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
92 nfnl_queue_msg_set_hook(msg, hdr->hook);
97 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
99 attr = tb[NFQA_TIMESTAMP];
101 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
104 tv.tv_sec = ntohll(timestamp->sec);
105 tv.tv_usec = ntohll(timestamp->usec);
106 nfnl_queue_msg_set_timestamp(msg, &tv);
109 attr = tb[NFQA_IFINDEX_INDEV];
111 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
113 attr = tb[NFQA_IFINDEX_OUTDEV];
115 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
117 attr = tb[NFQA_IFINDEX_PHYSINDEV];
119 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
121 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
123 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
125 attr = tb[NFQA_HWADDR];
127 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
129 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
130 ntohs(hw->hw_addrlen));
133 attr = tb[NFQA_PAYLOAD];
135 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
145 nfnl_queue_msg_put(msg);
149 static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
150 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
152 struct nfnl_queue_msg *msg;
155 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
158 err = pp->pp_cb((
struct nl_object *) msg, pp);
159 nfnl_queue_msg_put(msg);
165 static struct nl_msg *
166 __nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg,
169 struct nl_msg *nlmsg;
170 struct nfqnl_msg_verdict_hdr verdict;
173 nfnl_queue_msg_get_family(msg),
174 nfnl_queue_msg_get_group(msg));
178 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
179 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
180 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
181 goto nla_put_failure;
183 if (nfnl_queue_msg_test_mark(msg) &&
185 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
186 goto nla_put_failure;
196 nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
198 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
202 nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
204 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
213 int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
214 const struct nfnl_queue_msg *msg)
216 struct nl_msg *nlmsg;
219 nlmsg = nfnl_queue_msg_build_verdict(msg);
227 return wait_for_ack(nlh);
236 int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
237 const struct nfnl_queue_msg *msg)
239 struct nl_msg *nlmsg;
242 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
250 return wait_for_ack(nlh);
261 int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
262 const struct nfnl_queue_msg *msg,
263 const void *payload_data,
unsigned payload_len)
265 struct nl_msg *nlmsg;
270 nlmsg = nfnl_queue_msg_build_verdict(msg);
274 memset(iov, 0,
sizeof(iov));
276 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
277 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
279 nla.nla_type = NFQA_PAYLOAD;
280 nla.nla_len = payload_len +
sizeof(nla);
281 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
283 iov[1].iov_base = (
void *) &nla;
284 iov[1].iov_len =
sizeof(nla);
286 iov[2].iov_base = (
void *) payload_data;
287 iov[2].iov_len = NLA_ALIGN(payload_len);
295 return wait_for_ack(nlh);
298 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
299 static struct nl_cache_ops nfnl_queue_msg_ops = {
300 .co_name =
"netfilter/queue_msg",
301 .co_hdrsize = NFNL_HDRLEN,
303 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
304 END_OF_MSGTYPES_LIST,
306 .co_protocol = NETLINK_NETFILTER,
307 .co_msg_parser = queue_msg_parser,
308 .co_obj_ops = &queue_msg_obj_ops,
311 static void __init nfnl_msg_queue_init(
void)
316 static void __exit nfnl_queue_msg_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.
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
Finalize Netlink message.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Attribute validation policy.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
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 * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
Transmit Netlink message (taking IO vector)
int nla_len(const struct nlattr *nla)
Return length of the payload .
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from message.
uint16_t minlen
Minimal length of payload required.
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.
uint16_t nfnlmsg_res_id(struct nlmsghdr *nlh)
Get netfilter resource id from message.