nfq: deprecate nfq_set_verdict_mark() in favour of nfq_set_verdict2()
[platform/upstream/libnetfilter_queue.git] / src / libnetfilter_queue.c
1 /* libnetfilter_queue.c: generic library for access to nf_queue
2  *
3  * (C) 2005 by Harald Welte <laforge@gnumonks.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License version 2 
7  *  as published by the Free Software Foundation
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  2006-01-23 Andreas Florath <andreas@florath.net>
19  *      Fix __set_verdict() that it can now handle payload.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31
32 #include <libnfnetlink/libnfnetlink.h>
33 #include <libnetfilter_queue/libnetfilter_queue.h>
34
35 /**
36  * \mainpage
37  *
38  * libnetfilter_queue is a userspace library providing an API to packets that
39  * have been queued by the kernel packet filter. It is is part of a system that
40  * deprecates the old ip_queue / libipq mechanism.
41  *
42  * libnetfilter_queue homepage is:
43  *      http://netfilter.org/projects/libnetfilter_queue/
44  *
45  * \section Dependencies
46  * libnetfilter_queue requires libnfnetlink and a kernel that includes the
47  * nfnetlink_queue subsystem (i.e. 2.6.14 or later).
48  *
49  * \section Main Features
50  *  - receiving queued packets from the kernel nfnetlink_queue subsystem
51  *  - issuing verdicts and/or reinjecting altered packets to the kernel
52  *  nfnetlink_queue subsystem
53  * 
54  * \section Git Tree
55  * The current development version of libnetfilter_queue can be accessed
56  * at https://git.netfilter.org/cgi-bin/gitweb.cgi?p=libnetfilter_queue.git;a=summary.
57  *
58  * \section Privileges
59  * You need the CAP_NET_ADMIN capability in order to allow your application
60  * to receive from and to send packets to kernel-space.
61  *
62  * \section Using libnetfilter_queue
63  * 
64  * To write your own program using libnetfilter_queue, you should start by reading
65  * the doxygen documentation (start by \link LibrarySetup \endlink page) and nfqnl_test.c source file.
66  * 
67  */
68
69 struct nfq_handle
70 {
71         struct nfnl_handle *nfnlh;
72         struct nfnl_subsys_handle *nfnlssh;
73         struct nfq_q_handle *qh_list;
74 };
75
76 struct nfq_q_handle
77 {
78         struct nfq_q_handle *next;
79         struct nfq_handle *h;
80         u_int16_t id;
81
82         nfq_callback *cb;
83         void *data;
84 };
85
86 struct nfq_data {
87         struct nfattr **data;
88 };
89
90 int nfq_errno;
91
92 /***********************************************************************
93  * low level stuff 
94  ***********************************************************************/
95
96 static void del_qh(struct nfq_q_handle *qh)
97 {
98         struct nfq_q_handle *cur_qh, *prev_qh = NULL;
99
100         for (cur_qh = qh->h->qh_list; cur_qh; cur_qh = cur_qh->next) {
101                 if (cur_qh == qh) {
102                         if (prev_qh)
103                                 prev_qh->next = qh->next;
104                         else
105                                 qh->h->qh_list = qh->next;
106                         return;
107                 }
108                 prev_qh = cur_qh;
109         }
110 }
111
112 static void add_qh(struct nfq_q_handle *qh)
113 {
114         qh->next = qh->h->qh_list;
115         qh->h->qh_list = qh;
116 }
117
118 static struct nfq_q_handle *find_qh(struct nfq_handle *h, u_int16_t id)
119 {
120         struct nfq_q_handle *qh;
121
122         for (qh = h->qh_list; qh; qh = qh->next) {
123                 if (qh->id == id)
124                         return qh;
125         }
126         return NULL;
127 }
128
129 /* build a NFQNL_MSG_CONFIG message */
130         static int
131 __build_send_cfg_msg(struct nfq_handle *h, u_int8_t command,
132                 u_int16_t queuenum, u_int16_t pf)
133 {
134         union {
135                 char buf[NFNL_HEADER_LEN
136                         +NFA_LENGTH(sizeof(struct nfqnl_msg_config_cmd))];
137                 struct nlmsghdr nmh;
138         } u;
139         struct nfqnl_msg_config_cmd cmd;
140
141         nfnl_fill_hdr(h->nfnlssh, &u.nmh, 0, AF_UNSPEC, queuenum,
142                         NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
143
144         cmd.command = command;
145         cmd.pf = htons(pf);
146         nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_CMD, &cmd, sizeof(cmd));
147
148         return nfnl_query(h->nfnlh, &u.nmh);
149 }
150
151 static int __nfq_rcv_pkt(struct nlmsghdr *nlh, struct nfattr *nfa[],
152                 void *data)
153 {
154         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
155         struct nfq_handle *h = data;
156         u_int16_t queue_num = ntohs(nfmsg->res_id);
157         struct nfq_q_handle *qh = find_qh(h, queue_num);
158         struct nfq_data nfqa;
159
160         if (!qh)
161                 return -ENODEV;
162
163         if (!qh->cb)
164                 return -ENODEV;
165
166         nfqa.data = nfa;
167
168         return qh->cb(qh, nfmsg, &nfqa, qh->data);
169 }
170
171 static struct nfnl_callback pkt_cb = {
172         .call           = &__nfq_rcv_pkt,
173         .attr_count     = NFQA_MAX,
174 };
175
176 /* public interface */
177
178 struct nfnl_handle *nfq_nfnlh(struct nfq_handle *h)
179 {
180         return h->nfnlh;
181 }
182
183 /**
184  *
185  * \defgroup Queue Queue handling
186  *
187  * Once libnetfilter_queue library has been initialised (See 
188  * \link LibrarySetup \endlink), it is possible to bind the program to a
189  * specific queue. This can be done by using nfq_create_queue().
190  *
191  * The queue can then be tuned via nfq_set_mode() or nfq_set_queue_maxlen().
192  * 
193  * Here's a little code snippet that create queue numbered 0:
194  * \verbatim
195         printf("binding this socket to queue '0'\n");
196         qh = nfq_create_queue(h,  0, &cb, NULL);
197         if (!qh) {
198                 fprintf(stderr, "error during nfq_create_queue()\n");
199                 exit(1);
200         }
201
202         printf("setting copy_packet mode\n");
203         if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
204                 fprintf(stderr, "can't set packet_copy mode\n");
205                 exit(1);
206         }
207 \endverbatim
208  *
209  * Next step is the handling of incoming packets which can be done via a loop:
210  *
211  * \verbatim
212         fd = nfq_fd(h);
213
214         while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
215                 printf("pkt received\n");
216                 nfq_handle_packet(h, buf, rv);
217         }
218 \endverbatim
219  * When the decision on a packet has been choosed, the verdict has to be given
220  * by calling nfq_set_verdict() or nfq_set_verdict_mark().
221  *
222  * Data and information about the packet can be fetch by using message parsing
223  * functions (See \link Parsing \endlink).
224  * @{
225  */
226
227 /**
228  * nfq_fd - get the file descriptor associated with the nfqueue handler
229  * \param h Netfilter queue connection handle obtained via call to nfq_open()
230  *
231  * \return a file descriptor for the netlink connection associated with the
232  * given queue connection handle. The file descriptor can then be used for
233  * receiving the queued packets for processing.
234  *
235   * This function returns a file descriptor that can be used for communication
236  * over the netlink connection associated with the given queue connection
237  * handle.
238  */
239 int nfq_fd(struct nfq_handle *h)
240 {
241         return nfnl_fd(nfq_nfnlh(h));
242 }
243
244 /**
245  * @}
246  */
247
248 /**
249  * \defgroup LibrarySetup Library setup
250  *
251  * Library initialisation is made in two steps.
252  *
253  * First step is to call nfq_open() to open a NFQUEUE handler. 
254  *
255  * Second step is to tell the kernel that userspace queueing is handle by
256  * NFQUEUE for the selected protocol. This is made by calling nfq_unbind_pf()
257  * and nfq_bind_pf() with protocol information. The idea behind this is to
258  * enable simultaneously loaded modules to be used for queuing.
259  *
260  * Here's a little code snippet that bind with AF_INET:
261  * \verbatim
262         h = nfq_open();
263         if (!h) {
264                 fprintf(stderr, "error during nfq_open()\n");
265                 exit(1);
266         }
267
268         printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
269         if (nfq_unbind_pf(h, AF_INET) < 0) {
270                 fprintf(stderr, "error during nfq_unbind_pf()\n");
271                 exit(1);
272         }
273
274         printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
275         if (nfq_bind_pf(h, AF_INET) < 0) {
276                 fprintf(stderr, "error during nfq_bind_pf()\n");
277                 exit(1);
278         }
279 \endverbatim
280  * Once this is done, you can setup and use a \link Queue \endlink.
281  * @{
282  */
283
284 /**
285  * nfq_open - open a nfqueue handler
286  *
287  * This function obtains a netfilter queue connection handle. When you are
288  * finished with the handle returned by this function, you should destroy
289  * it by calling nfq_close(). A new netlink connection is obtained internally
290  * and associated with the queue connection handle returned.
291  *
292  * \return a pointer to a new queue handle or NULL on failure.
293  */
294 struct nfq_handle *nfq_open(void)
295 {
296         struct nfnl_handle *nfnlh = nfnl_open();
297         struct nfq_handle *qh;
298
299         if (!nfnlh)
300                 return NULL;
301
302         /* unset netlink sequence tracking by default */
303         nfnl_unset_sequence_tracking(nfnlh);
304
305         qh = nfq_open_nfnl(nfnlh);
306         if (!qh)
307                 nfnl_close(nfnlh);
308
309         return qh;
310 }
311
312 /**
313  * @}
314  */
315
316 /**
317  * nfq_open_nfnl - open a nfqueue handler from a existing nfnetlink handler
318  * \param nfnlh Netfilter netlink connection handle obtained by calling nfnl_open()
319  *
320  * This function obtains a netfilter queue connection handle using an existing
321  * netlink connection. This function is used internally to implement 
322  * nfq_open(), and should typically not be called directly.
323  *
324  * \return a pointer to a new queue handle or NULL on failure.
325  */
326 struct nfq_handle *nfq_open_nfnl(struct nfnl_handle *nfnlh)
327 {
328         struct nfq_handle *h;
329         int err;
330
331         h = malloc(sizeof(*h));
332         if (!h)
333                 return NULL;
334
335         memset(h, 0, sizeof(*h));
336         h->nfnlh = nfnlh;
337
338         h->nfnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_QUEUE, 
339                                       NFQNL_MSG_MAX, 0);
340         if (!h->nfnlssh) {
341                 /* FIXME: nfq_errno */
342                 goto out_free;
343         }
344
345         pkt_cb.data = h;
346         err = nfnl_callback_register(h->nfnlssh, NFQNL_MSG_PACKET, &pkt_cb);
347         if (err < 0) {
348                 nfq_errno = err;
349                 goto out_close;
350         }
351
352         return h;
353 out_close:
354         nfnl_subsys_close(h->nfnlssh);
355 out_free:
356         free(h);
357         return NULL;
358 }
359
360 /**
361  * \addtogroup LibrarySetup
362  *
363  * When the program has finished with libnetfilter_queue, it has to call
364  * the nfq_close() function to free all associated resources.
365  *
366  * @{
367  */
368
369 /**
370  * nfq_close - close a nfqueue handler
371  * \param h Netfilter queue connection handle obtained via call to nfq_open()
372  *
373  * This function closes the nfqueue handler and free associated resources.
374  *
375  * \return 0 on success, non-zero on failure. 
376  */
377 int nfq_close(struct nfq_handle *h)
378 {
379         int ret;
380         
381         ret = nfnl_close(h->nfnlh);
382         if (ret == 0)
383                 free(h);
384         return ret;
385 }
386
387 /**
388  * nfq_bind_pf - bind a nfqueue handler to a given protocol family
389  * \param h Netfilter queue connection handle obtained via call to nfq_open()
390  * \param pf protocol family to bind to nfqueue handler obtained from nfq_open()
391  *
392  * Binds the given queue connection handle to process packets belonging to 
393  * the given protocol family (ie. PF_INET, PF_INET6, etc).
394  *
395  * \return integer inferior to 0 in case of failure
396  */
397 int nfq_bind_pf(struct nfq_handle *h, u_int16_t pf)
398 {
399         return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_BIND, 0, pf);
400 }
401
402 /**
403  * nfq_unbind_pf - unbind nfqueue handler from a protocol family
404  * \param h Netfilter queue connection handle obtained via call to nfq_open()
405  * \param pf protocol family to unbind family from
406  *
407  * Unbinds the given queue connection handle from processing packets belonging
408  * to the given protocol family.
409  */
410 int nfq_unbind_pf(struct nfq_handle *h, u_int16_t pf)
411 {
412         return __build_send_cfg_msg(h, NFQNL_CFG_CMD_PF_UNBIND, 0, pf);
413 }
414
415
416
417 /**
418  * @}
419  */
420
421 /**
422  * \addtogroup Queue
423  * @{
424  */
425
426 /**
427  * nfq_create_queue - create a new queue handle and return it.
428  *
429  * \param h Netfilter queue connection handle obtained via call to nfq_open()
430  * \param num the number of the queue to bind to
431  * \param cb callback function to call for each queued packet
432  * \param data custom data to pass to the callback function
433  *
434  * \return a nfq_q_handle pointing to the newly created queue
435  *
436  * Creates a new queue handle, and returns it.  The new queue is identified by
437  * #num, and the callback specified by #cb will be called for each enqueued
438  * packet.  The #data argument will be passed unchanged to the callback.  If
439  * a queue entry with id #num already exists, this function will return failure
440  * and the existing entry is unchanged.
441  *
442  * The nfq_callback type is defined in libnetfilter_queue.h as:
443  * \verbatim
444 typedef int nfq_callback(struct nfq_q_handle *qh,
445                          struct nfgenmsg *nfmsg,
446                          struct nfq_data *nfad, void *data);
447 \endverbatim
448  *
449  * Parameters:
450  *  - qh The queue handle returned by nfq_create_queue
451  *  - nfmsg message objetc that contains the packet
452  *  - nfad Netlink packet data handle
453  *  - data the value passed to the data parameter of nfq_create_queue
454  *
455  * The callback should return < 0 to stop processing.
456  */
457
458 struct nfq_q_handle *nfq_create_queue(struct nfq_handle *h, 
459                 u_int16_t num,
460                 nfq_callback *cb,
461                 void *data)
462 {
463         int ret;
464         struct nfq_q_handle *qh;
465
466         if (find_qh(h, num))
467                 return NULL;
468
469         qh = malloc(sizeof(*qh));
470
471         memset(qh, 0, sizeof(*qh));
472         qh->h = h;
473         qh->id = num;
474         qh->cb = cb;
475         qh->data = data;
476
477         ret = __build_send_cfg_msg(h, NFQNL_CFG_CMD_BIND, num, 0);
478         if (ret < 0) {
479                 nfq_errno = ret;
480                 free(qh);
481                 return NULL;
482         }
483
484         add_qh(qh);
485         return qh;
486 }
487
488 /**
489  * @}
490  */
491
492 /**
493  * \addtogroup Queue
494  * @{
495  */
496
497 /**
498  * nfq_destroy_queue - destroy a queue handle
499  * \param qh queue handle that we want to destroy created via nfq_create_queue
500  *
501  * Removes the binding for the specified queue handle. This call also unbind
502  * from the nfqueue handler, so you don't have to call nfq_unbind_pf.
503  */
504 int nfq_destroy_queue(struct nfq_q_handle *qh)
505 {
506         int ret = __build_send_cfg_msg(qh->h, NFQNL_CFG_CMD_UNBIND, qh->id, 0);
507         if (ret == 0) {
508                 del_qh(qh);
509                 free(qh);
510         }
511
512         return ret;
513 }
514
515 /**
516  * nfq_handle_packet - handle a packet received from the nfqueue subsystem
517  * \param h Netfilter queue connection handle obtained via call to nfq_open()
518  * \param buf data to pass to the callback
519  * \param len length of packet data in buffer
520  *
521  * Triggers an associated callback for the given packet received from the
522  * queue. Packets can be read from the queue using nfq_fd() and recv(). See
523  * example code for nfq_fd().
524  *
525  * \return 0 on success, non-zero on failure.
526  */
527 int nfq_handle_packet(struct nfq_handle *h, char *buf, int len)
528 {
529         return nfnl_handle_packet(h->nfnlh, buf, len);
530 }
531
532 /**
533  * nfq_set_mode - set the amount of packet data that nfqueue copies to userspace
534  * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
535  * \param mode the part of the packet that we are interested in
536  * \param range size of the packet that we want to get
537  *
538  * Sets the amount of data to be copied to userspace for each packet queued
539  * to the given queue.
540  *
541  * - NFQNL_COPY_NONE - do not copy any data
542  * - NFQNL_COPY_META - copy only packet metadata
543  * - NFQNL_COPY_PACKET - copy entire packet
544  */
545 int nfq_set_mode(struct nfq_q_handle *qh,
546                 u_int8_t mode, u_int32_t range)
547 {
548         union {
549                 char buf[NFNL_HEADER_LEN
550                         +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
551                 struct nlmsghdr nmh;
552         } u;
553         struct nfqnl_msg_config_params params;
554
555         nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
556                         NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
557
558         params.copy_range = htonl(range);
559         params.copy_mode = mode;
560         nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_PARAMS, &params,
561                         sizeof(params));
562
563         return nfnl_query(qh->h->nfnlh, &u.nmh);
564 }
565
566 /**
567  * nfq_set_queue_maxlen - Set kernel queue maximum length parameter
568  * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
569  * \param queuelen the length of the queue
570  *
571  * Sets the size of the queue in kernel. This fixes the maximum number
572  * of packets the kernel will store before internally before dropping
573  * upcoming packets.
574  */
575 int nfq_set_queue_maxlen(struct nfq_q_handle *qh,
576                                 u_int32_t queuelen)
577 {
578         union {
579                 char buf[NFNL_HEADER_LEN
580                         +NFA_LENGTH(sizeof(struct nfqnl_msg_config_params))];
581                 struct nlmsghdr nmh;
582         } u;
583         u_int32_t queue_maxlen = htonl(queuelen);
584
585         nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
586                         NFQNL_MSG_CONFIG, NLM_F_REQUEST|NLM_F_ACK);
587
588         nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_CFG_QUEUE_MAXLEN, &queue_maxlen,
589                         sizeof(queue_maxlen));
590
591         return nfnl_query(qh->h->nfnlh, &u.nmh);
592 }
593
594 /**
595  * @}
596  */
597
598 static int __set_verdict(struct nfq_q_handle *qh, u_int32_t id,
599                 u_int32_t verdict, u_int32_t mark, int set_mark,
600                 u_int32_t data_len, unsigned char *data)
601 {
602         struct nfqnl_msg_verdict_hdr vh;
603         union {
604                 char buf[NFNL_HEADER_LEN
605                         +NFA_LENGTH(sizeof(mark))
606                         +NFA_LENGTH(sizeof(vh))];
607                 struct nlmsghdr nmh;
608         } u;
609
610         struct iovec iov[3];
611         int nvecs;
612
613         /* This must be declared here (and not inside the data
614          * handling block) because the iovec points to this. */
615         struct nfattr data_attr;
616
617         memset(iov, 0, sizeof(iov));
618
619         vh.verdict = htonl(verdict);
620         vh.id = htonl(id);
621
622         nfnl_fill_hdr(qh->h->nfnlssh, &u.nmh, 0, AF_UNSPEC, qh->id,
623                         NFQNL_MSG_VERDICT, NLM_F_REQUEST);
624
625         /* add verdict header */
626         nfnl_addattr_l(&u.nmh, sizeof(u), NFQA_VERDICT_HDR, &vh, sizeof(vh));
627
628         if (set_mark)
629                 nfnl_addattr32(&u.nmh, sizeof(u), NFQA_MARK, mark);
630
631         iov[0].iov_base = &u.nmh;
632         iov[0].iov_len = NLMSG_TAIL(&u.nmh) - (void *)&u.nmh;
633         nvecs = 1;
634
635         if (data_len) {
636                 nfnl_build_nfa_iovec(&iov[1], &data_attr, NFQA_PAYLOAD,
637                                 data_len, data);
638                 nvecs += 2;
639                 /* Add the length of the appended data to the message
640                  * header.  The size of the attribute is given in the
641                  * nfa_len field and is set in the nfnl_build_nfa_iovec()
642                  * function. */
643                 u.nmh.nlmsg_len += data_attr.nfa_len;
644         }
645
646         return nfnl_sendiov(qh->h->nfnlh, iov, nvecs, 0);
647 }
648
649 /**
650  * \addtogroup Queue
651  * @{
652  */
653
654 /**
655  * nfq_set_verdict - issue a verdict on a packet 
656  * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
657  * \param id    ID assigned to packet by netfilter.
658  * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
659  * \param data_len number of bytes of data pointed to by #buf
660  * \param buf the buffer that contains the packet data
661  *
662  * Can be obtained by: 
663  * \verbatim
664         int id;
665         struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(tb);
666         if (ph)
667                 id = ntohl(ph->packet_id);
668 \endverbatim
669  *
670  * Notifies netfilter of the userspace verdict for the given packet.  Every
671  * queued packet _must_ have a verdict specified by userspace, either by
672  * calling this function, or by calling the nfq_set_verdict_mark() function.
673  */
674 int nfq_set_verdict(struct nfq_q_handle *qh, u_int32_t id,
675                 u_int32_t verdict, u_int32_t data_len, 
676                 unsigned char *buf)
677 {
678         return __set_verdict(qh, id, verdict, 0, 0, data_len, buf);
679 }       
680
681 /**
682  * nfq_set_verdict2 - like nfq_set_verdict, but you can set the mark.
683  * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
684  * \param id    ID assigned to packet by netfilter.
685  * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
686  * \param mark mark to put on packet
687  * \param data_len number of bytes of data pointed to by #buf
688  * \param buf the buffer that contains the packet data
689  */
690 int nfq_set_verdict2(struct nfq_q_handle *qh, u_int32_t id,
691                      u_int32_t verdict, u_int32_t mark,
692                      u_int32_t data_len, unsigned char *buf)
693 {
694         return __set_verdict(qh, id, verdict, htonl(mark), 1, data_len, buf);
695 }
696
697 /**
698  * nfq_set_verdict_mark - like nfq_set_verdict, but you can set the mark.
699  * \param qh Netfilter queue handle obtained by call to nfq_create_queue().
700  * \param id    ID assigned to packet by netfilter.
701  * \param verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP)
702  * \param mark mark to put on packet
703  * \param data_len number of bytes of data pointed to by #buf
704  * \param buf the buffer that contains the packet data
705  *
706  * This function is deprecated since it is broken, its use is highly
707  * discouraged. Please, use nfq_set_verdict2 instead.
708  */
709 int nfq_set_verdict_mark(struct nfq_q_handle *qh, u_int32_t id,
710                 u_int32_t verdict, u_int32_t mark,
711                 u_int32_t data_len, unsigned char *buf)
712 {
713         return __set_verdict(qh, id, verdict, mark, 1, data_len, buf);
714 }
715
716 /**
717  * @}
718  */
719
720
721
722 /*************************************************************
723  * Message parsing functions 
724  *************************************************************/
725
726 /**
727  * \defgroup Parsing Message parsing functions
728  * @{
729  */
730
731 /**
732  * nfqnl_msg_packet_hdr - return the metaheader that wraps the packet
733  * \param nfad Netlink packet data handle passed to callback function
734  *
735  * \return the netfilter queue netlink packet header for the given
736  * nfq_data argument.  Typically, the nfq_data value is passed as the 3rd
737  * parameter to the callback function set by a call to nfq_create_queue().
738  *
739  * The nfqnl_msg_packet_hdr structure is defined in libnetfilter_queue.h as:
740  *
741  * \verbatim
742         struct nfqnl_msg_packet_hdr {
743                 u_int32_t       packet_id;      // unique ID of packet in queue
744                 u_int16_t       hw_protocol;    // hw protocol (network order)
745                 u_int8_t        hook;           // netfilter hook
746         } __attribute__ ((packed));
747 \endverbatim
748  */
749 struct nfqnl_msg_packet_hdr *nfq_get_msg_packet_hdr(struct nfq_data *nfad)
750 {
751         return nfnl_get_pointer_to_data(nfad->data, NFQA_PACKET_HDR,
752                                         struct nfqnl_msg_packet_hdr);
753 }
754
755 /**
756  * nfq_get_nfmark - get the packet mark
757  * \param nfad Netlink packet data handle passed to callback function
758  *
759  * \return the netfilter mark currently assigned to the given queued packet.
760  */
761 uint32_t nfq_get_nfmark(struct nfq_data *nfad)
762 {
763         return ntohl(nfnl_get_data(nfad->data, NFQA_MARK, u_int32_t));
764 }
765
766 /**
767  * nfq_get_timestamp - get the packet timestamp
768  * \param nfad Netlink packet data handle passed to callback function
769  * \param tv structure to fill with timestamp info
770  *
771  * Retrieves the received timestamp when the given queued packet.
772  *
773  * \return 0 on success, non-zero on failure.
774  */
775 int nfq_get_timestamp(struct nfq_data *nfad, struct timeval *tv)
776 {
777         struct nfqnl_msg_packet_timestamp *qpt;
778         qpt = nfnl_get_pointer_to_data(nfad->data, NFQA_TIMESTAMP,
779                                         struct nfqnl_msg_packet_timestamp);
780         if (!qpt)
781                 return -1;
782
783         tv->tv_sec = __be64_to_cpu(qpt->sec);
784         tv->tv_usec = __be64_to_cpu(qpt->usec);
785
786         return 0;
787 }
788
789 /**
790  * nfq_get_indev - get the interface that the packet was received through
791  * \param nfad Netlink packet data handle passed to callback function
792  *
793  * \return The index of the device the queued packet was received via.  If the
794  * returned index is 0, the packet was locally generated or the input
795  * interface is not known (ie. POSTROUTING?).
796  *
797  * \warning all nfq_get_dev() functions return 0 if not set, since linux
798  * only allows ifindex >= 1, see net/core/dev.c:2600  (in 2.6.13.1)
799  */
800 u_int32_t nfq_get_indev(struct nfq_data *nfad)
801 {
802         return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_INDEV, u_int32_t));
803 }
804
805 /**
806  * nfq_get_physindev - get the physical interface that the packet was received
807  * \param nfad Netlink packet data handle passed to callback function
808  *
809  * \return The index of the physical device the queued packet was received via.
810  * If the returned index is 0, the packet was locally generated or the
811  * physical input interface is no longer known (ie. POSTROUTING?).
812  */
813 u_int32_t nfq_get_physindev(struct nfq_data *nfad)
814 {
815         return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSINDEV, u_int32_t));
816 }
817
818 /**
819  * nfq_get_outdev - gets the interface that the packet will be routed out
820  * \param nfad Netlink packet data handle passed to callback function
821  *
822  * \return The index of the device the queued packet will be sent out.  If the
823  * returned index is 0, the packet is destined for localhost or the output
824  * interface is not yet known (ie. PREROUTING?).
825  */
826 u_int32_t nfq_get_outdev(struct nfq_data *nfad)
827 {
828         return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_OUTDEV, u_int32_t));
829 }
830
831 /**
832  * nfq_get_physoutdev - get the physical interface that the packet output
833  * \param nfad Netlink packet data handle passed to callback function
834  *
835  * The index of the physical device the queued packet will be sent out.
836  * If the returned index is 0, the packet is destined for localhost or the
837  * physical output interface is not yet known (ie. PREROUTING?).
838  * 
839  * \return The index of physical interface that the packet output will be routed out.
840  */
841 u_int32_t nfq_get_physoutdev(struct nfq_data *nfad)
842 {
843         return ntohl(nfnl_get_data(nfad->data, NFQA_IFINDEX_PHYSOUTDEV, u_int32_t));
844 }
845
846 /**
847  * nfq_get_indev_name - get the name of the interface the packet
848  * was received through
849  * \param nlif_handle pointer to a nlif interface resolving handle
850  * \param nfad Netlink packet data handle passed to callback function
851  * \param name pointer that will be set to the interface name string 
852  * \return -1 in case of error, >0 if it succeed. 
853  *
854  * The #name variable will point to the name of the input interface.
855  *
856  * To use a nlif_handle, You need first to call nlif_open() and to open
857  * an handler. Don't forget to store the result as it will be used 
858  * during all your program life:
859  * \verbatim
860         h = nlif_open();
861         if (h == NULL) {
862                 perror("nlif_open");
863                 exit(EXIT_FAILURE);
864         }
865 \endverbatim
866  * Once the handler is open, you need to fetch the interface table at a
867  * whole via a call to nlif_query.
868  * \verbatim
869         nlif_query(h);
870 \endverbatim
871  * libnfnetlink is able to update the interface mapping when a new interface
872  * appears. To do so, you need to call nlif_catch() on the handler after each
873  * interface related event. The simplest way to get and treat event is to run
874  * a select() or poll() against the nlif file descriptor. To get this file 
875  * descriptor, you need to use nlif_fd:
876  * \verbatim
877         if_fd = nlif_fd(h);
878 \endverbatim
879  * Don't forget to close the handler when you don't need the feature anymore:
880  * \verbatim
881         nlif_close(h);
882 \endverbatim
883  *
884  */
885 int nfq_get_indev_name(struct nlif_handle *nlif_handle,
886                         struct nfq_data *nfad, char *name)
887 {
888         u_int32_t ifindex = nfq_get_indev(nfad);
889         return nlif_index2name(nlif_handle, ifindex, name);
890 }
891
892 /**
893  * nfq_get_physindev_name - get the name of the physical interface the
894  * packet was received through
895  * \param nlif_handle pointer to a nlif interface resolving handle
896  * \param nfad Netlink packet data handle passed to callback function
897  * \param name pointer that will be set to the interface name string 
898  *
899  * The #name variable will point to the name of the input physical
900  * interface.
901  *
902  * See nfq_get_indev_name() documentation for nlif_handle usage.
903  *
904  * \return  -1 in case of error, > 0 if it succeed. 
905  */
906 int nfq_get_physindev_name(struct nlif_handle *nlif_handle,
907                            struct nfq_data *nfad, char *name)
908 {
909         u_int32_t ifindex = nfq_get_physindev(nfad);
910         return nlif_index2name(nlif_handle, ifindex, name);
911 }
912
913 /**
914  * nfq_get_outdev_name - get the name of the physical interface the
915  * packet will be sent to
916  * \param nlif_handle pointer to a nlif interface resolving handle
917  * \param nfad Netlink packet data handle passed to callback function
918  * \param name pointer that will be set to the interface name string 
919  *
920  * The #name variable will point to the name of the output interface.
921  *
922  * See nfq_get_indev_name() documentation for nlif_handle usage.
923  *
924  * \return  -1 in case of error, > 0 if it succeed. 
925  */
926 int nfq_get_outdev_name(struct nlif_handle *nlif_handle,
927                         struct nfq_data *nfad, char *name)
928 {
929         u_int32_t ifindex = nfq_get_outdev(nfad);
930         return nlif_index2name(nlif_handle, ifindex, name);
931 }
932
933 /**
934  * nfq_get_physoutdev_name - get the name of the interface the
935  * packet will be sent to
936  * \param nlif_handle pointer to a nlif interface resolving handle
937  * \param nfad Netlink packet data handle passed to callback function
938  * \param name pointer that will be set to the interface name string 
939  * The #name variable will point to the name of the physical
940  * output interface.
941  *
942  * See nfq_get_indev_name() documentation for nlif_handle usage.
943  *
944  * \return  -1 in case of error, > 0 if it succeed. 
945  */
946
947 int nfq_get_physoutdev_name(struct nlif_handle *nlif_handle,
948                             struct nfq_data *nfad, char *name)
949 {
950         u_int32_t ifindex = nfq_get_physoutdev(nfad);
951         return nlif_index2name(nlif_handle, ifindex, name);
952 }
953
954 /**
955  * nfq_get_packet_hw
956  *
957  * get hardware address 
958  *
959  * \param nfad Netlink packet data handle passed to callback function
960  *
961  * Retrieves the hardware address associated with the given queued packet.
962  * For ethernet packets, the hardware address returned (if any) will be the
963  * MAC address of the packet source host.  The destination MAC address is not
964  * known until after POSTROUTING and a successful ARP request, so cannot
965  * currently be retrieved.
966  *
967  * The nfqnl_msg_packet_hw structure is defined in libnetfilter_queue.h as:
968  * \verbatim
969         struct nfqnl_msg_packet_hw {
970                 u_int16_t       hw_addrlen;
971                 u_int16_t       _pad;
972                 u_int8_t        hw_addr[8];
973         } __attribute__ ((packed));
974 \endverbatim
975  */
976 struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
977 {
978         return nfnl_get_pointer_to_data(nfad->data, NFQA_HWADDR,
979                                         struct nfqnl_msg_packet_hw);
980 }
981
982 /**
983  * nfq_get_payload - get payload 
984  * \param nfad Netlink packet data handle passed to callback function
985  * \param data Pointer of pointer that will be pointed to the payload
986  *
987  * Retrieve the payload for a queued packet. The actual amount and type of
988  * data retrieved by this function will depend on the mode set with the
989  * nfq_set_mode() function.
990  *
991  * \return -1 on error, otherwise > 0.
992  */
993 int nfq_get_payload(struct nfq_data *nfad, char **data)
994 {
995         *data = nfnl_get_pointer_to_data(nfad->data, NFQA_PAYLOAD, char);
996         if (*data)
997                 return NFA_PAYLOAD(nfad->data[NFQA_PAYLOAD-1]);
998
999         return -1;
1000 }
1001
1002 /**
1003  * @}
1004  */