Skip to contentSkip to conversation

getting process pid with libipq/libnetfilter_queue

DISCUSSION Luca Zini 2 participants 2 messages Nov 5, 2005 netfilter

lore
Share & tools
lore

Luca Zini

author
#0 lore raw

I'm developing a sort of frontend for iptables to make a user-space interface that should filter connections in relation to the application that wants t connect.
To make this I need in user space the pid of the application that wants to connect (the application that sends or receives the packet that is filtered by -m state NEW ).

I started developing it using libipq, but i have no pid in ipq_packet_msg. I proposed my problem in a forum and a moderator proposed me a temporaney patch to ip_queue to get the pid in timestamp_usec:

--- linux-2.6.11.4/net/ipv4/netfilter/ip_queue.c.org 2005-06-13
20:56:37.000000000 +0200
+++ linux-2.6.11.4/net/ipv4/netfilter/ip_queue.c 2005-06-13
20:56:28.000000000 +0200
@@ -234,7 +234,9 @@

        pmsg->packet_id       = (unsigned long )entry;
        pmsg->data_len        = data_len;
        pmsg->timestamp_sec   = entry->skb->stamp.tv_sec;
-       pmsg->timestamp_usec  = entry->skb->stamp.tv_usec;
+       pmsg->timestamp_usec  = entry->skb->sk->sk_socket->file ?
+               entry->skb->sk->sk_socket->file->f_owner.pid : 0;
        pmsg->mark            = entry->skb->nfmark;
        pmsg->hook            = entry->info->hook;
        pmsg->hw_protocol     = entry->skb->protocol;

--- linux-2.6.11.4/net/socket.c.org 2005-06-13 20:51:53.000000000
+0200
+++ linux-2.6.11.4/net/socket.c 2005-06-13 20:39:11.000000000 +0200 @@ -404,6 +404,7 @@

                file->f_mode = FMODE_READ | FMODE_WRITE;
                file->f_flags = O_RDWR;
                file->f_pos = 0;
+               file->f_owner.pid = get_current()->pid;
                fd_install(fd, file);
        }

this patch works well if the connection was in OUTPUT chain, otherwise the system freezes itself.
I've just seen libnetfilter_queue, but, as with libipq, i did not find any solution to get the correct pid.
Have you any ideas? Can you help me?

regards
Luca Zini

#1 lore raw
Quoted 7 lines · Luca Zini

I'm developing a sort of frontend for iptables to make a user-space interface that should filter connections in relation to the application that wants t connect.
To make this I need in user space the pid of the application that wants to connect (the application that sends or receives the packet that is filtered by -m state NEW ).

Quoted 5 lines

I started developing it using libipq, but i have no pid in ipq_packet_msg. I proposed my problem in a forum and a moderator proposed me a temporaney patch to ip_queue to get the pid in timestamp_usec:

Why not simply use /proc interface :

1. you receive the packet from QUEUE|NFQUEUE and extract ipv4 header
2. you parse /proc/net/udp ou /proc/net/tcp to find the corresponding connection (search on the ipv4 header of the packet).
3. you get inode number which give you the process by lookng again in /proc

You can use netstat code or libnuclient code in the nufw project
(http://www.nufw.org) to get exemple of the code needed for step 3.

By the way, nufw is able to do this sort of filtering at the network level ;-)

BR,

Signature · 1 line
Eric Leblond <[email protected]>