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