00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <sys/types.h>
00020 #include <linux/netfilter/nfnetlink_queue.h>
00021
00022 #include <netlink-local.h>
00023 #include <netlink/attr.h>
00024 #include <netlink/netfilter/nfnl.h>
00025 #include <netlink/netfilter/queue_msg.h>
00026
00027 static struct nl_cache_ops nfnl_queue_msg_ops;
00028
00029 #if __BYTE_ORDER == __BIG_ENDIAN
00030 static uint64_t ntohll(uint64_t x)
00031 {
00032 return x;
00033 }
00034 #elif __BYTE_ORDER == __LITTLE_ENDIAN
00035 static uint64_t ntohll(uint64_t x)
00036 {
00037 return __bswap_64(x);
00038 }
00039 #endif
00040
00041 static struct nla_policy queue_policy[NFQA_MAX+1] = {
00042 [NFQA_PACKET_HDR] = {
00043 .minlen = sizeof(struct nfqnl_msg_packet_hdr),
00044 },
00045 [NFQA_VERDICT_HDR] = {
00046 .minlen = sizeof(struct nfqnl_msg_verdict_hdr),
00047 },
00048 [NFQA_MARK] = { .type = NLA_U32 },
00049 [NFQA_TIMESTAMP] = {
00050 .minlen = sizeof(struct nfqnl_msg_packet_timestamp),
00051 },
00052 [NFQA_IFINDEX_INDEV] = { .type = NLA_U32 },
00053 [NFQA_IFINDEX_OUTDEV] = { .type = NLA_U32 },
00054 [NFQA_IFINDEX_PHYSINDEV] = { .type = NLA_U32 },
00055 [NFQA_IFINDEX_PHYSOUTDEV] = { .type = NLA_U32 },
00056 [NFQA_HWADDR] = {
00057 .minlen = sizeof(struct nfqnl_msg_packet_hw),
00058 },
00059 };
00060
00061 int nfnlmsg_queue_msg_parse(struct nlmsghdr *nlh,
00062 struct nfnl_queue_msg **result)
00063 {
00064 struct nfnl_queue_msg *msg;
00065 struct nlattr *tb[NFQA_MAX+1];
00066 struct nlattr *attr;
00067 int err;
00068
00069 msg = nfnl_queue_msg_alloc();
00070 if (!msg)
00071 return -NLE_NOMEM;
00072
00073 msg->ce_msgtype = nlh->nlmsg_type;
00074
00075 err = nlmsg_parse(nlh, sizeof(struct nfgenmsg), tb, NFQA_MAX,
00076 queue_policy);
00077 if (err < 0)
00078 goto errout;
00079
00080 nfnl_queue_msg_set_group(msg, nfnlmsg_res_id(nlh));
00081 nfnl_queue_msg_set_family(msg, nfnlmsg_family(nlh));
00082
00083 attr = tb[NFQA_PACKET_HDR];
00084 if (attr) {
00085 struct nfqnl_msg_packet_hdr *hdr = nla_data(attr);
00086
00087 nfnl_queue_msg_set_packetid(msg, ntohl(hdr->packet_id));
00088 if (hdr->hw_protocol)
00089 nfnl_queue_msg_set_hwproto(msg, hdr->hw_protocol);
00090 nfnl_queue_msg_set_hook(msg, hdr->hook);
00091 }
00092
00093 attr = tb[NFQA_MARK];
00094 if (attr)
00095 nfnl_queue_msg_set_mark(msg, ntohl(nla_get_u32(attr)));
00096
00097 attr = tb[NFQA_TIMESTAMP];
00098 if (attr) {
00099 struct nfqnl_msg_packet_timestamp *timestamp = nla_data(attr);
00100 struct timeval tv;
00101
00102 tv.tv_sec = ntohll(timestamp->sec);
00103 tv.tv_usec = ntohll(timestamp->usec);
00104 nfnl_queue_msg_set_timestamp(msg, &tv);
00105 }
00106
00107 attr = tb[NFQA_IFINDEX_INDEV];
00108 if (attr)
00109 nfnl_queue_msg_set_indev(msg, ntohl(nla_get_u32(attr)));
00110
00111 attr = tb[NFQA_IFINDEX_OUTDEV];
00112 if (attr)
00113 nfnl_queue_msg_set_outdev(msg, ntohl(nla_get_u32(attr)));
00114
00115 attr = tb[NFQA_IFINDEX_PHYSINDEV];
00116 if (attr)
00117 nfnl_queue_msg_set_physindev(msg, ntohl(nla_get_u32(attr)));
00118
00119 attr = tb[NFQA_IFINDEX_PHYSOUTDEV];
00120 if (attr)
00121 nfnl_queue_msg_set_physoutdev(msg, ntohl(nla_get_u32(attr)));
00122
00123 attr = tb[NFQA_HWADDR];
00124 if (attr) {
00125 struct nfqnl_msg_packet_hw *hw = nla_data(attr);
00126
00127 nfnl_queue_msg_set_hwaddr(msg, hw->hw_addr,
00128 ntohs(hw->hw_addrlen));
00129 }
00130
00131 attr = tb[NFQA_PAYLOAD];
00132 if (attr) {
00133 err = nfnl_queue_msg_set_payload(msg, nla_data(attr),
00134 nla_len(attr));
00135 if (err < 0)
00136 goto errout;
00137 }
00138
00139 *result = msg;
00140 return 0;
00141
00142 errout:
00143 nfnl_queue_msg_put(msg);
00144 return err;
00145 }
00146
00147 static int queue_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00148 struct nlmsghdr *nlh, struct nl_parser_param *pp)
00149 {
00150 struct nfnl_queue_msg *msg;
00151 int err;
00152
00153 if ((err = nfnlmsg_queue_msg_parse(nlh, &msg)) < 0)
00154 goto errout;
00155
00156 err = pp->pp_cb((struct nl_object *) msg, pp);
00157 errout:
00158 nfnl_queue_msg_put(msg);
00159 return err;
00160 }
00161
00162
00163
00164 struct nl_msg *nfnl_queue_msg_build_verdict(const struct nfnl_queue_msg *msg)
00165 {
00166 struct nl_msg *nlmsg;
00167 struct nfqnl_msg_verdict_hdr verdict;
00168
00169 nlmsg = nfnlmsg_alloc_simple(NFNL_SUBSYS_QUEUE, NFQNL_MSG_VERDICT, 0,
00170 nfnl_queue_msg_get_family(msg),
00171 nfnl_queue_msg_get_group(msg));
00172 if (nlmsg == NULL)
00173 return NULL;
00174
00175 verdict.id = htonl(nfnl_queue_msg_get_packetid(msg));
00176 verdict.verdict = htonl(nfnl_queue_msg_get_verdict(msg));
00177 if (nla_put(nlmsg, NFQA_VERDICT_HDR, sizeof(verdict), &verdict) < 0)
00178 goto nla_put_failure;
00179
00180 if (nfnl_queue_msg_test_mark(msg) &&
00181 nla_put_u32(nlmsg, NFQA_MARK,
00182 ntohl(nfnl_queue_msg_get_mark(msg))) < 0)
00183 goto nla_put_failure;
00184
00185 return nlmsg;
00186
00187 nla_put_failure:
00188 nlmsg_free(nlmsg);
00189 return NULL;
00190 }
00191
00192 int nfnl_queue_msg_send_verdict(struct nl_sock *nlh,
00193 const struct nfnl_queue_msg *msg)
00194 {
00195 struct nl_msg *nlmsg;
00196 int err;
00197
00198 nlmsg = nfnl_queue_msg_build_verdict(msg);
00199 if (nlmsg == NULL)
00200 return -NLE_NOMEM;
00201
00202 err = nl_send_auto_complete(nlh, nlmsg);
00203 nlmsg_free(nlmsg);
00204 if (err < 0)
00205 return err;
00206 return wait_for_ack(nlh);
00207 }
00208
00209 #define NFNLMSG_QUEUE_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_QUEUE, (type))
00210 static struct nl_cache_ops nfnl_queue_msg_ops = {
00211 .co_name = "netfilter/queue_msg",
00212 .co_hdrsize = NFNL_HDRLEN,
00213 .co_msgtypes = {
00214 { NFNLMSG_QUEUE_TYPE(NFQNL_MSG_PACKET), NL_ACT_NEW, "new" },
00215 END_OF_MSGTYPES_LIST,
00216 },
00217 .co_protocol = NETLINK_NETFILTER,
00218 .co_msg_parser = queue_msg_parser,
00219 .co_obj_ops = &queue_msg_obj_ops,
00220 };
00221
00222 static void __init nfnl_msg_queue_init(void)
00223 {
00224 nl_cache_mngt_register(&nfnl_queue_msg_ops);
00225 }
00226
00227 static void __exit nfnl_queue_msg_exit(void)
00228 {
00229 nl_cache_mngt_unregister(&nfnl_queue_msg_ops);
00230 }
00231
00232