Blame libipq/libipq.3

Packit 7b22a4
.TH LIBIPQ 3 "16 October 2001" "Linux iptables 1.2" "Linux Programmer's Manual" 
Packit 7b22a4
.\"
Packit 7b22a4
.\"     Copyright (c) 2000-2001 Netfilter Core Team
Packit 7b22a4
.\"
Packit 7b22a4
.\"     This program is free software; you can redistribute it and/or modify
Packit 7b22a4
.\"     it under the terms of the GNU General Public License as published by
Packit 7b22a4
.\"     the Free Software Foundation; either version 2 of the License, or
Packit 7b22a4
.\"     (at your option) any later version.
Packit 7b22a4
.\"
Packit 7b22a4
.\"     This program is distributed in the hope that it will be useful,
Packit 7b22a4
.\"     but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7b22a4
.\"     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7b22a4
.\"     GNU General Public License for more details.
Packit 7b22a4
.\"
Packit 7b22a4
.\"     You should have received a copy of the GNU General Public License
Packit 7b22a4
.\"     along with this program; if not, write to the Free Software
Packit 7b22a4
.\"     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Packit 7b22a4
.\"
Packit 7b22a4
.\"
Packit 7b22a4
.SH NAME
Packit 7b22a4
libipq \(em iptables userspace packet queuing library.
Packit 7b22a4
.SH SYNOPSIS
Packit 7b22a4
.B #include <linux/netfilter.h>
Packit 7b22a4
.br
Packit 7b22a4
.B #include <libipq.h>
Packit 7b22a4
.SH DESCRIPTION
Packit 7b22a4
libipq is a development library for iptables userspace packet queuing.
Packit 7b22a4
.SS Userspace Packet Queuing
Packit 7b22a4
Netfilter provides a mechanism for passing packets out of the stack for
Packit 7b22a4
queueing to userspace, then receiving these packets back into the kernel
Packit 7b22a4
with a verdict specifying what to do with the packets (such as ACCEPT
Packit 7b22a4
or DROP).  These packets may also be modified in userspace prior to
Packit 7b22a4
reinjection back into the kernel.
Packit 7b22a4
.PP
Packit 7b22a4
For each supported protocol, a kernel module called a
Packit 7b22a4
.I queue handler
Packit 7b22a4
may register with Netfilter to perform the mechanics of passing
Packit 7b22a4
packets to and from userspace.
Packit 7b22a4
.PP
Packit 7b22a4
The standard queue handler for IPv4 is ip_queue.  It is provided as an
Packit 7b22a4
experimental module with 2.4 kernels, and uses a Netlink socket for
Packit 7b22a4
kernel/userspace communication.
Packit 7b22a4
.PP
Packit 7b22a4
Once ip_queue is loaded, IP packets may be selected with iptables
Packit 7b22a4
and queued for userspace processing via the QUEUE target.  For example,
Packit 7b22a4
running the following commands:
Packit 7b22a4
.PP
Packit 7b22a4
	# modprobe iptable_filter
Packit 7b22a4
.br	
Packit 7b22a4
	# modprobe ip_queue
Packit 7b22a4
.br	
Packit 7b22a4
	# iptables \-A OUTPUT \-p icmp \-j QUEUE
Packit 7b22a4
.PP
Packit 7b22a4
will cause any locally generated ICMP packets (e.g. ping output) to
Packit 7b22a4
be sent to the ip_queue module, which will then attempt to deliver the
Packit 7b22a4
packets to a userspace application.  If no userspace application is waiting,
Packit 7b22a4
the packets will be dropped
Packit 7b22a4
.PP
Packit 7b22a4
An application may receive and process these packets via libipq.
Packit 7b22a4
.PP
Packit 7b22a4
.PP
Packit 7b22a4
.SS Libipq Overview
Packit 7b22a4
Libipq provides an API for communicating with ip_queue.  The following is
Packit 7b22a4
an overview of API usage, refer to individual man pages for more details
Packit 7b22a4
on each function.
Packit 7b22a4
.PP
Packit 7b22a4
.B Initialisation
Packit 7b22a4
.br
Packit 7b22a4
To initialise the library, call
Packit 7b22a4
.BR ipq_create_handle (3).
Packit 7b22a4
This will attempt to bind to the Netlink socket used by ip_queue and
Packit 7b22a4
return an opaque context handle for subsequent library calls.
Packit 7b22a4
.PP
Packit 7b22a4
.B Setting the Queue Mode
Packit 7b22a4
.br
Packit 7b22a4
.BR ipq_set_mode (3)
Packit 7b22a4
allows the application to specify whether packet metadata, or packet
Packit 7b22a4
payloads as well as metadata are copied to userspace.  It is also used to
Packit 7b22a4
initially notify ip_queue that an application is ready to receive queue
Packit 7b22a4
messages.
Packit 7b22a4
.PP
Packit 7b22a4
.B Receiving Packets from the Queue
Packit 7b22a4
.br
Packit 7b22a4
.BR ipq_read (3)
Packit 7b22a4
waits for queue messages to arrive from ip_queue and copies
Packit 7b22a4
them into a supplied buffer.
Packit 7b22a4
Queue messages may be
Packit 7b22a4
.I packet messages
Packit 7b22a4
or
Packit 7b22a4
.I error messages.
Packit 7b22a4
.PP
Packit 7b22a4
The type of packet may be determined with
Packit 7b22a4
.BR ipq_message_type (3).
Packit 7b22a4
.PP
Packit 7b22a4
If it's a packet message, the metadata and optional payload may be retrieved with
Packit 7b22a4
.BR ipq_get_packet (3).
Packit 7b22a4
.PP
Packit 7b22a4
To retrieve the value of an error message, use
Packit 7b22a4
.BR ipq_get_msgerr (3).
Packit 7b22a4
.PP
Packit 7b22a4
.B Issuing Verdicts on Packets
Packit 7b22a4
.br
Packit 7b22a4
To issue a verdict on a packet, and optionally return a modified version
Packit 7b22a4
of the packet to the kernel, call
Packit 7b22a4
.BR ipq_set_verdict (3).
Packit 7b22a4
.PP
Packit 7b22a4
.B Error Handling
Packit 7b22a4
.br
Packit 7b22a4
An error string corresponding to the current value of the internal error
Packit 7b22a4
variable
Packit 7b22a4
.B ipq_errno
Packit 7b22a4
may be obtained with
Packit 7b22a4
.BR ipq_errstr (3).
Packit 7b22a4
.PP
Packit 7b22a4
For simple applications, calling
Packit 7b22a4
.BR ipq_perror (3)
Packit 7b22a4
will print the same message as
Packit 7b22a4
.BR ipq_errstr (3),
Packit 7b22a4
as well as the string corresponding to the global
Packit 7b22a4
.B errno
Packit 7b22a4
value (if set) to stderr.
Packit 7b22a4
.PP
Packit 7b22a4
.B Cleaning Up
Packit 7b22a4
.br
Packit 7b22a4
To free up the Netlink socket and destroy resources associated with
Packit 7b22a4
the context handle, call
Packit 7b22a4
.BR ipq_destroy_handle (3).
Packit 7b22a4
.SH SUMMARY
Packit 7b22a4
.TP 4
Packit 7b22a4
.BR ipq_create_handle (3)
Packit 7b22a4
Initialise library, return context handle.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_set_mode (3)
Packit 7b22a4
Set the queue mode, to copy either packet metadata, or payloads
Packit 7b22a4
as well as metadata to userspace.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_read (3)
Packit 7b22a4
Wait for a queue message to arrive from ip_queue and read it into
Packit 7b22a4
a buffer.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_message_type (3)
Packit 7b22a4
Determine message type in the buffer.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_get_packet (3)
Packit 7b22a4
Retrieve a packet message from the buffer.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_get_msgerr (3)
Packit 7b22a4
Retrieve an error message from the buffer.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_set_verdict (3)
Packit 7b22a4
Set a verdict on a packet, optionally replacing its contents.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_errstr (3)
Packit 7b22a4
Return an error message corresponding to the internal ipq_errno variable.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_perror (3)
Packit 7b22a4
Helper function to print error messages to stderr.
Packit 7b22a4
.TP
Packit 7b22a4
.BR ipq_destroy_handle (3)
Packit 7b22a4
Destroy context handle and associated resources.
Packit 7b22a4
.SH EXAMPLE
Packit 7b22a4
The following is an example of a simple application which receives
Packit 7b22a4
packets and issues NF_ACCEPT verdicts on each packet.
Packit 7b22a4
.RS
Packit 7b22a4
.nf
Packit 7b22a4
/*
Packit 7b22a4
 * This code is GPL.
Packit 7b22a4
 */
Packit 7b22a4
#include <linux/netfilter.h>
Packit 7b22a4
#include <libipq.h>
Packit 7b22a4
#include <stdio.h>
Packit 7b22a4
Packit 7b22a4
#define BUFSIZE 2048 
Packit 7b22a4
Packit 7b22a4
static void die(struct ipq_handle *h)
Packit 7b22a4
{
Packit 7b22a4
	ipq_perror("passer");
Packit 7b22a4
	ipq_destroy_handle(h);
Packit 7b22a4
	exit(1);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
int main(int argc, char **argv)
Packit 7b22a4
{
Packit 7b22a4
	int status;
Packit 7b22a4
	unsigned char buf[BUFSIZE];
Packit 7b22a4
	struct ipq_handle *h;
Packit 7b22a4
	
Packit 7b22a4
	h = ipq_create_handle(0, NFPROTO_IPV4);
Packit 7b22a4
	if (!h)
Packit 7b22a4
		die(h);
Packit 7b22a4
		
Packit 7b22a4
	status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
Packit 7b22a4
	if (status < 0)
Packit 7b22a4
		die(h);
Packit 7b22a4
		
Packit 7b22a4
	do{
Packit 7b22a4
		status = ipq_read(h, buf, BUFSIZE, 0);
Packit 7b22a4
		if (status < 0)
Packit 7b22a4
			die(h);
Packit 7b22a4
			
Packit 7b22a4
		switch (ipq_message_type(buf)) {
Packit 7b22a4
			case NLMSG_ERROR:
Packit 7b22a4
				fprintf(stderr, "Received error message %d\\n",
Packit 7b22a4
				        ipq_get_msgerr(buf));
Packit 7b22a4
				break;
Packit 7b22a4
				
Packit 7b22a4
			case IPQM_PACKET: {
Packit 7b22a4
				ipq_packet_msg_t *m = ipq_get_packet(buf);
Packit 7b22a4
				
Packit 7b22a4
				status = ipq_set_verdict(h, m->packet_id,
Packit 7b22a4
				                         NF_ACCEPT, 0, NULL);
Packit 7b22a4
				if (status < 0)
Packit 7b22a4
					die(h);
Packit 7b22a4
				break;
Packit 7b22a4
			}
Packit 7b22a4
			
Packit 7b22a4
			default:
Packit 7b22a4
				fprintf(stderr, "Unknown message type!\\n");
Packit 7b22a4
				break;
Packit 7b22a4
		}
Packit 7b22a4
	} while (1);
Packit 7b22a4
	
Packit 7b22a4
	ipq_destroy_handle(h);
Packit 7b22a4
	return 0;
Packit 7b22a4
}
Packit 7b22a4
.RE
Packit 7b22a4
.fi
Packit 7b22a4
.PP
Packit 7b22a4
Pointers to more libipq application examples may be found in The
Packit 7b22a4
Netfilter FAQ.
Packit 7b22a4
.SH DIAGNOSTICS
Packit 7b22a4
For information about monitoring and tuning ip_queue, refer to the
Packit 7b22a4
Linux 2.4 Packet Filtering HOWTO.
Packit 7b22a4
.PP
Packit 7b22a4
If an application modifies a packet, it needs to also update any
Packit 7b22a4
checksums for the packet.  Typically, the kernel will silently discard
Packit 7b22a4
modified packets with invalid checksums. 
Packit 7b22a4
.SH SECURITY
Packit 7b22a4
Processes require CAP_NET_ADMIN capabilty to access the kernel ip_queue
Packit 7b22a4
module.  Such processes can potentially access and modify any IP packets
Packit 7b22a4
received, generated or forwarded by the kernel.
Packit 7b22a4
.SH TODO
Packit 7b22a4
Per-handle
Packit 7b22a4
.B ipq_errno
Packit 7b22a4
values.
Packit 7b22a4
.SH BUGS
Packit 7b22a4
Probably.
Packit 7b22a4
.SH AUTHOR
Packit 7b22a4
James Morris <jmorris@intercode.com.au>
Packit 7b22a4
.SH COPYRIGHT
Packit 7b22a4
Copyright (c) 2000-2001 Netfilter Core Team.
Packit 7b22a4
.PP
Packit 7b22a4
Distributed under the GNU General Public License.
Packit 7b22a4
.SH CREDITS
Packit 7b22a4
Joost Remijn implemented the
Packit 7b22a4
.B ipq_read
Packit 7b22a4
timeout feature, which appeared in the 1.2.4 release of iptables.
Packit 7b22a4
.PP
Packit 7b22a4
Fernando Anton added support for IPv6.
Packit 7b22a4
.SH SEE ALSO
Packit 7b22a4
.BR iptables (8),
Packit 7b22a4
.BR ipq_create_handle (3),
Packit 7b22a4
.BR ipq_destroy_handle (3),
Packit 7b22a4
.BR ipq_errstr (3),
Packit 7b22a4
.BR ipq_get_msgerr (3),
Packit 7b22a4
.BR ipq_get_packet (3),
Packit 7b22a4
.BR ipq_message_type (3),
Packit 7b22a4
.BR ipq_perror (3),
Packit 7b22a4
.BR ipq_read (3),
Packit 7b22a4
.BR ipq_set_mode (3),
Packit 7b22a4
.BR ipq_set_verdict (3).
Packit 7b22a4
.PP
Packit 7b22a4
The Netfilter home page at http://netfilter.samba.org/
Packit 7b22a4
which has links to The Networking Concepts HOWTO, The Linux 2.4 Packet
Packit 7b22a4
Filtering HOWTO, The Linux 2.4 NAT HOWTO, The Netfilter Hacking HOWTO,
Packit 7b22a4
The Netfilter FAQ and many other useful resources.
Packit 7b22a4