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).
90 * - consider using fail-open option see nfq_set_queue_flags() (it requires
91 * Linux kernel >= 3.6)
96 struct nfnl_handle *nfnlh;
97 struct nfnl_subsys_handle *nfnlssh;
98 struct nfq_q_handle *qh_list;
103 struct nfq_q_handle *next;
104 struct nfq_handle *h;
112 struct nfattr **data;
117 /***********************************************************************
119 ***********************************************************************/
121 static void del_qh(struct nfq_q_handle *qh)
123 struct nfq_q_handle *cur_qh, *prev_qh = NULL;
125 for (cur_qh = qh->h->qh_list; cur_qh; cur_qh = cur_qh->next) {
128 prev_qh->next = qh->next;
130 qh->h->qh_list = qh->next;
137 static void add_qh(struct nfq_q_handle *qh)
139 qh->next = qh->h->qh_list;
143 static struct nfq_q_handle *find_qh(struct nfq_handle *h, u_int16_t id)
145 struct nfq_q_handle *qh;
147 for (qh = h->qh_list; qh; qh = qh->next) {
154 /* build a NFQNL_MSG_CONFIG message */
156 __build_send_cfg_msg(struct nfq_handle *h, u_int8_t command,
157 u_int16_t queuenum, u_int16_t pf)
160 char buf[NFNL_HEADER_LEN
161 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_cmd))];
164 struct nfqnl_msg_config_cmd cmd;
166 nfnl_fill_hdr(h->nfnlssh, &u.nmh, 0, AF_UNSPEC, queuenum,
167 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
169 cmd.command = command;
171 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_CMD, &cmd, sizeof(cmd));
173 return nfnl_query(h->nfnlh, &u.nmh);
176 static int __nfq_rcv_pkt(struct nlmsghdr *nlh, struct nfattr *nfa[],
179 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
180 struct nfq_handle *h = data;
181 u_int16_t queue_num = ntohs(nfmsg->res_id);
182 struct nfq_q_handle *qh = find_qh(h, queue_num);
183 struct nfq_data nfqa;
193 return qh->cb(qh, nfmsg, &nfqa, qh->data);
196 static struct nfnl_callback pkt_cb = {
197 .call = &__nfq_rcv_pkt,
198 .attr_count = NFQA_MAX,
201 /* public interface */
203 struct nfnl_handle *nfq_nfnlh(struct nfq_handle *h)
210 * \defgroup Queue Queue handling [DEPRECATED]
212 * Once libnetfilter_queue library has been initialised (See
213 * \link LibrarySetup \endlink), it is possible to bind the program to a
214 * specific queue. This can be done by using nfq_create_queue().
216 * The queue can then be tuned via nfq_set_mode() or nfq_set_queue_maxlen().
218 * Here's a little code snippet that create queue numbered 0:
220 printf("binding this socket to queue '0'\n");
221 qh = nfq_create_queue(h, 0, &cb, NULL);
223 fprintf(stderr, "error during nfq_create_queue()\n");
227 printf("setting copy_packet mode\n");
228 if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
229 fprintf(stderr, "can't set packet_copy mode\n");
234 * Next step is the handling of incoming packets which can be done via a loop:
239 while ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
240 printf("pkt received\n");
241 nfq_handle_packet(h, buf, rv);
244 * When the decision on a packet has been choosed, the verdict has to be given
245 * by calling nfq_set_verdict() or nfq_set_verdict2(). The verdict
246 * determines the destiny of the packet as follows:
248 * - NF_DROP discarded the packet
249 * - NF_ACCEPT the packet passes, continue iterations
250 * - NF_QUEUE inject the packet into a different queue
251 * (the target queue number is in the high 16 bits of the verdict)
252 * - NF_REPEAT iterate the same cycle once more
253 * - NF_STOP accept, but don't continue iterations
255 * The verdict NF_STOLEN must not be used, as it has special meaning in the
257 * When using NF_REPEAT, one way to prevent re-queueing of the same packet
258 * is to also set an nfmark using nfq_set_verdict2, and set up the nefilter
259 * rules to only queue a packet when the mark is not (yet) set.
261 * Data and information about the packet can be fetch by using message parsing
262 * functions (See \link Parsing \endlink).
267 * nfq_fd - get the file descriptor associated with the nfqueue handler
268 * \param h Netfilter queue connection handle obtained via call to nfq_open()
270 * \return a file descriptor for the netlink connection associated with the
271 * given queue connection handle. The file descriptor can then be used for
272 * receiving the queued packets for processing.
274 * This function returns a file descriptor that can be used for communication
275 * over the netlink connection associated with the given queue connection
278 int nfq_fd(struct nfq_handle *h)
280 return nfnl_fd(nfq_nfnlh(h));
288 * \defgroup LibrarySetup Library setup [DEPRECATED]
290 * Library initialisation is made in two steps.
292 * First step is to call nfq_open() to open a NFQUEUE handler.
294 * Second step is to tell the kernel that userspace queueing is handle by
295 * NFQUEUE for the selected protocol. This is made by calling nfq_unbind_pf()
296 * and nfq_bind_pf() with protocol information. The idea behind this is to
297 * enable simultaneously loaded modules to be used for queuing.
299 * Here's a little code snippet that bind with AF_INET:
303 fprintf(stderr, "error during nfq_open()\n");
307 printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
308 if (nfq_unbind_pf(h, AF_INET) < 0) {
309 fprintf(stderr, "error during nfq_unbind_pf()\n");
313 printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
314 if (nfq_bind_pf(h, AF_INET) < 0) {
315 fprintf(stderr, "error during nfq_bind_pf()\n");
319 * Once this is done, you can setup and use a \link Queue \endlink.
324 * nfq_open - open a nfqueue handler
326 * This function obtains a netfilter queue connection handle. When you are
327 * finished with the handle returned by this function, you should destroy
328 * it by calling nfq_close(). A new netlink connection is obtained internally
329 * and associated with the queue connection handle returned.
331 * \return a pointer to a new queue handle or NULL on failure.
333 struct nfq_handle *nfq_open(void)
335 struct nfnl_handle *nfnlh = nfnl_open();
336 struct nfq_handle *qh;
341 /* unset netlink sequence tracking by default */
342 nfnl_unset_sequence_tracking(nfnlh);
344 qh = nfq_open_nfnl(nfnlh);
356 * nfq_open_nfnl - open a nfqueue handler from a existing nfnetlink handler
357 * \param nfnlh Netfilter netlink connection handle obtained by calling nfnl_open()
359 * This function obtains a netfilter queue connection handle using an existing
360 * netlink connection. This function is used internally to implement
361 * nfq_open(), and should typically not be called directly.
363 * \return a pointer to a new queue handle or NULL on failure.
365 struct nfq_handle *nfq_open_nfnl(struct nfnl_handle *nfnlh)
367 struct nfq_handle *h;
370 h = malloc(sizeof(*h));
374 memset(h, 0, sizeof(*h));
377 h->nfnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_QUEUE,
380 /* FIXME: nfq_errno */
385 err = nfnl_callback_register(h->nfnlssh, NFQNL_MSG_PACKET, &pkt_cb);
393 nfnl_subsys_close(h->nfnlssh);
400 * \addtogroup LibrarySetup
402 * When the program has finished with libnetfilter_queue, it has to call
403 * the nfq_close() function to free all associated resources.
409 * nfq_close - close a nfqueue handler
410 * \param h Netfilter queue connection handle obtained via call to nfq_open()
412 * This function closes the nfqueue handler and free associated resources.
414 * \return 0 on success, non-zero on failure.
416 int nfq_close(struct nfq_handle *h)
420 ret = nfnl_close(h->nfnlh);
427 * nfq_bind_pf - bind a nfqueue handler to a given protocol family
428 * \param h Netfilter queue connection handle obtained via call to nfq_open()
429 * \param pf protocol family to bind to nfqueue handler obtained from nfq_open()
431 * Binds the given queue connection handle to process packets belonging to
432 * the given protocol family (ie. PF_INET, PF_INET6, etc).
434 * \return integer inferior to 0 in case of failure
436 int nfq_bind_pf(struct nfq_handle *h, u_int16_t pf)
438 return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_BIND, 0, pf);
442 * nfq_unbind_pf - unbind nfqueue handler from a protocol family
443 * \param h Netfilter queue connection handle obtained via call to nfq_open()
444 * \param pf protocol family to unbind family from
446 * Unbinds the given queue connection handle from processing packets belonging
447 * to the given protocol family.
449 int nfq_unbind_pf(struct nfq_handle *h, u_int16_t pf)
451 return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_UNBIND, 0, pf);
466 * nfq_create_queue - create a new queue handle and return it.
468 * \param h Netfilter queue connection handle obtained via call to nfq_open()
469 * \param num the number of the queue to bind to
470 * \param cb callback function to call for each queued packet
471 * \param data custom data to pass to the callback function
473 * \return a nfq_q_handle pointing to the newly created queue
475 * Creates a new queue handle, and returns it. The new queue is identified by
476 * #num, and the callback specified by #cb will be called for each enqueued
477 * packet. The #data argument will be passed unchanged to the callback. If
478 * a queue entry with id #num already exists, this function will return failure
479 * and the existing entry is unchanged.
481 * The nfq_callback type is defined in libnetfilter_queue.h as:
483 typedef int nfq_callback(struct nfq_q_handle *qh,
484 struct nfgenmsg *nfmsg,
485 struct nfq_data *nfad, void *data);
489 * - qh The queue handle returned by nfq_create_queue
490 * - nfmsg message objetc that contains the packet
491 * - nfad Netlink packet data handle
492 * - data the value passed to the data parameter of nfq_create_queue
494 * The callback should return < 0 to stop processing.
497 struct nfq_q_handle *nfq_create_queue(struct nfq_handle *h,
503 struct nfq_q_handle *qh;
508 qh = malloc(sizeof(*qh));
510 memset(qh, 0, sizeof(*qh));
516 ret = __build_send_cfg_msg(h, NFQNL_CFG_CMD_BIND, num, 0);
537 * nfq_destroy_queue - destroy a queue handle
538 * \param qh queue handle that we want to destroy created via nfq_create_queue
540 * Removes the binding for the specified queue handle. This call also unbind
541 * from the nfqueue handler, so you don't have to call nfq_unbind_pf.
543 int nfq_destroy_queue(struct nfq_q_handle *qh)
545 int ret = __build_send_cfg_msg(qh->h, NFQNL_CFG_CMD_UNBIND, qh->id, 0);
555 * nfq_handle_packet - handle a packet received from the nfqueue subsystem
556 * \param h Netfilter queue connection handle obtained via call to nfq_open()
557 * \param buf data to pass to the callback
558 * \param len length of packet data in buffer
560 * Triggers an associated callback for the given packet received from the
561 * queue. Packets can be read from the queue using nfq_fd() and recv(). See
562 * example code for nfq_fd().
564 * \return 0 on success, non-zero on failure.
566 int nfq_handle_packet(struct nfq_handle *h, char *buf, int len)
568 return nfnl_handle_packet(h->nfnlh, buf, len);
572 * nfq_set_mode - set the amount of packet data that nfqueue copies to userspace
573 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
574 * \param mode the part of the packet that we are interested in
575 * \param range size of the packet that we want to get
577 * Sets the amount of data to be copied to userspace for each packet queued
578 * to the given queue.
580 * - NFQNL_COPY_NONE - noop, do not use it
581 * - NFQNL_COPY_META - copy only packet metadata
582 * - NFQNL_COPY_PACKET - copy entire packet
584 * \return -1 on error; >=0 otherwise.
586 int nfq_set_mode(struct nfq_q_handle *qh,
587 u_int8_t mode, u_int32_t range)
590 char buf[NFNL_HEADER_LEN
591 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
594 struct nfqnl_msg_config_params params;
596 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
597 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
599 params.copy_range = htonl(range);
600 params.copy_mode = mode;
601 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_PARAMS, ¶ms,
604 return nfnl_query(qh->h->nfnlh, &u.nmh);
608 * nfq_set_queue_flags - set flags (options) for the kernel queue
609 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
610 * \param mask specifies which flag bits to modify
611 * \param flag bitmask of flags
613 * Here's a little code snippet to show how to use this API:
615 uint32_t flags = NFQA_CFG_F_FAIL_OPEN;
616 uint32_t mask = NFQA_CFG_F_FAIL_OPEN;
618 printf("Enabling fail-open on this q\n");
619 err = nfq_set_queue_flags(qh, mask, flags);
621 printf("Disabling fail-open on this q\n");
622 flags &= ~NFQA_CFG_F_FAIL_OPEN;
623 err = nfq_set_queue_flags(qh, mask, flags);
626 * If NFQA_CFG_F_FAIL_OPEN is used, the kernel will accept instead of
627 * drop packets that should have been enqueued to a full queue. This
628 * results in the system being able to handle high network load but at
629 * the depend of the control of the packets.
631 * \return -1 on error with errno set appropriately; =0 otherwise.
633 int nfq_set_queue_flags(struct nfq_q_handle *qh,
634 uint32_t mask, uint32_t flags)
637 char buf[NFNL_HEADER_LEN
638 +NFA_LENGTH(sizeof(mask)
639 +NFA_LENGTH(sizeof(flags)))];
644 flags = htonl(flags);
646 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
647 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
649 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_CFG_FLAGS, flags);
650 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_CFG_MASK, mask);
652 return nfnl_query(qh->h->nfnlh, &u.nmh);
656 * nfq_set_queue_maxlen - Set kernel queue maximum length parameter
657 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
658 * \param queuelen the length of the queue
660 * Sets the size of the queue in kernel. This fixes the maximum number
661 * of packets the kernel will store before internally before dropping
664 * \return -1 on error; >=0 otherwise.
666 int nfq_set_queue_maxlen(struct nfq_q_handle *qh,
670 char buf[NFNL_HEADER_LEN
671 +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
674 u_int32_t queue_maxlen = htonl(queuelen);
676 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
677 NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
679 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_QUEUE_MAXLEN, &queue_maxlen,
680 sizeof(queue_maxlen));
682 return nfnl_query(qh->h->nfnlh, &u.nmh);
689 static int __set_verdict(struct nfq_q_handle *qh, u_int32_t id,
690 u_int32_t verdict, u_int32_t mark, int set_mark,
691 u_int32_t data_len, const unsigned char *data,
692 enum nfqnl_msg_types type)
694 struct nfqnl_msg_verdict_hdr vh;
696 char buf[NFNL_HEADER_LEN
697 +NFA_LENGTH(sizeof(mark))
698 +NFA_LENGTH(sizeof(vh))];
705 /* This must be declared here (and not inside the data
706 * handling block) because the iovec points to this. */
707 struct nfattr data_attr;
709 memset(iov, 0, sizeof(iov));
711 vh.verdict = htonl(verdict);
714 nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
715 type, NLM_F_REQUEST);
717 /* add verdict header */
718 nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_VERDICT_HDR, &vh, sizeof(vh));
721 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_MARK, mark);
723 iov[0].iov_base = &u.nmh;
724 iov[0].iov_len = NLMSG_TAIL(&u.nmh) - (void *)&u.nmh;
728 /* The typecast here is to cast away data's const-ness: */
729 nfnl_build_nfa_iovec(&iov[1], &data_attr, NFQA_PAYLOAD,
730 data_len, (unsigned char *) data);
732 /* Add the length of the appended data to the message
733 * header. The size of the attribute is given in the
734 * nfa_len field and is set in the nfnl_build_nfa_iovec()
736 u.nmh.nlmsg_len += data_attr.nfa_len;
739 return nfnl_sendiov(qh->h->nfnlh, iov, nvecs, 0);
748 * nfq_set_verdict - issue a verdict on a packet
749 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
750 * \param id ID assigned to packet by netfilter.
751 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
752 * \param data_len number of bytes of data pointed to by #buf
753 * \param buf the buffer that contains the packet data
755 * Can be obtained by:
758 struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(tb);
760 id = ntohl(ph->packet_id);
763 * Notifies netfilter of the userspace verdict for the given packet. Every
764 * queued packet _must_ have a verdict specified by userspace, either by
765 * calling this function, the nfq_set_verdict2() function, or the _batch
766 * versions of these functions.
768 * \return -1 on error; >= 0 otherwise.
770 int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
771 u_int32_t verdict, u_int32_t data_len,
772 const unsigned char *buf)
774 return __set_verdict(qh, id, verdict, 0, 0, data_len, buf,
779 * nfq_set_verdict2 - like nfq_set_verdict, but you can set the mark.
780 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
781 * \param id ID assigned to packet by netfilter.
782 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
783 * \param mark mark to put on packet
784 * \param data_len number of bytes of data pointed to by #buf
785 * \param buf the buffer that contains the packet data
787 int nfq_set_verdict2(struct nfq_q_handle *qh, u_int32_t id,
788 u_int32_t verdict, u_int32_t mark,
789 u_int32_t data_len, const unsigned char *buf)
791 return __set_verdict(qh, id, verdict, htonl(mark), 1, data_len,
792 buf, NFQNL_MSG_VERDICT);
796 * nfq_set_verdict_batch - issue verdicts on several packets at once
797 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
798 * \param id maximum ID of the packets that the verdict should be applied to.
799 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
801 * Unlike nfq_set_verdict, the verdict is applied to all queued packets
802 * whose packet id is smaller or equal to #id.
804 * batch support was added in Linux 3.1.
805 * These functions will fail silently on older kernels.
807 int nfq_set_verdict_batch(struct nfq_q_handle *qh, u_int32_t id,
810 return __set_verdict(qh, id, verdict, 0, 0, 0, NULL,
811 NFQNL_MSG_VERDICT_BATCH);
815 * nfq_set_verdict_batch2 - like nfq_set_verdict_batch, but you can set a mark.
816 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
817 * \param id maximum ID of the packets that the verdict should be applied to.
818 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
819 * \param mark mark to put on packet
821 int nfq_set_verdict_batch2(struct nfq_q_handle *qh, u_int32_t id,
822 u_int32_t verdict, u_int32_t mark)
824 return __set_verdict(qh, id, verdict, htonl(mark), 1, 0,
825 NULL, NFQNL_MSG_VERDICT_BATCH);
829 * nfq_set_verdict_mark - like nfq_set_verdict, but you can set the mark.
830 * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
831 * \param id ID assigned to packet by netfilter.
832 * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
833 * \param mark the mark to put on the packet, in network byte order.
834 * \param data_len number of bytes of data pointed to by #buf
835 * \param buf the buffer that contains the packet data
837 * \return -1 on error; >= 0 otherwise.
839 * This function is deprecated since it is broken, its use is highly
840 * discouraged. Please, use nfq_set_verdict2 instead.
842 int nfq_set_verdict_mark(struct nfq_q_handle *qh, u_int32_t id,
843 u_int32_t verdict, u_int32_t mark,
844 u_int32_t data_len, const unsigned char *buf)
846 return __set_verdict(qh, id, verdict, mark, 1, data_len, buf,
856 /*************************************************************
857 * Message parsing functions
858 *************************************************************/
861 * \defgroup Parsing Message parsing functions [DEPRECATED]
866 * nfqnl_msg_packet_hdr - return the metaheader that wraps the packet
867 * \param nfad Netlink packet data handle passed to callback function
869 * \return the netfilter queue netlink packet header for the given
870 * nfq_data argument. Typically, the nfq_data value is passed as the 3rd
871 * parameter to the callback function set by a call to nfq_create_queue().
873 * The nfqnl_msg_packet_hdr structure is defined in libnetfilter_queue.h as:
876 struct nfqnl_msg_packet_hdr {
877 u_int32_t packet_id; // unique ID of packet in queue
878 u_int16_t hw_protocol; // hw protocol (network order)
879 u_int8_t hook; // netfilter hook
880 } __attribute__ ((packed));
883 struct nfqnl_msg_packet_hdr *nfq_get_msg_packet_hdr(struct nfq_data *nfad)
885 return nfnl_get_pointer_to_data(nfad->data, NFQA_PACKET_HDR,
886 struct nfqnl_msg_packet_hdr);
890 * nfq_get_nfmark - get the packet mark
891 * \param nfad Netlink packet data handle passed to callback function
893 * \return the netfilter mark currently assigned to the given queued packet.
895 uint32_t nfq_get_nfmark(struct nfq_data *nfad)
897 return ntohl(nfnl_get_data(nfad->data, NFQA_MARK, u_int32_t));
901 * nfq_get_timestamp - get the packet timestamp
902 * \param nfad Netlink packet data handle passed to callback function
903 * \param tv structure to fill with timestamp info
905 * Retrieves the received timestamp when the given queued packet.
907 * \return 0 on success, non-zero on failure.
909 int nfq_get_timestamp(struct nfq_data *nfad, struct timeval *tv)
911 struct nfqnl_msg_packet_timestamp *qpt;
912 qpt = nfnl_get_pointer_to_data(nfad->data, NFQA_TIMESTAMP,
913 struct nfqnl_msg_packet_timestamp);
917 tv->tv_sec = __be64_to_cpu(qpt->sec);
918 tv->tv_usec = __be64_to_cpu(qpt->usec);
924 * nfq_get_indev - get the interface that the packet was received through
925 * \param nfad Netlink packet data handle passed to callback function
927 * \return The index of the device the queued packet was received via. If the
928 * returned index is 0, the packet was locally generated or the input
929 * interface is not known (ie. POSTROUTING?).
931 * \warning all nfq_get_dev() functions return 0 if not set, since linux
932 * only allows ifindex >= 1, see net/core/dev.c:2600 (in 2.6.13.1)
934 u_int32_t nfq_get_indev(struct nfq_data *nfad)
936 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_INDEV, u_int32_t));
940 * nfq_get_physindev - get the physical interface that the packet was received
941 * \param nfad Netlink packet data handle passed to callback function
943 * \return The index of the physical device the queued packet was received via.
944 * If the returned index is 0, the packet was locally generated or the
945 * physical input interface is no longer known (ie. POSTROUTING?).
947 u_int32_t nfq_get_physindev(struct nfq_data *nfad)
949 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSINDEV, u_int32_t));
953 * nfq_get_outdev - gets the interface that the packet will be routed out
954 * \param nfad Netlink packet data handle passed to callback function
956 * \return The index of the device the queued packet will be sent out. If the
957 * returned index is 0, the packet is destined for localhost or the output
958 * interface is not yet known (ie. PREROUTING?).
960 u_int32_t nfq_get_outdev(struct nfq_data *nfad)
962 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_OUTDEV, u_int32_t));
966 * nfq_get_physoutdev - get the physical interface that the packet output
967 * \param nfad Netlink packet data handle passed to callback function
969 * The index of the physical device the queued packet will be sent out.
970 * If the returned index is 0, the packet is destined for localhost or the
971 * physical output interface is not yet known (ie. PREROUTING?).
973 * \return The index of physical interface that the packet output will be routed out.
975 u_int32_t nfq_get_physoutdev(struct nfq_data *nfad)
977 return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSOUTDEV, u_int32_t));
981 * nfq_get_indev_name - get the name of the interface the packet
982 * was received through
983 * \param nlif_handle pointer to a nlif interface resolving handle
984 * \param nfad Netlink packet data handle passed to callback function
985 * \param name pointer to the buffer to receive the interface name;
986 * not more than \c IFNAMSIZ bytes will be copied to it.
987 * \return -1 in case of error, >0 if it succeed.
989 * To use a nlif_handle, You need first to call nlif_open() and to open
990 * an handler. Don't forget to store the result as it will be used
991 * during all your program life:
999 * Once the handler is open, you need to fetch the interface table at a
1000 * whole via a call to nlif_query.
1004 * libnfnetlink is able to update the interface mapping when a new interface
1005 * appears. To do so, you need to call nlif_catch() on the handler after each
1006 * interface related event. The simplest way to get and treat event is to run
1007 * a select() or poll() against the nlif file descriptor. To get this file
1008 * descriptor, you need to use nlif_fd:
1012 * Don't forget to close the handler when you don't need the feature anymore:
1018 int nfq_get_indev_name(struct nlif_handle *nlif_handle,
1019 struct nfq_data *nfad, char *name)
1021 u_int32_t ifindex = nfq_get_indev(nfad);
1022 return nlif_index2name(nlif_handle, ifindex, name);
1026 * nfq_get_physindev_name - get the name of the physical interface the
1027 * packet was received through
1028 * \param nlif_handle pointer to a nlif interface resolving handle
1029 * \param nfad Netlink packet data handle passed to callback function
1030 * \param name pointer to the buffer to receive the interface name;
1031 * not more than \c IFNAMSIZ bytes will be copied to it.
1033 * See nfq_get_indev_name() documentation for nlif_handle usage.
1035 * \return -1 in case of error, > 0 if it succeed.
1037 int nfq_get_physindev_name(struct nlif_handle *nlif_handle,
1038 struct nfq_data *nfad, char *name)
1040 u_int32_t ifindex = nfq_get_physindev(nfad);
1041 return nlif_index2name(nlif_handle, ifindex, name);
1045 * nfq_get_outdev_name - get the name of the physical interface the
1046 * packet will be sent to
1047 * \param nlif_handle pointer to a nlif interface resolving handle
1048 * \param nfad Netlink packet data handle passed to callback function
1049 * \param name pointer to the buffer to receive the interface name;
1050 * not more than \c IFNAMSIZ bytes will be copied to it.
1052 * See nfq_get_indev_name() documentation for nlif_handle usage.
1054 * \return -1 in case of error, > 0 if it succeed.
1056 int nfq_get_outdev_name(struct nlif_handle *nlif_handle,
1057 struct nfq_data *nfad, char *name)
1059 u_int32_t ifindex = nfq_get_outdev(nfad);
1060 return nlif_index2name(nlif_handle, ifindex, name);
1064 * nfq_get_physoutdev_name - get the name of the interface the
1065 * packet will be sent to
1066 * \param nlif_handle pointer to a nlif interface resolving handle
1067 * \param nfad Netlink packet data handle passed to callback function
1068 * \param name pointer to the buffer to receive the interface name;
1069 * not more than \c IFNAMSIZ bytes will be copied to it.
1071 * See nfq_get_indev_name() documentation for nlif_handle usage.
1073 * \return -1 in case of error, > 0 if it succeed.
1076 int nfq_get_physoutdev_name(struct nlif_handle *nlif_handle,
1077 struct nfq_data *nfad, char *name)
1079 u_int32_t ifindex = nfq_get_physoutdev(nfad);
1080 return nlif_index2name(nlif_handle, ifindex, name);
1086 * get hardware address
1088 * \param nfad Netlink packet data handle passed to callback function
1090 * Retrieves the hardware address associated with the given queued packet.
1091 * For ethernet packets, the hardware address returned (if any) will be the
1092 * MAC address of the packet source host. The destination MAC address is not
1093 * known until after POSTROUTING and a successful ARP request, so cannot
1094 * currently be retrieved.
1096 * The nfqnl_msg_packet_hw structure is defined in libnetfilter_queue.h as:
1098 struct nfqnl_msg_packet_hw {
1099 u_int16_t hw_addrlen;
1101 u_int8_t hw_addr[8];
1102 } __attribute__ ((packed));
1105 struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
1107 return nfnl_get_pointer_to_data(nfad->data, NFQA_HWADDR,
1108 struct nfqnl_msg_packet_hw);
1112 * nfq_get_payload - get payload
1113 * \param nfad Netlink packet data handle passed to callback function
1114 * \param data Pointer of pointer that will be pointed to the payload
1116 * Retrieve the payload for a queued packet. The actual amount and type of
1117 * data retrieved by this function will depend on the mode set with the
1118 * nfq_set_mode() function.
1120 * \return -1 on error, otherwise > 0.
1122 int nfq_get_payload(struct nfq_data *nfad, unsigned char **data)
1124 *data = (unsigned char *)
1125 nfnl_get_pointer_to_data(nfad->data, NFQA_PAYLOAD, char);
1127 return NFA_PAYLOAD(nfad->data[NFQA_PAYLOAD-1]);
1136 #define SNPRINTF_FAILURE(ret, rem, offset, len) \
1148 * \defgroup Printing Printing [DEPRECATED]
1153 * nfq_snprintf_xml - print the enqueued packet in XML format into a buffer
1154 * \param buf The buffer that you want to use to print the logged packet
1155 * \param rem The size of the buffer that you have passed
1156 * \param tb Netlink packet data handle passed to callback function
1157 * \param flags The flag that tell what to print into the buffer
1159 * This function supports the following flags:
1161 * - NFQ_XML_HW: include the hardware link layer address
1162 * - NFQ_XML_MARK: include the packet mark
1163 * - NFQ_XML_DEV: include the device information
1164 * - NFQ_XML_PHYSDEV: include the physical device information
1165 * - NFQ_XML_PAYLOAD: include the payload (in hexadecimal)
1166 * - NFQ_XML_TIME: include the timestamp
1167 * - NFQ_XML_ALL: include all the logging information (all flags set)
1169 * You can combine this flags with an binary OR.
1171 * \return -1 in case of failure, otherwise the length of the string that
1172 * would have been printed into the buffer (in case that there is enough
1173 * room in it). See snprintf() return value for more information.
1175 int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
1177 struct nfqnl_msg_packet_hdr *ph;
1178 struct nfqnl_msg_packet_hw *hwph;
1179 u_int32_t mark, ifi;
1180 int size, offset = 0, len = 0, ret;
1181 unsigned char *data;
1183 size = snprintf(buf + offset, rem, "<pkt>");
1184 SNPRINTF_FAILURE(size, rem, offset, len);
1186 if (flags & NFQ_XML_TIME) {
1191 if (localtime_r(&t, &tm) == NULL)
1194 size = snprintf(buf + offset, rem, "<when>");
1195 SNPRINTF_FAILURE(size, rem, offset, len);
1197 size = snprintf(buf + offset, rem,
1198 "<hour>%d</hour>", tm.tm_hour);
1199 SNPRINTF_FAILURE(size, rem, offset, len);
1201 size = snprintf(buf + offset,
1202 rem, "<min>%02d</min>", tm.tm_min);
1203 SNPRINTF_FAILURE(size, rem, offset, len);
1205 size = snprintf(buf + offset,
1206 rem, "<sec>%02d</sec>", tm.tm_sec);
1207 SNPRINTF_FAILURE(size, rem, offset, len);
1209 size = snprintf(buf + offset, rem, "<wday>%d</wday>",
1211 SNPRINTF_FAILURE(size, rem, offset, len);
1213 size = snprintf(buf + offset, rem, "<day>%d</day>", tm.tm_mday);
1214 SNPRINTF_FAILURE(size, rem, offset, len);
1216 size = snprintf(buf + offset, rem, "<month>%d</month>",
1218 SNPRINTF_FAILURE(size, rem, offset, len);
1220 size = snprintf(buf + offset, rem, "<year>%d</year>",
1222 SNPRINTF_FAILURE(size, rem, offset, len);
1224 size = snprintf(buf + offset, rem, "</when>");
1225 SNPRINTF_FAILURE(size, rem, offset, len);
1228 ph = nfq_get_msg_packet_hdr(tb);
1230 size = snprintf(buf + offset, rem,
1231 "<hook>%u</hook><id>%u</id>",
1232 ph->hook, ntohl(ph->packet_id));
1233 SNPRINTF_FAILURE(size, rem, offset, len);
1235 hwph = nfq_get_packet_hw(tb);
1236 if (hwph && (flags & NFQ_XML_HW)) {
1237 int i, hlen = ntohs(hwph->hw_addrlen);
1239 size = snprintf(buf + offset, rem, "<hw><proto>%04x"
1241 ntohs(ph->hw_protocol));
1242 SNPRINTF_FAILURE(size, rem, offset, len);
1244 size = snprintf(buf + offset, rem, "<src>");
1245 SNPRINTF_FAILURE(size, rem, offset, len);
1247 for (i=0; i<hlen; i++) {
1248 size = snprintf(buf + offset, rem, "%02x",
1250 SNPRINTF_FAILURE(size, rem, offset, len);
1253 size = snprintf(buf + offset, rem, "</src></hw>");
1254 SNPRINTF_FAILURE(size, rem, offset, len);
1255 } else if (flags & NFQ_XML_HW) {
1256 size = snprintf(buf + offset, rem, "<hw><proto>%04x"
1258 ntohs(ph->hw_protocol));
1259 SNPRINTF_FAILURE(size, rem, offset, len);
1263 mark = nfq_get_nfmark(tb);
1264 if (mark && (flags & NFQ_XML_MARK)) {
1265 size = snprintf(buf + offset, rem, "<mark>%u</mark>", mark);
1266 SNPRINTF_FAILURE(size, rem, offset, len);
1269 ifi = nfq_get_indev(tb);
1270 if (ifi && (flags & NFQ_XML_DEV)) {
1271 size = snprintf(buf + offset, rem, "<indev>%u</indev>", ifi);
1272 SNPRINTF_FAILURE(size, rem, offset, len);
1275 ifi = nfq_get_outdev(tb);
1276 if (ifi && (flags & NFQ_XML_DEV)) {
1277 size = snprintf(buf + offset, rem, "<outdev>%u</outdev>", ifi);
1278 SNPRINTF_FAILURE(size, rem, offset, len);
1281 ifi = nfq_get_physindev(tb);
1282 if (ifi && (flags & NFQ_XML_PHYSDEV)) {
1283 size = snprintf(buf + offset, rem,
1284 "<physindev>%u</physindev>", ifi);
1285 SNPRINTF_FAILURE(size, rem, offset, len);
1288 ifi = nfq_get_physoutdev(tb);
1289 if (ifi && (flags & NFQ_XML_PHYSDEV)) {
1290 size = snprintf(buf + offset, rem,
1291 "<physoutdev>%u</physoutdev>", ifi);
1292 SNPRINTF_FAILURE(size, rem, offset, len);
1295 ret = nfq_get_payload(tb, &data);
1296 if (ret >= 0 && (flags & NFQ_XML_PAYLOAD)) {
1299 size = snprintf(buf + offset, rem, "<payload>");
1300 SNPRINTF_FAILURE(size, rem, offset, len);
1302 for (i=0; i<ret; i++) {
1303 size = snprintf(buf + offset, rem, "%02x",
1305 SNPRINTF_FAILURE(size, rem, offset, len);
1308 size = snprintf(buf + offset, rem, "</payload>");
1309 SNPRINTF_FAILURE(size, rem, offset, len);
1312 size = snprintf(buf + offset, rem, "</pkt>");
1313 SNPRINTF_FAILURE(size, rem, offset, len);