【发布时间】:2013-10-20 22:58:13
【问题描述】:
我正在为 linux 编写以太网网络驱动程序。我想接收数据包,编辑并重新发送它们。
我知道如何在packet_interceptor 函数中编辑数据包,但是如何在此函数中丢弃传入的数据包??
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <net/sock.h>
struct packet_type my_proto;
int packet_interceptor(struct sk_buff *skb,
struct net_device *dev,
struct packet_type *pt,
struct net_device *orig_dev) {
// I dont want certain packets go to upper in net_devices for further processing.
// How can I drop sk_buff here?!
return 0;
}
static int hello_init( void ) {
printk(KERN_INFO "Hello, world!\n");
my_proto.type = htons(ETH_P_ALL);
my_proto.dev = NULL;
my_proto.func = packet_interceptor;
dev_add_pack(&my_proto);
return 0;
}
static void hello_exit(void) {
dev_remove_pack(&my_proto);
printk(KERN_INFO "Bye, world\n");
}
module_init(hello_init);
module_exit(hello_exit);
【问题讨论】:
-
你用我的解决方案测试了吗?
-
嗨@Atle,这几天我的电脑出了点问题。感谢您的回复,一旦我可以启动我的电脑,我将对其进行测试:D。但我记得在我问你之前,我测试了这个解决方案,但它没有奏效。我会再托盘它。
-
除了使用 netfilter 或 rx_handler 之外,是否有任何解决方案?
标签: c network-programming linux-device-driver kernel-module netfilter