blob: 611fcdf5adaf063a97e4c888ef95ab39ad20da7b [file] [log] [blame]
James Morris460c7472001-10-16 14:41:02 +00001.TH LIBIPQ 3 "16 October 2001" "Linux iptables 1.2" "Linux Programmer's Manual"
James Morris949810c2000-11-20 14:13:31 +00002.\"
James Morris460c7472001-10-16 14:41:02 +00003.\" Copyright (c) 2000-2001 Netfilter Core Team
James Morris949810c2000-11-20 14:13:31 +00004.\"
5.\" This program is free software; you can redistribute it and/or modify
6.\" it under the terms of the GNU General Public License as published by
7.\" the Free Software Foundation; either version 2 of the License, or
8.\" (at your option) any later version.
9.\"
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.
14.\"
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18.\"
19.\"
20.SH NAME
Jan Engelhardt352ccfb2009-08-20 17:15:22 +020021libipq \(em iptables userspace packet queuing library.
James Morris949810c2000-11-20 14:13:31 +000022.SH SYNOPSIS
23.B #include <linux/netfilter.h>
24.br
25.B #include <libipq.h>
26.SH DESCRIPTION
27libipq is a development library for iptables userspace packet queuing.
28.SS Userspace Packet Queuing
29Netfilter provides a mechanism for passing packets out of the stack for
30queueing to userspace, then receiving these packets back into the kernel
31with a verdict specifying what to do with the packets (such as ACCEPT
32or DROP). These packets may also be modified in userspace prior to
33reinjection back into the kernel.
34.PP
35For each supported protocol, a kernel module called a
36.I queue handler
37may register with Netfilter to perform the mechanics of passing
38packets to and from userspace.
39.PP
40The standard queue handler for IPv4 is ip_queue. It is provided as an
41experimental module with 2.4 kernels, and uses a Netlink socket for
42kernel/userspace communication.
43.PP
44Once ip_queue is loaded, IP packets may be selected with iptables
45and queued for userspace processing via the QUEUE target. For example,
46running the following commands:
47.PP
48 # modprobe iptable_filter
49.br
50 # modprobe ip_queue
51.br
Jan Engelhardt352ccfb2009-08-20 17:15:22 +020052 # iptables \-A OUTPUT \-p icmp \-j QUEUE
James Morris949810c2000-11-20 14:13:31 +000053.PP
54will cause any locally generated ICMP packets (e.g. ping output) to
55be sent to the ip_queue module, which will then attempt to deliver the
56packets to a userspace application. If no userspace application is waiting,
57the packets will be dropped
58.PP
59An application may receive and process these packets via libipq.
60.PP
61.PP
62.SS Libipq Overview
63Libipq provides an API for communicating with ip_queue. The following is
64an overview of API usage, refer to individual man pages for more details
65on each function.
66.PP
67.B Initialisation
68.br
69To initialise the library, call
70.BR ipq_create_handle (3).
71This will attempt to bind to the Netlink socket used by ip_queue and
72return an opaque context handle for subsequent library calls.
73.PP
74.B Setting the Queue Mode
75.br
76.BR ipq_set_mode (3)
77allows the application to specify whether packet metadata, or packet
78payloads as well as metadata are copied to userspace. It is also used to
79initially notify ip_queue that an application is ready to receive queue
80messages.
81.PP
82.B Receiving Packets from the Queue
83.br
84.BR ipq_read (3)
85waits for queue messages to arrive from ip_queue and copies
86them into a supplied buffer.
87Queue messages may be
88.I packet messages
89or
90.I error messages.
91.PP
92The type of packet may be determined with
93.BR ipq_message_type (3).
94.PP
95If it's a packet message, the metadata and optional payload may be retrieved with
96.BR ipq_get_packet (3).
97.PP
98To retrieve the value of an error message, use
99.BR ipq_get_msgerr (3).
100.PP
101.B Issuing Verdicts on Packets
102.br
103To issue a verdict on a packet, and optionally return a modified version
104of the packet to the kernel, call
105.BR ipq_set_verdict (3).
106.PP
107.B Error Handling
108.br
109An error string corresponding to the current value of the internal error
110variable
111.B ipq_errno
112may be obtained with
113.BR ipq_errstr (3).
114.PP
115For simple applications, calling
116.BR ipq_perror (3)
117will print the same message as
118.BR ipq_errstr (3),
119as well as the string corresponding to the global
120.B errno
121value (if set) to stderr.
122.PP
123.B Cleaning Up
124.br
125To free up the Netlink socket and destroy resources associated with
126the context handle, call
127.BR ipq_destroy_handle (3).
128.SH SUMMARY
129.TP 4
130.BR ipq_create_handle (3)
131Initialise library, return context handle.
132.TP
133.BR ipq_set_mode (3)
134Set the queue mode, to copy either packet metadata, or payloads
135as well as metadata to userspace.
136.TP
137.BR ipq_read (3)
138Wait for a queue message to arrive from ip_queue and read it into
139a buffer.
140.TP
141.BR ipq_message_type (3)
142Determine message type in the buffer.
143.TP
144.BR ipq_get_packet (3)
145Retrieve a packet message from the buffer.
146.TP
147.BR ipq_get_msgerr (3)
148Retrieve an error message from the buffer.
149.TP
150.BR ipq_set_verdict (3)
151Set a verdict on a packet, optionally replacing its contents.
152.TP
153.BR ipq_errstr (3)
154Return an error message corresponding to the internal ipq_errno variable.
155.TP
156.BR ipq_perror (3)
157Helper function to print error messages to stderr.
158.TP
159.BR ipq_destroy_handle (3)
160Destroy context handle and associated resources.
161.SH EXAMPLE
162The following is an example of a simple application which receives
163packets and issues NF_ACCEPT verdicts on each packet.
164.RS
165.nf
166/*
167 * This code is GPL.
168 */
169#include <linux/netfilter.h>
170#include <libipq.h>
171#include <stdio.h>
172
173#define BUFSIZE 2048
174
175static void die(struct ipq_handle *h)
176{
177 ipq_perror("passer");
178 ipq_destroy_handle(h);
179 exit(1);
180}
181
182int main(int argc, char **argv)
183{
184 int status;
185 unsigned char buf[BUFSIZE];
186 struct ipq_handle *h;
187
Jan Engelhardt03d99482008-11-18 12:27:54 +0100188 h = ipq_create_handle(0, NFPROTO_IPV4);
James Morris949810c2000-11-20 14:13:31 +0000189 if (!h)
190 die(h);
191
192 status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
193 if (status < 0)
194 die(h);
195
196 do{
197 status = ipq_read(h, buf, BUFSIZE, 0);
198 if (status < 0)
199 die(h);
200
201 switch (ipq_message_type(buf)) {
202 case NLMSG_ERROR:
203 fprintf(stderr, "Received error message %d\\n",
204 ipq_get_msgerr(buf));
205 break;
206
207 case IPQM_PACKET: {
208 ipq_packet_msg_t *m = ipq_get_packet(buf);
209
210 status = ipq_set_verdict(h, m->packet_id,
211 NF_ACCEPT, 0, NULL);
212 if (status < 0)
213 die(h);
214 break;
215 }
216
217 default:
218 fprintf(stderr, "Unknown message type!\\n");
219 break;
220 }
221 } while (1);
222
223 ipq_destroy_handle(h);
224 return 0;
225}
226.RE
227.fi
228.PP
229Pointers to more libipq application examples may be found in The
230Netfilter FAQ.
231.SH DIAGNOSTICS
232For information about monitoring and tuning ip_queue, refer to the
233Linux 2.4 Packet Filtering HOWTO.
234.PP
235If an application modifies a packet, it needs to also update any
236checksums for the packet. Typically, the kernel will silently discard
237modified packets with invalid checksums.
238.SH SECURITY
239Processes require CAP_NET_ADMIN capabilty to access the kernel ip_queue
240module. Such processes can potentially access and modify any IP packets
241received, generated or forwarded by the kernel.
242.SH TODO
James Morris949810c2000-11-20 14:13:31 +0000243Per-handle
244.B ipq_errno
245values.
246.SH BUGS
247Probably.
248.SH AUTHOR
249James Morris <jmorris@intercode.com.au>
250.SH COPYRIGHT
James Morris460c7472001-10-16 14:41:02 +0000251Copyright (c) 2000-2001 Netfilter Core Team.
James Morris949810c2000-11-20 14:13:31 +0000252.PP
253Distributed under the GNU General Public License.
James Morris460c7472001-10-16 14:41:02 +0000254.SH CREDITS
James Morris67b63ae2001-10-16 16:58:25 +0000255Joost Remijn implemented the
James Morris460c7472001-10-16 14:41:02 +0000256.B ipq_read
257timeout feature, which appeared in the 1.2.4 release of iptables.
James Morrisffe96c52001-11-24 15:09:19 +0000258.PP
259Fernando Anton added support for IPv6.
James Morris949810c2000-11-20 14:13:31 +0000260.SH SEE ALSO
261.BR iptables (8),
262.BR ipq_create_handle (3),
263.BR ipq_destroy_handle (3),
264.BR ipq_errstr (3),
265.BR ipq_get_msgerr (3),
266.BR ipq_get_packet (3),
267.BR ipq_message_type (3),
268.BR ipq_perror (3),
269.BR ipq_read (3),
270.BR ipq_set_mode (3),
271.BR ipq_set_verdict (3).
272.PP
James Morris460c7472001-10-16 14:41:02 +0000273The Netfilter home page at http://netfilter.samba.org/
James Morris949810c2000-11-20 14:13:31 +0000274which has links to The Networking Concepts HOWTO, The Linux 2.4 Packet
275Filtering HOWTO, The Linux 2.4 NAT HOWTO, The Netfilter Hacking HOWTO,
276The Netfilter FAQ and many other useful resources.
277