1 .\"O .TH LIBIPQ 3 "16 October 2001" "Linux iptables 1.2" "Linux Programmer's Manual"
2 .TH LIBIPQ 3 "16 October 2001" "Linux iptables 1.2" "Linux Programmer's Manual"
4 .\" $Id: libipq.3,v 1.4 2001/10/16 16:58:25 jamesm Exp $
6 .\" Copyright (c) 2000-2001 Netfilter Core Team
8 .\" This program is free software; you can redistribute it and/or modify
9 .\" it under the terms of the GNU General Public License as published by
10 .\" the Free Software Foundation; either version 2 of the License, or
11 .\" (at your option) any later version.
13 .\" This program is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public License
19 .\" along with this program; if not, write to the Free Software
20 .\" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 .\" Japanese Version Copyright (c) 2003 Susumu ISHIZUKA
23 .\" all rights reserved.
24 .\" Translated Tue Jun 6 19:25:23 JST 2003
25 .\" by Susumu ISHIZUKA <szuka@isp.co.jp>
27 .\"WORD: userspace ¥æ¡¼¥¶¶õ´Ö
31 libipq \- iptables userspace packet queuing library.
34 .B #include <linux/netfilter.h>
36 .B #include <libipq.h>
39 .\"O libipq is a development library for iptables userspace packet queuing.
40 libipq ¤Ï iptables ¤ò »È¤Ã¤Æ ¥æ¡¼¥¶¶õ´Ö¤Ç¥Ñ¥±¥Ã¥ÈÁàºî¤ò
41 ¤¹¤ë¤¿¤á¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤¢¤ë¡£
42 .SS Userspace Packet Queuing
43 .\"O Netfilter provides a mechanism for passing packets out of the stack for
44 .\"O queueing to userspace, then receiving these packets back into the kernel
45 .\"O with a verdict specifying what to do with the packets (such as ACCEPT
46 .\"O or DROP). These packets may also be modified in userspace prior to
47 .\"O reinjection back into the kernel.
48 Netfilter ¤Ï¡¢¥æ¡¼¥¶¶õ´Ö¤Ç¥Ñ¥±¥Ã¥ÈÁàºî¤ò¤¹¤ë¤¿¤á¤Îµ¡¹½¤ò
49 Ä󶡤·¤Æ¤¤¤ë¡£ ¤³¤Îµ¡¹½¤Ï¥×¥í¥È¥³¥ë¥¹¥¿¥Ã¥¯¤«¤é¥Ñ¥±¥Ã¥È¤ò
50 ¥æ¡¼¥¶¶õ´Ö¤ËÅϤ·¤Æ¥¥å¡¼¥¤¥ó¥°(queuing) ¤·¡¢¥æ¡¼¥¶¶õ´Ö¤Ç
51 (ACCEPT ¤ä DROP ¤È¤¤¤Ã¤¿) ¥Ñ¥±¥Ã¥È½èÍý¤ÎȽÃǤò¹Ô¤Ã¤¿¸å¤Ë
52 ¥Ñ¥±¥Ã¥È¤ò¥«¡¼¥Í¥ë¤ËÌᤷ¤Æ¤¤¤ë¡£ ¤³¤ì¤é¤Î¥Ñ¥±¥Ã¥È¤Ï¡¢
53 ¥æ¡¼¥¶¶õ´Ö¤ÇÊѹ¹¤ò²Ã¤¨¤é¤ì¤Æ¡¢¥«¡¼¥Í¥ë¤ËÊÖ¤µ¤ì¤ë¤³¤È¤â¤¢¤ë¡£
55 .\"O For each supported protocol, a kernel module called a
57 .\"O may register with Netfilter to perform the mechanics of passing
58 .\"O packets to and from userspace.
59 ¥æ¡¼¥¶¶õ´Ö¤È¤Î¥Ñ¥±¥Ã¥È¤Î¤ä¤ê¼è¤ê¤Î¤¿¤á¤Ë
60 .I ¥¥å¡¼¥Ï¥ó¥É¥é¡¼ (queue handler)
61 ¤È¸Æ¤Ð¤ì¤ë¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤¬
62 Netfilter ¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¡£
63 ¥¥å¡¼¥Ï¥ó¥É¥é¡¼¤Ï¡¢³Æ¥×¥í¥È¥³¥ë¤´¤È¤Ë ÍÑ°Õ¤µ¤ì¤ë¡£
65 .\"O The standard queue handler for IPv4 is ip_queue. It is provided as an
66 .\"O experimental module with 2.4 kernels, and uses a Netlink socket for
67 .\"O kernel/userspace communication.
68 IPv4 ¤Ç¤Î ɸ½à¤Î¥¥å¡¼¥Ï¥ó¥É¥é¡¼¤Ï ip_queue ¤Ç¤¢¤ë¡£
69 2.4¥«¡¼¥Í¥ë¤Ç¤Ï experimental ¥â¥¸¥å¡¼¥ë¤È¤·¤ÆÄ󶡤µ¤ì¡¢
70 Netlink ¥½¥±¥Ã¥È¤ò»È¤Ã¤Æ¡¢¥«¡¼¥Í¥ë¤È¥æ¡¼¥¶¶õ´Ö¤È¤ÎÄÌ¿®¤ò
73 .\"O Once ip_queue is loaded, IP packets may be selected with iptables
74 .\"O and queued for userspace processing via the QUEUE target. For example,
75 .\"O running the following commands:
76 ip_queue ¤¬¡¢¥á¥â¥ê¾å¤Ë¥í¡¼¥É¤µ¤ì¤ë¤È IP ¥Ñ¥±¥Ã¥È¤Ï iptables ¤Ç
77 ¥Õ¥£¥ë¥¿¥ê¥ó¥°¤µ¤ì QUEUE ¥¿¡¼¥²¥Ã¥È¤Ë¤è¤Ã¤Æ ¥æ¡¼¥¶¶õ´Ö¤Î ½èÍý¤Î¤¿¤á¤Ë
78 ¥¥å¡¼¥¤¥ó¥°¤µ¤ì¤ë¡£ Îã¤È¤·¤Æ ¼¡¤Î°ìÏ¢¤Î ¥³¥Þ¥ó¥É¤¬È¯¹Ô¤µ¤ì¤ë¤È¡¢
80 # modprobe iptable_filter
84 # iptables -A OUTPUT -p icmp -j QUEUE
86 .\"O will cause any locally generated ICMP packets (e.g. ping output) to
87 .\"O be sent to the ip_queue module, which will then attempt to deliver the
88 .\"O packets to a userspace application. If no userspace application is waiting,
89 .\"O the packets will be dropped
90 ¥í¡¼¥«¥ë¥Û¥¹¥È¤«¤éÁ÷¿®¤µ¤ì¤¿ICMP¥Ñ¥±¥Ã¥È¡Êping¤ÎÁ÷¿®¤Ê¤É¡Ë¤¬
91 ip_queue¥â¥¸¥å¡¼¥ë¤ËÁ÷¤é¤ì¡¢¤½¤³¤«¤é¥Ñ¥±¥Ã¥È¤ò¥æ¡¼¥¶¶õ´Ö¤Î
92 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ËÅϤ½¤¦¤È¤¹¤ë¡£ ¥Ñ¥±¥Ã¥È¤ò¼õ¤±¼è¤ë
93 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤¬¤Ê¤¤¾ì¹ç¤Ï¡¢¥Ñ¥±¥Ã¥È¤ÏÇË´þ¡Êdrop¡Ë¤µ¤ì¤ë¡£
95 .\"O An application may receive and process these packets via libipq.
96 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢libipq ¤ò»È¤¦¤³¤È¤Ç¥Ñ¥±¥Ã¥È¤ò¼õ¿®¡¦
101 .\"O Libipq provides an API for communicating with ip_queue. The following is
102 .\"O an overview of API usage, refer to individual man pages for more details
103 .\"O on each function.
104 libipq ¤Ï¡¢ip_queue ¤ÈÄÌ¿®¤¹¤ë¤¿¤á¤Î API ¤òÄ󶡤¹¤ë¡£
105 °Ê²¼¤Ï¡¢API ¤Î´Êñ¤ÊÀâÌÀ¤Ç¤¢¤ë¡£ ´Ø¿ô¤Î¾ÜºÙ¤Ï³Æ manpage ¤ò
108 .\"O .B Initialisation
111 .\"O To initialise the library, call
112 .\"O .BR ipq_create_handle (3).
113 .\"O This will attempt to bind to the Netlink socket used by ip_queue and
114 .\"O return an opaque context handle for subsequent library calls.
116 .BR ipq_create_handle (3)
118 ¤³¤Î´Ø¿ô¤Ï ip_queue ¤¬»ÈÍѤ·¤Æ¤¤¤ë Netlink ¥½¥±¥Ã¥È¤ò bind
119 ¤·¡¢¤³¤Î¸å¤Î¥é¥¤¥Ö¥é¥ê´Ø¿ô¤¬»ÈÍѤ¹¤ë¥³¥ó¥Æ¥¥¹¥È¥Ï¥ó¥É¥ë¤ò
122 .\"O .B Setting the Queue Mode
123 .B ¥¥å¡¼¥â¡¼¥É¡Êqueue mode¡Ë¤ÎÀßÄê
125 .\"O .BR ipq_set_mode (3)
126 .\"O allows the application to specify whether packet metadata, or packet
127 .\"O payloads as well as metadata are copied to userspace. It is also used to
128 .\"O initially notify ip_queue that an application is ready to receive queue
131 ¤Ï¡¢¥æ¡¼¥¶¶õ´Ö¤Ë¥Ñ¥±¥Ã¥È¤Î¥á¥¿¥Ç¡¼¥¿¤À¤±¤ò
132 ¥³¥Ô¡¼¤¹¤ë¤Î¤«¡¢¥Ñ¥±¥Ã¥È¤Î¥á¥¿¥Ç¡¼¥¿¤È¥Ú¥¤¥í¡¼¥É¤ÎξÊý¤ò
133 ¥³¥Ô¡¼¤¹¤ë¤Î¤«»ØÄꤹ¤ë¡£¤³¤Î´Ø¿ô¤Ï¤Þ¤¿ ip_queue ¤Ë
134 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤¬¥¥å¡¼¥á¥Ã¥»¡¼¥¸ (queue message)¤ò¼õ¤±
135 ¼è¤ë½àÈ÷¤¬¤Ç¤¤¿¤³¤È¤òÄÌÃΤ¹¤ë¡£
137 .\"O .B Receiving Packets from the Queue
138 .B ¥¥å¡¼¤«¤é¥Ñ¥±¥Ã¥È¤ò¼õ¿®¤¹¤ë
140 .\"O .BR ipq_read (3)
141 .\"O waits for queue messages to arrive from ip_queue and copies
142 .\"O them into a supplied buffer.
143 .\"O Queue messages may be
144 .\"O .I packet messages
146 .\"O .I error messages.
148 ´Ø¿ô¤Ï ip_queue ¤«¤é¤Î¥¥å¡¼¥á¥Ã¥»¡¼¥¸¤ò
149 ÂԤäơ¢¥Ð¥Ã¥Õ¥¡¤Ë¥³¥Ô¡¼¤¹¤ë¡£ ¥¥å¡¼¥á¥Ã¥»¡¼¥¸¤Ï
150 .I ¥Ñ¥±¥Ã¥È ¥á¥Ã¥»¡¼¥¸
155 .\"O The type of packet may be determined with
156 .\"O .BR ipq_message_type (3).
158 .BR ipq_message_type (3)
161 .\"O If it's a packet message, the metadata and optional payload may be retrieved with
162 .\"O .BR ipq_get_packet (3).
163 ¥Ñ¥±¥Ã¥È¥á¥Ã¥»¡¼¥¸¤Î¾ì¹ç¤Ï¡¢¥á¥¿¥Ç¡¼¥¿¤È¥Ú¥¤¥í¡¼¥É¤ò
164 .BR ipq_get_packet (3)
167 .\"O To retrieve the value of an error message, use
168 .\"O .BR ipq_get_msgerr (3).
169 ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤ÎÃͤò¼èÆÀ¤¹¤ë¤Ë¤Ï¡¢
170 .BR ipq_get_msgerr (3)
173 .\"O .B Issuing Verdicts on Packets
174 .B ¥Ñ¥±¥Ã¥È¤Î½èÍýÆâÍƤÎȯ¹Ô
176 .\"O To issue a verdict on a packet, and optionally return a modified version
177 .\"O of the packet to the kernel, call
178 .\"O .BR ipq_set_verdict (3).
179 ¥Ñ¥±¥Ã¥È¤Î½èÍý¤ò·èÄꤷ¡¢É¬Íפʤé¥Ñ¥±¥Ã¥È¤ËÊѹ¹¤ò²Ã¤¨¤Æ
180 ¥«¡¼¥Í¥ë¤ËÊÖ¤¹»þ¤Ë¤Ï¡¢
181 .BR ipq_set_verdict (3)
184 .\"O .B Error Handling
187 .\"O An error string corresponding to the current value of the internal error
190 .\"O may be obtained with
191 .\"O .BR ipq_errstr (3).
192 ¸½ºß¤Î¥¨¥é¡¼¾õÂÖ¤ò³ÊǼ¤·¤Æ¤¤¤ëÊÑ¿ô
199 .\"O For simple applications, calling
200 .\"O .BR ipq_perror (3)
201 .\"O will print the same message as
202 .\"O .BR ipq_errstr (3),
203 .\"O as well as the string corresponding to the global
205 .\"O value (if set) to stderr.
206 ñ½ã¤Ê¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ë»È¤¨¤ë´Ø¿ô¤È¤·¤Æ¡¢
210 ¤ÇÊÖ¤µ¤ì¤ë¤Î¤ÈƱ¤¸Ê¸»úÎó¤òɸ½à½ÐÎÏ (stderr) ¤Ë
214 ¤½¤ì¤ËÂбþ¤¹¤ë¥¨¥é¡¼Ê¸»úÎó¤â½ÐÎϤ¹¤ë¡£
219 .\"O To free up the Netlink socket and destroy resources associated with
220 .\"O the context handle, call
221 .\"O .BR ipq_destroy_handle (3).
222 Netlink ¥½¥±¥Ã¥È¤ò²òÊü¤·¡¢¥³¥ó¥Æ¥¥¹¥È¥Ï¥ó¥É¥ë¤Ë´ØÏ¢ÉÕ¤±
223 ¤é¤ì¤¿¥ê¥½¡¼¥¹¤òºï½ü¤¹¤ë¤Ë¤Ï¡¢
224 .BR ipq_destroy_handle (3)
229 .BR ipq_create_handle (3)
230 .\"O Initialise library, return context handle.
231 ¤Ï¡¢¥é¥¤¥Ö¥é¥ê¤ò½é´ü²½¤·¡¢¥³¥ó¥Æ¥¥¹¥È¥Ï¥ó¥É¥ë¤òÊÖ¤¹¡£
234 .\"O Set the queue mode, to copy either packet metadata, or payloads
235 .\"O as well as metadata to userspace.
236 ¤Ï¡¢¥Ñ¥±¥Ã¥È¤Î¥á¥¿¥Ç¡¼¥¿¤À¤±¤ò¥³¥Ô¡¼¤¹¤ë¤«¡¢¥Ú¥¤¥í¡¼¥É¤â
237 ¥³¥Ô¡¼¤¹¤ë¤«¤ÎÆ°ºî¥â¡¼¥É¤ò¥»¥Ã¥È¤¹¤ë¡£
240 .\"O Wait for a queue message to arrive from ip_queue and read it into
242 ip_queue ¤«¤é¤Î¥á¥Ã¥»¡¼¥¸¤òÂÔ¤Á¡¢¼õ¿®¤¹¤ë¤È¥Ð¥Ã¥Õ¥¡¤Ë
245 .BR ipq_message_type (3)
246 .\"O Determine message type in the buffer.
247 ¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤µ¤ì¤¿¥á¥Ã¥»¡¼¥¸¤Î¥¿¥¤¥×¤òÊÖ¤¹¡£
249 .BR ipq_get_packet (3)
250 .\"O Retrieve a packet message from the buffer.
251 ¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤«¤é¥á¥Ã¥»¡¼¥¸¤òÆɤࡣ
253 .BR ipq_get_msgerr (3)
254 .\"O Retrieve an error message from the buffer.
255 ¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤«¤é¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤ò¼èÆÀ¤¹¤ë¡£
257 .BR ipq_set_verdict (3)
258 .\"O Set a verdict on a packet, optionally replacing its contents.
259 ¤Ï¡¢¥Ñ¥±¥Ã¥È¤ÎȽÃǤò²¼¤¹¡£ÆâÍƤò½ñ¤´¹¤¨¤ë¤³¤È¤â¤Ç¤¤ë¡£
262 .\"O Return an error message corresponding to the internal ipq_errno variable.
263 ¤Ï¡¢ÆâÉôÊÑ¿ô ipq_errno ¤ÎÃͤ˱þ¤¸¤¿¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤ò
267 .\"O Helper function to print error messages to stderr.
268 ¤Ï¡¢¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɸ½à½ÐÎÏ¡Êstderr¡Ë¤Ëɽ¼¨¤¹¤ë
271 .BR ipq_destroy_handle (3)
272 .\"O Destroy context handle and associated resources.
273 ¤Ï¡¢¥³¥ó¥Æ¥¥¹¥È¥Ï¥ó¥É¥ë¤òÇË´þ¤·¡¢¥ê¥½¡¼¥¹¤ò²òÊü¤¹¤ë¡£
276 .\"O The following is an example of a simple application which receives
277 .\"O packets and issues NF_ACCEPT verdicts on each packet.
278 ¼¡¤Î¥³¡¼¥É¤Ï¡¢¥Ñ¥±¥Ã¥È¤ò¼õ¤±¼è¤Ã¤Æ NF_ACCEPT ¤ÎȽÃǤòÊÖ¤¹Ã±½ã¤Ê
279 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÎÎã¤Ç¤¢¤ë¡£
285 #include <linux/netfilter.h>
291 static void die(struct ipq_handle *h)
293 ipq_perror("passer");
294 ipq_destroy_handle(h);
298 int main(int argc, char **argv)
301 unsigned char buf[BUFSIZE];
302 struct ipq_handle *h;
304 h = ipq_create_handle(0);
308 status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
313 status = ipq_read(h, buf, BUFSIZE, 0);
317 switch (ipq_message_type(buf)) {
319 fprintf(stderr, "Received error message %d\\n",
320 ipq_get_msgerr(buf));
324 ipq_packet_msg_t *m = ipq_get_packet(buf);
326 status = ipq_set_verdict(h, m->packet_id,
334 fprintf(stderr, "Unknown message type!\\n");
339 ipq_destroy_handle(h);
345 .\"O Pointers to more libipq application examples may be found in The
347 libipq ¤ò»È¤Ã¤¿¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÎÎã¤Ï
348 Netfilter FAQ ¤Ë¤â¤¢¤ë¡£
351 .\"O For information about monitoring and tuning ip_queue, refer to the
352 .\"O Linux 2.4 Packet Filtering HOWTO.
353 ip_queue ¤Î´Æ»ë¤È¥Á¥å¡¼¥Ë¥ó¥°¤Ë´Ø¤·¤Æ¤Ï Linux 2.4 Packet
354 Filtering HOWTO ¤ò»²¾È¤¹¤ë¤³¤È¡£
356 .\"O If an application modifies a packet, it needs to also update any
357 .\"O checksums for the packet. Typically, the kernel will silently discard
358 .\"O modified packets with invalid checksums.
359 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤¬¥Ñ¥±¥Ã¥È¤ËÊѹ¹¤ò²Ã¤¨¤¿»þ¤Ë¤Ï¡¢´ØÏ¢¤¹¤ë
360 ¥Á¥§¥Ã¥¯¥µ¥à¤âÊѹ¹¤¹¤ëɬÍפ¬¤¢¤ë¡£ Êѹ¹¤µ¤ì¤¿¥Ñ¥±¥Ã¥È¤Î
361 ¥Á¥§¥Ã¥¯¥µ¥à¤¬°Û¾ï¤Ê¤È¤¤Ë¤Ï¥«¡¼¥Í¥ë¤ÏÌۤäÆÇË´þ (silently discard) ¤¹¤ë¡£
364 .\"O Processes require CAP_NET_ADMIN capabilty to access the kernel ip_queue
365 .\"O module. Such processes can potentially access and modify any IP packets
366 .\"O received, generated or forwarded by the kernel.
367 ip_queue ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Ë¥¢¥¯¥»¥¹¤¹¤ë¥×¥í¥»¥¹¤Ï
368 CAP_NET_ADMIN ¸¢¸Â¤¬É¬ÍפǤ¢¤ë¡£ ¤½¤Î¤è¤¦¤Ê¥×¥í¥»¥¹¤Ï¡¢
369 ÀøºßŪ¤Ë¥«¡¼¥Í¥ë¤¬¼õ¿® (Á÷¿®¡¢Å¾Á÷) ¤¹¤ëÁ´¤Æ¤Î IP ¥Ñ¥±¥Ã¥È¤ò
370 ¼èÆÀ¤·Êѹ¹¤¹¤ë²ÄǽÀ¤¬¤¢¤ë¡£
377 ¤ò¥Ï¥ó¥É¥ë¤´¤È¤ËÍÑ°Õ¤¹¤ë¡£
384 James Morris <jmorris@intercode.com.au>
387 Copyright (c) 2000-2001 Netfilter Core Team.
389 Distributed under the GNU General Public License.
391 .\"O Joost Remijn implemented the
393 .\"O timeout feature, which appeared in the 1.2.4 release of iptables.
396 ¤Î¥¿¥¤¥à¥¢¥¦¥È¤ò¼ÂÁõ¤·¤¿¡£ ¤³¤Îµ¡Ç½¤Ï iptables ¤Î 1.2.4 ¤«¤é»ÈÍѤǤ¤ë¡£
400 .BR ipq_create_handle (3),
401 .BR ipq_destroy_handle (3),
403 .BR ipq_get_msgerr (3),
404 .BR ipq_get_packet (3),
405 .BR ipq_message_type (3),
408 .BR ipq_set_mode (3),
409 .BR ipq_set_verdict (3).
411 .\"O The Netfilter home page at http://netfilter.samba.org/
412 .\"O which has links to The Networking Concepts HOWTO, The Linux 2.4 Packet
413 .\"O Filtering HOWTO, The Linux 2.4 NAT HOWTO, The Netfilter Hacking HOWTO,
414 .\"O The Netfilter FAQ and many other useful resources.
415 Netfilter ¤Î¥Û¡¼¥à¥Ú¡¼¥¸¤Ï http://netfilter.samba.org/ ¤Ë¤¢¤ë¡£
416 The Networking Concepts HOWTO, The Linux 2.4 Packet
417 Filtering HOWTO, The Linux 2.4 NAT HOWTO, The Netfilter Hacking HOWTO,
418 The Netfilter FAQ ¤Ê¤É¤ÎÍ±×¤Ê ¾ðÊ󤬤¢¤ë¡£