21 #include <sys/types.h> 22 #include <linux/netfilter/nfnetlink_queue.h> 24 #include <netlink-private/netlink.h> 25 #include <netlink/attr.h> 26 #include <netlink/netfilter/nfnl.h> 27 #include <netlink/netfilter/queue_msg.h> 28 #include <netlink-private/utils.h> 30 static struct nl_cache_ops nfnl_queue_msg_ops;
32 static struct nla_policy queue_policy[NFQA_MAX+1] = {
34 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
36 [NFQA_VERDICT_HDR] = {
37 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
39 [NFQA_MARK] = { .type =
NLA_U32 },
41 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
43 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
44 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
45 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
46 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
48 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
52 int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
53 struct nfnl_queue_msg **result)
55 struct nfnl_queue_msg *msg;
56 struct nlattr *tb[NFQA_MAX+1];
60 msg = nfnl_queue_msg_alloc();
64 msg->ce_msgtype = nlh->nlmsg_type;
66 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
74 attr = tb[NFQA_PACKET_HDR];
76 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
78 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
80 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
81 nfnl_queue_msg_set_hook(msg, hdr->hook);
86 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
88 attr = tb[NFQA_TIMESTAMP];
90 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
93 tv.tv_sec = ntohll(timestamp->sec);
94 tv.tv_usec = ntohll(timestamp->usec);
95 nfnl_queue_msg_set_timestamp(msg, &tv);
98 attr = tb[NFQA_IFINDEX_INDEV];
100 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
102 attr = tb[NFQA_IFINDEX_OUTDEV];
104 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
106 attr = tb[NFQA_IFINDEX_PHYSINDEV];
108 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
110 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
112 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
114 attr = tb[NFQA_HWADDR];
116 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
118 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
119 ntohs(hw->hw_addrlen));
122 attr = tb[NFQA_PAYLOAD];
124 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
134 nfnl_queue_msg_put(msg);
138 static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
139 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
141 struct nfnl_queue_msg *msg;
144 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
147 err = pp->pp_cb((
struct nl_object *) msg, pp);
148 nfnl_queue_msg_put(msg);
154 static struct nl_msg *
155 __nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg,
158 struct nl_msg *nlmsg;
159 struct nfqnl_msg_verdict_hdr verdict;
162 nfnl_queue_msg_get_family(msg),
163 nfnl_queue_msg_get_group(msg));
167 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
168 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
169 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
170 goto nla_put_failure;
172 if (nfnl_queue_msg_test_mark(msg) &&
174 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
175 goto nla_put_failure;
185 nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
187 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
191 nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
193 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
202 int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
203 const struct nfnl_queue_msg *msg)
205 struct nl_msg *nlmsg;
208 nlmsg = nfnl_queue_msg_build_verdict(msg);
216 return wait_for_ack(nlh);
225 int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
226 const struct nfnl_queue_msg *msg)
228 struct nl_msg *nlmsg;
231 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
239 return wait_for_ack(nlh);
250 int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
251 const struct nfnl_queue_msg *msg,
252 const void *payload_data,
unsigned payload_len)
254 struct nl_msg *nlmsg;
259 nlmsg = nfnl_queue_msg_build_verdict(msg);
263 memset(iov, 0,
sizeof(iov));
265 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
266 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
268 nla.nla_type = NFQA_PAYLOAD;
269 nla.nla_len = payload_len +
sizeof(nla);
270 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
272 iov[1].iov_base = (
void *) &nla;
273 iov[1].iov_len =
sizeof(nla);
275 iov[2].iov_base = (
void *) payload_data;
276 iov[2].iov_len = NLA_ALIGN(payload_len);
284 return wait_for_ack(nlh);
287 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type)) 288 static struct nl_cache_ops nfnl_queue_msg_ops = {
289 .co_name =
"netfilter/queue_msg",
290 .co_hdrsize = NFNL_HDRLEN,
292 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
293 END_OF_MSGTYPES_LIST,
295 .co_protocol = NETLINK_NETFILTER,
296 .co_msg_parser = queue_msg_parser,
297 .co_obj_ops = &queue_msg_obj_ops,
300 static void __init nfnl_msg_queue_init(
void)
305 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.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a 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 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.