2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
15 #include <libmnl/libmnl.h>
17 #ifndef __aligned_be64
18 #define __aligned_be64 __be64 __attribute__((aligned(8)))
19 #define __aligned_le64 __le64 __attribute__((aligned(8)))
22 #include <linux/netfilter/nfnetlink_queue.h>
24 #include <libnetfilter_queue/libnetfilter_queue.h>
29 * \defgroup nfq_verd Verdict helpers
33 void nfq_nlmsg_verdict_put(struct nlmsghdr *nlh, int id, int verdict)
35 struct nfqnl_msg_verdict_hdr vh = {
36 .verdict = htonl(verdict),
39 mnl_attr_put(nlh, NFQA_VERDICT_HDR, sizeof(vh), &vh);
41 EXPORT_SYMBOL(nfq_nlmsg_verdict_put);
43 void nfq_nlmsg_verdict_put_mark(struct nlmsghdr *nlh, uint32_t mark)
45 mnl_attr_put_u32(nlh, NFQA_MARK, htonl(mark));
47 EXPORT_SYMBOL(nfq_nlmsg_verdict_put_mark);
50 nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt, uint32_t plen)
52 mnl_attr_put(nlh, NFQA_PAYLOAD, plen, pkt);
54 EXPORT_SYMBOL(nfq_nlmsg_verdict_put_pkt);
61 * \defgroup nfq_cfg Config helpers
66 * nfq_nlmsg_cfg_build_request- build netlink config message
67 * \param buf Buffer where netlink message is going to be written.
68 * \param cfg Structure that contains the config parameters.
69 * \param command nfqueue nfnetlink command.
71 * This function returns a pointer to the netlink message. If something goes
72 * wrong it returns NULL.
74 * Possible commands are:
76 * - NFQNL_CFG_CMD_NONE: Do nothing. It can be useful to know if the queue
77 * subsystem is working.
78 * - NFQNL_CFG_CMD_BIND: Binds the program to a specific queue.
79 * - NFQNL_CFG_CMD_UNBIND: Unbinds the program to a specifiq queue.
80 * - NFQNL_CFG_CMD_PF_BIND: Binds to process packets belonging to the given
81 * protocol family (ie. PF_INET, PF_INET6, etc).
82 * - NFQNL_CFG_CMD_PF_UNBIND: Unbinds from processing packets belonging to the
83 * given protocol family.
85 void nfq_nlmsg_cfg_put_cmd(struct nlmsghdr *nlh, uint16_t pf, uint8_t cmd)
87 struct nfqnl_msg_config_cmd command = {
91 mnl_attr_put(nlh, NFQA_CFG_CMD, sizeof(command), &command);
93 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_cmd);
95 void nfq_nlmsg_cfg_put_params(struct nlmsghdr *nlh, uint8_t mode, int range)
97 struct nfqnl_msg_config_params params = {
98 .copy_range = htonl(range),
101 mnl_attr_put(nlh, NFQA_CFG_PARAMS, sizeof(params), ¶ms);
103 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_params);
105 void nfq_nlmsg_cfg_put_qmaxlen(struct nlmsghdr *nlh, uint32_t queue_maxlen)
107 mnl_attr_put_u32(nlh, NFQA_CFG_QUEUE_MAXLEN, htonl(queue_maxlen));
109 EXPORT_SYMBOL(nfq_nlmsg_cfg_put_qmaxlen);
116 * \defgroup nlmsg Netlink message helper functions
120 static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
122 const struct nlattr **tb = data;
123 int type = mnl_attr_get_type(attr);
125 /* skip unsupported attribute in user-space */
126 if (mnl_attr_type_valid(attr, NFQA_MAX) < 0)
131 case NFQA_IFINDEX_INDEV:
132 case NFQA_IFINDEX_OUTDEV:
133 case NFQA_IFINDEX_PHYSINDEV:
134 case NFQA_IFINDEX_PHYSOUTDEV:
135 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
139 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
140 sizeof(struct nfqnl_msg_packet_timestamp)) < 0) {
145 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
146 sizeof(struct nfqnl_msg_packet_hw)) < 0) {
158 * nfq_pkt_parse - set packet attributes from netlink message
159 * \param nlh netlink message that you want to read.
160 * \param pkt pointer to the packet to set.
162 * This function returns MNL_CB_ERROR if any error occurs, or MNL_CB_OK on
165 int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
167 return mnl_attr_parse(nlh, sizeof(struct nfgenmsg),
168 nfq_pkt_parse_attr_cb, attr);
170 EXPORT_SYMBOL(nfq_nlmsg_parse);