netfilter フック機能を使用して、skbuffs からデータを出力しようとしています。唯一の問題は、このコードを実行すると、OS がフリーズし、強制シャットダウンを行う必要があることです。カーネルコーディングは初めてなので、誰かがこれを見て修正方法を説明してくれることを期待していました。
私が気付いたことの 1 つは、hook_func の最初に「if (!sb) return NF_ACCEPT;」という行を追加した場合です。その後、プログラムは正常に実行されます。編集: if ステートメントは常に開始されるため、skb データは出力されません。
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/netfilter_ipv4.h>
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct sk_buff *sb = *skb;
int kk=0;
for (kk=0; (kk<sb->len) && (kk < 300); kk++)
printk("%c", &sb->data+kk );
printk("\n----------------\n");
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* Fill in our hook structure */
nfho.hook = hook_func;
/* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our func first */
nf_register_hook(&nfho);
return 0;
}
/*Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
これについて本当に助けが必要です!乾杯ベン