我正在爲linux編寫以太網驅動程序。我想接收數據包,編輯並重新發送它們。 我知道如何編輯packet_interceptor
函數中的數據包,但是如何在此函數中丟棄傳入數據包?如何使用net_dev_add()API過濾和截獲Linux數據包?
#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
嗨@Atle,這幾天我的電腦有問題。感謝您的迴應,我會盡快啓動PC:D。但我記得貝弗我問你,我測試了這個解決方案,但它沒有奏效。我會再次托盤。 –