简体   繁体   English

在用户模式下被 sock_raw 捕获后,如何在内核中丢弃数据包?

[英]How can i drop packet in kernel after catched it by sock_raw in usermode?

I have use sock_raw get all ip packet from kernel.我已经使用 sock_raw 从内核获取所有 ip 数据包。

socket(PF_PACKET, SOCK_RAW, htons(protocol);

But packet still alive in kernel, how can i drop it?但是内核中的数据包仍然存在,我该如何丢弃它?

You cannot. 你不能。 When you receive the packet on a raw socket, the kernel has created a copy and delivered it to your receiving process. 当您在原始套接字上接收到数据包时,内核会创建一个副本并将其交付给接收进程。 The packet will continue being processed in the meantime according to the usual semantics. 在此期间,将根据通常的语义继续处理该数据包。 It's likely this will have completed (ie whatever the stack would normally do with it will already be done) by the time your process receives it. 到您的进程收到该消息时,它很可能已经完成(即,堆栈通常会执行的所有操作)。

However, if the packet is not actually destined to your box (eg you're receiving it only because you have the network interface in promiscuous mode), or if there is no local process [or in-kernel component] interested in receiving it, the packet will just be discarded anyway. 但是,如果实际上没有将数据包发送到您的机器上(例如,仅由于您的网络接口处于混杂模式而收到数据包),或者没有本地进程[或内核组件]有兴趣接收数据包,该数据包将被丢弃。

If you simply wish to receive all packets that arrive on an interface without processing them, you can simply bring the interface up in promiscuous mode without giving it an IP address. 如果您只希望接收到达接口的所有数据包而不处理它们,则可以简单地以混杂模式启动接口,而无需为其提供IP地址。 Then packets will be delivered to your raw socket but will then be discarded by the stack. 然后,数据包将被传递到原始套接字,但随后将被堆栈丢弃。

Old question, but others might find this answer useful.老问题,但其他人可能会发现这个答案很有用。

Depends on the usecase, but you can actually drop ingress packets after you get them from AF_PACKET SOCK_RAW.取决于用例,但您实际上可以在从 AF_PACKET SOCK_RAW 获取入口数据包后丢弃它们。 To do that, put an ingress qdisc where we have drop action.为此,请在我们有删除操作的地方放置一个入口 qdisc。 Example:例子:

sudo tc qdisc add dev eth0 ingress
sudo tc filter add dev eth0 parent ffff: matchall action drop

Explaination: this works, because the AF_PACKET sniff the packet's copy from the per-device tap, which is a little bit earlier than the ingress qdisc in the kernel network stack's packet processing pipeline.解释:这是可行的,因为 AF_PACKET 从 per-device tap 嗅探数据包的副本,这比内核网络堆栈的数据包处理管道中的入口 qdisc 早一点。 That way you can implement a simple userspace switch.这样你就可以实现一个简单的用户空间切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM