20 #include <sys/types.h>
21 #include <linux/netfilter/nfnetlink_queue.h>
23 #include <netlink-local.h>
24 #include <netlink/attr.h>
25 #include <netlink/netfilter/nfnl.h>
26 #include <netlink/netfilter/queue_msg.h>
30 #if __BYTE_ORDER == __BIG_ENDIAN
31 static uint64_t ntohll(uint64_t x)
35 #elif __BYTE_ORDER == __LITTLE_ENDIAN
36 static uint64_t ntohll(uint64_t x)
42 static struct nla_policy queue_policy[NFQA_MAX+1] = {
44 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
46 [NFQA_VERDICT_HDR] = {
47 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
49 [NFQA_MARK] = { .type =
NLA_U32 },
51 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
53 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
54 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
55 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
56 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
58 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
62 int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
63 struct nfnl_queue_msg **result)
65 struct nfnl_queue_msg *msg;
66 struct nlattr *tb[NFQA_MAX+1];
70 msg = nfnl_queue_msg_alloc();
74 msg->ce_msgtype = nlh->nlmsg_type;
76 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
84 attr = tb[NFQA_PACKET_HDR];
86 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
88 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
90 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
91 nfnl_queue_msg_set_hook(msg, hdr->hook);
96 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
98 attr = tb[NFQA_TIMESTAMP];
100 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
103 tv.tv_sec = ntohll(timestamp->sec);
104 tv.tv_usec = ntohll(timestamp->usec);
105 nfnl_queue_msg_set_timestamp(msg, &tv);
108 attr = tb[NFQA_IFINDEX_INDEV];
110 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
112 attr = tb[NFQA_IFINDEX_OUTDEV];
114 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
116 attr = tb[NFQA_IFINDEX_PHYSINDEV];
118 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
120 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
122 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
124 attr = tb[NFQA_HWADDR];
126 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
128 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
129 ntohs(hw->hw_addrlen));
132 attr = tb[NFQA_PAYLOAD];
134 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
144 nfnl_queue_msg_put(msg);
148 static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
151 struct nfnl_queue_msg *msg;
154 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
159 nfnl_queue_msg_put(msg);
165 struct nl_msg *nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
167 struct nl_msg *nlmsg;
168 struct nfqnl_msg_verdict_hdr verdict;
171 nfnl_queue_msg_get_family(msg),
172 nfnl_queue_msg_get_group(msg));
176 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
177 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
178 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
179 goto nla_put_failure;
181 if (nfnl_queue_msg_test_mark(msg) &&
183 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
184 goto nla_put_failure;
199 int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
200 const struct nfnl_queue_msg *msg)
202 struct nl_msg *nlmsg;
205 nlmsg = nfnl_queue_msg_build_verdict(msg);
209 err = nl_send_auto_complete(nlh, nlmsg);
213 return wait_for_ack(nlh);
224 int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
225 const struct nfnl_queue_msg *msg,
226 const void *payload_data,
unsigned payload_len)
228 struct nl_msg *nlmsg;
233 nlmsg = nfnl_queue_msg_build_verdict(msg);
237 memset(iov, 0,
sizeof(iov));
239 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
240 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
242 nla.nla_type = NFQA_PAYLOAD;
243 nla.nla_len = payload_len +
sizeof(nla);
244 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
246 iov[1].iov_base = (
void *) &nla;
247 iov[1].iov_len =
sizeof(nla);
249 iov[2].iov_base = (
void *) payload_data;
250 iov[2].iov_len = NLA_ALIGN(payload_len);
252 nl_complete_msg(nlh, nlmsg);
258 return wait_for_ack(nlh);
261 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
263 .
co_name =
"netfilter/queue_msg",
264 .co_hdrsize = NFNL_HDRLEN,
266 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
267 END_OF_MSGTYPES_LIST,
269 .co_protocol = NETLINK_NETFILTER,
270 .co_msg_parser = queue_msg_parser,
271 .co_obj_ops = &queue_msg_obj_ops,
274 static void __init nfnl_msg_queue_init(
void)
279 static void __exit nfnl_queue_msg_exit(
void)