Skip to contentSkip to conversation

QUEUEing packets to userspace problem

DISCUSSION Maarten Wijnants 1 participant 1 message Apr 29, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

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:

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 <config.h>
#endif

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

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

#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");

  int status;
  unsigned char buf[BUFSIZE];
  struct ipq_handle *h;
  
  // Initialize the libipq library
  h = ipq_create_handle(0, PF_INET);
  if (!h)
  	die(h);
  else
  	printf("Libipq handle created successfully!\n");

  status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
  if (status < 0)
  	die(h);
  else
  	printf("Lipipq mode set to IPQ_COPY_PACKET\n");
  
  // Continuously receive packets from kernel space, process them and reinject them into the kernel
  while(1) {   
  	// Grab the packet
  	status = ipq_read(h, buf, BUFSIZE, 0);
    	if (status < 0)
                	die(h);
    	else
    		printf("Libipq packet read successful\n");
    	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 = ipq_get_packet(buf);
		
    			// Print the payload of the packet
    			if ( m->data_len > 0 ) {
    				printf("Printing content of packet:\n");
                                                                /* To be included */
                                  	}
		
    			// Reinject the packet into kernel space
    			status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);
    			if (status < 0)
    				die(h);
			
    			break;
    		}
    		default: {
    			fprintf(stderr, "Unknown message type!\n");
    			break;
    		}
    	}

}

  printf("Closing down ...\n");

// 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