Netfilter module help
Hi all,
I am writing a kernel module that hooks into netfilter.
netfilter_ops.hook = main_hook; netfilter_ops.pf = PF_INET; netfilter_ops.hooknum = NF_IP_PRE_ROUTING; netfilter_ops.priority = NF_IP_PRI_FIRST;
nf_register_hook(&netfilter_ops);
In the main_hook function, I needs to put the process to sleep and will be woken up by another process because the decision to Accept or Deny a packet depends on a process running in the user space. However, all sleeping mechanism I tried cause the kernel hang. I tried msleep, wait_event_interrupt,wait_event_interrupt_timeout,... (of course I have a process to wake up the main_hook process). I tested these sleeping machenism in different modules and they worked fine. My question is can we make the process sleep in the main_hook function without causing the kernel hang and how to do it?
Thank you very much.
Tung.
Quoted 6 lines · tung tran
Hi all,
I am writing a kernel module that hooks into netfilter.netfilter_ops.hook = main_hook; netfilter_ops.pf = PF_INET; netfilter_ops.hooknum = NF_IP_PRE_ROUTING; netfilter_ops.priority = NF_IP_PRI_FIRST;
Quoted 6 lines
nf_register_hook(&netfilter_ops);In the
main_hookfunction, I needs to put the process to sleep and will be woken up by another process because the decision to Accept or Deny a packet depends on a process running in the user space. However, all sleeping mechanism I tried cause the kernel hang.
You cannot sleep in an interrupt handler.
Hi Jan,
Thanks for your quick reply. Could you please give me some advices to accomplish the task? I need to pass the packet from kernel to a user space process to decide if the packet is accepted or denied. Best,
Tung.
Quoted 18 lines · Jan Engelhardt
Quoted 6 lines · tung tran
Hi all,
I am writing a kernel module that hooks into netfilter.netfilter_ops.hook = main_hook; netfilter_ops.pf = PF_INET; netfilter_ops.hooknum = NF_IP_PRE_ROUTING; netfilter_ops.priority = NF_IP_PRI_FIRST;Quoted 6 lines
nf_register_hook(&netfilter_ops);In the
main_hookfunction, I needs to put the process to sleep and will be woken up by another process because the decision to Accept or Deny a packet depends on a process running in the user space. However, all sleeping mechanism I tried cause the kernel hang.You cannot sleep in an interrupt handler.
Quoted 4 lines · tung tran
Hi Jan,
Thanks for your quick reply. Could you please give me some advices to accomplish the task? I need to pass the packet from kernel to a user space process to decide if the packet is accepted or denied.
For that the existing NF_QUEUE mechanism should be used. If a hook returns NF_QUEUE then the kernel suspends the packet and submits it to a queue handler for querying the userspace. When the userspace responds the packet is either rejected or allowed to continue processing at the next hook handler.
The normal ip_queue handler uses netlink to talk to userspace.
There can only be one queue handler in the system.
iptables has support for this via the QUEUE psuedo-target.
Regards
Henrik
Hi Henrik,
Thank you very much. I really appreciate your help. Tung.
Quoted 21 lines · Henrik Nordstrom
Quoted 4 lines · tung tran
Hi Jan,
Thanks for your quick reply. Could you please give me some advices to accomplish the task? I need to pass the packet from kernel to a user space process to decide if the packet is accepted or denied.For that the existing
NF_QUEUEmechanism should be used. If a hook returnsNF_QUEUEthen the kernel suspends the packet and submits it to a queue handler for querying the userspace. When the userspace responds the packet is either rejected or allowed to continue processing at the next hook handler.The normal
ip_queuehandler uses netlink to talk to userspace.There can only be one queue handler in the system.
iptables has support for this via the QUEUE psuedo-target.
Regards
Henrik
Hi again,
I have another question, if I use QUEUE, is there any way that I can pass a packet back to a chain after processing it? For example, I use libipq to receive a packet from kernel (through the special QUEUE chain) and process the packet. After processing the packet, I do not want to ACCEPT or REJECT the packet, but pass it back to a different chain, for example INPUT. Can I do this?
Thanks,
Tung.
Quoted 28 lines · tung tran
Hi Henrik,
Thank you very much. I really appreciate your help. Tung.Quoted 21 lines · Henrik Nordstrom
Quoted 4 lines · tung tran
Hi Jan,
Thanks for your quick reply. Could you please give me some advices to accomplish the task? I need to pass the packet from kernel to a user space process to decide if the packet is accepted or denied.For that the existing
NF_QUEUEmechanism should be used. If a hook returnsNF_QUEUEthen the kernel suspends the packet and submits it to a queue handler for querying the userspace. When the userspace responds the packet is either rejected or allowed to continue processing at the next hook handler.The normal
ip_queuehandler uses netlink to talk to userspace.There can only be one queue handler in the system.
iptables has support for this via the QUEUE psuedo-target.
Regards
Henrik
Quoted 6 lines · tung tran
I have another question, if I use QUEUE, is there any way that I can pass a packet back to a chain after processing it? For example, I use libipq to receive a packet from kernel (through the special QUEUE chain) and process the packet. After processing the packet, I do not want to ACCEPT or REJECT the packet, but pass it back to a different chain, for example INPUT. Can I do this?
The verdict from the QUEUE handler needs to be a final ACCEPT/DROP verdict from that chain.
But on ACCEPT the packet continues as if the iptables rule had been an ACCEPT rule. The next priority handler will be called in the netfilter hook, and then the packet will continue in the ip stack as usual..
So you can use QUEUE in PREROUTE, return ACCEPT and then see it in INPUT again as that a different hook.
You can also use QUEUE in the raw table and then see the packet in filter/mangle/nat as these are different even if you are using the same hook.
Regards
Henrik
Hi Henrik,
Really really appreciate your help.
Thanks,
Tung.
Quoted 18 lines · Henrik Nordstrom
Quoted 6 lines · tung tran
I have another question, if I use QUEUE, is there any way that I can pass a packet back to a chain after processing it? For example, I use libipq to receive a packet from kernel (through the special QUEUE chain) and process the packet. After processing the packet, I do not want to ACCEPT or REJECT the packet, but pass it back to a different chain, for example INPUT. Can I do this?
The verdict from the QUEUE handler needs to be a final ACCEPT/DROP verdict from that chain.
But on ACCEPT the packet continues as if the iptables rule had been an ACCEPT rule. The next priority handler will be called in the netfilter hook, and then the packet will continue in the ip stack as usual..
So you can use QUEUE in PREROUTE, return ACCEPT and then see it in INPUT again as that a different hook.
Quoted 7 lines
You can also use QUEUE in the raw table and then see the packet in filter/mangle/nat as these are different even if you are using the same hook.
Regards
Henrik
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html