.TH "Queue" 3 "Thu Jan 28 2021" "Version 1.0.5" "libnetfilter_queue" \" -*- nroff -*- .ad l .nh .SH NAME Queue \- Queue handling [DEPRECATED] .SH SYNOPSIS .br .PP .SS "Functions" .in +1c .ti -1c .RI "int \fBnfq_fd\fP (struct nfq_handle *h)" .br .ti -1c .RI "struct nfq_q_handle * \fBnfq_create_queue\fP (struct nfq_handle *h, uint16_t num, nfq_callback *cb, void *data)" .br .ti -1c .RI "int \fBnfq_destroy_queue\fP (struct nfq_q_handle *qh)" .br .ti -1c .RI "int \fBnfq_handle_packet\fP (struct nfq_handle *h, char *buf, int len)" .br .ti -1c .RI "int \fBnfq_set_mode\fP (struct nfq_q_handle *qh, uint8_t mode, uint32_t range)" .br .ti -1c .RI "int \fBnfq_set_queue_flags\fP (struct nfq_q_handle *qh, uint32_t mask, uint32_t flags)" .br .ti -1c .RI "int \fBnfq_set_queue_maxlen\fP (struct nfq_q_handle *qh, uint32_t queuelen)" .br .ti -1c .RI "int \fBnfq_set_verdict\fP (struct nfq_q_handle *qh, uint32_t id, uint32_t verdict, uint32_t data_len, const unsigned char *buf)" .br .ti -1c .RI "int \fBnfq_set_verdict2\fP (struct nfq_q_handle *qh, uint32_t id, uint32_t verdict, uint32_t mark, uint32_t data_len, const unsigned char *buf)" .br .ti -1c .RI "int \fBnfq_set_verdict_batch\fP (struct nfq_q_handle *qh, uint32_t id, uint32_t verdict)" .br .ti -1c .RI "int \fBnfq_set_verdict_batch2\fP (struct nfq_q_handle *qh, uint32_t id, uint32_t verdict, uint32_t mark)" .br .ti -1c .RI "int \fBnfq_set_verdict_mark\fP (struct nfq_q_handle *qh, uint32_t id, uint32_t verdict, uint32_t mark, uint32_t data_len, const unsigned char *buf)" .br .in -1c .SH "Detailed Description" .PP Once libnetfilter_queue library has been initialised (See \fBLibrarySetup\fP), it is possible to bind the program to a specific queue\&. This can be done by using \fBnfq_create_queue()\fP\&. .PP The queue can then be tuned via \fBnfq_set_mode()\fP or \fBnfq_set_queue_maxlen()\fP\&. .PP Here's a little code snippet that create queue numbered 0: .PP .nf printf("binding this socket to queue '0'\n"); qh = nfq_create_queue(h, 0, &cb, NULL); if (!qh) { fprintf(stderr, "error during nfq_create_queue()\n"); exit(1); } printf("setting copy_packet mode\n"); if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) { fprintf(stderr, "can't set packet_copy mode\n"); exit(1); } .fi .PP .PP Next step is the handling of incoming packets which can be done via a loop: .PP .PP .nf fd = nfq_fd(h); while ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) { printf("pkt received\n"); nfq_handle_packet(h, buf, rv); } .fi .PP When the decision on a packet has been choosed, the verdict has to be given by calling \fBnfq_set_verdict()\fP or \fBnfq_set_verdict2()\fP\&. The verdict determines the destiny of the packet as follows: .PP .IP "\(bu" 2 NF_DROP discarded the packet .IP "\(bu" 2 NF_ACCEPT the packet passes, continue iterations .IP "\(bu" 2 NF_QUEUE inject the packet into a different queue (the target queue number is in the high 16 bits of the verdict) .IP "\(bu" 2 NF_REPEAT iterate the same cycle once more .IP "\(bu" 2 NF_STOP accept, but don't continue iterations .PP .PP The verdict NF_STOLEN must not be used, as it has special meaning in the kernel\&. When using NF_REPEAT, one way to prevent re-queueing of the same packet is to also set an nfmark using nfq_set_verdict2, and set up the nefilter rules to only queue a packet when the mark is not (yet) set\&. .PP Data and information about the packet can be fetch by using message parsing functions (See \fBParsing\fP)\&. .SH "Function Documentation" .PP .SS "struct nfq_q_handle* nfq_create_queue (struct nfq_handle * h, uint16_t num, nfq_callback * cb, void * data)" nfq_create_queue - create a new queue handle and return it\&. .PP \fBParameters\fP .RS 4 \fIh\fP Netfilter queue connection handle obtained via call to \fBnfq_open()\fP .br \fInum\fP the number of the queue to bind to .br \fIcb\fP callback function to call for each queued packet .br \fIdata\fP custom data to pass to the callback function .RE .PP \fBReturns\fP .RS 4 a nfq_q_handle pointing to the newly created queue .RE .PP Creates a new queue handle, and returns it\&. The new queue is identified by \fBnum\fP, and the callback specified by \fBcb\fP will be called for each enqueued packet\&. The \fBdata\fP argument will be passed unchanged to the callback\&. If a queue entry with id \fBnum\fP already exists, this function will return failure and the existing entry is unchanged\&. .PP The nfq_callback type is defined in libnetfilter_queue\&.h as: .PP .nf typedef int nfq_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfad, void *data); .fi .PP .PP Parameters: .IP "\(bu" 2 qh The queue handle returned by nfq_create_queue .IP "\(bu" 2 nfmsg message objetc that contains the packet .IP "\(bu" 2 nfad Netlink packet data handle .IP "\(bu" 2 data the value passed to the data parameter of nfq_create_queue .PP .PP The callback should return < 0 to stop processing\&. .PP Definition at line 538 of file libnetfilter_queue\&.c\&. .SS "int nfq_destroy_queue (struct nfq_q_handle * qh)" nfq_destroy_queue - destroy a queue handle .PP \fBParameters\fP .RS 4 \fIqh\fP queue handle that we want to destroy created via nfq_create_queue .RE .PP Removes the binding for the specified queue handle\&. This call also unbind from the nfqueue handler, so you don't have to call nfq_unbind_pf\&. .PP Definition at line 585 of file libnetfilter_queue\&.c\&. .SS "int nfq_fd (struct nfq_handle * h)" nfq_fd - get the file descriptor associated with the nfqueue handler .PP \fBParameters\fP .RS 4 \fIh\fP Netfilter queue connection handle obtained via call to \fBnfq_open()\fP .RE .PP \fBReturns\fP .RS 4 a file descriptor for the netlink connection associated with the given queue connection handle\&. The file descriptor can then be used for receiving the queued packets for processing\&. .RE .PP This function returns a file descriptor that can be used for communication over the netlink connection associated with the given queue connection handle\&. .PP Definition at line 308 of file libnetfilter_queue\&.c\&. .SS "int nfq_handle_packet (struct nfq_handle * h, char * buf, int len)" nfq_handle_packet - handle a packet received from the nfqueue subsystem .PP \fBParameters\fP .RS 4 \fIh\fP Netfilter queue connection handle obtained via call to \fBnfq_open()\fP .br \fIbuf\fP data to pass to the callback .br \fIlen\fP length of packet data in buffer .RE .PP Triggers an associated callback for the given packet received from the queue\&. Packets can be read from the queue using \fBnfq_fd()\fP and recv()\&. See example code for \fBnfq_fd()\fP\&. .PP \fBReturns\fP .RS 4 0 on success, non-zero on failure\&. .RE .PP .PP Definition at line 609 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_mode (struct nfq_q_handle * qh, uint8_t mode, uint32_t range)" nfq_set_mode - set the amount of packet data that nfqueue copies to userspace .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fImode\fP the part of the packet that we are interested in .br \fIrange\fP size of the packet that we want to get .RE .PP Sets the amount of data to be copied to userspace for each packet queued to the given queue\&. .PP .IP "\(bu" 2 NFQNL_COPY_NONE - noop, do not use it .IP "\(bu" 2 NFQNL_COPY_META - copy only packet metadata .IP "\(bu" 2 NFQNL_COPY_PACKET - copy entire packet .PP .PP \fBReturns\fP .RS 4 -1 on error; >=0 otherwise\&. .RE .PP .PP Definition at line 630 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_queue_flags (struct nfq_q_handle * qh, uint32_t mask, uint32_t flags)" nfq_set_queue_flags - set flags (options) for the kernel queue .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fImask\fP specifies which flag bits to modify .br \fIflags\fP bitmask of flags .RE .PP Existing flags, that you may want to combine, are: .PP .IP "\(bu" 2 NFQA_CFG_F_FAIL_OPEN (requires Linux kernel >= 3\&.6): the kernel will accept the packets if the kernel queue gets full\&. If this flag is not set, the default action in this case is to drop packets\&. .IP "\(bu" 2 NFQA_CFG_F_CONNTRACK (requires Linux kernel >= 3\&.6): the kernel will include the Connection Tracking system information\&. .IP "\(bu" 2 NFQA_CFG_F_GSO (requires Linux kernel >= 3\&.10): the kernel will not normalize offload packets, i\&.e\&. your application will need to be able to handle packets larger than the mtu\&. .PP Normalization is expensive, so this flag should always be set\&. Because attributes in netlink messages are limited to 65531 bytes, you also need to check the NFQA_CAP_LEN attribute, it contains the original size of the captured packet on the kernel side\&. If it is set and differs from the payload length, the packet was truncated\&. This also happens when limiting capture size with the NFQNL_COPY_PACKET setting, or when e\&.g\&. a local user sends a very large packet\&. .PP If your application validates checksums (e\&.g\&., tcp checksum), then you must also check if the NFQA_SKB_INFO attribute is present\&. If it is, you need to test the NFQA_SKB_CSUMNOTREADY bit: .PP .nf if (attr[NFQA_SKB_INFO]) { uint32_t info = ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])); if (info & NFQA_SKB_CSUMNOTREADY) validate_checksums = false; } .fi .PP if this bit is set, the layer 3/4 checksums of the packet appear incorrect, but are not (because they will be corrected later by the kernel)\&. Please see example/nf-queue\&.c in the libnetfilter_queue source for more details\&. .IP " \(bu" 4 NFQA_CFG_F_UID_GID: the kernel will dump UID and GID of the socket to which each packet belongs\&. .PP .PP .PP Here's a little code snippet to show how to use this API: .PP .nf uint32_t flags = NFQA_CFG_F_FAIL_OPEN; uint32_t mask = NFQA_CFG_F_FAIL_OPEN; printf("Enabling fail-open on this q\n"); err = nfq_set_queue_flags(qh, mask, flags); printf("Disabling fail-open on this q\n"); flags &= ~NFQA_CFG_F_FAIL_OPEN; err = nfq_set_queue_flags(qh, mask, flags); .fi .PP .IP "\(bu" 2 NFQA_CFG_F_SECCTX: the kernel will dump security context of the socket to which each packet belongs\&. .PP .PP \fBWarning\fP .RS 4 When fragmentation occurs and NFQA_CFG_F_GSO is NOT set then the kernel dumps UID/GID and security context fields only for one fragment\&. To deal with this limitation always set NFQA_CFG_F_GSO\&. .RE .PP \fBReturns\fP .RS 4 -1 on error with errno set appropriately; =0 otherwise\&. .RE .PP .PP Definition at line 719 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_queue_maxlen (struct nfq_q_handle * qh, uint32_t queuelen)" nfq_set_queue_maxlen - Set kernel queue maximum length parameter .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIqueuelen\fP the length of the queue .RE .PP Sets the size of the queue in kernel\&. This fixes the maximum number of packets the kernel will store before internally before dropping upcoming packets\&. .PP \fBReturns\fP .RS 4 -1 on error; >=0 otherwise\&. .RE .PP .PP Definition at line 752 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_verdict (struct nfq_q_handle * qh, uint32_t id, uint32_t verdict, uint32_t data_len, const unsigned char * buf)" nfq_set_verdict - issue a verdict on a packet .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIid\fP ID assigned to packet by netfilter\&. .br \fIverdict\fP verdict to return to netfilter (NF_ACCEPT, NF_DROP) .br \fIdata_len\fP number of bytes of data pointed to by \fBbuf\fP .br \fIbuf\fP the buffer that contains the packet data .RE .PP Can be obtained by: .PP .nf int id; struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(tb); if (ph) id = ntohl(ph->packet_id); .fi .PP .PP Notifies netfilter of the userspace verdict for the given packet\&. Every queued packet \fImust\fP have a verdict specified by userspace, either by calling this function, the \fBnfq_set_verdict2()\fP function, or the _batch versions of these functions\&. .PP \fBReturns\fP .RS 4 -1 on error; >= 0 otherwise\&. .RE .PP .PP Definition at line 856 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_verdict2 (struct nfq_q_handle * qh, uint32_t id, uint32_t verdict, uint32_t mark, uint32_t data_len, const unsigned char * buf)" nfq_set_verdict2 - like nfq_set_verdict, but you can set the mark\&. .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIid\fP ID assigned to packet by netfilter\&. .br \fIverdict\fP verdict to return to netfilter (NF_ACCEPT, NF_DROP) .br \fImark\fP mark to put on packet .br \fIdata_len\fP number of bytes of data pointed to by \fBbuf\fP .br \fIbuf\fP the buffer that contains the packet data .RE .PP .PP Definition at line 874 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_verdict_batch (struct nfq_q_handle * qh, uint32_t id, uint32_t verdict)" nfq_set_verdict_batch - issue verdicts on several packets at once .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIid\fP maximum ID of the packets that the verdict should be applied to\&. .br \fIverdict\fP verdict to return to netfilter (NF_ACCEPT, NF_DROP) .RE .PP Unlike nfq_set_verdict, the verdict is applied to all queued packets whose packet id is smaller or equal to \fBid\fP\&. .PP batch support was added in Linux 3\&.1\&. These functions will fail silently on older kernels\&. .PP Definition at line 895 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_verdict_batch2 (struct nfq_q_handle * qh, uint32_t id, uint32_t verdict, uint32_t mark)" nfq_set_verdict_batch2 - like nfq_set_verdict_batch, but you can set a mark\&. .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIid\fP maximum ID of the packets that the verdict should be applied to\&. .br \fIverdict\fP verdict to return to netfilter (NF_ACCEPT, NF_DROP) .br \fImark\fP mark to put on packet .RE .PP .PP Definition at line 910 of file libnetfilter_queue\&.c\&. .SS "int nfq_set_verdict_mark (struct nfq_q_handle * qh, uint32_t id, uint32_t verdict, uint32_t mark, uint32_t data_len, const unsigned char * buf)" nfq_set_verdict_mark - like nfq_set_verdict, but you can set the mark\&. .PP \fBParameters\fP .RS 4 \fIqh\fP Netfilter queue handle obtained by call to \fBnfq_create_queue()\fP\&. .br \fIid\fP ID assigned to packet by netfilter\&. .br \fIverdict\fP verdict to return to netfilter (NF_ACCEPT, NF_DROP) .br \fImark\fP the mark to put on the packet, in network byte order\&. .br \fIdata_len\fP number of bytes of data pointed to by \fBbuf\fP .br \fIbuf\fP the buffer that contains the packet data .RE .PP \fBReturns\fP .RS 4 -1 on error; >= 0 otherwise\&. .RE .PP This function is deprecated since it is broken, its use is highly discouraged\&. Please, use nfq_set_verdict2 instead\&. .PP Definition at line 932 of file libnetfilter_queue\&.c\&. .SH "Author" .PP Generated automatically by Doxygen for libnetfilter_queue from the source code\&.