From: "Maarten Wijnants" Subject: QUEUEing packets to userspace problem Date: Thu, 29 Apr 2004 15:59:49 +0200 Sender: netfilter-devel-admin@lists.netfilter.org Message-ID: <005001c42df2$3c94d960$061012ac@in.edm.luc.ac.be> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_004D_01C42E03.0011C280" Return-path: To: Errors-To: netfilter-devel-admin@lists.netfilter.org List-Help: List-Post: List-Subscribe: , List-Unsubscribe: , List-Archive: List-Id: netfilter-devel.vger.kernel.org This is a multi-part message in MIME format. ------=_NextPart_000_004D_01C42E03.0011C280 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hello all, I'm very new at Linux and especially Linux programming. I apologize if = this turns out to be a stupid question :-) When I try the libipq example in the man page, I do not receive any = packets in my userspace application. I have loaded the iptable_filter = and ip_queue modules, and I added the following rule to iptables: iptables -A OUTPUT -p icmp -j QUEUE Then i hit the "Execute Program" button of my KDeveloper (see below for = the full code). This way, locally created ping packets should be = delivered to this application right? But immediately after running the = code, i get the following error message:=20 Received error message 1 Unknown error ERROR and packets do not start comming in in the application when I ping = another PC. Here's my code: #ifdef HAVE_CONFIG_H #include #endif #include #include #include // Include the netfilter and libipq C libraries #include #include #define BUFSIZE 2048 static void die(struct ipq_handle *h) { ipq_perror(NULL); ipq_destroy_handle(h); printf("Exiting ...\n"); exit(1); } int main(int argc, char *argv[]) { printf("Starting up ...\n"); =20 int status; unsigned char buf[BUFSIZE]; struct ipq_handle *h; =20 // Initialize the libipq library h =3D ipq_create_handle(0, PF_INET); if (!h) die(h); else printf("Libipq handle created successfully!\n"); status =3D ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE); if (status < 0) die(h); else printf("Lipipq mode set to IPQ_COPY_PACKET\n"); =20 // Continuously receive packets from kernel space, process them and = reinject them into the kernel while(1) { =20 // Grab the packet status =3D ipq_read(h, buf, BUFSIZE, 0); if (status < 0) die(h); else printf("Libipq packet read successful\n"); =09 switch( ipq_message_type(buf) ) { case NLMSG_ERROR: { fprintf(stderr, "Received error message %d\n", = ipq_get_msgerr(buf)); printf("%s\n", ipq_errstr()); ipq_perror(NULL); break; } case IPQM_PACKET: { // In case of a packet and not an error -> get the packet (header = + payload) printf("Received a packet from kernel space"); ipq_packet_msg_t *m =3D ipq_get_packet(buf); =09 // Print the payload of the packet if ( m->data_len > 0 ) { printf("Printing content of packet:\n"); /* To be = included */ } =09 // Reinject the packet into kernel space status =3D ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL); if (status < 0) die(h); =09 break; } default: { fprintf(stderr, "Unknown message type!\n"); break; } } } =20 printf("Closing down ...\n"); =20 // Cleanup the libipq library ipq_destroy_handle(h); return EXIT_SUCCESS; } Does anyone have any ideas what i am doing wrong? I am using kernel = version 2.4.20, and I have also installed the latest version of = iptables, including the "devel" (header, libraries, ...) part. Thanks in = advance. Maarten ------=_NextPart_000_004D_01C42E03.0011C280 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hello all,
 
I'm very new at Linux and especially = Linux=20 programming. I apologize if this turns out to be a stupid question=20 :-)
 
When I try the libipq example in the = man page, I do=20 not receive any packets in my userspace application. I have loaded the=20 iptable_filter and ip_queue modules, and I added the following rule to=20 iptables:
 
iptables -A OUTPUT -p icmp -j = QUEUE
 
Then i hit the "Execute Program" button = of my=20 KDeveloper (see below for the full code). This way, locally created ping = packets=20 should be delivered to this application right? But immediately after = running the=20 code, i get the following error message:
 
Received error message 1
Unknown error
ERROR
 
and packets do not start comming in in = the=20 application when I ping another PC. Here's my code:
 
 
#ifdef HAVE_CONFIG_H
#include=20 <config.h>
#endif

#include <stdio.h>
#include=20 <stdlib.h>
#include <string.h>

// Include the = netfilter=20 and libipq C libraries
#include <linux/netfilter.h>
#include = <libipq.h>

#define BUFSIZE 2048

static void = die(struct=20 ipq_handle *h)
{
  ipq_perror(NULL);
 =20 ipq_destroy_handle(h);
  printf("Exiting=20 ...\n");
  exit(1);
}

int main(int argc, char=20 *argv[])
{
  printf("Starting up ...\n");
  =
  int=20 status;
  unsigned char buf[BUFSIZE];
  struct = ipq_handle=20 *h;
 
  // Initialize the libipq library
  h = =3D=20 ipq_create_handle(0, PF_INET);
  if (!h)
  = die(h);
 =20 else
  printf("Libipq handle created = successfully!\n");

 =20 status =3D ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
  if = (status <=20 0)
  die(h);
  else
  printf("Lipipq mode set = to=20 IPQ_COPY_PACKET\n");
 
  // Continuously receive = packets from=20 kernel space, process them and reinject them into the kernel
  = while(1)=20 {   
   // Grab the packet
  status = =3D=20 ipq_read(h, buf, BUFSIZE, 0);
    if (status <=20 0)
          =20       die(h);
   =20 else
    printf("Libipq packet read=20 successful\n");

    switch( = ipq_message_type(buf) )=20 {
    case NLMSG_ERROR: {
   =20 fprintf(stderr, "Received error message %d\n",=20 ipq_get_msgerr(buf));
    printf("%s\n",=20 ipq_errstr());
    = ipq_perror(NULL);
   =20 break;
    }
    case = IPQM_PACKET:=20 {
    // In case of a packet and not an error -> = get the=20 packet (header + payload)
    printf("Received a = packet=20 from kernel space");
    ipq_packet_msg_t *m =3D=20 ipq_get_packet(buf);

    // Print the payload = of the=20 packet
    if ( m->data_len > 0 )=20 {
    printf("Printing content of=20 packet:\n");
       =20             =    =20             =    =20             =    =20         /* To be included=20 */
           &= nbsp;           &n= bsp;           }
=
   =20 // Reinject the packet into kernel space
    = status =3D=20 ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, = NULL);
   =20 if (status < 0)
   =20 die(h);

    break;
    = }
    default: {
    = fprintf(stderr,=20 "Unknown message type!\n");
   =20 break;
    }
    }
 =20 }
 
  printf("Closing down ...\n");
  =
  //=20 Cleanup the libipq library
  ipq_destroy_handle(h);
  = return=20 EXIT_SUCCESS;
}
 
 
Does anyone have any ideas what i am = doing wrong? I=20 am using kernel version 2.4.20, and I have also installed the latest = version of=20 iptables, including the "devel" (header, libraries, ...) part. Thanks in = advance.
 
Maarten
------=_NextPart_000_004D_01C42E03.0011C280--