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);
160 nfnl_queue_msg_put(msg);
166 static struct nl_msg *
167 __nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg,
170 struct nl_msg *nlmsg;
171 struct nfqnl_msg_verdict_hdr verdict;
174 nfnl_queue_msg_get_family(msg),
175 nfnl_queue_msg_get_group(msg));
179 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
180 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
181 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
182 goto nla_put_failure;
184 if (nfnl_queue_msg_test_mark(msg) &&
186 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
187 goto nla_put_failure;
197 nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
199 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
203 nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
205 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
214 int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
215 const struct nfnl_queue_msg *msg)
217 struct nl_msg *nlmsg;
220 nlmsg = nfnl_queue_msg_build_verdict(msg);
228 return wait_for_ack(nlh);
237 int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
238 const struct nfnl_queue_msg *msg)
240 struct nl_msg *nlmsg;
243 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
251 return wait_for_ack(nlh);
262 int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
263 const struct nfnl_queue_msg *msg,
264 const void *payload_data,
unsigned payload_len)
266 struct nl_msg *nlmsg;
271 nlmsg = nfnl_queue_msg_build_verdict(msg);
275 memset(iov, 0,
sizeof(iov));
277 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
278 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
280 nla.nla_type = NFQA_PAYLOAD;
281 nla.nla_len = payload_len +
sizeof(nla);
282 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
284 iov[1].iov_base = (
void *) &nla;
285 iov[1].iov_len =
sizeof(nla);
287 iov[2].iov_base = (
void *) payload_data;
288 iov[2].iov_len = NLA_ALIGN(payload_len);
296 return wait_for_ack(nlh);
299 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
300 static struct nl_cache_ops nfnl_queue_msg_ops = {
301 .co_name =
"netfilter/queue_msg",
302 .co_hdrsize = NFNL_HDRLEN,
304 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
305 END_OF_MSGTYPES_LIST,
307 .co_protocol = NETLINK_NETFILTER,
308 .co_msg_parser = queue_msg_parser,
309 .co_obj_ops = &queue_msg_obj_ops,
312 static void __init nfnl_msg_queue_init(
void)
317 static void __exit nfnl_queue_msg_exit(
void)