1 /* libnetfilter_queue.c: generic library for access to nf_queue
3 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2005, 2008-2010 by Pablo Neira Ayuso <pablo@netfilter.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation (or any later at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * 2006-01-23 Andreas Florath <andreas@florath.net>
20 * Fix __set_verdict() that it can now handle payload.
30 #include <netinet/in.h>
31 #include <sys/socket.h>
33 #include <libnfnetlink/libnfnetlink.h>
34 #include <libnetfilter_queue/libnetfilter_queue.h>
39 * libnetfilter_queue is a userspace library providing an API to packets that
40 * have been queued by the kernel packet filter. It is is part of a system that
41 * deprecates the old ip_queue / libipq mechanism.
43 * libnetfilter_queue homepage is:
44 * http://netfilter.org/projects/libnetfilter_queue/
46 * \section Dependencies
47 * libnetfilter_queue requires libnfnetlink and a kernel that includes the
48 * nfnetlink_queue subsystem (i.e. 2.6.14 or later).
50 * \section Main Features
51 * - receiving queued packets from the kernel nfnetlink_queue subsystem
52 * - issuing verdicts and/or reinjecting altered packets to the kernel
53 * nfnetlink_queue subsystem
56 * The current development version of libnetfilter_queue can be accessed
57 * at https://git.netfilter.org/cgi-bin/gitweb.cgi?p=libnetfilter_queue.git;a=summary.
60 * You need the CAP_NET_ADMIN capability in order to allow your application
61 * to receive from and to send packets to kernel-space.
63 * \section Using libnetfilter_queue
65 * To write your own program using libnetfilter_queue, you should start by reading
66 * the doxygen documentation (start by \link LibrarySetup \endlink page) and
67 * nf-queue.c source file.
69 * \section errors ENOBUFS errors in recv()
71 * recv() may return -1 and errno is set to ENOBUFS in case that your
72 * application is not fast enough to retrieve the packets from the kernel.
73 * In that case, you can increase the socket buffer size by means of
74 * nfnl_rcvbufsiz(). Although this delays the appearance of ENOBUFS errors,
75 * you may hit it again sooner or later. The next section provides some hints
76 * on how to obtain the best performance for your application.
78 * \section perf Performance
79 * To improve your libnetfilter_queue application in terms of performance,
80 * you may consider the following tweaks:
82 * - increase the default socket buffer size by means of nfnl_rcvbufsiz().
83 * - set nice value of your process to -20 (maximum priority).
84 * - set the CPU affinity of your process to a spare core that is not used
85 * to handle NIC interruptions.
86 * - set NETLINK_NO_ENOBUFS socket option to avoid receiving ENOBUFS errors
87 * (requires Linux kernel >= 2.6.30).
88 * - see --queue-balance option in NFQUEUE target for multi-threaded apps
89 * (it requires Linux kernel >= 2.6.31).
94 struct nfnl_handle *nfnlh;
95 struct nfnl_subsys_handle *nfnlssh;
96 struct nfq_q_handle *qh_list;
101 struct nfq_q_handle *next;
102 struct nfq_handle *h;
110 struct nfattr **data;
115 /***********************************************************************
117 ***********************************************************************/
119 static void del_qh(struct nfq_q_handle *qh)
121 struct nfq_q_handle *cur_qh, *prev_qh = NULL;
123 for (cur_qh = qh->h->qh_list; cur_qh; cur_qh = cur_qh->next) {
126 prev_qh->next = qh->next;
128 qh->h->qh_list = qh->next;
135 static void add_qh(struct nfq_q_handle *qh)
137 qh->next = qh->h->qh_list;
141 static struct nfq_q_handle *find_qh(struct nfq_handle *h, u_int16_t id)
143 struct nfq_q_handle *qh;
145 for (qh = h->qh_list; qh; qh = qh->next) {
152 /* build a NFQNL_MSG_CONFIG message */
154 __build_send_cfg_msg(struct nfq_handle *h, u_int8_t command,
155 u_int16_t queuenum, u_int16_t pf)
158 char buf[NFNL_HEADER_LEN
159 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_cmd))];
162 struct nfqnl_msg_config_cmd cmd;
164 nfnl_fill_hdr(h->nfnlssh, &u.nmh, 0, AF_UNSPEC, queuenum,
165 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
167 cmd.command = command;
169 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_CMD, &cmd, sizeof(cmd));
171 return nfnl_query(h->nfnlh, &u.nmh);
174 static int __nfq_rcv_pkt(struct nlmsghdr *nlh, struct nfattr *nfa[],
177 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
178 struct nfq_handle *h = data;
179 u_int16_t queue_num = ntohs(nfmsg->res_id);
180 struct nfq_q_handle *qh = find_qh(h, queue_num);
181 struct nfq_data nfqa;
191 return qh->cb(qh, nfmsg, &nfqa, qh->data);
194 static struct nfnl_callback pkt_cb = {
195 .call = &__nfq_rcv_pkt,
196 .attr_count = NFQA_MAX,
199 /* public interface */
201 struct nfnl_handle *nfq_nfnlh(struct nfq_handle *h)
208 * \defgroup Queue Queue handling [DEPRECATED]
210 * Once libnetfilter_queue library has been initialised (See
211 * \link LibrarySetup \endlink), it is possible to bind the program to a
212 * specific queue. This can be done by using nfq_create_queue().
214 * The queue can then be tuned via nfq_set_mode() or nfq_set_queue_maxlen().
216 * Here's a little code snippet that create queue numbered 0:
218 printf("binding this socket to queue '0'\n");
219 qh = nfq_create_queue(h, 0, &cb, NULL);
221 fprintf(stderr, "error during nfq_create_queue()\n");
225 printf("setting copy_packet mode\n");
226 if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
227 fprintf(stderr, "can't set packet_copy mode\n");
232 * Next step is the handling of incoming packets which can be done via a loop:
237 while ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
238 printf("pkt received\n");
239 nfq_handle_packet(h, buf, rv);
242 * When the decision on a packet has been choosed, the verdict has to be given
243 * by calling nfq_set_verdict() or nfq_set_verdict2(). The verdict
244 * determines the destiny of the packet as follows:
246 * - NF_DROP discarded the packet
247 * - NF_ACCEPT the packet passes, continue iterations
248 * - NF_QUEUE inject the packet into a different queue
249 * (the target queue number is in the high 16 bits of the verdict)
250 * - NF_REPEAT iterate the same cycle once more
251 * - NF_STOP accept, but don't continue iterations
253 * The verdict NF_STOLEN must not be used, as it has special meaning in the
255 * When using NF_REPEAT, one way to prevent re-queueing of the same packet
256 * is to also set an nfmark using nfq_set_verdict2, and set up the nefilter
257 * rules to only queue a packet when the mark is not (yet) set.
259 * Data and information about the packet can be fetch by using message parsing
260 * functions (See \link Parsing \endlink).
265 * nfq_fd - get the file descriptor associated with the nfqueue handler
266 * \param h Netfilter queue connection handle obtained via call to nfq_open()
268 * \return a file descriptor for the netlink connection associated with the
269 * given queue connection handle. The file descriptor can then be used for
270 * receiving the queued packets for processing.
272 * This function returns a file descriptor that can be used for communication
273 * over the netlink connection associated with the given queue connection
276 int nfq_fd(struct nfq_handle *h)
278 return nfnl_fd(nfq_nfnlh(h));
286 * \defgroup LibrarySetup Library setup [DEPRECATED]
288 * Library initialisation is made in two steps.
290 * First step is to call nfq_open() to open a NFQUEUE handler.
292 * Second step is to tell the kernel that userspace queueing is handle by
293 * NFQUEUE for the selected protocol. This is made by calling nfq_unbind_pf()
294 * and nfq_bind_pf() with protocol information. The idea behind this is to
295 * enable simultaneously loaded modules to be used for queuing.
297 * Here's a little code snippet that bind with AF_INET:
301 fprintf(stderr, "error during nfq_open()\n");
305 printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
306 if (nfq_unbind_pf(h, AF_INET) < 0) {
307 fprintf(stderr, "error during nfq_unbind_pf()\n");
311 printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
312 if (nfq_bind_pf(h, AF_INET) < 0) {
313 fprintf(stderr, "error during nfq_bind_pf()\n");
317 * Once this is done, you can setup and use a \link Queue \endlink.
322 * nfq_open - open a nfqueue handler
324 * This function obtains a netfilter queue connection handle. When you are
325 * finished with the handle returned by this function, you should destroy
326 * it by calling nfq_close(). A new netlink connection is obtained internally
327 * and associated with the queue connection handle returned.
329 * \return a pointer to a new queue handle or NULL on failure.
331 struct nfq_handle *nfq_open(void)
333 struct nfnl_handle *nfnlh = nfnl_open();
334 struct nfq_handle *qh;
339 /* unset netlink sequence tracking by default */
340 nfnl_unset_sequence_tracking(nfnlh);
342 qh = nfq_open_nfnl(nfnlh);
354 * nfq_open_nfnl - open a nfqueue handler from a existing nfnetlink handler
355 * \param nfnlh Netfilter netlink connection handle obtained by calling nfnl_open()
357 * This function obtains a netfilter queue connection handle using an existing
358 * netlink connection. This function is used internally to implement
359 * nfq_open(), and should typically not be called directly.
361 * \return a pointer to a new queue handle or NULL on failure.
363 struct nfq_handle *nfq_open_nfnl(struct nfnl_handle *nfnlh)
365 struct nfq_handle *h;
368 h = malloc(sizeof(*h));
372 memset(h, 0, sizeof(*h));
375 h->nfnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_QUEUE,
378 /* FIXME: nfq_errno */
383 err = nfnl_callback_register(h->nfnlssh, NFQNL_MSG_PACKET, &pkt_cb);
391 nfnl_subsys_close(h->nfnlssh);
398 * \addtogroup LibrarySetup
400 * When the program has finished with libnetfilter_queue, it has to call
401 * the nfq_close() function to free all associated resources.
407 * nfq_close - close a nfqueue handler
408 * \param h Netfilter queue connection handle obtained via call to nfq_open()
410 * This function closes the nfqueue handler and free associated resources.
412 * \return 0 on success, non-zero on failure.
414 int nfq_close(struct nfq_handle *h)
418 ret = nfnl_close(h->nfnlh);
425 * nfq_bind_pf - bind a nfqueue handler to a given protocol family
426 * \param h Netfilter queue connection handle obtained via call to nfq_open()
427 * \param pf protocol family to bind to nfqueue handler obtained from nfq_open()
429 * Binds the given queue connection handle to process packets belonging to
430 * the given protocol family (ie. PF_INET, PF_INET6, etc).
432 * \return integer inferior to 0 in case of failure
434 int nfq_bind_pf(struct nfq_handle *h, u_int16_t pf)
436 return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_BIND, 0, pf);
440 * nfq_unbind_pf - unbind nfqueue handler from a protocol family
441 * \param h Netfilter queue connection handle obtained via call to nfq_open()
442 * \param pf protocol family to unbind family from
444 * Unbinds the given queue connection handle from processing packets belonging
445 * to the given protocol family.
447 int nfq_unbind_pf(struct nfq_handle *h, u_int16_t pf)
449 return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_UNBIND, 0, pf);
464 * nfq_create_queue - create a new queue handle and return it.
466 * \param h Netfilter queue connection handle obtained via call to nfq_open()
467 * \param num the number of the queue to bind to
468 * \param cb callback function to call for each queued packet
469 * \param data custom data to pass to the callback function
471 * \return a nfq_q_handle pointing to the newly created queue
473 * Creates a new queue handle, and returns it. The new queue is identified by
474 * #num, and the callback specified by #cb will be called for each enqueued
475 * packet. The #data argument will be passed unchanged to the callback. If
476 * a queue entry with id #num already exists, this function will return failure
477 * and the existing entry is unchanged.
479 * The nfq_callback type is defined in libnetfilter_queue.h as:
481 typedef int nfq_callback(struct nfq_q_handle *qh,
482 struct nfgenmsg *nfmsg,
483 struct nfq_data *nfad, void *data);
487 * - qh The queue handle returned by nfq_create_queue
488 * - nfmsg message objetc that contains the packet
489 * - nfad Netlink packet data handle
490 * - data the value passed to the data parameter of nfq_create_queue
492 * The callback should return < 0 to stop processing.
495 struct nfq_q_handle *nfq_create_queue(struct nfq_handle *h,
501 struct nfq_q_handle *qh;
506 qh = malloc(sizeof(*qh));
508 memset(qh, 0, sizeof(*qh));
514 ret = __build_send_cfg_msg(h, NFQNL_CFG_CMD_BIND, num, 0);
535 * nfq_destroy_queue - destroy a queue handle
536 * \param qh queue handle that we want to destroy created via nfq_create_queue
538 * Removes the binding for the specified queue handle. This call also unbind
539 * from the nfqueue handler, so you don't have to call nfq_unbind_pf.
541 int nfq_destroy_queue(struct nfq_q_handle *qh)
543 int ret = __build_send_cfg_msg(qh->h, NFQNL_CFG_CMD_UNBIND, qh->id, 0);
553 * nfq_handle_packet - handle a packet received from the nfqueue subsystem
554 * \param h Netfilter queue connection handle obtained via call to nfq_open()
555 * \param buf data to pass to the callback
556 * \param len length of packet data in buffer
558 * Triggers an associated callback for the given packet received from the
559 * queue. Packets can be read from the queue using nfq_fd() and recv(). See
560 * example code for nfq_fd().
562 * \return 0 on success, non-zero on failure.
564 int nfq_handle_packet(struct nfq_handle *h, char *buf, int len)
566 return nfnl_handle_packet(h->nfnlh, buf, len);
570 * nfq_set_mode - set the amount of packet data that nfqueue copies to userspace
571 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
572 * \param mode the part of the packet that we are interested in
573 * \param range size of the packet that we want to get
575 * Sets the amount of data to be copied to userspace for each packet queued
576 * to the given queue.
578 * - NFQNL_COPY_NONE - noop, do not use it
579 * - NFQNL_COPY_META - copy only packet metadata
580 * - NFQNL_COPY_PACKET - copy entire packet
582 * \return -1 on error; >=0 otherwise.
584 int nfq_set_mode(struct nfq_q_handle *qh,
585 u_int8_t mode, u_int32_t range)
588 char buf[NFNL_HEADER_LEN
589 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
592 struct nfqnl_msg_config_params params;
594 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
595 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
597 params.copy_range = htonl(range);
598 params.copy_mode = mode;
599 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_PARAMS, ¶ms,
602 return nfnl_query(qh->h->nfnlh, &u.nmh);
606 * nfq_set_queue_flags - set flags (options) for the kernel queue
607 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
608 * \param mask specifies which flag bits to modify
609 * \param flag bitmask of flags
611 * Here's a little code snippet to show how to use this API:
613 uint32_t flags = NFQA_CFG_F_FAIL_OPEN;
614 uint32_t mask = NFQA_CFG_F_FAIL_OPEN;
616 printf("Enabling fail-open on this q\n");
617 err = nfq_set_queue_flags(qh, mask, flags);
619 printf("Disabling fail-open on this q\n");
620 flags &= ~NFQA_CFG_F_FAIL_OPEN;
621 err = nfq_set_queue_flags(qh, mask, flags);
623 * \return -1 on error with errno set appropriately; =0 otherwise.
625 int nfq_set_queue_flags(struct nfq_q_handle *qh,
626 uint32_t mask, uint32_t flags)
629 char buf[NFNL_HEADER_LEN
630 +NFA_LENGTH(sizeof(mask)
631 +NFA_LENGTH(sizeof(flags)))];
636 flags = htonl(flags);
638 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
639 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
641 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_CFG_FLAGS, flags);
642 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_CFG_MASK, mask);
644 return nfnl_query(qh->h->nfnlh, &u.nmh);
648 * nfq_set_queue_maxlen - Set kernel queue maximum length parameter
649 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
650 * \param queuelen the length of the queue
652 * Sets the size of the queue in kernel. This fixes the maximum number
653 * of packets the kernel will store before internally before dropping
656 * \return -1 on error; >=0 otherwise.
658 int nfq_set_queue_maxlen(struct nfq_q_handle *qh,
662 char buf[NFNL_HEADER_LEN
663 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
666 u_int32_t queue_maxlen = htonl(queuelen);
668 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
669 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
671 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_QUEUE_MAXLEN, &queue_maxlen,
672 sizeof(queue_maxlen));
674 return nfnl_query(qh->h->nfnlh, &u.nmh);
681 static int __set_verdict(struct nfq_q_handle *qh, u_int32_t id,
682 u_int32_t verdict, u_int32_t mark, int set_mark,
683 u_int32_t data_len, const unsigned char *data,
684 enum nfqnl_msg_types type)
686 struct nfqnl_msg_verdict_hdr vh;
688 char buf[NFNL_HEADER_LEN
689 +NFA_LENGTH(sizeof(mark))
690 +NFA_LENGTH(sizeof(vh))];
697 /* This must be declared here (and not inside the data
698 * handling block) because the iovec points to this. */
699 struct nfattr data_attr;
701 memset(iov, 0, sizeof(iov));
703 vh.verdict = htonl(verdict);
706 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
707 type, NLM_F_REQUEST);
709 /* add verdict header */
710 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_VERDICT_HDR, &vh, sizeof(vh));
713 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_MARK, mark);
715 iov[0].iov_base = &u.nmh;
716 iov[0].iov_len = NLMSG_TAIL(&u.nmh) - (void *)&u.nmh;
720 /* The typecast here is to cast away data's const-ness: */
721 nfnl_build_nfa_iovec(&iov[1], &data_attr, NFQA_PAYLOAD,
722 data_len, (unsigned char *) data);
724 /* Add the length of the appended data to the message
725 * header. The size of the attribute is given in the
726 * nfa_len field and is set in the nfnl_build_nfa_iovec()
728 u.nmh.nlmsg_len += data_attr.nfa_len;
731 return nfnl_sendiov(qh->h->nfnlh, iov, nvecs, 0);
740 * nfq_set_verdict - issue a verdict on a packet
741 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
742 * \param id ID assigned to packet by netfilter.
743 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
744 * \param data_len number of bytes of data pointed to by #buf
745 * \param buf the buffer that contains the packet data
747 * Can be obtained by:
750 struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(tb);
752 id = ntohl(ph->packet_id);
755 * Notifies netfilter of the userspace verdict for the given packet. Every
756 * queued packet _must_ have a verdict specified by userspace, either by
757 * calling this function, the nfq_set_verdict2() function, or the _batch
758 * versions of these functions.
760 * \return -1 on error; >= 0 otherwise.
762 int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
763 u_int32_t verdict, u_int32_t data_len,
764 const unsigned char *buf)
766 return __set_verdict(qh, id, verdict, 0, 0, data_len, buf,
771 * nfq_set_verdict2 - like nfq_set_verdict, but you can set the mark.
772 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
773 * \param id ID assigned to packet by netfilter.
774 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
775 * \param mark mark to put on packet
776 * \param data_len number of bytes of data pointed to by #buf
777 * \param buf the buffer that contains the packet data
779 int nfq_set_verdict2(struct nfq_q_handle *qh, u_int32_t id,
780 u_int32_t verdict, u_int32_t mark,
781 u_int32_t data_len, const unsigned char *buf)
783 return __set_verdict(qh, id, verdict, htonl(mark), 1, data_len,
784 buf, NFQNL_MSG_VERDICT);
788 * nfq_set_verdict_batch - issue verdicts on several packets at once
789 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
790 * \param id maximum ID of the packets that the verdict should be applied to.
791 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
793 * Unlike nfq_set_verdict, the verdict is applied to all queued packets
794 * whose packet id is smaller or equal to #id.
796 * batch support was added in Linux 3.1.
797 * These functions will fail silently on older kernels.
799 int nfq_set_verdict_batch(struct nfq_q_handle *qh, u_int32_t id,
802 return __set_verdict(qh, id, verdict, 0, 0, 0, NULL,
803 NFQNL_MSG_VERDICT_BATCH);
807 * nfq_set_verdict_batch2 - like nfq_set_verdict_batch, but you can set a mark.
808 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
809 * \param id maximum ID of the packets that the verdict should be applied to.
810 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
811 * \param mark mark to put on packet
813 int nfq_set_verdict_batch2(struct nfq_q_handle *qh, u_int32_t id,
814 u_int32_t verdict, u_int32_t mark)
816 return __set_verdict(qh, id, verdict, htonl(mark), 1, 0,
817 NULL, NFQNL_MSG_VERDICT_BATCH);
821 * nfq_set_verdict_mark - like nfq_set_verdict, but you can set the mark.
822 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
823 * \param id ID assigned to packet by netfilter.
824 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
825 * \param mark the mark to put on the packet, in network byte order.
826 * \param data_len number of bytes of data pointed to by #buf
827 * \param buf the buffer that contains the packet data
829 * \return -1 on error; >= 0 otherwise.
831 * This function is deprecated since it is broken, its use is highly
832 * discouraged. Please, use nfq_set_verdict2 instead.
834 int nfq_set_verdict_mark(struct nfq_q_handle *qh, u_int32_t id,
835 u_int32_t verdict, u_int32_t mark,
836 u_int32_t data_len, const unsigned char *buf)
838 return __set_verdict(qh, id, verdict, mark, 1, data_len, buf,
848 /*************************************************************
849 * Message parsing functions
850 *************************************************************/
853 * \defgroup Parsing Message parsing functions [DEPRECATED]
858 * nfqnl_msg_packet_hdr - return the metaheader that wraps the packet
859 * \param nfad Netlink packet data handle passed to callback function
861 * \return the netfilter queue netlink packet header for the given
862 * nfq_data argument. Typically, the nfq_data value is passed as the 3rd
863 * parameter to the callback function set by a call to nfq_create_queue().
865 * The nfqnl_msg_packet_hdr structure is defined in libnetfilter_queue.h as:
868 struct nfqnl_msg_packet_hdr {
869 u_int32_t packet_id; // unique ID of packet in queue
870 u_int16_t hw_protocol; // hw protocol (network order)
871 u_int8_t hook; // netfilter hook
872 } __attribute__ ((packed));
875 struct nfqnl_msg_packet_hdr *nfq_get_msg_packet_hdr(struct nfq_data *nfad)
877 return nfnl_get_pointer_to_data(nfad->data, NFQA_PACKET_HDR,
878 struct nfqnl_msg_packet_hdr);
882 * nfq_get_nfmark - get the packet mark
883 * \param nfad Netlink packet data handle passed to callback function
885 * \return the netfilter mark currently assigned to the given queued packet.
887 uint32_t nfq_get_nfmark(struct nfq_data *nfad)
889 return ntohl(nfnl_get_data(nfad->data, NFQA_MARK, u_int32_t));
893 * nfq_get_timestamp - get the packet timestamp
894 * \param nfad Netlink packet data handle passed to callback function
895 * \param tv structure to fill with timestamp info
897 * Retrieves the received timestamp when the given queued packet.
899 * \return 0 on success, non-zero on failure.
901 int nfq_get_timestamp(struct nfq_data *nfad, struct timeval *tv)
903 struct nfqnl_msg_packet_timestamp *qpt;
904 qpt = nfnl_get_pointer_to_data(nfad->data, NFQA_TIMESTAMP,
905 struct nfqnl_msg_packet_timestamp);
909 tv->tv_sec = __be64_to_cpu(qpt->sec);
910 tv->tv_usec = __be64_to_cpu(qpt->usec);
916 * nfq_get_indev - get the interface that the packet was received through
917 * \param nfad Netlink packet data handle passed to callback function
919 * \return The index of the device the queued packet was received via. If the
920 * returned index is 0, the packet was locally generated or the input
921 * interface is not known (ie. POSTROUTING?).
923 * \warning all nfq_get_dev() functions return 0 if not set, since linux
924 * only allows ifindex >= 1, see net/core/dev.c:2600 (in 2.6.13.1)
926 u_int32_t nfq_get_indev(struct nfq_data *nfad)
928 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_INDEV, u_int32_t));
932 * nfq_get_physindev - get the physical interface that the packet was received
933 * \param nfad Netlink packet data handle passed to callback function
935 * \return The index of the physical device the queued packet was received via.
936 * If the returned index is 0, the packet was locally generated or the
937 * physical input interface is no longer known (ie. POSTROUTING?).
939 u_int32_t nfq_get_physindev(struct nfq_data *nfad)
941 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSINDEV, u_int32_t));
945 * nfq_get_outdev - gets the interface that the packet will be routed out
946 * \param nfad Netlink packet data handle passed to callback function
948 * \return The index of the device the queued packet will be sent out. If the
949 * returned index is 0, the packet is destined for localhost or the output
950 * interface is not yet known (ie. PREROUTING?).
952 u_int32_t nfq_get_outdev(struct nfq_data *nfad)
954 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_OUTDEV, u_int32_t));
958 * nfq_get_physoutdev - get the physical interface that the packet output
959 * \param nfad Netlink packet data handle passed to callback function
961 * The index of the physical device the queued packet will be sent out.
962 * If the returned index is 0, the packet is destined for localhost or the
963 * physical output interface is not yet known (ie. PREROUTING?).
965 * \return The index of physical interface that the packet output will be routed out.
967 u_int32_t nfq_get_physoutdev(struct nfq_data *nfad)
969 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSOUTDEV, u_int32_t));
973 * nfq_get_indev_name - get the name of the interface the packet
974 * was received through
975 * \param nlif_handle pointer to a nlif interface resolving handle
976 * \param nfad Netlink packet data handle passed to callback function
977 * \param name pointer to the buffer to receive the interface name;
978 * not more than \c IFNAMSIZ bytes will be copied to it.
979 * \return -1 in case of error, >0 if it succeed.
981 * To use a nlif_handle, You need first to call nlif_open() and to open
982 * an handler. Don't forget to store the result as it will be used
983 * during all your program life:
991 * Once the handler is open, you need to fetch the interface table at a
992 * whole via a call to nlif_query.
996 * libnfnetlink is able to update the interface mapping when a new interface
997 * appears. To do so, you need to call nlif_catch() on the handler after each
998 * interface related event. The simplest way to get and treat event is to run
999 * a select() or poll() against the nlif file descriptor. To get this file
1000 * descriptor, you need to use nlif_fd:
1004 * Don't forget to close the handler when you don't need the feature anymore:
1010 int nfq_get_indev_name(struct nlif_handle *nlif_handle,
1011 struct nfq_data *nfad, char *name)
1013 u_int32_t ifindex = nfq_get_indev(nfad);
1014 return nlif_index2name(nlif_handle, ifindex, name);
1018 * nfq_get_physindev_name - get the name of the physical interface the
1019 * packet was received through
1020 * \param nlif_handle pointer to a nlif interface resolving handle
1021 * \param nfad Netlink packet data handle passed to callback function
1022 * \param name pointer to the buffer to receive the interface name;
1023 * not more than \c IFNAMSIZ bytes will be copied to it.
1025 * See nfq_get_indev_name() documentation for nlif_handle usage.
1027 * \return -1 in case of error, > 0 if it succeed.
1029 int nfq_get_physindev_name(struct nlif_handle *nlif_handle,
1030 struct nfq_data *nfad, char *name)
1032 u_int32_t ifindex = nfq_get_physindev(nfad);
1033 return nlif_index2name(nlif_handle, ifindex, name);
1037 * nfq_get_outdev_name - get the name of the physical interface the
1038 * packet will be sent to
1039 * \param nlif_handle pointer to a nlif interface resolving handle
1040 * \param nfad Netlink packet data handle passed to callback function
1041 * \param name pointer to the buffer to receive the interface name;
1042 * not more than \c IFNAMSIZ bytes will be copied to it.
1044 * See nfq_get_indev_name() documentation for nlif_handle usage.
1046 * \return -1 in case of error, > 0 if it succeed.
1048 int nfq_get_outdev_name(struct nlif_handle *nlif_handle,
1049 struct nfq_data *nfad, char *name)
1051 u_int32_t ifindex = nfq_get_outdev(nfad);
1052 return nlif_index2name(nlif_handle, ifindex, name);
1056 * nfq_get_physoutdev_name - get the name of the interface the
1057 * packet will be sent to
1058 * \param nlif_handle pointer to a nlif interface resolving handle
1059 * \param nfad Netlink packet data handle passed to callback function
1060 * \param name pointer to the buffer to receive the interface name;
1061 * not more than \c IFNAMSIZ bytes will be copied to it.
1063 * See nfq_get_indev_name() documentation for nlif_handle usage.
1065 * \return -1 in case of error, > 0 if it succeed.
1068 int nfq_get_physoutdev_name(struct nlif_handle *nlif_handle,
1069 struct nfq_data *nfad, char *name)
1071 u_int32_t ifindex = nfq_get_physoutdev(nfad);
1072 return nlif_index2name(nlif_handle, ifindex, name);
1078 * get hardware address
1080 * \param nfad Netlink packet data handle passed to callback function
1082 * Retrieves the hardware address associated with the given queued packet.
1083 * For ethernet packets, the hardware address returned (if any) will be the
1084 * MAC address of the packet source host. The destination MAC address is not
1085 * known until after POSTROUTING and a successful ARP request, so cannot
1086 * currently be retrieved.
1088 * The nfqnl_msg_packet_hw structure is defined in libnetfilter_queue.h as:
1090 struct nfqnl_msg_packet_hw {
1091 u_int16_t hw_addrlen;
1093 u_int8_t hw_addr[8];
1094 } __attribute__ ((packed));
1097 struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
1099 return nfnl_get_pointer_to_data(nfad->data, NFQA_HWADDR,
1100 struct nfqnl_msg_packet_hw);
1104 * nfq_get_payload - get payload
1105 * \param nfad Netlink packet data handle passed to callback function
1106 * \param data Pointer of pointer that will be pointed to the payload
1108 * Retrieve the payload for a queued packet. The actual amount and type of
1109 * data retrieved by this function will depend on the mode set with the
1110 * nfq_set_mode() function.
1112 * \return -1 on error, otherwise > 0.
1114 int nfq_get_payload(struct nfq_data *nfad, unsigned char **data)
1116 *data = nfnl_get_pointer_to_data(nfad->data, NFQA_PAYLOAD, char);
1118 return NFA_PAYLOAD(nfad->data[NFQA_PAYLOAD-1]);
1127 #define SNPRINTF_FAILURE(ret, rem, offset, len) \
1139 * \defgroup Printing Printing [DEPRECATED]
1144 * nfq_snprintf_xml - print the enqueued packet in XML format into a buffer
1145 * \param buf The buffer that you want to use to print the logged packet
1146 * \param rem The size of the buffer that you have passed
1147 * \param tb Netlink packet data handle passed to callback function
1148 * \param flags The flag that tell what to print into the buffer
1150 * This function supports the following flags:
1152 * - NFQ_XML_HW: include the hardware link layer address
1153 * - NFQ_XML_MARK: include the packet mark
1154 * - NFQ_XML_DEV: include the device information
1155 * - NFQ_XML_PHYSDEV: include the physical device information
1156 * - NFQ_XML_PAYLOAD: include the payload (in hexadecimal)
1157 * - NFQ_XML_TIME: include the timestamp
1158 * - NFQ_XML_ALL: include all the logging information (all flags set)
1160 * You can combine this flags with an binary OR.
1162 * \return -1 in case of failure, otherwise the length of the string that
1163 * would have been printed into the buffer (in case that there is enough
1164 * room in it). See snprintf() return value for more information.
1166 int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
1168 struct nfqnl_msg_packet_hdr *ph;
1169 struct nfqnl_msg_packet_hw *hwph;
1170 u_int32_t mark, ifi;
1171 int size, offset = 0, len = 0, ret;
1172 unsigned char *data;
1174 size = snprintf(buf + offset, rem, "<pkt>");
1175 SNPRINTF_FAILURE(size, rem, offset, len);
1177 if (flags & NFQ_XML_TIME) {
1182 if (localtime_r(&t, &tm) == NULL)
1185 size = snprintf(buf + offset, rem, "<when>");
1186 SNPRINTF_FAILURE(size, rem, offset, len);
1188 size = snprintf(buf + offset, rem,
1189 "<hour>%d</hour>", tm.tm_hour);
1190 SNPRINTF_FAILURE(size, rem, offset, len);
1192 size = snprintf(buf + offset,
1193 rem, "<min>%02d</min>", tm.tm_min);
1194 SNPRINTF_FAILURE(size, rem, offset, len);
1196 size = snprintf(buf + offset,
1197 rem, "<sec>%02d</sec>", tm.tm_sec);
1198 SNPRINTF_FAILURE(size, rem, offset, len);
1200 size = snprintf(buf + offset, rem, "<wday>%d</wday>",
1202 SNPRINTF_FAILURE(size, rem, offset, len);
1204 size = snprintf(buf + offset, rem, "<day>%d</day>", tm.tm_mday);
1205 SNPRINTF_FAILURE(size, rem, offset, len);
1207 size = snprintf(buf + offset, rem, "<month>%d</month>",
1209 SNPRINTF_FAILURE(size, rem, offset, len);
1211 size = snprintf(buf + offset, rem, "<year>%d</year>",
1213 SNPRINTF_FAILURE(size, rem, offset, len);
1215 size = snprintf(buf + offset, rem, "</when>");
1216 SNPRINTF_FAILURE(size, rem, offset, len);
1219 ph = nfq_get_msg_packet_hdr(tb);
1221 size = snprintf(buf + offset, rem,
1222 "<hook>%u</hook><id>%u</id>",
1223 ph->hook, ntohl(ph->packet_id));
1224 SNPRINTF_FAILURE(size, rem, offset, len);
1226 hwph = nfq_get_packet_hw(tb);
1227 if (hwph && (flags & NFQ_XML_HW)) {
1228 int i, hlen = ntohs(hwph->hw_addrlen);
1230 size = snprintf(buf + offset, rem, "<hw><proto>%04x"
1232 ntohs(ph->hw_protocol));
1233 SNPRINTF_FAILURE(size, rem, offset, len);
1235 size = snprintf(buf + offset, rem, "<src>");
1236 SNPRINTF_FAILURE(size, rem, offset, len);
1238 for (i=0; i<hlen; i++) {
1239 size = snprintf(buf + offset, rem, "%02x",
1241 SNPRINTF_FAILURE(size, rem, offset, len);
1244 size = snprintf(buf + offset, rem, "</src></hw>");
1245 SNPRINTF_FAILURE(size, rem, offset, len);
1246 } else if (flags & NFQ_XML_HW) {
1247 size = snprintf(buf + offset, rem, "<hw><proto>%04x"
1249 ntohs(ph->hw_protocol));
1250 SNPRINTF_FAILURE(size, rem, offset, len);
1254 mark = nfq_get_nfmark(tb);
1255 if (mark && (flags & NFQ_XML_MARK)) {
1256 size = snprintf(buf + offset, rem, "<mark>%u</mark>", mark);
1257 SNPRINTF_FAILURE(size, rem, offset, len);
1260 ifi = nfq_get_indev(tb);
1261 if (ifi && (flags & NFQ_XML_DEV)) {
1262 size = snprintf(buf + offset, rem, "<indev>%u</indev>", ifi);
1263 SNPRINTF_FAILURE(size, rem, offset, len);
1266 ifi = nfq_get_outdev(tb);
1267 if (ifi && (flags & NFQ_XML_DEV)) {
1268 size = snprintf(buf + offset, rem, "<outdev>%u</outdev>", ifi);
1269 SNPRINTF_FAILURE(size, rem, offset, len);
1272 ifi = nfq_get_physindev(tb);
1273 if (ifi && (flags & NFQ_XML_PHYSDEV)) {
1274 size = snprintf(buf + offset, rem,
1275 "<physindev>%u</physindev>", ifi);
1276 SNPRINTF_FAILURE(size, rem, offset, len);
1279 ifi = nfq_get_physoutdev(tb);
1280 if (ifi && (flags & NFQ_XML_PHYSDEV)) {
1281 size = snprintf(buf + offset, rem,
1282 "<physoutdev>%u</physoutdev>", ifi);
1283 SNPRINTF_FAILURE(size, rem, offset, len);
1286 ret = nfq_get_payload(tb, &data);
1287 if (ret >= 0 && (flags & NFQ_XML_PAYLOAD)) {
1290 size = snprintf(buf + offset, rem, "<payload>");
1291 SNPRINTF_FAILURE(size, rem, offset, len);
1293 for (i=0; i<ret; i++) {
1294 size = snprintf(buf + offset, rem, "%02x",
1296 SNPRINTF_FAILURE(size, rem, offset, len);
1299 size = snprintf(buf + offset, rem, "</payload>");
1300 SNPRINTF_FAILURE(size, rem, offset, len);
1303 size = snprintf(buf + offset, rem, "</pkt>");
1304 SNPRINTF_FAILURE(size, rem, offset, len);