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> 27 #include <netlink-private/utils.h> 29 static struct nl_cache_ops nfnl_queue_msg_ops;
31 static struct nla_policy queue_policy[NFQA_MAX+1] = {
33 .
minlen =
sizeof(
struct nfqnl_msg_packet_hdr),
35 [NFQA_VERDICT_HDR] = {
36 .minlen =
sizeof(
struct nfqnl_msg_verdict_hdr),
38 [NFQA_MARK] = { .type =
NLA_U32 },
40 .minlen =
sizeof(
struct nfqnl_msg_packet_timestamp),
42 [NFQA_IFINDEX_INDEV] = { .type =
NLA_U32 },
43 [NFQA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
44 [NFQA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
45 [NFQA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
47 .minlen =
sizeof(
struct nfqnl_msg_packet_hw),
51 int nfnlmsg_queue_msg_parse(
struct nlmsghdr *nlh,
52 struct nfnl_queue_msg **result)
54 struct nfnl_queue_msg *msg;
55 struct nlattr *tb[NFQA_MAX+1];
59 msg = nfnl_queue_msg_alloc();
63 msg->ce_msgtype = nlh->nlmsg_type;
65 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFQA_MAX,
73 attr = tb[NFQA_PACKET_HDR];
75 struct nfqnl_msg_packet_hdr *hdr =
nla_data(attr);
77 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
79 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
80 nfnl_queue_msg_set_hook(msg, hdr->hook);
85 nfnl_queue_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
87 attr = tb[NFQA_TIMESTAMP];
89 struct nfqnl_msg_packet_timestamp *timestamp =
nla_data(attr);
92 tv.tv_sec = ntohll(timestamp->sec);
93 tv.tv_usec = ntohll(timestamp->usec);
94 nfnl_queue_msg_set_timestamp(msg, &tv);
97 attr = tb[NFQA_IFINDEX_INDEV];
99 nfnl_queue_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
101 attr = tb[NFQA_IFINDEX_OUTDEV];
103 nfnl_queue_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
105 attr = tb[NFQA_IFINDEX_PHYSINDEV];
107 nfnl_queue_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
109 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
111 nfnl_queue_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
113 attr = tb[NFQA_HWADDR];
115 struct nfqnl_msg_packet_hw *hw =
nla_data(attr);
117 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
118 ntohs(hw->hw_addrlen));
121 attr = tb[NFQA_PAYLOAD];
123 err = nfnl_queue_msg_set_payload(msg,
nla_data(attr),
133 nfnl_queue_msg_put(msg);
137 static int queue_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
138 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
140 struct nfnl_queue_msg *msg;
143 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
146 err = pp->pp_cb((
struct nl_object *) msg, pp);
147 nfnl_queue_msg_put(msg);
153 static struct nl_msg *
154 __nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg,
157 struct nl_msg *nlmsg;
158 struct nfqnl_msg_verdict_hdr verdict;
161 nfnl_queue_msg_get_family(msg),
162 nfnl_queue_msg_get_group(msg));
166 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
167 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
168 if (
nla_put(nlmsg, NFQA_VERDICT_HDR,
sizeof(verdict), &verdict) < 0)
169 goto nla_put_failure;
171 if (nfnl_queue_msg_test_mark(msg) &&
173 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
174 goto nla_put_failure;
184 nfnl_queue_msg_build_verdict(
const struct nfnl_queue_msg *msg)
186 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT);
190 nfnl_queue_msg_build_verdict_batch(
const struct nfnl_queue_msg *msg)
192 return __nfnl_queue_msg_build_verdict(msg, NFQNL_MSG_VERDICT_BATCH);
201 int nfnl_queue_msg_send_verdict(
struct nl_sock *nlh,
202 const struct nfnl_queue_msg *msg)
204 struct nl_msg *nlmsg;
207 nlmsg = nfnl_queue_msg_build_verdict(msg);
215 return wait_for_ack(nlh);
224 int nfnl_queue_msg_send_verdict_batch(
struct nl_sock *nlh,
225 const struct nfnl_queue_msg *msg)
227 struct nl_msg *nlmsg;
230 nlmsg = nfnl_queue_msg_build_verdict_batch(msg);
238 return wait_for_ack(nlh);
249 int nfnl_queue_msg_send_verdict_payload(
struct nl_sock *nlh,
250 const struct nfnl_queue_msg *msg,
251 const void *payload_data,
unsigned payload_len)
253 struct nl_msg *nlmsg;
258 nlmsg = nfnl_queue_msg_build_verdict(msg);
262 memset(iov, 0,
sizeof(iov));
264 iov[0].iov_base = (
void *)
nlmsg_hdr(nlmsg);
265 iov[0].iov_len =
nlmsg_hdr(nlmsg)->nlmsg_len;
267 nla.nla_type = NFQA_PAYLOAD;
268 nla.nla_len = payload_len +
sizeof(nla);
269 nlmsg_hdr(nlmsg)->nlmsg_len += nla.nla_len;
271 iov[1].iov_base = (
void *) &nla;
272 iov[1].iov_len =
sizeof(nla);
274 iov[2].iov_base = (
void *) payload_data;
275 iov[2].iov_len = NLA_ALIGN(payload_len);
283 return wait_for_ack(nlh);
286 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type)) 287 static struct nl_cache_ops nfnl_queue_msg_ops = {
288 .co_name =
"netfilter/queue_msg",
289 .co_hdrsize = NFNL_HDRLEN,
291 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW,
"new" },
292 END_OF_MSGTYPES_LIST,
294 .co_protocol = NETLINK_NETFILTER,
295 .co_msg_parser = queue_msg_parser,
296 .co_obj_ops = &queue_msg_obj_ops,
299 static void __init nfnl_msg_queue_init(
void)
304 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.